]> git.sesse.net Git - vlc/blob - modules/access/file.c
- use existing default route when configuring crossbars, allowing users to configure...
[vlc] / modules / access / file.c
1 /*****************************************************************************
2  * file.c: file input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #ifdef HAVE_SYS_TYPES_H
34 #   include <sys/types.h>
35 #endif
36 #ifdef HAVE_SYS_TIME_H
37 #   include <sys/time.h>
38 #endif
39 #ifdef HAVE_SYS_STAT_H
40 #   include <sys/stat.h>
41 #endif
42 #ifdef HAVE_FCNTL_H
43 #   include <fcntl.h>
44 #endif
45
46 #ifdef HAVE_UNISTD_H
47 #   include <unistd.h>
48 #elif defined( WIN32 ) && !defined( UNDER_CE )
49 #   include <io.h>
50 #endif
51
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)
56 #   ifdef lseek
57 #      undef lseek
58 #   endif
59 #   define lseek _lseeki64
60 #elif defined( UNDER_CE )
61 #   ifdef read
62 #      undef read
63 #   endif
64 #   define read(a,b,c) fread(b,1,c,a)
65 #   define close(a) fclose(a)
66 #   ifdef lseek
67 #      undef lseek
68 #   endif
69 #   define lseek fseek
70 #endif
71
72 /*****************************************************************************
73  * Module descriptor
74  *****************************************************************************/
75 static int  Open ( vlc_object_t * );
76 static void Close( vlc_object_t * );
77
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." )
86
87 vlc_module_begin();
88     set_description( _("Standard filesystem file input") );
89     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
90     add_string( "file-cat", NULL, NULL, CAT_TEXT, CAT_LONGTEXT, VLC_TRUE );
91     set_capability( "access2", 50 );
92     add_shortcut( "file" );
93     add_shortcut( "stream" );
94     add_shortcut( "kfir" );
95     set_callbacks( Open, Close );
96 vlc_module_end();
97
98
99 /*****************************************************************************
100  * Exported prototypes
101  *****************************************************************************/
102 static int  Seek( access_t *, int64_t );
103 static int  Read( access_t *, uint8_t *, int );
104 static int  Control( access_t *, int, va_list );
105
106 static int  _OpenFile( access_t *, char * );
107
108 typedef struct
109 {
110     char     *psz_name;
111     int64_t  i_size;
112
113 } file_entry_t;
114
115 struct access_sys_t
116 {
117     unsigned int i_nb_reads;
118     vlc_bool_t   b_kfir;
119
120     /* Files list */
121     int          i_file;
122     file_entry_t **file;
123
124     /* Current file */
125     int  i_index;
126 #ifndef UNDER_CE
127     int  fd;
128     int  fd_backup;
129 #else
130     FILE *fd;
131     FILE *fd_backup;
132 #endif
133
134     /* */
135     vlc_bool_t b_seekable;
136     vlc_bool_t b_pace_control;
137 };
138
139 /*****************************************************************************
140  * Open: open the file
141  *****************************************************************************/
142 static int Open( vlc_object_t *p_this )
143 {
144     access_t     *p_access = (access_t*)p_this;
145     access_sys_t *p_sys;
146     char *psz_name = p_access->psz_path;
147     char *psz;
148
149 #ifdef HAVE_SYS_STAT_H
150     int                 i_stat;
151     struct stat         stat_info;
152 #endif
153     vlc_bool_t          b_stdin;
154
155     file_entry_t *      p_file;
156
157
158     b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
159
160 #ifdef HAVE_SYS_STAT_H
161     if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
162     {
163         msg_Warn( p_access, "cannot stat() file `%s' (%s)",
164                   psz_name, strerror(errno));
165         return VLC_EGENERIC;
166     }
167 #endif
168
169     p_access->pf_read = Read;
170     p_access->pf_block = NULL;
171     p_access->pf_seek = Seek;
172     p_access->pf_control = Control;
173     p_access->info.i_update = 0;
174     p_access->info.i_size = 0;
175     p_access->info.i_pos = 0;
176     p_access->info.b_eof = VLC_FALSE;
177     p_access->info.i_title = 0;
178     p_access->info.i_seekpoint = 0;
179     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
180     p_sys->i_nb_reads = 0;
181     p_sys->b_kfir = VLC_FALSE;
182     p_sys->file = NULL;
183     p_sys->i_file = 0;
184     p_sys->i_index = 0;
185     p_sys->fd = -1;
186
187     if( !strcasecmp( p_access->psz_access, "stream" ) )
188     {
189         p_sys->b_seekable = VLC_FALSE;
190         p_sys->b_pace_control = VLC_FALSE;
191     }
192     else if( !strcasecmp( p_access->psz_access, "kfir" ) )
193     {
194         p_sys->b_seekable = VLC_FALSE;
195         p_sys->b_pace_control = VLC_FALSE;
196         p_sys->b_kfir = VLC_TRUE;
197     }
198     else
199     {
200         /* file:%s or %s */
201         p_sys->b_pace_control = VLC_TRUE;
202
203         if( b_stdin )
204         {
205             p_sys->b_seekable = VLC_FALSE;
206         }
207 #ifdef UNDER_CE
208         else if( VLC_TRUE )
209         {
210             /* We'll update i_size after it's been opened */
211             p_sys->b_seekable = VLC_TRUE;
212         }
213 #elif defined( HAVE_SYS_STAT_H )
214         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode) ||
215                  S_ISBLK(stat_info.st_mode) )
216         {
217             p_sys->b_seekable = VLC_TRUE;
218             p_access->info.i_size = stat_info.st_size;
219         }
220         else if( S_ISFIFO(stat_info.st_mode)
221 #   if !defined( SYS_BEOS ) && !defined( WIN32 )
222                   || S_ISSOCK(stat_info.st_mode)
223 #   endif
224                )
225         {
226             p_sys->b_seekable = VLC_FALSE;
227         }
228 #endif
229         else
230         {
231             msg_Err( p_access, "unknown file type for `%s'", psz_name );
232             return VLC_EGENERIC;
233         }
234     }
235
236     msg_Dbg( p_access, "opening file `%s'", psz_name );
237
238     if( b_stdin )
239     {
240         p_sys->fd = 0;
241     }
242     else if( _OpenFile( p_access, psz_name ) )
243     {
244         free( p_sys );
245         return VLC_EGENERIC;
246     }
247
248     if( p_sys->b_seekable && !p_access->info.i_size )
249     {
250         /* FIXME that's bad because all others access will be probed */
251         msg_Err( p_access, "file %s is empty, aborting", psz_name );
252         free( p_sys );
253         return VLC_EGENERIC;
254     }
255
256     /* Update default_pts to a suitable value for file access */
257     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
258
259     /*
260      * Get the additional list of files
261      */
262     p_file = malloc( sizeof(file_entry_t) );
263     p_file->i_size = p_access->info.i_size;
264     p_file->psz_name = strdup( psz_name );
265     TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
266
267     psz = var_CreateGetString( p_access, "file-cat" );
268     if( *psz )
269     {
270         char *psz_parser = psz_name = psz;
271         int64_t i_size;
272
273         while( psz_name && *psz_name )
274         {
275             psz_parser = strchr( psz_name, ',' );
276             if( psz_parser ) *psz_parser = 0;
277
278             psz_name = strdup( psz_name );
279             if( psz_name )
280             {
281                 msg_Dbg( p_access, "adding file `%s'", psz_name );
282                 i_size = 0;
283
284 #ifdef HAVE_SYS_STAT_H
285                 if( !stat( psz_name, &stat_info ) )
286                 {
287                     p_access->info.i_size += stat_info.st_size;
288                     i_size = stat_info.st_size;
289                 }
290                 else
291                 {
292                     msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
293                 }
294 #endif
295                 p_file = malloc( sizeof(file_entry_t) );
296                 p_file->i_size = i_size;
297                 p_file->psz_name = psz_name;
298
299                 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
300             }
301
302             psz_name = psz_parser;
303             if( psz_name ) psz_name++;
304         }
305     }
306     free( psz );
307
308     return VLC_SUCCESS;
309 }
310
311 /*****************************************************************************
312  * Close: close the target
313  *****************************************************************************/
314 static void Close( vlc_object_t * p_this )
315 {
316     access_t     *p_access = (access_t*)p_this;
317     access_sys_t *p_sys = p_access->p_sys;
318     int i;
319
320     close( p_sys->fd );
321
322     for( i = 0; i < p_sys->i_file; i++ )
323     {
324         free( p_sys->file[i]->psz_name );
325         free( p_sys->file[i] );
326     }
327     free( p_sys->file );
328
329     free( p_sys );
330 }
331
332 /*****************************************************************************
333  * Read: standard read on a file descriptor.
334  *****************************************************************************/
335 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
336 {
337     access_sys_t *p_sys = p_access->p_sys;
338     int i_ret;
339
340 #if !defined(WIN32) && !defined(UNDER_CE)
341     if( !p_sys->b_pace_control )
342     {
343         if( !p_sys->b_kfir )
344         {
345             /* Find if some data is available. This won't work under Windows. */
346             struct timeval  timeout;
347             fd_set          fds;
348
349             /* Initialize file descriptor set */
350             FD_ZERO( &fds );
351             FD_SET( p_sys->fd, &fds );
352
353             /* We'll wait 0.5 second if nothing happens */
354             timeout.tv_sec = 0;
355             timeout.tv_usec = 500000;
356
357             /* Find if some data is available */
358             while( (i_ret = select( p_sys->fd + 1, &fds, NULL, NULL, &timeout )) == 0
359                     || (i_ret < 0 && errno == EINTR) )
360             {
361                 FD_ZERO( &fds );
362                 FD_SET( p_sys->fd, &fds );
363                 timeout.tv_sec = 0;
364                 timeout.tv_usec = 500000;
365
366                 if( p_access->b_die )
367                     return 0;
368             }
369
370             if( i_ret < 0 )
371             {
372                 msg_Err( p_access, "select error (%s)", strerror(errno) );
373                 return -1;
374             }
375
376             i_ret = read( p_sys->fd, p_buffer, i_len );
377         }
378         else
379         {
380             /* b_kfir ; work around a buggy poll() driver implementation */
381             while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
382                     !p_access->b_die )
383             {
384                 msleep( INPUT_ERROR_SLEEP );
385             }
386         }
387     }
388     else
389 #endif /* WIN32 || UNDER_CE */
390     {
391         /* b_pace_control || WIN32 */
392         i_ret = read( p_sys->fd, p_buffer, i_len );
393     }
394
395     if( i_ret < 0 )
396     {
397         if( errno != EINTR && errno != EAGAIN )
398             msg_Err( p_access, "read failed (%s)", strerror(errno) );
399
400         /* Delay a bit to avoid consuming all the CPU. This is particularly
401          * useful when reading from an unconnected FIFO. */
402         msleep( INPUT_ERROR_SLEEP );
403     }
404
405     p_sys->i_nb_reads++;
406 #ifdef HAVE_SYS_STAT_H
407     if( p_access->info.i_size != 0 &&
408         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
409     {
410         struct stat stat_info;
411         int i_file = p_sys->i_index;
412
413         if ( fstat( p_sys->fd, &stat_info ) == -1 )
414         {
415             msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
416         }
417         else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
418         {
419             p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
420             p_access->info.i_update |= INPUT_UPDATE_SIZE;
421         }
422     }
423 #endif
424
425     /* If we reached an EOF then switch to the next file in the list */
426     if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
427     {
428         char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
429         p_sys->fd_backup = p_sys->fd;
430
431         msg_Dbg( p_access, "opening file `%s'", psz_name );
432
433         if ( _OpenFile( p_access, psz_name ) )
434         {
435             p_sys->fd = p_sys->fd_backup;
436             return 0;
437         }
438
439         close( p_sys->fd_backup );
440
441         /* We have to read some data */
442         return Read( p_access, p_buffer, i_len );
443     }
444
445     if( i_ret > 0 )
446         p_access->info.i_pos += i_ret;
447     else if( i_ret == 0 )
448         p_access->info.b_eof = VLC_TRUE;
449
450     return i_ret;
451 }
452
453 /*****************************************************************************
454  * Seek: seek to a specific location in a file
455  *****************************************************************************/
456 static int Seek( access_t *p_access, int64_t i_pos )
457 {
458     access_sys_t *p_sys = p_access->p_sys;
459     int64_t i_size = 0;
460
461     /* Check which file we need to access */
462     if( p_sys->i_file > 1 )
463     {
464         int i;
465         char *psz_name;
466         p_sys->fd_backup = p_sys->fd;
467
468         for( i = 0; i < p_sys->i_file - 1; i++ )
469         {
470             if( i_pos < p_sys->file[i]->i_size + i_size )
471                 break;
472             i_size += p_sys->file[i]->i_size;
473         }
474         psz_name = p_sys->file[i]->psz_name;
475
476         msg_Dbg( p_access, "opening file `%s'", psz_name );
477
478         if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
479         {
480             /* Close old file */
481             close( p_sys->fd_backup );
482             p_sys->i_index = i;
483         }
484         else
485         {
486             p_sys->fd = p_sys->fd_backup;
487         }
488     }
489
490     lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
491
492     p_access->info.i_pos = i_pos;
493     if( p_access->info.i_size < p_access->info.i_pos )
494     {
495         msg_Err( p_access, "seeking too far" );
496         p_access->info.i_pos = p_access->info.i_size;
497     }
498     else if( p_access->info.i_pos < 0 )
499     {
500         msg_Err( p_access, "seeking too early" );
501         p_access->info.i_pos = 0;
502     }
503     /* Reset eof */
504     p_access->info.b_eof = VLC_FALSE;
505
506     /* FIXME */
507     return VLC_SUCCESS;
508 }
509
510 /*****************************************************************************
511  * Control:
512  *****************************************************************************/
513 static int Control( access_t *p_access, int i_query, va_list args )
514 {
515     access_sys_t *p_sys = p_access->p_sys;
516     vlc_bool_t   *pb_bool;
517     int          *pi_int;
518     int64_t      *pi_64;
519
520     switch( i_query )
521     {
522         /* */
523         case ACCESS_CAN_SEEK:
524         case ACCESS_CAN_FASTSEEK:
525             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
526             *pb_bool = p_sys->b_seekable;
527             break;
528
529         case ACCESS_CAN_PAUSE:
530         case ACCESS_CAN_CONTROL_PACE:
531             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
532             *pb_bool = p_sys->b_pace_control;
533             break;
534
535         /* */
536         case ACCESS_GET_MTU:
537             pi_int = (int*)va_arg( args, int * );
538             *pi_int = 0;
539             break;
540
541         case ACCESS_GET_PTS_DELAY:
542             pi_64 = (int64_t*)va_arg( args, int64_t * );
543             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
544             break;
545
546         /* */
547         case ACCESS_SET_PAUSE_STATE:
548             /* Nothing to do */
549             break;
550
551         case ACCESS_GET_TITLE_INFO:
552         case ACCESS_SET_TITLE:
553         case ACCESS_SET_SEEKPOINT:
554         case ACCESS_SET_PRIVATE_ID_STATE:
555             return VLC_EGENERIC;
556
557         default:
558             msg_Warn( p_access, "unimplemented query in control" );
559             return VLC_EGENERIC;
560
561     }
562     return VLC_SUCCESS;
563 }
564
565
566 /*****************************************************************************
567  * OpenFile: Opens a specific file
568  *****************************************************************************/
569 static int _OpenFile( access_t * p_access, char * psz_name )
570 {
571     access_sys_t *p_sys = p_access->p_sys;
572
573 #ifdef UNDER_CE
574     p_sys->fd = fopen( psz_name, "rb" );
575     if ( !p_sys->fd )
576     {
577         msg_Err( p_access, "cannot open file %s" );
578         return VLC_EGENERIC;
579     }
580
581     fseek( p_sys->fd, 0, SEEK_END );
582     p_access->info.i_size = ftell( p_sys->fd );
583     p_access->info.i_update |= INPUT_UPDATE_SIZE;
584     fseek( p_sys->fd, 0, SEEK_SET );
585 #else
586
587     p_sys->fd = open( psz_name, O_NONBLOCK /*| O_LARGEFILE*/ );
588     if ( p_sys->fd == -1 )
589     {
590         msg_Err( p_access, "cannot open file %s (%s)", psz_name,
591                  strerror(errno) );
592         return VLC_EGENERIC;
593     }
594 #endif
595
596     return VLC_SUCCESS;
597 }