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