1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: file.c,v 1.15 2003/03/30 18:14:35 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 #if defined( WIN32 ) && !defined( UNDER_CE )
55 # define lseek _lseeki64
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close ( vlc_object_t * );
63 static void Seek ( input_thread_t *, off_t );
64 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
66 /*****************************************************************************
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." )
75 set_description( _("Standard filesystem file input") );
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 );
84 /*****************************************************************************
85 * _input_socket_t: private access plug-in data, modified to add private
87 *****************************************************************************/
88 typedef struct _input_socket_s
90 input_socket_t _socket;
92 unsigned int i_nb_reads;
95 /*****************************************************************************
97 *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
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
104 struct stat stat_info;
106 _input_socket_t * p_access_data;
111 b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
113 #ifdef HAVE_SYS_STAT_H
114 if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
117 msg_Warn( p_input, "cannot stat() file `%s' (%s)",
118 psz_name, strerror(errno));
120 msg_Warn( p_input, "cannot stat() file `%s'", psz_name );
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;
131 vlc_mutex_lock( &p_input->stream.stream_lock );
133 if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
136 p_input->stream.b_pace_control = 0;
137 p_input->stream.b_seekable = 0;
138 p_input->stream.p_selected_area->i_size = 0;
143 p_input->stream.b_pace_control = 1;
147 p_input->stream.b_seekable = 0;
148 p_input->stream.p_selected_area->i_size = 0;
153 /* We'll update i_size after it's been opened */
154 p_input->stream.b_seekable = 1;
156 #elif defined( HAVE_SYS_STAT_H )
157 else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
158 || S_ISBLK(stat_info.st_mode) )
160 p_input->stream.b_seekable = 1;
161 p_input->stream.p_selected_area->i_size = stat_info.st_size;
163 else if( S_ISFIFO(stat_info.st_mode)
164 # if !defined( SYS_BEOS ) && !defined( WIN32 )
165 || S_ISSOCK(stat_info.st_mode)
169 p_input->stream.b_seekable = 0;
170 p_input->stream.p_selected_area->i_size = 0;
175 vlc_mutex_unlock( &p_input->stream.stream_lock );
176 msg_Err( p_input, "unknown file type for `%s'", psz_name );
181 p_input->stream.p_selected_area->i_tell = 0;
182 p_input->stream.i_method = INPUT_METHOD_FILE;
183 vlc_mutex_unlock( &p_input->stream.stream_lock );
185 msg_Dbg( p_input, "opening file `%s'", psz_name );
186 p_access_data = malloc( sizeof(_input_socket_t) );
187 p_input->p_access_data = (void *)p_access_data;
188 if( p_access_data == NULL )
190 msg_Err( p_input, "out of memory" );
194 p_access_data->i_nb_reads = 0;
197 p_access_data->_socket.i_handle = 0;
202 wchar_t psz_filename[MAX_PATH];
203 MultiByteToWideChar( CP_ACP, 0, psz_name, -1, psz_filename, MAX_PATH );
205 p_access_data->_socket.i_handle = (int)CreateFile( psz_filename,
206 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
207 FILE_ATTRIBUTE_NORMAL, NULL );
209 if( (HANDLE)p_access_data->_socket.i_handle == INVALID_HANDLE_VALUE )
211 msg_Err( p_input, "cannot open file %s", psz_name );
212 free( p_access_data );
215 p_input->stream.p_selected_area->i_size =
216 GetFileSize( (HANDLE)p_access_data->_socket.i_handle, NULL );
218 p_access_data->_socket.i_handle = open( psz_name,
219 O_NONBLOCK /*| O_LARGEFILE*/ );
220 if( p_access_data->_socket.i_handle == -1 )
223 msg_Err( p_input, "cannot open file %s (%s)", psz_name,
226 msg_Err( p_input, "cannot open file %s", psz_name );
228 free( p_access_data );
234 if ( p_input->stream.b_seekable
235 && !p_input->stream.p_selected_area->i_size )
237 msg_Err( p_input, "file %s is empty, aborting", psz_name );
238 free( p_access_data );
242 /* Update default_pts to a suitable value for file access */
243 p_input->i_pts_delay = config_GetInt( p_input, "file-caching" ) * 1000;
248 /*****************************************************************************
249 * Close: close the target
250 *****************************************************************************/
251 static void Close( vlc_object_t * p_this )
253 input_thread_t * p_input = (input_thread_t *)p_this;
254 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
256 msg_Info( p_input, "closing `%s/%s://%s'",
257 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
260 CloseHandle( (HANDLE)p_access_data->i_handle );
262 close( p_access_data->i_handle );
265 free( p_access_data );
268 /*****************************************************************************
269 * Read: standard read on a file descriptor.
270 *****************************************************************************/
271 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
273 _input_socket_t * p_access_data = (_input_socket_t *)p_input->p_access_data;
277 if( !ReadFile( (HANDLE)p_access_data->_socket.i_handle, p_buffer, i_len,
278 (LPDWORD)&i_ret, NULL ) )
283 i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
289 if ( errno != EINTR && errno != EAGAIN )
290 msg_Err( p_input, "read failed (%s)", strerror(errno) );
292 msg_Err( p_input, "read failed" );
295 /* Delay a bit to avoid consuming all the CPU. This is particularly
296 * useful when reading from an unconnected FIFO. */
297 msleep( INPUT_ERROR_SLEEP );
300 p_access_data->i_nb_reads++;
301 #ifdef HAVE_SYS_STAT_H
302 if ( p_input->stream.p_selected_area->i_size != 0
303 && (p_access_data->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
305 struct stat stat_info;
306 if ( fstat( p_access_data->_socket.i_handle, &stat_info ) == -1 )
309 msg_Warn( p_input, "couldn't stat again the file (%s)",
312 msg_Warn( p_input, "couldn't stat again the file" );
315 else if ( p_input->stream.p_selected_area->i_size != stat_info.st_size )
317 p_input->stream.p_selected_area->i_size = stat_info.st_size;
318 p_input->stream.b_changed = 1;
326 /*****************************************************************************
327 * Seek: seek to a specific location in a file
328 *****************************************************************************/
329 static void Seek( input_thread_t * p_input, off_t i_pos )
331 #define S p_input->stream
332 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
334 lseek( p_access_data->i_handle, i_pos, SEEK_SET );
336 vlc_mutex_lock( &S.stream_lock );
337 S.p_selected_area->i_tell = i_pos;
338 if( S.p_selected_area->i_tell > S.p_selected_area->i_size )
340 msg_Err( p_input, "seeking too far" );
341 S.p_selected_area->i_tell = S.p_selected_area->i_size;
343 else if( S.p_selected_area->i_tell < 0 )
345 msg_Err( p_input, "seeking too early" );
346 S.p_selected_area->i_tell = 0;
348 vlc_mutex_unlock( &S.stream_lock );