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", true, NULL,
63 FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true);
66 static block_t *Block (access_t *);
67 static int Seek (access_t *, int64_t);
68 static int Control (access_t *, int, va_list);
77 #define MMAP_SIZE (1 << 20)
79 static int Open (vlc_object_t *p_this)
81 access_t *p_access = (access_t *)p_this;
83 const char *path = p_access->psz_path;
86 if (!var_CreateGetBool (p_this, "file-mmap"))
87 return VLC_EGENERIC; /* disabled */
89 STANDARD_BLOCK_ACCESS_INIT;
91 if (!strcmp (p_access->psz_path, "-"))
95 msg_Dbg (p_access, "opening file %s", path);
96 fd = utf8_open (path, O_RDONLY | O_NOCTTY, 0666);
101 msg_Warn (p_access, "cannot open %s: %m", path);
105 /* mmap() is only safe for regular and block special files.
106 * For other types, it may be some idiosyncrasic interface (e.g. packet
107 * sockets are a good example), if it works at all. */
112 msg_Err (p_access, "cannot stat %s: %m", path);
116 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode))
118 msg_Dbg (p_access, "skipping non regular file %s", path);
122 /* Autodetect mmap() support */
125 void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
126 if (addr != MAP_FAILED)
132 p_sys->page_size = sysconf (_SC_PAGE_SIZE);
133 p_sys->mtu = MMAP_SIZE;
134 if (p_sys->mtu < p_sys->page_size)
135 p_sys->mtu = p_sys->page_size;
138 p_access->info.i_size = st.st_size;
139 #ifdef HAVE_POSIX_FADVISE
140 posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
153 static void Close (vlc_object_t * p_this)
155 access_t *p_access = (access_t *)p_this;
156 access_sys_t *p_sys = p_access->p_sys;
158 close (p_sys->fd); /* don't care about error when only reading */
162 static block_t *Block (access_t *p_access)
164 access_sys_t *p_sys = p_access->p_sys;
166 /* Check if file size changed... */
169 if ((fstat (p_sys->fd, &st) == 0)
170 && (st.st_size != p_access->info.i_size))
172 p_access->info.i_size = st.st_size;
173 p_access->info.i_update |= INPUT_UPDATE_SIZE;
176 if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
178 /* We are at end of file */
179 p_access->info.b_eof = true;
180 msg_Dbg (p_access, "at end of memory mapped file");
185 int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
186 if (dbgpos != p_access->info.i_pos)
187 msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
188 p_access->info.i_pos, dbgpos);
191 const uintptr_t page_mask = p_sys->page_size - 1;
192 /* Start the mapping on a page boundary: */
193 off_t outer_offset = p_access->info.i_pos & ~page_mask;
194 /* Skip useless bytes at the beginning of the first page: */
195 size_t inner_offset = p_access->info.i_pos & page_mask;
196 /* Map no more bytes than remain: */
197 size_t length = p_sys->mtu;
198 if (outer_offset + length > p_access->info.i_size)
199 length = p_access->info.i_size - outer_offset;
201 assert (outer_offset <= p_access->info.i_pos); /* and */
202 assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
203 assert (outer_offset < p_access->info.i_size); /* imply */
206 /* NOTE: We use PROT_WRITE and MAP_PRIVATE so that the block can be
207 * modified down the chain, without messing up with the underlying
208 * original file. This does NOT need open write permission. */
209 void *addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE,
210 p_sys->fd, outer_offset);
211 if (addr == MAP_FAILED)
213 msg_Err (p_access, "memory mapping failed (%m)");
214 intf_UserFatal (p_access, false, _("File reading failed"),
215 _("VLC could not read the file."));
216 msleep (INPUT_ERROR_SLEEP);
219 #ifdef HAVE_POSIX_MADVISE
220 posix_madvise (addr, length, POSIX_MADV_SEQUENTIAL);
223 block_t *block = block_mmap_Alloc (addr, length);
227 block->p_buffer += inner_offset;
228 block->i_buffer -= inner_offset;
231 msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
232 (unsigned long)length, addr, (unsigned long)outer_offset);
234 /* Compare normal I/O with memory mapping */
235 char *buf = malloc (block->i_buffer);
236 ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
238 if (i_read != (ssize_t)block->i_buffer)
239 msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
240 (unsigned)block->i_buffer);
241 if (memcmp (buf, block->p_buffer, block->i_buffer))
242 msg_Err (p_access, "inconsistent data buffer");
246 p_access->info.i_pos = outer_offset + length;
251 static int Seek (access_t *p_access, int64_t i_pos)
254 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
257 p_access->info.i_pos = i_pos;
258 p_access->info.b_eof = false;
263 static int Control (access_t *p_access, int query, va_list args)
265 access_sys_t *p_sys = p_access->p_sys;
269 case ACCESS_CAN_SEEK:
270 case ACCESS_CAN_FASTSEEK:
271 case ACCESS_CAN_PAUSE:
272 case ACCESS_CAN_CONTROL_PACE:
273 *((bool *)va_arg (args, bool *)) = true;
277 *((int *)va_arg (args, int *)) = p_sys->mtu;
280 case ACCESS_GET_PTS_DELAY:
281 *((int64_t *)va_arg (args, int64_t *)) = DEFAULT_PTS_DELAY;
284 case ACCESS_GET_TITLE_INFO:
285 case ACCESS_GET_META:
288 case ACCESS_SET_PAUSE_STATE:
291 case ACCESS_SET_TITLE:
292 case ACCESS_SET_SEEKPOINT:
293 case ACCESS_SET_PRIVATE_ID_STATE:
294 case ACCESS_SET_PRIVATE_ID_CA:
295 case ACCESS_GET_PRIVATE_ID_STATE:
296 case ACCESS_GET_CONTENT_TYPE:
300 msg_Warn (p_access, "unimplemented query %d in control", query);