Working proof of concept with up to date gstreamer bindings and Rust 2018.

This commit is contained in:
Sean McNamara 2019-12-25 12:19:48 -05:00
parent 6707389732
commit ed04c049cc

View file

@ -8,7 +8,7 @@ use glib::MainLoop;
use zerocopy::*; use zerocopy::*;
pub struct GstreamerSink { pub struct GstreamerSink {
tx: SyncSender<Vec<i16>>, tx: SyncSender<Vec<u8>>,
pipeline: gst::Pipeline pipeline: gst::Pipeline
} }
@ -21,6 +21,7 @@ impl Open for GstreamerSink {
Some(x) => format!("{}{}", pipeline_str_preamble, x), Some(x) => format!("{}{}", pipeline_str_preamble, x),
None => format!("{}{}", pipeline_str_preamble, pipeline_str_rest) None => format!("{}{}", pipeline_str_preamble, pipeline_str_rest)
}; };
println!("Pipeline: {}", pipeline_str);
gst::init().unwrap(); gst::init().unwrap();
let pipelinee = gst::parse_launch(&*pipeline_str).expect("New Pipeline error"); let pipelinee = gst::parse_launch(&*pipeline_str).expect("New Pipeline error");
@ -29,47 +30,20 @@ impl Open for GstreamerSink {
let mut mainloop = glib::MainLoop::new(None, false); let mut mainloop = glib::MainLoop::new(None, false);
let mut appsrce : gst::Element = pipeline.get_by_name("appsrc0").expect("Couldn't get appsrc from pipeline"); let mut appsrce : gst::Element = pipeline.get_by_name("appsrc0").expect("Couldn't get appsrc from pipeline");
let mut appsrc : gst_app::AppSrc = appsrce.dynamic_cast::<gst_app::AppSrc>().expect("Couldnt cast AppSrc element at runtime!"); let mut appsrc : gst_app::AppSrc = appsrce.dynamic_cast::<gst_app::AppSrc>().expect("Couldnt cast AppSrc element at runtime!");
//let mut appsrc = gst_app::AppSrc::new_from_element(appsrc_element.to_element());
let bufferpool = gst::BufferPool::new(); let bufferpool = gst::BufferPool::new();
let appsrc_caps = appsrc.get_caps().expect("get appsrc caps failed"); let appsrc_caps = appsrc.get_caps().expect("get appsrc caps failed");
let mut conf = bufferpool.get_config(); let mut conf = bufferpool.get_config();
conf.set_params(Some(&appsrc_caps), 2048 * 2, 0, 0); conf.set_params(Some(&appsrc_caps), 8192, 0, 0);
if bufferpool.set_active(true).is_err(){ bufferpool.set_config(conf).expect("Couldn't configure the buffer pool");
panic!("Couldn't activate buffer pool"); bufferpool.set_active(true).expect("Couldn't activate buffer pool");
}
/* let (tx, rx) = sync_channel::<Vec<u8>>(128);
thread::spawn(move || {
let bus_receiver = bus.receiver();
for message in bus_receiver.iter() {
match message.parse() {
gst::message::StateChanged(x) =>
println!("element `{}` state changed", message.src_name()),
gst::message::Error(x) => {
println!("error msg from element `{}`: {}, quitting", message.src_name(), error.message());
break;
},
gst::message::Eos(ref _msg) => {
println!("eos received; quitting");
break;
},
_ =>
println!("Pipe message: {} from {} at {}", message.type_name(), message.src_name(), message.timestamp())
}
}
});*/
let (tx, rx) = sync_channel::<Vec<i16>>(64);
thread::spawn(move || { thread::spawn(move || {
for data in rx { for data in rx {
let mut buffer = bufferpool.acquire_buffer(None).expect("acquire buffer"); let mut buffer = bufferpool.acquire_buffer(None).expect("acquire buffer");
//assert!(data.len() <= buffer.len::<i16>());
let mutbuf = buffer.make_mut(); let mutbuf = buffer.make_mut();
mutbuf.set_size(data.len() * 2); mutbuf.set_size(data.len());
mutbuf.map_writable().unwrap().as_mut_slice().clone_from_slice(&data[..].as_bytes()); mutbuf.copy_from_slice(0, data.as_bytes());
//buffer.set_live(true);
let res = appsrc.push_buffer(buffer).expect("Failed to push buffer"); let res = appsrc.push_buffer(buffer).expect("Failed to push buffer");
} }
}); });
@ -110,20 +84,17 @@ impl Open for GstreamerSink {
impl Sink for GstreamerSink { impl Sink for GstreamerSink {
fn start(&mut self) -> io::Result<()> { fn start(&mut self) -> io::Result<()> {
//self.pipeline.play();
self.pipeline.set_state(gst::State::Playing).expect("Unable to set the pipeline to the `Playing` state"); self.pipeline.set_state(gst::State::Playing).expect("Unable to set the pipeline to the `Playing` state");
Ok(()) Ok(())
} }
fn stop(&mut self) -> io::Result<()> { fn stop(&mut self) -> io::Result<()> {
//self.pipeline.pause();
self.pipeline.set_state(gst::State::Paused).expect("Unable to set the pipeline to the `Paused` state"); self.pipeline.set_state(gst::State::Paused).expect("Unable to set the pipeline to the `Paused` state");
Ok(()) Ok(())
} }
fn write(&mut self, data: &[i16]) -> io::Result<()> { fn write(&mut self, data: &[i16]) -> io::Result<()> {
// Copy expensively to avoid thread synchronization // Copy expensively (in to_vec()) to avoid thread synchronization
let data = data.to_vec(); let deighta : &[u8] = data.as_bytes();
self.tx.send(data).expect("tx send failed in write function"); self.tx.send(deighta.to_vec()).expect("tx send failed in write function");
Ok(()) Ok(())
} }
} }