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 * );
119 unsigned int i_nb_reads;
125 vlc_bool_t b_seekable;
126 vlc_bool_t b_pace_control;
129 /*****************************************************************************
130 * Open: open the file
131 *****************************************************************************/
132 static int Open( vlc_object_t *p_this )
134 access_t *p_access = (access_t*)p_this;
137 vlc_bool_t b_stdin = !strcmp (p_access->psz_path, "-");
139 /* Update default_pts to a suitable value for file access */
140 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
142 STANDARD_READ_ACCESS_INIT;
143 p_sys->i_nb_reads = 0;
144 p_sys->b_kfir = VLC_FALSE;
145 int fd = p_sys->fd = -1;
147 if (!strcasecmp (p_access->psz_access, "stream"))
149 p_sys->b_seekable = VLC_FALSE;
150 p_sys->b_pace_control = VLC_FALSE;
152 else if (!strcasecmp (p_access->psz_access, "kfir"))
154 p_sys->b_seekable = VLC_FALSE;
155 p_sys->b_pace_control = VLC_FALSE;
156 p_sys->b_kfir = VLC_TRUE;
160 p_sys->b_seekable = VLC_TRUE;
161 p_sys->b_pace_control = VLC_TRUE;
165 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
170 fd = open_file (p_access, p_access->psz_path);
172 #ifdef HAVE_SYS_STAT_H
178 msg_Err (p_access, "fstat(%d): %m", fd);
180 if (S_ISDIR (st.st_mode))
181 /* The directory plugin takes care of that */
182 msg_Dbg (p_access, "file is a directory, aborting");
198 #ifdef HAVE_SYS_STAT_H
199 p_access->info.i_size = st.st_size;
200 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
201 && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
202 p_sys->b_seekable = VLC_FALSE;
205 /* Autodetect mmap() support */
206 if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
208 void *addr = mmap (NULL, 1, PROT_READ, MAP_PRIVATE, fd, 0);
209 if (addr != MAP_FAILED)
211 /* Does the file system support mmap? */
213 p_access->pf_read = NULL;
214 p_access->pf_block = mmapBlock;
215 msg_Dbg (p_this, "mmap enabled");
218 msg_Dbg (p_this, "mmap disabled (%m)");
221 msg_Dbg (p_this, "mmap disabled (non regular file)");
224 p_sys->b_seekable = !b_stdin;
225 # warning File size not known!
228 if (p_sys->b_seekable && (p_access->info.i_size == 0))
230 /* FIXME that's bad because all others access will be probed */
231 msg_Err (p_access, "file is empty, aborting");
239 /*****************************************************************************
240 * Close: close the target
241 *****************************************************************************/
242 static void Close (vlc_object_t * p_this)
244 access_t *p_access = (access_t*)p_this;
245 access_sys_t *p_sys = p_access->p_sys;
251 /*****************************************************************************
252 * Read: standard read on a file descriptor.
253 *****************************************************************************/
254 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
256 access_sys_t *p_sys = p_access->p_sys;
260 #if !defined(WIN32) && !defined(UNDER_CE)
261 if( !p_sys->b_pace_control )
265 /* Find if some data is available. This won't work under Windows. */
270 if( p_access->b_die )
273 memset (&ufd, 0, sizeof (ufd));
277 i_ret = poll (&ufd, 1, 500);
281 i_ret = read (fd, p_buffer, i_len);
285 /* b_kfir ; work around a buggy poll() driver implementation */
286 while (((i_ret = read (fd, p_buffer, i_len)) == 0)
289 msleep( INPUT_ERROR_SLEEP );
294 #endif /* WIN32 || UNDER_CE */
295 /* b_pace_control || WIN32 */
296 i_ret = read( fd, p_buffer, i_len );
307 msg_Err (p_access, "read failed (%m)");
308 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
309 _("VLC could not read the file."));
312 /* Delay a bit to avoid consuming all the CPU. This is particularly
313 * useful when reading from an unconnected FIFO. */
314 msleep( INPUT_ERROR_SLEEP );
319 #ifdef HAVE_SYS_STAT_H
320 if( p_access->info.i_size != 0 &&
321 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
325 if ((fstat (fd, &st) == 0)
326 && (p_access->info.i_size != st.st_size))
328 p_access->info.i_size = st.st_size;
329 p_access->info.i_update |= INPUT_UPDATE_SIZE;
335 p_access->info.i_pos += i_ret;
336 else if( i_ret == 0 )
337 p_access->info.b_eof = VLC_TRUE;
343 # define MMAP_SIZE (1 << 18)
348 //vlc_object_t *owner;
353 static void mmapRelease (block_t *block)
355 block_sys_t *p_sys = (block_sys_t *)block;
357 munmap (p_sys->base_addr, p_sys->length);
358 //vlc_object_release (p_sys->owner);
362 static block_t *mmapBlock (access_t *p_access)
364 access_sys_t *p_sys = p_access->p_sys;
366 const int flags = MAP_SHARED;
367 const size_t pagesize = sysconf (_SC_PAGE_SIZE);
368 off_t offset = p_access->info.i_pos & ~(pagesize - 1);
369 size_t align = p_access->info.i_pos & (pagesize - 1);
370 size_t length = (MMAP_SIZE > pagesize) ? MMAP_SIZE : pagesize;
373 if (p_access->info.i_pos >= p_access->info.i_size)
375 /* End of file - check that file size hasn't change... */
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 paste 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 msg_Dbg (p_access, "mapped %lu bytes at %p from offset %lu",
416 (unsigned long)length, addr, (unsigned long)offset);
417 block_sys_t *block = malloc (sizeof (*block));
420 munmap (addr, length);
424 block_Init (&block->self, ((uint8_t *)addr) + align, length - align);
425 block->self.pf_release = mmapRelease;
426 block->base_addr = addr;
427 block->length = length;
428 //vlc_object_yield (block->owner = VLC_OBJECT (p_access));
434 /*****************************************************************************
435 * Seek: seek to a specific location in a file
436 *****************************************************************************/
437 static int Seek (access_t *p_access, int64_t i_pos)
439 if (i_pos > p_access->info.i_size)
441 msg_Err (p_access, "seeking too far");
442 i_pos = p_access->info.i_size;
446 msg_Err (p_access, "seeking too early");
450 p_access->info.i_pos = i_pos;
451 p_access->info.b_eof = VLC_FALSE;
454 if (p_access->pf_block == NULL)
456 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
460 /*****************************************************************************
462 *****************************************************************************/
463 static int Control( access_t *p_access, int i_query, va_list args )
465 access_sys_t *p_sys = p_access->p_sys;
473 case ACCESS_CAN_SEEK:
474 case ACCESS_CAN_FASTSEEK:
475 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
476 *pb_bool = p_sys->b_seekable;
479 case ACCESS_CAN_PAUSE:
480 case ACCESS_CAN_CONTROL_PACE:
481 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
482 *pb_bool = p_sys->b_pace_control;
487 pi_int = (int*)va_arg( args, int * );
491 case ACCESS_GET_PTS_DELAY:
492 pi_64 = (int64_t*)va_arg( args, int64_t * );
493 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
497 case ACCESS_SET_PAUSE_STATE:
501 case ACCESS_GET_TITLE_INFO:
502 case ACCESS_SET_TITLE:
503 case ACCESS_SET_SEEKPOINT:
504 case ACCESS_SET_PRIVATE_ID_STATE:
505 case ACCESS_GET_META:
506 case ACCESS_GET_CONTENT_TYPE:
510 msg_Warn( p_access, "unimplemented query in control" );
518 static char *expand_path (const access_t *p_access, const char *path)
520 if (strncmp (path, "~/", 2) == 0)
524 // TODO: we should also support the ~cmassiot/ syntax
525 if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
531 if (!strcasecmp (p_access->psz_access, "file")
532 && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
533 // Explorer can open path such as file:/C:/ or file:///C:/
534 // hence remove leading / if found
535 return strdup (path + 1);
538 return strdup (path);
542 /*****************************************************************************
543 * open_file: Opens a specific file
544 *****************************************************************************/
545 static int open_file (access_t *p_access, const char *psz_name)
547 char *path = expand_path (p_access, psz_name);
550 p_sys->fd = utf8_fopen( path, "rb" );
553 msg_Err( p_access, "cannot open file %s", psz_name );
554 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
555 _("VLC could not open the file \"%s\"."), psz_name );
560 fseek( p_sys->fd, 0, SEEK_END );
561 p_access->info.i_size = ftell( p_sys->fd );
562 p_access->info.i_update |= INPUT_UPDATE_SIZE;
563 fseek( p_sys->fd, 0, SEEK_SET );
565 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
569 msg_Err (p_access, "cannot open file %s (%m)", psz_name);
570 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
571 _("VLC could not open the file \"%s\"."), psz_name);
575 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
576 /* We'd rather use any available memory for reading ahead
577 * than for caching what we've already seen/heard */
578 fcntl (fd, F_RDAHEAD, 1);
579 fcntl (fd, F_NOCACHE, 1);