es_out.c 65.4 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
dionoea's avatar
dionoea 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

zorglub's avatar
zorglub committed
36 37 38
#include <vlc_input.h>
#include <vlc_es_out.h>
#include <vlc_block.h>
39
#include <vlc_aout.h>
40 41 42

#include "input_internal.h"

43 44
#include "../stream_output/stream_output.h"

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

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

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

60
    bool b_selected;
61 62

    /* Clock for this program */
63
    input_clock_t *p_clock;
64

65
    char    *psz_name;
66
    char    *psz_now_playing;
67
    char    *psz_publisher;
68

69
    vlc_epg_t *p_epg;
70 71 72 73 74 75 76 77
} es_out_pgrm_t;

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

78
    /* Misc. */
79 80 81 82 83 84 85
    int64_t i_preroll_end;

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

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

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

    /* Field for CC track from a master video */
    es_out_id_t *p_master;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
};

struct es_out_sys_t
{
    input_thread_t *p_input;

    /* 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 */
114
    bool  b_active;
115 116 117 118 119 120 121 122
    int         i_mode;

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

    /* es to select */
123 124
    int         i_audio_last, i_audio_id;
    int         i_sub_last, i_sub_id;
125
    int         i_default_sub_id;   /* As specified in container; if applicable */
126 127 128 129 130 131 132 133 134 135 136
    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;
137 138 139

    /* Rate used to rescale ES ts */
    int         i_rate;
140 141 142

    /* Record */
    sout_instance_t *p_sout_record;
143 144 145 146 147
};

static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
static void         EsOutDel    ( es_out_t *, es_out_id_t * );
148
static void         EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
149 150 151 152
static int          EsOutControl( es_out_t *, int i_query, va_list );

static void         EsOutAddInfo( es_out_t *, es_out_id_t *es );

153
static bool EsIsSelected( es_out_id_t *es );
154
static void EsSelect( es_out_t *out, es_out_id_t *es );
155
static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
156 157 158 159 160
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 );

161 162
static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
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;
}


181 182 183
/*****************************************************************************
 * input_EsOutNew:
 *****************************************************************************/
184
es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
185 186 187 188
{
    vlc_value_t  val;
    int i;

189
    es_out_t     *out = malloc( sizeof( es_out_t ) );
190
    if( !out ) return NULL;
191 192

    es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
193 194 195 196 197 198
    if( !p_sys )
    {
        free( out );
        return NULL;
    }

199 200 201 202 203
    out->pf_add     = EsOutAdd;
    out->pf_send    = EsOutSend;
    out->pf_del     = EsOutDel;
    out->pf_control = EsOutControl;
    out->p_sys      = p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
204
    out->b_sout     = p_input->p->p_sout != NULL;
205 206 207

    p_sys->p_input = p_input;

208
    p_sys->b_active = false;
209 210 211
    p_sys->i_mode   = ES_OUT_MODE_AUTO;


212
    TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
213 214 215
    p_sys->p_pgrm   = NULL;

    p_sys->i_id    = 0;
216 217

    TAB_INIT( p_sys->i_es, p_sys->es );
218 219 220 221 222 223 224 225 226 227 228 229

    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;

230 231
    p_sys->i_default_sub_id   = -1;

232
    if( !p_input->b_preparsing )
233
    {
234 235 236 237 238 239 240 241
        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] );
        }
242
        free( val.psz_string );
243 244 245 246 247 248 249 250 251

        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] );
        }
252
        free( val.psz_string );
253
    }
254
    else
255
    {
256 257
        p_sys->ppsz_sub_language = NULL;
        p_sys->ppsz_audio_language = NULL;
258 259
    }

260 261 262 263 264 265
    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;

266 267 268 269 270 271 272
    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;

273 274
    p_sys->i_rate = i_rate;

275 276
    p_sys->p_sout_record = NULL;

277 278 279 280 281 282 283 284 285 286 287
    return out;
}

/*****************************************************************************
 * input_EsOutDelete:
 *****************************************************************************/
void input_EsOutDelete( es_out_t *out )
{
    es_out_sys_t *p_sys = out->p_sys;
    int i;

288 289 290
    if( p_sys->p_sout_record )
        input_EsOutSetRecord( out, false );

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

296 297
        free( p_sys->es[i]->psz_language );
        free( p_sys->es[i]->psz_language_code );
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        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 );
    }
314
    free( p_sys->es );
315

316
    /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
317 318
    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
319
        es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
Laurent Aimar's avatar
Laurent Aimar committed
320
        input_clock_Delete( p_pgrm->p_clock );
321 322 323
        free( p_pgrm->psz_now_playing );
        free( p_pgrm->psz_publisher );
        free( p_pgrm->psz_name );
