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