Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / dataurisrc / gstdataurisrc.c
1 /* GStreamer
2  *
3  * Copyright (C) 2009 Igalia S.L
4  * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:element-dataurisrc
24  *
25  * dataurisrc handles data: URIs, see <ulink url="http://tools.ietf.org/html/rfc2397">RFC 2397</ulink> for more information.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
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.
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include "gstdataurisrc.h"
40
41 #include <string.h>
42 #include <gst/base/gsttypefindhelper.h>
43
44 GST_DEBUG_CATEGORY (data_uri_src_debug);
45 #define GST_CAT_DEFAULT (data_uri_src_debug)
46
47 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS_ANY);
51
52 enum
53 {
54   PROP_0,
55   PROP_URI,
56 };
57
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);
63
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);
71
72 static void gst_data_uri_src_handler_init (gpointer g_iface,
73     gpointer iface_data);
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,
78     const gchar * uri);
79
80 static void
81 _do_init (GType gtype)
82 {
83   static const GInterfaceInfo urihandler_info = {
84     gst_data_uri_src_handler_init,
85     0, 0
86   };
87
88   GST_DEBUG_CATEGORY_INIT (data_uri_src_debug, "dataurisrc", 0,
89       "data: URI source");
90   g_type_add_interface_static (gtype, GST_TYPE_URI_HANDLER, &urihandler_info);
91 }
92
93 GST_BOILERPLATE_FULL (GstDataURISrc, gst_data_uri_src, GstBaseSrc,
94     GST_TYPE_BASE_SRC, _do_init);
95
96 static void
97 gst_data_uri_src_base_init (gpointer klass)
98 {
99   GstElementClass *element_class = (GstElementClass *) (klass);
100
101   gst_element_class_add_static_pad_template (element_class, &src_template);
102   gst_element_class_set_details_simple (element_class,
103       "data: URI source element", "Source", "Handles data: uris",
104       "Philippe Normand <pnormand@igalia.com>, "
105       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
106
107 }
108
109 static void
110 gst_data_uri_src_class_init (GstDataURISrcClass * klass)
111 {
112   GObjectClass *gobject_class = (GObjectClass *) klass;
113   GstBaseSrcClass *basesrc_class = (GstBaseSrcClass *) klass;
114
115   gobject_class->finalize = gst_data_uri_src_finalize;
116   gobject_class->set_property = gst_data_uri_src_set_property;
117   gobject_class->get_property = gst_data_uri_src_get_property;
118
119   g_object_class_install_property (gobject_class, PROP_URI,
120       g_param_spec_string ("uri",
121           "URI",
122           "URI that should be used",
123           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124
125   basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_data_uri_src_get_caps);
126   basesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_data_uri_src_get_size);
127   basesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_data_uri_src_is_seekable);
128   basesrc_class->create = GST_DEBUG_FUNCPTR (gst_data_uri_src_create);
129   basesrc_class->check_get_range =
130       GST_DEBUG_FUNCPTR (gst_data_uri_src_check_get_range);
131   basesrc_class->start = GST_DEBUG_FUNCPTR (gst_data_uri_src_start);
132 }
133
134 static void
135 gst_data_uri_src_init (GstDataURISrc * src, GstDataURISrcClass * g_class)
136 {
137   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_BYTES);
138 }
139
140 static void
141 gst_data_uri_src_finalize (GObject * object)
142 {
143   GstDataURISrc *src = GST_DATA_URI_SRC (object);
144
145   g_free (src->uri);
146   src->uri = NULL;
147
148   if (src->buffer)
149     gst_buffer_unref (src->buffer);
150   src->buffer = NULL;
151
152   G_OBJECT_CLASS (parent_class)->finalize (object);
153 }
154
155 static void
156 gst_data_uri_src_set_property (GObject * object, guint prop_id,
157     const GValue * value, GParamSpec * pspec)
158 {
159   GstDataURISrc *src = GST_DATA_URI_SRC (object);
160
161   switch (prop_id) {
162     case PROP_URI:
163       gst_data_uri_src_set_uri (GST_URI_HANDLER (src),
164           g_value_get_string (value));
165       break;
166     default:
167       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168       break;
169   }
170 }
171
172 static void
173 gst_data_uri_src_get_property (GObject * object,
174     guint prop_id, GValue * value, GParamSpec * pspec)
175 {
176   GstDataURISrc *src = GST_DATA_URI_SRC (object);
177
178   switch (prop_id) {
179     case PROP_URI:
180       g_value_set_string (value,
181           gst_data_uri_src_get_uri (GST_URI_HANDLER (src)));
182       break;
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186   }
187 }
188
189 static GstCaps *
190 gst_data_uri_src_get_caps (GstBaseSrc * basesrc)
191 {
192   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
193   GstCaps *caps;
194
195   GST_OBJECT_LOCK (src);
196   if (!src->buffer || !GST_BUFFER_CAPS (src->buffer))
197     caps = gst_caps_new_empty ();
198   else
199     caps = gst_buffer_get_caps (src->buffer);
200   GST_OBJECT_UNLOCK (src);
201
202   return caps;
203 }
204
205 static gboolean
206 gst_data_uri_src_get_size (GstBaseSrc * basesrc, guint64 * size)
207 {
208   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
209   gboolean ret;
210
211   GST_OBJECT_LOCK (src);
212   if (!src->buffer) {
213     ret = FALSE;
214     *size = -1;
215   } else {
216     ret = TRUE;
217     *size = GST_BUFFER_SIZE (src->buffer);
218   }
219   GST_OBJECT_UNLOCK (src);
220
221   return ret;
222 }
223
224 static gboolean
225 gst_data_uri_src_is_seekable (GstBaseSrc * basesrc)
226 {
227   return TRUE;
228 }
229
230 static GstFlowReturn
231 gst_data_uri_src_create (GstBaseSrc * basesrc, guint64 offset, guint size,
232     GstBuffer ** buf)
233 {
234   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
235   GstFlowReturn ret;
236
237   GST_OBJECT_LOCK (src);
238
239   if (!src->buffer)
240     goto no_buffer;
241
242   /* This is only correct because GstBaseSrc already clips size for us to be no
243    * larger than the max. available size if a segment at the end is requested */
244   if (offset + size > GST_BUFFER_SIZE (src->buffer)) {
245     ret = GST_FLOW_UNEXPECTED;
246   } else {
247     ret = GST_FLOW_OK;
248     *buf = gst_buffer_create_sub (src->buffer, offset, size);
249     gst_buffer_set_caps (*buf, GST_BUFFER_CAPS (src->buffer));
250   }
251
252   GST_OBJECT_UNLOCK (src);
253
254   return ret;
255
256 /* ERRORS */
257 no_buffer:
258   {
259     GST_OBJECT_UNLOCK (src);
260     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), (NULL));
261     return GST_FLOW_NOT_NEGOTIATED;
262   }
263 }
264
265 static gboolean
266 gst_data_uri_src_check_get_range (GstBaseSrc * basesrc)
267 {
268   return TRUE;
269 }
270
271 static gboolean
272 gst_data_uri_src_start (GstBaseSrc * basesrc)
273 {
274   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
275
276   GST_OBJECT_LOCK (src);
277
278   if (src->uri == NULL || *src->uri == '\0' || src->buffer == NULL)
279     goto no_uri;
280
281   GST_OBJECT_UNLOCK (src);
282
283   return TRUE;
284
285 /* ERRORS */
286 no_uri:
287   {
288     GST_OBJECT_UNLOCK (src);
289     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
290         ("No valid data URI specified, or the data URI could not be parsed."),
291         ("%s", src->uri));
292     return FALSE;
293   }
294 }
295
296 static void
297 gst_data_uri_src_handler_init (gpointer g_iface, gpointer iface_data)
298 {
299   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
300
301   iface->get_type = gst_data_uri_src_get_uri_type;
302   iface->get_protocols = gst_data_uri_src_get_protocols;
303   iface->get_uri = gst_data_uri_src_get_uri;
304   iface->set_uri = gst_data_uri_src_set_uri;
305 }
306
307 static GstURIType
308 gst_data_uri_src_get_uri_type (void)
309 {
310   return GST_URI_SRC;
311 }
312
313 static gchar **
314 gst_data_uri_src_get_protocols (void)
315 {
316   static gchar *protocols[] = { (char *) "data", 0 };
317
318   return protocols;
319 }
320
321 static const gchar *
322 gst_data_uri_src_get_uri (GstURIHandler * handler)
323 {
324   GstDataURISrc *src = GST_DATA_URI_SRC (handler);
325
326   return src->uri;
327 }
328
329 static gboolean
330 gst_data_uri_src_set_uri (GstURIHandler * handler, const gchar * uri)
331 {
332   GstDataURISrc *src = GST_DATA_URI_SRC (handler);
333   gboolean ret = FALSE;
334   gchar *mimetype = NULL;
335   const gchar *parameters_start;
336   const gchar *data_start;
337   const gchar *orig_uri = uri;
338   GstCaps *caps;
339   gboolean base64 = FALSE;
340   gchar *charset = NULL;
341
342   GST_OBJECT_LOCK (src);
343   if (GST_STATE (src) >= GST_STATE_PAUSED)
344     goto wrong_state;
345
346   /* uri must be an URI as defined in RFC 2397
347    * data:[<mediatype>][;base64],<data>
348    */
349   if (strncmp ("data:", uri, 5) != 0)
350     goto invalid_uri;
351
352   uri += 5;
353
354   parameters_start = strchr (uri, ';');
355   data_start = strchr (uri, ',');
356   if (data_start == NULL)
357     goto invalid_uri;
358
359   if (data_start != uri && parameters_start != uri)
360     mimetype =
361         g_strndup (uri,
362         (parameters_start ? parameters_start : data_start) - uri);
363   else
364     mimetype = g_strdup ("text/plain");
365
366   GST_DEBUG_OBJECT (src, "Mimetype: %s", mimetype);
367
368   if (parameters_start != NULL) {
369     gchar **walk;
370     gchar *parameters =
371         g_strndup (parameters_start + 1, data_start - parameters_start - 1);
372     gchar **parameters_strv;
373
374     parameters_strv = g_strsplit (parameters, ";", -1);
375
376     GST_DEBUG_OBJECT (src, "Parameters: ");
377     walk = parameters_strv;
378     while (*walk) {
379       GST_DEBUG_OBJECT (src, "\t %s", *walk);
380       if (strcmp ("base64", *walk) == 0) {
381         base64 = TRUE;
382       } else if (strncmp ("charset=", *walk, 8) == 0) {
383         charset = g_strdup (*walk + 8);
384       }
385       walk++;
386     }
387     g_free (parameters);
388     g_strfreev (parameters_strv);
389   }
390
391   /* Skip comma */
392   data_start += 1;
393   if (base64) {
394     gsize bsize;
395
396     src->buffer = gst_buffer_new ();
397     GST_BUFFER_DATA (src->buffer) =
398         (guint8 *) g_base64_decode (data_start, &bsize);
399     GST_BUFFER_MALLOCDATA (src->buffer) = GST_BUFFER_DATA (src->buffer);
400     GST_BUFFER_SIZE (src->buffer) = bsize;
401   } else {
402     gchar *data;
403
404     /* URI encoded, i.e. "percent" encoding */
405     data = g_uri_unescape_string (data_start, NULL);
406     if (data == NULL)
407       goto invalid_uri_encoded_data;
408
409     src->buffer = gst_buffer_new ();
410     GST_BUFFER_DATA (src->buffer) = (guint8 *) data;
411     GST_BUFFER_MALLOCDATA (src->buffer) = GST_BUFFER_DATA (src->buffer);
412     GST_BUFFER_SIZE (src->buffer) = strlen (data) + 1;
413   }
414
415   /* Convert to UTF8 */
416   if (strcmp ("text/plain", mimetype) == 0 &&
417       charset && g_ascii_strcasecmp ("US-ASCII", charset) != 0
418       && g_ascii_strcasecmp ("UTF-8", charset) != 0) {
419     gsize read;
420     gsize written;
421     gchar *old_data = (gchar *) GST_BUFFER_DATA (src->buffer);
422     gchar *data;
423
424     data =
425         g_convert_with_fallback (old_data, -1, "UTF-8", charset, (char *) "*",
426         &read, &written, NULL);
427     g_free (old_data);
428     GST_BUFFER_DATA (src->buffer) = GST_BUFFER_MALLOCDATA (src->buffer) =
429         (guint8 *) data;
430     GST_BUFFER_SIZE (src->buffer) = written;
431   }
432
433   caps = gst_type_find_helper_for_buffer (GST_OBJECT (src), src->buffer, NULL);
434   if (!caps)
435     caps = gst_caps_new_simple (mimetype, NULL);
436   gst_buffer_set_caps (src->buffer, caps);
437   gst_caps_unref (caps);
438
439   g_free (src->uri);
440   src->uri = g_strdup (orig_uri);
441
442   ret = TRUE;
443
444 out:
445
446   GST_OBJECT_UNLOCK (src);
447
448   g_free (mimetype);
449   g_free (charset);
450
451   return ret;
452
453 invalid_uri:
454   {
455     GST_WARNING_OBJECT (src, "invalid URI '%s'", uri);
456     goto out;
457   }
458 wrong_state:
459   {
460     GST_WARNING_OBJECT (src, "Can't set URI in %s state",
461         gst_element_state_get_name (GST_STATE (src)));
462     goto out;
463   }
464 invalid_uri_encoded_data:
465   {
466     GST_WARNING_OBJECT (src, "Failed to parse data encoded in URI '%s'", uri);
467     goto out;
468   }
469 }
470
471 static gboolean
472 plugin_init (GstPlugin * plugin)
473 {
474   return gst_element_register (plugin, "dataurisrc",
475       GST_RANK_PRIMARY, GST_TYPE_DATA_URI_SRC);
476 }
477
478 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
479     GST_VERSION_MINOR,
480     "dataurisrc",
481     "data: URI source",
482     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);