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