]> git.sesse.net Git - vlc/blobdiff - modules/access/mmap.c
Check for file size change at every read (improve reading of downloading
[vlc] / modules / access / mmap.c
index 8742e65ab5874fa62186d9ade96d59e5e058fa47..887c0bc28e0e01cbb0ea9af6ee9714bbe57d97b3 100644 (file)
@@ -23,7 +23,8 @@
 # include <config.h>
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_access.h>
 #include <vlc_input.h>
 #include <vlc_charset.h>
 #define FILE_MMAP_LONGTEXT N_( \
     "Try to use memory mapping to read files and block devices." )
 
+#ifndef NDEBUG
+/*# define MMAP_DEBUG 1*/
+#endif
+
 static int Open (vlc_object_t *);
 static void Close (vlc_object_t *);
 
 vlc_module_begin();
-    set_shortname (_("MMap"));
-    set_description (_("Memory-mapped file input"));
+    set_shortname (N_("MMap"));
+    set_description (N_("Memory-mapped file input"));
     set_category (CAT_INPUT);
     set_subcategory (SUBCAT_INPUT_ACCESS);
-    set_capability ("access2", 52);
+    set_capability ("access", 52);
     add_shortcut ("file");
     set_callbacks (Open, Close);
 
-    add_bool ("file-mmap", VLC_TRUE, NULL,
-              FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, VLC_TRUE);
+    add_bool ("file-mmap", true, NULL,
+              FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true);
 vlc_module_end();
 
 static block_t *Block (access_t *);
@@ -131,7 +136,9 @@ static int Open (vlc_object_t *p_this)
     p_sys->fd = fd;
 
     p_access->info.i_size = st.st_size;
+#ifdef HAVE_POSIX_FADVISE    
     posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
+#endif 
 
     return VLC_SUCCESS;
 
@@ -155,34 +162,32 @@ static void Close (vlc_object_t * p_this)
 static block_t *Block (access_t *p_access)
 {
     access_sys_t *p_sys = p_access->p_sys;
-#ifndef NDEBUG
+
+    /* Check if file size changed... */
+    struct stat st;
+
+    if ((fstat (p_sys->fd, &st) == 0)
+     && (st.st_size != p_access->info.i_size))
+    {
+        p_access->info.i_size = st.st_size;
+        p_access->info.i_update |= INPUT_UPDATE_SIZE;
+    }
+
+    if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
+    {
+        /* We are at end of file */
+        p_access->info.b_eof = true;
+        msg_Dbg (p_access, "at end of memory mapped file");
+        return NULL;
+    }
+
+#ifdef MMAP_DEBUG
     int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
     if (dbgpos != p_access->info.i_pos)
         msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
                  p_access->info.i_pos, dbgpos);
 #endif
 
-    if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
-    {
-        /* End of file - check if file size changed... */
-        struct stat st;
-
-        if ((fstat (p_sys->fd, &st) == 0)
-         && (st.st_size != p_access->info.i_size))
-        {
-            p_access->info.i_size = st.st_size;
-            p_access->info.i_update |= INPUT_UPDATE_SIZE;
-        }
-
-        /* Really at end of file then */
-        if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
-        {
-            p_access->info.b_eof = VLC_TRUE;
-            msg_Dbg (p_access, "at end of memory mapped file");
-            return NULL;
-        }
-    }
-
     const uintptr_t page_mask = p_sys->page_size - 1;
     /* Start the mapping on a page boundary: */
     off_t  outer_offset = p_access->info.i_pos & ~page_mask;
@@ -206,11 +211,14 @@ static block_t *Block (access_t *p_access)
     if (addr == MAP_FAILED)
     {
         msg_Err (p_access, "memory mapping failed (%m)");
-        intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
+        intf_UserFatal (p_access, false, _("File reading failed"),
                         _("VLC could not read the file."));
         msleep (INPUT_ERROR_SLEEP);
         return NULL;
     }
+#ifdef HAVE_POSIX_MADVISE    
+    posix_madvise (addr, length, POSIX_MADV_SEQUENTIAL);
+#endif
 
     block_t *block = block_mmap_Alloc (addr, length);
     if (block == NULL)
@@ -219,7 +227,7 @@ static block_t *Block (access_t *p_access)
     block->p_buffer += inner_offset;
     block->i_buffer -= inner_offset;
 
-#ifndef NDEBUG
+#ifdef MMAP_DEBUG
     msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
              (unsigned long)length, addr, (unsigned long)outer_offset);
 
@@ -242,8 +250,12 @@ static block_t *Block (access_t *p_access)
 
 static int Seek (access_t *p_access, int64_t i_pos)
 {
+#ifdef MMAP_DEBUG
+    lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
+#endif
+
     p_access->info.i_pos = i_pos;
-    p_access->info.b_eof = VLC_FALSE;
+    p_access->info.b_eof = false;
     return VLC_SUCCESS;
 }
 
@@ -258,7 +270,7 @@ static int Control (access_t *p_access, int query, va_list args)
         case ACCESS_CAN_FASTSEEK:
         case ACCESS_CAN_PAUSE:
         case ACCESS_CAN_CONTROL_PACE:
-            *((vlc_bool_t *)va_arg (args, vlc_bool_t *)) = VLC_TRUE;
+            *((bool *)va_arg (args, bool *)) = true;
             return VLC_SUCCESS;
 
         case ACCESS_GET_MTU: