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

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
34
#include <errno.h>
35
#if defined (_WIN32)
36 37
#  include <direct.h>
#endif
38
#include <sys/stat.h>
39
#include <unistd.h>
40 41

#include <vlc_common.h>
42
#include <vlc_fs.h>
43
#ifdef _WIN32
44 45
#  include <vlc_charset.h>
#endif
46 47 48 49 50 51 52 53 54 55 56
#include <vlc_input.h>
#include <vlc_es_out.h>
#include <vlc_block.h>
#include "input_internal.h"
#include "es_out.h"
#include "es_out_timeshift.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

57 58 59 60 61 62 63
/* XXX attribute_packed is (and MUST be) used ONLY to reduce memory usage */
#ifdef HAVE_ATTRIBUTE_PACKED
#   define attribute_packed __attribute__((__packed__))
#else
#   define attribute_packed
#endif

64 65 66 67 68 69 70 71
enum
{
    C_ADD,
    C_SEND,
    C_DEL,
    C_CONTROL,
};

72
typedef struct attribute_packed
Laurent Aimar's avatar
Laurent Aimar committed
73 74 75
{
    es_out_id_t *p_es;
    es_format_t *p_fmt;
76
} ts_cmd_add_t;
Laurent Aimar's avatar
Laurent Aimar committed
77

78
typedef struct attribute_packed
Laurent Aimar's avatar
Laurent Aimar committed
79 80 81 82
{
    es_out_id_t *p_es;
} ts_cmd_del_t;

83
typedef struct attribute_packed
Laurent Aimar's avatar
Laurent Aimar committed
84 85 86
{
    es_out_id_t *p_es;
    block_t *p_block;
87
    int     i_offset;  /* We do not use file > INT_MAX */
Laurent Aimar's avatar
Laurent Aimar committed
88 89
} ts_cmd_send_t;

90
typedef struct attribute_packed
Laurent Aimar's avatar
Laurent Aimar committed
91 92 93
{
    int  i_query;

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    union
    {
        bool b_bool;
        int  i_int;
        int64_t i_i64;
        es_out_id_t *p_es;
        struct
        {
            int     i_int;
            int64_t i_i64;
        } int_i64;
        struct
        {
            int        i_int;
            vlc_meta_t *p_meta;
        } int_meta;
        struct
        {
            int       i_int;
            vlc_epg_t *p_epg;
        } int_epg;
        struct
        {
            es_out_id_t *p_es;
            bool        b_bool;
        } es_bool;
        struct
        {
            es_out_id_t *p_es;
            es_format_t *p_fmt;
        } es_fmt;
125 126 127 128 129 130 131
        struct
        {
            /* FIXME Really too big (double make the whole thing too big) */
            double  f_position;
            mtime_t i_time;
            mtime_t i_length;
        } times;
132 133 134
        struct
        {
            mtime_t i_pts_delay;
135
            mtime_t i_pts_jitter;
136 137
            int     i_cr_average;
        } jitter;
138
    } u;
Laurent Aimar's avatar
Laurent Aimar committed
139 140
} ts_cmd_control_t;

141
typedef struct attribute_packed
142
{
143
    int8_t  i_type;
144
    mtime_t i_date;
145 146
    union
    {
Laurent Aimar's avatar
Laurent Aimar committed
147 148 149 150
        ts_cmd_add_t     add;
        ts_cmd_del_t     del;
        ts_cmd_send_t    send;
        ts_cmd_control_t control;
151
    } u;
152 153
} ts_cmd_t;

154 155 156 157 158 159
typedef struct ts_storage_t ts_storage_t;
struct ts_storage_t
{
    ts_storage_t *p_next;

    /* */
160
#ifdef _WIN32
161
    char    *psz_file;  /* Filename */
162
#endif
163
    size_t  i_file_max; /* Max size in bytes */
164 165 166
    int64_t i_file_size;/* Current size in bytes */
    FILE    *p_filew;   /* FILE handle for data writing */
    FILE    *p_filer;   /* FILE handle for data reading */
167 168 169 170 171 172 173 174

