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