gstreamer-rs/examples/src/bin/futures.rs

68 lines
2.1 KiB
Rust
Raw Normal View History

// This example demonstrates how to use the gstreamer crate in conjunction
// with the future trait. The example waits for either an error to occur,
// or for an EOS message. When a message notifying about either of both
// is received, the future is resolved.
2017-07-31 13:12:08 +00:00
use std::env;
use futures::{executor::LocalPool, prelude::*};
use gst::prelude::*;
2017-11-12 18:07:02 +00:00
#[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 {:?}: {} ({:?})",
2021-04-11 19:39:50 +00:00
err.src().map(|s| s.path_string()),
err.error(),
err.debug()
);
break;
2017-07-31 13:12:08 +00:00
}
_ => (),
};
}
}
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();
2021-04-11 19:39:50 +00:00
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();
2017-07-31 13:12:08 +00:00
// Run until our message loop finishes, e.g. EOS/error happens
pool.run_until(message_loop(bus));
2017-07-31 13:12:08 +00:00
pipeline
.set_state(gst::State::Null)
.expect("Unable to set the pipeline to the `Null` state");
2017-07-31 13:12:08 +00:00
}
2017-11-12 18:07:02 +00:00
fn main() {
2021-04-10 11:42:04 +00:00
// tutorials_common::run is only required to set up the application environment on macOS
// (but not necessary in normal Cocoa applications where this is set up automatically)
2017-11-12 18:07:02 +00:00
examples_common::run(example_main);
}