Register types from Rust and abstract the source to be able to handle other implementations too

This commit is contained in:
Sebastian Dröge 2016-05-14 15:44:40 +03:00
parent 4d989404b0
commit ec01d11393
8 changed files with 386 additions and 291 deletions

View file

@ -6,7 +6,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/plugin.c", "src/rsfilesrc.c", "src/rsfilesink.c"];
let files = ["src/plugin.c", "src/rssource.c", "src/rsfilesink.c"];
let mut config = gcc::Config::new();
config.include("src");

View file

@ -8,3 +8,40 @@ pub mod utils;
pub mod rssource;
pub mod rsfilesrc;
pub mod rsfilesink;
use utils::*;
use rssource::Source;
use rsfilesrc::FileSrc;
use std::os::raw::c_void;
use libc::{c_char};
use std::ffi::CString;
extern "C" {
fn gst_rs_source_register(plugin: *const c_void,
name: *const c_char,
long_name: *const c_char,
description: *const c_char,
classification: *const c_char,
author: *const c_char,
rank: i32,
create_instance: extern fn() -> *mut Box<Source>,
protocols: *const c_char) -> GBoolean;
}
#[no_mangle]
pub extern "C" fn sources_register(plugin: *const c_void) -> GBoolean {
unsafe {
gst_rs_source_register(plugin,
CString::new("rsfilesrc").unwrap().as_ptr(),
CString::new("File Source").unwrap().as_ptr(),
CString::new("Reads local files").unwrap().as_ptr(),
CString::new("Source/File").unwrap().as_ptr(),
CString::new("Sebastian Dröge <sebastian@centricular.com>").unwrap().as_ptr(),
256 + 100,
FileSrc::new_ptr,
CString::new("file").unwrap().as_ptr());
}
return GBoolean::True;
}

View file

