gstreamer-rs/gstreamer-app/src/app_src.rs

173 lines
5.2 KiB
Rust
Raw Normal View History

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.
use AppSrc;
use ffi;
use glib::translate::*;
use gst;
use glib::source::CallbackGuard;
use glib_ffi::{gboolean, gpointer};
use std::ptr;
use std::mem;
2017-08-01 14:42:15 +00:00
pub struct AppSrcCallbacks {
need_data: Option<Box<Fn(&AppSrc, u32) + Send + Sync + 'static>>,
enough_data: Option<Box<Fn(&AppSrc) + Send + Sync + 'static>>,
seek_data: Option<Box<Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>>,
2017-08-01 14:42:15 +00:00
callbacks: ffi::GstAppSrcCallbacks,
}
unsafe impl Send for AppSrcCallbacks {}
unsafe impl Sync for AppSrcCallbacks {}
impl AppSrcCallbacks {
pub fn new() -> AppSrcCallbacksBuilder {
skip_assert_initialized!();
AppSrcCallbacksBuilder {
need_data: None,
enough_data: None,
seek_data: None,
}
}
}
pub struct AppSrcCallbacksBuilder {
need_data: Option<Box<Fn(&AppSrc, u32) + Send + Sync + 'static>>,
enough_data: Option<Box<Fn(&AppSrc) + Send + Sync + 'static>>,
seek_data: Option<Box<Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>>,
}
impl AppSrcCallbacksBuilder {
pub fn new() -> Self {
skip_assert_initialized!();
AppSrcCallbacksBuilder {
need_data: None,
enough_data: None,
seek_data: None,
}
}
pub fn need_data<F: Fn(&AppSrc, u32) + Send + Sync + 'static>(self, need_data: F) -> Self {
Self {
need_data: Some(Box::new(need_data)),
..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
}
}
pub fn seek_data<F: Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>(self, seek_data: F) -> Self {
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 {
need_data: self.need_data,
enough_data: self.enough_data,
seek_data: self.seek_data,
2017-08-01 14:42:15 +00:00
callbacks: ffi::GstAppSrcCallbacks {
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(
appsrc: *mut ffi::GstAppSrc,
length: u32,
callbacks: gpointer,
) {
let _guard = CallbackGuard::new();
let callbacks = &*(callbacks as *const AppSrcCallbacks);
2017-08-01 14:42:15 +00:00
callbacks.need_data.as_ref().map(|f| f(&from_glib_borrow(appsrc), length));
2017-08-01 14:42:15 +00:00
}
unsafe extern "C" fn trampoline_enough_data(appsrc: *mut ffi::GstAppSrc, callbacks: gpointer) {
let _guard = CallbackGuard::new();
let callbacks = &*(callbacks as *const AppSrcCallbacks);
2017-08-01 14:42:15 +00:00
callbacks.enough_data.as_ref().map(|f| f(&from_glib_borrow(appsrc)));
2017-08-01 14:42:15 +00:00
}
unsafe extern "C" fn trampoline_seek_data(
appsrc: *mut ffi::GstAppSrc,
offset: u64,
callbacks: gpointer,
) -> gboolean {
let _guard = CallbackGuard::new();
let callbacks = &*(callbacks as *const AppSrcCallbacks);
2017-08-01 14:42:15 +00:00
callbacks.seek_data.as_ref().map(|f| f(&from_glib_borrow(appsrc), offset)).unwrap_or(false).to_glib()
2017-08-01 14:42:15 +00:00
}
unsafe extern "C" fn destroy_callbacks(ptr: gpointer) {
let _guard = CallbackGuard::new();
Box::<AppSrcCallbacks>::from_raw(ptr as *mut _);
2017-08-01 14:42:15 +00:00
}
impl AppSrc {
pub fn push_buffer(&self, buffer: gst::Buffer) -> gst::FlowReturn {
unsafe {
from_glib(ffi::gst_app_src_push_buffer(
self.to_glib_none().0,
buffer.into_ptr(),
))
}
}
pub fn set_callbacks(&self, callbacks: AppSrcCallbacks) {
unsafe {
ffi::gst_app_src_set_callbacks(
self.to_glib_none().0,
mut_override(&callbacks.callbacks),
Box::into_raw(Box::new(callbacks)) as *mut _,
Some(destroy_callbacks),
);
}
}
pub fn set_latency(&self, min: gst::ClockTime, max: gst::ClockTime) {
unsafe {
ffi::gst_app_src_set_latency(self.to_glib_none().0, min.to_glib(), max.to_glib());
}
}
pub fn get_latency(&self) -> (gst::ClockTime, gst::ClockTime) {
unsafe {
let mut min = mem::uninitialized();
let mut max = mem::uninitialized();
ffi::gst_app_src_get_latency(self.to_glib_none().0, &mut min, &mut max);
(from_glib(min), from_glib(max))
}
}
2017-08-01 14:42:15 +00:00
}