    /* */
    int      i_cmd_r;
    int      i_cmd_w;
    int      i_cmd_max;
    ts_cmd_t *p_cmd;
};

175 176
typedef struct
{
177
    vlc_thread_t   thread;
178 179
    input_thread_t *p_input;
    es_out_t       *p_out;
180 181
    int64_t        i_tmp_size_max;
    const char     *psz_tmp_path;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

    /* Lock for all following fields */
    vlc_mutex_t    lock;
    vlc_cond_t     wait;

    /* */
    bool           b_paused;
    mtime_t        i_pause_date;

    /* */
    int            i_rate;
    int            i_rate_source;
    mtime_t        i_rate_date;
    mtime_t        i_rate_delay;

197 198 199
    /* */
    mtime_t        i_buffering_delay;

200
    /* */
201 202 203
    ts_storage_t   *p_storage_r;
    ts_storage_t   *p_storage_w;

204 205 206 207
    mtime_t        i_cmd_delay;

} ts_thread_t;

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
struct es_out_id_t
{
    es_out_id_t *p_es;
};

struct es_out_sys_t
{
    input_thread_t *p_input;
	es_out_t       *p_out;

    /* Configuration */
    int64_t        i_tmp_size_max;    /* Maximal temporary file size in byte */
    char           *psz_tmp_path;     /* Path for temporary files */

    /* Lock for all following fields */
    vlc_mutex_t    lock;

    /* */
    bool           b_delayed;
227
    ts_thread_t   *p_ts;
228

229 230 231 232 233
    /* */
    bool           b_input_paused;
    bool           b_input_paused_source;
    int            i_input_rate;
    int            i_input_rate_source;
234

235 236 237 238 239 240 241 242 243 244 245
    /* */
    int            i_es;
    es_out_id_t    **pp_es;
};

static es_out_id_t *Add    ( es_out_t *, const es_format_t * );
static int          Send   ( es_out_t *, es_out_id_t *, block_t * );
static void         Del    ( es_out_t *, es_out_id_t * );
static int          Control( es_out_t *, int i_query, va_list );
static void         Destroy( es_out_t * );

246
static int          TsStart( es_out_t * );
Laurent Aimar's avatar
Laurent Aimar committed
247 248
static void         TsAutoStop( es_out_t * );

249
static void         TsStop( ts_thread_t * );
250
static void         TsPushCmd( ts_thread_t *, ts_cmd_t * );
251
static int          TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
252
static bool         TsHasCmd( ts_thread_t * );
Laurent Aimar's avatar
Laurent Aimar committed
253
static bool         TsIsUnused( ts_thread_t * );
254 255
static int          TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
static int          TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
256

257
static void         *TsRun( void * );
258

259
static ts_storage_t *TsStorageNew( const char *psz_path, int64_t i_tmp_size_max );
260
static void         TsStorageDelete( ts_storage_t * );
261 262
static void         TsStoragePack( ts_storage_t *p_storage );
static bool         TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
263
static bool         TsStorageIsEmpty( ts_storage_t * );
264
static void         TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
265
static void         TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
266

267 268
static void CmdClean( ts_cmd_t * );
static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
269 270 271 272 273 274

static int  CmdInitAdd    ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
static void CmdInitSend   ( ts_cmd_t *, es_out_id_t *, block_t * );
static int  CmdInitDel    ( ts_cmd_t *, es_out_id_t * );
static int  CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );

275 276 277 278 279 280
/* */
static void CmdCleanAdd    ( ts_cmd_t * );
static void CmdCleanSend   ( ts_cmd_t * );
static void CmdCleanControl( ts_cmd_t *p_cmd );

/* XXX these functions will take the destination es_out_t */
281 282 283 284 285
static void CmdExecuteAdd    ( es_out_t *, ts_cmd_t * );
static int  CmdExecuteSend   ( es_out_t *, ts_cmd_t * );
static void CmdExecuteDel    ( es_out_t *, ts_cmd_t * );
static int  CmdExecuteControl( es_out_t *, ts_cmd_t * );

286
/* File helpers */
287
static int GetTmpFile( char **ppsz_file, const char *psz_path );
288 289 290 291

