]> git.sesse.net Git - vlc/blob - modules/access/file.c
* src/misc/darwin_specific.m: Partial attempt at fixing a memory leak,
[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.7 2002/12/31 01:54:35 massiot 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( _MSC_VER ) && 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 reading") );
70     add_category_hint( N_("file"), NULL );
71     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT );
72     set_capability( "access", 50 );
73     add_shortcut( "file" );
74     add_shortcut( "stream" );
75     set_callbacks( Open, Close );
76 vlc_module_end();
77  
78 /*****************************************************************************
79  * Open: open the file
80  *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
82 {
83     input_thread_t *    p_input = (input_thread_t *)p_this;
84     char *              psz_name = p_input->psz_name;
85 #ifdef HAVE_SYS_STAT_H
86     int                 i_stat;
87     struct stat         stat_info;                                              
88 #endif
89     input_socket_t *    p_access_data;
90     vlc_bool_t          b_stdin;
91
92     p_input->i_mtu = 0;
93
94     b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
95
96 #ifdef HAVE_SYS_STAT_H
97     if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
98     {
99 #   ifdef HAVE_ERRNO_H
100         msg_Err( p_input, "cannot stat() file `%s' (%s)",
101                           psz_name, strerror(errno));
102 #   else
103         msg_Err( p_input, "cannot stat() file `%s'", psz_name );
104 #   endif
105         return VLC_EGENERIC;
106     }
107 #endif
108
109     p_input->pf_read = Read;
110     p_input->pf_set_program = input_SetProgram;
111     p_input->pf_set_area = NULL;
112     p_input->pf_seek = Seek;
113
114     vlc_mutex_lock( &p_input->stream.stream_lock );
115
116     p_input->stream.b_connected = 1;
117     if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
118     {
119         /* stream:%s */
120         p_input->stream.b_pace_control = 0;
121         p_input->stream.b_seekable = 0;
122         p_input->stream.p_selected_area->i_size = 0;
123     }
124     else
125     {
126         /* file:%s or %s */
127         p_input->stream.b_pace_control = 1;
128
129         if( b_stdin )
130         {
131             p_input->stream.b_seekable = 0;
132             p_input->stream.p_selected_area->i_size = 0;
133         }
134 #ifdef UNDER_CE
135         else if( VLC_TRUE )
136         {
137             /* We'll update i_size after it's been opened */
138             p_input->stream.b_seekable = 1;
139         }
140 #elif defined( HAVE_SYS_STAT_H )
141         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
142                   || S_ISBLK(stat_info.st_mode) )
143         {
144             p_input->stream.b_seekable = 1;
145             p_input->stream.p_selected_area->i_size = stat_info.st_size;
146         }
147         else if( S_ISFIFO(stat_info.st_mode)
148 #   if !defined( SYS_BEOS ) && !defined( WIN32 )
149                   || S_ISSOCK(stat_info.st_mode)
150 #   endif
151                )
152         {
153             p_input->stream.b_seekable = 0;
154             p_input->stream.p_selected_area->i_size = 0;
155         }
156 #endif
157         else
158         {
159             vlc_mutex_unlock( &p_input->stream.stream_lock );
160             msg_Err( p_input, "unknown file type for `%s'", psz_name );
161             return VLC_EGENERIC;
162         }
163     }
164  
165     p_input->stream.p_selected_area->i_tell = 0;
166     p_input->stream.i_method = INPUT_METHOD_FILE;
167     vlc_mutex_unlock( &p_input->stream.stream_lock );
168  
169     msg_Dbg( p_input, "opening file `%s'", psz_name );
170     p_access_data = malloc( sizeof(input_socket_t) );
171     p_input->p_access_data = (void *)p_access_data;
172     if( p_access_data == NULL )
173     {
174         msg_Err( p_input, "out of memory" );
175         return VLC_ENOMEM;
176     }
177
178     if( b_stdin )
179     {
180         p_access_data->i_handle = 0;
181     }
182     else
183     {
184 #ifdef UNDER_CE
185         wchar_t psz_filename[MAX_PATH];
186         MultiByteToWideChar( CP_ACP, 0, psz_name, -1, psz_filename, MAX_PATH );
187
188         p_access_data->i_handle = (int)CreateFile( psz_filename,
189             GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 
190             FILE_ATTRIBUTE_NORMAL, NULL );
191
192         if( (HANDLE)p_access_data->i_handle == INVALID_HANDLE_VALUE )
193         {
194             msg_Err( p_input, "cannot open file %s", psz_name );
195             free( p_access_data );
196             return VLC_EGENERIC;
197         }
198         p_input->stream.p_selected_area->i_size =
199                         GetFileSize( (HANDLE)p_access_data->i_handle, NULL );
200 #else
201         p_access_data->i_handle = open( psz_name,
202                                         O_NONBLOCK /*| O_LARGEFILE*/ );
203         if( p_access_data->i_handle == -1 )
204         {
205 #   ifdef HAVE_ERRNO_H
206             msg_Err( p_input, "cannot open file %s (%s)", psz_name,
207                               strerror(errno) );
208 #   else
209             msg_Err( p_input, "cannot open file %s", psz_name );
210 #   endif
211             free( p_access_data );
212             return VLC_EGENERIC;
213         }
214 #endif
215     }
216
217     if ( p_input->stream.b_seekable
218           && !p_input->stream.p_selected_area->i_size )
219     {
220         msg_Err( p_input, "file %s is empty, aborting", psz_name );
221         free( p_access_data );
222         return VLC_EGENERIC;
223     }
224
225     /* Update default_pts to a suitable value for file access */
226     p_input->i_pts_delay = config_GetInt( p_input, "file-caching" ) * 1000;
227
228     return VLC_SUCCESS;
229 }
230
231 /*****************************************************************************
232  * Close: close the target
233  *****************************************************************************/
234 static void Close( vlc_object_t * p_this )
235 {
236     input_thread_t * p_input = (input_thread_t *)p_this;
237     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
238
239     msg_Info( p_input, "closing `%s/%s://%s'", 
240               p_input->psz_access, p_input->psz_demux, p_input->psz_name );
241  
242 #ifdef UNDER_CE
243     CloseHandle( (HANDLE)p_access_data->i_handle );
244 #else
245     close( p_access_data->i_handle );
246 #endif
247
248     free( p_access_data );
249 }
250
251 /*****************************************************************************
252  * Read: standard read on a file descriptor.
253  *****************************************************************************/
254 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
255 {
256     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
257     ssize_t i_ret;
258  
259 #ifdef UNDER_CE
260     if( !ReadFile( (HANDLE)p_access_data->i_handle, p_buffer, i_len,
261                    (LPDWORD)&i_ret, NULL ) )
262     {
263         i_ret = -1;
264     }
265 #else
266     i_ret = read( p_access_data->i_handle, p_buffer, i_len );
267 #endif
268
269     if( i_ret < 0 )
270     {
271 #   ifdef HAVE_ERRNO_H
272         if ( errno != EINTR && errno != EAGAIN )
273             msg_Err( p_input, "read failed (%s)", strerror(errno) );
274 #   else
275         msg_Err( p_input, "read failed" );
276 #   endif
277
278         /* Delay a bit to avoid consuming all the CPU. This is particularly
279          * useful when reading from an unconnected FIFO. */
280         msleep( INPUT_ERROR_SLEEP );
281     }
282  
283     return i_ret;
284 }
285
286 /*****************************************************************************
287  * Seek: seek to a specific location in a file
288  *****************************************************************************/
289 static void Seek( input_thread_t * p_input, off_t i_pos )
290 {
291 #define S p_input->stream
292     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
293
294     lseek( p_access_data->i_handle, i_pos, SEEK_SET );
295
296     vlc_mutex_lock( &S.stream_lock );
297     S.p_selected_area->i_tell = i_pos;
298     if( S.p_selected_area->i_tell > S.p_selected_area->i_size )
299     {
300         msg_Err( p_input, "seeking too far" );
301         S.p_selected_area->i_tell = S.p_selected_area->i_size;
302     }
303     else if( S.p_selected_area->i_tell < 0 )
304     {
305         msg_Err( p_input, "seeking too early" );
306         S.p_selected_area->i_tell = 0;
307     }
308     vlc_mutex_unlock( &S.stream_lock );
309 #undef S
310 }
311