es_out.c 79.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * es_out.c: Es Out handler for input.
 *****************************************************************************
4
 * Copyright (C) 2003-2004 the VideoLAN team
5 6 7
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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.
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <stdio.h>
33 34
#include <assert.h>
#include <vlc_common.h>
35

36 37 38
#include <vlc_input.h>
#include <vlc_es_out.h>
#include <vlc_block.h>
39
#include <vlc_aout.h>
40 41

#include "input_internal.h"
42 43 44
#include "clock.h"
#include "decoder.h"
#include "es_out.h"
45
#include "event.h"
46

47 48
#include "../stream_output/stream_output.h"

49
#include <vlc_iso_lang.h>
50
/* FIXME we should find a better way than including that */
51
#include "../text/iso-639_def.h"
52 53 54 55 56 57 58 59 60 61 62 63

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
typedef struct
{
    /* Program ID */
    int i_id;

    /* Number of es for this pgrm */
    int i_es;

64
    bool b_selected;
65 66

    /* Clock for this program */
67
    input_clock_t *p_clock;
68

69
    char    *psz_name;
70
    char    *psz_now_playing;
71
    char    *psz_publisher;
72

73
    vlc_epg_t *p_epg;
74 75 76 77 78 79 80 81 82 83 84 85 86
} es_out_pgrm_t;

struct es_out_id_t
{
    /* ES ID */
    int       i_id;
    es_out_pgrm_t *p_pgrm;

    /* Channel in the track type */
    int         i_channel;
    es_format_t fmt;
    char        *psz_language;
    char        *psz_language_code;
87

88
    decoder_t   *p_dec;
89
    decoder_t   *p_dec_record;
90 91

    /* Fields for Video with CC */
92
    bool  pb_cc_present[4];
93 94 95 96
    es_out_id_t  *pp_cc_es[4];

    /* Field for CC track from a master video */
    es_out_id_t *p_master;
97 98 99

    /* ID for the meta data */
    int         i_meta_id;
100 101 102 103 104 105
};

struct es_out_sys_t
{
    input_thread_t *p_input;

106 107 108
    /* */
    vlc_mutex_t   lock;

109 110 111 112 113 114 115 116 117 118 119 120
    /* all programs */
    int           i_pgrm;
    es_out_pgrm_t **pgrm;
    es_out_pgrm_t **pp_selected_pgrm; /* --programs */
    es_out_pgrm_t *p_pgrm;  /* Master program */

    /* all es */
    int         i_id;
    int         i_es;
    es_out_id_t **es;

    /* mode gestion */
121
    bool  b_active;
122 123 124 125 126 127 128 129
    int         i_mode;

    /* es count */
    int         i_audio;
    int         i_video;
    int         i_sub;

    /* es to select */
130 131
    int         i_audio_last, i_audio_id;
    int         i_sub_last, i_sub_id;
132
    int         i_default_sub_id;   /* As specified in container; if applicable */
133 134 135 136 137 138 139 140 141 142 143
    char        **ppsz_audio_language;
    char        **ppsz_sub_language;

    /* current main es */
    es_out_id_t *p_es_audio;
    es_out_id_t *p_es_video;
    es_out_id_t *p_es_sub;

    /* delay */
    int64_t i_audio_delay;
    int64_t i_spu_delay;
144

145 146 147
    /* Clock configuration */
    mtime_t     i_pts_delay;
    int         i_cr_average;
148
    int         i_rate;
149

150 151
    /* */
    bool        b_paused;
152
    mtime_t     i_pause_date;
153

154
    /* Current preroll */
155
    mtime_t     i_preroll_end;
156

157 158
    /* Used for buffering */
    bool        b_buffering;
159 160 161
    mtime_t     i_buffering_extra_initial;
    mtime_t     i_buffering_extra_stream;
    mtime_t     i_buffering_extra_system;
162

163 164
    /* Record */
    sout_instance_t *p_sout_record;
165 166
};

167
static es_out_id_t *EsOutAdd    ( es_out_t *, const es_format_t * );
168 169 170
static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
static void         EsOutDel    ( es_out_t *, es_out_id_t * );
static int          EsOutControl( es_out_t *, int i_query, va_list );
171
static void         EsOutDelete ( es_out_t * );
172

173
static void         EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
Laurent Aimar's avatar
Laurent Aimar committed
174
static void         EsOutUpdateInfo( es_out_t *, es_out_id_t *es, const es_format_t *, const vlc_meta_t * );
175
static int          EsOutSetRecord(  es_out_t *, bool b_record );
176

177
static bool EsIsSelected( es_out_id_t *es );
178
static void EsSelect( es_out_t *out, es_out_id_t *es );
179
static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
180 181 182
static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
183
static void EsOutProgramsChangeRate( es_out_t *out );
184
static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
185 186 187 188 189
static char *LanguageGetName( const char *psz_code );
static char *LanguageGetCode( const char *psz_lang );
static char **LanguageSplit( const char *psz_langs );
static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );

190 191
static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
    VLC_FOURCC('c', 'c', '1', ' '),
    VLC_FOURCC('c', 'c', '2', ' '),
    VLC_FOURCC('c', 'c', '3', ' '),
    VLC_FOURCC('c', 'c', '4', ' '),
};
static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
{
    int i;
    for( i = 0; i < 4; i++ )
    {
        if( fcc == EsOutFourccClosedCaptions[i] )
            return i;
    }
    return -1;
}