/*****************************************************************************
 * input_EsOutTimeshiftNew:
 *****************************************************************************/
292
es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
{
    es_out_t *p_out = malloc( sizeof(*p_out) );
    if( !p_out )
        return NULL;

    es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
    if( !p_sys )
    {
        free( p_out );
        return NULL;
    }

    /* */
    p_out->pf_add     = Add;
    p_out->pf_send    = Send;
    p_out->pf_del     = Del;
    p_out->pf_control = Control;
    p_out->pf_destroy = Destroy;
    p_out->p_sys      = p_sys;

    /* */
314 315
    p_sys->b_input_paused = false;
    p_sys->b_input_paused_source = false;
316
    p_sys->p_input = p_input;
317 318 319
    p_sys->i_input_rate = i_rate;
    p_sys->i_input_rate_source = i_rate;

320 321
    p_sys->p_out = p_next_out;
    vlc_mutex_init_recursive( &p_sys->lock );
322

323
    p_sys->b_delayed = false;
324
    p_sys->p_ts = NULL;
325 326 327

    TAB_INIT( p_sys->i_es, p_sys->pp_es );

328 329 330 331 332 333
    /* */
    const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
    if( i_tmp_size_max < 0 )
        p_sys->i_tmp_size_max = 50*1024*1024;
    else
        p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
334 335
    msg_Dbg( p_input, "using timeshift granularity of %d MiB",
             (int)p_sys->i_tmp_size_max/(1024*1024) );
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
    p_sys->psz_tmp_path = var_InheritString( p_input, "input-timeshift-path" );
#if defined (_WIN32) && !VLC_WINSTORE_APP
    if( p_sys->psz_tmp_path == NULL )
    {
        const DWORD count = GetTempPath( 0, NULL );
        if( count > 0 )
        {
            TCHAR *path = malloc( (count + 1) * sizeof(TCHAR) );
            if( path != NULL )
            {
                DWORD ret = GetTempPath( count + 1, path );
                if( ret != 0 && ret <= count )
                    p_sys->psz_tmp_path = FromT( path );
                free( path );
            }
        }
    }
    if( p_sys->psz_tmp_path == NULL )
    {
        wchar_t *wpath = _wgetcwd( NULL, 0 );
        if( wpath != NULL )
        {
            p_sys->psz_tmp_path = FromWide( wpath );
            free( wpath );
        }
    }
    if( p_sys->psz_tmp_path == NULL )
        p_sys->psz_tmp_path = strdup( "C:" );
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379
    if( p_sys->psz_tmp_path != NULL )
    {
        size_t len = strlen( p_sys->psz_tmp_path );

        while( len > 0 && p_sys->psz_tmp_path[len - 1] == DIR_SEP_CHAR )
            len--;

        p_sys->psz_tmp_path[len] = '\0';
    }
#endif
    if( p_sys->psz_tmp_path != NULL )
        msg_Dbg( p_input, "using timeshift path: %s", p_sys->psz_tmp_path );
    else
        msg_Dbg( p_input, "using default timeshift path" );
380

381 382 383 384 385 386 387 388 389 390
#if 0
#define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
    S(ts_cmd_t);
    S(ts_cmd_control_t);
    S(ts_cmd_send_t);
    S(ts_cmd_del_t);
    S(ts_cmd_add_t);
#undef S
#endif

391 392 393 394 395 396 397 398 399 400
    return p_out;
}

/*****************************************************************************
 * Internal functions
 *****************************************************************************/
static void Destroy( es_out_t *p_out )
{
    es_out_sys_t *p_sys = p_out->p_sys;

401
    if( p_sys->b_delayed )
402
    {
403
        TsStop( p_sys->p_ts );
404 405
        p_sys->b_delayed = false;
    }
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

    while( p_sys->i_es > 0 )
        Del( p_out, p_sys->pp_es[0] );
    TAB_CLEAN( p_sys->i_es, p_sys->pp_es  );

    free( p_sys->psz_tmp_path );
    vlc_mutex_destroy( &p_sys->lock );
    free( p_sys );
    free( p_out );
}

