upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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);
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_CAPS
78 };
79
80 #define DEFAULT_LOCATION "%05d"
81 #define DEFAULT_INDEX 0
82
83
84 GST_BOILERPLATE (GstMultiFileSrc, gst_multi_file_src, GstPushSrc,
85     GST_TYPE_PUSH_SRC);
86
87 static void
88 gst_multi_file_src_base_init (gpointer g_class)
89 {
90   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
91
92   GST_DEBUG_CATEGORY_INIT (gst_multi_file_src_debug, "multifilesrc", 0,
93       "multifilesrc element");
94
95   gst_element_class_add_pad_template (gstelement_class,
96       gst_static_pad_template_get (&gst_multi_file_src_pad_template));
97   gst_element_class_set_details_simple (gstelement_class, "Multi-File Source",
98       "Source/File",
99       "Read a sequentially named set of files into buffers",
100       "David Schleef <ds@schleef.org>");
101 }
102
103 static void
104 gst_multi_file_src_class_init (GstMultiFileSrcClass * klass)
105 {
106   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
107   GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
108   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
109
110   gobject_class->set_property = gst_multi_file_src_set_property;
111   gobject_class->get_property = gst_multi_file_src_get_property;
112
113   g_object_class_install_property (gobject_class, ARG_LOCATION,
114       g_param_spec_string ("location", "File Location",
115           "Pattern to create file names of input files.  File names are "
116           "created by calling sprintf() with the pattern and the current "
117           "index.", DEFAULT_LOCATION,
118           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119   g_object_class_install_property (gobject_class, ARG_INDEX,
120       g_param_spec_int ("index", "File Index",
121           "Index to use with location property to create file names.  The "
122           "index is incremented by one for each buffer read.",
123           0, 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
130   gobject_class->dispose = gst_multi_file_src_dispose;
131
132   gstbasesrc_class->get_caps = gst_multi_file_src_getcaps;
133   gstbasesrc_class->query = gst_multi_file_src_query;
134
135   gstpushsrc_class->create = gst_multi_file_src_create;
136
137   if (sizeof (off_t) < 8) {
138     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT,
139         sizeof (off_t));
140   }
141 }
142
143 static void
144 gst_multi_file_src_init (GstMultiFileSrc * multifilesrc,
145     GstMultiFileSrcClass * g_class)
146 {
147   multifilesrc->index = DEFAULT_INDEX;
148   multifilesrc->filename = g_strdup (DEFAULT_LOCATION);
149   multifilesrc->successful_read = FALSE;
150 }
151
152 static void
153 gst_multi_file_src_dispose (GObject * object)
154 {
155   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
156
157   g_free (src->filename);
158   src->filename = NULL;
159   if (src->caps)
160     gst_caps_unref (src->caps);
161
162   G_OBJECT_CLASS (parent_class)->dispose (object);
163 }
164
165 static GstCaps *
166 gst_multi_file_src_getcaps (GstBaseSrc * src)
167 {
168   GstMultiFileSrc *multi_file_src = GST_MULTI_FILE_SRC (src);
169
170   GST_DEBUG_OBJECT (src, "returning %" GST_PTR_FORMAT, multi_file_src->caps);
171
172   if (multi_file_src->caps) {
173     return gst_caps_ref (multi_file_src->caps);
174   } else {
175     return gst_caps_new_any ();
176   }
177 }
178
179 static gboolean
180 gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query)
181 {
182   gboolean res;
183   GstMultiFileSrc *mfsrc;
184
185   mfsrc = GST_MULTI_FILE_SRC (src);
186
187   switch (GST_QUERY_TYPE (query)) {
188     case GST_QUERY_POSITION:
189     {
190       GstFormat format;
191
192       gst_query_parse_position (query, &format, NULL);
193       switch (format) {
194         case GST_FORMAT_BUFFERS:
195         case GST_FORMAT_DEFAULT:
196           gst_query_set_position (query, GST_FORMAT_BUFFERS, mfsrc->index);
197           res = TRUE;
198           break;
199         default:
200           res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
201           break;
202       }
203       break;
204     }
205     default:
206       res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
207       break;
208   }
209   return res;
210 }
211
212 static gboolean
213 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
214 {
215   g_free (src->filename);
216   if (location != NULL) {
217     src->filename = g_strdup (location);
218   } else {
219     src->filename = NULL;
220   }
221
222   return TRUE;
223 }
224
225 static void
226 gst_multi_file_src_set_property (GObject * object, guint prop_id,
227     const GValue * value, GParamSpec * pspec)
228 {
229   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
230
231   switch (prop_id) {
232     case ARG_LOCATION:
233       gst_multi_file_src_set_location (src, g_value_get_string (value));
234       break;
235     case ARG_INDEX:
236       src->index = g_value_get_int (value);
237       break;
238     case ARG_CAPS:
239     {
240       const GstCaps *caps = gst_value_get_caps (value);
241       GstCaps *new_caps;
242
243       if (caps == NULL) {
244         new_caps = gst_caps_new_any ();
245       } else {
246         new_caps = gst_caps_copy (caps);
247       }
248       gst_caps_replace (&src->caps, new_caps);
249       gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
250     }
251       break;
252     default:
253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254       break;
255   }
256 }
257
258 static void
259 gst_multi_file_src_get_property (GObject * object, guint prop_id,
260     GValue * value, GParamSpec * pspec)
261 {
262   GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
263
264   switch (prop_id) {
265     case ARG_LOCATION:
266       g_value_set_string (value, src->filename);
267       break;
268     case ARG_INDEX:
269       g_value_set_int (value, src->index);
270       break;
271     case ARG_CAPS:
272       gst_value_set_caps (value, src->caps);
273       break;
274     default:
275       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276       break;
277   }
278 }
279
280 static gchar *
281 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
282 {
283   gchar *filename;
284
285   filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
286
287   return filename;
288 }
289
290 static GstFlowReturn
291 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
292 {
293   GstMultiFileSrc *multifilesrc;
294   gsize size;
295   gchar *data;
296   gchar *filename;
297   GstBuffer *buf;
298   gboolean ret;
299   GError *error = NULL;
300
301   multifilesrc = GST_MULTI_FILE_SRC (src);
302
303   filename = gst_multi_file_src_get_filename (multifilesrc);
304
305   GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
306
307   ret = g_file_get_contents (filename, &data, &size, &error);
308   if (!ret) {
309     if (multifilesrc->successful_read) {
310       /* If we've read at least one buffer successfully, not finding the
311        * next file is EOS. */
312       g_free (filename);
313       if (error != NULL)
314         g_error_free (error);
315       return GST_FLOW_UNEXPECTED;
316     } else {
317       goto handle_error;
318     }
319   }
320
321   multifilesrc->successful_read = TRUE;
322   multifilesrc->index++;
323
324   buf = gst_buffer_new ();
325   GST_BUFFER_DATA (buf) = (unsigned char *) data;
326   GST_BUFFER_MALLOCDATA (buf) = GST_BUFFER_DATA (buf);
327   GST_BUFFER_SIZE (buf) = size;
328   GST_BUFFER_OFFSET (buf) = multifilesrc->offset;
329   GST_BUFFER_OFFSET_END (buf) = multifilesrc->offset + size;
330   multifilesrc->offset += size;
331   gst_buffer_set_caps (buf, multifilesrc->caps);
332
333   GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
334
335   g_free (filename);
336   *buffer = buf;
337   return GST_FLOW_OK;
338
339 handle_error:
340   {
341     if (error != NULL) {
342       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
343           ("Error while reading from file \"%s\".", filename),
344           ("%s", error->message));
345       g_error_free (error);
346     } else {
347       GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
348           ("Error while reading from file \"%s\".", filename),
349           ("%s", g_strerror (errno)));
350     }
351     g_free (filename);
352     return GST_FLOW_ERROR;
353   }
354 }