gstreamer_rtsp_server/
rtsp_session_pool.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::mem::transmute;
4
5use glib::{
6    ffi::{gboolean, gpointer},
7    prelude::*,
8    source::Priority,
9    translate::*,
10    ControlFlow,
11};
12
13use crate::{ffi, RTSPSessionPool};
14
15unsafe extern "C" fn trampoline_watch<
16    F: FnMut(&RTSPSessionPool) -> ControlFlow + Send + 'static,
17>(
18    pool: *mut ffi::GstRTSPSessionPool,
19    func: gpointer,
20) -> gboolean {
21    let func: &mut F = &mut *(func as *mut F);
22    func(&from_glib_borrow(pool)).into_glib()
23}
24
25unsafe extern "C" fn destroy_closure_watch<
26    F: FnMut(&RTSPSessionPool) -> ControlFlow + Send + 'static,
27>(
28    ptr: gpointer,
29) {
30    let _ = Box::<F>::from_raw(ptr as *mut _);
31}
32
33fn into_raw_watch<F: FnMut(&RTSPSessionPool) -> ControlFlow + Send + 'static>(func: F) -> gpointer {
34    #[allow(clippy::type_complexity)]
35    let func: Box<F> = Box::new(func);
36    Box::into_raw(func) as gpointer
37}
38
39mod sealed {
40    pub trait Sealed {}
41    impl<T: super::IsA<super::RTSPSessionPool>> Sealed for T {}
42}
43
44pub trait RTSPSessionPoolExtManual: sealed::Sealed + IsA<RTSPSessionPool> + 'static {
45    /// Create a [`glib::Source`][crate::glib::Source] that will be dispatched when the session should be cleaned
46    /// up.
47    ///
48    /// # Returns
49    ///
50    /// a [`glib::Source`][crate::glib::Source]
51    #[doc(alias = "gst_rtsp_session_pool_create_watch")]
52    fn create_watch<F>(&self, name: Option<&str>, priority: Priority, func: F) -> glib::Source
53    where
54        F: FnMut(&RTSPSessionPool) -> ControlFlow + Send + 'static,
55    {
56        skip_assert_initialized!();
57        unsafe {
58            let source = ffi::gst_rtsp_session_pool_create_watch(self.as_ref().to_glib_none().0);
59            glib::ffi::g_source_set_callback(
60                source,
61                Some(transmute::<
62                    *mut (),
63                    unsafe extern "C" fn(glib::ffi::gpointer) -> i32,
64                >(trampoline_watch::<F> as *mut ())),
65                into_raw_watch(func),
66                Some(destroy_closure_watch::<F>),
67            );
68            glib::ffi::g_source_set_priority(source, priority.into_glib());
69
70            if let Some(name) = name {
71                glib::ffi::g_source_set_name(source, name.to_glib_none().0);
72            }
73
74            from_glib_full(source)
75        }
76    }
77}
78
79impl<O: IsA<RTSPSessionPool>> RTSPSessionPoolExtManual for O {}