file.c 13.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * file.c: file input (file: access plug-in)
 *****************************************************************************
4
 * Copyright (C) 2001-2006 the VideoLAN team
5
 * Copyright © 2006 Rémi Denis-Courmont
gbazin's avatar
gbazin committed
6
 * $Id$
7 8
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9
 *          Rémi Denis-Courmont <rem # videolan # org>
10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
15
 *
16 17 18 19 20 21 22
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
30 31 32
#include <vlc_input.h>
#include <vlc_access.h>
#include <vlc_interface.h>
33

34
#include <string.h>
35
#include <errno.h>
36 37 38 39 40 41 42 43 44 45
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
46 47 48
#if defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
#else
49
#   include <unistd.h>
50
#   include <poll.h>
51 52
#endif

gbazin's avatar
 
gbazin committed
53
#if defined( WIN32 ) && !defined( UNDER_CE )
54 55 56 57 58 59 60 61 62 63 64 65 66 67
#   ifdef lseek
#      undef lseek
#   endif
#   define lseek _lseeki64
#elif defined( UNDER_CE )
#   ifdef read
#      undef read
#   endif
#   define read(a,b,c) fread(b,1,c,a)
#   define close(a) fclose(a)
#   ifdef lseek
#      undef lseek
#   endif
#   define lseek fseek
gbazin's avatar
gbazin committed
68 69
#endif

zorglub's avatar
zorglub committed
70
#include <vlc_charset.h>
71

72 73 74
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
75 76 77
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

Christophe Massiot's avatar
Christophe Massiot committed
78
#define CACHING_TEXT N_("Caching value in ms")
gbazin's avatar
 
gbazin committed
79
#define CACHING_LONGTEXT N_( \
80
    "Caching value for files. This " \
zorglub's avatar
zorglub committed
81
    "value should be set in milliseconds." )
gbazin's avatar
gbazin committed
82 83
#define CAT_TEXT N_("Concatenate with additional files")
#define CAT_LONGTEXT N_( \
zorglub's avatar
zorglub committed
84 85
    "Play split files as if they were part of a unique file. " \
    "You need to specify a comma-separated list of files." )
gbazin's avatar
 
gbazin committed
86

87
vlc_module_begin();
zorglub's avatar
zorglub committed
88
    set_description( _("File input") );
zorglub's avatar
zorglub committed
89
    set_shortname( _("File") );
zorglub's avatar
zorglub committed
90 91
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
92
    add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
93
    add_obsolete_string( "file-cat" );
94
    set_capability( "access2", 50 );
95 96
    add_shortcut( "file" );
    add_shortcut( "stream" );
97
    add_shortcut( "kfir" );
98 99
    set_callbacks( Open, Close );
vlc_module_end();
100 101


102
/*****************************************************************************
103
 * Exported prototypes
104
 *****************************************************************************/
105 106 107 108
static int  Seek( access_t *, int64_t );
static int  Read( access_t *, uint8_t *, int );
static int  Control( access_t *, int, va_list );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
109
static int  open_file( access_t *, const char * );
gbazin's avatar
gbazin committed
110

111
struct access_sys_t
112
{
113 114
    unsigned int i_nb_reads;
    vlc_bool_t   b_kfir;
gbazin's avatar
gbazin committed
115

116
    int fd;
gbazin's avatar
gbazin committed
117

118 119 120 121
    /* */
    vlc_bool_t b_seekable;
    vlc_bool_t b_pace_control;
};
122

123 124 125 126 127
/*****************************************************************************
 * Open: open the file
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
128 129 130
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;

131
    vlc_bool_t    b_stdin = !strcmp (p_access->psz_path, "-");
132

133 134
    /* Update default_pts to a suitable value for file access */
    var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
135

136
    STANDARD_READ_ACCESS_INIT;
137 138
    p_sys->i_nb_reads = 0;
    p_sys->b_kfir = VLC_FALSE;
139
    int fd = p_sys->fd = -1;
140

141
    if (!strcasecmp (p_access->psz_access, "stream"))
142
    {
143 144
        p_sys->b_seekable = VLC_FALSE;
        p_sys->b_pace_control = VLC_FALSE;
145
    }
146
    else if (!strcasecmp (p_access->psz_access, "kfir"))
147
    {
148 149 150
        p_sys->b_seekable = VLC_FALSE;
        p_sys->b_pace_control = VLC_FALSE;
        p_sys->b_kfir = VLC_TRUE;
151
    }
152 153
    else
    {
154
        p_sys->b_seekable = VLC_TRUE;
155
        p_sys->b_pace_control = VLC_TRUE;
156
    }
