1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
5 * Copyright © 2006-2007 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 *****************************************************************************/
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_interface.h>
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
51 #if defined( WIN32 ) && !defined( UNDER_CE )
59 #if defined( WIN32 ) && !defined( UNDER_CE )
63 # define lseek _lseeki64
64 #elif defined( UNDER_CE )
68 # define read(a,b,c) fread(b,1,c,a)
69 # define close(a) fclose(a)
76 #include <vlc_charset.h>
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Open ( vlc_object_t
* );
82 static void Close( vlc_object_t
* );
84 #define CACHING_TEXT N_("Caching value in ms")
85 #define CACHING_LONGTEXT N_( \
86 "Caching value for files. This " \
87 "value should be set in milliseconds." )
90 set_description( N_("File input") );
91 set_shortname( N_("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
, true );
95 add_obsolete_string( "file-cat" );
96 set_capability( "access", 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 ssize_t
Read( access_t
*, uint8_t *, size_t );
109 static int Control( access_t
*, int, va_list );
111 static int open_file( access_t
*, const char * );
115 unsigned int i_nb_reads
;
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 bool 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
= false;
141 int fd
= p_sys
->fd
= -1;
143 if (!strcasecmp (p_access
->psz_access
, "stream"))
145 p_sys
->b_seekable
= false;
146 p_sys
->b_pace_control
= false;
148 else if (!strcasecmp (p_access
->psz_access
, "kfir"))
150 p_sys
->b_seekable
= false;
151 p_sys
->b_pace_control
= false;
152 p_sys
->b_kfir
= true;
156 p_sys
->b_seekable
= true;
157 p_sys
->b_pace_control
= 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): %m", fd
);
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
)
197 && !S_ISBLK (st
.st_mode
)
198 && !S_ISCHR (st
.st_mode
))
199 p_sys
->b_seekable
= false;
201 p_sys
->b_seekable
= !b_stdin
;
202 # warning File size not known!
208 /*****************************************************************************
209 * Close: close the target
210 *****************************************************************************/
211 static void Close (vlc_object_t
* p_this
)
213 access_t
*p_access
= (access_t
*)p_this
;
214 access_sys_t
*p_sys
= p_access
->p_sys
;
220 /*****************************************************************************
221 * Read: standard read on a file descriptor.
222 *****************************************************************************/
223 static ssize_t
Read( access_t
*p_access
, uint8_t *p_buffer
, size_t i_len
)
225 access_sys_t
*p_sys
= p_access
->p_sys
;
229 #if !defined(WIN32) && !defined(UNDER_CE)
230 if( !p_sys
->b_pace_control
)
234 /* Find if some data is available. This won't work under Windows. */
239 if( p_access
->b_die
)
242 memset (&ufd
, 0, sizeof (ufd
));
246 i_ret
= poll (&ufd
, 1, 500);
250 i_ret
= read (fd
, p_buffer
, i_len
);
254 /* b_kfir ; work around a buggy poll() driver implementation */
255 while (((i_ret
= read (fd
, p_buffer
, i_len
)) == 0)
258 msleep( INPUT_ERROR_SLEEP
);
263 #endif /* WIN32 || UNDER_CE */
264 /* b_pace_control || WIN32 */
265 i_ret
= read( fd
, p_buffer
, i_len
);
276 msg_Err (p_access
, "read failed (%m)");
277 intf_UserFatal (p_access
, false, _("File reading failed"),
278 _("VLC could not read the file."));
281 /* Delay a bit to avoid consuming all the CPU. This is particularly
282 * useful when reading from an unconnected FIFO. */
283 msleep( INPUT_ERROR_SLEEP
);
288 #ifdef HAVE_SYS_STAT_H
289 if( p_access
->info
.i_size
!= 0 &&
290 (p_sys
->i_nb_reads
% INPUT_FSTAT_NB_READS
) == 0 )
294 if ((fstat (fd
, &st
) == 0)
295 && (p_access
->info
.i_size
!= st
.st_size
))
297 p_access
->info
.i_size
= st
.st_size
;
298 p_access
->info
.i_update
|= INPUT_UPDATE_SIZE
;
304 p_access
->info
.i_pos
+= i_ret
;
305 else if( i_ret
== 0 )
306 p_access
->info
.b_eof
= true;
312 /*****************************************************************************
313 * Seek: seek to a specific location in a file
314 *****************************************************************************/
315 static int Seek (access_t
*p_access
, int64_t i_pos
)
317 p_access
->info
.i_pos
= i_pos
;
318 p_access
->info
.b_eof
= false;
320 lseek (p_access
->p_sys
->fd
, i_pos
, SEEK_SET
);
324 /*****************************************************************************
326 *****************************************************************************/
327 static int Control( access_t
*p_access
, int i_query
, va_list args
)
329 access_sys_t
*p_sys
= p_access
->p_sys
;
337 case ACCESS_CAN_SEEK
:
338 case ACCESS_CAN_FASTSEEK
:
339 pb_bool
= (bool*)va_arg( args
, bool* );
340 *pb_bool
= p_sys
->b_seekable
;
343 case ACCESS_CAN_PAUSE
:
344 case ACCESS_CAN_CONTROL_PACE
:
345 pb_bool
= (bool*)va_arg( args
, bool* );
346 *pb_bool
= p_sys
->b_pace_control
;
351 pi_int
= (int*)va_arg( args
, int * );
355 case ACCESS_GET_PTS_DELAY
:
356 pi_64
= (int64_t*)va_arg( args
, int64_t * );
357 *pi_64
= var_GetInteger( p_access
, "file-caching" ) * INT64_C(1000);
361 case ACCESS_SET_PAUSE_STATE
:
365 case ACCESS_GET_TITLE_INFO
:
366 case ACCESS_SET_TITLE
:
367 case ACCESS_SET_SEEKPOINT
:
368 case ACCESS_SET_PRIVATE_ID_STATE
:
369 case ACCESS_GET_META
:
370 case ACCESS_GET_PRIVATE_ID_STATE
:
371 case ACCESS_GET_CONTENT_TYPE
:
375 msg_Warn( p_access
, "unimplemented query %d in control", i_query
);
382 /*****************************************************************************
383 * open_file: Opens a specific file
384 *****************************************************************************/
385 static int open_file (access_t
*p_access
, const char *path
)
388 if (!strcasecmp (p_access
->psz_access
, "file")
389 && ('/' == path
[0]) && isalpha (path
[1])
390 && (':' == path
[2]) && ('/' == path
[3]))
391 /* Explorer can open path such as file:/C:/ or file:///C:/
392 * hence remove leading / if found */
397 p_sys
->fd
= utf8_fopen( path
, "rb" );
400 msg_Err( p_access
, "cannot open file %s", path
);
401 intf_UserFatal( p_access
, false, _("File reading failed"),
402 _("VLC could not open the file \"%s\"."), path
);
406 fseek( p_sys
->fd
, 0, SEEK_END
);
407 p_access
->info
.i_size
= ftell( p_sys
->fd
);
408 p_access
->info
.i_update
|= INPUT_UPDATE_SIZE
;
409 fseek( p_sys
->fd
, 0, SEEK_SET
);
411 int fd
= utf8_open (path
, O_RDONLY
| O_NONBLOCK
/* O_LARGEFILE*/, 0666);
414 msg_Err (p_access
, "cannot open file %s (%m)", path
);
415 intf_UserFatal (p_access
, false, _("File reading failed"),
416 _("VLC could not open the file \"%s\"."), path
);
420 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
421 /* We'd rather use any available memory for reading ahead
422 * than for caching what we've already seen/heard */
423 fcntl (fd
, F_RDAHEAD
, 1);
424 fcntl (fd
, F_NOCACHE
, 1);