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