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