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 * @title: multifilesrc
24 * @see_also: #GstFileSrc
26 * Reads buffers from sequentially named files. If used together with an image
27 * decoder, one needs to use the #GstMultiFileSrc:caps property or a capsfilter
28 * to force to caps containing a framerate. Otherwise image decoders send EOS
29 * after the first picture. We also need a videorate element to set timestamps
30 * on all buffers after the first one in accordance with the framerate.
32 * File names are created by replacing "\%d" with the index using `printf()`.
34 * ## Example launch line
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);
62 static void gst_multi_file_src_uri_handler_init (gpointer g_iface,
66 static GstStaticPadTemplate gst_multi_file_src_pad_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
72 GST_DEBUG_CATEGORY_STATIC (gst_multi_file_src_debug);
73 #define GST_CAT_DEFAULT gst_multi_file_src_debug
86 #define DEFAULT_LOCATION "%05d"
87 #define DEFAULT_INDEX 0
89 #define gst_multi_file_src_parent_class parent_class
90 G_DEFINE_TYPE_WITH_CODE (GstMultiFileSrc, gst_multi_file_src, GST_TYPE_PUSH_SRC,
91 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
92 gst_multi_file_src_uri_handler_init));
93 GST_ELEMENT_REGISTER_DEFINE (multifilesrc, "multifilesrc", GST_RANK_NONE,
94 gst_multi_file_src_get_type ());
97 is_seekable (GstBaseSrc * src)
99 GstMultiFileSrc *mfs = GST_MULTI_FILE_SRC (src);
101 if (mfs->fps_n != -1)
108 do_seek (GstBaseSrc * bsrc, GstSegment * segment)
111 GstClockTime position;
112 GstMultiFileSrc *src;
114 src = GST_MULTI_FILE_SRC (bsrc);
116 segment->time = segment->start;
117 position = segment->position;
118 reverse = segment->rate < 0;
121 GST_FIXME_OBJECT (src, "Handle reverse playback");
126 /* now move to the position indicated */
128 src->index = gst_util_uint64_scale (position,
129 src->fps_n, src->fps_d * GST_SECOND);
132 GST_WARNING_OBJECT (src, "No FPS set, can not seek");
141 gst_multi_file_src_class_init (GstMultiFileSrcClass * klass)
143 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
145 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
146 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
148 gobject_class->set_property = gst_multi_file_src_set_property;
149 gobject_class->get_property = gst_multi_file_src_get_property;
151 g_object_class_install_property (gobject_class, PROP_LOCATION,
152 g_param_spec_string ("location", "File Location",
153 "Pattern to create file names of input files. File names are "
154 "created by calling sprintf() with the pattern and the current "
155 "index.", DEFAULT_LOCATION,
156 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (gobject_class, PROP_INDEX,
158 g_param_spec_int ("index", "File Index",
159 "Index to use with location property to create file names. The "
160 "index is incremented by one for each buffer read.",
161 0, INT_MAX, DEFAULT_INDEX,
162 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (gobject_class, PROP_START_INDEX,
164 g_param_spec_int ("start-index", "Start Index",
165 "Start value of index. The initial value of index can be set "
166 "either by setting index or start-index. When the end of the loop "
167 "is reached, the index will be set to the value start-index.",
168 0, INT_MAX, DEFAULT_INDEX,
169 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170 g_object_class_install_property (gobject_class, PROP_STOP_INDEX,
171 g_param_spec_int ("stop-index", "Stop Index",
172 "Stop value of index. The special value -1 means no stop.",
173 -1, INT_MAX, DEFAULT_INDEX,
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (gobject_class, PROP_CAPS,
176 g_param_spec_boxed ("caps", "Caps",
177 "Caps describing the format of the data.",
178 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179 g_object_class_install_property (gobject_class, PROP_LOOP,
180 g_param_spec_boolean ("loop", "Loop",
181 "Whether to repeat from the beginning when all files have been read.",
182 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 gobject_class->dispose = gst_multi_file_src_dispose;
186 gstbasesrc_class->get_caps = gst_multi_file_src_getcaps;
187 gstbasesrc_class->query = gst_multi_file_src_query;
188 gstbasesrc_class->is_seekable = is_seekable;
189 gstbasesrc_class->do_seek = do_seek;
191 gstpushsrc_class->create = gst_multi_file_src_create;
193 GST_DEBUG_CATEGORY_INIT (gst_multi_file_src_debug, "multifilesrc", 0,
194 "multifilesrc element");
196 gst_element_class_add_static_pad_template (gstelement_class,
197 &gst_multi_file_src_pad_template);
198 gst_element_class_set_static_metadata (gstelement_class, "Multi-File Source",
199 "Source/File", "Read a sequentially named set of files into buffers",
200 "David Schleef <ds@schleef.org>");
204 gst_multi_file_src_init (GstMultiFileSrc * multifilesrc)
206 multifilesrc->start_index = DEFAULT_INDEX;
207 multifilesrc->index = DEFAULT_INDEX;
208 multifilesrc->stop_index = -1;
209 multifilesrc->filename = g_strdup (DEFAULT_LOCATION);
210 multifilesrc->successful_read = FALSE;
211 multifilesrc->fps_n = multifilesrc->fps_d = -1;
216 gst_multi_file_src_dispose (GObject * object)
218 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
220 g_free (src->filename);
221 src->filename = NULL;
223 gst_caps_unref (src->caps);
225 G_OBJECT_CLASS (parent_class)->dispose (object);
229 gst_multi_file_src_getcaps (GstBaseSrc * src, GstCaps * filter)
231 GstMultiFileSrc *multi_file_src = GST_MULTI_FILE_SRC (src);
233 GST_DEBUG_OBJECT (src, "returning %" GST_PTR_FORMAT, multi_file_src->caps);
235 if (multi_file_src->caps) {
237 return gst_caps_intersect_full (filter, multi_file_src->caps,
238 GST_CAPS_INTERSECT_FIRST);
240 return gst_caps_ref (multi_file_src->caps);
243 return gst_caps_ref (filter);
245 return gst_caps_new_any ();
250 gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query)
253 GstMultiFileSrc *mfsrc;
255 mfsrc = GST_MULTI_FILE_SRC (src);
257 switch (GST_QUERY_TYPE (query)) {
258 case GST_QUERY_POSITION:
262 gst_query_parse_position (query, &format, NULL);
264 case GST_FORMAT_BUFFERS:
265 case GST_FORMAT_DEFAULT:
266 gst_query_set_position (query, format,
267 mfsrc->index - mfsrc->start_index);
271 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
277 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
284 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
286 g_free (src->filename);
287 if (location != NULL) {
288 src->filename = g_strdup (location);
290 src->filename = NULL;
297 gst_multi_file_src_set_property (GObject * object, guint prop_id,
298 const GValue * value, GParamSpec * pspec)
300 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
304 gst_multi_file_src_set_location (src, g_value_get_string (value));
307 GST_OBJECT_LOCK (src);
308 /* index was really meant to be read-only, but for backwards-compatibility
309 * we set start_index to make it work as it used to */
310 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED))
311 src->start_index = g_value_get_int (value);
313 src->index = g_value_get_int (value);
314 GST_OBJECT_UNLOCK (src);
316 case PROP_START_INDEX:
317 src->start_index = g_value_get_int (value);
319 case PROP_STOP_INDEX:
320 src->stop_index = g_value_get_int (value);
324 GstStructure *st = NULL;
325 const GstCaps *caps = gst_value_get_caps (value);
329 new_caps = gst_caps_new_any ();
331 new_caps = gst_caps_copy (caps);
333 gst_caps_replace (&src->caps, new_caps);
334 gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
336 if (new_caps && gst_caps_get_size (new_caps) == 1 &&
337 (st = gst_caps_get_structure (new_caps, 0))
338 && gst_structure_get_fraction (st, "framerate", &src->fps_n,
340 GST_INFO_OBJECT (src, "Setting framerate to %d/%d", src->fps_n,
346 gst_caps_unref (new_caps);
350 src->loop = g_value_get_boolean (value);
353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359 gst_multi_file_src_get_property (GObject * object, guint prop_id,
360 GValue * value, GParamSpec * pspec)
362 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
366 g_value_set_string (value, src->filename);
369 g_value_set_int (value, src->index);
371 case PROP_START_INDEX:
372 g_value_set_int (value, src->start_index);
374 case PROP_STOP_INDEX:
375 g_value_set_int (value, src->stop_index);
378 gst_value_set_caps (value, src->caps);
381 g_value_set_boolean (value, src->loop);
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
390 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
394 GST_DEBUG ("%d", multifilesrc->index);
395 filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
401 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
403 GstMultiFileSrc *multifilesrc;
409 GError *error = NULL;
411 multifilesrc = GST_MULTI_FILE_SRC (src);
413 if (multifilesrc->index < multifilesrc->start_index) {
414 multifilesrc->index = multifilesrc->start_index;
417 if (multifilesrc->stop_index != -1 &&
418 multifilesrc->index > multifilesrc->stop_index) {
419 if (multifilesrc->loop)
420 multifilesrc->index = multifilesrc->start_index;
425 filename = gst_multi_file_src_get_filename (multifilesrc);
427 GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
429 ret = g_file_get_contents (filename, &data, &size, &error);
431 if (multifilesrc->successful_read) {
432 /* If we've read at least one buffer successfully, not finding the
433 * next file is EOS. */
436 g_error_free (error);
438 if (multifilesrc->loop) {
440 multifilesrc->index = multifilesrc->start_index;
442 filename = gst_multi_file_src_get_filename (multifilesrc);
443 ret = g_file_get_contents (filename, &data, &size, &error);
447 g_error_free (error);
459 multifilesrc->successful_read = TRUE;
460 multifilesrc->index++;
462 buf = gst_buffer_new ();
463 gst_buffer_append_memory (buf,
464 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
465 GST_BUFFER_OFFSET (buf) = multifilesrc->offset;
466 GST_BUFFER_OFFSET_END (buf) = multifilesrc->offset + size;
467 multifilesrc->offset += size;
469 GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
478 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
479 ("Error while reading from file \"%s\".", filename),
480 ("%s", error->message));
481 g_error_free (error);
483 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
484 ("Error while reading from file \"%s\".", filename),
485 ("%s", g_strerror (errno)));
488 return GST_FLOW_ERROR;
493 gst_multi_file_src_uri_get_type (GType type)
498 static const gchar *const *
499 gst_multi_file_src_uri_get_protocols (GType type)
501 static const gchar *protocols[] = { "multifile", NULL };
503 return (const gchar * const *) protocols;
507 gst_multi_file_src_uri_get_uri (GstURIHandler * handler)
509 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (handler);
512 GST_OBJECT_LOCK (src);
513 if (src->filename != NULL) {
514 GstUri *uri = gst_uri_new ("multifle", NULL, NULL, GST_URI_NO_PORT,
515 src->filename, NULL, NULL);
517 ret = gst_uri_to_string (uri);
522 GST_OBJECT_UNLOCK (src);
528 gst_multi_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
531 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (handler);
535 gsturi = gst_uri_from_string (uri);
539 /* This should get us the unescaped path */
540 path = gst_uri_get_path (gsturi);
544 GST_OBJECT_LOCK (src);
545 gst_multi_file_src_set_location (src, path);
546 GST_OBJECT_UNLOCK (src);
549 gst_uri_unref (gsturi);
556 GST_WARNING_OBJECT (src, "Invalid multifile URI '%s'", uri);
557 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
558 "Invalid multifile URI");
560 gst_uri_unref (gsturi);
566 gst_multi_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
568 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
570 iface->get_type = gst_multi_file_src_uri_get_type;
571 iface->get_protocols = gst_multi_file_src_uri_get_protocols;
572 iface->get_uri = gst_multi_file_src_uri_get_uri;
573 iface->set_uri = gst_multi_file_src_uri_set_uri;