Merge branch 'master' into 0.11
[platform/upstream/gstreamer.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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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.
29  *
30  * File names are created by replacing "\%d" with the index using printf().
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch multifilesrc location="img.%04d.png" index=0 caps="image/png,framerate=\(fraction\)12/1" ! \
36  *     pngdec ! ffmpegcolorspace ! theoraenc ! oggmux ! \
37  *     filesink location="images.ogg"
38  * ]| This pipeline creates a video file "images.ogg" by joining multiple PNG
39  * files named img.0000.png, img.0001.png, etc.
40  * </refsect2>
41 */
42
43 #ifdef HAVE_CONFIG_H
44 #  include "config.h"
45 #endif
46
47 #include "gstmultifilesrc.h"
48
49
50 static GstFlowReturn gst_multi_file_src_create (GstPushSrc * src,
51     GstBuffer ** buffer);
52
53 static void gst_multi_file_src_dispose (GObject * object);
54
55 static void gst_multi_file_src_set_property (GObject * object, guint prop_id,
56     const GValue * value, GParamSpec * pspec);
57 static void gst_multi_file_src_get_property (GObject * object, guint prop_id,
58     GValue * value, GParamSpec * pspec);
59 static GstCaps *gst_multi_file_src_getcaps (GstBaseSrc * src, GstCaps * filter);
60 static gboolean gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query);
61
62
63 static GstStaticPadTemplate gst_multi_file_src_pad_template =
64 GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS_ANY);
68
69 GST_DEBUG_CATEGORY_STATIC (gst_multi_file_src_debug);
70 #define GST_CAT_DEFAULT gst_multi_file_src_debug
71
72 enum
73 {
74   ARG_0,
75   ARG_LOCATION,
76   ARG_INDEX,
77   ARG_START_INDEX,
78   ARG_STOP_INDEX,
79   ARG_CAPS,
80   ARG_LOOP
81 };
82
83 #define DEFAULT_LOCATION "%05d"
84 #define DEFAULT_INDEX 0
85
86 #define gst_multi_file_src_parent_class parent_class
87 G_DEFINE_TYPE (GstMultiFileSrc, gst_multi_file_src, GST_TYPE_PUSH_SRC);
88
89
90 static void
91 gst_multi_file_src_class_init (GstMultiFileSrcClass * klass)
92 {
93   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
95   GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
96   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
97
98   gobject_class->set_property = gst_multi_file_src_set_property;
99   gobject_class->get_property = gst_multi_file_src_get_property;
100
101   g_object_class_install_property (gobject_class, ARG_LOCATION,
102       g_param_spec_string ("location", "File Location",
103           "Pattern to create file names of input files.  File names are "
104           "created by calling sprintf() with the pattern and the current "
105           "index.", DEFAULT_LOCATION,
106           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107   g_object_class_install_property (gobject_class, ARG_INDEX,
108       g_param_spec_int ("index", "File Index",
109           "Index to use with location property to create file names.  The "
110           "index is incremented by one for each buffer read.",
111           0, INT_MAX, DEFAULT_INDEX,
112           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
113   g_object_class_install_property (gobject_class, ARG_START_INDEX,
114       g_param_spec_int ("start-index", "Start Index",
115           "Start value of index.  The initial value of index can be set "
116           "either by setting index or start-index.  When the end of the loop "
117           "is reached, the index will be set to the value start-index.",
118           0, INT_MAX, DEFAULT_INDEX,
119           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
120   g_object_class_install_property (gobject_class, ARG_STOP_INDEX,
121       g_param_spec_int ("stop-index", "Start Index",
122           "Stop value of index.  The special value -1 means no stop.",
123           -1, INT_MAX, DEFAULT_INDEX,
124           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125   g_object_class_install_property (gobject_class, ARG_CAPS,
126       g_param_spec_boxed ("caps", "Caps",
127           "Caps describing the format of the data.",
128           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129   g_object_class_install_property (gobject_class, ARG_LOOP,
130       g_param_spec_boolean ("loop", "Loop",
131           "Whether to repeat from the beginning when all files have been read.",
132           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133
134   gobject_class->dispose = gst_multi_file_src_dispose;
135
136   gstbasesrc_class->get_caps = gst_multi_file_src_getcaps;
137   gstbasesrc_class->query = gst_multi_file_src_query;
138
139   gstpushsrc_class->create = gst_multi_file_src_create;
140
141   if (sizeof (off_t) < 8) {
142     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT,
143         sizeof (off_t));
144   }
145
146   GST_DEBUG_CATEGORY_INIT (gst_multi_file_src_debug, "multifilesrc", 0,
147       "multifilesrc element");
148
149   gst_element_class_add_pad_template (gstelement_class,
150       gst_static_pad_template_get (&gst_multi_file_src_pad_template));
151   gst_element_class_set_details_simple (gstelement_class, "Multi-File Source",
152       "Source/File",
153       "Read a sequentially named set of files into buffers",
154       "David Schleef <ds@schleef.org>");
155 }
156
157 static void
158 gst_multi_file_src_init (GstMultiFileSrc * multifilesrc)
159 {
160   multifilesrc->start_index = DEFAULT_INDEX;
161   multifilesrc->index = DEFAULT_INDEX;
162   multifilesrc->stop_index = -1;
163   multifilesrc->filename = g_strdup (DEFAULT_LOCATION);
164   multifilesrc->successful_read = FALSE;
165 }
166
167 static void
168 gst_multi_file_src_dispose (GObject * object)
169 {
170   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
171
172   g_free (src->filename);
173   src->filename = NULL;
174   if (src->caps)
175     gst_caps_unref (src->caps);
176
177   G_OBJECT_CLASS (parent_class)->dispose (object);
178 }
179
180 static GstCaps *
181 gst_multi_file_src_getcaps (GstBaseSrc * src, GstCaps * filter)
182 {
183   GstMultiFileSrc *multi_file_src = GST_MULTI_FILE_SRC (src);
184
185   GST_DEBUG_OBJECT (src, "returning %" GST_PTR_FORMAT, multi_file_src->caps);
186
187   if (multi_file_src->caps) {
188     if (filter)
189       return gst_caps_intersect_full (filter, multi_file_src->caps,
190           GST_CAPS_INTERSECT_FIRST);
191     else
192       return gst_caps_ref (multi_file_src->caps);
193   } else {
194     if (filter)
195       return gst_caps_ref (filter);
196     else
197       return gst_caps_new_any ();
198   }
199 }
200
201 static gboolean
202 gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query)
203 {
204   gboolean res;
205   GstMultiFileSrc *mfsrc;
206
207   mfsrc = GST_MULTI_FILE_SRC (src);
208
209   switch (GST_QUERY_TYPE (query)) {
210     case GST_QUERY_POSITION:
211     {
212       GstFormat format;
213
214       gst_query_parse_position (query, &format, NULL);
215       switch (format) {
216         case GST_FORMAT_BUFFERS:
217         case GST_FORMAT_DEFAULT:
218           gst_query_set_position (query, GST_FORMAT_BUFFERS, mfsrc->index);
219           res = TRUE;
220           break;
221         default:
222           res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
223           break;
224       }
225       break;
226     }
227     default:
228       res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
229       break;
230   }
231   return res;
232 }
233
234 static gboolean
235 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
236 {
237   g_free (src->filename);
238   if (location != NULL) {
239     src->filename = g_strdup (location);
240   } else {
241     src->filename = NULL;
242   }
243
244   return TRUE;
245 }
246
247 static void
248 gst_multi_file_src_set_property (GObject * object, guint prop_id,
249     const GValue * value, GParamSpec * pspec)
250 {
251   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
252
253   switch (prop_id) {
254     case ARG_LOCATION:
255       gst_multi_file_src_set_location (src, g_value_get_string (value));
256       break;
257     case ARG_INDEX:
258       src->index = g_value_get_int (value);
259       break;
260     case ARG_START_INDEX:
261       src->start_index = g_value_get_int (value);
262       break;
263     case ARG_STOP_INDEX:
264       src->stop_index = g_value_get_int (value);
265       break;
266     case ARG_CAPS:
267     {
268       const GstCaps *caps = gst_value_get_caps (value);
269       GstCaps *new_caps;
270
271       if (caps == NULL) {
272         new_caps = gst_caps_new_any ();
273       } else {
274         new_caps = gst_caps_copy (caps);
275       }
276       gst_caps_replace (&src->caps, new_caps);
277       gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
278     }
279       break;
280     case ARG_LOOP:
281       src->loop = g_value_get_boolean (value);
282       break;
283     default:
284       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285       break;
286   }
287 }
288
289 static void
290 gst_multi_file_src_get_property (GObject * object, guint prop_id,
291     GValue * value, GParamSpec * pspec)
292 {
293   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
294
295   switch (prop_id) {
296     case ARG_LOCATION:
297       g_value_set_string (value, src->filename);
298       break;
299     case ARG_INDEX:
300       g_value_set_int (value, src->index);
301       break;
302     case ARG_START_INDEX:
303       g_value_set_int (value, src->start_index);
304       break;
305     case ARG_STOP_INDEX:
306       g_value_set_int (value, src->stop_index);
307       break;
308     case ARG_CAPS:
309       gst_value_set_caps (value, src->caps);
310       break;
311     case ARG_LOOP:
312       g_value_set_boolean (value, src->loop);
313       break;
314     default:
315       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316       break;
317   }
318 }
319
320 static gchar *
321 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
322 {
323   gchar *filename;
324
325   GST_DEBUG ("%d", multifilesrc->index);
326   filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
327
328   return filename;
329 }
330
331 static GstFlowReturn
332 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
333 {
334   GstMultiFileSrc *multifilesrc;
335   gsize size;
336   gchar *data;
337   gchar *filename;
338   GstBuffer *buf;
339   gboolean ret;
340   GError *error = NULL;
341
342   multifilesrc = GST_MULTI_FILE_SRC (src);
343
344   if (multifilesrc->index < multifilesrc->start_index) {
345     multifilesrc->index = multifilesrc->start_index;
346   }
347   filename = gst_multi_file_src_get_filename (multifilesrc);
348
349   GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
350
351   ret = g_file_get_contents (filename, &data, &size, &error);
352   if (!ret) {
353     if (multifilesrc->successful_read) {
354       /* If we've read at least one buffer successfully, not finding the
355        * next file is EOS. */
356       g_free (filename);
357       if (error != NULL)
358         g_error_free (error);
359
360       if (multifilesrc->loop) {
361         error = NULL;
362         multifilesrc->index = multifilesrc->start_index;
363
364         filename = gst_multi_file_src_get_filename (multifilesrc);
365         ret = g_file_get_contents (filename, &data, &size, &error);
366         if (!ret) {
367           g_free (filename);
368           if (error != NULL)
369             g_error_free (error);
370
371           return GST_FLOW_EOS;
372         }
373       } else {
374         return GST_FLOW_EOS;
375       }
376     } else {
377       goto handle_error;
378     }
379   }
380
381   multifilesrc->successful_read = TRUE;
382   multifilesrc->index++;
383   if (multifilesrc->stop_index != -1 &&
384       multifilesrc->index >= multifilesrc->stop_index) {
385     multifilesrc->index = multifilesrc->start_index;
386   }
387
388   buf = gst_buffer_new ();
389   gst_buffer_take_memory (buf, -1,
390       gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
391   GST_BUFFER_OFFSET (buf) = multifilesrc->offset;
392   GST_BUFFER_OFFSET_END (buf) = multifilesrc->offset + size;
393   multifilesrc->offset += size;
394
395   GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
396
397   g_free (filename);
398   *buffer = buf;
399   return GST_FLOW_OK;
400
401 handle_error:
402   {
403     if (error != NULL) {
404       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
405           ("Error while reading from file \"%s\".", filename),
406           ("%s", error->message));
407       g_error_free (error);
408     } else {
409       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
410           ("Error while reading from file \"%s\".", filename),
411           ("%s", g_strerror (errno)));
412     }
413     g_free (filename);
414     return GST_FLOW_ERROR;
415   }
416 }