input.c 40.2 KB
Newer Older
1
2
/*****************************************************************************
 * input.c: input thread
Michel Kaempf's avatar
Michel Kaempf committed
3
4
 * Read an MPEG2 stream, demultiplex and parse it before sending it to
 * decoders.
5
 *****************************************************************************
6
 * Copyright (C) 1998-2002 VideoLAN
7
 * $Id: input.c,v 1.240 2003/09/13 17:42:16 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
33
#include <vlc/input.h>
#include <vlc/vout.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
34

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

39
#include "vlc_playlist.h"
40

41
42
#include "stream_output.h"

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

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

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

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

static es_out_t *EsOutCreate ( input_thread_t * );
static void      EsOutRelease( es_out_t * );

68
69
70
71
72
73
74
75
76
77
78
/*****************************************************************************
 * 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
79

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

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

gbazin's avatar
   
gbazin committed
102
103
104
105
106
107
    /* 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
108
109
110
111
112
113
114
115
116
    /* 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 );

    var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    var_Create( p_input, "sout-keep",  VLC_VAR_BOOL | VLC_VAR_DOINHERIT );

    /* play status */

    /* position variable */
    var_Create( p_input, "position",  VLC_VAR_FLOAT );  /* position 0.0 -> 1.0 */
    val.f_float = 0.0;
    var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "position", PositionCallback, NULL );

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

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

gbazin's avatar
   
gbazin committed
154

155
    /* Initialize thread properties */
Sam Hocevar's avatar
   
Sam Hocevar committed
156
    p_input->b_eof      = 0;
157
    p_input->p_sys      = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
158
159

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

162
163
164
    /* Stream */
    p_input->s = NULL;

165
166
167
    /* es out */
    p_input->p_es_out = NULL;

Stéphane Borel's avatar
   
Stéphane Borel committed
168
    /* Demux */
169
170
171
172
    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
173
174

    /* Access */
175
    p_input->p_access = NULL;
176

Sam Hocevar's avatar
   
Sam Hocevar committed
177
178
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;
gbazin's avatar
   
gbazin committed
179
    p_input->i_pts_delay = DEFAULT_PTS_DELAY;
Sam Hocevar's avatar
   
Sam Hocevar committed
180

181
182
183
184
185
186
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

    /* Set locks. */
187
    vlc_mutex_init( p_input, &p_input->stream.stream_lock );
188
    vlc_cond_init( p_input, &p_input->stream.stream_wait );
189
    vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
Michel Kaempf's avatar
Michel Kaempf committed
190

191
    /* Initialize stream description */
Stéphane Borel's avatar
   
Stéphane Borel committed
192
    p_input->stream.b_changed = 0;
193
194
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
195
    p_input->stream.i_pgrm_number = 0;
196
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
197
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
198
    p_input->stream.i_mux_rate = 0;
199
    p_input->stream.b_seekable = 0;
200
    p_input->stream.p_sout = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
201

Sam Hocevar's avatar
   
Sam Hocevar committed
202
203
204
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
   
Stéphane Borel committed
205
206
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
207
    p_input->stream.p_selected_area = NULL;
208
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
209

Sam Hocevar's avatar
   
Sam Hocevar committed
210
211
212
213
    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
214
    /* By default there is one area in a stream */
gbazin's avatar
   
gbazin committed
215
    input_AddArea( p_input, 0, 1 );
216
    p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
Stéphane Borel's avatar
   
Stéphane Borel committed
217

218
    /* Initialize stream control properties. */
219
    p_input->stream.control.i_status = INIT_S;
220
221
    p_input->stream.control.i_rate = DEFAULT_RATE;
    p_input->stream.control.b_mute = 0;
222
223
    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );

224
225
226
227
228
229
230
231
232
233
234
    /* 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;

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

237
238
    p_info = input_InfoCategory( p_input, _("General") );
    input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
239
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
   
Sam Hocevar committed
240

Sam Hocevar's avatar
Sam Hocevar committed
241
    /* Create thread and wait for its readiness. */