210 211 212
/*****************************************************************************
 * input_EsOutNew:
 *****************************************************************************/
213
es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
214 215 216 217
{
    vlc_value_t  val;
    int i;

218 219 220
    es_out_t     *out = malloc( sizeof( *out ) );
    if( !out )
        return NULL;
221

222
    es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
223 224 225 226 227 228
    if( !p_sys )
    {
        free( out );
        return NULL;
    }

229 230 231 232
    out->pf_add     = EsOutAdd;
    out->pf_send    = EsOutSend;
    out->pf_del     = EsOutDel;
    out->pf_control = EsOutControl;
233
    out->pf_destroy = EsOutDelete;
234
    out->p_sys      = p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
235
    out->b_sout     = p_input->p->p_sout != NULL;
236

237 238

    vlc_mutex_init_recursive( &p_sys->lock );
239 240
    p_sys->p_input = p_input;

241
    p_sys->b_active = false;
242 243 244
    p_sys->i_mode   = ES_OUT_MODE_AUTO;


245
    TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
246 247 248
    p_sys->p_pgrm   = NULL;

    p_sys->i_id    = 0;
249 250

    TAB_INIT( p_sys->i_es, p_sys->es );
251 252 253 254 255 256 257 258 259 260 261 262

    p_sys->i_audio = 0;
    p_sys->i_video = 0;
    p_sys->i_sub   = 0;

    /* */
    var_Get( p_input, "audio-track", &val );
    p_sys->i_audio_last = val.i_int;

    var_Get( p_input, "sub-track", &val );
    p_sys->i_sub_last = val.i_int;

263 264
    p_sys->i_default_sub_id   = -1;

265
    if( !p_input->b_preparsing )
266
    {
267 268 269 270 271 272 273 274
        var_Get( p_input, "audio-language", &val );
        p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
        if( p_sys->ppsz_audio_language )
        {
            for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
                msg_Dbg( p_input, "selected audio language[%d] %s",
                         i, p_sys->ppsz_audio_language[i] );
        }
275
        free( val.psz_string );
276 277 278 279 280 281 282 283 284

        var_Get( p_input, "sub-language", &val );
        p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
        if( p_sys->ppsz_sub_language )
        {
            for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
                msg_Dbg( p_input, "selected subtitle language[%d] %s",
                         i, p_sys->ppsz_sub_language[i] );
        }
285
        free( val.psz_string );
286
    }
287
    else
288
    {
289 290
        p_sys->ppsz_sub_language = NULL;
        p_sys->ppsz_audio_language = NULL;
291 292
    }

293 294 295 296 297 298
    var_Get( p_input, "audio-track-id", &val );
    p_sys->i_audio_id = val.i_int;

    var_Get( p_input, "sub-track-id", &val );
    p_sys->i_sub_id = val.i_int;

299 300 301 302 303 304 305
    p_sys->p_es_audio = NULL;
    p_sys->p_es_video = NULL;
    p_sys->p_es_sub   = NULL;

    p_sys->i_audio_delay= 0;
    p_sys->i_spu_delay  = 0;

306
    p_sys->b_paused = false;
307
    p_sys->i_pause_date = -1;
308

309
    p_sys->i_rate = i_rate;
310 311
    p_sys->i_pts_delay = 0;
    p_sys->i_cr_average = 0;
312

313
    p_sys->b_buffering = true;
314 315 316
    p_sys->i_buffering_extra_initial = 0;
    p_sys->i_buffering_extra_stream = 0;
    p_sys->i_buffering_extra_system = 0;
317
    p_sys->i_preroll_end = -1;
318

319 320
    p_sys->p_sout_record = NULL;

321 322 323 324 325 326
    return out;
}

/*****************************************************************************
 *
 *****************************************************************************/
327 328 329 330 331 332
static void EsOutDelete( es_out_t *out )
{
    es_out_sys_t *p_sys = out->p_sys;
    int i;

    if( p_sys->p_sout_record )
333
        EsOutSetRecord( out, false );
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

    for( i = 0; i < p_sys->i_es; i++ )
    {
        if( p_sys->es[i]->p_dec )
            input_DecoderDelete( p_sys->es[i]->p_dec );

        free( p_sys->es[i]->psz_language );
        free( p_sys->es[i]->psz_language_code );
        es_format_Clean( &p_sys->es[i]->fmt );

        free( p_sys->es[i] );
    }
    if( p_sys->ppsz_audio_language )
    {
        for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
            free( p_sys->ppsz_audio_language[i] );
        free( p_sys->ppsz_audio_language );
    }
    if( p_sys->ppsz_sub_language )
    {
        for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
            free( p_sys->ppsz_sub_language[i] );
        free( p_sys->ppsz_sub_language );
    }
    free( p_sys->es );

    /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
        input_clock_Delete( p_pgrm->p_clock );
        free( p_pgrm->psz_now_playing );
        free( p_pgrm->psz_publisher );
        free( p_pgrm->psz_name );
        if( p_pgrm->p_epg )
            vlc_epg_Delete( p_pgrm->p_epg );

        free( p_pgrm );
    }
    TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
    vlc_mutex_destroy( &p_sys->lock );

    free( p_sys );
    free( out );
}

