]> git.sesse.net Git - vlc/blob - modules/access/file.c
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / access / file.c
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
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Rémi Denis-Courmont <rem # videolan # org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_input.h>
35 #include <vlc_access.h>
36 #include <vlc_interface.h>
37
38 #include <assert.h>
39 #include <errno.h>
40 #ifdef HAVE_SYS_TYPES_H
41 #   include <sys/types.h>
42 #endif
43 #ifdef HAVE_SYS_STAT_H
44 #   include <sys/stat.h>
45 #endif
46 #ifdef HAVE_FCNTL_H
47 #   include <fcntl.h>
48 #endif
49
50 #if defined( WIN32 ) && !defined( UNDER_CE )
51 #   include <io.h>
52 #   include <ctype.h>
53 #else
54 #   include <unistd.h>
55 #   include <poll.h>
56 #endif
57
58 #if defined( WIN32 ) && !defined( UNDER_CE )
59 #   ifdef lseek
60 #      undef lseek
61 #   endif
62 #   define lseek _lseeki64
63 #elif defined( UNDER_CE )
64 #   ifdef read
65 #      undef read
66 #   endif
67 #   define read(a,b,c) fread(b,1,c,a)
68 #   define close(a) fclose(a)
69 #   ifdef lseek
70 #      undef lseek
71 #   endif
72 #   define lseek fseek
73 #endif
74
75 #include <vlc_charset.h>
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80 static int  Open ( vlc_object_t * );
81 static void Close( vlc_object_t * );
82
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
88 vlc_module_begin();
89     set_description( _("File input") );
90     set_shortname( _("File") );
91     set_category( CAT_INPUT );
92     set_subcategory( SUBCAT_INPUT_ACCESS );
93     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true );
94     add_obsolete_string( "file-cat" );
95     set_capability( "access", 50 );
96     add_shortcut( "file" );
97     add_shortcut( "stream" );
98     add_shortcut( "kfir" );
99     set_callbacks( Open, Close );
100 vlc_module_end();
101
102
103 /*****************************************************************************
104  * Exported prototypes
105  *****************************************************************************/
106 static int  Seek( access_t *, int64_t );
107 static ssize_t Read( access_t *, uint8_t *, size_t );
108 static int  Control( access_t *, int, va_list );
109
110 static int  open_file( access_t *, const char * );
111
112 struct access_sys_t
113 {
114     unsigned int i_nb_reads;
115     bool   b_kfir;
116
117     int fd;
118
119     /* */
120     bool b_seekable;
121     bool b_pace_control;
122 };
123
124 /*****************************************************************************
125  * Open: open the file
126  *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
128 {
129     access_t     *p_access = (access_t*)p_this;
130     access_sys_t *p_sys;
131
132     bool    b_stdin = !strcmp (p_access->psz_path, "-");
133
134     /* Update default_pts to a suitable value for file access */
135     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
136
137     STANDARD_READ_ACCESS_INIT;
138     p_sys->i_nb_reads = 0;
139     p_sys->b_kfir = false;
140     int fd = p_sys->fd = -1;
141
142     if (!strcasecmp (p_access->psz_access, "stream"))
143     {
144         p_sys->b_seekable = false;
145         p_sys->b_pace_control = false;
146     }
147     else if (!strcasecmp (p_access->psz_access, "kfir"))
148     {
149         p_sys->b_seekable = false;
150         p_sys->b_pace_control = false;
151         p_sys->b_kfir = true;
152     }
153     else
154     {
155         p_sys->b_seekable = true;
156         p_sys->b_pace_control = true;
157     }
158
159     /* Open file */
160     msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
161
162     if (b_stdin)
163         fd = dup (0);
164     else
165         fd = open_file (p_access, p_access->psz_path);
166
167 #ifdef HAVE_SYS_STAT_H
168     struct stat st;
169
170     while (fd != -1)
171     {
172         if (fstat (fd, &st))
173             msg_Err (p_access, "fstat(%d): %m", fd);
174         else
175         if (S_ISDIR (st.st_mode))
176             /* The directory plugin takes care of that */
177             msg_Dbg (p_access, "file is a directory, aborting");
178         else
179             break; // success
180
181         close (fd);
182         fd = -1;
183     }
184 #endif
185
186     if (fd == -1)
187     {
188         free (p_sys);
189         return VLC_EGENERIC;
190     }
191     p_sys->fd = fd;
192
193 #ifdef HAVE_SYS_STAT_H
194     p_access->info.i_size = st.st_size;
195     if (!S_ISREG (st.st_mode)
196      && !S_ISBLK (st.st_mode)
197      && !S_ISCHR (st.st_mode))
198         p_sys->b_seekable = false;
199 #else
200     p_sys->b_seekable = !b_stdin;
201 # warning File size not known!
202 #endif
203
204     return VLC_SUCCESS;
205 }
206
207 /*****************************************************************************
208  * Close: close the target
209  *****************************************************************************/
210 static void Close (vlc_object_t * p_this)
211 {
212     access_t     *p_access = (access_t*)p_this;
213     access_sys_t *p_sys = p_access->p_sys;
214
215     close (p_sys->fd);
216     free (p_sys);
217 }
218
219 /*****************************************************************************
220  * Read: standard read on a file descriptor.
221  *****************************************************************************/
222 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
223 {
224     access_sys_t *p_sys = p_access->p_sys;
225     ssize_t i_ret;
226     int fd = p_sys->fd;
227
228 #if !defined(WIN32) && !defined(UNDER_CE)
229     if( !p_sys->b_pace_control )
230     {
231         if( !p_sys->b_kfir )
232         {
233             /* Find if some data is available. This won't work under Windows. */
234             do
235             {
236                 struct pollfd ufd;
237
238                 if( p_access->b_die )
239                     return 0;
240
241                 memset (&ufd, 0, sizeof (ufd));
242                 ufd.fd = fd;
243                 ufd.events = POLLIN;
244
245                 i_ret = poll (&ufd, 1, 500);
246             }
247             while (i_ret <= 0);
248
249             i_ret = read (fd, p_buffer, i_len);
250         }
251         else
252         {
253             /* b_kfir ; work around a buggy poll() driver implementation */
254             while (((i_ret = read (fd, p_buffer, i_len)) == 0)
255                 && !p_access->b_die)
256             {
257                 msleep( INPUT_ERROR_SLEEP );
258             }
259         }
260     }
261     else
262 #endif /* WIN32 || UNDER_CE */
263         /* b_pace_control || WIN32 */
264         i_ret = read( fd, p_buffer, i_len );
265
266     if( i_ret < 0 )
267     {
268         switch (errno)
269         {
270             case EINTR:
271             case EAGAIN:
272                 break;
273
274             default:
275                 msg_Err (p_access, "read failed (%m)");
276                 intf_UserFatal (p_access, false, _("File reading failed"),
277                                 _("VLC could not read the file."));
278         }
279
280         /* Delay a bit to avoid consuming all the CPU. This is particularly
281          * useful when reading from an unconnected FIFO. */
282         msleep( INPUT_ERROR_SLEEP );
283     }
284
285     p_sys->i_nb_reads++;
286
287 #ifdef HAVE_SYS_STAT_H
288     if( p_access->info.i_size != 0 &&
289         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
290     {
291         struct stat st;
292
293         if ((fstat (fd, &st) == 0)
294          && (p_access->info.i_size != st.st_size))
295         {
296             p_access->info.i_size = st.st_size;
297             p_access->info.i_update |= INPUT_UPDATE_SIZE;
298         }
299     }
300 #endif
301
302     if( i_ret > 0 )
303         p_access->info.i_pos += i_ret;
304     else if( i_ret == 0 )
305         p_access->info.b_eof = true;
306
307     return i_ret;
308 }
309
310
311 /*****************************************************************************
312  * Seek: seek to a specific location in a file
313  *****************************************************************************/
314 static int Seek (access_t *p_access, int64_t i_pos)
315 {
316     p_access->info.i_pos = i_pos;
317     p_access->info.b_eof = false;
318
319     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
320     return VLC_SUCCESS;
321 }
322
323 /*****************************************************************************
324  * Control:
325  *****************************************************************************/
326 static int Control( access_t *p_access, int i_query, va_list args )
327 {
328     access_sys_t *p_sys = p_access->p_sys;
329     bool   *pb_bool;
330     int          *pi_int;
331     int64_t      *pi_64;
332
333     switch( i_query )
334     {
335         /* */
336         case ACCESS_CAN_SEEK:
337         case ACCESS_CAN_FASTSEEK:
338             pb_bool = (bool*)va_arg( args, bool* );
339             *pb_bool = p_sys->b_seekable;
340             break;
341
342         case ACCESS_CAN_PAUSE:
343         case ACCESS_CAN_CONTROL_PACE:
344             pb_bool = (bool*)va_arg( args, bool* );
345             *pb_bool = p_sys->b_pace_control;
346             break;
347
348         /* */
349         case ACCESS_GET_MTU:
350             pi_int = (int*)va_arg( args, int * );
351             *pi_int = 0;
352             break;
353
354         case ACCESS_GET_PTS_DELAY:
355             pi_64 = (int64_t*)va_arg( args, int64_t * );
356             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
357             break;
358
359         /* */
360         case ACCESS_SET_PAUSE_STATE:
361             /* Nothing to do */
362             break;
363
364         case ACCESS_GET_TITLE_INFO:
365         case ACCESS_SET_TITLE:
366         case ACCESS_SET_SEEKPOINT:
367         case ACCESS_SET_PRIVATE_ID_STATE:
368         case ACCESS_GET_META:
369         case ACCESS_GET_PRIVATE_ID_STATE:
370         case ACCESS_GET_CONTENT_TYPE:
371             return VLC_EGENERIC;
372
373         default:
374             msg_Warn( p_access, "unimplemented query %d in control", i_query );
375             return VLC_EGENERIC;
376
377     }
378     return VLC_SUCCESS;
379 }
380
381 /*****************************************************************************
382  * open_file: Opens a specific file
383  *****************************************************************************/
384 static int open_file (access_t *p_access, const char *path)
385 {
386 #if defined(WIN32)
387     if (!strcasecmp (p_access->psz_access, "file")
388       && ('/' == path[0]) && isalpha (path[1])
389       && (':' == path[2]) && ('/' == path[3]))
390         /* Explorer can open path such as file:/C:/ or file:///C:/
391          * hence remove leading / if found */
392         path++;
393 #endif
394
395 #ifdef UNDER_CE
396     p_sys->fd = utf8_fopen( path, "rb" );
397     if ( !p_sys->fd )
398     {
399         msg_Err( p_access, "cannot open file %s", path );
400         intf_UserFatal( p_access, false, _("File reading failed"),
401                         _("VLC could not open the file \"%s\"."), path );
402         return VLC_EGENERIC;
403     }
404
405     fseek( p_sys->fd, 0, SEEK_END );
406     p_access->info.i_size = ftell( p_sys->fd );
407     p_access->info.i_update |= INPUT_UPDATE_SIZE;
408     fseek( p_sys->fd, 0, SEEK_SET );
409 #else
410     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
411     if (fd == -1)
412     {
413         msg_Err (p_access, "cannot open file %s (%m)", path);
414         intf_UserFatal (p_access, false, _("File reading failed"),
415                         _("VLC could not open the file \"%s\"."), path);
416         return -1;
417     }
418
419 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
420     /* We'd rather use any available memory for reading ahead
421      * than for caching what we've already seen/heard */
422     fcntl (fd, F_RDAHEAD, 1);
423     fcntl (fd, F_NOCACHE, 1);
424 # endif
425 #endif
426
427     return fd;
428 }