157

158 159
    /* Open file */
    msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
160

161 162 163 164
    if (b_stdin)
        fd = dup (0);
    else
        fd = open_file (p_access, p_access->psz_path);
165

166
#ifdef HAVE_SYS_STAT_H
167
    struct stat st;
gbazin's avatar
gbazin committed
168

169
    while (fd != -1)
gbazin's avatar
gbazin committed
170
    {
171 172
        if (fstat (fd, &st))
            msg_Err (p_access, "fstat(%d): %s", fd, strerror (errno));
173
        else
174 175 176 177 178
        if (S_ISDIR (st.st_mode))
            /* The directory plugin takes care of that */
            msg_Dbg (p_access, "file is a directory, aborting");
        else
            break; // success
gbazin's avatar
gbazin committed
179

180 181 182
        close (fd);
        fd = -1;
    }
183
#endif
gbazin's avatar
gbazin committed
184

185 186 187 188 189 190
    if (fd == -1)
    {
        free (p_sys);
        return VLC_EGENERIC;
    }
    p_sys->fd = fd;
191 192

#ifdef HAVE_SYS_STAT_H
193 194 195 196
    p_access->info.i_size = st.st_size;
    if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
     && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
        p_sys->b_seekable = VLC_FALSE;
197
#else
198
    p_sys->b_seekable = !b_stdin;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
199
# warning File size not known!
200 201
#endif

202
    if (p_sys->b_seekable && (p_access->info.i_size == 0))
203 204
    {
        /* FIXME that's bad because all others access will be probed */
205
        msg_Err (p_access, "file is empty, aborting");
206 207
        Close (p_this);
        return VLC_EGENERIC;
gbazin's avatar
gbazin committed
208 209
    }

210
    return VLC_SUCCESS;
211 212 213
}

/*****************************************************************************
214
 * Close: close the target
215
 *****************************************************************************/
216
static void Close (vlc_object_t * p_this)
217
{
218 219
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
220

221
    close (p_sys->fd);
222
    free (p_sys);
223 224 225 226 227
}

/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
228
static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
229
{
230 231
    access_sys_t *p_sys = p_access->p_sys;
    int i_ret;
232
    int fd = p_sys->fd;
233

234
#if !defined(WIN32) && !defined(UNDER_CE)
235
    if( !p_sys->b_pace_control )
236
    {
237
        if( !p_sys->b_kfir )
238
        {
239
            /* Find if some data is available. This won't work under Windows. */
240
            do
241
            {
242
                struct pollfd ufd;
243

244
                if( p_access->b_die )
245 246
                    return 0;

247
                memset (&ufd, 0, sizeof (ufd));
248
                ufd.fd = fd;
249 250
                ufd.events = POLLIN;

251
                i_ret = poll (&ufd, 1, 500);
252
            }
253
            while (i_ret <= 0);
254

255
            i_ret = read (fd, p_buffer, i_len);
256 257
        }
        else
258
        {
259
            /* b_kfir ; work around a buggy poll() driver implementation */
260 261
            while (((i_ret = read (fd, p_buffer, i_len)) == 0)
                && !p_access->b_die)
262
            {
263
                msleep( INPUT_ERROR_SLEEP );
264
            }
265 266
        }
    }
267
    else
268
#endif /* WIN32 || UNDER_CE */
269
        /* b_pace_control || WIN32 */
270
        i_ret = read( fd, p_buffer, i_len );
271

272 273
    if( i_ret < 0 )
    {
274
        switch (errno)
275
        {
276 277 278 279 280 281 282 283 284
            case EINTR:
            case EAGAIN:
                break;

            default:
                msg_Err (p_access, "read failed (%s)", strerror (errno));
                intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
                                _("VLC could not read file \"%s\"."),
                                strerror (errno));
285
        }
286 287 288 289

        /* Delay a bit to avoid consuming all the CPU. This is particularly
         * useful when reading from an unconnected FIFO. */
        msleep( INPUT_ERROR_SLEEP );
290
    }
291 292

    p_sys->i_nb_reads++;
293

