input.c 35.6 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-2002 VideoLAN
7
 * $Id: input.c,v 1.266 2003/11/27 05:46:01 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
46
#include "codecs.h"
#include "modules/demux/util/sub.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
47

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

Sam Hocevar's avatar
   
Sam Hocevar committed
58
static  int RunThread       ( input_thread_t *p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
59
60
61
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
62

gbazin's avatar
   
gbazin committed
63
64
static void ParseOption     ( input_thread_t *p_input,
                              const char *psz_option );
65

66
67
68
69
70
71
72
73
74
75
76
/*****************************************************************************
 * 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 );
gbazin's avatar
   
gbazin committed
77

78
/*****************************************************************************
79
 * input_CreateThread: creates a new input thread
80
 *****************************************************************************
81
82
 * This function creates a new input, and returns a pointer
 * to its description. On error, it returns NULL.
83
 *****************************************************************************/
84
input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
85
                                      playlist_item_t *p_item )
Michel Kaempf's avatar
Michel Kaempf committed
86
{
87
    input_thread_t *    p_input;                        /* thread descriptor */
88
    input_info_category_t * p_info;
89
    vlc_value_t val;
gbazin's avatar
   
gbazin committed
90
    int i;
91

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

gbazin's avatar
   
gbazin committed
100
101
102
103
104
105
    /* Parse input options */
    for( i = 0; i < p_item->i_options; i++ )
    {
        ParseOption( p_input, p_item->ppsz_options[i] );
    }

gbazin's avatar
   
gbazin committed
106
107
108
109
110
    /* 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 );
111
112
    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 );
113
    var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
gbazin's avatar
   
gbazin committed
114
115

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

    /* play status */

    /* position variable */
124
125
    var_Create( p_input, "position",  VLC_VAR_FLOAT );  /* position 0.0->1.0 */
    var_Create( p_input, "position-offset",  VLC_VAR_FLOAT );  /* relative */
126
127
128
    val.f_float = 0.0;
    var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "position", PositionCallback, NULL );
129
    var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
130
131
132

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

    /* 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 );

160
161
162
163
    /* 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 );
gbazin's avatar
   
gbazin committed
164

165
    /* Initialize thread properties */
Sam Hocevar's avatar
   
Sam Hocevar committed
166
    p_input->b_eof      = 0;
167
    p_input->p_sys      = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
168
169

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

172
173
174
    /* Stream */
    p_input->s = NULL;

175
176
177
    /* es out */
    p_input->p_es_out = NULL;

Stéphane Borel's avatar
   
Stéphane Borel committed
178
    /* Demux */
179
180
181
182
    p_input->p_demux   = NULL;
    p_input->pf_demux  = NULL;
    p_input->pf_rewind = NULL;
    p_input->pf_demux_control = NULL;
Stéphane Borel's avatar
   
Stéphane Borel committed
183
184

    /* Access */
185
    p_input->p_access = NULL;
186

Sam Hocevar's avatar
   
Sam Hocevar committed
187
188
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;
gbazin's avatar
   
gbazin committed
189
    p_input->i_pts_delay = DEFAULT_PTS_DELAY;
Sam Hocevar's avatar
   
Sam Hocevar committed
190

191
192
193
194
195
196
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

    /* Set locks. */
197
    vlc_mutex_init( p_input, &p_input->stream.stream_lock );
198
    vlc_cond_init( p_input, &p_input->stream.stream_wait );
199
    vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
Michel Kaempf's avatar
Michel Kaempf committed
200

201
    /* Initialize stream description */
Stéphane Borel's avatar
   
Stéphane Borel committed
202
    p_input->stream.b_changed = 0;
203
204
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
205
    p_input->stream.i_pgrm_number = 0;
206
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
207
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
208
    p_input->stream.i_mux_rate = 0;
209
    p_input->stream.b_seekable = 0;
210
    p_input->stream.p_sout = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
211

Sam Hocevar's avatar
   
Sam Hocevar committed
212
213
214
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
   
Stéphane Borel committed
215
216
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
217
    p_input->stream.p_selected_area = NULL;
218
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
219

Sam Hocevar's avatar
   
Sam Hocevar committed
220
221
222
223
    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
224
    /* By default there is one area in a stream */
gbazin's avatar
   
gbazin committed
225
    input_AddArea( p_input, 0, 1 );
226
    p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
Stéphane Borel's avatar
   
Stéphane Borel committed
227

228
    /* Initialize stream control properties. */
229
    p_input->stream.control.i_status = INIT_S;
230
231
    p_input->stream.control.i_rate = DEFAULT_RATE;
    p_input->stream.control.b_mute = 0;
232
233
    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );

234
235
236
237
238
239
240
241
242
243
244
    /* Initialize input info */
    p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
    if( !p_input->stream.p_info )
    {
        msg_Err( p_input, "No memory!" );
        return NULL;
    }
    p_input->stream.p_info->psz_name = strdup("General") ;
    p_input->stream.p_info->p_info = NULL;
    p_input->stream.p_info->p_next = NULL;

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

247
248
    p_info = input_InfoCategory( p_input, _("General") );
    input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
249
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
   
Sam Hocevar committed
250

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

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

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

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

/*****************************************************************************
 * 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 */
287
    vlc_thread_join( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
288
289
290

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

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

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

Sam Hocevar's avatar
   
Sam Hocevar committed
308
309
310
311
312
    if( InitThread( p_input ) )
    {
        /* If we failed, wait before we are killed, and exit */
        p_input->b_error = 1;
        ErrorThread( p_input );
gbazin's avatar
   
gbazin committed
313
        EndThread( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
314
        return 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
315
    }
Michel Kaempf's avatar
Michel Kaempf committed
316

317
    /* initialization is complete */
318
    vlc_mutex_lock( &p_input->stream.stream_lock );
319
320
    p_input->stream.b_changed        = 1;
    p_input->stream.control.i_status = PLAYING_S;
321
322
    vlc_mutex_unlock( &p_input->stream.stream_lock );

323
324
325
    val.i_int = PLAYING_S;
    var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );

Sam Hocevar's avatar
   
Sam Hocevar committed
326
327
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
328
        unsigned int i, i_count;
329

Sam Hocevar's avatar
   
Sam Hocevar committed
330
        p_input->c_loops++;
Sam Hocevar's avatar
   
Sam Hocevar committed
331

332
        vlc_mutex_lock( &p_input->stream.stream_lock );
333

334
335
336
337
338
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
   
Johan Bilien committed
339
340
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
341
342

                p_input->pf_set_program( p_input,
343
                                         p_input->stream.p_new_program );
344

345
346
347
348
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

349
350
351
352
353
354
355
356
357
358
359
                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;
        }
360

361
362
        if( p_input->stream.p_new_area )
        {
363
364
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
365
                input_AccessReinit( p_input );
366
367
368

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

369
370
371
372
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

373
374
375
376
377
378
379
380
381
                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;
                }
            }
382
383
384
            p_input->stream.p_new_area = NULL;
        }

385
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
   
Sam Hocevar committed
386
        {
387
            if( p_input->stream.p_selected_area->i_size > 0 )
388
            {
389
                unsigned int i;
390
                mtime_t      i_time;
391
392
                double f = (double)p_input->stream.p_selected_area->i_seek /
                           (double)p_input->stream.p_selected_area->i_size;
393

Christophe Massiot's avatar
Christophe Massiot committed
394
                vlc_mutex_unlock( &p_input->stream.stream_lock );
395
                demux_Control( p_input, DEMUX_SET_POSITION, f );
Christophe Massiot's avatar
Christophe Massiot committed
396
                vlc_mutex_lock( &p_input->stream.stream_lock );
397

398
399
400
401
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

402
403
                for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
                {
404
                    pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
405
406
407
408

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

Laurent Aimar's avatar
Laurent Aimar committed
410
                vlc_mutex_unlock( &p_input->stream.stream_lock );
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
                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
426
                vlc_mutex_lock( &p_input->stream.stream_lock );
427
            }
428
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
   
Sam Hocevar committed
429
        }
430

431
432
433
434
435
436
437
438
439
440
441
442
        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;
        }

443
444
445
446
447
448
449
450
451
452
453
454
455
456
        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;
        }

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

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

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

        if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
        {
476
477
478
479
            int i;
            mtime_t i_time;
            mtime_t i_length;
            double  d_pos;
480
481

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

498
499
500
501
502
503
504
            /* update subs */
            for( i = 0; i < p_input->p_sys->i_sub; i++ )
            {
                subtitle_Demux( p_input->p_sys->sub[i], i_time );
            }

            i_update_next = mdate() + 150000LL;
505
        }
506
507
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
508
    if( p_input->b_error || p_input->b_eof )
509
510
511
    {
        ErrorThread( p_input );
    }
512

513
    EndThread( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
514

Sam Hocevar's avatar
   
Sam Hocevar committed
515
    return 0;
516
517
}

518
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
519
 * InitThread: init the input Thread
