Commit d72f1f71 authored by Sebastian Dröge's avatar Sebastian Dröge 🍵
Browse files

Port C code for the source to pure Rust

parent 50829bde
......@@ -51,11 +51,10 @@ impl FileSink {
}
fn validate_uri(uri: &Url) -> Result<(), UriError> {
let _ = try!(uri.to_file_path()
.or_else(|_| {
Err(UriError::new(UriErrorKind::UnsupportedProtocol,
let _ = try!(uri.to_file_path().or_else(|_| {
Err(UriError::new(UriErrorKind::UnsupportedProtocol,
Some(format!("Unsupported file URI '{}'", uri.as_str()))))
}));
}));
Ok(())
}
......@@ -69,12 +68,11 @@ impl Sink for FileSink {
return Err(error_msg!(SinkError::Failure, ["Sink already started"]));
}
let location = try!(uri.to_file_path()
.or_else(|_| {
error!(self.logger, "Unsupported file URI '{}'", uri.as_str());
Err(error_msg!(SinkError::Failure,
["Unsupported file URI '{}'", uri.as_str()]))
}));
let location = try!(uri.to_file_path().or_else(|_| {
error!(self.logger, "Unsupported file URI '{}'", uri.as_str());
Err(error_msg!(SinkError::Failure,
["Unsupported file URI '{}'", uri.as_str()]))
}));
let file = try!(File::create(location.as_path()).or_else(|err| {
......
......@@ -49,11 +49,10 @@ impl FileSrc {
}
fn validate_uri(uri: &Url) -> Result<(), UriError> {
let _ = try!(uri.to_file_path()
.or_else(|_| {
Err(UriError::new(UriErrorKind::UnsupportedProtocol,
let _ = try!(uri.to_file_path().or_else(|_| {
Err(UriError::new(UriErrorKind::UnsupportedProtocol,
Some(format!("Unsupported file URI '{}'", uri.as_str()))))
}));
}));
Ok(())
}
......@@ -68,9 +67,7 @@ impl Source for FileSrc {
fn get_size(&self) -> Option<u64> {
if let StreamingState::Started { ref file, .. } = self.streaming_state {
file.metadata()
.ok()
.map(|m| m.len())
file.metadata().ok().map(|m| m.len())
} else {
None
}
......@@ -81,12 +78,11 @@ impl Source for FileSrc {
return Err(error_msg!(SourceError::Failure, ["Source already started"]));
}
let location = try!(uri.to_file_path()
.or_else(|_| {
error!(self.logger, "Unsupported file URI '{}'", uri.as_str());
Err(error_msg!(SourceError::Failure,
["Unsupported file URI '{}'", uri.as_str()]))
}));
let location = try!(uri.to_file_path().or_else(|_| {
error!(self.logger, "Unsupported file URI '{}'", uri.as_str());
Err(error_msg!(SourceError::Failure,
["Unsupported file URI '{}'", uri.as_str()]))
}));
let file = try!(File::open(location.as_path()).or_else(|err| {
error!(self.logger,
......
......@@ -26,29 +26,29 @@ use filesink::FileSink;
fn plugin_init(plugin: &Plugin) -> bool {
source_register(plugin,
&SourceInfo {
name: "rsfilesrc",
long_name: "File Source",
description: "Reads local files",
classification: "Source/File",
author: "Sebastian Dröge <sebastian@centricular.com>",
SourceInfo {
name: "rsfilesrc".into(),
long_name: "File Source".into(),
description: "Reads local files".into(),
classification: "Source/File".into(),
author: "Sebastian Dröge <sebastian@centricular.com>".into(),
rank: 256 + 100,
create_instance: FileSrc::new_boxed,
protocols: "file",
protocols: vec!["file".into()],
push_only: false,
});
sink_register(plugin,
&SinkInfo {
name: "rsfilesink",
long_name: "File Sink",
description: "Writes to local files",
classification: "Sink/File",
author: "Luis de Bethencourt <luisbg@osg.samsung.com>",
rank: 256 + 100,
create_instance: FileSink::new_boxed,
protocols: "file",
});
name: "rsfilesink",
long_name: "File Sink",
description: "Writes to local files",
classification: "Sink/File",
author: "Luis de Bethencourt <luisbg@osg.samsung.com>",
rank: 256 + 100,
create_instance: FileSink::new_boxed,
protocols: "file",
});
true
}
......
......@@ -160,10 +160,10 @@ impl AudioFormat {
&[("layout", &"interleaved".into()),
("format",
&if self.width == 8 {
"U8".into()
} else {
"S16LE".into()
})]))
"U8".into()
} else {
"S16LE".into()
})]))
} else {
None
}
......@@ -236,14 +236,17 @@ impl AudioFormat {
};
if self.rate != 0 {
caps.as_mut()
.map(|c| c.get_mut().unwrap().set_simple(&[("rate", &(self.rate as i32).into())]));
caps.as_mut().map(|c| {
c.get_mut().unwrap().set_simple(&[("rate",
&(self.rate as i32).into())])
});
}
if self.channels != 0 {
caps.as_mut()
.map(|c| {
c.get_mut().unwrap().set_simple(&[("channels", &(self.channels as i32).into())])
});
caps.as_mut().map(|c| {
c.get_mut().unwrap().set_simple(&[("channels",
&(self.channels as i32)
.into())])
});
}
caps
......@@ -342,23 +345,27 @@ impl VideoFormat {
if let (Some(width), Some(height)) = (self.width, self.height) {
caps.as_mut().map(|c| {
c.get_mut().unwrap().set_simple(&[("width", &(width as i32).into()),
("height", &(height as i32).into())])
});
c.get_mut().unwrap().set_simple(&[("width",
&(width as i32).into()),
("height",
&(height as i32).into())])
});
}
if let Some(par) = self.pixel_aspect_ratio {
if *par.numer() != 0 && par.numer() != par.denom() {
caps.as_mut().map(|c| {
c.get_mut().unwrap().set_simple(&[("pixel-aspect-ratio", &par.into())])
});
c.get_mut().unwrap().set_simple(&[("pixel-aspect-ratio",
&par.into())])
});
}
}
if let Some(fps) = self.framerate {
if *fps.numer() != 0 {
caps.as_mut()
.map(|c| c.get_mut().unwrap().set_simple(&[("framerate", &fps.into())]));
caps.as_mut().map(|c| {
c.get_mut().unwrap().set_simple(&[("framerate", &fps.into())])
});
}
}
......@@ -538,16 +545,14 @@ impl FlvDemux {
let mut streams = Vec::new();
if audio_changed {
if let Some(caps) = streaming_state.audio
.as_ref()
.and_then(|a| a.to_caps()) {
if let Some(caps) =
streaming_state.audio.as_ref().and_then(|a| a.to_caps()) {
streams.push(Stream::new(AUDIO_STREAM_ID, caps, String::from("audio")));
}
}
if video_changed {
if let Some(caps) = streaming_state.video
.as_ref()
.and_then(|v| v.to_caps()) {
if let Some(caps) =
streaming_state.video.as_ref().and_then(|v| v.to_caps()) {
streams.push(Stream::new(VIDEO_STREAM_ID, caps, String::from("video")));
}
}
......@@ -692,9 +697,8 @@ impl FlvDemux {
self.adapter.flush(offset as usize).unwrap();
}
let mut buffer = self.adapter
.get_buffer((tag_header.data_size - 1 - offset) as usize)
.unwrap();
let mut buffer =
self.adapter.get_buffer((tag_header.data_size - 1 - offset) as usize).unwrap();
{
let buffer = buffer.get_mut().unwrap();
......@@ -847,9 +851,8 @@ impl FlvDemux {
self.adapter.flush(offset as usize).unwrap();
}
let mut buffer = self.adapter
.get_buffer((tag_header.data_size - 1 - offset) as usize)
.unwrap();
let mut buffer =
self.adapter.get_buffer((tag_header.data_size - 1 - offset) as usize).unwrap();
{
let buffer = buffer.get_mut().unwrap();
......
......@@ -28,16 +28,16 @@ use flvdemux::FlvDemux;
fn plugin_init(plugin: &Plugin) -> bool {
demuxer_register(plugin,
&DemuxerInfo {
name: "rsflvdemux",
long_name: "FLV Demuxer",
description: "Demuxes FLV Streams",
classification: "Codec/Demuxer",
author: "Sebastian Dröge <sebastian@centricular.com>",
rank: 256 + 100,
create_instance: FlvDemux::new_boxed,
input_caps: &Caps::new_simple("video/x-flv", &[]),
output_caps: &Caps::new_any(),
});
name: "rsflvdemux",
long_name: "FLV Demuxer",
description: "Demuxes FLV Streams",
classification: "Codec/Demuxer",
author: "Sebastian Dröge <sebastian@centricular.com>",
rank: 256 + 100,
create_instance: FlvDemux::new_boxed,
input_caps: &Caps::new_simple("video/x-flv", &[]),
output_caps: &Caps::new_any(),
});
true
}
......
......@@ -91,8 +91,7 @@ impl HttpSrc {
let size = response.headers().get().map(|&ContentLength(cl)| cl + start);
let accept_byte_ranges = if let Some(&AcceptRanges(ref ranges)) =
response.headers()
.get() {
response.headers().get() {
ranges.iter().any(|u| *u == RangeUnit::Bytes)
} else {
false
......@@ -117,14 +116,14 @@ impl HttpSrc {
debug!(self.logger, "Request successful: {:?}", response);
Ok(StreamingState::Started {
uri: uri,
response: response,
seekable: seekable,
position: 0,
size: size,
start: start,
stop: stop,
})
uri: uri,
response: response,
seekable: seekable,
position: 0,
size: size,
start: start,
stop: stop,
})
}
}
......
......@@ -24,15 +24,15 @@ use httpsrc::HttpSrc;
fn plugin_init(plugin: &Plugin) -> bool {
source_register(plugin,
&SourceInfo {
name: "rshttpsrc",
long_name: "HTTP/HTTPS Source",
description: "Reads HTTP/HTTPS streams",
classification: "Source/File",
author: "Sebastian Dröge <sebastian@centricular.com>",
SourceInfo {
name: "rshttpsrc".into(),
long_name: "HTTP/HTTPS Source".into(),
description: "Reads HTTP/HTTPS streams".into(),
classification: "Source/File".into(),
author: "Sebastian Dröge <sebastian@centricular.com>".into(),
rank: 256 + 100,
create_instance: HttpSrc::new_boxed,
protocols: "http:https",
protocols: vec!["http".into(), "https".into()],
push_only: true,
});
......
......@@ -17,6 +17,7 @@ num-rational = { version = "0.1", default-features = false, features = [] }
glib-sys = { git = "https://github.com/gtk-rs/sys.git" }
gobject-sys = { git = "https://github.com/gtk-rs/sys.git" }
gstreamer-sys = { git = "https://github.com/sdroege/gstreamer-sys.git", features = ["v1_10"] }
gstreamer-base-sys = { git = "https://github.com/sdroege/gstreamer-sys.git", features = ["v1_10"] }
derivative = "1.0"
[build-dependencies]
......
......@@ -14,7 +14,7 @@ fn main() {
let gstbase = pkg_config::probe_library("gstreamer-base-1.0").unwrap();
let includes = [gstreamer.include_paths, gstbase.include_paths];
let files = ["src/source.c", "src/sink.c", "src/demuxer.c"];
let files = ["src/sink.c", "src/demuxer.c"];
let mut config = gcc::Config::new();
config.include("src");
......
......@@ -247,7 +247,7 @@ impl DemuxerWrapper {
}
}
fn handle_buffer(&self, buffer: GstRc<Buffer>) -> GstFlowReturn {
fn handle_buffer(&self, buffer: GstRc<Buffer>) -> gst::GstFlowReturn {
extern "C" {
fn gst_rs_demuxer_stream_eos(raw: *mut gst::GstElement, index: u32);
fn gst_rs_demuxer_add_stream(raw: *mut gst::GstElement,
......@@ -262,7 +262,7 @@ impl DemuxerWrapper {
fn gst_rs_demuxer_stream_push_buffer(raw: *mut gst::GstElement,
index: u32,
buffer: *mut gst::GstBuffer)
-> GstFlowReturn;
-> gst::GstFlowReturn;
};
let mut res = {
......@@ -290,7 +290,7 @@ impl DemuxerWrapper {
match res {
HandleBufferResult::NeedMoreData => {
return GstFlowReturn::Ok;
return gst::GST_FLOW_OK;
}
HandleBufferResult::StreamAdded(stream) => {
let stream_id_cstr = CString::new(stream.stream_id.as_bytes()).unwrap();
......@@ -323,7 +323,7 @@ impl DemuxerWrapper {
let flow_ret = unsafe {
gst_rs_demuxer_stream_push_buffer(self.raw, index, buffer.into_ptr())
};
if flow_ret != GstFlowReturn::Ok {
if flow_ret != gst::GST_FLOW_OK {
return flow_ret;
}
}
......@@ -334,7 +334,7 @@ impl DemuxerWrapper {
gst_rs_demuxer_stream_eos(self.raw, index);
}
return GstFlowReturn::Eos;
return gst::GST_FLOW_EOS;
}
HandleBufferResult::Again => {
// nothing, just call again
......@@ -483,10 +483,10 @@ pub unsafe extern "C" fn demuxer_seek(ptr: *mut DemuxerWrapper,
#[no_mangle]
pub unsafe extern "C" fn demuxer_handle_buffer(ptr: *mut DemuxerWrapper,
buffer: *mut gst::GstBuffer)
-> GstFlowReturn {
-> gst::GstFlowReturn {
let wrap: &mut DemuxerWrapper = &mut *ptr;
panic_to_error!(wrap, GstFlowReturn::Error, {
panic_to_error!(wrap, gst::GST_FLOW_ERROR, {
let buffer = GstRc::new_from_owned_ptr(buffer);
wrap.handle_buffer(buffer)
})
......
......@@ -15,8 +15,6 @@ use std::borrow::Cow;
use url::Url;
use utils::*;
use glib;
use gst;
......@@ -145,12 +143,12 @@ pub enum FlowError {
}
impl FlowError {
pub fn to_native(&self) -> GstFlowReturn {
pub fn to_native(&self) -> gst::GstFlowReturn {
match *self {
FlowError::Flushing => GstFlowReturn::Flushing,
FlowError::Eos => GstFlowReturn::Eos,
FlowError::NotNegotiated(..) => GstFlowReturn::NotNegotiated,
FlowError::Error(..) => GstFlowReturn::Error,
FlowError::Flushing => gst::GST_FLOW_FLUSHING,
FlowError::Eos => gst::GST_FLOW_EOS,
FlowError::NotNegotiated(..) => gst::GST_FLOW_NOT_NEGOTIATED,
FlowError::Error(..) => gst::GST_FLOW_ERROR,
}
}
}
......
......@@ -22,6 +22,7 @@ extern crate derivative;
pub extern crate gobject_sys as gobject;
pub extern crate glib_sys as glib;
pub extern crate gstreamer_sys as gst;
pub extern crate gstreamer_base_sys as gst_base;
#[macro_use]
pub mod utils;
......
......@@ -153,7 +153,7 @@ impl<'a, T: MiniObject> From<&'a mut T> for GstRc<T> {
}
#[repr(C)]
pub struct GstRefPtr<T: MiniObject>(*mut T::PtrType);
pub struct GstRefPtr<T: MiniObject>(pub *mut T::PtrType);
#[derive(Hash, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct GstRef<'a, T: 'a + MiniObject> {
......
......@@ -177,13 +177,13 @@ impl SinkWrapper {
}
}
fn render(&self, buffer: &Buffer) -> GstFlowReturn {
fn render(&self, buffer: &Buffer) -> gst::GstFlowReturn {
let sink = &mut self.sink.lock().unwrap();
trace!(self.logger, "Rendering buffer {:?}", buffer);
match sink.render(buffer) {
Ok(..) => GstFlowReturn::Ok,
Ok(..) => gst::GST_FLOW_OK,
Err(flow_error) => {
error!(self.logger, "Failed to render: {:?}", flow_error);
match flow_error {
......@@ -281,9 +281,9 @@ pub unsafe extern "C" fn sink_stop(ptr: *const SinkWrapper) -> glib::gboolean {
#[no_mangle]
pub unsafe extern "C" fn sink_render(ptr: *const SinkWrapper,
buffer: GstRefPtr<Buffer>)
-> GstFlowReturn {
-> gst::GstFlowReturn {
let wrap: &SinkWrapper = &*ptr;
panic_to_error!(wrap, GstFlowReturn::Error, {
panic_to_error!(wrap, gst::GST_FLOW_ERROR, {
let buffer: GstRef<Buffer> = GstRef::new(&buffer);
wrap.render(buffer.as_ref())
})
......
/* Copyright (C) 2016-2017 Sebastian Dröge <sebastian@centricular.com>
*
* 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.
*/
#include "source.h"
#include <string.h>
#include <stdint.h>
typedef struct
{
gchar *long_name;
gchar *description;
gchar *classification;
gchar *author;
void *create_instance;
gchar **protocols;
} ElementData;
static GHashTable *sources;
/* Declarations for Rust code */
extern gboolean sources_register (void *plugin);
extern void *source_new (GstRsSrc * source, void *create_instance);
extern void source_drop (void *rssource);
extern GstFlowReturn source_fill (void *rssource, guint64 offset, guint size,
GstBuffer * buffer);
extern gboolean source_seek (void *rssource, uint64_t start, uint64_t stop);
extern gboolean source_set_uri (void *rssource, const char *uri, GError ** err);
extern char *source_get_uri (void *rssource);
extern uint64_t source_get_size (void *rssource);
extern gboolean source_is_seekable (void *rssource);
extern gboolean source_start (void *rssource);
extern gboolean source_stop (void *rssource);
extern void cstring_drop (void *str);
GST_DEBUG_CATEGORY_STATIC (gst_rs_src_debug);
#define GST_CAT_DEFAULT gst_rs_src_debug
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
enum
{
PROP_0,
PROP_URI
};
static void gst_rs_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
static void gst_rs_src_finalize (GObject * object);
static void gst_rs_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_rs_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static gboolean gst_rs_src_start (GstBaseSrc * basesrc);
static gboolean gst_rs_src_stop (GstBaseSrc * basesrc);
static gboolean gst_rs_src_is_seekable (GstBaseSrc * src);
static gboolean gst_rs_src_get_size (GstBaseSrc * src, guint64 * size);
static GstFlowReturn gst_rs_src_fill (GstBaseSrc * src, guint64 offset,
guint length, GstBuffer * buf);
static gboolean gst_rs_src_do_seek (GstBaseSrc * src, GstSegment * segment);
static GObjectClass *parent_class;
static void
gst_rs_src_class_init (GstRsSrcClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSrcClass *gstbasesrc_class;
ElementData *data = g_hash_table_lookup (sources,
GSIZE_TO_POINTER (G_TYPE_FROM_CLASS (klass)));
g_assert (data != NULL);
gobject_class = G_OBJECT_CLASS (klass);
gstelement_class = GST_ELEMENT_CLASS (klass);
gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
gobject_class->set_property = gst_rs_src_set_property;
gobject_class->get_property = gst_rs_src_get_property;
g_object_class_install_property (gobject_class, PROP_URI,
g_param_spec_string ("uri", "URI",
"URI to read from", NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
GST_PARAM_MUTABLE_READY));
gobject_class->finalize = gst_rs_src_finalize;
gst_element_class_set_static_metadata (gstelement_class,
data->long_name, data->classification, data->description, data->author);
gst_element_class_add_static_pad_template (gstelement_class, &src_template);
gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_rs_src_start);
gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_rs_src_stop);
gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_rs_src_is_seekable);
gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_rs_src_get_size);
gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_rs_src_fill);
gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_rs_src_do_seek);
}
static void
gst_rs_src_init (GstRsSrc * src, GstRsSrcClass * klass)
{
ElementData *data = g_hash_table_lookup (sources,
GSIZE_TO_POINTER (G_TYPE_FROM_CLASS (klass)));
g_assert (data != NULL);
gst_base_src_set_blocksize (GST_BASE_SRC (src), 4096);
GST_DEBUG_OBJECT (src, "Instantiating");
src->instance = source_new (src, data->create_instance);
}