input.c 40 KB
Newer Older
1 2
/*****************************************************************************
 * input.c: input thread
3
 * Read a stream, demultiplex and parse it before sending it to
Michel Kaempf's avatar
Michel Kaempf committed
4
 * decoders.
5
 *****************************************************************************
6
 * Copyright (C) 1998-2004 VideoLAN
Laurent Aimar's avatar
Laurent Aimar committed
7
 * $Id: input.c,v 1.286 2004/02/02 12:58:29 fenrir Exp $
8
 *
9
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
10 11 12 13 14
 *
 * 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.
15
 *
16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
20
 *
21 22 23
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
25

26
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
27
 * Preamble
28
 *****************************************************************************/
29
#include <stdlib.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
30

31
#include <vlc/vlc.h>
32
#include <vlc/input.h>
33
#include <vlc/decoder.h>
34
#include <vlc/vout.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
35

36 37
#ifdef HAVE_SYS_TIMES_H
#   include <sys/times.h>
38
#endif
39

40
#include "vlc_playlist.h"
41

42 43
#include "stream_output.h"

44
#include "vlc_interface.h"
45
#include "codecs.h"
46
#include "vlc_meta.h"
47
#include "../../modules/demux/util/sub.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
48

49
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
50
 * Local prototypes
51
 *****************************************************************************/
52 53 54 55 56 57 58
struct input_thread_sys_t
{
    /* subtitles */
    int              i_sub;
    subtitle_demux_t **sub;
};

Sam Hocevar's avatar
 
Sam Hocevar committed
59
static  int RunThread       ( input_thread_t *p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
60 61 62
static  int InitThread      ( input_thread_t *p_input );
static void ErrorThread     ( input_thread_t *p_input );
static void EndThread       ( input_thread_t *p_input );
Michel Kaempf's avatar
Michel Kaempf committed
63

Gildas Bazin's avatar
 
Gildas Bazin committed
64 65
static void ParseOption     ( input_thread_t *p_input,
                              const char *psz_option );
66

67 68 69 70 71 72 73 74 75 76 77
/*****************************************************************************
 * Callbacks
 *****************************************************************************/
static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data );
static int TimeCallback    ( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data );
static int StateCallback   ( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data );
static int RateCallback    ( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data );
Gildas Bazin's avatar
 
Gildas Bazin committed
78

79
/*****************************************************************************
80
 * input_CreateThread: creates a new input thread
81
 *****************************************************************************
82 83
 * This function creates a new input, and returns a pointer
 * to its description. On error, it returns NULL.
84
 *****************************************************************************/
85 86 87
input_thread_t *__input_CreateThread( vlc_object_t *p_parent, char *psz_uri,
                                      char **ppsz_options, int i_options )

Michel Kaempf's avatar
Michel Kaempf committed
88
{
89
    input_thread_t *    p_input;                        /* thread descriptor */
90
    vlc_value_t val;
91
    int i;
92

93
    /* Allocate descriptor */
94
    p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
95
    if( p_input == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
96
    {
97
        msg_Err( p_parent, "out of memory" );
98
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
99
    }
100

Gildas Bazin's avatar
 
Gildas Bazin committed
101
    /* Parse input options */
102
    for( i = 0; i < i_options; i++ )
Gildas Bazin's avatar
 
Gildas Bazin committed
103
    {
104 105
        msg_Dbg( p_input, "option: %s", ppsz_options[i] );
        ParseOption( p_input, ppsz_options[i] );
Gildas Bazin's avatar
 
Gildas Bazin committed
106 107
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
108 109 110 111 112
    /* Create a few object variables we'll need later on */
    var_Create( p_input, "video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_input, "audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_input, "audio-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
    var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
113 114
    var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
    var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
115
    var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
Gildas Bazin's avatar
 
Gildas Bazin committed
116 117

    var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
118
    var_Create( p_input, "sout-all",   VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
Gildas Bazin's avatar
 
Gildas Bazin committed
119 120
    var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
121 122
    var_Create( p_input, "sout-keep",  VLC_VAR_BOOL | VLC_VAR_DOINHERIT );

123 124 125
    /* decoders */
    var_Create( p_input, "minimize-threads", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );

126 127 128
    /* play status */

    /* position variable */
129 130
    var_Create( p_input, "position",  VLC_VAR_FLOAT );  /* position 0.0->1.0 */
    var_Create( p_input, "position-offset",  VLC_VAR_FLOAT );  /* relative */
131 132 133
    val.f_float = 0.0;
    var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "position", PositionCallback, NULL );
134
    var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
135 136 137

    /* time variable */
    var_Create( p_input, "time",  VLC_VAR_TIME );
138
    var_Create( p_input, "time-offset",  VLC_VAR_TIME );    /* relative */
139 140 141
    val.i_time = 0;
    var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "time", TimeCallback, NULL );
142
    var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    /* length variable */
    var_Create( p_input, "length",  VLC_VAR_TIME );
    val.i_time = 0;
    var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );

    /* rate variable */
    var_Create( p_input, "rate", VLC_VAR_INTEGER );
    var_Create( p_input, "rate-slower", VLC_VAR_VOID );
    var_Create( p_input, "rate-faster", VLC_VAR_VOID );
    val.i_int = DEFAULT_RATE;
    var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "rate", RateCallback, NULL );
    var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
    var_AddCallback( p_input, "rate-faster", RateCallback, NULL );

    /* state variable */
    var_Create( p_input, "state", VLC_VAR_INTEGER );
    val.i_int = INIT_S;
    var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "state", StateCallback, NULL );

