RsFileSink writes buffers to a file

This commit is contained in:
Luis de Bethencourt 2016-05-15 14:44:32 +01:00 committed by Sebastian Dröge
parent 87bd914cad
commit d6d306f916
3 changed files with 35 additions and 14 deletions

View file

@ -76,8 +76,17 @@ impl Sink for FileSink {
match self.location {
None => return false,
Some(ref location) => {
match File::create(location.as_path()) {
Ok(file) => {
self.file = Some(file);
return true;
},
Err(err) => {
println_err!("Could not open file for writing '{}': {}", location.to_str().unwrap_or("Non-UTF8 path"), err.to_string());
return false;
}
}
},
}
}
@ -88,8 +97,20 @@ impl Sink for FileSink {
true
}
fn render(&mut self) -> Result<usize, GstFlowReturn> {
println!("Got a buffer!");
Err(GstFlowReturn::Ok)
fn render(&mut self, data: &mut [u8]) -> GstFlowReturn {
match self.file {
None => return GstFlowReturn::Error,
Some(ref mut f) => {
match f.write_all(data) {
Ok(_) => {
return GstFlowReturn::Ok
},
Err(err) => {
println_err!("Failed to write: {}", err);
return GstFlowReturn::Error
},
}
},
}
}
}

View file

@ -16,7 +16,7 @@ static GHashTable *sinks;
/* Declarations for Rust code */
extern gboolean sinks_register (void *plugin);
extern GstFlowReturn sink_render (void * filesink);
extern GstFlowReturn sink_render (void * filesink, void * data, size_t data_len);
extern gboolean sink_set_uri (void * filesink, const char *uri);
extern char * sink_get_uri (void * filesink);
extern gboolean sink_start (void * filesink);
@ -132,9 +132,12 @@ static GstFlowReturn
gst_rs_sink_render (GstBaseSink * basesink, GstBuffer * buffer)
{
GstRsSink *sink = GST_RS_SINK (basesink);
GstMapInfo map;
GstFlowReturn ret;
ret = sink_render (sink->instance);
gst_buffer_map (buffer, &map, GST_MAP_READ);
ret = sink_render (sink->instance, map.data, map.size);
gst_buffer_unmap (buffer, &map);
return ret;
}

View file

@ -1,5 +1,6 @@
use libc::{c_char};
use std::ffi::{CStr, CString};
use std::slice;
use std::ptr;
use utils::*;
@ -9,7 +10,7 @@ pub trait Sink {
fn get_uri(&self) -> Option<String>;
fn start(&mut self) -> bool;
fn stop(&mut self) -> bool;
fn render(&mut self) -> Result<usize, GstFlowReturn>;
fn render(&mut self, data: &mut [u8]) -> GstFlowReturn;
}
#[no_mangle]
@ -37,15 +38,11 @@ pub extern "C" fn sink_get_uri(ptr: *mut Box<Sink>) -> *mut c_char {
}
#[no_mangle]
pub extern "C" fn sink_render(ptr: *mut Box<Sink>) -> GstFlowReturn {
pub extern "C" fn sink_render(ptr: *mut Box<Sink>, data_ptr: *mut u8, data_len: usize) -> GstFlowReturn {
let source: &mut Box<Sink> = unsafe { &mut *ptr };
match source.render() {
Ok(data) => {
GstFlowReturn::Ok
},
Err(ret) => ret,
}
let mut data = unsafe { slice::from_raw_parts_mut(data_ptr, data_len) };
source.render(data)
}
#[no_mangle]