2016-05-15 15:54:09 +00:00
|
|
|
// Copyright (C) 2016 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
// 2016 Luis de Bethencourt <luisbg@osg.samsung.com>
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
// Boston, MA 02110-1301, USA.
|
|
|
|
|
2016-05-15 14:43:13 +00:00
|
|
|
use libc::c_char;
|
2016-07-20 09:46:03 +00:00
|
|
|
use std::os::raw::c_void;
|
2016-05-14 13:31:39 +00:00
|
|
|
use std::ffi::{CStr, CString};
|
2016-05-15 13:44:32 +00:00
|
|
|
use std::slice;
|
2016-05-14 13:31:39 +00:00
|
|
|
use std::ptr;
|
2016-05-24 20:24:05 +00:00
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2016-05-24 20:24:05 +00:00
|
|
|
use url::Url;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
|
|
|
use utils::*;
|
2016-08-27 08:16:17 +00:00
|
|
|
use error::*;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum SinkError {
|
|
|
|
Failure,
|
|
|
|
OpenFailed,
|
|
|
|
NotFound,
|
|
|
|
WriteFailed,
|
|
|
|
SeekFailed,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToGError for SinkError {
|
|
|
|
fn to_gerror(&self) -> (u32, i32) {
|
|
|
|
match *self {
|
|
|
|
SinkError::Failure => (gst_library_error_domain(), 1),
|
|
|
|
SinkError::OpenFailed => (gst_resource_error_domain(), 6),
|
|
|
|
SinkError::NotFound => (gst_resource_error_domain(), 3),
|
|
|
|
SinkError::WriteFailed => (gst_resource_error_domain(), 10),
|
|
|
|
SinkError::SeekFailed => (gst_resource_error_domain(), 11),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
pub struct SinkWrapper {
|
|
|
|
sink_raw: *mut c_void,
|
|
|
|
uri: Mutex<(Option<Url>, bool)>,
|
|
|
|
uri_validator: Box<UriValidator>,
|
|
|
|
sink: Mutex<Box<Sink>>,
|
2016-07-20 09:46:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
pub trait Sink {
|
|
|
|
fn uri_validator(&self) -> Box<UriValidator>;
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
fn start(&mut self, uri: &Url) -> Result<(), ErrorMessage>;
|
|
|
|
fn stop(&mut self) -> Result<(), ErrorMessage>;
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
fn render(&mut self, data: &[u8]) -> Result<(), FlowError>;
|
2016-07-20 09:46:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
impl SinkWrapper {
|
|
|
|
fn new(sink_raw: *mut c_void, sink: Box<Sink>) -> SinkWrapper {
|
|
|
|
SinkWrapper {
|
|
|
|
sink_raw: sink_raw,
|
|
|
|
uri: Mutex::new((None, false)),
|
|
|
|
uri_validator: sink.uri_validator(),
|
|
|
|
sink: Mutex::new(sink),
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 09:46:03 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn sink_new(sink: *mut c_void,
|
2016-09-01 21:52:28 +00:00
|
|
|
create_instance: fn() -> Box<Sink>)
|
|
|
|
-> *mut SinkWrapper {
|
|
|
|
Box::into_raw(Box::new(SinkWrapper::new(sink, create_instance())))
|
2016-07-20 09:46:03 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 14:47:38 +00:00
|
|
|
#[no_mangle]
|
2016-09-01 21:52:28 +00:00
|
|
|
pub unsafe extern "C" fn sink_drop(ptr: *mut SinkWrapper) {
|
2016-08-22 20:03:06 +00:00
|
|
|
Box::from_raw(ptr);
|
2016-05-15 14:47:38 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 13:31:39 +00:00
|
|
|
#[no_mangle]
|
2016-09-01 21:52:28 +00:00
|
|
|
pub unsafe extern "C" fn sink_set_uri(ptr: *mut SinkWrapper,
|
2016-08-22 21:07:44 +00:00
|
|
|
uri_ptr: *const c_char,
|
|
|
|
cerr: *mut c_void)
|
|
|
|
-> GBoolean {
|
2016-09-01 21:52:28 +00:00
|
|
|
let wrap: &mut SinkWrapper = &mut *ptr;
|
|
|
|
let uri_storage = &mut wrap.uri.lock().unwrap();
|
|
|
|
|
|
|
|
if uri_storage.1 {
|
|
|
|
UriError::new(UriErrorKind::BadState, Some("Already started".to_string()))
|
|
|
|
.into_gerror(cerr);
|
|
|
|
return GBoolean::False;
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
uri_storage.0 = None;
|
2016-05-14 13:31:39 +00:00
|
|
|
if uri_ptr.is_null() {
|
2016-09-01 21:52:28 +00:00
|
|
|
GBoolean::True
|
2016-05-14 13:31:39 +00:00
|
|
|
} else {
|
2016-08-22 20:03:06 +00:00
|
|
|
let uri_str = CStr::from_ptr(uri_ptr).to_str().unwrap();
|
|
|
|
|
2016-05-24 20:24:05 +00:00
|
|
|
match Url::parse(uri_str) {
|
2016-08-22 21:07:44 +00:00
|
|
|
Ok(uri) => {
|
2016-09-01 21:52:28 +00:00
|
|
|
if let Err(err) = (*wrap.uri_validator)(&uri) {
|
2016-08-24 21:47:56 +00:00
|
|
|
err.into_gerror(cerr);
|
2016-09-01 21:52:28 +00:00
|
|
|
|
2016-08-22 21:07:44 +00:00
|
|
|
GBoolean::False
|
|
|
|
} else {
|
2016-09-01 21:52:28 +00:00
|
|
|
uri_storage.0 = Some(uri);
|
|
|
|
|
2016-08-22 21:07:44 +00:00
|
|
|
GBoolean::True
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 20:24:05 +00:00
|
|
|
Err(err) => {
|
2016-08-24 21:47:56 +00:00
|
|
|
UriError::new(UriErrorKind::BadUri,
|
|
|
|
Some(format!("Failed to parse URI '{}': {}", uri_str, err)))
|
|
|
|
.into_gerror(cerr);
|
2016-09-01 21:52:28 +00:00
|
|
|
|
2016-05-24 20:24:05 +00:00
|
|
|
GBoolean::False
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2016-09-01 21:52:28 +00:00
|
|
|
pub unsafe extern "C" fn sink_get_uri(ptr: *const SinkWrapper) -> *mut c_char {
|
|
|
|
let wrap: &SinkWrapper = &*ptr;
|
|
|
|
let uri_storage = &mut wrap.uri.lock().unwrap();
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
match uri_storage.0 {
|
|
|
|
Some(ref uri) => CString::new(uri.as_ref().as_bytes()).unwrap().into_raw(),
|
2016-07-20 08:28:58 +00:00
|
|
|
None => ptr::null_mut(),
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2016-09-01 21:52:28 +00:00
|
|
|
pub unsafe extern "C" fn sink_render(ptr: *mut SinkWrapper,
|
2016-08-22 20:03:06 +00:00
|
|
|
data_ptr: *const u8,
|
|
|
|
data_len: usize)
|
|
|
|
-> GstFlowReturn {
|
2016-09-01 21:52:28 +00:00
|
|
|
let wrap: &mut SinkWrapper = &mut *ptr;
|
|
|
|
let sink = &mut wrap.sink.lock().unwrap();
|
2016-08-22 20:03:06 +00:00
|
|
|
let data = slice::from_raw_parts(data_ptr, data_len);
|
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
match sink.render(data) {
|
|
|
|
Ok(..) => GstFlowReturn::Ok,
|
|
|
|
Err(flow_error) => {
|
|
|
|
match flow_error {
|
|
|
|
FlowError::NotNegotiated(ref msg) |
|
2016-09-01 21:52:28 +00:00
|
|
|
FlowError::Error(ref msg) => msg.post(wrap.sink_raw),
|
2016-08-27 08:16:17 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
flow_error.to_native()
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2016-09-01 21:52:28 +00:00
|
|
|
pub unsafe extern "C" fn sink_start(ptr: *mut SinkWrapper) -> GBoolean {
|
|
|
|
let wrap: &mut SinkWrapper = &mut *ptr;
|
|
|
|
let sink = &mut wrap.sink.lock().unwrap();
|
|
|
|
let uri_storage = &mut wrap.uri.lock().unwrap();
|
|
|
|
|
|
|
|
let (uri, started) = match **uri_storage {
|
|
|
|
(Some(ref uri), ref mut started) => (uri, started),
|
|
|
|
(None, _) => {
|
|
|
|
error_msg!(SinkError::OpenFailed, ["No URI given"]).post(wrap.sink_raw);
|
|
|
|
return GBoolean::False;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match sink.start(uri) {
|
|
|
|
Ok(..) => {
|
|
|
|
*started = true;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
GBoolean::True
|
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
Err(ref msg) => {
|
2016-09-01 21:52:28 +00:00
|
|
|
msg.post(wrap.sink_raw);
|
2016-08-27 08:16:17 +00:00
|
|
|
GBoolean::False
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2016-09-01 21:52:28 +00:00
|
|
|
pub unsafe extern "C" fn sink_stop(ptr: *mut SinkWrapper) -> GBoolean {
|
|
|
|
let wrap: &mut SinkWrapper = &mut *ptr;
|
|
|
|
let sink = &mut wrap.sink.lock().unwrap();
|
|
|
|
let uri_storage = &mut wrap.uri.lock().unwrap();
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
match sink.stop() {
|
2016-09-01 21:52:28 +00:00
|
|
|
Ok(..) => {
|
|
|
|
uri_storage.1 = false;
|
|
|
|
GBoolean::True
|
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
Err(ref msg) => {
|
2016-09-01 21:52:28 +00:00
|
|
|
msg.post(wrap.sink_raw);
|
2016-08-27 08:16:17 +00:00
|
|
|
GBoolean::False
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|