2017-08-01 14:42:15 +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.
|
|
|
|
|
2018-04-01 08:30:03 +00:00
|
|
|
use glib::translate::*;
|
2019-03-19 07:58:20 +00:00
|
|
|
use glib_sys::{gboolean, gpointer};
|
2018-04-01 08:30:03 +00:00
|
|
|
use gst;
|
2019-03-19 07:58:20 +00:00
|
|
|
use gst_app_sys;
|
2018-04-25 08:10:06 +00:00
|
|
|
use std::cell::RefCell;
|
2017-12-16 12:04:36 +00:00
|
|
|
use std::mem;
|
2018-04-01 08:30:03 +00:00
|
|
|
use std::ptr;
|
2018-04-25 08:10:06 +00:00
|
|
|
use AppSrc;
|
2017-08-01 14:42:15 +00:00
|
|
|
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2017-08-01 14:42:15 +00:00
|
|
|
pub struct AppSrcCallbacks {
|
2019-06-06 06:09:34 +00:00
|
|
|
need_data: Option<RefCell<Box<dyn FnMut(&AppSrc, u32) + Send + 'static>>>,
|
|
|
|
enough_data: Option<Box<dyn Fn(&AppSrc) + Send + Sync + 'static>>,
|
|
|
|
seek_data: Option<Box<dyn Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>>,
|
2019-03-19 07:58:20 +00:00
|
|
|
callbacks: gst_app_sys::GstAppSrcCallbacks,
|
2017-08-01 14:42:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 12:14:29 +00:00
|
|
|
unsafe impl Send for AppSrcCallbacks {}
|
|
|
|
unsafe impl Sync for AppSrcCallbacks {}
|
|
|
|
|
2017-12-16 09:37:00 +00:00
|
|
|
impl AppSrcCallbacks {
|
2019-02-28 08:54:32 +00:00
|
|
|
#[allow(clippy::new_ret_no_self)]
|
2017-12-16 09:37:00 +00:00
|
|
|
pub fn new() -> AppSrcCallbacksBuilder {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
|
|
|
|
AppSrcCallbacksBuilder {
|
|
|
|
need_data: None,
|
|
|
|
enough_data: None,
|
|
|
|
seek_data: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2017-12-10 13:18:54 +00:00
|
|
|
pub struct AppSrcCallbacksBuilder {
|
2019-06-06 06:09:34 +00:00
|
|
|
need_data: Option<RefCell<Box<dyn FnMut(&AppSrc, u32) + Send + 'static>>>,
|
|
|
|
enough_data: Option<Box<dyn Fn(&AppSrc) + Send + Sync + 'static>>,
|
|
|
|
seek_data: Option<Box<dyn Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>>,
|
2017-12-10 13:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppSrcCallbacksBuilder {
|
2018-04-25 08:08:44 +00:00
|
|
|
pub fn need_data<F: FnMut(&AppSrc, u32) + Send + 'static>(self, need_data: F) -> Self {
|
2017-12-10 13:18:54 +00:00
|
|
|
Self {
|
2018-04-25 08:08:44 +00:00
|
|
|
need_data: Some(RefCell::new(Box::new(need_data))),
|
2017-12-10 13:18:54 +00:00
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enough_data<F: Fn(&AppSrc) + Send + Sync + 'static>(self, enough_data: F) -> Self {
|
|
|
|
Self {
|
|
|
|
enough_data: Some(Box::new(enough_data)),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 12:53:00 +00:00
|
|
|
pub fn seek_data<F: Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>(
|
|
|
|
self,
|
|
|
|
seek_data: F,
|
|
|
|
) -> Self {
|
2017-12-10 13:18:54 +00:00
|
|
|
Self {
|
|
|
|
seek_data: Some(Box::new(seek_data)),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> AppSrcCallbacks {
|
|
|
|
let have_need_data = self.need_data.is_some();
|
|
|
|
let have_enough_data = self.enough_data.is_some();
|
|
|
|
let have_seek_data = self.seek_data.is_some();
|
|
|
|
|
2017-08-01 14:42:15 +00:00
|
|
|
AppSrcCallbacks {
|
2017-12-10 13:18:54 +00:00
|
|
|
need_data: self.need_data,
|
|
|
|
enough_data: self.enough_data,
|
|
|
|
seek_data: self.seek_data,
|
2019-03-19 07:58:20 +00:00
|
|
|
callbacks: gst_app_sys::GstAppSrcCallbacks {
|
2017-12-16 12:53:00 +00:00
|
|
|
need_data: if have_need_data {
|
|
|
|
Some(trampoline_need_data)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
enough_data: if have_enough_data {
|
|
|
|
Some(trampoline_enough_data)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
seek_data: if have_seek_data {
|
|
|
|
Some(trampoline_seek_data)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2017-08-01 14:42:15 +00:00
|
|
|
_gst_reserved: [
|
|
|
|
ptr::null_mut(),
|
|
|
|
ptr::null_mut(),
|
|
|
|
ptr::null_mut(),
|
|
|
|
ptr::null_mut(),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn trampoline_need_data(
|
2019-03-19 07:58:20 +00:00
|
|
|
appsrc: *mut gst_app_sys::GstAppSrc,
|
2017-08-01 14:42:15 +00:00
|
|
|
length: u32,
|
|
|
|
callbacks: gpointer,
|
|
|
|
) {
|
2017-08-02 17:09:00 +00:00
|
|
|
let callbacks = &*(callbacks as *const AppSrcCallbacks);
|
2017-08-01 14:42:15 +00:00
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
if let Some(ref need_data) = callbacks.need_data {
|
|
|
|
(&mut *need_data.borrow_mut())(&from_glib_borrow(appsrc), length);
|
|
|
|
}
|
2017-08-01 14:42:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe extern "C" fn trampoline_enough_data(
|
|
|
|
appsrc: *mut gst_app_sys::GstAppSrc,
|
|
|
|
callbacks: gpointer,
|
|
|
|
) {
|
2017-08-02 17:09:00 +00:00
|
|
|
let callbacks = &*(callbacks as *const AppSrcCallbacks);
|
2017-08-01 14:42:15 +00:00
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
if let Some(ref enough_data) = callbacks.enough_data {
|
|
|
|
(*enough_data)(&from_glib_borrow(appsrc));
|
|
|
|
}
|
2017-08-01 14:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn trampoline_seek_data(
|
2019-03-19 07:58:20 +00:00
|
|
|
appsrc: *mut gst_app_sys::GstAppSrc,
|
2017-08-01 14:42:15 +00:00
|
|
|
offset: u64,
|
|
|
|
callbacks: gpointer,
|
|
|
|
) -> gboolean {
|
2017-08-02 17:09:00 +00:00
|
|
|
let callbacks = &*(callbacks as *const AppSrcCallbacks);
|
2017-08-01 14:42:15 +00:00
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
let ret = if let Some(ref seek_data) = callbacks.seek_data {
|
|
|
|
(*seek_data)(&from_glib_borrow(appsrc), offset)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
ret.to_glib()
|
2017-08-01 14:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn destroy_callbacks(ptr: gpointer) {
|
2017-11-11 15:05:08 +00:00
|
|
|
Box::<AppSrcCallbacks>::from_raw(ptr as *mut _);
|
2017-08-01 14:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppSrc {
|
2019-01-08 16:13:37 +00:00
|
|
|
pub fn end_of_stream(&self) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2019-03-19 07:58:20 +00:00
|
|
|
let ret: gst::FlowReturn = unsafe {
|
|
|
|
from_glib(gst_app_sys::gst_app_src_end_of_stream(
|
|
|
|
self.to_glib_none().0,
|
|
|
|
))
|
|
|
|
};
|
2019-01-08 16:13:37 +00:00
|
|
|
ret.into_result()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_buffer(&self, buffer: gst::Buffer) -> Result<gst::FlowSuccess, gst::FlowError> {
|
|
|
|
let ret: gst::FlowReturn = unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib(gst_app_sys::gst_app_src_push_buffer(
|
2017-08-01 14:42:15 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
buffer.into_ptr(),
|
|
|
|
))
|
2019-01-08 16:13:37 +00:00
|
|
|
};
|
|
|
|
ret.into_result()
|
2017-08-01 14:42:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 07:52:36 +00:00
|
|
|
#[cfg(any(feature = "v1_14", feature = "dox"))]
|
2019-01-08 16:13:37 +00:00
|
|
|
pub fn push_buffer_list(
|
|
|
|
&self,
|
|
|
|
list: gst::BufferList,
|
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError> {
|
|
|
|
let ret: gst::FlowReturn = unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib(gst_app_sys::gst_app_src_push_buffer_list(
|
2018-03-15 07:52:36 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
list.into_ptr(),
|
|
|
|
))
|
2019-01-08 16:13:37 +00:00
|
|
|
};
|
|
|
|
ret.into_result()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_sample(&self, sample: &gst::Sample) -> Result<gst::FlowSuccess, gst::FlowError> {
|
|
|
|
let ret: gst::FlowReturn = unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib(gst_app_sys::gst_app_src_push_sample(
|
2019-01-08 16:13:37 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
sample.to_glib_none().0,
|
|
|
|
))
|
|
|
|
};
|
|
|
|
ret.into_result()
|
2018-03-15 07:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 14:42:15 +00:00
|
|
|
pub fn set_callbacks(&self, callbacks: AppSrcCallbacks) {
|
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
gst_app_sys::gst_app_src_set_callbacks(
|
2017-08-01 14:42:15 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
mut_override(&callbacks.callbacks),
|
|
|
|
Box::into_raw(Box::new(callbacks)) as *mut _,
|
|
|
|
Some(destroy_callbacks),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-12-16 12:04:36 +00:00
|
|
|
|
|
|
|
pub fn set_latency(&self, min: gst::ClockTime, max: gst::ClockTime) {
|
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
gst_app_sys::gst_app_src_set_latency(
|
|
|
|
self.to_glib_none().0,
|
|
|
|
min.to_glib(),
|
|
|
|
max.to_glib(),
|
|
|
|
);
|
2017-12-16 12:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_latency(&self) -> (gst::ClockTime, gst::ClockTime) {
|
|
|
|
unsafe {
|
|
|
|
let mut min = mem::uninitialized();
|
|
|
|
let mut max = mem::uninitialized();
|
2019-03-19 07:58:20 +00:00
|
|
|
gst_app_sys::gst_app_src_get_latency(self.to_glib_none().0, &mut min, &mut max);
|
2017-12-16 12:04:36 +00:00
|
|
|
(from_glib(min), from_glib(max))
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 14:42:15 +00:00
|
|
|
}
|