Add bindings for gst_video::convert_sample() and ::convert_sample_async()

This commit is contained in:
Sebastian Dröge 2017-12-24 14:30:38 +02:00
parent f2ef2b6bea
commit 93723026eb
2 changed files with 95 additions and 0 deletions

View file

@ -0,0 +1,87 @@
// 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;
use gst_ffi;
use glib_ffi;
use gst;
use glib;
use glib::translate::{from_glib_full, from_glib_none, ToGlib, ToGlibPtr};
use std::ptr;
use std::mem;
pub fn convert_sample(
sample: &gst::Sample,
caps: &gst::Caps,
timeout: gst::ClockTime,
) -> Result<gst::Sample, glib::Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::gst_video_convert_sample(
sample.to_glib_none().0,
caps.to_glib_none().0,
timeout.to_glib(),
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
pub fn convert_sample_async<F>(
sample: &gst::Sample,
caps: &gst::Caps,
timeout: gst::ClockTime,
func: F,
) where
F: FnOnce(Result<gst::Sample, glib::Error>) + Send + 'static,
{
unsafe extern "C" fn convert_sample_async_trampoline<F>(
sample: *mut gst_ffi::GstSample,
error: *mut glib_ffi::GError,
user_data: glib_ffi::gpointer,
) where
F: FnOnce(Result<gst::Sample, glib::Error>) + Send + 'static,
{
callback_guard!();
let callback: &mut Option<Box<F>> = mem::transmute(user_data);
let callback = callback.take().unwrap();
if error.is_null() {
callback(Ok(from_glib_none(sample)))
} else {
callback(Err(from_glib_none(error)))
}
}
unsafe extern "C" fn convert_sample_async_free<F>(user_data: glib_ffi::gpointer)
where
F: FnOnce(Result<gst::Sample, glib::Error>) + Send + 'static,
{
callback_guard!();
let _: Box<Option<Box<F>>> = Box::from_raw(user_data as *mut _);
}
unsafe {
let user_data: Box<Option<Box<F>>> = Box::new(Some(Box::new(func)));
ffi::gst_video_convert_sample_async(
sample.to_glib_none().0,
caps.to_glib_none().0,
timeout.to_glib(),
Some(convert_sample_async_trampoline::<F>),
Box::into_raw(user_data) as glib_ffi::gpointer,
Some(convert_sample_async_free::<F>),
);
}
}

View file

@ -31,6 +31,12 @@ macro_rules! skip_assert_initialized {
)
}
macro_rules! callback_guard {
() => (
let _guard = ::glib::CallbackGuard::new();
)
}
pub use glib::{Cast, Continue, Error, IsA, StaticType, ToValue, Type, TypedValue, Value};
#[cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
@ -52,6 +58,8 @@ mod video_overlay;
pub use video_overlay::VideoOverlayExtManual;
mod video_event;
pub use video_event::*;
mod functions;
pub use functions::*;
// Re-export all the traits in a prelude module, so that applications
// can always "use gst::prelude::*" without getting conflicts