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 $
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 /*****************************************************************************
80 *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
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
87 struct stat stat_info;
89 input_socket_t * p_access_data;
94 b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
96 #ifdef HAVE_SYS_STAT_H
97 if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
100 msg_Err( p_input, "cannot stat() file `%s' (%s)",
101 psz_name, strerror(errno));
103 msg_Err( p_input, "cannot stat() file `%s'", psz_name );
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;
114 vlc_mutex_lock( &p_input->stream.stream_lock );
116 p_input->stream.b_connected = 1;
117 if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
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;
127 p_input->stream.b_pace_control = 1;
131 p_input->stream.b_seekable = 0;
132 p_input->stream.p_selected_area->i_size = 0;
137 /* We'll update i_size after it's been opened */
138 p_input->stream.b_seekable = 1;
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) )
144 p_input->stream.b_seekable = 1;
145 p_input->stream.p_selected_area->i_size = stat_info.st_size;
147 else if( S_ISFIFO(stat_info.st_mode)
148 # if !defined( SYS_BEOS ) && !defined( WIN32 )
149 || S_ISSOCK(stat_info.st_mode)
153 p_input->stream.b_seekable = 0;
154 p_input->stream.p_selected_area->i_size = 0;
159 vlc_mutex_unlock( &p_input->stream.stream_lock );
160 msg_Err( p_input, "unknown file type for `%s'", psz_name );
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 );
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 )
174 msg_Err( p_input, "out of memory" );
180 p_access_data->i_handle = 0;
185 wchar_t psz_filename[MAX_PATH];
186 MultiByteToWideChar( CP_ACP, 0, psz_name, -1, psz_filename, MAX_PATH );
188 p_access_data->i_handle = (int)CreateFile( psz_filename,
189 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
190 FILE_ATTRIBUTE_NORMAL, NULL );
192 if( (HANDLE)p_access_data->i_handle == INVALID_HANDLE_VALUE )
194 msg_Err( p_input, "cannot open file %s", psz_name );
195 free( p_access_data );
198 p_input->stream.p_selected_area->i_size =
199 GetFileSize( (HANDLE)p_access_data->i_handle, NULL );
201 p_access_data->i_handle = open( psz_name,
202 O_NONBLOCK /*| O_LARGEFILE*/ );
203 if( p_access_data->i_handle == -1 )
206 msg_Err( p_input, "cannot open file %s (%s)", psz_name,
209 msg_Err( p_input, "cannot open file %s", psz_name );
211 free( p_access_data );
217 if ( p_input->stream.b_seekable
218 && !p_input->stream.p_selected_area->i_size )
220 msg_Err( p_input, "file %s is empty, aborting", psz_name );
221 free( p_access_data );
225 /* Update default_pts to a suitable value for file access */
226 p_input->i_pts_delay = config_GetInt( p_input, "file-caching" ) * 1000;
231 /*****************************************************************************
232 * Close: close the target
233 *****************************************************************************/
234 static void Close( vlc_object_t * p_this )
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;
239 msg_Info( p_input, "closing `%s/%s://%s'",
240 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
243 CloseHandle( (HANDLE)p_access_data->i_handle );
245 close( p_access_data->i_handle );
248 free( p_access_data );
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 )
256 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
260 if( !ReadFile( (HANDLE)p_access_data->i_handle, p_buffer, i_len,
261 (LPDWORD)&i_ret, NULL ) )
266 i_ret = read( p_access_data->i_handle, p_buffer, i_len );
272 if ( errno != EINTR && errno != EAGAIN )
273 msg_Err( p_input, "read failed (%s)", strerror(errno) );
275 msg_Err( p_input, "read failed" );
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 );
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 )
291 #define S p_input->stream
292 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
294 lseek( p_access_data->i_handle, i_pos, SEEK_SET );
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 )
300 msg_Err( p_input, "seeking too far" );
301 S.p_selected_area->i_tell = S.p_selected_area->i_size;
303 else if( S.p_selected_area->i_tell < 0 )
305 msg_Err( p_input, "seeking too early" );
306 S.p_selected_area->i_tell = 0;
308 vlc_mutex_unlock( &S.stream_lock );