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-14 13:31:39 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
use std::io::Write;
|
2016-05-23 17:29:01 +00:00
|
|
|
use std::sync::Mutex;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
|
|
|
use utils::*;
|
|
|
|
use rssink::*;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FileSink {
|
2016-05-23 17:29:01 +00:00
|
|
|
location: Mutex<Option<PathBuf>>,
|
2016-05-14 13:31:39 +00:00
|
|
|
file: Option<File>,
|
|
|
|
position: u64,
|
|
|
|
}
|
|
|
|
|
2016-05-15 15:26:17 +00:00
|
|
|
unsafe impl Sync for FileSink {}
|
|
|
|
unsafe impl Send for FileSink {}
|
|
|
|
|
2016-05-14 13:31:39 +00:00
|
|
|
impl FileSink {
|
|
|
|
fn new() -> FileSink {
|
2016-05-23 17:29:01 +00:00
|
|
|
FileSink { location: Mutex::new(None), file: None, position: 0 }
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_source() -> Box<Sink> {
|
|
|
|
Box::new(FileSink::new())
|
|
|
|
}
|
|
|
|
pub extern "C" fn new_ptr() -> *mut Box<Sink> {
|
|
|
|
let instance = Box::new(FileSink::new_source());
|
|
|
|
return Box::into_raw(instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for FileSink {
|
2016-05-24 20:24:05 +00:00
|
|
|
fn set_uri(&mut self, uri: Option<Url>) -> bool {
|
|
|
|
match uri {
|
2016-05-14 13:31:39 +00:00
|
|
|
None => {
|
2016-05-23 17:29:01 +00:00
|
|
|
let mut location = self.location.lock().unwrap();
|
|
|
|
*location = None;
|
2016-05-14 13:31:39 +00:00
|
|
|
return true;
|
|
|
|
},
|
2016-05-24 20:24:05 +00:00
|
|
|
Some(ref uri) => {
|
|
|
|
match uri.to_file_path().ok() {
|
|
|
|
Some(p) => {
|
|
|
|
let mut location = self.location.lock().unwrap();
|
|
|
|
*location = Some(p);
|
|
|
|
return true;
|
2016-05-14 13:31:39 +00:00
|
|
|
},
|
2016-05-24 20:24:05 +00:00
|
|
|
None => {
|
2016-05-23 17:29:01 +00:00
|
|
|
let mut location = self.location.lock().unwrap();
|
|
|
|
*location = None;
|
2016-05-24 20:24:05 +00:00
|
|
|
println_err!("Unsupported file URI '{}'", uri.as_str());
|
2016-05-14 13:31:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 20:24:05 +00:00
|
|
|
fn get_uri(&self) -> Option<Url> {
|
2016-05-23 17:29:01 +00:00
|
|
|
let location = self.location.lock().unwrap();
|
|
|
|
(*location).as_ref()
|
2016-05-15 14:43:13 +00:00
|
|
|
.map(|l| Url::from_file_path(l).ok())
|
|
|
|
.and_then(|i| i) // join()
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn start(&mut self) -> bool {
|
|
|
|
self.file = None;
|
|
|
|
self.position = 0;
|
|
|
|
|
2016-05-23 17:29:01 +00:00
|
|
|
let location = self.location.lock().unwrap();
|
|
|
|
match *location {
|
2016-05-14 13:31:39 +00:00
|
|
|
None => return false,
|
|
|
|
Some(ref location) => {
|
2016-05-15 13:44:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&mut self) -> bool {
|
|
|
|
self.file = None;
|
|
|
|
self.position = 0;
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2016-05-15 14:43:13 +00:00
|
|
|
fn render(&mut self, data: &[u8]) -> GstFlowReturn {
|
2016-05-15 13:44:32 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
2016-05-13 15:07:26 +00:00
|
|
|
}
|