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

View file

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

View file

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

View file

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