2017-07-29 14:33:26 +00:00
|
|
|
extern crate gstreamer as gst;
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::prelude::*;
|
2017-07-29 14:33:26 +00:00
|
|
|
|
|
|
|
extern crate glib;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
|
2017-11-12 18:07:02 +00:00
|
|
|
#[path = "../examples-common.rs"]
|
|
|
|
mod examples_common;
|
|
|
|
|
|
|
|
fn example_main() {
|
2017-07-29 14:33:26 +00:00
|
|
|
let pipeline_str = env::args().collect::<Vec<String>>()[1..].join(" ");
|
|
|
|
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2017-08-17 14:58:15 +00:00
|
|
|
let main_loop = glib::MainLoop::new(None, false);
|
2017-07-29 14:33:26 +00:00
|
|
|
|
|
|
|
let pipeline = gst::parse_launch(&pipeline_str).unwrap();
|
|
|
|
let bus = pipeline.get_bus().unwrap();
|
|
|
|
|
|
|
|
let ret = pipeline.set_state(gst::State::Playing);
|
|
|
|
assert_ne!(ret, gst::StateChangeReturn::Failure);
|
|
|
|
|
|
|
|
let main_loop_clone = main_loop.clone();
|
|
|
|
|
|
|
|
let pipeline_clone = pipeline.clone();
|
|
|
|
glib::timeout_add_seconds(1, move || {
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::QueryView;
|
|
|
|
|
2017-07-29 14:33:26 +00:00
|
|
|
let pipeline = &pipeline_clone;
|
|
|
|
|
2017-08-17 14:58:15 +00:00
|
|
|
//let pos = pipeline.query_position(gst::Format::Time).unwrap_or(-1);
|
|
|
|
//let dur = pipeline.query_duration(gst::Format::Time).unwrap_or(-1);
|
2017-07-29 14:33:26 +00:00
|
|
|
let pos = {
|
2017-08-17 14:58:15 +00:00
|
|
|
let mut q = gst::Query::new_position(gst::Format::Time);
|
2017-11-11 10:21:55 +00:00
|
|
|
if pipeline.query(q.get_mut().unwrap()) {
|
|
|
|
match q.view() {
|
2017-11-11 11:22:31 +00:00
|
|
|
QueryView::Position(ref p) => Some(p.get_result()),
|
2017-11-11 10:21:55 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2017-07-29 14:33:26 +00:00
|
|
|
}
|
2017-12-09 16:20:21 +00:00
|
|
|
}.and_then(|pos| pos.try_into_time().ok())
|
2017-11-11 10:21:55 +00:00
|
|
|
.unwrap();
|
2017-07-29 14:33:26 +00:00
|
|
|
|
|
|
|
let dur = {
|
2017-08-17 14:58:15 +00:00
|
|
|
let mut q = gst::Query::new_duration(gst::Format::Time);
|
2017-11-11 10:21:55 +00:00
|
|
|
if pipeline.query(q.get_mut().unwrap()) {
|
|
|
|
match q.view() {
|
2017-11-11 11:22:31 +00:00
|
|
|
QueryView::Duration(ref p) => Some(p.get_result()),
|
2017-11-11 10:21:55 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2017-07-29 14:33:26 +00:00
|
|
|
}
|
2017-12-09 16:20:21 +00:00
|
|
|
}.and_then(|dur| dur.try_into_time().ok())
|
2017-11-11 10:21:55 +00:00
|
|
|
.unwrap();
|
2017-07-29 14:33:26 +00:00
|
|
|
|
|
|
|
println!("{} / {}", pos, dur);
|
|
|
|
|
|
|
|
glib::Continue(true)
|
|
|
|
});
|
|
|
|
|
|
|
|
//bus.add_signal_watch();
|
|
|
|
//bus.connect_message(move |_, msg| {
|
|
|
|
bus.add_watch(move |_, msg| {
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::MessageView;
|
|
|
|
|
2017-07-29 14:33:26 +00:00
|
|
|
let main_loop = &main_loop_clone;
|
|
|
|
match msg.view() {
|
2017-07-30 14:06:44 +00:00
|
|
|
MessageView::Eos(..) => main_loop.quit(),
|
2017-07-29 14:33:26 +00:00
|
|
|
MessageView::Error(err) => {
|
|
|
|
println!(
|
2017-11-16 11:58:56 +00:00
|
|
|
"Error from {:?}: {} ({:?})",
|
|
|
|
msg.get_src().map(|s| s.get_path_string()),
|
2017-07-29 14:33:26 +00:00
|
|
|
err.get_error(),
|
|
|
|
err.get_debug()
|
|
|
|
);
|
|
|
|
main_loop.quit();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
glib::Continue(true)
|
|
|
|
});
|
|
|
|
|
|
|
|
main_loop.run();
|
|
|
|
|
|
|
|
let ret = pipeline.set_state(gst::State::Null);
|
|
|
|
assert_ne!(ret, gst::StateChangeReturn::Failure);
|
|
|
|
}
|
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);
|
|
|
|
}
|