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

79 lines
2.8 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
extern crate gstreamer as gst;
use gst::prelude::*;
2017-07-31 13:12:08 +00:00
extern crate futures;
use futures::executor::block_on;
use futures::prelude::*;
2017-07-31 13:12:08 +00:00
use std::env;
2017-11-12 18:07:02 +00:00
#[path = "../examples-common.rs"]
mod examples_common;
fn example_main() {
// Read the pipeline to launch from the commandline, using the launch syntax.
2017-07-31 13:12:08 +00:00
let pipeline_str = env::args().collect::<Vec<String>>()[1..].join(" ");
gst::init().unwrap();
// Create a pipeline from the launch-syntax given on the cli.
2017-07-31 13:12:08 +00:00
let pipeline = gst::parse_launch(&pipeline_str).unwrap();
let bus = pipeline.get_bus().unwrap();
pipeline
.set_state(gst::State::Playing)
.expect("Unable to set the pipeline to the `Playing` state");
2017-07-31 13:12:08 +00:00
// BusStream implements the Stream trait, but Stream::for_each is
// calling a closure for each item and returns a Future that resolves
// when the stream is done or an error has happened
let messages = gst::BusStream::new(&bus)
.for_each(|msg| {
use gst::MessageView;
// Determine whether we want to resolve the future, or we still have
// to wait. The future is resolved when either an error occurs, or the
// pipeline succeeded execution (got an EOS event).
let quit = match msg.view() {
MessageView::Eos(..) => true,
MessageView::Error(err) => {
println!(
"Error from {:?}: {} ({:?})",
err.get_src().map(|s| s.get_path_string()),
err.get_error(),
err.get_debug()
);
true
}
_ => false,
};
if quit {
Err(()) // This resolves the future that is returned by for_each
// FIXME: At the moment, EOS messages also result in the future to be resolved
// by an error. This should probably be changed in the future.
} else {
Ok(()) // Continue - do not resolve the future yet.
2017-07-31 13:12:08 +00:00
}
2018-10-08 12:02:23 +00:00
})
.and_then(|_| Ok(()));
2017-07-31 13:12:08 +00:00
// Synchronously wait on the future we created above.
let _ = block_on(messages);
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() {
// tutorials_common::run is only required to set up the application environent on macOS
// (but not necessary in normal Cocoa applications where this is set up autmatically)
examples_common::run(example_main);
}