diff --git a/src/rsfilesink.rs b/src/rsfilesink.rs index 24547e18..20bc6518 100644 --- a/src/rsfilesink.rs +++ b/src/rsfilesink.rs @@ -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) -> 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 { + fn get_uri(&self) -> Option { 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 { diff --git a/src/rsfilesrc.rs b/src/rsfilesrc.rs index 0358fd85..007024b4 100644 --- a/src/rsfilesrc.rs +++ b/src/rsfilesrc.rs @@ -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) -> 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 { + fn get_uri(&self) -> Option { 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 { diff --git a/src/rshttpsrc.rs b/src/rshttpsrc.rs index 34beb5bd..e189f321 100644 --- a/src/rshttpsrc.rs +++ b/src/rshttpsrc.rs @@ -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) -> 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 { + fn get_uri(&self) -> Option { 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 { diff --git a/src/rssink.rs b/src/rssink.rs index a4710436..8a75c12d 100644 --- a/src/rssink.rs +++ b/src/rssink.rs @@ -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; + fn set_uri(&mut self, uri: Option) -> bool; + fn get_uri(&self) -> Option; // Called from the streaming thread only fn start(&mut self) -> bool; @@ -41,23 +44,30 @@ pub extern "C" fn sink_drop(ptr: *mut Box) { #[no_mangle] pub extern "C" fn sink_set_uri(ptr: *mut Box, uri_ptr: *const c_char) -> GBoolean{ - let source: &mut Box = unsafe { &mut *ptr }; + let sink: &mut Box = 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) -> *mut c_char { - let source: &Box = unsafe { &*ptr }; + let sink: &Box = 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) -> *mut c_char { #[no_mangle] pub extern "C" fn sink_render(ptr: *mut Box, data_ptr: *const u8, data_len: usize) -> GstFlowReturn { - let source: &mut Box = unsafe { &mut *ptr }; + let sink: &mut Box = 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) -> GBoolean { - let source: &mut Box = unsafe { &mut *ptr }; + let sink: &mut Box = unsafe { &mut *ptr }; - GBoolean::from_bool(source.start()) + GBoolean::from_bool(sink.start()) } #[no_mangle] pub extern "C" fn sink_stop(ptr: *mut Box) -> GBoolean { - let source: &mut Box = unsafe { &mut *ptr }; + let sink: &mut Box = unsafe { &mut *ptr }; - GBoolean::from_bool(source.stop()) + GBoolean::from_bool(sink.stop()) } diff --git a/src/rssource.rs b/src/rssource.rs index 0de7faf2..62ce995e 100644 --- a/src/rssource.rs +++ b/src/rssource.rs @@ -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; + fn set_uri(&mut self, uri: Option) -> bool; + fn get_uri(&self) -> Option; // 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, 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) -> *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() }