1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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)
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Open ( vlc_object_t * );
76 static void Close( vlc_object_t * );
78 #define CACHING_TEXT N_("Caching value in ms")
79 #define CACHING_LONGTEXT N_( \
80 "Allows you to modify the default caching value for file streams. This " \
81 "value should be set in millisecond units." )
82 #define CAT_TEXT N_("Concatenate with additional files")
83 #define CAT_LONGTEXT N_( \
84 "Allows you to play split files as if they were part of a unique file. " \
85 "Specify a comma-separated list of files." )
88 set_description( _("Standard filesystem file input") );
89 set_name( _("File") );
90 set_category( CAT_INPUT );
91 set_subcategory( SUBCAT_INPUT_ACCESS );
92 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
93 add_string( "file-cat", NULL, NULL, CAT_TEXT, CAT_LONGTEXT, VLC_TRUE );
94 set_capability( "access2", 50 );
95 add_shortcut( "file" );
96 add_shortcut( "stream" );
97 add_shortcut( "kfir" );
98 set_callbacks( Open, Close );
102 /*****************************************************************************
103 * Exported prototypes
104 *****************************************************************************/
105 static int Seek( access_t *, int64_t );
106 static int Read( access_t *, uint8_t *, int );
107 static int Control( access_t *, int, va_list );
109 static int _OpenFile( access_t *, char * );
120 unsigned int i_nb_reads;
138 vlc_bool_t b_seekable;
139 vlc_bool_t b_pace_control;
142 /*****************************************************************************
143 * Open: open the file
144 *****************************************************************************/
145 static int Open( vlc_object_t *p_this )
147 access_t *p_access = (access_t*)p_this;
149 char *psz_name = p_access->psz_path;
152 #ifdef HAVE_SYS_STAT_H
154 struct stat stat_info;
158 file_entry_t * p_file;
161 b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
163 #ifdef HAVE_SYS_STAT_H
164 if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
166 msg_Warn( p_access, "cannot stat() file `%s' (%s)",
167 psz_name, strerror(errno));
172 p_access->pf_read = Read;
173 p_access->pf_block = NULL;
174 p_access->pf_seek = Seek;
175 p_access->pf_control = Control;
176 p_access->info.i_update = 0;
177 p_access->info.i_size = 0;
178 p_access->info.i_pos = 0;
179 p_access->info.b_eof = VLC_FALSE;
180 p_access->info.i_title = 0;
181 p_access->info.i_seekpoint = 0;
182 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
183 p_sys->i_nb_reads = 0;
184 p_sys->b_kfir = VLC_FALSE;
190 if( !strcasecmp( p_access->psz_access, "stream" ) )
192 p_sys->b_seekable = VLC_FALSE;
193 p_sys->b_pace_control = VLC_FALSE;
195 else if( !strcasecmp( p_access->psz_access, "kfir" ) )
197 p_sys->b_seekable = VLC_FALSE;
198 p_sys->b_pace_control = VLC_FALSE;
199 p_sys->b_kfir = VLC_TRUE;
204 p_sys->b_pace_control = VLC_TRUE;
208 p_sys->b_seekable = VLC_FALSE;
213 /* We'll update i_size after it's been opened */
214 p_sys->b_seekable = VLC_TRUE;
216 #elif defined( HAVE_SYS_STAT_H )
217 else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode) ||
218 S_ISBLK(stat_info.st_mode) )
220 p_sys->b_seekable = VLC_TRUE;
221 p_access->info.i_size = stat_info.st_size;
223 else if( S_ISFIFO(stat_info.st_mode)
224 # if !defined( SYS_BEOS ) && !defined( WIN32 )
225 || S_ISSOCK(stat_info.st_mode)
229 p_sys->b_seekable = VLC_FALSE;
234 msg_Err( p_access, "unknown file type for `%s'", psz_name );
239 msg_Dbg( p_access, "opening file `%s'", psz_name );
245 else if( _OpenFile( p_access, psz_name ) )
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", psz_name );
259 /* Update default_pts to a suitable value for file access */
260 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
263 * Get the additional list of files
265 p_file = malloc( sizeof(file_entry_t) );
266 p_file->i_size = p_access->info.i_size;
267 p_file->psz_name = strdup( psz_name );
268 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
270 psz = var_CreateGetString( p_access, "file-cat" );
273 char *psz_parser = psz_name = psz;
276 while( psz_name && *psz_name )
278 psz_parser = strchr( psz_name, ',' );
279 if( psz_parser ) *psz_parser = 0;
281 psz_name = strdup( psz_name );
284 msg_Dbg( p_access, "adding file `%s'", psz_name );
287 #ifdef HAVE_SYS_STAT_H
288 if( !stat( psz_name, &stat_info ) )
290 p_access->info.i_size += stat_info.st_size;
291 i_size = stat_info.st_size;
295 msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
298 p_file = malloc( sizeof(file_entry_t) );
299 p_file->i_size = i_size;
300 p_file->psz_name = psz_name;
302 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
305 psz_name = psz_parser;
306 if( psz_name ) psz_name++;
314 /*****************************************************************************
315 * Close: close the target
316 *****************************************************************************/
317 static void Close( vlc_object_t * p_this )
319 access_t *p_access = (access_t*)p_this;
320 access_sys_t *p_sys = p_access->p_sys;
325 for( i = 0; i < p_sys->i_file; i++ )
327 free( p_sys->file[i]->psz_name );
328 free( p_sys->file[i] );
335 /*****************************************************************************
336 * Read: standard read on a file descriptor.
337 *****************************************************************************/
338 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
340 access_sys_t *p_sys = p_access->p_sys;
343 #if !defined(WIN32) && !defined(UNDER_CE)
344 if( !p_sys->b_pace_control )
348 /* Find if some data is available. This won't work under Windows. */
349 struct timeval timeout;
352 /* Initialize file descriptor set */
354 FD_SET( p_sys->fd, &fds );
356 /* We'll wait 0.5 second if nothing happens */
358 timeout.tv_usec = 500000;
360 /* Find if some data is available */
361 while( (i_ret = select( p_sys->fd + 1, &fds, NULL, NULL, &timeout )) == 0
362 || (i_ret < 0 && errno == EINTR) )
365 FD_SET( p_sys->fd, &fds );
367 timeout.tv_usec = 500000;
369 if( p_access->b_die )
375 msg_Err( p_access, "select error (%s)", strerror(errno) );
379 i_ret = read( p_sys->fd, p_buffer, i_len );
383 /* b_kfir ; work around a buggy poll() driver implementation */
384 while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
387 msleep( INPUT_ERROR_SLEEP );
392 #endif /* WIN32 || UNDER_CE */
394 /* b_pace_control || WIN32 */
395 i_ret = read( p_sys->fd, p_buffer, i_len );
400 if( errno != EINTR && errno != EAGAIN )
401 msg_Err( p_access, "read failed (%s)", strerror(errno) );
403 /* Delay a bit to avoid consuming all the CPU. This is particularly
404 * useful when reading from an unconnected FIFO. */
405 msleep( INPUT_ERROR_SLEEP );
409 #ifdef HAVE_SYS_STAT_H
410 if( p_access->info.i_size != 0 &&
411 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
413 struct stat stat_info;
414 int i_file = p_sys->i_index;
416 if ( fstat( p_sys->fd, &stat_info ) == -1 )
418 msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
420 else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
422 p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
423 p_access->info.i_update |= INPUT_UPDATE_SIZE;
428 /* If we reached an EOF then switch to the next file in the list */
429 if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
431 char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
432 p_sys->fd_backup = p_sys->fd;
434 msg_Dbg( p_access, "opening file `%s'", psz_name );
436 if ( _OpenFile( p_access, psz_name ) )
438 p_sys->fd = p_sys->fd_backup;
442 close( p_sys->fd_backup );
444 /* We have to read some data */
445 return Read( p_access, p_buffer, i_len );
449 p_access->info.i_pos += i_ret;
450 else if( i_ret == 0 )
451 p_access->info.b_eof = VLC_TRUE;
456 /*****************************************************************************
457 * Seek: seek to a specific location in a file
458 *****************************************************************************/
459 static int Seek( access_t *p_access, int64_t i_pos )
461 access_sys_t *p_sys = p_access->p_sys;
464 /* Check which file we need to access */
465 if( p_sys->i_file > 1 )
469 p_sys->fd_backup = p_sys->fd;
471 for( i = 0; i < p_sys->i_file - 1; i++ )
473 if( i_pos < p_sys->file[i]->i_size + i_size )
475 i_size += p_sys->file[i]->i_size;
477 psz_name = p_sys->file[i]->psz_name;
479 msg_Dbg( p_access, "opening file `%s'", psz_name );
481 if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
484 close( p_sys->fd_backup );
489 p_sys->fd = p_sys->fd_backup;
493 lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
495 p_access->info.i_pos = i_pos;
496 if( p_access->info.i_size < p_access->info.i_pos )
498 msg_Err( p_access, "seeking too far" );
499 p_access->info.i_pos = p_access->info.i_size;
501 else if( p_access->info.i_pos < 0 )
503 msg_Err( p_access, "seeking too early" );
504 p_access->info.i_pos = 0;
507 p_access->info.b_eof = VLC_FALSE;
513 /*****************************************************************************
515 *****************************************************************************/
516 static int Control( access_t *p_access, int i_query, va_list args )
518 access_sys_t *p_sys = p_access->p_sys;
526 case ACCESS_CAN_SEEK:
527 case ACCESS_CAN_FASTSEEK:
528 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
529 *pb_bool = p_sys->b_seekable;
532 case ACCESS_CAN_PAUSE:
533 case ACCESS_CAN_CONTROL_PACE:
534 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
535 *pb_bool = p_sys->b_pace_control;
540 pi_int = (int*)va_arg( args, int * );
544 case ACCESS_GET_PTS_DELAY:
545 pi_64 = (int64_t*)va_arg( args, int64_t * );
546 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
550 case ACCESS_SET_PAUSE_STATE:
554 case ACCESS_GET_TITLE_INFO:
555 case ACCESS_SET_TITLE:
556 case ACCESS_SET_SEEKPOINT:
557 case ACCESS_SET_PRIVATE_ID_STATE:
561 msg_Warn( p_access, "unimplemented query in control" );
569 /*****************************************************************************
570 * OpenFile: Opens a specific file
571 *****************************************************************************/
572 static int _OpenFile( access_t * p_access, char * psz_name )
574 access_sys_t *p_sys = p_access->p_sys;
577 p_sys->fd = fopen( psz_name, "rb" );
580 msg_Err( p_access, "cannot open file %s" );
584 fseek( p_sys->fd, 0, SEEK_END );
585 p_access->info.i_size = ftell( p_sys->fd );
586 p_access->info.i_update |= INPUT_UPDATE_SIZE;
587 fseek( p_sys->fd, 0, SEEK_SET );
590 p_sys->fd = open( psz_name, O_NONBLOCK /*| O_LARGEFILE*/ );
591 if ( p_sys->fd == -1 )
593 msg_Err( p_access, "cannot open file %s (%s)", psz_name,