gstreamer-rs/gstreamer/src/promise.rs

114 lines
3.3 KiB
Rust
Raw Normal View History

2018-03-15 09:43:35 +00:00
// Copyright (C) 2018 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 glib::translate::*;
2019-03-19 07:58:20 +00:00
use glib_sys;
use gst_sys;
2018-04-25 08:10:06 +00:00
use PromiseResult;
use Structure;
use StructureRef;
2018-03-15 09:43:35 +00:00
glib_wrapper! {
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
2019-03-19 07:58:20 +00:00
pub struct Promise(Shared<gst_sys::GstPromise>);
2018-03-15 09:43:35 +00:00
match fn {
2019-03-19 07:58:20 +00:00
ref => |ptr| gst_sys::gst_mini_object_ref(ptr as *mut _),
unref => |ptr| gst_sys::gst_mini_object_unref(ptr as *mut _),
get_type => || gst_sys::gst_promise_get_type(),
2018-03-15 09:43:35 +00:00
}
}
impl Promise {
#[cfg(any(feature = "v1_14", feature = "dox"))]
pub fn new() -> Promise {
assert_initialized_main_thread!();
2019-03-19 07:58:20 +00:00
unsafe { from_glib_full(gst_sys::gst_promise_new()) }
2018-03-15 09:43:35 +00:00
}
#[cfg(any(feature = "v1_14", feature = "dox"))]
pub fn new_with_change_func<F>(func: F) -> Promise
2018-04-01 08:30:03 +00:00
where
F: FnOnce(&Promise) + Send + 'static,
{
let user_data: Box<Option<F>> = Box::new(Some(func));
2018-03-15 09:43:35 +00:00
unsafe extern "C" fn trampoline<F: FnOnce(&Promise) + Send + 'static>(
2019-03-19 07:58:20 +00:00
promise: *mut gst_sys::GstPromise,
user_data: glib_sys::gpointer,
2018-03-15 09:43:35 +00:00
) {
let user_data: &mut Option<F> = &mut *(user_data as *mut _);
2018-03-15 09:43:35 +00:00
let callback = user_data.take().unwrap();
callback(&from_glib_borrow(promise));
}
unsafe extern "C" fn free_user_data<F: FnOnce(&Promise) + Send + 'static>(
2019-03-19 07:58:20 +00:00
user_data: glib_sys::gpointer,
2018-03-15 09:43:35 +00:00
) {
let _: Box<Option<F>> = Box::from_raw(user_data as *mut _);
2018-03-15 09:43:35 +00:00
}
unsafe {
2019-03-19 07:58:20 +00:00
from_glib_full(gst_sys::gst_promise_new_with_change_func(
Some(trampoline::<F>),
2018-03-15 09:43:35 +00:00
Box::into_raw(user_data) as *mut _,
Some(free_user_data::<F>),
2018-03-15 09:43:35 +00:00
))
}
}
#[cfg(any(feature = "v1_14", feature = "dox"))]
pub fn expire(&self) {
unsafe {
2019-03-19 07:58:20 +00:00
gst_sys::gst_promise_expire(self.to_glib_none().0);
2018-03-15 09:43:35 +00:00
}
}
#[cfg(any(feature = "v1_14", feature = "dox"))]
pub fn get_reply(&self) -> Option<&StructureRef> {
unsafe {
2019-03-19 07:58:20 +00:00
let s = gst_sys::gst_promise_get_reply(self.to_glib_none().0);
2018-03-15 09:43:35 +00:00
if s.is_null() {
None
} else {
Some(StructureRef::from_glib_borrow(s))
}
}
}
#[cfg(any(feature = "v1_14", feature = "dox"))]
pub fn interrupt(&self) {
unsafe {
2019-03-19 07:58:20 +00:00
gst_sys::gst_promise_interrupt(self.to_glib_none().0);
2018-03-15 09:43:35 +00:00
}
}
#[cfg(any(feature = "v1_14", feature = "dox"))]
pub fn reply(&self, s: Structure) {
unsafe {
2019-03-19 07:58:20 +00:00
gst_sys::gst_promise_reply(self.to_glib_none().0, s.into_ptr());
2018-03-15 09:43:35 +00:00
}
}
#[cfg(any(feature = "v1_14", feature = "dox"))]
pub fn wait(&self) -> PromiseResult {
2019-03-19 07:58:20 +00:00
unsafe { from_glib(gst_sys::gst_promise_wait(self.to_glib_none().0)) }
2018-03-15 09:43:35 +00:00
}
}
#[cfg(any(feature = "v1_14", feature = "dox"))]
impl Default for Promise {
fn default() -> Self {
Self::new()
}
}
unsafe impl Send for Promise {}
unsafe impl Sync for Promise {}