@ -1,15 +1,13 @@
#include <gst/gst.h>
#include "rsfilesrc.h"
#include "rssource.h"
#include "rsfilesink.h"
static gboolean
plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "rsfilesrc", GST_RANK_PRIMARY+100,
GST_TYPE_RSFILE_SRC)) {
if (!gst_rs_source_plugin_init (plugin))
return FALSE;
}
if (!gst_element_register (plugin, "rsfilesink", GST_RANK_NONE,
GST_TYPE_RSFILE_SINK)) {

View file

@ -1,242 +0,0 @@
#include "rsfilesrc.h"
#include <string.h>
#include <stdint.h>
/* Declarations for Rust code */
extern void * filesrc_new (void);
extern void source_drop (void * filesrc);
extern GstFlowReturn source_fill (void * filesrc, uint64_t offset, void * data, size_t * data_len);
extern gboolean source_set_uri (void * filesrc, const char *uri);
extern char * source_get_uri (void * filesrc);
extern uint64_t source_get_size (void * filesrc);
extern gboolean source_is_seekable (void * filesrc);
extern gboolean source_start (void * filesrc);
extern gboolean source_stop (void * filesrc);
GST_DEBUG_CATEGORY_STATIC (gst_rsfile_src_debug);
#define GST_CAT_DEFAULT gst_rsfile_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_rsfile_src_uri_handler_init (gpointer g_iface,
gpointer iface_data);
static void gst_rsfile_src_finalize (GObject * object);
static void gst_rsfile_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_rsfile_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static gboolean gst_rsfile_src_start (GstBaseSrc * basesrc);
static gboolean gst_rsfile_src_stop (GstBaseSrc * basesrc);
static gboolean gst_rsfile_src_is_seekable (GstBaseSrc * src);
static gboolean gst_rsfile_src_get_size (GstBaseSrc * src, guint64 * size);
static GstFlowReturn gst_rsfile_src_fill (GstBaseSrc * src, guint64 offset,
guint length, GstBuffer * buf);
#define _do_init \
G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rsfile_src_uri_handler_init); \
GST_DEBUG_CATEGORY_INIT (gst_rsfile_src_debug, "rsfilesrc", 0, "rsfilesrc element");
#define gst_rsfile_src_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstRsfileSrc, gst_rsfile_src, GST_TYPE_BASE_SRC, _do_init);
static void
gst_rsfile_src_class_init (GstRsfileSrcClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSrcClass *gstbasesrc_class;
gobject_class = G_OBJECT_CLASS (klass);
gstelement_class = GST_ELEMENT_CLASS (klass);
gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
gobject_class->set_property = gst_rsfile_src_set_property;
gobject_class->get_property = gst_rsfile_src_get_property;
g_object_class_install_property (gobject_class, PROP_URI,
g_param_spec_string ("uri", "URI",
"URI of the file to read", NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
GST_PARAM_MUTABLE_READY));
gobject_class->finalize = gst_rsfile_src_finalize;
gst_element_class_set_static_metadata (gstelement_class,
"File Source",
"Source/Rsfile",
"Read from arbitrary point in a file",
"Sebastian Dröge <sebastian@centricular.com>");
gst_element_class_add_static_pad_template (gstelement_class, &src_template);
gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_rsfile_src_start);
gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_rsfile_src_stop);
gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_rsfile_src_is_seekable);
gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_rsfile_src_get_size);
gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_rsfile_src_fill);
}
static void
gst_rsfile_src_init (GstRsfileSrc * src)
{
gst_base_src_set_blocksize (GST_BASE_SRC (src), 4096);
src->instance = filesrc_new ();
}
static void
gst_rsfile_src_finalize (GObject * object)
{
GstRsfileSrc *src = GST_RSFILE_SRC (object);
source_drop (src->instance);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_rsfile_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstRsfileSrc *src = GST_RSFILE_SRC (object);
switch (prop_id) {
case PROP_URI:
source_set_uri (src->instance, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_rsfile_src_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstRsfileSrc *src = GST_RSFILE_SRC (object);
switch (prop_id) {
case PROP_URI:
g_value_take_string (value, source_get_uri (src->instance));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstFlowReturn
gst_rsfile_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
GstBuffer * buf)
{
GstRsfileSrc *src = GST_RSFILE_SRC (basesrc);
GstMapInfo map;
GstFlowReturn ret;
gsize size;
gst_buffer_map (buf, &map, GST_MAP_READWRITE);
size = map.size;
ret = source_fill (src->instance, offset, map.data, &size);
gst_buffer_unmap (buf, &map);
if (ret == GST_FLOW_OK)
gst_buffer_resize (buf, 0, size);
return ret;
}
static gboolean
gst_rsfile_src_is_seekable (GstBaseSrc * basesrc)
{
GstRsfileSrc *src = GST_RSFILE_SRC (basesrc);
return source_is_seekable (src->instance);
}
static gboolean
gst_rsfile_src_get_size (GstBaseSrc * basesrc, guint64 * size)
{
GstRsfileSrc *src = GST_RSFILE_SRC (basesrc);
*size = source_get_size (src->instance);
return TRUE;
}
/* open the rsfile, necessary to go to READY state */
static gboolean
gst_rsfile_src_start (GstBaseSrc * basesrc)
{
GstRsfileSrc *src = GST_RSFILE_SRC (basesrc);
return source_start (src->instance);
}
/* unmap and close the rsfile */
static gboolean
gst_rsfile_src_stop (GstBaseSrc * basesrc)
{
GstRsfileSrc *src = GST_RSFILE_SRC (basesrc);
return source_stop (src->instance);
}
static GstURIType
gst_rsfile_src_uri_get_type (GType type)
{
return GST_URI_SRC;
}
static const gchar *const *
gst_rsfile_src_uri_get_protocols (GType type)
{
static const gchar *protocols[] = { "file", NULL };
return protocols;
}
static gchar *
gst_rsfile_src_uri_get_uri (GstURIHandler * handler)
{
GstRsfileSrc *src = GST_RSFILE_SRC (handler);
return source_get_uri (src->instance);
}
static gboolean
gst_rsfile_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
GError ** err)
{
GstRsfileSrc *src = GST_RSFILE_SRC (handler);
if (!source_set_uri (src->instance, uri)) {
g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
"Can't handle URI '%s'", uri);
return FALSE;
}
return TRUE;
}
static void
gst_rsfile_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
{
GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
iface->get_type = gst_rsfile_src_uri_get_type;
iface->get_protocols = gst_rsfile_src_uri_get_protocols;
iface->get_uri = gst_rsfile_src_uri_get_uri;
iface->set_uri = gst_rsfile_src_uri_set_uri;
}

View file

@ -1,39 +0,0 @@
#ifndef __GST_RSFILE_SRC_H__
#define __GST_RSFILE_SRC_H__
#include <gst/gst.h>
#include <gst/base/gstbasesrc.h>
G_BEGIN_DECLS
#define GST_TYPE_RSFILE_SRC \
(gst_rsfile_src_get_type())
#define GST_RSFILE_SRC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RSFILE_SRC,GstRsfileSrc))
#define GST_RSFILE_SRC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RSFILE_SRC,GstRsfileSrcClass))
#define GST_IS_RSFILE_SRC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RSFILE_SRC))
#define GST_IS_RSFILE_SRC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RSFILE_SRC))
#define GST_RSFILE_SRC_CAST(obj) ((GstRsfileSrc*) obj)
typedef struct _GstRsfileSrc GstRsfileSrc;
typedef struct _GstRsfileSrcClass GstRsfileSrcClass;
struct _GstRsfileSrc {
GstBaseSrc element;
gchar *location;
gpointer instance;
};
struct _GstRsfileSrcClass {
GstBaseSrcClass parent_class;
};
G_GNUC_INTERNAL GType gst_rsfile_src_get_type (void);
G_END_DECLS
#endif /* __GST_RSFILE_SRC_H__ */

View file

@ -24,6 +24,10 @@ impl FileSrc {
fn new_source() -> Box<Source> {
Box::new(FileSrc::new())
}
pub extern "C" fn new_ptr() -> *mut Box<Source> {
let instance = Box::new(FileSrc::new_source());
return Box::into_raw(instance);
}
}
impl Source for FileSrc {
@ -138,8 +142,3 @@ impl Source for FileSrc {
}
}
#[no_mangle]
pub extern "C" fn filesrc_new() -> *mut Box<Source> {
let instance = Box::new(FileSrc::new_source());
return Box::into_raw(instance);
}

311
src/rssource.c Normal file
View file

@ -0,0 +1,311 @@
#include "rssource.h"
#include <string.h>
#include <stdint.h>
typedef struct {
gchar *name;
gchar *long_name;
gchar *description;
gchar *classification;
gchar *author;
void * (*create_instance) (void);
gchar **protocols;
} ElementData;
static GHashTable *sources;
/* Declarations for Rust code */
extern gboolean sources_register (void *plugin);
extern void source_drop (void * filesrc);
extern GstFlowReturn source_fill (void * filesrc, uint64_t offset, void * data, size_t * data_len);
extern gboolean source_set_uri (void * filesrc, const char *uri);
extern char * source_get_uri (void * filesrc);
extern uint64_t source_get_size (void * filesrc);
extern gboolean source_is_seekable (void * filesrc);
extern gboolean source_start (void * filesrc);
extern gboolean source_stop (void * filesrc);
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 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);
}
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);
src->instance = data->create_instance ();
}
static void
gst_rs_src_finalize (GObject * object)
{
GstRsSrc *src = GST_RS_SRC (object);
source_drop (src->instance);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_rs_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstRsSrc *src = GST_RS_SRC (object);
switch (prop_id) {
case PROP_URI:
source_set_uri (src->instance, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_rs_src_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstRsSrc *src = GST_RS_SRC (object);
switch (prop_id) {
case PROP_URI:
g_value_take_string (value, source_get_uri (src->instance));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstFlowReturn
gst_rs_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
GstBuffer * buf)
{
GstRsSrc *src = GST_RS_SRC (basesrc);
GstMapInfo map;
GstFlowReturn ret;
gsize size;
gst_buffer_map (buf, &map, GST_MAP_READWRITE);
size = map.size;
ret = source_fill (src->instance, offset, map.data, &size);
gst_buffer_unmap (buf, &map);
if (ret == GST_FLOW_OK)
gst_buffer_resize (buf, 0, size);
return ret;
}
static gboolean
gst_rs_src_is_seekable (GstBaseSrc * basesrc)
{
GstRsSrc *src = GST_RS_SRC (basesrc);
return source_is_seekable (src->instance);
}
static gboolean
gst_rs_src_get_size (GstBaseSrc * basesrc, guint64 * size)
{
GstRsSrc *src = GST_RS_SRC (basesrc);
*size = source_get_size (src->instance);
return TRUE;
}
/* open the rs, necessary to go to READY state */
static gboolean
gst_rs_src_start (GstBaseSrc * basesrc)
{
GstRsSrc *src = GST_RS_SRC (basesrc);
return source_start (src->instance);
}
/* unmap and close the rs */
static gboolean
gst_rs_src_stop (GstBaseSrc * basesrc)
{
GstRsSrc *src = GST_RS_SRC (basesrc);
return source_stop (src->instance);
}
static GstURIType
gst_rs_src_uri_get_type (GType type)
{
return GST_URI_SRC;
}
static const gchar *const *
gst_rs_src_uri_get_protocols (GType type)
{
static const gchar *protocols[] = { "file", NULL };
return protocols;
}
static gchar *
gst_rs_src_uri_get_uri (GstURIHandler * handler)
{
GstRsSrc *src = GST_RS_SRC (handler);
return source_get_uri (src->instance);
}
static gboolean
gst_rs_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
GError ** err)
{
GstRsSrc *src = GST_RS_SRC (handler);
if (!source_set_uri (src->instance, uri)) {
g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
"Can't handle URI '%s'", uri);
return FALSE;
}
return TRUE;
}
static void
gst_rs_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
{
GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
iface->get_type = gst_rs_src_uri_get_type;
iface->get_protocols = gst_rs_src_uri_get_protocols;
iface->get_uri = gst_rs_src_uri_get_uri;
iface->set_uri = gst_rs_src_uri_set_uri;
}
gboolean
gst_rs_source_plugin_init (GstPlugin * plugin)
{
sources = g_hash_table_new (g_direct_hash, g_direct_equal);
GST_DEBUG_CATEGORY_INIT (gst_rs_src_debug, "rssrc", 0, "rssrc element");
parent_class = g_type_class_ref (GST_TYPE_BASE_SRC);
return sources_register (plugin);
}
gboolean
gst_rs_source_register (GstPlugin * plugin, const gchar *name, const gchar * long_name, const gchar * description, const gchar * classification, const gchar * author, GstRank rank, void * (*create_instance) (void), const gchar *protocols)
{
GTypeInfo type_info = {
sizeof (GstRsSrcClass),
NULL,
NULL,
(GClassInitFunc) gst_rs_src_class_init,
NULL,
NULL,
sizeof (GstRsSrc),
0,
(GInstanceInitFunc) gst_rs_src_init
};
GInterfaceInfo iface_info = {
gst_rs_src_uri_handler_init,
NULL,
NULL
};
GType type;
gchar *type_name;
ElementData *data;
data = g_new0 (ElementData, 1);
data->name = g_strdup (name);
data->long_name = g_strdup (long_name);
data->description = g_strdup (description);
data->classification = g_strdup (classification);
data->author = g_strdup (author);
data->create_instance = create_instance;
data->protocols = g_strsplit (protocols, ":", -1);
type_name = g_strconcat ("RsSrc-", name, NULL);
type = g_type_register_static (GST_TYPE_BASE_SRC, type_name, &type_info, 0);
g_free (type_name);
g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &iface_info);
g_hash_table_insert (sources, GSIZE_TO_POINTER (type), data);
if (!gst_element_register (plugin, name, rank, type))
return FALSE;
return TRUE;
}

31
src/rssource.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef __GST_RS_SRC_H__
#define __GST_RS_SRC_H__
#include <gst/gst.h>
#include <gst/base/gstbasesrc.h>
G_BEGIN_DECLS
#define GST_RS_SRC(obj) \
((GstRsSrc *)obj)
#define GST_RS_SRC_CLASS(klass) \
((GstRsSrcKlass *)klass)
typedef struct _GstRsSrc GstRsSrc;
typedef struct _GstRsSrcClass GstRsSrcClass;
struct _GstRsSrc {
GstBaseSrc element;
gpointer instance;
};
struct _GstRsSrcClass {
GstBaseSrcClass parent_class;
};
G_GNUC_INTERNAL gboolean gst_rs_source_plugin_init (GstPlugin * plugin);
G_END_DECLS
#endif /* __GST_RS_SRC_H__ */