static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
{
    es_out_sys_t *p_sys = p_out->p_sys;
    ts_cmd_t cmd;

    es_out_id_t *p_es = malloc( sizeof( *p_es ) );
    if( !p_es )
        return NULL;

    vlc_mutex_lock( &p_sys->lock );

Laurent Aimar's avatar
Laurent Aimar committed
428 429
    TsAutoStop( p_out );

430 431 432 433 434 435 436 437 438 439
    if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
    {
        vlc_mutex_unlock( &p_sys->lock );
        free( p_es );
        return NULL;
    }

    TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );

    if( p_sys->b_delayed )
440
        TsPushCmd( p_sys->p_ts, &cmd );
441
    else
442
        CmdExecuteAdd( p_sys->p_out, &cmd );
443 444 445 446 447 448 449 450 451 452 453 454 455

    vlc_mutex_unlock( &p_sys->lock );

    return p_es;
}
static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
{
    es_out_sys_t *p_sys = p_out->p_sys;
    ts_cmd_t cmd;
    int i_ret = VLC_SUCCESS;

    vlc_mutex_lock( &p_sys->lock );

Laurent Aimar's avatar
Laurent Aimar committed
456 457
    TsAutoStop( p_out );

458 459
    CmdInitSend( &cmd, p_es, p_block );
    if( p_sys->b_delayed )
460
        TsPushCmd( p_sys->p_ts, &cmd );
461
    else
462
        i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
463 464 465 466 467 468 469 470 471 472 473 474

    vlc_mutex_unlock( &p_sys->lock );

    return i_ret;
}
static void Del( es_out_t *p_out, es_out_id_t *p_es )
{
    es_out_sys_t *p_sys = p_out->p_sys;
    ts_cmd_t cmd;

    vlc_mutex_lock( &p_sys->lock );

Laurent Aimar's avatar
Laurent Aimar committed
475 476
    TsAutoStop( p_out );

477 478
    CmdInitDel( &cmd, p_es );
    if( p_sys->b_delayed )
479
        TsPushCmd( p_sys->p_ts, &cmd );
480
    else
481
        CmdExecuteDel( p_sys->p_out, &cmd );
482 483 484 485 486

    TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );

    vlc_mutex_unlock( &p_sys->lock );
}
487 488 489 490 491

