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

80 lines
2.4 KiB
Rust
Raw Normal View History

extern crate gstreamer as gst;
use gst::prelude::*;
extern crate glib;
extern crate futures;
use futures::future;
use futures::prelude::*;
use std::env;
#[path = "../examples-common.rs"]
mod examples_common;
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();
ctx.push_thread_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(" ");
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.get_bus().unwrap();
pipeline
.set_state(gst::State::Playing)
.expect("Unable to set the pipeline to the `Playing` state");
// BusStream implements the Stream trait. Stream::for_each is calling a closure for each item
// and returns a Future that resolves when the stream is done
let loop_clone = loop_.clone();
let messages = gst::BusStream::new(&bus).for_each(move |msg| {
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_clone.quit(),
MessageView::Error(err) => {
println!(
"Error from {:?}: {} ({:?})",
err.get_src().map(|s| s.get_path_string()),
err.get_error(),
err.get_debug()
);
loop_clone.quit();
}
_ => (),
}
// New future to resolve for each message: nothing here
future::ready(())
});
// Spawn our message handling stream
ctx.spawn_local(messages);
// And run until something is quitting the loop, i.e. an EOS
// or error message is received above
loop_.run();
pipeline
.set_state(gst::State::Null)
.expect("Unable to set the pipeline to the `Null` state");
ctx.pop_thread_default();
}
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);
}