splitfilesrc: fix up docs for 0.11
[platform/upstream/gst-plugins-good.git] / gst / multifile / gstsplitfilesrc.c
1 /* GStreamer Split File Source
2  * Copyright (C) 2011 Collabora Ltd. <tim.muller@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 /**
20  * SECTION:element-splitfilesrc
21  * @see_also: #GstFileSrc, #GstMultiFileSrc
22  *
23  * Reads data from multiple files, presenting those files as one continuous
24  * file to downstream elements. This is useful for reading a large file that
25  * had to be split into multiple parts due to filesystem file size limitations,
26  * for example.
27  *
28  * The files to select are chosen via the location property, which supports
29  * (and expects) shell-style wildcards (but only for the filename, not for
30  * directories). The results will be sorted.
31  *
32  * <refsect2>
33  * <title>Example launch lines</title>
34  * |[
35  * gst-launch splitfilesrc location="/path/to/part-*.mpg" ! decodebin ! ...
36  * ]| Plays the different parts as if they were one single MPEG file.
37  * |[
38  * gst-launch playbin uri="splitfile://path/to/foo.avi.*"
39  * ]| Plays the different parts as if they were one single AVI file.
40  * </refsect2>
41  *
42  * Since: 0.10.31
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #  include "config.h"
47 #endif
48
49 #include "gstsplitfilesrc.h"
50 #include "patternspec.h"
51
52 #include <string.h>
53
54 #ifdef G_OS_WIN32
55 #define DEFAULT_PATTERN_MATCH_MODE MATCH_MODE_UTF8
56 #else
57 #define DEFAULT_PATTERN_MATCH_MODE MATCH_MODE_AUTO
58 #endif
59
60 enum
61 {
62   PROP_LOCATION = 1
63 };
64
65 #define DEFAULT_LOCATION NULL
66
67 static void gst_split_file_src_uri_handler_init (gpointer g_iface,
68     gpointer iface_data);
69 static void gst_split_file_src_set_property (GObject * object, guint prop_id,
70     const GValue * value, GParamSpec * pspec);
71 static void gst_split_file_src_get_property (GObject * object, guint prop_id,
72     GValue * value, GParamSpec * pspec);
73 static void gst_split_file_src_finalize (GObject * obj);
74
75 static gboolean gst_split_file_src_start (GstBaseSrc * basesrc);
76 static gboolean gst_split_file_src_stop (GstBaseSrc * basesrc);
77 static gboolean gst_split_file_src_can_seek (GstBaseSrc * basesrc);
78 static gboolean gst_split_file_src_get_size (GstBaseSrc * basesrc, guint64 * s);
79 static gboolean gst_split_file_src_unlock (GstBaseSrc * basesrc);
80 static GstFlowReturn gst_split_file_src_create (GstBaseSrc * basesrc,
81     guint64 offset, guint size, GstBuffer ** buffer);
82
83 static GstStaticPadTemplate gst_split_file_src_pad_template =
84 GST_STATIC_PAD_TEMPLATE ("src",
85     GST_PAD_SRC,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS_ANY);
88
89 GST_DEBUG_CATEGORY_STATIC (splitfilesrc_debug);
90 #define GST_CAT_DEFAULT splitfilesrc_debug
91
92
93 G_DEFINE_TYPE_WITH_CODE (GstSplitFileSrc, gst_split_file_src, GST_TYPE_BASE_SRC,
94     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
95         gst_split_file_src_uri_handler_init));
96
97 #ifdef G_OS_WIN32
98 #define WIN32_BLURB " Location string must be in UTF-8 encoding (on Windows)."
99 #else
100 #define WIN32_BLURB             /* nothing */
101 #endif
102
103 static void
104 gst_split_file_src_class_init (GstSplitFileSrcClass * klass)
105 {
106   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
107   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
108   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
109
110   gobject_class->set_property = gst_split_file_src_set_property;
111   gobject_class->get_property = gst_split_file_src_get_property;
112   gobject_class->finalize = gst_split_file_src_finalize;
113
114   g_object_class_install_property (gobject_class, PROP_LOCATION,
115       g_param_spec_string ("location", "File Location",
116           "Wildcard pattern to match file names of the input files. If "
117           "the location is an absolute path or contains directory components, "
118           "only the base file name part will be considered for pattern "
119           "matching. The results will be sorted." WIN32_BLURB,
120           DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
121
122   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_split_file_src_start);
123   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_split_file_src_stop);
124   gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_split_file_src_create);
125   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_split_file_src_get_size);
126   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_split_file_src_unlock);
127   gstbasesrc_class->is_seekable =
128       GST_DEBUG_FUNCPTR (gst_split_file_src_can_seek);
129
130   GST_DEBUG_CATEGORY_INIT (splitfilesrc_debug, "splitfilesrc", 0,
131       "splitfilesrc element");
132
133   gst_element_class_add_pad_template (gstelement_class,
134       gst_static_pad_template_get (&gst_split_file_src_pad_template));
135
136   gst_element_class_set_static_metadata (gstelement_class, "Split-File Source",
137       "Source/File",
138       "Read a sequentially named set of files as if it was one large file",
139       "Tim-Philipp Müller <tim.muller@collabora.co.uk>");
140 }
141
142 static void
143 gst_split_file_src_init (GstSplitFileSrc * splitfilesrc)
144 {
145 }
146
147 static void
148 gst_split_file_src_finalize (GObject * obj)
149 {
150   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (obj);
151
152   g_free (src->location);
153   src->location = NULL;
154
155   G_OBJECT_CLASS (gst_split_file_src_parent_class)->finalize (obj);
156 }
157
158 static gboolean
159 gst_split_file_src_can_seek (GstBaseSrc * basesrc)
160 {
161   return TRUE;
162 }
163
164 static gboolean
165 gst_split_file_src_unlock (GstBaseSrc * basesrc)
166 {
167   /* This is not actually that useful, since all normal file
168    * operations are fully blocking anyway */
169 #if 0
170   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (basesrc);
171
172   GST_DEBUG_OBJECT (src, "cancelling pending I/O operation if there is one");
173   /* g_cancellable_cancel (src->cancellable); */
174   GST_DEBUG_OBJECT (src, "done");
175 #endif
176
177   return TRUE;
178 }
179
180 static gboolean
181 gst_split_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
182 {
183   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (basesrc);
184
185   *size = src->parts[src->num_parts - 1].stop + 1;
186   return TRUE;
187 }
188
189 static void
190 gst_split_file_src_set_location (GstSplitFileSrc * src, const char *location)
191 {
192   GST_OBJECT_LOCK (src);
193   g_free (src->location);
194
195   if (location != NULL && g_str_has_prefix (location, "splitfile://"))
196     src->location = gst_uri_get_location (location);
197   else
198     src->location = g_strdup (location);
199 #ifdef G_OS_WIN32
200   if (!g_utf8_validate (src->location, -1, NULL)) {
201     g_warning ("splitfilesrc 'location' property must be in UTF-8 "
202         "encoding on Windows");
203   }
204 #endif
205   GST_OBJECT_UNLOCK (src);
206 }
207
208 static void
209 gst_split_file_src_set_property (GObject * object, guint prop_id,
210     const GValue * value, GParamSpec * pspec)
211 {
212   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (object);
213
214   switch (prop_id) {
215     case PROP_LOCATION:
216       gst_split_file_src_set_location (src, g_value_get_string (value));
217       break;
218     default:
219       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220       break;
221   }
222 }
223
224 static void
225 gst_split_file_src_get_property (GObject * object, guint prop_id,
226     GValue * value, GParamSpec * pspec)
227 {
228   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (object);
229
230   switch (prop_id) {
231     case PROP_LOCATION:
232       GST_OBJECT_LOCK (src);
233       g_value_set_string (value, src->location);
234       GST_OBJECT_UNLOCK (src);
235       break;
236     default:
237       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238       break;
239   }
240 }
241
242 static int
243 gst_split_file_src_array_sortfunc (gchar ** a, gchar ** b)
244 {
245   return strcmp (*a, *b);
246 }
247
248 static gchar **
249 gst_split_file_src_find_files (GstSplitFileSrc * src, const gchar * dirname,
250     const gchar * basename, GError ** err)
251 {
252   PatternSpec *pspec;
253   GPtrArray *files;
254   const gchar *name;
255   GDir *dir;
256
257   if (dirname == NULL || basename == NULL)
258     goto invalid_location;
259
260   GST_INFO_OBJECT (src, "checking in directory '%s' for pattern '%s'",
261       dirname, basename);
262
263   dir = g_dir_open (dirname, 0, err);
264   if (dir == NULL)
265     return NULL;
266
267   if (DEFAULT_PATTERN_MATCH_MODE == MATCH_MODE_UTF8 &&
268       !g_utf8_validate (basename, -1, NULL)) {
269     goto not_utf8;
270   }
271
272   /* mode will be AUTO on linux/unix and UTF8 on win32 */
273   pspec = pattern_spec_new (basename, DEFAULT_PATTERN_MATCH_MODE);
274
275   files = g_ptr_array_new ();
276
277   while ((name = g_dir_read_name (dir))) {
278     GST_TRACE_OBJECT (src, "check: %s", name);
279     if (pattern_match_string (pspec, name)) {
280       GST_DEBUG_OBJECT (src, "match: %s", name);
281       g_ptr_array_add (files, g_build_filename (dirname, name, NULL));
282     }
283   }
284
285   if (files->len == 0)
286     goto no_matches;
287
288   g_ptr_array_sort (files, (GCompareFunc) gst_split_file_src_array_sortfunc);
289   g_ptr_array_add (files, NULL);
290
291   pattern_spec_free (pspec);
292   g_dir_close (dir);
293
294   return (gchar **) g_ptr_array_free (files, FALSE);
295
296 /* ERRORS */
297 invalid_location:
298   {
299     g_set_error_literal (err, G_FILE_ERROR, G_FILE_ERROR_INVAL,
300         "No filename specified.");
301     return NULL;
302   }
303 not_utf8:
304   {
305     g_dir_close (dir);
306     g_set_error_literal (err, G_FILE_ERROR, G_FILE_ERROR_INVAL,
307         "Filename pattern must be UTF-8 on Windows.");
308     return NULL;
309   }
310 no_matches:
311   {
312     pattern_spec_free (pspec);
313     g_dir_close (dir);
314     g_set_error_literal (err, G_FILE_ERROR, G_FILE_ERROR_NOENT,
315         "Found no files matching the pattern.");
316     return NULL;
317   }
318 }
319
320 static gboolean
321 gst_split_file_src_start (GstBaseSrc * basesrc)
322 {
323   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (basesrc);
324   GCancellable *cancel;
325   gboolean ret = FALSE;
326   guint64 offset;
327   GError *err = NULL;
328   gchar *basename = NULL;
329   gchar *dirname = NULL;
330   gchar **files;
331   guint i;
332
333   GST_OBJECT_LOCK (src);
334   if (src->location != NULL && src->location[0] != '\0') {
335     basename = g_path_get_basename (src->location);
336     dirname = g_path_get_dirname (src->location);
337   }
338   GST_OBJECT_UNLOCK (src);
339
340   files = gst_split_file_src_find_files (src, dirname, basename, &err);
341
342   if (files == NULL || *files == NULL)
343     goto no_files;
344
345   src->num_parts = g_strv_length (files);
346   src->parts = g_new0 (GstFilePart, src->num_parts);
347
348   cancel = src->cancellable;
349
350   offset = 0;
351   for (i = 0; i < src->num_parts; ++i) {
352     GFileInputStream *stream;
353     GFileInfo *info;
354     goffset size;
355     GFile *file;
356
357     file = g_file_new_for_path (files[i]);
358     stream = g_file_read (file, cancel, &err);
359     g_object_unref (file);
360
361     if (err != NULL)
362       goto open_read_error;
363
364     info = g_file_input_stream_query_info (stream, "standard::*", NULL, &err);
365     if (err != NULL) {
366       g_object_unref (stream);
367       goto query_info_error;
368     }
369
370     size = g_file_info_get_size (info);
371     g_object_unref (info);
372
373     src->parts[i].stream = stream;
374     src->parts[i].path = g_strdup (files[i]);
375     src->parts[i].start = offset;
376     src->parts[i].stop = offset + size - 1;
377
378     GST_DEBUG ("[%010" G_GUINT64_FORMAT "-%010" G_GUINT64_FORMAT "] %s",
379         src->parts[i].start, src->parts[i].stop, src->parts[i].path);
380
381     offset += size;
382   }
383
384   GST_INFO ("Successfully opened %u file parts for reading", src->num_parts);
385
386   src->cur_part = 0;
387
388   src->cancellable = g_cancellable_new ();
389
390   ret = TRUE;
391
392 done:
393   if (err != NULL)
394     g_error_free (err);
395   g_strfreev (files);
396   g_free (basename);
397   g_free (dirname);
398   return ret;
399
400 /* ERRORS */
401 no_files:
402   {
403     if (err->code == G_IO_ERROR_CANCELLED)
404       goto cancelled;
405
406     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, ("%s", err->message),
407         ("Failed to find files in '%s' for pattern '%s'",
408             GST_STR_NULL (dirname), GST_STR_NULL (basename)));
409     goto done;
410   }
411 open_read_error:
412   {
413     if (err->code == G_IO_ERROR_CANCELLED)
414       goto cancelled;
415
416     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, ("%s", err->message),
417         ("Failed to open file '%s' for reading", files[i]));
418     goto done;
419   }
420 query_info_error:
421   {
422     if (err->code == G_IO_ERROR_CANCELLED)
423       goto cancelled;
424
425     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, ("%s", err->message),
426         ("Failed to query info for file '%s'", files[i]));
427     goto done;
428   }
429 cancelled:
430   {
431     GST_DEBUG_OBJECT (src, "I/O operation cancelled from another thread");
432     goto done;
433   }
434 }
435
436 static gboolean
437 gst_split_file_src_stop (GstBaseSrc * basesrc)
438 {
439   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (basesrc);
440   guint i;
441
442   for (i = 0; i < src->num_parts; ++i) {
443     if (src->parts[i].stream != NULL)
444       g_object_unref (src->parts[i].stream);
445     g_free (src->parts[i].path);
446   }
447   g_free (src->parts);
448   src->parts = NULL;
449   src->num_parts = 0;
450
451   g_object_unref (src->cancellable);
452   src->cancellable = NULL;
453
454   return TRUE;
455 }
456
457 static gboolean
458 gst_split_file_src_find_part_for_offset (GstSplitFileSrc * src, guint64 offset,
459     guint * part_number)
460 {
461   GstFilePart *part;
462   guint i;
463
464   /* TODO: could use gst_util_array_binary_search() here */
465   part = src->parts;
466   for (i = 0; i < src->num_parts; ++i) {
467     if (offset >= part->start && offset <= part->stop) {
468       *part_number = i;
469       return TRUE;
470     }
471     ++part;
472   }
473
474   return FALSE;
475 }
476
477 static GstFlowReturn
478 gst_split_file_src_create (GstBaseSrc * basesrc, guint64 offset, guint size,
479     GstBuffer ** buffer)
480 {
481   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (basesrc);
482   GstFilePart cur_part;
483   GInputStream *stream;
484   GCancellable *cancel;
485   GSeekable *seekable;
486   GstBuffer *buf;
487   GError *err = NULL;
488   guint64 read_offset;
489   GstMapInfo map;
490   guint8 *data;
491   guint to_read;
492
493   cur_part = src->parts[src->cur_part];
494   if (offset < cur_part.start || offset > cur_part.stop) {
495     if (!gst_split_file_src_find_part_for_offset (src, offset, &src->cur_part))
496       return GST_FLOW_EOS;
497     cur_part = src->parts[src->cur_part];
498   }
499
500   GST_LOG_OBJECT (src, "current part: %u (%" G_GUINT64_FORMAT " - "
501       "%" G_GUINT64_FORMAT ", %s)", src->cur_part, cur_part.start,
502       cur_part.stop, cur_part.path);
503
504   buf = gst_buffer_new_allocate (NULL, size, NULL);
505
506   GST_BUFFER_OFFSET (buf) = offset;
507
508   gst_buffer_map (buf, &map, GST_MAP_WRITE);
509   data = map.data;
510
511   cancel = src->cancellable;
512
513   while (size > 0) {
514     guint64 bytes_to_end_of_part;
515     gsize read = 0;
516
517     /* we want the offset into the file part */
518     read_offset = offset - cur_part.start;
519
520     GST_LOG ("Reading part %03u from offset %" G_GUINT64_FORMAT " (%s)",
521         src->cur_part, read_offset, cur_part.path);
522
523     /* FIXME: only seek when needed (hopefully gio is smart) */
524     seekable = G_SEEKABLE (cur_part.stream);
525     if (!g_seekable_seek (seekable, read_offset, G_SEEK_SET, cancel, &err))
526       goto seek_failed;
527
528     GST_LOG_OBJECT (src, "now: %" G_GUINT64_FORMAT, g_seekable_tell (seekable));
529
530     bytes_to_end_of_part = (cur_part.stop - cur_part.start) + 1 - read_offset;
531     to_read = MIN (size, bytes_to_end_of_part);
532
533     GST_LOG_OBJECT (src, "reading %u bytes from part %u (bytes to end of "
534         "part: %u)", to_read, src->cur_part, (guint) bytes_to_end_of_part);
535
536     stream = G_INPUT_STREAM (cur_part.stream);
537
538     /* NB: we won't try to read beyond EOF */
539     if (!g_input_stream_read_all (stream, data, to_read, &read, cancel, &err))
540       goto read_failed;
541
542     GST_LOG_OBJECT (src, "read %u bytes", (guint) read);
543
544     data += read;
545     size -= read;
546     offset += read;
547
548     /* are we done? */
549     if (size == 0)
550       break;
551
552     GST_LOG_OBJECT (src, "%u bytes left to read for this chunk", size);
553
554     /* corner case, this should never really happen (assuming basesrc clips
555      * requests beyond the file size) */
556     if (read < to_read) {
557       if (src->cur_part == src->num_parts - 1) {
558         /* last file part, stop reading and truncate buffer */
559         gst_buffer_set_size (buf, offset - GST_BUFFER_OFFSET (buf));
560         break;
561       } else {
562         goto file_part_changed;
563       }
564     }
565
566     ++src->cur_part;
567     cur_part = src->parts[src->cur_part];
568   }
569
570   GST_BUFFER_OFFSET_END (buf) = offset;
571
572   gst_buffer_unmap (buf, &map);
573
574   *buffer = buf;
575   GST_LOG_OBJECT (src, "read %" G_GSIZE_FORMAT " bytes into buf %p",
576       gst_buffer_get_size (buf), buf);
577   return GST_FLOW_OK;
578
579 /* ERRORS */
580 seek_failed:
581   {
582     if (err->code == G_IO_ERROR_CANCELLED)
583       goto cancelled;
584
585     GST_ELEMENT_ERROR (src, RESOURCE, SEEK, (NULL),
586         ("Seek to %" G_GUINT64_FORMAT " in %s failed", read_offset,
587             cur_part.path));
588     g_error_free (err);
589     gst_buffer_unref (buf);
590     return GST_FLOW_ERROR;
591   }
592 read_failed:
593   {
594     if (err->code == G_IO_ERROR_CANCELLED)
595       goto cancelled;
596
597     GST_ELEMENT_ERROR (src, RESOURCE, READ, ("%s", err->message),
598         ("Read from %" G_GUINT64_FORMAT " in %s failed", read_offset,
599             cur_part.path));
600     g_error_free (err);
601     gst_buffer_unref (buf);
602     return GST_FLOW_ERROR;
603   }
604 file_part_changed:
605   {
606     GST_ELEMENT_ERROR (src, RESOURCE, READ,
607         ("Read error while reading file part %s", cur_part.path),
608         ("Short read in file part, file may have been modified since start"));
609     gst_buffer_unref (buf);
610     return GST_FLOW_ERROR;
611   }
612 cancelled:
613   {
614     GST_DEBUG_OBJECT (src, "I/O operation cancelled from another thread");
615     g_error_free (err);
616     gst_buffer_unref (buf);
617     return GST_FLOW_FLUSHING;
618   }
619 }
620
621 static guint
622 gst_split_file_src_uri_get_type (GType type)
623 {
624   return GST_URI_SRC;
625 }
626
627 static const gchar *const *
628 gst_split_file_src_uri_get_protocols (GType type)
629 {
630   static const gchar *protocols[] = { "splitfile", NULL };
631
632   return (const gchar * const *) protocols;
633 }
634
635 static gchar *
636 gst_split_file_src_uri_get_uri (GstURIHandler * handler)
637 {
638   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (handler);
639   gchar *ret;
640
641   GST_OBJECT_LOCK (src);
642   if (src->location != NULL)
643     ret = g_strdup_printf ("splitfile://%s", src->location);
644   else
645     ret = NULL;
646   GST_OBJECT_UNLOCK (src);
647
648   return ret;
649 }
650
651 static gboolean
652 gst_split_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
653     GError ** error)
654 {
655   GstSplitFileSrc *src = GST_SPLIT_FILE_SRC (handler);
656
657   gst_split_file_src_set_location (src, uri);
658
659   return TRUE;
660 }
661
662 static void
663 gst_split_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
664 {
665   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
666
667   iface->get_type = gst_split_file_src_uri_get_type;
668   iface->get_protocols = gst_split_file_src_uri_get_protocols;
669   iface->get_uri = gst_split_file_src_uri_get_uri;
670   iface->set_uri = gst_split_file_src_uri_set_uri;
671 }