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 int Read( access_t *, uint8_t *, int );
110 static int Control( access_t *, int, va_list );
111 static block_t *mmapBlock( access_t * );
113 static int open_file( access_t *, const char * );
117 unsigned int i_nb_reads;
123 vlc_bool_t b_seekable;
124 vlc_bool_t b_pace_control;
127 /*****************************************************************************
128 * Open: open the file
129 *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
132 access_t *p_access = (access_t*)p_this;
135 vlc_bool_t b_stdin = !strcmp (p_access->psz_path, "-");
137 /* Update default_pts to a suitable value for file access */
138 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
140 STANDARD_READ_ACCESS_INIT;
141 p_sys->i_nb_reads = 0;
142 p_sys->b_kfir = VLC_FALSE;
143 int fd = p_sys->fd = -1;
145 if (!strcasecmp (p_access->psz_access, "stream"))
147 p_sys->b_seekable = VLC_FALSE;
148 p_sys->b_pace_control = VLC_FALSE;
150 else if (!strcasecmp (p_access->psz_access, "kfir"))
152 p_sys->b_seekable = VLC_FALSE;
153 p_sys->b_pace_control = VLC_FALSE;
154 p_sys->b_kfir = VLC_TRUE;
158 p_sys->b_seekable = VLC_TRUE;
159 p_sys->b_pace_control = VLC_TRUE;
163 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
168 fd = open_file (p_access, p_access->psz_path);
170 #ifdef HAVE_SYS_STAT_H
176 msg_Err (p_access, "fstat(%d): %m", fd);
178 if (S_ISDIR (st.st_mode))
179 /* The directory plugin takes care of that */
180 msg_Dbg (p_access, "file is a directory, aborting");
196 #ifdef HAVE_SYS_STAT_H
197 p_access->info.i_size = st.st_size;
198 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
199 && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
200 p_sys->b_seekable = VLC_FALSE;
203 /* Autodetect mmap() support */
204 if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
206 void *addr = mmap (NULL, 1, PROT_READ, MAP_PRIVATE, fd, 0);
207 if (addr != MAP_FAILED)
209 /* Does the file system support mmap? */
211 p_access->pf_read = NULL;
212 p_access->pf_block = mmapBlock;
213 msg_Dbg (p_this, "mmap enabled");
216 msg_Dbg (p_this, "mmap disabled (%m)");
219 msg_Dbg (p_this, "mmap disabled (non regular file)");
222 p_sys->b_seekable = !b_stdin;
223 # warning File size not known!
226 if (p_sys->b_seekable && (p_access->info.i_size == 0))
228 /* FIXME that's bad because all others access will be probed */
229 msg_Err (p_access, "file is empty, aborting");
237 /*****************************************************************************
238 * Close: close the target
239 *****************************************************************************/
240 static void Close (vlc_object_t * p_this)
242 access_t *p_access = (access_t*)p_this;
243 access_sys_t *p_sys = p_access->p_sys;
249 /*****************************************************************************
250 * Read: standard read on a file descriptor.
251 *****************************************************************************/
252 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
254 access_sys_t *p_sys = p_access->p_sys;
258 #if !defined(WIN32) && !defined(UNDER_CE)
259 if( !p_sys->b_pace_control )
263 /* Find if some data is available. This won't work under Windows. */
268 if( p_access->b_die )
271 memset (&ufd, 0, sizeof (ufd));
275 i_ret = poll (&ufd, 1, 500);
279 i_ret = read (fd, p_buffer, i_len);
283 /* b_kfir ; work around a buggy poll() driver implementation */
284 while (((i_ret = read (fd, p_buffer, i_len)) == 0)
287 msleep( INPUT_ERROR_SLEEP );
292 #endif /* WIN32 || UNDER_CE */
293 /* b_pace_control || WIN32 */
294 i_ret = read( fd, p_buffer, i_len );
305 msg_Err (p_access, "read failed (%m)");
306 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
307 _("VLC could not read the file."));
310 /* Delay a bit to avoid consuming all the CPU. This is particularly
311 * useful when reading from an unconnected FIFO. */
312 msleep( INPUT_ERROR_SLEEP );
317 #ifdef HAVE_SYS_STAT_H
318 if( p_access->info.i_size != 0 &&
319 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
323 if ((fstat (fd, &st) == 0)
324 && (p_access->info.i_size != st.st_size))
326 p_access->info.i_size = st.st_size;
327 p_access->info.i_update |= INPUT_UPDATE_SIZE;
333 p_access->info.i_pos += i_ret;
334 else if( i_ret == 0 )
335 p_access->info.b_eof = VLC_TRUE;
341 # define MMAP_SIZE (1 << 18)
346 //vlc_object_t *owner;
351 static void mmapRelease (block_t *block)
353 block_sys_t *p_sys = (block_sys_t *)block;
355 munmap (p_sys->base_addr, p_sys->length);
356 //vlc_object_release (p_sys->owner);
360 static block_t *mmapBlock (access_t *p_access)
362 access_sys_t *p_sys = p_access->p_sys;
364 const int flags = MAP_SHARED;
365 const size_t pagesize = sysconf (_SC_PAGE_SIZE);
366 off_t offset = p_access->info.i_pos & ~(pagesize - 1);
367 size_t align = p_access->info.i_pos & (pagesize - 1);
368 size_t length = (MMAP_SIZE > pagesize) ? MMAP_SIZE : pagesize;
371 if (p_access->info.i_pos >= p_access->info.i_size)
373 /* End of file - check that file size hasn't change... */
376 if ((fstat (p_sys->fd, &st) == 0)
377 && (st.st_size != p_access->info.i_size))
379 p_access->info.i_size = st.st_size;
380 p_access->info.i_update |= INPUT_UPDATE_SIZE;
383 /* Really at end of file then */
384 if (p_access->info.i_pos >= p_access->info.i_size)
386 p_access->info.b_eof = VLC_TRUE;
387 msg_Dbg (p_access, "at end of memory mapped file");
392 if (offset + length > p_access->info.i_size)
393 /* Don't mmap paste end of file */
394 length = p_access->info.i_size - offset;
396 assert (offset <= p_access->info.i_pos); /* and */
397 assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
398 assert (offset < p_access->info.i_size); /* imply */
401 addr = mmap (NULL, length, PROT_READ, flags, p_sys->fd, offset);
402 if (addr == MAP_FAILED)
404 msg_Err (p_access, "memory mapping failed (%m)");
405 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
406 _("VLC could not read the file."));
407 msleep( INPUT_ERROR_SLEEP );
411 p_access->info.i_pos = offset + length;
413 msg_Dbg (p_access, "mapped %lu bytes at %p from offset %lu",
414 (unsigned long)length, addr, (unsigned long)offset);
415 block_sys_t *block = malloc (sizeof (*block));
418 munmap (addr, length);
422 block_Init (&block->self, ((uint8_t *)addr) + align, length - align);
423 block->self.pf_release = mmapRelease;
424 block->base_addr = addr;
425 block->length = length;
426 //vlc_object_yield (block->owner = VLC_OBJECT (p_access));
432 /*****************************************************************************
433 * Seek: seek to a specific location in a file
434 *****************************************************************************/
435 static int Seek (access_t *p_access, int64_t i_pos)
437 if (i_pos > p_access->info.i_size)
439 msg_Err (p_access, "seeking too far");
440 i_pos = p_access->info.i_size;
444 msg_Err (p_access, "seeking too early");
448 p_access->info.i_pos = i_pos;
449 p_access->info.b_eof = VLC_FALSE;
452 if (p_access->pf_block == NULL)
453 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
458 /*****************************************************************************
460 *****************************************************************************/
461 static int Control( access_t *p_access, int i_query, va_list args )
463 access_sys_t *p_sys = p_access->p_sys;
471 case ACCESS_CAN_SEEK:
472 case ACCESS_CAN_FASTSEEK:
473 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
474 *pb_bool = p_sys->b_seekable;
477 case ACCESS_CAN_PAUSE:
478 case ACCESS_CAN_CONTROL_PACE:
479 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
480 *pb_bool = p_sys->b_pace_control;
485 pi_int = (int*)va_arg( args, int * );
489 case ACCESS_GET_PTS_DELAY:
490 pi_64 = (int64_t*)va_arg( args, int64_t * );
491 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
495 case ACCESS_SET_PAUSE_STATE:
499 case ACCESS_GET_TITLE_INFO:
500 case ACCESS_SET_TITLE:
501 case ACCESS_SET_SEEKPOINT:
502 case ACCESS_SET_PRIVATE_ID_STATE:
503 case ACCESS_GET_META:
504 case ACCESS_GET_CONTENT_TYPE:
508 msg_Warn( p_access, "unimplemented query in control" );
516 static char *expand_path (const access_t *p_access, const char *path)
518 if (strncmp (path, "~/", 2) == 0)
522 // TODO: we should also support the ~cmassiot/ syntax
523 if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
529 if (!strcasecmp (p_access->psz_access, "file")
530 && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
531 // Explorer can open path such as file:/C:/ or file:///C:/
532 // hence remove leading / if found
533 return strdup (path + 1);
536 return strdup (path);
540 /*****************************************************************************
541 * open_file: Opens a specific file
542 *****************************************************************************/
543 static int open_file (access_t *p_access, const char *psz_name)
545 char *path = expand_path (p_access, psz_name);
548 p_sys->fd = utf8_fopen( path, "rb" );
551 msg_Err( p_access, "cannot open file %s", psz_name );
552 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
553 _("VLC could not open the file \"%s\"."), psz_name );
558 fseek( p_sys->fd, 0, SEEK_END );
559 p_access->info.i_size = ftell( p_sys->fd );
560 p_access->info.i_update |= INPUT_UPDATE_SIZE;
561 fseek( p_sys->fd, 0, SEEK_SET );
563 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
567 msg_Err (p_access, "cannot open file %s (%m)", psz_name);
568 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
569 _("VLC could not open the file \"%s\"."), psz_name);
573 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
574 /* We'd rather use any available memory for reading ahead
575 * than for caching what we've already seen/heard */
576 fcntl (fd, F_RDAHEAD, 1);
577 fcntl (fd, F_NOCACHE, 1);