es_out.c 85 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
#include <vlc_fourcc.h>
41 42

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

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

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

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

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

65
    bool b_selected;
66
    bool b_scrambled;
67 68

    /* Clock for this program */
69
    input_clock_t *p_clock;
70

71
    char    *psz_name;
72
    char    *psz_now_playing;
73
    char    *psz_publisher;
74

75
    vlc_epg_t *p_epg;
76 77 78 79 80 81 82 83
} es_out_pgrm_t;

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

84 85 86
    /* */
    bool b_scrambled;

87 88 89 90 91
    /* Channel in the track type */
    int         i_channel;
    es_format_t fmt;
    char        *psz_language;
    char        *psz_language_code;
92

93
    decoder_t   *p_dec;
94
    decoder_t   *p_dec_record;
95 96

    /* Fields for Video with CC */
97
    bool  pb_cc_present[4];
98 99 100 101
    es_out_id_t  *pp_cc_es[4];

    /* Field for CC track from a master video */
    es_out_id_t *p_master;
102 103 104

    /* ID for the meta data */
    int         i_meta_id;
105 106 107 108 109 110
};

struct es_out_sys_t
{
    input_thread_t *p_input;

111 112 113
    /* */
    vlc_mutex_t   lock;

114 115 116 117 118 119 120 121 122 123 124 125
    /* 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 */
126
    bool  b_active;
127 128 129 130 131 132 133 134
    int         i_mode;

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

    /* es to select */
135 136
    int         i_audio_last, i_audio_id;
    int         i_sub_last, i_sub_id;
137
    int         i_default_sub_id;   /* As specified in container; if applicable */
138 139 140 141 142 143 144 145 146 147 148
    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;
149

150 151 152
    /* Clock configuration */
    mtime_t     i_pts_delay;
    int         i_cr_average;
153
    int         i_rate;
154

Laurent Aimar's avatar
Laurent Aimar committed
155 156
    /* */
    bool        b_paused;
157
    mtime_t     i_pause_date;
Laurent Aimar's avatar
Laurent Aimar committed
158

159
    /* Current preroll */
Laurent Aimar's avatar
Laurent Aimar committed
160
    mtime_t     i_preroll_end;
161

162 163
    /* Used for buffering */
    bool        b_buffering;
Laurent Aimar's avatar
Laurent Aimar committed
164 165 166
    mtime_t     i_buffering_extra_initial;
    mtime_t     i_buffering_extra_stream;
    mtime_t     i_buffering_extra_system;
167

168 169
    /* Record */
    sout_instance_t *p_sout_record;
170 171
};

172
static es_out_id_t *EsOutAdd    ( es_out_t *, const es_format_t * );
173 174 175
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 );
176
static void         EsOutDelete ( es_out_t * );
177

178
static void         EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
Laurent Aimar's avatar
Laurent Aimar committed
179
static void         EsOutUpdateInfo( es_out_t *, es_out_id_t *es, const es_format_t *, const vlc_meta_t * );
180
static int          EsOutSetRecord(  es_out_t *, bool b_record );
181

182
static bool EsIsSelected( es_out_id_t *es );
183
static void EsSelect( es_out_t *out, es_out_id_t *es );
184
static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
185 186 187
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 );
Laurent Aimar's avatar
Laurent Aimar committed
188
static void EsOutProgramsChangeRate( es_out_t *out );
189
static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
190

191 192 193 194 195
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 );

196 197
static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
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;
}
214 215
static inline bool EsFmtIsTeletext( const es_format_t *p_fmt )
{
216
    return p_fmt->i_cat == SPU_ES && p_fmt->i_codec == VLC_CODEC_TELETEXT;
217
}
218

219 220 221
/*****************************************************************************
 * input_EsOutNew:
 *****************************************************************************/