static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
{
    es_out_sys_t *p_sys = p_out->p_sys;

492
    if( p_sys->b_delayed && TsHasCmd( p_sys->p_ts ) )
493 494 495 496 497 498 499 500 501 502 503 504
        *pb_empty = false;
    else
        *pb_empty = es_out_GetEmpty( p_sys->p_out );

    return VLC_SUCCESS;
}
static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
{
    es_out_sys_t *p_sys = p_out->p_sys;

    if( p_sys->b_delayed )
    {
505
        assert( !p_sys->p_input->p->b_can_pace_control );
506 507 508 509 510 511 512 513 514
        *pi_wakeup = 0;
    }
    else
    {
        *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
    }

    return VLC_SUCCESS;
}
515
static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
516 517 518 519 520 521
{
    es_out_sys_t *p_sys = p_out->p_sys;

    if( p_sys->b_delayed )
        *pb_buffering = true;
    else
522
        *pb_buffering = es_out_GetBuffering( p_sys->p_out );
523 524 525 526 527 528

    return VLC_SUCCESS;
}
static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
{
    es_out_sys_t *p_sys = p_out->p_sys;
529 530 531 532 533 534 535 536 537
    int i_ret;

    if( !p_sys->b_delayed && !b_source_paused == !b_paused )
    {
        i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
    }
    else
    {
        i_ret = VLC_EGENERIC;
538
        if( !p_sys->p_input->p->b_can_pace_control )
539 540 541 542
        {
            if( !p_sys->b_delayed )
                TsStart( p_out );
            if( p_sys->b_delayed )
543
                i_ret = TsChangePause( p_sys->p_ts, b_source_paused, b_paused, i_date );
544 545 546 547 548 549
        }
        else
        {
            /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
             * and so be able to advertize correctly pace control property in access
             * module */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
550
            msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
551 552
        }
    }
553

554
    if( !i_ret )
555
    {
556 557
        p_sys->b_input_paused_source = b_source_paused;
        p_sys->b_input_paused = b_paused;
558
    }
559
    return i_ret;
560 561 562 563
}
static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
{
    es_out_sys_t *p_sys = p_out->p_sys;
564
    int i_ret;
565

566 567 568 569 570
    if( !p_sys->b_delayed && i_src_rate == i_rate )
    {
        i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
    }
    else
571
    {
572
        i_ret = VLC_EGENERIC;
573
        if( !p_sys->p_input->p->b_can_pace_control )
574 575 576 577
        {
            if( !p_sys->b_delayed )
                TsStart( p_out );
            if( p_sys->b_delayed )
578
                i_ret = TsChangeRate( p_sys->p_ts, i_src_rate, i_rate );
579 580 581 582 583 584
        }
        else
        {
            /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
             * and so be able to advertize correctly pace control property in access
             * module */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
585
            msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
586 587
        }

588
    }
589 590 591 592 593 594 595

    if( !i_ret )
    {
        p_sys->i_input_rate_source = i_src_rate;
        p_sys->i_input_rate = i_rate;
    }
    return i_ret;
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
}
static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
{
    es_out_sys_t *p_sys = p_out->p_sys;

    if( !p_sys->b_delayed )
        return es_out_SetTime( p_sys->p_out, i_date );

    /* TODO */
    msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
    return VLC_EGENERIC;
}
static int ControlLockedSetFrameNext( es_out_t *p_out )
{
    es_out_sys_t *p_sys = p_out->p_sys;

Laurent Aimar's avatar
Laurent Aimar committed
612
    return es_out_SetFrameNext( p_sys->p_out );
613 614
}

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
{
    es_out_sys_t *p_sys = p_out->p_sys;

    switch( i_query )
    {
    /* Pass-through control */
    case ES_OUT_SET_MODE:
    case ES_OUT_SET_GROUP:
    case ES_OUT_SET_PCR:
    case ES_OUT_SET_GROUP_PCR:
    case ES_OUT_RESET_PCR:
    case ES_OUT_SET_NEXT_DISPLAY_TIME:
    case ES_OUT_SET_GROUP_META:
    case ES_OUT_SET_GROUP_EPG:
630
    case ES_OUT_SET_ES_SCRAMBLED_STATE:
631
    case ES_OUT_DEL_GROUP:
632
    case ES_OUT_SET_META:
633 634 635 636 637
    case ES_OUT_SET_ES:
    case ES_OUT_RESTART_ES:
    case ES_OUT_SET_ES_DEFAULT:
    case ES_OUT_SET_ES_STATE:
    case ES_OUT_SET_ES_FMT:
638
    case ES_OUT_SET_TIMES:
639
    case ES_OUT_SET_JITTER:
640
    case ES_OUT_SET_EOS:
641 642 643 644 645 646
    {
        ts_cmd_t cmd;
        if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
            return VLC_EGENERIC;
        if( p_sys->b_delayed )
        {
647
            TsPushCmd( p_sys->p_ts, &cmd );
648 649
            return VLC_SUCCESS;
        }
650
        return CmdExecuteControl( p_sys->p_out, &cmd );
651 652
    }

653 654 655 656 657 658 659 660 661 662 663
    /* Special control when delayed */
    case ES_OUT_GET_ES_STATE:
    {
        es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
        bool *pb_enabled = (bool*)va_arg( args, bool* );

        if( p_sys->b_delayed )
        {
            *pb_enabled = true;
            return VLC_SUCCESS;
        }
664
        return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
665 666
    }
    /* Special internal input control */
667 668 669 670 671
    case ES_OUT_GET_EMPTY:
    {
        bool *pb_empty = (bool*)va_arg( args, bool* );
        return ControlLockedGetEmpty( p_out, pb_empty );
    }
672
    case ES_OUT_GET_WAKE_UP: /* TODO ? */
673 674 675 676
    {
        mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
        return ControlLockedGetWakeup( p_out, pi_wakeup );
    }
677
    case ES_OUT_GET_BUFFERING:
678 679
    {
        bool *pb_buffering = (bool *)va_arg( args, bool* );
680
        return ControlLockedGetBuffering( p_out, pb_buffering );
681
    }
682
    case ES_OUT_SET_PAUSE_STATE:
683 684 685 686 687 688 689
    {
        const bool b_source_paused = (bool)va_arg( args, int );
        const bool b_paused = (bool)va_arg( args, int );
        const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );

        return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
    }
690
    case ES_OUT_SET_RATE:
691 692 693 694 695 696
    {
        const int i_src_rate = (int)va_arg( args, int );
        const int i_rate = (int)va_arg( args, int );

        return ControlLockedSetRate( p_out, i_src_rate, i_rate );
    }
697
    case ES_OUT_SET_TIME:
698 699 700 701 702
    {
        const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );

        return ControlLockedSetTime( p_out, i_date );
    }
