2 * Copyright (C) 2006 David A. Schleef <ds@schleef.org>
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.
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.
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.
22 * SECTION:element-multifilesrc
23 * @see_also: #GstFileSrc
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.
30 * File names are created by replacing "\%d" with the index using printf().
33 * <title>Example launch line</title>
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.
47 #include "gstmultifilesrc.h"
50 static GstFlowReturn gst_multi_file_src_create (GstPushSrc * src,
53 static void gst_multi_file_src_dispose (GObject * object);
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);
63 static GstStaticPadTemplate gst_multi_file_src_pad_template =
64 GST_STATIC_PAD_TEMPLATE ("src",
69 GST_DEBUG_CATEGORY_STATIC (gst_multi_file_src_debug);
70 #define GST_CAT_DEFAULT gst_multi_file_src_debug
80 #define DEFAULT_LOCATION "%05d"
81 #define DEFAULT_INDEX 0
84 GST_BOILERPLATE (GstMultiFileSrc, gst_multi_file_src, GstPushSrc,
88 gst_multi_file_src_base_init (gpointer g_class)
90 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
92 GST_DEBUG_CATEGORY_INIT (gst_multi_file_src_debug, "multifilesrc", 0,
93 "multifilesrc element");
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",
99 "Read a sequentially named set of files into buffers",
100 "David Schleef <ds@schleef.org>");
104 gst_multi_file_src_class_init (GstMultiFileSrcClass * klass)
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);
110 gobject_class->set_property = gst_multi_file_src_set_property;
111 gobject_class->get_property = gst_multi_file_src_get_property;
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));
130 gobject_class->dispose = gst_multi_file_src_dispose;
132 gstbasesrc_class->get_caps = gst_multi_file_src_getcaps;
133 gstbasesrc_class->query = gst_multi_file_src_query;
135 gstpushsrc_class->create = gst_multi_file_src_create;
137 if (sizeof (off_t) < 8) {
138 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT,
144 gst_multi_file_src_init (GstMultiFileSrc * multifilesrc,
145 GstMultiFileSrcClass * g_class)
147 multifilesrc->index = DEFAULT_INDEX;
148 multifilesrc->filename = g_strdup (DEFAULT_LOCATION);
149 multifilesrc->successful_read = FALSE;
153 gst_multi_file_src_dispose (GObject * object)
155 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
157 g_free (src->filename);
158 src->filename = NULL;
160 gst_caps_unref (src->caps);
162 G_OBJECT_CLASS (parent_class)->dispose (object);
166 gst_multi_file_src_getcaps (GstBaseSrc * src)
168 GstMultiFileSrc *multi_file_src = GST_MULTI_FILE_SRC (src);
170 GST_DEBUG_OBJECT (src, "returning %" GST_PTR_FORMAT, multi_file_src->caps);
172 if (multi_file_src->caps) {
173 return gst_caps_ref (multi_file_src->caps);
175 return gst_caps_new_any ();
180 gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query)
183 GstMultiFileSrc *mfsrc;
185 mfsrc = GST_MULTI_FILE_SRC (src);
187 switch (GST_QUERY_TYPE (query)) {
188 case GST_QUERY_POSITION:
192 gst_query_parse_position (query, &format, NULL);
194 case GST_FORMAT_BUFFERS:
195 case GST_FORMAT_DEFAULT:
196 gst_query_set_position (query, GST_FORMAT_BUFFERS, mfsrc->index);
200 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
206 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
213 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
215 g_free (src->filename);
216 if (location != NULL) {
217 src->filename = g_strdup (location);
219 src->filename = NULL;
226 gst_multi_file_src_set_property (GObject * object, guint prop_id,
227 const GValue * value, GParamSpec * pspec)
229 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
233 gst_multi_file_src_set_location (src, g_value_get_string (value));
236 src->index = g_value_get_int (value);
240 const GstCaps *caps = gst_value_get_caps (value);
244 new_caps = gst_caps_new_any ();
246 new_caps = gst_caps_copy (caps);
248 gst_caps_replace (&src->caps, new_caps);
249 gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 gst_multi_file_src_get_property (GObject * object, guint prop_id,
260 GValue * value, GParamSpec * pspec)
262 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
266 g_value_set_string (value, src->filename);
269 g_value_set_int (value, src->index);
272 gst_value_set_caps (value, src->caps);
275 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
285 filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
291 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
293 GstMultiFileSrc *multifilesrc;
299 GError *error = NULL;
301 multifilesrc = GST_MULTI_FILE_SRC (src);
303 filename = gst_multi_file_src_get_filename (multifilesrc);
305 GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
307 ret = g_file_get_contents (filename, &data, &size, &error);
309 if (multifilesrc->successful_read) {
310 /* If we've read at least one buffer successfully, not finding the
311 * next file is EOS. */
314 g_error_free (error);
315 return GST_FLOW_UNEXPECTED;
321 multifilesrc->successful_read = TRUE;
322 multifilesrc->index++;
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);
333 GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
342 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
343 ("Error while reading from file \"%s\".", filename),
344 ("%s", error->message));
345 g_error_free (error);
347 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
348 ("Error while reading from file \"%s\".", filename),
349 ("%s", g_strerror (errno)));
352 return GST_FLOW_ERROR;