Don't wrtie to the page we have mmaped read only.
[platform/upstream/gstreamer.git] / gst / elements / gstfilesrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
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 #include <gst/gst.h>
24
25 #include "gstfilesrc.h"
26
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <errno.h>
32
33
34 /**********************************************************************
35  * GStreamer Default File Source
36  * Theory of Operation
37  *
38  * This source uses mmap(2) to efficiently load data from a file.
39  * To do this without seriously polluting the applications' memory
40  * space, it must do so in smaller chunks, say 1-4MB at a time.
41  * Buffers are then subdivided from these mmap'd chunks, to directly
42  * make use of the mmap.
43  *
44  * To handle refcounting so that the mmap can be freed at the appropriate
45  * time, a buffer will be created for each mmap'd region, and all new
46  * buffers will be sub-buffers of this top-level buffer.  As they are 
47  * freed, the refcount goes down on the mmap'd buffer and its free()
48  * function is called, which will call munmap(2) on itself.
49  *
50  * If a buffer happens to cross the boundaries of an mmap'd region, we
51  * have to decide whether it's more efficient to copy the data into a
52  * new buffer, or mmap() just that buffer.  There will have to be a
53  * breakpoint size to determine which will be done.  The mmap() size
54  * has a lot to do with this as well, because you end up in double-
55  * jeopardy: the larger the outgoing buffer, the more data to copy when
56  * it overlaps, *and* the more frequently you'll have buffers that *do*
57  * overlap.
58  *
59  * Seeking is another tricky aspect to do efficiently.  The initial
60  * implementation of this source won't make use of these features, however.
61  * The issue is that if an application seeks backwards in a file, *and*
62  * that region of the file is covered by an mmap that hasn't been fully
63  * deallocated, we really should re-use it.  But keeping track of these
64  * regions is tricky because we have to lock the structure that holds
65  * them.  We need to settle on a locking primitive (GMutex seems to be
66  * a really good option...), then we can do that.
67  */
68
69
70 GstElementDetails gst_filesrc_details = {
71   "File Source",
72   "Source/File",
73   "Read from arbitrary point in a file",
74   VERSION,
75   "Erik Walthinsen <omega@cse.ogi.edu>",
76   "(C) 1999",
77 };
78
79 //#define fs_print(format,args...) g_print(format, ## args)
80 #define fs_print(format,args...)
81
82 /* FileSrc signals and args */
83 enum {
84   /* FILL ME */
85   LAST_SIGNAL
86 };
87
88 enum {
89   ARG_0,
90   ARG_LOCATION,
91   ARG_FILESIZE,
92   ARG_FD,
93   ARG_BLOCKSIZE,
94   ARG_OFFSET,
95   ARG_MAPSIZE,
96   ARG_TOUCH,
97 };
98
99
100 static void             gst_filesrc_class_init          (GstFileSrcClass *klass);
101 static void             gst_filesrc_init                (GstFileSrc *filesrc);
102
103 static void             gst_filesrc_set_property        (GObject *object, guint prop_id, 
104                                                          const GValue *value, GParamSpec *pspec);
105 static void             gst_filesrc_get_property        (GObject *object, guint prop_id, 
106                                                          GValue *value, GParamSpec *pspec);
107
108 static GstBuffer *      gst_filesrc_get                 (GstPad *pad);
109 static gboolean         gst_filesrc_srcpad_event        (GstPad *pad, GstEvent *event);
110
111 static GstElementStateReturn    gst_filesrc_change_state        (GstElement *element);
112
113
114 static GstElementClass *parent_class = NULL;
115 //static guint gst_filesrc_signals[LAST_SIGNAL] = { 0 };
116
117 GType
118 gst_filesrc_get_type(void)
119 {
120   static GType filesrc_type = 0;
121
122   if (!filesrc_type) {
123     static const GTypeInfo filesrc_info = {
124       sizeof(GstFileSrcClass),      NULL,
125       NULL,
126       (GClassInitFunc)gst_filesrc_class_init,
127       NULL,
128       NULL,
129       sizeof(GstFileSrc),
130       0,
131       (GInstanceInitFunc)gst_filesrc_init,
132     };
133     filesrc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSrc", &filesrc_info, 0);
134   }
135   return filesrc_type;
136 }
137
138 static void
139 gst_filesrc_class_init (GstFileSrcClass *klass)
140 {
141   GObjectClass *gobject_class;
142   GstElementClass *gstelement_class;
143
144   gobject_class = (GObjectClass*)klass;
145   gstelement_class = (GstElementClass*)klass;
146
147   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
148
149   gst_element_install_std_props (
150           GST_ELEMENT_CLASS (klass),
151           "fd",           ARG_FD,           G_PARAM_READABLE,
152           "offset",       ARG_OFFSET,       G_PARAM_READWRITE,
153           "filesize",     ARG_FILESIZE,     G_PARAM_READABLE,
154           "location",     ARG_LOCATION,     G_PARAM_READWRITE,
155           "blocksize",    ARG_BLOCKSIZE,    G_PARAM_READWRITE,
156           "mmapsize",     ARG_MAPSIZE,      G_PARAM_READWRITE,
157           "touch",        ARG_TOUCH,        G_PARAM_READWRITE,
158           NULL);
159
160   gobject_class->set_property = gst_filesrc_set_property;
161   gobject_class->get_property = gst_filesrc_get_property;
162
163   gstelement_class->change_state = gst_filesrc_change_state;
164 }
165
166 static gint
167 gst_filesrc_bufcmp (gconstpointer a, gconstpointer b)
168 {
169 //  GstBuffer *bufa = (GstBuffer *)a, *bufb = (GstBuffer *)b;
170
171   // sort first by offset, then in reverse by size
172   if (GST_BUFFER_OFFSET(a) < GST_BUFFER_OFFSET(b)) return -1;
173   else if (GST_BUFFER_OFFSET(a) > GST_BUFFER_OFFSET(b)) return 1;
174   else if (GST_BUFFER_SIZE(a) > GST_BUFFER_SIZE(b)) return -1;
175   else if (GST_BUFFER_SIZE(a) < GST_BUFFER_SIZE(b)) return 1;
176   else return 0;
177 }
178
179 static void
180 gst_filesrc_init (GstFileSrc *src)
181 {
182   src->srcpad = gst_pad_new ("src", GST_PAD_SRC);
183   gst_pad_set_get_function (src->srcpad,gst_filesrc_get);
184   gst_pad_set_event_function (src->srcpad,gst_filesrc_srcpad_event);
185   gst_element_add_pad (GST_ELEMENT (src), src->srcpad);
186
187   src->pagesize = getpagesize();
188
189   src->filename = NULL;
190   src->fd = 0;
191   src->filelen = 0;
192
193   src->curoffset = 0;
194   src->block_size = 4096;
195   src->touch = TRUE;
196
197   src->mapbuf = NULL;
198   src->mapsize = 4 * 1024 * 1024;               // default is 4MB
199
200   src->map_regions = g_tree_new(gst_filesrc_bufcmp);
201   src->map_regions_lock = g_mutex_new();
202
203   src->seek_happened = FALSE;
204 }
205
206
207 static void
208 gst_filesrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
209 {
210   GstFileSrc *src;
211
212   /* it's not null if we got it, but it might not be ours */
213   g_return_if_fail (GST_IS_FILESRC (object));
214
215   src = GST_FILESRC (object);
216
217   switch (prop_id) {
218     case ARG_LOCATION:
219       /* the element must be stopped in order to do this */
220       g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
221
222       if (src->filename) g_free (src->filename);
223       /* clear the filename if we get a NULL (is that possible?) */
224       if (g_value_get_string (value) == NULL) {
225         gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
226         src->filename = NULL;
227       /* otherwise set the new filename */
228       } else {
229         src->filename = g_strdup (g_value_get_string (value));
230       }
231       break;
232     case ARG_BLOCKSIZE:
233       src->block_size = g_value_get_ulong (value);
234       break;
235     case ARG_OFFSET:
236       src->curoffset = g_value_get_int64 (value);
237       break;
238     case ARG_MAPSIZE:
239       if ((src->mapsize % src->pagesize) == 0)
240         src->mapsize = g_value_get_ulong (value);
241       else
242         GST_INFO(0, "invalid mapsize, must a multiple of pagesize, which is %d\n",src->pagesize);
243       break;
244     case ARG_TOUCH:
245       src->touch = g_value_get_boolean (value);
246       break;
247     default:
248       break;
249   }
250 }
251
252 static void
253 gst_filesrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
254 {
255   GstFileSrc *src;
256
257   /* it's not null if we got it, but it might not be ours */
258   g_return_if_fail (GST_IS_FILESRC (object));
259
260   src = GST_FILESRC (object);
261
262   switch (prop_id) {
263     case ARG_LOCATION:
264       g_value_set_string (value, src->filename);
265       break;
266     case ARG_FILESIZE:
267       g_value_set_int64 (value, src->filelen);
268       break;
269     case ARG_FD:
270       g_value_set_int (value, src->fd);
271       break;
272     case ARG_BLOCKSIZE:
273       g_value_set_ulong (value, src->block_size);
274       break;
275     case ARG_OFFSET:
276       g_value_set_int64 (value, src->curoffset);
277       break;
278     case ARG_MAPSIZE:
279       g_value_set_ulong (value, src->mapsize);
280       break;
281     case ARG_TOUCH:
282       g_value_set_boolean (value, src->touch);
283       break;
284     default:
285       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286       break;
287   }
288 }
289
290 static void
291 gst_filesrc_free_parent_mmap (GstBuffer *buf)
292 {
293   GstFileSrc *src = GST_FILESRC(GST_BUFFER_POOL_PRIVATE(buf));
294
295   fs_print ("freeing mmap()d buffer at %d+%d\n",GST_BUFFER_OFFSET(buf),GST_BUFFER_SIZE(buf));
296
297   // remove the buffer from the list of available mmap'd regions
298   g_mutex_lock(src->map_regions_lock);
299   g_tree_remove(src->map_regions,buf);
300   // check to see if the tree is empty
301   if (g_tree_nnodes(src->map_regions) == 0) {
302     // we have to free the bufferpool we don't have yet
303   }
304   g_mutex_unlock(src->map_regions_lock);
305
306 #ifdef MADV_DONTNEED
307   // madvise to tell the kernel what to do with it
308   madvise(GST_BUFFER_DATA(buf),GST_BUFFER_SIZE(buf),MADV_DONTNEED);
309 #endif
310   // now unmap the memory
311   munmap(GST_BUFFER_DATA(buf),GST_BUFFER_MAXSIZE(buf));
312 }
313
314 static GstBuffer *
315 gst_filesrc_map_region (GstFileSrc *src, off_t offset, size_t size)
316 {
317   GstBuffer *buf;
318   gint retval;
319
320   g_return_val_if_fail (offset >= 0, NULL);
321
322   fs_print  ("mapping region %08lx+%08lx from file into memory\n",offset,size);
323
324   // time to allocate a new mapbuf
325   buf = gst_buffer_new();
326   // mmap() the data into this new buffer
327   GST_BUFFER_DATA(buf) = mmap (NULL, size, PROT_READ, MAP_SHARED, src->fd, offset);
328   if (GST_BUFFER_DATA(buf) == NULL) {
329     fprintf (stderr, "ERROR: gstfilesrc couldn't map file!\n");
330   } else if (GST_BUFFER_DATA(buf) == MAP_FAILED) {
331     g_error ("gstfilesrc mmap(0x%x, %d, 0x%llx) : %s",
332              size, src->fd, offset, sys_errlist[errno]);
333   }
334 #ifdef MADV_SEQUENTIAL
335   // madvise to tell the kernel what to do with it
336   retval = madvise(GST_BUFFER_DATA(buf),GST_BUFFER_SIZE(buf),MADV_SEQUENTIAL);
337 #endif
338   // fill in the rest of the fields
339   GST_BUFFER_FLAGS(buf) = GST_BUFFER_READONLY | GST_BUFFER_ORIGINAL;
340   GST_BUFFER_SIZE(buf) = size;
341   GST_BUFFER_MAXSIZE(buf) = size;
342   GST_BUFFER_OFFSET(buf) = offset;
343   GST_BUFFER_TIMESTAMP(buf) = -1LL;
344   GST_BUFFER_POOL_PRIVATE(buf) = src;
345   GST_BUFFER_FREE_FUNC(buf) = gst_filesrc_free_parent_mmap;
346
347   g_mutex_lock(src->map_regions_lock);
348   g_tree_insert(src->map_regions,buf,buf);
349   g_mutex_unlock(src->map_regions_lock);
350
351   return buf;
352 }
353
354 static GstBuffer *
355 gst_filesrc_map_small_region (GstFileSrc *src, off_t offset, size_t size)
356 {
357   size_t mapsize;
358   off_t mod, mapbase;
359   GstBuffer *map;
360
361 //  printf("attempting to map a small buffer at %d+%d\n",offset,size);
362
363   // if the offset starts at a non-page boundary, we have to special case
364   if ((mod = offset % src->pagesize)) {
365     GstBuffer *ret;
366
367     mapbase = offset - mod;
368     mapsize = ((size + mod + src->pagesize - 1) / src->pagesize) * src->pagesize;
369 //    printf("not on page boundaries, resizing map to %d+%d\n",mapbase,mapsize);
370     map = gst_filesrc_map_region(src, mapbase, mapsize);
371     ret = gst_buffer_create_sub (map, offset - mapbase, size);
372
373     gst_buffer_unref (map);
374
375     return ret;
376   }
377
378   return gst_filesrc_map_region(src,offset,size);
379 }
380
381 typedef struct {
382   off_t offset;
383   off_t size;
384 } GstFileSrcRegion;
385
386 // This allows us to search for a potential mmap region.
387 static gint
388 gst_filesrc_search_region_match (gpointer a, gpointer b)
389 {
390   GstFileSrcRegion *r = (GstFileSrcRegion *)b;
391
392   // trying to walk b down the tree, current node is a
393   if (r->offset < GST_BUFFER_OFFSET(a)) return -1;
394   else if (r->offset >= (GST_BUFFER_OFFSET(a) + GST_BUFFER_SIZE(a))) return 1;
395   else if ((r->offset + r->size) <= (GST_BUFFER_OFFSET(a) + GST_BUFFER_SIZE(a))) return 0;
396
397   return -2;
398 }
399
400 /**
401  * gst_filesrc_get:
402  * @pad: #GstPad to push a buffer from
403  *
404  * Push a new buffer from the filesrc at the current offset.
405  */
406 static GstBuffer *
407 gst_filesrc_get (GstPad *pad)
408 {
409   GstFileSrc *src;
410   GstBuffer *buf = NULL, *map;
411   size_t readsize;
412   off_t readend,mapstart,mapend;
413   GstFileSrcRegion region;
414   int i;
415
416   g_return_val_if_fail (pad != NULL, NULL);
417   src = GST_FILESRC (gst_pad_get_parent (pad));
418   g_return_val_if_fail (GST_FLAG_IS_SET (src, GST_FILESRC_OPEN), NULL);
419
420   // check for seek
421   if (src->seek_happened) {
422     src->seek_happened = FALSE;
423     return GST_BUFFER (gst_event_new(GST_EVENT_DISCONTINUOUS));
424   }
425
426   // check for EOF
427   if (src->curoffset == src->filelen) {
428     gst_element_set_state (GST_ELEMENT (src), GST_STATE_PAUSED);
429     return GST_BUFFER (gst_event_new(GST_EVENT_EOS));
430   }
431
432   // calculate end pointers so we don't have to do so repeatedly later
433   readsize = src->block_size;
434   readend = src->curoffset + src->block_size;           // note this is the byte *after* the read
435   mapstart = GST_BUFFER_OFFSET(src->mapbuf);
436   mapend = mapstart + GST_BUFFER_SIZE(src->mapbuf);     // note this is the byte *after* the map
437
438   // check to see if we're going to overflow the end of the file
439   if (readend > src->filelen) {
440     readsize = src->filelen - src->curoffset;
441     readend = src->curoffset;
442   }
443
444   // if the start is past the mapstart
445   if (src->curoffset >= mapstart) {
446     // if the end is before the mapend, the buffer is in current mmap region...
447     // ('cause by definition if readend is in the buffer, so's readstart)
448     if (readend <= mapend) {
449       fs_print ("read buf %d+%d lives in current mapbuf %d+%d, creating subbuffer of mapbuf\n",
450              src->curoffset,readsize,GST_BUFFER_OFFSET(src->mapbuf),GST_BUFFER_SIZE(src->mapbuf));
451       buf = gst_buffer_create_sub (src->mapbuf, src->curoffset - GST_BUFFER_OFFSET(src->mapbuf),
452                                    readsize);
453
454     // if the start actually is within the current mmap region, map an overlap buffer
455     } else if (src->curoffset < mapend) {
456       fs_print ("read buf %d+%d starts in mapbuf %d+%d but ends outside, creating new mmap\n",
457              src->curoffset,readsize,GST_BUFFER_OFFSET(src->mapbuf),GST_BUFFER_SIZE(src->mapbuf));
458       buf = gst_filesrc_map_small_region (src, src->curoffset, readsize);
459     }
460
461     // the only other option is that buffer is totally outside, which means we search for it
462
463   // now we can assume that the start is *before* the current mmap region
464   // if the readend is past mapstart, we have two options
465   } else if (readend >= mapstart) {
466     // either the read buffer overlaps the start of the mmap region
467     // or the read buffer fully contains the current mmap region
468     // either way, it's really not relevant, we just create a new region anyway
469     fs_print ("read buf %d+%d starts before mapbuf %d+%d, but overlaps it\n",
470              src->curoffset,readsize,GST_BUFFER_OFFSET(src->mapbuf),GST_BUFFER_SIZE(src->mapbuf));
471     buf = gst_filesrc_map_small_region (src, src->curoffset, readsize);
472   }
473
474   // then deal with the case where the read buffer is totally outside
475   if (buf == NULL) {
476     // first check to see if there's a map that covers the right region already
477     fs_print ("searching for mapbuf to cover %d+%d\n",src->curoffset,readsize);
478     region.offset = src->curoffset;
479     region.size = readsize;
480     map = g_tree_search (src->map_regions,
481                          (GCompareFunc) gst_filesrc_search_region_match,
482                          &region);
483
484     // if we found an exact match, subbuffer it
485     if (map != NULL) {
486       fs_print ("found mapbuf at %d+%d, creating subbuffer\n",GST_BUFFER_OFFSET(map),GST_BUFFER_SIZE(map));
487       buf = gst_buffer_create_sub (map, src->curoffset - GST_BUFFER_OFFSET(map), readsize);
488
489     // otherwise we need to create something out of thin air
490     } else {
491       // if the read buffer crosses a mmap region boundary, create a one-off region
492       if ((src->curoffset / src->mapsize) != (readend / src->mapsize)) {
493         fs_print ("read buf %d+%d crosses a %d-byte boundary, creating a one-off\n",
494                src->curoffset,readsize,src->mapsize);
495         buf = gst_filesrc_map_small_region (src, src->curoffset, readsize);
496
497       // otherwise we will create a new mmap region and set it to the default
498       } else {
499         off_t nextmap = src->curoffset - (src->curoffset % src->mapsize);
500         fs_print ("read buf %d+%d in new mapbuf at %d+%d, mapping and subbuffering\n",
501                src->curoffset,readsize,nextmap,src->mapsize);
502         // first, we're done with the old mapbuf
503         gst_buffer_unref(src->mapbuf);
504         // create a new one
505         src->mapbuf = gst_filesrc_map_region (src, nextmap, src->mapsize);
506         // subbuffer it
507         buf = gst_buffer_create_sub (src->mapbuf, src->curoffset - GST_BUFFER_OFFSET(src->mapbuf), readsize);
508       }
509     }
510   }
511
512   /* if we need to touch the buffer (to bring it into memory), do so */
513   if (src->touch) {
514     volatile guchar *p = GST_BUFFER_DATA (buf), c;
515     for (i=0;i<GST_BUFFER_SIZE(buf);i+=src->pagesize)
516       c = p[i];
517   }
518
519   /* we're done, return the buffer */
520   src->curoffset += GST_BUFFER_SIZE(buf);
521   return buf;
522 }
523
524 /* open the file and mmap it, necessary to go to READY state */
525 static gboolean 
526 gst_filesrc_open_file (GstFileSrc *src)
527 {
528   g_return_val_if_fail (!GST_FLAG_IS_SET (src ,GST_FILESRC_OPEN), FALSE);
529
530   GST_DEBUG(0, "opening file %s\n",src->filename);
531
532   /* open the file */
533   src->fd = open (src->filename, O_RDONLY);
534   if (src->fd < 0) {
535     perror ("open");
536     gst_element_error (GST_ELEMENT (src), g_strconcat("opening file \"", src->filename, "\"", NULL));
537     return FALSE;
538   } else {
539     /* find the file length */
540     src->filelen = lseek (src->fd, 0, SEEK_END);
541     lseek (src->fd, 0, SEEK_SET);
542
543     // allocate the first mmap'd region
544     src->mapbuf = gst_filesrc_map_region (src, 0, src->mapsize);
545
546     src->curoffset = 0;
547
548     GST_FLAG_SET (src, GST_FILESRC_OPEN);
549   }
550   return TRUE;
551 }
552
553 /* unmap and close the file */
554 static void
555 gst_filesrc_close_file (GstFileSrc *src)
556 {
557   g_return_if_fail (GST_FLAG_IS_SET (src, GST_FILESRC_OPEN));
558
559   g_print ("close\n");
560   /* close the file */
561   close (src->fd);
562
563   /* zero out a lot of our state */
564   src->fd = 0;
565   src->filelen = 0;
566   src->curoffset = 0;
567
568   GST_FLAG_UNSET (src, GST_FILESRC_OPEN);
569 }
570
571
572 static GstElementStateReturn
573 gst_filesrc_change_state (GstElement *element)
574 {
575   g_return_val_if_fail (GST_IS_FILESRC (element), GST_STATE_FAILURE);
576
577   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
578     if (GST_FLAG_IS_SET (element, GST_FILESRC_OPEN))
579       gst_filesrc_close_file (GST_FILESRC (element));
580   } else {
581     if (!GST_FLAG_IS_SET (element, GST_FILESRC_OPEN)) {
582       if (!gst_filesrc_open_file (GST_FILESRC (element)))
583         return GST_STATE_FAILURE;
584     }
585   }
586
587   if (GST_ELEMENT_CLASS (parent_class)->change_state)
588     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
589
590   return GST_STATE_SUCCESS;
591 }
592
593 static gboolean
594 gst_filesrc_srcpad_event (GstPad *pad, GstEvent *event)
595 {
596   GstFileSrc *src = GST_FILESRC(GST_PAD_PARENT(pad));
597
598   switch (GST_EVENT_TYPE (event)) {
599     case GST_EVENT_SEEK:
600       src->curoffset = (guint64) GST_EVENT_SEEK_OFFSET (event);
601       src->seek_happened = TRUE;
602       gst_event_free (event);
603       // push a discontinuous event?
604     default:
605       break;
606   }
607
608   return TRUE;
609 }