mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2024-11-26 03:21:03 +00:00
examples: app*: refactor error enum to a utils module
The module also contains create_element, link_elements and set_state functions to help reduce boilerplate.
This commit is contained in:
parent
86fc001e35
commit
7326377f5f
3 changed files with 83 additions and 102 deletions
|
@ -5,56 +5,23 @@ use gst_app::*;
|
||||||
|
|
||||||
extern crate glib;
|
extern crate glib;
|
||||||
|
|
||||||
use std::fmt;
|
|
||||||
use std::u64;
|
use std::u64;
|
||||||
use std::i16;
|
use std::i16;
|
||||||
use std::i32;
|
use std::i32;
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub mod utils;
|
||||||
enum AppSinkExError {
|
|
||||||
InitFailed(glib::Error),
|
|
||||||
ElementNotFound(&'static str),
|
|
||||||
ElementLinkFailed(&'static str, &'static str),
|
|
||||||
SetStateError(&'static str),
|
|
||||||
ElementError(std::string::String, glib::Error, std::string::String),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for AppSinkExError {
|
fn create_pipeline() -> Result<Pipeline, utils::ExampleError> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
gst::init().map_err(|e| utils::ExampleError::InitFailed(e))?;
|
||||||
match *self {
|
|
||||||
AppSinkExError::InitFailed(ref e) => {
|
|
||||||
write!(f, "GStreamer initialization failed: {:?}", e)
|
|
||||||
}
|
|
||||||
AppSinkExError::ElementNotFound(ref e) => write!(f, "Element {} not found", e),
|
|
||||||
AppSinkExError::ElementLinkFailed(ref e1, ref e2) => {
|
|
||||||
write!(f, "Link failed between {} and {}", e1, e2)
|
|
||||||
}
|
|
||||||
AppSinkExError::SetStateError(ref state) => {
|
|
||||||
write!(f, "Pipeline failed to switch to state {}", state)
|
|
||||||
}
|
|
||||||
AppSinkExError::ElementError(ref element, ref err, ref debug) => {
|
|
||||||
write!(f, "Error from {}: {} ({:?})", element, err, debug)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_pipeline() -> Result<Pipeline, AppSinkExError> {
|
|
||||||
gst::init().map_err(|e| AppSinkExError::InitFailed(e))?;
|
|
||||||
let pipeline = gst::Pipeline::new(None);
|
let pipeline = gst::Pipeline::new(None);
|
||||||
let src = gst::ElementFactory::make("audiotestsrc", None)
|
let src = utils::create_element("audiotestsrc")?;
|
||||||
.ok_or(AppSinkExError::ElementNotFound("audiotestsrc"))?;
|
let sink = utils::create_element("appsink")?;
|
||||||
let sink = gst::ElementFactory::make("appsink", None)
|
|
||||||
.ok_or(AppSinkExError::ElementNotFound("appsink"))?;
|
|
||||||
|
|
||||||
pipeline
|
pipeline
|
||||||
.add_many(&[&src, &sink])
|
.add_many(&[&src, &sink])
|
||||||
.expect("Unable to add elements in the pipeline");
|
.expect("Unable to add elements in the pipeline");
|
||||||
|
|
||||||
gst::Element::link(&src, &sink)
|
utils::link_elements(&src, &sink)?;
|
||||||
.map_err(|_| {
|
|
||||||
AppSinkExError::ElementLinkFailed("audiotestsrc", "appsink")
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let appsink = sink.clone()
|
let appsink = sink.clone()
|
||||||
.dynamic_cast::<AppSink>()
|
.dynamic_cast::<AppSink>()
|
||||||
|
@ -105,12 +72,10 @@ fn create_pipeline() -> Result<Pipeline, AppSinkExError> {
|
||||||
Ok(pipeline)
|
Ok(pipeline)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main_loop() -> Result<(), AppSinkExError> {
|
fn main_loop() -> Result<(), utils::ExampleError> {
|
||||||
let pipeline = create_pipeline()?;
|
let pipeline = create_pipeline()?;
|
||||||
|
|
||||||
if let gst::StateChangeReturn::Failure = pipeline.set_state(gst::State::Playing) {
|
utils::set_state(&pipeline, gst::State::Playing)?;
|
||||||
return Err(AppSinkExError::SetStateError("playing"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let bus = pipeline
|
let bus = pipeline
|
||||||
.get_bus()
|
.get_bus()
|
||||||
|
@ -125,10 +90,8 @@ fn main_loop() -> Result<(), AppSinkExError> {
|
||||||
match msg.view() {
|
match msg.view() {
|
||||||
MessageView::Eos(..) => break,
|
MessageView::Eos(..) => break,
|
||||||
MessageView::Error(err) => {
|
MessageView::Error(err) => {
|
||||||
if let gst::StateChangeReturn::Failure = pipeline.set_state(gst::State::Null) {
|
utils::set_state(&pipeline, gst::State::Null)?;
|
||||||
return Err(AppSinkExError::SetStateError("null"));
|
return Err(utils::ExampleError::ElementError(
|
||||||
}
|
|
||||||
return Err(AppSinkExError::ElementError(
|
|
||||||
msg.get_src().get_path_string(),
|
msg.get_src().get_path_string(),
|
||||||
err.get_error(),
|
err.get_error(),
|
||||||
err.get_debug().unwrap(),
|
err.get_debug().unwrap(),
|
||||||
|
@ -138,9 +101,7 @@ fn main_loop() -> Result<(), AppSinkExError> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let gst::StateChangeReturn::Failure = pipeline.set_state(gst::State::Null) {
|
utils::set_state(&pipeline, gst::State::Null)?;
|
||||||
return Err(AppSinkExError::SetStateError("null"));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,61 +5,27 @@ use gst_app::*;
|
||||||
|
|
||||||
extern crate glib;
|
extern crate glib;
|
||||||
|
|
||||||
use std::fmt;
|
|
||||||
use std::u64;
|
use std::u64;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
const WIDTH: usize = 320;
|
const WIDTH: usize = 320;
|
||||||
const HEIGHT: usize = 240;
|
const HEIGHT: usize = 240;
|
||||||
|
|
||||||
#[derive(Debug)]
|
fn create_pipeline() -> Result<(Pipeline, AppSrc), utils::ExampleError> {
|
||||||
enum AppSrcExError {
|
gst::init().map_err(|e| utils::ExampleError::InitFailed(e))?;
|
||||||
InitFailed(glib::Error),
|
|
||||||
ElementNotFound(&'static str),
|
|
||||||
ElementLinkFailed(&'static str, &'static str),
|
|
||||||
SetStateError(&'static str),
|
|
||||||
ElementError(std::string::String, glib::Error, std::string::String),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for AppSrcExError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match *self {
|
|
||||||
AppSrcExError::InitFailed(ref e) => {
|
|
||||||
write!(f, "GStreamer initialization failed: {:?}", e)
|
|
||||||
}
|
|
||||||
AppSrcExError::ElementNotFound(ref e) => write!(f, "Element {} not found", e),
|
|
||||||
AppSrcExError::ElementLinkFailed(ref e1, ref e2) => {
|
|
||||||
write!(f, "Link failed between {} and {}", e1, e2)
|
|
||||||
}
|
|
||||||
AppSrcExError::SetStateError(ref state) => {
|
|
||||||
write!(f, "Pipeline failed to switch to state {}", state)
|
|
||||||
}
|
|
||||||
AppSrcExError::ElementError(ref element, ref err, ref debug) => {
|
|
||||||
write!(f, "Error from {}: {} ({:?})", element, err, debug)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_pipeline() -> Result<(Pipeline, AppSrc), AppSrcExError> {
|
|
||||||
gst::init().map_err(|e| AppSrcExError::InitFailed(e))?;
|
|
||||||
|
|
||||||
let pipeline = gst::Pipeline::new(None);
|
let pipeline = gst::Pipeline::new(None);
|
||||||
let src = gst::ElementFactory::make("appsrc", None)
|
let src = utils::create_element("appsrc")?;
|
||||||
.ok_or(AppSrcExError::ElementNotFound("appsrc"))?;
|
let videoconvert = utils::create_element("videoconvert")?;
|
||||||
|
let sink = utils::create_element("autovideosink")?;
|
||||||
let videoconvert = gst::ElementFactory::make("videoconvert", None)
|
|
||||||
.ok_or(AppSrcExError::ElementNotFound("videoconvert"))?;
|
|
||||||
let sink = gst::ElementFactory::make("autovideosink", None)
|
|
||||||
.ok_or(AppSrcExError::ElementNotFound("autovideosink"))?;
|
|
||||||
|
|
||||||
pipeline
|
pipeline
|
||||||
.add_many(&[&src, &videoconvert, &sink])
|
.add_many(&[&src, &videoconvert, &sink])
|
||||||
.expect("Unable to add elements in the pipeline");
|
.expect("Unable to add elements in the pipeline");
|
||||||
gst::Element::link(&src, &videoconvert)
|
utils::link_elements(&src, &videoconvert)?;
|
||||||
.map_err(|_| AppSrcExError::ElementLinkFailed("src", "videoconvert"))?;
|
utils::link_elements(&videoconvert, &sink)?;
|
||||||
gst::Element::link(&videoconvert, &sink)
|
|
||||||
.map_err(|_| AppSrcExError::ElementLinkFailed("videoconvert", "sink"))?;
|
|
||||||
|
|
||||||
let appsrc = src.clone()
|
let appsrc = src.clone()
|
||||||
.dynamic_cast::<AppSrc>()
|
.dynamic_cast::<AppSrc>()
|
||||||
|
@ -80,7 +46,7 @@ fn create_pipeline() -> Result<(Pipeline, AppSrc), AppSrcExError> {
|
||||||
Ok((pipeline, appsrc))
|
Ok((pipeline, appsrc))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main_loop() -> Result<(), AppSrcExError> {
|
fn main_loop() -> Result<(), utils::ExampleError> {
|
||||||
let (pipeline, appsrc) = create_pipeline()?;
|
let (pipeline, appsrc) = create_pipeline()?;
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
|
@ -111,9 +77,7 @@ fn main_loop() -> Result<(), AppSrcExError> {
|
||||||
appsrc.end_of_stream();
|
appsrc.end_of_stream();
|
||||||
});
|
});
|
||||||
|
|
||||||
if let gst::StateChangeReturn::Failure = pipeline.set_state(gst::State::Playing) {
|
utils::set_state(&pipeline, gst::State::Playing)?;
|
||||||
return Err(AppSrcExError::SetStateError("playing"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let bus = pipeline
|
let bus = pipeline
|
||||||
.get_bus()
|
.get_bus()
|
||||||
|
@ -128,8 +92,8 @@ fn main_loop() -> Result<(), AppSrcExError> {
|
||||||
match msg.view() {
|
match msg.view() {
|
||||||
MessageView::Eos(..) => break,
|
MessageView::Eos(..) => break,
|
||||||
MessageView::Error(err) => {
|
MessageView::Error(err) => {
|
||||||
pipeline.set_state(gst::State::Null);
|
utils::set_state(&pipeline, gst::State::Null)?;
|
||||||
return Err(AppSrcExError::ElementError(
|
return Err(utils::ExampleError::ElementError(
|
||||||
msg.get_src().get_path_string(),
|
msg.get_src().get_path_string(),
|
||||||
err.get_error(),
|
err.get_error(),
|
||||||
err.get_debug().unwrap(),
|
err.get_debug().unwrap(),
|
||||||
|
@ -139,9 +103,7 @@ fn main_loop() -> Result<(), AppSrcExError> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let gst::StateChangeReturn::Failure = pipeline.set_state(gst::State::Null) {
|
utils::set_state(&pipeline, gst::State::Null)?;
|
||||||
return Err(AppSrcExError::SetStateError("null"));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
58
examples/src/bin/utils/mod.rs
Normal file
58
examples/src/bin/utils/mod.rs
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
|
||||||
|
extern crate gstreamer as gst;
|
||||||
|
use gst::*;
|
||||||
|
|
||||||
|
extern crate glib;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum ExampleError {
|
||||||
|
InitFailed(glib::Error),
|
||||||
|
ElementNotFound(&'static str),
|
||||||
|
ElementLinkFailed(::std::string::String, ::std::string::String),
|
||||||
|
SetStateError(::std::string::String),
|
||||||
|
ElementError(::std::string::String, glib::Error, ::std::string::String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ExampleError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
ExampleError::InitFailed(ref e) => {
|
||||||
|
write!(f, "GStreamer initialization failed: {:?}", e)
|
||||||
|
}
|
||||||
|
ExampleError::ElementNotFound(ref e) => write!(f, "Element {} not found", e),
|
||||||
|
ExampleError::ElementLinkFailed(ref e1, ref e2) => {
|
||||||
|
write!(f, "Link failed between {} and {}", e1, e2)
|
||||||
|
}
|
||||||
|
ExampleError::SetStateError(ref state) => {
|
||||||
|
write!(f, "Pipeline failed to switch to state {}", state)
|
||||||
|
}
|
||||||
|
ExampleError::ElementError(ref element, ref err, ref debug) => {
|
||||||
|
write!(f, "Error from {}: {} ({:?})", element, err, debug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_element(name: &'static str) -> Result<gst::Element, ExampleError> {
|
||||||
|
gst::ElementFactory::make(name, None).ok_or(ExampleError::ElementNotFound(name))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn link_elements(e1: &gst::Element, e2: &gst::Element) -> Result<(), ExampleError> {
|
||||||
|
match gst::Element::link(e1, e2) {
|
||||||
|
Ok(o) => Ok(o),
|
||||||
|
Err(_) => Err(ExampleError::ElementLinkFailed(
|
||||||
|
e1.get_name(),
|
||||||
|
e2.get_name(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_state(e: &gst::Pipeline, state: gst::State) -> Result<(), ExampleError> {
|
||||||
|
if let gst::StateChangeReturn::Failure = e.set_state(state) {
|
||||||
|
return Err(ExampleError::SetStateError(
|
||||||
|
gst::Element::state_get_name(state).unwrap(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in a new issue