Pass Urls directly to the Source/Sink implementations instead of having them handle strings

This commit is contained in:
Sebastian Dröge 2016-05-24 23:24:05 +03:00
parent 84c0d4c74c
commit 3db8882ce8
5 changed files with 77 additions and 92 deletions

View file

@ -51,35 +51,24 @@ impl FileSink {
}
impl Sink for FileSink {
fn set_uri(&mut self, uri_str: Option<&str>) -> bool {
match uri_str {
fn set_uri(&mut self, uri: Option<Url>) -> bool {
match uri {
None => {
let mut location = self.location.lock().unwrap();
*location = None;
return true;
},
Some(ref uri_str) => {
let uri_parsed = Url::parse(uri_str);
match uri_parsed {
Ok(u) => {
match u.to_file_path().ok() {
Some(p) => {
let mut location = self.location.lock().unwrap();
*location = Some(p);
return true;
},
None => {
let mut location = self.location.lock().unwrap();
*location = None;
println_err!("Unsupported file URI '{}'", uri_str);
return false;
}
}
Some(ref uri) => {
match uri.to_file_path().ok() {
Some(p) => {
let mut location = self.location.lock().unwrap();
*location = Some(p);
return true;
},
Err(err) => {
None => {
let mut location = self.location.lock().unwrap();
*location = None;
println_err!("Failed to parse URI '{}': {}", uri_str, err);
println_err!("Unsupported file URI '{}'", uri.as_str());
return false;
}
}
@ -87,12 +76,11 @@ impl Sink for FileSink {
}
}
fn get_uri(&self) -> Option<String> {
fn get_uri(&self) -> Option<Url> {
let location = self.location.lock().unwrap();
(*location).as_ref()
.map(|l| Url::from_file_path(l).ok())
.and_then(|i| i) // join()
.map(|u| u.into_string())
}
fn start(&mut self) -> bool {

View file

@ -52,35 +52,24 @@ impl FileSrc {
}
impl Source for FileSrc {
fn set_uri(&mut self, uri_str: Option<&str>) -> bool {
match uri_str {
fn set_uri(&mut self, uri: Option<Url>) -> bool {
match uri {
None => {
let mut location = self.location.lock().unwrap();
*location = None;
return true;
},
Some(ref uri_str) => {
let uri_parsed = Url::parse(uri_str);
match uri_parsed {
Ok(u) => {
match u.to_file_path().ok() {
Some(p) => {
let mut location = self.location.lock().unwrap();
*location = Some(p);
return true;
},
None => {
let mut location = self.location.lock().unwrap();
*location = None;
println_err!("Unsupported file URI '{}'", uri_str);
return false;
}
}
Some(uri) => {
match uri.to_file_path().ok() {
Some(p) => {
let mut location = self.location.lock().unwrap();
*location = Some(p);
return true;
},
Err(err) => {
None => {
let mut location = self.location.lock().unwrap();
*location = None;
println_err!("Failed to parse URI '{}': {}", uri_str, err);
println_err!("Unsupported file URI '{}'", uri.as_str());
return false;
}
}
@ -88,12 +77,11 @@ impl Source for FileSrc {
}
}
fn get_uri(&self) -> Option<String> {
fn get_uri(&self) -> Option<Url> {
let location = self.location.lock().unwrap();
(*location).as_ref()
.map(|l| Url::from_file_path(l).ok())
.and_then(|i| i) // join()
.map(|u| u.into_string())
}
fn is_seekable(&self) -> bool {

View file

@ -126,48 +126,37 @@ impl HttpSrc {
}
impl Source for HttpSrc {
fn set_uri(&mut self, uri_str: Option<&str>) -> bool {
fn set_uri(&mut self, uri: Option<Url>) -> bool {
if self.response.is_some() {
println_err!("Can't set URI after starting");
return false;
}
match uri_str {
match uri {
None => {
let mut url = self.url.lock().unwrap();
*url = None;
return true;
},
Some(ref uri_str) => {
let uri_parsed = Url::parse(uri_str);
match uri_parsed {
Ok(u) => {
if u.scheme() == "http" ||
u.scheme() == "https" {
let mut url = self.url.lock().unwrap();
*url = Some(u);
return true;
} else {
let mut url = self.url.lock().unwrap();
*url = None;
println_err!("Unsupported file URI '{}'", uri_str);
return false;
}
},
Err(err) => {
let mut url = self.url.lock().unwrap();
*url = None;
println_err!("Failed to parse URI '{}': {}", uri_str, err);
return false;
}
Some(uri) => {
if uri.scheme() == "http" ||
uri.scheme() == "https" {
let mut url = self.url.lock().unwrap();
*url = Some(uri);
return true;
} else {
let mut url = self.url.lock().unwrap();
*url = None;
println_err!("Unsupported URI '{}'", uri.as_str());
return false;
}
}
}
}
fn get_uri(&self) -> Option<String> {
fn get_uri(&self) -> Option<Url> {
let url = self.url.lock().unwrap();
(*url).as_ref().map(|u| String::from(u.as_str()))
(*url).as_ref().map(|u| u.clone())
}
fn is_seekable(&self) -> bool {

View file

@ -20,13 +20,16 @@ use libc::c_char;
use std::ffi::{CStr, CString};
use std::slice;
use std::ptr;
use std::io::Write;
use url::Url;
use utils::*;
pub trait Sink: Sync + Send {
// Called from any thread at any time
fn set_uri(&mut self, uri_str: Option<&str>) -> bool;
fn get_uri(&self) -> Option<String>;
fn set_uri(&mut self, uri: Option<Url>) -> bool;
fn get_uri(&self) -> Option<Url>;
// Called from the streaming thread only
fn start(&mut self) -> bool;
@ -41,23 +44,30 @@ pub extern "C" fn sink_drop(ptr: *mut Box<Sink>) {
#[no_mangle]
pub extern "C" fn sink_set_uri(ptr: *mut Box<Sink>, uri_ptr: *const c_char) -> GBoolean{
let source: &mut Box<Sink> = unsafe { &mut *ptr };
let sink: &mut Box<Sink> = unsafe { &mut *ptr };
if uri_ptr.is_null() {
GBoolean::from_bool(source.set_uri(None))
GBoolean::from_bool(sink.set_uri(None))
} else {
let uri = unsafe { CStr::from_ptr(uri_ptr) };
GBoolean::from_bool(source.set_uri(Some(uri.to_str().unwrap())))
let uri_str = unsafe { CStr::from_ptr(uri_ptr) }.to_str().unwrap();
match Url::parse(uri_str) {
Ok(uri) => GBoolean::from_bool(sink.set_uri(Some(uri))),
Err(err) => {
sink.set_uri(None);
println_err!("Failed to parse URI '{}': {}", uri_str, err);
GBoolean::False
}
}
}
}
#[no_mangle]
pub extern "C" fn sink_get_uri(ptr: *const Box<Sink>) -> *mut c_char {
let source: &Box<Sink> = unsafe { &*ptr };
let sink: &Box<Sink> = unsafe { &*ptr };
match source.get_uri() {
match sink.get_uri() {
Some(uri) =>
CString::new(uri.into_bytes()).unwrap().into_raw(),
CString::new(uri.into_string().into_bytes()).unwrap().into_raw(),
None =>
ptr::null_mut()
}
@ -65,22 +75,22 @@ pub extern "C" fn sink_get_uri(ptr: *const Box<Sink>) -> *mut c_char {
#[no_mangle]
pub extern "C" fn sink_render(ptr: *mut Box<Sink>, data_ptr: *const u8, data_len: usize) -> GstFlowReturn {
let source: &mut Box<Sink> = unsafe { &mut *ptr };
let sink: &mut Box<Sink> = unsafe { &mut *ptr };
let data = unsafe { slice::from_raw_parts(data_ptr, data_len) };
source.render(data)
sink.render(data)
}
#[no_mangle]
pub extern "C" fn sink_start(ptr: *mut Box<Sink>) -> GBoolean {
let source: &mut Box<Sink> = unsafe { &mut *ptr };
let sink: &mut Box<Sink> = unsafe { &mut *ptr };
GBoolean::from_bool(source.start())
GBoolean::from_bool(sink.start())
}
#[no_mangle]
pub extern "C" fn sink_stop(ptr: *mut Box<Sink>) -> GBoolean {
let source: &mut Box<Sink> = unsafe { &mut *ptr };
let sink: &mut Box<Sink> = unsafe { &mut *ptr };
GBoolean::from_bool(source.stop())
GBoolean::from_bool(sink.stop())
}

View file

@ -19,13 +19,16 @@ use libc::c_char;
use std::ffi::{CStr, CString};
use std::slice;
use std::ptr;
use std::io::Write;
use url::Url;
use utils::*;
pub trait Source: Sync + Send {
// Called from any thread at any time
fn set_uri(&mut self, uri_str: Option<&str>) -> bool;
fn get_uri(&self) -> Option<String>;
fn set_uri(&mut self, uri: Option<Url>) -> bool;
fn get_uri(&self) -> Option<Url>;
// Called from any thread between start/stop
fn is_seekable(&self) -> bool;
@ -50,8 +53,15 @@ pub extern "C" fn source_set_uri(ptr: *mut Box<Source>, uri_ptr: *const c_char)
if uri_ptr.is_null() {
GBoolean::from_bool(source.set_uri(None))
} else {
let uri = unsafe { CStr::from_ptr(uri_ptr) };
GBoolean::from_bool(source.set_uri(Some(uri.to_str().unwrap())))
let uri_str = unsafe { CStr::from_ptr(uri_ptr) }.to_str().unwrap();
match Url::parse(uri_str) {
Ok(uri) => GBoolean::from_bool(source.set_uri(Some(uri))),
Err(err) => {
source.set_uri(None);
println_err!("Failed to parse URI '{}': {}", uri_str, err);
GBoolean::False
}
}
}
}
@ -61,7 +71,7 @@ pub extern "C" fn source_get_uri(ptr: *mut Box<Source>) -> *mut c_char {
match source.get_uri() {
Some(uri) =>
CString::new(uri.into_bytes()).unwrap().into_raw(),
CString::new(uri.into_string().into_bytes()).unwrap().into_raw(),
None =>
ptr::null_mut()
}