324 325 326 327
        if( p_pgrm->p_epg )
            vlc_epg_Delete( p_pgrm->p_epg );

        free( p_pgrm );
328
    }
329
    TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

    free( p_sys );
    free( out );
}

es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
{
    int i;
    if( i_id < 0 )
    {
        /* Special HACK, -i_id is tha 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;
}

352 353
mtime_t input_EsOutGetWakeup( es_out_t *out )
{
354 355
    es_out_sys_t   *p_sys = out->p_sys;
    input_thread_t *p_input = p_sys->p_input;
356 357 358

    if( !p_sys->p_pgrm )
        return 0;
359 360 361 362 363 364 365

    /* We do not have a wake up date if the input cannot have its speed
     * controlled or sout is imposing its own */
    if( !p_input->b_can_pace_control || p_input->p->b_out_pace_control )
        return 0;

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

368
static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
369 370 371 372 373 374 375
{
    es_out_sys_t      *p_sys = out->p_sys;
    int i;

    for( i = 0; i < p_sys->i_es; i++ )
    {
        es_out_id_t *es = p_sys->es[i];
zorglub's avatar
zorglub committed
376

377 378 379
        /* Send a dummy block to let decoder know that
         * there is a discontinuity */
        if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
380
        {
381
            input_DecoderDiscontinuity( es->p_dec, b_flush );
382 383 384
            if( es->p_dec_record )
                input_DecoderDiscontinuity( es->p_dec_record, b_flush );
        }
385 386
    }
}
387
void input_EsOutChangeRate( es_out_t *out, int i_rate )
388 389 390 391
{
    es_out_sys_t      *p_sys = out->p_sys;
    int i;

392
    p_sys->i_rate = i_rate;
393
    EsOutDiscontinuity( out, false, false );
394

395
    for( i = 0; i < p_sys->i_pgrm; i++ )
396
        input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, i_rate );
397
}
398

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
int input_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;

432
#ifdef ENABLE_SOUT
433
        p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
434
#endif
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
        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_sys->p_sout_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;
        }
462
#ifdef ENABLE_SOUT
463
        sout_DeleteInstance( p_sys->p_sout_record );
464
#endif
465 466 467 468 469
        p_sys->p_sout_record = NULL;
    }

    return VLC_SUCCESS;
}
470 471 472 473 474 475 476 477 478
void input_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;
}
479 480 481 482 483 484 485 486 487
void input_EsOutChangeState( es_out_t *out )
{
    es_out_sys_t *p_sys = out->p_sys;
    input_thread_t *p_input = p_sys->p_input;

    if( p_input->i_state  == PAUSE_S )
    {
        /* Send discontinuity to decoders (it will allow them to flush
         *                  * if implemented */
488
        EsOutDiscontinuity( out, false, false );
489 490 491 492 493 494 495
    }
    else
    {
        /* Out of pause, reset pcr */
        es_out_Control( out, ES_OUT_RESET_PCR );
    }
}
Laurent Aimar's avatar
Laurent Aimar committed
496 497 498 499 500
void input_EsOutChangePosition( es_out_t *out )
{
    //es_out_sys_t *p_sys = out->p_sys;

    es_out_Control( out, ES_OUT_RESET_PCR );
501
    EsOutDiscontinuity( out, true, false );
Laurent Aimar's avatar
Laurent Aimar committed
502
}
503

504
bool input_EsOutDecodersEmpty( es_out_t *out )
505 506 507 508 509 510 511 512 513
{
    es_out_sys_t      *p_sys = out->p_sys;
    int i;

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

        if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
514
            return false;
515 516
        if( es->p_dec_record && !input_DecoderEmpty( es->p_dec_record ) )
            return false;
517
    }
518
    return true;
519 520 521 522 523
}

/*****************************************************************************
 *
 *****************************************************************************/
524
static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
525
                                     bool b_delete )
526 527 528
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
529
    const  bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
530 531
    vlc_value_t       val, text;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
532
    const char *psz_var;
533

534
    if( fmt->i_cat == AUDIO_ES )
535
        psz_var = "audio-es";
536
    else if( fmt->i_cat == VIDEO_ES )
537
        psz_var = "video-es";
538
    else if( fmt->i_cat == SPU_ES )
539 540 541 542 543 544
        psz_var = "spu-es";
    else
        return;

    if( b_delete )
    {
545 546 547
        if( b_teletext )
            var_SetInteger( p_sys->p_input, "teletext-es", -1 );

548
        val.i_int = i_id;
549
        var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
550

551
        var_SetBool( p_sys->p_input, "intf-change", true );
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
        return;
    }

    /* Get the number of ES already added */
    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 */
568
    if( fmt->psz_description && *fmt->psz_description )
