2017-07-12 07:28:42 +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 std::cell::RefCell;
|
|
|
|
use std::mem::transmute;
|
|
|
|
use ffi;
|
|
|
|
use glib;
|
|
|
|
use glib::translate::*;
|
2017-07-31 11:16:42 +00:00
|
|
|
use glib::source::{CallbackGuard, Continue, Priority, SourceId};
|
2017-07-12 07:28:42 +00:00
|
|
|
use glib_ffi;
|
|
|
|
use glib_ffi::{gboolean, gpointer};
|
2017-08-01 17:52:29 +00:00
|
|
|
use std::ptr;
|
2017-07-12 07:28:42 +00:00
|
|
|
|
|
|
|
use Bus;
|
|
|
|
use BusSyncReply;
|
|
|
|
use Message;
|
|
|
|
|
|
|
|
unsafe extern "C" fn trampoline_watch(
|
|
|
|
bus: *mut ffi::GstBus,
|
|
|
|
msg: *mut ffi::GstMessage,
|
|
|
|
func: gpointer,
|
|
|
|
) -> gboolean {
|
|
|
|
let _guard = CallbackGuard::new();
|
2017-09-10 11:54:43 +00:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
|
2017-08-13 22:40:43 +00:00
|
|
|
let func: &RefCell<Box<FnMut(&Bus, &Message) -> Continue + Send + 'static>> = transmute(func);
|
2017-09-09 13:01:32 +00:00
|
|
|
(&mut *func.borrow_mut())(&from_glib_borrow(bus), &Message::from_glib_borrow(msg)).to_glib()
|
2017-07-12 07:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn destroy_closure_watch(ptr: gpointer) {
|
|
|
|
let _guard = CallbackGuard::new();
|
2017-08-13 22:40:43 +00:00
|
|
|
Box::<RefCell<Box<FnMut(&Bus, &Message) -> Continue + Send + 'static>>>::from_raw(
|
|
|
|
ptr as *mut _,
|
|
|
|
);
|
2017-07-12 07:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn into_raw_watch<F: FnMut(&Bus, &Message) -> Continue + Send + 'static>(func: F) -> gpointer {
|
2017-09-10 11:54:43 +00:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
|
2017-07-12 07:28:42 +00:00
|
|
|
let func: Box<RefCell<Box<FnMut(&Bus, &Message) -> Continue + Send + 'static>>> =
|
|
|
|
Box::new(RefCell::new(Box::new(func)));
|
|
|
|
Box::into_raw(func) as gpointer
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn trampoline_sync(
|
|
|
|
bus: *mut ffi::GstBus,
|
|
|
|
msg: *mut ffi::GstMessage,
|
|
|
|
func: gpointer,
|
|
|
|
) -> ffi::GstBusSyncReply {
|
|
|
|
let _guard = CallbackGuard::new();
|
2017-09-10 11:54:43 +00:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
|
2017-08-13 22:40:43 +00:00
|
|
|
let f: &&(Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static) = transmute(func);
|
2017-09-09 13:01:32 +00:00
|
|
|
f(&from_glib_borrow(bus), &Message::from_glib_borrow(msg)).to_glib()
|
2017-07-12 07:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn destroy_closure_sync(ptr: gpointer) {
|
|
|
|
let _guard = CallbackGuard::new();
|
2017-08-13 22:40:43 +00:00
|
|
|
Box::<Box<Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static>>::from_raw(ptr as *mut _);
|
2017-07-12 07:28:42 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 22:40:43 +00:00
|
|
|
fn into_raw_sync<F: Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static>(
|
|
|
|
func: F,
|
|
|
|
) -> gpointer {
|
|
|
|
let func: Box<Box<Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static>> =
|
2017-08-01 14:28:36 +00:00
|
|
|
Box::new(Box::new(func));
|
2017-07-12 07:28:42 +00:00
|
|
|
Box::into_raw(func) as gpointer
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bus {
|
2017-12-18 07:38:40 +00:00
|
|
|
pub fn add_signal_watch_full(&self, priority: Priority) {
|
|
|
|
unsafe {
|
|
|
|
ffi::gst_bus_add_signal_watch_full(self.to_glib_none().0, priority.to_glib());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 07:28:42 +00:00
|
|
|
pub fn create_watch<'a, N: Into<Option<&'a str>>, F>(
|
|
|
|
&self,
|
|
|
|
name: N,
|
|
|
|
priority: Priority,
|
|
|
|
func: F,
|
|
|
|
) -> Option<glib::Source>
|
|
|
|
where
|
|
|
|
F: FnMut(&Bus, &Message) -> Continue + Send + 'static,
|
|
|
|
{
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2017-07-12 07:28:42 +00:00
|
|
|
unsafe {
|
|
|
|
let source = ffi::gst_bus_create_watch(self.to_glib_none().0);
|
|
|
|
let trampoline = trampoline_watch as gpointer;
|
|
|
|
glib_ffi::g_source_set_callback(
|
|
|
|
source,
|
|
|
|
Some(transmute(trampoline)),
|
|
|
|
into_raw_watch(func),
|
|
|
|
Some(destroy_closure_watch),
|
|
|
|
);
|
|
|
|
glib_ffi::g_source_set_priority(source, priority.to_glib());
|
|
|
|
|
|
|
|
let name = name.into();
|
|
|
|
if let Some(name) = name {
|
|
|
|
glib_ffi::g_source_set_name(source, name.to_glib_none().0);
|
|
|
|
}
|
|
|
|
|
|
|
|
from_glib_full(source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_watch<F>(&self, func: F) -> SourceId
|
|
|
|
where
|
|
|
|
F: FnMut(&Bus, &Message) -> Continue + Send + 'static,
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
from_glib(ffi::gst_bus_add_watch_full(
|
|
|
|
self.to_glib_none().0,
|
|
|
|
glib_ffi::G_PRIORITY_DEFAULT,
|
|
|
|
Some(trampoline_watch),
|
|
|
|
into_raw_watch(func),
|
|
|
|
Some(destroy_closure_watch),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_sync_handler<F>(&self, func: F)
|
|
|
|
where
|
2017-08-01 14:28:36 +00:00
|
|
|
F: Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static,
|
2017-07-12 07:28:42 +00:00
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
ffi::gst_bus_set_sync_handler(
|
|
|
|
self.to_glib_none().0,
|
|
|
|
Some(trampoline_sync),
|
|
|
|
into_raw_sync(func),
|
|
|
|
Some(destroy_closure_sync),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 17:52:29 +00:00
|
|
|
|
|
|
|
pub fn unset_sync_handler(&self) {
|
2017-12-20 17:30:14 +00:00
|
|
|
unsafe {
|
|
|
|
ffi::gst_bus_set_sync_handler(self.to_glib_none().0, None, ptr::null_mut(), None)
|
|
|
|
}
|
2017-08-01 17:52:29 +00:00
|
|
|
}
|
2017-07-12 07:28:42 +00:00
|
|
|
}
|
2017-08-17 10:07:32 +00:00
|
|
|
|
2017-11-12 12:33:02 +00:00
|
|
|
#[cfg(any(feature = "futures", feature = "dox"))]
|
|
|
|
mod futures {
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use futures;
|
|
|
|
use futures::{Async, Poll};
|
|
|
|
use futures::task::Task;
|
|
|
|
use futures::stream::Stream;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub struct BusStream(Bus, Arc<Mutex<Option<Task>>>);
|
|
|
|
|
|
|
|
impl BusStream {
|
|
|
|
pub fn new(bus: &Bus) -> Self {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
let task = Arc::new(Mutex::new(None));
|
2017-11-26 21:59:03 +00:00
|
|
|
let task_clone = Arc::clone(&task);
|
2017-11-12 12:33:02 +00:00
|
|
|
|
|
|
|
bus.set_sync_handler(move |_, _| {
|
|
|
|
let mut task = task_clone.lock().unwrap();
|
|
|
|
if let Some(task) = task.take() {
|
|
|
|
// FIXME: Force type...
|
|
|
|
let task: Task = task;
|
|
|
|
task.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
BusSyncReply::Pass
|
|
|
|
});
|
|
|
|
|
|
|
|
BusStream(bus.clone(), task)
|
|
|
|
}
|
2017-08-17 10:07:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 12:33:02 +00:00
|
|
|
impl Drop for BusStream {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.0.unset_sync_handler();
|
|
|
|
}
|
2017-08-17 10:07:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 12:33:02 +00:00
|
|
|
impl Stream for BusStream {
|
|
|
|
type Item = Message;
|
|
|
|
type Error = ();
|
2017-08-17 10:07:32 +00:00
|
|
|
|
2017-11-12 12:33:02 +00:00
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
let mut task = self.1.lock().unwrap();
|
2017-08-17 10:07:32 +00:00
|
|
|
|
2017-11-12 12:33:02 +00:00
|
|
|
let msg = self.0.pop();
|
|
|
|
if let Some(msg) = msg {
|
|
|
|
Ok(Async::Ready(Some(msg)))
|
|
|
|
} else {
|
|
|
|
*task = Some(futures::task::current());
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
2017-08-17 10:07:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 12:33:02 +00:00
|
|
|
|
|
|
|
#[cfg(any(feature = "futures", feature = "dox"))]
|
|
|
|
pub use bus::futures::BusStream;
|