2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2018-03-15 09:43:35 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::PromiseResult;
|
|
|
|
use crate::Structure;
|
|
|
|
use crate::StructureRef;
|
2018-03-15 09:43:35 +00:00
|
|
|
use glib::translate::*;
|
|
|
|
|
2020-05-27 10:46:04 +00:00
|
|
|
use std::ptr;
|
2020-02-09 17:07:18 +00:00
|
|
|
use std::task::{Context, Poll};
|
2021-04-23 11:49:59 +00:00
|
|
|
use std::{ops::Deref, pin::Pin};
|
2020-02-09 17:07:18 +00:00
|
|
|
|
2020-12-17 22:38:06 +00:00
|
|
|
glib::wrapper! {
|
2019-01-22 15:43:29 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2020-11-21 13:46:48 +00:00
|
|
|
pub struct Promise(Shared<ffi::GstPromise>);
|
2018-03-15 09:43:35 +00:00
|
|
|
|
|
|
|
match fn {
|
2020-11-21 13:46:48 +00:00
|
|
|
ref => |ptr| ffi::gst_mini_object_ref(ptr as *mut _),
|
|
|
|
unref => |ptr| ffi::gst_mini_object_unref(ptr as *mut _),
|
2021-04-14 20:30:38 +00:00
|
|
|
type_ => || ffi::gst_promise_get_type(),
|
2018-03-15 09:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 10:40:41 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
|
|
pub enum PromiseError {
|
|
|
|
Interrupted,
|
|
|
|
Expired,
|
|
|
|
Other(PromiseResult),
|
|
|
|
}
|
|
|
|
|
2018-03-15 09:43:35 +00:00
|
|
|
impl Promise {
|
|
|
|
pub fn new() -> Promise {
|
|
|
|
assert_initialized_main_thread!();
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib_full(ffi::gst_promise_new()) }
|
2018-03-15 09:43:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 08:53:35 +00:00
|
|
|
pub fn with_change_func<F>(func: F) -> Promise
|
2018-04-01 08:30:03 +00:00
|
|
|
where
|
2020-05-27 10:46:04 +00:00
|
|
|
F: FnOnce(Result<Option<&StructureRef>, PromiseError>) + Send + 'static,
|
2018-04-01 08:30:03 +00:00
|
|
|
{
|
2020-03-22 14:18:47 +00:00
|
|
|
assert_initialized_main_thread!();
|
2019-01-30 13:02:03 +00:00
|
|
|
let user_data: Box<Option<F>> = Box::new(Some(func));
|
2018-03-15 09:43:35 +00:00
|
|
|
|
2019-11-13 10:40:41 +00:00
|
|
|
unsafe extern "C" fn trampoline<
|
2020-05-27 10:46:04 +00:00
|
|
|
F: FnOnce(Result<Option<&StructureRef>, PromiseError>) + Send + 'static,
|
2019-11-13 10:40:41 +00:00
|
|
|
>(
|
2020-11-21 13:46:48 +00:00
|
|
|
promise: *mut ffi::GstPromise,
|
|
|
|
user_data: glib::ffi::gpointer,
|
2018-03-15 09:43:35 +00:00
|
|
|
) {
|
2019-01-30 13:02:03 +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();
|
|
|
|
|
2020-04-05 14:52:56 +00:00
|
|
|
let promise: Borrowed<Promise> = from_glib_borrow(promise);
|
2019-11-13 10:40:41 +00:00
|
|
|
|
|
|
|
let res = match promise.wait() {
|
2021-04-20 13:12:36 +00:00
|
|
|
PromiseResult::Replied => Ok(promise.get_reply()),
|
2019-11-13 10:40:41 +00:00
|
|
|
PromiseResult::Interrupted => Err(PromiseError::Interrupted),
|
|
|
|
PromiseResult::Expired => Err(PromiseError::Expired),
|
|
|
|
PromiseResult::Pending => {
|
|
|
|
panic!("Promise resolved but returned Pending");
|
|
|
|
}
|
|
|
|
err => Err(PromiseError::Other(err)),
|
|
|
|
};
|
|
|
|
|
|
|
|
callback(res);
|
2018-03-15 09:43:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 10:40:41 +00:00
|
|
|
unsafe extern "C" fn free_user_data<
|
2020-05-27 10:46:04 +00:00
|
|
|
F: FnOnce(Result<Option<&StructureRef>, PromiseError>) + Send + 'static,
|
2019-11-13 10:40:41 +00:00
|
|
|
>(
|
2020-11-21 13:46:48 +00:00
|
|
|
user_data: glib::ffi::gpointer,
|
2018-03-15 09:43:35 +00:00
|
|
|
) {
|
2019-01-30 13:02:03 +00:00
|
|
|
let _: Box<Option<F>> = Box::from_raw(user_data as *mut _);
|
2018-03-15 09:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib_full(ffi::gst_promise_new_with_change_func(
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(trampoline::<F>),
|
2018-03-15 09:43:35 +00:00
|
|
|
Box::into_raw(user_data) as *mut _,
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(free_user_data::<F>),
|
2018-03-15 09:43:35 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 11:49:59 +00:00
|
|
|
pub fn new_future() -> (Self, PromiseFuture) {
|
2020-02-09 17:07:18 +00:00
|
|
|
use futures_channel::oneshot;
|
|
|
|
|
2020-05-27 11:47:31 +00:00
|
|
|
// We only use the channel as a convenient waker
|
2020-02-09 17:07:18 +00:00
|
|
|
let (sender, receiver) = oneshot::channel();
|
2020-06-11 08:53:35 +00:00
|
|
|
let promise = Self::with_change_func(move |_res| {
|
2020-05-27 11:47:31 +00:00
|
|
|
let _ = sender.send(());
|
2020-02-09 17:07:18 +00:00
|
|
|
});
|
|
|
|
|
2021-04-23 11:49:59 +00:00
|
|
|
(promise.clone(), PromiseFuture(promise, receiver))
|
2020-02-09 17:07:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 09:43:35 +00:00
|
|
|
pub fn expire(&self) {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_promise_expire(self.to_glib_none().0);
|
2018-03-15 09:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 13:12:36 +00:00
|
|
|
pub fn get_reply(&self) -> Option<&StructureRef> {
|
2018-03-15 09:43:35 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let s = ffi::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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn interrupt(&self) {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_promise_interrupt(self.to_glib_none().0);
|
2018-03-15 09:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:46:04 +00:00
|
|
|
pub fn reply(&self, s: Option<Structure>) {
|
2018-03-15 09:43:35 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_promise_reply(
|
2020-05-27 10:46:04 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
s.map(|s| s.into_ptr()).unwrap_or(ptr::null_mut()),
|
|
|
|
);
|
2018-03-15 09:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wait(&self) -> PromiseResult {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib(ffi::gst_promise_wait(self.to_glib_none().0)) }
|
2018-03-15 09:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Promise {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for Promise {}
|
|
|
|
unsafe impl Sync for Promise {}
|
2019-11-13 11:13:14 +00:00
|
|
|
|
2020-02-09 17:07:18 +00:00
|
|
|
#[derive(Debug)]
|
2021-04-23 11:49:59 +00:00
|
|
|
pub struct PromiseFuture(Promise, futures_channel::oneshot::Receiver<()>);
|
2020-02-09 17:07:18 +00:00
|
|
|
|
2021-04-23 11:49:59 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct PromiseReply(Promise);
|
|
|
|
|
|
|
|
impl std::future::Future for PromiseFuture {
|
|
|
|
type Output = Result<Option<PromiseReply>, PromiseError>;
|
2020-02-09 17:07:18 +00:00
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, context: &mut Context) -> Poll<Self::Output> {
|
2020-05-27 11:47:31 +00:00
|
|
|
match Pin::new(&mut self.1).poll(context) {
|
2020-02-09 17:07:18 +00:00
|
|
|
Poll::Ready(Err(_)) => panic!("Sender dropped before callback was called"),
|
2020-05-27 11:47:31 +00:00
|
|
|
Poll::Ready(Ok(())) => {
|
|
|
|
let res = match self.0.wait() {
|
2021-04-23 11:49:59 +00:00
|
|
|
PromiseResult::Replied => {
|
|
|
|
if self.0.get_reply().is_none() {
|
2020-05-27 11:47:31 +00:00
|
|
|
Ok(None)
|
|
|
|
} else {
|
2021-04-23 11:49:59 +00:00
|
|
|
Ok(Some(PromiseReply(self.0.clone())))
|
2020-05-27 11:47:31 +00:00
|
|
|
}
|
2021-04-23 11:49:59 +00:00
|
|
|
}
|
2020-05-27 11:47:31 +00:00
|
|
|
PromiseResult::Interrupted => Err(PromiseError::Interrupted),
|
|
|
|
PromiseResult::Expired => Err(PromiseError::Expired),
|
|
|
|
PromiseResult::Pending => {
|
|
|
|
panic!("Promise resolved but returned Pending");
|
|
|
|
}
|
|
|
|
err => Err(PromiseError::Other(err)),
|
|
|
|
};
|
|
|
|
Poll::Ready(res)
|
|
|
|
}
|
2020-02-09 17:07:18 +00:00
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 11:49:59 +00:00
|
|
|
impl Deref for PromiseReply {
|
|
|
|
type Target = StructureRef;
|
|
|
|
|
|
|
|
fn deref(&self) -> &StructureRef {
|
|
|
|
self.0.get_reply().expect("Promise without reply")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:13:14 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_change_func() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2019-11-13 11:13:14 +00:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
2020-06-11 08:53:35 +00:00
|
|
|
let promise = Promise::with_change_func(move |res| {
|
2020-05-27 10:46:04 +00:00
|
|
|
sender.send(res.map(|s| s.map(ToOwned::to_owned))).unwrap();
|
2019-11-13 11:13:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
thread::spawn(move || {
|
2020-05-27 10:46:04 +00:00
|
|
|
promise.reply(Some(crate::Structure::new("foo/bar", &[])));
|
2019-11-13 11:13:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let res = receiver.recv().unwrap();
|
2020-05-27 10:46:04 +00:00
|
|
|
let res = res.expect("promise failed").expect("promise returned None");
|
2021-04-11 19:39:50 +00:00
|
|
|
assert_eq!(res.name(), "foo/bar");
|
2019-11-13 11:13:14 +00:00
|
|
|
}
|
|
|
|
}
|