242
243
    if( vlc_thread_create( p_input, "input", RunThread,
                           VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
Michel Kaempf's avatar
Michel Kaempf committed
244
    {
245
        msg_Err( p_input, "cannot create input thread" );
Michel Kaempf's avatar
Michel Kaempf committed
246
        free( p_input );
247
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
248
    }
249

250
    return p_input;
Michel Kaempf's avatar
Michel Kaempf committed
251
252
}

253
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
254
 * input_StopThread: mark an input thread as zombie
255
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
256
 * This function should not return until the thread is effectively cancelled.
257
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
258
void input_StopThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
259
{
Sam Hocevar's avatar
   
Sam Hocevar committed
260
261
    /* Make the thread exit from a possible vlc_cond_wait() */
    vlc_mutex_lock( &p_input->stream.stream_lock );
262
263
    /* Request thread destruction */
    p_input->b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
264

265
266
    vlc_cond_signal( &p_input->stream.stream_wait );
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
267
268
269
270
271
272
273
274
275
276
}

/*****************************************************************************
 * 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 */
277
    vlc_thread_join( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
278
279
280

    /* Destroy Mutex locks */
    vlc_mutex_destroy( &p_input->stream.control.control_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
281
    vlc_cond_destroy( &p_input->stream.stream_wait );
Sam Hocevar's avatar
   
Sam Hocevar committed
282
    vlc_mutex_destroy( &p_input->stream.stream_lock );
Michel Kaempf's avatar
Michel Kaempf committed
283
284
}

285
/*****************************************************************************
286
 * RunThread: main thread loop
287
 *****************************************************************************
288
 * Thread in charge of processing the network packets and demultiplexing.
289
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
290
static int RunThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
291
{
292
293
294
    vlc_value_t  val;
    mtime_t      i_update_next = -1;

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

Sam Hocevar's avatar
   
Sam Hocevar committed
298
299
300
301
302
    if( InitThread( p_input ) )
    {
        /* If we failed, wait before we are killed, and exit */
        p_input->b_error = 1;
        ErrorThread( p_input );
Sam Hocevar's avatar
Sam Hocevar committed
303
        p_input->b_dead = 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
304
        return 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
305
    }
Michel Kaempf's avatar
Michel Kaempf committed
306

307
    /* initialization is complete */
308
    vlc_mutex_lock( &p_input->stream.stream_lock );
309
310
    p_input->stream.b_changed        = 1;
    p_input->stream.control.i_status = PLAYING_S;
311
312
    vlc_mutex_unlock( &p_input->stream.stream_lock );

313
314
315
    val.i_int = PLAYING_S;
    var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );

Sam Hocevar's avatar
   
Sam Hocevar committed
316
317
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
318
        unsigned int i, i_count;
319

Sam Hocevar's avatar
   
Sam Hocevar committed
320
        p_input->c_loops++;
Sam Hocevar's avatar
   
Sam Hocevar committed
321

322
        vlc_mutex_lock( &p_input->stream.stream_lock );
323

324
325
326
327
328
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
   
Johan Bilien committed
329
330
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
331
332

                p_input->pf_set_program( p_input,
333
                                         p_input->stream.p_new_program );
334

335
336
337
338
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

339
340
341
342
343
344
345
346
347
348
349
                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;
        }
350

351
352
        if( p_input->stream.p_new_area )
        {
353
354
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
355
                input_AccessReinit( p_input );
356
357
358

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

359
360
361
362
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

363
364
365
366
367
368
369
370
371
                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;
                }
            }
372
373
374
            p_input->stream.p_new_area = NULL;
        }

375
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
   
Sam Hocevar committed
376
        {
377
            if( p_input->stream.p_selected_area->i_size > 0 )
378
            {
379
                unsigned int i;
380
                mtime_t      i_time;
381
382
                double f = (double)p_input->stream.p_selected_area->i_seek /
                           (double)p_input->stream.p_selected_area->i_size;
383

Christophe Massiot's avatar
Christophe Massiot committed
384
                vlc_mutex_unlock( &p_input->stream.stream_lock );
385
                demux_Control( p_input, DEMUX_SET_POSITION, f );
Christophe Massiot's avatar
Christophe Massiot committed
386
                vlc_mutex_lock( &p_input->stream.stream_lock );
387

388
389
390
391
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

392
393
                for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
                {
394
                    pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
395
396
397
398

                    /* Reinitialize synchro. */
                    p_pgrm->i_synchro_state = SYNCHRO_REINIT;
                }
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

                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 );
                    }
                }
415
            }
416
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
   
Sam Hocevar committed
417
        }
418

419
420
421
422
423
424
425
426
427
428
429
430
        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;
        }

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

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

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

gbazin's avatar
   
gbazin committed
450
        if( i_count == 0 )
Sam Hocevar's avatar
   
Sam Hocevar committed
451
        {
452
            /* End of file - we do not set b_die because only the
453
             * playlist is allowed to do so. */
454
            msg_Info( p_input, "EOF reached" );
455
456
457
458
459
            p_input->b_eof = 1;
        }
        else if( i_count < 0 )
        {
            p_input->b_error = 1;
460
        }
