2018-11-05 10:10:14 +00:00
|
|
|
// This example demonstrates the use of the decodebin element
|
|
|
|
// The decodebin element tries to automatically detect the incoming
|
|
|
|
// format and to autoplug the appropriate demuxers / decoders to handle it.
|
|
|
|
// and decode it to raw audio, video or subtitles.
|
|
|
|
// Before the pipeline hasn't been prerolled, the decodebin can't possibly know what
|
|
|
|
// format it gets as its input. So at first, the pipeline looks like this:
|
|
|
|
|
|
|
|
// {filesrc} - {decodebin}
|
|
|
|
|
|
|
|
// As soon as the decodebin has detected the stream format, it will try to decode every
|
|
|
|
// contained stream to its raw format.
|
|
|
|
// The application connects a signal-handler to decodebin's pad-added signal, which tells us
|
|
|
|
// whenever the decodebin provided us with another contained (raw) stream from the input file.
|
|
|
|
|
|
|
|
// This application supports audio and video streams. Video streams are
|
|
|
|
// displayed using an autovideosink, and audiostreams are played back using autoaudiosink.
|
|
|
|
// So for a file that contains one audio and one video stream,
|
|
|
|
// the pipeline looks like the following:
|
|
|
|
|
|
|
|
// /-[audio]-{audioconvert}-{audioresample}-{autoaudiosink}
|
|
|
|
// {filesrc}-{decodebin}-|
|
|
|
|
// \-[video]-{viceoconvert}-{videoscale}-{autovideosink}
|
|
|
|
|
|
|
|
// Both auto-sinks at the end automatically select the best available (actual) sink. Since the
|
|
|
|
// selection of available actual sinks is platform specific
|
|
|
|
// (like using pulseaudio for audio output on linux, e.g.),
|
|
|
|
// we need to add the audioconvert and audioresample elements before handing the stream to the
|
|
|
|
// autoaudiosink, because we need to make sure, that the stream is always supported by the actual sink.
|
|
|
|
// Especially Windows APIs tend to be quite picky about samplerate and sample-format.
|
|
|
|
// The same applies to videostreams.
|
|
|
|
|
2020-12-20 15:09:22 +00:00
|
|
|
use gst::element_error;
|
|
|
|
use gst::element_warning;
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::prelude::*;
|
2017-07-05 07:40:02 +00:00
|
|
|
|
2020-05-02 22:16:39 +00:00
|
|
|
#[cfg(feature = "v1_10")]
|
|
|
|
use glib::GBoxed;
|
2017-07-05 07:40:02 +00:00
|
|
|
|
|
|
|
use std::env;
|
2017-11-16 11:58:56 +00:00
|
|
|
#[cfg(feature = "v1_10")]
|
2017-11-16 11:39:34 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
2020-05-02 23:23:38 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
use derive_more::{Display, Error};
|
2017-07-05 07:40:02 +00:00
|
|
|
|
2017-11-12 18:07:02 +00:00
|
|
|
#[path = "../examples-common.rs"]
|
|
|
|
mod examples_common;
|
|
|
|
|
2020-05-02 23:23:38 +00:00
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "Missing element {}", _0)]
|
|
|
|
struct MissingElement(#[error(not(source))] &'static str);
|
2017-11-16 11:39:34 +00:00
|
|
|
|
2020-05-02 23:23:38 +00:00
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "Received error from {}: {} (debug: {:?})", src, error, debug)]
|
2017-11-16 11:39:34 +00:00
|
|
|
struct ErrorMessage {
|
|
|
|
src: String,
|
|
|
|
error: String,
|
|
|
|
debug: Option<String>,
|
2020-05-02 23:23:38 +00:00
|
|
|
source: glib::Error,
|
2017-11-16 11:39:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 06:57:17 +00:00
|
|
|
#[cfg(feature = "v1_10")]
|
2020-03-19 11:22:20 +00:00
|
|
|
#[derive(Clone, Debug, GBoxed)]
|
|
|
|
#[gboxed(type_name = "ErrorValue")]
|
2018-11-28 06:57:17 +00:00
|
|
|
struct ErrorValue(Arc<Mutex<Option<Error>>>);
|
|
|
|
|
2017-11-16 11:39:34 +00:00
|
|
|
fn example_main() -> Result<(), Error> {
|
|
|
|
gst::init()?;
|
2017-07-05 07:40:02 +00:00
|
|
|
|
|
|
|
let args: Vec<_> = env::args().collect();
|
|
|
|
let uri: &str = if args.len() == 2 {
|
|
|
|
args[1].as_ref()
|
|
|
|
} else {
|
2017-11-10 15:53:32 +00:00
|
|
|
println!("Usage: decodebin file_path");
|
2017-11-27 11:01:03 +00:00
|
|
|
std::process::exit(-1)
|
2017-07-05 07:40:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let pipeline = gst::Pipeline::new(None);
|
2019-12-17 19:00:42 +00:00
|
|
|
let src = gst::ElementFactory::make("filesrc", None).map_err(|_| MissingElement("filesrc"))?;
|
2017-11-16 11:39:34 +00:00
|
|
|
let decodebin =
|
2019-12-17 19:00:42 +00:00
|
|
|
gst::ElementFactory::make("decodebin", None).map_err(|_| MissingElement("decodebin"))?;
|
2017-07-05 07:40:02 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Tell the filesrc what file to load
|
2017-11-16 11:39:34 +00:00
|
|
|
src.set_property("location", &uri)?;
|
2017-07-05 07:40:02 +00:00
|
|
|
|
2017-11-16 11:39:34 +00:00
|
|
|
pipeline.add_many(&[&src, &decodebin])?;
|
|
|
|
gst::Element::link_many(&[&src, &decodebin])?;
|
2017-07-05 07:40:02 +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 does, 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
|
|
|
// Connect to decodebin's pad-added signal, that is emitted whenever
|
|
|
|
// it found another stream from the input file and found a way to decode it to its raw format.
|
|
|
|
// decodebin automatically adds a src-pad for this raw stream, which
|
|
|
|
// we can use to build the follow-up pipeline.
|
2017-11-16 11:39:34 +00:00
|
|
|
decodebin.connect_pad_added(move |dbin, src_pad| {
|
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,
|
|
|
|
};
|
2017-07-05 07:40:02 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Try to detect whether the raw stream decodebin provided us with
|
|
|
|
// just now is either audio or video (or none of both, e.g. subtitles).
|
2017-07-10 21:02:08 +00:00
|
|
|
let (is_audio, is_video) = {
|
2017-11-16 11:39:34 +00:00
|
|
|
let media_type = src_pad.get_current_caps().and_then(|caps| {
|
|
|
|
caps.get_structure(0).map(|s| {
|
|
|
|
let name = s.get_name();
|
|
|
|
(name.starts_with("audio/"), name.starts_with("video/"))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
match media_type {
|
|
|
|
None => {
|
2020-12-20 15:09:22 +00:00
|
|
|
element_warning!(
|
2017-11-16 11:39:34 +00:00
|
|
|
dbin,
|
|
|
|
gst::CoreError::Negotiation,
|
|
|
|
("Failed to get media type from pad {}", src_pad.get_name())
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Some(media_type) => media_type,
|
|
|
|
}
|
2017-07-05 07:40:02 +00:00
|
|
|
};
|
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// We create a closure here, calling it directly below it, because this greatly
|
|
|
|
// improves readability for error-handling. Like this, we can simply use the
|
|
|
|
// ?-operator within the closure, and handle the actual error down below where
|
|
|
|
// we call the insert_sink(..) closure.
|
2017-11-16 11:39:34 +00:00
|
|
|
let insert_sink = |is_audio, is_video| -> Result<(), Error> {
|
|
|
|
if is_audio {
|
2018-11-05 10:10:14 +00:00
|
|
|
// decodebin found a raw audiostream, so we build the follow-up pipeline to
|
|
|
|
// play it on the default audio playback device (using autoaudiosink).
|
2019-12-17 19:00:42 +00:00
|
|
|
let queue = gst::ElementFactory::make("queue", None)
|
|
|
|
.map_err(|_| MissingElement("queue"))?;
|
2017-11-16 11:39:34 +00:00
|
|
|
let convert = gst::ElementFactory::make("audioconvert", None)
|
2019-12-17 19:00:42 +00:00
|
|
|
.map_err(|_| MissingElement("audioconvert"))?;
|
2017-11-16 11:39:34 +00:00
|
|
|
let resample = gst::ElementFactory::make("audioresample", None)
|
2019-12-17 19:00:42 +00:00
|
|
|
.map_err(|_| MissingElement("audioresample"))?;
|
2017-11-16 11:39:34 +00:00
|
|
|
let sink = gst::ElementFactory::make("autoaudiosink", None)
|
2019-12-17 19:00:42 +00:00
|
|
|
.map_err(|_| MissingElement("autoaudiosink"))?;
|
2017-11-16 11:39:34 +00:00
|
|
|
|
|
|
|
let elements = &[&queue, &convert, &resample, &sink];
|
|
|
|
pipeline.add_many(elements)?;
|
|
|
|
gst::Element::link_many(elements)?;
|
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// !!ATTENTION!!:
|
|
|
|
// This is quite important and people forget it often. Without making sure that
|
|
|
|
// the new elements have the same state as the pipeline, things will fail later.
|
|
|
|
// They would still be in Null state and can't process data.
|
2017-11-16 11:39:34 +00:00
|
|
|
for e in elements {
|
|
|
|
e.sync_state_with_parent()?;
|
|
|
|
}
|
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Get the queue element's sink pad and link the decodebin's newly created
|
|
|
|
// src pad for the audio stream to it.
|
2017-11-16 11:39:34 +00:00
|
|
|
let sink_pad = queue.get_static_pad("sink").expect("queue has no sinkpad");
|
2019-01-08 16:13:37 +00:00
|
|
|
src_pad.link(&sink_pad)?;
|
2017-11-16 11:39:34 +00:00
|
|
|
} else if is_video {
|
2018-11-05 10:10:14 +00:00
|
|
|
// decodebin found a raw videostream, so we build the follow-up pipeline to
|
|
|
|
// display it using the autovideosink.
|
2019-12-17 19:00:42 +00:00
|
|
|
let queue = gst::ElementFactory::make("queue", None)
|
|
|
|
.map_err(|_| MissingElement("queue"))?;
|
2017-11-16 11:39:34 +00:00
|
|
|
let convert = gst::ElementFactory::make("videoconvert", None)
|
2019-12-17 19:00:42 +00:00
|
|
|
.map_err(|_| MissingElement("videoconvert"))?;
|
2017-11-16 11:39:34 +00:00
|
|
|
let scale = gst::ElementFactory::make("videoscale", None)
|
2019-12-17 19:00:42 +00:00
|
|
|
.map_err(|_| MissingElement("videoscale"))?;
|
2017-11-16 11:39:34 +00:00
|
|
|
let sink = gst::ElementFactory::make("autovideosink", None)
|
2019-12-17 19:00:42 +00:00
|
|
|
.map_err(|_| MissingElement("autovideosink"))?;
|
2017-11-16 11:39:34 +00:00
|
|
|
|
|
|
|
let elements = &[&queue, &convert, &scale, &sink];
|
|
|
|
pipeline.add_many(elements)?;
|
|
|
|
gst::Element::link_many(elements)?;
|
|
|
|
|
|
|
|
for e in elements {
|
|
|
|
e.sync_state_with_parent()?
|
|
|
|
}
|
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Get the queue element's sink pad and link the decodebin's newly created
|
|
|
|
// src pad for the video stream to it.
|
2017-11-16 11:39:34 +00:00
|
|
|
let sink_pad = queue.get_static_pad("sink").expect("queue has no sinkpad");
|
2019-01-08 16:13:37 +00:00
|
|
|
src_pad.link(&sink_pad)?;
|
2017-07-05 08:09:49 +00:00
|
|
|
}
|
2017-07-05 07:40:02 +00:00
|
|
|
|
2017-11-16 11:39:34 +00:00
|
|
|
Ok(())
|
|
|
|
};
|
2017-07-05 07:40:02 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// When adding and linking new elements in a callback fails, error information is often sparse.
|
|
|
|
// GStreamer's built-in debugging can be hard to link back to the exact position within the code
|
|
|
|
// that failed. Since callbacks are called from random threads within the pipeline, it can get hard
|
|
|
|
// to get good error information. The macros used in the following can solve that. With the use
|
|
|
|
// of those, one can send arbitrary rust types (using the pipeline's bus) into the mainloop.
|
|
|
|
// What we send here is unpacked down below, in the iteration-code over sent bus-messages.
|
|
|
|
// Because we are using the failure crate for error details here, we even get a backtrace for
|
|
|
|
// where the error was constructed. (If RUST_BACKTRACE=1 is set)
|
2017-11-16 11:39:34 +00:00
|
|
|
if let Err(err) = insert_sink(is_audio, is_video) {
|
2018-11-05 10:10:14 +00:00
|
|
|
// The following sends a message of type Error on the bus, containing our detailed
|
|
|
|
// error information.
|
2017-11-16 11:39:34 +00:00
|
|
|
#[cfg(feature = "v1_10")]
|
2020-12-20 15:09:22 +00:00
|
|
|
element_error!(
|
2017-11-16 11:39:34 +00:00
|
|
|
dbin,
|
|
|
|
gst::LibraryError::Failed,
|
|
|
|
("Failed to insert sink"),
|
|
|
|
details: gst::Structure::builder("error-details")
|
|
|
|
.field("error",
|
2018-11-28 06:57:17 +00:00
|
|
|
&ErrorValue(Arc::new(Mutex::new(Some(err)))))
|
2017-11-16 11:39:34 +00:00
|
|
|
.build()
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "v1_10"))]
|
2020-12-20 15:09:22 +00:00
|
|
|
element_error!(
|
2017-11-16 11:58:56 +00:00
|
|
|
dbin,
|
|
|
|
gst::LibraryError::Failed,
|
|
|
|
("Failed to insert sink"),
|
|
|
|
["{}", err]
|
|
|
|
);
|
2017-07-05 07:40:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline.set_state(gst::State::Playing)?;
|
2017-07-05 07:40:02 +00:00
|
|
|
|
2017-11-16 11:39:34 +00:00
|
|
|
let bus = pipeline
|
|
|
|
.get_bus()
|
|
|
|
.expect("Pipeline without bus. Shouldn't happen!");
|
2017-07-05 07:40:02 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// This code iterates over all messages that are sent across our pipeline's bus.
|
|
|
|
// In the callback ("pad-added" on the decodebin), we sent better error information
|
|
|
|
// using a bus message. This is the position where we get those messages and log
|
|
|
|
// the contained information.
|
2018-12-27 22:06:03 +00:00
|
|
|
for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) {
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::MessageView;
|
|
|
|
|
2017-07-05 07:40:02 +00:00
|
|
|
match msg.view() {
|
2017-07-30 14:06:44 +00:00
|
|
|
MessageView::Eos(..) => break,
|
2017-07-05 07:40:02 +00:00
|
|
|
MessageView::Error(err) => {
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline.set_state(gst::State::Null)?;
|
2017-11-16 11:39:34 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "v1_10")]
|
|
|
|
{
|
|
|
|
match err.get_details() {
|
2018-11-05 10:10:14 +00:00
|
|
|
// This bus-message of type error contained our custom error-details struct
|
|
|
|
// that we sent in the pad-added callback above. So we unpack it and log
|
|
|
|
// the detailed error information here. details contains a glib::SendValue.
|
|
|
|
// The unpacked error is the converted to a Result::Err, stopping the
|
|
|
|
// application's execution.
|
2017-11-16 11:39:34 +00:00
|
|
|
Some(details) if details.get_name() == "error-details" => details
|
2018-11-28 06:57:17 +00:00
|
|
|
.get::<&ErrorValue>("error")
|
2019-08-13 15:00:17 +00:00
|
|
|
.unwrap()
|
2018-11-28 06:57:17 +00:00
|
|
|
.and_then(|v| v.0.lock().unwrap().take())
|
2018-10-08 12:02:23 +00:00
|
|
|
.map(Result::Err)
|
2017-11-16 11:39:34 +00:00
|
|
|
.expect("error-details message without actual error"),
|
2017-12-20 17:30:14 +00:00
|
|
|
_ => Err(ErrorMessage {
|
2018-12-09 16:09:20 +00:00
|
|
|
src: msg
|
2018-07-27 10:36:40 +00:00
|
|
|
.get_src()
|
2018-12-09 16:09:20 +00:00
|
|
|
.map(|s| String::from(s.get_path_string()))
|
2017-12-20 17:30:14 +00:00
|
|
|
.unwrap_or_else(|| String::from("None")),
|
2020-03-19 11:31:52 +00:00
|
|
|
error: err.get_error().to_string(),
|
2019-12-22 07:59:23 +00:00
|
|
|
debug: err.get_debug(),
|
2020-05-02 23:23:38 +00:00
|
|
|
source: err.get_error(),
|
2018-10-08 12:02:23 +00:00
|
|
|
}
|
|
|
|
.into()),
|
2017-11-16 11:39:34 +00:00
|
|
|
}?;
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "v1_10"))]
|
|
|
|
{
|
2019-10-04 07:47:48 +00:00
|
|
|
return Err(ErrorMessage {
|
2018-12-09 16:09:20 +00:00
|
|
|
src: msg
|
2018-07-27 10:36:40 +00:00
|
|
|
.get_src()
|
2018-12-09 16:09:20 +00:00
|
|
|
.map(|s| String::from(s.get_path_string()))
|
2017-11-27 11:01:03 +00:00
|
|
|
.unwrap_or_else(|| String::from("None")),
|
2020-04-01 14:06:23 +00:00
|
|
|
error: err.get_error().to_string(),
|
2019-12-22 07:59:23 +00:00
|
|
|
debug: err.get_debug(),
|
2020-05-02 23:23:38 +00:00
|
|
|
source: err.get_error(),
|
2019-10-04 07:47:48 +00:00
|
|
|
}
|
|
|
|
.into());
|
2017-11-16 11:39:34 +00:00
|
|
|
}
|
2017-07-10 21:33:24 +00:00
|
|
|
}
|
2017-07-05 07:40:02 +00:00
|
|
|
MessageView::StateChanged(s) => {
|
2017-07-10 21:33:24 +00:00
|
|
|
println!(
|
2017-11-16 11:58:56 +00:00
|
|
|
"State changed from {:?}: {:?} -> {:?} ({:?})",
|
2018-01-29 12:25:12 +00:00
|
|
|
s.get_src().map(|s| s.get_path_string()),
|
2017-07-10 21:33:24 +00:00
|
|
|
s.get_old(),
|
|
|
|
s.get_current(),
|
|
|
|
s.get_pending()
|
|
|
|
);
|
|
|
|
}
|
2017-07-05 07:40:02 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline.set_state(gst::State::Null)?;
|
2017-11-16 11:39:34 +00:00
|
|
|
|
|
|
|
Ok(())
|
2017-07-05 07:40:02 +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-16 11:39:34 +00:00
|
|
|
match examples_common::run(example_main) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => eprintln!("Error! {}", e),
|
|
|
|
}
|
2017-11-12 18:07:02 +00:00
|
|
|
}
|