222
es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
223
{
224 225 226
    es_out_t     *out = malloc( sizeof( *out ) );
    if( !out )
        return NULL;
227

228
    es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
229 230 231 232 233 234
    if( !p_sys )
    {
        free( out );
        return NULL;
    }

235 236 237 238
    out->pf_add     = EsOutAdd;
    out->pf_send    = EsOutSend;
    out->pf_del     = EsOutDel;
    out->pf_control = EsOutControl;
239
    out->pf_destroy = EsOutDelete;
240
    out->p_sys      = p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
241
    out->b_sout     = p_input->p->p_sout != NULL;
242

243 244

    vlc_mutex_init_recursive( &p_sys->lock );
245 246
    p_sys->p_input = p_input;

247
    p_sys->b_active = false;
248 249 250
    p_sys->i_mode   = ES_OUT_MODE_AUTO;


251
    TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
252 253 254
    p_sys->p_pgrm   = NULL;

    p_sys->i_id    = 0;
255 256

    TAB_INIT( p_sys->i_es, p_sys->es );
257 258 259 260 261 262

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

    /* */
263
    p_sys->i_audio_last = var_GetInteger( p_input, "audio-track" );
264

265
    p_sys->i_sub_last = var_GetInteger( p_input, "sub-track" );
266

267 268
    p_sys->i_default_sub_id   = -1;

269
    if( !p_input->b_preparsing )
270
    {
271 272 273 274
        char *psz_string;

        psz_string = var_GetString( p_input, "audio-language" );
        p_sys->ppsz_audio_language = LanguageSplit( psz_string );
275 276
        if( p_sys->ppsz_audio_language )
        {
277
            for( int i = 0; p_sys->ppsz_audio_language[i]; i++ )
278 279 280
                msg_Dbg( p_input, "selected audio language[%d] %s",
                         i, p_sys->ppsz_audio_language[i] );
        }
281
        free( psz_string );
282

283 284
        psz_string = var_GetString( p_input, "sub-language" );
        p_sys->ppsz_sub_language = LanguageSplit( psz_string );
285 286
        if( p_sys->ppsz_sub_language )
        {
287
            for( int i = 0; p_sys->ppsz_sub_language[i]; i++ )
288 289 290
                msg_Dbg( p_input, "selected subtitle language[%d] %s",
                         i, p_sys->ppsz_sub_language[i] );
        }
291
        free( psz_string );
292
    }
293
    else
294
    {
295 296
        p_sys->ppsz_sub_language = NULL;
        p_sys->ppsz_audio_language = NULL;
297 298
    }

299
    p_sys->i_audio_id = var_GetInteger( p_input, "audio-track-id" );
300

301
    p_sys->i_sub_id = var_GetInteger( p_input, "sub-track-id" );
302

303 304 305 306 307 308 309
    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;

Laurent Aimar's avatar
Laurent Aimar committed
310
    p_sys->b_paused = false;
311
    p_sys->i_pause_date = -1;
Laurent Aimar's avatar
Laurent Aimar committed
312

313
    p_sys->i_rate = i_rate;
314 315
    p_sys->i_pts_delay = 0;
    p_sys->i_cr_average = 0;
316

317
    p_sys->b_buffering = true;
Laurent Aimar's avatar
Laurent Aimar committed
318 319 320
    p_sys->i_buffering_extra_initial = 0;
    p_sys->i_buffering_extra_stream = 0;
    p_sys->i_buffering_extra_system = 0;
321
    p_sys->i_preroll_end = -1;
322

323 324
    p_sys->p_sout_record = NULL;

325 326 327 328 329 330
    return out;
}

/*****************************************************************************
 *
 *****************************************************************************/
331 332 333
static void EsOutDelete( es_out_t *out )
{
    es_out_sys_t *p_sys = out->p_sys;
334
    input_thread_t *p_input = p_sys->p_input;
335 336 337
    int i;

    if( p_sys->p_sout_record )
338
        EsOutSetRecord( out, false );
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

    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 );
370 371 372 373 374 375 376 377 378 379 380 381

        /* Remove SDT and EPG entries */
        char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
        input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
        char *psz_epg;
        if( asprintf( &psz_epg, "EPG %s", psz_cat ) >= 0 )
        {
            input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
            free( psz_epg );
        }
        free( psz_cat );

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
        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 );
}

397 398 399 400 401 402 403 404 405 406 407
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
     *
408 409
     * 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 ||
410 411 412 413 414 415 416
        p_input->p->b_out_pace_control ||
        p_sys->b_buffering )
        return 0;

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

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
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;
}
433

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
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;
}

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
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 )
    {
480 481
        char *psz_path = var_CreateGetNonEmptyString( p_input, "input-record-path" );
        if( !psz_path )
482 483 484 485 486 487 488 489
        {
            if( var_CountChoices( p_input, "video-es" ) )
                psz_path = config_GetUserDir( VLC_VIDEOS_DIR );
            else if( var_CountChoices( p_input, "audio-es" ) )
                psz_path = config_GetUserDir( VLC_MUSIC_DIR );
            else
                psz_path = config_GetUserDir( VLC_DOWNLOAD_DIR );
        }
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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547

        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;
}
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
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 */
573
                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;