461
462
463

        if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
        {
464
465
466
467
            int i;
            mtime_t i_time;
            mtime_t i_length;
            double  d_pos;
468
469

            /* update input status variables */
470
            if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
471
            {
472
                val.f_float = (float)d_pos;
473
474
                var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
            }
475
            if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
476
            {
477
                val.i_time = i_time;
478
479
                var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
            }
480
            if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
481
            {
482
                val.i_time = i_length;
483
484
485
                var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
            }

486
487
488
489
490
491
492
            /* 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;
493
        }
494
495
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
496
    if( p_input->b_error || p_input->b_eof )
497
498
499
    {
        ErrorThread( p_input );
    }
500

501
    EndThread( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
502

Sam Hocevar's avatar
   
Sam Hocevar committed
503
    return 0;
504
505
}

506
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
507
 * InitThread: init the input Thread
508
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
509
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
510
{
511
    float f_fps;
Christophe Massiot's avatar
Christophe Massiot committed
512
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
513
    char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
gbazin's avatar
   
gbazin committed
514
    vlc_value_t val;
515
516
    subtitle_demux_t *p_sub;
    int64_t i_microsecondperframe;
gbazin's avatar
   
gbazin committed
517

Christophe Massiot's avatar
Christophe Massiot committed
518
519
520
521
522
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
523
#if defined( WIN32 ) || defined( UNDER_CE )
Christophe Massiot's avatar
Christophe Massiot committed
524
    if( psz_parser - p_input->psz_dupsource == 1 )
gbazin's avatar
   
gbazin committed
525
    {
526
        msg_Warn( p_input, "drive letter %c: found in source string",
Christophe Massiot's avatar
Christophe Massiot committed
527
                           p_input->psz_dupsource[0] ) ;
Stéphane Borel's avatar
   
Stéphane Borel committed
528
        psz_parser = "";
gbazin's avatar
   
gbazin committed
529
530
    }
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
531

Christophe Massiot's avatar
Christophe Massiot committed
532
    if( !*psz_parser )
533
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
534
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
535
        p_input->psz_name = p_input->psz_source;
Christophe Massiot's avatar
Christophe Massiot committed
536
        free( p_input->psz_dupsource );
537
        p_input->psz_dupsource = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
538
    }
Christophe Massiot's avatar
Christophe Massiot committed
539
540
541
    else
    {
        *psz_parser++ = '\0';
542

Xavier Marchesini's avatar
   
Xavier Marchesini committed
543
544
545
546
        /* let's skip '//' */
        if( psz_parser[0] == '/' && psz_parser[1] == '/' )
        {
            psz_parser += 2 ;
547
        }
Xavier Marchesini's avatar
   
Xavier Marchesini committed
548
549

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

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

Stéphane Borel's avatar
   
Stéphane Borel committed
554
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
555
556
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
557
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
558
559
560
561
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
562
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
            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
579

Christophe Massiot's avatar
Christophe Massiot committed
580
581
582
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
   
Sam Hocevar committed
583
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
584
585
586
587
588
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
589
    }
Christophe Massiot's avatar
Christophe Massiot committed
590

591
592
    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
593
594

    if( input_AccessInit( p_input ) == -1 )
595
    {
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
        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 );
611
    }
Christophe Massiot's avatar
Christophe Massiot committed
612

613
614
    p_input->p_es_out = EsOutCreate( p_input );

615
    /* Find and open appropriate access module */
616
617
    p_input->p_access = module_Need( p_input, "access",
                                     p_input->psz_access );
Christophe Massiot's avatar
Christophe Massiot committed
618

619
620
621
622
623
624
625
626
627
628
629
630
631
632
    if ( p_input->p_access == NULL
          && (*p_input->psz_demux || *p_input->psz_access) )
    {
        /* Maybe we got something like :
         * /Volumes/toto:titi/gabu.mpg */
        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 );
    }

633
    if( p_input->p_access == NULL )
634
    {
635
        msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
636
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
637
638
639
640
641
        if ( p_input->stream.p_sout != NULL )
        {
            sout_DeleteInstance( p_input->stream.p_sout );
        }
        return VLC_EGENERIC;
642
    }
Christophe Massiot's avatar
Christophe Massiot committed
643
644
645

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
646
    {
Christophe Massiot's avatar
Christophe Massiot committed
647
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
648
    }
