Use gstreamer-video API in appsrc example and make frame generation a bit more efficient

This commit is contained in:
Sebastian Dröge 2017-08-11 17:59:05 +03:00
parent b655c838b2
commit b392c82ba9
3 changed files with 24 additions and 19 deletions

1
Cargo.lock generated
View file

@ -114,6 +114,7 @@ dependencies = [
"gstreamer-app 0.1.0", "gstreamer-app 0.1.0",
"gstreamer-audio 0.1.0", "gstreamer-audio 0.1.0",
"gstreamer-player 0.1.0", "gstreamer-player 0.1.0",
"gstreamer-video 0.1.0",
"gtk 0.1.3 (git+https://github.com/gtk-rs/gtk)", "gtk 0.1.3 (git+https://github.com/gtk-rs/gtk)",
"send-cell 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "send-cell 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -8,6 +8,7 @@ glib = { version = "0.1.3", git = "https://github.com/gtk-rs/glib" }
gstreamer = { path = "../gstreamer" } gstreamer = { path = "../gstreamer" }
gstreamer-app = { path = "../gstreamer-app" } gstreamer-app = { path = "../gstreamer-app" }
gstreamer-audio = { path = "../gstreamer-audio" } gstreamer-audio = { path = "../gstreamer-audio" }
gstreamer-video = { path = "../gstreamer-video" }
gstreamer-player = { path = "../gstreamer-player", optional = true } gstreamer-player = { path = "../gstreamer-player", optional = true }
gtk = { version = "0.1.3", git = "https://github.com/gtk-rs/gtk", features = ["v3_6"] } gtk = { version = "0.1.3", git = "https://github.com/gtk-rs/gtk", features = ["v3_6"] }
gio = { version = "0.1.3", git = "https://github.com/gtk-rs/gio" } gio = { version = "0.1.3", git = "https://github.com/gtk-rs/gio" }

View file

@ -2,6 +2,7 @@ extern crate gstreamer as gst;
use gst::*; use gst::*;
extern crate gstreamer_app as gst_app; extern crate gstreamer_app as gst_app;
use gst_app::*; use gst_app::*;
extern crate gstreamer_video as gst_video;
extern crate glib; extern crate glib;
@ -30,15 +31,13 @@ fn create_pipeline() -> Result<(Pipeline, AppSrc), utils::ExampleError> {
let appsrc = src.clone() let appsrc = src.clone()
.dynamic_cast::<AppSrc>() .dynamic_cast::<AppSrc>()
.expect("Source element is expected to be an appsrc!"); .expect("Source element is expected to be an appsrc!");
appsrc.set_caps(&Caps::new_simple(
"video/x-raw", let info = gst_video::VideoInfo::new(gst_video::VideoFormat::Bgrx, WIDTH as u32, HEIGHT as u32)
&[ .fps(Fraction::new(2, 1))
("format", &"BGRx"), .build()
("width", &(WIDTH as i32)), .unwrap();
("height", &(HEIGHT as i32)),
("framerate", &Fraction::new(2, 1)), appsrc.set_caps(&info.to_caps().unwrap());
],
));
appsrc.set_property_format(Format::Time); appsrc.set_property_format(Format::Time);
appsrc.set_max_bytes(1); appsrc.set_max_bytes(1);
appsrc.set_property_block(true); appsrc.set_property_block(true);
@ -53,21 +52,25 @@ fn main_loop() -> Result<(), utils::ExampleError> {
for i in 0..100 { for i in 0..100 {
println!("Producing frame {}", i); println!("Producing frame {}", i);
// TODO: This is not very efficient
let mut vec = Vec::with_capacity(WIDTH * HEIGHT * 4);
let r = if i % 2 == 0 { 0 } else { 255 }; let r = if i % 2 == 0 { 0 } else { 255 };
let g = if i % 3 == 0 { 0 } else { 255 }; let g = if i % 3 == 0 { 0 } else { 255 };
let b = if i % 5 == 0 { 0 } else { 255 }; let b = if i % 5 == 0 { 0 } else { 255 };
for _ in 0..(WIDTH * HEIGHT) { let mut buffer = gst::Buffer::with_size(WIDTH * HEIGHT * 4).unwrap();
vec.push(b); {
vec.push(g); let buffer = buffer.get_mut().unwrap();
vec.push(r); buffer.set_pts(i * 500_000_000);
vec.push(0);
}
let mut buffer = Buffer::from_vec(vec).expect("Unable to create a Buffer"); let mut data = buffer.map_writable().unwrap();
buffer.get_mut().unwrap().set_pts(i * 500_000_000);
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;
}
}
if appsrc.push_buffer(buffer) != FlowReturn::Ok { if appsrc.push_buffer(buffer) != FlowReturn::Ok {
break; break;