splitmuxsink: Allow time and bytes to reach their respective thresholds
[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, format,
262               mfsrc->index - mfsrc->start_index);
263           res = TRUE;
264           break;
265         default:
266           res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
267           break;
268       }
269       break;
270     }
271     default:
272       res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
273       break;
274   }
275   return res;
276 }
277
278 static gboolean
279 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
280 {
281   g_free (src->filename);
282   if (location != NULL) {
283     src->filename = g_strdup (location);
284   } else {
285     src->filename = NULL;
286   }
287
288   return TRUE;
289 }
290
291 static void
292 gst_multi_file_src_set_property (GObject * object, guint prop_id,
293     const GValue * value, GParamSpec * pspec)
294 {
295   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
296
297   switch (prop_id) {
298     case PROP_LOCATION:
299       gst_multi_file_src_set_location (src, g_value_get_string (value));
300       break;
301     case PROP_INDEX:
302       GST_OBJECT_LOCK (src);
303       /* index was really meant to be read-only, but for backwards-compatibility
304        * we set start_index to make it work as it used to */
305       if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED))
306         src->start_index = g_value_get_int (value);
307       else
308         src->index = g_value_get_int (value);
309       GST_OBJECT_UNLOCK (src);
310       break;
311     case PROP_START_INDEX:
312       src->start_index = g_value_get_int (value);
313       break;
314     case PROP_STOP_INDEX:
315       src->stop_index = g_value_get_int (value);
316       break;
317     case PROP_CAPS:
318     {
319       GstStructure *st = NULL;
320       const GstCaps *caps = gst_value_get_caps (value);
321       GstCaps *new_caps;
322
323       if (caps == NULL) {
324         new_caps = gst_caps_new_any ();
325       } else {
326         new_caps = gst_caps_copy (caps);
327       }
328       gst_caps_replace (&src->caps, new_caps);
329       gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
330
331       if (new_caps && gst_caps_get_size (new_caps) == 1 &&
332           (st = gst_caps_get_structure (new_caps, 0))
333           && gst_structure_get_fraction (st, "framerate", &src->fps_n,
334               &src->fps_d)) {
335         GST_INFO_OBJECT (src, "Seting framerate to %d/%d", src->fps_n,
336             src->fps_d);
337       } else {
338         src->fps_n = -1;
339         src->fps_d = -1;
340       }
341     }
342       break;
343     case PROP_LOOP:
344       src->loop = g_value_get_boolean (value);
345       break;
346     default:
347       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
348       break;
349   }
350 }
351
352 static void
353 gst_multi_file_src_get_property (GObject * object, guint prop_id,
354     GValue * value, GParamSpec * pspec)
355 {
356   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
357
358   switch (prop_id) {
359     case PROP_LOCATION:
360       g_value_set_string (value, src->filename);
361       break;
362     case PROP_INDEX:
363       g_value_set_int (value, src->index);
364       break;
365     case PROP_START_INDEX:
366       g_value_set_int (value, src->start_index);
367       break;
368     case PROP_STOP_INDEX:
369       g_value_set_int (value, src->stop_index);
370       break;
371     case PROP_CAPS:
372       gst_value_set_caps (value, src->caps);
373       break;
374     case PROP_LOOP:
375       g_value_set_boolean (value, src->loop);
376       break;
377     default:
378       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
379       break;
380   }
381 }
382
383 static gchar *
384 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
385 {
386   gchar *filename;
387
388   GST_DEBUG ("%d", multifilesrc->index);
389   filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
390
391   return filename;
392 }
393
394 static GstFlowReturn
395 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
396 {
397   GstMultiFileSrc *multifilesrc;
398   gsize size;
399   gchar *data;
400   gchar *filename;
401   GstBuffer *buf;
402   gboolean ret;
403   GError *error = NULL;
404
405   multifilesrc = GST_MULTI_FILE_SRC (src);
406
407   if (multifilesrc->index < multifilesrc->start_index) {
408     multifilesrc->index = multifilesrc->start_index;
409   }
410
411   if (multifilesrc->stop_index != -1 &&
412       multifilesrc->index > multifilesrc->stop_index) {
413     if (multifilesrc->loop)
414       multifilesrc->index = multifilesrc->start_index;
415     else
416       return GST_FLOW_EOS;
417   }
418
419   filename = gst_multi_file_src_get_filename (multifilesrc);
420
421   GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
422
423   ret = g_file_get_contents (filename, &data, &size, &error);
424   if (!ret) {
425     if (multifilesrc->successful_read) {
426       /* If we've read at least one buffer successfully, not finding the
427        * next file is EOS. */
428       g_free (filename);
429       if (error != NULL)
430         g_error_free (error);
431
432       if (multifilesrc->loop) {
433         error = NULL;
434         multifilesrc->index = multifilesrc->start_index;
435
436         filename = gst_multi_file_src_get_filename (multifilesrc);
437         ret = g_file_get_contents (filename, &data, &size, &error);
438         if (!ret) {
439           g_free (filename);
440           if (error != NULL)
441             g_error_free (error);
442
443           return GST_FLOW_EOS;
444         }
445       } else {
446         return GST_FLOW_EOS;
447       }
448     } else {
449       goto handle_error;
450     }
451   }
452
453   multifilesrc->successful_read = TRUE;
454   multifilesrc->index++;
455
456   buf = gst_buffer_new ();
457   gst_buffer_append_memory (buf,
458       gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
459   GST_BUFFER_OFFSET (buf) = multifilesrc->offset;
460   GST_BUFFER_OFFSET_END (buf) = multifilesrc->offset + size;
461   multifilesrc->offset += size;
462
463   GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
464
465   g_free (filename);
466   *buffer = buf;
467   return GST_FLOW_OK;
468
469 handle_error:
470   {
471     if (error != NULL) {
472       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
473           ("Error while reading from file \"%s\".", filename),
474           ("%s", error->message));
475       g_error_free (error);
476     } else {
477       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
478           ("Error while reading from file \"%s\".", filename),
479           ("%s", g_strerror (errno)));
480     }
481     g_free (filename);
482     return GST_FLOW_ERROR;
483   }
484 }