examples: Simplify basic futures / async examples a bit

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

View file

@ -11,7 +11,25 @@ use gst::prelude::*;
#[path = "../examples-common.rs"]
mod examples_common;
async fn message_loop(bus: gst::Bus) {
fn example_main() {
// Read the pipeline to launch from the commandline, using the launch syntax.
let pipeline_str = env::args().collect::<Vec<String>>()[1..].join(" ");
gst::init().unwrap();
// Create a pipeline from the launch-syntax given on the cli.
let pipeline = gst::parse::launch(&pipeline_str).unwrap();
let bus = pipeline.bus().unwrap();
pipeline
.set_state(gst::State::Playing)
.expect("Unable to set the pipeline to the `Playing` state");
// Use a LocalPool as executor. This runs single threaded on this very thread.
let mut pool = LocalPool::new();
// Run until our message loop finishes, e.g. EOS/error happens
pool.run_until(async {
let mut messages = bus.stream();
while let Some(msg) = messages.next().await {
@ -33,27 +51,7 @@ async fn message_loop(bus: gst::Bus) {
_ => (),
};
}
}
fn example_main() {
// Read the pipeline to launch from the commandline, using the launch syntax.
let pipeline_str = env::args().collect::<Vec<String>>()[1..].join(" ");
gst::init().unwrap();
// Create a pipeline from the launch-syntax given on the cli.
let pipeline = gst::parse::launch(&pipeline_str).unwrap();
let bus = pipeline.bus().unwrap();
pipeline
.set_state(gst::State::Playing)
.expect("Unable to set the pipeline to the `Playing` state");
// Use a LocalPool as executor. This runs single threaded on this very thread.
let mut pool = LocalPool::new();
// Run until our message loop finishes, e.g. EOS/error happens
pool.run_until(message_loop(bus));
});
pipeline
.set_state(gst::State::Null)

View file

@ -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::<Vec<String>>()[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)