165 166 167 168
    /* state variable */
    var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
    val.b_bool = VLC_FALSE;
    var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
169

170
    /* Initialize thread properties */
Sam Hocevar's avatar
 
Sam Hocevar committed
171
    p_input->b_eof      = 0;
172
    p_input->p_sys      = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
173 174

    /* Set target */
175
    p_input->psz_source = strdup( psz_uri );
Sam Hocevar's avatar
 
Sam Hocevar committed
176

177 178 179
    /* Stream */
    p_input->s = NULL;

180 181 182
    /* es out */
    p_input->p_es_out = NULL;

Stéphane Borel's avatar
 
Stéphane Borel committed
183
    /* Demux */
184 185 186 187
    p_input->p_demux   = NULL;
    p_input->pf_demux  = NULL;
    p_input->pf_rewind = NULL;
    p_input->pf_demux_control = NULL;
188
    p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
Stéphane Borel's avatar
 
Stéphane Borel committed
189 190

    /* Access */
191
    p_input->p_access = NULL;
192

Sam Hocevar's avatar
 
Sam Hocevar committed
193 194
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
195
    p_input->i_pts_delay = DEFAULT_PTS_DELAY;
Sam Hocevar's avatar
 
Sam Hocevar committed
196

197 198 199 200 201 202
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

    /* Set locks. */
203
    vlc_mutex_init( p_input, &p_input->stream.stream_lock );
204
    vlc_cond_init( p_input, &p_input->stream.stream_wait );
205
    vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
Michel Kaempf's avatar
Michel Kaempf committed
206

207
    /* Initialize stream description */
Stéphane Borel's avatar
 
Stéphane Borel committed
208
    p_input->stream.b_changed = 0;
209 210
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
211
    p_input->stream.i_pgrm_number = 0;
212
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
213
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
214
    p_input->stream.i_mux_rate = 0;
215
    p_input->stream.b_seekable = 0;
216
    p_input->stream.p_sout = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
217

Sam Hocevar's avatar
 
Sam Hocevar committed
218 219 220
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
 
Stéphane Borel committed
221 222
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
223
    p_input->stream.p_selected_area = NULL;
224
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
225

Sam Hocevar's avatar
 
Sam Hocevar committed
226 227 228 229
    p_input->stream.pp_selected_es = NULL;
    p_input->stream.p_removed_es = NULL;
    p_input->stream.p_newly_selected_es = NULL;

Sam Hocevar's avatar
 
Sam Hocevar committed
230
    /* By default there is one area in a stream */
Gildas Bazin's avatar
 
Gildas Bazin committed
231
    input_AddArea( p_input, 0, 1 );
232
    p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
Stéphane Borel's avatar
 
Stéphane Borel committed
233

234
    /* Initialize stream control properties. */
235
    p_input->stream.control.i_status = INIT_S;
236 237
    p_input->stream.control.i_rate = DEFAULT_RATE;
    p_input->stream.control.b_mute = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
238
    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale");
239

240
    msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
Michel Kaempf's avatar
Michel Kaempf committed
241

Gildas Bazin's avatar
 
