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., 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)
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 "Allows you to modify the default caching value for file streams. This " \
83 "value should be set in millisecond units." )
84 #define CAT_TEXT N_("Concatenate with additional files")
85 #define CAT_LONGTEXT N_( \
86 "Allows you to play split files as if they were part of a unique file. " \
87 "Specify a comma-separated list of files." )
90 set_description( _("Standard filesystem 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 psz = malloc( strlen(p_access->p_vlc->psz_homedir)
168 + strlen(psz_name) );
169 /* This is incomplete : we should also support the ~cmassiot/
171 sprintf( psz, "%s/%s", p_access->p_vlc->psz_homedir, psz_name + 2 );
176 else if( !strcasecmp( p_access->psz_access, "file" )
177 && ('/' == psz_name[0]) && psz_name[1]
178 && (':' == psz_name[2]) && ('/' == psz_name[3]) )
181 ** explorer can open path such as file:/C:/ or file:///C:/...
182 ** hence remove leading / if found
188 #ifdef HAVE_SYS_STAT_H
189 psz = ToLocale( psz_name );
190 if( stat( psz, &stat_info ) )
192 msg_Warn( p_access, "%s: %s", psz_name, strerror( errno ) );
201 p_access->pf_read = Read;
202 p_access->pf_block = NULL;
203 p_access->pf_seek = Seek;
204 p_access->pf_control = Control;
205 p_access->info.i_update = 0;
206 p_access->info.i_size = 0;
207 p_access->info.i_pos = 0;
208 p_access->info.b_eof = VLC_FALSE;
209 p_access->info.i_title = 0;
210 p_access->info.i_seekpoint = 0;
211 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
212 p_sys->i_nb_reads = 0;
213 p_sys->b_kfir = VLC_FALSE;
221 if( !strcasecmp( p_access->psz_access, "stream" ) )
223 p_sys->b_seekable = VLC_FALSE;
224 p_sys->b_pace_control = VLC_FALSE;
226 else if( !strcasecmp( p_access->psz_access, "kfir" ) )
228 p_sys->b_seekable = VLC_FALSE;
229 p_sys->b_pace_control = VLC_FALSE;
230 p_sys->b_kfir = VLC_TRUE;
235 p_sys->b_pace_control = VLC_TRUE;
239 p_sys->b_seekable = VLC_FALSE;
244 /* We'll update i_size after it's been opened */
245 p_sys->b_seekable = VLC_TRUE;
247 #elif defined( HAVE_SYS_STAT_H )
248 else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode) ||
249 S_ISBLK(stat_info.st_mode) )
251 p_sys->b_seekable = VLC_TRUE;
252 p_access->info.i_size = stat_info.st_size;
254 else if( S_ISFIFO(stat_info.st_mode)
255 # if !defined( SYS_BEOS ) && !defined( WIN32 )
256 || S_ISSOCK(stat_info.st_mode)
260 p_sys->b_seekable = VLC_FALSE;
265 msg_Err( p_access, "unknown file type for `%s'", psz_name );
270 msg_Dbg( p_access, "opening file `%s'", psz_name );
276 else if( _OpenFile( p_access, psz_name ) )
283 if( p_sys->b_seekable && !p_access->info.i_size )
285 /* FIXME that's bad because all others access will be probed */
286 msg_Err( p_access, "file %s is empty, aborting", psz_name );
292 /* Update default_pts to a suitable value for file access */
293 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
296 * Get the additional list of files
298 p_file = malloc( sizeof(file_entry_t) );
299 p_file->i_size = p_access->info.i_size;
300 p_file->psz_name = psz_name;
301 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
303 psz = var_CreateGetString( p_access, "file-cat" );
306 char *psz_parser = psz_name = psz;
309 while( psz_name && *psz_name )
311 psz_parser = strchr( psz_name, ',' );
312 if( psz_parser ) *psz_parser = 0;
314 psz_name = strdup( psz_name );
317 msg_Dbg( p_access, "adding file `%s'", psz_name );
320 #ifdef HAVE_SYS_STAT_H
321 if( !stat( psz_name, &stat_info ) )
323 p_access->info.i_size += stat_info.st_size;
324 i_size = stat_info.st_size;
328 msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
331 p_file = malloc( sizeof(file_entry_t) );
332 p_file->i_size = i_size;
333 p_file->psz_name = psz_name;
335 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
338 psz_name = psz_parser;
339 if( psz_name ) psz_name++;
347 /*****************************************************************************
348 * Close: close the target
349 *****************************************************************************/
350 static void Close( vlc_object_t * p_this )
352 access_t *p_access = (access_t*)p_this;
353 access_sys_t *p_sys = p_access->p_sys;
358 for( i = 0; i < p_sys->i_file; i++ )
360 free( p_sys->file[i]->psz_name );
361 free( p_sys->file[i] );
368 /*****************************************************************************
369 * Read: standard read on a file descriptor.
370 *****************************************************************************/
371 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
373 access_sys_t *p_sys = p_access->p_sys;
376 #if !defined(WIN32) && !defined(UNDER_CE)
377 if( !p_sys->b_pace_control )
381 /* Find if some data is available. This won't work under Windows. */
382 struct timeval timeout;
385 /* Initialize file descriptor set */
387 FD_SET( p_sys->fd, &fds );
389 /* We'll wait 0.5 second if nothing happens */
391 timeout.tv_usec = 500000;
393 /* Find if some data is available */
394 while( (i_ret = select( p_sys->fd + 1, &fds, NULL, NULL, &timeout )) == 0
395 || (i_ret < 0 && errno == EINTR) )
398 FD_SET( p_sys->fd, &fds );
400 timeout.tv_usec = 500000;
402 if( p_access->b_die )
408 msg_Err( p_access, "select error (%s)", strerror(errno) );
412 i_ret = read( p_sys->fd, p_buffer, i_len );
416 /* b_kfir ; work around a buggy poll() driver implementation */
417 while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
420 msleep( INPUT_ERROR_SLEEP );
425 #endif /* WIN32 || UNDER_CE */
427 /* b_pace_control || WIN32 */
428 i_ret = read( p_sys->fd, p_buffer, i_len );
433 if( errno != EINTR && errno != EAGAIN )
434 msg_Err( p_access, "read failed (%s)", strerror(errno) );
436 /* Delay a bit to avoid consuming all the CPU. This is particularly
437 * useful when reading from an unconnected FIFO. */
438 msleep( INPUT_ERROR_SLEEP );
442 #ifdef HAVE_SYS_STAT_H
443 if( p_access->info.i_size != 0 &&
444 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
446 struct stat stat_info;
447 int i_file = p_sys->i_index;
449 if ( fstat( p_sys->fd, &stat_info ) == -1 )
451 msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
453 else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
455 p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
456 p_sys->file[i_file]->i_size = stat_info.st_size;
457 p_access->info.i_update |= INPUT_UPDATE_SIZE;
462 /* If we reached an EOF then switch to the next file in the list */
463 if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
465 char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
466 p_sys->fd_backup = p_sys->fd;
468 msg_Dbg( p_access, "opening file `%s'", psz_name );
470 if ( _OpenFile( p_access, psz_name ) )
472 p_sys->fd = p_sys->fd_backup;
476 close( p_sys->fd_backup );
478 /* We have to read some data */
479 return Read( p_access, p_buffer, i_len );
483 p_access->info.i_pos += i_ret;
484 else if( i_ret == 0 )
485 p_access->info.b_eof = VLC_TRUE;
490 /*****************************************************************************
491 * Seek: seek to a specific location in a file
492 *****************************************************************************/
493 static int Seek( access_t *p_access, int64_t i_pos )
495 access_sys_t *p_sys = p_access->p_sys;
498 /* Check which file we need to access */
499 if( p_sys->i_file > 1 )
503 p_sys->fd_backup = p_sys->fd;
505 for( i = 0; i < p_sys->i_file - 1; i++ )
507 if( i_pos < p_sys->file[i]->i_size + i_size )
509 i_size += p_sys->file[i]->i_size;
511 psz_name = p_sys->file[i]->psz_name;
513 msg_Dbg( p_access, "opening file `%s'", psz_name );
515 if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
518 close( p_sys->fd_backup );
523 p_sys->fd = p_sys->fd_backup;
527 lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
529 p_access->info.i_pos = i_pos;
530 if( p_access->info.i_size < p_access->info.i_pos )
532 msg_Err( p_access, "seeking too far" );
533 p_access->info.i_pos = p_access->info.i_size;
535 else if( p_access->info.i_pos < 0 )
537 msg_Err( p_access, "seeking too early" );
538 p_access->info.i_pos = 0;
541 p_access->info.b_eof = VLC_FALSE;
547 /*****************************************************************************
549 *****************************************************************************/
550 static int Control( access_t *p_access, int i_query, va_list args )
552 access_sys_t *p_sys = p_access->p_sys;
560 case ACCESS_CAN_SEEK:
561 case ACCESS_CAN_FASTSEEK:
562 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
563 *pb_bool = p_sys->b_seekable;
566 case ACCESS_CAN_PAUSE:
567 case ACCESS_CAN_CONTROL_PACE:
568 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
569 *pb_bool = p_sys->b_pace_control;
574 pi_int = (int*)va_arg( args, int * );
578 case ACCESS_GET_PTS_DELAY:
579 pi_64 = (int64_t*)va_arg( args, int64_t * );
580 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
584 case ACCESS_SET_PAUSE_STATE:
588 case ACCESS_GET_TITLE_INFO:
589 case ACCESS_SET_TITLE:
590 case ACCESS_SET_SEEKPOINT:
591 case ACCESS_SET_PRIVATE_ID_STATE:
592 case ACCESS_GET_META:
596 msg_Warn( p_access, "unimplemented query in control" );
604 /*****************************************************************************
605 * OpenFile: Opens a specific file
606 *****************************************************************************/
607 static int _OpenFile( access_t * p_access, const char * psz_name )
609 access_sys_t *p_sys = p_access->p_sys;
610 const char *psz_localname;
612 psz_localname = ToLocale( psz_name );
615 p_sys->fd = fopen( psz_localname, "rb" );
616 LocaleFree( psz_localname );
619 msg_Err( p_access, "cannot open file %s", psz_name );
623 fseek( p_sys->fd, 0, SEEK_END );
624 p_access->info.i_size = ftell( p_sys->fd );
625 p_access->info.i_update |= INPUT_UPDATE_SIZE;
626 fseek( p_sys->fd, 0, SEEK_SET );
629 p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
630 LocaleFree( psz_localname );
631 if ( p_sys->fd == -1 )
633 msg_Err( p_access, "cannot open file %s (%s)", psz_name,