380 381 382 383 384 385 386 387 388 389 390
static mtime_t EsOutGetWakeup( es_out_t *out )
{
    es_out_sys_t   *p_sys = out->p_sys;
    input_thread_t *p_input = p_sys->p_input;

    if( !p_sys->p_pgrm )
        return 0;

    /* We do not have a wake up date if the input cannot have its speed
     * controlled or sout is imposing its own or while buffering
     *
391 392
     * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
    if( !p_input->p->b_can_pace_control ||
393 394 395 396 397 398 399
        p_input->p->b_out_pace_control ||
        p_sys->b_buffering )
        return 0;

    return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
}

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
{
    int i;
    if( i_id < 0 )
    {
        /* Special HACK, -i_id is the cat of the stream */
        return (es_out_id_t*)((uint8_t*)NULL-i_id);
    }

    for( i = 0; i < out->p_sys->i_es; i++ )
    {
        if( out->p_sys->es[i]->i_id == i_id )
            return out->p_sys->es[i];
    }
    return NULL;
}
416

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
static bool EsOutDecodersIsEmpty( es_out_t *out )
{
    es_out_sys_t      *p_sys = out->p_sys;
    int i;

    if( p_sys->b_buffering && p_sys->p_pgrm )
    {
        EsOutDecodersStopBuffering( out, true );
        if( p_sys->b_buffering )
            return true;
    }

    for( i = 0; i < p_sys->i_es; i++ )
    {
        es_out_id_t *es = p_sys->es[i];

        if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
            return false;
        if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
            return false;
    }
    return true;
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
{
    es_out_sys_t *p_sys = out->p_sys;

    if( i_cat == AUDIO_ES )
        p_sys->i_audio_delay = i_delay;
    else if( i_cat == SPU_ES )
        p_sys->i_spu_delay = i_delay;

    for( int i = 0; i < p_sys->i_es; i++ )
        EsOutDecoderChangeDelay( out, p_sys->es[i] );
}

static int EsOutSetRecord(  es_out_t *out, bool b_record )
{
    es_out_sys_t   *p_sys = out->p_sys;
    input_thread_t *p_input = p_sys->p_input;

    assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );

    if( b_record )
    {
        char *psz_path = var_CreateGetString( p_input, "input-record-path" );
        if( !psz_path || *psz_path == '\0' )
        {
            free( psz_path );
            psz_path = strdup( config_GetHomeDir() );
        }

        char *psz_sout = NULL;  // TODO conf

        if( !psz_sout && psz_path )
        {
            char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
            if( psz_file )
            {
                if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
                    psz_sout = NULL;
                free( psz_file );
            }
        }
        free( psz_path );

        if( !psz_sout )
            return VLC_EGENERIC;

#ifdef ENABLE_SOUT
        p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
#endif
        free( psz_sout );

        if( !p_sys->p_sout_record )
            return VLC_EGENERIC;

        for( int i = 0; i < p_sys->i_es; i++ )
        {
            es_out_id_t *p_es = p_sys->es[i];

            if( !p_es->p_dec || p_es->p_master )
                continue;

            p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
            if( p_es->p_dec_record && p_sys->b_buffering )
                input_DecoderStartBuffering( p_es->p_dec_record );
        }
    }
    else
    {
        for( int i = 0; i < p_sys->i_es; i++ )
        {
            es_out_id_t *p_es = p_sys->es[i];

            if( !p_es->p_dec_record )
                continue;

            input_DecoderDelete( p_es->p_dec_record );
            p_es->p_dec_record = NULL;
        }
#ifdef ENABLE_SOUT
        sout_DeleteInstance( p_sys->p_sout_record );
#endif
        p_sys->p_sout_record = NULL;
    }

    return VLC_SUCCESS;
}
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
{
    es_out_sys_t *p_sys = out->p_sys;

    /* XXX the order is important */
    if( b_paused )
    {
        EsOutDecodersChangePause( out, true, i_date );
        EsOutProgramChangePause( out, true, i_date );
    }
    else
    {
        if( p_sys->i_buffering_extra_initial > 0 )
        {
            mtime_t i_stream_start;
            mtime_t i_system_start;
            mtime_t i_stream_duration;
            mtime_t i_system_duration;
            int i_ret;
            i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
                                          &i_stream_start, &i_system_start,
                                          &i_stream_duration, &i_system_duration );
            if( !i_ret )
            {
                /* FIXME pcr != exactly what wanted */
552
                const mtime_t i_used = /*(i_stream_duration - p_sys->p_input->p->i_pts_delay)*/ p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
553 554 555 556 557 558 559 560 561 562 563 564
                i_date -= i_used;
            }
            p_sys->i_buffering_extra_initial = 0;
            p_sys->i_buffering_extra_stream = 0;
            p_sys->i_buffering_extra_system = 0;
        }
        EsOutProgramChangePause( out, false, i_date );
        EsOutDecodersChangePause( out, false, i_date );

        EsOutProgramsChangeRate( out );
    }
    p_sys->b_paused = b_paused;
565
    p_sys->i_pause_date = i_date;
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
}

static void EsOutChangeRate( es_out_t *out, int i_rate )
{
    es_out_sys_t      *p_sys = out->p_sys;

    p_sys->i_rate = i_rate;

    if( !p_sys->b_paused )
        EsOutProgramsChangeRate( out );
}

