1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
5 * Copyright © 2006 Rémi Denis-Courmont
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan # org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
30 #include <vlc/input.h>
31 #include <vlc_interaction.h>
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
39 #ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
46 #if defined( WIN32 ) && !defined( UNDER_CE )
53 #if defined( WIN32 ) && !defined( UNDER_CE )
54 /* fstat() support for large files on win32 */
55 # define fstat(a,b) _fstati64(a,b)
59 # define lseek _lseeki64
60 #elif defined( UNDER_CE )
64 # define read(a,b,c) fread(b,1,c,a)
65 # define close(a) fclose(a)
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Open ( vlc_object_t * );
78 static void Close( vlc_object_t * );
80 #define CACHING_TEXT N_("Caching value in ms")
81 #define CACHING_LONGTEXT N_( \
82 "Caching value for files. This " \
83 "value should be set in milliseconds." )
84 #define CAT_TEXT N_("Concatenate with additional files")
85 #define CAT_LONGTEXT N_( \
86 "Play split files as if they were part of a unique file. " \
87 "You need to specify a comma-separated list of files." )
90 set_description( _("File input") );
91 set_shortname( _("File") );
92 set_category( CAT_INPUT );
93 set_subcategory( SUBCAT_INPUT_ACCESS );
94 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
95 add_deprecated( "file-cat", VLC_TRUE );
96 set_capability( "access2", 50 );
97 add_shortcut( "file" );
98 add_shortcut( "stream" );
99 add_shortcut( "kfir" );
100 set_callbacks( Open, Close );
104 /*****************************************************************************
105 * Exported prototypes
106 *****************************************************************************/
107 static int Seek( access_t *, int64_t );
108 static int Read( access_t *, uint8_t *, int );
109 static int Control( access_t *, int, va_list );
111 static int open_file( access_t *, const char * );
115 unsigned int i_nb_reads;
121 vlc_bool_t b_seekable;
122 vlc_bool_t b_pace_control;
125 /*****************************************************************************
126 * Open: open the file
127 *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
130 access_t *p_access = (access_t*)p_this;
133 vlc_bool_t b_stdin = !strcmp (p_access->psz_path, "-");
135 /* Update default_pts to a suitable value for file access */
136 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
138 STANDARD_READ_ACCESS_INIT;
139 p_sys->i_nb_reads = 0;
140 p_sys->b_kfir = VLC_FALSE;
141 int fd = p_sys->fd = -1;
143 if (!strcasecmp (p_access->psz_access, "stream"))
145 p_sys->b_seekable = VLC_FALSE;
146 p_sys->b_pace_control = VLC_FALSE;
148 else if (!strcasecmp (p_access->psz_access, "kfir"))
150 p_sys->b_seekable = VLC_FALSE;
151 p_sys->b_pace_control = VLC_FALSE;
152 p_sys->b_kfir = VLC_TRUE;
156 p_sys->b_seekable = VLC_TRUE;
157 p_sys->b_pace_control = VLC_TRUE;
161 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
166 fd = open_file (p_access, p_access->psz_path);
168 #ifdef HAVE_SYS_STAT_H
174 msg_Err (p_access, "fstat(%d): %s", fd, strerror (errno));
176 if (S_ISDIR (st.st_mode))
177 /* The directory plugin takes care of that */
178 msg_Dbg (p_access, "file is a directory, aborting");
194 #ifdef HAVE_SYS_STAT_H
195 p_access->info.i_size = st.st_size;
196 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
197 && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
198 p_sys->b_seekable = VLC_FALSE;
200 p_sys->b_seekable = !b_stdin;
203 if (p_sys->b_seekable && (p_access->info.i_size == 0))
205 /* FIXME that's bad because all others access will be probed */
206 msg_Err (p_access, "file is empty, aborting");
214 /*****************************************************************************
215 * Close: close the target
216 *****************************************************************************/
217 static void Close (vlc_object_t * p_this)
219 access_t *p_access = (access_t*)p_this;
220 access_sys_t *p_sys = p_access->p_sys;
226 /*****************************************************************************
227 * Read: standard read on a file descriptor.
228 *****************************************************************************/
229 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
231 access_sys_t *p_sys = p_access->p_sys;
235 #if !defined(WIN32) && !defined(UNDER_CE)
236 if( !p_sys->b_pace_control )
240 /* Find if some data is available. This won't work under Windows. */
245 if( p_access->b_die )
248 memset (&ufd, 0, sizeof (ufd));
252 i_ret = poll (&ufd, 1, 500);
256 i_ret = read (fd, p_buffer, i_len);
260 /* b_kfir ; work around a buggy poll() driver implementation */
261 while (((i_ret = read (fd, p_buffer, i_len)) == 0)
264 msleep( INPUT_ERROR_SLEEP );
269 #endif /* WIN32 || UNDER_CE */
270 /* b_pace_control || WIN32 */
271 i_ret = read( fd, p_buffer, i_len );
282 msg_Err (p_access, "read failed (%s)", strerror (errno));
283 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
284 _("VLC could not read file \"%s\"."),
288 /* Delay a bit to avoid consuming all the CPU. This is particularly
289 * useful when reading from an unconnected FIFO. */
290 msleep( INPUT_ERROR_SLEEP );
295 #ifdef HAVE_SYS_STAT_H
296 if( p_access->info.i_size != 0 &&
297 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
301 if ((fstat (fd, &st) == 0)
302 && (p_access->info.i_size != st.st_size))
304 p_access->info.i_size = st.st_size;
305 p_access->info.i_update |= INPUT_UPDATE_SIZE;
311 p_access->info.i_pos += i_ret;
312 else if( i_ret == 0 )
313 p_access->info.b_eof = VLC_TRUE;
318 /*****************************************************************************
319 * Seek: seek to a specific location in a file
320 *****************************************************************************/
321 static int Seek (access_t *p_access, int64_t i_pos)
323 access_sys_t *p_sys = p_access->p_sys;
325 if (p_access->info.i_size < p_access->info.i_pos)
327 msg_Err (p_access, "seeking too far");
328 i_pos = p_access->info.i_pos = p_access->info.i_size;
330 else if (p_access->info.i_pos < 0)
332 msg_Err (p_access, "seeking too early");
333 i_pos = p_access->info.i_pos = 0;
336 p_access->info.i_pos = i_pos;
337 p_access->info.b_eof = VLC_FALSE;
339 /* Determine which file we need to access */
340 lseek (p_sys->fd, i_pos, SEEK_SET);
344 /*****************************************************************************
346 *****************************************************************************/
347 static int Control( access_t *p_access, int i_query, va_list args )
349 access_sys_t *p_sys = p_access->p_sys;
357 case ACCESS_CAN_SEEK:
358 case ACCESS_CAN_FASTSEEK:
359 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
360 *pb_bool = p_sys->b_seekable;
363 case ACCESS_CAN_PAUSE:
364 case ACCESS_CAN_CONTROL_PACE:
365 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
366 *pb_bool = p_sys->b_pace_control;
371 pi_int = (int*)va_arg( args, int * );
375 case ACCESS_GET_PTS_DELAY:
376 pi_64 = (int64_t*)va_arg( args, int64_t * );
377 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
381 case ACCESS_SET_PAUSE_STATE:
385 case ACCESS_GET_TITLE_INFO:
386 case ACCESS_SET_TITLE:
387 case ACCESS_SET_SEEKPOINT:
388 case ACCESS_SET_PRIVATE_ID_STATE:
389 case ACCESS_GET_META:
393 msg_Warn( p_access, "unimplemented query in control" );
401 static char *expand_path (const access_t *p_access, const char *path)
403 if (strncmp (path, "~/", 2) == 0)
407 // TODO: we should also support the ~cmassiot/ syntax
408 if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
414 if (!strcasecmp (p_access->psz_access, "file")
415 && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
416 // Explorer can open path such as file:/C:/ or file:///C:/
417 // hence remove leading / if found
418 return strdup (path + 1);
421 return strdup (path);
425 /*****************************************************************************
426 * open_file: Opens a specific file
427 *****************************************************************************/
428 static int open_file (access_t *p_access, const char *psz_name)
430 char *path = expand_path (p_access, psz_name);
433 p_sys->fd = utf8_fopen( path, "rb" );
436 msg_Err( p_access, "cannot open file %s", psz_name );
437 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
438 _("VLC could not open file \"%s\"."), psz_name );
443 fseek( p_sys->fd, 0, SEEK_END );
444 p_access->info.i_size = ftell( p_sys->fd );
445 p_access->info.i_update |= INPUT_UPDATE_SIZE;
446 fseek( p_sys->fd, 0, SEEK_SET );
448 const char *psz_localname = ToLocale (path);
449 if (psz_localname == NULL)
451 msg_Err (p_access, "incorrect file name %s", psz_name);
456 // FIXME: support non-ANSI filenames on Win32
457 int fd = open (path, O_NONBLOCK /*| O_LARGEFILE*/);
458 LocaleFree (psz_localname);
463 msg_Err (p_access, "cannot open file %s (%s)", psz_name,
465 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
466 _("VLC could not open file \"%s\" (%s)."),
467 psz_name, strerror (errno));
471 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
472 /* We'd rather use any available memory for reading ahead
473 * than for caching what we've already seen/heard */
474 fcntl (fd, F_RDAHEAD, 1);
475 fcntl (fd, F_NOCACHE, 1);