703
    case ES_OUT_SET_FRAME_NEXT:
704 705 706
    {
        return ControlLockedSetFrameNext( p_out );
    }
707 708 709 710
    case ES_OUT_GET_PCR_SYSTEM:
    {
        if( p_sys->b_delayed )
            return VLC_EGENERIC;
711

712
        mtime_t *pi_system = (mtime_t*)va_arg( args, mtime_t * );
713 714
        mtime_t *pi_delay  = (mtime_t*)va_arg( args, mtime_t * );
        return es_out_ControlGetPcrSystem( p_sys->p_out, pi_system, pi_delay );
715
    }
716 717 718 719 720 721 722 723 724 725
    case ES_OUT_MODIFY_PCR_SYSTEM:
    {
        const bool    b_absolute = va_arg( args, int );
        const mtime_t i_system   = va_arg( args, mtime_t );

        if( b_absolute && p_sys->b_delayed )
            return VLC_EGENERIC;

        return es_out_ControlModifyPcrSystem( p_sys->p_out, b_absolute, i_system );
    }
Laurent Aimar's avatar
Laurent Aimar committed
726 727 728 729 730 731
    case ES_OUT_GET_GROUP_FORCED:
    {
        int *pi_group = va_arg( args, int * );
        return es_out_Control( p_sys->p_out, ES_OUT_GET_GROUP_FORCED, pi_group );
    }

732 733
    default:
        msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
734 735 736 737 738 739 740 741
        // ft
    /* Invalid queries for this es_out level */
    case ES_OUT_SET_ES_BY_ID:
    case ES_OUT_RESTART_ES_BY_ID:
    case ES_OUT_SET_ES_DEFAULT_BY_ID:
    case ES_OUT_GET_ES_OBJECTS_BY_ID:
    case ES_OUT_SET_DELAY:
    case ES_OUT_SET_RECORD_STATE:
742
        vlc_assert_unreachable();
743 744 745 746 747 748 749 750 751
        return VLC_EGENERIC;
    }
}
static int Control( es_out_t *p_out, int i_query, va_list args )
{
    es_out_sys_t *p_sys = p_out->p_sys;
    int i_ret;

    vlc_mutex_lock( &p_sys->lock );
Laurent Aimar's avatar
Laurent Aimar committed
752 753 754

    TsAutoStop( p_out );

755
    i_ret = ControlLocked( p_out, i_query, args );
Laurent Aimar's avatar
Laurent Aimar committed
756

757 758 759 760 761
    vlc_mutex_unlock( &p_sys->lock );

    return i_ret;
}

762 763 764
/*****************************************************************************
 *
 *****************************************************************************/
