2018-11-05 10:10:14 +00:00
|
|
|
// This example demonstrates how to use GStreamer's query functionality.
|
|
|
|
// These are a way to query information from either elements or pads.
|
|
|
|
// Such information could for example be the current position within
|
|
|
|
// the stream (i.e. the playing time). Queries can traverse the pipeline
|
|
|
|
// (both up and downstream). This functionality is essential, since most
|
|
|
|
// queries can only answered by specific elements in a pipeline (such as the
|
|
|
|
// stream's duration, which often can only be answered by the demuxer).
|
|
|
|
// Since gstreamer has many elements that itself contain other elements that
|
|
|
|
// we don't know of, we can simply send a query for the duration into the
|
|
|
|
// pipeline and the query is passed along until an element feels capable
|
|
|
|
// of answering.
|
|
|
|
// For convenience, the API has a set of pre-defined queries, but also
|
|
|
|
// allows custom queries (which can be defined and used by your own elements).
|
|
|
|
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::prelude::*;
|
2017-07-29 14:33:26 +00:00
|
|
|
|
2019-06-03 15:42:00 +00:00
|
|
|
use std::convert::TryInto;
|
2017-07-29 14:33:26 +00:00
|
|
|
use std::env;
|
|
|
|
|
2017-11-12 18:07:02 +00:00
|
|
|
#[path = "../examples-common.rs"]
|
|
|
|
mod examples_common;
|
|
|
|
|
|
|
|
fn example_main() {
|
2018-11-05 10:10:14 +00:00
|
|
|
// Get a string containing the passed pipeline launch syntax
|
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
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Let GStreamer create a pipeline from the parsed launch syntax on the cli.
|
2017-07-29 14:33:26 +00:00
|
|
|
let pipeline = gst::parse_launch(&pipeline_str).unwrap();
|
2021-04-11 19:39:50 +00:00
|
|
|
let bus = pipeline.bus().unwrap();
|
2017-07-29 14:33:26 +00:00
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline
|
|
|
|
.set_state(gst::State::Playing)
|
|
|
|
.expect("Unable to set the pipeline to the `Playing` state");
|
2017-07-29 14:33:26 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Need to move a new reference into the closure.
|
|
|
|
// !!ATTENTION!!:
|
|
|
|
// It might seem appealing to use pipeline.clone() here, because that greatly
|
|
|
|
// simplifies the code within the callback. What this actually dose, however, is creating
|
|
|
|
// a memory leak. The clone of a pipeline is a new strong reference on the pipeline.
|
|
|
|
// Storing this strong reference of the pipeline within the callback (we are moving it in!),
|
|
|
|
// which is in turn stored in another strong reference on the pipeline is creating a
|
|
|
|
// reference cycle.
|
|
|
|
// DO NOT USE pipeline.clone() TO USE THE PIPELINE WITHIN A CALLBACK
|
2018-07-27 10:07:24 +00:00
|
|
|
let pipeline_weak = pipeline.downgrade();
|
2018-11-05 10:10:14 +00:00
|
|
|
// Add a timeout to the main loop. This closure will be executed
|
|
|
|
// in an interval of 1 second.
|
2018-07-27 10:07:24 +00:00
|
|
|
let timeout_id = glib::timeout_add_seconds(1, move || {
|
2018-11-05 10:10:14 +00:00
|
|
|
// Here we temporarily retrieve a strong reference on the pipeline from the weak one
|
|
|
|
// we moved into this callback.
|
2018-07-27 10:07:24 +00:00
|
|
|
let pipeline = match pipeline_weak.upgrade() {
|
|
|
|
Some(pipeline) => pipeline,
|
|
|
|
None => return glib::Continue(true),
|
|
|
|
};
|
2017-07-29 14:33:26 +00:00
|
|
|
|
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);
|
2021-04-28 22:29:13 +00:00
|
|
|
let pos: Option<gst::ClockTime> = {
|
2018-11-05 10:10:14 +00:00
|
|
|
// Create a new position query and send it to the pipeline.
|
|
|
|
// This will traverse all elements in the pipeline, until one feels
|
|
|
|
// capable of answering the query.
|
2020-06-23 17:24:17 +00:00
|
|
|
let mut q = gst::query::Position::new(gst::Format::Time);
|
2018-02-15 14:05:51 +00:00
|
|
|
if pipeline.query(&mut q) {
|
2021-04-11 19:39:50 +00:00
|
|
|
Some(q.result())
|
2017-11-11 10:21:55 +00:00
|
|
|
} else {
|
|
|
|
None
|
2017-07-29 14:33:26 +00:00
|
|
|
}
|
2018-10-08 12:02:23 +00:00
|
|
|
}
|
2019-06-03 15:42:00 +00:00
|
|
|
.and_then(|pos| pos.try_into().ok())
|
2018-10-08 06:32:08 +00:00
|
|
|
.unwrap();
|
2017-07-29 14:33:26 +00:00
|
|
|
|
2021-04-28 22:29:13 +00:00
|
|
|
let dur: Option<gst::ClockTime> = {
|
2018-11-05 10:10:14 +00:00
|
|
|
// Create a new duration query and send it to the pipeline.
|
|
|
|
// This will traverse all elements in the pipeline, until one feels
|
|
|
|
// capable of answering the query.
|
2020-06-23 17:24:17 +00:00
|
|
|
let mut q = gst::query::Duration::new(gst::Format::Time);
|
2018-02-15 14:05:51 +00:00
|
|
|
if pipeline.query(&mut q) {
|
2021-04-11 19:39:50 +00:00
|
|
|
Some(q.result())
|
2017-11-11 10:21:55 +00:00
|
|
|
} else {
|
|
|
|
None
|
2017-07-29 14:33:26 +00:00
|
|
|
}
|
2018-10-08 12:02:23 +00:00
|
|
|
}
|
2019-06-03 15:42:00 +00:00
|
|
|
.and_then(|dur| dur.try_into().ok())
|
2018-10-08 06:32:08 +00:00
|
|
|
.unwrap();
|
2017-07-29 14:33:26 +00:00
|
|
|
|
2021-04-28 22:29:13 +00:00
|
|
|
println!("{} / {}", pos.display(), dur.display());
|
2017-07-29 14:33:26 +00:00
|
|
|
|
|
|
|
glib::Continue(true)
|
|
|
|
});
|
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Need to move a new reference into the closure.
|
|
|
|
let main_loop_clone = main_loop.clone();
|
2017-07-29 14:33:26 +00:00
|
|
|
//bus.add_signal_watch();
|
2021-02-28 16:32:44 +00:00
|
|
|
//bus.connect_message(None, move |_, msg| {
|
2017-07-29 14:33:26 +00:00
|
|
|
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 {:?}: {} ({:?})",
|
2021-04-11 19:39:50 +00:00
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error(),
|
|
|
|
err.debug()
|
2017-07-29 14:33:26 +00:00
|
|
|
);
|
|
|
|
main_loop.quit();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
glib::Continue(true)
|
2019-12-17 19:00:42 +00:00
|
|
|
})
|
|
|
|
.expect("Failed to add bus watch");
|
2017-07-29 14:33:26 +00:00
|
|
|
|
|
|
|
main_loop.run();
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline
|
|
|
|
.set_state(gst::State::Null)
|
|
|
|
.expect("Unable to set the pipeline to the `Null` state");
|
2018-07-27 10:07:24 +00:00
|
|
|
|
2019-01-16 20:23:56 +00:00
|
|
|
bus.remove_watch().unwrap();
|
2021-10-12 06:30:10 +00:00
|
|
|
timeout_id.remove();
|
2017-07-29 14:33:26 +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);
|
|
|
|
}
|