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 *****************************************************************************/
34 #include <vlc_input.h>
35 #include <vlc_access.h>
36 #include <vlc_interface.h>
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
50 #if defined( WIN32 ) && !defined( UNDER_CE )
57 # include <sys/mman.h>
60 #if defined( WIN32 ) && !defined( UNDER_CE )
64 # define lseek _lseeki64
65 #elif defined( UNDER_CE )
69 # define read(a,b,c) fread(b,1,c,a)
70 # define close(a) fclose(a)
77 #include <vlc_charset.h>
79 /*****************************************************************************
81 *****************************************************************************/
82 static int Open ( vlc_object_t * );
83 static void Close( vlc_object_t * );
85 #define CACHING_TEXT N_("Caching value in ms")
86 #define CACHING_LONGTEXT N_( \
87 "Caching value for files. This " \
88 "value should be set in milliseconds." )
89 #define CAT_TEXT N_("Concatenate with additional files")
90 #define CAT_LONGTEXT N_( \
91 "Play split files as if they were part of a unique file. " \
92 "You need to specify a comma-separated list of files." )
95 set_description( _("File input") );
96 set_shortname( _("File") );
97 set_category( CAT_INPUT );
98 set_subcategory( SUBCAT_INPUT_ACCESS );
99 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
100 add_obsolete_string( "file-cat" );
101 set_capability( "access2", 50 );
102 add_shortcut( "file" );
103 add_shortcut( "stream" );
104 add_shortcut( "kfir" );
105 set_callbacks( Open, Close );
109 /*****************************************************************************
110 * Exported prototypes
111 *****************************************************************************/
112 static int Seek( access_t *, int64_t );
113 static ssize_t Read( access_t *, uint8_t *, size_t );
114 static int Control( access_t *, int, va_list );
116 static block_t *mmapBlock( access_t * );
119 static int open_file( access_t *, const char * );
124 unsigned int i_nb_reads;
130 vlc_bool_t b_seekable;
131 vlc_bool_t b_pace_control;
134 /*****************************************************************************
135 * Open: open the file
136 *****************************************************************************/
137 static int Open( vlc_object_t *p_this )
139 access_t *p_access = (access_t*)p_this;
142 vlc_bool_t b_stdin = !strcmp (p_access->psz_path, "-");
144 /* Update default_pts to a suitable value for file access */
145 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
147 STANDARD_READ_ACCESS_INIT;
148 p_sys->i_nb_reads = 0;
149 p_sys->b_kfir = VLC_FALSE;
150 int fd = p_sys->fd = -1;
152 if (!strcasecmp (p_access->psz_access, "stream"))
154 p_sys->b_seekable = VLC_FALSE;
155 p_sys->b_pace_control = VLC_FALSE;
157 else if (!strcasecmp (p_access->psz_access, "kfir"))
159 p_sys->b_seekable = VLC_FALSE;
160 p_sys->b_pace_control = VLC_FALSE;
161 p_sys->b_kfir = VLC_TRUE;
165 p_sys->b_seekable = VLC_TRUE;
166 p_sys->b_pace_control = VLC_TRUE;
170 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
175 fd = open_file (p_access, p_access->psz_path);
177 #ifdef HAVE_SYS_STAT_H
183 msg_Err (p_access, "fstat(%d): %m", fd);
185 if (S_ISDIR (st.st_mode))
186 /* The directory plugin takes care of that */
187 msg_Dbg (p_access, "file is a directory, aborting");
203 #ifdef HAVE_SYS_STAT_H
204 p_access->info.i_size = st.st_size;
205 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
206 && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
207 p_sys->b_seekable = VLC_FALSE;
210 p_sys->pagemask = sysconf (_SC_PAGE_SIZE) - 1;
212 /* Autodetect mmap() support */
213 if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
215 /* TODO: Do not allow PROT_WRITE, we should not need it.
216 * However, this far, "block" ownership seems such that whoever
217 * "receives" a block can freely modify its content. Hence we _may_
218 * need PROT_WRITE not to default memory protection.
219 * NOTE: With MAP_PRIVATE, changes are not committed to the underlying
220 * file, write open permission is not required.
222 void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
223 if (addr != MAP_FAILED)
225 /* Does the file system support mmap? */
227 p_access->pf_read = NULL;
228 p_access->pf_block = mmapBlock;
229 msg_Dbg (p_this, "mmap enabled");
232 msg_Dbg (p_this, "mmap disabled (%m)");
235 msg_Dbg (p_this, "mmap disabled (non regular file)");
238 p_sys->b_seekable = !b_stdin;
239 # warning File size not known!
242 if (p_sys->b_seekable && (p_access->info.i_size == 0))
244 /* FIXME that's bad because all others access will be probed */
245 msg_Err (p_access, "file is empty, aborting");
253 /*****************************************************************************
254 * Close: close the target
255 *****************************************************************************/
256 static void Close (vlc_object_t * p_this)
258 access_t *p_access = (access_t*)p_this;
259 access_sys_t *p_sys = p_access->p_sys;
265 /*****************************************************************************
266 * Read: standard read on a file descriptor.
267 *****************************************************************************/
268 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
270 access_sys_t *p_sys = p_access->p_sys;
274 #if !defined(WIN32) && !defined(UNDER_CE)
275 if( !p_sys->b_pace_control )
279 /* Find if some data is available. This won't work under Windows. */
284 if( p_access->b_die )
287 memset (&ufd, 0, sizeof (ufd));
291 i_ret = poll (&ufd, 1, 500);
295 i_ret = read (fd, p_buffer, i_len);
299 /* b_kfir ; work around a buggy poll() driver implementation */
300 while (((i_ret = read (fd, p_buffer, i_len)) == 0)
303 msleep( INPUT_ERROR_SLEEP );
308 #endif /* WIN32 || UNDER_CE */
309 /* b_pace_control || WIN32 */
310 i_ret = read( fd, p_buffer, i_len );
321 msg_Err (p_access, "read failed (%m)");
322 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
323 _("VLC could not read the file."));
326 /* Delay a bit to avoid consuming all the CPU. This is particularly
327 * useful when reading from an unconnected FIFO. */
328 msleep( INPUT_ERROR_SLEEP );
333 #ifdef HAVE_SYS_STAT_H
334 if( p_access->info.i_size != 0 &&
335 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
339 if ((fstat (fd, &st) == 0)
340 && (p_access->info.i_size != st.st_size))
342 p_access->info.i_size = st.st_size;
343 p_access->info.i_update |= INPUT_UPDATE_SIZE;
349 p_access->info.i_pos += i_ret;
350 else if( i_ret == 0 )
351 p_access->info.b_eof = VLC_TRUE;
357 # define MMAP_SIZE (1 << 20)
359 static block_t *mmapBlock (access_t *p_access)
361 access_sys_t *p_sys = p_access->p_sys;
363 const int flags = MAP_SHARED;
364 off_t offset = p_access->info.i_pos & ~p_sys->pagemask;
365 size_t align = p_access->info.i_pos & p_sys->pagemask;
366 size_t length = (MMAP_SIZE > p_sys->pagemask) ? MMAP_SIZE : (p_sys->pagemask + 1);
370 int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
371 if (dbgpos != p_access->info.i_pos)
372 msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
373 p_access->info.i_pos, dbgpos);
376 if (p_access->info.i_pos >= p_access->info.i_size)
378 /* End of file - check if file size changed... */
381 if ((fstat (p_sys->fd, &st) == 0)
382 && (st.st_size != p_access->info.i_size))
384 p_access->info.i_size = st.st_size;
385 p_access->info.i_update |= INPUT_UPDATE_SIZE;
388 /* Really at end of file then */
389 if (p_access->info.i_pos >= p_access->info.i_size)
391 p_access->info.b_eof = VLC_TRUE;
392 msg_Dbg (p_access, "at end of memory mapped file");
397 if (offset + length > p_access->info.i_size)
398 /* Don't mmap beyond end of file */
399 length = p_access->info.i_size - offset;
401 assert (offset <= p_access->info.i_pos); /* and */
402 assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
403 assert (offset < p_access->info.i_size); /* imply */
406 addr = mmap (NULL, length, PROT_READ, flags, p_sys->fd, offset);
407 if (addr == MAP_FAILED)
409 msg_Err (p_access, "memory mapping failed (%m)");
410 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
411 _("VLC could not read the file."));
412 msleep( INPUT_ERROR_SLEEP );
416 p_access->info.i_pos = offset + length;
418 block_t *block = block_mmap_Alloc (addr, length);
422 block->p_buffer += align;
423 block->i_buffer -= align;
426 msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
427 (unsigned long)length, addr, (unsigned long)offset);
429 /* Compare normal I/O with memory mapping */
430 char *buf = malloc (block->i_buffer);
431 ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
433 if (i_read != (ssize_t)block->i_buffer)
434 msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
435 (unsigned)block->i_buffer);
436 if (memcmp (buf, block->p_buffer, block->i_buffer))
437 msg_Err (p_access, "inconsistent data buffer");
445 /*****************************************************************************
446 * Seek: seek to a specific location in a file
447 *****************************************************************************/
448 static int Seek (access_t *p_access, int64_t i_pos)
450 /* FIXME: i_size should really be unsigned */
451 if ((uint64_t)i_pos > (uint64_t)p_access->info.i_size)
453 /* This should only happen with corrupted files.
454 * But it also seems to happen with buggy demuxes (ASF) */
455 msg_Err (p_access, "seeking too far (0x"I64Fx" / 0x"I64Fx")",
456 i_pos, p_access->info.i_size);
457 i_pos = p_access->info.i_size;
460 p_access->info.i_pos = i_pos;
461 p_access->info.b_eof = VLC_FALSE;
463 #if defined (HAVE_MMAP) && defined (NDEBUG)
464 if (p_access->pf_block == NULL)
466 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
470 /*****************************************************************************
472 *****************************************************************************/
473 static int Control( access_t *p_access, int i_query, va_list args )
475 access_sys_t *p_sys = p_access->p_sys;
483 case ACCESS_CAN_SEEK:
484 case ACCESS_CAN_FASTSEEK:
485 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
486 *pb_bool = p_sys->b_seekable;
489 case ACCESS_CAN_PAUSE:
490 case ACCESS_CAN_CONTROL_PACE:
491 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
492 *pb_bool = p_sys->b_pace_control;
497 pi_int = (int*)va_arg( args, int * );
501 case ACCESS_GET_PTS_DELAY:
502 pi_64 = (int64_t*)va_arg( args, int64_t * );
503 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
507 case ACCESS_SET_PAUSE_STATE:
511 case ACCESS_GET_TITLE_INFO:
512 case ACCESS_SET_TITLE:
513 case ACCESS_SET_SEEKPOINT:
514 case ACCESS_SET_PRIVATE_ID_STATE:
515 case ACCESS_GET_META:
516 case ACCESS_GET_CONTENT_TYPE:
520 msg_Warn( p_access, "unimplemented query in control" );
528 static char *expand_path (const access_t *p_access, const char *path)
530 if (strncmp (path, "~/", 2) == 0)
534 // TODO: we should also support the ~cmassiot/ syntax
535 if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
541 if (!strcasecmp (p_access->psz_access, "file")
542 && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
543 // Explorer can open path such as file:/C:/ or file:///C:/
544 // hence remove leading / if found
545 return strdup (path + 1);
548 return strdup (path);
552 /*****************************************************************************
553 * open_file: Opens a specific file
554 *****************************************************************************/
555 static int open_file (access_t *p_access, const char *psz_name)
557 char *path = expand_path (p_access, psz_name);
560 p_sys->fd = utf8_fopen( path, "rb" );
563 msg_Err( p_access, "cannot open file %s", psz_name );
564 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
565 _("VLC could not open the file \"%s\"."), psz_name );
570 fseek( p_sys->fd, 0, SEEK_END );
571 p_access->info.i_size = ftell( p_sys->fd );
572 p_access->info.i_update |= INPUT_UPDATE_SIZE;
573 fseek( p_sys->fd, 0, SEEK_SET );
575 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
579 msg_Err (p_access, "cannot open file %s (%m)", psz_name);
580 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
581 _("VLC could not open the file \"%s\"."), psz_name);
585 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
586 /* We'd rather use any available memory for reading ahead
587 * than for caching what we've already seen/heard */
588 fcntl (fd, F_RDAHEAD, 1);
589 fcntl (fd, F_NOCACHE, 1);