520
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
521
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
522
{
523
    float f_fps;
Christophe Massiot's avatar
Christophe Massiot committed
524
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
525
    char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
gbazin's avatar
   
gbazin committed
526
    vlc_value_t val;
527
528
    subtitle_demux_t *p_sub;
    int64_t i_microsecondperframe;
gbazin's avatar
   
gbazin committed
529

Christophe Massiot's avatar
Christophe Massiot committed
530
531
532
533
534
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
535
#if defined( WIN32 ) || defined( UNDER_CE )
Christophe Massiot's avatar
Christophe Massiot committed
536
    if( psz_parser - p_input->psz_dupsource == 1 )
gbazin's avatar
   
gbazin committed
537
    {
538
        msg_Warn( p_input, "drive letter %c: found in source string",
Christophe Massiot's avatar
Christophe Massiot committed
539
                           p_input->psz_dupsource[0] ) ;
Stéphane Borel's avatar
   
Stéphane Borel committed
540
        psz_parser = "";
gbazin's avatar
   
gbazin committed
541
542
    }
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
543

Christophe Massiot's avatar
Christophe Massiot committed
544
    if( !*psz_parser )
545
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
546
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
547
        p_input->psz_name = p_input->psz_source;
Christophe Massiot's avatar
Christophe Massiot committed
548
        free( p_input->psz_dupsource );
549
        p_input->psz_dupsource = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
550
    }
Christophe Massiot's avatar
Christophe Massiot committed
551
552
553
    else
    {
        *psz_parser++ = '\0';
554

Xavier Marchesini's avatar
   
Xavier Marchesini committed
555
556
557
558
        /* let's skip '//' */
        if( psz_parser[0] == '/' && psz_parser[1] == '/' )
        {
            psz_parser += 2 ;
559
        }
Xavier Marchesini's avatar
   
Xavier Marchesini committed
560
561

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

Christophe Massiot's avatar
Christophe Massiot committed
563
        /* Come back to parse the access and demux plug-ins */
Christophe Massiot's avatar
Christophe Massiot committed
564
        psz_parser = p_input->psz_dupsource;
gbazin's avatar
   
gbazin committed
565

Stéphane Borel's avatar
   
Stéphane Borel committed
566
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
567
568
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
569
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
570
571
572
573
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
574
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
            psz_parser++;
        }
        else
        {
            p_input->psz_access = psz_parser;

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

            if( *psz_parser == '/' )
            {
                *psz_parser++ = '\0';
            }
        }
gbazin's avatar
   
gbazin committed
591

Christophe Massiot's avatar
Christophe Massiot committed
592
593
594
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
   
Sam Hocevar committed
595
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
596
597
598
599
600
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
601
    }
Christophe Massiot's avatar
Christophe Massiot committed
602

603
604
    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
605
606

    if( input_AccessInit( p_input ) == -1 )
607
    {
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
        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 );
            return VLC_EGENERIC;
        }
        free( val.psz_string );
623
    }
Christophe Massiot's avatar
Christophe Massiot committed
624

625
    p_input->p_es_out = input_EsOutNew( p_input );
626
627
    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 );
628

629
    /* Find and open appropriate access module */
630
631
    p_input->p_access = module_Need( p_input, "access",
                                     p_input->psz_access );
Christophe Massiot's avatar
Christophe Massiot committed
632

gbazin's avatar
   
gbazin committed
633
634
635
636
#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 */
637
638
639
640
641
642
643
644
645
646
647
    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 );
    }
gbazin's avatar
   
gbazin committed
648
#endif
649

650
    if( p_input->p_access == NULL )
651
    {
652
        msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
653
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
654
655
656
657
658
        if ( p_input->stream.p_sout != NULL )
        {
            sout_DeleteInstance( p_input->stream.p_sout );
        }
        return VLC_EGENERIC;
659
    }
Christophe Massiot's avatar
Christophe Massiot committed
660
661
662

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
663
    {
Christophe Massiot's avatar
Christophe Massiot committed
664
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
665
    }
666
667
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
668
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
669
    }
670

gbazin's avatar
   
gbazin committed
671
672
    /* If the desynchronisation requested by the user is < 0, we need to
     * cache more data. */
gbazin's avatar
   
gbazin committed
673
674
675
676
    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);
gbazin's avatar
   
gbazin committed
677

Sam Hocevar's avatar
   
Sam Hocevar committed
678
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
679
    {
Christophe Massiot's avatar
Christophe Massiot committed
680
681
        while( !input_FillBuffer( p_input ) )
        {
682
            if( p_input->b_die || p_input->b_error || p_input->b_eof )
Christophe Massiot's avatar
Christophe Massiot committed
683
            {
684
                module_Unneed( p_input, p_input->p_access );
685
686
687
688
689
                if ( p_input->stream.p_sout != NULL )
                {
                    sout_DeleteInstance( p_input->stream.p_sout );
                }
                return VLC_EGENERIC;
Christophe Massiot's avatar
Christophe Massiot committed
690
691
            }
        }
692
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
693

694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
    /* Create the stream_t facilities */
    p_input->s = stream_OpenInput( p_input );
    if( p_input->s == NULL )
    {
        /* should nver occur yet */

        msg_Err( p_input, "cannot create stream_t !" );
        module_Unneed( p_input, p_input->p_access );
        if ( p_input->stream.p_sout != NULL )
        {
            sout_DeleteInstance( p_input->stream.p_sout );
        }
        return VLC_EGENERIC;
    }

709
    /* Find and open appropriate demux module */
gbazin's avatar
   
gbazin committed
710
711
712
713
    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
714

715
    if( p_input->p_demux == NULL )
716
    {
717
        msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
718
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
719
        stream_Release( p_input->s );
720
        module_Unneed( p_input, p_input->p_access );
721
        if ( p_input->stream.p_sout != NULL )
722
        {
723
            sout_DeleteInstance( p_input->stream.p_sout );
724
        }
725
        return VLC_EGENERIC;
726
727
    }

728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
    /* 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;

    /* 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 );
    }

743
    /* Look for and add subtitle files */
744
745
746
    var_Get( p_input, "sub-file", &val );
    if( val.psz_string && *val.psz_string )
    {
747
        if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string), i_microsecondperframe, 0 ) ) )
