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 glib;
|
2018-06-24 11:44:38 +00:00
|
|
|
use glib::source::{Continue, Priority, SourceId};
|
2018-04-01 08:30:03 +00:00
|
|
|
use glib::translate::*;
|
2019-03-19 07:58:20 +00:00
|
|
|
use glib_sys;
|
|
|
|
use glib_sys::{gboolean, gpointer};
|
|
|
|
use gst_sys;
|
2018-04-01 08:30:03 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::mem::transmute;
|
2017-08-01 17:52:29 +00:00
|
|
|
use std::ptr;
|
2018-03-15 08:39:12 +00:00
|
|
|
|
2017-07-12 07:28:42 +00:00
|
|
|
use Bus;
|
|
|
|
use BusSyncReply;
|
|
|
|
use Message;
|
|
|
|
|
2019-02-10 09:43:55 +00:00
|
|
|
unsafe extern "C" fn trampoline_watch<F: FnMut(&Bus, &Message) -> Continue + 'static>(
|
2019-03-19 07:58:20 +00:00
|
|
|
bus: *mut gst_sys::GstBus,
|
|
|
|
msg: *mut gst_sys::GstMessage,
|
2017-07-12 07:28:42 +00:00
|
|
|
func: gpointer,
|
|
|
|
) -> gboolean {
|
2019-02-10 09:43:55 +00:00
|
|
|
let func: &RefCell<F> = &*(func as *const RefCell<F>);
|
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
|
|
|
}
|
|
|
|
|
2019-02-10 09:43:55 +00:00
|
|
|
unsafe extern "C" fn destroy_closure_watch<F: FnMut(&Bus, &Message) -> Continue + 'static>(
|
2019-01-30 13:02:03 +00:00
|
|
|
ptr: gpointer,
|
|
|
|
) {
|
|
|
|
Box::<RefCell<F>>::from_raw(ptr as *mut _);
|
2017-07-12 07:28:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 09:43:55 +00:00
|
|
|
fn into_raw_watch<F: FnMut(&Bus, &Message) -> Continue + 'static>(func: F) -> gpointer {
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-01-30 13:02:03 +00:00
|
|
|
let func: Box<RefCell<F>> = Box::new(RefCell::new(func));
|
2017-07-12 07:28:42 +00:00
|
|
|
Box::into_raw(func) as gpointer
|
|
|
|
}
|
|
|
|
|
2019-01-30 13:02:03 +00:00
|
|
|
unsafe extern "C" fn trampoline_sync<
|
|
|
|
F: Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static,
|
|
|
|
>(
|
2019-03-19 07:58:20 +00:00
|
|
|
bus: *mut gst_sys::GstBus,
|
|
|
|
msg: *mut gst_sys::GstMessage,
|
2017-07-12 07:28:42 +00:00
|
|
|
func: gpointer,
|
2019-03-19 07:58:20 +00:00
|
|
|
) -> gst_sys::GstBusSyncReply {
|
2019-02-10 09:43:55 +00:00
|
|
|
let f: &F = &*(func as *const F);
|
2018-07-24 12:35:26 +00:00
|
|
|
let res = f(&from_glib_borrow(bus), &Message::from_glib_borrow(msg)).to_glib();
|
|
|
|
|
2019-03-19 07:58:20 +00:00
|
|
|
if res == gst_sys::GST_BUS_DROP {
|
|
|
|
gst_sys::gst_mini_object_unref(msg as *mut _);
|
2018-07-24 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res
|
2017-07-12 07:28:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 13:02:03 +00:00
|
|
|
unsafe extern "C" fn destroy_closure_sync<
|
|
|
|
F: Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static,
|
|
|
|
>(
|
|
|
|
ptr: gpointer,
|
|
|
|
) {
|
|
|
|
Box::<F>::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 {
|
2019-01-30 13:02:03 +00:00
|
|
|
let func: Box<F> = 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 {
|
2019-03-19 07:58:20 +00:00
|
|
|
gst_sys::gst_bus_add_signal_watch_full(self.to_glib_none().0, priority.to_glib());
|
2017-12-18 07:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2018-05-19 07:36:15 +00:00
|
|
|
) -> glib::Source
|
2017-07-12 07:28:42 +00:00
|
|
|
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 {
|
2019-03-19 07:58:20 +00:00
|
|
|
let source = gst_sys::gst_bus_create_watch(self.to_glib_none().0);
|
|
|
|
glib_sys::g_source_set_callback(
|
2017-07-12 07:28:42 +00:00
|
|
|
source,
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(transmute(trampoline_watch::<F> as usize)),
|
2017-07-12 07:28:42 +00:00
|
|
|
into_raw_watch(func),
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(destroy_closure_watch::<F>),
|
2017-07-12 07:28:42 +00:00
|
|
|
);
|
2019-03-19 07:58:20 +00:00
|
|
|
glib_sys::g_source_set_priority(source, priority.to_glib());
|
2017-07-12 07:28:42 +00:00
|
|
|
|
|
|
|
let name = name.into();
|
|
|
|
if let Some(name) = name {
|
2019-03-19 07:58:20 +00:00
|
|
|
glib_sys::g_source_set_name(source, name.to_glib_none().0);
|
2017-07-12 07:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
from_glib_full(source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:30:05 +00:00
|
|
|
pub fn add_watch<F>(&self, func: F) -> Option<SourceId>
|
2017-07-12 07:28:42 +00:00
|
|
|
where
|
|
|
|
F: FnMut(&Bus, &Message) -> Continue + Send + 'static,
|
|
|
|
{
|
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
let res = gst_sys::gst_bus_add_watch_full(
|
2017-07-12 07:28:42 +00:00
|
|
|
self.to_glib_none().0,
|
2019-03-19 07:58:20 +00:00
|
|
|
glib_sys::G_PRIORITY_DEFAULT,
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(trampoline_watch::<F>),
|
2017-07-12 07:28:42 +00:00
|
|
|
into_raw_watch(func),
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(destroy_closure_watch::<F>),
|
2019-02-15 11:30:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if res == 0 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(from_glib(res))
|
|
|
|
}
|
2017-07-12 07:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:30:05 +00:00
|
|
|
pub fn add_watch_local<F>(&self, func: F) -> Option<SourceId>
|
2019-02-10 09:43:55 +00:00
|
|
|
where
|
|
|
|
F: FnMut(&Bus, &Message) -> Continue + 'static,
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
assert!(glib::MainContext::ref_thread_default().is_owner());
|
|
|
|
|
2019-03-19 07:58:20 +00:00
|
|
|
let res = gst_sys::gst_bus_add_watch_full(
|
2019-02-10 09:43:55 +00:00
|
|
|
self.to_glib_none().0,
|
2019-03-19 07:58:20 +00:00
|
|
|
glib_sys::G_PRIORITY_DEFAULT,
|
2019-02-10 09:43:55 +00:00
|
|
|
Some(trampoline_watch::<F>),
|
|
|
|
into_raw_watch(func),
|
|
|
|
Some(destroy_closure_watch::<F>),
|
2019-02-15 11:30:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if res == 0 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(from_glib(res))
|
|
|
|
}
|
2019-02-10 09:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 07:28:42 +00:00
|
|
|
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 {
|
2019-03-19 07:58:20 +00:00
|
|
|
gst_sys::gst_bus_set_sync_handler(
|
2017-07-12 07:28:42 +00:00
|
|
|
self.to_glib_none().0,
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(trampoline_sync::<F>),
|
2017-07-12 07:28:42 +00:00
|
|
|
into_raw_sync(func),
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(destroy_closure_sync::<F>),
|
2017-07-12 07:28:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 17:52:29 +00:00
|
|
|
|
|
|
|
pub fn unset_sync_handler(&self) {
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe {
|
|
|
|
gst_sys::gst_bus_set_sync_handler(self.to_glib_none().0, None, ptr::null_mut(), None)
|
|
|
|
}
|
2017-08-01 17:52:29 +00:00
|
|
|
}
|
2018-12-27 22:06:03 +00:00
|
|
|
|
|
|
|
pub fn iter(&self) -> Iter {
|
|
|
|
self.iter_timed(0.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter_timed(&self, timeout: ::ClockTime) -> Iter {
|
|
|
|
Iter { bus: self, timeout }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 15:43:29 +00:00
|
|
|
#[derive(Debug)]
|
2018-12-27 22:06:03 +00:00
|
|
|
pub struct Iter<'a> {
|
|
|
|
bus: &'a Bus,
|
|
|
|
timeout: ::ClockTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for Iter<'a> {
|
|
|
|
type Item = Message;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Message> {
|
|
|
|
self.bus.timed_pop(self.timeout)
|
|
|
|
}
|
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 {
|
2018-04-01 08:30:03 +00:00
|
|
|
use super::*;
|
2018-04-23 14:55:31 +00:00
|
|
|
use futures_core::stream::Stream;
|
|
|
|
use futures_core::task::{Context, Waker};
|
|
|
|
use futures_core::{Async, Poll};
|
2018-04-01 08:30:03 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2017-11-12 12:33:02 +00:00
|
|
|
|
2019-01-22 15:43:29 +00:00
|
|
|
#[derive(Debug)]
|
2018-04-23 14:55:31 +00:00
|
|
|
pub struct BusStream(Bus, Arc<Mutex<Option<Waker>>>);
|
2017-11-12 12:33:02 +00:00
|
|
|
|
|
|
|
impl BusStream {
|
|
|
|
pub fn new(bus: &Bus) -> Self {
|
|
|
|
skip_assert_initialized!();
|
2018-04-23 14:55:31 +00:00
|
|
|
let waker = Arc::new(Mutex::new(None));
|
|
|
|
let waker_clone = Arc::clone(&waker);
|
2017-11-12 12:33:02 +00:00
|
|
|
|
|
|
|
bus.set_sync_handler(move |_, _| {
|
2018-04-23 14:55:31 +00:00
|
|
|
let mut waker = waker_clone.lock().unwrap();
|
|
|
|
if let Some(waker) = waker.take() {
|
2017-11-12 12:33:02 +00:00
|
|
|
// FIXME: Force type...
|
2018-04-23 14:55:31 +00:00
|
|
|
let waker: Waker = waker;
|
|
|
|
waker.wake();
|
2017-11-12 12:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BusSyncReply::Pass
|
|
|
|
});
|
|
|
|
|
2018-04-23 14:55:31 +00:00
|
|
|
BusStream(bus.clone(), waker)
|
2017-11-12 12:33:02 +00:00
|
|
|
}
|
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
|
|
|
|
2018-04-23 14:55:31 +00:00
|
|
|
fn poll_next(&mut self, ctx: &mut Context) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
let BusStream(ref bus, ref waker) = *self;
|
2017-08-17 10:07:32 +00:00
|
|
|
|
2018-04-23 14:55:31 +00:00
|
|
|
let msg = bus.pop();
|
2017-11-12 12:33:02 +00:00
|
|
|
if let Some(msg) = msg {
|
|
|
|
Ok(Async::Ready(Some(msg)))
|
|
|
|
} else {
|
2018-04-23 14:55:31 +00:00
|
|
|
let mut waker = waker.lock().unwrap();
|
|
|
|
*waker = Some(ctx.waker().clone());
|
|
|
|
Ok(Async::Pending)
|
2017-11-12 12:33:02 +00:00
|
|
|
}
|
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;
|
2019-01-29 14:14:06 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sync_handler() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let bus = Bus::new();
|
|
|
|
let msgs = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
let msgs_clone = msgs.clone();
|
|
|
|
bus.set_sync_handler(move |_, msg| {
|
|
|
|
msgs_clone.lock().unwrap().push(msg.clone());
|
|
|
|
BusSyncReply::Pass
|
|
|
|
});
|
|
|
|
|
|
|
|
bus.post(&::Message::new_eos().build()).unwrap();
|
|
|
|
|
|
|
|
let msgs = msgs.lock().unwrap();
|
|
|
|
assert_eq!(msgs.len(), 1);
|
|
|
|
match msgs[0].view() {
|
|
|
|
::MessageView::Eos(_) => (),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|