3 * Copyright (C) 2009 Igalia S.L
4 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include "gstdataurisrc.h"
29 #include <gst/base/gsttypefindhelper.h>
31 GST_DEBUG_CATEGORY (data_uri_src_debug);
32 #define GST_CAT_DEFAULT (data_uri_src_debug)
34 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
45 static void gst_data_uri_src_finalize (GObject * object);
46 static void gst_data_uri_src_set_property (GObject * object,
47 guint prop_id, const GValue * value, GParamSpec * pspec);
48 static void gst_data_uri_src_get_property (GObject * object,
49 guint prop_id, GValue * value, GParamSpec * pspec);
51 static GstCaps *gst_data_uri_src_get_caps (GstBaseSrc * src);
52 static gboolean gst_data_uri_src_get_size (GstBaseSrc * src, guint64 * size);
53 static gboolean gst_data_uri_src_is_seekable (GstBaseSrc * src);
54 static GstFlowReturn gst_data_uri_src_create (GstBaseSrc * src, guint64 offset,
55 guint size, GstBuffer ** buf);
56 static gboolean gst_data_uri_src_check_get_range (GstBaseSrc * src);
58 static void gst_data_uri_src_handler_init (gpointer g_iface,
60 static GstURIType gst_data_uri_src_get_uri_type (void);
61 static gchar **gst_data_uri_src_get_protocols (void);
62 static const gchar *gst_data_uri_src_get_uri (GstURIHandler * handler);
63 static gboolean gst_data_uri_src_set_uri (GstURIHandler * handler,
67 _do_init (GType gtype)
69 static const GInterfaceInfo urihandler_info = {
70 gst_data_uri_src_handler_init,
74 GST_DEBUG_CATEGORY_INIT (data_uri_src_debug, "dataurisrc", 0,
76 g_type_add_interface_static (gtype, GST_TYPE_URI_HANDLER, &urihandler_info);
79 GST_BOILERPLATE_FULL (GstDataURISrc, gst_data_uri_src, GstBaseSrc,
80 GST_TYPE_BASE_SRC, _do_init);
83 gst_data_uri_src_base_init (gpointer klass)
85 GstElementClass *element_class = (GstElementClass *) (klass);
87 gst_element_class_add_pad_template (element_class,
88 gst_static_pad_template_get (&src_template));
89 gst_element_class_set_details_simple (element_class,
90 "data: URI source element", "Source", "Handles data: uris",
91 "Philippe Normand <pnormand@igalia.com>, "
92 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
97 gst_data_uri_src_class_init (GstDataURISrcClass * klass)
99 GObjectClass *gobject_class = (GObjectClass *) klass;
100 GstBaseSrcClass *basesrc_class = (GstBaseSrcClass *) klass;
102 gobject_class->finalize = gst_data_uri_src_finalize;
103 gobject_class->set_property = gst_data_uri_src_set_property;
104 gobject_class->get_property = gst_data_uri_src_get_property;
106 g_object_class_install_property (gobject_class, PROP_URI,
107 g_param_spec_string ("uri",
109 "URI that should be used",
110 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_data_uri_src_get_caps);
113 basesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_data_uri_src_get_size);
114 basesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_data_uri_src_is_seekable);
115 basesrc_class->create = GST_DEBUG_FUNCPTR (gst_data_uri_src_create);
116 basesrc_class->check_get_range =
117 GST_DEBUG_FUNCPTR (gst_data_uri_src_check_get_range);
121 gst_data_uri_src_init (GstDataURISrc * src, GstDataURISrcClass * g_class)
123 gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_BYTES);
127 gst_data_uri_src_finalize (GObject * object)
129 GstDataURISrc *src = GST_DATA_URI_SRC (object);
135 gst_buffer_unref (src->buffer);
138 G_OBJECT_CLASS (parent_class)->finalize (object);
142 gst_data_uri_src_set_property (GObject * object, guint prop_id,
143 const GValue * value, GParamSpec * pspec)
145 GstDataURISrc *src = GST_DATA_URI_SRC (object);
149 gst_data_uri_src_set_uri (GST_URI_HANDLER (src),
150 g_value_get_string (value));
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 gst_data_uri_src_get_property (GObject * object,
160 guint prop_id, GValue * value, GParamSpec * pspec)
162 GstDataURISrc *src = GST_DATA_URI_SRC (object);
166 g_value_set_string (value,
167 gst_data_uri_src_get_uri (GST_URI_HANDLER (src)));
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 gst_data_uri_src_get_caps (GstBaseSrc * basesrc)
178 GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
181 GST_OBJECT_LOCK (src);
182 if (!src->buffer || !GST_BUFFER_CAPS (src->buffer))
183 caps = gst_caps_new_empty ();
185 caps = gst_buffer_get_caps (src->buffer);
186 GST_OBJECT_UNLOCK (src);
192 gst_data_uri_src_get_size (GstBaseSrc * basesrc, guint64 * size)
194 GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
197 GST_OBJECT_LOCK (src);
203 *size = GST_BUFFER_SIZE (src->buffer);
205 GST_OBJECT_UNLOCK (src);
211 gst_data_uri_src_is_seekable (GstBaseSrc * basesrc)
217 gst_data_uri_src_create (GstBaseSrc * basesrc, guint64 offset, guint size,
220 GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
223 GST_OBJECT_LOCK (src);
225 ret = GST_FLOW_NOT_NEGOTIATED;
226 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), (NULL));
227 } else if (offset + size > GST_BUFFER_SIZE (src->buffer)) {
228 ret = GST_FLOW_UNEXPECTED;
231 *buf = gst_buffer_create_sub (src->buffer, offset, size);
232 gst_buffer_set_caps (*buf, GST_BUFFER_CAPS (src->buffer));
234 GST_OBJECT_UNLOCK (src);
240 gst_data_uri_src_check_get_range (GstBaseSrc * basesrc)
246 gst_data_uri_src_handler_init (gpointer g_iface, gpointer iface_data)
248 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
250 iface->get_type = gst_data_uri_src_get_uri_type;
251 iface->get_protocols = gst_data_uri_src_get_protocols;
252 iface->get_uri = gst_data_uri_src_get_uri;
253 iface->set_uri = gst_data_uri_src_set_uri;
257 gst_data_uri_src_get_uri_type (void)
263 gst_data_uri_src_get_protocols (void)
265 static gchar *protocols[] = { "data", 0 };
271 gst_data_uri_src_get_uri (GstURIHandler * handler)
273 GstDataURISrc *src = GST_DATA_URI_SRC (handler);
279 gst_data_uri_src_set_uri (GstURIHandler * handler, const gchar * uri)
281 GstDataURISrc *src = GST_DATA_URI_SRC (handler);
282 gboolean ret = FALSE;
283 gchar *mimetype = NULL;
284 const gchar *parameters_start;
285 const gchar *data_start;
287 gboolean base64 = FALSE;
288 gchar *charset = NULL;
290 GST_OBJECT_LOCK (src);
291 if (GST_STATE (src) >= GST_STATE_PAUSED)
294 /* uri must be an URI as defined in RFC 2397
295 * data:[<mediatype>][;base64],<data>
297 if (strncmp ("data:", uri, 5) != 0)
302 parameters_start = strchr (uri, ';');
303 data_start = strchr (uri, ',');
304 if (data_start == NULL)
307 if (data_start != uri && parameters_start != uri)
310 (parameters_start ? parameters_start : data_start) - uri);
312 mimetype = g_strdup ("text/plain");
314 GST_DEBUG_OBJECT (src, "Mimetype: %s", mimetype);
316 if (parameters_start != NULL) {
319 g_strndup (parameters_start + 1, data_start - parameters_start - 1);
320 gchar **parameters_strv;
322 parameters_strv = g_strsplit (parameters, ";", -1);
324 GST_DEBUG_OBJECT (src, "Parameters: ");
325 walk = parameters_strv;
327 GST_DEBUG_OBJECT (src, "\t %s", *walk);
328 if (strcmp ("base64", *walk) == 0) {
330 } else if (strncmp ("charset=", *walk, 8) == 0) {
331 charset = g_strdup (*walk + 8);
336 g_strfreev (parameters_strv);
344 src->buffer = gst_buffer_new ();
345 GST_BUFFER_DATA (src->buffer) =
346 (guint8 *) g_base64_decode (data_start, &bsize);
347 GST_BUFFER_MALLOCDATA (src->buffer) = GST_BUFFER_DATA (src->buffer);
348 GST_BUFFER_SIZE (src->buffer) = bsize;
352 /* URI encoded, i.e. "percent" encoding */
353 data = g_uri_unescape_string (data_start, NULL);
355 goto invalid_uri_encoded_data;
357 src->buffer = gst_buffer_new ();
358 GST_BUFFER_DATA (src->buffer) = (guint8 *) data;
359 GST_BUFFER_MALLOCDATA (src->buffer) = GST_BUFFER_DATA (src->buffer);
360 GST_BUFFER_SIZE (src->buffer) = strlen (data) + 1;
363 /* Convert to UTF8 */
364 if (strcmp ("text/plain", mimetype) == 0 &&
365 charset && strcasecmp ("US-ASCII", charset) != 0
366 && strcasecmp ("UTF-8", charset) != 0) {
369 gchar *old_data = (gchar *) GST_BUFFER_DATA (src->buffer);
373 g_convert_with_fallback (old_data, -1, "UTF-8", charset, "*", &read,
376 GST_BUFFER_DATA (src->buffer) = GST_BUFFER_MALLOCDATA (src->buffer) =
378 GST_BUFFER_SIZE (src->buffer) = written;
381 caps = gst_type_find_helper_for_buffer (GST_OBJECT (src), src->buffer, NULL);
383 caps = gst_caps_new_simple (mimetype, NULL);
384 gst_buffer_set_caps (src->buffer, caps);
385 gst_caps_unref (caps);
388 GST_OBJECT_UNLOCK (src);
398 GST_OBJECT_UNLOCK (src);
399 GST_ELEMENT_ERROR (src, STREAM, FORMAT, (NULL), (NULL));
404 GST_OBJECT_UNLOCK (src);
405 GST_ELEMENT_ERROR (src, CORE, FAILED, (NULL), (NULL));
408 invalid_uri_encoded_data:
410 GST_OBJECT_UNLOCK (src);
411 GST_ELEMENT_ERROR (src, STREAM, FORMAT, (NULL), (NULL));
417 plugin_init (GstPlugin * plugin)
419 return gst_element_register (plugin, "dataurisrc",
420 GST_RANK_PRIMARY, GST_TYPE_DATA_URI_SRC);
423 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
427 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);