From 69af6a5975f7862bee7dd06399643c2b2cf22d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 28 Dec 2018 00:06:03 +0200 Subject: [PATCH] bus: Add iter() and iter_timed() that return Iterators around the corresponding pop() functions And make use of them in the examples where it makes sense. --- examples/src/bin/appsink.rs | 2 +- examples/src/bin/appsrc.rs | 2 +- examples/src/bin/decodebin.rs | 2 +- examples/src/bin/encodebin.rs | 2 +- examples/src/bin/ges.rs | 2 +- examples/src/bin/glupload.rs | 2 +- examples/src/bin/launch.rs | 2 +- examples/src/bin/pad_probes.rs | 2 +- examples/src/bin/pango-cairo.rs | 2 +- examples/src/bin/playbin.rs | 2 +- examples/src/bin/rtpfecclient.rs | 2 +- examples/src/bin/rtpfecserver.rs | 2 +- examples/src/bin/tagsetter.rs | 2 +- examples/src/bin/toc.rs | 2 +- examples/src/bin/transmux.rs | 2 +- gstreamer/src/bus.rs | 21 +++++++++++++++++++++ tutorials/src/bin/basic-tutorial-1.rs | 2 +- tutorials/src/bin/basic-tutorial-2.rs | 2 +- tutorials/src/bin/basic-tutorial-3.rs | 2 +- tutorials/src/bin/basic-tutorial-6.rs | 2 +- tutorials/src/bin/basic-tutorial-7.rs | 2 +- 21 files changed, 41 insertions(+), 20 deletions(-) diff --git a/examples/src/bin/appsink.rs b/examples/src/bin/appsink.rs index 132690474..7373f6f01 100644 --- a/examples/src/bin/appsink.rs +++ b/examples/src/bin/appsink.rs @@ -165,7 +165,7 @@ fn main_loop(pipeline: gst::Pipeline) -> Result<(), Error> { .get_bus() .expect("Pipeline without bus. Shouldn't happen!"); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/appsrc.rs b/examples/src/bin/appsrc.rs index 7ab65d4e5..fb6175542 100644 --- a/examples/src/bin/appsrc.rs +++ b/examples/src/bin/appsrc.rs @@ -147,7 +147,7 @@ fn main_loop(pipeline: gst::Pipeline) -> Result<(), Error> { .get_bus() .expect("Pipeline without bus. Shouldn't happen!"); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/decodebin.rs b/examples/src/bin/decodebin.rs index cd8a44a0e..a14efc828 100644 --- a/examples/src/bin/decodebin.rs +++ b/examples/src/bin/decodebin.rs @@ -253,7 +253,7 @@ fn example_main() -> Result<(), Error> { // In the callback ("pad-added" on the decodebin), we sent better error information // using a bus message. This is the position where we get those messages and log // the contained information. - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/encodebin.rs b/examples/src/bin/encodebin.rs index 8e5fb417d..f22a56474 100644 --- a/examples/src/bin/encodebin.rs +++ b/examples/src/bin/encodebin.rs @@ -286,7 +286,7 @@ fn example_main() -> Result<(), Error> { .get_bus() .expect("Pipeline without bus. Shouldn't happen!"); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/ges.rs b/examples/src/bin/ges.rs index 697f203cb..49b3c16a0 100644 --- a/examples/src/bin/ges.rs +++ b/examples/src/bin/ges.rs @@ -104,7 +104,7 @@ fn main_loop(uri: &str) -> Result<(), glib::BoolError> { assert_ne!(ret, gst::StateChangeReturn::Failure); let bus = pipeline.get_bus().unwrap(); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/glupload.rs b/examples/src/bin/glupload.rs index 52247701e..c6251e1ce 100644 --- a/examples/src/bin/glupload.rs +++ b/examples/src/bin/glupload.rs @@ -502,7 +502,7 @@ impl App { fn gst_loop(bus: gst::Bus) -> Result<(), Error> { use gst::MessageView; - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { match msg.view() { MessageView::Eos(..) => break, MessageView::Error(err) => { diff --git a/examples/src/bin/launch.rs b/examples/src/bin/launch.rs index f7acbd124..ffa71d794 100644 --- a/examples/src/bin/launch.rs +++ b/examples/src/bin/launch.rs @@ -46,7 +46,7 @@ fn example_main() { let ret = pipeline.set_state(gst::State::Playing); assert_ne!(ret, gst::StateChangeReturn::Failure); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/pad_probes.rs b/examples/src/bin/pad_probes.rs index d690287f1..bcea609e1 100644 --- a/examples/src/bin/pad_probes.rs +++ b/examples/src/bin/pad_probes.rs @@ -79,7 +79,7 @@ fn example_main() { assert_ne!(ret, gst::StateChangeReturn::Failure); let bus = pipeline.get_bus().unwrap(); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/pango-cairo.rs b/examples/src/bin/pango-cairo.rs index cd648fc22..e733fc37c 100644 --- a/examples/src/bin/pango-cairo.rs +++ b/examples/src/bin/pango-cairo.rs @@ -241,7 +241,7 @@ fn main_loop(pipeline: gst::Pipeline) -> Result<(), Error> { .get_bus() .expect("Pipeline without bus. Shouldn't happen!"); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/playbin.rs b/examples/src/bin/playbin.rs index d52cb53ca..3e5944663 100644 --- a/examples/src/bin/playbin.rs +++ b/examples/src/bin/playbin.rs @@ -106,7 +106,7 @@ fn example_main() { let ret = playbin.set_state(gst::State::Playing); assert_ne!(ret, gst::StateChangeReturn::Failure); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/rtpfecclient.rs b/examples/src/bin/rtpfecclient.rs index 1ea881fba..59a7aa724 100644 --- a/examples/src/bin/rtpfecclient.rs +++ b/examples/src/bin/rtpfecclient.rs @@ -249,7 +249,7 @@ fn example_main() -> Result<(), Error> { let ret = pipeline.set_state(gst::State::Playing); assert_ne!(ret, gst::StateChangeReturn::Failure); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/rtpfecserver.rs b/examples/src/bin/rtpfecserver.rs index 1122427f6..decef4421 100644 --- a/examples/src/bin/rtpfecserver.rs +++ b/examples/src/bin/rtpfecserver.rs @@ -180,7 +180,7 @@ fn example_main() -> Result<(), Error> { let ret = pipeline.set_state(gst::State::Playing); assert_ne!(ret, gst::StateChangeReturn::Failure); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/tagsetter.rs b/examples/src/bin/tagsetter.rs index f6727516a..aa8791eeb 100644 --- a/examples/src/bin/tagsetter.rs +++ b/examples/src/bin/tagsetter.rs @@ -96,7 +96,7 @@ fn example_main() -> Result<(), Error> { pipeline.set_state(gst::State::Playing).into_result()?; - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/toc.rs b/examples/src/bin/toc.rs index faf3d6d63..087e953fe 100644 --- a/examples/src/bin/toc.rs +++ b/examples/src/bin/toc.rs @@ -90,7 +90,7 @@ fn example_main() { // functionality like timeouts or GLib socket notifications, so this is sufficient. // The bus is manually operated by repeatedly calling timed_pop on the bus with // the desired timeout for when to stop waiting for new messages. (None = Wait forever) - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/examples/src/bin/transmux.rs b/examples/src/bin/transmux.rs index 5dee982cf..4bcd0debb 100644 --- a/examples/src/bin/transmux.rs +++ b/examples/src/bin/transmux.rs @@ -159,7 +159,7 @@ fn example_main() -> Result<(), Error> { .get_bus() .expect("Pipeline without bus. Shouldn't happen!"); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/gstreamer/src/bus.rs b/gstreamer/src/bus.rs index bf70d005b..b24008320 100644 --- a/gstreamer/src/bus.rs +++ b/gstreamer/src/bus.rs @@ -140,6 +140,27 @@ impl Bus { pub fn unset_sync_handler(&self) { unsafe { ffi::gst_bus_set_sync_handler(self.to_glib_none().0, None, ptr::null_mut(), None) } } + + pub fn iter(&self) -> Iter { + self.iter_timed(0.into()) + } + + pub fn iter_timed(&self, timeout: ::ClockTime) -> Iter { + Iter { bus: self, timeout } + } +} + +pub struct Iter<'a> { + bus: &'a Bus, + timeout: ::ClockTime, +} + +impl<'a> Iterator for Iter<'a> { + type Item = Message; + + fn next(&mut self) -> Option { + self.bus.timed_pop(self.timeout) + } } #[cfg(any(feature = "futures", feature = "dox"))] diff --git a/tutorials/src/bin/basic-tutorial-1.rs b/tutorials/src/bin/basic-tutorial-1.rs index 2bbee8999..7f27856de 100644 --- a/tutorials/src/bin/basic-tutorial-1.rs +++ b/tutorials/src/bin/basic-tutorial-1.rs @@ -19,7 +19,7 @@ fn tutorial_main() { // Wait until error or EOS let bus = pipeline.get_bus().unwrap(); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { diff --git a/tutorials/src/bin/basic-tutorial-2.rs b/tutorials/src/bin/basic-tutorial-2.rs index 11637b375..f15560a23 100644 --- a/tutorials/src/bin/basic-tutorial-2.rs +++ b/tutorials/src/bin/basic-tutorial-2.rs @@ -34,7 +34,7 @@ fn tutorial_main() { // Wait until error or EOS let bus = pipeline.get_bus().unwrap(); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { MessageView::Error(err) => { diff --git a/tutorials/src/bin/basic-tutorial-3.rs b/tutorials/src/bin/basic-tutorial-3.rs index 3e99f2f16..21e655a4c 100644 --- a/tutorials/src/bin/basic-tutorial-3.rs +++ b/tutorials/src/bin/basic-tutorial-3.rs @@ -94,7 +94,7 @@ fn tutorial_main() { // Wait until error or EOS let bus = pipeline.get_bus().unwrap(); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { MessageView::Error(err) => { diff --git a/tutorials/src/bin/basic-tutorial-6.rs b/tutorials/src/bin/basic-tutorial-6.rs index 838b785d2..14fd80ee7 100644 --- a/tutorials/src/bin/basic-tutorial-6.rs +++ b/tutorials/src/bin/basic-tutorial-6.rs @@ -121,7 +121,7 @@ fn tutorial_main() { // Wait until error, EOS or State Change let bus = pipeline.get_bus().unwrap(); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { match msg.view() { MessageView::Error(err) => { println!( diff --git a/tutorials/src/bin/basic-tutorial-7.rs b/tutorials/src/bin/basic-tutorial-7.rs index b7d7b44a7..0836b1292 100644 --- a/tutorials/src/bin/basic-tutorial-7.rs +++ b/tutorials/src/bin/basic-tutorial-7.rs @@ -68,7 +68,7 @@ fn tutorial_main() { .into_result() .expect("Unable to set the pipeline to the Playing state."); let bus = pipeline.get_bus().unwrap(); - while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) { + for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) { use gst::MessageView; match msg.view() { MessageView::Error(err) => {