filesrc: set default block size from local define
[platform/upstream/gstreamer.git] / plugins / elements / gstfilesrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000,2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstfilesrc.c:
6  *
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.
11  *
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.
16  *
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.
21  */
22 /**
23  * SECTION:element-filesrc
24  * @see_also: #GstFileSrc
25  *
26  * Read data from a file in the local file system.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch filesrc location=song.ogg ! decodebin2 ! autoaudiosink
32  * ]| Play a song.ogg from local dir.
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39
40 #include <gst/gst.h>
41 #include "gstfilesrc.h"
42
43 #include <stdio.h>
44 #include <sys/types.h>
45 #ifdef G_OS_WIN32
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. */
49 #define stat __stat64
50 #define fstat _fstat64
51 #undef lseek
52 #define lseek _lseeki64
53 #undef off_t
54 #define off_t guint64
55 /* Prevent stat.h from defining the stat* functions as
56  * _stat*, since we're explicitly overriding that */
57 #undef _INC_STAT_INL
58 #endif
59 #include <sys/stat.h>
60 #include <fcntl.h>
61
62 #ifdef HAVE_UNISTD_H
63 #  include <unistd.h>
64 #endif
65
66 #ifdef HAVE_MMAP
67 # include <sys/mman.h>
68 #endif
69
70 #include <errno.h>
71 #include <string.h>
72
73 #include "../../gst/gst-i18n-lib.h"
74
75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
76     GST_PAD_SRC,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS_ANY);
79
80 /* FIXME we should be using glib for this */
81 #ifndef S_ISREG
82 #define S_ISREG(mode) ((mode)&_S_IFREG)
83 #endif
84 #ifndef S_ISDIR
85 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
86 #endif
87 #ifndef S_ISSOCK
88 #define S_ISSOCK(x) (0)
89 #endif
90 #ifndef O_BINARY
91 #define O_BINARY (0)
92 #endif
93
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. */
97 static int
98 gst_open (const gchar * filename, int flags, int mode)
99 {
100 #ifdef G_OS_WIN32
101   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
102   int retval;
103   int save_errno;
104
105   if (wfilename == NULL) {
106     errno = EINVAL;
107     return -1;
108   }
109
110   retval = _wopen (wfilename, flags, mode);
111   save_errno = errno;
112
113   g_free (wfilename);
114
115   errno = save_errno;
116   return retval;
117 #else
118   return open (filename, flags, mode);
119 #endif
120 }
121
122
123 /**********************************************************************
124  * GStreamer Default File Source
125  * Theory of Operation
126  *
127  * Update: see GstFileSrc:use-mmap property documentation below
128  *         for why use of mmap() is disabled by default.
129  *
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.
135  *
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.
141  *
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*
149  * overlap.
150  *
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.
159  */
160
161
162 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
163 #define GST_CAT_DEFAULT gst_file_src_debug
164
165 /* FileSrc signals and args */
166 enum
167 {
168   /* FILL ME */
169   LAST_SIGNAL
170 };
171
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
177
178 enum
179 {
180   ARG_0,
181   ARG_LOCATION,
182   ARG_FD,
183   ARG_MMAPSIZE,
184   ARG_SEQUENTIAL,
185   ARG_TOUCH,
186   ARG_USEMMAP
187 };
188
189 static void gst_file_src_finalize (GObject * object);
190
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);
195
196 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
197 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
198
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);
204
205 static void gst_file_src_uri_handler_init (gpointer g_iface,
206     gpointer iface_data);
207
208 static void
209 _do_init (GType filesrc_type)
210 {
211   static const GInterfaceInfo urihandler_info = {
212     gst_file_src_uri_handler_init,
213     NULL,
214     NULL
215   };
216
217   g_type_add_interface_static (filesrc_type, GST_TYPE_URI_HANDLER,
218       &urihandler_info);
219   GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
220 }
221
222 GST_BOILERPLATE_FULL (GstFileSrc, gst_file_src, GstBaseSrc, GST_TYPE_BASE_SRC,
223     _do_init);
224
225 static void
226 gst_file_src_base_init (gpointer g_class)
227 {
228   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
229
230   gst_element_class_set_details_simple (gstelement_class,
231       "File Source",
232       "Source/File",
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));
237 }
238
239 static void
240 gst_file_src_class_init (GstFileSrcClass * klass)
241 {
242   GObjectClass *gobject_class;
243   GstBaseSrcClass *gstbasesrc_class;
244
245   gobject_class = G_OBJECT_CLASS (klass);
246   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
247
248   gobject_class->set_property = gst_file_src_set_property;
249   gobject_class->get_property = gst_file_src_get_property;
250
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",
268           DEFAULT_TOUCH,
269           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
270           GST_PARAM_MUTABLE_PLAYING));
271   /**
272    * GstFileSrc:use-mmap
273    *
274    * Whether to use mmap(). Set to TRUE to force use of mmap() instead of
275    * read() for reading data.
276    *
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
286    * otherwise damaged.
287    *
288    **/
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));
300
301   gobject_class->finalize = gst_file_src_finalize;
302
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);
309
310   if (sizeof (off_t) < 8) {
311     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
312         sizeof (off_t));
313   }
314 }
315
316 static void
317 gst_file_src_init (GstFileSrc * src, GstFileSrcClass * g_class)
318 {
319 #ifdef HAVE_MMAP
320   src->pagesize = getpagesize ();
321 #endif
322
323   src->filename = NULL;
324   src->fd = 0;
325   src->uri = NULL;
326
327   src->touch = DEFAULT_TOUCH;
328
329   src->mapbuf = NULL;
330   src->mapsize = DEFAULT_MMAPSIZE;      /* default is 4MB */
331   src->use_mmap = DEFAULT_USEMMAP;
332   src->sequential = DEFAULT_SEQUENTIAL;
333
334   src->is_regular = FALSE;
335
336   gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
337 }
338
339 static void
340 gst_file_src_finalize (GObject * object)
341 {
342   GstFileSrc *src;
343
344   src = GST_FILE_SRC (object);
345
346   g_free (src->filename);
347   g_free (src->uri);
348
349   G_OBJECT_CLASS (parent_class)->finalize (object);
350 }
351
352 static gboolean
353 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
354 {
355   GstState state;
356
357   /* the element must be stopped in order to do this */
358   GST_OBJECT_LOCK (src);
359   state = GST_STATE (src);
360   if (state != GST_STATE_READY && state != GST_STATE_NULL)
361     goto wrong_state;
362   GST_OBJECT_UNLOCK (src);
363
364   g_free (src->filename);
365   g_free (src->uri);
366
367   /* clear the filename if we get a NULL (is that possible?) */
368   if (location == NULL) {
369     src->filename = NULL;
370     src->uri = NULL;
371   } else {
372     /* we store the filename as received by the application. On Windows this
373      * should be UTF8 */
374     src->filename = g_strdup (location);
375     src->uri = gst_filename_to_uri (location, NULL);
376     GST_INFO ("filename : %s", src->filename);
377     GST_INFO ("uri      : %s", src->uri);
378   }
379   g_object_notify (G_OBJECT (src), "location");
380   gst_uri_handler_new_uri (GST_URI_HANDLER (src), src->uri);
381
382   return TRUE;
383
384   /* ERROR */
385 wrong_state:
386   {
387     g_warning ("Changing the `location' property on filesrc when a file is "
388         "open is not supported.");
389     GST_OBJECT_UNLOCK (src);
390     return FALSE;
391   }
392 }
393
394 static void
395 gst_file_src_set_property (GObject * object, guint prop_id,
396     const GValue * value, GParamSpec * pspec)
397 {
398   GstFileSrc *src;
399
400   g_return_if_fail (GST_IS_FILE_SRC (object));
401
402   src = GST_FILE_SRC (object);
403
404   switch (prop_id) {
405     case ARG_LOCATION:
406       gst_file_src_set_location (src, g_value_get_string (value));
407       break;
408     case ARG_MMAPSIZE:
409       if ((src->mapsize % src->pagesize) == 0) {
410         src->mapsize = g_value_get_ulong (value);
411       } else {
412         GST_INFO_OBJECT (src,
413             "invalid mapsize, must be a multiple of pagesize, which is %d",
414             src->pagesize);
415       }
416       break;
417     case ARG_TOUCH:
418       src->touch = g_value_get_boolean (value);
419       break;
420     case ARG_SEQUENTIAL:
421       src->sequential = g_value_get_boolean (value);
422       break;
423     case ARG_USEMMAP:
424       src->use_mmap = g_value_get_boolean (value);
425       break;
426     default:
427       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
428       break;
429   }
430 }
431
432 static void
433 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
434     GParamSpec * pspec)
435 {
436   GstFileSrc *src;
437
438   g_return_if_fail (GST_IS_FILE_SRC (object));
439
440   src = GST_FILE_SRC (object);
441
442   switch (prop_id) {
443     case ARG_LOCATION:
444       g_value_set_string (value, src->filename);
445       break;
446     case ARG_FD:
447       g_value_set_int (value, src->fd);
448       break;
449     case ARG_MMAPSIZE:
450       g_value_set_ulong (value, src->mapsize);
451       break;
452     case ARG_TOUCH:
453       g_value_set_boolean (value, src->touch);
454       break;
455     case ARG_SEQUENTIAL:
456       g_value_set_boolean (value, src->sequential);
457       break;
458     case ARG_USEMMAP:
459       g_value_set_boolean (value, src->use_mmap);
460       break;
461     default:
462       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
463       break;
464   }
465 }
466
467 /***
468  * mmap code below
469  */
470
471 #ifdef HAVE_MMAP
472
473 /* GstMmapBuffer */
474
475 typedef struct _GstMmapBuffer GstMmapBuffer;
476 typedef struct _GstMmapBufferClass GstMmapBufferClass;
477
478 #define GST_TYPE_MMAP_BUFFER                         (gst_mmap_buffer_get_type())
479
480 #define GST_IS_MMAP_BUFFER(obj)  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MMAP_BUFFER))
481 #define GST_IS_MMAP_BUFFER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MMAP_BUFFER))
482 #define GST_MMAP_BUFFER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_MMAP_BUFFER, GstMmapBufferClass))
483 #define GST_MMAP_BUFFER(obj)     (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MMAP_BUFFER, GstMmapBuffer))
484 #define GST_MMAP_BUFFER_CLASS(klass)  (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MMAP_BUFFER, GstMmapBufferClass))
485
486
487
488 struct _GstMmapBuffer
489 {
490   GstBuffer buffer;
491
492   GstFileSrc *filesrc;
493 };
494
495 struct _GstMmapBufferClass
496 {
497   GstBufferClass buffer_class;
498 };
499
500 static void gst_mmap_buffer_finalize (GstMmapBuffer * mmap_buffer);
501
502 GType gst_mmap_buffer_get_type (void);
503
504 G_DEFINE_TYPE (GstMmapBuffer, gst_mmap_buffer, GST_TYPE_BUFFER);
505
506 static void
507 gst_mmap_buffer_class_init (GstMmapBufferClass * g_class)
508 {
509   GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
510
511   mini_object_class->finalize =
512       (GstMiniObjectFinalizeFunction) gst_mmap_buffer_finalize;
513 }
514
515 static void
516 gst_mmap_buffer_init (GstMmapBuffer * buf)
517 {
518   GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_READONLY);
519   /* before we re-enable this flag, we probably need to fix _copy()
520    * _make_writable(), etc. in GstMiniObject/GstBuffer as well */
521   /* GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_ORIGINAL); */
522 }
523
524 static void
525 gst_mmap_buffer_finalize (GstMmapBuffer * mmap_buffer)
526 {
527   guint size;
528   gpointer data;
529   guint64 offset;
530   GstFileSrc *src;
531   GstBuffer *buffer = GST_BUFFER (mmap_buffer);
532
533   /* get info */
534   size = GST_BUFFER_SIZE (buffer);
535   offset = GST_BUFFER_OFFSET (buffer);
536   data = GST_BUFFER_DATA (buffer);
537   src = mmap_buffer->filesrc;
538
539   GST_LOG ("freeing mmap()d buffer at %" G_GUINT64_FORMAT "+%u", offset, size);
540
541 #ifdef MADV_DONTNEED
542   /* madvise to tell the kernel what to do with it */
543   if (madvise (data, size, MADV_DONTNEED) < 0) {
544     GST_WARNING_OBJECT (src, "warning: madvise failed: %s", g_strerror (errno));
545   }
546 #endif
547
548   /* now unmap the memory */
549   if (munmap (data, size) < 0) {
550     GST_WARNING_OBJECT (src, "warning: munmap failed: %s", g_strerror (errno));
551   }
552
553   /* cast to unsigned long, since there's no gportable way to print
554    * guint64 as hex */
555   GST_LOG ("unmapped region %08lx+%08lx at %p",
556       (gulong) offset, (gulong) size, data);
557
558   GST_MINI_OBJECT_CLASS (gst_mmap_buffer_parent_class)->finalize
559       (GST_MINI_OBJECT (mmap_buffer));
560 }
561
562 static GstBuffer *
563 gst_file_src_map_region (GstFileSrc * src, off_t offset, gsize size,
564     gboolean testonly)
565 {
566   GstBuffer *buf;
567   void *mmapregion;
568
569   g_return_val_if_fail (offset >= 0, NULL);
570
571   /* FIXME ? use goffset and friends if we require glib >= 2.20 */
572   GST_LOG_OBJECT (src, "mapping region %08" G_GINT64_MODIFIER "x+%08lx "
573       "from file into memory", (gint64) offset, (gulong) size);
574
575   mmapregion = mmap (NULL, size, PROT_READ, MAP_SHARED, src->fd, offset);
576
577   if (mmapregion == NULL || mmapregion == MAP_FAILED)
578     goto mmap_failed;
579
580   GST_LOG_OBJECT (src, "mapped region %08lx+%08lx from file into memory at %p",
581       (gulong) offset, (gulong) size, mmapregion);
582
583   /* time to allocate a new mapbuf */
584   buf = (GstBuffer *) gst_mini_object_new (GST_TYPE_MMAP_BUFFER);
585   /* mmap() the data into this new buffer */
586   GST_BUFFER_DATA (buf) = mmapregion;
587   GST_MMAP_BUFFER (buf)->filesrc = src;
588
589 #ifdef MADV_SEQUENTIAL
590   if (src->sequential) {
591     /* madvise to tell the kernel what to do with it */
592     if (madvise (mmapregion, size, MADV_SEQUENTIAL) < 0) {
593       GST_WARNING_OBJECT (src, "warning: madvise failed: %s",
594           g_strerror (errno));
595     }
596   }
597 #endif
598
599   /* fill in the rest of the fields */
600   GST_BUFFER_SIZE (buf) = size;
601   GST_BUFFER_OFFSET (buf) = offset;
602   GST_BUFFER_OFFSET_END (buf) = offset + size;
603   GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
604
605   return buf;
606
607   /* ERROR */
608 mmap_failed:
609   {
610     if (!testonly) {
611       GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
612           ("mmap (0x%08lx, %d, 0x%" G_GINT64_MODIFIER "x) failed: %s",
613               (gulong) size, src->fd, (guint64) offset, g_strerror (errno)));
614     }
615     return NULL;
616   }
617 }
618
619 static GstBuffer *
620 gst_file_src_map_small_region (GstFileSrc * src, off_t offset, gsize size)
621 {
622   GstBuffer *ret;
623   off_t mod;
624   guint pagesize;
625
626   GST_LOG_OBJECT (src,
627       "attempting to map a small buffer at %" G_GUINT64_FORMAT "+%d",
628       (guint64) offset, (gint) size);
629
630   pagesize = src->pagesize;
631
632   mod = offset % pagesize;
633
634   /* if the offset starts at a non-page boundary, we have to special case */
635   if (mod != 0) {
636     gsize mapsize;
637     off_t mapbase;
638     GstBuffer *map;
639
640     mapbase = offset - mod;
641     mapsize = ((size + mod + pagesize - 1) / pagesize) * pagesize;
642
643     GST_LOG_OBJECT (src,
644         "not on page boundaries, resizing to map to %" G_GUINT64_FORMAT "+%d",
645         (guint64) mapbase, (gint) mapsize);
646
647     map = gst_file_src_map_region (src, mapbase, mapsize, FALSE);
648     if (map == NULL)
649       return NULL;
650
651     ret = gst_buffer_create_sub (map, offset - mapbase, size);
652     GST_BUFFER_OFFSET (ret) = GST_BUFFER_OFFSET (map) + offset - mapbase;
653
654     gst_buffer_unref (map);
655   } else {
656     ret = gst_file_src_map_region (src, offset, size, FALSE);
657   }
658
659   return ret;
660 }
661
662 static GstFlowReturn
663 gst_file_src_create_mmap (GstFileSrc * src, guint64 offset, guint length,
664     GstBuffer ** buffer)
665 {
666   GstBuffer *buf = NULL;
667   gsize readsize, mapsize;
668   off_t readend, mapstart, mapend;
669   int i;
670
671   /* calculate end pointers so we don't have to do so repeatedly later */
672   readsize = length;
673   readend = offset + readsize;  /* note this is the byte *after* the read */
674
675   mapstart = GST_BUFFER_OFFSET (src->mapbuf);
676   mapsize = GST_BUFFER_SIZE (src->mapbuf);
677   mapend = mapstart + mapsize;  /* note this is the byte *after* the map */
678
679   GST_LOG ("attempting to read %08lx, %08lx, %08lx, %08lx",
680       (unsigned long) readsize, (unsigned long) readend,
681       (unsigned long) mapstart, (unsigned long) mapend);
682
683   /* if the start is past the mapstart */
684   if (offset >= mapstart) {
685     /* if the end is before the mapend, the buffer is in current mmap region... */
686     /* ('cause by definition if readend is in the buffer, so's readstart) */
687     if (readend <= mapend) {
688       GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%u lives in "
689           "current mapbuf %u+%u, creating subbuffer of mapbuf",
690           offset, (guint) readsize, (guint) mapstart, (guint) mapsize);
691       buf = gst_buffer_create_sub (src->mapbuf, offset - mapstart, readsize);
692       GST_BUFFER_OFFSET (buf) = offset;
693
694       /* if the start actually is within the current mmap region, map an overlap buffer */
695     } else if (offset < mapend) {
696       GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%u starts in "
697           "mapbuf %u+%u but ends outside, creating new mmap",
698           offset, (guint) readsize, (guint) mapstart, (guint) mapsize);
699       buf = gst_file_src_map_small_region (src, offset, readsize);
700       if (buf == NULL)
701         goto could_not_mmap;
702     }
703
704     /* the only other option is that buffer is totally outside, which means we search for it */
705
706     /* now we can assume that the start is *before* the current mmap region */
707     /* if the readend is past mapstart, we have two options */
708   } else if (readend >= mapstart) {
709     /* either the read buffer overlaps the start of the mmap region */
710     /* or the read buffer fully contains the current mmap region    */
711     /* either way, it's really not relevant, we just create a new region anyway */
712     GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%d starts before "
713         "mapbuf %d+%d, but overlaps it", (guint64) offset, (gint) readsize,
714         (gint) mapstart, (gint) mapsize);
715     buf = gst_file_src_map_small_region (src, offset, readsize);
716     if (buf == NULL)
717       goto could_not_mmap;
718   }
719
720   /* then deal with the case where the read buffer is totally outside */
721   if (buf == NULL) {
722     /* first check to see if there's a map that covers the right region already */
723     GST_LOG_OBJECT (src, "searching for mapbuf to cover %" G_GUINT64_FORMAT
724         "+%d", offset, (int) readsize);
725
726     /* if the read buffer crosses a mmap region boundary, create a one-off region */
727     if ((offset / src->mapsize) != (readend / src->mapsize)) {
728       GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%d crosses a "
729           "%d-byte boundary, creating a one-off", offset, (int) readsize,
730           (int) src->mapsize);
731       buf = gst_file_src_map_small_region (src, offset, readsize);
732       if (buf == NULL)
733         goto could_not_mmap;
734
735       /* otherwise we will create a new mmap region and set it to the default */
736     } else {
737       gsize mapsize;
738
739       off_t nextmap = offset - (offset % src->mapsize);
740
741       GST_LOG_OBJECT (src, "read buf %" G_GUINT64_FORMAT "+%d in new mapbuf "
742           "at %" G_GUINT64_FORMAT "+%d, mapping and subbuffering",
743           offset, (gint) readsize, (guint64) nextmap, (gint) src->mapsize);
744       /* first, we're done with the old mapbuf */
745       gst_buffer_unref (src->mapbuf);
746       mapsize = src->mapsize;
747
748       /* double the mapsize as long as the readsize is smaller */
749       while (readsize + offset > nextmap + mapsize) {
750         GST_LOG_OBJECT (src, "readsize smaller then mapsize %08x %d",
751             (guint) readsize, (gint) mapsize);
752         mapsize <<= 1;
753       }
754       /* create a new one */
755       src->mapbuf = gst_file_src_map_region (src, nextmap, mapsize, FALSE);
756       if (src->mapbuf == NULL)
757         goto could_not_mmap;
758
759       /* subbuffer it */
760       buf = gst_buffer_create_sub (src->mapbuf, offset - nextmap, readsize);
761       GST_BUFFER_OFFSET (buf) =
762           GST_BUFFER_OFFSET (src->mapbuf) + offset - nextmap;
763     }
764   }
765
766   /* if we need to touch the buffer (to bring it into memory), do so */
767   if (src->touch) {
768     volatile guchar *p = GST_BUFFER_DATA (buf);
769
770     /* read first byte of each page */
771     for (i = 0; i < GST_BUFFER_SIZE (buf); i += src->pagesize)
772       (void) p[i];
773   }
774
775   /* we're done, return the buffer */
776   *buffer = buf;
777
778   return GST_FLOW_OK;
779
780   /* ERROR */
781 could_not_mmap:
782   {
783     return GST_FLOW_ERROR;
784   }
785 }
786 #endif
787
788 /***
789  * read code below
790  * that is to say, you shouldn't read the code below, but the code that reads
791  * stuff is below.  Well, you shouldn't not read the code below, feel free
792  * to read it of course.  It's just that "read code below" is a pretty crappy
793  * documentation string because it sounds like we're expecting you to read
794  * the code to understand what it does, which, while true, is really not
795  * the sort of attitude we want to be advertising.  No sir.
796  *
797  */
798
799 static GstFlowReturn
800 gst_file_src_create_read (GstFileSrc * src, guint64 offset, guint length,
801     GstBuffer ** buffer)
802 {
803   int ret;
804   GstBuffer *buf;
805   guint to_read, bytes_read;
806
807   if (G_UNLIKELY (src->read_position != offset)) {
808     off_t res;
809
810     res = lseek (src->fd, offset, SEEK_SET);
811     if (G_UNLIKELY (res < 0 || res != offset))
812       goto seek_failed;
813
814     src->read_position = offset;
815   }
816
817   buf = gst_buffer_try_new_and_alloc (length);
818   if (G_UNLIKELY (buf == NULL && length > 0)) {
819     GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", length);
820     return GST_FLOW_ERROR;
821   }
822
823   /* No need to read anything if length is 0 */
824   GST_BUFFER_SIZE (buf) = 0;
825   GST_BUFFER_OFFSET (buf) = offset;
826   GST_BUFFER_OFFSET_END (buf) = offset;
827   bytes_read = 0;
828   to_read = length;
829   while (to_read > 0) {
830     GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
831         to_read, offset + bytes_read);
832     errno = 0;
833     ret = read (src->fd, GST_BUFFER_DATA (buf) + bytes_read, to_read);
834     if (G_UNLIKELY (ret < 0)) {
835       if (errno == EAGAIN || errno == EINTR)
836         continue;
837       goto could_not_read;
838     }
839
840     /* files should eos if they read 0 and more was requested */
841     if (G_UNLIKELY (ret == 0)) {
842       /* .. but first we should return any remaining data */
843       if (bytes_read > 0)
844         break;
845       goto eos;
846     }
847
848     to_read -= ret;
849     bytes_read += ret;
850
851     src->read_position += ret;
852   }
853
854   GST_BUFFER_SIZE (buf) = bytes_read;
855   GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
856
857   *buffer = buf;
858
859   return GST_FLOW_OK;
860
861   /* ERROR */
862 seek_failed:
863   {
864     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
865     return GST_FLOW_ERROR;
866   }
867 could_not_read:
868   {
869     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
870     gst_buffer_unref (buf);
871     return GST_FLOW_ERROR;
872   }
873 eos:
874   {
875     GST_DEBUG ("EOS");
876     gst_buffer_unref (buf);
877     return GST_FLOW_UNEXPECTED;
878   }
879 }
880
881 static GstFlowReturn
882 gst_file_src_create (GstBaseSrc * basesrc, guint64 offset, guint length,
883     GstBuffer ** buffer)
884 {
885   GstFileSrc *src;
886   GstFlowReturn ret;
887
888   src = GST_FILE_SRC_CAST (basesrc);
889
890 #ifdef HAVE_MMAP
891   if (src->using_mmap) {
892     ret = gst_file_src_create_mmap (src, offset, length, buffer);
893   } else {
894     ret = gst_file_src_create_read (src, offset, length, buffer);
895   }
896 #else
897   ret = gst_file_src_create_read (src, offset, length, buffer);
898 #endif
899
900   return ret;
901 }
902
903 static gboolean
904 gst_file_src_query (GstBaseSrc * basesrc, GstQuery * query)
905 {
906   gboolean ret = FALSE;
907   GstFileSrc *src = GST_FILE_SRC (basesrc);
908
909   switch (GST_QUERY_TYPE (query)) {
910     case GST_QUERY_URI:
911       gst_query_set_uri (query, src->uri);
912       ret = TRUE;
913       break;
914     default:
915       ret = FALSE;
916       break;
917   }
918
919   if (!ret)
920     ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
921
922   return ret;
923 }
924
925 static gboolean
926 gst_file_src_is_seekable (GstBaseSrc * basesrc)
927 {
928   GstFileSrc *src = GST_FILE_SRC (basesrc);
929
930   return src->seekable;
931 }
932
933 static gboolean
934 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
935 {
936   struct stat stat_results;
937   GstFileSrc *src;
938
939   src = GST_FILE_SRC (basesrc);
940
941   if (!src->seekable) {
942     /* If it isn't seekable, we won't know the length (but fstat will still
943      * succeed, and wrongly say our length is zero. */
944     return FALSE;
945   }
946
947   if (fstat (src->fd, &stat_results) < 0)
948     goto could_not_stat;
949
950   *size = stat_results.st_size;
951
952   return TRUE;
953
954   /* ERROR */
955 could_not_stat:
956   {
957     return FALSE;
958   }
959 }
960
961 /* open the file and mmap it, necessary to go to READY state */
962 static gboolean
963 gst_file_src_start (GstBaseSrc * basesrc)
964 {
965   GstFileSrc *src = GST_FILE_SRC (basesrc);
966   struct stat stat_results;
967
968   if (src->filename == NULL || src->filename[0] == '\0')
969     goto no_filename;
970
971   GST_INFO_OBJECT (src, "opening file %s", src->filename);
972
973   /* open the file */
974   src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
975
976   if (src->fd < 0)
977     goto open_failed;
978
979   /* check if it is a regular file, otherwise bail out */
980   if (fstat (src->fd, &stat_results) < 0)
981     goto no_stat;
982
983   if (S_ISDIR (stat_results.st_mode))
984     goto was_directory;
985
986   if (S_ISSOCK (stat_results.st_mode))
987     goto was_socket;
988
989   src->using_mmap = FALSE;
990   src->read_position = 0;
991
992   /* record if it's a regular (hence seekable and lengthable) file */
993   if (S_ISREG (stat_results.st_mode))
994     src->is_regular = TRUE;
995
996 #ifdef HAVE_MMAP
997   if (src->use_mmap) {
998     /* FIXME: maybe we should only try to mmap if it's a regular file */
999     /* allocate the first mmap'd region if it's a regular file ? */
1000     src->mapbuf = gst_file_src_map_region (src, 0, src->mapsize, TRUE);
1001     if (src->mapbuf != NULL) {
1002       GST_DEBUG_OBJECT (src, "using mmap for file");
1003       src->using_mmap = TRUE;
1004       src->seekable = TRUE;
1005     }
1006   }
1007   if (src->mapbuf == NULL)
1008 #endif
1009   {
1010     /* If not in mmap mode, we need to check if the underlying file is
1011      * seekable. */
1012     off_t res = lseek (src->fd, 0, SEEK_END);
1013
1014     if (res < 0) {
1015       GST_LOG_OBJECT (src, "disabling seeking, not in mmap mode and lseek "
1016           "failed: %s", g_strerror (errno));
1017       src->seekable = FALSE;
1018     } else {
1019       src->seekable = TRUE;
1020     }
1021     lseek (src->fd, 0, SEEK_SET);
1022   }
1023
1024   /* We can only really do seeking on regular files - for other file types, we
1025    * don't know their length, so seeking isn't useful/meaningful */
1026   src->seekable = src->seekable && src->is_regular;
1027
1028   gst_base_src_set_dynamic_size (basesrc, src->seekable);
1029
1030   return TRUE;
1031
1032   /* ERROR */
1033 no_filename:
1034   {
1035     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1036         (_("No file name specified for reading.")), (NULL));
1037     return FALSE;
1038   }
1039 open_failed:
1040   {
1041     switch (errno) {
1042       case ENOENT:
1043         GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
1044             ("No such file \"%s\"", src->filename));
1045         break;
1046       default:
1047         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1048             (_("Could not open file \"%s\" for reading."), src->filename),
1049             GST_ERROR_SYSTEM);
1050         break;
1051     }
1052     return FALSE;
1053   }
1054 no_stat:
1055   {
1056     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1057         (_("Could not get info on \"%s\"."), src->filename), (NULL));
1058     close (src->fd);
1059     return FALSE;
1060   }
1061 was_directory:
1062   {
1063     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1064         (_("\"%s\" is a directory."), src->filename), (NULL));
1065     close (src->fd);
1066     return FALSE;
1067   }
1068 was_socket:
1069   {
1070     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1071         (_("File \"%s\" is a socket."), src->filename), (NULL));
1072     close (src->fd);
1073     return FALSE;
1074   }
1075 }
1076
1077 /* unmap and close the file */
1078 static gboolean
1079 gst_file_src_stop (GstBaseSrc * basesrc)
1080 {
1081   GstFileSrc *src = GST_FILE_SRC (basesrc);
1082
1083   /* close the file */
1084   close (src->fd);
1085
1086   /* zero out a lot of our state */
1087   src->fd = 0;
1088   src->is_regular = FALSE;
1089
1090   if (src->mapbuf) {
1091     gst_buffer_unref (src->mapbuf);
1092     src->mapbuf = NULL;
1093   }
1094
1095   return TRUE;
1096 }
1097
1098 /*** GSTURIHANDLER INTERFACE *************************************************/
1099
1100 static GstURIType
1101 gst_file_src_uri_get_type (void)
1102 {
1103   return GST_URI_SRC;
1104 }
1105
1106 static gchar **
1107 gst_file_src_uri_get_protocols (void)
1108 {
1109   static gchar *protocols[] = { (char *) "file", NULL };
1110
1111   return protocols;
1112 }
1113
1114 static const gchar *
1115 gst_file_src_uri_get_uri (GstURIHandler * handler)
1116 {
1117   GstFileSrc *src = GST_FILE_SRC (handler);
1118
1119   return src->uri;
1120 }
1121
1122 static gboolean
1123 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1124 {
1125   gchar *location, *hostname = NULL;
1126   gboolean ret = FALSE;
1127   GstFileSrc *src = GST_FILE_SRC (handler);
1128   GError *error = NULL;
1129
1130   if (strcmp (uri, "file://") == 0) {
1131     /* Special case for "file://" as this is used by some applications
1132      *  to test with gst_element_make_from_uri if there's an element
1133      *  that supports the URI protocol. */
1134     gst_file_src_set_location (src, NULL);
1135     return TRUE;
1136   }
1137
1138   location = g_filename_from_uri (uri, &hostname, &error);
1139
1140   if (!location || error) {
1141     if (error) {
1142       GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
1143           error->message);
1144       g_error_free (error);
1145     } else {
1146       GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc", uri);
1147     }
1148     goto beach;
1149   }
1150
1151   if ((hostname) && (strcmp (hostname, "localhost"))) {
1152     /* Only 'localhost' is permitted */
1153     GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
1154     goto beach;
1155   }
1156 #ifdef G_OS_WIN32
1157   /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
1158    * correctly on windows, it leaves them with an extra backslash
1159    * at the start if they're of the mozilla-style file://///host/path/file 
1160    * form. Correct this.
1161    */
1162   if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
1163     g_memmove (location, location + 1, strlen (location + 1) + 1);
1164 #endif
1165
1166   ret = gst_file_src_set_location (src, location);
1167
1168 beach:
1169   if (location)
1170     g_free (location);
1171   if (hostname)
1172     g_free (hostname);
1173
1174   return ret;
1175 }
1176
1177 static void
1178 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1179 {
1180   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1181
1182   iface->get_type = gst_file_src_uri_get_type;
1183   iface->get_protocols = gst_file_src_uri_get_protocols;
1184   iface->get_uri = gst_file_src_uri_get_uri;
1185   iface->set_uri = gst_file_src_uri_set_uri;
1186 }