569
    {
570
        if( psz_language && *psz_language )
571
        {
572 573 574 575
            text.psz_string = malloc( strlen( fmt->psz_description) +
                                      strlen( psz_language ) + 10 );
            sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
                                                   psz_language );
576
        }
577
        else text.psz_string = strdup( fmt->psz_description );
578 579 580
    }
    else
    {
581
        if( psz_language && *psz_language )
582
        {
583 584
            if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
                text.psz_string = NULL;
585 586 587
        }
        else
        {
588 589
            if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
                text.psz_string = NULL;
590 591 592
        }
    }

593
    val.i_int = i_id;
594 595 596 597
    var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );

    free( text.psz_string );

598
    if( b_teletext )
599 600 601 602
    {
        if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
            var_SetInteger( p_sys->p_input, "teletext-es", i_id );
    }
603

604
    var_SetBool( p_sys->p_input, "intf-change", true );
605 606
}

607
static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
608
                              bool b_delete )
609 610 611 612
{
    EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
}

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
/* 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;
    vlc_value_t       val;
    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;
zorglub's avatar
zorglub committed
629
        msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
630 631 632

        for( i = 0; i < p_sys->i_es; i++ )
        {
633
            if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
634
                p_sys->i_mode != ES_OUT_MODE_ALL )
635
                EsUnselect( out, p_sys->es[i], true );
636 637 638 639 640 641 642
        }

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

zorglub's avatar
zorglub committed
643
    msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
644 645

    /* Mark it selected */
646
    p_pgrm->b_selected = true;
647 648 649 650 651 652 653 654 655 656 657 658

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

    /* Update "program" */
    val.i_int = p_pgrm->i_id;
    var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );

    /* Update "es-*" */
    var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
    var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
    var_Change( p_input, "spu-es",   VLC_VAR_CLEARCHOICES, NULL, NULL );
659
    var_SetInteger( p_input, "teletext-es", -1 );
660 661 662
    for( i = 0; i < p_sys->i_es; i++ )
    {
        if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
663 664
            EsOutESVarUpdate( out, p_sys->es[i], false );
        EsOutSelect( out, p_sys->es[i], false );
665 666
    }

667
    /* Update now playing */
668 669 670 671
    input_item_SetNowPlaying( p_input->p->input.p_item,
                              p_pgrm->psz_now_playing );
    input_item_SetPublisher( p_input->p->input.p_item,
                             p_pgrm->psz_publisher );
672

673
    var_SetBool( p_sys->p_input, "intf-change", true );
674 675 676 677 678 679 680 681 682 683 684 685
}

/* 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;
    vlc_value_t       val;

    es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
686 687
    if( !p_pgrm )
        return NULL;
688 689 690 691

    /* Init */
    p_pgrm->i_id = i_group;
    p_pgrm->i_es = 0;
692
    p_pgrm->b_selected = false;
693
    p_pgrm->psz_name = NULL;
694
    p_pgrm->psz_now_playing = NULL;
695 696
    p_pgrm->psz_publisher = NULL;
    p_pgrm->p_epg = NULL;
697
    p_pgrm->p_clock = input_clock_New( p_input->p->input.i_cr_average, p_sys->i_rate );
698 699 700 701 702
    if( !p_pgrm->p_clock )
    {
        free( p_pgrm );
        return NULL;
    }
703 704 705 706 707 708 709 710 711 712 713 714 715 716

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

    /* Update "program" variable */
    val.i_int = i_group;
    var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );

    if( i_group == var_GetInteger( p_input, "program" ) )
    {
        EsOutProgramSelect( out, p_pgrm );
    }
    else
    {
717
        var_SetBool( p_sys->p_input, "intf-change", true );
718 719 720 721
    }
    return p_pgrm;
}

722 723 724
/* EsOutDelProgram:
 *  Delete a program
 */
725
static int EsOutProgramDel( es_out_t *out, int i_group )
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
{
    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;
    vlc_value_t       val;
    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;
        }
    }

742 743
    if( p_pgrm == NULL )
        return VLC_EGENERIC;
744 745 746 747 748

    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 );
749
        return VLC_EGENERIC;
750 751 752 753 754
    }

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

    /* If program is selected we need to unselect it */
755 756 757
    if( p_sys->p_pgrm == p_pgrm )
        p_sys->p_pgrm = NULL;

Laurent Aimar's avatar
Laurent Aimar committed
758
    input_clock_Delete( p_pgrm->p_clock );
759

760 761 762
    free( p_pgrm->psz_name );
    free( p_pgrm->psz_now_playing );
    free( p_pgrm->psz_publisher );
763 764
    if( p_pgrm->p_epg )
        vlc_epg_Delete( p_pgrm->p_epg );
765 766 767 768 769 770
    free( p_pgrm );

    /* Update "program" variable */
    val.i_int = i_group;
    var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );

771
    var_SetBool( p_sys->p_input, "intf-change", true );
