input.c 37.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
 *****************************************************************************
zorglub's avatar
zorglub committed
6
 * Copyright (C) 1998-2004 VideoLAN
7
 * $Id: input.c,v 1.284 2004/01/29 17:51:08 zorglub 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 "../../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
85
86
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
87
{
88
    input_thread_t *    p_input;                        /* thread descriptor */
89
    vlc_value_t val;
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
    /* Parse input options */
101
    for( i = 0; i < i_options; i++ )
gbazin's avatar
   
gbazin committed
102
    {
103
104
        msg_Dbg( p_input, "option: %s", ppsz_options[i] );
        ParseOption( p_input, ppsz_options[i] );
gbazin's avatar
   
gbazin committed
105
106
    }

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

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

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

125
126
127
    /* play status */

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

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

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

164
165
166
167
    /* 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
168

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

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

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

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

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

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

Sam Hocevar's avatar
   
Sam Hocevar committed
192
193
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;
gbazin's avatar
   
gbazin committed
194
    p_input->i_pts_delay = DEFAULT_PTS_DELAY;
Sam Hocevar's avatar
   
Sam Hocevar committed
195

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

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

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

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

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

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

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

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

gbazin's avatar
   
gbazin committed
241
242
243
244
245
    /* 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 );
246
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
   
Sam Hocevar committed
247

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

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

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

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

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

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

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

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

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

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

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

gbazin's avatar
   
gbazin committed
316
317
        input_DelInfo( p_input );

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

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

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

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

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

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

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

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

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

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

353
354
355
356
357
358
359
360
361
362
363
                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;
        }
364

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

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

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

377
378
379
380
381
382
383
384
385
                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;
                }
            }
386
387
388
            p_input->stream.p_new_area = NULL;
        }

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

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

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

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

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

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

435
436
437
438
439
440
441
442
443
444
445
446
        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;
        }

447
448
449
450
451
452
453
454
455
456
457
458
459
460
        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;
        }

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

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

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

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

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

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

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

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

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

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

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

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

536
537
    subtitle_demux_t *p_sub_toselect = NULL;

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

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

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

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

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

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

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

611
612
    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
613
614

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

622
623
624
625
626
627
628
629
630
631
632
633
        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 );
634
635
636
637
638
639
640

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

646
    p_input->p_es_out = input_EsOutNew( p_input );
647
648
    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 );
649

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

gbazin's avatar
   
gbazin committed
654
655
656
657
#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 */
658
659
660
661
662
663
664
665
666
667
668
    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
669
#endif
670

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

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

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

gbazin's avatar
   
gbazin committed
699
700
    /* If the desynchronisation requested by the user is < 0, we need to
     * cache more data. */
gbazin's avatar
   
gbazin committed
701
702
703
704
    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
705

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

728
    /* Create the stream_t facilities */
729
    p_input->s = input_StreamNew( p_input );
730
731
    if( p_input->s == NULL )
    {
zorglub's avatar
zorglub committed
732
        /* should never occur yet */
733

zorglub's avatar
zorglub committed
734
        msg_Err( p_input, "cannot create stream_t" );
735

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

750
    /* Find and open appropriate demux module */
gbazin's avatar
   
gbazin committed
751
752
753
754
    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
755

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

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

776
777
778
779
780
    /* 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;

sigmunau's avatar
sigmunau committed
781
782
783
784
785
786
787
788
    /* 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 )
        {
789
            playlist_SetDuration( p_playlist, -1 , i_length );
790
            val.b_bool = p_playlist->i_index;
791
            var_Set( p_playlist, "item-change", val );
sigmunau's avatar
sigmunau committed
792
793
            vlc_object_release( p_playlist );
        }
794
795
    }

sigmunau's avatar
sigmunau committed
796

797
798
799
800
801
802
803
804
805
806
    /* 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 );
    }

807
    /* Look for and add subtitle files */
808
809
810
    var_Get( p_input, "sub-file", &val );
    if( val.psz_string && *val.psz_string )
    {
811
        msg_Dbg( p_input, "force subtitle: %s", val.psz_string );
812
        subtitle_demux_t *p_sub;
zorglub's avatar
zorglub committed
813
        if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
hartman's avatar
hartman committed
814
                                    i_microsecondperframe ) ) )
815
        {
816
            p_sub_toselect = p_sub;
817
818
819
820
            TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
        }
    }
    if( val.psz_string ) free( val.psz_string );
821

822
823
    var_Get( p_input, "sub-autodetect-file", &val );
    if( val.b_bool )
824
    {
825
        subtitle_demux_t *p_sub;
826
        int i;
827
        char **tmp = subtitles_Detect( p_input, "", p_input->psz_name );
828
        char **tmp2 = tmp;
829
        for( i = 0; *tmp2 != NULL; i++ )
830
        {
gbazin's avatar
   
gbazin committed
831
            if( ( p_sub = subtitle_New( p_input, *tmp2,
hartman's avatar
hartman committed
832
                                        i_microsecondperframe ) ) )
833
834
835
            {
                TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
            }
gbazin's avatar
   
gbazin committed
836
            free( *tmp2++ );
837
        }
gbazin's avatar
   
gbazin committed
838
        free( tmp );
839
840
    }

841
    es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
842
843
844
845
846
847
848
    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 );
849
850
    if( p_sub_toselect )
    {
zorglub's avatar
zorglub committed
851
852
        es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
                        p_sub_toselect->p_es, VLC_TRUE );
853
    }
854

855
    return VLC_SUCCESS;
Michel Kaempf's avatar
Michel Kaempf committed
856
857
}

858
/*****************************************************************************
859
 * ErrorThread: RunThread() error loop
860
 *****************************************************************************
861
 * This function is called when an error occured during thread main's loop.
862
 *****************************************************************************/
863
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
864
{
865
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
866
    {
867
868
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
869
870
871
    }
}

872
/*****************************************************************************
873
 * EndThread: end the input thread
874
 *****************************************************************************/
875
static void EndThread( input_thread_t * p_input )
876
{
877
    int i;
878
#ifdef HAVE_SYS_TIMES_H
879
880
881
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
   
Sam Hocevar committed
882