multifile: error out if no filename was set
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / multifile / gstmultifilesrc.c
1 /* GStreamer
2  * Copyright (C) 2006 David A. Schleef <ds@schleef.org>
3  *
4  * gstmultifilesrc.c:
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 /**
22  * SECTION:element-multifilesrc
23  * @title: multifilesrc
24  * @see_also: #GstFileSrc
25  *
26  * Reads buffers from sequentially named files. If used together with an image
27  * decoder, one needs to use the #GstMultiFileSrc:caps property or a capsfilter
28  * to force to caps containing a framerate. Otherwise image decoders send EOS
29  * after the first picture. We also need a videorate element to set timestamps
30  * on all buffers after the first one in accordance with the framerate.
31  *
32  * File names are created by replacing "\%d" with the index using `printf()`.
33  *
34  * ## Example launch line
35  * |[
36  * gst-launch-1.0 multifilesrc location="img.%04d.png" index=0 caps="image/png,framerate=\(fraction\)12/1" ! \
37  *     pngdec ! videoconvert ! videorate ! theoraenc ! oggmux ! \
38  *     filesink location="images.ogg"
39  * ]| This pipeline creates a video file "images.ogg" by joining multiple PNG
40  * files named img.0000.png, img.0001.png, etc.
41  *
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #  include "config.h"
46 #endif
47
48 #include "gstmultifilesrc.h"
49 #include <glib/gi18n-lib.h>
50
51 static GstFlowReturn gst_multi_file_src_create (GstPushSrc * src,
52     GstBuffer ** buffer);
53
54 static void gst_multi_file_src_dispose (GObject * object);
55
56 static void gst_multi_file_src_set_property (GObject * object, guint prop_id,
57     const GValue * value, GParamSpec * pspec);
58 static void gst_multi_file_src_get_property (GObject * object, guint prop_id,
59     GValue * value, GParamSpec * pspec);
60 static GstCaps *gst_multi_file_src_getcaps (GstBaseSrc * src, GstCaps * filter);
61 static gboolean gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query);
62 static void gst_multi_file_src_uri_handler_init (gpointer g_iface,
63     gpointer iface_data);
64
65
66 static GstStaticPadTemplate gst_multi_file_src_pad_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
68     GST_PAD_SRC,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS_ANY);
71
72 GST_DEBUG_CATEGORY_STATIC (gst_multi_file_src_debug);
73 #define GST_CAT_DEFAULT gst_multi_file_src_debug
74
75 enum
76 {
77   PROP_0,
78   PROP_LOCATION,
79   PROP_INDEX,
80   PROP_START_INDEX,
81   PROP_STOP_INDEX,
82   PROP_CAPS,
83   PROP_LOOP
84 };
85
86 #define DEFAULT_LOCATION "%05d"
87 #define DEFAULT_INDEX 0
88
89 #define gst_multi_file_src_parent_class parent_class
90 G_DEFINE_TYPE_WITH_CODE (GstMultiFileSrc, gst_multi_file_src, GST_TYPE_PUSH_SRC,
91     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
92         gst_multi_file_src_uri_handler_init));
93 GST_ELEMENT_REGISTER_DEFINE (multifilesrc, "multifilesrc", GST_RANK_NONE,
94     gst_multi_file_src_get_type ());
95
96 static gboolean
97 is_seekable (GstBaseSrc * src)
98 {
99   GstMultiFileSrc *mfs = GST_MULTI_FILE_SRC (src);
100
101   if (mfs->fps_n != -1)
102     return TRUE;
103
104   return FALSE;
105 }
106
107 static gboolean
108 do_seek (GstBaseSrc * bsrc, GstSegment * segment)
109 {
110   gboolean reverse;
111   GstClockTime position;
112   GstMultiFileSrc *src;
113
114   src = GST_MULTI_FILE_SRC (bsrc);
115
116   segment->time = segment->start;
117   position = segment->position;
118   reverse = segment->rate < 0;
119
120   if (reverse) {
121     GST_FIXME_OBJECT (src, "Handle reverse playback");
122
123     return FALSE;
124   }
125
126   /* now move to the position indicated */
127   if (src->fps_n) {
128     src->index = gst_util_uint64_scale (position,
129         src->fps_n, src->fps_d * GST_SECOND);
130   } else {
131     src->index = 0;
132     GST_WARNING_OBJECT (src, "No FPS set, can not seek");
133
134     return FALSE;
135   }
136
137   return TRUE;
138 }
139
140 static void
141 gst_multi_file_src_class_init (GstMultiFileSrcClass * klass)
142 {
143   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
145   GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
146   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
147
148   gobject_class->set_property = gst_multi_file_src_set_property;
149   gobject_class->get_property = gst_multi_file_src_get_property;
150
151   g_object_class_install_property (gobject_class, PROP_LOCATION,
152       g_param_spec_string ("location", "File Location",
153           "Pattern to create file names of input files.  File names are "
154           "created by calling sprintf() with the pattern and the current "
155           "index.", DEFAULT_LOCATION,
156           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157   g_object_class_install_property (gobject_class, PROP_INDEX,
158       g_param_spec_int ("index", "File Index",
159           "Index to use with location property to create file names.  The "
160           "index is incremented by one for each buffer read.",
161           0, INT_MAX, DEFAULT_INDEX,
162           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163   g_object_class_install_property (gobject_class, PROP_START_INDEX,
164       g_param_spec_int ("start-index", "Start Index",
165           "Start value of index.  The initial value of index can be set "
166           "either by setting index or start-index.  When the end of the loop "
167           "is reached, the index will be set to the value start-index.",
168           0, INT_MAX, DEFAULT_INDEX,
169           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170   g_object_class_install_property (gobject_class, PROP_STOP_INDEX,
171       g_param_spec_int ("stop-index", "Stop Index",
172           "Stop value of index.  The special value -1 means no stop.",
173           -1, INT_MAX, DEFAULT_INDEX,
174           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175   g_object_class_install_property (gobject_class, PROP_CAPS,
176       g_param_spec_boxed ("caps", "Caps",
177           "Caps describing the format of the data.",
178           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179   g_object_class_install_property (gobject_class, PROP_LOOP,
180       g_param_spec_boolean ("loop", "Loop",
181           "Whether to repeat from the beginning when all files have been read.",
182           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183
184   gobject_class->dispose = gst_multi_file_src_dispose;
185
186   gstbasesrc_class->get_caps = gst_multi_file_src_getcaps;
187   gstbasesrc_class->query = gst_multi_file_src_query;
188   gstbasesrc_class->is_seekable = is_seekable;
189   gstbasesrc_class->do_seek = do_seek;
190
191   gstpushsrc_class->create = gst_multi_file_src_create;
192
193   GST_DEBUG_CATEGORY_INIT (gst_multi_file_src_debug, "multifilesrc", 0,
194       "multifilesrc element");
195
196   gst_element_class_add_static_pad_template (gstelement_class,
197       &gst_multi_file_src_pad_template);
198   gst_element_class_set_static_metadata (gstelement_class, "Multi-File Source",
199       "Source/File", "Read a sequentially named set of files into buffers",
200       "David Schleef <ds@schleef.org>");
201 }
202
203 static void
204 gst_multi_file_src_init (GstMultiFileSrc * multifilesrc)
205 {
206   multifilesrc->start_index = DEFAULT_INDEX;
207   multifilesrc->index = DEFAULT_INDEX;
208   multifilesrc->stop_index = -1;
209   multifilesrc->filename = g_strdup (DEFAULT_LOCATION);
210   multifilesrc->successful_read = FALSE;
211   multifilesrc->fps_n = multifilesrc->fps_d = -1;
212
213 }
214
215 static void
216 gst_multi_file_src_dispose (GObject * object)
217 {
218   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
219
220   g_free (src->filename);
221   src->filename = NULL;
222   if (src->caps)
223     gst_caps_unref (src->caps);
224
225   G_OBJECT_CLASS (parent_class)->dispose (object);
226 }
227
228 static GstCaps *
229 gst_multi_file_src_getcaps (GstBaseSrc * src, GstCaps * filter)
230 {
231   GstMultiFileSrc *multi_file_src = GST_MULTI_FILE_SRC (src);
232
233   GST_DEBUG_OBJECT (src, "returning %" GST_PTR_FORMAT, multi_file_src->caps);
234
235   if (multi_file_src->caps) {
236     if (filter)
237       return gst_caps_intersect_full (filter, multi_file_src->caps,
238           GST_CAPS_INTERSECT_FIRST);
239     else
240       return gst_caps_ref (multi_file_src->caps);
241   } else {
242     if (filter)
243       return gst_caps_ref (filter);
244     else
245       return gst_caps_new_any ();
246   }
247 }
248
249 static gboolean
250 gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query)
251 {
252   gboolean res;
253   GstMultiFileSrc *mfsrc;
254
255   mfsrc = GST_MULTI_FILE_SRC (src);
256
257   switch (GST_QUERY_TYPE (query)) {
258     case GST_QUERY_POSITION:
259     {
260       GstFormat format;
261
262       gst_query_parse_position (query, &format, NULL);
263       switch (format) {
264         case GST_FORMAT_BUFFERS:
265         case GST_FORMAT_DEFAULT:
266           gst_query_set_position (query, format,
267               mfsrc->index - mfsrc->start_index);
268           res = TRUE;
269           break;
270         default:
271           res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
272           break;
273       }
274       break;
275     }
276     default:
277       res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
278       break;
279   }
280   return res;
281 }
282
283 static gboolean
284 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
285 {
286   g_free (src->filename);
287   if (location != NULL) {
288     src->filename = g_strdup (location);
289   } else {
290     src->filename = NULL;
291   }
292
293   return TRUE;
294 }
295
296 static void
297 gst_multi_file_src_set_property (GObject * object, guint prop_id,
298     const GValue * value, GParamSpec * pspec)
299 {
300   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
301
302   switch (prop_id) {
303     case PROP_LOCATION:
304       gst_multi_file_src_set_location (src, g_value_get_string (value));
305       break;
306     case PROP_INDEX:
307       GST_OBJECT_LOCK (src);
308       /* index was really meant to be read-only, but for backwards-compatibility
309        * we set start_index to make it work as it used to */
310       if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED))
311         src->start_index = g_value_get_int (value);
312       else
313         src->index = g_value_get_int (value);
314       GST_OBJECT_UNLOCK (src);
315       break;
316     case PROP_START_INDEX:
317       src->start_index = g_value_get_int (value);
318       break;
319     case PROP_STOP_INDEX:
320       src->stop_index = g_value_get_int (value);
321       break;
322     case PROP_CAPS:
323     {
324       GstStructure *st = NULL;
325       const GstCaps *caps = gst_value_get_caps (value);
326       GstCaps *new_caps;
327
328       if (caps == NULL) {
329         new_caps = gst_caps_new_any ();
330       } else {
331         new_caps = gst_caps_copy (caps);
332       }
333       gst_caps_replace (&src->caps, new_caps);
334       gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
335
336       if (new_caps && gst_caps_get_size (new_caps) == 1 &&
337           (st = gst_caps_get_structure (new_caps, 0))
338           && gst_structure_get_fraction (st, "framerate", &src->fps_n,
339               &src->fps_d)) {
340         GST_INFO_OBJECT (src, "Setting framerate to %d/%d", src->fps_n,
341             src->fps_d);
342       } else {
343         src->fps_n = -1;
344         src->fps_d = -1;
345       }
346       gst_caps_unref (new_caps);
347     }
348       break;
349     case PROP_LOOP:
350       src->loop = g_value_get_boolean (value);
351       break;
352     default:
353       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354       break;
355   }
356 }
357
358 static void
359 gst_multi_file_src_get_property (GObject * object, guint prop_id,
360     GValue * value, GParamSpec * pspec)
361 {
362   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
363
364   switch (prop_id) {
365     case PROP_LOCATION:
366       g_value_set_string (value, src->filename);
367       break;
368     case PROP_INDEX:
369       g_value_set_int (value, src->index);
370       break;
371     case PROP_START_INDEX:
372       g_value_set_int (value, src->start_index);
373       break;
374     case PROP_STOP_INDEX:
375       g_value_set_int (value, src->stop_index);
376       break;
377     case PROP_CAPS:
378       gst_value_set_caps (value, src->caps);
379       break;
380     case PROP_LOOP:
381       g_value_set_boolean (value, src->loop);
382       break;
383     default:
384       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385       break;
386   }
387 }
388
389 static gchar *
390 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
391 {
392   gchar *filename;
393
394   GST_DEBUG ("%d", multifilesrc->index);
395   if (multifilesrc->filename != NULL) {
396     filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
397   } else {
398     GST_WARNING_OBJECT (multifilesrc, "No filename location set!");
399     filename = NULL;
400   }
401
402   return filename;
403 }
404
405 static GstFlowReturn
406 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
407 {
408   GstMultiFileSrc *multifilesrc;
409   gsize size;
410   gchar *data;
411   gchar *filename;
412   GstBuffer *buf;
413   gboolean ret;
414   GError *error = NULL;
415
416   multifilesrc = GST_MULTI_FILE_SRC (src);
417
418   if (multifilesrc->index < multifilesrc->start_index) {
419     multifilesrc->index = multifilesrc->start_index;
420   }
421
422   if (multifilesrc->stop_index != -1 &&
423       multifilesrc->index > multifilesrc->stop_index) {
424     if (multifilesrc->loop)
425       multifilesrc->index = multifilesrc->start_index;
426     else
427       return GST_FLOW_EOS;
428   }
429
430   filename = gst_multi_file_src_get_filename (multifilesrc);
431
432   if (!filename)
433     goto error_no_filename;
434
435   GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
436
437   ret = g_file_get_contents (filename, &data, &size, &error);
438   if (!ret) {
439     if (multifilesrc->successful_read) {
440       /* If we've read at least one buffer successfully, not finding the
441        * next file is EOS. */
442       g_free (filename);
443       if (error != NULL)
444         g_error_free (error);
445
446       if (multifilesrc->loop) {
447         error = NULL;
448         multifilesrc->index = multifilesrc->start_index;
449
450         filename = gst_multi_file_src_get_filename (multifilesrc);
451         ret = g_file_get_contents (filename, &data, &size, &error);
452         if (!ret) {
453           g_free (filename);
454           if (error != NULL)
455             g_error_free (error);
456
457           return GST_FLOW_EOS;
458         }
459       } else {
460         return GST_FLOW_EOS;
461       }
462     } else {
463       goto handle_error;
464     }
465   }
466
467   multifilesrc->successful_read = TRUE;
468   multifilesrc->index++;
469
470   buf = gst_buffer_new ();
471   gst_buffer_append_memory (buf,
472       gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
473   GST_BUFFER_OFFSET (buf) = multifilesrc->offset;
474   GST_BUFFER_OFFSET_END (buf) = multifilesrc->offset + size;
475   multifilesrc->offset += size;
476
477   GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
478
479   g_free (filename);
480   *buffer = buf;
481   return GST_FLOW_OK;
482
483 error_no_filename:
484   {
485     GST_ELEMENT_ERROR (multifilesrc, RESOURCE, NOT_FOUND,
486         (_("No file name specified for reading.")), (NULL));
487     return GST_FLOW_ERROR;
488   }
489 handle_error:
490   {
491     if (error != NULL) {
492       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
493           ("Error while reading from file \"%s\".", filename),
494           ("%s", error->message));
495       g_error_free (error);
496     } else {
497       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
498           ("Error while reading from file \"%s\".", filename),
499           ("%s", g_strerror (errno)));
500     }
501     g_free (filename);
502     return GST_FLOW_ERROR;
503   }
504 }
505
506 static GstURIType
507 gst_multi_file_src_uri_get_type (GType type)
508 {
509   return GST_URI_SRC;
510 }
511
512 static const gchar *const *
513 gst_multi_file_src_uri_get_protocols (GType type)
514 {
515   static const gchar *protocols[] = { "multifile", NULL };
516
517   return (const gchar * const *) protocols;
518 }
519
520 static gchar *
521 gst_multi_file_src_uri_get_uri (GstURIHandler * handler)
522 {
523   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (handler);
524   gchar *ret;
525
526   GST_OBJECT_LOCK (src);
527   if (src->filename != NULL) {
528     GstUri *uri = gst_uri_new ("multifle", NULL, NULL, GST_URI_NO_PORT,
529         src->filename, NULL, NULL);
530
531     ret = gst_uri_to_string (uri);
532     gst_uri_unref (uri);
533   } else {
534     ret = NULL;
535   }
536   GST_OBJECT_UNLOCK (src);
537
538   return ret;
539 }
540
541 static gboolean
542 gst_multi_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
543     GError ** error)
544 {
545   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (handler);
546   GstUri *gsturi;
547   gchar *path;
548
549   gsturi = gst_uri_from_string (uri);
550   if (gsturi == NULL)
551     goto invalid_uri;
552
553   /* This should get us the unescaped path */
554   path = gst_uri_get_path (gsturi);
555   if (path == NULL)
556     goto invalid_uri;
557
558   GST_OBJECT_LOCK (src);
559   gst_multi_file_src_set_location (src, path);
560   GST_OBJECT_UNLOCK (src);
561
562   g_free (path);
563   gst_uri_unref (gsturi);
564
565   return TRUE;
566
567 /* ERRORS */
568 invalid_uri:
569   {
570     GST_WARNING_OBJECT (src, "Invalid multifile URI '%s'", uri);
571     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
572         "Invalid multifile URI");
573     if (gsturi)
574       gst_uri_unref (gsturi);
575     return FALSE;
576   }
577 }
578
579 static void
580 gst_multi_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
581 {
582   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
583
584   iface->get_type = gst_multi_file_src_uri_get_type;
585   iface->get_protocols = gst_multi_file_src_uri_get_protocols;
586   iface->get_uri = gst_multi_file_src_uri_get_uri;
587   iface->set_uri = gst_multi_file_src_uri_set_uri;
588 }