772 773

    return VLC_SUCCESS;
774 775
}

776 777
/* EsOutProgramMeta:
 */
778 779 780 781
static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
{
    char *psz = NULL;
    if( p_pgrm->psz_name )
782 783
    {
        if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
ivoire's avatar
ivoire committed
784
            return NULL;
785
    }
786
    else
787 788
    {
        if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
ivoire's avatar
ivoire committed
789
            return NULL;
790
    }
791 792 793
    return psz;
}

794 795 796
static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
{
    es_out_sys_t      *p_sys = out->p_sys;
797
    es_out_pgrm_t     *p_pgrm = NULL;
798
    input_thread_t    *p_input = p_sys->p_input;
799
    char              *psz_cat;
800 801
    const char        *psz_title = NULL;
    const char        *psz_provider = NULL;
802 803 804
    int i;

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

806
    /* Check against empty meta data (empty for what we handle) */
807 808 809 810
    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 )
811 812 813
    {
        return;
    }
814
    /* Find program */
815 816 817 818 819 820 821 822
    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;
        }
    }
823 824
    if( p_pgrm == NULL )
        p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
825

826
    /* */
827 828
    psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
    psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
829 830

    /* Update the description text of the program */
zorglub's avatar
zorglub committed
831
    if( psz_title && *psz_title )
832 833 834 835
    {
        vlc_value_t val;
        vlc_value_t text;

836 837 838 839 840 841 842 843 844
        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 );
        }
845
        free( p_pgrm->psz_name );
846 847
        p_pgrm->psz_name = strdup( psz_title );

848 849 850 851 852 853
        /* ugly but it works */
        val.i_int = i_group;
        var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );

        if( psz_provider && *psz_provider )
        {
854 855 856 857 858
            if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
            {
                var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
                free( text.psz_string );
            }
859 860 861
        }
        else
        {
862
            text.psz_string = (char *)psz_title;
863 864 865 866
            var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
        }
    }

867 868 869
    psz_cat = EsOutProgramGetMetaName( p_pgrm );
    if( psz_provider )
    {
870
        if( p_sys->p_pgrm == p_pgrm )
871 872
            input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
        input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
873
    }
874 875 876 877 878 879 880 881
    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 );
882 883 884 885 886 887 888 889 890 891 892 893

    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];
894
        bool b_add = true;
895 896 897
        int j;

        for( j = 0; j < p_dst->i_event; j++ )
898
        {
899 900
            if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
            {
901
                b_add = false;
902 903 904 905
                break;
            }
            if( p_dst->pp_event[j]->i_start > p_evt->i_start )
                break;
906
        }
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
        if( b_add )
        {
            vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
            if( !p_copy )
                break;
            memset( p_copy, 0, sizeof(vlc_epg_event_t) );
            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
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
960 961 962
    char *psz_epg;
    if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
        psz_epg = NULL;
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
    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 */
988
    free( p_pgrm->psz_now_playing );
989 990 991
    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 );
992 993

    if( p_pgrm == p_sys->p_pgrm )
994
        input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
995 996

    if( p_pgrm->psz_now_playing )
997 998 999 1000 1001
    {
        input_Control( p_input, INPUT_ADD_INFO, psz_cat,
            input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
            p_pgrm->psz_now_playing );
    }
1002
    else
1003 1004 1005 1006
    {
        input_Control( p_input, INPUT_DEL_INFO, psz_cat,
            input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
    }
1007

1008 1009 1010
    free( psz_cat );
}

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
/* EsOutAdd:
 *  Add an es_out
 */
static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;

    es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
    es_out_pgrm_t     *p_pgrm = NULL;
    int i;

1023 1024
    if( !es ) return NULL;

1025 1026 1027
    if( fmt->i_group < 0 )
    {
        msg_Err( p_input, "invalid group number" );
1028
        free( es );
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
        return NULL;
    }

    /* 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 */
    if( fmt->i_id < 0 )
        fmt->i_id = out->p_sys->i_id;
    es->i_id = fmt->i_id;
    es->p_pgrm = p_pgrm;
    es_format_Copy( &es->fmt, fmt );
    es->i_preroll_end = -1;

    switch( fmt->i_cat )
    {
    case AUDIO_ES:
1061 1062 1063
    {
        audio_replay_gain_t rg;

1064
        es->i_channel = p_sys->i_audio;
1065 1066

        memset( &rg, 0, sizeof(rg) );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1067
        vlc_mutex_lock( &p_input->p->input.p_item->lock );
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
        vlc_mutex_unlock( &p_input->p->input.p_item->lock );

        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];
            }
        }
1084
        break;
1085
    }
1086 1087 1088

    case VIDEO_ES:
        es->i_channel = p_sys->i_video;
1089
        if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )