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>
|
2018-12-02 13:19:37 +00:00
|
|
|
// 2018 François Laignel <fengalin@free.fr>
|
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.
|
2022-01-15 19:18:47 +00:00
|
|
|
//
|
2022-03-14 08:22:53 +00:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
2016-05-15 15:54:09 +00:00
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2018-12-02 13:19:37 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
use gst::subclass::prelude::*;
|
|
|
|
use gst_base::subclass::prelude::*;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2018-12-02 13:19:37 +00:00
|
|
|
use std::fs::File;
|
2016-05-14 13:31:39 +00:00
|
|
|
use std::io::Write;
|
2018-12-02 13:19:37 +00:00
|
|
|
use std::sync::Mutex;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2018-12-02 13:19:37 +00:00
|
|
|
use url::Url;
|
2016-12-25 11:16:12 +00:00
|
|
|
|
2020-11-14 17:09:42 +00:00
|
|
|
use crate::file_location::FileLocation;
|
2018-12-02 13:19:37 +00:00
|
|
|
|
|
|
|
const DEFAULT_LOCATION: Option<FileLocation> = None;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-08-22 19:25:58 +00:00
|
|
|
#[derive(Debug)]
|
2018-12-02 13:19:37 +00:00
|
|
|
struct Settings {
|
|
|
|
location: Option<FileLocation>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Settings {
|
|
|
|
location: DEFAULT_LOCATION,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum State {
|
2016-08-22 19:25:58 +00:00
|
|
|
Stopped,
|
|
|
|
Started { file: File, position: u64 },
|
|
|
|
}
|
|
|
|
|
2018-12-02 13:19:37 +00:00
|
|
|
impl Default for State {
|
|
|
|
fn default() -> State {
|
|
|
|
State::Stopped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[derive(Default)]
|
2016-05-14 13:31:39 +00:00
|
|
|
pub struct FileSink {
|
2018-12-02 13:19:37 +00:00
|
|
|
settings: Mutex<Settings>,
|
|
|
|
state: Mutex<State>,
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 14:24:55 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
2019-10-31 22:34:21 +00:00
|
|
|
"rsfilesink",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("File Sink"),
|
2020-11-22 14:24:55 +00:00
|
|
|
)
|
|
|
|
});
|
2019-10-31 22:34:21 +00:00
|
|
|
|
2016-05-14 13:31:39 +00:00
|
|
|
impl FileSink {
|
2022-10-09 13:06:59 +00:00
|
|
|
fn set_location(&self, location: Option<FileLocation>) -> Result<(), glib::Error> {
|
2018-12-02 13:19:37 +00:00
|
|
|
let state = self.state.lock().unwrap();
|
|
|
|
if let State::Started { .. } = *state {
|
2019-11-11 12:29:41 +00:00
|
|
|
return Err(glib::Error::new(
|
2018-12-30 10:02:28 +00:00
|
|
|
gst::URIError::BadState,
|
2018-12-18 09:23:45 +00:00
|
|
|
"Changing the `location` property on a started `filesink` is not supported",
|
2018-12-02 13:19:37 +00:00
|
|
|
));
|
2016-12-25 11:16:12 +00:00
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2018-12-02 13:19:37 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
settings.location = match location {
|
|
|
|
Some(location) => {
|
|
|
|
match settings.location {
|
|
|
|
Some(ref location_cur) => {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2019-10-31 22:34:21 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2018-12-18 09:23:45 +00:00
|
|
|
"Changing `location` from {:?} to {}",
|
2018-12-02 13:19:37 +00:00
|
|
|
location_cur,
|
|
|
|
location,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
None => {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::info!(CAT, imp: self, "Setting `location` to {}", location,);
|
2018-12-02 13:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(location)
|
|
|
|
}
|
|
|
|
None => {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::info!(CAT, imp: self, "Resetting `location` to None",);
|
2018-12-02 13:19:37 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[glib::object_subclass]
|
2018-12-02 13:19:37 +00:00
|
|
|
impl ObjectSubclass for FileSink {
|
2022-10-23 15:42:58 +00:00
|
|
|
const NAME: &'static str = "GstRsFileSink";
|
2020-11-14 17:09:42 +00:00
|
|
|
type Type = super::FileSink;
|
2018-12-02 13:19:37 +00:00
|
|
|
type ParentType = gst_base::BaseSink;
|
2021-01-21 18:21:29 +00:00
|
|
|
type Interfaces = (gst::URIHandler,);
|
2016-09-01 21:52:28 +00:00
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2018-12-02 13:19:37 +00:00
|
|
|
impl ObjectImpl for FileSink {
|
2021-01-21 18:21:29 +00:00
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
2022-08-18 12:04:15 +00:00
|
|
|
vec![glib::ParamSpecString::builder("location")
|
|
|
|
.nick("File Location")
|
|
|
|
.blurb("Location of the file to write")
|
|
|
|
.build()]
|
2021-01-21 18:21:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
PROPERTIES.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
2021-04-12 12:49:54 +00:00
|
|
|
match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"location" => {
|
2021-04-25 12:41:22 +00:00
|
|
|
let res = match value.get::<Option<String>>() {
|
2019-08-12 22:45:36 +00:00
|
|
|
Ok(Some(location)) => FileLocation::try_from_path_str(location)
|
2022-10-09 13:06:59 +00:00
|
|
|
.and_then(|file_location| self.set_location(Some(file_location))),
|
|
|
|
Ok(None) => self.set_location(None),
|
2019-08-12 22:45:36 +00:00
|
|
|
Err(_) => unreachable!("type checked upstream"),
|
2018-12-02 13:19:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = res {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "Failed to set property `location`: {}", err);
|
2018-12-02 13:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
2021-04-12 12:49:54 +00:00
|
|
|
match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"location" => {
|
2018-12-02 13:19:37 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
let location = settings
|
|
|
|
.location
|
|
|
|
.as_ref()
|
|
|
|
.map(|location| location.to_string());
|
|
|
|
|
2020-11-19 15:55:57 +00:00
|
|
|
location.to_value()
|
2018-12-02 13:19:37 +00:00
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
2016-08-22 19:25:58 +00:00
|
|
|
}
|
2018-12-02 13:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2021-10-23 08:57:31 +00:00
|
|
|
impl GstObjectImpl for FileSink {}
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
impl ElementImpl for FileSink {
|
|
|
|
fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
|
|
|
|
static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
|
|
|
|
gst::subclass::ElementMetadata::new(
|
|
|
|
"File Sink",
|
|
|
|
"Sink/File",
|
|
|
|
"Write stream to a file",
|
|
|
|
"François Laignel <fengalin@free.fr>, Luis de Bethencourt <luisbg@osg.samsung.com>",
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(&*ELEMENT_METADATA)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pad_templates() -> &'static [gst::PadTemplate] {
|
|
|
|
static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
|
|
|
|
let caps = gst::Caps::new_any();
|
|
|
|
let sink_pad_template = gst::PadTemplate::new(
|
|
|
|
"sink",
|
|
|
|
gst::PadDirection::Sink,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
vec![sink_pad_template]
|
|
|
|
});
|
|
|
|
|
|
|
|
PAD_TEMPLATES.as_ref()
|
|
|
|
}
|
|
|
|
}
|
2018-12-02 13:19:37 +00:00
|
|
|
|
|
|
|
impl BaseSinkImpl for FileSink {
|
2022-10-09 13:06:59 +00:00
|
|
|
fn start(&self) -> Result<(), gst::ErrorMessage> {
|
2018-12-02 13:19:37 +00:00
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
if let State::Started { .. } = *state {
|
2019-01-21 20:03:13 +00:00
|
|
|
unreachable!("FileSink already started");
|
2018-12-02 13:19:37 +00:00
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2018-12-02 13:19:37 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2019-01-26 15:07:51 +00:00
|
|
|
let location = settings.location.as_ref().ok_or_else(|| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::error_msg!(
|
2019-01-26 15:07:51 +00:00
|
|
|
gst::ResourceError::Settings,
|
|
|
|
["File location is not defined"]
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let file = File::create(location).map_err(|err| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::error_msg!(
|
2019-01-26 15:07:51 +00:00
|
|
|
gst::ResourceError::OpenWrite,
|
|
|
|
[
|
|
|
|
"Could not open file {} for writing: {}",
|
|
|
|
location,
|
|
|
|
err.to_string(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
})?;
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::debug!(CAT, imp: self, "Opened file {:?}", file);
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2018-12-02 13:19:37 +00:00
|
|
|
*state = State::Started { file, position: 0 };
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::info!(CAT, imp: self, "Started");
|
2018-12-02 13:19:37 +00:00
|
|
|
|
2019-01-26 15:07:51 +00:00
|
|
|
Ok(())
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn stop(&self) -> Result<(), gst::ErrorMessage> {
|
2018-12-02 13:19:37 +00:00
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
if let State::Stopped = *state {
|
2020-12-20 18:43:45 +00:00
|
|
|
return Err(gst::error_msg!(
|
2019-01-26 15:07:51 +00:00
|
|
|
gst::ResourceError::Settings,
|
2018-12-02 13:19:37 +00:00
|
|
|
["FileSink not started"]
|
2019-01-26 15:07:51 +00:00
|
|
|
));
|
2018-12-02 13:19:37 +00:00
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2018-12-02 13:19:37 +00:00
|
|
|
*state = State::Stopped;
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::info!(CAT, imp: self, "Stopped");
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2019-01-26 15:07:51 +00:00
|
|
|
Ok(())
|
2018-12-02 13:19:37 +00:00
|
|
|
}
|
2016-12-27 15:47:39 +00:00
|
|
|
|
2018-12-02 13:19:37 +00:00
|
|
|
// TODO: implement seek in BYTES format
|
2016-12-27 15:47:39 +00:00
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn render(&self, buffer: &gst::Buffer) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2018-12-02 13:19:37 +00:00
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
let (file, position) = match *state {
|
|
|
|
State::Started {
|
2017-04-12 13:46:11 +00:00
|
|
|
ref mut file,
|
|
|
|
ref mut position,
|
|
|
|
} => (file, position),
|
2018-12-02 13:19:37 +00:00
|
|
|
State::Stopped => {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::element_imp_error!(self, gst::CoreError::Failed, ["Not started yet"]);
|
2019-01-11 23:45:05 +00:00
|
|
|
return Err(gst::FlowError::Error);
|
2016-07-20 08:28:58 +00:00
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
};
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::trace!(CAT, imp: self, "Rendering {:?}", buffer);
|
2019-12-18 05:50:10 +00:00
|
|
|
let map = buffer.map_readable().map_err(|_| {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::element_imp_error!(self, gst::CoreError::Failed, ["Failed to map buffer"]);
|
2019-01-11 23:45:05 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
2016-09-12 13:00:28 +00:00
|
|
|
|
2019-01-11 23:45:05 +00:00
|
|
|
file.write_all(map.as_ref()).map_err(|err| {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::element_imp_error!(
|
|
|
|
self,
|
2019-01-11 23:45:05 +00:00
|
|
|
gst::ResourceError::Write,
|
|
|
|
["Failed to write buffer: {}", err]
|
|
|
|
);
|
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2019-01-11 23:45:05 +00:00
|
|
|
*position += map.len() as u64;
|
2016-09-01 21:52:28 +00:00
|
|
|
|
2019-01-11 23:45:05 +00:00
|
|
|
Ok(gst::FlowSuccess::Ok)
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
2016-05-13 15:07:26 +00:00
|
|
|
}
|
2018-12-02 13:19:37 +00:00
|
|
|
|
|
|
|
impl URIHandlerImpl for FileSink {
|
2021-01-21 18:21:29 +00:00
|
|
|
const URI_TYPE: gst::URIType = gst::URIType::Sink;
|
|
|
|
|
2021-04-20 12:57:40 +00:00
|
|
|
fn protocols() -> &'static [&'static str] {
|
2021-01-21 18:21:29 +00:00
|
|
|
&["file"]
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn uri(&self) -> Option<String> {
|
2018-12-02 13:19:37 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
|
|
|
|
// Conversion to Url already checked while building the `FileLocation`
|
|
|
|
settings.location.as_ref().map(|location| {
|
|
|
|
Url::from_file_path(location)
|
|
|
|
.expect("FileSink::get_uri couldn't build `Url` from `location`")
|
2021-05-08 06:41:14 +00:00
|
|
|
.into()
|
2018-12-02 13:19:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn set_uri(&self, uri: &str) -> Result<(), glib::Error> {
|
2018-12-02 13:19:37 +00:00
|
|
|
// Special case for "file://" as this is used by some applications to test
|
|
|
|
// with `gst_element_make_from_uri` if there's an element that supports the URI protocol
|
|
|
|
|
2019-05-25 17:23:55 +00:00
|
|
|
if uri != "file://" {
|
|
|
|
let file_location = FileLocation::try_from_uri_str(uri)?;
|
2022-10-09 13:06:59 +00:00
|
|
|
self.set_location(Some(file_location))
|
2019-05-25 17:23:55 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-02 13:19:37 +00:00
|
|
|
}
|
|
|
|
}
|