2018-11-05 10:10:14 +00:00
|
|
|
// This example shows how to use the appsrc element.
|
|
|
|
// It operates the following pipeline:
|
|
|
|
|
|
|
|
// {appsrc} - {videoconvert} - {autovideosink}
|
|
|
|
|
|
|
|
// The application itself provides the video-data for the pipeline, by providing
|
|
|
|
// it in the callback of the appsrc element. Videoconvert makes sure that the
|
|
|
|
// format the application provides can be displayed by the autovideosink
|
|
|
|
// at the end of the pipeline.
|
|
|
|
// The application provides data of the following format:
|
|
|
|
// Video / BGRx (4 bytes) / 2 fps
|
|
|
|
|
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-11-12 09:15:37 +00:00
|
|
|
extern crate glib;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-11-12 09:15:37 +00:00
|
|
|
#[derive(Debug, Fail)]
|
|
|
|
#[fail(display = "Missing element {}", _0)]
|
|
|
|
struct MissingElement(&'static str);
|
|
|
|
|
|
|
|
#[derive(Debug, Fail)]
|
2018-07-27 10:36:40 +00:00
|
|
|
#[fail(
|
|
|
|
display = "Received error from {}: {} (debug: {:?})",
|
2018-10-28 13:47:02 +00:00
|
|
|
src, error, debug
|
2018-07-27 10:36:40 +00:00
|
|
|
)]
|
2017-11-12 09:15:37 +00:00
|
|
|
struct ErrorMessage {
|
|
|
|
src: String,
|
|
|
|
error: String,
|
|
|
|
debug: Option<String>,
|
2018-02-22 10:18:37 +00:00
|
|
|
#[cause]
|
|
|
|
cause: glib::Error,
|
2017-11-12 09:15:37 +00:00
|
|
|
}
|
2017-08-04 09:54:11 +00:00
|
|
|
|
2017-08-01 18:29:49 +00:00
|
|
|
const WIDTH: usize = 320;
|
|
|
|
const HEIGHT: usize = 240;
|
|
|
|
|
2018-05-18 08:37:57 +00:00
|
|
|
fn create_pipeline() -> Result<gst::Pipeline, Error> {
|
2017-11-12 09:15:37 +00:00
|
|
|
gst::init()?;
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2017-08-03 14:52:08 +00:00
|
|
|
let pipeline = gst::Pipeline::new(None);
|
2017-11-12 09:15:37 +00:00
|
|
|
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"))?;
|
2017-08-03 14:52:08 +00:00
|
|
|
|
2017-11-12 09:15:37 +00:00
|
|
|
pipeline.add_many(&[&src, &videoconvert, &sink])?;
|
|
|
|
gst::Element::link_many(&[&src, &videoconvert, &sink])?;
|
2017-08-03 14:52:08 +00:00
|
|
|
|
2018-07-27 10:07:24 +00:00
|
|
|
let appsrc = src
|
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
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Specify the format we want to provide as application into the pipeline
|
|
|
|
// by creating a video info with the given format and creating caps from it for the appsrc element.
|
|
|
|
let video_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");
|
2017-08-11 14:59:05 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
appsrc.set_caps(&video_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
|
|
|
|
2018-05-18 08:37:57 +00:00
|
|
|
// Our frame counter, that is stored in the mutable environment
|
|
|
|
// of the closure of the need-data callback
|
2018-05-18 08:42:24 +00:00
|
|
|
//
|
|
|
|
// Alternatively we could also simply start a new thread that
|
|
|
|
// pushes a buffer to the appsrc whenever it wants to, but this
|
|
|
|
// is not really needed here. It is *not required* to use the
|
|
|
|
// need-data callback.
|
2018-05-18 08:37:57 +00:00
|
|
|
let mut i = 0;
|
|
|
|
appsrc.set_callbacks(
|
2018-11-05 10:10:14 +00:00
|
|
|
// Since our appsrc element operates in pull mode (it asks us to provide data),
|
|
|
|
// we add a handler for the need-data callback and provide new data from there.
|
|
|
|
// In our case, we told gstreamer that we do 2 frames per second. While the
|
|
|
|
// buffers of all elements of the pipeline are still empty, this will be called
|
|
|
|
// a couple of times until all of them are filled. After this initial period,
|
|
|
|
// this handler will be called (on average) twice per second.
|
2018-05-18 08:37:57 +00:00
|
|
|
gst_app::AppSrcCallbacks::new()
|
|
|
|
.need_data(move |appsrc, _| {
|
2018-11-05 10:10:14 +00:00
|
|
|
// We only produce 100 frames
|
2018-05-18 08:37:57 +00:00
|
|
|
if i == 100 {
|
|
|
|
let _ = appsrc.end_of_stream();
|
|
|
|
return;
|
|
|
|
}
|
2017-08-03 14:52:08 +00:00
|
|
|
|
2018-05-18 08:37:57 +00:00
|
|
|
println!("Producing frame {}", i);
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2018-05-18 08:37:57 +00:00
|
|
|
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-01 18:29:49 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Create the buffer that can hold exactly one BGRx frame.
|
|
|
|
let mut buffer = gst::Buffer::with_size(video_info.size()).unwrap();
|
2018-05-18 08:37:57 +00:00
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
2018-11-05 10:10:14 +00:00
|
|
|
// For each frame we produce, we set the timestamp when it should be displayed
|
|
|
|
// (pts = presentation time stamp)
|
|
|
|
// The autovideosink will use this information to display the frame at the right time.
|
2018-05-18 08:37:57 +00:00
|
|
|
buffer.set_pts(i * 500 * gst::MSECOND);
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// At this point, buffer is only a reference to an existing memory region somewhere.
|
|
|
|
// When we want to access its content, we have to map it while requesting the required
|
|
|
|
// mode of access (read, read/write).
|
|
|
|
// See: https://gstreamer.freedesktop.org/documentation/plugin-development/advanced/allocation.html
|
2018-05-18 08:37:57 +00:00
|
|
|
let mut data = buffer.map_writable().unwrap();
|
2017-08-11 14:59:05 +00:00
|
|
|
|
2018-05-18 08:37:57 +00:00
|
|
|
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-11 14:59:05 +00:00
|
|
|
}
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2018-05-18 08:37:57 +00:00
|
|
|
i += 1;
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2018-05-18 08:37:57 +00:00
|
|
|
// appsrc already handles the error here
|
|
|
|
let _ = appsrc.push_buffer(buffer);
|
2018-10-08 12:02:23 +00:00
|
|
|
})
|
|
|
|
.build(),
|
2018-05-18 08:37:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(pipeline)
|
|
|
|
}
|
2017-08-01 18:29:49 +00:00
|
|
|
|
2018-05-18 08:37:57 +00:00
|
|
|
fn main_loop(pipeline: gst::Pipeline) -> Result<(), Error> {
|
2017-11-12 09:15:37 +00:00
|
|
|
pipeline.set_state(gst::State::Playing).into_result()?;
|
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
|
|
|
|
2018-12-27 22:06:03 +00:00
|
|
|
for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) {
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::MessageView;
|
|
|
|
|
2017-08-01 18:29:49 +00:00
|
|
|
match msg.view() {
|
|
|
|
MessageView::Eos(..) => break,
|
|
|
|
MessageView::Error(err) => {
|
2017-11-12 09:15:37 +00:00
|
|
|
pipeline.set_state(gst::State::Null).into_result()?;
|
|
|
|
Err(ErrorMessage {
|
2018-07-27 10:36:40 +00:00
|
|
|
src: err
|
|
|
|
.get_src()
|
2017-11-16 11:58:56 +00:00
|
|
|
.map(|s| s.get_path_string())
|
2017-11-27 11:01:03 +00:00
|
|
|
.unwrap_or_else(|| String::from("None")),
|
2017-11-12 09:15:37 +00:00
|
|
|
error: err.get_error().description().into(),
|
|
|
|
debug: err.get_debug(),
|
|
|
|
cause: err.get_error(),
|
|
|
|
})?;
|
2017-08-01 18:29:49 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 09:15:37 +00:00
|
|
|
pipeline.set_state(gst::State::Null).into_result()?;
|
2017-08-03 14:52:08 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-11-12 18:07:02 +00:00
|
|
|
fn example_main() {
|
2018-05-18 08:37:57 +00:00
|
|
|
match create_pipeline().and_then(main_loop) {
|
2017-08-03 14:52:08 +00:00
|
|
|
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);
|
|
|
|
}
|