Add Element::add_many(), ::remove_many(), Bin::link_many(), ::unlink_many()

This commit is contained in:
Sebastian Dröge 2017-07-05 11:09:49 +03:00
parent e3902bbbf6
commit 291f329fc3
4 changed files with 90 additions and 26 deletions

View file

@ -30,10 +30,8 @@ fn main() {
gobject_ffi::g_object_set_property(src.to_glib_none().0, "location".to_glib_none().0, uri.to_glib_none().0);
}
// TODO: Add Bin::add_many(), Element::link_many()
pipeline.add(&src).unwrap();
pipeline.add(&decodebin).unwrap();
src.link(&decodebin).unwrap();
pipeline.add_many(&[&src, &decodebin]).unwrap();
gst::Element::link_many(&[&src, &decodebin]).unwrap();
// Need to move a new reference into the closure
let pipeline_clone = pipeline.clone();
@ -55,19 +53,14 @@ fn main() {
let convert = gst::ElementFactory::make("audioconvert", None).unwrap();
let resample = gst::ElementFactory::make("audioresample", None).unwrap();
let sink = gst::ElementFactory::make("autoaudiosink", None).unwrap();
pipeline.add(&queue).unwrap();
pipeline.add(&convert).unwrap();
pipeline.add(&resample).unwrap();
pipeline.add(&sink).unwrap();
queue.link(&convert).unwrap();
convert.link(&resample).unwrap();
resample.link(&sink).unwrap();
let elements = &[&queue, &convert, &resample, &sink];
pipeline.add_many(elements).unwrap();
gst::Element::link_many(elements).unwrap();
queue.sync_state_with_parent().unwrap();
convert.sync_state_with_parent().unwrap();
resample.sync_state_with_parent().unwrap();
sink.sync_state_with_parent().unwrap();
for e in elements {
e.sync_state_with_parent().unwrap();
}
let sink_pad = queue.get_static_pad("sink").unwrap();
assert_eq!(src_pad.link(&sink_pad), gst::PadLinkReturn::Ok);
@ -76,19 +69,14 @@ fn main() {
let convert = gst::ElementFactory::make("videoconvert", None).unwrap();
let scale = gst::ElementFactory::make("videoscale", None).unwrap();
let sink = gst::ElementFactory::make("autovideosink", None).unwrap();
pipeline.add(&queue).unwrap();
pipeline.add(&convert).unwrap();
pipeline.add(&scale).unwrap();
pipeline.add(&sink).unwrap();
queue.link(&convert).unwrap();
convert.link(&scale).unwrap();
scale.link(&sink).unwrap();
let elements = &[&queue, &convert, &scale, &sink];
pipeline.add_many(elements).unwrap();
gst::Element::link_many(elements).unwrap();
queue.sync_state_with_parent().unwrap();
convert.sync_state_with_parent().unwrap();
scale.sync_state_with_parent().unwrap();
sink.sync_state_with_parent().unwrap();
for e in elements {
e.sync_state_with_parent().unwrap();
}
let sink_pad = queue.get_static_pad("sink").unwrap();
assert_eq!(src_pad.link(&sink_pad), gst::PadLinkReturn::Ok);

41
gstreamer/src/bin.rs Normal file
View file

@ -0,0 +1,41 @@
use Bin;
use Element;
use glib;
use glib::IsA;
use glib::translate::{ToGlibPtr, from_glib};
use ffi;
pub trait BinExtManual {
fn add_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError>;
fn remove_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError>;
}
impl<O: IsA<Bin>> BinExtManual for O {
fn add_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError> {
for e in elements {
unsafe {
let ret: bool = from_glib(ffi::gst_bin_add(self.to_glib_none().0, e.to_glib_none().0));
if !ret {
return Err(glib::BoolError("Failed to add elements"));
}
}
}
return Ok(());
}
fn remove_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError> {
for e in elements {
unsafe {
let ret: bool = from_glib(ffi::gst_bin_remove(self.to_glib_none().0, e.to_glib_none().0));
if !ret {
return Err(glib::BoolError("Failed to add elements"));
}
}
}
return Ok(());
}
}

30
gstreamer/src/element.rs Normal file
View file

@ -0,0 +1,30 @@
use Element;
use glib;
use glib::IsA;
use glib::translate::{ToGlibPtr, from_glib};
use ffi;
impl Element {
pub fn link_many<E: IsA<Element>>(elements: &[&E]) -> Result<(), glib::BoolError> {
for (e1, e2) in elements.iter().zip(elements.iter().skip(1)) {
unsafe {
let ret: bool = from_glib(ffi::gst_element_link(e1.to_glib_none().0, e2.to_glib_none().0));
if !ret {
return Err(glib::BoolError("Failed to link elements"));
}
}
}
return Ok(());
}
pub fn unlink_many<E: IsA<Element>>(elements: &[&E]) {
for (e1, e2) in elements.iter().zip(elements.iter().skip(1)) {
unsafe {
ffi::gst_element_unlink(e1.to_glib_none().0, e2.to_glib_none().0);
}
}
}
}

View file

@ -31,6 +31,7 @@ pub use glib::{
mod auto;
pub use auto::*;
pub use auto::traits::*;
pub use auto::functions::{parse_launch, parse_bin_from_description};
pub mod miniobject;
@ -39,6 +40,10 @@ pub mod message;
pub use message::Message;
pub use message::MessageView;
mod element;
mod bin;
pub use bin::BinExtManual;
use std::ptr;
pub fn init() -> Result<(), glib::Error> {