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
39pub trait RTSPSessionPoolExtManual: IsA<RTSPSessionPool> + 'static {
40    /// Create a [`glib::Source`][crate::glib::Source] that will be dispatched when the session should be cleaned
41    /// up.
42    ///
43    /// # Returns
44    ///
45    /// a [`glib::Source`][crate::glib::Source]
46    #[doc(alias = "gst_rtsp_session_pool_create_watch")]
47    fn create_watch<F>(&self, name: Option<&str>, priority: Priority, func: F) -> glib::Source
48    where
49        F: FnMut(&RTSPSessionPool) -> ControlFlow + Send + 'static,
50    {
51        skip_assert_initialized!();
52        unsafe {
53            let source = ffi::gst_rtsp_session_pool_create_watch(self.as_ref().to_glib_none().0);
54            glib::ffi::g_source_set_callback(
55                source,
56                Some(transmute::<
57                    *mut (),
58                    unsafe extern "C" fn(glib::ffi::gpointer) -> i32,
59                >(trampoline_watch::<F> as *mut ())),
60                into_raw_watch(func),
61                Some(destroy_closure_watch::<F>),
62            );
63            glib::ffi::g_source_set_priority(source, priority.into_glib());
64
65            if let Some(name) = name {
66                glib::ffi::g_source_set_name(source, name.to_glib_none().0);
67            }
68
69            from_glib_full(source)
70        }
71    }
72}
73
74impl<O: IsA<RTSPSessionPool>> RTSPSessionPoolExtManual for O {}