gstreamer-rs/examples/src/bin/appsrc.rs

144 lines
4.2 KiB
Rust
Raw Normal View History

2017-08-01 18:29:49 +00:00
extern crate gstreamer as gst;
use gst::prelude::*;
2017-08-01 18:29:49 +00:00
extern crate gstreamer_app as gst_app;
extern crate gstreamer_video as gst_video;
2017-08-01 18:29:49 +00:00
extern crate glib;
2017-08-01 18:29:49 +00:00
use std::thread;
use std::error::Error as StdError;
extern crate failure;
use failure::Error;
#[macro_use]
extern crate failure_derive;
2017-11-12 18:07:02 +00:00
#[path = "../examples-common.rs"]
mod examples_common;
#[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,
}
2017-08-01 18:29:49 +00:00
const WIDTH: usize = 320;
const HEIGHT: usize = 240;
fn create_pipeline() -> Result<(gst::Pipeline, gst_app::AppSrc), Error> {
gst::init()?;
2017-08-01 18:29:49 +00:00
let pipeline = gst::Pipeline::new(None);
let src = gst::ElementFactory::make("appsrc", None).ok_or(MissingElement("appsrc"))?;
let videoconvert =
gst::ElementFactory::make("videoconvert", None).ok_or(MissingElement("videoconvert"))?;
let sink =
gst::ElementFactory::make("autovideosink", None).ok_or(MissingElement("autovideosink"))?;
pipeline.add_many(&[&src, &videoconvert, &sink])?;
gst::Element::link_many(&[&src, &videoconvert, &sink])?;
let appsrc = src.clone()
.dynamic_cast::<gst_app::AppSrc>()
.expect("Source element is expected to be an appsrc!");
let info = gst_video::VideoInfo::new(gst_video::VideoFormat::Bgrx, WIDTH as u32, HEIGHT as u32)
.fps(gst::Fraction::new(2, 1))
.build()
.expect("Failed to create video info");
appsrc.set_caps(&info.to_caps().unwrap());
appsrc.set_property_format(gst::Format::Time);
2017-08-01 18:29:49 +00:00
appsrc.set_max_bytes(1);
appsrc.set_property_block(true);
Ok((pipeline, appsrc))
}
fn main_loop(pipeline: gst::Pipeline, appsrc: gst_app::AppSrc) -> Result<(), Error> {
2017-08-01 18:29:49 +00:00
thread::spawn(move || {
for i in 0..100 {
println!("Producing frame {}", i);
let r = if i % 2 == 0 { 0 } else { 255 };
let g = if i % 3 == 0 { 0 } else { 255 };
let b = if i % 5 == 0 { 0 } else { 255 };
let mut buffer = gst::Buffer::with_size(WIDTH * HEIGHT * 4).unwrap();
{
let buffer = buffer.get_mut().unwrap();
buffer.set_pts(i * 500 * gst::MSECOND);
2017-08-01 18:29:49 +00:00
let mut data = buffer.map_writable().unwrap();
for p in data.as_mut_slice().chunks_mut(4) {
assert_eq!(p.len(), 4);
p[0] = b;
p[1] = g;
p[2] = r;
p[3] = 0;
}
}
2017-08-01 18:29:49 +00:00
if appsrc.push_buffer(buffer) != gst::FlowReturn::Ok {
2017-08-01 18:29:49 +00:00
break;
}
}
let _ = appsrc.end_of_stream();
2017-08-01 18:29:49 +00:00
});
pipeline.set_state(gst::State::Playing).into_result()?;
2017-08-01 18:29:49 +00:00
let bus = pipeline
.get_bus()
.expect("Pipeline without bus. Shouldn't happen!");
2017-08-01 18:29:49 +00:00
while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) {
use gst::MessageView;
2017-08-01 18:29:49 +00:00
match msg.view() {
MessageView::Eos(..) => break,
MessageView::Error(err) => {
pipeline.set_state(gst::State::Null).into_result()?;
Err(ErrorMessage {
src: err.get_src()
.map(|s| s.get_path_string())
.unwrap_or_else(|| String::from("None")),
error: err.get_error().description().into(),
debug: err.get_debug(),
cause: err.get_error(),
})?;
2017-08-01 18:29:49 +00:00
}
_ => (),
}
}
pipeline.set_state(gst::State::Null).into_result()?;
Ok(())
}
2017-11-12 18:07:02 +00:00
fn example_main() {
match create_pipeline().and_then(|(pipeline, appsrc)| main_loop(pipeline, appsrc)) {
Ok(r) => r,
Err(e) => eprintln!("Error! {}", e),
}
2017-08-01 18:29:49 +00:00
}
2017-11-12 18:07:02 +00:00
fn main() {
// 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)
examples_common::run(example_main);
}