748
        {
749
            /* Select this ES by default */
750
            es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_sub->p_es, VLC_TRUE );
751

752
753
754
755
            TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
        }
    }
    if( val.psz_string ) free( val.psz_string );
756

757
758
    var_Get( p_input, "sub-autodetect-file", &val );
    if( val.b_bool )
759
    {
760
        int i;
761
762
        char **tmp = subtitles_Detect( p_input, "", p_input->psz_source );
        char **tmp2 = tmp;
763
        for( i = 0; *tmp2 != NULL; i++ )
764
        {
765
            if( ( p_sub = subtitle_New( p_input, strdup(*tmp2++), i_microsecondperframe, i ) ) )
766
767
768
            {
                TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
            }
769
        }
770
        free(tmp);
771
772
    }

773
    es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
774
775
776
777
778
779
780
    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 );
781

782
    return VLC_SUCCESS;
Michel Kaempf's avatar
Michel Kaempf committed
783
784
}

785
/*****************************************************************************
786
 * ErrorThread: RunThread() error loop
787
 *****************************************************************************
788
 * This function is called when an error occured during thread main's loop.
789
 *****************************************************************************/
790
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
791
{
792
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
793
    {
794
795
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
796
797
798
    }
}

799
/*****************************************************************************
800
 * EndThread: end the input thread
801
 *****************************************************************************/
802
static void EndThread( input_thread_t * p_input )
803
{
804
    int i;
805
#ifdef HAVE_SYS_TIMES_H
806
807
808
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
   
Sam Hocevar committed
809

810
    msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
811
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
812
#else
813
    msg_Dbg( p_input, "%ld loops", p_input->c_loops );
814
#endif
815

816
817
    input_DumpStream( p_input );

gbazin's avatar
   
gbazin committed
818
    /* Free demultiplexer's data */
gbazin's avatar
   
gbazin committed
819
    if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
gbazin's avatar
   
gbazin committed
820
821
822
823

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

824
    /* Close optional stream output instance */
gbazin's avatar
   
gbazin committed
825
    if( p_input->stream.p_sout )
826
    {
827
828
        vlc_object_t *p_pl =
            vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
        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 );
        }
847
848
    }

849
    /* Destroy subtitles demuxers */
gbazin's avatar
   
gbazin committed
850
    if( p_input->p_sys )
851
    {
gbazin's avatar
   
gbazin committed
852
853
854
855
856
857
858
859
        for( i = 0; i < p_input->p_sys->i_sub; i++ )
        {
            subtitle_Close( p_input->p_sys->sub[i] );
        }
        if( p_input->p_sys->i_sub > 0 )
        {
            free( p_input->p_sys->sub );
        }
860

gbazin's avatar
   
gbazin committed
861
862
863
        /* Free input_thread_sys_t */
        free( p_input->p_sys );
    }
864

865
    /* Destroy the stream_t facilities */
gbazin's avatar
   
gbazin committed
866
    if( p_input->s ) stream_Release( p_input->s );
867

868
    /* Destroy es out */
gbazin's avatar
   
gbazin committed
869
    if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
870

871
    /* Close the access plug-in */
gbazin's avatar
   
gbazin committed
872
    if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
Sam Hocevar's avatar
   
Sam Hocevar committed
873

Christophe Massiot's avatar
Christophe Massiot committed
874
875
    input_AccessEnd( p_input );

876
877
878
879
    /* Free info structures XXX destroy es before 'cause vorbis */
    msg_Dbg( p_input, "freeing info structures...");
    input_DelInfo( p_input );

Christophe Massiot's avatar
Christophe Massiot committed
880
    free( p_input->psz_source );
gbazin's avatar
   
gbazin committed
881
    if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
882
883
884

    /* Tell we're dead */
    p_input->b_dead = 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
885
886
}

gbazin's avatar
   
gbazin committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959