demux-run.c 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/**
 * @file demux-run.c
 */
/*****************************************************************************
 * Copyright © 2016 Rémi Denis-Courmont
 *
 * 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.
 *
 * Rémi Denis-Courmont reserves the right to redistribute this file under
 * the terms of the GNU Lesser General Public License as published by the
 * the Free Software Foundation; either version 2.1 or the License, or
 * (at his 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
 * 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 Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/


#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <vlc_common.h>
#include <vlc_access.h>
#include <vlc_block.h>
#include <vlc_demux.h>
41 42
#include <vlc_input.h>
#include <vlc_meta.h>
43 44 45 46 47 48 49
#include <vlc_es_out.h>
#include <vlc_url.h>
#include "../lib/libvlc_internal.h"

#include <vlc/vlc.h>

#include "demux-run.h"
50
#include "decoder.h"
51 52 53 54 55 56 57 58 59 60

struct test_es_out_t
{
    struct es_out_t out;
    struct es_out_id_t *ids;
};

struct es_out_id_t
{
    struct es_out_id_t *next;
61 62
#ifdef HAVE_DECODERS
    decoder_t *decoder;
63
    es_format_t fmt;
64
#endif
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
};

static es_out_id_t *EsOutAdd(es_out_t *out, const es_format_t *fmt)
{
    struct test_es_out_t *ctx = (struct test_es_out_t *) out;

    if (fmt->i_group < 0)
        return NULL;

    es_out_id_t *id = malloc(sizeof (*id));
    if (unlikely(id == NULL))
        return NULL;

    id->next = ctx->ids;
    ctx->ids = id;
80
#ifdef HAVE_DECODERS
81 82
    es_format_Copy(&id->fmt, fmt);
    id->decoder = test_decoder_create((void *)out->p_sys, &id->fmt);
83
#endif
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

    debug("[%p] Added   ES\n", (void *)id);
    return id;
}

static void EsOutCheckId(es_out_t *out, es_out_id_t *id)
{
    struct test_es_out_t *ctx = (struct test_es_out_t *) out;

    for (es_out_id_t *ids = ctx->ids; ids != NULL; ids = ids->next)
        if (ids == id)
            return;

    abort();
}

static int EsOutSend(es_out_t *out, es_out_id_t *id, block_t *block)
{
    //debug("[%p] Sent    ES: %zu\n", (void *)idd, block->i_buffer);
    EsOutCheckId(out, id);
104 105 106 107 108 109
#ifdef HAVE_DECODERS
    if (id->decoder)
        test_decoder_process(id->decoder, block);
    else
#endif
        block_Release(block);
110 111 112
    return VLC_SUCCESS;
}

113 114 115 116 117 118 119 120
static void IdDelete(es_out_id_t *id)
{
#ifdef HAVE_DECODERS
    if (id->decoder)
    {
        /* Drain */
        test_decoder_process(id->decoder, NULL);
        test_decoder_destroy(id->decoder);
121
        es_format_Clean(&id->fmt);
122 123 124 125 126
    }
#endif
    free(id);
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140
static void EsOutDelete(es_out_t *out, es_out_id_t *id)
{
    struct test_es_out_t *ctx = (struct test_es_out_t *) out;
    es_out_id_t **pp = &ctx->ids;

    while (*pp != id)
    {
        if (*pp == NULL)
            abort();
        pp = &((*pp)->next);
    }

    debug("[%p] Deleted ES\n", (void *)id);
    *pp = id->next;
141
    IdDelete(id);
142 143 144 145 146 147 148 149 150
}