Gildas Bazin committed
242 243 244 245 246
    /* Initialize input info */
    p_input->stream.p_info = NULL;
    p_input->stream.p_info = input_InfoCategory( p_input, _("General") );
    input_AddInfo( p_input->stream.p_info, _("Playlist Item"),
                   p_input->psz_source );
247
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
 
Sam Hocevar committed
248

Sam Hocevar's avatar
Sam Hocevar committed
249
    /* Create thread and wait for its readiness. */
250 251
    if( vlc_thread_create( p_input, "input", RunThread,
                           VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
Michel Kaempf's avatar
Michel Kaempf committed
252
    {
253
        msg_Err( p_input, "cannot create input thread" );
Gildas Bazin's avatar
 
Gildas Bazin committed
254
        input_DelInfo( p_input );
Michel Kaempf's avatar
Michel Kaempf committed
255
        free( p_input );
256
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
257
    }
258

259
    return p_input;
Michel Kaempf's avatar
Michel Kaempf committed
260 261
}

262
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
263
 * input_StopThread: mark an input thread as zombie
264
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
265
 * This function should not return until the thread is effectively cancelled.
266
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
267
void input_StopThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
268
{
Sam Hocevar's avatar
 
Sam Hocevar committed
269 270
    /* Make the thread exit from a possible vlc_cond_wait() */
    vlc_mutex_lock( &p_input->stream.stream_lock );
271 272
    /* Request thread destruction */
    p_input->b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
273

274 275
    vlc_cond_signal( &p_input->stream.stream_wait );
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
276 277 278 279 280 281 282 283 284 285
}

/*****************************************************************************
 * input_DestroyThread: mark an input thread as zombie
 *****************************************************************************
 * This function should not return until the thread is effectively cancelled.
 *****************************************************************************/
void input_DestroyThread( input_thread_t *p_input )
{
    /* Join the thread */
286
    vlc_thread_join( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
287 288 289

    /* Destroy Mutex locks */
    vlc_mutex_destroy( &p_input->stream.control.control_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
290
    vlc_cond_destroy( &p_input->stream.stream_wait );
Sam Hocevar's avatar
 
Sam Hocevar committed
291
    vlc_mutex_destroy( &p_input->stream.stream_lock );
Michel Kaempf's avatar
Michel Kaempf committed
292 293
}

294
/*****************************************************************************
295
 * RunThread: main thread loop
296
 *****************************************************************************
297
 * Thread in charge of processing the network packets and demultiplexing.
298
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
299
static int RunThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
300
{
301 302 303
    vlc_value_t  val;
    mtime_t      i_update_next = -1;

Sam Hocevar's avatar
Sam Hocevar committed
304 305 306
    /* Signal right now, otherwise we'll get stuck in a peek */
    vlc_thread_ready( p_input );

Sam Hocevar's avatar
 
Sam Hocevar committed
307 308 309 310
    if( InitThread( p_input ) )
    {
        /* If we failed, wait before we are killed, and exit */
        p_input->b_error = 1;
311

Sam Hocevar's avatar
 
Sam Hocevar committed
312
        ErrorThread( p_input );
313 314 315 316

        /* Tell we're dead */
        p_input->b_dead = 1;

Gildas Bazin's avatar
 
Gildas Bazin committed
317 318
        input_DelInfo( p_input );

Sam Hocevar's avatar
 
Sam Hocevar committed
319
        return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
320
    }
Michel Kaempf's avatar
Michel Kaempf committed
321

322
    /* initialization is complete */
323
    vlc_mutex_lock( &p_input->stream.stream_lock );
324 325
    p_input->stream.b_changed        = 1;
    p_input->stream.control.i_status = PLAYING_S;
326 327
    vlc_mutex_unlock( &p_input->stream.stream_lock );

328 329 330
    val.i_int = PLAYING_S;
    var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );

Sam Hocevar's avatar
 
Sam Hocevar committed
331 332
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
333
        unsigned int i, i_count;
334

Sam Hocevar's avatar
 
Sam Hocevar committed
335
        p_input->c_loops++;
Sam Hocevar's avatar
 
Sam Hocevar committed
336

337
        vlc_mutex_lock( &p_input->stream.stream_lock );
338

339 340 341 342 343
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
 
Johan Bilien committed
344 345
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
346 347

                p_input->pf_set_program( p_input,
348
                                         p_input->stream.p_new_program );
349

350 351 352 353
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

354 355 356 357 358 359 360 361 362 363 364
                for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
                {
                    pgrm_descriptor_t * p_pgrm
                                            = p_input->stream.pp_programs[i];

                    /* Reinitialize synchro. */
                    p_pgrm->i_synchro_state = SYNCHRO_REINIT;
                }
            }
            p_input->stream.p_new_program = NULL;
        }
