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)
56 # define FILESTAT _stati64
60 # define lseek _lseeki64
61 #elif defined( UNDER_CE )
65 # define read(a,b,c) fread(b,1,c,a)
66 # define close(a) fclose(a)
72 # define FILESTAT stat
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open ( vlc_object_t * );
81 static void Close( vlc_object_t * );
83 #define CACHING_TEXT N_("Caching value in ms")
84 #define CACHING_LONGTEXT N_( \
85 "Caching value for files. This " \
86 "value should be set in milliseconds." )
87 #define CAT_TEXT N_("Concatenate with additional files")
88 #define CAT_LONGTEXT N_( \
89 "Play split files as if they were part of a unique file. " \
90 "You need to specify a comma-separated list of files." )
93 set_description( _("File input") );
94 set_shortname( _("File") );
95 set_category( CAT_INPUT );
96 set_subcategory( SUBCAT_INPUT_ACCESS );
97 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
98 add_deprecated( "file-cat", VLC_TRUE );
99 set_capability( "access2", 50 );
100 add_shortcut( "file" );
101 add_shortcut( "stream" );
102 add_shortcut( "kfir" );
103 set_callbacks( Open, Close );
107 /*****************************************************************************
108 * Exported prototypes
109 *****************************************************************************/
110 static int Seek( access_t *, int64_t );
111 static int Read( access_t *, uint8_t *, int );
112 static int Control( access_t *, int, va_list );
114 static int open_file( access_t *, const char * );
118 unsigned int i_nb_reads;
124 vlc_bool_t b_seekable;
125 vlc_bool_t b_pace_control;
128 /*****************************************************************************
129 * Open: open the file
130 *****************************************************************************/
131 static int Open( vlc_object_t *p_this )
133 access_t *p_access = (access_t*)p_this;
136 vlc_bool_t b_stdin = !strcmp (p_access->psz_path, "-");
138 /* Update default_pts to a suitable value for file access */
139 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
141 STANDARD_READ_ACCESS_INIT;
142 p_sys->i_nb_reads = 0;
143 p_sys->b_kfir = VLC_FALSE;
144 int fd = p_sys->fd = -1;
146 if (!strcasecmp (p_access->psz_access, "stream"))
148 p_sys->b_seekable = VLC_FALSE;
149 p_sys->b_pace_control = VLC_FALSE;
151 else if (!strcasecmp (p_access->psz_access, "kfir"))
153 p_sys->b_seekable = VLC_FALSE;
154 p_sys->b_pace_control = VLC_FALSE;
155 p_sys->b_kfir = VLC_TRUE;
159 p_sys->b_seekable = VLC_TRUE;
160 p_sys->b_pace_control = VLC_TRUE;
164 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
169 fd = open_file (p_access, p_access->psz_path);
171 #ifdef HAVE_SYS_STAT_H
177 msg_Err (p_access, "fstat(%d): %s", fd, strerror (errno));
179 if (S_ISDIR (st.st_mode))
180 /* The directory plugin takes care of that */
181 msg_Dbg (p_access, "file is a directory, aborting");
197 #ifdef HAVE_SYS_STAT_H
198 p_access->info.i_size = st.st_size;
199 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
200 && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
201 p_sys->b_seekable = VLC_FALSE;
203 p_sys->b_seekable = !b_stdin;
204 # warning File size not known!
207 if (p_sys->b_seekable && (p_access->info.i_size == 0))
209 /* FIXME that's bad because all others access will be probed */
210 msg_Err (p_access, "file is empty, aborting");
214 msg_Dbg (p_access, "opened file of size "I64Fd" (FIXME: remove this)",
215 p_access->info.i_size);
220 /*****************************************************************************
221 * Close: close the target
222 *****************************************************************************/
223 static void Close (vlc_object_t * p_this)
225 access_t *p_access = (access_t*)p_this;
226 access_sys_t *p_sys = p_access->p_sys;
232 /*****************************************************************************
233 * Read: standard read on a file descriptor.
234 *****************************************************************************/
235 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
237 access_sys_t *p_sys = p_access->p_sys;
241 #if !defined(WIN32) && !defined(UNDER_CE)
242 if( !p_sys->b_pace_control )
246 /* Find if some data is available. This won't work under Windows. */
251 if( p_access->b_die )
254 memset (&ufd, 0, sizeof (ufd));
258 i_ret = poll (&ufd, 1, 500);
262 i_ret = read (fd, p_buffer, i_len);
266 /* b_kfir ; work around a buggy poll() driver implementation */
267 while (((i_ret = read (fd, p_buffer, i_len)) == 0)
270 msleep( INPUT_ERROR_SLEEP );
275 #endif /* WIN32 || UNDER_CE */
276 /* b_pace_control || WIN32 */
277 i_ret = read( fd, p_buffer, i_len );
288 msg_Err (p_access, "read failed (%s)", strerror (errno));
289 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
290 _("VLC could not read file \"%s\"."),
294 /* Delay a bit to avoid consuming all the CPU. This is particularly
295 * useful when reading from an unconnected FIFO. */
296 msleep( INPUT_ERROR_SLEEP );
301 #ifdef HAVE_SYS_STAT_H
302 if( p_access->info.i_size != 0 &&
303 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
307 if ((fstat (fd, &st) == 0)
308 && (p_access->info.i_size != st.st_size))
310 p_access->info.i_size = st.st_size;
311 p_access->info.i_update |= INPUT_UPDATE_SIZE;
317 p_access->info.i_pos += i_ret;
318 else if( i_ret == 0 )
319 p_access->info.b_eof = VLC_TRUE;
324 /*****************************************************************************
325 * Seek: seek to a specific location in a file
326 *****************************************************************************/
327 static int Seek (access_t *p_access, int64_t i_pos)
329 if (i_pos > p_access->info.i_size)
331 msg_Err (p_access, "seeking too far");
332 i_pos = p_access->info.i_size;
336 msg_Err (p_access, "seeking too early");
340 p_access->info.i_pos = i_pos;
341 p_access->info.b_eof = VLC_FALSE;
343 /* Determine which file we need to access */
344 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
348 /*****************************************************************************
350 *****************************************************************************/
351 static int Control( access_t *p_access, int i_query, va_list args )
353 access_sys_t *p_sys = p_access->p_sys;
361 case ACCESS_CAN_SEEK:
362 case ACCESS_CAN_FASTSEEK:
363 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
364 *pb_bool = p_sys->b_seekable;
367 case ACCESS_CAN_PAUSE:
368 case ACCESS_CAN_CONTROL_PACE:
369 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
370 *pb_bool = p_sys->b_pace_control;
375 pi_int = (int*)va_arg( args, int * );
379 case ACCESS_GET_PTS_DELAY:
380 pi_64 = (int64_t*)va_arg( args, int64_t * );
381 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
385 case ACCESS_SET_PAUSE_STATE:
389 case ACCESS_GET_TITLE_INFO:
390 case ACCESS_SET_TITLE:
391 case ACCESS_SET_SEEKPOINT:
392 case ACCESS_SET_PRIVATE_ID_STATE:
393 case ACCESS_GET_META:
397 msg_Warn( p_access, "unimplemented query in control" );
405 static char *expand_path (const access_t *p_access, const char *path)
407 if (strncmp (path, "~/", 2) == 0)
411 // TODO: we should also support the ~cmassiot/ syntax
412 if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
418 if (!strcasecmp (p_access->psz_access, "file")
419 && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
420 // Explorer can open path such as file:/C:/ or file:///C:/
421 // hence remove leading / if found
422 return strdup (path + 1);
425 return strdup (path);
429 /*****************************************************************************
430 * open_file: Opens a specific file
431 *****************************************************************************/
432 static int open_file (access_t *p_access, const char *psz_name)
434 char *path = expand_path (p_access, psz_name);
437 p_sys->fd = utf8_fopen( path, "rb" );
440 msg_Err( p_access, "cannot open file %s", psz_name );
441 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
442 _("VLC could not open file \"%s\"."), psz_name );
447 fseek( p_sys->fd, 0, SEEK_END );
448 p_access->info.i_size = ftell( p_sys->fd );
449 p_access->info.i_update |= INPUT_UPDATE_SIZE;
450 fseek( p_sys->fd, 0, SEEK_SET );
452 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
455 msg_Err (p_access, "cannot open file %s (%s)", psz_name,
457 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
458 _("VLC could not open file \"%s\" (%s)."),
459 psz_name, strerror (errno));
463 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
464 /* We'd rather use any available memory for reading ahead
465 * than for caching what we've already seen/heard */
466 fcntl (fd, F_RDAHEAD, 1);
467 fcntl (fd, F_NOCACHE, 1);