1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/input.h>
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
39 #ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
48 #elif 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;
159 file_entry_t * p_file;
161 b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
165 if( psz_name[0] == '~' && psz_name[1] == '/' )
167 /* This is incomplete : we should also support the ~cmassiot/
169 asprintf( &psz, "%s/%s", p_access->p_vlc->psz_homedir, psz_name + 2 );
174 else if( !strcasecmp( p_access->psz_access, "file" )
175 && ('/' == psz_name[0]) && psz_name[1]
176 && (':' == psz_name[2]) && ('/' == psz_name[3]) )
179 ** explorer can open path such as file:/C:/ or file:///C:/...
180 ** hence remove leading / if found
186 #ifdef HAVE_SYS_STAT_H
187 if( utf8_stat( psz_name, &stat_info ) )
189 msg_Warn( p_access, "%s: %s", psz_name, strerror( errno ) );
196 p_access->pf_read = Read;
197 p_access->pf_block = NULL;
198 p_access->pf_seek = Seek;
199 p_access->pf_control = Control;
200 p_access->info.i_update = 0;
201 p_access->info.i_size = 0;
202 p_access->info.i_pos = 0;
203 p_access->info.b_eof = VLC_FALSE;
204 p_access->info.i_title = 0;
205 p_access->info.i_seekpoint = 0;
206 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
207 p_sys->i_nb_reads = 0;
208 p_sys->b_kfir = VLC_FALSE;
216 if( !strcasecmp( p_access->psz_access, "stream" ) )
218 p_sys->b_seekable = VLC_FALSE;
219 p_sys->b_pace_control = VLC_FALSE;
221 else if( !strcasecmp( p_access->psz_access, "kfir" ) )
223 p_sys->b_seekable = VLC_FALSE;
224 p_sys->b_pace_control = VLC_FALSE;
225 p_sys->b_kfir = VLC_TRUE;
230 p_sys->b_pace_control = VLC_TRUE;
234 p_sys->b_seekable = VLC_FALSE;
239 /* We'll update i_size after it's been opened */
240 p_sys->b_seekable = VLC_TRUE;
242 #elif defined( HAVE_SYS_STAT_H )
243 else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode) ||
244 S_ISBLK(stat_info.st_mode) )
246 p_sys->b_seekable = VLC_TRUE;
247 p_access->info.i_size = stat_info.st_size;
249 else if( S_ISFIFO(stat_info.st_mode)
250 # if !defined( SYS_BEOS ) && !defined( WIN32 )
251 || S_ISSOCK(stat_info.st_mode)
255 p_sys->b_seekable = VLC_FALSE;
260 msg_Err( p_access, "unknown file type for `%s'", psz_name );
265 msg_Dbg( p_access, "opening file `%s'", psz_name );
271 else if( _OpenFile( p_access, psz_name ) )
278 if( p_sys->b_seekable && !p_access->info.i_size )
280 /* FIXME that's bad because all others access will be probed */
281 msg_Err( p_access, "file %s is empty, aborting", psz_name );
287 /* Update default_pts to a suitable value for file access */
288 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
291 * Get the additional list of files
293 p_file = malloc( sizeof(file_entry_t) );
294 p_file->i_size = p_access->info.i_size;
295 p_file->psz_name = psz_name;
296 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
298 psz = var_CreateGetString( p_access, "file-cat" );
301 char *psz_parser = psz_name = psz;
304 while( psz_name && *psz_name )
306 psz_parser = strchr( psz_name, ',' );
307 if( psz_parser ) *psz_parser = 0;
309 psz_name = strdup( psz_name );
312 msg_Dbg( p_access, "adding file `%s'", psz_name );
315 #ifdef HAVE_SYS_STAT_H
316 if( !stat( psz_name, &stat_info ) )
318 p_access->info.i_size += stat_info.st_size;
319 i_size = stat_info.st_size;
323 msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
326 p_file = malloc( sizeof(file_entry_t) );
327 p_file->i_size = i_size;
328 p_file->psz_name = psz_name;
330 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
333 psz_name = psz_parser;
334 if( psz_name ) psz_name++;
342 /*****************************************************************************
343 * Close: close the target
344 *****************************************************************************/
345 static void Close( vlc_object_t * p_this )
347 access_t *p_access = (access_t*)p_this;
348 access_sys_t *p_sys = p_access->p_sys;
353 for( i = 0; i < p_sys->i_file; i++ )
355 free( p_sys->file[i]->psz_name );
356 free( p_sys->file[i] );
363 /*****************************************************************************
364 * Read: standard read on a file descriptor.
365 *****************************************************************************/
366 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
368 access_sys_t *p_sys = p_access->p_sys;
371 #if !defined(WIN32) && !defined(UNDER_CE)
372 if( !p_sys->b_pace_control )
376 /* Find if some data is available. This won't work under Windows. */
377 struct timeval timeout;
380 /* Initialize file descriptor set */
382 FD_SET( p_sys->fd, &fds );
384 /* We'll wait 0.5 second if nothing happens */
386 timeout.tv_usec = 500000;
388 /* Find if some data is available */
389 while( (i_ret = select( p_sys->fd + 1, &fds, NULL, NULL, &timeout )) == 0
390 || (i_ret < 0 && errno == EINTR) )
393 FD_SET( p_sys->fd, &fds );
395 timeout.tv_usec = 500000;
397 if( p_access->b_die )
403 msg_Err( p_access, "select error (%s)", strerror(errno) );
407 i_ret = read( p_sys->fd, p_buffer, i_len );
411 /* b_kfir ; work around a buggy poll() driver implementation */
412 while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
415 msleep( INPUT_ERROR_SLEEP );
420 #endif /* WIN32 || UNDER_CE */
422 /* b_pace_control || WIN32 */
423 i_ret = read( p_sys->fd, p_buffer, i_len );
428 if( errno != EINTR && errno != EAGAIN )
429 msg_Err( p_access, "read failed (%s)", strerror(errno) );
431 /* Delay a bit to avoid consuming all the CPU. This is particularly
432 * useful when reading from an unconnected FIFO. */
433 msleep( INPUT_ERROR_SLEEP );
437 #ifdef HAVE_SYS_STAT_H
438 if( p_access->info.i_size != 0 &&
439 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
441 struct stat stat_info;
442 int i_file = p_sys->i_index;
444 if ( fstat( p_sys->fd, &stat_info ) == -1 )
446 msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
448 else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
450 p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
451 p_sys->file[i_file]->i_size = stat_info.st_size;
452 p_access->info.i_update |= INPUT_UPDATE_SIZE;
457 /* If we reached an EOF then switch to the next file in the list */
458 if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
460 char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
461 p_sys->fd_backup = p_sys->fd;
463 msg_Dbg( p_access, "opening file `%s'", psz_name );
465 if ( _OpenFile( p_access, psz_name ) )
467 p_sys->fd = p_sys->fd_backup;
471 close( p_sys->fd_backup );
473 /* We have to read some data */
474 return Read( p_access, p_buffer, i_len );
478 p_access->info.i_pos += i_ret;
479 else if( i_ret == 0 )
480 p_access->info.b_eof = VLC_TRUE;
485 /*****************************************************************************
486 * Seek: seek to a specific location in a file
487 *****************************************************************************/
488 static int Seek( access_t *p_access, int64_t i_pos )
490 access_sys_t *p_sys = p_access->p_sys;
493 /* Check which file we need to access */
494 if( p_sys->i_file > 1 )
498 p_sys->fd_backup = p_sys->fd;
500 for( i = 0; i < p_sys->i_file - 1; i++ )
502 if( i_pos < p_sys->file[i]->i_size + i_size )
504 i_size += p_sys->file[i]->i_size;
506 psz_name = p_sys->file[i]->psz_name;
508 msg_Dbg( p_access, "opening file `%s'", psz_name );
510 if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
513 close( p_sys->fd_backup );
518 p_sys->fd = p_sys->fd_backup;
522 lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
524 p_access->info.i_pos = i_pos;
525 if( p_access->info.i_size < p_access->info.i_pos )
527 msg_Err( p_access, "seeking too far" );
528 p_access->info.i_pos = p_access->info.i_size;
530 else if( p_access->info.i_pos < 0 )
532 msg_Err( p_access, "seeking too early" );
533 p_access->info.i_pos = 0;
536 p_access->info.b_eof = VLC_FALSE;
542 /*****************************************************************************
544 *****************************************************************************/
545 static int Control( access_t *p_access, int i_query, va_list args )
547 access_sys_t *p_sys = p_access->p_sys;
555 case ACCESS_CAN_SEEK:
556 case ACCESS_CAN_FASTSEEK:
557 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
558 *pb_bool = p_sys->b_seekable;
561 case ACCESS_CAN_PAUSE:
562 case ACCESS_CAN_CONTROL_PACE:
563 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
564 *pb_bool = p_sys->b_pace_control;
569 pi_int = (int*)va_arg( args, int * );
573 case ACCESS_GET_PTS_DELAY:
574 pi_64 = (int64_t*)va_arg( args, int64_t * );
575 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
579 case ACCESS_SET_PAUSE_STATE:
583 case ACCESS_GET_TITLE_INFO:
584 case ACCESS_SET_TITLE:
585 case ACCESS_SET_SEEKPOINT:
586 case ACCESS_SET_PRIVATE_ID_STATE:
587 case ACCESS_GET_META:
591 msg_Warn( p_access, "unimplemented query in control" );
599 /*****************************************************************************
600 * OpenFile: Opens a specific file
601 *****************************************************************************/
602 static int _OpenFile( access_t * p_access, const char * psz_name )
604 access_sys_t *p_sys = p_access->p_sys;
607 p_sys->fd = utf8_fopen( psz_name, "rb" );
610 msg_Err( p_access, "cannot open file %s", psz_name );
614 fseek( p_sys->fd, 0, SEEK_END );
615 p_access->info.i_size = ftell( p_sys->fd );
616 p_access->info.i_update |= INPUT_UPDATE_SIZE;
617 fseek( p_sys->fd, 0, SEEK_SET );
619 const char *psz_localname = ToLocale( psz_name );
620 if( psz_localname == NULL )
622 msg_Err( p_access, "incorrect file name %s", psz_name );
626 p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
627 LocaleFree( psz_localname );
628 if ( p_sys->fd == -1 )
630 msg_Err( p_access, "cannot open file %s (%s)", psz_name,
635 #if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
636 /* We'd rather use any available memory for reading ahead
637 * than for caching what we've already seen/heard */
638 fcntl(p_sys->fd, F_RDAHEAD, 1);
639 fcntl(p_sys->fd, F_NOCACHE, 1);