demux: mkv: handle WAVE_FORMAT_MPEG_ADTS_AAC
[vlc.git] / modules / access / file.c
blob3b041fe699c5f313714a6e9e1ab639d8e022f69d
1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
5 * Copyright © 2006-2007 Rémi Denis-Courmont
6 * $Id$
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <assert.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #ifdef HAVE_FSTATVFS
36 # include <sys/statvfs.h>
37 # if defined (HAVE_SYS_MOUNT_H)
38 # include <sys/param.h>
39 # include <sys/mount.h>
40 # endif
41 #endif
42 #ifdef HAVE_LINUX_MAGIC_H
43 # include <sys/vfs.h>
44 # include <linux/magic.h>
45 #endif
47 #if defined( _WIN32 )
48 # include <io.h>
49 # include <ctype.h>
50 # include <shlwapi.h>
51 #else
52 # include <unistd.h>
53 #endif
54 #include <dirent.h>
56 #include <vlc_common.h>
57 #include "fs.h"
58 #include <vlc_input.h>
59 #include <vlc_access.h>
60 #include <vlc_dialog.h>
61 #ifdef _WIN32
62 # include <vlc_charset.h>
63 #endif
64 #include <vlc_fs.h>
65 #include <vlc_url.h>
66 #include <vlc_interrupt.h>
68 struct access_sys_t
70 int fd;
72 bool b_pace_control;
75 #if !defined (_WIN32) && !defined (__OS2__)
76 static bool IsRemote (int fd)
78 #if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
79 struct statvfs stf;
81 if (fstatvfs (fd, &stf))
82 return false;
83 /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
84 return !(stf.f_flag & MNT_LOCAL);
86 #elif defined (HAVE_LINUX_MAGIC_H)
87 struct statfs stf;
89 if (fstatfs (fd, &stf))
90 return false;
92 switch ((unsigned long)stf.f_type)
94 case AFS_SUPER_MAGIC:
95 case CODA_SUPER_MAGIC:
96 case NCP_SUPER_MAGIC:
97 case NFS_SUPER_MAGIC:
98 case SMB_SUPER_MAGIC:
99 case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
100 return true;
102 return false;
104 #else
105 (void)fd;
106 return false;
108 #endif
110 # define IsRemote(fd,path) IsRemote(fd)
112 #else /* _WIN32 || __OS2__ */
113 static bool IsRemote (const char *path)
115 # if !defined(__OS2__) && !VLC_WINSTORE_APP
116 wchar_t *wpath = ToWide (path);
117 bool is_remote = (wpath != NULL && PathIsNetworkPathW (wpath));
118 free (wpath);
119 return is_remote;
120 # else
121 return (! strncmp(path, "\\\\", 2));
122 # endif
124 # define IsRemote(fd,path) IsRemote(path)
125 #endif
127 #ifndef HAVE_POSIX_FADVISE
128 # define posix_fadvise(fd, off, len, adv)
129 #endif
131 static ssize_t Read (access_t *, void *, size_t);
132 static int FileSeek (access_t *, uint64_t);
133 static int NoSeek (access_t *, uint64_t);
134 static int FileControl (access_t *, int, va_list);
136 /*****************************************************************************
137 * FileOpen: open the file
138 *****************************************************************************/
139 int FileOpen( vlc_object_t *p_this )
141 access_t *p_access = (access_t*)p_this;
143 /* Open file */
144 int fd = -1;
146 if (!strcasecmp (p_access->psz_name, "fd"))
148 char *end;
149 int oldfd = strtol (p_access->psz_location, &end, 10);
151 if (*end == '\0')
152 fd = vlc_dup (oldfd);
153 else if (*end == '/' && end > p_access->psz_location)
155 char *name = vlc_uri_decode_duplicate (end - 1);
156 if (name != NULL)
158 name[0] = '.';
159 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
160 free (name);
164 else
166 if (unlikely(p_access->psz_filepath == NULL))
167 return VLC_EGENERIC;
168 fd = vlc_open (p_access->psz_filepath, O_RDONLY | O_NONBLOCK);
171 if (fd == -1)
173 msg_Err (p_access, "cannot open file %s (%s)",
174 p_access->psz_filepath ? p_access->psz_filepath
175 : p_access->psz_location,
176 vlc_strerror_c(errno));
177 vlc_dialog_display_error (p_access, _("File reading failed"),
178 _("VLC could not open the file \"%s\" (%s)."),
179 p_access->psz_filepath ? p_access->psz_filepath
180 : p_access->psz_location,
181 vlc_strerror(errno));
182 return VLC_EGENERIC;
185 struct stat st;
186 if (fstat (fd, &st))
188 msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
189 goto error;
192 #if O_NONBLOCK
193 /* Force blocking mode back */
194 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL) & ~O_NONBLOCK);
195 #endif
197 /* Directories can be opened and read from, but only readdir() knows
198 * how to parse the data. The directory plugin will do it. */
199 if (S_ISDIR (st.st_mode))
201 #ifdef HAVE_FDOPENDIR
202 DIR *p_dir = fdopendir(fd);
203 if (!p_dir) {
204 msg_Err (p_access, "fdopendir error: %s", vlc_strerror_c(errno));
205 goto error;
207 return DirInit (p_access, p_dir);
208 #else
209 msg_Dbg (p_access, "ignoring directory");
210 goto error;
211 #endif
214 access_sys_t *p_sys = vlc_malloc(p_this, sizeof (*p_sys));
215 if (unlikely(p_sys == NULL))
216 goto error;
217 p_access->pf_read = Read;
218 p_access->pf_block = NULL;
219 p_access->pf_control = FileControl;
220 p_access->p_sys = p_sys;
221 p_sys->fd = fd;
223 if (S_ISREG (st.st_mode) || S_ISBLK (st.st_mode))
225 p_access->pf_seek = FileSeek;
226 p_sys->b_pace_control = true;
228 /* Demuxers will need the beginning of the file for probing. */
229 posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
230 /* In most cases, we only read the file once. */
231 posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
232 #ifdef F_NOCACHE
233 fcntl (fd, F_NOCACHE, 0);
234 #endif
235 #ifdef F_RDAHEAD
236 if (IsRemote(fd, p_access->psz_filepath))
237 fcntl (fd, F_RDAHEAD, 0);
238 else
239 fcntl (fd, F_RDAHEAD, 1);
240 #endif
242 else
244 p_access->pf_seek = NoSeek;
245 p_sys->b_pace_control = strcasecmp (p_access->psz_name, "stream");
248 return VLC_SUCCESS;
250 error:
251 vlc_close (fd);
252 return VLC_EGENERIC;
255 /*****************************************************************************
256 * FileClose: close the target
257 *****************************************************************************/
258 void FileClose (vlc_object_t * p_this)
260 access_t *p_access = (access_t*)p_this;
262 if (p_access->pf_read == NULL)
264 DirClose (p_this);
265 return;
268 access_sys_t *p_sys = p_access->p_sys;
270 vlc_close (p_sys->fd);
274 static ssize_t Read (access_t *p_access, void *p_buffer, size_t i_len)
276 access_sys_t *p_sys = p_access->p_sys;
277 int fd = p_sys->fd;
279 ssize_t val = vlc_read_i11e (fd, p_buffer, i_len);
280 if (val < 0)
282 switch (errno)
284 case EINTR:
285 case EAGAIN:
286 return -1;
289 msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
290 vlc_dialog_display_error (p_access, _("File reading failed"),
291 _("VLC could not read the file (%s)."),
292 vlc_strerror(errno));
293 val = 0;
296 return val;
299 /*****************************************************************************
300 * Seek: seek to a specific location in a file
301 *****************************************************************************/
302 static int FileSeek (access_t *p_access, uint64_t i_pos)
304 access_sys_t *sys = p_access->p_sys;
306 if (lseek(sys->fd, i_pos, SEEK_SET) == (off_t)-1)
307 return VLC_EGENERIC;
308 return VLC_SUCCESS;
311 static int NoSeek (access_t *p_access, uint64_t i_pos)
313 /* vlc_assert_unreachable(); ?? */
314 (void) p_access; (void) i_pos;
315 return VLC_EGENERIC;
318 /*****************************************************************************
319 * Control:
320 *****************************************************************************/
321 static int FileControl( access_t *p_access, int i_query, va_list args )
323 access_sys_t *p_sys = p_access->p_sys;
324 bool *pb_bool;
325 int64_t *pi_64;
327 switch( i_query )
329 case STREAM_CAN_SEEK:
330 case STREAM_CAN_FASTSEEK:
331 pb_bool = va_arg( args, bool * );
332 *pb_bool = (p_access->pf_seek != NoSeek);
333 break;
335 case STREAM_CAN_PAUSE:
336 case STREAM_CAN_CONTROL_PACE:
337 pb_bool = va_arg( args, bool * );
338 *pb_bool = p_sys->b_pace_control;
339 break;
341 case STREAM_GET_SIZE:
343 struct stat st;
345 if (fstat (p_sys->fd, &st) || !S_ISREG(st.st_mode))
346 return VLC_EGENERIC;
347 *va_arg( args, uint64_t * ) = st.st_size;
348 break;
351 case STREAM_GET_PTS_DELAY:
352 pi_64 = va_arg( args, int64_t * );
353 if (IsRemote (p_sys->fd, p_access->psz_filepath))
354 *pi_64 = var_InheritInteger (p_access, "network-caching");
355 else
356 *pi_64 = var_InheritInteger (p_access, "file-caching");
357 *pi_64 *= 1000;
358 break;
360 case STREAM_SET_PAUSE_STATE:
361 /* Nothing to do */
362 break;
364 default:
365 return VLC_EGENERIC;
368 return VLC_SUCCESS;