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