avio.c 11.7 KB
Newer Older
1 2 3 4 5 6 7 8
/*****************************************************************************
 * avio.c: access using libavformat library
 *****************************************************************************
 * Copyright (C) 2009 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29 30 31
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_access.h>
32
#include <vlc_sout.h>
33 34 35
#include <vlc_avcodec.h>

#include "avio.h"
36
#include "../codec/avcodec/avcommon.h"
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51
#if LIBAVFORMAT_VERSION_MAJOR < 54
# define AVIOContext URLContext

# define avio_open url_open
# define avio_close url_close
# define avio_read url_read
# define avio_seek url_seek
# define avio_pause av_url_read_pause

# define AVIO_FLAG_READ URL_RDONLY
# define AVIO_FLAG_WRITE URL_WRONLY
# define avio_size url_filesize
#endif

52 53 54
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
55
#ifndef MERGE_FFMPEG
56 57 58
vlc_module_begin()
    AVIO_MODULE
vlc_module_end()
59
#endif
60 61 62 63 64

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static ssize_t Read   (access_t *, uint8_t *, size_t);
65
static int     Seek   (access_t *, uint64_t);
66
static int     Control(access_t *, int, va_list);
67
static ssize_t Write(sout_access_out_t *, block_t *);
68
static int     OutControl(sout_access_out_t *, int, va_list);
69
static int     OutSeek (sout_access_out_t *, off_t);
70

71 72 73 74
static int UrlInterruptCallback(void *access)
{
    return !vlc_object_alive((vlc_object_t*)access);
}
75 76

struct access_sys_t {
77
    AVIOContext *context;
78 79
};

80
struct sout_access_out_sys_t {
81
    AVIOContext *context;
82 83
};

84

85 86
/* */

87 88
#if LIBAVFORMAT_VERSION_MAJOR < 54
static vlc_object_t *current_access = NULL;
89

90 91 92 93 94 95 96 97 98 99 100 101
static int UrlInterruptCallbackSingle(void)
{
    return UrlInterruptCallback(current_access);
}

static int SetupAvioCb(vlc_object_t *access)
{
    static vlc_mutex_t avio_lock = VLC_STATIC_MUTEX;
    vlc_mutex_lock(&avio_lock);
    assert(!access != !current_access);
    if (access && current_access) {
        vlc_mutex_unlock(&avio_lock);
102 103
        return VLC_EGENERIC;
    }
104
    url_set_interrupt_cb(access ? UrlInterruptCallbackSingle : NULL);
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    current_access = access;

    vlc_mutex_unlock(&avio_lock);

    return VLC_SUCCESS;
}
#endif

/* */

int OpenAvio(vlc_object_t *object)
{
    access_t *access = (access_t*)object;
    access_sys_t *sys = malloc(sizeof(*sys));
    if (!sys)
        return VLC_ENOMEM;
    sys->context = NULL;
123 124 125 126 127 128 129

    /* We accept:
     * - avio://full_url
     * - url (only a subset of available protocols).
     */
    char *url;
    if (!strcmp(access->psz_access, "avio"))
130 131 132
        url = strdup(access->psz_location);
    else if (asprintf(&url, "%s://%s", access->psz_access,
                      access->psz_location) < 0)
133 134
        url = NULL;

135 136 137 138
    if (!url) {
        free(sys);
        return VLC_ENOMEM;
    }
139

140
    /* */
141
    vlc_init_avformat();
142 143 144 145 146 147 148 149 150

    int ret;
#if LIBAVFORMAT_VERSION_MAJOR < 54
    ret = avio_open(&sys->context, url, AVIO_FLAG_READ);
#else
    AVIOInterruptCB cb = {
        .callback = UrlInterruptCallback,
        .opaque = access,
    };
151 152 153 154 155 156 157 158 159 160 161
    AVDictionary *options = NULL;
    char *psz_opts = var_InheritString(access, "avio-options");
    if (psz_opts && *psz_opts) {
        options = vlc_av_get_options(psz_opts);
        free(psz_opts);
    }
    ret = avio_open2(&sys->context, url, AVIO_FLAG_READ, &cb, &options);
    AVDictionaryEntry *t = NULL;
    while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
        msg_Err( access, "unknown option \"%s\"", t->key );
    av_dict_free(&options);
162 163 164 165 166 167 168
#endif
    if (ret < 0) {
        errno = AVUNERROR(ret);
        msg_Err(access, "Failed to open %s: %m", url);
        free(url);
        goto error;
    }
169 170
    free(url);

171 172 173 174 175
#if LIBAVFORMAT_VERSION_MAJOR < 54
    /* We can accept only one active user at any time */
    if (SetupAvioCb(VLC_OBJECT(access))) {
        msg_Err(access, "Module aready in use");
        avio_close(sys->context);
176 177
        goto error;
    }
178 179 180 181 182 183 184 185 186 187
#endif

    int64_t size = avio_size(sys->context);
    bool seekable;
#if LIBAVFORMAT_VERSION_MAJOR < 54
    seekable = !sys->context->is_streamed;
#else
    seekable = sys->context->seekable;
#endif
    msg_Dbg(access, "%sseekable, size=%"PRIi64, seekable ? "" : "not ", size);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    /* */
    access_InitFields(access);
    access->info.i_size = size > 0 ? size : 0;

    access->pf_read = Read;
    access->pf_block = NULL;
    access->pf_control = Control;
    access->pf_seek = Seek;
    access->p_sys = sys;

    return VLC_SUCCESS;

error:
    free(sys);
    return VLC_EGENERIC;
}
Rafaël Carré's avatar
Rafaël Carré committed
205

