2017-02-16 15:52:27 +00:00
|
|
|
// Copyright (C) 2016-2017 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
// 2016 Luis de Bethencourt <luisbg@osg.samsung.com>
|
2016-05-15 15:54:09 +00:00
|
|
|
//
|
2017-02-16 15:52:27 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2016-05-15 15:54:09 +00:00
|
|
|
|
2016-05-14 13:31:39 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
2018-05-04 12:16:59 +00:00
|
|
|
use gst_plugin::error::*;
|
2017-12-20 19:53:43 +00:00
|
|
|
use gst_plugin_simple::error::*;
|
2017-10-11 10:32:44 +00:00
|
|
|
use gst_plugin_simple::sink::*;
|
2018-05-01 14:16:12 +00:00
|
|
|
use gst_plugin_simple::UriValidator;
|
2016-12-25 11:16:12 +00:00
|
|
|
|
2017-09-16 17:35:01 +00:00
|
|
|
use gst;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-08-22 19:25:58 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum StreamingState {
|
|
|
|
Stopped,
|
|
|
|
Started { file: File, position: u64 },
|
|
|
|
}
|
|
|
|
|
2016-05-14 13:31:39 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FileSink {
|
2016-09-01 21:52:28 +00:00
|
|
|
streaming_state: StreamingState,
|
2017-09-16 17:35:01 +00:00
|
|
|
cat: gst::DebugCategory,
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FileSink {
|
2017-11-20 11:22:26 +00:00
|
|
|
pub fn new(_sink: &BaseSink) -> FileSink {
|
2016-12-25 11:16:12 +00:00
|
|
|
FileSink {
|
|
|
|
streaming_state: StreamingState::Stopped,
|
2017-09-16 17:35:01 +00:00
|
|
|
cat: gst::DebugCategory::new(
|
|
|
|
"rsfilesink",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
"Rust file source",
|
2017-07-31 13:36:35 +00:00
|
|
|
),
|
2016-12-25 11:16:12 +00:00
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 11:22:26 +00:00
|
|
|
pub fn new_boxed(sink: &BaseSink) -> Box<SinkImpl> {
|
2017-09-16 17:35:01 +00:00
|
|
|
Box::new(FileSink::new(sink))
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 21:52:28 +00:00
|
|
|
fn validate_uri(uri: &Url) -> Result<(), UriError> {
|
2017-12-20 17:30:32 +00:00
|
|
|
let _ = try!(uri.to_file_path().or_else(|_| Err(UriError::new(
|
|
|
|
gst::URIError::UnsupportedProtocol,
|
|
|
|
format!("Unsupported file URI '{}'", uri.as_str()),
|
|
|
|
))));
|
2016-09-01 21:52:28 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2017-09-24 19:28:28 +00:00
|
|
|
impl SinkImpl for FileSink {
|
2016-09-01 21:52:28 +00:00
|
|
|
fn uri_validator(&self) -> Box<UriValidator> {
|
|
|
|
Box::new(validate_uri)
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 17:53:59 +00:00
|
|
|
fn start(&mut self, sink: &BaseSink, uri: Url) -> Result<(), gst::ErrorMessage> {
|
2016-09-01 21:52:28 +00:00
|
|
|
if let StreamingState::Started { .. } = self.streaming_state {
|
2017-12-20 17:53:59 +00:00
|
|
|
return Err(gst_error_msg!(
|
2017-09-16 17:35:01 +00:00
|
|
|
gst::LibraryError::Failed,
|
|
|
|
["Sink already started"]
|
|
|
|
));
|
2016-08-22 19:25:58 +00:00
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2017-07-31 13:36:35 +00:00
|
|
|
let location = try!(uri.to_file_path().or_else(|_| {
|
2017-09-16 17:35:01 +00:00
|
|
|
gst_error!(
|
|
|
|
self.cat,
|
|
|
|
obj: sink,
|
|
|
|
"Unsupported file URI '{}'",
|
|
|
|
uri.as_str()
|
|
|
|
);
|
2017-12-20 17:53:59 +00:00
|
|
|
Err(gst_error_msg!(
|
2017-09-16 17:35:01 +00:00
|
|
|
gst::LibraryError::Failed,
|
2017-07-31 13:36:35 +00:00
|
|
|
["Unsupported file URI '{}'", uri.as_str()]
|
|
|
|
))
|
|
|
|
}));
|
2016-09-01 21:52:28 +00:00
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
let file = try!(File::create(location.as_path()).or_else(|err| {
|
2017-09-16 17:35:01 +00:00
|
|
|
gst_error!(
|
|
|
|
self.cat,
|
|
|
|
obj: sink,
|
2017-07-31 13:36:35 +00:00
|
|
|
"Could not open file for writing: {}",
|
|
|
|
err.to_string()
|
|
|
|
);
|
2017-12-20 17:53:59 +00:00
|
|
|
Err(gst_error_msg!(
|
2017-09-16 17:35:01 +00:00
|
|
|
gst::ResourceError::OpenWrite,
|
2017-07-31 13:36:35 +00:00
|
|
|
[
|
|
|
|
"Could not open file for writing '{}': {}",
|
|
|
|
location.to_str().unwrap_or("Non-UTF8 path"),
|
2018-05-01 14:16:12 +00:00
|
|
|
err.to_string(),
|
2017-07-31 13:36:35 +00:00
|
|
|
]
|
|
|
|
))
|
2016-08-27 08:16:17 +00:00
|
|
|
}));
|
|
|
|
|
2017-09-16 17:35:01 +00:00
|
|
|
gst_debug!(self.cat, obj: sink, "Opened file {:?}", file);
|
2016-12-27 15:47:39 +00:00
|
|
|
|
2018-10-11 10:49:48 +00:00
|
|
|
self.streaming_state = StreamingState::Started { file, position: 0 };
|
2016-08-27 08:16:17 +00:00
|
|
|
|
|
|
|
Ok(())
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 17:53:59 +00:00
|
|
|
fn stop(&mut self, _sink: &BaseSink) -> Result<(), gst::ErrorMessage> {
|
2016-09-01 21:52:28 +00:00
|
|
|
self.streaming_state = StreamingState::Stopped;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
Ok(())
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 11:22:26 +00:00
|
|
|
fn render(&mut self, sink: &BaseSink, buffer: &gst::BufferRef) -> Result<(), FlowError> {
|
2017-09-16 17:35:01 +00:00
|
|
|
let cat = self.cat;
|
2017-08-01 09:49:23 +00:00
|
|
|
let streaming_state = &mut self.streaming_state;
|
2016-12-27 15:47:39 +00:00
|
|
|
|
2017-09-16 17:35:01 +00:00
|
|
|
gst_trace!(cat, obj: sink, "Rendering {:?}", buffer);
|
2016-12-27 15:47:39 +00:00
|
|
|
|
2017-08-01 09:49:23 +00:00
|
|
|
let (file, position) = match *streaming_state {
|
2017-04-12 13:46:11 +00:00
|
|
|
StreamingState::Started {
|
|
|
|
ref mut file,
|
|
|
|
ref mut position,
|
|
|
|
} => (file, position),
|
2016-08-27 08:16:17 +00:00
|
|
|
StreamingState::Stopped => {
|
2017-12-20 17:53:59 +00:00
|
|
|
return Err(FlowError::Error(gst_error_msg!(
|
2017-12-20 17:30:32 +00:00
|
|
|
gst::LibraryError::Failed,
|
|
|
|
["Not started yet"]
|
|
|
|
)));
|
2016-07-20 08:28:58 +00:00
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
};
|
|
|
|
|
2017-09-16 17:35:01 +00:00
|
|
|
let map = match buffer.map_readable() {
|
2016-09-12 13:00:28 +00:00
|
|
|
None => {
|
2017-12-20 17:53:59 +00:00
|
|
|
return Err(FlowError::Error(gst_error_msg!(
|
2017-09-16 17:35:01 +00:00
|
|
|
gst::LibraryError::Failed,
|
|
|
|
["Failed to map buffer"]
|
|
|
|
)));
|
2016-09-12 13:00:28 +00:00
|
|
|
}
|
|
|
|
Some(map) => map,
|
|
|
|
};
|
|
|
|
let data = map.as_slice();
|
|
|
|
|
2017-07-31 13:36:35 +00:00
|
|
|
try!(file.write_all(data).or_else(|err| {
|
2017-09-16 17:35:01 +00:00
|
|
|
gst_error!(cat, obj: sink, "Failed to write: {}", err);
|
2017-12-20 17:53:59 +00:00
|
|
|
Err(FlowError::Error(gst_error_msg!(
|
2017-09-16 17:35:01 +00:00
|
|
|
gst::ResourceError::Write,
|
2017-07-31 13:36:35 +00:00
|
|
|
["Failed to write: {}", err]
|
|
|
|
)))
|
|
|
|
}));
|
2016-08-27 08:16:17 +00:00
|
|
|
|
|
|
|
*position += data.len() as u64;
|
2016-09-01 21:52:28 +00:00
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
Ok(())
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
2016-05-13 15:07:26 +00:00
|
|
|
}
|