Add -Wwrite-strings
[platform/upstream/gstreamer.git] / plugins / elements / 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_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>");
107
108 }
109
110 static void
111 gst_data_uri_src_class_init (GstDataURISrcClass * klass)
112 {
113   GObjectClass *gobject_class = (GObjectClass *) klass;
114   GstBaseSrcClass *basesrc_class = (GstBaseSrcClass *) klass;
115
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;
119
120   g_object_class_install_property (gobject_class, PROP_URI,
121       g_param_spec_string ("uri",
122           "URI",
123           "URI that should be used",
124           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125
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);
133 }
134
135 static void
136 gst_data_uri_src_init (GstDataURISrc * src, GstDataURISrcClass * g_class)
137 {
138   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_BYTES);
139 }
140
141 static void
142 gst_data_uri_src_finalize (GObject * object)
143 {
144   GstDataURISrc *src = GST_DATA_URI_SRC (object);
145
146   g_free (src->uri);
147   src->uri = NULL;
148
149   if (src->buffer)
150     gst_buffer_unref (src->buffer);
151   src->buffer = NULL;
152
153   G_OBJECT_CLASS (parent_class)->finalize (object);
154 }
155
156 static void
157 gst_data_uri_src_set_property (GObject * object, guint prop_id,
158     const GValue * value, GParamSpec * pspec)
159 {
160   GstDataURISrc *src = GST_DATA_URI_SRC (object);
161
162   switch (prop_id) {
163     case PROP_URI:
164       gst_data_uri_src_set_uri (GST_URI_HANDLER (src),
165           g_value_get_string (value));
166       break;
167     default:
168       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169       break;
170   }
171 }
172
173 static void
174 gst_data_uri_src_get_property (GObject * object,
175     guint prop_id, GValue * value, GParamSpec * pspec)
176 {
177   GstDataURISrc *src = GST_DATA_URI_SRC (object);
178
179   switch (prop_id) {
180     case PROP_URI:
181       g_value_set_string (value,
182           gst_data_uri_src_get_uri (GST_URI_HANDLER (src)));
183       break;
184     default:
185       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186       break;
187   }
188 }
189
190 static GstCaps *
191 gst_data_uri_src_get_caps (GstBaseSrc * basesrc)
192 {
193   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
194   GstCaps *caps;
195
196   GST_OBJECT_LOCK (src);
197   if (!src->buffer || !GST_BUFFER_CAPS (src->buffer))
198     caps = gst_caps_new_empty ();
199   else
200     caps = gst_buffer_get_caps (src->buffer);
201   GST_OBJECT_UNLOCK (src);
202
203   return caps;
204 }
205
206 static gboolean
207 gst_data_uri_src_get_size (GstBaseSrc * basesrc, guint64 * size)
208 {
209   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
210   gboolean ret;
211
212   GST_OBJECT_LOCK (src);
213   if (!src->buffer) {
214     ret = FALSE;
215     *size = -1;
216   } else {
217     ret = TRUE;
218     *size = GST_BUFFER_SIZE (src->buffer);
219   }
220   GST_OBJECT_UNLOCK (src);
221
222   return ret;
223 }
224
225 static gboolean
226 gst_data_uri_src_is_seekable (GstBaseSrc * basesrc)
227 {
228   return TRUE;
229 }
230
231 static GstFlowReturn
232 gst_data_uri_src_create (GstBaseSrc * basesrc, guint64 offset, guint size,
233     GstBuffer ** buf)
234 {
235   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
236   GstFlowReturn ret;
237
238   GST_OBJECT_LOCK (src);
239
240   if (!src->buffer)
241     goto no_buffer;
242
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;
247   } else {
248     ret = GST_FLOW_OK;
249     *buf = gst_buffer_create_sub (src->buffer, offset, size);
250     gst_buffer_set_caps (*buf, GST_BUFFER_CAPS (src->buffer));
251   }
252
253   GST_OBJECT_UNLOCK (src);
254
255   return ret;
256
257 /* ERRORS */
258 no_buffer:
259   {
260     GST_OBJECT_UNLOCK (src);
261     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), (NULL));
262     return GST_FLOW_NOT_NEGOTIATED;
263   }
264 }
265
266 static gboolean
267 gst_data_uri_src_check_get_range (GstBaseSrc * basesrc)
268 {
269   return TRUE;
270 }
271
272 static gboolean
273 gst_data_uri_src_start (GstBaseSrc * basesrc)
274 {
275   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
276
277   GST_OBJECT_LOCK (src);
278
279   if (src->uri == NULL || *src->uri == '\0' || src->buffer == NULL)
280     goto no_uri;
281
282   GST_OBJECT_UNLOCK (src);
283
284   return TRUE;
285
286 /* ERRORS */
287 no_uri:
288   {
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."),
292         ("%s", src->uri));
293     return FALSE;
294   }
295 }
296
297 static void
298 gst_data_uri_src_handler_init (gpointer g_iface, gpointer iface_data)
299 {
300   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
301
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;
306 }
307
308 static GstURIType
309 gst_data_uri_src_get_uri_type (void)
310 {
311   return GST_URI_SRC;
312 }
313
314 static gchar **
315 gst_data_uri_src_get_protocols (void)
316 {
317   static gchar *protocols[] = { (char *) "data", 0 };
318
319   return protocols;
320 }
321
322 static const gchar *
323 gst_data_uri_src_get_uri (GstURIHandler * handler)
324 {
325   GstDataURISrc *src = GST_DATA_URI_SRC (handler);
326
327   return src->uri;
328 }
329
330 static gboolean
331 gst_data_uri_src_set_uri (GstURIHandler * handler, const gchar * uri)
332 {
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;
339   GstCaps *caps;
340   gboolean base64 = FALSE;
341   gchar *charset = NULL;
342
343   GST_OBJECT_LOCK (src);
344   if (GST_STATE (src) >= GST_STATE_PAUSED)
345     goto wrong_state;
346
347   /* uri must be an URI as defined in RFC 2397
348    * data:[<mediatype>][;base64],<data>
349    */
350   if (strncmp ("data:", uri, 5) != 0)
351     goto invalid_uri;
352
353   uri += 5;
354
355   parameters_start = strchr (uri, ';');
356   data_start = strchr (uri, ',');
357   if (data_start == NULL)
358     goto invalid_uri;
359
360   if (data_start != uri && parameters_start != uri)
361     mimetype =
362         g_strndup (uri,
363         (parameters_start ? parameters_start : data_start) - uri);
364   else
365     mimetype = g_strdup ("text/plain");
366
367   GST_DEBUG_OBJECT (src, "Mimetype: %s", mimetype);
368
369   if (parameters_start != NULL) {
370     gchar **walk;
371     gchar *parameters =
372         g_strndup (parameters_start + 1, data_start - parameters_start - 1);
373     gchar **parameters_strv;
374
375     parameters_strv = g_strsplit (parameters, ";", -1);
376
377     GST_DEBUG_OBJECT (src, "Parameters: ");
378     walk = parameters_strv;
379     while (*walk) {
380       GST_DEBUG_OBJECT (src, "\t %s", *walk);
381       if (strcmp ("base64", *walk) == 0) {
382         base64 = TRUE;
383       } else if (strncmp ("charset=", *walk, 8) == 0) {
384         charset = g_strdup (*walk + 8);
385       }
386       walk++;
387     }
388     g_free (parameters);
389     g_strfreev (parameters_strv);
390   }
391
392   /* Skip comma */
393   data_start += 1;
394   if (base64) {
395     gsize bsize;
396
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;
402   } else {
403     gchar *data;
404
405     /* URI encoded, i.e. "percent" encoding */
406     data = g_uri_unescape_string (data_start, NULL);
407     if (data == NULL)
408       goto invalid_uri_encoded_data;
409
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;
414   }
415
416   /* Convert to UTF8 */
417   if (strcmp ("text/plain", mimetype) == 0 &&
418       charset && strcasecmp ("US-ASCII", charset) != 0
419       && strcasecmp ("UTF-8", charset) != 0) {
420     gsize read;
421     gsize written;
422     gchar *old_data = (gchar *) GST_BUFFER_DATA (src->buffer);
423     gchar *data;
424
425     data =
426         g_convert_with_fallback (old_data, -1, "UTF-8", charset, (char *) "*",
427         &read, &written, NULL);
428     g_free (old_data);
429     GST_BUFFER_DATA (src->buffer) = GST_BUFFER_MALLOCDATA (src->buffer) =
430         (guint8 *) data;
431     GST_BUFFER_SIZE (src->buffer) = written;
432   }
433
434   caps = gst_type_find_helper_for_buffer (GST_OBJECT (src), src->buffer, NULL);
435   if (!caps)
436     caps = gst_caps_new_simple (mimetype, NULL);
437   gst_buffer_set_caps (src->buffer, caps);
438   gst_caps_unref (caps);
439
440   g_free (src->uri);
441   src->uri = g_strdup (orig_uri);
442
443   ret = TRUE;
444
445 out:
446
447   GST_OBJECT_UNLOCK (src);
448
449   g_free (mimetype);
450   g_free (charset);
451
452   return ret;
453
454 invalid_uri:
455   {
456     GST_WARNING_OBJECT (src, "invalid URI '%s'", uri);
457     goto out;
458   }
459 wrong_state:
460   {
461     GST_WARNING_OBJECT (src, "Can't set URI in %s state",
462         gst_element_state_get_name (GST_STATE (src)));
463     goto out;
464   }
465 invalid_uri_encoded_data:
466   {
467     GST_WARNING_OBJECT (src, "Failed to parse data encoded in URI '%s'", uri);
468     goto out;
469   }
470 }
471
472 static gboolean
473 plugin_init (GstPlugin * plugin)
474 {
475   return gst_element_register (plugin, "dataurisrc",
476       GST_RANK_PRIMARY, GST_TYPE_DATA_URI_SRC);
477 }
478
479 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
480     GST_VERSION_MINOR,
481     "dataurisrc",
482     "data: URI source",
483     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);