]> git.sesse.net Git - vlc/blob - modules/access/file.c
typo
[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 #include <vlc/vlc.h>
30 #include <vlc_input.h>
31 #include <vlc_access.h>
32 #include <vlc_interface.h>
33
34 #include <assert.h>
35 #include <errno.h>
36 #ifdef HAVE_SYS_TYPES_H
37 #   include <sys/types.h>
38 #endif
39 #ifdef HAVE_SYS_STAT_H
40 #   include <sys/stat.h>
41 #endif
42 #ifdef HAVE_FCNTL_H
43 #   include <fcntl.h>
44 #endif
45
46 #if defined( WIN32 ) && !defined( UNDER_CE )
47 #   include <io.h>
48 #else
49 #   include <unistd.h>
50 #   include <poll.h>
51 #endif
52 #ifdef HAVE_MMAP
53 #   include <sys/mman.h>
54 #endif
55
56 #if defined( WIN32 ) && !defined( UNDER_CE )
57 #   ifdef lseek
58 #      undef lseek
59 #   endif
60 #   define lseek _lseeki64
61 #elif defined( UNDER_CE )
62 #   ifdef read
63 #      undef read
64 #   endif
65 #   define read(a,b,c) fread(b,1,c,a)
66 #   define close(a) fclose(a)
67 #   ifdef lseek
68 #      undef lseek
69 #   endif
70 #   define lseek fseek
71 #endif
72
73 #include <vlc_charset.h>
74
75 /*****************************************************************************
76  * Module descriptor
77  *****************************************************************************/
78 static int  Open ( vlc_object_t * );
79 static void Close( vlc_object_t * );
80
81 #define CACHING_TEXT N_("Caching value in ms")
82 #define CACHING_LONGTEXT N_( \
83     "Caching value for files. This " \
84     "value should be set in milliseconds." )
85 #define CAT_TEXT N_("Concatenate with additional files")
86 #define CAT_LONGTEXT N_( \
87     "Play split files as if they were part of a unique file. " \
88     "You need to specify a comma-separated list of files." )
89
90 vlc_module_begin();
91     set_description( _("File input") );
92     set_shortname( _("File") );
93     set_category( CAT_INPUT );
94     set_subcategory( SUBCAT_INPUT_ACCESS );
95     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
96     add_obsolete_string( "file-cat" );
97     set_capability( "access2", 50 );
98     add_shortcut( "file" );
99     add_shortcut( "stream" );
100     add_shortcut( "kfir" );
101     set_callbacks( Open, Close );
102 vlc_module_end();
103
104
105 /*****************************************************************************
106  * Exported prototypes
107  *****************************************************************************/
108 static int  Seek( access_t *, int64_t );
109 static int  Read( access_t *, uint8_t *, int );
110 static int  Control( access_t *, int, va_list );
111 static block_t *mmapBlock( access_t * );
112
113 static int  open_file( access_t *, const char * );
114
115 struct access_sys_t
116 {
117     unsigned int i_nb_reads;
118     vlc_bool_t   b_kfir;
119
120     int fd;
121
122     /* */
123     vlc_bool_t b_seekable;
124     vlc_bool_t b_pace_control;
125 };
126
127 /*****************************************************************************
128  * Open: open the file
129  *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
131 {
132     access_t     *p_access = (access_t*)p_this;
133     access_sys_t *p_sys;
134
135     vlc_bool_t    b_stdin = !strcmp (p_access->psz_path, "-");
136
137     /* Update default_pts to a suitable value for file access */
138     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
139
140     STANDARD_READ_ACCESS_INIT;
141     p_sys->i_nb_reads = 0;
142     p_sys->b_kfir = VLC_FALSE;
143     int fd = p_sys->fd = -1;
144
145     if (!strcasecmp (p_access->psz_access, "stream"))
146     {
147         p_sys->b_seekable = VLC_FALSE;
148         p_sys->b_pace_control = VLC_FALSE;
149     }
150     else if (!strcasecmp (p_access->psz_access, "kfir"))
151     {
152         p_sys->b_seekable = VLC_FALSE;
153         p_sys->b_pace_control = VLC_FALSE;
154         p_sys->b_kfir = VLC_TRUE;
155     }
156     else
157     {
158         p_sys->b_seekable = VLC_TRUE;
159         p_sys->b_pace_control = VLC_TRUE;
160     }
161
162     /* Open file */
163     msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
164
165     if (b_stdin)
166         fd = dup (0);
167     else
168         fd = open_file (p_access, p_access->psz_path);
169
170 #ifdef HAVE_SYS_STAT_H
171     struct stat st;
172
173     while (fd != -1)
174     {
175         if (fstat (fd, &st))
176             msg_Err (p_access, "fstat(%d): %m", fd);
177         else
178         if (S_ISDIR (st.st_mode))
179             /* The directory plugin takes care of that */
180             msg_Dbg (p_access, "file is a directory, aborting");
181         else
182             break; // success
183
184         close (fd);
185         fd = -1;
186     }
187 #endif
188
189     if (fd == -1)
190     {
191         free (p_sys);
192         return VLC_EGENERIC;
193     }
194     p_sys->fd = fd;
195
196 #ifdef HAVE_SYS_STAT_H
197     p_access->info.i_size = st.st_size;
198     if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
199      && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
200         p_sys->b_seekable = VLC_FALSE;
201
202 # ifdef HAVE_MMAP
203     /* Autodetect mmap() support */
204     if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
205     {
206         void *addr = mmap (NULL, 1, PROT_READ, MAP_PRIVATE, fd, 0);
207         if (addr != MAP_FAILED)
208         {
209             /* Does the file system support mmap? */
210             munmap (addr, 1);
211             p_access->pf_read = NULL;
212             p_access->pf_block = mmapBlock;
213             msg_Dbg (p_this, "mmap enabled");
214         }
215         else
216             msg_Dbg (p_this, "mmap disabled (%m)");
217     }
218     else
219         msg_Dbg (p_this, "mmap disabled (non regular file)");
220 # endif
221 #else
222     p_sys->b_seekable = !b_stdin;
223 # warning File size not known!
224 #endif
225
226     if (p_sys->b_seekable && (p_access->info.i_size == 0))
227     {
228         /* FIXME that's bad because all others access will be probed */
229         msg_Err (p_access, "file is empty, aborting");
230         Close (p_this);
231         return VLC_EGENERIC;
232     }
233
234     return VLC_SUCCESS;
235 }
236
237 /*****************************************************************************
238  * Close: close the target
239  *****************************************************************************/
240 static void Close (vlc_object_t * p_this)
241 {
242     access_t     *p_access = (access_t*)p_this;
243     access_sys_t *p_sys = p_access->p_sys;
244
245     close (p_sys->fd);
246     free (p_sys);
247 }
248
249 /*****************************************************************************
250  * Read: standard read on a file descriptor.
251  *****************************************************************************/
252 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
253 {
254     access_sys_t *p_sys = p_access->p_sys;
255     int i_ret;
256     int fd = p_sys->fd;
257
258 #if !defined(WIN32) && !defined(UNDER_CE)
259     if( !p_sys->b_pace_control )
260     {
261         if( !p_sys->b_kfir )
262         {
263             /* Find if some data is available. This won't work under Windows. */
264             do
265             {
266                 struct pollfd ufd;
267
268                 if( p_access->b_die )
269                     return 0;
270
271                 memset (&ufd, 0, sizeof (ufd));
272                 ufd.fd = fd;
273                 ufd.events = POLLIN;
274
275                 i_ret = poll (&ufd, 1, 500);
276             }
277             while (i_ret <= 0);
278
279             i_ret = read (fd, p_buffer, i_len);
280         }
281         else
282         {
283             /* b_kfir ; work around a buggy poll() driver implementation */
284             while (((i_ret = read (fd, p_buffer, i_len)) == 0)
285                 && !p_access->b_die)
286             {
287                 msleep( INPUT_ERROR_SLEEP );
288             }
289         }
290     }
291     else
292 #endif /* WIN32 || UNDER_CE */
293         /* b_pace_control || WIN32 */
294         i_ret = read( fd, p_buffer, i_len );
295
296     if( i_ret < 0 )
297     {
298         switch (errno)
299         {
300             case EINTR:
301             case EAGAIN:
302                 break;
303
304             default:
305                 msg_Err (p_access, "read failed (%m)");
306                 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
307                                 _("VLC could not read the file."));
308         }
309
310         /* Delay a bit to avoid consuming all the CPU. This is particularly
311          * useful when reading from an unconnected FIFO. */
312         msleep( INPUT_ERROR_SLEEP );
313     }
314
315     p_sys->i_nb_reads++;
316
317 #ifdef HAVE_SYS_STAT_H
318     if( p_access->info.i_size != 0 &&
319         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
320     {
321         struct stat st;
322
323         if ((fstat (fd, &st) == 0)
324          && (p_access->info.i_size != st.st_size))
325         {
326             p_access->info.i_size = st.st_size;
327             p_access->info.i_update |= INPUT_UPDATE_SIZE;
328         }
329     }
330 #endif
331
332     if( i_ret > 0 )
333         p_access->info.i_pos += i_ret;
334     else if( i_ret == 0 )
335         p_access->info.b_eof = VLC_TRUE;
336
337     return i_ret;
338 }
339
340 #ifdef HAVE_MMAP
341 # define MMAP_SIZE (1 << 18)
342
343 struct block_sys_t
344 {
345     block_t self;
346     //vlc_object_t *owner;
347     void *base_addr;
348     size_t length;
349 };
350
351 static void mmapRelease (block_t *block)
352 {
353     block_sys_t *p_sys = (block_sys_t *)block;
354
355     munmap (p_sys->base_addr, p_sys->length);
356     //vlc_object_release (p_sys->owner);
357     free (p_sys);
358 }
359
360 static block_t *mmapBlock (access_t *p_access)
361 {
362     access_sys_t *p_sys = p_access->p_sys;
363
364     const int flags = MAP_SHARED;
365     const size_t pagesize = sysconf (_SC_PAGE_SIZE);
366     off_t offset = p_access->info.i_pos & ~(pagesize - 1);
367     size_t align = p_access->info.i_pos & (pagesize - 1);
368     size_t length = (MMAP_SIZE > pagesize) ? MMAP_SIZE : pagesize;
369     void *addr;
370
371     if (p_access->info.i_pos >= p_access->info.i_size)
372     {
373         /* End of file - check that file size hasn't change... */
374         struct stat st;
375
376         if ((fstat (p_sys->fd, &st) == 0)
377          && (st.st_size != p_access->info.i_size))
378         {
379             p_access->info.i_size = st.st_size;
380             p_access->info.i_update |= INPUT_UPDATE_SIZE;
381         }
382
383         /* Really at end of file then */
384         if (p_access->info.i_pos >= p_access->info.i_size)
385         {
386             p_access->info.b_eof = VLC_TRUE;
387             msg_Dbg (p_access, "at end of memory mapped file");
388             return NULL;
389         }
390     }
391
392     if (offset + length > p_access->info.i_size)
393         /* Don't mmap paste end of file */
394         length = p_access->info.i_size - offset;
395
396     assert (offset <= p_access->info.i_pos);               /* and */
397     assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
398     assert (offset < p_access->info.i_size);               /* imply */
399     assert (length > 0);
400
401     addr = mmap (NULL, length, PROT_READ, flags, p_sys->fd, offset);
402     if (addr == MAP_FAILED)
403     {
404         msg_Err (p_access, "memory mapping failed (%m)");
405         intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
406                         _("VLC could not read the file."));
407         msleep( INPUT_ERROR_SLEEP );
408         return NULL;
409     }
410
411     p_access->info.i_pos = offset + length;
412
413     msg_Dbg (p_access, "mapped %lu bytes at %p from offset %lu",
414              (unsigned long)length, addr, (unsigned long)offset);
415     block_sys_t *block = malloc (sizeof (*block));
416     if (block == NULL)
417     {
418         munmap (addr, length);
419         return NULL;
420     }
421
422     block_Init (&block->self, ((uint8_t *)addr) + align, length - align);
423     block->self.pf_release = mmapRelease;
424     block->base_addr = addr;
425     block->length = length;
426     //vlc_object_yield (block->owner = VLC_OBJECT (p_access));
427
428     return &block->self;
429 }
430 #endif
431
432 /*****************************************************************************
433  * Seek: seek to a specific location in a file
434  *****************************************************************************/
435 static int Seek (access_t *p_access, int64_t i_pos)
436 {
437     if (i_pos > p_access->info.i_size)
438     {
439         msg_Err (p_access, "seeking too far");
440         i_pos = p_access->info.i_size;
441     }
442     else if (i_pos < 0)
443     {
444         msg_Err (p_access, "seeking too early");
445         i_pos = 0;
446     }
447
448     p_access->info.i_pos = i_pos;
449     p_access->info.b_eof = VLC_FALSE;
450
451 #ifdef HAVE_MMAP
452     if (p_access->pf_block == NULL)
453         lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
454 #endif
455     return VLC_SUCCESS;
456 }
457
458 /*****************************************************************************
459  * Control:
460  *****************************************************************************/
461 static int Control( access_t *p_access, int i_query, va_list args )
462 {
463     access_sys_t *p_sys = p_access->p_sys;
464     vlc_bool_t   *pb_bool;
465     int          *pi_int;
466     int64_t      *pi_64;
467
468     switch( i_query )
469     {
470         /* */
471         case ACCESS_CAN_SEEK:
472         case ACCESS_CAN_FASTSEEK:
473             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
474             *pb_bool = p_sys->b_seekable;
475             break;
476
477         case ACCESS_CAN_PAUSE:
478         case ACCESS_CAN_CONTROL_PACE:
479             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
480             *pb_bool = p_sys->b_pace_control;
481             break;
482
483         /* */
484         case ACCESS_GET_MTU:
485             pi_int = (int*)va_arg( args, int * );
486             *pi_int = 0;
487             break;
488
489         case ACCESS_GET_PTS_DELAY:
490             pi_64 = (int64_t*)va_arg( args, int64_t * );
491             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
492             break;
493
494         /* */
495         case ACCESS_SET_PAUSE_STATE:
496             /* Nothing to do */
497             break;
498
499         case ACCESS_GET_TITLE_INFO:
500         case ACCESS_SET_TITLE:
501         case ACCESS_SET_SEEKPOINT:
502         case ACCESS_SET_PRIVATE_ID_STATE:
503         case ACCESS_GET_META:
504         case ACCESS_GET_CONTENT_TYPE:
505             return VLC_EGENERIC;
506
507         default:
508             msg_Warn( p_access, "unimplemented query in control" );
509             return VLC_EGENERIC;
510
511     }
512     return VLC_SUCCESS;
513 }
514
515
516 static char *expand_path (const access_t *p_access, const char *path)
517 {
518     if (strncmp (path, "~/", 2) == 0)
519     {
520         char *res;
521
522          // TODO: we should also support the ~cmassiot/ syntax
523          if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
524              return NULL;
525          return res;
526     }
527
528 #if defined(WIN32)
529     if (!strcasecmp (p_access->psz_access, "file")
530       && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
531         // Explorer can open path such as file:/C:/ or file:///C:/
532         // hence remove leading / if found
533         return strdup (path + 1);
534 #endif
535
536     return strdup (path);
537 }
538
539
540 /*****************************************************************************
541  * open_file: Opens a specific file
542  *****************************************************************************/
543 static int open_file (access_t *p_access, const char *psz_name)
544 {
545     char *path = expand_path (p_access, psz_name);
546
547 #ifdef UNDER_CE
548     p_sys->fd = utf8_fopen( path, "rb" );
549     if ( !p_sys->fd )
550     {
551         msg_Err( p_access, "cannot open file %s", psz_name );
552         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
553                         _("VLC could not open the file \"%s\"."), psz_name );
554         free (path);
555         return VLC_EGENERIC;
556     }
557
558     fseek( p_sys->fd, 0, SEEK_END );
559     p_access->info.i_size = ftell( p_sys->fd );
560     p_access->info.i_update |= INPUT_UPDATE_SIZE;
561     fseek( p_sys->fd, 0, SEEK_SET );
562 #else
563     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
564     free (path);
565     if (fd == -1)
566     {
567         msg_Err (p_access, "cannot open file %s (%m)", psz_name);
568         intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
569                         _("VLC could not open the file \"%s\"."), psz_name);
570         return -1;
571     }
572
573 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
574     /* We'd rather use any available memory for reading ahead
575      * than for caching what we've already seen/heard */
576     fcntl (fd, F_RDAHEAD, 1);
577     fcntl (fd, F_NOCACHE, 1);
578 # endif
579 #endif
580
581     return fd;
582 }