gstreamer_rtsp_server/
rtsp_session_pool.rs1use 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 #[doc(alias = "gst_rtsp_session_pool_create_watch")]
45 fn create_watch<F>(&self, name: Option<&str>, priority: Priority, func: F) -> glib::Source
46 where
47 F: FnMut(&RTSPSessionPool) -> ControlFlow + Send + 'static,
48 {
49 skip_assert_initialized!();
50 unsafe {
51 let source = ffi::gst_rtsp_session_pool_create_watch(self.as_ref().to_glib_none().0);
52 glib::ffi::g_source_set_callback(
53 source,
54 Some(transmute::<
55 *mut (),
56 unsafe extern "C" fn(glib::ffi::gpointer) -> i32,
57 >(trampoline_watch::<F> as *mut ())),
58 into_raw_watch(func),
59 Some(destroy_closure_watch::<F>),
60 );
61 glib::ffi::g_source_set_priority(source, priority.into_glib());
62
63 if let Some(name) = name {
64 glib::ffi::g_source_set_name(source, name.to_glib_none().0);
65 }
66
67 from_glib_full(source)
68 }
69 }
70}
71
72impl<O: IsA<RTSPSessionPool>> RTSPSessionPoolExtManual for O {}