649
650
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
651
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
652
    }
653

gbazin's avatar
   
gbazin committed
654
655
656
657
658
    /* If the desynchronisation requested by the user is < 0, we need to
     * cache more data. */
    if( p_input->p_vlc->i_desync < 0 )
        p_input->i_pts_delay -= p_input->p_vlc->i_desync;

Sam Hocevar's avatar
   
Sam Hocevar committed
659
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
660
    {
Christophe Massiot's avatar
Christophe Massiot committed
661
662
        while( !input_FillBuffer( p_input ) )
        {
663
            if( p_input->b_die || p_input->b_error || p_input->b_eof )
Christophe Massiot's avatar
Christophe Massiot committed
664
            {
665
                module_Unneed( p_input, p_input->p_access );
666
667
668
669
670
                if ( p_input->stream.p_sout != NULL )
                {
                    sout_DeleteInstance( p_input->stream.p_sout );
                }
                return VLC_EGENERIC;
Christophe Massiot's avatar
Christophe Massiot committed
671
672
            }
        }
673
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
674

675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
    /* 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;
    }

690
    /* Find and open appropriate demux module */
691
692
    p_input->p_demux = module_Need( p_input, "demux",
                                    p_input->psz_demux );
Sam Hocevar's avatar
   
Sam Hocevar committed
693

694
    if( p_input->p_demux == NULL )
695
    {
696
        msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
697
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
698
        stream_Release( p_input->s );
699
        module_Unneed( p_input, p_input->p_access );
700
        if ( p_input->stream.p_sout != NULL )
701
        {
702
            sout_DeleteInstance( p_input->stream.p_sout );
703
        }
704
        return VLC_EGENERIC;
705
706
    }

707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
    /* 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 );
    }

    /* Now add subtitles (for now only one) */
    if( ( p_sub = subtitle_New( p_input, NULL, i_microsecondperframe ) ) )
    {
        TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );

        /* see if it should be selected */
        var_Get( p_sub, "sub-file", &val );
        if( val.psz_string && *val.psz_string )
        {
            subtitle_Select( p_sub );
        }
    }

735
    return VLC_SUCCESS;
Michel Kaempf's avatar
Michel Kaempf committed
736
737
}

738
/*****************************************************************************
739
 * ErrorThread: RunThread() error loop
740
 *****************************************************************************
741
 * This function is called when an error occured during thread main's loop.
742
 *****************************************************************************/
743
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
744
{
745
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
746
    {
747
748
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
749
750
751
    }
}

752
/*****************************************************************************
753
 * EndThread: end the input thread
754
 *****************************************************************************/
755
static void EndThread( input_thread_t * p_input )
756
{
757
    int i;
758
#ifdef HAVE_SYS_TIMES_H
759
760
761
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
   
Sam Hocevar committed
762

763
    msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
764
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
765
#else
766
    msg_Dbg( p_input, "%ld loops", p_input->c_loops );
767
#endif
768

769
770
    input_DumpStream( p_input );

Sam Hocevar's avatar
   
Sam Hocevar committed
771
772
773
    /* Free all ES and destroy all decoder threads */
    input_EndStream( p_input );

774
775
776
    /* Close optional stream output instance */
    if ( p_input->stream.p_sout != NULL )
    {
777
778
        vlc_object_t *p_pl =
            vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
        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 );
        }
797
798
    }

799
800
801
802
803
804
805
806
807
808
809
810
811
    /* Destroy subtitles demuxers */
    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 );
    }

    /* Free input_thread_sys_t */
    free( p_input->p_sys );

Sam Hocevar's avatar
   
Sam Hocevar committed
812
    /* Free demultiplexer's data */
813
    module_Unneed( p_input, p_input->p_demux );
Sam Hocevar's avatar
   
Sam Hocevar committed
814

815
816
817
    /* Destroy the stream_t facilities */
    stream_Release( p_input->s );

818
819
820
    /* Destroy es out */
    EsOutRelease( p_input->p_es_out );

821
    /* Close the access plug-in */
822
    module_Unneed( p_input, p_input->p_access );
Sam Hocevar's avatar
   
Sam Hocevar committed
823

Christophe Massiot's avatar
Christophe Massiot committed
824
825
    input_AccessEnd( p_input );

826
827
828
829
    /* 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
830
    free( p_input->psz_source );
831
    if ( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
832
833
834

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

gbazin's avatar
   
gbazin committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
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
/*****************************************************************************
 * ParseOption: parses the options for the input
 *****************************************************************************
 * This function parses the input (config) options and creates their associated
 * object variables.
 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
 * bar is the value of the option.
 *****************************************************************************/
