input.c 98.3 KB
Newer Older
1 2
/*****************************************************************************
 * input.c: input thread
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1998-2007 VLC authors and VideoLAN
5
 * $Id$
6
 *
7
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8
 *          Laurent Aimar <fenrir@via.ecp.fr>
9
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * 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
13
 * (at your option) any later version.
14
 *
15 16
 * 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
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * 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.
23
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
24

25
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
26
 * Preamble
27
 *****************************************************************************/
28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33

Christophe Mutricy's avatar
Christophe Mutricy committed
34
#include <limits.h>
35
#include <assert.h>
36
#include <errno.h>
37
#include <math.h>
38
#include <sys/stat.h>
39

40
#include "input_internal.h"
41
#include "event.h"
42
#include "es_out.h"
43
#include "es_out_timeshift.h"
44
#include "access.h"
45
#include "demux.h"
46
#include "stream.h"
47
#include "item.h"
48
#include "resource.h"
49

Clément Stenac's avatar
Clément Stenac committed
50
#include <vlc_sout.h>
51
#include <vlc_dialog.h>
Clément Stenac's avatar
Clément Stenac committed
52 53
#include <vlc_url.h>
#include <vlc_charset.h>
54
#include <vlc_fs.h>
55
#include <vlc_strings.h>
56
#include <vlc_modules.h>
57

58
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
59
 * Local prototypes
60
 *****************************************************************************/
61
static  void *Run            ( void * );
62

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
63
static input_thread_t * Create  ( vlc_object_t *, input_item_t *,
64
                                  const char *, bool, input_resource_t * );
65
static  int             Init    ( input_thread_t *p_input );
66
static void             End     ( input_thread_t *p_input );
67
static void             MainLoop( input_thread_t *p_input, bool b_interactive );
68

69
static inline int ControlPop( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline, bool b_postpone_seek );
70
static void       ControlRelease( int i_type, vlc_value_t val );
71
static bool       ControlIsSeekRequest( int i_type );
72
static bool       Control( input_thread_t *, int, vlc_value_t );
73
static void       ControlPause( input_thread_t *, mtime_t );
74

75 76
static int  UpdateTitleSeekpointFromDemux( input_thread_t * );
static void UpdateGenericFromDemux( input_thread_t * );
77
static void UpdateTitleListfromDemux( input_thread_t * );
Michel Kaempf's avatar
Michel Kaempf committed
78

79
static void MRLSections( const char *, int *, int *, int *, int *);
80 81

static input_source_t *InputSourceNew( input_thread_t *);
82
static int  InputSourceInit( input_thread_t *, input_source_t *,
83 84
                             const char *, const char *psz_forced_demux,
                             bool b_in_can_fail );
85
static void InputSourceClean( input_source_t * );
Laurent Aimar's avatar
Laurent Aimar committed
86 87
static void InputSourceMeta( input_thread_t *, input_source_t *, vlc_meta_t * );

88 89
/* TODO */
//static void InputGetAttachments( input_thread_t *, input_source_t * );
90
static void SlaveDemux( input_thread_t *p_input );
91
static void SlaveSeek( input_thread_t *p_input );
92

93
static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
94
static void InputUpdateMeta( input_thread_t *p_input, demux_t *p_demux );
95 96 97
static void InputGetExtraFiles( input_thread_t *p_input,
                                int *pi_list, char ***pppsz_list,
                                const char *psz_access, const char *psz_path );
98

99 100
static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment, demux_t ***ppp_attachment_demux,
                              int i_new, input_attachment_t **pp_new, demux_t *p_demux );
101

102 103 104
enum {
    SUB_NOFLAG = 0x00,
    SUB_FORCED = 0x01,
105
    SUB_CANFAIL = 0x02,
106
};
107

108 109
static void input_SubtitleAdd( input_thread_t *, const char *, unsigned );
static void input_SubtitleFileAdd( input_thread_t *, char *, unsigned );
110 111
static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */

112
#undef input_Create
113 114 115 116 117 118 119 120 121 122 123 124
/**
 * Create a new input_thread_t.
 *
 * You need to call input_Start on it when you are done
 * adding callback on the variables/events you want to monitor.
 *
 * \param p_parent a vlc_object
 * \param p_item an input item
 * \param psz_log an optional prefix for this input logs
 * \param p_resource an optional input ressource
 * \return a pointer to the spawned input thread
 */
125 126 127
input_thread_t *input_Create( vlc_object_t *p_parent,
                              input_item_t *p_item,
                              const char *psz_log, input_resource_t *p_resource )