206 207 208 209
/* */
int OutOpenAvio(vlc_object_t *object)
{
    sout_access_out_t *access = (sout_access_out_t*)object;
210
    sout_access_out_sys_t *sys = malloc(sizeof(*sys));
211 212
    if (!sys)
        return VLC_ENOMEM;
213
    sys->context = NULL;
214 215

    /* */
216
    vlc_init_avformat();
217

218
    if (!access->psz_path)
219 220
        goto error;

221 222 223 224 225 226 227 228
    int ret;
#if LIBAVFORMAT_VERSION_MAJOR < 54
    ret = avio_open(&sys->context, access->psz_path, AVIO_FLAG_WRITE);
#else
    AVIOInterruptCB cb = {
        .callback = UrlInterruptCallback,
        .opaque = access,
    };
229 230 231 232 233 234
    AVDictionary *options = NULL;
    char *psz_opts = var_InheritString(access, "avio-options");
    if (psz_opts && *psz_opts) {
        options = vlc_av_get_options(psz_opts);
        free(psz_opts);
    }
235
    ret = avio_open2(&sys->context, access->psz_path, AVIO_FLAG_WRITE,
236 237 238 239 240
                     &cb, &options);
    AVDictionaryEntry *t = NULL;
    while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
        msg_Err( access, "unknown option \"%s\"", t->key );
    av_dict_free(&options);
241 242 243 244 245 246
#endif
    if (ret < 0) {
        errno = AVUNERROR(ret);
        msg_Err(access, "Failed to open %s", access->psz_path);
        goto error;
    }
247

248 249 250 251
#if LIBAVFORMAT_VERSION_MAJOR < 54
    /* We can accept only one active user at any time */
    if (SetupAvioCb(VLC_OBJECT(access))) {
        msg_Err(access, "Module aready in use");
252 253
        goto error;
    }
254
#endif
255 256 257 258 259 260 261 262 263 264 265 266

    access->pf_write = Write;
    access->pf_control = OutControl;
    access->pf_seek = OutSeek;
    access->p_sys = sys;

    return VLC_SUCCESS;

error:
    free(sys);
    return VLC_EGENERIC;
}
Rafaël Carré's avatar
Rafaël Carré committed
267

268 269 270 271 272
void CloseAvio(vlc_object_t *object)
{
    access_t *access = (access_t*)object;
    access_sys_t *sys = access->p_sys;

273 274 275
#if LIBAVFORMAT_VERSION_MAJOR < 54
    SetupAvioCb(NULL);
#endif
276

277
    avio_close(sys->context);
278 279 280
    free(sys);
}

281 282 283 284 285
void OutCloseAvio(vlc_object_t *object)
{
    sout_access_out_t *access = (sout_access_out_t*)object;
    sout_access_out_sys_t *sys = access->p_sys;

286 287 288
#if LIBAVFORMAT_VERSION_MAJOR < 54
    SetupAvioCb(NULL);
#endif
289

290
    avio_close(sys->context);
291 292
    free(sys);
}
Rafaël Carré's avatar
Rafaël Carré committed
293

294 295
static ssize_t Read(access_t *access, uint8_t *data, size_t size)
{
296 297 298 299 300
    int r = avio_read(access->p_sys->context, data, size);
    if (r > 0)
        access->info.i_pos += r;
    else
        access->info.b_eof = true;
301 302
    return r;
}
Rafaël Carré's avatar
Rafaël Carré committed
303

304 305 306
/*****************************************************************************
 * Write:
 *****************************************************************************/
