1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: file.c,v 1.21 2003/06/09 09:29:12 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/input.h>
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
50 #elif defined( WIN32 ) && !defined( UNDER_CE )
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)
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
66 static void Seek ( input_thread_t *, off_t );
67 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
69 /*****************************************************************************
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." )
78 set_description( _("Standard filesystem file input") );
79 add_category_hint( N_("file"), NULL, VLC_TRUE );
80 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
81 set_capability( "access", 50 );
82 add_shortcut( "file" );
83 add_shortcut( "stream" );
84 add_shortcut( "kfir" );
85 set_callbacks( Open, Close );
88 /*****************************************************************************
89 * _input_socket_t: private access plug-in data, modified to add private
91 *****************************************************************************/
92 typedef struct _input_socket_s
94 input_socket_t _socket;
96 unsigned int i_nb_reads;
100 /*****************************************************************************
101 * Open: open the file
102 *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
105 input_thread_t * p_input = (input_thread_t *)p_this;
106 char * psz_name = p_input->psz_name;
107 #ifdef HAVE_SYS_STAT_H
109 #if defined( WIN32 ) && !defined( UNDER_CE )
110 struct _stati64 stat_info;
112 struct stat stat_info;
115 _input_socket_t * p_access_data;
116 vlc_bool_t b_stdin, b_kfir = 0;
120 b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
122 #ifdef HAVE_SYS_STAT_H
123 if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
126 msg_Warn( p_input, "cannot stat() file `%s' (%s)",
127 psz_name, strerror(errno));
129 msg_Warn( p_input, "cannot stat() file `%s'", psz_name );
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;
140 vlc_mutex_lock( &p_input->stream.stream_lock );
142 if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
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;
149 else if( *p_input->psz_access &&
150 !strncmp( p_input->psz_access, "kfir", 7 ) )
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;
161 p_input->stream.b_pace_control = 1;
165 p_input->stream.b_seekable = 0;
166 p_input->stream.p_selected_area->i_size = 0;
171 /* We'll update i_size after it's been opened */
172 p_input->stream.b_seekable = 1;
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) )
178 p_input->stream.b_seekable = 1;
179 p_input->stream.p_selected_area->i_size = stat_info.st_size;
181 else if( S_ISFIFO(stat_info.st_mode)
182 # if !defined( SYS_BEOS ) && !defined( WIN32 )
183 || S_ISSOCK(stat_info.st_mode)
187 p_input->stream.b_seekable = 0;
188 p_input->stream.p_selected_area->i_size = 0;
193 vlc_mutex_unlock( &p_input->stream.stream_lock );
194 msg_Err( p_input, "unknown file type for `%s'", psz_name );
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 );
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 )
208 msg_Err( p_input, "out of memory" );
212 p_access_data->i_nb_reads = 0;
213 p_access_data->b_kfir = b_kfir;
216 p_access_data->_socket.i_handle = 0;
221 wchar_t psz_filename[MAX_PATH];
222 MultiByteToWideChar( CP_ACP, 0, psz_name, -1, psz_filename, MAX_PATH );
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 );
228 if( (HANDLE)p_access_data->_socket.i_handle == INVALID_HANDLE_VALUE )
230 msg_Err( p_input, "cannot open file %s", psz_name );
231 free( p_access_data );
234 p_input->stream.p_selected_area->i_size =
235 GetFileSize( (HANDLE)p_access_data->_socket.i_handle, NULL );
237 p_access_data->_socket.i_handle = open( psz_name,
238 O_NONBLOCK /*| O_LARGEFILE*/ );
239 if( p_access_data->_socket.i_handle == -1 )
242 msg_Err( p_input, "cannot open file %s (%s)", psz_name,
245 msg_Err( p_input, "cannot open file %s", psz_name );
247 free( p_access_data );
253 if ( p_input->stream.b_seekable
254 && !p_input->stream.p_selected_area->i_size )
256 msg_Err( p_input, "file %s is empty, aborting", psz_name );
257 free( p_access_data );
261 /* Update default_pts to a suitable value for file access */
262 p_input->i_pts_delay = config_GetInt( p_input, "file-caching" ) * 1000;
267 /*****************************************************************************
268 * Close: close the target
269 *****************************************************************************/
270 static void Close( vlc_object_t * p_this )
272 input_thread_t * p_input = (input_thread_t *)p_this;
273 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
275 msg_Info( p_input, "closing `%s/%s://%s'",
276 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
279 CloseHandle( (HANDLE)p_access_data->i_handle );
281 close( p_access_data->i_handle );
284 free( p_access_data );
287 /*****************************************************************************
288 * Read: standard read on a file descriptor.
289 *****************************************************************************/
290 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
292 _input_socket_t * p_access_data = (_input_socket_t *)p_input->p_access_data;
296 if( !ReadFile( (HANDLE)p_access_data->_socket.i_handle, p_buffer, i_len,
297 (LPDWORD)&i_ret, NULL ) )
303 if ( !p_input->stream.b_pace_control )
305 if ( !p_access_data->b_kfir )
307 /* Find if some data is available. This won't work under Windows. */
308 struct timeval timeout;
311 /* Initialize file descriptor set */
313 FD_SET( p_access_data->_socket.i_handle, &fds );
315 /* We'll wait 0.5 second if nothing happens */
317 timeout.tv_usec = 500000;
319 /* Find if some data is available */
320 while( (i_ret = select( p_access_data->_socket.i_handle + 1, &fds,
321 NULL, NULL, &timeout )) == 0
322 || (i_ret < 0 && errno == EINTR) )
325 FD_SET( p_access_data->_socket.i_handle, &fds );
327 timeout.tv_usec = 500000;
329 if( p_input->b_die || p_input->b_error )
337 msg_Err( p_input, "select error (%s)", strerror(errno) );
341 i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
345 /* b_kfir ; work around a buggy poll() driver implementation */
346 while ( (i_ret = read( p_access_data->_socket.i_handle, p_buffer,
348 !p_input->b_die && !p_input->b_error )
350 msleep(INPUT_ERROR_SLEEP);
357 /* b_pace_control || WIN32 */
358 i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
365 if ( errno != EINTR && errno != EAGAIN )
366 msg_Err( p_input, "read failed (%s)", strerror(errno) );
368 msg_Err( p_input, "read failed" );
371 /* Delay a bit to avoid consuming all the CPU. This is particularly
372 * useful when reading from an unconnected FIFO. */
373 msleep( INPUT_ERROR_SLEEP );
376 p_access_data->i_nb_reads++;
377 #ifdef HAVE_SYS_STAT_H
378 if ( p_input->stream.p_selected_area->i_size != 0
379 && (p_access_data->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
381 #if defined( WIN32 ) && !defined( UNDER_CE )
382 struct _stati64 stat_info;
384 struct stat stat_info;
386 if ( fstat( p_access_data->_socket.i_handle, &stat_info ) == -1 )
389 msg_Warn( p_input, "couldn't stat again the file (%s)",
392 msg_Warn( p_input, "couldn't stat again the file" );
395 else if ( p_input->stream.p_selected_area->i_size != stat_info.st_size )
397 p_input->stream.p_selected_area->i_size = stat_info.st_size;
398 p_input->stream.b_changed = 1;
406 /*****************************************************************************
407 * Seek: seek to a specific location in a file
408 *****************************************************************************/
409 static void Seek( input_thread_t * p_input, off_t i_pos )
411 #define S p_input->stream
412 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
414 #if defined( WIN32 ) && !defined( UNDER_CE )
415 _lseeki64( p_access_data->i_handle, i_pos, SEEK_SET );
417 lseek( p_access_data->i_handle, i_pos, SEEK_SET );
420 vlc_mutex_lock( &S.stream_lock );
421 S.p_selected_area->i_tell = i_pos;
422 if( S.p_selected_area->i_tell > S.p_selected_area->i_size )
424 msg_Err( p_input, "seeking too far" );
425 S.p_selected_area->i_tell = S.p_selected_area->i_size;
427 else if( S.p_selected_area->i_tell < 0 )
429 msg_Err( p_input, "seeking too early" );
430 S.p_selected_area->i_tell = 0;
432 vlc_mutex_unlock( &S.stream_lock );