static void EsOutChangePosition( es_out_t *out )
{
    es_out_sys_t      *p_sys = out->p_sys;

582 583
    input_SendEventCache( p_sys->p_input, 0.0 );

584 585 586 587 588 589 590 591 592 593
    for( int i = 0; i < p_sys->i_es; i++ )
    {
        es_out_id_t *p_es = p_sys->es[i];

        if( !p_es->p_dec )
            continue;

        input_DecoderStartBuffering( p_es->p_dec );

        if( p_es->p_dec_record )
Laurent Aimar's avatar
Laurent Aimar committed
594
            input_DecoderStartBuffering( p_es->p_dec_record );
595 596 597 598 599 600 601 602 603 604 605 606
    }

    for( int i = 0; i < p_sys->i_pgrm; i++ )
        input_clock_Reset( p_sys->pgrm[i]->p_clock );

    p_sys->b_buffering = true;
    p_sys->i_buffering_extra_initial = 0;
    p_sys->i_buffering_extra_stream = 0;
    p_sys->i_buffering_extra_system = 0;
    p_sys->i_preroll_end = -1;
}

607

608

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
{
    es_out_sys_t *p_sys = out->p_sys;
    int i_ret;

    mtime_t i_stream_start;
    mtime_t i_system_start;
    mtime_t i_stream_duration;
    mtime_t i_system_duration;
    i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
                                  &i_stream_start, &i_system_start,
                                  &i_stream_duration, &i_system_duration );
    assert( !i_ret || b_forced );
    if( i_ret )
        return;

625
    mtime_t i_preroll_duration = 0;
626 627
    if( p_sys->i_preroll_end >= 0 )
        i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
628

629
    const mtime_t i_buffering_duration = p_sys->i_pts_delay +
630
                                         i_preroll_duration +
631
                                         p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
632 633

    if( i_stream_duration <= i_buffering_duration && !b_forced )
634
    {
635 636 637 638
        const double f_level = (double)i_stream_duration / i_buffering_duration;
        input_SendEventCache( p_sys->p_input, f_level );

        msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
639 640
        return;
    }
641
    input_SendEventCache( p_sys->p_input, 1.0 );
642 643 644 645

    msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
              (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
    p_sys->b_buffering = false;
646
    p_sys->i_preroll_end = -1;
647

648 649 650 651 652 653
    if( p_sys->i_buffering_extra_initial > 0 )
    {
        /* FIXME wrong ? */
        return;
    }

654 655 656 657 658
    const mtime_t i_decoder_buffering_start = mdate();
    for( int i = 0; i < p_sys->i_es; i++ )
    {
        es_out_id_t *p_es = p_sys->es[i];

659
        if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
660 661 662 663 664 665 666 667 668
            continue;
        input_DecoderWaitBuffering( p_es->p_dec );
        if( p_es->p_dec_record )
            input_DecoderWaitBuffering( p_es->p_dec_record );
    }

    msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
              (int)(mdate() - i_decoder_buffering_start)/1000 );

669 670 671 672
    /* Here is a good place to destroy unused vout with every demuxer */
    input_ressource_TerminateVout( p_sys->p_input->p->p_ressource );

    /* */
673 674 675 676
    const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
    const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();

    input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_current_date + i_wakeup_delay - i_buffering_duration );
677 678 679 680 681 682 683 684 685 686

    for( int i = 0; i < p_sys->i_es; i++ )
    {
        es_out_id_t *p_es = p_sys->es[i];

        if( !p_es->p_dec )
            continue;

        input_DecoderStopBuffering( p_es->p_dec );
        if( p_es->p_dec_record )
Laurent Aimar's avatar
Laurent Aimar committed
687
            input_DecoderStopBuffering( p_es->p_dec_record );
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    }
}
static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
{
    es_out_sys_t *p_sys = out->p_sys;

    /* Pause decoders first */
    for( int i = 0; i < p_sys->i_es; i++ )
    {
        es_out_id_t *es = p_sys->es[i];

        if( es->p_dec )
        {
            input_DecoderChangePause( es->p_dec, b_paused, i_date );
            if( es->p_dec_record )
                input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
        }
    }
}
static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
{
    es_out_sys_t *p_sys = out->p_sys;

    for( int i = 0; i < p_sys->i_pgrm; i++ )
        input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
}

static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
{
    es_out_sys_t *p_sys = out->p_sys;

    mtime_t i_delay = 0;
    if( p_es->fmt.i_cat == AUDIO_ES )
        i_delay = p_sys->i_audio_delay;
    else if( p_es->fmt.i_cat == SPU_ES )
        i_delay = p_sys->i_spu_delay;

    if( i_delay != 0 )
    {
        if( p_es->p_dec )
            input_DecoderChangeDelay( p_es->p_dec, i_delay );
        if( p_es->p_dec_record )
730
            input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
731 732
    }
}
733 734 735
static void EsOutProgramsChangeRate( es_out_t *out )
{
    es_out_sys_t      *p_sys = out->p_sys;
736

737 738 739
    for( int i = 0; i < p_sys->i_pgrm; i++ )
        input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
}
740

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
static void EsOutFrameNext( es_out_t *out )
{
    es_out_sys_t *p_sys = out->p_sys;
    es_out_id_t *p_es_video = NULL;

    if( p_sys->b_buffering )
    {
        msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
        return;
    }

    assert( p_sys->b_paused );

    for( int i = 0; i < p_sys->i_es; i++ )
    {
        es_out_id_t *p_es = p_sys->es[i];

        if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
        {
            p_es_video = p_es;
            break;
        }
    }

    if( !p_es_video )
    {
        msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
        return;
    }

    mtime_t i_duration;
    input_DecoderFrameNext( p_es_video->p_dec, &i_duration );

    msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );

    if( i_duration <= 0 )
        i_duration = 40*1000;

    /* FIXME it is not a clean way ? */
    if( p_sys->i_buffering_extra_initial <= 0 )
    {
        mtime_t i_stream_start;
        mtime_t i_system_start;
        mtime_t i_stream_duration;
        mtime_t i_system_duration;
        int i_ret;

        i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
                                      &i_stream_start, &i_system_start,
                                      &i_stream_duration, &i_system_duration );
        if( i_ret )
            return;

