From 605d4fbf24a1fd1e1e4e7d0f31920d7e61499f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 9 Feb 2020 18:53:36 +0200 Subject: [PATCH] video: Add Future variant of convert_sample_async() Requires to be spawned on the GLib main context futures executor as the function itself requires a GLib main context to call the callback. --- gstreamer-video/Cargo.toml | 2 ++ gstreamer-video/src/functions.rs | 29 +++++++++++++++++++++++++++++ gstreamer-video/src/lib.rs | 2 ++ 3 files changed, 33 insertions(+) diff --git a/gstreamer-video/Cargo.toml b/gstreamer-video/Cargo.toml index c8950e0c9..a8f5d6a35 100644 --- a/gstreamer-video/Cargo.toml +++ b/gstreamer-video/Cargo.toml @@ -24,6 +24,8 @@ glib = { version = "0.9" } gstreamer = { version = "0.15", path = "../gstreamer" } gstreamer-base = { version = "0.15", path = "../gstreamer-base" } lazy_static = "1.0" +futures-channel = "0.3" +futures-util = "0.3" [build-dependencies] rustdoc-stripper = { version = "0.1", optional = true } diff --git a/gstreamer-video/src/functions.rs b/gstreamer-video/src/functions.rs index 987b2ce3f..33b465cb8 100644 --- a/gstreamer-video/src/functions.rs +++ b/gstreamer-video/src/functions.rs @@ -107,6 +107,35 @@ unsafe fn convert_sample_async_unsafe( ); } +pub fn convert_sample_future( + sample: &gst::Sample, + caps: &gst::Caps, + timeout: gst::ClockTime, +) -> std::pin::Pin> + 'static>> +{ + use futures_channel::oneshot; + use futures_util::future::lazy; + use futures_util::future::FutureExt; + + let (sender, receiver) = oneshot::channel(); + + let sample = sample.clone(); + let caps = caps.clone(); + let future = lazy(move |_| { + assert!( + glib::MainContext::ref_thread_default().is_owner(), + "Spawning futures only allowed if the thread is owning the MainContext" + ); + + convert_sample_async(&sample, &caps, timeout, move |res| { + let _ = sender.send(res); + }); + }) + .then(|_| receiver.map(|res| res.expect("Sender dropped before callback was called"))); + + Box::pin(future) +} + #[cfg(test)] mod tests { use super::*; diff --git a/gstreamer-video/src/lib.rs b/gstreamer-video/src/lib.rs index 6766471d3..6ed325673 100644 --- a/gstreamer-video/src/lib.rs +++ b/gstreamer-video/src/lib.rs @@ -18,6 +18,8 @@ extern crate glib_sys; extern crate gobject_sys; #[macro_use] extern crate gstreamer as gst; +extern crate futures_channel; +extern crate futures_util; extern crate gstreamer_base as gst_base; extern crate gstreamer_base_sys as gst_base_sys; extern crate gstreamer_sys as gst_sys;