static void ParseOption( input_thread_t *p_input, const char *psz_option )
{
    char *psz_name = (char *)psz_option;
    char *psz_value = strchr( psz_option, '=' );
    int  i_name_len, i_type;
    vlc_bool_t b_isno = VLC_FALSE;
    vlc_value_t val;

    if( psz_value ) i_name_len = psz_value - psz_option;
    else i_name_len = strlen( psz_option );

    /* It's too much of an hassle to remove the ':' when we parse
     * the cmd line :) */
    if( i_name_len && *psz_name == ':' )
    {
        psz_name++;
        i_name_len--;
    }

    if( i_name_len == 0 ) return;

    psz_name = strndup( psz_name, i_name_len );
    if( psz_value ) psz_value++;

    i_type = config_GetType( p_input, psz_name );

    if( !i_type && !psz_value )
    {
        /* check for "no-foo" or "nofoo" */
        if( !strncmp( psz_name, "no-", 3 ) )
        {
            memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
        }
        else if( !strncmp( psz_name, "no", 2 ) )
        {
            memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
        }
        else goto cleanup;           /* Option doesn't exist */

        b_isno = VLC_TRUE;
        i_type = config_GetType( p_input, psz_name );

        if( !i_type ) goto cleanup;  /* Option doesn't exist */
    }
    else if( !i_type ) goto cleanup; /* Option doesn't exist */

    if( ( i_type != VLC_VAR_BOOL ) &&
        ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */

    /* Create the variable in the input object.
     * Children of the input object will be able to retreive this value
     * thanks to the inheritance property of the object variables. */
    var_Create( p_input, psz_name, i_type );

    switch( i_type )
    {
    case VLC_VAR_BOOL:
        val.b_bool = !b_isno;
        break;

    case VLC_VAR_INTEGER:
        val.i_int = atoi( psz_value );
        break;

    case VLC_VAR_FLOAT:
        val.f_float = atof( psz_value );
        break;

    case VLC_VAR_STRING:
    case VLC_VAR_FILE:
    case VLC_VAR_DIRECTORY:
        val.psz_string = psz_value;
        break;

    default:
        goto cleanup;
        break;
    }

    var_Set( p_input, psz_name, val );

    msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );

  cleanup:
    if( psz_name ) free( psz_name );
    return;
}
932

933
934
935
936
937
938
939
940
941
/*****************************************************************************
 * es_out_t input handler
 *****************************************************************************/
struct es_out_sys_t
{
    input_thread_t *p_input;

    int         i_id;
    es_out_id_t **id;
942
943
944

    vlc_bool_t  i_audio;
    vlc_bool_t  i_video;
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
};
struct es_out_id_t
{
    es_descriptor_t *p_es;
};

static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
static int          EsOutSend   ( es_out_t *, es_out_id_t *, pes_packet_t * );
static void         EsOutDel    ( es_out_t *, es_out_id_t * );
static int          EsOutControl( es_out_t *, int i_query, va_list );

static es_out_t *EsOutCreate( input_thread_t *p_input )
{
    es_out_t *out = malloc( sizeof( es_out_t ) );

    out->pf_add     = EsOutAdd;
    out->pf_send    = EsOutSend;
    out->pf_del     = EsOutDel;
    out->pf_control = EsOutControl;

    out->p_sys = malloc( sizeof( es_out_sys_t ) );
    out->p_sys->p_input = p_input;
    out->p_sys->i_id    = 0;
    out->p_sys->id      = NULL;
969
970
    out->p_sys->i_audio = -1;
    out->p_sys->i_video = -1;
971
972
973
974
    return out;
}
static void      EsOutRelease( es_out_t *out )
{
975
976
977
978
979
980
981
982
983
984
985
986
    es_out_sys_t *p_sys = out->p_sys;
    int i;

    for( i = 0; i < p_sys->i_id; i++ )
    {
        free( p_sys->id[i] );
    }
    if( p_sys->id )
    {
        free( p_sys->id );
    }
    free( p_sys );
987
988
989
990
991
    free( out );
}

static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
{
992
993
994
995
996
997
998
999
1000
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    es_out_id_t       *id = malloc( sizeof( es_out_id_t ) );
    pgrm_descriptor_t *p_prgm = NULL;

    vlc_mutex_lock( &p_input->stream.stream_lock );
    if( fmt->i_group >= 0 )
    {
        /* search program */