2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 * SECTION:element-filesrc
24 * @see_also: #GstFileSrc
26 * Read data from a file in the local file system.
29 * <title>Example launch line</title>
31 * gst-launch filesrc location=song.ogg ! decodebin2 ! autoaudiosink
32 * ]| Play a song.ogg from local dir.
41 #include "gstfilesrc.h"
44 #include <sys/types.h>
46 #include <io.h> /* lseek, open, close, read */
47 /* On win32, stat* default to 32 bit; we need the 64-bit
48 * variants, so explicitly define it that way. */
50 #define fstat _fstat64
52 #define lseek _lseeki64
55 /* Prevent stat.h from defining the stat* functions as
56 * _stat*, since we're explicitly overriding that */
67 # include <sys/mman.h>
73 #include "../../gst/gst-i18n-lib.h"
75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
80 /* FIXME we should be using glib for this */
82 #define S_ISREG(mode) ((mode)&_S_IFREG)
85 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
88 #define S_ISSOCK(x) (0)
94 /* Copy of glib's g_open due to win32 libc/cross-DLL brokenness: we can't
95 * use the 'file descriptor' opened in glib (and returned from this function)
96 * in this library, as they may have unrelated C runtimes. */
98 gst_open (const gchar * filename, int flags, int mode)
101 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
105 if (wfilename == NULL) {
110 retval = _wopen (wfilename, flags, mode);
118 return open (filename, flags, mode);
123 /**********************************************************************
124 * GStreamer Default File Source
125 * Theory of Operation
127 * Update: see GstFileSrc:use-mmap property documentation below
128 * for why use of mmap() is disabled by default.
130 * This source uses mmap(2) to efficiently load data from a file.
131 * To do this without seriously polluting the applications' memory
132 * space, it must do so in smaller chunks, say 1-4MB at a time.
133 * Buffers are then subdivided from these mmap'd chunks, to directly
134 * make use of the mmap.
136 * To handle refcounting so that the mmap can be freed at the appropriate
137 * time, a buffer will be created for each mmap'd region, and all new
138 * buffers will be sub-buffers of this top-level buffer. As they are
139 * freed, the refcount goes down on the mmap'd buffer and its free()
140 * function is called, which will call munmap(2) on itself.
142 * If a buffer happens to cross the boundaries of an mmap'd region, we
143 * have to decide whether it's more efficient to copy the data into a
144 * new buffer, or mmap() just that buffer. There will have to be a
145 * breakpoint size to determine which will be done. The mmap() size
146 * has a lot to do with this as well, because you end up in double-
147 * jeopardy: the larger the outgoing buffer, the more data to copy when
148 * it overlaps, *and* the more frequently you'll have buffers that *do*
151 * Seeking is another tricky aspect to do efficiently. The initial
152 * implementation of this source won't make use of these features, however.
153 * The issue is that if an application seeks backwards in a file, *and*
154 * that region of the file is covered by an mmap that hasn't been fully
155 * deallocated, we really should re-use it. But keeping track of these
156 * regions is tricky because we have to lock the structure that holds
157 * them. We need to settle on a locking primitive (GMutex seems to be
158 * a really good option...), then we can do that.
162 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
163 #define GST_CAT_DEFAULT gst_file_src_debug
165 /* FileSrc signals and args */
172 #define DEFAULT_BLOCKSIZE 4*1024
173 #define DEFAULT_MMAPSIZE 4*1024*1024
174 #define DEFAULT_TOUCH TRUE
175 #define DEFAULT_USEMMAP FALSE
176 #define DEFAULT_SEQUENTIAL FALSE
189 static void gst_file_src_finalize (GObject * object);
191 static void gst_file_src_set_property (GObject * object, guint prop_id,
192 const GValue * value, GParamSpec * pspec);
193 static void gst_file_src_get_property (GObject * object, guint prop_id,
194 GValue * value, GParamSpec * pspec);
196 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
197 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
199 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
200 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
201 static GstFlowReturn gst_file_src_create (GstBaseSrc * src, guint64 offset,
202 guint length, GstBuffer ** buffer);
203 static gboolean gst_file_src_query (GstBaseSrc * src, GstQuery * query);
205 static void gst_file_src_uri_handler_init (gpointer g_iface,
206 gpointer iface_data);
209 _do_init (GType filesrc_type)
211 static const GInterfaceInfo urihandler_info = {
212 gst_file_src_uri_handler_init,
217 g_type_add_interface_static (filesrc_type, GST_TYPE_URI_HANDLER,
219 GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
222 GST_BOILERPLATE_FULL (GstFileSrc, gst_file_src, GstBaseSrc, GST_TYPE_BASE_SRC,
226 gst_file_src_base_init (gpointer g_class)
228 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
230 gst_element_class_set_details_simple (gstelement_class,
233 "Read from arbitrary point in a file",
234 "Erik Walthinsen <omega@cse.ogi.edu>");
235 gst_element_class_add_pad_template (gstelement_class,
236 gst_static_pad_template_get (&srctemplate));
240 gst_file_src_class_init (GstFileSrcClass * klass)
242 GObjectClass *gobject_class;
243 GstBaseSrcClass *gstbasesrc_class;
245 gobject_class = G_OBJECT_CLASS (klass);
246 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
248 gobject_class->set_property = gst_file_src_set_property;
249 gobject_class->get_property = gst_file_src_get_property;
251 g_object_class_install_property (gobject_class, ARG_FD,
252 g_param_spec_int ("fd", "File-descriptor",
253 "File-descriptor for the file being mmap()d", 0, G_MAXINT, 0,
254 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (gobject_class, ARG_LOCATION,
256 g_param_spec_string ("location", "File Location",
257 "Location of the file to read", NULL,
258 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
259 GST_PARAM_MUTABLE_READY));
260 g_object_class_install_property (gobject_class, ARG_MMAPSIZE,
261 g_param_spec_ulong ("mmapsize", "mmap() Block Size",
262 "Size in bytes of mmap()d regions", 0, G_MAXULONG, DEFAULT_MMAPSIZE,
263 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
264 GST_PARAM_MUTABLE_PLAYING));
265 g_object_class_install_property (gobject_class, ARG_TOUCH,
266 g_param_spec_boolean ("touch", "Touch mapped region read data",
267 "Touch mmapped data regions to force them to be read from disk",
269 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
270 GST_PARAM_MUTABLE_PLAYING));
272 * GstFileSrc:use-mmap
274 * Whether to use mmap(). Set to TRUE to force use of mmap() instead of
275 * read() for reading data.
277 * Use of mmap() is disabled by default since with mmap() there are a
278 * number of occasions where the process/application will be notified of
279 * read errors via a SIGBUS signal from the kernel, which will lead to
280 * the application being killed if not handled by the application. This
281 * is something that is difficult to work around for a library like
282 * GStreamer, hence use of mmap() is disabled by default. Said errors
283 * can occur for example when an external device (e.g. an external hard
284 * drive or a portable music player) are unplugged while in use, or when
285 * a CD/DVD medium cannot be be read because the medium is scratched or
289 g_object_class_install_property (gobject_class, ARG_USEMMAP,
290 g_param_spec_boolean ("use-mmap", "Use mmap to read data",
291 "Whether to use mmap() instead of read()",
292 DEFAULT_USEMMAP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
293 GST_PARAM_MUTABLE_READY));
294 g_object_class_install_property (gobject_class, ARG_SEQUENTIAL,
295 g_param_spec_boolean ("sequential", "Optimise for sequential mmap access",
296 "Whether to use madvise to hint to the kernel that access to "
297 "mmap pages will be sequential",
298 DEFAULT_SEQUENTIAL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
299 GST_PARAM_MUTABLE_PLAYING));
301 gobject_class->finalize = gst_file_src_finalize;
303 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
304 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
305 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
306 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
307 gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_file_src_create);
308 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_file_src_query);
310 if (sizeof (off_t) < 8) {
311 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
317 gst_file_src_init (GstFileSrc * src, GstFileSrcClass * g_class)
320 src->pagesize = getpagesize ();
323 src->filename = NULL;
327 src->touch = DEFAULT_TOUCH;
330 src->mapsize = DEFAULT_MMAPSIZE; /* default is 4MB */
331 src->use_mmap = DEFAULT_USEMMAP;
332 src->sequential = DEFAULT_SEQUENTIAL;
334 src->is_regular = FALSE;
338 gst_file_src_finalize (GObject * object)
342 src = GST_FILE_SRC (object);
344 g_free (src->filename);
347 G_OBJECT_CLASS (parent_class)->finalize (object);
351 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
355 /* the element must be stopped in order to do this */
356 GST_OBJECT_LOCK (src);
357 state = GST_STATE (src);
358 if (state != GST_STATE_READY && state != GST_STATE_NULL)
360 GST_OBJECT_UNLOCK (src);
362 g_free (src->filename);
365 /* clear the filename if we get a NULL (is that possible?) */
366 if (location == NULL) {
367 src->filename = NULL;
370 /* we store the filename as received by the application. On Windows this
372 src->filename = g_strdup (location);
373 src->uri = gst_filename_to_uri (location, NULL);
374 GST_INFO ("filename : %s", src->filename);
375 GST_INFO ("uri : %s", src->uri);
377 g_object_notify (G_OBJECT (src), "location");
378 gst_uri_handler_new_uri (GST_URI_HANDLER (src), src->uri);
385 g_warning ("Changing the `location' property on filesrc when a file is "
386 "open is not supported.");
387 GST_OBJECT_UNLOCK (src);
393 gst_file_src_set_property (GObject * object, guint prop_id,
394 const GValue * value, GParamSpec * pspec)
398 g_return_if_fail (GST_IS_FILE_SRC (object));
400 src = GST_FILE_SRC (object);
404 gst_file_src_set_location (src, g_value_get_string (value));
407 if ((src->mapsize % src->pagesize) == 0) {
408 src->mapsize = g_value_get_ulong (value);
410 GST_INFO_OBJECT (src,
411 "invalid mapsize, must be a multiple of pagesize, which is %d",
416 src->touch = g_value_get_boolean (value);
419 src->sequential = g_value_get_boolean (value);
422 src->use_mmap = g_value_get_boolean (value);
425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
431 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
436 g_return_if_fail (GST_IS_FILE_SRC (object));
438 src = GST_FILE_SRC (object);
442 g_value_set_string (value, src->filename);
445 g_value_set_int (value, src->fd);
448 g_value_set_ulong (value, src->mapsize);
451 g_value_set_boolean (value, src->touch);
454 g_value_set_boolean (value, src->sequential);
457 g_value_set_boolean (value, src->use_mmap);
460 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
473 typedef struct _GstMmapBuffer GstMmapBuffer;
474 typedef struct _GstMmapBufferClass GstMmapBufferClass;
476 #define GST_TYPE_MMAP_BUFFER (gst_mmap_buffer_get_type())
478 #define GST_IS_MMAP_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MMAP_BUFFER))
479 #define GST_IS_MMAP_BUFFER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MMAP_BUFFER))
480 #define GST_MMAP_BUFFER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_MMAP_BUFFER, GstMmapBufferClass))
481 #define GST_MMAP_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MMAP_BUFFER, GstMmapBuffer))
482 #define GST_MMAP_BUFFER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MMAP_BUFFER, GstMmapBufferClass))
486 struct _GstMmapBuffer
493 struct _GstMmapBufferClass
495 GstBufferClass buffer_class;
498 static void gst_mmap_buffer_finalize (GstMmapBuffer * mmap_buffer);
500 GType gst_mmap_buffer_get_type (void);
502 G_DEFINE_TYPE (GstMmapBuffer, gst_mmap_buffer, GST_TYPE_BUFFER);
505 gst_mmap_buffer_class_init (GstMmapBufferClass * g_class)
507 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
509 mini_object_class->finalize =
510 (GstMiniObjectFinalizeFunction) gst_mmap_buffer_finalize;
514 gst_mmap_buffer_init (GstMmapBuffer * buf)
516 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_READONLY);
517 /* before we re-enable this flag, we probably need to fix _copy()
518 * _make_writable(), etc. in GstMiniObject/GstBuffer as well */
519 /* GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_ORIGINAL); */
523 gst_mmap_buffer_finalize (GstMmapBuffer * mmap_buffer)
529 GstBuffer *buffer = GST_BUFFER (mmap_buffer);
532 size = GST_BUFFER_SIZE (buffer);
533 offset = GST_BUFFER_OFFSET (buffer);
534 data = GST_BUFFER_DATA (buffer);
535 src = mmap_buffer->filesrc;
537 GST_LOG ("freeing mmap()d buffer at %" G_GUINT64_FORMAT "+%u", offset, size);
540 /* madvise to tell the kernel what to do with it */
541 if (madvise (data, size, MADV_DONTNEED) < 0) {
542 GST_WARNING_OBJECT (src, "warning: madvise failed: %s", g_strerror (errno));
546 /* now unmap the memory */
547 if (munmap (data, size) < 0) {
548 GST_WARNING_OBJECT (src, "warning: munmap failed: %s", g_strerror (errno));
551 /* cast to unsigned long, since there's no gportable way to print
553 GST_LOG ("unmapped region %08lx+%08lx at %p",
554 (gulong) offset, (gulong) size, data);
556 GST_MINI_OBJECT_CLASS (gst_mmap_buffer_parent_class)->finalize
557 (GST_MINI_OBJECT (mmap_buffer));
561 gst_file_src_map_region (GstFileSrc * src, off_t offset, gsize size,
567 g_return_val_if_fail (offset >= 0, NULL);
569 /* FIXME ? use goffset and friends if we require glib >= 2.20 */
570 GST_LOG_OBJECT (src, "mapping region %08" G_GINT64_MODIFIER "x+%08lx "
571 "from file into memory", (gint64) offset, (gulong) size);
573 mmapregion = mmap (NULL, size, PROT_READ, MAP_SHARED, src->fd, offset);
575 if (mmapregion == NULL || mmapregion == MAP_FAILED)
578 GST_LOG_OBJECT (src, "mapped region %08lx+%08lx from file into memory at %p",
579 (gulong) offset, (gulong) size, mmapregion);
581 /* time to allocate a new mapbuf */
582 buf = (GstBuffer *) gst_mini_object_new (GST_TYPE_MMAP_BUFFER);
583 /* mmap() the data into this new buffer */
584 GST_BUFFER_DATA (buf) = mmapregion;
585 GST_MMAP_BUFFER (buf)->filesrc = src;
587 #ifdef MADV_SEQUENTIAL
588 if (src->sequential) {
589 /* madvise to tell the kernel what to do with it */
590 if (madvise (mmapregion, size, MADV_SEQUENTIAL) < 0) {
591 GST_WARNING_OBJECT (src, "warning: madvise failed: %s",
597 /* fill in the rest of the fields */
598 GST_BUFFER_SIZE (buf) = size;
599 GST_BUFFER_OFFSET (buf) = offset;
600 GST_BUFFER_OFFSET_END (buf) = offset + size;
601 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
609 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
610 ("mmap (0x%08lx, %d, 0x%" G_GINT64_MODIFIER "x) failed: %s",
611 (gulong) size, src->fd, (guint64) offset, g_strerror (errno)));
618 gst_file_src_map_small_region (GstFileSrc * src, off_t offset, gsize size)
625 "attempting to map a small buffer at %" G_GUINT64_FORMAT "+%d",
626 (guint64) offset, (gint) size);
628 pagesize = src->pagesize;
630 mod = offset % pagesize;
632 /* if the offset starts at a non-page boundary, we have to special case */
638 mapbase = offset - mod;
639 mapsize = ((size + mod + pagesize - 1) / pagesize) * pagesize;
642 "not on page boundaries, resizing to map to %" G_GUINT64_FORMAT "+%d",
643 (guint64) mapbase, (gint) mapsize);
645 map = gst_file_src_map_region (src, mapbase, mapsize, FALSE);
649 ret = gst_buffer_create_sub (map, offset - mapbase, size);
650 GST_BUFFER_OFFSET (ret) = GST_BUFFER_OFFSET (map) + offset - mapbase;
652 gst_buffer_unref (map);
654 ret = gst_file_src_map_region (src, offset, size, FALSE);
661 gst_file_src_create_mmap (GstFileSrc * src, guint64 offset, guint length,
664 GstBuffer *buf = NULL;
665 gsize readsize, mapsize;
666 off_t readend, mapstart, mapend;
669 /* calculate end pointers so we don't have to do so repeatedly later */
671 readend = offset + readsize; /* note this is the byte *after* the read */
673 mapstart = GST_BUFFER_OFFSET (src->mapbuf);
674 mapsize = GST_BUFFER_SIZE (src->mapbuf);
675 mapend = mapstart + mapsize; /* note this is the byte *after* the map */
677 GST_LOG ("attempting to read %08lx, %08lx, %08lx, %08lx",
678 (unsigned long) readsize, (unsigned long) readend,
679 (unsigned long) mapstart, (unsigned long) mapend);
681 /* if the start is past the mapstart */
682 if (offset >= mapstart) {
683 /* if the end is before the mapend, the buffer is in current mmap region... */
684 /* ('cause by definition if readend is in the buffer, so's readstart) */
685 if (readend <= mapend) {
686 GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%u lives in "
687 "current mapbuf %u+%u, creating subbuffer of mapbuf",
688 offset, (guint) readsize, (guint) mapstart, (guint) mapsize);
689 buf = gst_buffer_create_sub (src->mapbuf, offset - mapstart, readsize);
690 GST_BUFFER_OFFSET (buf) = offset;
692 /* if the start actually is within the current mmap region, map an overlap buffer */
693 } else if (offset < mapend) {
694 GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%u starts in "
695 "mapbuf %u+%u but ends outside, creating new mmap",
696 offset, (guint) readsize, (guint) mapstart, (guint) mapsize);
697 buf = gst_file_src_map_small_region (src, offset, readsize);
702 /* the only other option is that buffer is totally outside, which means we search for it */
704 /* now we can assume that the start is *before* the current mmap region */
705 /* if the readend is past mapstart, we have two options */
706 } else if (readend >= mapstart) {
707 /* either the read buffer overlaps the start of the mmap region */
708 /* or the read buffer fully contains the current mmap region */
709 /* either way, it's really not relevant, we just create a new region anyway */
710 GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%d starts before "
711 "mapbuf %d+%d, but overlaps it", (guint64) offset, (gint) readsize,
712 (gint) mapstart, (gint) mapsize);
713 buf = gst_file_src_map_small_region (src, offset, readsize);
718 /* then deal with the case where the read buffer is totally outside */
720 /* first check to see if there's a map that covers the right region already */
721 GST_LOG_OBJECT (src, "searching for mapbuf to cover %" G_GUINT64_FORMAT
722 "+%d", offset, (int) readsize);
724 /* if the read buffer crosses a mmap region boundary, create a one-off region */
725 if ((offset / src->mapsize) != (readend / src->mapsize)) {
726 GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%d crosses a "
727 "%d-byte boundary, creating a one-off", offset, (int) readsize,
729 buf = gst_file_src_map_small_region (src, offset, readsize);
733 /* otherwise we will create a new mmap region and set it to the default */
737 off_t nextmap = offset - (offset % src->mapsize);
739 GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%d in new mapbuf "
740 "at %" G_GUINT64_FORMAT "+%d, mapping and subbuffering",
741 offset, (gint) readsize, (guint64) nextmap, (gint) src->mapsize);
742 /* first, we're done with the old mapbuf */
743 gst_buffer_unref (src->mapbuf);
744 mapsize = src->mapsize;
746 /* double the mapsize as long as the readsize is smaller */
747 while (readsize + offset > nextmap + mapsize) {
748 GST_LOG_OBJECT (src, "readsize smaller then mapsize %08x %d",
749 (guint) readsize, (gint) mapsize);
752 /* create a new one */
753 src->mapbuf = gst_file_src_map_region (src, nextmap, mapsize, FALSE);
754 if (src->mapbuf == NULL)
758 buf = gst_buffer_create_sub (src->mapbuf, offset - nextmap, readsize);
759 GST_BUFFER_OFFSET (buf) =
760 GST_BUFFER_OFFSET (src->mapbuf) + offset - nextmap;
764 /* if we need to touch the buffer (to bring it into memory), do so */
766 volatile guchar *p = GST_BUFFER_DATA (buf);
768 /* read first byte of each page */
769 for (i = 0; i < GST_BUFFER_SIZE (buf); i += src->pagesize)
773 /* we're done, return the buffer */
781 return GST_FLOW_ERROR;
788 * that is to say, you shouldn't read the code below, but the code that reads
789 * stuff is below. Well, you shouldn't not read the code below, feel free
790 * to read it of course. It's just that "read code below" is a pretty crappy
791 * documentation string because it sounds like we're expecting you to read
792 * the code to understand what it does, which, while true, is really not
793 * the sort of attitude we want to be advertising. No sir.
798 gst_file_src_create_read (GstFileSrc * src, guint64 offset, guint length,
803 guint to_read, bytes_read;
805 if (G_UNLIKELY (src->read_position != offset)) {
808 res = lseek (src->fd, offset, SEEK_SET);
809 if (G_UNLIKELY (res < 0 || res != offset))
812 src->read_position = offset;
815 buf = gst_buffer_try_new_and_alloc (length);
816 if (G_UNLIKELY (buf == NULL && length > 0)) {
817 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", length);
818 return GST_FLOW_ERROR;
821 /* No need to read anything if length is 0 */
822 GST_BUFFER_SIZE (buf) = 0;
823 GST_BUFFER_OFFSET (buf) = offset;
824 GST_BUFFER_OFFSET_END (buf) = offset;
827 while (to_read > 0) {
828 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
829 to_read, offset + bytes_read);
831 ret = read (src->fd, GST_BUFFER_DATA (buf) + bytes_read, to_read);
832 if (G_UNLIKELY (ret < 0)) {
833 if (errno == EAGAIN || errno == EINTR)
838 /* files should eos if they read 0 and more was requested */
839 if (G_UNLIKELY (ret == 0))
845 src->read_position += ret;
848 GST_BUFFER_SIZE (buf) = bytes_read;
849 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
858 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
859 return GST_FLOW_ERROR;
863 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
864 gst_buffer_unref (buf);
865 return GST_FLOW_ERROR;
870 gst_buffer_unref (buf);
871 return GST_FLOW_UNEXPECTED;
876 gst_file_src_create (GstBaseSrc * basesrc, guint64 offset, guint length,
882 src = GST_FILE_SRC_CAST (basesrc);
885 if (src->using_mmap) {
886 ret = gst_file_src_create_mmap (src, offset, length, buffer);
888 ret = gst_file_src_create_read (src, offset, length, buffer);
891 ret = gst_file_src_create_read (src, offset, length, buffer);
898 gst_file_src_query (GstBaseSrc * basesrc, GstQuery * query)
900 gboolean ret = FALSE;
901 GstFileSrc *src = GST_FILE_SRC (basesrc);
903 switch (GST_QUERY_TYPE (query)) {
905 gst_query_set_uri (query, src->uri);
914 ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
920 gst_file_src_is_seekable (GstBaseSrc * basesrc)
922 GstFileSrc *src = GST_FILE_SRC (basesrc);
924 return src->seekable;
928 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
930 struct stat stat_results;
933 src = GST_FILE_SRC (basesrc);
935 if (!src->seekable) {
936 /* If it isn't seekable, we won't know the length (but fstat will still
937 * succeed, and wrongly say our length is zero. */
941 if (fstat (src->fd, &stat_results) < 0)
944 *size = stat_results.st_size;
955 /* open the file and mmap it, necessary to go to READY state */
957 gst_file_src_start (GstBaseSrc * basesrc)
959 GstFileSrc *src = GST_FILE_SRC (basesrc);
960 struct stat stat_results;
962 if (src->filename == NULL || src->filename[0] == '\0')
965 GST_INFO_OBJECT (src, "opening file %s", src->filename);
968 src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
973 /* check if it is a regular file, otherwise bail out */
974 if (fstat (src->fd, &stat_results) < 0)
977 if (S_ISDIR (stat_results.st_mode))
980 if (S_ISSOCK (stat_results.st_mode))
983 src->using_mmap = FALSE;
984 src->read_position = 0;
986 /* record if it's a regular (hence seekable and lengthable) file */
987 if (S_ISREG (stat_results.st_mode))
988 src->is_regular = TRUE;
992 /* FIXME: maybe we should only try to mmap if it's a regular file */
993 /* allocate the first mmap'd region if it's a regular file ? */
994 src->mapbuf = gst_file_src_map_region (src, 0, src->mapsize, TRUE);
995 if (src->mapbuf != NULL) {
996 GST_DEBUG_OBJECT (src, "using mmap for file");
997 src->using_mmap = TRUE;
998 src->seekable = TRUE;
1001 if (src->mapbuf == NULL)
1004 /* If not in mmap mode, we need to check if the underlying file is
1006 off_t res = lseek (src->fd, 0, SEEK_END);
1009 GST_LOG_OBJECT (src, "disabling seeking, not in mmap mode and lseek "
1010 "failed: %s", g_strerror (errno));
1011 src->seekable = FALSE;
1013 src->seekable = TRUE;
1015 lseek (src->fd, 0, SEEK_SET);
1018 /* We can only really do seeking on regular files - for other file types, we
1019 * don't know their length, so seeking isn't useful/meaningful */
1020 src->seekable = src->seekable && src->is_regular;
1022 gst_base_src_set_dynamic_size (basesrc, src->seekable);
1029 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1030 (_("No file name specified for reading.")), (NULL));
1037 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
1038 ("No such file \"%s\"", src->filename));
1041 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1042 (_("Could not open file \"%s\" for reading."), src->filename),
1050 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1051 (_("Could not get info on \"%s\"."), src->filename), (NULL));
1057 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1058 (_("\"%s\" is a directory."), src->filename), (NULL));
1064 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1065 (_("File \"%s\" is a socket."), src->filename), (NULL));
1071 /* unmap and close the file */
1073 gst_file_src_stop (GstBaseSrc * basesrc)
1075 GstFileSrc *src = GST_FILE_SRC (basesrc);
1077 /* close the file */
1080 /* zero out a lot of our state */
1082 src->is_regular = FALSE;
1085 gst_buffer_unref (src->mapbuf);
1092 /*** GSTURIHANDLER INTERFACE *************************************************/
1095 gst_file_src_uri_get_type (void)
1101 gst_file_src_uri_get_protocols (void)
1103 static gchar *protocols[] = { (char *) "file", NULL };
1108 static const gchar *
1109 gst_file_src_uri_get_uri (GstURIHandler * handler)
1111 GstFileSrc *src = GST_FILE_SRC (handler);
1117 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1119 gchar *location, *hostname = NULL;
1120 gboolean ret = FALSE;
1121 GstFileSrc *src = GST_FILE_SRC (handler);
1122 GError *error = NULL;
1124 if (strcmp (uri, "file://") == 0) {
1125 /* Special case for "file://" as this is used by some applications
1126 * to test with gst_element_make_from_uri if there's an element
1127 * that supports the URI protocol. */
1128 gst_file_src_set_location (src, NULL);
1132 location = g_filename_from_uri (uri, &hostname, &error);
1134 if (!location || error) {
1136 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
1138 g_error_free (error);
1140 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc", uri);
1145 if ((hostname) && (strcmp (hostname, "localhost"))) {
1146 /* Only 'localhost' is permitted */
1147 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
1151 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
1152 * correctly on windows, it leaves them with an extra backslash
1153 * at the start if they're of the mozilla-style file://///host/path/file
1154 * form. Correct this.
1156 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
1157 g_memmove (location, location + 1, strlen (location + 1) + 1);
1160 ret = gst_file_src_set_location (src, location);
1172 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1174 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1176 iface->get_type = gst_file_src_uri_get_type;
1177 iface->get_protocols = gst_file_src_uri_get_protocols;
1178 iface->get_uri = gst_file_src_uri_get_uri;
1179 iface->set_uri = gst_file_src_uri_set_uri;