2017-08-01 18:29:49 +00:00
|
|
|
extern crate gstreamer as gst;
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::prelude::*;
|
2017-08-01 18:29:49 +00:00
|
|
|
extern crate gstreamer_app as gst_app;
|
2017-08-11 14:59:05 +00:00
|
|
|
extern crate gstreamer_video as gst_video;
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2017-08-03 14:52:08 +00:00
|
|
|
extern crate glib;
|
|
|
|
|
2017-08-01 18:29:49 +00:00
|
|
|
use std::u64;
|
|
|
|
use std::thread;
|
|
|
|
|
2017-08-04 09:54:11 +00:00
|
|
|
pub mod utils;
|
|
|
|
|
2017-08-01 18:29:49 +00:00
|
|
|
const WIDTH: usize = 320;
|
|
|
|
const HEIGHT: usize = 240;
|
|
|
|
|
2017-08-17 14:58:15 +00:00
|
|
|
fn create_pipeline() -> Result<(gst::Pipeline, gst_app::AppSrc), utils::ExampleError> {
|
2017-08-04 12:36:12 +00:00
|
|
|
gst::init().map_err(utils::ExampleError::InitFailed)?;
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2017-08-03 14:52:08 +00:00
|
|
|
let pipeline = gst::Pipeline::new(None);
|
2017-08-04 09:54:11 +00:00
|
|
|
let src = utils::create_element("appsrc")?;
|
|
|
|
let videoconvert = utils::create_element("videoconvert")?;
|
|
|
|
let sink = utils::create_element("autovideosink")?;
|
2017-08-03 14:52:08 +00:00
|
|
|
|
|
|
|
pipeline
|
|
|
|
.add_many(&[&src, &videoconvert, &sink])
|
|
|
|
.expect("Unable to add elements in the pipeline");
|
2017-08-04 09:54:11 +00:00
|
|
|
utils::link_elements(&src, &videoconvert)?;
|
|
|
|
utils::link_elements(&videoconvert, &sink)?;
|
2017-08-03 14:52:08 +00:00
|
|
|
|
|
|
|
let appsrc = src.clone()
|
2017-08-17 14:58:15 +00:00
|
|
|
.dynamic_cast::<gst_app::AppSrc>()
|
2017-08-03 14:52:08 +00:00
|
|
|
.expect("Source element is expected to be an appsrc!");
|
2017-08-11 14:59:05 +00:00
|
|
|
|
|
|
|
let info = gst_video::VideoInfo::new(gst_video::VideoFormat::Bgrx, WIDTH as u32, HEIGHT as u32)
|
2017-08-17 14:58:15 +00:00
|
|
|
.fps(gst::Fraction::new(2, 1))
|
2017-08-11 14:59:05 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
appsrc.set_caps(&info.to_caps().unwrap());
|
2017-08-17 14:58:15 +00:00
|
|
|
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);
|
|
|
|
|
2017-08-03 14:52:08 +00:00
|
|
|
Ok((pipeline, appsrc))
|
|
|
|
}
|
|
|
|
|
2017-08-04 09:54:11 +00:00
|
|
|
fn main_loop() -> Result<(), utils::ExampleError> {
|
2017-08-03 14:52:08 +00:00
|
|
|
let (pipeline, appsrc) = create_pipeline()?;
|
|
|
|
|
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 };
|
|
|
|
|
2017-08-11 14:59:05 +00:00
|
|
|
let mut buffer = gst::Buffer::with_size(WIDTH * HEIGHT * 4).unwrap();
|
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
|
|
|
buffer.set_pts(i * 500_000_000);
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2017-08-11 14:59:05 +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
|
|
|
|
2017-08-17 14:58:15 +00:00
|
|
|
if appsrc.push_buffer(buffer) != gst::FlowReturn::Ok {
|
2017-08-01 18:29:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
appsrc.end_of_stream();
|
|
|
|
});
|
|
|
|
|
2017-08-04 09:54:11 +00:00
|
|
|
utils::set_state(&pipeline, gst::State::Playing)?;
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2017-08-03 14:52:08 +00:00
|
|
|
let bus = pipeline
|
|
|
|
.get_bus()
|
|
|
|
.expect("Pipeline without bus. Shouldn't happen!");
|
2017-08-01 18:29:49 +00:00
|
|
|
|
|
|
|
loop {
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::MessageView;
|
|
|
|
|
2017-08-01 18:29:49 +00:00
|
|
|
let msg = match bus.timed_pop(u64::MAX) {
|
|
|
|
None => break,
|
|
|
|
Some(msg) => msg,
|
|
|
|
};
|
|
|
|
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::Eos(..) => break,
|
|
|
|
MessageView::Error(err) => {
|
2017-08-04 09:54:11 +00:00
|
|
|
utils::set_state(&pipeline, gst::State::Null)?;
|
|
|
|
return Err(utils::ExampleError::ElementError(
|
2017-08-01 18:29:49 +00:00
|
|
|
msg.get_src().get_path_string(),
|
|
|
|
err.get_error(),
|
2017-08-03 14:52:08 +00:00
|
|
|
err.get_debug().unwrap(),
|
|
|
|
));
|
2017-08-01 18:29:49 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 09:54:11 +00:00
|
|
|
utils::set_state(&pipeline, gst::State::Null)?;
|
2017-08-03 14:52:08 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
match main_loop() {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => eprintln!("Error! {}", e),
|
|
|
|
}
|
2017-08-01 18:29:49 +00:00
|
|
|
}
|