2016-05-15 15:54:09 +00:00
|
|
|
/* Copyright (C) 2016 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2016-05-14 12:44:40 +00:00
|
|
|
#include "rssource.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-07-20 08:29:27 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2016-05-14 12:44:40 +00:00
|
|
|
gchar *long_name;
|
|
|
|
gchar *description;
|
|
|
|
gchar *classification;
|
|
|
|
gchar *author;
|
2016-07-20 09:46:03 +00:00
|
|
|
void *create_instance;
|
2016-05-14 12:44:40 +00:00
|
|
|
gchar **protocols;
|
|
|
|
} ElementData;
|
|
|
|
static GHashTable *sources;
|
|
|
|
|
|
|
|
/* Declarations for Rust code */
|
|
|
|
extern gboolean sources_register (void *plugin);
|
2016-07-20 09:46:03 +00:00
|
|
|
extern void *source_new (GstRsSrc * source, void *create_instance);
|
2016-08-22 20:03:06 +00:00
|
|
|
extern void source_drop (void *rssource);
|
|
|
|
extern GstFlowReturn source_fill (void *rssource,
|
2016-07-20 18:06:17 +00:00
|
|
|
uint64_t offset, void *data, size_t * data_len);
|
2016-08-22 20:03:06 +00:00
|
|
|
extern gboolean source_do_seek (void *rssource, uint64_t start, uint64_t stop);
|
2016-08-22 21:07:44 +00:00
|
|
|
extern gboolean source_set_uri (void *rssource, const char *uri, GError ** err);
|
2016-08-22 20:03:06 +00:00
|
|
|
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);
|
2016-07-20 08:29:27 +00:00
|
|
|
|
|
|
|
extern void cstring_drop (void *str);
|
2016-05-19 07:47:31 +00:00
|
|
|
|
2016-05-14 12:44:40 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2016-07-20 08:29:27 +00:00
|
|
|
static void gst_rs_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
|
2016-05-14 12:44:40 +00:00
|
|
|
|
|
|
|
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);
|
2016-05-14 14:57:25 +00:00
|
|
|
static gboolean gst_rs_src_do_seek (GstBaseSrc * src, GstSegment * segment);
|
2016-05-14 12:44:40 +00:00
|
|
|
|
|
|
|
static GObjectClass *parent_class;
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rs_src_class_init (GstRsSrcClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
GstBaseSrcClass *gstbasesrc_class;
|
2016-07-20 08:29:27 +00:00
|
|
|
ElementData *data = g_hash_table_lookup (sources,
|
|
|
|
GSIZE_TO_POINTER (G_TYPE_FROM_CLASS (klass)));
|
2016-05-14 12:44:40 +00:00
|
|
|
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,
|
2016-07-20 08:29:27 +00:00
|
|
|
data->long_name, data->classification, data->description, data->author);
|
2016-05-14 12:44:40 +00:00
|
|
|
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);
|
2016-05-14 14:57:25 +00:00
|
|
|
gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_rs_src_do_seek);
|
2016-05-14 12:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rs_src_init (GstRsSrc * src, GstRsSrcClass * klass)
|
|
|
|
{
|
2016-07-20 08:29:27 +00:00
|
|
|
ElementData *data = g_hash_table_lookup (sources,
|
|
|
|
GSIZE_TO_POINTER (G_TYPE_FROM_CLASS (klass)));
|
2016-05-14 12:44:40 +00:00
|
|
|
g_assert (data != NULL);
|
|
|
|
|
|
|
|
gst_base_src_set_blocksize (GST_BASE_SRC (src), 4096);
|
|
|
|
|
2016-07-20 09:46:03 +00:00
|
|
|
src->instance = source_new (src, data->create_instance);
|
2016-05-14 12:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rs_src_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstRsSrc *src = GST_RS_SRC (object);
|
|
|
|
|
2016-08-22 20:03:06 +00:00
|
|
|
source_drop (src->instance);
|
2016-05-14 12:44:40 +00:00
|
|
|
|
|
|
|
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:
|
2016-08-22 21:07:44 +00:00
|
|
|
source_set_uri (src->instance, g_value_get_string (value), NULL);
|
2016-05-14 12:44:40 +00:00
|
|
|
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) {
|
2016-07-20 08:29:27 +00:00
|
|
|
case PROP_URI:{
|
2016-08-22 20:03:06 +00:00
|
|
|
gchar *str = source_get_uri (src->instance);
|
2016-05-19 07:47:31 +00:00
|
|
|
g_value_set_string (value, str);
|
|
|
|
if (str)
|
|
|
|
cstring_drop (str);
|
2016-05-14 12:44:40 +00:00
|
|
|
break;
|
2016-05-19 07:47:31 +00:00
|
|
|
}
|
2016-05-14 12:44:40 +00:00
|
|
|
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;
|
2016-05-14 14:43:43 +00:00
|
|
|
gsize map_size, size;
|
2016-05-14 12:44:40 +00:00
|
|
|
|
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READWRITE);
|
2016-05-14 14:43:43 +00:00
|
|
|
size = length;
|
|
|
|
map_size = map.size;
|
2016-08-22 20:03:06 +00:00
|
|
|
ret = source_fill (src->instance, offset, map.data, &size);
|
2016-05-14 12:44:40 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
2016-05-14 14:43:43 +00:00
|
|
|
if (ret == GST_FLOW_OK && size != map_size)
|
2016-05-14 12:44:40 +00:00
|
|
|
gst_buffer_resize (buf, 0, size);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_rs_src_is_seekable (GstBaseSrc * basesrc)
|
|
|
|
{
|
|
|
|
GstRsSrc *src = GST_RS_SRC (basesrc);
|
|
|
|
|
2016-08-22 20:03:06 +00:00
|
|
|
return source_is_seekable (src->instance);
|
2016-05-14 12:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_rs_src_get_size (GstBaseSrc * basesrc, guint64 * size)
|
|
|
|
{
|
|
|
|
GstRsSrc *src = GST_RS_SRC (basesrc);
|
|
|
|
|
2016-08-22 20:03:06 +00:00
|
|
|
*size = source_get_size (src->instance);
|
2016-05-14 12:44:40 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-08-22 20:03:06 +00:00
|
|
|
return source_start (src->instance);
|
2016-05-14 12:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_rs_src_stop (GstBaseSrc * basesrc)
|
|
|
|
{
|
|
|
|
GstRsSrc *src = GST_RS_SRC (basesrc);
|
|
|
|
|
2016-08-22 20:03:06 +00:00
|
|
|
return source_stop (src->instance);
|
2016-05-14 12:44:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 14:57:25 +00:00
|
|
|
static gboolean
|
|
|
|
gst_rs_src_do_seek (GstBaseSrc * basesrc, GstSegment * segment)
|
|
|
|
{
|
|
|
|
GstRsSrc *src = GST_RS_SRC (basesrc);
|
|
|
|
gboolean ret;
|
|
|
|
|
2016-08-22 20:03:06 +00:00
|
|
|
ret = source_do_seek (src->instance, segment->start, segment->stop);
|
2016-05-14 14:57:25 +00:00
|
|
|
if (!ret)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return GST_BASE_SRC_CLASS (parent_class)->do_seek (basesrc, segment);
|
|
|
|
}
|
|
|
|
|
2016-05-14 12:44:40 +00:00
|
|
|
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)
|
|
|
|
{
|
2016-07-20 08:29:27 +00:00
|
|
|
ElementData *data = g_hash_table_lookup (sources, GSIZE_TO_POINTER (type));
|
2016-05-14 14:04:53 +00:00
|
|
|
g_assert (data != NULL);
|
2016-05-14 12:44:40 +00:00
|
|
|
|
2016-05-14 14:04:53 +00:00
|
|
|
return (const gchar * const *) data->protocols;
|
2016-05-14 12:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
gst_rs_src_uri_get_uri (GstURIHandler * handler)
|
|
|
|
{
|
|
|
|
GstRsSrc *src = GST_RS_SRC (handler);
|
|
|
|
|
2016-08-22 20:03:06 +00:00
|
|
|
return source_get_uri (src->instance);
|
2016-05-14 12:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_rs_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
|
|
|
|
GError ** err)
|
|
|
|
{
|
|
|
|
GstRsSrc *src = GST_RS_SRC (handler);
|
|
|
|
|
2016-08-22 21:07:44 +00:00
|
|
|
if (!source_set_uri (src->instance, uri, err))
|
2016-05-14 12:44:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
void
|
|
|
|
gst_rs_source_error (GstRsSrc * src, GQuark error_domain, gint error_code,
|
|
|
|
const gchar * message, const gchar * debug, const gchar * file,
|
|
|
|
const gchar * function, guint line)
|
|
|
|
{
|
|
|
|
gst_element_message_full (GST_ELEMENT (src), GST_MESSAGE_ERROR, error_domain,
|
|
|
|
error_code, g_strdup (message), g_strdup (debug), file, function, line);
|
|
|
|
}
|
|
|
|
|
2016-05-14 12:44:40 +00:00
|
|
|
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
|
2016-07-20 08:29:27 +00:00
|
|
|
gst_rs_source_register (GstPlugin * plugin, const gchar * name,
|
|
|
|
const gchar * long_name, const gchar * description,
|
|
|
|
const gchar * classification, const gchar * author, GstRank rank,
|
2016-07-20 09:46:03 +00:00
|
|
|
void *create_instance, const gchar * protocols, gboolean push_only)
|
2016-05-14 12:44:40 +00:00
|
|
|
{
|
|
|
|
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->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);
|
2016-07-20 08:29:27 +00:00
|
|
|
type =
|
|
|
|
g_type_register_static (push_only ? GST_TYPE_PUSH_SRC : GST_TYPE_BASE_SRC,
|
|
|
|
type_name, &type_info, 0);
|
2016-05-14 12:44:40 +00:00
|
|
|
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;
|
|
|
|
}
|