128 129 130 131
{
    return Create( p_parent, p_item, psz_log, false, p_resource );
}

132
#undef input_Read
133
/**
134
 * Initialize an input thread and run it until it stops by itself.
135 136 137 138 139
 *
 * \param p_parent a vlc_object
 * \param p_item an input item
 * \return an error code, VLC_SUCCESS on success
 */
140
int input_Read( vlc_object_t *p_parent, input_item_t *p_item )
141
{
142
    input_thread_t *p_input = Create( p_parent, p_item, NULL, false, NULL );
143 144 145
    if( !p_input )
        return VLC_EGENERIC;

146 147
    if( !Init( p_input ) )
    {
148
        MainLoop( p_input, false );
149 150 151 152
        End( p_input );
    }

    vlc_object_release( p_input );
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    return VLC_SUCCESS;
}

/**
 * Initialize an input and initialize it to preparse the item
 * This function is blocking. It will only accept parsing regular files.
 *
 * \param p_parent a vlc_object_t
 * \param p_item an input item
 * \return VLC_SUCCESS or an error
 */
int input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
{
    input_thread_t *p_input;

    /* Allocate descriptor */
    p_input = Create( p_parent, p_item, NULL, true, NULL );
    if( !p_input )
        return VLC_EGENERIC;

173 174 175 176 177
    if( !Init( p_input ) ) {
        /* if the demux is a playlist, call Mainloop that will call
         * demux_Demux in order to fetch sub items */
        bool b_is_playlist = false;

178 179
        if ( input_item_ShouldPreparseSubItems( p_item )
          && demux_Control( p_input->p->input.p_demux,
180 181 182 183 184
                            DEMUX_IS_PLAYLIST,
                            &b_is_playlist ) )
            b_is_playlist = false;
        if( b_is_playlist )
            MainLoop( p_input, false );
185
        End( p_input );
186
    }
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

    vlc_object_release( p_input );

    return VLC_SUCCESS;
}

/**
 * Start a input_thread_t created by input_Create.
 *
 * You must not start an already running input_thread_t.
 *
 * \param the input thread to start
 */
int input_Start( input_thread_t *p_input )
{
202
    assert( !p_input->p->is_running );
203
    /* Create thread and wait for its readiness. */
204 205 206
    p_input->p->is_running = !vlc_clone( &p_input->p->thread,
                                         Run, p_input, VLC_THREAD_PRIORITY_INPUT );
    if( !p_input->p->is_running )
207 208 209 210 211 212 213 214 215 216 217 218 219
    {
        input_ChangeState( p_input, ERROR_S );
        msg_Err( p_input, "cannot create input thread" );
        return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}

/**
 * Request a running input thread to stop and die
 *
 * \param p_input the input thread to stop
 */
220
void input_Stop( input_thread_t *p_input )
221 222
{
    /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
223
     */
224
    ObjectKillChildrens( VLC_OBJECT(p_input) );
225 226 227 228 229 230 231 232 233 234 235 236 237 238

    input_thread_private_t *sys = p_input->p;

    vlc_mutex_lock( &sys->lock_control );
    /* Discard all pending controls */
    for( int i = 0; i < sys->i_control; i++ )
    {
        input_control_t *ctrl = &sys->control[i];
        ControlRelease( ctrl->i_type, ctrl->val );
    }
    sys->i_control = 0;
    sys->is_stopped = true;
    vlc_cond_signal( &sys->wait_control );
    vlc_mutex_unlock( &sys->lock_control );
239 240
}

Laurent Aimar's avatar
Laurent Aimar committed
241 242 243 244 245
/**
 * Close an input
 *
 * It does not call input_Stop itself.
 */
246
void input_Close( input_thread_t *p_input )
Laurent Aimar's avatar
Laurent Aimar committed
247
{
248 249 250
    if( p_input->p->is_running )
        vlc_join( p_input->p->thread, NULL );
    vlc_object_release( p_input );
Laurent Aimar's avatar
Laurent Aimar committed
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
/**
 * Input destructor (called when the object's refcount reaches 0).
 */
static void input_Destructor( vlc_object_t *obj )
{
    input_thread_t *p_input = (input_thread_t *)obj;
#ifndef NDEBUG
    char * psz_name = input_item_GetName( p_input->p->p_item );
    msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
    free( psz_name );
#endif

    if( p_input->p->p_es_out_display )
        es_out_Delete( p_input->p->p_es_out_display );

    if( p_input->p->p_resource )
        input_resource_Release( p_input->p->p_resource );
    if( p_input->p->p_resource_private )
        input_resource_Release( p_input->p->p_resource_private );

    vlc_gc_decref( p_input->p->p_item );

    vlc_mutex_destroy( &p_input->p->counters.counters_lock );

    for( int i = 0; i < p_input->p->i_control; i++ )
    {
        input_control_t *p_ctrl = &p_input->p->control[i];
        ControlRelease( p_ctrl->i_type, p_ctrl->val );
    }

    vlc_cond_destroy( &p_input->p->wait_control );
    vlc_mutex_destroy( &p_input->p->lock_control );
    free( p_input->p );
}

288 289 290 291 292 293 294 295 296 297 298 299
/**
 * Get the item from an input thread
 * FIXME it does not increase ref count of the item.
 * if it is used after p_input is destroyed nothing prevent it from
 * being freed.
 */
input_item_t *input_GetItem( input_thread_t *p_input )
{
    assert( p_input && p_input->p );
    return p_input->p->p_item;
}

300
/*****************************************************************************
301 302
 * This function creates a new input, and returns a pointer
 * to its description. On error, it returns NULL.
303
 *
Laurent Aimar's avatar
Laurent Aimar committed
304
 * XXX Do not forget to update vlc_input.h if you add new variables.
305
 *****************************************************************************/
306
static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
307
                               const char *psz_header, bool b_quick,
308
                               input_resource_t *p_resource )
Michel Kaempf's avatar
Michel Kaempf committed
309
{
310
    input_thread_t *p_input = NULL;                 /* thread descriptor */
311

312
    /* Allocate descriptor */
313
    p_input = vlc_custom_create( p_parent, sizeof( *p_input ), "input" );
314
    if( p_input == NULL )
315
        return NULL;
316 317 318 319 320 321

    /* Construct a nice name for the input timer */
    char psz_timer_name[255];
    char * psz_name = input_item_GetName( p_item );
    snprintf( psz_timer_name, sizeof(psz_timer_name),
              "input launching for '%s'", psz_name );
322 323 324

    msg_Dbg( p_input, "Creating an input for '%s'", psz_name);

325 326
    free( psz_name );

327 328
    p_input->p = calloc( 1, sizeof( input_thread_private_t ) );
    if( !p_input->p )
329 330
    {
        vlc_object_release( p_input );
331
        return NULL;
332
    }
333

334
    /* Parse input options */
335
    input_item_ApplyOptions( VLC_OBJECT(p_input), p_item );
336

337
    p_input->b_preparsing = b_quick;
338
    p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
339

340
    /* Init Common fields */
341
    p_input->p->b_can_pace_control = true;
Clément Stenac's avatar
Clément Stenac committed
342
    p_input->p->i_start = 0;
Laurent Aimar's avatar
Laurent Aimar committed
343
    p_input->p->i_time  = 0;
Clément Stenac's avatar
Clément Stenac committed
344
    p_input->p->i_stop  = 0;
345
    p_input->p->i_run   = 0;
Clément Stenac's avatar
Clément Stenac committed
346
    p_input->p->i_title = 0;
347
    p_input->p->title = NULL;
Clément Stenac's avatar
Clément Stenac committed
348
    p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
Laurent Aimar's avatar
Laurent Aimar committed
349
    p_input->p->i_state = INIT_S;
350 351
    p_input->p->is_running = false;
    p_input->p->is_stopped = false;
352
    p_input->p->b_recording = false;
353
    p_input->p->i_rate = INPUT_RATE_DEFAULT;
354 355
    memset( &p_input->p->bookmark, 0, sizeof(p_input->p->bookmark) );
    TAB_INIT( p_input->p->i_bookmark, p_input->p->pp_bookmark );
356
    TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
357
    p_input->p->attachment_demux = NULL;
358
    p_input->p->p_sout   = NULL;
359
    p_input->p->b_out_pace_control = false;
360

Pierre's avatar
Pierre committed
361
    vlc_gc_incref( p_item ); /* Released in Destructor() */
362 363 364
    p_input->p->p_item = p_item;

    /* Init Input fields */
Clément Stenac's avatar
Clément Stenac committed
365
    p_input->p->input.p_demux  = NULL;
366
    p_input->p->input.b_title_demux = false;
Clément Stenac's avatar
Clément Stenac committed
367 368 369
    p_input->p->input.i_title  = 0;
    p_input->p->input.title    = NULL;
    p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
370 371 372 373
    p_input->p->input.b_can_pace_control = true;
    p_input->p->input.b_can_rate_control = true;
    p_input->p->input.b_rescale_ts = true;
    p_input->p->input.b_eof = false;
Clément Stenac's avatar
Clément Stenac committed
374

375
    vlc_mutex_lock( &p_item->lock );
376 377

    if( !p_item->p_stats )
378
        p_item->p_stats = stats_NewInputStats( p_input );
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

    /* setup the preparse depth of the item
     * if we are preparsing, use the i_preparse_depth of the parent item */
    if( !p_input->b_preparsing )
    {
        char *psz_rec = var_InheritString( p_parent, "recursive" );

        if( psz_rec != NULL )
        {
            if ( !strcasecmp( psz_rec, "none" ) )
                p_item->i_preparse_depth = 0;
            else if ( !strcasecmp( psz_rec, "collapse" ) )
                p_item->i_preparse_depth = 1;
            else
                p_item->i_preparse_depth = -1; /* default is expand */
            free (psz_rec);
        } else
            p_item->i_preparse_depth = -1;
    }

399
    vlc_mutex_unlock( &p_item->lock );
400

401
    /* No slave */
Clément Stenac's avatar
Clément Stenac committed
402 403
    p_input->p->i_slave = 0;
    p_input->p->slave   = NULL;
404

405
    /* */
406
    if( p_resource )
407 408
    {
        p_input->p->p_resource_private = NULL;
409
        p_input->p->p_resource = input_resource_Hold( p_resource );
410
    }
411
    else
412 413
    {
        p_input->p->p_resource_private = input_resource_New( VLC_OBJECT( p_input ) );
414
        p_input->p->p_resource = input_resource_Hold( p_input->p->p_resource_private );
415
    }
416
    input_resource_SetInput( p_input->p->p_resource, p_input );
417

418
    /* Init control buffer */
419
    vlc_mutex_init( &p_input->p->lock_control );
420
    vlc_cond_init( &p_input->p->wait_control );
Clément Stenac's avatar
Clément Stenac committed
421
    p_input->p->i_control = 0;
422

423 424
    /* Create Object Variables for private use only */
    input_ConfigVarInit( p_input );
Michel Kaempf's avatar
Michel Kaempf committed
425

426
    /* Create Objects variables for public Get and Set */
427
    input_ControlVarInit( p_input );
428

429
    /* */
430
    if( !p_input->b_preparsing )
Gildas Bazin's avatar
Gildas Bazin committed
431
    {
432 433
        char *psz_bookmarks = var_GetNonEmptyString( p_input, "bookmarks" );
        if( psz_bookmarks )
Gildas Bazin's avatar
Gildas Bazin committed
434
        {
435 436
            /* FIXME: have a common cfg parsing routine used by sout and others */
            char *psz_parser, *psz_start, *psz_end;
437
            psz_parser = psz_bookmarks;
438
            while( (psz_start = strchr( psz_parser, '{' ) ) )
Gildas Bazin's avatar
Gildas Bazin committed
439
            {
440
                 seekpoint_t *p_seekpoint;
441 442 443 444 445 446 447 448
                 char backup;
                 psz_start++;
                 psz_end = strchr( psz_start, '}' );
                 if( !psz_end ) break;
                 psz_parser = psz_end + 1;
                 backup = *psz_parser;
                 *psz_parser = 0;
                 *psz_end = ',';
449 450

                 p_seekpoint = vlc_seekpoint_New();
451 452 453 454 455
                 while( (psz_end = strchr( psz_start, ',' ) ) )
                 {
                     *psz_end = 0;
                     if( !strncmp( psz_start, "name=", 5 ) )
                     {
456
                         p_seekpoint->psz_name = strdup(psz_start + 5);
457 458 459 460 461 462 463
                     }
                     else if( !strncmp( psz_start, "bytes=", 6 ) )
                     {
                         p_seekpoint->i_byte_offset = atoll(psz_start + 6);
                     }
                     else if( !strncmp( psz_start, "time=", 5 ) )
                     {
464
                         p_seekpoint->i_time_offset = atoll(psz_start + 5) *
Ilkka Ollakka's avatar
Ilkka Ollakka committed
465
                                                        CLOCK_FREQ;
466 467
                     }
                     psz_start = psz_end + 1;
Gildas Bazin's avatar
Gildas Bazin committed
468
                }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
469
                msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
470 471 472 473 474
                                  p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
                                  p_seekpoint->i_time_offset );
                input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
                vlc_seekpoint_Delete( p_seekpoint );
                *psz_parser = backup;
Gildas Bazin's avatar
Gildas Bazin committed
475
            }
476
            free( psz_bookmarks );
Gildas Bazin's avatar
Gildas Bazin committed
477 478 479
        }
    }

480
    /* Remove 'Now playing' info as it is probably outdated */
481
    input_item_SetNowPlaying( p_item, NULL );
482
    input_item_SetESNowPlaying( p_item, NULL );
483
    input_SendEventMeta( p_input );
484

485 486 487 488
    /* */
    if( p_input->b_preparsing )
        p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;

489 490 491 492
    /* Make sure the interaction option is honored */
    if( !var_InheritBool( p_input, "interact" ) )
        p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;

493
    /* */
494
    memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
495
    vlc_mutex_init( &p_input->p->counters.counters_lock );
496

497 498 499
    p_input->p->p_es_out_display = input_EsOutNew( p_input, p_input->p->i_rate );
    p_input->p->p_es_out = NULL;

500
    /* Set the destructor when we are sure we are initialized */
501
    vlc_object_set_destructor( p_input, input_Destructor );
502

503 504 505
    return p_input;
}

506
/*****************************************************************************
507
 * Run: main thread loop
508 509
 * This is the "normal" thread that spawns the input processing chain,
 * reads the stream, cleans up and waits
510
 *****************************************************************************/
511
static void *Run( void *obj )
Michel Kaempf's avatar
Michel Kaempf committed
512
{
513
    input_thread_t *p_input = (input_thread_t *)obj;
514

515 516 517
    if( !Init( p_input ) )
    {
        MainLoop( p_input, true ); /* FIXME it can be wrong (like with VLM) */
518

519 520 521
        /* Clean up */
        End( p_input );
    }
522

523
    input_SendEventDead( p_input );
524
    return NULL;
525 526
}

527 528 529 530 531 532 533 534 535 536 537
bool input_Stopped( input_thread_t *input )
{
    input_thread_private_t *sys = input->p;
    bool ret;

    vlc_mutex_lock( &sys->lock_control );
    ret = sys->is_stopped;
    vlc_mutex_unlock( &sys->lock_control );
    return ret;
}

538 539 540
/*****************************************************************************
 * Main loop: Fill buffers from access, and demux
 *****************************************************************************/
541 542 543 544 545

/**
 * MainLoopDemux
 * It asks the demuxer to demux some data
 */
546
static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t i_start_mdate )
547
{
548
    int i_ret;
549

550
    *pb_changed = false;
551

Laurent Aimar's avatar
Laurent Aimar committed
552
    if( ( p_input->p->i_stop > 0 && p_input->p->i_time >= p_input->p->i_stop ) ||
553
        ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
554 555
        i_ret = 0; /* EOF */
    else
Laurent Aimar's avatar
Laurent Aimar committed
556
        i_ret = demux_Demux( p_input->p->input.p_demux );
557 558

    if( i_ret > 0 )
559
    {
560
        if( p_input->p->input.p_demux->info.i_update )
561
        {
562 563 564 565 566
            if( p_input->p->input.p_demux->info.i_update & INPUT_UPDATE_TITLE_LIST )
            {
                UpdateTitleListfromDemux( p_input );
                p_input->p->input.p_demux->info.i_update &= ~INPUT_UPDATE_TITLE_LIST;
            }
567 568 569 570 571 572
            if( p_input->p->input.b_title_demux )
            {
                i_ret = UpdateTitleSeekpointFromDemux( p_input );
                *pb_changed = true;
            }
            UpdateGenericFromDemux( p_input );
573 574
        }
    }
575

576 577
    if( i_ret == 0 )    /* EOF */
    {
578 579
        msg_Dbg( p_input, "EOF reached" );
        p_input->p->input.b_eof = true;
580
        es_out_Eos(p_input->p->p_es_out);
581 582 583 584 585 586 587
    }
    else if( i_ret < 0 )
    {
        input_ChangeState( p_input, ERROR_S );
    }

    if( i_ret > 0 && p_input->p->i_slave > 0 )
588
        SlaveDemux( p_input );
589 590
}

591 592 593
static int MainLoopTryRepeat( input_thread_t *p_input, mtime_t *pi_start_mdate )
{
    int i_repeat = var_GetInteger( p_input, "input-repeat" );
594
    if( i_repeat <= 0 )
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
        return VLC_EGENERIC;

    vlc_value_t val;

    msg_Dbg( p_input, "repeating the same input (%d)", i_repeat );
    if( i_repeat > 0 )
    {
        i_repeat--;
        var_SetInteger( p_input, "input-repeat", i_repeat );
    }

    /* Seek to start title/seekpoint */
    val.i_int = p_input->p->input.i_title_start -
        p_input->p->input.i_title_offset;
    if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
        val.i_int = 0;
    input_ControlPush( p_input,
                       INPUT_CONTROL_SET_TITLE, &val );

    val.i_int = p_input->p->input.i_seekpoint_start -
        p_input->p->input.i_seekpoint_offset;
    if( val.i_int > 0 /* TODO: check upper boundary */ )
        input_ControlPush( p_input,
                           INPUT_CONTROL_SET_SEEKPOINT, &val );

    /* Seek to start position */
    if( p_input->p->i_start > 0 )
    {
623
        val.i_int = p_input->p->i_start;
624 625 626 627
        input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &val );
    }
    else
    {
628
        val.f_float = 0.f;
629 630 631 632 633 634 635 636
        input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION, &val );
    }

    /* */
    *pi_start_mdate = mdate();
    return VLC_SUCCESS;
}

637
/**
638
 * Update timing infos and statistics.
639
 */
640
static void MainLoopStatistics( input_thread_t *p_input )
641
{
642 643 644
    double f_position = 0.0;
    mtime_t i_time = 0;
    mtime_t i_length = 0;
645 646

    /* update input status variables */
647
    if( demux_Control( p_input->p->input.p_demux,
648 649
                       DEMUX_GET_POSITION, &f_position ) )
        f_position = 0.0;
650

651
    if( demux_Control( p_input->p->input.p_demux,
652 653
                       DEMUX_GET_TIME, &i_time ) )
        i_time = 0;
Laurent Aimar's avatar
Laurent Aimar committed
654
    p_input->p->i_time = i_time;
655 656

    if( demux_Control( p_input->p->input.p_demux,
657 658
                       DEMUX_GET_LENGTH, &i_length ) )
        i_length = 0;
659

660
    es_out_SetTimes( p_input->p->p_es_out, f_position, i_time, i_length );
661 662 663 664

    /* update current bookmark */
    vlc_mutex_lock( &p_input->p->p_item->lock );
    p_input->p->bookmark.i_time_offset = i_time;
665
    p_input->p->bookmark.i_byte_offset = -1;
666
    vlc_mutex_unlock( &p_input->p->p_item->lock );
667

668
    stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
669
    input_SendEventStatistics( p_input );
670 671 672 673 674 675
}

/**
 * MainLoop
 * The main input loop.
 */
676
static void MainLoop( input_thread_t *p_input, bool b_interactive )
677 678
{
    mtime_t i_start_mdate = mdate();
679
    mtime_t i_intf_update = 0;
680
    mtime_t i_last_seek_mdate = 0;
681 682 683 684

    if( b_interactive && var_InheritBool( p_input, "start-paused" ) )
        ControlPause( p_input, i_start_mdate );

685
    bool b_pause_after_eof = b_interactive &&
686
                             var_InheritBool( p_input, "play-and-pause" );
687

688
    while( !input_Stopped( p_input ) && p_input->p->i_state != ERROR_S )
689
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
690
        mtime_t i_wakeup = -1;
691
        bool b_paused = p_input->p->i_state == PAUSE_S;
Laurent Aimar's avatar
Laurent Aimar committed
692
        /* FIXME if p_input->p->i_state == PAUSE_S the access/access_demux
693 694
         * is paused -> this may cause problem with some of them
         * The same problem can be seen when seeking while paused */
695 696
        if( b_paused )
            b_paused = !es_out_GetBuffering( p_input->p->p_es_out ) || p_input->p->input.b_eof;
697 698

        if( !b_paused )
699
        {
700 701
            if( !p_input->p->input.b_eof )
            {
702
                bool b_force_update = false;
703

704
                MainLoopDemux( p_input, &b_force_update, i_start_mdate );
705
                i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
706 707 708

                if( b_force_update )
                    i_intf_update = 0;
709
            }
710
            else if( !es_out_GetEmpty( p_input->p->p_es_out ) )
711 712 713 714
            {
                msg_Dbg( p_input, "waiting decoder fifos to empty" );
                i_wakeup = mdate() + INPUT_IDLE_SLEEP;
            }
715 716 717
            /* Pause after eof only if the input is pausable.
             * This way we won't trigger timeshifting for nothing */
            else if( b_pause_after_eof && p_input->p->b_can_pause )
718
            {
719 720
                vlc_value_t val = { .i_int = PAUSE_S };

721 722 723 724 725
                msg_Dbg( p_input, "pausing at EOF (pause after each)");
                Control( p_input, INPUT_CONTROL_SET_STATE, val );

                b_paused = true;
            }
726 727
            else
            {
728 729
                if( MainLoopTryRepeat( p_input, &i_start_mdate ) )
                    break;
730
            }
731 732 733 734 735 736 737 738

            /* Update interface and statistics */
            mtime_t now = mdate();
            if( now >= i_intf_update )
            {
                MainLoopStatistics( p_input );
                i_intf_update = now + INT64_C(250000);
            }
739
        }
740

741 742 743
        /* Handle control */
        for( ;; )
        {
744
            mtime_t i_deadline = i_wakeup;
745 746 747 748 749 750

            /* Postpone seeking until ES buffering is complete or at most
             * 125 ms. */
            bool b_postpone = es_out_GetBuffering( p_input->p->p_es_out )
                            && !p_input->p->input.b_eof;
            if( b_postpone )
751
            {
752 753 754 755
                mtime_t now = mdate();

                /* Recheck ES buffer level every 20 ms when seeking */
                if( now < i_last_seek_mdate + INT64_C(125000)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
756
                 && (i_deadline < 0 || i_deadline > now + INT64_C(20000)) )
757 758 759 760
                    i_deadline = now + INT64_C(20000);
                else
                    b_postpone = false;
            }
761

762 763
            int i_type;
            vlc_value_t val;
764

765 766 767 768 769 770
            if( ControlPop( p_input, &i_type, &val, i_deadline, b_postpone ) )
            {
                if( b_postpone )
                    continue;
                break; /* Wake-up time reached */
            }
771

772
#ifndef NDEBUG
773
            msg_Dbg( p_input, "control type=%d", i_type );
774
#endif
775 776 777 778 779
            if( Control( p_input, i_type, val ) )
            {
                if( ControlIsSeekRequest( i_type ) )
                    i_last_seek_mdate = mdate();
                i_intf_update = 0;
780
            }
781

782
            /* Update the wakeup time */
783
            if( i_wakeup != 0 )
784
                i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
785
        }
786
    }
787 788
}

789
static void InitStatistics( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
790
{
791
    if( p_input->b_preparsing ) return;
792

793
    /* Prepare statistics */
794 795
#define INIT_COUNTER( c, compute ) p_input->p->counters.p_##c = \
 stats_CounterCreate( STATS_##compute);
796
    if( libvlc_stats( p_input ) )
797
    {
798 799 800 801 802 803 804 805 806 807 808 809 810 811
        INIT_COUNTER( read_bytes, COUNTER );
        INIT_COUNTER( read_packets, COUNTER );
        INIT_COUNTER( demux_read, COUNTER );
        INIT_COUNTER( input_bitrate, DERIVATIVE );
        INIT_COUNTER( demux_bitrate, DERIVATIVE );
        INIT_COUNTER( demux_corrupted, COUNTER );
        INIT_COUNTER( demux_discontinuity, COUNTER );
        INIT_COUNTER( played_abuffers, COUNTER );
        INIT_COUNTER( lost_abuffers, COUNTER );
        INIT_COUNTER( displayed_pictures, COUNTER );
        INIT_COUNTER( lost_pictures, COUNTER );
        INIT_COUNTER( decoded_audio, COUNTER );
        INIT_COUNTER( decoded_video, COUNTER );
        INIT_COUNTER( decoded_sub, COUNTER );
812 813 814
        p_input->p->counters.p_sout_send_bitrate = NULL;
        p_input->p->counters.p_sout_sent_packets = NULL;
        p_input->p->counters.p_sout_sent_bytes = NULL;
815
    }
816
}
817

818
#ifdef ENABLE_SOUT
819 820
static int InitSout( input_thread_t * p_input )
{
821 822
    if( p_input->b_preparsing )
        return VLC_SUCCESS;
823 824

    /* Find a usable sout and attach it to p_input */
825
    char *psz = var_GetNonEmptyString( p_input, "sout" );
826
    if( psz && strncasecmp( p_input->p->p_item->psz_uri, "vlc:", 4 ) )
827
    {
828
        p_input->p->p_sout  = input_resource_RequestSout( p_input->p->p_resource, NULL, psz );
829
        if( !p_input->p->p_sout )
Clément Stenac's avatar
Clément Stenac committed
830
        {
831 832 833 834 835
            input_ChangeState( p_input, ERROR_S );
            msg_Err( p_input, "cannot start stream output instance, " \
                              "aborting" );
            free( psz );
            return VLC_EGENERIC;
836
        }
837
        if( libvlc_stats( p_input ) )
838
        {
839 840 841
            INIT_COUNTER( sout_sent_packets, COUNTER );
            INIT_COUNTER( sout_sent_bytes, COUNTER );
            INIT_COUNTER( sout_send_bitrate, DERIVATIVE );
842
        }
843
    }
844
    else
845
    {
846
        input_resource_RequestSout( p_input->p->p_resource, NULL, NULL );
847
    }
848
    free( psz );
849

850 851
    return VLC_SUCCESS;
}
852
#endif
853

854 855
static void InitTitle( input_thread_t * p_input )
{
Laurent Aimar's avatar
Laurent Aimar committed
856
    input_source_t *p_master = &p_input->p->input;
857

Laurent Aimar's avatar
Laurent Aimar committed
858 859
    if( p_input->b_preparsing )
        return;
860

861
    vlc_mutex_lock( &p_input->p->p_item->lock );
862
    /* Create global title (from master) */
Laurent Aimar's avatar
Laurent Aimar committed
863 864 865 866
    p_input->p->i_title = p_master->i_title;
    p_input->p->title   = p_master->title;
    p_input->p->i_title_offset = p_master->i_title_offset;
    p_input->p->i_seekpoint_offset = p_master->i_seekpoint_offset;
867 868 869 870
    if( p_input->p->i_title > 0 )
    {
        /* Setup variables */
        input_ControlVarNavigation( p_input );
871
        input_SendEventTitle( p_input, 0 );
872 873 874
    }

    /* Global flag */
875
    p_input->p->b_can_pace_control    = p_master->b_can_pace_control;
Laurent Aimar's avatar
Laurent Aimar committed
876 877
    p_input->p->b_can_pause        = p_master->b_can_pause;
    p_input->p->b_can_rate_control = p_master->b_can_rate_control;
878
    vlc_mutex_unlock( &p_input->p->p_item->lock );
879
}
880

881 882 883
static void StartTitle( input_thread_t * p_input )
{
    vlc_value_t val;
884

885 886 887 888 889
    /* Start title/chapter */
    val.i_int = p_input->p->input.i_title_start -
                p_input->p->input.i_title_offset;
    if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
        input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
Laurent Aimar's avatar
Laurent Aimar committed
890

891 892 893 894 895
    val.i_int = p_input->p->input.i_seekpoint_start -
                p_input->p->input.i_seekpoint_offset;
    if( val.i_int > 0 /* TODO: check upper boundary */ )
        input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );

Laurent Aimar's avatar
Laurent Aimar committed
896
    /* Start/stop/run time */
897
    p_input->p->i_start = llroundf(1000000.f
898
                                     * var_GetFloat( p_input, "start-time" ));
899
    p_input->p->i_stop  = llroundf(1000000.f
900
                                     * var_GetFloat( p_input, "stop-time" ));
901
    p_input->p->i_run   = llroundf(1000000.f
902
                                     * var_GetFloat( p_input, "run-time" ));
903
    if( p_input->p->i_run < 0 )
904
    {
905 906
        msg_Warn( p_input, "invalid run-time ignored" );
        p_input->p->i_run = 0;
Rafaël Carré's avatar
Rafaël Carré committed
907
    }
908

909
    if( p_input->p->i_start > 0 )
910
    {
911
        vlc_value_t s;
912

913
        msg_Dbg( p_input, "starting at time: %ds",
Ilkka Ollakka's avatar
Ilkka Ollakka committed
914
                 (int)( p_input->p->i_start / CLOCK_FREQ ) );
915

916
        s.i_int = p_input->p->i_start;
917
        input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
918 919 920 921 922 923
    }
    if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
    {
        msg_Warn( p_input, "invalid stop-time ignored" );
        p_input->p->i_stop = 0;
    }
924
    p_input->p->b_fast_seek = var_GetBool( p_input, "input-fast-seek" );
Laurent Aimar's avatar
Laurent Aimar committed
925
}
926

Laurent Aimar's avatar
Laurent Aimar committed
927 928
static void LoadSubtitles( input_thread_t *p_input )
{
929 930
    /* Load subtitles */
    /* Get fps and set it if not already set */
931 932
    const float f_fps = p_input->p->f_fps;
    if( f_fps > 1.f )
933 934 935
    {
        var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
        var_SetFloat( p_input, "sub-original-fps", f_fps );
936

937
        float f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
938
        if( f_requested_fps != f_fps )
939
        {
940 941 942
            var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
                                            VLC_VAR_DOINHERIT );
            var_SetFloat( p_input, "sub-fps", f_requested_fps );
943
        }
944
    }
945

Laurent Aimar's avatar
Laurent Aimar committed
946
    const int i_delay = var_CreateGetInteger( p_input, "sub-delay" );
947
    if( i_delay != 0 )
948
        var_SetInteger( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
949 950

    /* Look for and add subtitle files */
951
    unsigned i_flags = SUB_FORCED