2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2017-08-01 14:52:11 +00:00
|
|
|
|
2020-11-21 18:06:11 +00:00
|
|
|
use crate::AppSink;
|
|
|
|
use glib::ffi::gpointer;
|
2018-04-01 08:30:03 +00:00
|
|
|
use glib::translate::*;
|
2018-04-25 08:08:44 +00:00
|
|
|
use std::cell::RefCell;
|
2020-03-05 08:31:48 +00:00
|
|
|
use std::panic;
|
2018-04-25 08:10:06 +00:00
|
|
|
use std::ptr;
|
2020-03-05 08:31:48 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2017-08-01 14:52:11 +00:00
|
|
|
|
2020-01-29 12:40:17 +00:00
|
|
|
#[cfg(any(feature = "v1_10"))]
|
|
|
|
use {
|
|
|
|
futures_core::Stream,
|
2020-05-13 19:13:11 +00:00
|
|
|
glib::prelude::*,
|
2020-01-29 12:40:17 +00:00
|
|
|
std::{
|
|
|
|
pin::Pin,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
task::{Context, Poll, Waker},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2017-08-01 14:52:11 +00:00
|
|
|
pub struct AppSinkCallbacks {
|
2019-06-06 06:09:34 +00:00
|
|
|
eos: Option<RefCell<Box<dyn FnMut(&AppSink) + Send + 'static>>>,
|
2019-01-08 16:13:37 +00:00
|
|
|
new_preroll: Option<
|
2019-06-06 06:09:34 +00:00
|
|
|
RefCell<
|
|
|
|
Box<dyn FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static>,
|
|
|
|
>,
|
2019-01-08 16:13:37 +00:00
|
|
|
>,
|
|
|
|
new_sample: Option<
|
2019-06-06 06:09:34 +00:00
|
|
|
RefCell<
|
|
|
|
Box<dyn FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static>,
|
|
|
|
>,
|
2019-01-08 16:13:37 +00:00
|
|
|
>,
|
2020-03-05 08:31:48 +00:00
|
|
|
panicked: AtomicBool,
|
2020-11-21 18:06:11 +00:00
|
|
|
callbacks: ffi::GstAppSinkCallbacks,
|
2017-08-01 14:52:11 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-06-25 16:22:25 +00:00
|
|
|
pub fn builder() -> 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
|
|
|
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2017-12-16 09:37:00 +00:00
|
|
|
pub struct AppSinkCallbacksBuilder {
|
2019-06-06 06:09:34 +00:00
|
|
|
eos: Option<RefCell<Box<dyn FnMut(&AppSink) + Send + 'static>>>,
|
2019-01-08 16:13:37 +00:00
|
|
|
new_preroll: Option<
|
2019-06-06 06:09:34 +00:00
|
|
|
RefCell<
|
|
|
|
Box<dyn FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static>,
|
|
|
|
>,
|
2019-01-08 16:13:37 +00:00
|
|
|
>,
|
|
|
|
new_sample: Option<
|
2019-06-06 06:09:34 +00:00
|
|
|
RefCell<
|
|
|
|
Box<dyn FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static>,
|
|
|
|
>,
|
2019-01-08 16:13:37 +00:00
|
|
|
>,
|
2017-12-16 09:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppSinkCallbacksBuilder {
|
2019-10-26 09:32:02 +00:00
|
|
|
pub fn eos<F: FnMut(&AppSink) + Send + 'static>(self, eos: F) -> Self {
|
2017-12-10 13:18:54 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pub fn new_preroll<
|
2019-10-26 09:32:02 +00:00
|
|
|
F: FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static,
|
2019-01-08 16:13:37 +00:00
|
|
|
>(
|
2017-12-10 13:18:54 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pub fn new_sample<
|
2019-10-26 09:32:02 +00:00
|
|
|
F: FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static,
|
2019-01-08 16:13:37 +00:00
|
|
|
>(
|
2017-12-10 13:18:54 +00:00
|
|
|
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,
|
2020-03-05 08:31:48 +00:00
|
|
|
panicked: AtomicBool::new(false),
|
2020-11-21 18:06: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(),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 08:31:48 +00:00
|
|
|
fn post_panic_error_message(element: &AppSink, err: &dyn std::any::Any) {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2020-03-05 08:31:48 +00:00
|
|
|
if let Some(cause) = err.downcast_ref::<&str>() {
|
2020-12-20 15:09:22 +00:00
|
|
|
gst::element_error!(&element, gst::LibraryError::Failed, ["Panicked: {}", cause]);
|
2020-03-05 08:31:48 +00:00
|
|
|
} else if let Some(cause) = err.downcast_ref::<String>() {
|
2020-12-20 15:09:22 +00:00
|
|
|
gst::element_error!(&element, gst::LibraryError::Failed, ["Panicked: {}", cause]);
|
2020-03-05 08:31:48 +00:00
|
|
|
} else {
|
2020-12-20 15:09:22 +00:00
|
|
|
gst::element_error!(&element, gst::LibraryError::Failed, ["Panicked"]);
|
2020-03-05 08:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:06:11 +00:00
|
|
|
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);
|
2020-04-05 14:52:56 +00:00
|
|
|
let element: Borrowed<AppSink> = from_glib_borrow(appsink);
|
2020-03-05 08:31:48 +00:00
|
|
|
|
|
|
|
if callbacks.panicked.load(Ordering::Relaxed) {
|
2020-04-05 14:52:56 +00:00
|
|
|
let element: Borrowed<AppSink> = from_glib_borrow(appsink);
|
2020-12-20 15:09:22 +00:00
|
|
|
gst::element_error!(&element, gst::LibraryError::Failed, ["Panicked"]);
|
2020-03-05 08:31:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-01 14:52:11 +00:00
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
if let Some(ref eos) = callbacks.eos {
|
2020-03-05 08:31:48 +00:00
|
|
|
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
|
|
|
(&mut *eos.borrow_mut())(&element)
|
|
|
|
}));
|
|
|
|
match result {
|
|
|
|
Ok(result) => result,
|
|
|
|
Err(err) => {
|
|
|
|
callbacks.panicked.store(true, Ordering::Relaxed);
|
|
|
|
post_panic_error_message(&element, &err);
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 07:21:06 +00:00
|
|
|
}
|
2017-08-01 14:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn trampoline_new_preroll(
|
2020-11-21 18:06:11 +00:00
|
|
|
appsink: *mut ffi::GstAppSink,
|
2017-08-01 14:52:11 +00:00
|
|
|
callbacks: gpointer,
|
2020-11-21 18:06:11 +00:00
|
|
|
) -> gst::ffi::GstFlowReturn {
|
2017-08-02 17:09:00 +00:00
|
|
|
let callbacks = &*(callbacks as *const AppSinkCallbacks);
|
2020-04-05 14:52:56 +00:00
|
|
|
let element: Borrowed<AppSink> = from_glib_borrow(appsink);
|
2020-03-05 08:31:48 +00:00
|
|
|
|
|
|
|
if callbacks.panicked.load(Ordering::Relaxed) {
|
2020-04-05 14:52:56 +00:00
|
|
|
let element: Borrowed<AppSink> = from_glib_borrow(appsink);
|
2020-12-20 15:09:22 +00:00
|
|
|
gst::element_error!(&element, gst::LibraryError::Failed, ["Panicked"]);
|
2021-04-27 15:15:46 +00:00
|
|
|
return gst::FlowReturn::Error.into_glib();
|
2020-03-05 08:31:48 +00:00
|
|
|
}
|
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 {
|
2020-03-05 08:31:48 +00:00
|
|
|
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
|
|
|
(&mut *new_preroll.borrow_mut())(&element).into()
|
|
|
|
}));
|
|
|
|
match result {
|
|
|
|
Ok(result) => result,
|
|
|
|
Err(err) => {
|
|
|
|
callbacks.panicked.store(true, Ordering::Relaxed);
|
|
|
|
post_panic_error_message(&element, &err);
|
|
|
|
|
|
|
|
gst::FlowReturn::Error
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 07:21:06 +00:00
|
|
|
} else {
|
|
|
|
gst::FlowReturn::Error
|
|
|
|
};
|
|
|
|
|
2021-04-27 15:15:46 +00:00
|
|
|
ret.into_glib()
|
2017-08-01 14:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn trampoline_new_sample(
|
2020-11-21 18:06:11 +00:00
|
|
|
appsink: *mut ffi::GstAppSink,
|
2017-08-01 14:52:11 +00:00
|
|
|
callbacks: gpointer,
|
2020-11-21 18:06:11 +00:00
|
|
|
) -> gst::ffi::GstFlowReturn {
|
2017-08-02 17:09:00 +00:00
|
|
|
let callbacks = &*(callbacks as *const AppSinkCallbacks);
|
2020-04-05 14:52:56 +00:00
|
|
|
let element: Borrowed<AppSink> = from_glib_borrow(appsink);
|
2020-03-05 08:31:48 +00:00
|
|
|
|
|
|
|
if callbacks.panicked.load(Ordering::Relaxed) {
|
2020-04-05 14:52:56 +00:00
|
|
|
let element: Borrowed<AppSink> = from_glib_borrow(appsink);
|
2020-12-20 15:09:22 +00:00
|
|
|
gst::element_error!(&element, gst::LibraryError::Failed, ["Panicked"]);
|
2021-04-27 15:15:46 +00:00
|
|
|
return gst::FlowReturn::Error.into_glib();
|
2020-03-05 08:31:48 +00:00
|
|
|
}
|
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 {
|
2020-03-05 08:31:48 +00:00
|
|
|
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
|
|
|
(&mut *new_sample.borrow_mut())(&element).into()
|
|
|
|
}));
|
|
|
|
match result {
|
|
|
|
Ok(result) => result,
|
|
|
|
Err(err) => {
|
|
|
|
callbacks.panicked.store(true, Ordering::Relaxed);
|
|
|
|
post_panic_error_message(&element, &err);
|
|
|
|
|
|
|
|
gst::FlowReturn::Error
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 07:21:06 +00:00
|
|
|
} else {
|
|
|
|
gst::FlowReturn::Error
|
|
|
|
};
|
|
|
|
|
2021-04-27 15:15:46 +00:00
|
|
|
ret.into_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 {
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_app_sink_set_callbacks")]
|
2017-08-01 14:52:11 +00:00
|
|
|
pub fn set_callbacks(&self, callbacks: AppSinkCallbacks) {
|
2020-02-09 19:30:53 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
static SET_ONCE_QUARK: Lazy<glib::Quark> =
|
|
|
|
Lazy::new(|| glib::Quark::from_string("gstreamer-rs-app-sink-callbacks"));
|
|
|
|
|
2017-08-01 14:52:11 +00:00
|
|
|
unsafe {
|
2020-02-09 19:30:53 +00:00
|
|
|
let sink = self.to_glib_none().0;
|
|
|
|
|
2020-02-15 09:05:25 +00:00
|
|
|
// This is not thread-safe before 1.16.3, see
|
|
|
|
// https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/merge_requests/570
|
|
|
|
if gst::version() < (1, 16, 3, 0) {
|
2021-04-27 15:15:46 +00:00
|
|
|
if !glib::gobject_ffi::g_object_get_qdata(
|
|
|
|
sink as *mut _,
|
|
|
|
SET_ONCE_QUARK.into_glib(),
|
|
|
|
)
|
|
|
|
.is_null()
|
2020-02-15 09:05:25 +00:00
|
|
|
{
|
|
|
|
panic!("AppSink callbacks can only be set once");
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:06:11 +00:00
|
|
|
glib::gobject_ffi::g_object_set_qdata(
|
2020-02-15 09:05:25 +00:00
|
|
|
sink as *mut _,
|
2021-04-27 15:15:46 +00:00
|
|
|
SET_ONCE_QUARK.into_glib(),
|
2020-02-15 09:05:25 +00:00
|
|
|
1 as *mut _,
|
|
|
|
);
|
|
|
|
}
|
2020-02-09 19:30:53 +00:00
|
|
|
|
2020-11-21 18:06:11 +00:00
|
|
|
ffi::gst_app_sink_set_callbacks(
|
2020-02-09 19:30:53 +00:00
|
|
|
sink,
|
2017-08-01 14:52:11 +00:00
|
|
|
mut_override(&callbacks.callbacks),
|
|
|
|
Box::into_raw(Box::new(callbacks)) as *mut _,
|
|
|
|
Some(destroy_callbacks),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 16:13:37 +00:00
|
|
|
|
2020-01-29 12:40:17 +00:00
|
|
|
#[cfg(any(feature = "v1_10"))]
|
|
|
|
pub fn stream(&self) -> AppSinkStream {
|
|
|
|
AppSinkStream::new(self)
|
|
|
|
}
|
2019-01-08 16:13:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 12:40:17 +00:00
|
|
|
#[cfg(any(feature = "v1_10"))]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct AppSinkStream {
|
2020-05-13 19:13:11 +00:00
|
|
|
app_sink: glib::WeakRef<AppSink>,
|
2020-01-29 12:40:17 +00:00
|
|
|
waker_reference: Arc<Mutex<Option<Waker>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(feature = "v1_10"))]
|
|
|
|
impl AppSinkStream {
|
|
|
|
fn new(app_sink: &AppSink) -> Self {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
|
|
|
|
let waker_reference = Arc::new(Mutex::new(None as Option<Waker>));
|
|
|
|
|
|
|
|
app_sink.set_callbacks(
|
2020-06-25 16:22:25 +00:00
|
|
|
AppSinkCallbacks::builder()
|
2020-01-29 12:40:17 +00:00
|
|
|
.new_sample({
|
|
|
|
let waker_reference = Arc::clone(&waker_reference);
|
|
|
|
|
|
|
|
move |_| {
|
|
|
|
if let Some(waker) = waker_reference.lock().unwrap().take() {
|
|
|
|
waker.wake();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(gst::FlowSuccess::Ok)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.eos({
|
|
|
|
let waker_reference = Arc::clone(&waker_reference);
|
|
|
|
|
|
|
|
move |_| {
|
|
|
|
if let Some(waker) = waker_reference.lock().unwrap().take() {
|
|
|
|
waker.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
Self {
|
2020-05-13 19:13:11 +00:00
|
|
|
app_sink: app_sink.downgrade(),
|
2020-01-29 12:40:17 +00:00
|
|
|
waker_reference,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 09:05:25 +00:00
|
|
|
#[cfg(any(feature = "v1_10"))]
|
|
|
|
impl Drop for AppSinkStream {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// This is not thread-safe before 1.16.3, see
|
|
|
|
// https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/merge_requests/570
|
|
|
|
if gst::version() >= (1, 16, 3, 0) {
|
2020-05-13 19:13:11 +00:00
|
|
|
if let Some(app_sink) = self.app_sink.upgrade() {
|
2020-06-25 16:22:25 +00:00
|
|
|
app_sink.set_callbacks(AppSinkCallbacks::builder().build());
|
2020-05-13 19:13:11 +00:00
|
|
|
}
|
2020-02-15 09:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:40:17 +00:00
|
|
|
#[cfg(any(feature = "v1_10"))]
|
|
|
|
impl Stream for AppSinkStream {
|
|
|
|
type Item = gst::Sample;
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, context: &mut Context) -> Poll<Option<Self::Item>> {
|
2020-01-29 13:16:29 +00:00
|
|
|
let mut waker = self.waker_reference.lock().unwrap();
|
|
|
|
|
2020-05-13 19:13:11 +00:00
|
|
|
let app_sink = match self.app_sink.upgrade() {
|
|
|
|
Some(app_sink) => app_sink,
|
|
|
|
None => return Poll::Ready(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
app_sink
|
2020-10-13 16:08:52 +00:00
|
|
|
.try_pull_sample(gst::ClockTime::zero())
|
2020-01-29 12:40:17 +00:00
|
|
|
.map(|sample| Poll::Ready(Some(sample)))
|
|
|
|
.unwrap_or_else(|| {
|
2020-05-13 19:13:11 +00:00
|
|
|
if app_sink.is_eos() {
|
2020-01-29 12:40:17 +00:00
|
|
|
return Poll::Ready(None);
|
|
|
|
}
|
|
|
|
|
2020-01-29 13:16:29 +00:00
|
|
|
waker.replace(context.waker().to_owned());
|
2020-01-29 12:40:17 +00:00
|
|
|
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 13:16:29 +00:00
|
|
|
#[cfg(any(feature = "v1_10"))]
|
2020-01-29 12:40:17 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-01-29 13:16:29 +00:00
|
|
|
use futures_util::StreamExt;
|
|
|
|
use gst::prelude::*;
|
2020-01-29 12:40:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_app_sink_stream() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-01-29 13:16:29 +00:00
|
|
|
let videotestsrc = gst::ElementFactory::make("videotestsrc", None).unwrap();
|
|
|
|
let appsink = gst::ElementFactory::make("appsink", None).unwrap();
|
|
|
|
|
|
|
|
videotestsrc.set_property("num-buffers", &5).unwrap();
|
|
|
|
|
|
|
|
let pipeline = gst::Pipeline::new(None);
|
|
|
|
pipeline.add(&videotestsrc).unwrap();
|
|
|
|
pipeline.add(&appsink).unwrap();
|
|
|
|
|
|
|
|
videotestsrc.link(&appsink).unwrap();
|
|
|
|
|
|
|
|
let app_sink_stream = appsink.dynamic_cast::<AppSink>().unwrap().stream();
|
|
|
|
let samples_future = app_sink_stream.collect::<Vec<gst::Sample>>();
|
|
|
|
|
|
|
|
pipeline.set_state(gst::State::Playing).unwrap();
|
|
|
|
let samples = futures_executor::block_on(samples_future);
|
|
|
|
pipeline.set_state(gst::State::Null).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(samples.len(), 5);
|
2020-01-29 12:40:17 +00:00
|
|
|
}
|
|
|
|
}
|