Switch appsrc example to failure based error handling

This commit is contained in:
Sebastian Dröge 2017-11-12 10:15:37 +01:00
parent 1481cba5d9
commit c9027fb244
2 changed files with 44 additions and 27 deletions

View file

@ -14,13 +14,11 @@ use std::i32;
use std::error::Error as StdError; use std::error::Error as StdError;
extern crate failure; extern crate failure;
use failure::Error;
#[macro_use] #[macro_use]
extern crate failure_derive; extern crate failure_derive;
#[allow(unused_imports)]
use failure::{Error, Fail};
#[derive(Debug, Fail)] #[derive(Debug, Fail)]
#[fail(display = "Missing element {}", _0)] #[fail(display = "Missing element {}", _0)]
struct MissingElement(&'static str); struct MissingElement(&'static str);

View file

@ -3,26 +3,46 @@ use gst::prelude::*;
extern crate gstreamer_app as gst_app; extern crate gstreamer_app as gst_app;
extern crate gstreamer_video as gst_video; extern crate gstreamer_video as gst_video;
extern crate glib;
use std::thread; use std::thread;
pub mod utils; use std::error::Error as StdError;
extern crate failure;
use failure::Error;
#[macro_use]
extern crate failure_derive;
#[derive(Debug, Fail)]
#[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,
}
const WIDTH: usize = 320; const WIDTH: usize = 320;
const HEIGHT: usize = 240; const HEIGHT: usize = 240;
fn create_pipeline() -> Result<(gst::Pipeline, gst_app::AppSrc), utils::ExampleError> { fn create_pipeline() -> Result<(gst::Pipeline, gst_app::AppSrc), Error> {
gst::init().map_err(utils::ExampleError::InitFailed)?; gst::init()?;
let pipeline = gst::Pipeline::new(None); let pipeline = gst::Pipeline::new(None);
let src = utils::create_element("appsrc")?; let src = gst::ElementFactory::make("appsrc", None).ok_or(MissingElement("appsrc"))?;
let videoconvert = utils::create_element("videoconvert")?; let videoconvert =
let sink = utils::create_element("autovideosink")?; gst::ElementFactory::make("videoconvert", None).ok_or(MissingElement("videoconvert"))?;
let sink =
gst::ElementFactory::make("autovideosink", None).ok_or(MissingElement("autovideosink"))?;
pipeline pipeline.add_many(&[&src, &videoconvert, &sink])?;
.add_many(&[&src, &videoconvert, &sink]) gst::Element::link_many(&[&src, &videoconvert, &sink])?;
.expect("Unable to add elements in the pipeline");
utils::link_elements(&src, &videoconvert)?;
utils::link_elements(&videoconvert, &sink)?;
let appsrc = src.clone() let appsrc = src.clone()
.dynamic_cast::<gst_app::AppSrc>() .dynamic_cast::<gst_app::AppSrc>()
@ -31,7 +51,7 @@ fn create_pipeline() -> Result<(gst::Pipeline, gst_app::AppSrc), utils::ExampleE
let info = gst_video::VideoInfo::new(gst_video::VideoFormat::Bgrx, WIDTH as u32, HEIGHT as u32) let info = gst_video::VideoInfo::new(gst_video::VideoFormat::Bgrx, WIDTH as u32, HEIGHT as u32)
.fps(gst::Fraction::new(2, 1)) .fps(gst::Fraction::new(2, 1))
.build() .build()
.unwrap(); .expect("Failed to create video info");
appsrc.set_caps(&info.to_caps().unwrap()); appsrc.set_caps(&info.to_caps().unwrap());
appsrc.set_property_format(gst::Format::Time); appsrc.set_property_format(gst::Format::Time);
@ -41,9 +61,7 @@ fn create_pipeline() -> Result<(gst::Pipeline, gst_app::AppSrc), utils::ExampleE
Ok((pipeline, appsrc)) Ok((pipeline, appsrc))
} }
fn main_loop() -> Result<(), utils::ExampleError> { fn main_loop(pipeline: gst::Pipeline, appsrc: gst_app::AppSrc) -> Result<(), Error> {
let (pipeline, appsrc) = create_pipeline()?;
thread::spawn(move || { thread::spawn(move || {
for i in 0..100 { for i in 0..100 {
println!("Producing frame {}", i); println!("Producing frame {}", i);
@ -76,7 +94,7 @@ fn main_loop() -> Result<(), utils::ExampleError> {
let _ = appsrc.end_of_stream(); let _ = appsrc.end_of_stream();
}); });
utils::set_state(&pipeline, gst::State::Playing)?; pipeline.set_state(gst::State::Playing).into_result()?;
let bus = pipeline let bus = pipeline
.get_bus() .get_bus()
@ -88,24 +106,25 @@ fn main_loop() -> Result<(), utils::ExampleError> {
match msg.view() { match msg.view() {
MessageView::Eos(..) => break, MessageView::Eos(..) => break,
MessageView::Error(err) => { MessageView::Error(err) => {
utils::set_state(&pipeline, gst::State::Null)?; pipeline.set_state(gst::State::Null).into_result()?;
return Err(utils::ExampleError::ElementError( Err(ErrorMessage {
msg.get_src().get_path_string(), src: msg.get_src().get_path_string(),
err.get_error(), error: err.get_error().description().into(),
err.get_debug().unwrap(), debug: err.get_debug(),
)); cause: err.get_error(),
})?;
} }
_ => (), _ => (),
} }
} }
utils::set_state(&pipeline, gst::State::Null)?; pipeline.set_state(gst::State::Null).into_result()?;
Ok(()) Ok(())
} }
fn main() { fn main() {
match main_loop() { match create_pipeline().and_then(|(pipeline, appsrc)| main_loop(pipeline, appsrc)) {
Ok(r) => r, Ok(r) => r,
Err(e) => eprintln!("Error! {}", e), Err(e) => eprintln!("Error! {}", e),
} }