diff --git a/examples/src/bin/futures.rs b/examples/src/bin/futures.rs index 17542a9c7..7249ad176 100644 --- a/examples/src/bin/futures.rs +++ b/examples/src/bin/futures.rs @@ -11,30 +11,6 @@ use gst::prelude::*; #[path = "../examples-common.rs"] mod examples_common; -async fn message_loop(bus: gst::Bus) { - let mut messages = bus.stream(); - - while let Some(msg) = messages.next().await { - use gst::MessageView; - - // Determine whether we want to quit: on EOS or error message - // we quit, otherwise simply continue. - match msg.view() { - MessageView::Eos(..) => break, - MessageView::Error(err) => { - println!( - "Error from {:?}: {} ({:?})", - err.src().map(|s| s.path_string()), - err.error(), - err.debug() - ); - break; - } - _ => (), - }; - } -} - fn example_main() { // Read the pipeline to launch from the commandline, using the launch syntax. let pipeline_str = env::args().collect::>()[1..].join(" "); @@ -53,7 +29,29 @@ fn example_main() { let mut pool = LocalPool::new(); // Run until our message loop finishes, e.g. EOS/error happens - pool.run_until(message_loop(bus)); + pool.run_until(async { + let mut messages = bus.stream(); + + while let Some(msg) = messages.next().await { + use gst::MessageView; + + // Determine whether we want to quit: on EOS or error message + // we quit, otherwise simply continue. + match msg.view() { + MessageView::Eos(..) => break, + MessageView::Error(err) => { + println!( + "Error from {:?}: {} ({:?})", + err.src().map(|s| s.path_string()), + err.error(), + err.debug() + ); + break; + } + _ => (), + }; + } + }); pipeline .set_state(gst::State::Null) diff --git a/examples/src/bin/glib-futures.rs b/examples/src/bin/glib-futures.rs index b3f915ef4..451dbc594 100644 --- a/examples/src/bin/glib-futures.rs +++ b/examples/src/bin/glib-futures.rs @@ -6,35 +6,10 @@ use gst::prelude::*; #[path = "../examples-common.rs"] mod examples_common; -async fn message_handler(loop_: glib::MainLoop, bus: gst::Bus) { - let mut messages = bus.stream(); - - while let Some(msg) = messages.next().await { - use gst::MessageView; - - // Determine whether we want to quit: on EOS or error message - // we quit, otherwise simply continue. - match msg.view() { - MessageView::Eos(..) => loop_.quit(), - MessageView::Error(err) => { - println!( - "Error from {:?}: {} ({:?})", - err.src().map(|s| s.path_string()), - err.error(), - err.debug() - ); - loop_.quit(); - } - _ => (), - } - } -} - fn example_main() { // Get the default main context and make it also the thread default, then create // a main loop for it let ctx = glib::MainContext::default(); - let loop_ = glib::MainLoop::new(Some(&ctx), false); // Read the pipeline to launch from the commandline, using the launch syntax. let pipeline_str = env::args().collect::>()[1..].join(" "); @@ -50,11 +25,29 @@ fn example_main() { .expect("Unable to set the pipeline to the `Playing` state"); // Spawn our message handling stream - ctx.spawn_local(message_handler(loop_.clone(), bus)); + ctx.block_on(async { + let mut messages = bus.stream(); - // And run until something is quitting the loop, i.e. an EOS - // or error message is received above - loop_.run(); + while let Some(msg) = messages.next().await { + use gst::MessageView; + + // Determine whether we want to quit: on EOS or error message + // we quit, otherwise simply continue. + match msg.view() { + MessageView::Eos(..) => break, + MessageView::Error(err) => { + println!( + "Error from {:?}: {} ({:?})", + err.src().map(|s| s.path_string()), + err.error(), + err.debug() + ); + break; + } + _ => (), + } + } + }); pipeline .set_state(gst::State::Null)