574 575 576 577 578 579 580 581 582 583 584 585
                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;
586
    p_sys->i_pause_date = i_date;
587 588 589 590 591 592 593
}

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;
594
    EsOutProgramsChangeRate( out );
595 596 597 598 599 600
}

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

601 602
    input_SendEventCache( p_sys->p_input, 0.0 );

603 604 605 606 607 608 609 610 611 612
    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
613
            input_DecoderStartBuffering( p_es->p_dec_record );
614 615 616 617 618 619 620 621 622 623 624 625
    }

    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;
}

626

627

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
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;

644
    mtime_t i_preroll_duration = 0;
645 646
    if( p_sys->i_preroll_end >= 0 )
        i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
647

648
    const mtime_t i_buffering_duration = p_sys->i_pts_delay +
Laurent Aimar's avatar
Laurent Aimar committed
649
                                         i_preroll_duration +
650
                                         p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
Laurent Aimar's avatar
Laurent Aimar committed
651 652

    if( i_stream_duration <= i_buffering_duration && !b_forced )
653
    {
654 655 656 657
        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) );
658 659
        return;
    }
660
    input_SendEventCache( p_sys->p_input, 1.0 );
661 662 663 664

    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;
665
    p_sys->i_preroll_end = -1;
666

Laurent Aimar's avatar
Laurent Aimar committed
667 668 669 670 671 672
    if( p_sys->i_buffering_extra_initial > 0 )
    {
        /* FIXME wrong ? */
        return;
    }

673 674 675 676 677
    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];

Laurent Aimar's avatar
Laurent Aimar committed
678
        if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
679 680 681 682 683 684 685 686 687
            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 );

688
    /* Here is a good place to destroy unused vout with every demuxer */
689
    input_resource_TerminateVout( p_sys->p_input->p->p_resource );
690 691

    /* */
692 693 694 695
    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 );
696 697 698 699 700 701 702 703 704 705

    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
706
            input_DecoderStopBuffering( p_es->p_dec_record );
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
    }
}
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 );
        }
    }
}
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

static bool EsOutIsExtraBufferingAllowed( es_out_t *out )
{
    es_out_sys_t *p_sys = out->p_sys;

    size_t i_size = 0;
    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 )
            i_size += input_DecoderGetFifoSize( p_es->p_dec );
        if( p_es->p_dec_record )
            i_size += input_DecoderGetFifoSize( p_es->p_dec_record );
    }
    //fprintf( stderr, "----- EsOutIsExtraBufferingAllowed =% 5d kbytes -- ", i_size / 1024 );

    /* TODO maybe we want to be able to tune it ? */
#if defined(OPTIMIZE_MEMORY)
    const size_t i_level_high = 500000;  /* 0.5 Mbytes */
#else
    const size_t i_level_high = 10000000; /* 10 Mbytes */
#endif
    return i_size < i_level_high;
}

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
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 )
hartman's avatar
hartman committed
775
            input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
776 777
    }
}
Laurent Aimar's avatar
Laurent Aimar committed
778 779 780
static void EsOutProgramsChangeRate( es_out_t *out )
{
    es_out_sys_t      *p_sys = out->p_sys;
781

Laurent Aimar's avatar
Laurent Aimar committed
782 783 784
    for( int i = 0; i < p_sys->i_pgrm; i++ )
        input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
}
785

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
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;

839
        p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
840 841 842 843 844 845 846 847 848 849 850 851 852 853
        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;
}
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
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;
872

873
    mtime_t i_delay;
874

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
    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;
894
        i_delay = p_sys->i_pts_delay - i_consumed;
895 896 897 898 899
    }
    if( i_delay < 0 )
        return 0;
    return i_delay;
}
900

901 902
static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
                                     const es_format_t *fmt, const char *psz_language,
903
                                     bool b_delete )
904 905 906 907 908 909 910
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    vlc_value_t       val, text;

    if( b_delete )
    {
911 912
        if( EsFmtIsTeletext( fmt ) )
            input_SendEventTeletextDel( p_sys->p_input, i_id );
913

914
        input_SendEventEsDel( p_input, fmt->i_cat, i_id );
915 916 917 918
        return;
    }

    /* Get the number of ES already added */
