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);
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));
96 is_seekable (GstBaseSrc * src)
98 GstMultiFileSrc *mfs = GST_MULTI_FILE_SRC (src);
100 if (mfs->fps_n != -1)
107 do_seek (GstBaseSrc * bsrc, GstSegment * segment)
110 GstClockTime position;
111 GstMultiFileSrc *src;
113 src = GST_MULTI_FILE_SRC (bsrc);
115 segment->time = segment->start;
116 position = segment->position;
117 reverse = segment->rate < 0;
120 GST_FIXME_OBJECT (src, "Handle reverse playback");
125 /* now move to the position indicated */
127 src->index = gst_util_uint64_scale (position,
128 src->fps_n, src->fps_d * GST_SECOND);
131 GST_WARNING_OBJECT (src, "No FPS set, can not seek");
140 gst_multi_file_src_class_init (GstMultiFileSrcClass * klass)
142 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
143 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
144 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
145 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
147 gobject_class->set_property = gst_multi_file_src_set_property;
148 gobject_class->get_property = gst_multi_file_src_get_property;
150 g_object_class_install_property (gobject_class, PROP_LOCATION,
151 g_param_spec_string ("location", "File Location",
152 "Pattern to create file names of input files. File names are "
153 "created by calling sprintf() with the pattern and the current "
154 "index.", DEFAULT_LOCATION,
155 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
156 g_object_class_install_property (gobject_class, PROP_INDEX,
157 g_param_spec_int ("index", "File Index",
158 "Index to use with location property to create file names. The "
159 "index is incremented by one for each buffer read.",
160 0, INT_MAX, DEFAULT_INDEX,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 g_object_class_install_property (gobject_class, PROP_START_INDEX,
163 g_param_spec_int ("start-index", "Start Index",
164 "Start value of index. The initial value of index can be set "
165 "either by setting index or start-index. When the end of the loop "
166 "is reached, the index will be set to the value start-index.",
167 0, INT_MAX, DEFAULT_INDEX,
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (gobject_class, PROP_STOP_INDEX,
170 g_param_spec_int ("stop-index", "Stop Index",
171 "Stop value of index. The special value -1 means no stop.",
172 -1, INT_MAX, DEFAULT_INDEX,
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 g_object_class_install_property (gobject_class, PROP_CAPS,
175 g_param_spec_boxed ("caps", "Caps",
176 "Caps describing the format of the data.",
177 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 g_object_class_install_property (gobject_class, PROP_LOOP,
179 g_param_spec_boolean ("loop", "Loop",
180 "Whether to repeat from the beginning when all files have been read.",
181 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 gobject_class->dispose = gst_multi_file_src_dispose;
185 gstbasesrc_class->get_caps = gst_multi_file_src_getcaps;
186 gstbasesrc_class->query = gst_multi_file_src_query;
187 gstbasesrc_class->is_seekable = is_seekable;
188 gstbasesrc_class->do_seek = do_seek;
190 gstpushsrc_class->create = gst_multi_file_src_create;
192 GST_DEBUG_CATEGORY_INIT (gst_multi_file_src_debug, "multifilesrc", 0,
193 "multifilesrc element");
195 gst_element_class_add_static_pad_template (gstelement_class,
196 &gst_multi_file_src_pad_template);
197 gst_element_class_set_static_metadata (gstelement_class, "Multi-File Source",
198 "Source/File", "Read a sequentially named set of files into buffers",
199 "David Schleef <ds@schleef.org>");
203 gst_multi_file_src_init (GstMultiFileSrc * multifilesrc)
205 multifilesrc->start_index = DEFAULT_INDEX;
206 multifilesrc->index = DEFAULT_INDEX;
207 multifilesrc->stop_index = -1;
208 multifilesrc->filename = g_strdup (DEFAULT_LOCATION);
209 multifilesrc->successful_read = FALSE;
210 multifilesrc->fps_n = multifilesrc->fps_d = -1;
215 gst_multi_file_src_dispose (GObject * object)
217 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
219 g_free (src->filename);
220 src->filename = NULL;
222 gst_caps_unref (src->caps);
224 G_OBJECT_CLASS (parent_class)->dispose (object);
228 gst_multi_file_src_getcaps (GstBaseSrc * src, GstCaps * filter)
230 GstMultiFileSrc *multi_file_src = GST_MULTI_FILE_SRC (src);
232 GST_DEBUG_OBJECT (src, "returning %" GST_PTR_FORMAT, multi_file_src->caps);
234 if (multi_file_src->caps) {
236 return gst_caps_intersect_full (filter, multi_file_src->caps,
237 GST_CAPS_INTERSECT_FIRST);
239 return gst_caps_ref (multi_file_src->caps);
242 return gst_caps_ref (filter);
244 return gst_caps_new_any ();
249 gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query)
252 GstMultiFileSrc *mfsrc;
254 mfsrc = GST_MULTI_FILE_SRC (src);
256 switch (GST_QUERY_TYPE (query)) {
257 case GST_QUERY_POSITION:
261 gst_query_parse_position (query, &format, NULL);
263 case GST_FORMAT_BUFFERS:
264 case GST_FORMAT_DEFAULT:
265 gst_query_set_position (query, format,
266 mfsrc->index - mfsrc->start_index);
270 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
276 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
283 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
285 g_free (src->filename);
286 if (location != NULL) {
287 src->filename = g_strdup (location);
289 src->filename = NULL;
296 gst_multi_file_src_set_property (GObject * object, guint prop_id,
297 const GValue * value, GParamSpec * pspec)
299 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
303 gst_multi_file_src_set_location (src, g_value_get_string (value));
306 GST_OBJECT_LOCK (src);
307 /* index was really meant to be read-only, but for backwards-compatibility
308 * we set start_index to make it work as it used to */
309 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED))
310 src->start_index = g_value_get_int (value);
312 src->index = g_value_get_int (value);
313 GST_OBJECT_UNLOCK (src);
315 case PROP_START_INDEX:
316 src->start_index = g_value_get_int (value);
318 case PROP_STOP_INDEX:
319 src->stop_index = g_value_get_int (value);
323 GstStructure *st = NULL;
324 const GstCaps *caps = gst_value_get_caps (value);
328 new_caps = gst_caps_new_any ();
330 new_caps = gst_caps_copy (caps);
332 gst_caps_replace (&src->caps, new_caps);
333 gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
335 if (new_caps && gst_caps_get_size (new_caps) == 1 &&
336 (st = gst_caps_get_structure (new_caps, 0))
337 && gst_structure_get_fraction (st, "framerate", &src->fps_n,
339 GST_INFO_OBJECT (src, "Seting framerate to %d/%d", src->fps_n,
348 src->loop = g_value_get_boolean (value);
351 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357 gst_multi_file_src_get_property (GObject * object, guint prop_id,
358 GValue * value, GParamSpec * pspec)
360 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
364 g_value_set_string (value, src->filename);
367 g_value_set_int (value, src->index);
369 case PROP_START_INDEX:
370 g_value_set_int (value, src->start_index);
372 case PROP_STOP_INDEX:
373 g_value_set_int (value, src->stop_index);
376 gst_value_set_caps (value, src->caps);
379 g_value_set_boolean (value, src->loop);
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
392 GST_DEBUG ("%d", multifilesrc->index);
393 filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
399 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
401 GstMultiFileSrc *multifilesrc;
407 GError *error = NULL;
409 multifilesrc = GST_MULTI_FILE_SRC (src);
411 if (multifilesrc->index < multifilesrc->start_index) {
412 multifilesrc->index = multifilesrc->start_index;
415 if (multifilesrc->stop_index != -1 &&
416 multifilesrc->index > multifilesrc->stop_index) {
417 if (multifilesrc->loop)
418 multifilesrc->index = multifilesrc->start_index;
423 filename = gst_multi_file_src_get_filename (multifilesrc);
425 GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
427 ret = g_file_get_contents (filename, &data, &size, &error);
429 if (multifilesrc->successful_read) {
430 /* If we've read at least one buffer successfully, not finding the
431 * next file is EOS. */
434 g_error_free (error);
436 if (multifilesrc->loop) {
438 multifilesrc->index = multifilesrc->start_index;
440 filename = gst_multi_file_src_get_filename (multifilesrc);
441 ret = g_file_get_contents (filename, &data, &size, &error);
445 g_error_free (error);
457 multifilesrc->successful_read = TRUE;
458 multifilesrc->index++;
460 buf = gst_buffer_new ();
461 gst_buffer_append_memory (buf,
462 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
463 GST_BUFFER_OFFSET (buf) = multifilesrc->offset;
464 GST_BUFFER_OFFSET_END (buf) = multifilesrc->offset + size;
465 multifilesrc->offset += size;
467 GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
476 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
477 ("Error while reading from file \"%s\".", filename),
478 ("%s", error->message));
479 g_error_free (error);
481 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
482 ("Error while reading from file \"%s\".", filename),
483 ("%s", g_strerror (errno)));
486 return GST_FLOW_ERROR;
491 gst_multi_file_src_uri_get_type (GType type)
496 static const gchar *const *
497 gst_multi_file_src_uri_get_protocols (GType type)
499 static const gchar *protocols[] = { "multifile", NULL };
501 return (const gchar * const *) protocols;
505 gst_multi_file_src_uri_get_uri (GstURIHandler * handler)
507 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (handler);
510 GST_OBJECT_LOCK (src);
511 if (src->filename != NULL) {
512 GstUri *uri = gst_uri_new ("multifle", NULL, NULL, GST_URI_NO_PORT,
513 src->filename, NULL, NULL);
515 ret = gst_uri_to_string (uri);
520 GST_OBJECT_UNLOCK (src);
526 gst_multi_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
529 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (handler);
533 gsturi = gst_uri_from_string (uri);
537 /* This should get us the unescaped path */
538 path = gst_uri_get_path (gsturi);
542 GST_OBJECT_LOCK (src);
543 gst_multi_file_src_set_location (src, path);
544 GST_OBJECT_UNLOCK (src);
547 gst_uri_unref (gsturi);
554 GST_WARNING_OBJECT (src, "Invalid multifile URI '%s'", uri);
555 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
556 "Invalid multifile URI");
558 gst_uri_unref (gsturi);
564 gst_multi_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
566 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
568 iface->get_type = gst_multi_file_src_uri_get_type;
569 iface->get_protocols = gst_multi_file_src_uri_get_protocols;
570 iface->get_uri = gst_multi_file_src_uri_get_uri;
571 iface->set_uri = gst_multi_file_src_uri_set_uri;