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.
23 * SECTION:element-dataurisrc
25 * dataurisrc handles data: URIs, see <ulink url="http://tools.ietf.org/html/rfc2397">RFC 2397</ulink> for more information.
28 * <title>Example launch line</title>
30 * gst-launch-0.10 -v dataurisrc uri="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAfElEQVQ4je2MwQnAIAxFgziA4EnczIsO4MEROo/gzZWc4xdTbe1R6LGRR74heYS7iKElzfcMiRnt4hf8gk8EayB6luefue/HzlJfCA50XsNjYRxprZmenXNIKSGEsC+QUqK1hhgj521BzhnWWiilUGvdF5RS4L2HMQZCCJy8sHMm2TYdJAAAAABJRU5ErkJggg==" ! pngdec ! ffmpegcolorspace ! freeze ! ffmpegcolorspace ! autovideosink
31 * ]| This pipeline displays a small 16x16 PNG image from the data URI.
39 #include "gstdataurisrc.h"
42 #include <gst/base/gsttypefindhelper.h>
44 GST_DEBUG_CATEGORY (data_uri_src_debug);
45 #define GST_CAT_DEFAULT (data_uri_src_debug)
47 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
58 static void gst_data_uri_src_finalize (GObject * object);
59 static void gst_data_uri_src_set_property (GObject * object,
60 guint prop_id, const GValue * value, GParamSpec * pspec);
61 static void gst_data_uri_src_get_property (GObject * object,
62 guint prop_id, GValue * value, GParamSpec * pspec);
64 static GstCaps *gst_data_uri_src_get_caps (GstBaseSrc * src);
65 static gboolean gst_data_uri_src_get_size (GstBaseSrc * src, guint64 * size);
66 static gboolean gst_data_uri_src_is_seekable (GstBaseSrc * src);
67 static GstFlowReturn gst_data_uri_src_create (GstBaseSrc * src, guint64 offset,
68 guint size, GstBuffer ** buf);
69 static gboolean gst_data_uri_src_check_get_range (GstBaseSrc * src);
70 static gboolean gst_data_uri_src_start (GstBaseSrc * src);
72 static void gst_data_uri_src_handler_init (gpointer g_iface,
74 static GstURIType gst_data_uri_src_get_uri_type (void);
75 static gchar **gst_data_uri_src_get_protocols (void);
76 static const gchar *gst_data_uri_src_get_uri (GstURIHandler * handler);
77 static gboolean gst_data_uri_src_set_uri (GstURIHandler * handler,
81 _do_init (GType gtype)
83 static const GInterfaceInfo urihandler_info = {
84 gst_data_uri_src_handler_init,
88 GST_DEBUG_CATEGORY_INIT (data_uri_src_debug, "dataurisrc", 0,
90 g_type_add_interface_static (gtype, GST_TYPE_URI_HANDLER, &urihandler_info);
93 GST_BOILERPLATE_FULL (GstDataURISrc, gst_data_uri_src, GstBaseSrc,
94 GST_TYPE_BASE_SRC, _do_init);
97 gst_data_uri_src_base_init (gpointer klass)
99 GstElementClass *element_class = (GstElementClass *) (klass);
101 gst_element_class_add_pad_template (element_class,
102 gst_static_pad_template_get (&src_template));
103 gst_element_class_set_details_simple (element_class,
104 "data: URI source element", "Source", "Handles data: uris",
105 "Philippe Normand <pnormand@igalia.com>, "
106 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
111 gst_data_uri_src_class_init (GstDataURISrcClass * klass)
113 GObjectClass *gobject_class = (GObjectClass *) klass;
114 GstBaseSrcClass *basesrc_class = (GstBaseSrcClass *) klass;
116 gobject_class->finalize = gst_data_uri_src_finalize;
117 gobject_class->set_property = gst_data_uri_src_set_property;
118 gobject_class->get_property = gst_data_uri_src_get_property;
120 g_object_class_install_property (gobject_class, PROP_URI,
121 g_param_spec_string ("uri",
123 "URI that should be used",
124 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_data_uri_src_get_caps);
127 basesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_data_uri_src_get_size);
128 basesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_data_uri_src_is_seekable);
129 basesrc_class->create = GST_DEBUG_FUNCPTR (gst_data_uri_src_create);
130 basesrc_class->check_get_range =
131 GST_DEBUG_FUNCPTR (gst_data_uri_src_check_get_range);
132 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_data_uri_src_start);
136 gst_data_uri_src_init (GstDataURISrc * src, GstDataURISrcClass * g_class)
138 gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_BYTES);
142 gst_data_uri_src_finalize (GObject * object)
144 GstDataURISrc *src = GST_DATA_URI_SRC (object);
150 gst_buffer_unref (src->buffer);
153 G_OBJECT_CLASS (parent_class)->finalize (object);
157 gst_data_uri_src_set_property (GObject * object, guint prop_id,
158 const GValue * value, GParamSpec * pspec)
160 GstDataURISrc *src = GST_DATA_URI_SRC (object);
164 gst_data_uri_src_set_uri (GST_URI_HANDLER (src),
165 g_value_get_string (value));
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 gst_data_uri_src_get_property (GObject * object,
175 guint prop_id, GValue * value, GParamSpec * pspec)
177 GstDataURISrc *src = GST_DATA_URI_SRC (object);
181 g_value_set_string (value,
182 gst_data_uri_src_get_uri (GST_URI_HANDLER (src)));
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 gst_data_uri_src_get_caps (GstBaseSrc * basesrc)
193 GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
196 GST_OBJECT_LOCK (src);
197 if (!src->buffer || !GST_BUFFER_CAPS (src->buffer))
198 caps = gst_caps_new_empty ();
200 caps = gst_buffer_get_caps (src->buffer);
201 GST_OBJECT_UNLOCK (src);
207 gst_data_uri_src_get_size (GstBaseSrc * basesrc, guint64 * size)
209 GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
212 GST_OBJECT_LOCK (src);
218 *size = GST_BUFFER_SIZE (src->buffer);
220 GST_OBJECT_UNLOCK (src);
226 gst_data_uri_src_is_seekable (GstBaseSrc * basesrc)
232 gst_data_uri_src_create (GstBaseSrc * basesrc, guint64 offset, guint size,
235 GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
238 GST_OBJECT_LOCK (src);
243 /* This is only correct because GstBaseSrc already clips size for us to be no
244 * larger than the max. available size if a segment at the end is requested */
245 if (offset + size > GST_BUFFER_SIZE (src->buffer)) {
246 ret = GST_FLOW_UNEXPECTED;
249 *buf = gst_buffer_create_sub (src->buffer, offset, size);
250 gst_buffer_set_caps (*buf, GST_BUFFER_CAPS (src->buffer));
253 GST_OBJECT_UNLOCK (src);
260 GST_OBJECT_UNLOCK (src);
261 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), (NULL));
262 return GST_FLOW_NOT_NEGOTIATED;
267 gst_data_uri_src_check_get_range (GstBaseSrc * basesrc)
273 gst_data_uri_src_start (GstBaseSrc * basesrc)
275 GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
277 GST_OBJECT_LOCK (src);
279 if (src->uri == NULL || *src->uri == '\0' || src->buffer == NULL)
282 GST_OBJECT_UNLOCK (src);
289 GST_OBJECT_UNLOCK (src);
290 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
291 ("No valid data URI specified, or the data URI could not be parsed."),
298 gst_data_uri_src_handler_init (gpointer g_iface, gpointer iface_data)
300 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
302 iface->get_type = gst_data_uri_src_get_uri_type;
303 iface->get_protocols = gst_data_uri_src_get_protocols;
304 iface->get_uri = gst_data_uri_src_get_uri;
305 iface->set_uri = gst_data_uri_src_set_uri;
309 gst_data_uri_src_get_uri_type (void)
315 gst_data_uri_src_get_protocols (void)
317 static gchar *protocols[] = { (char *) "data", 0 };
323 gst_data_uri_src_get_uri (GstURIHandler * handler)
325 GstDataURISrc *src = GST_DATA_URI_SRC (handler);
331 gst_data_uri_src_set_uri (GstURIHandler * handler, const gchar * uri)
333 GstDataURISrc *src = GST_DATA_URI_SRC (handler);
334 gboolean ret = FALSE;
335 gchar *mimetype = NULL;
336 const gchar *parameters_start;
337 const gchar *data_start;
338 const gchar *orig_uri = uri;
340 gboolean base64 = FALSE;
341 gchar *charset = NULL;
343 GST_OBJECT_LOCK (src);
344 if (GST_STATE (src) >= GST_STATE_PAUSED)
347 /* uri must be an URI as defined in RFC 2397
348 * data:[<mediatype>][;base64],<data>
350 if (strncmp ("data:", uri, 5) != 0)
355 parameters_start = strchr (uri, ';');
356 data_start = strchr (uri, ',');
357 if (data_start == NULL)
360 if (data_start != uri && parameters_start != uri)
363 (parameters_start ? parameters_start : data_start) - uri);
365 mimetype = g_strdup ("text/plain");
367 GST_DEBUG_OBJECT (src, "Mimetype: %s", mimetype);
369 if (parameters_start != NULL) {
372 g_strndup (parameters_start + 1, data_start - parameters_start - 1);
373 gchar **parameters_strv;
375 parameters_strv = g_strsplit (parameters, ";", -1);
377 GST_DEBUG_OBJECT (src, "Parameters: ");
378 walk = parameters_strv;
380 GST_DEBUG_OBJECT (src, "\t %s", *walk);
381 if (strcmp ("base64", *walk) == 0) {
383 } else if (strncmp ("charset=", *walk, 8) == 0) {
384 charset = g_strdup (*walk + 8);
389 g_strfreev (parameters_strv);
397 src->buffer = gst_buffer_new ();
398 GST_BUFFER_DATA (src->buffer) =
399 (guint8 *) g_base64_decode (data_start, &bsize);
400 GST_BUFFER_MALLOCDATA (src->buffer) = GST_BUFFER_DATA (src->buffer);
401 GST_BUFFER_SIZE (src->buffer) = bsize;
405 /* URI encoded, i.e. "percent" encoding */
406 data = g_uri_unescape_string (data_start, NULL);
408 goto invalid_uri_encoded_data;
410 src->buffer = gst_buffer_new ();
411 GST_BUFFER_DATA (src->buffer) = (guint8 *) data;
412 GST_BUFFER_MALLOCDATA (src->buffer) = GST_BUFFER_DATA (src->buffer);
413 GST_BUFFER_SIZE (src->buffer) = strlen (data) + 1;
416 /* Convert to UTF8 */
417 if (strcmp ("text/plain", mimetype) == 0 &&
418 charset && strcasecmp ("US-ASCII", charset) != 0
419 && strcasecmp ("UTF-8", charset) != 0) {
422 gchar *old_data = (gchar *) GST_BUFFER_DATA (src->buffer);
426 g_convert_with_fallback (old_data, -1, "UTF-8", charset, (char *) "*",
427 &read, &written, NULL);
429 GST_BUFFER_DATA (src->buffer) = GST_BUFFER_MALLOCDATA (src->buffer) =
431 GST_BUFFER_SIZE (src->buffer) = written;
434 caps = gst_type_find_helper_for_buffer (GST_OBJECT (src), src->buffer, NULL);
436 caps = gst_caps_new_simple (mimetype, NULL);
437 gst_buffer_set_caps (src->buffer, caps);
438 gst_caps_unref (caps);
441 src->uri = g_strdup (orig_uri);
447 GST_OBJECT_UNLOCK (src);
456 GST_WARNING_OBJECT (src, "invalid URI '%s'", uri);
461 GST_WARNING_OBJECT (src, "Can't set URI in %s state",
462 gst_element_state_get_name (GST_STATE (src)));
465 invalid_uri_encoded_data:
467 GST_WARNING_OBJECT (src, "Failed to parse data encoded in URI '%s'", uri);
473 plugin_init (GstPlugin * plugin)
475 return gst_element_register (plugin, "dataurisrc",
476 GST_RANK_PRIMARY, GST_TYPE_DATA_URI_SRC);
479 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
483 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);