mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2024-11-29 04:51:09 +00:00
Add failure-based error handling to the decodebin example
This also makes use of directly reporting errors from a callback, by transferring it via the error message to the main thread.
This commit is contained in:
parent
d430467ed5
commit
86b787ac81
2 changed files with 148 additions and 55 deletions
|
@ -28,6 +28,7 @@ gtkvideooverlay-x11 = ["gtkvideooverlay"]
|
||||||
gtkvideooverlay-quartz = ["gtkvideooverlay"]
|
gtkvideooverlay-quartz = ["gtkvideooverlay"]
|
||||||
tokio = ["gstreamer/futures", "futures", "tokio-core"]
|
tokio = ["gstreamer/futures", "futures", "tokio-core"]
|
||||||
default-features = []
|
default-features = []
|
||||||
|
v1_10 = ["gstreamer/v1_10"]
|
||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
travis-ci = { repository = "sdroege/gstreamer-rs", branch = "master" }
|
travis-ci = { repository = "sdroege/gstreamer-rs", branch = "master" }
|
||||||
|
|
|
@ -1,15 +1,37 @@
|
||||||
|
#[macro_use]
|
||||||
extern crate gstreamer as gst;
|
extern crate gstreamer as gst;
|
||||||
use gst::prelude::*;
|
use gst::prelude::*;
|
||||||
|
|
||||||
extern crate glib;
|
extern crate glib;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::error::Error as StdError;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
extern crate failure;
|
||||||
|
use failure::Error;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate failure_derive;
|
||||||
|
|
||||||
#[path = "../examples-common.rs"]
|
#[path = "../examples-common.rs"]
|
||||||
mod examples_common;
|
mod examples_common;
|
||||||
|
|
||||||
fn example_main() {
|
#[derive(Debug, Fail)]
|
||||||
gst::init().unwrap();
|
#[fail(display = "Missing element {}", _0)]
|
||||||
|
struct MissingElement(&'static str);
|
||||||
|
|
||||||
|
#[derive(Debug, Fail)]
|
||||||
|
#[fail(display = "Received error from {}: {} (debug: {:?})", src, error, debug)]
|
||||||
|
struct ErrorMessage {
|
||||||
|
src: String,
|
||||||
|
error: String,
|
||||||
|
debug: Option<String>,
|
||||||
|
#[cause] cause: glib::Error,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn example_main() -> Result<(), Error> {
|
||||||
|
gst::init()?;
|
||||||
|
|
||||||
let args: Vec<_> = env::args().collect();
|
let args: Vec<_> = env::args().collect();
|
||||||
let uri: &str = if args.len() == 2 {
|
let uri: &str = if args.len() == 2 {
|
||||||
|
@ -20,69 +42,110 @@ fn example_main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let pipeline = gst::Pipeline::new(None);
|
let pipeline = gst::Pipeline::new(None);
|
||||||
let src = gst::ElementFactory::make("filesrc", None).unwrap();
|
let src = gst::ElementFactory::make("filesrc", None).ok_or(MissingElement("filesrc"))?;
|
||||||
let decodebin = gst::ElementFactory::make("decodebin", None).unwrap();
|
let decodebin =
|
||||||
|
gst::ElementFactory::make("decodebin", None).ok_or(MissingElement("decodebin"))?;
|
||||||
|
|
||||||
src.set_property("location", &glib::Value::from(uri))
|
src.set_property("location", &uri)?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
pipeline.add_many(&[&src, &decodebin]).unwrap();
|
pipeline.add_many(&[&src, &decodebin])?;
|
||||||
gst::Element::link_many(&[&src, &decodebin]).unwrap();
|
gst::Element::link_many(&[&src, &decodebin])?;
|
||||||
|
|
||||||
// Need to move a new reference into the closure
|
// Need to move a new reference into the closure
|
||||||
let pipeline_clone = pipeline.clone();
|
let pipeline_clone = pipeline.clone();
|
||||||
decodebin.connect_pad_added(move |_, src_pad| {
|
decodebin.connect_pad_added(move |dbin, src_pad| {
|
||||||
let pipeline = &pipeline_clone;
|
let pipeline = &pipeline_clone;
|
||||||
|
|
||||||
let (is_audio, is_video) = {
|
let (is_audio, is_video) = {
|
||||||
let caps = src_pad.get_current_caps().unwrap();
|
let media_type = src_pad.get_current_caps().and_then(|caps| {
|
||||||
let structure = caps.get_structure(0).unwrap();
|
caps.get_structure(0).map(|s| {
|
||||||
let name = structure.get_name();
|
let name = s.get_name();
|
||||||
|
(name.starts_with("audio/"), name.starts_with("video/"))
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
(name.starts_with("audio/"), name.starts_with("video/"))
|
match media_type {
|
||||||
|
None => {
|
||||||
|
gst_element_warning!(
|
||||||
|
dbin,
|
||||||
|
gst::CoreError::Negotiation,
|
||||||
|
("Failed to get media type from pad {}", src_pad.get_name())
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Some(media_type) => media_type,
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if is_audio {
|
let insert_sink = |is_audio, is_video| -> Result<(), Error> {
|
||||||
let queue = gst::ElementFactory::make("queue", None).unwrap();
|
if is_audio {
|
||||||
let convert = gst::ElementFactory::make("audioconvert", None).unwrap();
|
let queue =
|
||||||
let resample = gst::ElementFactory::make("audioresample", None).unwrap();
|
gst::ElementFactory::make("queue", None).ok_or(MissingElement("queue"))?;
|
||||||
let sink = gst::ElementFactory::make("autoaudiosink", None).unwrap();
|
let convert = gst::ElementFactory::make("audioconvert", None)
|
||||||
|
.ok_or(MissingElement("audioconvert"))?;
|
||||||
|
let resample = gst::ElementFactory::make("audioresample", None)
|
||||||
|
.ok_or(MissingElement("audioresample"))?;
|
||||||
|
let sink = gst::ElementFactory::make("autoaudiosink", None)
|
||||||
|
.ok_or(MissingElement("autoaudiosink"))?;
|
||||||
|
|
||||||
let elements = &[&queue, &convert, &resample, &sink];
|
let elements = &[&queue, &convert, &resample, &sink];
|
||||||
pipeline.add_many(elements).unwrap();
|
pipeline.add_many(elements)?;
|
||||||
gst::Element::link_many(elements).unwrap();
|
gst::Element::link_many(elements)?;
|
||||||
|
|
||||||
for e in elements {
|
for e in elements {
|
||||||
e.sync_state_with_parent().unwrap();
|
e.sync_state_with_parent()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let sink_pad = queue.get_static_pad("sink").expect("queue has no sinkpad");
|
||||||
|
src_pad.link(&sink_pad).into_result()?;
|
||||||
|
} else if is_video {
|
||||||
|
let queue =
|
||||||
|
gst::ElementFactory::make("queue", None).ok_or(MissingElement("queue"))?;
|
||||||
|
let convert = gst::ElementFactory::make("videoconvert", None)
|
||||||
|
.ok_or(MissingElement("videoconvert"))?;
|
||||||
|
let scale = gst::ElementFactory::make("videoscale", None)
|
||||||
|
.ok_or(MissingElement("videoscale"))?;
|
||||||
|
let sink = gst::ElementFactory::make("autovideosink", None)
|
||||||
|
.ok_or(MissingElement("autovideosink"))?;
|
||||||
|
|
||||||
|
let elements = &[&queue, &convert, &scale, &sink];
|
||||||
|
pipeline.add_many(elements)?;
|
||||||
|
gst::Element::link_many(elements)?;
|
||||||
|
|
||||||
|
for e in elements {
|
||||||
|
e.sync_state_with_parent()?
|
||||||
|
}
|
||||||
|
|
||||||
|
let sink_pad = queue.get_static_pad("sink").expect("queue has no sinkpad");
|
||||||
|
src_pad.link(&sink_pad).into_result()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let sink_pad = queue.get_static_pad("sink").unwrap();
|
Ok(())
|
||||||
assert_eq!(src_pad.link(&sink_pad), gst::PadLinkReturn::Ok);
|
};
|
||||||
} else if is_video {
|
|
||||||
let queue = gst::ElementFactory::make("queue", None).unwrap();
|
|
||||||
let convert = gst::ElementFactory::make("videoconvert", None).unwrap();
|
|
||||||
let scale = gst::ElementFactory::make("videoscale", None).unwrap();
|
|
||||||
let sink = gst::ElementFactory::make("autovideosink", None).unwrap();
|
|
||||||
|
|
||||||
let elements = &[&queue, &convert, &scale, &sink];
|
if let Err(err) = insert_sink(is_audio, is_video) {
|
||||||
pipeline.add_many(elements).unwrap();
|
#[cfg(feature = "v1_10")]
|
||||||
gst::Element::link_many(elements).unwrap();
|
gst_element_error!(
|
||||||
|
dbin,
|
||||||
|
gst::LibraryError::Failed,
|
||||||
|
("Failed to insert sink"),
|
||||||
|
details: gst::Structure::builder("error-details")
|
||||||
|
.field("error",
|
||||||
|
glib::AnySendValue::new(Arc::new(Mutex::new(Some(err)))))
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
|
||||||
for e in elements {
|
#[cfg(not(feature = "v1_10"))]
|
||||||
e.sync_state_with_parent().unwrap();
|
gst_element_error!(dbin, gst::LibraryError::Failed, ("Failed to insert sink"));
|
||||||
}
|
|
||||||
|
|
||||||
let sink_pad = queue.get_static_pad("sink").unwrap();
|
|
||||||
assert_eq!(src_pad.link(&sink_pad), gst::PadLinkReturn::Ok);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assert_ne!(
|
pipeline.set_state(gst::State::Playing).into_result()?;
|
||||||
pipeline.set_state(gst::State::Playing),
|
|
||||||
gst::StateChangeReturn::Failure
|
|
||||||
);
|
|
||||||
|
|
||||||
let bus = pipeline.get_bus().unwrap();
|
let bus = pipeline
|
||||||
|
.get_bus()
|
||||||
|
.expect("Pipeline without bus. Shouldn't happen!");
|
||||||
|
|
||||||
while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) {
|
while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) {
|
||||||
use gst::MessageView;
|
use gst::MessageView;
|
||||||
|
@ -90,12 +153,39 @@ fn example_main() {
|
||||||
match msg.view() {
|
match msg.view() {
|
||||||
MessageView::Eos(..) => break,
|
MessageView::Eos(..) => break,
|
||||||
MessageView::Error(err) => {
|
MessageView::Error(err) => {
|
||||||
println!(
|
pipeline.set_state(gst::State::Null).into_result()?;
|
||||||
"Error from {}: {} ({:?})",
|
|
||||||
msg.get_src().get_path_string(),
|
#[cfg(feature = "v1_10")]
|
||||||
err.get_error(),
|
{
|
||||||
err.get_debug()
|
match err.get_details() {
|
||||||
);
|
Some(details) if details.get_name() == "error-details" => details
|
||||||
|
.get::<&glib::AnySendValue>("error")
|
||||||
|
.cloned()
|
||||||
|
.and_then(|v| {
|
||||||
|
v.downcast_ref::<Arc<Mutex<Option<Error>>>>()
|
||||||
|
.and_then(|v| v.lock().unwrap().take())
|
||||||
|
})
|
||||||
|
.map(Result::Err)
|
||||||
|
.expect("error-details message without actual error"),
|
||||||
|
_ => Err(
|
||||||
|
ErrorMessage {
|
||||||
|
src: msg.get_src().get_path_string(),
|
||||||
|
error: err.get_error().description().into(),
|
||||||
|
debug: err.get_debug(),
|
||||||
|
cause: err.get_error(),
|
||||||
|
}.into(),
|
||||||
|
),
|
||||||
|
}?;
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "v1_10"))]
|
||||||
|
{
|
||||||
|
Err(ErrorMessage {
|
||||||
|
src: msg.get_src().get_path_string(),
|
||||||
|
error: err.get_error().description().into(),
|
||||||
|
debug: err.get_debug(),
|
||||||
|
cause: err.get_error(),
|
||||||
|
})?;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
MessageView::StateChanged(s) => {
|
MessageView::StateChanged(s) => {
|
||||||
|
@ -111,14 +201,16 @@ fn example_main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_ne!(
|
pipeline.set_state(gst::State::Null).into_result()?;
|
||||||
pipeline.set_state(gst::State::Null),
|
|
||||||
gst::StateChangeReturn::Failure
|
Ok(())
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// tutorials_common::run is only required to set up the application environent on macOS
|
// 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)
|
// (but not necessary in normal Cocoa applications where this is set up autmatically)
|
||||||
examples_common::run(example_main);
|
match examples_common::run(example_main) {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(e) => eprintln!("Error! {}", e),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue