gstreamer_net/
ptp_clock.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::num::NonZeroU64;
4
5use glib::translate::*;
6
7use crate::{ffi, PtpClock};
8
9impl PtpClock {
10    // rustdoc-stripper-ignore-next
11    /// Initialize GStreamer PTP clock support
12    ///
13    /// This is automatically called once the first PTP clock instance is created.
14    #[doc(alias = "gst_ptp_init")]
15    pub fn init(clock_id: Option<u64>, interfaces: &[&str]) -> Result<(), glib::BoolError> {
16        skip_assert_initialized!();
17        unsafe {
18            let res: bool = from_glib(ffi::gst_ptp_init(
19                clock_id.unwrap_or(u64::MAX),
20                interfaces.to_glib_none().0,
21            ));
22
23            if res {
24                Ok(())
25            } else {
26                Err(glib::bool_error!("Failed to initialize PTP subsystem"))
27            }
28        }
29    }
30
31    #[cfg(feature = "v1_24")]
32    #[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
33    // rustdoc-stripper-ignore-next
34    /// Initialize GStreamer PTP clock support
35    ///
36    /// This is automatically called once the first PTP clock instance is created.
37    #[doc(alias = "gst_ptp_init_full")]
38    pub fn init_full(s: &gst::StructureRef) -> Result<(), glib::BoolError> {
39        skip_assert_initialized!();
40        unsafe {
41            let res: bool = from_glib(ffi::gst_ptp_init_full(s.as_ptr()));
42
43            if res {
44                Ok(())
45            } else {
46                Err(glib::bool_error!("Failed to initialize PTP subsystem"))
47            }
48        }
49    }
50
51    // rustdoc-stripper-ignore-next
52    /// Deinitialize GStreamer PTP clock support
53    ///
54    /// Any PTP clocks that are still running will not receive any updates anymore.
55    #[doc(alias = "gst_ptp_deinit")]
56    pub fn deinit() {
57        unsafe {
58            ffi::gst_ptp_deinit();
59        }
60    }
61
62    // rustdoc-stripper-ignore-next
63    /// Check if the GStreamer PTP clock support is initialized
64    #[doc(alias = "gst_ptp_is_initialized")]
65    pub fn is_initialized() -> bool {
66        unsafe { from_glib(ffi::gst_ptp_is_initialized()) }
67    }
68
69    // rustdoc-stripper-ignore-next
70    /// Check if GStreamer PTP clocks are supported
71    #[doc(alias = "gst_ptp_is_supported")]
72    pub fn is_supported() -> bool {
73        unsafe { from_glib(ffi::gst_ptp_is_supported()) }
74    }
75
76    // rustdoc-stripper-ignore-next
77    /// Add a PTP clock statistics callback
78    #[doc(alias = "gst_ptp_statistics_callback_add")]
79    pub fn add_statistics_callback<
80        F: Fn(u8, &gst::StructureRef) -> glib::ControlFlow + 'static + Send + Sync,
81    >(
82        func: F,
83    ) -> PtpStatisticsCallback {
84        unsafe {
85            unsafe extern "C" fn trampoline<
86                F: Fn(u8, &gst::StructureRef) -> glib::ControlFlow + 'static + Send + Sync,
87            >(
88                domain: u8,
89                stats: *const gst::ffi::GstStructure,
90                user_data: glib::ffi::gpointer,
91            ) -> glib::ffi::gboolean {
92                let callback = &*(user_data as *const F);
93                callback(domain, gst::StructureRef::from_glib_borrow(stats)).into_glib()
94            }
95
96            unsafe extern "C" fn destroy<
97                F: Fn(u8, &gst::StructureRef) -> glib::ControlFlow + 'static + Send + Sync,
98            >(
99                user_data: glib::ffi::gpointer,
100            ) {
101                let _ = Box::from_raw(user_data as *mut F);
102            }
103
104            let user_data = Box::new(func);
105            let id = ffi::gst_ptp_statistics_callback_add(
106                Some(trampoline::<F>),
107                Box::into_raw(user_data) as glib::ffi::gpointer,
108                Some(destroy::<F>),
109            );
110            debug_assert_ne!(id, 0);
111
112            PtpStatisticsCallback(NonZeroU64::new_unchecked(id as _))
113        }
114    }
115}
116
117#[derive(Debug)]
118pub struct PtpStatisticsCallback(NonZeroU64);
119
120impl PtpStatisticsCallback {
121    #[doc(alias = "gst_ptp_statistics_callback_remove")]
122    pub fn remove(self) {
123        unsafe {
124            ffi::gst_ptp_statistics_callback_remove(self.0.get() as _);
125        }
126    }
127}