static int EsOutControl(es_out_t *out, int query, va_list args)
{
    switch (query)
    {
        case ES_OUT_SET_ES:
            break;
        case ES_OUT_RESTART_ES:
151 152 153 154 155 156 157 158 159
        {
#ifdef HAVE_DECODERS
            es_out_id_t* id = va_arg(args, es_out_id_t*);
            EsOutCheckId(out, id);
            test_decoder_destroy(id->decoder);
            test_decoder_create((void*)out->p_sys, &id->fmt);
#endif
            break;
        }
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        case ES_OUT_SET_ES_DEFAULT:
        case ES_OUT_SET_ES_STATE:
            break;
        case ES_OUT_GET_ES_STATE:
            EsOutCheckId(out, va_arg(args, es_out_id_t *));
            *va_arg(args, bool *) = true;
            break;
        case ES_OUT_SET_ES_CAT_POLICY:
            break;
        case ES_OUT_SET_GROUP:
        case ES_OUT_SET_PCR:
        case ES_OUT_SET_GROUP_PCR:
        case ES_OUT_RESET_PCR:
        case ES_OUT_SET_ES_FMT:
        case ES_OUT_SET_NEXT_DISPLAY_TIME:
        case ES_OUT_SET_GROUP_META:
        case ES_OUT_SET_GROUP_EPG:
        case ES_OUT_DEL_GROUP:
        case ES_OUT_SET_ES_SCRAMBLED_STATE:
            break;
        case ES_OUT_GET_EMPTY:
            *va_arg(args, bool *) = true;
            break;
        case ES_OUT_SET_META:
            break;
        case ES_OUT_GET_PCR_SYSTEM:
        case ES_OUT_MODIFY_PCR_SYSTEM:
            abort();
        default:
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}

static void EsOutDestroy(es_out_t *out)
{
    struct test_es_out_t *ctx = (struct test_es_out_t *)out;
    es_out_id_t *id;

    while ((id = ctx->ids) != NULL)
    {
        ctx->ids = id->next;
202
        IdDelete(id);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    }
    free(ctx);
}

static es_out_t *test_es_out_create(vlc_object_t *parent)
{
    struct test_es_out_t *ctx = malloc(sizeof (*ctx));
    if (ctx == NULL)
    {
        fprintf(stderr, "Error: cannot create ES output.\n");
        return NULL;
    }

    ctx->ids = NULL;

    es_out_t *out = &ctx->out;
    out->pf_add = EsOutAdd;
    out->pf_send = EsOutSend;
    out->pf_del = EsOutDelete;
    out->pf_control = EsOutControl;
    out->pf_destroy = EsOutDestroy;
    out->p_sys = (void *)parent;

    return out;
}

229 230 231
static unsigned demux_test_and_clear_flags(demux_t *demux, unsigned flags)
{
    unsigned update;
232 233 234
    if (demux_Control(demux, DEMUX_TEST_AND_CLEAR_FLAGS, &update))
        return 0;
    return update;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
}

static void demux_get_title_list(demux_t *demux)
{
    int title;
    int title_offset;
    int seekpoint_offset;
    input_title_t **title_list;

    if (demux_Control(demux, DEMUX_GET_TITLE_INFO, &title_list, &title,
                      &title_offset, &seekpoint_offset) == VLC_SUCCESS)
    {
        for (int i = 0; i < title; i++)
            vlc_input_title_Delete(title_list[i]);
    }
}

static void demux_get_meta(demux_t *demux)
{
    vlc_meta_t *p_meta = vlc_meta_New();
    if (unlikely(p_meta == NULL) )
        return;

    input_attachment_t **attachment;
    int i_attachment;

    demux_Control(demux, DEMUX_GET_META, p_meta);
    demux_Control(demux, DEMUX_GET_ATTACHMENTS, &attachment, &i_attachment);

    vlc_meta_Delete(p_meta);
}

267
static int demux_process_stream(const struct vlc_run_args *args, stream_t *s)
268
{
269
    const char *name = args->name;
270 271 272 273 274 275 276 277 278 279
    if (name == NULL)
        name = "any";

    if (s == NULL)
        return -1;

    es_out_t *out = test_es_out_create(VLC_OBJECT(s));
    if (out == NULL)
        return -1;

280
    demux_t *demux = demux_New(VLC_OBJECT(s), name, s, out);
281 282 283 284 285 286 287 288 289 290 291 292
    if (demux == NULL)
    {
        es_out_Delete(out);
        vlc_stream_Delete(s);
        debug("Error: cannot create demultiplexer: %s\n", name);
        return -1;
    }

    uintmax_t i = 0;
    int val;

    while ((val = demux_Demux(demux)) == VLC_DEMUXER_SUCCESS)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    {
        if (args->test_demux_controls)
        {
            if (demux_test_and_clear_flags(demux, INPUT_UPDATE_TITLE_LIST))
                demux_get_title_list(demux);

            if (demux_test_and_clear_flags(demux, INPUT_UPDATE_META))
                demux_get_meta(demux);

            int seekpoint = 0;
            double position = 0.0;
            mtime_t time = 0;
            mtime_t length = 0;

            /* Call controls for increased code coverage */
            demux_Control(demux, DEMUX_GET_SEEKPOINT, &seekpoint);
            demux_Control(demux, DEMUX_GET_POSITION, &position);
            demux_Control(demux, DEMUX_GET_TIME, &time);
            demux_Control(demux, DEMUX_GET_LENGTH, &length);
        }
        i++;
    }
315 316 317 318 319 320 321 322 323

    demux_Delete(demux);
    es_out_Delete(out);

    debug("Completed with %ju iteration(s).\n", i);

    return val == VLC_DEMUXER_EOF ? 0 : -1;
}

324
int vlc_demux_process_url(const struct vlc_run_args *args, const char *url)
325
{
326
    libvlc_instance_t *vlc = libvlc_create(args);
327 328 329 330 331 332 333
    if (vlc == NULL)
        return -1;

    stream_t *s = vlc_access_NewMRL(VLC_OBJECT(vlc->p_libvlc_int), url);
    if (s == NULL)
        fprintf(stderr, "Error: cannot create input stream: %s\n", url);

334
    int ret = demux_process_stream(args, s);
335 336 337 338
    libvlc_release(vlc);
    return ret;
}

339
int vlc_demux_process_path(const struct vlc_run_args *args, const char *path)
340 341 342 343 344 345 346 347
{
    char *url = vlc_path2uri(path, NULL);
    if (url == NULL)
    {
        fprintf(stderr, "Error: cannot convert path to URL: %s\n", path);
        return -1;
    }

348
    int ret = vlc_demux_process_url(args, url);
349 350 351 352
    free(url);
    return ret;
}

353
int vlc_demux_process_memory(const struct vlc_run_args *args,
354 355
                             const unsigned char *buf, size_t length)
{
356
    libvlc_instance_t *vlc = libvlc_create(args);
357 358 359 360 361 362 363 364
    if (vlc == NULL)
        return -1;

    stream_t *s = vlc_stream_MemoryNew(VLC_OBJECT(vlc->p_libvlc_int),
                                       (void *)buf, length, true);
    if (s == NULL)
        fprintf(stderr, "Error: cannot create input stream\n");

365
    int ret = demux_process_stream(args, s);
366 367 368 369 370 371 372 373 374 375
    libvlc_release(vlc);
    return ret;
}

#ifdef HAVE_STATIC_MODULES
# include <vlc_plugin.h>

typedef int (*vlc_plugin_cb)(int (*)(void *, void *, int, ...), void *);
extern vlc_plugin_cb vlc_static_modules[];

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
#ifdef HAVE_DECODERS
#define DECODER_PLUGINS(f) \
    f(adpcm) \
    f(aes3) \
    f(araw) \
    f(g711) \
    f(lpcm) \
    f(uleaddvaudio) \
    f(rawvideo) \
    f(cc) \
    f(cvdsub) \
    f(dvbsub) \
    f(scte18) \
    f(scte27) \
    f(spudec) \
    f(stl) \
    f(subsdec) \
    f(subsusf) \
    f(svcdsub) \
    f(textst) \
    f(substx3g)
#else
#define DECODER_PLUGINS(f)
#endif

401
#define PLUGINS(f) \
402
    f(xml) \
Thomas Guillem's avatar
Thomas Guillem committed
403
    f(console) \
404 405 406 407 408 409 410 411 412 413 414
    f(filesystem) \
    f(xml) \
    f(aiff) \
    f(asf) \
    f(au) \
    f(avi) \
    f(caf) \
    f(es) \
    f(flacsys) \
    f(h26x) \
    f(mjpeg) \
415
    PLUGIN_MKV(f) \
416 417 418 419 420 421 422 423 424 425 426 427 428 429
    f(mp4) \
    f(nsc) \
    f(nsv) \
    f(ps) \
    f(pva) \
    f(sap) \
    f(smf) \
    f(subtitle) \
    PLUGIN_TS(f) \
    f(tta) \
    f(ttml) \
    f(ty) \
    f(voc) \
    f(wav) \
430
    f(webvtt) \
431 432
    f(xa) \
    f(a52) \
433
    f(copy) \
434 435 436 437 438 439 440 441 442
    f(dts) \
    f(flac) \
    f(h264) \
    f(hevc) \
    f(mlp) \
    f(mpeg4audio) \
    f(mpeg4video) \
    f(mpegaudio) \
    f(mpegvideo) \
443
    f(vc1) \
444 445
    f(rawvid) \
    f(rawaud) \
446
    f(ogg) \
447 448
    DECODER_PLUGINS(f)

449 450 451 452 453 454
#ifdef HAVE_DVBPSI
# define PLUGIN_TS(f) f(ts)
#else
# define PLUGIN_TS(f)
#endif

455 456 457 458 459 460
#ifdef HAVE_MATROSKA
# define PLUGIN_MKV(f) f(mkv)
#else
# define PLUGIN_MKV(f)
#endif

461 462 463 464 465 466 467 468 469 470 471
#define DECL_PLUGIN(p) \
    int vlc_entry__##p(int (*)(void *, void *, int, ...), void *);

#define FUNC_PLUGIN(p) \
    vlc_entry__##p,

PLUGINS(DECL_PLUGIN)

__attribute__((visibility("default")))
vlc_plugin_cb vlc_static_modules[] = { PLUGINS(FUNC_PLUGIN) NULL };
#endif /* HAVE_STATIC_MODULES */