Skip to main content

grafton_ndi/advanced/
routing.rs

1//! Metadata a router replays to each new connection.
2
3use crate::{routing::Router, sys::advanced::native, MetadataSource, Result};
4
5impl Router {
6    /// Send `metadata` to every connected receiver now, and to each receiver
7    /// that connects later.
8    ///
9    /// The router keeps its own copy of owned or captured text; the input
10    /// may be released immediately afterward, even for future connections.
11    ///
12    /// # Errors
13    ///
14    /// [`Error::UnsupportedCapability`](crate::Error::UnsupportedCapability)
15    /// without an Advanced runtime (or in a build without the Advanced SDK).
16    #[doc(alias = "NDIlib_routing_add_connection_metadata")]
17    pub fn add_connection_metadata<S: MetadataSource + ?Sized>(
18        &mut self,
19        metadata: &S,
20    ) -> Result<()> {
21        let (table, router, registrations) = self.connection_metadata_parts()?;
22        native!(RouterConnectionMetadata, table, uses(metadata, router, registrations) => {
23            registrations.add(metadata, |metadata| {
24                // SAFETY: the router owns this stable descriptor and its text
25                // until native clear returns or after native destruction.
26                unsafe { table.NDIlib_routing_add_connection_metadata(router, metadata) };
27            });
28            Ok(())
29        })
30    }
31
32    /// Forget the metadata added with
33    /// [`add_connection_metadata`](Self::add_connection_metadata).
34    ///
35    /// # Errors
36    ///
37    /// As for [`add_connection_metadata`](Self::add_connection_metadata).
38    #[doc(alias = "NDIlib_routing_clear_connection_metadata")]
39    pub fn clear_connection_metadata(&mut self) -> Result<()> {
40        let (table, router, registrations) = self.connection_metadata_parts()?;
41        native!(RouterConnectionMetadata, table, uses(router, registrations) => {
42            registrations.clear(|| {
43                // SAFETY: the router and its registrations remain live until
44                // this native clear returns.
45                unsafe { table.NDIlib_routing_clear_connection_metadata(router) };
46            });
47            Ok(())
48        })
49    }
50}