794
        p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
795 796 797 798 799 800 801 802 803 804 805 806 807 808
        p_sys->i_buffering_extra_system =
        p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
    }

    const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );

    p_sys->b_buffering = true;
    p_sys->i_buffering_extra_system += i_duration;
    p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
                                      ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
                                                INPUT_RATE_DEFAULT / i_rate;

    p_sys->i_preroll_end = -1;
}
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
static mtime_t EsOutGetBuffering( es_out_t *out )
{
    es_out_sys_t *p_sys = out->p_sys;

    if( !p_sys->p_pgrm )
        return 0;

    int i_ret;
    mtime_t i_stream_start;
    mtime_t i_system_start;
    mtime_t i_stream_duration;
    mtime_t i_system_duration;
    i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
                                  &i_stream_start, &i_system_start,
                                  &i_stream_duration, &i_system_duration );

    if( i_ret )
        return 0;
827

828
    mtime_t i_delay;
829

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
    if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
    {
        i_delay = i_stream_duration;
    }
    else
    {
        mtime_t i_system_duration;
        if( p_sys->b_paused )
        {
            i_system_duration = p_sys->i_pause_date  - i_system_start;
            if( p_sys->i_buffering_extra_initial > 0 )
                i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
        }
        else
        {
            i_system_duration = mdate() - i_system_start;
        }

        const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
849
        i_delay = p_sys->i_pts_delay - i_consumed;
850 851 852 853 854
    }
    if( i_delay < 0 )
        return 0;
    return i_delay;
}
855

856
static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
857
                                     bool b_delete )
858 859 860
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
861
    const  bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
862 863 864 865
    vlc_value_t       val, text;

    if( b_delete )
    {
866
        /* TODO it should probably be a list */
867
        if( b_teletext )
868
            input_SendEventTeletext( p_sys->p_input, -1 );
869

870
        input_SendEventEsDel( p_input, SPU_ES, i_id );
871 872 873 874
        return;
    }

    /* Get the number of ES already added */
875 876 877 878 879 880 881 882
    const char *psz_var;
    if( fmt->i_cat == AUDIO_ES )
        psz_var = "audio-es";
    else if( fmt->i_cat == VIDEO_ES )
        psz_var = "video-es";
    else
        psz_var = "spu-es";

883 884 885 886 887 888 889 890 891 892 893 894
    var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
    if( val.i_int == 0 )
    {
        vlc_value_t val2;

        /* First one, we need to add the "Disable" choice */
        val2.i_int = -1; text.psz_string = _("Disable");
        var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
        val.i_int++;
    }

    /* Take care of the ES description */
895
    if( fmt->psz_description && *fmt->psz_description )
896
    {
897
        if( psz_language && *psz_language )
898
        {
899 900 901 902
            text.psz_string = malloc( strlen( fmt->psz_description) +
                                      strlen( psz_language ) + 10 );
            sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
                                                   psz_language );
903
        }
904
        else text.psz_string = strdup( fmt->psz_description );
905 906 907
    }
    else
    {
908
        if( psz_language && *psz_language )
909
        {
910 911
            if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
                text.psz_string = NULL;
912 913 914
        }
        else
        {
915 916
            if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
                text.psz_string = NULL;
917 918 919
        }
    }

920
    input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
921 922 923

    free( text.psz_string );

924
    if( b_teletext )
925
    {
926
        /* TODO it should probably be a list */
927
        if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
928
            input_SendEventTeletext( p_sys->p_input, i_id );
929
    }
930 931
}

932
static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
933
                              bool b_delete )
934 935 936 937
{
    EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
}

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
/* EsOutProgramSelect:
 *  Select a program and update the object variable
 */
static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    int               i;

    if( p_sys->p_pgrm == p_pgrm )
        return; /* Nothing to do */

    if( p_sys->p_pgrm )
    {
        es_out_pgrm_t *old = p_sys->p_pgrm;
953
        msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
954 955 956

        for( i = 0; i < p_sys->i_es; i++ )
        {
957
            if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
958
                p_sys->i_mode != ES_OUT_MODE_ALL )
959
                EsUnselect( out, p_sys->es[i], true );
960 961 962 963 964 965 966
        }

        p_sys->p_es_audio = NULL;
        p_sys->p_es_sub = NULL;
        p_sys->p_es_video = NULL;
    }

967
    msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
968 969

    /* Mark it selected */
970
    p_pgrm->b_selected = true;
971 972 973 974 975

    /* Switch master stream */
    p_sys->p_pgrm = p_pgrm;

    /* Update "program" */
976
    input_SendEventProgramSelect( p_input, p_pgrm->i_id );