365

366 367
        if( p_input->stream.p_new_area )
        {
368 369
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
370
                input_AccessReinit( p_input );
371 372 373

                p_input->pf_set_area( p_input, p_input->stream.p_new_area );

374 375 376 377
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

378 379 380 381 382 383 384 385 386
                for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
                {
                    pgrm_descriptor_t * p_pgrm
                                            = p_input->stream.pp_programs[i];

                    /* Reinitialize synchro. */
                    p_pgrm->i_synchro_state = SYNCHRO_REINIT;
                }
            }
387 388 389
            p_input->stream.p_new_area = NULL;
        }

390
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
 
Sam Hocevar committed
391
        {
392
            if( p_input->stream.p_selected_area->i_size > 0 )
393
            {
394
                unsigned int i;
395
                mtime_t      i_time;
396 397
                double f = (double)p_input->stream.p_selected_area->i_seek /
                           (double)p_input->stream.p_selected_area->i_size;
398

Christophe Massiot's avatar
Christophe Massiot committed
399
                vlc_mutex_unlock( &p_input->stream.stream_lock );
400
                demux_Control( p_input, DEMUX_SET_POSITION, f );
Christophe Massiot's avatar
Christophe Massiot committed
401
                vlc_mutex_lock( &p_input->stream.stream_lock );
402

403 404 405 406
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

407 408
                for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
                {
409
                    pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
410 411 412 413

                    /* Reinitialize synchro. */
                    p_pgrm->i_synchro_state = SYNCHRO_REINIT;
                }
414

Laurent Aimar's avatar
Laurent Aimar committed
415
                vlc_mutex_unlock( &p_input->stream.stream_lock );
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
                if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
                {
                    int i;
                    vlc_value_t val;

                    /* Help in bar display */
                    val.i_time = i_time;
                    var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );

                    /* Seek subs */
                    for( i = 0; i < p_input->p_sys->i_sub; i++ )
                    {
                        subtitle_Seek( p_input->p_sys->sub[i], i_time );
                    }
                }
Laurent Aimar's avatar
Laurent Aimar committed
431
                vlc_mutex_lock( &p_input->stream.stream_lock );
432
            }
433
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
 
Sam Hocevar committed
434
        }
435

436 437 438 439 440 441 442 443 444 445 446 447
        if( p_input->stream.p_removed_es )
        {
            input_UnselectES( p_input, p_input->stream.p_removed_es );
            p_input->stream.p_removed_es = NULL;
        }

        if( p_input->stream.p_newly_selected_es )
        {
            input_SelectES( p_input, p_input->stream.p_newly_selected_es );
            p_input->stream.p_newly_selected_es = NULL;
        }

448 449 450 451 452 453 454 455 456 457 458 459 460 461
        if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
        {
            if( p_input->stream.b_new_mute )
            {
                input_EscapeAudioDiscontinuity( p_input );
            }

            vlc_mutex_lock( &p_input->stream.control.control_lock );
            p_input->stream.control.b_mute = p_input->stream.b_new_mute;
            vlc_mutex_unlock( &p_input->stream.control.control_lock );

            p_input->stream.b_new_mute = MUTE_NO_CHANGE;
        }

462
        vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
463

Christophe Massiot's avatar
Christophe Massiot committed
464 465
        /* Read and demultiplex some data. */
        i_count = p_input->pf_demux( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
466

Gildas Bazin's avatar
 
Gildas Bazin committed
467
        if( i_count == 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
468
        {
469
            /* End of file - we do not set b_die because only the
470
             * playlist is allowed to do so. */
471
            msg_Info( p_input, "EOF reached" );
472 473 474 475 476
            p_input->b_eof = 1;
        }
        else if( i_count < 0 )
        {
            p_input->b_error = 1;
477
        }
478 479 480

        if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
        {
481 482 483 484
            int i;
            mtime_t i_time;
            mtime_t i_length;
            double  d_pos;
485 486

            /* update input status variables */
487
            if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
488
            {
489
                val.f_float = (float)d_pos;
490 491
                var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
            }
492
            if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
493
            {
494
                val.i_time = i_time;
495 496
                var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
            }
497
            if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
498
            {
499
                val.i_time = i_length;
500 501 502
                var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
            }

503 504 505 506 507 508
            /* update subs */
            for( i = 0; i < p_input->p_sys->i_sub; i++ )
            {
                subtitle_Demux( p_input->p_sys->sub[i], i_time );
            }

509
            i_update_next = mdate() + I64C(150000);
510
        }
511 512
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
513
    if( p_input->b_error || p_input->b_eof )
514 515 516
    {
        ErrorThread( p_input );
    }
517

518
    EndThread( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
519

Sam Hocevar's avatar
 
Sam Hocevar committed
520
    return 0;
521 522
}

523
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
524
 * InitThread: init the input Thread
525
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
526
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
527
{
528
    vlc_meta_t *meta;
529
    float f_fps;
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
530 531
    playlist_t *p_playlist;
    mtime_t i_length;
532

Christophe Massiot's avatar
Christophe Massiot committed
533
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
534
    char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
Gildas Bazin's avatar
 
Gildas Bazin committed
535
    vlc_value_t val;
536
    int64_t i_microsecondperframe;
Gildas Bazin's avatar
 
Gildas Bazin committed
537

538 539
    subtitle_demux_t *p_sub_toselect = NULL;

Christophe Massiot's avatar
Christophe Massiot committed
540 541 542 543 544
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
545
#if defined( WIN32 ) || defined( UNDER_CE )
Christophe Massiot's avatar
Christophe Massiot committed
546
    if( psz_parser - p_input->psz_dupsource == 1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
547
    {
548
        msg_Warn( p_input, "drive letter %c: found in source string",
Christophe Massiot's avatar
Christophe Massiot committed
549
                           p_input->psz_dupsource[0] ) ;
Stéphane Borel's avatar
 
Stéphane Borel committed
550
        psz_parser = "";
Gildas Bazin's avatar
 
Gildas Bazin committed
551 552
    }
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
553

Christophe Massiot's avatar
Christophe Massiot committed
554
    if( !*psz_parser )
555
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
556
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
557
        p_input->psz_name = p_input->psz_source;
Christophe Massiot's avatar
Christophe Massiot committed
558
        free( p_input->psz_dupsource );
559
        p_input->psz_dupsource = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
560
    }
Christophe Massiot's avatar
Christophe Massiot committed
561 562 563
    else
    {
        *psz_parser++ = '\0';
564

Xavier Marchesini's avatar
 
Xavier Marchesini committed
565 566 567 568
        /* let's skip '//' */
        if( psz_parser[0] == '/' && psz_parser[1] == '/' )
        {
            psz_parser += 2 ;
569
        }
Xavier Marchesini's avatar
 
Xavier Marchesini committed
570 571

        p_input->psz_name = psz_parser ;
Sam Hocevar's avatar
 
Sam Hocevar committed
572

Christophe Massiot's avatar
Christophe Massiot committed
573
        /* Come back to parse the access and demux plug-ins */
Christophe Massiot's avatar
Christophe Massiot committed
574
        psz_parser = p_input->psz_dupsource;
Gildas Bazin's avatar
 
Gildas Bazin committed
575

Stéphane Borel's avatar
 
Stéphane Borel committed
576
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
577 578
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
579
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
580 581 582 583
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
584
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
            psz_parser++;
        }
        else
        {
            p_input->psz_access = psz_parser;

            while( *psz_parser && *psz_parser != '/' )
            {
                psz_parser++;
            }

            if( *psz_parser == '/' )
            {
                *psz_parser++ = '\0';
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
601

Christophe Massiot's avatar
Christophe Massiot committed
602 603 604
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
 
Sam Hocevar committed
605
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
606 607 608 609 610
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
611
    }
Christophe Massiot's avatar
Christophe Massiot committed
612

613 614
    msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
             p_input->psz_access, p_input->psz_demux, p_input->psz_name );
Christophe Massiot's avatar
Christophe Massiot committed
615 616

    if( input_AccessInit( p_input ) == -1 )
617
    {
618 619 620 621 622 623
        free( p_input->psz_source );
        if( p_input->psz_dupsource != NULL )
        {
            free( p_input->psz_dupsource );
        }

624 625 626 627 628 629 630 631 632 633 634 635
        return VLC_EGENERIC;
    }

    /* Initialize optional stream output. (before demuxer)*/
    var_Get( p_input, "sout", &val );
    if( val.psz_string != NULL )
    {
        if ( *val.psz_string && (p_input->stream.p_sout =
             sout_NewInstance( p_input, val.psz_string )) == NULL )
        {
            msg_Err( p_input, "cannot start stream output instance, aborting" );
            free( val.psz_string );
636 637 638 639 640 641 642

            input_AccessEnd( p_input );
            free( p_input->psz_source );
            if( p_input->psz_dupsource != NULL )
            {
                free( p_input->psz_dupsource );
            }
643 644 645
            return VLC_EGENERIC;
        }
        free( val.psz_string );
646
    }
Christophe Massiot's avatar
Christophe Massiot committed
647

648
    p_input->p_es_out = input_EsOutNew( p_input );
649 650
    es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
    es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
651

652
    /* Find and open appropriate access module */
653 654
    p_input->p_access = module_Need( p_input, "access",
                                     p_input->psz_access );
Christophe Massiot's avatar
Christophe Massiot committed
655

Gildas Bazin's avatar
 
Gildas Bazin committed
656 657 658 659
#ifndef WIN32      /* Remove this gross hack from the win32 build as colons
                    * are forbidden in filenames on Win32. */

    /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
660 661 662 663 664 665 666 667 668 669 670
    if ( p_input->p_access == NULL
          && (*p_input->psz_demux || *p_input->psz_access) )
    {
        p_input->psz_access = p_input->psz_demux = "";
        p_input->psz_name = p_input->psz_source;
        free( p_input->psz_dupsource);
        p_input->psz_dupsource = NULL;

        p_input->p_access = module_Need( p_input, "access",
                                         p_input->psz_access );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
671
#endif
672

673
    if( p_input->p_access == NULL )
674
    {
675
        msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
676
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
677 678 679 680
        if ( p_input->stream.p_sout != NULL )
        {
            sout_DeleteInstance( p_input->stream.p_sout );
        }
681 682 683 684 685 686 687

        input_AccessEnd( p_input );
        free( p_input->psz_source );
        if( p_input->psz_dupsource != NULL )
        {
            free( p_input->psz_dupsource );
        }
688
        return VLC_EGENERIC;
689
    }
Christophe Massiot's avatar
Christophe Massiot committed
690 691 692

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
693
    {
Christophe Massiot's avatar
Christophe Massiot committed
694
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
695
    }
696 697
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
698
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
699
    }
700

Gildas Bazin's avatar
 
Gildas Bazin committed
701 702
    /* If the desynchronisation requested by the user is < 0, we need to
     * cache more data. */
Gildas Bazin's avatar
 
Gildas Bazin committed
703 704 705 706
    var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_input, "audio-desync", &val );
    if( val.i_int < 0 )
        p_input->i_pts_delay -= (val.i_int * 1000);
Gildas Bazin's avatar
 
Gildas Bazin committed
707

Sam Hocevar's avatar
 
Sam Hocevar committed
708
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
709
    {
Christophe Massiot's avatar
Christophe Massiot committed
710 711
        while( !input_FillBuffer( p_input ) )
        {
712
            if( p_input->b_die || p_input->b_error || p_input->b_eof )
Christophe Massiot's avatar
Christophe Massiot committed
713
            {
714
                module_Unneed( p_input, p_input->p_access );
715 716 717 718
                if ( p_input->stream.p_sout != NULL )
                {
                    sout_DeleteInstance( p_input->stream.p_sout );
                }
719 720 721 722 723 724
                input_AccessEnd( p_input );
                free( p_input->psz_source );
                if( p_input->psz_dupsource != NULL )
                {
                    free( p_input->psz_dupsource );
                }
725
                return VLC_EGENERIC;
Christophe Massiot's avatar
Christophe Massiot committed
726 727
            }
        }
728
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
729

730
    /* Create the stream_t facilities */
731
    p_input->s = input_StreamNew( p_input );
732 733
    if( p_input->s == NULL )
    {
Clément Stenac's avatar
Clément Stenac committed
734
        /* should never occur yet */
735

Clément Stenac's avatar
Clément Stenac committed
736
        msg_Err( p_input, "cannot create stream_t" );
737

738 739 740 741 742
        module_Unneed( p_input, p_input->p_access );
        if ( p_input->stream.p_sout != NULL )
        {
            sout_DeleteInstance( p_input->stream.p_sout );
        }
743 744 745 746 747 748
        input_AccessEnd( p_input );
        free( p_input->psz_source );
        if( p_input->psz_dupsource != NULL )
        {
            free( p_input->psz_dupsource );
        }
749 750 751
        return VLC_EGENERIC;
    }

752
    /* Find and open appropriate demux module */
Gildas Bazin's avatar
 
Gildas Bazin committed
753 754 755 756
    p_input->p_demux =
        module_Need( p_input, "demux",
                     (p_input->psz_demux && *p_input->psz_demux) ?
                     p_input->psz_demux : "$demux" );
Sam Hocevar's avatar
 
Sam Hocevar committed
757

758
    if( p_input->p_demux == NULL )
759
    {
760
        msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
761
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
762

763
        input_StreamDelete( p_input->s );
764
        module_Unneed( p_input, p_input->p_access );
765
        if ( p_input->stream.p_sout != NULL )
766
        {
767
            sout_DeleteInstance( p_input->stream.p_sout );
768
        }
769 770 771 772 773 774
        input_AccessEnd( p_input );
        free( p_input->psz_source );
        if( p_input->psz_dupsource != NULL )
        {
            free( p_input->psz_dupsource );
        }
775
        return VLC_EGENERIC;
776 777
    }

778 779 780 781 782
    /* Init input_thread_sys_t */
    p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
    p_input->p_sys->i_sub = 0;
    p_input->p_sys->sub   = NULL;

783 784 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 839 840 841 842 843 844 845 846 847 848 849 850 851
    /* get meta informations */
    if( !demux_Control( p_input, DEMUX_GET_META, &meta ) )
    {
        playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_input,
                                         VLC_OBJECT_PLAYLIST,  FIND_PARENT);
        playlist_item_t *p_item = NULL;
        input_info_category_t *p_cat;
        int i;

        if( p_playlist )
        {
            vlc_mutex_lock( &p_playlist->object_lock );
            p_item = playlist_ItemGetByPos( p_playlist, -1 );
            if( p_item )
            {
                vlc_mutex_lock( &p_item->lock );
            }
            vlc_mutex_unlock( &p_playlist->object_lock );
        }

        msg_Dbg( p_input, "meta informations:" );
        if( meta->i_meta > 0 )
        {
            p_cat = input_InfoCategory( p_input, _("File") );
            for( i = 0; i < meta->i_meta; i++ )
            {
                msg_Dbg( p_input, "  - '%s' = '%s'", _(meta->name[i]), meta->value[i] );
                input_AddInfo( p_cat, _(meta->name[i]), "%s", meta->value[i] );
                if( p_item )
                {
                    playlist_ItemAddInfo( p_item, _("File"),
                                          _(meta->name[i]), "%s", meta->value[i] );
                }
            }
        }
        for( i = 0; i < meta->i_track; i++ )
        {
            vlc_meta_t *tk = meta->track[i];
            int j;

            msg_Dbg( p_input, "  - track[%d]:", i );
            if( tk->i_meta > 0 )
            {
                char *psz_cat = malloc( strlen(_("Stream")) + 10 );
                sprintf( psz_cat, "%s %d", _("Stream"), i );
                p_cat = input_InfoCategory( p_input, psz_cat );

                for( j = 0; j < tk->i_meta; j++ )
                {
                    msg_Dbg( p_input, "     - '%s' = '%s'", _(tk->name[j]), tk->value[j] );
                    input_AddInfo( p_cat, _(tk->name[j]), "%s", tk->value[j] );
                    if( p_item )
                    {
                        playlist_ItemAddInfo( p_item, psz_cat,
                                              _(tk->name[j]), "%s", tk->value[j] );
                    }
                }
            }
        }

        if( p_item )
        {
            vlc_mutex_unlock( &p_item->lock );
        }
        if( p_playlist ) vlc_object_release( p_playlist );

        vlc_meta_Delete( meta );
    }

Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
852 853 854 855 856 857 858 859
    /* get length */
    if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
    {
        p_playlist = (playlist_t*)vlc_object_find( p_input,
                                                   VLC_OBJECT_PLAYLIST,
                                                   FIND_PARENT );
        if( p_playlist )
        {
860
            playlist_SetDuration( p_playlist, -1 , i_length );
861
            val.b_bool = p_playlist->i_index;
862
            var_Set( p_playlist, "item-change", val );
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
863 864
            vlc_object_release( p_playlist );
        }
865 866
    }

Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
867

868 869 870 871 872 873 874 875 876 877
    /* get fps */
    if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
    {
        i_microsecondperframe = 0;
    }
    else
    {
        i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
    }

878
    /* Look for and add subtitle files */
879 880 881
    var_Get( p_input, "sub-file", &val );
    if( val.psz_string && *val.psz_string )
    {
882
        subtitle_demux_t *p_sub;
Laurent Aimar's avatar
Laurent Aimar committed
883 884

        msg_Dbg( p_input, "force subtitle: %s", val.psz_string );
Clément Stenac's avatar
Clément Stenac committed
885
        if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
886
                                    i_microsecondperframe ) ) )
887
        {
888
            p_sub_toselect = p_sub;
889 890 891 892
            TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
        }
    }
    if( val.psz_string ) free( val.psz_string );
893

894 895
    var_Get( p_input, "sub-autodetect-file", &val );
    if( val.b_bool )
896
    {
897
        subtitle_demux_t *p_sub;
898
        int i;
899
        char **tmp = subtitles_Detect( p_input, "", p_input->psz_name );
900
        char **tmp2 = tmp;
901
        for( i = 0; *tmp2 != NULL; i++ )
902
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
903
            if( ( p_sub = subtitle_New( p_input, *tmp2,
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
904
                                        i_microsecondperframe ) ) )
905 906 907
            {
                TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
908
            free( *tmp2++ );
909
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
910
        free( tmp );
911 912
    }

913
    es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
914 915 916 917 918 919 920
    val.b_bool =  VLC_FALSE;
    if( p_input->stream.p_sout )
    {
        var_Get( p_input, "sout-all", &val );
    }
    es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
                    val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
921 922
    if( p_sub_toselect )
    {
Clément Stenac's avatar
Clément Stenac committed
923 924
        es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
                        p_sub_toselect->p_es, VLC_TRUE );
925
    }
926

927
    return VLC_SUCCESS;
Michel Kaempf's avatar
Michel Kaempf committed
928 929
}

930
/*****************************************************************************
931
 * ErrorThread: RunThread() error loop
932
 *****************************************************************************
933
 * This function is called when an error occured during thread main's loop.
934
 *****************************************************************************/
935
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
936
{
937
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
938
    {
939 940
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
941 942 943
    }
}

944
/*****************************************************************************
945
 * EndThread: end the input thread
946
 *****************************************************************************/
947
static void EndThread( input_thread_t * p_input )
948
{
949
    int i;
950
#ifdef HAVE_SYS_TIMES_H
951 952 953
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
 
Sam Hocevar committed
954

955
    msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
956
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
957
#else
958
    msg_Dbg( p_input, "%ld loops", p_input->c_loops );
959
#endif
960

961 962
    input_DumpStream( p_input );

Gildas Bazin's avatar
 
Gildas Bazin committed
963
    /* Free demultiplexer's data */
Gildas Bazin's avatar
 
Gildas Bazin committed
964
    if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
Gildas Bazin's avatar
 
Gildas Bazin committed
965 966 967 968

    /* Free all ES and destroy all decoder threads */
    input_EndStream( p_input );

969
    /* Close optional stream output instance */
Gildas Bazin's avatar
 
Gildas Bazin committed
970
    if( p_input->stream.p_sout )
971
    {
972 973
        vlc_object_t *p_pl =
            vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
        vlc_value_t keep;

        if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
        {
            /* attach sout to the playlist */
            msg_Warn( p_input, "keeping sout" );
            vlc_object_detach( p_input->stream.p_sout );
            vlc_object_attach( p_input->stream.p_sout, p_pl );
        }
        else
        {
            msg_Warn( p_input, "destroying sout" );
            sout_DeleteInstance( p_input->stream.p_sout );
        }
        if( p_pl )
        {
            vlc_object_release( p_pl );
        }
992 993
    }

994
    /* Destroy subti