examples: Simplify basic futures / async examples a bit

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1724>
This commit is contained in:
Sebastian Dröge 2025-04-29 21:32:22 +03:00
parent 60e4b22202
commit e320b9fefb
2 changed files with 45 additions and 54 deletions

View file

@ -11,30 +11,6 @@ use gst::prelude::*;
#[path = "../examples-common.rs"] #[path = "../examples-common.rs"]
mod examples_common; 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() { fn example_main() {
// Read the pipeline to launch from the commandline, using the launch syntax. // Read the pipeline to launch from the commandline, using the launch syntax.
let pipeline_str = env::args().collect::<Vec<String>>()[1..].join(" "); let pipeline_str = env::args().collect::<Vec<String>>()[1..].join(" ");
@ -53,7 +29,29 @@ fn example_main() {
let mut pool = LocalPool::new(); let mut pool = LocalPool::new();
// Run until our message loop finishes, e.g. EOS/error happens // 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 pipeline
.set_state(gst::State::Null) .set_state(gst::State::Null)

View file

@ -6,35 +6,10 @@ use gst::prelude::*;
#[path = "../examples-common.rs"] #[path = "../examples-common.rs"]
mod examples_common; 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() { fn example_main() {
// Get the default main context and make it also the thread default, then create // Get the default main context and make it also the thread default, then create
// a main loop for it // a main loop for it
let ctx = glib::MainContext::default(); 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. // Read the pipeline to launch from the commandline, using the launch syntax.
let pipeline_str = env::args().collect::<Vec<String>>()[1..].join(" "); let pipeline_str = env::args().collect::<Vec<String>>()[1..].join(" ");
@ -50,11 +25,29 @@ fn example_main() {
.expect("Unable to set the pipeline to the `Playing` state"); .expect("Unable to set the pipeline to the `Playing` state");
// Spawn our message handling stream // 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 while let Some(msg) = messages.next().await {
// or error message is received above use gst::MessageView;
loop_.run();
// 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 pipeline
.set_state(gst::State::Null) .set_state(gst::State::Null)