977 978

    /* Update "es-*" */
979 980 981 982 983
    input_SendEventEsDel( p_input, AUDIO_ES, -1 );
    input_SendEventEsDel( p_input, VIDEO_ES, -1 );
    input_SendEventEsDel( p_input, SPU_ES, -1 );

    /* TODO event */
984
    var_SetInteger( p_input, "teletext-es", -1 );
985

986 987 988
    for( i = 0; i < p_sys->i_es; i++ )
    {
        if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
989 990
            EsOutESVarUpdate( out, p_sys->es[i], false );
        EsOutSelect( out, p_sys->es[i], false );
991 992
    }

993
    /* Update now playing */
994 995
    input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
    input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
996

997
    input_SendEventMeta( p_input );
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
}

/* EsOutAddProgram:
 *  Add a program
 */
static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;

    es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1009 1010
    if( !p_pgrm )
        return NULL;
1011 1012 1013 1014

    /* Init */
    p_pgrm->i_id = i_group;
    p_pgrm->i_es = 0;
1015
    p_pgrm->b_selected = false;
1016
    p_pgrm->psz_name = NULL;
1017
    p_pgrm->psz_now_playing = NULL;
1018 1019
    p_pgrm->psz_publisher = NULL;
    p_pgrm->p_epg = NULL;
1020
    p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1021 1022 1023 1024 1025
    if( !p_pgrm->p_clock )
    {
        free( p_pgrm );
        return NULL;
    }
1026 1027
    input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );

1028 1029 1030 1031 1032

    /* Append it */
    TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );

    /* Update "program" variable */
1033
    input_SendEventProgramAdd( p_input, i_group, NULL );
1034 1035 1036

    if( i_group == var_GetInteger( p_input, "program" ) )
        EsOutProgramSelect( out, p_pgrm );
1037

1038 1039 1040
    return p_pgrm;
}

1041 1042 1043
/* EsOutDelProgram:
 *  Delete a program
 */
1044
static int EsOutProgramDel( es_out_t *out, int i_group )
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    es_out_pgrm_t     *p_pgrm = NULL;
    int               i;

    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        if( p_sys->pgrm[i]->i_id == i_group )
        {
            p_pgrm = p_sys->pgrm[i];
            break;
        }
    }

1060 1061
    if( p_pgrm == NULL )
        return VLC_EGENERIC;
1062 1063 1064 1065 1066

    if( p_pgrm->i_es )
    {
        msg_Dbg( p_input, "can't delete program %d which still has %i ES",
                 i_group, p_pgrm->i_es );
1067
        return VLC_EGENERIC;
1068 1069 1070 1071 1072
    }

    TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );

    /* If program is selected we need to unselect it */
1073 1074 1075
    if( p_sys->p_pgrm == p_pgrm )
        p_sys->p_pgrm = NULL;

Laurent Aimar's avatar
Laurent Aimar committed
1076
    input_clock_Delete( p_pgrm->p_clock );
1077

1078 1079 1080
    free( p_pgrm->psz_name );
    free( p_pgrm->psz_now_playing );
    free( p_pgrm->psz_publisher );
1081 1082
    if( p_pgrm->p_epg )
        vlc_epg_Delete( p_pgrm->p_epg );
1083 1084 1085
    free( p_pgrm );

    /* Update "program" variable */
1086
    input_SendEventProgramDel( p_input, i_group );
1087 1088

    return VLC_SUCCESS;
1089 1090
}

1091 1092
/* EsOutProgramMeta:
 */
1093 1094 1095 1096
static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
{
    char *psz = NULL;
    if( p_pgrm->psz_name )
1097 1098
    {
        if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
Rémi Duraffort's avatar
Rémi Duraffort committed
1099
            return NULL;
1100
    }
1101
    else
1102 1103
    {
        if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
Rémi Duraffort's avatar
Rémi Duraffort committed
1104
            return NULL;
1105
    }
1106 1107 1108
    return psz;
}

1109 1110 1111
static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
{
    es_out_sys_t      *p_sys = out->p_sys;
1112
    es_out_pgrm_t     *p_pgrm = NULL;
1113
    input_thread_t    *p_input = p_sys->p_input;
1114
    char              *psz_cat;
1115 1116
    const char        *psz_title = NULL;
    const char        *psz_provider = NULL;
1117 1118 1119
    int i;

    msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1120

1121
    /* Check against empty meta data (empty for what we handle) */
1122 1123 1124 1125
    if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
        !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
        !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
        vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1126 1127 1128
    {
        return;
    }
1129
    /* Find program */
1130 1131 1132 1133 1134 1135 1136 1137
    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        if( p_sys->pgrm[i]->i_id == i_group )
        {
            p_pgrm = p_sys->pgrm[i];
            break;
        }
    }
1138 1139
    if( p_pgrm == NULL )
        p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
1140

1141
    /* */
1142 1143
    psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
    psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1144 1145

    /* Update the description text of the program */
Clément Stenac's avatar
Clément Stenac committed
1146
    if( psz_title && *psz_title )