307
static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
308 309 310 311
{
    access_sys_t *p_sys = (access_sys_t*)p_access->p_sys;
    size_t i_write = 0;

312
    while (p_buffer != NULL) {
313
        block_t *p_next = p_buffer->p_next;
314

315
#if LIBAVFORMAT_VERSION_MAJOR < 54
316 317 318 319 320 321
        int written = url_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
        if (written < 0) {
            errno = AVUNERROR(written);
            goto error;
        }
        i_write += written;
322 323
#else
        avio_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
324 325 326 327 328 329
        avio_flush(p_sys->context);
        if (p_sys->context->error) {
            errno = AVUNERROR(p_sys->context->error);
            p_sys->context->error = 0; /* FIXME? */
            goto error;
        }
330 331 332
        i_write += p_buffer->i_buffer;
#endif

333
        block_Release(p_buffer);
334 335 336 337 338

        p_buffer = p_next;
    }

    return i_write;
339 340 341 342 343

error:
    msg_Err(p_access, "Wrote only %zu bytes (%m)", i_write);
    block_ChainRelease( p_buffer );
    return i_write;
344
}
Rafaël Carré's avatar
Rafaël Carré committed
345

346
static int Seek(access_t *access, uint64_t position)
347 348
{
    access_sys_t *sys = access->p_sys;
349 350
    int ret;

351 352 353 354
#ifndef EOVERFLOW
# define EOVERFLOW EFBIG
#endif

355 356 357 358 359 360 361
    if (position > INT64_MAX)
        ret = AVERROR(EOVERFLOW);
    else
        ret = avio_seek(sys->context, position, SEEK_SET);
    if (ret < 0) {
        errno = AVUNERROR(ret);
        msg_Err(access, "Seek to %"PRIu64" failed: %m", position);
362 363 364 365 366 367 368
        if (access->info.i_size <= 0 || position != access->info.i_size)
            return VLC_EGENERIC;
    }
    access->info.i_pos = position;
    access->info.b_eof = false;
    return VLC_SUCCESS;
}
Rafaël Carré's avatar
Rafaël Carré committed
369

370
static int OutSeek(sout_access_out_t *p_access, off_t i_pos)
371 372 373
{
    sout_access_out_sys_t *sys = p_access->p_sys;

374
    if (avio_seek(sys->context, i_pos, SEEK_SET) < 0)
375 376 377 378
        return VLC_EGENERIC;
    return VLC_SUCCESS;
}

379
static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
380 381 382 383
{
    sout_access_out_sys_t *p_sys = p_access->p_sys;

    VLC_UNUSED(p_sys);
384 385 386 387 388 389 390 391 392 393
    switch (i_query) {
    case ACCESS_OUT_CONTROLS_PACE: {
        bool *pb = va_arg(args, bool *);
        //*pb = strcmp(p_access->psz_access, "stream");
        *pb = false;
        break;
    }

    default:
        return VLC_EGENERIC;
394 395 396
    }
    return VLC_SUCCESS;
}
Rafaël Carré's avatar
Rafaël Carré committed
397

398 399 400
static int Control(access_t *access, int query, va_list args)
{
    access_sys_t *sys = access->p_sys;
401
    bool *b;
402 403 404

    switch (query) {
    case ACCESS_CAN_SEEK:
405 406 407
    case ACCESS_CAN_FASTSEEK: /* FIXME how to do that ? */
        b = va_arg(args, bool *);
#if LIBAVFORMAT_VERSION_MAJOR < 54
408
        *b = !sys->context->is_streamed;
409 410 411
#else
        *b = sys->context->seekable;
#endif
412
        return VLC_SUCCESS;
413 414 415 416 417 418 419
    case ACCESS_CAN_PAUSE:
        b = va_arg(args, bool *);
#if LIBAVFORMAT_VERSION_MAJOR < 54
        *b = sys->context->prot->url_read_pause != NULL;
#else
        *b = sys->context->read_pause != NULL;
#endif
420
        return VLC_SUCCESS;
421 422
    case ACCESS_CAN_CONTROL_PACE:
        b = va_arg(args, bool *);
423 424 425 426 427
        *b = true; /* FIXME */
        return VLC_SUCCESS;
    case ACCESS_GET_PTS_DELAY: {
        int64_t *delay = va_arg(args, int64_t *);
        *delay = DEFAULT_PTS_DELAY; /* FIXME */
428
        return VLC_SUCCESS;
429 430 431
    }
    case ACCESS_SET_PAUSE_STATE: {
        bool is_paused = va_arg(args, int);
432
        if (avio_pause(sys->context, is_paused)< 0)
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
            return VLC_EGENERIC;
        return VLC_SUCCESS;
    }
    case ACCESS_GET_TITLE_INFO:
    case ACCESS_GET_META:
    case ACCESS_GET_CONTENT_TYPE:
    case ACCESS_GET_SIGNAL:
    case ACCESS_SET_TITLE:
    case ACCESS_SET_SEEKPOINT:
    case ACCESS_SET_PRIVATE_ID_STATE:
    case ACCESS_SET_PRIVATE_ID_CA:
    case ACCESS_GET_PRIVATE_ID_STATE:
    default:
        return VLC_EGENERIC;
    }
}