]> git.sesse.net Git - vlc/blob - modules/access/file.c
* include/vlc_threads.h:
[vlc] / modules / access / file.c
1 /*****************************************************************************
2  * file.c: file input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: file.c,v 1.23 2004/03/01 12:50:39 gbazin Exp $
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(a,b) _stati64(a,b)
57 #define fstat(a,b) _fstati64(a,b)
58 #endif
59
60 /*****************************************************************************
61  * Exported prototypes
62  *****************************************************************************/
63 static int     Open   ( vlc_object_t * );
64 static void    Close  ( vlc_object_t * );
65
66 static void    Seek   ( input_thread_t *, off_t );
67 static ssize_t Read   ( input_thread_t *, byte_t *, size_t );
68
69 /*****************************************************************************
70  * Module descriptor
71  *****************************************************************************/
72 #define CACHING_TEXT N_("Caching value in ms")
73 #define CACHING_LONGTEXT N_( \
74     "Allows you to modify the default caching value for file streams. This " \
75     "value should be set in miliseconds units." )
76
77 vlc_module_begin();
78     set_description( _("Standard filesystem file input") );
79     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
80     set_capability( "access", 50 );
81     add_shortcut( "file" );
82     add_shortcut( "stream" );
83     add_shortcut( "kfir" );
84     set_callbacks( Open, Close );
85 vlc_module_end();
86  
87 /*****************************************************************************
88  * _input_socket_t: private access plug-in data, modified to add private
89  *                  fields
90  *****************************************************************************/
91 typedef struct _input_socket_s
92 {
93     input_socket_t      _socket;
94
95     unsigned int        i_nb_reads;
96     vlc_bool_t          b_kfir;
97 } _input_socket_t;
98
99 /*****************************************************************************
100  * Open: open the file
101  *****************************************************************************/
102 static int Open( vlc_object_t *p_this )
103 {
104     input_thread_t *    p_input = (input_thread_t *)p_this;
105     char *              psz_name = p_input->psz_name;
106 #ifdef HAVE_SYS_STAT_H
107     int                 i_stat;
108 #if defined( WIN32 ) && !defined( UNDER_CE )
109     struct _stati64     stat_info;
110 #else
111     struct stat         stat_info;
112 #endif
113 #endif
114     _input_socket_t *   p_access_data;
115     vlc_bool_t          b_stdin, b_kfir = 0;
116     vlc_value_t         val;
117
118     p_input->i_mtu = 0;
119
120     b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
121
122 #ifdef HAVE_SYS_STAT_H
123     if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
124     {
125 #   ifdef HAVE_ERRNO_H
126         msg_Warn( p_input, "cannot stat() file `%s' (%s)",
127                   psz_name, strerror(errno));
128 #   else
129         msg_Warn( p_input, "cannot stat() file `%s'", psz_name );
130 #   endif
131         return VLC_EGENERIC;
132     }
133 #endif
134
135     p_input->pf_read = Read;
136     p_input->pf_set_program = input_SetProgram;
137     p_input->pf_set_area = NULL;
138     p_input->pf_seek = Seek;
139
140     vlc_mutex_lock( &p_input->stream.stream_lock );
141
142     if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
143     {
144         /* stream:%s */
145         p_input->stream.b_pace_control = 0;
146         p_input->stream.b_seekable = 0;
147         p_input->stream.p_selected_area->i_size = 0;
148     }
149     else if( *p_input->psz_access &&
150              !strncmp( p_input->psz_access, "kfir", 7 ) )
151     {
152         /* stream:%s */
153         p_input->stream.b_pace_control = 0;
154         p_input->stream.b_seekable = 0;
155         p_input->stream.p_selected_area->i_size = 0;
156         b_kfir = 1;
157     }
158     else
159     {
160         /* file:%s or %s */
161         p_input->stream.b_pace_control = 1;
162
163         if( b_stdin )
164         {
165             p_input->stream.b_seekable = 0;
166             p_input->stream.p_selected_area->i_size = 0;
167         }
168 #ifdef UNDER_CE
169         else if( VLC_TRUE )
170         {
171             /* We'll update i_size after it's been opened */
172             p_input->stream.b_seekable = 1;
173         }
174 #elif defined( HAVE_SYS_STAT_H )
175         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
176                   || S_ISBLK(stat_info.st_mode) )
177         {
178             p_input->stream.b_seekable = 1;
179             p_input->stream.p_selected_area->i_size = stat_info.st_size;
180         }
181         else if( S_ISFIFO(stat_info.st_mode)
182 #   if !defined( SYS_BEOS ) && !defined( WIN32 )
183                   || S_ISSOCK(stat_info.st_mode)
184 #   endif
185                )
186         {
187             p_input->stream.b_seekable = 0;
188             p_input->stream.p_selected_area->i_size = 0;
189         }
190 #endif
191         else
192         {
193             vlc_mutex_unlock( &p_input->stream.stream_lock );
194             msg_Err( p_input, "unknown file type for `%s'", psz_name );
195             return VLC_EGENERIC;
196         }
197     }
198  
199     p_input->stream.p_selected_area->i_tell = 0;
200     p_input->stream.i_method = INPUT_METHOD_FILE;
201     vlc_mutex_unlock( &p_input->stream.stream_lock );
202  
203     msg_Dbg( p_input, "opening file `%s'", psz_name );
204     p_access_data = malloc( sizeof(_input_socket_t) );
205     p_input->p_access_data = (void *)p_access_data;
206     if( p_access_data == NULL )
207     {
208         msg_Err( p_input, "out of memory" );
209         return VLC_ENOMEM;
210     }
211
212     p_access_data->i_nb_reads = 0;
213     p_access_data->b_kfir = b_kfir;
214     if( b_stdin )
215     {
216         p_access_data->_socket.i_handle = 0;
217     }
218     else
219     {
220 #ifdef UNDER_CE
221         wchar_t psz_filename[MAX_PATH];
222         MultiByteToWideChar( CP_ACP, 0, psz_name, -1, psz_filename, MAX_PATH );
223
224         p_access_data->_socket.i_handle = (int)CreateFile( psz_filename,
225             GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 
226             FILE_ATTRIBUTE_NORMAL, NULL );
227
228         if( (HANDLE)p_access_data->_socket.i_handle == INVALID_HANDLE_VALUE )
229         {
230             msg_Err( p_input, "cannot open file %s", psz_name );
231             free( p_access_data );
232             return VLC_EGENERIC;
233         }
234         p_input->stream.p_selected_area->i_size =
235                 GetFileSize( (HANDLE)p_access_data->_socket.i_handle, NULL );
236 #else
237         p_access_data->_socket.i_handle = open( psz_name,
238                                                 O_NONBLOCK /*| O_LARGEFILE*/ );
239         if( p_access_data->_socket.i_handle == -1 )
240         {
241 #   ifdef HAVE_ERRNO_H
242             msg_Err( p_input, "cannot open file %s (%s)", psz_name,
243                               strerror(errno) );
244 #   else
245             msg_Err( p_input, "cannot open file %s", psz_name );
246 #   endif
247             free( p_access_data );
248             return VLC_EGENERIC;
249         }
250 #endif
251     }
252
253     if ( p_input->stream.b_seekable
254           && !p_input->stream.p_selected_area->i_size )
255     {
256         msg_Err( p_input, "file %s is empty, aborting", psz_name );
257         free( p_access_data );
258         return VLC_EGENERIC;
259     }
260
261     /* Update default_pts to a suitable value for file access */
262     var_Create( p_input, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
263     var_Get( p_input, "file-caching", &val );
264     p_input->i_pts_delay = val.i_int * 1000;
265
266     return VLC_SUCCESS;
267 }
268
269 /*****************************************************************************
270  * Close: close the target
271  *****************************************************************************/
272 static void Close( vlc_object_t * p_this )
273 {
274     input_thread_t * p_input = (input_thread_t *)p_this;
275     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
276
277     msg_Info( p_input, "closing `%s/%s://%s'", 
278               p_input->psz_access, p_input->psz_demux, p_input->psz_name );
279  
280 #ifdef UNDER_CE
281     CloseHandle( (HANDLE)p_access_data->i_handle );
282 #else
283     close( p_access_data->i_handle );
284 #endif
285
286     free( p_access_data );
287 }
288
289 /*****************************************************************************
290  * Read: standard read on a file descriptor.
291  *****************************************************************************/
292 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
293 {
294     _input_socket_t * p_access_data = (_input_socket_t *)p_input->p_access_data;
295     ssize_t i_ret;
296  
297 #ifdef UNDER_CE
298     if( !ReadFile( (HANDLE)p_access_data->_socket.i_handle, p_buffer, i_len,
299                    (LPDWORD)&i_ret, NULL ) )
300     {
301         i_ret = -1;
302     }
303 #else
304 #ifndef WIN32
305     if ( !p_input->stream.b_pace_control )
306     {
307         if ( !p_access_data->b_kfir )
308         {
309             /* Find if some data is available. This won't work under Windows. */
310             struct timeval  timeout;
311             fd_set          fds;
312
313             /* Initialize file descriptor set */
314             FD_ZERO( &fds );
315             FD_SET( p_access_data->_socket.i_handle, &fds );
316
317             /* We'll wait 0.5 second if nothing happens */
318             timeout.tv_sec = 0;
319             timeout.tv_usec = 500000;
320
321             /* Find if some data is available */
322             while( (i_ret = select( p_access_data->_socket.i_handle + 1, &fds,
323                                     NULL, NULL, &timeout )) == 0
324                     || (i_ret < 0 && errno == EINTR) )
325             {
326                 FD_ZERO( &fds );
327                 FD_SET( p_access_data->_socket.i_handle, &fds );
328                 timeout.tv_sec = 0;
329                 timeout.tv_usec = 500000;
330
331                 if( p_input->b_die || p_input->b_error )
332                 {
333                     return 0;
334                 }
335             }
336
337             if( i_ret < 0 )
338             {
339                 msg_Err( p_input, "select error (%s)", strerror(errno) );
340                 return -1;
341             }
342
343             i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
344         }
345         else
346         {
347             /* b_kfir ; work around a buggy poll() driver implementation */
348             while ( (i_ret = read( p_access_data->_socket.i_handle, p_buffer,
349                                    i_len )) == 0 &&
350                       !p_input->b_die && !p_input->b_error )
351             {
352                 msleep(INPUT_ERROR_SLEEP);
353             }
354         }
355     }
356     else
357 #   endif
358     {
359         /* b_pace_control || WIN32 */
360         i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
361     }
362 #endif
363
364     if( i_ret < 0 )
365     {
366 #ifdef HAVE_ERRNO_H
367         if ( errno != EINTR && errno != EAGAIN )
368             msg_Err( p_input, "read failed (%s)", strerror(errno) );
369 #else
370         msg_Err( p_input, "read failed" );
371 #endif
372
373         /* Delay a bit to avoid consuming all the CPU. This is particularly
374          * useful when reading from an unconnected FIFO. */
375         msleep( INPUT_ERROR_SLEEP );
376     }
377  
378     p_access_data->i_nb_reads++;
379 #ifdef HAVE_SYS_STAT_H
380     if ( p_input->stream.p_selected_area->i_size != 0
381             && (p_access_data->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
382     {
383 #if defined( WIN32 ) && !defined( UNDER_CE )
384         struct _stati64 stat_info;
385 #else
386         struct stat stat_info;
387 #endif
388         if ( fstat( p_access_data->_socket.i_handle, &stat_info ) == -1 )
389         {
390 #   ifdef HAVE_ERRNO_H
391             msg_Warn( p_input, "couldn't stat again the file (%s)",
392                       strerror(errno) );
393 #   else
394             msg_Warn( p_input, "couldn't stat again the file" );
395 #   endif
396         }
397         else if ( p_input->stream.p_selected_area->i_size != stat_info.st_size )
398         {
399             p_input->stream.p_selected_area->i_size = stat_info.st_size;
400             p_input->stream.b_changed = 1;
401         }
402     }
403 #endif
404
405     return i_ret;
406 }
407
408 /*****************************************************************************
409  * Seek: seek to a specific location in a file
410  *****************************************************************************/
411 static void Seek( input_thread_t * p_input, off_t i_pos )
412 {
413 #define S p_input->stream
414     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
415
416 #if defined( WIN32 ) && !defined( UNDER_CE )
417     _lseeki64( p_access_data->i_handle, i_pos, SEEK_SET );
418 #else
419     lseek( p_access_data->i_handle, i_pos, SEEK_SET );
420 #endif
421
422     vlc_mutex_lock( &S.stream_lock );
423     S.p_selected_area->i_tell = i_pos;
424     if( S.p_selected_area->i_tell > S.p_selected_area->i_size )
425     {
426         msg_Err( p_input, "seeking too far" );
427         S.p_selected_area->i_tell = S.p_selected_area->i_size;
428     }
429     else if( S.p_selected_area->i_tell < 0 )
430     {
431         msg_Err( p_input, "seeking too early" );
432         S.p_selected_area->i_tell = 0;
433     }
434     vlc_mutex_unlock( &S.stream_lock );
435 #undef S
436 }
437