1147
    {
1148 1149 1150 1151 1152 1153 1154 1155 1156
        if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
        {
            char *psz_cat = EsOutProgramGetMetaName( p_pgrm );

            /* Remove old entries */
            input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
            /* TODO update epg name */
            free( psz_cat );
        }
1157
        free( p_pgrm->psz_name );
1158 1159
        p_pgrm->psz_name = strdup( psz_title );

1160
        char *psz_text;
1161 1162
        if( psz_provider && *psz_provider )
        {
1163 1164
            if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
                psz_text = NULL;
1165 1166 1167
        }
        else
        {
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
            psz_text = strdup( psz_title );
        }

        /* ugly but it works */
        if( psz_text )
        {
            input_SendEventProgramDel( p_input, i_group );
            input_SendEventProgramAdd( p_input, i_group, psz_text );

            free( psz_text );
1178 1179 1180
        }
    }

1181 1182 1183
    psz_cat = EsOutProgramGetMetaName( p_pgrm );
    if( psz_provider )
    {
1184
        if( p_sys->p_pgrm == p_pgrm )
1185
        {
1186
            input_item_SetPublisher( p_input->p->p_item, psz_provider );
1187 1188
            input_SendEventMeta( p_input );
        }
1189
        input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1190
    }
1191 1192 1193 1194 1195 1196 1197 1198
    char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
    for( i = 0; ppsz_all_keys[i]; i++ )
    {
        input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
                       vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
        free( ppsz_all_keys[i] );
    }
    free( ppsz_all_keys );
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

    free( psz_cat );
}

static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
{
    int i;

    /* Add new event */
    for( i = 0; i < p_src->i_event; i++ )
    {
        vlc_epg_event_t *p_evt = p_src->pp_event[i];
1211
        bool b_add = true;
1212 1213 1214
        int j;

        for( j = 0; j < p_dst->i_event; j++ )
1215
        {
1216 1217
            if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
            {
1218
                b_add = false;
1219 1220 1221 1222
                break;
            }
            if( p_dst->pp_event[j]->i_start > p_evt->i_start )
                break;
1223
        }
1224 1225
        if( b_add )
        {
1226
            vlc_epg_event_t *p_copy = calloc( 1, sizeof(vlc_epg_event_t) );
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
            if( !p_copy )
                break;
            p_copy->i_start = p_evt->i_start;
            p_copy->i_duration = p_evt->i_duration;
            p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
            p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
            p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
            TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
        }
    }
    /* Update current */
    vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );

    /* Keep only 1 old event  */
    if( p_dst->p_current )
    {
        while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
            TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
    }
}

static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    es_out_pgrm_t     *p_pgrm = NULL;
    char *psz_cat;
    int i;

    /* Find program */
    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        if( p_sys->pgrm[i]->i_id == i_group )
        {
            p_pgrm = p_sys->pgrm[i];
            break;
        }
    }
    if( p_pgrm == NULL )
        p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */

    /* Merge EPG */
    if( !p_pgrm->p_epg )
        p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
    vlc_epg_Merge( p_pgrm->p_epg, p_epg );

    /* Update info */
    psz_cat = EsOutProgramGetMetaName( p_pgrm );
#ifdef HAVE_LOCALTIME_R
1276 1277 1278
    char *psz_epg;
    if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
        psz_epg = NULL;
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
    msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
    for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
    {
        const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
        time_t t_start = (time_t)p_evt->i_start;
        struct tm tm_start;
        char psz_start[128];

        localtime_r( &t_start, &tm_start );

        snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
        if( p_evt->psz_short_description || p_evt->psz_description )
            input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
                           p_evt->psz_name,
                           p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
                           p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
        else
            input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
                           p_evt->psz_name,
                           p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
    }
    free( psz_epg );
#endif
    /* Update now playing */
1304
    free( p_pgrm->psz_now_playing );
1305 1306 1307
    p_pgrm->psz_now_playing = NULL;
    if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
        p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
1308 1309

    if( p_pgrm == p_sys->p_pgrm )
1310
    {
1311
        input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1312 1313
        input_SendEventMeta( p_input );
    }
1314 1315

    if( p_pgrm->psz_now_playing )
1316 1317 1318 1319 1320
    {
        input_Control( p_input, INPUT_ADD_INFO, psz_cat,
            input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
            p_pgrm->psz_now_playing );
    }
1321
    else
1322 1323 1324 1325
    {
        input_Control( p_input, INPUT_DEL_INFO, psz_cat,
            input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
    }
1326

1327 1328 1329
    free( psz_cat );
}

1330 1331 1332
/* EsOutAdd:
 *  Add an es_out
 */
1333
static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;

    if( fmt->i_group < 0 )
    {
        msg_Err( p_input, "invalid group number" );
        return NULL;
    }

1344 1345 1346 1347 1348 1349 1350 1351 1352
    es_out_id_t       *es = malloc( sizeof( *es ) );
    es_out_pgrm_t     *p_pgrm = NULL;
    int i;

    if( !es )
        return NULL;

    vlc_mutex_lock( &p_sys->lock );

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
    /* Search the program */
    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        if( fmt->i_group == p_sys->pgrm[i]->i_id )
        {
            p_pgrm = p_sys->pgrm[i];
            break;
        }
    }
    if( p_pgrm == NULL )
    {
        /* Create a new one */
        p_pgrm = EsOutProgramAdd( out, fmt->i_group );
    }

    /* Increase ref count for program */
    p_pgrm->i_es++;

    /* Set up ES */
    es->p_pgrm = p_pgrm;
    es_format_Copy( &es->fmt, fmt );
1374 1375
    if( es->fmt.i_id < 0 )
        es->fmt.i_id = out->p_sys->i_id;
