gstreamer-rs/gstreamer-rtsp-server/src/rtsp_session_pool.rs

67 lines
2.2 KiB
Rust
Raw Normal View History

2020-12-15 10:53:31 +00:00
// Take a look at the license at the top of the repository in the LICENSE file.
use crate::RTSPSessionPool;
use glib::ffi::{gboolean, gpointer};
2021-04-26 12:15:53 +00:00
use glib::prelude::*;
use glib::source::{Continue, Priority};
2018-04-01 08:30:03 +00:00
use glib::translate::*;
use std::cell::RefCell;
use std::mem::transmute;
unsafe extern "C" fn trampoline_watch<F: FnMut(&RTSPSessionPool) -> Continue + Send + 'static>(
pool: *mut ffi::GstRTSPSessionPool,
func: gpointer,
) -> gboolean {
2019-02-21 17:30:36 +00:00
let func: &RefCell<F> = &*(func as *const RefCell<F>);
(&mut *func.borrow_mut())(&from_glib_borrow(pool)).into_glib()
}
unsafe extern "C" fn destroy_closure_watch<
F: FnMut(&RTSPSessionPool) -> Continue + Send + 'static,
>(
ptr: gpointer,
) {
Box::<RefCell<F>>::from_raw(ptr as *mut _);
}
fn into_raw_watch<F: FnMut(&RTSPSessionPool) -> Continue + Send + 'static>(func: F) -> gpointer {
#[allow(clippy::type_complexity)]
let func: Box<RefCell<F>> = Box::new(RefCell::new(func));
Box::into_raw(func) as gpointer
}
pub trait RTSPSessionPoolExtManual: 'static {
#[doc(alias = "gst_rtsp_session_pool_create_watch")]
fn create_watch<F>(&self, name: Option<&str>, priority: Priority, func: F) -> glib::Source
where
F: FnMut(&RTSPSessionPool) -> Continue + Send + 'static;
}
impl<O: IsA<RTSPSessionPool>> RTSPSessionPoolExtManual for O {
fn create_watch<F>(&self, name: Option<&str>, priority: Priority, func: F) -> glib::Source
where
F: FnMut(&RTSPSessionPool) -> Continue + Send + 'static,
{
skip_assert_initialized!();
unsafe {
let source = ffi::gst_rtsp_session_pool_create_watch(self.as_ref().to_glib_none().0);
glib::ffi::g_source_set_callback(
source,
Some(transmute::<
_,
unsafe extern "C" fn(glib::ffi::gpointer) -> i32,
>(trampoline_watch::<F> as *const ())),
into_raw_watch(func),
Some(destroy_closure_watch::<F>),
);
glib::ffi::g_source_set_priority(source, priority.into_glib());
if let Some(name) = name {
glib::ffi::g_source_set_name(source, name.to_glib_none().0);
}
from_glib_full(source)
}
}
}