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_string( "file-cat", NULL, NULL, CAT_TEXT, CAT_LONGTEXT, 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 OpenFile( access_t *, const char * );
115 unsigned int i_nb_reads;
127 vlc_bool_t b_seekable;
128 vlc_bool_t b_pace_control;
131 /*****************************************************************************
132 * Open: open the file
133 *****************************************************************************/
134 static int Open( vlc_object_t *p_this )
136 access_t *p_access = (access_t*)p_this;
140 vlc_bool_t b_stdin = !strcmp (p_access->psz_path, "-");
142 /* Update default_pts to a suitable value for file access */
143 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
145 STANDARD_READ_ACCESS_INIT;
146 p_sys->i_nb_reads = 0;
147 p_sys->b_kfir = VLC_FALSE;
151 if (!strcasecmp (p_access->psz_access, "stream"))
153 p_sys->b_seekable = VLC_FALSE;
154 p_sys->b_pace_control = VLC_FALSE;
156 else if (!strcasecmp (p_access->psz_access, "kfir"))
158 p_sys->b_seekable = VLC_FALSE;
159 p_sys->b_pace_control = VLC_FALSE;
160 p_sys->b_kfir = VLC_TRUE;
164 p_sys->b_seekable = VLC_TRUE;
165 p_sys->b_pace_control = VLC_TRUE;
168 /* Count number of files */
169 catlist = var_CreateGetString (p_access, "file-cat");
178 for (const char *ptr = catlist; ptr != NULL; p_sys->filec++)
180 ptr = strchr (ptr, ',');
186 p_sys->filev = calloc (p_sys->filec, sizeof (p_sys->filev[0]));
187 if (p_sys->filev == NULL)
194 p_sys->sizev = calloc (p_sys->filec, sizeof (p_sys->sizev[0]));
195 if (p_sys->sizev == NULL)
204 char *filename = catlist;
205 for (unsigned i = 0; i < p_sys->filec; i++)
211 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
216 fd = OpenFile (p_access, p_access->psz_path);
220 assert (filename != NULL);
222 char *ptr = strchr (filename, ',');
226 msg_Dbg (p_access, "opening additionnal file `%s'", filename);
227 fd = OpenFile (p_access, filename);
231 #ifdef HAVE_SYS_STAT_H
234 if ((fd != -1) && fstat (fd, &st))
236 msg_Err (p_access, "fstat(%d): %s", fd, strerror (errno));
249 p_sys->filev[i] = fd;
251 #ifdef HAVE_SYS_STAT_H
252 p_sys->sizev[i] = st.st_size;
253 p_access->info.i_size += st.st_size;
255 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
256 && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
257 // If one file is not seekable, the concatenation is not either
258 p_sys->b_seekable = VLC_FALSE;
260 p_sys->b_seekable = !b_stdin;
267 if (p_sys->b_seekable && !p_access->info.i_size)
269 /* FIXME that's bad because all others access will be probed */
270 msg_Err (p_access, "file %s is empty, aborting", p_access->psz_path);
278 /*****************************************************************************
279 * Close: close the target
280 *****************************************************************************/
281 static void Close (vlc_object_t * p_this)
283 access_t *p_access = (access_t*)p_this;
284 access_sys_t *p_sys = p_access->p_sys;
286 for (unsigned i = 0; i < p_sys->filec; i++)
287 close (p_sys->filev[i]);
294 /*****************************************************************************
295 * Read: standard read on a file descriptor.
296 *****************************************************************************/
297 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
299 access_sys_t *p_sys = p_access->p_sys;
301 int fd = p_sys->filev[p_sys->filep];
303 #if !defined(WIN32) && !defined(UNDER_CE)
304 if( !p_sys->b_pace_control )
308 /* Find if some data is available. This won't work under Windows. */
313 if( p_access->b_die )
316 memset (&ufd, 0, sizeof (ufd));
320 i_ret = poll (&ufd, 1, 500);
324 i_ret = read (fd, p_buffer, i_len);
328 /* b_kfir ; work around a buggy poll() driver implementation */
329 while (((i_ret = read (fd, p_buffer, i_len)) == 0)
332 msleep( INPUT_ERROR_SLEEP );
337 #endif /* WIN32 || UNDER_CE */
338 /* b_pace_control || WIN32 */
339 i_ret = read( fd, p_buffer, i_len );
350 msg_Err (p_access, "read failed (%s)", strerror (errno));
351 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
352 _("VLC could not read file \"%s\"."),
356 /* Delay a bit to avoid consuming all the CPU. This is particularly
357 * useful when reading from an unconnected FIFO. */
358 msleep( INPUT_ERROR_SLEEP );
363 #ifdef HAVE_SYS_STAT_H
364 if( p_access->info.i_size != 0 &&
365 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
368 int i = p_sys->filep;
370 if ((fstat (fd, &st) == 0)
371 && (p_sys->sizev[i] != st.st_size))
373 p_access->info.i_size += st.st_size - p_sys->sizev[i];
374 p_sys->sizev[i] = st.st_size;
375 p_access->info.i_update |= INPUT_UPDATE_SIZE;
380 /* If we reached an EOF then switch to the next file in the list */
383 if (++p_sys->filep < p_sys->filec)
384 /* We have to read some data */
385 return Read (p_access, p_buffer, i_len);
391 p_access->info.i_pos += i_ret;
392 else if( i_ret == 0 )
393 p_access->info.b_eof = VLC_TRUE;
398 /*****************************************************************************
399 * Seek: seek to a specific location in a file
400 *****************************************************************************/
401 static int Seek (access_t *p_access, int64_t i_pos)
403 access_sys_t *p_sys = p_access->p_sys;
405 if (p_access->info.i_size < p_access->info.i_pos)
407 msg_Err (p_access, "seeking too far");
408 i_pos = p_access->info.i_pos = p_access->info.i_size;
410 else if (p_access->info.i_pos < 0)
412 msg_Err (p_access, "seeking too early");
413 i_pos = p_access->info.i_pos = 0;
416 p_access->info.i_pos = i_pos;
417 p_access->info.b_eof = VLC_FALSE;
419 /* Determine which file we need to access */
421 assert (p_sys->filec > 0);
423 while (i_pos > p_sys->sizev[i])
425 i_pos -= p_sys->sizev[i++];
426 assert (i < p_sys->filec);
430 lseek (p_sys->filev[i], i_pos, SEEK_SET);
434 /*****************************************************************************
436 *****************************************************************************/
437 static int Control( access_t *p_access, int i_query, va_list args )
439 access_sys_t *p_sys = p_access->p_sys;
447 case ACCESS_CAN_SEEK:
448 case ACCESS_CAN_FASTSEEK:
449 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
450 *pb_bool = p_sys->b_seekable;
453 case ACCESS_CAN_PAUSE:
454 case ACCESS_CAN_CONTROL_PACE:
455 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
456 *pb_bool = p_sys->b_pace_control;
461 pi_int = (int*)va_arg( args, int * );
465 case ACCESS_GET_PTS_DELAY:
466 pi_64 = (int64_t*)va_arg( args, int64_t * );
467 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
471 case ACCESS_SET_PAUSE_STATE:
475 case ACCESS_GET_TITLE_INFO:
476 case ACCESS_SET_TITLE:
477 case ACCESS_SET_SEEKPOINT:
478 case ACCESS_SET_PRIVATE_ID_STATE:
479 case ACCESS_GET_META:
483 msg_Warn( p_access, "unimplemented query in control" );
491 static char *expand_path (const access_t *p_access, const char *path)
493 if (strncmp (path, "~/", 2) == 0)
497 // TODO: we should also support the ~cmassiot/ syntax
498 if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
504 if (!strcasecmp (p_access->psz_access, "file")
505 && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
506 // Explorer can open path such as file:/C:/ or file:///C:/
507 // hence remove leading / if found
508 return strdup (path + 1);
511 return strdup (path);
515 /*****************************************************************************
516 * OpenFile: Opens a specific file
517 *****************************************************************************/
518 static int OpenFile (access_t *p_access, const char *psz_name)
520 char *path = expand_path (p_access, psz_name);
523 p_sys->fd = utf8_fopen( path, "rb" );
526 msg_Err( p_access, "cannot open file %s", psz_name );
527 intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
528 _("VLC could not open file \"%s\"."), psz_name );
533 fseek( p_sys->fd, 0, SEEK_END );
534 p_access->info.i_size = ftell( p_sys->fd );
535 p_access->info.i_update |= INPUT_UPDATE_SIZE;
536 fseek( p_sys->fd, 0, SEEK_SET );
538 const char *psz_localname = ToLocale (path);
539 if (psz_localname == NULL)
541 msg_Err (p_access, "incorrect file name %s", psz_name);
546 // FIXME: support non-ANSI filenames on Win32
547 int fd = open (path, O_NONBLOCK /*| O_LARGEFILE*/);
548 LocaleFree (psz_localname);
553 msg_Err (p_access, "cannot open file %s (%s)", psz_name,
555 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
556 _("VLC could not open file \"%s\" (%s)."),
557 psz_name, strerror (errno));
561 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
562 /* We'd rather use any available memory for reading ahead
563 * than for caching what we've already seen/heard */
564 fcntl (fd, F_RDAHEAD, 1);
565 fcntl (fd, F_NOCACHE, 1);