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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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. We also need a videorate element to set timestamps
29 * on all buffers after the first one in accordance with the framerate.
31 * File names are created by replacing "\%d" with the index using printf().
34 * <title>Example launch line</title>
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.
48 #include "gstmultifilesrc.h"
51 static GstFlowReturn gst_multi_file_src_create (GstPushSrc * src,
54 static void gst_multi_file_src_dispose (GObject * object);
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);
64 static GstStaticPadTemplate gst_multi_file_src_pad_template =
65 GST_STATIC_PAD_TEMPLATE ("src",
70 GST_DEBUG_CATEGORY_STATIC (gst_multi_file_src_debug);
71 #define GST_CAT_DEFAULT gst_multi_file_src_debug
84 #define DEFAULT_LOCATION "%05d"
85 #define DEFAULT_INDEX 0
87 #define gst_multi_file_src_parent_class parent_class
88 G_DEFINE_TYPE (GstMultiFileSrc, gst_multi_file_src, GST_TYPE_PUSH_SRC);
92 is_seekable (GstBaseSrc * src)
94 GstMultiFileSrc *mfs = GST_MULTI_FILE_SRC (src);
103 do_seek (GstBaseSrc * bsrc, GstSegment * segment)
106 GstClockTime position;
107 GstMultiFileSrc *src;
109 src = GST_MULTI_FILE_SRC (bsrc);
111 segment->time = segment->start;
112 position = segment->position;
113 reverse = segment->rate < 0;
116 GST_FIXME_OBJECT (src, "Handle reverse playback");
121 /* now move to the position indicated */
123 src->index = gst_util_uint64_scale (position,
124 src->fps_n, src->fps_d * GST_SECOND);
127 GST_WARNING_OBJECT (src, "No FPS set, can not seek");
136 gst_multi_file_src_class_init (GstMultiFileSrcClass * klass)
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);
143 gobject_class->set_property = gst_multi_file_src_set_property;
144 gobject_class->get_property = gst_multi_file_src_get_property;
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));
179 gobject_class->dispose = gst_multi_file_src_dispose;
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;
186 gstpushsrc_class->create = gst_multi_file_src_create;
188 GST_DEBUG_CATEGORY_INIT (gst_multi_file_src_debug, "multifilesrc", 0,
189 "multifilesrc element");
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>");
199 gst_multi_file_src_init (GstMultiFileSrc * multifilesrc)
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;
211 gst_multi_file_src_dispose (GObject * object)
213 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
215 g_free (src->filename);
216 src->filename = NULL;
218 gst_caps_unref (src->caps);
220 G_OBJECT_CLASS (parent_class)->dispose (object);
224 gst_multi_file_src_getcaps (GstBaseSrc * src, GstCaps * filter)
226 GstMultiFileSrc *multi_file_src = GST_MULTI_FILE_SRC (src);
228 GST_DEBUG_OBJECT (src, "returning %" GST_PTR_FORMAT, multi_file_src->caps);
230 if (multi_file_src->caps) {
232 return gst_caps_intersect_full (filter, multi_file_src->caps,
233 GST_CAPS_INTERSECT_FIRST);
235 return gst_caps_ref (multi_file_src->caps);
238 return gst_caps_ref (filter);
240 return gst_caps_new_any ();
245 gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query)
248 GstMultiFileSrc *mfsrc;
250 mfsrc = GST_MULTI_FILE_SRC (src);
252 switch (GST_QUERY_TYPE (query)) {
253 case GST_QUERY_POSITION:
257 gst_query_parse_position (query, &format, NULL);
259 case GST_FORMAT_BUFFERS:
260 case GST_FORMAT_DEFAULT:
261 gst_query_set_position (query, format,
262 mfsrc->index - mfsrc->start_index);
266 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
272 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
279 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
281 g_free (src->filename);
282 if (location != NULL) {
283 src->filename = g_strdup (location);
285 src->filename = NULL;
292 gst_multi_file_src_set_property (GObject * object, guint prop_id,
293 const GValue * value, GParamSpec * pspec)
295 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
299 gst_multi_file_src_set_location (src, g_value_get_string (value));
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);
308 src->index = g_value_get_int (value);
309 GST_OBJECT_UNLOCK (src);
311 case PROP_START_INDEX:
312 src->start_index = g_value_get_int (value);
314 case PROP_STOP_INDEX:
315 src->stop_index = g_value_get_int (value);
319 GstStructure *st = NULL;
320 const GstCaps *caps = gst_value_get_caps (value);
324 new_caps = gst_caps_new_any ();
326 new_caps = gst_caps_copy (caps);
328 gst_caps_replace (&src->caps, new_caps);
329 gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
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,
335 GST_INFO_OBJECT (src, "Seting framerate to %d/%d", src->fps_n,
344 src->loop = g_value_get_boolean (value);
347 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
353 gst_multi_file_src_get_property (GObject * object, guint prop_id,
354 GValue * value, GParamSpec * pspec)
356 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
360 g_value_set_string (value, src->filename);
363 g_value_set_int (value, src->index);
365 case PROP_START_INDEX:
366 g_value_set_int (value, src->start_index);
368 case PROP_STOP_INDEX:
369 g_value_set_int (value, src->stop_index);
372 gst_value_set_caps (value, src->caps);
375 g_value_set_boolean (value, src->loop);
378 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
384 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
388 GST_DEBUG ("%d", multifilesrc->index);
389 filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
395 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
397 GstMultiFileSrc *multifilesrc;
403 GError *error = NULL;
405 multifilesrc = GST_MULTI_FILE_SRC (src);
407 if (multifilesrc->index < multifilesrc->start_index) {
408 multifilesrc->index = multifilesrc->start_index;
411 if (multifilesrc->stop_index != -1 &&
412 multifilesrc->index > multifilesrc->stop_index) {
413 if (multifilesrc->loop)
414 multifilesrc->index = multifilesrc->start_index;
419 filename = gst_multi_file_src_get_filename (multifilesrc);
421 GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
423 ret = g_file_get_contents (filename, &data, &size, &error);
425 if (multifilesrc->successful_read) {
426 /* If we've read at least one buffer successfully, not finding the
427 * next file is EOS. */
430 g_error_free (error);
432 if (multifilesrc->loop) {
434 multifilesrc->index = multifilesrc->start_index;
436 filename = gst_multi_file_src_get_filename (multifilesrc);
437 ret = g_file_get_contents (filename, &data, &size, &error);
441 g_error_free (error);
453 multifilesrc->successful_read = TRUE;
454 multifilesrc->index++;
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;
463 GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
472 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
473 ("Error while reading from file \"%s\".", filename),
474 ("%s", error->message));
475 g_error_free (error);
477 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
478 ("Error while reading from file \"%s\".", filename),
479 ("%s", g_strerror (errno)));
482 return GST_FLOW_ERROR;