765
static void TsDestroy( ts_thread_t *p_ts )
766 767 768
{
    vlc_cond_destroy( &p_ts->wait );
    vlc_mutex_destroy( &p_ts->lock );
769
    free( p_ts );
770
}
771 772 773
static int TsStart( es_out_t *p_out )
{
    es_out_sys_t *p_sys = p_out->p_sys;
774
    ts_thread_t *p_ts;
775 776 777

    assert( !p_sys->b_delayed );

778
    p_sys->p_ts = p_ts = calloc(1, sizeof(*p_ts));
779
    if( !p_ts )
780 781
        return VLC_EGENERIC;

782 783
    p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
    p_ts->psz_tmp_path = p_sys->psz_tmp_path;
784 785 786 787 788 789 790 791 792 793
    p_ts->p_input = p_sys->p_input;
    p_ts->p_out = p_sys->p_out;
    vlc_mutex_init( &p_ts->lock );
    vlc_cond_init( &p_ts->wait );
    p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
    p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
    p_ts->i_rate_source = p_sys->i_input_rate_source;
    p_ts->i_rate        = p_sys->i_input_rate;
    p_ts->i_rate_date = -1;
    p_ts->i_rate_delay = 0;
794
    p_ts->i_buffering_delay = 0;
795
    p_ts->i_cmd_delay = 0;
796 797
    p_ts->p_storage_r = NULL;
    p_ts->p_storage_w = NULL;
798

799
    p_sys->b_delayed = true;
800
    if( vlc_clone( &p_ts->thread, TsRun, p_ts, VLC_THREAD_PRIORITY_INPUT ) )
801
    {
802
        msg_Err( p_sys->p_input, "cannot create timeshift thread" );
803

804
        TsDestroy( p_ts );
805 806 807 808 809 810 811

        p_sys->b_delayed = false;
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}
Laurent Aimar's avatar
Laurent Aimar committed
812 813 814 815
static void TsAutoStop( es_out_t *p_out )
{
    es_out_sys_t *p_sys = p_out->p_sys;

816
    if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_ts ) )
Laurent Aimar's avatar
Laurent Aimar committed
817 818 819
        return;

    msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
820
    TsStop( p_sys->p_ts );
Laurent Aimar's avatar
Laurent Aimar committed
821 822 823

    p_sys->b_delayed = false;
}
824
static void TsStop( ts_thread_t *p_ts )
825
{
826 827
    vlc_cancel( p_ts->thread );
    vlc_join( p_ts->thread, NULL );
828

829
    vlc_mutex_lock( &p_ts->lock );
Laurent Aimar's avatar
Laurent Aimar committed
830 831 832 833
    for( ;; )
    {
        ts_cmd_t cmd;

834
        if( TsPopCmdLocked( p_ts, &cmd, true ) )
Laurent Aimar's avatar
Laurent Aimar committed
835 836 837 838
            break;

        CmdClean( &cmd );
    }
839 840 841
    assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
    if( p_ts->p_storage_r )
        TsStorageDelete( p_ts->p_storage_r );
842
    vlc_mutex_unlock( &p_ts->lock );
Laurent Aimar's avatar
Laurent Aimar committed
843

844
    TsDestroy( p_ts );
845
}
846
static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
847
{
848
    vlc_mutex_lock( &p_ts->lock );
849

850
    if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
851
    {
852
        ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
853

854 855 856
        if( !p_storage )
        {
            CmdClean( p_cmd );
857 858
            vlc_mutex_unlock( &p_ts->lock );
            /* TODO warn the user (but only once) */
859 860
            return;
        }
861

862 863 864 865 866 867
        if( !p_ts->p_storage_w )
        {
            p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
        }
        else
        {
868
            TsStoragePack( p_ts->p_storage_w );
869 870 871
            p_ts->p_storage_w->p_next = p_storage;
            p_ts->p_storage_w = p_storage;
        }
872
    }
873

874
    /* TODO return error and warn the user (but only once) */
875
    TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
876 877 878 879

    vlc_cond_signal( &p_ts->wait );

    vlc_mutex_unlock( &p_ts->lock );
880
}
881
static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
882
{
883
    vlc_assert_locked( &p_ts->lock );
884

885
    if( TsStorageIsEmpty( p_ts->p_storage_r ) )
886 887
        return VLC_EGENERIC;

888
    TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
889 890 891 892 893 894 895 896 897 898

    while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
    {
        ts_storage_t *p_next = p_ts->p_storage_r->p_next;
        if( !p_next )
            break;

        TsStorageDelete( p_ts->p_storage_r );
        p_ts->p_storage_r = p_next;
    }
899 900 901

    return VLC_SUCCESS;
}
902
static bool TsHasCmd( ts_thread_t *p_ts )
903
{
904
    bool b_cmd;
905

906
    vlc_mutex_lock( &p_ts->lock );
907
    b_cmd =  TsStorageIsEmpty( p_ts->p_storage_r );
908 909 910 911
    vlc_mutex_unlock( &p_ts->lock );

    return b_cmd;
}
Laurent Aimar's avatar
Laurent Aimar committed
912 913 914 915 916 917 918
static bool TsIsUnused( ts_thread_t *p_ts )
{
    bool b_unused;

    vlc_mutex_lock( &p_ts->lock );
    b_unused = !p_ts->b_paused &&
               p_ts->i_rate == p_ts->i_rate_source &&
919
               TsStorageIsEmpty( p_ts->p_storage_r );
Laurent Aimar's avatar
Laurent Aimar committed
920 921 922 923
    vlc_mutex_unlock( &p_ts->lock );

    return b_unused;
}
924 925 926
static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
{
    vlc_mutex_lock( &p_ts->lock );
927 928 929 930 931

    int i_ret;
    if( b_paused )
    {
        assert( !b_source_paused );
932
        i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
933 934 935
    }
    else
    {
936
        i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
937 938 939 940 941 942
    }

    if( !i_ret )
    {
        if( !b_paused )
        {
943
            assert( p_ts->i_pause_date > 0 );
944

945
            p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
946 947
        }

948 949
        p_ts->b_paused = b_paused;
        p_ts->i_pause_date = i_date;
950

951
        vlc_cond_signal( &p_ts->wait );
952
    }
953
    vlc_mutex_unlock( &p_ts->lock );
954 955
    return i_ret;
}
956
static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
957
{
958
    int i_ret;
959

960 961
    vlc_mutex_lock( &p_ts->lock );
    p_ts->i_cmd_delay += p_ts->i_rate_delay;
962

963 964 965 966
    p_ts->i_rate_date = -1;
    p_ts->i_rate_delay = 0;
    p_ts->i_rate = i_rate;
    p_ts->i_rate_source = i_src_rate;
967

968 969
    i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
    vlc_mutex_unlock( &p_ts->lock );
970

971
    return i_ret;
972 973
}

