1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan # org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc/input.h>
30 #include <vlc_interaction.h>
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
45 #if defined( WIN32 ) && !defined( UNDER_CE )
52 #if defined( WIN32 ) && !defined( UNDER_CE )
53 /* fstat() support for large files on win32 */
54 # define fstat(a,b) _fstati64(a,b)
58 # define lseek _lseeki64
59 #elif defined( UNDER_CE )
63 # define read(a,b,c) fread(b,1,c,a)
64 # define close(a) fclose(a)
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Open ( vlc_object_t * );
77 static void Close( vlc_object_t * );
79 #define CACHING_TEXT N_("Caching value in ms")
80 #define CACHING_LONGTEXT N_( \
81 "Caching value for files. This " \
82 "value should be set in milliseconds." )
83 #define CAT_TEXT N_("Concatenate with additional files")
84 #define CAT_LONGTEXT N_( \
85 "Play split files as if they were part of a unique file. " \
86 "You need to specify a comma-separated list of files." )
89 set_description( _("File input") );
90 set_shortname( _("File") );
91 set_category( CAT_INPUT );
92 set_subcategory( SUBCAT_INPUT_ACCESS );
93 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
94 add_string( "file-cat", NULL, NULL, CAT_TEXT, CAT_LONGTEXT, VLC_TRUE );
95 set_capability( "access2", 50 );
96 add_shortcut( "file" );
97 add_shortcut( "stream" );
98 add_shortcut( "kfir" );
99 set_callbacks( Open, Close );
103 /*****************************************************************************
104 * Exported prototypes
105 *****************************************************************************/
106 static int Seek( access_t *, int64_t );
107 static int Read( access_t *, uint8_t *, int );
108 static int Control( access_t *, int, va_list );
110 static int _OpenFile( access_t *, const char * );
121 unsigned int i_nb_reads;
137 vlc_bool_t b_seekable;
138 vlc_bool_t b_pace_control;
141 /*****************************************************************************
142 * Open: open the file
143 *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
146 access_t *p_access = (access_t*)p_this;
148 char *psz_name = strdup( p_access->psz_path );
150 #ifdef HAVE_SYS_STAT_H
151 struct stat stat_info;
153 file_entry_t * p_file;
154 vlc_bool_t b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
158 if( psz_name[0] == '~' && psz_name[1] == '/' )
162 /* This is incomplete : we should also support the ~cmassiot/
164 asprintf( &psz, "%s/%s", p_access->p_libvlc->psz_homedir, psz_name + 2 );
169 else if( !strcasecmp( p_access->psz_access, "file" )
170 && ('/' == psz_name[0]) && psz_name[1]
171 && (':' == psz_name[2]) && ('/' == psz_name[3]) )
174 ** explorer can open path such as file:/C:/ or file:///C:/...
175 ** hence remove leading / if found
177 strcpy( psz_name, p_access->psz_path + 1 );
182 STANDARD_READ_ACCESS_INIT;
183 p_sys->i_nb_reads = 0;
184 p_sys->b_kfir = VLC_FALSE;
192 msg_Dbg( p_access, "opening file `%s'", psz_name );
197 _OpenFile( p_access, psz_name );
199 if( p_sys->fd == -1 )
206 #ifdef HAVE_SYS_STAT_H
207 if( fstat( p_sys->fd, &stat_info ) )
209 msg_Err( p_access, "%s: %s", p_access->psz_path, strerror( errno ) );
217 if( !strcasecmp( p_access->psz_access, "stream" ) )
219 p_sys->b_seekable = VLC_FALSE;
220 p_sys->b_pace_control = VLC_FALSE;
222 else if( !strcasecmp( p_access->psz_access, "kfir" ) )
224 p_sys->b_seekable = VLC_FALSE;
225 p_sys->b_pace_control = VLC_FALSE;
226 p_sys->b_kfir = VLC_TRUE;
231 p_sys->b_pace_control = VLC_TRUE;
234 p_sys->b_seekable = VLC_FALSE;
236 #if defined( HAVE_SYS_STAT_H )
237 if( S_ISREG(stat_info.st_mode) || S_ISBLK(stat_info.st_mode)
238 || ( S_ISCHR(stat_info.st_mode) && (stat_info.st_size > 0) ) )
240 p_sys->b_seekable = VLC_TRUE;
241 p_access->info.i_size = stat_info.st_size;
244 p_sys->b_seekable = VLC_FALSE;
246 /* We'll update i_size after it's been opened */
247 p_sys->b_seekable = VLC_TRUE;
251 if( p_sys->b_seekable && !p_access->info.i_size )
253 /* FIXME that's bad because all others access will be probed */
254 msg_Err( p_access, "file %s is empty, aborting", p_access->psz_path );
261 /* Update default_pts to a suitable value for file access */
262 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
265 * Get the additional list of files
267 p_file = malloc( sizeof(file_entry_t) );
268 p_file->i_size = p_access->info.i_size;
269 p_file->psz_name = psz_name;
270 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
272 psz_name = var_CreateGetString( p_access, "file-cat" );
275 char *psz_parser = psz_name;
278 while( psz_name && *psz_name )
280 psz_parser = strchr( psz_name, ',' );
281 if( psz_parser ) *psz_parser = 0;
283 psz_name = strdup( psz_name );
286 msg_Dbg( p_access, "adding file `%s'", psz_name );
289 #ifdef HAVE_SYS_STAT_H
290 if( !stat( psz_name, &stat_info ) )
292 p_access->info.i_size += stat_info.st_size;
293 i_size = stat_info.st_size;
297 msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
300 p_file = malloc( sizeof(file_entry_t) );
301 p_file->i_size = i_size;
302 p_file->psz_name = psz_name;
304 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
307 psz_name = psz_parser;
308 if( psz_name ) psz_name++;
316 /*****************************************************************************
317 * Close: close the target
318 *****************************************************************************/
319 static void Close( vlc_object_t * p_this )
321 access_t *p_access = (access_t*)p_this;
322 access_sys_t *p_sys = p_access->p_sys;
327 for( i = 0; i < p_sys->i_file; i++ )
329 free( p_sys->file[i]->psz_name );
330 free( p_sys->file[i] );
337 /*****************************************************************************
338 * Read: standard read on a file descriptor.
339 *****************************************************************************/
340 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
342 access_sys_t *p_sys = p_access->p_sys;
345 #if !defined(WIN32) && !defined(UNDER_CE)
346 if( !p_sys->b_pace_control )
350 /* Find if some data is available. This won't work under Windows. */
355 if( p_access->b_die )
358 memset (&ufd, 0, sizeof (ufd));
362 i_ret = poll( &ufd, 1, 500 );
367 msg_Err( p_access, "poll error: %s",
376 i_ret = read( p_sys->fd, p_buffer, i_len );
380 /* b_kfir ; work around a buggy poll() driver implementation */
381 while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
384 msleep( INPUT_ERROR_SLEEP );
389 #endif /* WIN32 || UNDER_CE */
391 /* b_pace_control || WIN32 */
392 i_ret = read( p_sys->fd, p_buffer, i_len );
397 if( errno != EINTR && errno != EAGAIN )
399 msg_Err( p_access, "read failed (%s)", strerror(errno) );
400 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
401 _("VLC could not read file \"%s\"."),
405 /* Delay a bit to avoid consuming all the CPU. This is particularly
406 * useful when reading from an unconnected FIFO. */
407 msleep( INPUT_ERROR_SLEEP );
411 #ifdef HAVE_SYS_STAT_H
412 if( p_access->info.i_size != 0 &&
413 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
415 struct stat stat_info;
416 int i_file = p_sys->i_index;
418 if ( fstat( p_sys->fd, &stat_info ) == -1 )
420 msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
422 else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
424 p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
425 p_sys->file[i_file]->i_size = stat_info.st_size;
426 p_access->info.i_update |= INPUT_UPDATE_SIZE;
431 /* If we reached an EOF then switch to the next file in the list */
432 if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
434 char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
435 int fd_backup = p_sys->fd;
437 msg_Dbg( p_access, "opening file `%s'", psz_name );
439 if ( _OpenFile( p_access, psz_name ) )
441 p_sys->fd = fd_backup;
447 /* We have to read some data */
448 return Read( p_access, p_buffer, i_len );
452 p_access->info.i_pos += i_ret;
453 else if( i_ret == 0 )
454 p_access->info.b_eof = VLC_TRUE;
459 /*****************************************************************************
460 * Seek: seek to a specific location in a file
461 *****************************************************************************/
462 static int Seek( access_t *p_access, int64_t i_pos )
464 access_sys_t *p_sys = p_access->p_sys;
467 /* Check which file we need to access */
468 if( p_sys->i_file > 1 )
472 int fd_backup = p_sys->fd;
474 for( i = 0; i < p_sys->i_file - 1; i++ )
476 if( i_pos < p_sys->file[i]->i_size + i_size )
478 i_size += p_sys->file[i]->i_size;
480 psz_name = p_sys->file[i]->psz_name;
482 msg_Dbg( p_access, "opening file `%s'", psz_name );
484 if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
492 p_sys->fd = fd_backup;
496 lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
498 p_access->info.i_pos = i_pos;
499 if( p_access->info.i_size < p_access->info.i_pos )
501 msg_Err( p_access, "seeking too far" );
502 p_access->info.i_pos = p_access->info.i_size;
504 else if( p_access->info.i_pos < 0 )
506 msg_Err( p_access, "seeking too early" );
507 p_access->info.i_pos = 0;
510 p_access->info.b_eof = VLC_FALSE;
516 /*****************************************************************************
518 *****************************************************************************/
519 static int Control( access_t *p_access, int i_query, va_list args )
521 access_sys_t *p_sys = p_access->p_sys;
529 case ACCESS_CAN_SEEK:
530 case ACCESS_CAN_FASTSEEK:
531 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
532 *pb_bool = p_sys->b_seekable;
535 case ACCESS_CAN_PAUSE:
536 case ACCESS_CAN_CONTROL_PACE:
537 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
538 *pb_bool = p_sys->b_pace_control;
543 pi_int = (int*)va_arg( args, int * );
547 case ACCESS_GET_PTS_DELAY:
548 pi_64 = (int64_t*)va_arg( args, int64_t * );
549 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
553 case ACCESS_SET_PAUSE_STATE:
557 case ACCESS_GET_TITLE_INFO:
558 case ACCESS_SET_TITLE:
559 case ACCESS_SET_SEEKPOINT:
560 case ACCESS_SET_PRIVATE_ID_STATE:
561 case ACCESS_GET_META:
565 msg_Warn( p_access, "unimplemented query in control" );
573 /*****************************************************************************
574 * OpenFile: Opens a specific file
575 *****************************************************************************/
576 static int _OpenFile( access_t * p_access, const char * psz_name )
578 access_sys_t *p_sys = p_access->p_sys;
581 p_sys->fd = utf8_fopen( psz_name, "rb" );
584 msg_Err( p_access, "cannot open file %s", psz_name );
585 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
586 _("VLC could not open file \"%s\"."), psz_name );
590 fseek( p_sys->fd, 0, SEEK_END );
591 p_access->info.i_size = ftell( p_sys->fd );
592 p_access->info.i_update |= INPUT_UPDATE_SIZE;
593 fseek( p_sys->fd, 0, SEEK_SET );
595 const char *psz_localname = ToLocale( psz_name );
596 if( psz_localname == NULL )
598 msg_Err( p_access, "incorrect file name %s", psz_name );
602 // FIXME: support non-ANSI filenames on Win32
603 p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
604 LocaleFree( psz_localname );
606 if ( p_sys->fd == -1 )
608 msg_Err( p_access, "cannot open file %s (%s)", psz_name,
610 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
611 _("VLC could not open file \"%s\" (%s)."),
612 psz_name, strerror(errno) );
616 #if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
617 /* We'd rather use any available memory for reading ahead
618 * than for caching what we've already seen/heard */
619 fcntl(p_sys->fd, F_RDAHEAD, 1);
620 fcntl(p_sys->fd, F_NOCACHE, 1);