]> git.sesse.net Git - vlc/blob - modules/access/file.c
* Massive spelling corrections.
[vlc] / modules / access / file.c
1 /*****************************************************************************
2  * file.c: file input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 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  * Exported prototypes
66  *****************************************************************************/
67 static int     Open   ( vlc_object_t * );
68 static void    Close  ( vlc_object_t * );
69
70 static void    Seek   ( input_thread_t *, off_t );
71 static ssize_t Read   ( input_thread_t *, byte_t *, size_t );
72
73 static int     _OpenFile( input_thread_t *, char * );
74
75 /*****************************************************************************
76  * Module descriptor
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( "access", 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  * _input_socket_t: private access plug-in data, modified to add private
100  *                  fields
101  *****************************************************************************/
102 typedef struct file_entry_s
103 {
104     char     *psz_name;
105     int64_t  i_size;
106
107 } file_entry_t;
108
109 typedef struct _input_socket_s
110 {
111     input_socket_t      _socket;
112
113     unsigned int        i_nb_reads;
114     vlc_bool_t          b_kfir;
115
116     /* Files list */
117     file_entry_t **p_files;
118     int          i_files;
119
120     /* Current file */
121     int  i_index;
122
123 } _input_socket_t;
124
125 /*****************************************************************************
126  * Open: open the file
127  *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
129 {
130     input_thread_t *    p_input = (input_thread_t *)p_this;
131     char *              psz_name = p_input->psz_name;
132 #ifdef HAVE_SYS_STAT_H
133     int                 i_stat;
134     struct stat         stat_info;
135 #endif
136     _input_socket_t *   p_access_data;
137     vlc_bool_t          b_stdin, b_kfir = 0;
138     vlc_value_t         val;
139
140     file_entry_t *      p_file;
141
142     p_input->i_mtu = 0;
143
144     b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
145
146 #ifdef HAVE_SYS_STAT_H
147     if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
148     {
149 #   ifdef HAVE_ERRNO_H
150         msg_Warn( p_input, "cannot stat() file `%s' (%s)",
151                   psz_name, strerror(errno));
152 #   else
153         msg_Warn( p_input, "cannot stat() file `%s'", psz_name );
154 #   endif
155         return VLC_EGENERIC;
156     }
157 #endif
158
159     p_input->pf_read = Read;
160     p_input->pf_set_program = input_SetProgram;
161     p_input->pf_set_area = NULL;
162     p_input->pf_seek = Seek;
163
164     vlc_mutex_lock( &p_input->stream.stream_lock );
165
166     if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
167     {
168         /* stream:%s */
169         p_input->stream.b_pace_control = 0;
170         p_input->stream.b_seekable = 0;
171         p_input->stream.p_selected_area->i_size = 0;
172     }
173     else if( *p_input->psz_access &&
174              !strncmp( p_input->psz_access, "kfir", 7 ) )
175     {
176         /* stream:%s */
177         p_input->stream.b_pace_control = 0;
178         p_input->stream.b_seekable = 0;
179         p_input->stream.p_selected_area->i_size = 0;
180         b_kfir = 1;
181     }
182     else
183     {
184         /* file:%s or %s */
185         p_input->stream.b_pace_control = 1;
186
187         if( b_stdin )
188         {
189             p_input->stream.b_seekable = 0;
190             p_input->stream.p_selected_area->i_size = 0;
191         }
192 #ifdef UNDER_CE
193         else if( VLC_TRUE )
194         {
195             /* We'll update i_size after it's been opened */
196             p_input->stream.b_seekable = 1;
197         }
198 #elif defined( HAVE_SYS_STAT_H )
199         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
200                   || S_ISBLK(stat_info.st_mode) )
201         {
202             p_input->stream.b_seekable = 1;
203             p_input->stream.p_selected_area->i_size = stat_info.st_size;
204         }
205         else if( S_ISFIFO(stat_info.st_mode)
206 #   if !defined( SYS_BEOS ) && !defined( WIN32 )
207                   || S_ISSOCK(stat_info.st_mode)
208 #   endif
209                )
210         {
211             p_input->stream.b_seekable = 0;
212             p_input->stream.p_selected_area->i_size = 0;
213         }
214 #endif
215         else
216         {
217             vlc_mutex_unlock( &p_input->stream.stream_lock );
218             msg_Err( p_input, "unknown file type for `%s'", psz_name );
219             return VLC_EGENERIC;
220         }
221     }
222  
223     p_input->stream.p_selected_area->i_tell = 0;
224     p_input->stream.i_method = INPUT_METHOD_FILE;
225     vlc_mutex_unlock( &p_input->stream.stream_lock );
226  
227     msg_Dbg( p_input, "opening file `%s'", psz_name );
228     p_access_data = malloc( sizeof(_input_socket_t) );
229     p_input->p_access_data = (void *)p_access_data;
230     if( p_access_data == NULL )
231     {
232         msg_Err( p_input, "out of memory" );
233         return VLC_ENOMEM;
234     }
235
236     p_access_data->i_nb_reads = 0;
237     p_access_data->b_kfir = b_kfir;
238     if( b_stdin )
239     {
240         p_access_data->_socket.i_handle = 0;
241     }
242     else
243     {
244         if( _OpenFile( p_input, psz_name ) != VLC_SUCCESS )
245         {
246             free( p_access_data );
247             return VLC_EGENERIC;
248         }
249     }
250
251     if ( p_input->stream.b_seekable
252           && !p_input->stream.p_selected_area->i_size )
253     {
254         msg_Err( p_input, "file %s is empty, aborting", psz_name );
255         free( p_access_data );
256         return VLC_EGENERIC;
257     }
258
259     /* Update default_pts to a suitable value for file access */
260     var_Create( p_input, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
261     var_Get( p_input, "file-caching", &val );
262     p_input->i_pts_delay = val.i_int * 1000;
263
264     /*
265      * Get the additional list of files
266      */
267     p_access_data->p_files = NULL;
268     p_access_data->i_files = p_access_data->i_index = 0;
269     p_file = (file_entry_t *)malloc( sizeof(file_entry_t) );
270     p_file->i_size = p_input->stream.p_selected_area->i_size;
271     p_file->psz_name = strdup( psz_name );
272     TAB_APPEND( p_access_data->i_files, p_access_data->p_files, p_file );
273
274     var_Create( p_input, "file-cat", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
275     var_Get( p_input, "file-cat", &val );
276
277     if( val.psz_string && *val.psz_string )
278     {
279         char *psz_parser = psz_name = val.psz_string;
280         int64_t i_size;
281
282         while( psz_name && *psz_name )
283         {
284             psz_parser = strchr( psz_name, ',' );
285             if( psz_parser ) *psz_parser = 0;
286
287             psz_name = strdup( psz_name );
288             if( psz_name )
289             {
290                 msg_Dbg( p_input, "adding file `%s'", psz_name );
291
292 #ifdef HAVE_SYS_STAT_H
293                 if( !stat( psz_name, &stat_info ) )
294                 {
295                     p_input->stream.p_selected_area->i_size +=
296                         stat_info.st_size;
297                     i_size = stat_info.st_size;
298                 }
299                 else
300                 {
301                     msg_Dbg( p_input, "cannot stat() file `%s'", psz_name );
302                     i_size = 0;
303                 }
304 #endif
305
306                 p_file = (file_entry_t *)malloc( sizeof(file_entry_t) );
307                 p_file->i_size = i_size;
308                 p_file->psz_name = psz_name;
309
310                 TAB_APPEND( p_access_data->i_files, p_access_data->p_files,
311                             p_file );
312             }
313
314             psz_name = psz_parser;
315             if( psz_name ) psz_name++;
316         }
317     }
318
319     if( val.psz_string ) free( val.psz_string );
320
321     return VLC_SUCCESS;
322 }
323
324 /*****************************************************************************
325  * Close: close the target
326  *****************************************************************************/
327 static void Close( vlc_object_t * p_this )
328 {
329     input_thread_t *p_input = (input_thread_t *)p_this;
330     _input_socket_t *p_access_data = (_input_socket_t *)p_input->p_access_data;
331     int i;
332
333     msg_Info( p_input, "closing `%s/%s://%s'", 
334               p_input->psz_access, p_input->psz_demux, p_input->psz_name );
335  
336     close( p_access_data->_socket.i_handle );
337
338     for( i = 0; i < p_access_data->i_files; i++ )
339     {
340         free( p_access_data->p_files[i]->psz_name );
341         free( p_access_data->p_files[i] );
342     }
343     if( p_access_data->p_files ) free( p_access_data->p_files );
344
345     free( p_access_data );
346 }
347
348 /*****************************************************************************
349  * Read: standard read on a file descriptor.
350  *****************************************************************************/
351 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
352 {
353     _input_socket_t *p_access_data = (_input_socket_t *)p_input->p_access_data;
354     ssize_t i_ret;
355  
356 #ifdef UNDER_CE
357     if( !ReadFile( (HANDLE)p_access_data->_socket.i_handle, p_buffer, i_len,
358                    (LPDWORD)&i_ret, NULL ) )
359     {
360         i_ret = -1;
361     }
362 #else
363 #ifndef WIN32
364     if ( !p_input->stream.b_pace_control )
365     {
366         if ( !p_access_data->b_kfir )
367         {
368             /* Find if some data is available. This won't work under Windows. */
369             struct timeval  timeout;
370             fd_set          fds;
371
372             /* Initialize file descriptor set */
373             FD_ZERO( &fds );
374             FD_SET( p_access_data->_socket.i_handle, &fds );
375
376             /* We'll wait 0.5 second if nothing happens */
377             timeout.tv_sec = 0;
378             timeout.tv_usec = 500000;
379
380             /* Find if some data is available */
381             while( (i_ret = select( p_access_data->_socket.i_handle + 1, &fds,
382                                     NULL, NULL, &timeout )) == 0
383                     || (i_ret < 0 && errno == EINTR) )
384             {
385                 FD_ZERO( &fds );
386                 FD_SET( p_access_data->_socket.i_handle, &fds );
387                 timeout.tv_sec = 0;
388                 timeout.tv_usec = 500000;
389
390                 if( p_input->b_die || p_input->b_error )
391                 {
392                     return 0;
393                 }
394             }
395
396             if( i_ret < 0 )
397             {
398                 msg_Err( p_input, "select error (%s)", strerror(errno) );
399                 return -1;
400             }
401
402             i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
403         }
404         else
405         {
406             /* b_kfir ; work around a buggy poll() driver implementation */
407             while ( (i_ret = read( p_access_data->_socket.i_handle, p_buffer,
408                                    i_len )) == 0 &&
409                       !p_input->b_die && !p_input->b_error )
410             {
411                 msleep(INPUT_ERROR_SLEEP);
412             }
413         }
414     }
415     else
416 #   endif
417     {
418         /* b_pace_control || WIN32 */
419         i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
420     }
421 #endif
422
423     if( i_ret < 0 )
424     {
425 #ifdef HAVE_ERRNO_H
426         if ( errno != EINTR && errno != EAGAIN )
427             msg_Err( p_input, "read failed (%s)", strerror(errno) );
428 #else
429         msg_Err( p_input, "read failed" );
430 #endif
431
432         /* Delay a bit to avoid consuming all the CPU. This is particularly
433          * useful when reading from an unconnected FIFO. */
434         msleep( INPUT_ERROR_SLEEP );
435     }
436  
437     p_access_data->i_nb_reads++;
438 #ifdef HAVE_SYS_STAT_H
439     if ( p_input->stream.p_selected_area->i_size != 0
440             && (p_access_data->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
441     {
442         struct stat stat_info;
443         int i_file = p_access_data->i_index;
444
445         if ( fstat( p_access_data->_socket.i_handle, &stat_info ) == -1 )
446         {
447 #   ifdef HAVE_ERRNO_H
448             msg_Warn( p_input, "couldn't stat again the file (%s)",
449                       strerror(errno) );
450 #   else
451             msg_Warn( p_input, "couldn't stat again the file" );
452 #   endif
453         }
454         else if ( p_access_data->p_files[i_file]->i_size != stat_info.st_size )
455         {
456             p_input->stream.p_selected_area->i_size +=
457                 (stat_info.st_size - p_access_data->p_files[i_file]->i_size);
458             p_input->stream.b_changed = 1;
459         }
460     }
461 #endif
462
463     /* If we reached an EOF then switch to the next file in the list */
464     if ( i_ret == 0 && p_access_data->i_index + 1 < p_access_data->i_files )
465     {
466         int i_handle = p_access_data->_socket.i_handle;
467         char *psz_name =
468             p_access_data->p_files[++p_access_data->i_index]->psz_name;
469
470         msg_Dbg( p_input, "opening file `%s'", psz_name );
471
472         if ( _OpenFile( p_input, psz_name ) != VLC_SUCCESS )
473         {
474             p_access_data->_socket.i_handle = i_handle;
475             return 0;
476         }
477
478         close( i_handle );
479
480         /* We have to read some data */
481         return Read( p_input, p_buffer, i_len );
482     }
483
484     return i_ret;
485 }
486
487 /*****************************************************************************
488  * Seek: seek to a specific location in a file
489  *****************************************************************************/
490 static void Seek( input_thread_t * p_input, off_t i_pos )
491 {
492 #define S p_input->stream
493     _input_socket_t *p_access_data = (_input_socket_t *)p_input->p_access_data;
494     int64_t i_size = 0;
495
496     /* Check which file we need to access */
497     if ( p_access_data->i_files > 1 )
498     {
499         int i, i_handle = p_access_data->_socket.i_handle;
500         char * psz_name;
501
502         for ( i = 0; i < p_access_data->i_files - 1; i++ )
503         {
504             if( i_pos < p_access_data->p_files[i]->i_size + i_size ) break;
505             i_size += p_access_data->p_files[i]->i_size;
506         }
507
508         psz_name = p_access_data->p_files[i]->psz_name;
509
510         msg_Dbg( p_input, "opening file `%s'", psz_name );
511         if ( i != p_access_data->i_index &&
512              _OpenFile( p_input, psz_name ) == VLC_SUCCESS )
513         {
514             /* Close old file */
515             close( i_handle );
516             p_access_data->i_index = i;
517         }
518         else
519         {
520             p_access_data->_socket.i_handle = i_handle;
521         }
522     }
523
524 #if defined( WIN32 ) && !defined( UNDER_CE )
525     _lseeki64( p_access_data->_socket.i_handle, i_pos - i_size, SEEK_SET );
526 #else
527     lseek( p_access_data->_socket.i_handle, i_pos - i_size, SEEK_SET );
528 #endif
529
530     vlc_mutex_lock( &S.stream_lock );
531     S.p_selected_area->i_tell = i_pos;
532     if( S.p_selected_area->i_tell > S.p_selected_area->i_size )
533     {
534         msg_Err( p_input, "seeking too far" );
535         S.p_selected_area->i_tell = S.p_selected_area->i_size;
536     }
537     else if( S.p_selected_area->i_tell < 0 )
538     {
539         msg_Err( p_input, "seeking too early" );
540         S.p_selected_area->i_tell = 0;
541     }
542     vlc_mutex_unlock( &S.stream_lock );
543 #undef S
544 }
545
546 /*****************************************************************************
547  * OpenFile: Opens a specific file
548  *****************************************************************************/
549 static int _OpenFile( input_thread_t * p_input, char * psz_name )
550 {
551     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
552
553 #ifdef UNDER_CE
554     wchar_t psz_filename[MAX_PATH];
555     MultiByteToWideChar( CP_ACP, 0, psz_name, -1, psz_filename, MAX_PATH );
556
557     p_access_data->i_handle =
558        (int)CreateFile( psz_filename, GENERIC_READ, FILE_SHARE_READ,
559                         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
560
561     if ( (HANDLE)p_access_data->i_handle == INVALID_HANDLE_VALUE )
562     {
563         msg_Err( p_input, "cannot open file %s", psz_name );
564         return VLC_EGENERIC;
565     }
566     p_input->stream.p_selected_area->i_size =
567         GetFileSize( (HANDLE)p_access_data->i_handle, NULL );
568 #else
569
570     p_access_data->i_handle = open( psz_name, O_NONBLOCK /*| O_LARGEFILE*/ );
571     if ( p_access_data->i_handle == -1 )
572     {
573 #   ifdef HAVE_ERRNO_H
574         msg_Err( p_input, "cannot open file %s (%s)", psz_name,
575                  strerror(errno) );
576 #   else
577         msg_Err( p_input, "cannot open file %s", psz_name );
578 #   endif
579         return VLC_EGENERIC;
580     }
581 #endif
582
583     return VLC_SUCCESS;
584 }