974
static void *TsRun( void *p_data )
975
{
976
    ts_thread_t *p_ts = p_data;
977
    mtime_t i_buffering_date = -1;
978 979 980 981

    for( ;; )
    {
        ts_cmd_t cmd;
982
        mtime_t  i_deadline;
983
        bool b_buffering;
984

985
        /* Pop a command to execute */
986 987
        vlc_mutex_lock( &p_ts->lock );
        mutex_cleanup_push( &p_ts->lock );
988

989 990 991
        for( ;; )
        {
            const int canc = vlc_savecancel();
992
            b_buffering = es_out_GetBuffering( p_ts->p_out );
993

994
            if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
995 996
            {
                vlc_restorecancel( canc );
997
                break;
998 999
            }
            vlc_restorecancel( canc );
1000

1001
            vlc_cond_wait( &p_ts->wait, &p_ts->lock );
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
        }

        if( b_buffering && i_buffering_date < 0 )
        {
            i_buffering_date = cmd.i_date;
        }
        else if( i_buffering_date > 0 )
        {
            p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
            if( b_buffering )
                i_buffering_date = cmd.i_date;
            else
                i_buffering_date = -1;
        }
1016

1017 1018
        if( p_ts->i_rate_date < 0 )
            p_ts->i_rate_date = cmd.i_date;
1019

1020 1021
        p_ts->i_rate_delay = 0;
        if( p_ts->i_rate_source != p_ts->i_rate )
1022
        {
1023 1024
            const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
            p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
1025
        }
1026
        if( p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay < 0 && p_ts->i_rate != p_ts->i_rate_source )
1027
        {
1028 1029
            const int canc = vlc_savecancel();

1030
            /* Auto reset to rate 1.0 */
1031
            msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
1032

1033
            p_ts->i_cmd_delay = 0;
1034
            p_ts->i_buffering_delay = 0;
1035

1036
            p_ts->i_rate_delay = 0;
1037
            p_ts->i_rate_date = -1;
1038
            p_ts->i_rate = p_ts->i_rate_source;