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 /* stat() support for large files on win32 */
54 # define stat _stati64
55 # define fstat(a,b) _fstati64(a,b)
59 # define lseek _lseeki64
60 #elif defined( UNDER_CE )
64 # define read(a,b,c) fread(b,1,c,a)
65 # define close(a) fclose(a)
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Open ( vlc_object_t * );
78 static void Close( vlc_object_t * );
80 #define CACHING_TEXT N_("Caching value in ms")
81 #define CACHING_LONGTEXT N_( \
82 "Caching value for files. This " \
83 "value should be set in milliseconds." )
84 #define CAT_TEXT N_("Concatenate with additional files")
85 #define CAT_LONGTEXT N_( \
86 "Play split files as if they were part of a unique file. " \
87 "You need to specify a comma-separated list of files." )
90 set_description( _("File input") );
91 set_shortname( _("File") );
92 set_category( CAT_INPUT );
93 set_subcategory( SUBCAT_INPUT_ACCESS );
94 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
95 add_string( "file-cat", NULL, NULL, CAT_TEXT, CAT_LONGTEXT, VLC_TRUE );
96 set_capability( "access2", 50 );
97 add_shortcut( "file" );
98 add_shortcut( "stream" );
99 add_shortcut( "kfir" );
100 set_callbacks( Open, Close );
104 /*****************************************************************************
105 * Exported prototypes
106 *****************************************************************************/
107 static int Seek( access_t *, int64_t );
108 static int Read( access_t *, uint8_t *, int );
109 static int Control( access_t *, int, va_list );
111 static int _OpenFile( access_t *, const char * );
122 unsigned int i_nb_reads;
140 vlc_bool_t b_seekable;
141 vlc_bool_t b_pace_control;
144 /*****************************************************************************
145 * Open: open the file
146 *****************************************************************************/
147 static int Open( vlc_object_t *p_this )
149 access_t *p_access = (access_t*)p_this;
151 char *psz_name = strdup( p_access->psz_path );
154 #ifdef HAVE_SYS_STAT_H
155 struct stat stat_info;
157 file_entry_t * p_file;
158 vlc_bool_t b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
162 if( psz_name[0] == '~' && psz_name[1] == '/' )
164 /* This is incomplete : we should also support the ~cmassiot/
166 asprintf( &psz, "%s/%s", p_access->p_libvlc->psz_homedir, psz_name + 2 );
171 else if( !strcasecmp( p_access->psz_access, "file" )
172 && ('/' == psz_name[0]) && psz_name[1]
173 && (':' == psz_name[2]) && ('/' == psz_name[3]) )
176 ** explorer can open path such as file:/C:/ or file:///C:/...
177 ** hence remove leading / if found
179 strcpy( psz_name, p_access->psz_path + 1 );
183 #ifdef HAVE_SYS_STAT_H
184 if( utf8_stat( psz_name, &stat_info ) )
186 msg_Warn( p_access, "%s: %s", psz_name, strerror( errno ) );
193 STANDARD_READ_ACCESS_INIT;
194 p_sys->i_nb_reads = 0;
195 p_sys->b_kfir = VLC_FALSE;
203 if( !strcasecmp( p_access->psz_access, "stream" ) )
205 p_sys->b_seekable = VLC_FALSE;
206 p_sys->b_pace_control = VLC_FALSE;
208 else if( !strcasecmp( p_access->psz_access, "kfir" ) )
210 p_sys->b_seekable = VLC_FALSE;
211 p_sys->b_pace_control = VLC_FALSE;
212 p_sys->b_kfir = VLC_TRUE;
217 p_sys->b_pace_control = VLC_TRUE;
221 p_sys->b_seekable = VLC_FALSE;
226 /* We'll update i_size after it's been opened */
227 p_sys->b_seekable = VLC_TRUE;
229 #elif defined( HAVE_SYS_STAT_H )
230 else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode) ||
231 S_ISBLK(stat_info.st_mode) )
233 p_sys->b_seekable = VLC_TRUE;
234 p_access->info.i_size = stat_info.st_size;
236 else if( S_ISFIFO(stat_info.st_mode)
237 # if !defined( SYS_BEOS ) && !defined( WIN32 )
238 || S_ISSOCK(stat_info.st_mode)
242 p_sys->b_seekable = VLC_FALSE;
247 msg_Err( p_access, "unknown file type for `%s'", psz_name );
248 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
249 _("\"%s\"'s file type is unknown."),
256 msg_Dbg( p_access, "opening file `%s'", psz_name );
262 else if( _OpenFile( p_access, psz_name ) )
269 if( p_sys->b_seekable && !p_access->info.i_size )
271 /* FIXME that's bad because all others access will be probed */
272 msg_Err( p_access, "file %s is empty, aborting", psz_name );
278 /* Update default_pts to a suitable value for file access */
279 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
282 * Get the additional list of files
284 p_file = malloc( sizeof(file_entry_t) );
285 p_file->i_size = p_access->info.i_size;
286 p_file->psz_name = psz_name;
287 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
289 psz = var_CreateGetString( p_access, "file-cat" );
292 char *psz_parser = psz_name = psz;
295 while( psz_name && *psz_name )
297 psz_parser = strchr( psz_name, ',' );
298 if( psz_parser ) *psz_parser = 0;
300 psz_name = strdup( psz_name );
303 msg_Dbg( p_access, "adding file `%s'", psz_name );
306 #ifdef HAVE_SYS_STAT_H
307 if( !stat( psz_name, &stat_info ) )
309 p_access->info.i_size += stat_info.st_size;
310 i_size = stat_info.st_size;
314 msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
317 p_file = malloc( sizeof(file_entry_t) );
318 p_file->i_size = i_size;
319 p_file->psz_name = psz_name;
321 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
324 psz_name = psz_parser;
325 if( psz_name ) psz_name++;
333 /*****************************************************************************
334 * Close: close the target
335 *****************************************************************************/
336 static void Close( vlc_object_t * p_this )
338 access_t *p_access = (access_t*)p_this;
339 access_sys_t *p_sys = p_access->p_sys;
344 for( i = 0; i < p_sys->i_file; i++ )
346 free( p_sys->file[i]->psz_name );
347 free( p_sys->file[i] );
354 /*****************************************************************************
355 * Read: standard read on a file descriptor.
356 *****************************************************************************/
357 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
359 access_sys_t *p_sys = p_access->p_sys;
362 #if !defined(WIN32) && !defined(UNDER_CE)
363 if( !p_sys->b_pace_control )
367 /* Find if some data is available. This won't work under Windows. */
372 if( p_access->b_die )
375 memset (&ufd, 0, sizeof (ufd));
379 i_ret = poll( &ufd, 1, 500 );
384 msg_Err( p_access, "poll error: %s",
393 i_ret = read( p_sys->fd, p_buffer, i_len );
397 /* b_kfir ; work around a buggy poll() driver implementation */
398 while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
401 msleep( INPUT_ERROR_SLEEP );
406 #endif /* WIN32 || UNDER_CE */
408 /* b_pace_control || WIN32 */
409 i_ret = read( p_sys->fd, p_buffer, i_len );
414 if( errno != EINTR && errno != EAGAIN )
416 msg_Err( p_access, "read failed (%s)", strerror(errno) );
417 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
418 _("VLC could not read file \"%s\"."),
422 /* Delay a bit to avoid consuming all the CPU. This is particularly
423 * useful when reading from an unconnected FIFO. */
424 msleep( INPUT_ERROR_SLEEP );
428 #ifdef HAVE_SYS_STAT_H
429 if( p_access->info.i_size != 0 &&
430 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
432 struct stat stat_info;
433 int i_file = p_sys->i_index;
435 if ( fstat( p_sys->fd, &stat_info ) == -1 )
437 msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
439 else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
441 p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
442 p_sys->file[i_file]->i_size = stat_info.st_size;
443 p_access->info.i_update |= INPUT_UPDATE_SIZE;
448 /* If we reached an EOF then switch to the next file in the list */
449 if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
451 char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
452 p_sys->fd_backup = p_sys->fd;
454 msg_Dbg( p_access, "opening file `%s'", psz_name );
456 if ( _OpenFile( p_access, psz_name ) )
458 p_sys->fd = p_sys->fd_backup;
462 close( p_sys->fd_backup );
464 /* We have to read some data */
465 return Read( p_access, p_buffer, i_len );
469 p_access->info.i_pos += i_ret;
470 else if( i_ret == 0 )
471 p_access->info.b_eof = VLC_TRUE;
476 /*****************************************************************************
477 * Seek: seek to a specific location in a file
478 *****************************************************************************/
479 static int Seek( access_t *p_access, int64_t i_pos )
481 access_sys_t *p_sys = p_access->p_sys;
484 /* Check which file we need to access */
485 if( p_sys->i_file > 1 )
489 p_sys->fd_backup = p_sys->fd;
491 for( i = 0; i < p_sys->i_file - 1; i++ )
493 if( i_pos < p_sys->file[i]->i_size + i_size )
495 i_size += p_sys->file[i]->i_size;
497 psz_name = p_sys->file[i]->psz_name;
499 msg_Dbg( p_access, "opening file `%s'", psz_name );
501 if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
504 close( p_sys->fd_backup );
509 p_sys->fd = p_sys->fd_backup;
513 lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
515 p_access->info.i_pos = i_pos;
516 if( p_access->info.i_size < p_access->info.i_pos )
518 msg_Err( p_access, "seeking too far" );
519 p_access->info.i_pos = p_access->info.i_size;
521 else if( p_access->info.i_pos < 0 )
523 msg_Err( p_access, "seeking too early" );
524 p_access->info.i_pos = 0;
527 p_access->info.b_eof = VLC_FALSE;
533 /*****************************************************************************
535 *****************************************************************************/
536 static int Control( access_t *p_access, int i_query, va_list args )
538 access_sys_t *p_sys = p_access->p_sys;
546 case ACCESS_CAN_SEEK:
547 case ACCESS_CAN_FASTSEEK:
548 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
549 *pb_bool = p_sys->b_seekable;
552 case ACCESS_CAN_PAUSE:
553 case ACCESS_CAN_CONTROL_PACE:
554 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
555 *pb_bool = p_sys->b_pace_control;
560 pi_int = (int*)va_arg( args, int * );
564 case ACCESS_GET_PTS_DELAY:
565 pi_64 = (int64_t*)va_arg( args, int64_t * );
566 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
570 case ACCESS_SET_PAUSE_STATE:
574 case ACCESS_GET_TITLE_INFO:
575 case ACCESS_SET_TITLE:
576 case ACCESS_SET_SEEKPOINT:
577 case ACCESS_SET_PRIVATE_ID_STATE:
578 case ACCESS_GET_META:
582 msg_Warn( p_access, "unimplemented query in control" );
590 /*****************************************************************************
591 * OpenFile: Opens a specific file
592 *****************************************************************************/
593 static int _OpenFile( access_t * p_access, const char * psz_name )
595 access_sys_t *p_sys = p_access->p_sys;
598 p_sys->fd = utf8_fopen( psz_name, "rb" );
601 msg_Err( p_access, "cannot open file %s", psz_name );
602 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
603 _("VLC could not open file \"%s\"."), psz_name );
607 fseek( p_sys->fd, 0, SEEK_END );
608 p_access->info.i_size = ftell( p_sys->fd );
609 p_access->info.i_update |= INPUT_UPDATE_SIZE;
610 fseek( p_sys->fd, 0, SEEK_SET );
612 const char *psz_localname = ToLocale( psz_name );
613 if( psz_localname == NULL )
615 msg_Err( p_access, "incorrect file name %s", psz_name );
619 // FIXME: support non-ANSI filenames on Win32
620 p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
621 LocaleFree( psz_localname );
623 if ( p_sys->fd == -1 )
625 msg_Err( p_access, "cannot open file %s (%s)", psz_name,
627 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
628 _("VLC could not open file \"%s\" (%s)."),
629 psz_name, strerror(errno) );
633 #if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
634 /* We'd rather use any available memory for reading ahead
635 * than for caching what we've already seen/heard */
636 fcntl(p_sys->fd, F_RDAHEAD, 1);
637 fcntl(p_sys->fd, F_NOCACHE, 1);