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 );
153 #ifdef HAVE_SYS_STAT_H
154 struct stat stat_info;
156 file_entry_t * p_file;
157 vlc_bool_t b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
161 if( psz_name[0] == '~' && psz_name[1] == '/' )
165 /* This is incomplete : we should also support the ~cmassiot/
167 asprintf( &psz, "%s/%s", p_access->p_libvlc->psz_homedir, psz_name + 2 );
172 else if( !strcasecmp( p_access->psz_access, "file" )
173 && ('/' == psz_name[0]) && psz_name[1]
174 && (':' == psz_name[2]) && ('/' == psz_name[3]) )
177 ** explorer can open path such as file:/C:/ or file:///C:/...
178 ** hence remove leading / if found
180 strcpy( psz_name, p_access->psz_path + 1 );
184 #ifdef HAVE_SYS_STAT_H
185 if( utf8_stat( psz_name, &stat_info ) )
187 msg_Warn( p_access, "%s: %s", psz_name, strerror( errno ) );
194 STANDARD_READ_ACCESS_INIT;
195 p_sys->i_nb_reads = 0;
196 p_sys->b_kfir = VLC_FALSE;
204 if( !strcasecmp( p_access->psz_access, "stream" ) )
206 p_sys->b_seekable = VLC_FALSE;
207 p_sys->b_pace_control = VLC_FALSE;
209 else if( !strcasecmp( p_access->psz_access, "kfir" ) )
211 p_sys->b_seekable = VLC_FALSE;
212 p_sys->b_pace_control = VLC_FALSE;
213 p_sys->b_kfir = VLC_TRUE;
218 p_sys->b_pace_control = VLC_TRUE;
222 p_sys->b_seekable = VLC_FALSE;
227 /* We'll update i_size after it's been opened */
228 p_sys->b_seekable = VLC_TRUE;
230 #elif defined( HAVE_SYS_STAT_H )
231 else if( S_ISREG(stat_info.st_mode) || S_ISBLK(stat_info.st_mode)
232 || ( S_ISCHR(stat_info.st_mode) && (stat_info.st_size > 0) ) )
234 p_sys->b_seekable = VLC_TRUE;
235 p_access->info.i_size = stat_info.st_size;
237 else if( S_ISCHR(stat_info.st_mode) || S_ISFIFO(stat_info.st_mode)
239 || S_ISSOCK(stat_info.st_mode)
243 p_sys->b_seekable = VLC_FALSE;
248 msg_Err( p_access, "unknown file type for `%s'", psz_name );
249 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
250 _("\"%s\"'s file type is unknown."),
257 msg_Dbg( p_access, "opening file `%s'", psz_name );
263 else if( _OpenFile( p_access, psz_name ) )
270 if( p_sys->b_seekable && !p_access->info.i_size )
272 /* FIXME that's bad because all others access will be probed */
273 msg_Err( p_access, "file %s is empty, aborting", psz_name );
279 /* Update default_pts to a suitable value for file access */
280 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
283 * Get the additional list of files
285 p_file = malloc( sizeof(file_entry_t) );
286 p_file->i_size = p_access->info.i_size;
287 p_file->psz_name = psz_name;
288 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
290 psz_name = var_CreateGetString( p_access, "file-cat" );
293 char *psz_parser = psz_name;
296 while( psz_name && *psz_name )
298 psz_parser = strchr( psz_name, ',' );
299 if( psz_parser ) *psz_parser = 0;
301 psz_name = strdup( psz_name );
304 msg_Dbg( p_access, "adding file `%s'", psz_name );
307 #ifdef HAVE_SYS_STAT_H
308 if( !stat( psz_name, &stat_info ) )
310 p_access->info.i_size += stat_info.st_size;
311 i_size = stat_info.st_size;
315 msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
318 p_file = malloc( sizeof(file_entry_t) );
319 p_file->i_size = i_size;
320 p_file->psz_name = psz_name;
322 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
325 psz_name = psz_parser;
326 if( psz_name ) psz_name++;
334 /*****************************************************************************
335 * Close: close the target
336 *****************************************************************************/
337 static void Close( vlc_object_t * p_this )
339 access_t *p_access = (access_t*)p_this;
340 access_sys_t *p_sys = p_access->p_sys;
345 for( i = 0; i < p_sys->i_file; i++ )
347 free( p_sys->file[i]->psz_name );
348 free( p_sys->file[i] );
355 /*****************************************************************************
356 * Read: standard read on a file descriptor.
357 *****************************************************************************/
358 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
360 access_sys_t *p_sys = p_access->p_sys;
363 #if !defined(WIN32) && !defined(UNDER_CE)
364 if( !p_sys->b_pace_control )
368 /* Find if some data is available. This won't work under Windows. */
373 if( p_access->b_die )
376 memset (&ufd, 0, sizeof (ufd));
380 i_ret = poll( &ufd, 1, 500 );
385 msg_Err( p_access, "poll error: %s",
394 i_ret = read( p_sys->fd, p_buffer, i_len );
398 /* b_kfir ; work around a buggy poll() driver implementation */
399 while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
402 msleep( INPUT_ERROR_SLEEP );
407 #endif /* WIN32 || UNDER_CE */
409 /* b_pace_control || WIN32 */
410 i_ret = read( p_sys->fd, p_buffer, i_len );
415 if( errno != EINTR && errno != EAGAIN )
417 msg_Err( p_access, "read failed (%s)", strerror(errno) );
418 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
419 _("VLC could not read file \"%s\"."),
423 /* Delay a bit to avoid consuming all the CPU. This is particularly
424 * useful when reading from an unconnected FIFO. */
425 msleep( INPUT_ERROR_SLEEP );
429 #ifdef HAVE_SYS_STAT_H
430 if( p_access->info.i_size != 0 &&
431 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
433 struct stat stat_info;
434 int i_file = p_sys->i_index;
436 if ( fstat( p_sys->fd, &stat_info ) == -1 )
438 msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
440 else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
442 p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
443 p_sys->file[i_file]->i_size = stat_info.st_size;
444 p_access->info.i_update |= INPUT_UPDATE_SIZE;
449 /* If we reached an EOF then switch to the next file in the list */
450 if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
452 char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
453 p_sys->fd_backup = p_sys->fd;
455 msg_Dbg( p_access, "opening file `%s'", psz_name );
457 if ( _OpenFile( p_access, psz_name ) )
459 p_sys->fd = p_sys->fd_backup;
463 close( p_sys->fd_backup );
465 /* We have to read some data */
466 return Read( p_access, p_buffer, i_len );
470 p_access->info.i_pos += i_ret;
471 else if( i_ret == 0 )
472 p_access->info.b_eof = VLC_TRUE;
477 /*****************************************************************************
478 * Seek: seek to a specific location in a file
479 *****************************************************************************/
480 static int Seek( access_t *p_access, int64_t i_pos )
482 access_sys_t *p_sys = p_access->p_sys;
485 /* Check which file we need to access */
486 if( p_sys->i_file > 1 )
490 p_sys->fd_backup = p_sys->fd;
492 for( i = 0; i < p_sys->i_file - 1; i++ )
494 if( i_pos < p_sys->file[i]->i_size + i_size )
496 i_size += p_sys->file[i]->i_size;
498 psz_name = p_sys->file[i]->psz_name;
500 msg_Dbg( p_access, "opening file `%s'", psz_name );
502 if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
505 close( p_sys->fd_backup );
510 p_sys->fd = p_sys->fd_backup;
514 lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
516 p_access->info.i_pos = i_pos;
517 if( p_access->info.i_size < p_access->info.i_pos )
519 msg_Err( p_access, "seeking too far" );
520 p_access->info.i_pos = p_access->info.i_size;
522 else if( p_access->info.i_pos < 0 )
524 msg_Err( p_access, "seeking too early" );
525 p_access->info.i_pos = 0;
528 p_access->info.b_eof = VLC_FALSE;
534 /*****************************************************************************
536 *****************************************************************************/
537 static int Control( access_t *p_access, int i_query, va_list args )
539 access_sys_t *p_sys = p_access->p_sys;
547 case ACCESS_CAN_SEEK:
548 case ACCESS_CAN_FASTSEEK:
549 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
550 *pb_bool = p_sys->b_seekable;
553 case ACCESS_CAN_PAUSE:
554 case ACCESS_CAN_CONTROL_PACE:
555 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
556 *pb_bool = p_sys->b_pace_control;
561 pi_int = (int*)va_arg( args, int * );
565 case ACCESS_GET_PTS_DELAY:
566 pi_64 = (int64_t*)va_arg( args, int64_t * );
567 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
571 case ACCESS_SET_PAUSE_STATE:
575 case ACCESS_GET_TITLE_INFO:
576 case ACCESS_SET_TITLE:
577 case ACCESS_SET_SEEKPOINT:
578 case ACCESS_SET_PRIVATE_ID_STATE:
579 case ACCESS_GET_META:
583 msg_Warn( p_access, "unimplemented query in control" );
591 /*****************************************************************************
592 * OpenFile: Opens a specific file
593 *****************************************************************************/
594 static int _OpenFile( access_t * p_access, const char * psz_name )
596 access_sys_t *p_sys = p_access->p_sys;
599 p_sys->fd = utf8_fopen( psz_name, "rb" );
602 msg_Err( p_access, "cannot open file %s", psz_name );
603 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
604 _("VLC could not open file \"%s\"."), psz_name );
608 fseek( p_sys->fd, 0, SEEK_END );
609 p_access->info.i_size = ftell( p_sys->fd );
610 p_access->info.i_update |= INPUT_UPDATE_SIZE;
611 fseek( p_sys->fd, 0, SEEK_SET );
613 const char *psz_localname = ToLocale( psz_name );
614 if( psz_localname == NULL )
616 msg_Err( p_access, "incorrect file name %s", psz_name );
620 // FIXME: support non-ANSI filenames on Win32
621 p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
622 LocaleFree( psz_localname );
624 if ( p_sys->fd == -1 )
626 msg_Err( p_access, "cannot open file %s (%s)", psz_name,
628 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
629 _("VLC could not open file \"%s\" (%s)."),
630 psz_name, strerror(errno) );
634 #if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
635 /* We'd rather use any available memory for reading ahead
636 * than for caching what we've already seen/heard */
637 fcntl(p_sys->fd, F_RDAHEAD, 1);
638 fcntl(p_sys->fd, F_NOCACHE, 1);