1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: file.c,v 1.8 2003/01/03 13:07:17 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_STAT_H
36 # include <sys/stat.h>
47 #elif defined( _MSC_VER ) && 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 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 );
78 /*****************************************************************************
79 * _input_socket_t: private access plug-in data, modified to add private
81 *****************************************************************************/
82 typedef struct _input_socket_s
84 input_socket_t _socket;
86 unsigned int i_nb_reads;
89 /*****************************************************************************
91 *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
94 input_thread_t * p_input = (input_thread_t *)p_this;
95 char * psz_name = p_input->psz_name;
96 #ifdef HAVE_SYS_STAT_H
98 struct stat stat_info;
100 _input_socket_t * p_access_data;
105 b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
107 #ifdef HAVE_SYS_STAT_H
108 if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
111 msg_Err( p_input, "cannot stat() file `%s' (%s)",
112 psz_name, strerror(errno));
114 msg_Err( p_input, "cannot stat() file `%s'", psz_name );
120 p_input->pf_read = Read;
121 p_input->pf_set_program = input_SetProgram;
122 p_input->pf_set_area = NULL;
123 p_input->pf_seek = Seek;
125 vlc_mutex_lock( &p_input->stream.stream_lock );
127 p_input->stream.b_connected = 1;
128 if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
131 p_input->stream.b_pace_control = 0;
132 p_input->stream.b_seekable = 0;
133 p_input->stream.p_selected_area->i_size = 0;
138 p_input->stream.b_pace_control = 1;
142 p_input->stream.b_seekable = 0;
143 p_input->stream.p_selected_area->i_size = 0;
148 /* We'll update i_size after it's been opened */
149 p_input->stream.b_seekable = 1;
151 #elif defined( HAVE_SYS_STAT_H )
152 else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
153 || S_ISBLK(stat_info.st_mode) )
155 p_input->stream.b_seekable = 1;
156 p_input->stream.p_selected_area->i_size = stat_info.st_size;
158 else if( S_ISFIFO(stat_info.st_mode)
159 # if !defined( SYS_BEOS ) && !defined( WIN32 )
160 || S_ISSOCK(stat_info.st_mode)
164 p_input->stream.b_seekable = 0;
165 p_input->stream.p_selected_area->i_size = 0;
170 vlc_mutex_unlock( &p_input->stream.stream_lock );
171 msg_Err( p_input, "unknown file type for `%s'", psz_name );
176 p_input->stream.p_selected_area->i_tell = 0;
177 p_input->stream.i_method = INPUT_METHOD_FILE;
178 vlc_mutex_unlock( &p_input->stream.stream_lock );
180 msg_Dbg( p_input, "opening file `%s'", psz_name );
181 p_access_data = malloc( sizeof(_input_socket_t) );
182 p_input->p_access_data = (void *)p_access_data;
183 if( p_access_data == NULL )
185 msg_Err( p_input, "out of memory" );
189 p_access_data->i_nb_reads = 0;
192 p_access_data->_socket.i_handle = 0;
197 wchar_t psz_filename[MAX_PATH];
198 MultiByteToWideChar( CP_ACP, 0, psz_name, -1, psz_filename, MAX_PATH );
200 p_access_data->_socket.i_handle = (int)CreateFile( psz_filename,
201 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
202 FILE_ATTRIBUTE_NORMAL, NULL );
204 if( (HANDLE)p_access_data->_socket.i_handle == INVALID_HANDLE_VALUE )
206 msg_Err( p_input, "cannot open file %s", psz_name );
207 free( p_access_data );
210 p_input->stream.p_selected_area->i_size =
211 GetFileSize( (HANDLE)p_access_data->_socket.i_handle, NULL );
213 p_access_data->_socket.i_handle = open( psz_name,
214 O_NONBLOCK /*| O_LARGEFILE*/ );
215 if( p_access_data->_socket.i_handle == -1 )
218 msg_Err( p_input, "cannot open file %s (%s)", psz_name,
221 msg_Err( p_input, "cannot open file %s", psz_name );
223 free( p_access_data );
229 if ( p_input->stream.b_seekable
230 && !p_input->stream.p_selected_area->i_size )
232 msg_Err( p_input, "file %s is empty, aborting", psz_name );
233 free( p_access_data );
237 /* Update default_pts to a suitable value for file access */
238 p_input->i_pts_delay = config_GetInt( p_input, "file-caching" ) * 1000;
243 /*****************************************************************************
244 * Close: close the target
245 *****************************************************************************/
246 static void Close( vlc_object_t * p_this )
248 input_thread_t * p_input = (input_thread_t *)p_this;
249 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
251 msg_Info( p_input, "closing `%s/%s://%s'",
252 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
255 CloseHandle( (HANDLE)p_access_data->i_handle );
257 close( p_access_data->i_handle );
260 free( p_access_data );
263 /*****************************************************************************
264 * Read: standard read on a file descriptor.
265 *****************************************************************************/
266 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
268 _input_socket_t * p_access_data = (_input_socket_t *)p_input->p_access_data;
272 if( !ReadFile( (HANDLE)p_access_data->_socket.i_handle, p_buffer, i_len,
273 (LPDWORD)&i_ret, NULL ) )
278 i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
284 if ( errno != EINTR && errno != EAGAIN )
285 msg_Err( p_input, "read failed (%s)", strerror(errno) );
287 msg_Err( p_input, "read failed" );
290 /* Delay a bit to avoid consuming all the CPU. This is particularly
291 * useful when reading from an unconnected FIFO. */
292 msleep( INPUT_ERROR_SLEEP );
295 p_access_data->i_nb_reads++;
296 #ifdef HAVE_SYS_STAT_H
297 if ( p_input->stream.p_selected_area->i_size != 0
298 && (p_access_data->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
300 struct stat stat_info;
301 if ( fstat( p_access_data->_socket.i_handle, &stat_info ) == -1 )
304 msg_Warn( p_input, "couldn't stat again the file (%s)",
307 msg_Warn( p_input, "couldn't stat again the file" );
312 p_input->stream.p_selected_area->i_size = stat_info.st_size;
313 p_input->stream.b_changed = 1;
321 /*****************************************************************************
322 * Seek: seek to a specific location in a file
323 *****************************************************************************/
324 static void Seek( input_thread_t * p_input, off_t i_pos )
326 #define S p_input->stream
327 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
329 lseek( p_access_data->i_handle, i_pos, SEEK_SET );
331 vlc_mutex_lock( &S.stream_lock );
332 S.p_selected_area->i_tell = i_pos;
333 if( S.p_selected_area->i_tell > S.p_selected_area->i_size )
335 msg_Err( p_input, "seeking too far" );
336 S.p_selected_area->i_tell = S.p_selected_area->i_size;
338 else if( S.p_selected_area->i_tell < 0 )
340 msg_Err( p_input, "seeking too early" );
341 S.p_selected_area->i_tell = 0;
343 vlc_mutex_unlock( &S.stream_lock );