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 );
96 add_obsolete_string( "file-cat" );
97 set_capability( "access2", 50 );
98 add_shortcut( "file" );
99 add_shortcut( "stream" );
100 add_shortcut( "kfir" );
101 set_callbacks( Open, Close );
105 /*****************************************************************************
106 * Exported prototypes
107 *****************************************************************************/
108 static int Seek( access_t *, int64_t );
109 static ssize_t Read( access_t *, uint8_t *, size_t );
110 static int Control( access_t *, int, va_list );
112 static block_t *mmapBlock( access_t * );
115 static int open_file( access_t *, const char * );
120 unsigned int i_nb_reads;
126 vlc_bool_t b_seekable;
127 vlc_bool_t b_pace_control;
130 /*****************************************************************************
131 * Open: open the file
132 *****************************************************************************/
133 static int Open( vlc_object_t *p_this )
135 access_t *p_access = (access_t*)p_this;
138 vlc_bool_t b_stdin = !strcmp (p_access->psz_path, "-");
140 /* Update default_pts to a suitable value for file access */
141 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
143 STANDARD_READ_ACCESS_INIT;
144 p_sys->i_nb_reads = 0;
145 p_sys->b_kfir = VLC_FALSE;
146 int fd = p_sys->fd = -1;
148 if (!strcasecmp (p_access->psz_access, "stream"))
150 p_sys->b_seekable = VLC_FALSE;
151 p_sys->b_pace_control = VLC_FALSE;
153 else if (!strcasecmp (p_access->psz_access, "kfir"))
155 p_sys->b_seekable = VLC_FALSE;
156 p_sys->b_pace_control = VLC_FALSE;
157 p_sys->b_kfir = VLC_TRUE;
161 p_sys->b_seekable = VLC_TRUE;
162 p_sys->b_pace_control = VLC_TRUE;
166 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
171 fd = open_file (p_access, p_access->psz_path);
173 #ifdef HAVE_SYS_STAT_H
179 msg_Err (p_access, "fstat(%d): %m", fd);
181 if (S_ISDIR (st.st_mode))
182 /* The directory plugin takes care of that */
183 msg_Dbg (p_access, "file is a directory, aborting");
199 #ifdef HAVE_SYS_STAT_H
200 p_access->info.i_size = st.st_size;
201 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
202 && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
203 p_sys->b_seekable = VLC_FALSE;
206 p_sys->pagemask = sysconf (_SC_PAGE_SIZE) - 1;
208 /* Autodetect mmap() support */
209 if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
211 void *addr = mmap (NULL, 1, PROT_READ, MAP_PRIVATE, fd, 0);
212 if (addr != MAP_FAILED)
214 /* Does the file system support mmap? */
216 p_access->pf_read = NULL;
217 p_access->pf_block = mmapBlock;
218 msg_Dbg (p_this, "mmap enabled");
221 msg_Dbg (p_this, "mmap disabled (%m)");
224 msg_Dbg (p_this, "mmap disabled (non regular file)");
227 p_sys->b_seekable = !b_stdin;
228 # warning File size not known!
231 if (p_sys->b_seekable && (p_access->info.i_size == 0))
233 /* FIXME that's bad because all others access will be probed */
234 msg_Err (p_access, "file is empty, aborting");
242 /*****************************************************************************
243 * Close: close the target
244 *****************************************************************************/
245 static void Close (vlc_object_t * p_this)
247 access_t *p_access = (access_t*)p_this;
248 access_sys_t *p_sys = p_access->p_sys;
254 /*****************************************************************************
255 * Read: standard read on a file descriptor.
256 *****************************************************************************/
257 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
259 access_sys_t *p_sys = p_access->p_sys;
263 #if !defined(WIN32) && !defined(UNDER_CE)
264 if( !p_sys->b_pace_control )
268 /* Find if some data is available. This won't work under Windows. */
273 if( p_access->b_die )
276 memset (&ufd, 0, sizeof (ufd));
280 i_ret = poll (&ufd, 1, 500);
284 i_ret = read (fd, p_buffer, i_len);
288 /* b_kfir ; work around a buggy poll() driver implementation */
289 while (((i_ret = read (fd, p_buffer, i_len)) == 0)
292 msleep( INPUT_ERROR_SLEEP );
297 #endif /* WIN32 || UNDER_CE */
298 /* b_pace_control || WIN32 */
299 i_ret = read( fd, p_buffer, i_len );
310 msg_Err (p_access, "read failed (%m)");
311 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
312 _("VLC could not read the file."));
315 /* Delay a bit to avoid consuming all the CPU. This is particularly
316 * useful when reading from an unconnected FIFO. */
317 msleep( INPUT_ERROR_SLEEP );
322 #ifdef HAVE_SYS_STAT_H
323 if( p_access->info.i_size != 0 &&
324 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
328 if ((fstat (fd, &st) == 0)
329 && (p_access->info.i_size != st.st_size))
331 p_access->info.i_size = st.st_size;
332 p_access->info.i_update |= INPUT_UPDATE_SIZE;
338 p_access->info.i_pos += i_ret;
339 else if( i_ret == 0 )
340 p_access->info.b_eof = VLC_TRUE;
346 # define MMAP_SIZE (1 << 20)
351 //vlc_object_t *owner;
356 static void mmapRelease (block_t *block)
358 block_sys_t *p_sys = (block_sys_t *)block;
360 munmap (p_sys->base_addr, p_sys->length);
361 //vlc_object_release (p_sys->owner);
365 static block_t *mmapBlock (access_t *p_access)
367 access_sys_t *p_sys = p_access->p_sys;
369 const int flags = MAP_SHARED;
370 off_t offset = p_access->info.i_pos & ~p_sys->pagemask;
371 size_t align = p_access->info.i_pos & p_sys->pagemask;
372 size_t length = (MMAP_SIZE > p_sys->pagemask) ? MMAP_SIZE : (p_sys->pagemask + 1);
376 int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
377 if (dbgpos != p_access->info.i_pos)
378 msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
379 p_access->info.i_pos, dbgpos);
382 if (p_access->info.i_pos >= p_access->info.i_size)
384 /* End of file - check if file size changed... */
387 if ((fstat (p_sys->fd, &st) == 0)
388 && (st.st_size != p_access->info.i_size))
390 p_access->info.i_size = st.st_size;
391 p_access->info.i_update |= INPUT_UPDATE_SIZE;
394 /* Really at end of file then */
395 if (p_access->info.i_pos >= p_access->info.i_size)
397 p_access->info.b_eof = VLC_TRUE;
398 msg_Dbg (p_access, "at end of memory mapped file");
403 if (offset + length > p_access->info.i_size)
404 /* Don't mmap beyond end of file */
405 length = p_access->info.i_size - offset;
407 assert (offset <= p_access->info.i_pos); /* and */
408 assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
409 assert (offset < p_access->info.i_size); /* imply */
412 addr = mmap (NULL, length, PROT_READ, flags, p_sys->fd, offset);
413 if (addr == MAP_FAILED)
415 msg_Err (p_access, "memory mapping failed (%m)");
416 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
417 _("VLC could not read the file."));
418 msleep( INPUT_ERROR_SLEEP );
422 p_access->info.i_pos = offset + length;
424 msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
425 (unsigned long)length, addr, (unsigned long)offset);
426 block_sys_t *block = malloc (sizeof (*block));
429 munmap (addr, length);
433 block_Init (&block->self, ((uint8_t *)addr) + align, length - align);
434 block->self.pf_release = mmapRelease;
435 block->base_addr = addr;
436 block->length = length;
437 //vlc_object_yield (block->owner = VLC_OBJECT (p_access));
440 /* Compare normal I/O with memory mapping */
441 char buf[block->self.i_buffer];
442 ssize_t i_read = read (p_sys->fd, buf, block->self.i_buffer);
444 if (i_read != (ssize_t)block->self.i_buffer)
445 msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
446 (unsigned)block->self.i_buffer);
447 if (memcmp (buf, block->self.p_buffer, block->self.i_buffer))
448 msg_Err (p_access, "inconsistent data buffer");
455 /*****************************************************************************
456 * Seek: seek to a specific location in a file
457 *****************************************************************************/
458 static int Seek (access_t *p_access, int64_t i_pos)
460 /* FIXME: i_size should really be unsigned */
461 if ((uint64_t)i_pos > (uint64_t)p_access->info.i_size)
463 /* This should only happen with corrupted files.
464 * But it also seems to happen with buggy demuxes (ASF) */
465 msg_Err (p_access, "seeking too far (0x"I64Fx" / 0x"I64Fx")",
466 i_pos, p_access->info.i_size);
467 i_pos = p_access->info.i_size;
470 p_access->info.i_pos = i_pos;
471 p_access->info.b_eof = VLC_FALSE;
473 #if defined (HAVE_MMAP) && defined (NDEBUG)
474 if (p_access->pf_block == NULL)
476 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
480 /*****************************************************************************
482 *****************************************************************************/
483 static int Control( access_t *p_access, int i_query, va_list args )
485 access_sys_t *p_sys = p_access->p_sys;
493 case ACCESS_CAN_SEEK:
494 case ACCESS_CAN_FASTSEEK:
495 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
496 *pb_bool = p_sys->b_seekable;
499 case ACCESS_CAN_PAUSE:
500 case ACCESS_CAN_CONTROL_PACE:
501 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
502 *pb_bool = p_sys->b_pace_control;
507 pi_int = (int*)va_arg( args, int * );
511 case ACCESS_GET_PTS_DELAY:
512 pi_64 = (int64_t*)va_arg( args, int64_t * );
513 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
517 case ACCESS_SET_PAUSE_STATE:
521 case ACCESS_GET_TITLE_INFO:
522 case ACCESS_SET_TITLE:
523 case ACCESS_SET_SEEKPOINT:
524 case ACCESS_SET_PRIVATE_ID_STATE:
525 case ACCESS_GET_META:
526 case ACCESS_GET_CONTENT_TYPE:
530 msg_Warn( p_access, "unimplemented query in control" );
538 static char *expand_path (const access_t *p_access, const char *path)
540 if (strncmp (path, "~/", 2) == 0)
544 // TODO: we should also support the ~cmassiot/ syntax
545 if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
551 if (!strcasecmp (p_access->psz_access, "file")
552 && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
553 // Explorer can open path such as file:/C:/ or file:///C:/
554 // hence remove leading / if found
555 return strdup (path + 1);
558 return strdup (path);
562 /*****************************************************************************
563 * open_file: Opens a specific file
564 *****************************************************************************/
565 static int open_file (access_t *p_access, const char *psz_name)
567 char *path = expand_path (p_access, psz_name);
570 p_sys->fd = utf8_fopen( path, "rb" );
573 msg_Err( p_access, "cannot open file %s", psz_name );
574 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
575 _("VLC could not open the file \"%s\"."), psz_name );
580 fseek( p_sys->fd, 0, SEEK_END );
581 p_access->info.i_size = ftell( p_sys->fd );
582 p_access->info.i_update |= INPUT_UPDATE_SIZE;
583 fseek( p_sys->fd, 0, SEEK_SET );
585 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
589 msg_Err (p_access, "cannot open file %s (%m)", psz_name);
590 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
591 _("VLC could not open the file \"%s\"."), psz_name);
595 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
596 /* We'd rather use any available memory for reading ahead
597 * than for caching what we've already seen/heard */
598 fcntl (fd, F_RDAHEAD, 1);
599 fcntl (fd, F_NOCACHE, 1);