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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21 /**
22 * SECTION:element-multifilesrc
23 * @title: multifilesrc
24 * @see_also: #GstFileSrc
25 *
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.
31 *
32 * File names are created by replacing "\%d" with the index using `printf()`.
33 *
34 * ## Example launch line
35 * |[
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.
41 *
42 */
43
44 #ifdef HAVE_CONFIG_H
45 # include "config.h"
46 #endif
47
48 #include "gstmultifilesrc.h"
49
50
51 static GstFlowReturn gst_multi_file_src_create (GstPushSrc * src,
52 GstBuffer ** buffer);
53
54 static void gst_multi_file_src_dispose (GObject * object);
55
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,
63 gpointer iface_data);
64
65
66 static GstStaticPadTemplate gst_multi_file_src_pad_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
68 GST_PAD_SRC,
69 GST_PAD_ALWAYS,
70 GST_STATIC_CAPS_ANY);
71
72 GST_DEBUG_CATEGORY_STATIC (gst_multi_file_src_debug);
73 #define GST_CAT_DEFAULT gst_multi_file_src_debug
74
75 enum
76 {
77 PROP_0,
78 PROP_LOCATION,
79 PROP_INDEX,
80 PROP_START_INDEX,
81 PROP_STOP_INDEX,
82 PROP_CAPS,
83 PROP_LOOP
84 };
85
86 #define DEFAULT_LOCATION "%05d"
87 #define DEFAULT_INDEX 0
88
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 ());
95
96 static gboolean
is_seekable(GstBaseSrc * src)97 is_seekable (GstBaseSrc * src)
98 {
99 GstMultiFileSrc *mfs = GST_MULTI_FILE_SRC (src);
100
101 if (mfs->fps_n != -1)
102 return TRUE;
103
104 return FALSE;
105 }
106
107 static gboolean
do_seek(GstBaseSrc * bsrc,GstSegment * segment)108 do_seek (GstBaseSrc * bsrc, GstSegment * segment)
109 {
110 gboolean reverse;
111 GstClockTime position;
112 GstMultiFileSrc *src;
113
114 src = GST_MULTI_FILE_SRC (bsrc);
115
116 segment->time = segment->start;
117 position = segment->position;
118 reverse = segment->rate < 0;
119
120 if (reverse) {
121 GST_FIXME_OBJECT (src, "Handle reverse playback");
122
123 return FALSE;
124 }
125
126 /* now move to the position indicated */
127 if (src->fps_n) {
128 src->index = gst_util_uint64_scale (position,
129 src->fps_n, src->fps_d * GST_SECOND);
130 } else {
131 src->index = 0;
132 GST_WARNING_OBJECT (src, "No FPS set, can not seek");
133
134 return FALSE;
135 }
136
137 return TRUE;
138 }
139
140 static void
gst_multi_file_src_class_init(GstMultiFileSrcClass * klass)141 gst_multi_file_src_class_init (GstMultiFileSrcClass * klass)
142 {
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);
147
148 gobject_class->set_property = gst_multi_file_src_set_property;
149 gobject_class->get_property = gst_multi_file_src_get_property;
150
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));
183
184 gobject_class->dispose = gst_multi_file_src_dispose;
185
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;
190
191 gstpushsrc_class->create = gst_multi_file_src_create;
192
193 GST_DEBUG_CATEGORY_INIT (gst_multi_file_src_debug, "multifilesrc", 0,
194 "multifilesrc element");
195
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>");
201 }
202
203 static void
gst_multi_file_src_init(GstMultiFileSrc * multifilesrc)204 gst_multi_file_src_init (GstMultiFileSrc * multifilesrc)
205 {
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;
212
213 }
214
215 static void
gst_multi_file_src_dispose(GObject * object)216 gst_multi_file_src_dispose (GObject * object)
217 {
218 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
219
220 g_free (src->filename);
221 src->filename = NULL;
222 if (src->caps)
223 gst_caps_unref (src->caps);
224
225 G_OBJECT_CLASS (parent_class)->dispose (object);
226 }
227
228 static GstCaps *
gst_multi_file_src_getcaps(GstBaseSrc * src,GstCaps * filter)229 gst_multi_file_src_getcaps (GstBaseSrc * src, GstCaps * filter)
230 {
231 GstMultiFileSrc *multi_file_src = GST_MULTI_FILE_SRC (src);
232
233 GST_DEBUG_OBJECT (src, "returning %" GST_PTR_FORMAT, multi_file_src->caps);
234
235 if (multi_file_src->caps) {
236 if (filter)
237 return gst_caps_intersect_full (filter, multi_file_src->caps,
238 GST_CAPS_INTERSECT_FIRST);
239 else
240 return gst_caps_ref (multi_file_src->caps);
241 } else {
242 if (filter)
243 return gst_caps_ref (filter);
244 else
245 return gst_caps_new_any ();
246 }
247 }
248
249 static gboolean
gst_multi_file_src_query(GstBaseSrc * src,GstQuery * query)250 gst_multi_file_src_query (GstBaseSrc * src, GstQuery * query)
251 {
252 gboolean res;
253 GstMultiFileSrc *mfsrc;
254
255 mfsrc = GST_MULTI_FILE_SRC (src);
256
257 switch (GST_QUERY_TYPE (query)) {
258 case GST_QUERY_POSITION:
259 {
260 GstFormat format;
261
262 gst_query_parse_position (query, &format, NULL);
263 switch (format) {
264 case GST_FORMAT_BUFFERS:
265 case GST_FORMAT_DEFAULT:
266 gst_query_set_position (query, format,
267 mfsrc->index - mfsrc->start_index);
268 res = TRUE;
269 break;
270 default:
271 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
272 break;
273 }
274 break;
275 }
276 default:
277 res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
278 break;
279 }
280 return res;
281 }
282
283 static gboolean
gst_multi_file_src_set_location(GstMultiFileSrc * src,const gchar * location)284 gst_multi_file_src_set_location (GstMultiFileSrc * src, const gchar * location)
285 {
286 g_free (src->filename);
287 if (location != NULL) {
288 src->filename = g_strdup (location);
289 } else {
290 src->filename = NULL;
291 }
292
293 return TRUE;
294 }
295
296 static void
gst_multi_file_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)297 gst_multi_file_src_set_property (GObject * object, guint prop_id,
298 const GValue * value, GParamSpec * pspec)
299 {
300 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
301
302 switch (prop_id) {
303 case PROP_LOCATION:
304 gst_multi_file_src_set_location (src, g_value_get_string (value));
305 break;
306 case PROP_INDEX:
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);
312 else
313 src->index = g_value_get_int (value);
314 GST_OBJECT_UNLOCK (src);
315 break;
316 case PROP_START_INDEX:
317 src->start_index = g_value_get_int (value);
318 break;
319 case PROP_STOP_INDEX:
320 src->stop_index = g_value_get_int (value);
321 break;
322 case PROP_CAPS:
323 {
324 GstStructure *st = NULL;
325 const GstCaps *caps = gst_value_get_caps (value);
326 GstCaps *new_caps;
327
328 if (caps == NULL) {
329 new_caps = gst_caps_new_any ();
330 } else {
331 new_caps = gst_caps_copy (caps);
332 }
333 gst_caps_replace (&src->caps, new_caps);
334 gst_pad_set_caps (GST_BASE_SRC_PAD (src), new_caps);
335
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,
339 &src->fps_d)) {
340 GST_INFO_OBJECT (src, "Setting framerate to %d/%d", src->fps_n,
341 src->fps_d);
342 } else {
343 src->fps_n = -1;
344 src->fps_d = -1;
345 }
346 gst_caps_unref (new_caps);
347 }
348 break;
349 case PROP_LOOP:
350 src->loop = g_value_get_boolean (value);
351 break;
352 default:
353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354 break;
355 }
356 }
357
358 static void
gst_multi_file_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)359 gst_multi_file_src_get_property (GObject * object, guint prop_id,
360 GValue * value, GParamSpec * pspec)
361 {
362 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (object);
363
364 switch (prop_id) {
365 case PROP_LOCATION:
366 g_value_set_string (value, src->filename);
367 break;
368 case PROP_INDEX:
369 g_value_set_int (value, src->index);
370 break;
371 case PROP_START_INDEX:
372 g_value_set_int (value, src->start_index);
373 break;
374 case PROP_STOP_INDEX:
375 g_value_set_int (value, src->stop_index);
376 break;
377 case PROP_CAPS:
378 gst_value_set_caps (value, src->caps);
379 break;
380 case PROP_LOOP:
381 g_value_set_boolean (value, src->loop);
382 break;
383 default:
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385 break;
386 }
387 }
388
389 static gchar *
gst_multi_file_src_get_filename(GstMultiFileSrc * multifilesrc)390 gst_multi_file_src_get_filename (GstMultiFileSrc * multifilesrc)
391 {
392 gchar *filename;
393
394 GST_DEBUG ("%d", multifilesrc->index);
395 filename = g_strdup_printf (multifilesrc->filename, multifilesrc->index);
396
397 return filename;
398 }
399
400 static GstFlowReturn
gst_multi_file_src_create(GstPushSrc * src,GstBuffer ** buffer)401 gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
402 {
403 GstMultiFileSrc *multifilesrc;
404 gsize size;
405 gchar *data;
406 gchar *filename;
407 GstBuffer *buf;
408 gboolean ret;
409 GError *error = NULL;
410
411 multifilesrc = GST_MULTI_FILE_SRC (src);
412
413 if (multifilesrc->index < multifilesrc->start_index) {
414 multifilesrc->index = multifilesrc->start_index;
415 }
416
417 if (multifilesrc->stop_index != -1 &&
418 multifilesrc->index > multifilesrc->stop_index) {
419 if (multifilesrc->loop)
420 multifilesrc->index = multifilesrc->start_index;
421 else
422 return GST_FLOW_EOS;
423 }
424
425 filename = gst_multi_file_src_get_filename (multifilesrc);
426
427 GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
428
429 ret = g_file_get_contents (filename, &data, &size, &error);
430 if (!ret) {
431 if (multifilesrc->successful_read) {
432 /* If we've read at least one buffer successfully, not finding the
433 * next file is EOS. */
434 g_free (filename);
435 if (error != NULL)
436 g_error_free (error);
437
438 if (multifilesrc->loop) {
439 error = NULL;
440 multifilesrc->index = multifilesrc->start_index;
441
442 filename = gst_multi_file_src_get_filename (multifilesrc);
443 ret = g_file_get_contents (filename, &data, &size, &error);
444 if (!ret) {
445 g_free (filename);
446 if (error != NULL)
447 g_error_free (error);
448
449 return GST_FLOW_EOS;
450 }
451 } else {
452 return GST_FLOW_EOS;
453 }
454 } else {
455 goto handle_error;
456 }
457 }
458
459 multifilesrc->successful_read = TRUE;
460 multifilesrc->index++;
461
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;
468
469 GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
470
471 g_free (filename);
472 *buffer = buf;
473 return GST_FLOW_OK;
474
475 handle_error:
476 {
477 if (error != NULL) {
478 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
479 ("Error while reading from file \"%s\".", filename),
480 ("%s", error->message));
481 g_error_free (error);
482 } else {
483 GST_ELEMENT_ERROR (multifilesrc, RESOURCE, READ,
484 ("Error while reading from file \"%s\".", filename),
485 ("%s", g_strerror (errno)));
486 }
487 g_free (filename);
488 return GST_FLOW_ERROR;
489 }
490 }
491
492 static GstURIType
gst_multi_file_src_uri_get_type(GType type)493 gst_multi_file_src_uri_get_type (GType type)
494 {
495 return GST_URI_SRC;
496 }
497
498 static const gchar *const *
gst_multi_file_src_uri_get_protocols(GType type)499 gst_multi_file_src_uri_get_protocols (GType type)
500 {
501 static const gchar *protocols[] = { "multifile", NULL };
502
503 return (const gchar * const *) protocols;
504 }
505
506 static gchar *
gst_multi_file_src_uri_get_uri(GstURIHandler * handler)507 gst_multi_file_src_uri_get_uri (GstURIHandler * handler)
508 {
509 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (handler);
510 gchar *ret;
511
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);
516
517 ret = gst_uri_to_string (uri);
518 gst_uri_unref (uri);
519 } else {
520 ret = NULL;
521 }
522 GST_OBJECT_UNLOCK (src);
523
524 return ret;
525 }
526
527 static gboolean
gst_multi_file_src_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)528 gst_multi_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
529 GError ** error)
530 {
531 GstMultiFileSrc *src = GST_MULTI_FILE_SRC (handler);
532 GstUri *gsturi;
533 gchar *path;
534
535 gsturi = gst_uri_from_string (uri);
536 if (gsturi == NULL)
537 goto invalid_uri;
538
539 /* This should get us the unescaped path */
540 path = gst_uri_get_path (gsturi);
541 if (path == NULL)
542 goto invalid_uri;
543
544 GST_OBJECT_LOCK (src);
545 gst_multi_file_src_set_location (src, path);
546 GST_OBJECT_UNLOCK (src);
547
548 g_free (path);
549 gst_uri_unref (gsturi);
550
551 return TRUE;
552
553 /* ERRORS */
554 invalid_uri:
555 {
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");
559 if (gsturi)
560 gst_uri_unref (gsturi);
561 return FALSE;
562 }
563 }
564
565 static void
gst_multi_file_src_uri_handler_init(gpointer g_iface,gpointer iface_data)566 gst_multi_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
567 {
568 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
569
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;
574 }
575