1 /*****************************************************************************
2 * mmap.c: memory-mapped file input
3 *****************************************************************************
4 * Copyright © 2007-2008 Rémi Denis-Courmont
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program 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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_access.h>
29 #include <vlc_input.h>
30 #include <vlc_charset.h>
31 #include <vlc_interface.h>
36 #include <sys/types.h>
42 #define FILE_MMAP_TEXT N_("Use file memory mapping")
43 #define FILE_MMAP_LONGTEXT N_( \
44 "Try to use memory mapping to read files and block devices." )
47 /*# define MMAP_DEBUG 1*/
50 static int Open (vlc_object_t *);
51 static void Close (vlc_object_t *);
54 set_shortname (N_("MMap"));
55 set_description (N_("Memory-mapped file input"));
56 set_category (CAT_INPUT);
57 set_subcategory (SUBCAT_INPUT_ACCESS);
58 set_capability ("access", 52);
59 add_shortcut ("file");
60 set_callbacks (Open, Close);
62 add_bool ("file-mmap", false, NULL,
64 add_bool ("file-mmap", true, NULL,
66 FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true);
69 static block_t *Block (access_t *);
70 static int Seek (access_t *, int64_t);
71 static int Control (access_t *, int, va_list);
80 #define MMAP_SIZE (1 << 20)
82 static int Open (vlc_object_t *p_this)
84 access_t *p_access = (access_t *)p_this;
86 const char *path = p_access->psz_path;
89 if (!var_CreateGetBool (p_this, "file-mmap"))
90 return VLC_EGENERIC; /* disabled */
92 STANDARD_BLOCK_ACCESS_INIT;
94 if (!strcmp (p_access->psz_path, "-"))
98 msg_Dbg (p_access, "opening file %s", path);
99 fd = utf8_open (path, O_RDONLY | O_NOCTTY, 0666);
104 msg_Warn (p_access, "cannot open %s: %m", path);
108 /* mmap() is only safe for regular and block special files.
109 * For other types, it may be some idiosyncrasic interface (e.g. packet
110 * sockets are a good example), if it works at all. */
115 msg_Err (p_access, "cannot stat %s: %m", path);
119 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode))
121 msg_Dbg (p_access, "skipping non regular file %s", path);
125 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
126 /* We'd rather use any available memory for reading ahead
127 * than for caching what we've already mmap'ed */
128 fcntl (fd, F_RDAHEAD, 1);
129 fcntl (fd, F_NOCACHE, 1);
132 /* Autodetect mmap() support */
135 void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
136 if (addr != MAP_FAILED)
142 p_sys->page_size = sysconf (_SC_PAGE_SIZE);
143 p_sys->mtu = MMAP_SIZE;
144 if (p_sys->mtu < p_sys->page_size)
145 p_sys->mtu = p_sys->page_size;
148 p_access->info.i_size = st.st_size;
149 #ifdef HAVE_POSIX_FADVISE
150 posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
163 static void Close (vlc_object_t * p_this)
165 access_t *p_access = (access_t *)p_this;
166 access_sys_t *p_sys = p_access->p_sys;
168 close (p_sys->fd); /* don't care about error when only reading */
172 static block_t *Block (access_t *p_access)
174 access_sys_t *p_sys = p_access->p_sys;
176 /* Check if file size changed... */
179 if ((fstat (p_sys->fd, &st) == 0)
180 && (st.st_size != p_access->info.i_size))
182 p_access->info.i_size = st.st_size;
183 p_access->info.i_update |= INPUT_UPDATE_SIZE;
186 if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
188 /* We are at end of file */
189 p_access->info.b_eof = true;
190 msg_Dbg (p_access, "at end of memory mapped file");
195 int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
196 if (dbgpos != p_access->info.i_pos)
197 msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
198 p_access->info.i_pos, dbgpos);
201 const uintptr_t page_mask = p_sys->page_size - 1;
202 /* Start the mapping on a page boundary: */
203 off_t outer_offset = p_access->info.i_pos & ~page_mask;
204 /* Skip useless bytes at the beginning of the first page: */
205 size_t inner_offset = p_access->info.i_pos & page_mask;
206 /* Map no more bytes than remain: */
207 size_t length = p_sys->mtu;
208 if (outer_offset + length > p_access->info.i_size)
209 length = p_access->info.i_size - outer_offset;
211 assert (outer_offset <= p_access->info.i_pos); /* and */
212 assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
213 assert (outer_offset < p_access->info.i_size); /* imply */
216 /* NOTE: We use PROT_WRITE and MAP_PRIVATE so that the block can be
217 * modified down the chain, without messing up with the underlying
218 * original file. This does NOT need open write permission. */
219 void *addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE,
220 p_sys->fd, outer_offset);
221 if (addr == MAP_FAILED)
223 msg_Err (p_access, "memory mapping failed (%m)");
224 intf_UserFatal (p_access, false, _("File reading failed"),
225 _("VLC could not read the file."));
228 #ifdef HAVE_POSIX_MADVISE
229 posix_madvise (addr, length, POSIX_MADV_SEQUENTIAL);
232 block_t *block = block_mmap_Alloc (addr, length);
236 block->p_buffer += inner_offset;
237 block->i_buffer -= inner_offset;
240 msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
241 (unsigned long)length, addr, (unsigned long)outer_offset);
243 /* Compare normal I/O with memory mapping */
244 char *buf = malloc (block->i_buffer);
245 ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
247 if (i_read != (ssize_t)block->i_buffer)
248 msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
249 (unsigned)block->i_buffer);
250 if (memcmp (buf, block->p_buffer, block->i_buffer))
251 msg_Err (p_access, "inconsistent data buffer");
255 p_access->info.i_pos = outer_offset + length;
259 p_access->info.b_eof = true;
264 static int Seek (access_t *p_access, int64_t i_pos)
267 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
270 p_access->info.i_pos = i_pos;
271 p_access->info.b_eof = false;
276 static int Control (access_t *p_access, int query, va_list args)
278 access_sys_t *p_sys = p_access->p_sys;
282 case ACCESS_CAN_SEEK:
283 case ACCESS_CAN_FASTSEEK:
284 case ACCESS_CAN_PAUSE:
285 case ACCESS_CAN_CONTROL_PACE:
286 *((bool *)va_arg (args, bool *)) = true;
290 *((int *)va_arg (args, int *)) = p_sys->mtu;
293 case ACCESS_GET_PTS_DELAY:
295 int delay_ms = var_CreateGetInteger (p_access, "file-caching");
296 *((int64_t *)va_arg (args, int64_t *)) = delay_ms * INT64_C (1000);
300 case ACCESS_GET_TITLE_INFO:
301 case ACCESS_GET_META:
304 case ACCESS_SET_PAUSE_STATE:
307 case ACCESS_SET_TITLE:
308 case ACCESS_SET_SEEKPOINT:
309 case ACCESS_SET_PRIVATE_ID_STATE:
310 case ACCESS_SET_PRIVATE_ID_CA:
311 case ACCESS_GET_PRIVATE_ID_STATE:
312 case ACCESS_GET_CONTENT_TYPE:
316 msg_Warn (p_access, "unimplemented query %d in control", query);