2017-08-01 14:52:11 +00:00
|
|
|
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use ffi;
|
2018-04-01 08:30:03 +00:00
|
|
|
use glib::translate::*;
|
2017-08-01 14:52:11 +00:00
|
|
|
use glib_ffi::gpointer;
|
2018-04-01 08:30:03 +00:00
|
|
|
use gst;
|
|
|
|
use gst_ffi;
|
2018-04-25 08:08:44 +00:00
|
|
|
use std::cell::RefCell;
|
2018-04-25 08:10:06 +00:00
|
|
|
use std::ptr;
|
|
|
|
use AppSink;
|
2017-08-01 14:52:11 +00:00
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
|
2017-08-01 14:52:11 +00:00
|
|
|
pub struct AppSinkCallbacks {
|
2018-04-25 08:08:44 +00:00
|
|
|
eos: Option<RefCell<Box<FnMut(&AppSink) + Send + 'static>>>,
|
|
|
|
new_preroll: Option<RefCell<Box<FnMut(&AppSink) -> gst::FlowReturn + Send + 'static>>>,
|
|
|
|
new_sample: Option<RefCell<Box<FnMut(&AppSink) -> gst::FlowReturn + Send + 'static>>>,
|
2017-08-01 14:52:11 +00:00
|
|
|
callbacks: ffi::GstAppSinkCallbacks,
|
|
|
|
}
|
|
|
|
|
2017-12-16 12:14:29 +00:00
|
|
|
unsafe impl Send for AppSinkCallbacks {}
|
|
|
|
unsafe impl Sync for AppSinkCallbacks {}
|
|
|
|
|
2017-12-16 09:37:00 +00:00
|
|
|
impl AppSinkCallbacks {
|
|
|
|
pub fn new() -> AppSinkCallbacksBuilder {
|
2017-08-30 09:48:01 +00:00
|
|
|
skip_assert_initialized!();
|
2017-12-10 13:18:54 +00:00
|
|
|
AppSinkCallbacksBuilder {
|
|
|
|
eos: None,
|
|
|
|
new_preroll: None,
|
|
|
|
new_sample: None,
|
|
|
|
}
|
|
|
|
}
|
2017-12-16 09:37:00 +00:00
|
|
|
}
|
2017-12-10 13:18:54 +00:00
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
|
2017-12-16 09:37:00 +00:00
|
|
|
pub struct AppSinkCallbacksBuilder {
|
2018-04-25 08:08:44 +00:00
|
|
|
eos: Option<RefCell<Box<FnMut(&AppSink) + Send + 'static>>>,
|
|
|
|
new_preroll: Option<RefCell<Box<FnMut(&AppSink) -> gst::FlowReturn + Send + 'static>>>,
|
|
|
|
new_sample: Option<RefCell<Box<FnMut(&AppSink) -> gst::FlowReturn + Send + 'static>>>,
|
2017-12-16 09:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppSinkCallbacksBuilder {
|
2017-12-10 13:18:54 +00:00
|
|
|
pub fn eos<F: Fn(&AppSink) + Send + Sync + 'static>(self, eos: F) -> Self {
|
|
|
|
Self {
|
2018-04-25 08:08:44 +00:00
|
|
|
eos: Some(RefCell::new(Box::new(eos))),
|
2017-12-10 13:18:54 +00:00
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_preroll<F: Fn(&AppSink) -> gst::FlowReturn + Send + Sync + 'static>(
|
|
|
|
self,
|
|
|
|
new_preroll: F,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
2018-04-25 08:08:44 +00:00
|
|
|
new_preroll: Some(RefCell::new(Box::new(new_preroll))),
|
2017-12-10 13:18:54 +00:00
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_sample<F: Fn(&AppSink) -> gst::FlowReturn + Send + Sync + 'static>(
|
|
|
|
self,
|
|
|
|
new_sample: F,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
2018-04-25 08:08:44 +00:00
|
|
|
new_sample: Some(RefCell::new(Box::new(new_sample))),
|
2017-12-10 13:18:54 +00:00
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> AppSinkCallbacks {
|
|
|
|
let have_eos = self.eos.is_some();
|
|
|
|
let have_new_preroll = self.new_preroll.is_some();
|
|
|
|
let have_new_sample = self.new_sample.is_some();
|
2017-08-30 09:48:01 +00:00
|
|
|
|
2017-08-01 14:52:11 +00:00
|
|
|
AppSinkCallbacks {
|
2017-12-10 13:18:54 +00:00
|
|
|
eos: self.eos,
|
|
|
|
new_preroll: self.new_preroll,
|
|
|
|
new_sample: self.new_sample,
|
2017-08-01 14:52:11 +00:00
|
|
|
callbacks: ffi::GstAppSinkCallbacks {
|
2017-12-10 13:18:54 +00:00
|
|
|
eos: if have_eos { Some(trampoline_eos) } else { None },
|
|
|
|
new_preroll: if have_new_preroll {
|
|
|
|
Some(trampoline_new_preroll)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
new_sample: if have_new_sample {
|
|
|
|
Some(trampoline_new_sample)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2017-08-01 14:52:11 +00:00
|
|
|
_gst_reserved: [
|
|
|
|
ptr::null_mut(),
|
|
|
|
ptr::null_mut(),
|
|
|
|
ptr::null_mut(),
|
|
|
|
ptr::null_mut(),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn trampoline_eos(appsink: *mut ffi::GstAppSink, callbacks: gpointer) {
|
2017-08-02 17:09:00 +00:00
|
|
|
let callbacks = &*(callbacks as *const AppSinkCallbacks);
|
2017-08-01 14:52:11 +00:00
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
if let Some(ref eos) = callbacks.eos {
|
|
|
|
(&mut *eos.borrow_mut())(&from_glib_borrow(appsink))
|
|
|
|
}
|
2017-08-01 14:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn trampoline_new_preroll(
|
|
|
|
appsink: *mut ffi::GstAppSink,
|
|
|
|
callbacks: gpointer,
|
|
|
|
) -> gst_ffi::GstFlowReturn {
|
2017-08-02 17:09:00 +00:00
|
|
|
let callbacks = &*(callbacks as *const AppSinkCallbacks);
|
2017-08-01 14:52:11 +00:00
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
let ret = if let Some(ref new_preroll) = callbacks.new_preroll {
|
|
|
|
(&mut *new_preroll.borrow_mut())(&from_glib_borrow(appsink))
|
|
|
|
} else {
|
|
|
|
gst::FlowReturn::Error
|
|
|
|
};
|
|
|
|
|
|
|
|
ret.to_glib()
|
2017-08-01 14:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn trampoline_new_sample(
|
|
|
|
appsink: *mut ffi::GstAppSink,
|
|
|
|
callbacks: gpointer,
|
|
|
|
) -> gst_ffi::GstFlowReturn {
|
2017-08-02 17:09:00 +00:00
|
|
|
let callbacks = &*(callbacks as *const AppSinkCallbacks);
|
2017-08-01 14:52:11 +00:00
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
let ret = if let Some(ref new_sample) = callbacks.new_sample {
|
|
|
|
(&mut *new_sample.borrow_mut())(&from_glib_borrow(appsink))
|
|
|
|
} else {
|
|
|
|
gst::FlowReturn::Error
|
|
|
|
};
|
|
|
|
|
|
|
|
ret.to_glib()
|
2017-08-01 14:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn destroy_callbacks(ptr: gpointer) {
|
2017-11-11 15:05:08 +00:00
|
|
|
Box::<AppSinkCallbacks>::from_raw(ptr as *mut _);
|
2017-08-01 14:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppSink {
|
|
|
|
pub fn set_callbacks(&self, callbacks: AppSinkCallbacks) {
|
|
|
|
unsafe {
|
|
|
|
ffi::gst_app_sink_set_callbacks(
|
|
|
|
self.to_glib_none().0,
|
|
|
|
mut_override(&callbacks.callbacks),
|
|
|
|
Box::into_raw(Box::new(callbacks)) as *mut _,
|
|
|
|
Some(destroy_callbacks),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|