forked from mirrors/gstreamer-rs
Switch appsink example to failure based error handling
This commit is contained in:
parent
d857fbd349
commit
90700aa9b2
2 changed files with 72 additions and 26 deletions
|
@ -17,7 +17,8 @@ futures = { version = "0.1", optional = true }
|
||||||
tokio-core = { version = "0.1", optional = true }
|
tokio-core = { version = "0.1", optional = true }
|
||||||
send-cell = "0.1"
|
send-cell = "0.1"
|
||||||
byte-slice-cast = "0.1"
|
byte-slice-cast = "0.1"
|
||||||
failure = "0.0.1"
|
failure = { git = "https://github.com/withoutboats/failure.git" }
|
||||||
|
failure_derive = { git = "https://github.com/withoutboats/failure_derive.git" }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
gst-player = ["gstreamer-player"]
|
gst-player = ["gstreamer-player"]
|
||||||
|
|
|
@ -1,27 +1,49 @@
|
||||||
|
#[macro_use]
|
||||||
extern crate gstreamer as gst;
|
extern crate gstreamer as gst;
|
||||||
use gst::prelude::*;
|
use gst::prelude::*;
|
||||||
extern crate gstreamer_app as gst_app;
|
extern crate gstreamer_app as gst_app;
|
||||||
extern crate gstreamer_audio as gst_audio;
|
extern crate gstreamer_audio as gst_audio;
|
||||||
|
|
||||||
|
extern crate glib;
|
||||||
|
|
||||||
extern crate byte_slice_cast;
|
extern crate byte_slice_cast;
|
||||||
use byte_slice_cast::*;
|
use byte_slice_cast::*;
|
||||||
|
|
||||||
use std::i16;
|
use std::i16;
|
||||||
use std::i32;
|
use std::i32;
|
||||||
|
use std::error::Error as StdError;
|
||||||
|
|
||||||
pub mod utils;
|
extern crate failure;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate failure_derive;
|
||||||
|
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use failure::{Error, Fail};
|
||||||
|
|
||||||
|
#[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,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_pipeline() -> Result<gst::Pipeline, Error> {
|
||||||
|
gst::init()?;
|
||||||
|
|
||||||
fn create_pipeline() -> Result<gst::Pipeline, utils::ExampleError> {
|
|
||||||
gst::init().map_err(utils::ExampleError::InitFailed)?;
|
|
||||||
let pipeline = gst::Pipeline::new(None);
|
let pipeline = gst::Pipeline::new(None);
|
||||||
let src = utils::create_element("audiotestsrc")?;
|
let src =
|
||||||
let sink = utils::create_element("appsink")?;
|
gst::ElementFactory::make("audiotestsrc", None).ok_or(MissingElement("audiotestsrc"))?;
|
||||||
|
let sink = gst::ElementFactory::make("appsink", None).ok_or(MissingElement("appsink"))?;
|
||||||
|
|
||||||
pipeline
|
pipeline.add_many(&[&src, &sink])?;
|
||||||
.add_many(&[&src, &sink])
|
src.link(&sink)?;
|
||||||
.expect("Unable to add elements in the pipeline");
|
|
||||||
|
|
||||||
utils::link_elements(&src, &sink)?;
|
|
||||||
|
|
||||||
let appsink = sink.clone()
|
let appsink = sink.clone()
|
||||||
.dynamic_cast::<gst_app::AppSink>()
|
.dynamic_cast::<gst_app::AppSink>()
|
||||||
|
@ -49,17 +71,39 @@ fn create_pipeline() -> Result<gst::Pipeline, utils::ExampleError> {
|
||||||
Some(sample) => sample,
|
Some(sample) => sample,
|
||||||
};
|
};
|
||||||
|
|
||||||
let buffer = sample
|
let buffer = if let Some(buffer) = sample.get_buffer() {
|
||||||
.get_buffer()
|
buffer
|
||||||
.expect("Unable to extract buffer from the sample");
|
} else {
|
||||||
|
gst_element_error!(
|
||||||
|
appsink,
|
||||||
|
gst::ResourceError::Failed,
|
||||||
|
("Failed to get buffer from appsink")
|
||||||
|
);
|
||||||
|
|
||||||
let map = buffer
|
return gst::FlowReturn::Error;
|
||||||
.map_readable()
|
};
|
||||||
.expect("Unable to map buffer for reading");
|
|
||||||
|
let map = if let Some(map) = buffer.map_readable() {
|
||||||
|
map
|
||||||
|
} else {
|
||||||
|
gst_element_error!(
|
||||||
|
appsink,
|
||||||
|
gst::ResourceError::Failed,
|
||||||
|
("Failed to map buffer readable")
|
||||||
|
);
|
||||||
|
|
||||||
|
return gst::FlowReturn::Error;
|
||||||
|
};
|
||||||
|
|
||||||
let samples = if let Ok(samples) = map.as_slice().as_slice_of::<i16>() {
|
let samples = if let Ok(samples) = map.as_slice().as_slice_of::<i16>() {
|
||||||
samples
|
samples
|
||||||
} else {
|
} else {
|
||||||
|
gst_element_error!(
|
||||||
|
appsink,
|
||||||
|
gst::ResourceError::Failed,
|
||||||
|
("Failed to interprete buffer as S16 PCM")
|
||||||
|
);
|
||||||
|
|
||||||
return gst::FlowReturn::Error;
|
return gst::FlowReturn::Error;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -80,10 +124,10 @@ fn create_pipeline() -> Result<gst::Pipeline, utils::ExampleError> {
|
||||||
Ok(pipeline)
|
Ok(pipeline)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main_loop() -> Result<(), utils::ExampleError> {
|
fn main_loop() -> Result<(), Error> {
|
||||||
let pipeline = create_pipeline()?;
|
let pipeline = create_pipeline()?;
|
||||||
|
|
||||||
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()
|
||||||
|
@ -95,18 +139,19 @@ 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(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue