2017-07-07 11:47:28 +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.
|
|
|
|
|
2017-07-05 08:09:49 +00:00
|
|
|
use Bin;
|
|
|
|
use Element;
|
|
|
|
|
|
|
|
use glib;
|
2019-01-16 20:23:56 +00:00
|
|
|
use glib::object::Cast;
|
2019-01-16 13:23:53 +00:00
|
|
|
use glib::object::IsA;
|
2019-01-16 20:23:56 +00:00
|
|
|
use glib::signal::connect_raw;
|
|
|
|
use glib::signal::SignalHandlerId;
|
|
|
|
use glib::translate::*;
|
2019-01-16 11:32:58 +00:00
|
|
|
use glib::GString;
|
2017-07-05 08:09:49 +00:00
|
|
|
|
|
|
|
use ffi;
|
|
|
|
|
2019-01-16 20:23:56 +00:00
|
|
|
use std::boxed::Box as Box_;
|
|
|
|
use std::mem::transmute;
|
2018-11-11 07:33:12 +00:00
|
|
|
use std::path;
|
|
|
|
|
2018-12-08 09:22:42 +00:00
|
|
|
pub trait GstBinExtManual: 'static {
|
2017-07-05 08:09:49 +00:00
|
|
|
fn add_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError>;
|
|
|
|
fn remove_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError>;
|
2017-09-17 15:41:02 +00:00
|
|
|
|
2019-01-16 20:23:56 +00:00
|
|
|
fn connect_do_latency<F: Fn(&Self) -> Result<(), glib::BoolError> + Send + Sync + 'static>(
|
|
|
|
&self,
|
|
|
|
f: F,
|
|
|
|
) -> SignalHandlerId;
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
fn iterate_all_by_interface(&self, iface: glib::types::Type) -> ::Iterator<Element>;
|
|
|
|
fn iterate_elements(&self) -> ::Iterator<Element>;
|
|
|
|
fn iterate_recurse(&self) -> ::Iterator<Element>;
|
|
|
|
fn iterate_sinks(&self) -> ::Iterator<Element>;
|
|
|
|
fn iterate_sorted(&self) -> ::Iterator<Element>;
|
|
|
|
fn iterate_sources(&self) -> ::Iterator<Element>;
|
2017-10-25 10:56:32 +00:00
|
|
|
fn get_children(&self) -> Vec<Element>;
|
2018-11-11 07:33:12 +00:00
|
|
|
|
2018-12-09 16:06:12 +00:00
|
|
|
fn debug_to_dot_data(&self, details: ::DebugGraphDetails) -> GString;
|
2018-11-11 07:33:12 +00:00
|
|
|
fn debug_to_dot_file<Q: AsRef<path::Path>>(&self, details: ::DebugGraphDetails, file_name: Q);
|
|
|
|
fn debug_to_dot_file_with_ts<Q: AsRef<path::Path>>(
|
|
|
|
&self,
|
|
|
|
details: ::DebugGraphDetails,
|
|
|
|
file_name: Q,
|
|
|
|
);
|
2017-07-05 08:09:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 11:30:41 +00:00
|
|
|
impl<O: IsA<Bin>> GstBinExtManual for O {
|
2017-07-05 08:09:49 +00:00
|
|
|
fn add_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError> {
|
|
|
|
for e in elements {
|
|
|
|
unsafe {
|
2019-01-16 11:32:58 +00:00
|
|
|
let ret: bool = from_glib(ffi::gst_bin_add(
|
|
|
|
self.as_ref().to_glib_none().0,
|
|
|
|
e.as_ref().to_glib_none().0,
|
|
|
|
));
|
2017-07-05 08:09:49 +00:00
|
|
|
if !ret {
|
2019-01-04 12:15:58 +00:00
|
|
|
return Err(glib_bool_error!("Failed to add elements"));
|
2017-07-05 08:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:40:31 +00:00
|
|
|
Ok(())
|
2017-07-05 08:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError> {
|
|
|
|
for e in elements {
|
|
|
|
unsafe {
|
2017-07-10 21:33:24 +00:00
|
|
|
let ret: bool = from_glib(ffi::gst_bin_remove(
|
2019-01-16 11:32:58 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
|
|
|
e.as_ref().to_glib_none().0,
|
2017-07-10 21:33:24 +00:00
|
|
|
));
|
2017-07-05 08:09:49 +00:00
|
|
|
if !ret {
|
2019-01-16 20:23:56 +00:00
|
|
|
return Err(glib_bool_error!("Failed to remove elements"));
|
2017-07-05 08:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:40:31 +00:00
|
|
|
Ok(())
|
2017-07-05 08:09:49 +00:00
|
|
|
}
|
2017-09-17 15:41:02 +00:00
|
|
|
|
2019-01-16 20:23:56 +00:00
|
|
|
fn connect_do_latency<F: Fn(&Self) -> Result<(), glib::BoolError> + Send + Sync + 'static>(
|
|
|
|
&self,
|
|
|
|
f: F,
|
|
|
|
) -> SignalHandlerId {
|
|
|
|
unsafe {
|
|
|
|
let f: Box_<Box_<Fn(&Self) -> Result<(), glib::BoolError> + Send + Sync + 'static>> =
|
|
|
|
Box_::new(Box_::new(f));
|
|
|
|
connect_raw(
|
|
|
|
self.as_ptr() as *mut _,
|
|
|
|
b"do-latency\0".as_ptr() as *const _,
|
|
|
|
transmute(do_latency_trampoline::<Self> as usize),
|
|
|
|
Box_::into_raw(f) as *mut _,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
fn iterate_all_by_interface(&self, iface: glib::types::Type) -> ::Iterator<Element> {
|
|
|
|
unsafe {
|
2017-10-17 09:06:51 +00:00
|
|
|
from_glib_full(ffi::gst_bin_iterate_all_by_interface(
|
2019-01-16 11:32:58 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
2017-10-17 09:06:51 +00:00
|
|
|
iface.to_glib(),
|
|
|
|
))
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iterate_elements(&self) -> ::Iterator<Element> {
|
2019-01-16 11:32:58 +00:00
|
|
|
unsafe {
|
|
|
|
from_glib_full(ffi::gst_bin_iterate_elements(
|
|
|
|
self.as_ref().to_glib_none().0,
|
|
|
|
))
|
|
|
|
}
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn iterate_recurse(&self) -> ::Iterator<Element> {
|
2019-01-16 11:32:58 +00:00
|
|
|
unsafe { from_glib_full(ffi::gst_bin_iterate_recurse(self.as_ref().to_glib_none().0)) }
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn iterate_sinks(&self) -> ::Iterator<Element> {
|
2019-01-16 11:32:58 +00:00
|
|
|
unsafe { from_glib_full(ffi::gst_bin_iterate_sinks(self.as_ref().to_glib_none().0)) }
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn iterate_sorted(&self) -> ::Iterator<Element> {
|
2019-01-16 11:32:58 +00:00
|
|
|
unsafe { from_glib_full(ffi::gst_bin_iterate_sorted(self.as_ref().to_glib_none().0)) }
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn iterate_sources(&self) -> ::Iterator<Element> {
|
2019-01-16 11:32:58 +00:00
|
|
|
unsafe { from_glib_full(ffi::gst_bin_iterate_sources(self.as_ref().to_glib_none().0)) }
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
2017-10-25 10:56:32 +00:00
|
|
|
|
|
|
|
fn get_children(&self) -> Vec<Element> {
|
|
|
|
unsafe {
|
2019-01-16 11:32:58 +00:00
|
|
|
let bin: &ffi::GstBin = &*(self.as_ptr() as *const _);
|
2017-10-25 10:56:32 +00:00
|
|
|
::utils::MutexGuard::lock(&bin.element.object.lock);
|
|
|
|
FromGlibPtrContainer::from_glib_none(bin.children)
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 07:33:12 +00:00
|
|
|
|
2018-12-09 16:06:12 +00:00
|
|
|
fn debug_to_dot_data(&self, details: ::DebugGraphDetails) -> GString {
|
2018-11-11 07:33:12 +00:00
|
|
|
::debug_bin_to_dot_data(self, details)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn debug_to_dot_file<Q: AsRef<path::Path>>(&self, details: ::DebugGraphDetails, file_name: Q) {
|
|
|
|
::debug_bin_to_dot_file(self, details, file_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn debug_to_dot_file_with_ts<Q: AsRef<path::Path>>(
|
|
|
|
&self,
|
|
|
|
details: ::DebugGraphDetails,
|
|
|
|
file_name: Q,
|
|
|
|
) {
|
|
|
|
::debug_bin_to_dot_file_with_ts(self, details, file_name)
|
|
|
|
}
|
2017-07-05 08:09:49 +00:00
|
|
|
}
|
2017-10-26 11:29:37 +00:00
|
|
|
|
2019-01-16 20:23:56 +00:00
|
|
|
unsafe extern "C" fn do_latency_trampoline<P>(
|
|
|
|
this: *mut ffi::GstBin,
|
|
|
|
f: glib_ffi::gpointer,
|
|
|
|
) -> glib_ffi::gboolean
|
|
|
|
where
|
|
|
|
P: IsA<Bin>,
|
|
|
|
{
|
|
|
|
let f: &&(Fn(&P) -> Result<(), glib::BoolError> + Send + Sync + 'static) = transmute(f);
|
|
|
|
match f(&Bin::from_glib_borrow(this).unsafe_cast()) {
|
|
|
|
Ok(()) => true,
|
|
|
|
Err(err) => {
|
|
|
|
gst_error!(::CAT_RUST, obj: &Bin::from_glib_borrow(this), "{}", err);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.to_glib()
|
|
|
|
}
|
|
|
|
|
2017-10-26 11:29:37 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use prelude::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_children() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let bin = ::Bin::new(None);
|
|
|
|
bin.add(&::ElementFactory::make("identity", "identity0").unwrap())
|
|
|
|
.unwrap();
|
|
|
|
bin.add(&::ElementFactory::make("identity", "identity1").unwrap())
|
|
|
|
.unwrap();
|
|
|
|
|
2018-07-27 10:36:40 +00:00
|
|
|
let mut child_names = bin
|
|
|
|
.get_children()
|
2017-10-26 11:29:37 +00:00
|
|
|
.iter()
|
|
|
|
.map(|c| c.get_name())
|
2018-12-09 16:06:12 +00:00
|
|
|
.collect::<Vec<GString>>();
|
2017-10-26 11:29:37 +00:00
|
|
|
child_names.sort();
|
|
|
|
assert_eq!(
|
|
|
|
child_names,
|
|
|
|
vec![String::from("identity0"), String::from("identity1")]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|