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