]> git.sesse.net Git - vlc/blob - modules/access/file.c
Reading regular and block files can always be paced
[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_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_dialog.h>
38
39 #include <assert.h>
40 #include <errno.h>
41 #ifdef HAVE_SYS_TYPES_H
42 #   include <sys/types.h>
43 #endif
44 #ifdef HAVE_SYS_STAT_H
45 #   include <sys/stat.h>
46 #endif
47 #ifdef HAVE_FCNTL_H
48 #   include <fcntl.h>
49 #endif
50
51 #if defined( WIN32 )
52 #   include <io.h>
53 #   include <ctype.h>
54 #else
55 #   include <unistd.h>
56 #   include <poll.h>
57 #endif
58
59 #if defined( WIN32 ) && !defined( UNDER_CE )
60 #   ifdef lseek
61 #      undef lseek
62 #   endif
63 #   define lseek _lseeki64
64 #elif defined( UNDER_CE )
65 /* FIXME the commandline on wince is a mess */
66 # define dup(a) -1
67 #endif
68
69 #include <vlc_charset.h>
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 static int  Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
76
77 #define CACHING_TEXT N_("Caching value in ms")
78 #define CACHING_LONGTEXT N_( \
79     "Caching value for files. This " \
80     "value should be set in milliseconds." )
81
82 vlc_module_begin ()
83     set_description( N_("File input") )
84     set_shortname( N_("File") )
85     set_category( CAT_INPUT )
86     set_subcategory( SUBCAT_INPUT_ACCESS )
87     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
88     add_obsolete_string( "file-cat" )
89     set_capability( "access", 50 )
90     add_shortcut( "file" )
91     add_shortcut( "fd" )
92     add_shortcut( "stream" )
93     set_callbacks( Open, Close )
94 vlc_module_end ()
95
96
97 /*****************************************************************************
98  * Exported prototypes
99  *****************************************************************************/
100 static int  Seek( access_t *, int64_t );
101 static int  NoSeek( access_t *, int64_t );
102 static ssize_t Read( access_t *, uint8_t *, size_t );
103 static int  Control( access_t *, int, va_list );
104
105 static int  open_file( access_t *, const char * );
106
107 struct access_sys_t
108 {
109     unsigned int i_nb_reads;
110
111     int fd;
112
113     /* */
114     bool b_pace_control;
115 };
116
117 /*****************************************************************************
118  * Open: open the file
119  *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
121 {
122     access_t     *p_access = (access_t*)p_this;
123     access_sys_t *p_sys;
124
125     /* Update default_pts to a suitable value for file access */
126     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
127
128     STANDARD_READ_ACCESS_INIT;
129     p_sys->i_nb_reads = 0;
130     p_sys->b_pace_control = true;
131
132     /* Open file */
133     int fd = -1;
134
135     if (!strcasecmp (p_access->psz_access, "fd"))
136         fd = dup (atoi (p_access->psz_path));
137     else if (!strcmp (p_access->psz_path, "-"))
138         fd = dup (0);
139     else
140     {
141         msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
142         fd = open_file (p_access, p_access->psz_path);
143     }
144     if (fd == -1)
145         goto error;
146
147 #ifdef HAVE_SYS_STAT_H
148     struct stat st;
149
150     if (fstat (fd, &st))
151     {
152         msg_Err (p_access, "failed to read (%m)");
153         goto error;
154     }
155     /* Directories can be opened and read from, but only readdir() knows
156      * how to parse the data. The directory plugin will do it. */
157     if (S_ISDIR (st.st_mode))
158     {
159         msg_Dbg (p_access, "ignoring directory");
160         goto error;
161     }
162     if (S_ISREG (st.st_mode))
163         p_access->info.i_size = st.st_size;
164     else if (!S_ISBLK (st.st_mode))
165     {
166         p_access->pf_seek = NoSeek;
167         p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
168     }
169 #else
170 # warning File size not known!
171 #endif
172
173     p_sys->fd = fd;
174     return VLC_SUCCESS;
175
176 error:
177     if (fd != -1)
178         close (fd);
179     free (p_sys);
180     return VLC_EGENERIC;
181 }
182
183 /*****************************************************************************
184  * Close: close the target
185  *****************************************************************************/
186 static void Close (vlc_object_t * p_this)
187 {
188     access_t     *p_access = (access_t*)p_this;
189     access_sys_t *p_sys = p_access->p_sys;
190
191     close (p_sys->fd);
192     free (p_sys);
193 }
194
195
196 #include <vlc_network.h>
197
198 /*****************************************************************************
199  * Read: standard read on a file descriptor.
200  *****************************************************************************/
201 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
202 {
203     access_sys_t *p_sys = p_access->p_sys;
204     int fd = p_sys->fd;
205     ssize_t i_ret;
206
207 #ifndef WIN32
208     if (p_access->pf_seek == NoSeek)
209         i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
210     else
211 #endif
212         i_ret = read (fd, p_buffer, i_len);
213
214     if( i_ret < 0 )
215     {
216         switch (errno)
217         {
218             case EINTR:
219             case EAGAIN:
220                 break;
221
222             default:
223                 msg_Err (p_access, "failed to read (%m)");
224                 dialog_Fatal (p_access, _("File reading failed"), "%s",
225                               _("VLC could not read the file."));
226                 p_access->info.b_eof = true;
227                 return 0;
228         }
229     }
230     else if( i_ret > 0 )
231         p_access->info.i_pos += i_ret;
232     else
233         p_access->info.b_eof = true;
234
235     p_sys->i_nb_reads++;
236
237 #ifdef HAVE_SYS_STAT_H
238     if( p_access->info.i_size != 0 &&
239         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
240     {
241         struct stat st;
242
243         if ((fstat (fd, &st) == 0)
244          && (p_access->info.i_size != st.st_size))
245         {
246             p_access->info.i_size = st.st_size;
247             p_access->info.i_update |= INPUT_UPDATE_SIZE;
248         }
249     }
250 #endif
251     return i_ret;
252 }
253
254
255 /*****************************************************************************
256  * Seek: seek to a specific location in a file
257  *****************************************************************************/
258 static int Seek (access_t *p_access, int64_t i_pos)
259 {
260     p_access->info.i_pos = i_pos;
261     p_access->info.b_eof = false;
262
263     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
264     return VLC_SUCCESS;
265 }
266
267 static int NoSeek (access_t *p_access, int64_t i_pos)
268 {
269     /* assert(0); ?? */
270     (void) p_access; (void) i_pos;
271     return VLC_EGENERIC;
272 }
273
274 /*****************************************************************************
275  * Control:
276  *****************************************************************************/
277 static int Control( access_t *p_access, int i_query, va_list args )
278 {
279     access_sys_t *p_sys = p_access->p_sys;
280     bool    *pb_bool;
281     int64_t *pi_64;
282
283     switch( i_query )
284     {
285         /* */
286         case ACCESS_CAN_SEEK:
287         case ACCESS_CAN_FASTSEEK:
288             pb_bool = (bool*)va_arg( args, bool* );
289             *pb_bool = (p_access->pf_seek != NoSeek);
290             break;
291
292         case ACCESS_CAN_PAUSE:
293         case ACCESS_CAN_CONTROL_PACE:
294             pb_bool = (bool*)va_arg( args, bool* );
295             *pb_bool = p_sys->b_pace_control;
296             break;
297
298         /* */
299         case ACCESS_GET_PTS_DELAY:
300             pi_64 = (int64_t*)va_arg( args, int64_t * );
301             *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C(1000);
302             break;
303
304         /* */
305         case ACCESS_SET_PAUSE_STATE:
306             /* Nothing to do */
307             break;
308
309         case ACCESS_GET_TITLE_INFO:
310         case ACCESS_SET_TITLE:
311         case ACCESS_SET_SEEKPOINT:
312         case ACCESS_SET_PRIVATE_ID_STATE:
313         case ACCESS_GET_META:
314         case ACCESS_GET_PRIVATE_ID_STATE:
315         case ACCESS_GET_CONTENT_TYPE:
316             return VLC_EGENERIC;
317
318         default:
319             msg_Warn( p_access, "unimplemented query %d in control", i_query );
320             return VLC_EGENERIC;
321
322     }
323     return VLC_SUCCESS;
324 }
325
326 /*****************************************************************************
327  * open_file: Opens a specific file
328  *****************************************************************************/
329 static int open_file (access_t *p_access, const char *path)
330 {
331 #if defined(WIN32)
332     if (!strcasecmp (p_access->psz_access, "file")
333       && ('/' == path[0]) && isalpha (path[1])
334       && (':' == path[2]) && ('/' == path[3]))
335         /* Explorer can open path such as file:/C:/ or file:///C:/
336          * hence remove leading / if found */
337         path++;
338 #endif
339
340     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
341     if (fd == -1)
342     {
343         msg_Err (p_access, "cannot open file %s (%m)", path);
344         dialog_Fatal (p_access, _("File reading failed"),
345                       _("VLC could not open the file \"%s\"."), path);
346         return -1;
347     }
348
349 #if defined(HAVE_FCNTL)
350     /* We'd rather use any available memory for reading ahead
351      * than for caching what we've already seen/heard */
352 # if defined(F_RDAHEAD)
353     fcntl (fd, F_RDAHEAD, 1);
354 # endif
355 # if defined(F_NOCACHE)
356     fcntl (fd, F_NOCACHE, 1);
357 # endif
358 #endif
359
360     return fd;
361 }