919 920 921 922 923 924 925 926
    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";

927 928 929 930 931 932 933 934 935 936 937 938
    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 */
939
    if( fmt->psz_description && *fmt->psz_description )
940
    {
941
        if( psz_language && *psz_language )
942
        {
943 944 945 946
            text.psz_string = malloc( strlen( fmt->psz_description) +
                                      strlen( psz_language ) + 10 );
            sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
                                                   psz_language );
947
        }
948
        else text.psz_string = strdup( fmt->psz_description );
949 950 951
    }
    else
    {
952
        if( psz_language && *psz_language )
953
        {
954 955
            if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
                text.psz_string = NULL;
956 957 958
        }
        else
        {
959 960
            if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
                text.psz_string = NULL;
961 962 963
        }
    }

964
    input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
965
    if( EsFmtIsTeletext( fmt ) )
966 967 968 969 970 971 972 973
    {
        char psz_page[3+1];
        snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
                  fmt->subs.teletext.i_magazine,
                  fmt->subs.teletext.i_page );
        input_SendEventTeletextAdd( p_sys->p_input,
                                    i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
    }
974 975 976 977

    free( text.psz_string );
}

978
static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
979
                              bool b_delete )
980 981 982 983
{
    EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
}

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
/* 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;
zorglub's avatar
zorglub committed
999
        msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
1000 1001 1002

        for( i = 0; i < p_sys->i_es; i++ )
        {
1003
            if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
1004
                p_sys->i_mode != ES_OUT_MODE_ALL )
1005
                EsUnselect( out, p_sys->es[i], true );
1006 1007 1008 1009 1010 1011 1012
        }

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

zorglub's avatar
zorglub committed
1013
    msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
1014 1015

    /* Mark it selected */
1016
    p_pgrm->b_selected = true;
1017 1018 1019 1020 1021

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

    /* Update "program" */
1022
    input_SendEventProgramSelect( p_input, p_pgrm->i_id );
1023 1024

    /* Update "es-*" */
1025 1026 1027
    input_SendEventEsDel( p_input, AUDIO_ES, -1 );
    input_SendEventEsDel( p_input, VIDEO_ES, -1 );
    input_SendEventEsDel( p_input, SPU_ES, -1 );
1028
    input_SendEventTeletextDel( p_input, -1 );
1029
    input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
1030 1031

    /* TODO event */
1032
    var_SetInteger( p_input, "teletext-es", -1 );
1033

1034 1035 1036
    for( i = 0; i < p_sys->i_es; i++ )
    {
        if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
1037 1038
            EsOutESVarUpdate( out, p_sys->es[i], false );
        EsOutSelect( out, p_sys->es[i], false );
1039 1040
    }

1041
    /* Update now playing */
1042 1043
    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 );
1044

1045
    input_SendEventMeta( p_input );
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
}

/* 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 ) );
1057 1058
    if( !p_pgrm )
        return NULL;
1059 1060 1061 1062

    /* Init */
    p_pgrm->i_id = i_group;
    p_pgrm->i_es = 0;
1063
    p_pgrm->b_selected = false;
1064
    p_pgrm->b_scrambled = false;
1065
    p_pgrm->psz_name = NULL;
1066
    p_pgrm->psz_now_playing = NULL;
1067 1068
    p_pgrm->psz_publisher = NULL;
    p_pgrm->p_epg = NULL;
1069
    p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1070 1071 1072 1073 1074
    if( !p_pgrm->p_clock )
    {
        free( p_pgrm );
        return NULL;
    }
1075 1076
    if( p_sys->b_paused )
        input_clock_ChangePause( p_pgrm->p_clock, p_sys->b_paused, p_sys->i_pause_date );
1077 1078
    input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );

1079 1080 1081 1082
    /* Append it */
    TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );

    /* Update "program" variable */
1083
    input_SendEventProgramAdd( p_input, i_group, NULL );
1084 1085 1086

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

1088 1089 1090
    return p_pgrm;
}

1091 1092 1093
/* EsOutDelProgram:
 *  Delete a program
 */
1094
static int EsOutProgramDel( es_out_t *out, int i_group )
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
{
    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