294
#ifdef HAVE_SYS_STAT_H
295 296
    if( p_access->info.i_size != 0 &&
        (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
297
    {
298
        struct stat st;
gbazin's avatar
gbazin committed
299

300
        if ((fstat (fd, &st) == 0)
301
         && (p_access->info.i_size != st.st_size))
302
        {
303
            p_access->info.i_size = st.st_size;
304
            p_access->info.i_update |= INPUT_UPDATE_SIZE;
305 306 307 308
        }
    }
#endif

309 310 311 312 313
    if( i_ret > 0 )
        p_access->info.i_pos += i_ret;
    else if( i_ret == 0 )
        p_access->info.b_eof = VLC_TRUE;

314 315 316 317 318 319
    return i_ret;
}

/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
320
static int Seek (access_t *p_access, int64_t i_pos)
321
{
322
    if (i_pos > p_access->info.i_size)
gbazin's avatar
gbazin committed
323
    {
324
        msg_Err (p_access, "seeking too far");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
325
        i_pos = p_access->info.i_size;
gbazin's avatar
gbazin committed
326
    }
327
    else if (i_pos < 0)
328
    {
329
        msg_Err (p_access, "seeking too early");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
330
        i_pos = 0;
331
    }
332 333 334 335 336

    p_access->info.i_pos = i_pos;
    p_access->info.b_eof = VLC_FALSE;

    /* Determine which file we need to access */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
337
    lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
338 339 340 341 342 343 344 345 346 347 348 349 350 351
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    access_sys_t *p_sys = p_access->p_sys;
    vlc_bool_t   *pb_bool;
    int          *pi_int;
    int64_t      *pi_64;

    switch( i_query )
352
    {
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = p_sys->b_seekable;
            break;

        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = p_sys->b_pace_control;
            break;

        /* */
        case ACCESS_GET_MTU:
            pi_int = (int*)va_arg( args, int * );
            *pi_int = 0;
            break;

        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
374
            *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
375
            break;
376

377 378 379 380 381 382 383 384
        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
385
        case ACCESS_SET_PRIVATE_ID_STATE:
386
        case ACCESS_GET_META:
387 388 389
            return VLC_EGENERIC;

        default:
390
            msg_Warn( p_access, "unimplemented query in control" );
391 392
            return VLC_EGENERIC;

393
    }
394
    return VLC_SUCCESS;
395 396
}

397

398 399
static char *expand_path (const access_t *p_access, const char *path)
{
Rafaël Carré's avatar
Rafaël Carré committed
400
    if (strncmp (path, "~/", 2) == 0)
401 402 403 404 405 406 407 408 409 410
    {
        char *res;

         // TODO: we should also support the ~cmassiot/ syntax
         if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
             return NULL;
         return res;
    }

#if defined(WIN32)
Rafaël Carré's avatar
Rafaël Carré committed
411
    if (!strcasecmp (p_access->psz_access, "file")
412 413 414 415 416 417 418 419 420 421
      && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
        // Explorer can open path such as file:/C:/ or file:///C:/
        // hence remove leading / if found
        return strdup (path + 1);
#endif

    return strdup (path);
}


gbazin's avatar
gbazin committed
422
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
423
 * open_file: Opens a specific file
gbazin's avatar
gbazin committed
424
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
425
static int open_file (access_t *p_access, const char *psz_name)
gbazin's avatar
gbazin committed
426
{
427
    char *path = expand_path (p_access, psz_name);
gbazin's avatar
gbazin committed
428 429

#ifdef UNDER_CE
430
    p_sys->fd = utf8_fopen( path, "rb" );
431
    if ( !p_sys->fd )
gbazin's avatar
gbazin committed
432
    {
433
        msg_Err( p_access, "cannot open file %s", psz_name );
434 435
        intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
                        _("VLC could not open file \"%s\"."), psz_name );
436
        free (path);
gbazin's avatar
gbazin committed
437 438
        return VLC_EGENERIC;
    }
439 440 441

    fseek( p_sys->fd, 0, SEEK_END );
    p_access->info.i_size = ftell( p_sys->fd );
442
    p_access->info.i_update |= INPUT_UPDATE_SIZE;
443
    fseek( p_sys->fd, 0, SEEK_SET );
gbazin's avatar
gbazin committed
444
#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
445
    int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
446
    free (path);
447
    if (fd == -1)
gbazin's avatar
gbazin committed
448
    {
449 450 451
        msg_Err (p_access, "cannot open file %s (%s)", psz_name,
                 strerror (errno));
        intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"), 
452
                        _("VLC could not open file \"%s\" (%s)."),
453 454
                        psz_name, strerror (errno));
        return -1;
gbazin's avatar
gbazin committed
455
    }
456

457
# if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
458 459
    /* We'd rather use any available memory for reading ahead
     * than for caching what we've already seen/heard */
460 461 462
    fcntl (fd, F_RDAHEAD, 1);
    fcntl (fd, F_NOCACHE, 1);
# endif
gbazin's avatar
gbazin committed
463 464
#endif

465
    return fd;
gbazin's avatar
gbazin committed
466
}