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

135 lines
5.1 KiB
Rust
Raw Normal View History

// 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).
use gst::prelude::*;
2017-07-29 14:33:26 +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() {
// 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();
let main_loop = glib::MainLoop::new(None, false);
2017-07-29 14:33:26 +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
pipeline
.set_state(gst::State::Playing)
.expect("Unable to set the pipeline to the `Playing` state");
2017-07-29 14:33:26 +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
let pipeline_weak = pipeline.downgrade();
// Add a timeout to the main loop. This closure will be executed
// in an interval of 1 second.
let timeout_id = glib::timeout_add_seconds(1, move || {
// Here we temporarily retrieve a strong reference on the pipeline from the weak one
// we moved into this callback.
let pipeline = match pipeline_weak.upgrade() {
Some(pipeline) => pipeline,
None => return glib::Continue(true),
};
2017-07-29 14:33:26 +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> = {
// 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.
let mut q = gst::query::Position::new(gst::Format::Time);
if pipeline.query(&mut q) {
2021-04-11 19:39:50 +00:00
Some(q.result())
} else {
None
2017-07-29 14:33:26 +00:00
}
2018-10-08 12:02:23 +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> = {
// 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.
let mut q = gst::query::Duration::new(gst::Format::Time);
if pipeline.query(&mut q) {
2021-04-11 19:39:50 +00:00
Some(q.result())
} else {
None
2017-07-29 14:33:26 +00:00
}
2018-10-08 12:02:23 +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)
});
// 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();
//bus.connect_message(None, move |_, msg| {
2017-07-29 14:33:26 +00:00
bus.add_watch(move |_, msg| {
use gst::MessageView;
2017-07-29 14:33:26 +00:00
let main_loop = &main_loop_clone;
match msg.view() {
MessageView::Eos(..) => main_loop.quit(),
2017-07-29 14:33:26 +00:00
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()
2017-07-29 14:33:26 +00:00
);
main_loop.quit();
}
_ => (),
};
glib::Continue(true)
})
.expect("Failed to add bus watch");
2017-07-29 14:33:26 +00:00
main_loop.run();
pipeline
.set_state(gst::State::Null)
.expect("Unable to set the pipeline to the `Null` state");
bus.remove_watch().unwrap();
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);
}