gst-plugins-rs/gst-plugin-file/src/filesink.rs

158 lines
4.5 KiB
Rust
Raw Normal View History

// 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
//
// 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
use std::fs::File;
use url::Url;
use std::io::Write;
use gst_plugin::error::*;
use gst_plugin_simple::error::*;
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
use gst;
#[derive(Debug)]
enum StreamingState {
Stopped,
Started { file: File, position: u64 },
}
#[derive(Debug)]
pub struct FileSink {
streaming_state: StreamingState,
cat: gst::DebugCategory,
}
impl FileSink {
pub fn new(_sink: &BaseSink) -> FileSink {
2016-12-25 11:16:12 +00:00
FileSink {
streaming_state: StreamingState::Stopped,
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
}
}
pub fn new_boxed(sink: &BaseSink) -> Box<SinkImpl> {
Box::new(FileSink::new(sink))
}
}
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()),
))));
Ok(())
}
2017-09-24 19:28:28 +00:00
impl SinkImpl for FileSink {
fn uri_validator(&self) -> Box<UriValidator> {
Box::new(validate_uri)
}
fn start(&mut self, sink: &BaseSink, uri: Url) -> Result<(), gst::ErrorMessage> {
if let StreamingState::Started { .. } = self.streaming_state {
return Err(gst_error_msg!(
gst::LibraryError::Failed,
["Sink already started"]
));
}
2017-07-31 13:36:35 +00:00
let location = try!(uri.to_file_path().or_else(|_| {
gst_error!(
self.cat,
obj: sink,
"Unsupported file URI '{}'",
uri.as_str()
);
Err(gst_error_msg!(
gst::LibraryError::Failed,
2017-07-31 13:36:35 +00:00
["Unsupported file URI '{}'", uri.as_str()]
))
}));
let file = try!(File::create(location.as_path()).or_else(|err| {
gst_error!(
self.cat,
obj: sink,
2017-07-31 13:36:35 +00:00
"Could not open file for writing: {}",
err.to_string()
);
Err(gst_error_msg!(
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
]
))
}));
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 };
Ok(())
}
fn stop(&mut self, _sink: &BaseSink) -> Result<(), gst::ErrorMessage> {
self.streaming_state = StreamingState::Stopped;
Ok(())
}
fn render(&mut self, sink: &BaseSink, buffer: &gst::BufferRef) -> Result<(), FlowError> {
let cat = self.cat;
let streaming_state = &mut self.streaming_state;
2016-12-27 15:47:39 +00:00
gst_trace!(cat, obj: sink, "Rendering {:?}", buffer);
2016-12-27 15:47:39 +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),
StreamingState::Stopped => {
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
}
};
let map = match buffer.map_readable() {
None => {
return Err(FlowError::Error(gst_error_msg!(
gst::LibraryError::Failed,
["Failed to map buffer"]
)));
}
Some(map) => map,
};
let data = map.as_slice();
2017-07-31 13:36:35 +00:00
try!(file.write_all(data).or_else(|err| {
gst_error!(cat, obj: sink, "Failed to write: {}", err);
Err(FlowError::Error(gst_error_msg!(
gst::ResourceError::Write,
2017-07-31 13:36:35 +00:00
["Failed to write: {}", err]
)))
}));
*position += data.len() as u64;
Ok(())
}
2016-05-13 15:07:26 +00:00
}