splitmuxsrc: Connect to demux signals before activating
[platform/upstream/gst-plugins-good.git] / 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  * @see_also: #GstFileSrc
24  *
25  * Reads buffers from sequentially named files. If used together with an image
26  * decoder, one needs to use the #GstMultiFileSrc:caps property or a capsfilter
27  * to force to caps containing a framerate. Otherwise image decoders send EOS
28  * after the first picture. We also need a videorate element to set timestamps
29  * on all buffers after the first one in accordance with the framerate.
30  *
31  * File names are created by replacing "\%d" with the index using printf().
32  *
33  * <refsect2>
34  * <title>Example launch line</title>
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  * </refsect2>
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #  include "config.h"
46 #endif
47
48 #include "gstmultifilesrc.h"
49
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
63
64 static GstStaticPadTemplate gst_multi_file_src_pad_template =
65 GST_STATIC_PAD_TEMPLATE ("src",
66     GST_PAD_SRC,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS_ANY);
69
70 GST_DEBUG_CATEGORY_STATIC (gst_multi_file_src_debug);
71 #define GST_CAT_DEFAULT gst_multi_file_src_debug
72
73 enum
74 {
75   PROP_0,
76   PROP_LOCATION,
77   PROP_INDEX,
78   PROP_START_INDEX,
79   PROP_STOP_INDEX,
80   PROP_CAPS,
81   PROP_LOOP
82 };
83
84 #define DEFAULT_LOCATION "%05d"
85 #define DEFAULT_INDEX 0
86
87 #define gst_multi_file_src_parent_class parent_class
88 G_DEFINE_TYPE (GstMultiFileSrc, gst_multi_file_src, GST_TYPE_PUSH_SRC);
89
90
91 static gboolean
92 is_seekable (GstBaseSrc * src)
93 {
94   GstMultiFileSrc *mfs = GST_MULTI_FILE_SRC (src);
95
96   if (mfs->fps_n != -1)
97     return TRUE;
98
99   return FALSE;
100 }
101
102 static gboolean
103 do_seek (GstBaseSrc * bsrc, GstSegment * segment)
104 {
105   gboolean reverse;
106   GstClockTime position;
107   GstMultiFileSrc *src;
108
109   src = GST_MULTI_FILE_SRC (bsrc);
110
111   segment->time = segment->start;
112   position = segment->position;
113   reverse = segment->rate < 0;
114
115   if (reverse) {
116     GST_FIXME_OBJECT (src, "Handle reverse playback");
117
118     return FALSE;
119   }
120
121   /* now move to the position indicated */
122   if (src->fps_n) {
123     src->index = gst_util_uint64_scale (position,
124         src->fps_n, src->fps_d * GST_SECOND);
125   } else {
126     src->index = 0;
127     GST_WARNING_OBJECT (src, "No FPS set, can not seek");
128
129     return FALSE;
130   }
131
132   return TRUE;
133 }
134
135 static void
136 gst_multi_file_src_class_init (GstMultiFileSrcClass * klass)
137 {
138   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
140   GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
141   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
142
143   gobject_class->set_property = gst_multi_file_src_set_property;
144   gobject_class->get_property = gst_multi_file_src_get_property;
145
146   g_object_class_install_property (gobject_class, PROP_LOCATION,
147       g_param_spec_string ("location", "File Location",
148           "Pattern to create file names of input files.  File names are "
149           "created by calling sprintf() with the pattern and the current "
150           "index.", DEFAULT_LOCATION,
151           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152   g_object_class_install_property (gobject_class, PROP_INDEX,
153       g_param_spec_int ("index", "File Index",
154           "Index to use with location property to create file names.  The "
155           "index is incremented by one for each buffer read.",
156           0, INT_MAX, DEFAULT_INDEX,
157           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158   g_object_class_install_property (gobject_class, PROP_START_INDEX,
159       g_param_spec_int ("start-index", "Start Index",
160           "Start value of index.  The initial value of index can be set "
161           "either by setting index or start-index.  When the end of the loop "
162           "is reached, the index will be set to the value start-index.",
163           0, INT_MAX, DEFAULT_INDEX,
164           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165   g_object_class_install_property (gobject_class, PROP_STOP_INDEX,
166       g_param_spec_int ("stop-index", "Stop Index",
167           "Stop value of index.  The special value -1 means no stop.",
168           -1, INT_MAX, DEFAULT_INDEX,
169           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170   g_object_class_install_property (gobject_class, PROP_CAPS,
171       g_param_spec_boxed ("caps", "Caps",
172           "Caps describing the format of the data.",
173           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174   g_object_class_install_property (gobject_class, PROP_LOOP,
175       g_param_spec_boolean ("loop", "Loop",
176           "Whether to repeat from the beginning when all files have been read.",
177           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178
179   gobject_class->dispose = gst_multi_file_src_dispose;
180
181   gstbasesrc_class->get_caps = gst_multi_file_src_getcaps;
182   gstbasesrc_class->query = gst_multi_file_src_query;
183   gstbasesrc_class->is_seekable = is_seekable;
184   gstbasesrc_class->do_seek = do_seek;
185
186   gstpushsrc_class->create = gst_multi_file_src_create;
187
188   GST_DEBUG_CATEGORY_INIT (gst_multi_file_src_debug, "multifilesrc", 0,
189       "multifilesrc element");
190
191   gst_element_class_add_static_pad_template (gstelement_class,
192       &gst_multi_file_src_pad_template);
193   gst_element_class_set_static_metadata (gstelement_class, "Multi-File Source",
194       "Source/File", "Read a sequentially named set of files into buffers",
195       "David Schleef <ds@schleef.org>");
196 }
197
198 static void
199 gst_multi_file_src_init (GstMultiFileSrc * multifilesrc)
200 {
201   multifilesrc->start_index = DEFAULT_INDEX;
202   multifilesrc->index = DEFAULT_INDEX;
203   multifilesrc->stop_index = -1;
204   multifilesrc->filename = g_strdup (DEFAULT_LOCATION);
205   multifilesrc->successful_read = FALSE;
206   multifilesrc->fps_n = multifilesrc->fps_d = -1;
207
208 }
209
210 static void
211 gst_multi_file_src_dispose (GObject * object)
212 {
213   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
214
215   g_free (src->filename);
216   src->filename = NULL;
217   if (src->caps)
218     gst_caps_unref (src->caps);
219
220   G_OBJECT_CLASS (parent_class)->dispose (object);
221 }
222
223 static GstCaps *
224 gst_multi_file_src_getcaps (GstBaseSrc * src, GstCaps * filter)
225 {
226   GstMultiFileSrc *multi_file_src = GST_MULTI_FILE_SRC (src);
227
228   GST_DEBUG_OBJECT (src, "returning %" GST_PTR_FORMAT, multi_file_src->caps);
229
230   if (multi_file_src->caps) {
231     if (filter)
232       return gst_caps_intersect_full (filter, multi_file_src->caps,
233           GST_CAPS_INTERSECT_FIRST);
234     else
235       return gst_caps_ref (multi_file_src->caps);
236   } else {
237     if (filter)
238       return gst_caps_ref (filter);
239     else
240       return gst_caps_new_any ();
241   }
242 }
243
244 static gboolean
245 gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query)
246 {
247   gboolean res;
248   GstMultiFileSrc *mfsrc;
249
250   mfsrc = GST_MULTI_FILE_SRC (src);
251
252   switch (GST_QUERY_TYPE (query)) {
253     case GST_QUERY_POSITION:
254     {
255       GstFormat format;
256
257       gst_query_parse_position (query, &format, NULL);
258       switch (format) {
259         case GST_FORMAT_BUFFERS:
260         case GST_FORMAT_DEFAULT:
261           gst_query_set_position (query, GST_FORMAT_BUFFERS, mfsrc->index);
262           res = TRUE;
263           break;
264         default:
265           res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
266           break;
267       }
268       break;
269     }
270     default:
271       res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
272       break;
273   }
274   return res;
275 }
276
277 static gboolean
278 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
279 {
280   g_free (src->filename);
281   if (location != NULL) {
282     src->filename = g_strdup (location);
283   } else {
284     src->filename = NULL;
285   }
286
287   return TRUE;
288 }
289
290 static void
291 gst_multi_file_src_set_property (GObject * object, guint prop_id,
292     const GValue * value, GParamSpec * pspec)
293 {
294   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
295
296   switch (prop_id) {
297     case PROP_LOCATION:
298       gst_multi_file_src_set_location (src, g_value_get_string (value));
299       break;
300     case PROP_INDEX:
301       GST_OBJECT_LOCK (src);
302       /* index was really meant to be read-only, but for backwards-compatibility
303        * we set start_index to make it work as it used to */
304       if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED))
305         src->start_index = g_value_get_int (value);
306       else
307         src->index = g_value_get_int (value);
308       GST_OBJECT_UNLOCK (src);
309       break;
310     case PROP_START_INDEX:
311       src->start_index = g_value_get_int (value);
312       break;
313     case PROP_STOP_INDEX:
314       src->stop_index = g_value_get_int (value);
315       break;
316     case PROP_CAPS:
317     {
318       GstStructure *st = NULL;
319       const GstCaps *caps = gst_value_get_caps (value);
320       GstCaps *new_caps;
321
322       if (caps == NULL) {
323         new_caps = gst_caps_new_any ();
324       } else {
325         new_caps = gst_caps_copy (caps);
326       }
327       gst_caps_replace (&src->caps, new_caps);
328       gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
329
330       if (new_caps && gst_caps_get_size (new_caps) == 1 &&
331           (st = gst_caps_get_structure (new_caps, 0))
332           && gst_structure_get_fraction (st, "framerate", &src->fps_n,
333               &src->fps_d)) {
334         GST_INFO_OBJECT (src, "Seting framerate to %d/%d", src->fps_n,
335             src->fps_d);
336       } else {
337         src->fps_n = -1;
338         src->fps_d = -1;
339       }
340     }
341       break;
342     case PROP_LOOP:
343       src->loop = g_value_get_boolean (value);
344       break;
345     default:
346       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347       break;
348   }
349 }
350
351 static void
352 gst_multi_file_src_get_property (GObject * object, guint prop_id,
353     GValue * value, GParamSpec * pspec)
354 {
355   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
356
357   switch (prop_id) {
358     case PROP_LOCATION:
359       g_value_set_string (value, src->filename);
360       break;
361     case PROP_INDEX:
362       g_value_set_int (value, src->index);
363       break;
364     case PROP_START_INDEX:
365       g_value_set_int (value, src->start_index);
366       break;
367     case PROP_STOP_INDEX:
368       g_value_set_int (value, src->stop_index);
369       break;
370     case PROP_CAPS:
371       gst_value_set_caps (value, src->caps);
372       break;
373     case PROP_LOOP:
374       g_value_set_boolean (value, src->loop);
375       break;
376     default:
377       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378       break;
379   }
380 }
381
382 static gchar *
383 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
384 {
385   gchar *filename;
386
387   GST_DEBUG ("%d", multifilesrc->index);
388   filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
389
390   return filename;
391 }
392
393 static GstFlowReturn
394 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
395 {
396   GstMultiFileSrc *multifilesrc;
397   gsize size;
398   gchar *data;
399   gchar *filename;
400   GstBuffer *buf;
401   gboolean ret;
402   GError *error = NULL;
403
404   multifilesrc = GST_MULTI_FILE_SRC (src);
405
406   if (multifilesrc->index < multifilesrc->start_index) {
407     multifilesrc->index = multifilesrc->start_index;
408   }
409
410   if (multifilesrc->stop_index != -1 &&
411       multifilesrc->index > multifilesrc->stop_index) {
412     if (multifilesrc->loop)
413       multifilesrc->index = multifilesrc->start_index;
414     else
415       return GST_FLOW_EOS;
416   }
417
418   filename = gst_multi_file_src_get_filename (multifilesrc);
419
420   GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
421
422   ret = g_file_get_contents (filename, &data, &size, &error);
423   if (!ret) {
424     if (multifilesrc->successful_read) {
425       /* If we've read at least one buffer successfully, not finding the
426        * next file is EOS. */
427       g_free (filename);
428       if (error != NULL)
429         g_error_free (error);
430
431       if (multifilesrc->loop) {
432         error = NULL;
433         multifilesrc->index = multifilesrc->start_index;
434
435         filename = gst_multi_file_src_get_filename (multifilesrc);
436         ret = g_file_get_contents (filename, &data, &size, &error);
437         if (!ret) {
438           g_free (filename);
439           if (error != NULL)
440             g_error_free (error);
441
442           return GST_FLOW_EOS;
443         }
444       } else {
445         return GST_FLOW_EOS;
446       }
447     } else {
448       goto handle_error;
449     }
450   }
451
452   multifilesrc->successful_read = TRUE;
453   multifilesrc->index++;
454
455   buf = gst_buffer_new ();
456   gst_buffer_append_memory (buf,
457       gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
458   GST_BUFFER_OFFSET (buf) = multifilesrc->offset;
459   GST_BUFFER_OFFSET_END (buf) = multifilesrc->offset + size;
460   multifilesrc->offset += size;
461
462   GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
463
464   g_free (filename);
465   *buffer = buf;
466   return GST_FLOW_OK;
467
468 handle_error:
469   {
470     if (error != NULL) {
471       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
472           ("Error while reading from file \"%s\".", filename),
473           ("%s", error->message));
474       g_error_free (error);
475     } else {
476       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
477           ("Error while reading from file \"%s\".", filename),
478           ("%s", g_strerror (errno)));
479     }
480     g_free (filename);
481     return GST_FLOW_ERROR;
482   }
483 }