1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
5 * Copyright © 2006-2007 Rémi Denis-Courmont
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan # org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
30 #include <vlc_input.h>
31 #include <vlc_access.h>
32 #include <vlc_interface.h>
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
39 #ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
46 #if defined( WIN32 ) && !defined( UNDER_CE )
53 # include <sys/mman.h>
56 #if defined( WIN32 ) && !defined( UNDER_CE )
60 # define lseek _lseeki64
61 #elif defined( UNDER_CE )
65 # define read(a,b,c) fread(b,1,c,a)
66 # define close(a) fclose(a)
73 #include <vlc_charset.h>
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Open ( vlc_object_t * );
79 static void Close( vlc_object_t * );
81 #define CACHING_TEXT N_("Caching value in ms")
82 #define CACHING_LONGTEXT N_( \
83 "Caching value for files. This " \
84 "value should be set in milliseconds." )
85 #define CAT_TEXT N_("Concatenate with additional files")
86 #define CAT_LONGTEXT N_( \
87 "Play split files as if they were part of a unique file. " \
88 "You need to specify a comma-separated list of files." )
91 set_description( _("File input") );
92 set_shortname( _("File") );
93 set_category( CAT_INPUT );
94 set_subcategory( SUBCAT_INPUT_ACCESS );
95 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
97 add_obsolete_string( "file-cat" );
98 set_capability( "access2", 50 );
99 add_shortcut( "file" );
100 add_shortcut( "stream" );
101 add_shortcut( "kfir" );
102 set_callbacks( Open, Close );
106 /*****************************************************************************
107 * Exported prototypes
108 *****************************************************************************/
109 static int Seek( access_t *, int64_t );
110 static ssize_t Read( access_t *, uint8_t *, size_t );
111 static int Control( access_t *, int, va_list );
113 static block_t *mmapBlock( access_t * );
116 static int open_file( access_t *, const char * );
121 unsigned int i_nb_reads;
127 vlc_bool_t b_seekable;
128 vlc_bool_t b_pace_control;
131 /*****************************************************************************
132 * Open: open the file
133 *****************************************************************************/
134 static int Open( vlc_object_t *p_this )
136 access_t *p_access = (access_t*)p_this;
139 vlc_bool_t b_stdin = !strcmp (p_access->psz_path, "-");
141 /* Update default_pts to a suitable value for file access */
142 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
144 STANDARD_READ_ACCESS_INIT;
145 p_sys->i_nb_reads = 0;
146 p_sys->b_kfir = VLC_FALSE;
147 int fd = p_sys->fd = -1;
149 if (!strcasecmp (p_access->psz_access, "stream"))
151 p_sys->b_seekable = VLC_FALSE;
152 p_sys->b_pace_control = VLC_FALSE;
154 else if (!strcasecmp (p_access->psz_access, "kfir"))
156 p_sys->b_seekable = VLC_FALSE;
157 p_sys->b_pace_control = VLC_FALSE;
158 p_sys->b_kfir = VLC_TRUE;
162 p_sys->b_seekable = VLC_TRUE;
163 p_sys->b_pace_control = VLC_TRUE;
167 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
172 fd = open_file (p_access, p_access->psz_path);
174 #ifdef HAVE_SYS_STAT_H
180 msg_Err (p_access, "fstat(%d): %m", fd);
182 if (S_ISDIR (st.st_mode))
183 /* The directory plugin takes care of that */
184 msg_Dbg (p_access, "file is a directory, aborting");
200 #ifdef HAVE_SYS_STAT_H
201 p_access->info.i_size = st.st_size;
202 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
203 && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
204 p_sys->b_seekable = VLC_FALSE;
207 p_sys->pagemask = sysconf (_SC_PAGE_SIZE) - 1;
209 /* Autodetect mmap() support */
210 if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
212 /* TODO: Do not allow PROT_WRITE, we should not need it.
213 * However, this far, "block" ownership seems such that whoever
214 * "receives" a block can freely modify its content. Hence we _may_
215 * need PROT_WRITE not to default memory protection.
216 * NOTE: With MAP_PRIVATE, changes are not committed to the underlying
217 * file, write open permission is not required.
219 void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
220 if (addr != MAP_FAILED)
222 /* Does the file system support mmap? */
224 p_access->pf_read = NULL;
225 p_access->pf_block = mmapBlock;
226 msg_Dbg (p_this, "mmap enabled");
229 msg_Dbg (p_this, "mmap disabled (%m)");
232 msg_Dbg (p_this, "mmap disabled (non regular file)");
235 p_sys->b_seekable = !b_stdin;
236 # warning File size not known!
239 if (p_sys->b_seekable && (p_access->info.i_size == 0))
241 /* FIXME that's bad because all others access will be probed */
242 msg_Err (p_access, "file is empty, aborting");
250 /*****************************************************************************
251 * Close: close the target
252 *****************************************************************************/
253 static void Close (vlc_object_t * p_this)
255 access_t *p_access = (access_t*)p_this;
256 access_sys_t *p_sys = p_access->p_sys;
262 /*****************************************************************************
263 * Read: standard read on a file descriptor.
264 *****************************************************************************/
265 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
267 access_sys_t *p_sys = p_access->p_sys;
271 #if !defined(WIN32) && !defined(UNDER_CE)
272 if( !p_sys->b_pace_control )
276 /* Find if some data is available. This won't work under Windows. */
281 if( p_access->b_die )
284 memset (&ufd, 0, sizeof (ufd));
288 i_ret = poll (&ufd, 1, 500);
292 i_ret = read (fd, p_buffer, i_len);
296 /* b_kfir ; work around a buggy poll() driver implementation */
297 while (((i_ret = read (fd, p_buffer, i_len)) == 0)
300 msleep( INPUT_ERROR_SLEEP );
305 #endif /* WIN32 || UNDER_CE */
306 /* b_pace_control || WIN32 */
307 i_ret = read( fd, p_buffer, i_len );
318 msg_Err (p_access, "read failed (%m)");
319 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
320 _("VLC could not read the file."));
323 /* Delay a bit to avoid consuming all the CPU. This is particularly
324 * useful when reading from an unconnected FIFO. */
325 msleep( INPUT_ERROR_SLEEP );
330 #ifdef HAVE_SYS_STAT_H
331 if( p_access->info.i_size != 0 &&
332 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
336 if ((fstat (fd, &st) == 0)
337 && (p_access->info.i_size != st.st_size))
339 p_access->info.i_size = st.st_size;
340 p_access->info.i_update |= INPUT_UPDATE_SIZE;
346 p_access->info.i_pos += i_ret;
347 else if( i_ret == 0 )
348 p_access->info.b_eof = VLC_TRUE;
354 # define MMAP_SIZE (1 << 20)
356 static block_t *mmapBlock (access_t *p_access)
358 access_sys_t *p_sys = p_access->p_sys;
360 const int flags = MAP_SHARED;
361 off_t offset = p_access->info.i_pos & ~p_sys->pagemask;
362 size_t align = p_access->info.i_pos & p_sys->pagemask;
363 size_t length = (MMAP_SIZE > p_sys->pagemask) ? MMAP_SIZE : (p_sys->pagemask + 1);
367 int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
368 if (dbgpos != p_access->info.i_pos)
369 msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
370 p_access->info.i_pos, dbgpos);
373 if (p_access->info.i_pos >= p_access->info.i_size)
375 /* End of file - check if file size changed... */
378 if ((fstat (p_sys->fd, &st) == 0)
379 && (st.st_size != p_access->info.i_size))
381 p_access->info.i_size = st.st_size;
382 p_access->info.i_update |= INPUT_UPDATE_SIZE;
385 /* Really at end of file then */
386 if (p_access->info.i_pos >= p_access->info.i_size)
388 p_access->info.b_eof = VLC_TRUE;
389 msg_Dbg (p_access, "at end of memory mapped file");
394 if (offset + length > p_access->info.i_size)
395 /* Don't mmap beyond end of file */
396 length = p_access->info.i_size - offset;
398 assert (offset <= p_access->info.i_pos); /* and */
399 assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
400 assert (offset < p_access->info.i_size); /* imply */
403 addr = mmap (NULL, length, PROT_READ, flags, p_sys->fd, offset);
404 if (addr == MAP_FAILED)
406 msg_Err (p_access, "memory mapping failed (%m)");
407 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
408 _("VLC could not read the file."));
409 msleep( INPUT_ERROR_SLEEP );
413 p_access->info.i_pos = offset + length;
415 block_t *block = block_mmap_Alloc (addr, length);
419 block->p_buffer += align;
420 block->i_buffer -= align;
423 msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
424 (unsigned long)length, addr, (unsigned long)offset);
426 /* Compare normal I/O with memory mapping */
427 char *buf = malloc (block->i_buffer);
428 ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
430 if (i_read != (ssize_t)block->i_buffer)
431 msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
432 (unsigned)block->i_buffer);
433 if (memcmp (buf, block->p_buffer, block->i_buffer))
434 msg_Err (p_access, "inconsistent data buffer");
442 /*****************************************************************************
443 * Seek: seek to a specific location in a file
444 *****************************************************************************/
445 static int Seek (access_t *p_access, int64_t i_pos)
447 /* FIXME: i_size should really be unsigned */
448 if ((uint64_t)i_pos > (uint64_t)p_access->info.i_size)
450 /* This should only happen with corrupted files.
451 * But it also seems to happen with buggy demuxes (ASF) */
452 msg_Err (p_access, "seeking too far (0x"I64Fx" / 0x"I64Fx")",
453 i_pos, p_access->info.i_size);
454 i_pos = p_access->info.i_size;
457 p_access->info.i_pos = i_pos;
458 p_access->info.b_eof = VLC_FALSE;
460 #if defined (HAVE_MMAP) && defined (NDEBUG)
461 if (p_access->pf_block == NULL)
463 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
467 /*****************************************************************************
469 *****************************************************************************/
470 static int Control( access_t *p_access, int i_query, va_list args )
472 access_sys_t *p_sys = p_access->p_sys;
480 case ACCESS_CAN_SEEK:
481 case ACCESS_CAN_FASTSEEK:
482 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
483 *pb_bool = p_sys->b_seekable;
486 case ACCESS_CAN_PAUSE:
487 case ACCESS_CAN_CONTROL_PACE:
488 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
489 *pb_bool = p_sys->b_pace_control;
494 pi_int = (int*)va_arg( args, int * );
498 case ACCESS_GET_PTS_DELAY:
499 pi_64 = (int64_t*)va_arg( args, int64_t * );
500 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
504 case ACCESS_SET_PAUSE_STATE:
508 case ACCESS_GET_TITLE_INFO:
509 case ACCESS_SET_TITLE:
510 case ACCESS_SET_SEEKPOINT:
511 case ACCESS_SET_PRIVATE_ID_STATE:
512 case ACCESS_GET_META:
513 case ACCESS_GET_CONTENT_TYPE:
517 msg_Warn( p_access, "unimplemented query in control" );
525 static char *expand_path (const access_t *p_access, const char *path)
527 if (strncmp (path, "~/", 2) == 0)
531 // TODO: we should also support the ~cmassiot/ syntax
532 if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
538 if (!strcasecmp (p_access->psz_access, "file")
539 && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
540 // Explorer can open path such as file:/C:/ or file:///C:/
541 // hence remove leading / if found
542 return strdup (path + 1);
545 return strdup (path);
549 /*****************************************************************************
550 * open_file: Opens a specific file
551 *****************************************************************************/
552 static int open_file (access_t *p_access, const char *psz_name)
554 char *path = expand_path (p_access, psz_name);
557 p_sys->fd = utf8_fopen( path, "rb" );
560 msg_Err( p_access, "cannot open file %s", psz_name );
561 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
562 _("VLC could not open the file \"%s\"."), psz_name );
567 fseek( p_sys->fd, 0, SEEK_END );
568 p_access->info.i_size = ftell( p_sys->fd );
569 p_access->info.i_update |= INPUT_UPDATE_SIZE;
570 fseek( p_sys->fd, 0, SEEK_SET );
572 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
576 msg_Err (p_access, "cannot open file %s (%m)", psz_name);
577 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
578 _("VLC could not open the file \"%s\"."), psz_name);
582 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
583 /* We'd rather use any available memory for reading ahead
584 * than for caching what we've already seen/heard */
585 fcntl (fd, F_RDAHEAD, 1);
586 fcntl (fd, F_NOCACHE, 1);