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

66 lines
2.1 KiB
Rust
Raw Normal View History

use glib;
use glib::object::IsA;
use glib::source::{Continue, Priority};
2018-04-01 08:30:03 +00:00
use glib::translate::*;
2019-03-19 07:58:20 +00:00
use glib_sys;
use glib_sys::{gboolean, gpointer};
use gst_rtsp_server_sys;
2018-04-01 08:30:03 +00:00
use std::cell::RefCell;
use std::mem::transmute;
2018-04-25 08:10:06 +00:00
use RTSPSessionPool;
unsafe extern "C" fn trampoline_watch<F: FnMut(&RTSPSessionPool) -> Continue + Send + 'static>(
2019-03-19 07:58:20 +00:00
pool: *mut gst_rtsp_server_sys::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)).to_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 {
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 {
2019-03-19 07:58:20 +00:00
let source = gst_rtsp_server_sys::gst_rtsp_session_pool_create_watch(
self.as_ref().to_glib_none().0,
);
glib_sys::g_source_set_callback(
source,
Some(transmute(trampoline_watch::<F> as usize)),
into_raw_watch(func),
Some(destroy_closure_watch::<F>),
);
2019-03-19 07:58:20 +00:00
glib_sys::g_source_set_priority(source, priority.to_glib());
if let Some(name) = name {
2019-03-19 07:58:20 +00:00
glib_sys::g_source_set_name(source, name.to_glib_none().0);
}
from_glib_full(source)
}
}
}