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 $
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_STAT_H
36 # include <sys/stat.h>
47 #elif defined( WIN32 ) && !defined( UNDER_CE )
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close ( vlc_object_t * );
57 static void Seek ( input_thread_t *, off_t );
58 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
60 /*****************************************************************************
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." )
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 );
79 /*****************************************************************************
80 * _input_socket_t: private access plug-in data, modified to add private
82 *****************************************************************************/
83 typedef struct _input_socket_s
85 input_socket_t _socket;
87 unsigned int i_nb_reads;
91 /*****************************************************************************
93 *****************************************************************************/
94 static int Open( vlc_object_t *p_this )
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
100 struct stat stat_info;
102 _input_socket_t * p_access_data;
103 vlc_bool_t b_stdin, b_kfir = 0;
107 b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
109 #ifdef HAVE_SYS_STAT_H
110 if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
113 msg_Warn( p_input, "cannot stat() file `%s' (%s)",
114 psz_name, strerror(errno));
116 msg_Warn( p_input, "cannot stat() file `%s'", psz_name );
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;
127 vlc_mutex_lock( &p_input->stream.stream_lock );
129 if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
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;
136 else if( *p_input->psz_access &&
137 !strncmp( p_input->psz_access, "kfir", 7 ) )
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;
148 p_input->stream.b_pace_control = 1;
152 p_input->stream.b_seekable = 0;
153 p_input->stream.p_selected_area->i_size = 0;
158 /* We'll update i_size after it's been opened */
159 p_input->stream.b_seekable = 1;
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) )
165 p_input->stream.b_seekable = 1;
166 p_input->stream.p_selected_area->i_size = stat_info.st_size;
168 else if( S_ISFIFO(stat_info.st_mode)
169 # if !defined( SYS_BEOS ) && !defined( WIN32 )
170 || S_ISSOCK(stat_info.st_mode)
174 p_input->stream.b_seekable = 0;
175 p_input->stream.p_selected_area->i_size = 0;
180 vlc_mutex_unlock( &p_input->stream.stream_lock );
181 msg_Err( p_input, "unknown file type for `%s'", psz_name );
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 );
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 )
195 msg_Err( p_input, "out of memory" );
199 p_access_data->i_nb_reads = 0;
200 p_access_data->b_kfir = b_kfir;
203 p_access_data->_socket.i_handle = 0;
208 wchar_t psz_filename[MAX_PATH];
209 MultiByteToWideChar( CP_ACP, 0, psz_name, -1, psz_filename, MAX_PATH );
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 );
215 if( (HANDLE)p_access_data->_socket.i_handle == INVALID_HANDLE_VALUE )
217 msg_Err( p_input, "cannot open file %s", psz_name );
218 free( p_access_data );
221 p_input->stream.p_selected_area->i_size =
222 GetFileSize( (HANDLE)p_access_data->_socket.i_handle, NULL );
224 p_access_data->_socket.i_handle = open( psz_name,
225 O_NONBLOCK /*| O_LARGEFILE*/ );
226 if( p_access_data->_socket.i_handle == -1 )
229 msg_Err( p_input, "cannot open file %s (%s)", psz_name,
232 msg_Err( p_input, "cannot open file %s", psz_name );
234 free( p_access_data );
240 if ( p_input->stream.b_seekable
241 && !p_input->stream.p_selected_area->i_size )
243 msg_Err( p_input, "file %s is empty, aborting", psz_name );
244 free( p_access_data );
248 /* Update default_pts to a suitable value for file access */
249 p_input->i_pts_delay = config_GetInt( p_input, "file-caching" ) * 1000;
254 /*****************************************************************************
255 * Close: close the target
256 *****************************************************************************/
257 static void Close( vlc_object_t * p_this )
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;
262 msg_Info( p_input, "closing `%s/%s://%s'",
263 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
266 CloseHandle( (HANDLE)p_access_data->i_handle );
268 close( p_access_data->i_handle );
271 free( p_access_data );
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 )
279 _input_socket_t * p_access_data = (_input_socket_t *)p_input->p_access_data;
283 if( !ReadFile( (HANDLE)p_access_data->_socket.i_handle, p_buffer, i_len,
284 (LPDWORD)&i_ret, NULL ) )
290 if ( !p_input->stream.b_pace_control )
292 if ( !p_access_data->b_kfir )
294 /* Find if some data is available. This won't work under Windows. */
295 struct timeval timeout;
298 /* Initialize file descriptor set */
300 FD_SET( p_access_data->_socket.i_handle, &fds );
302 /* We'll wait 0.5 second if nothing happens */
304 timeout.tv_usec = 500000;
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) )
312 FD_SET( p_access_data->_socket.i_handle, &fds );
314 timeout.tv_usec = 500000;
316 if( p_input->b_die || p_input->b_error )
324 msg_Err( p_input, "select error (%s)", strerror(errno) );
328 i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
332 /* b_kfir ; work around a buggy poll() driver implementation */
333 while ( (i_ret = read( p_access_data->_socket.i_handle, p_buffer,
335 !p_input->b_die && !p_input->b_error )
337 msleep(INPUT_ERROR_SLEEP);
344 /* b_pace_control || WIN32 */
345 i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
352 if ( errno != EINTR && errno != EAGAIN )
353 msg_Err( p_input, "read failed (%s)", strerror(errno) );
355 msg_Err( p_input, "read failed" );
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 );
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 )
368 struct stat stat_info;
369 if ( fstat( p_access_data->_socket.i_handle, &stat_info ) == -1 )
372 msg_Warn( p_input, "couldn't stat again the file (%s)",
375 msg_Warn( p_input, "couldn't stat again the file" );
378 else if ( p_input->stream.p_selected_area->i_size != stat_info.st_size )
380 p_input->stream.p_selected_area->i_size = stat_info.st_size;
381 p_input->stream.b_changed = 1;
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 )
394 #define S p_input->stream
395 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
397 #if defined( WIN32 ) && !defined( UNDER_CE )
398 _lseeki64( p_access_data->i_handle, i_pos, SEEK_SET );
400 lseek( p_access_data->i_handle, i_pos, SEEK_SET );
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 )
407 msg_Err( p_input, "seeking too far" );
408 S.p_selected_area->i_tell = S.p_selected_area->i_size;
410 else if( S.p_selected_area->i_tell < 0 )
412 msg_Err( p_input, "seeking too early" );
413 S.p_selected_area->i_tell = 0;
415 vlc_mutex_unlock( &S.stream_lock );