1376
    es->i_id = es->fmt.i_id;
1377
    es->i_meta_id = out->p_sys->i_id;
1378

1379
    switch( es->fmt.i_cat )
1380 1381
    {
    case AUDIO_ES:
1382 1383 1384
    {
        audio_replay_gain_t rg;

1385
        es->i_channel = p_sys->i_audio;
1386 1387

        memset( &rg, 0, sizeof(rg) );
1388 1389 1390
        vlc_mutex_lock( &p_input->p->p_item->lock );
        vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
        vlc_mutex_unlock( &p_input->p->p_item->lock );
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404

        for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
        {
            if( !es->fmt.audio_replay_gain.pb_peak[i] )
            {
                es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
                es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
            }
            if( !es->fmt.audio_replay_gain.pb_gain[i] )
            {
                es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
                es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
            }
        }
1405
        break;
1406
    }
1407 1408 1409

    case VIDEO_ES:
        es->i_channel = p_sys->i_video;
1410
        if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1411 1412
            vlc_ureduce( &es->fmt.video.i_frame_rate,
                         &es->fmt.video.i_frame_rate_base,
1413 1414
                         es->fmt.video.i_frame_rate,
                         es->fmt.video.i_frame_rate_base, 0 );
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
        break;

    case SPU_ES:
        es->i_channel = p_sys->i_sub;
        break;

    default:
        es->i_channel = 0;
        break;
    }
1425 1426
    es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
    es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1427
    es->p_dec = NULL;
1428
    es->p_dec_record = NULL;
1429
    for( i = 0; i < 4; i++ )
1430
        es->pb_cc_present[i] = false;
1431
    es->p_master = NULL;
1432 1433

    if( es->p_pgrm == p_sys->p_pgrm )
1434
        EsOutESVarUpdate( out, es, false );
1435 1436

    /* Select it if needed */
1437
    EsOutSelect( out, es, false );
1438 1439 1440 1441


    TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
    p_sys->i_id++;  /* always incremented */
1442
    switch( es->fmt.i_cat )
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
    {
        case AUDIO_ES:
            p_sys->i_audio++;
            break;
        case SPU_ES:
            p_sys->i_sub++;
            break;
        case VIDEO_ES:
            p_sys->i_video++;
            break;
    }

Laurent Aimar's avatar
Laurent Aimar committed
1455
    EsOutUpdateInfo( out, es, &es->fmt, NULL );
1456

1457 1458
    vlc_mutex_unlock( &p_sys->lock );

1459 1460 1461
    return es;
}

1462
static bool EsIsSelected( es_out_id_t *es )
1463 1464 1465
{
    if( es->p_master )
    {
1466
        bool b_decode = false;
1467
        if( es->p_master->p_dec )
1468 1469 1470 1471 1472
        {
            int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
            if( i_channel != -1 )
                input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
        }
1473 1474 1475 1476 1477 1478 1479
        return b_decode;
    }
    else
    {
        return es->p_dec != NULL;
    }
}
1480 1481 1482 1483 1484
static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
{
    es_out_sys_t   *p_sys = out->p_sys;
    input_thread_t *p_input = p_sys->p_input;

1485
    p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
    if( p_es->p_dec )
    {
        if( p_sys->b_buffering )
            input_DecoderStartBuffering( p_es->p_dec );

        if( !p_es->p_master && p_sys->p_sout_record )
        {
            p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
            if( p_es->p_dec_record && p_sys->b_buffering )
                input_DecoderStartBuffering( p_es->p_dec_record );
        }
    }
1498 1499

    EsOutDecoderChangeDelay( out, p_es );
1500 1501 1502
}
static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
{
1503
    VLC_UNUSED(out);
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

    if( !p_es->p_dec )
        return;

    input_DecoderDelete( p_es->p_dec );
    p_es->p_dec = NULL;

    if( p_es->p_dec_record )
    {
        input_DecoderDelete( p_es->p_dec_record );
        p_es->p_dec_record = NULL;
    }
}

1518 1519 1520 1521 1522 1523
static void EsSelect( es_out_t *out, es_out_id_t *es )
{
    es_out_sys_t   *p_sys = out->p_sys;
    input_thread_t *p_input = p_sys->p_input;
    vlc_value_t    val;

1524
    if( EsIsSelected( es ) )
1525 1526 1527 1528 1529
    {
        msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
        return;
    }

1530
    if( es->p_master )
1531
    {
1532
        int i_channel;
1533 1534 1535
        if( !es->p_master->p_dec )
            return;

1536
        i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1537
        if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1538 1539
            return;
    }
1540
    else
1541
    {
1542
        if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1543
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1544
            if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1545 1546 1547 1548 1549
            {
                msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
                         es->i_id );
                return;
            }
1550
        }
1551
        else if( es->fmt.i_cat == AUDIO_ES )
1552
        {
1553
            var_Get( p_input, "audio", &val );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1554
            if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1555 1556 1557 1558 1559 1560 1561 1562 1563
            {
                msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
                         es->i_id );
                return;
            }
        }
        if( es->fmt.i_cat == SPU_ES )
        {
            var_Get( p_input, "spu", &val );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1564
            if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1565 1566 1567 1568 1569
            {
                msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
                         es->i_id );
                return;
            }
1570
        }
1571

1572 1573
        EsCreateDecoder( out, es );