Skip to main content

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