input.c 23.1 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.236 2003/08/02 15:22:07 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>
Sam Hocevar's avatar
   
Sam Hocevar committed
32

33
#include <string.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
43
#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
44
#include "input_ext-plugins.h"
Michel Lespinasse's avatar
Yop,    
Michel Lespinasse committed
45

46
#include "stream_output.h"
47
#include <vlc/vout.h>
48

49
#include "vlc_interface.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
50

51
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
52
 * Local prototypes
53
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
54
static  int RunThread       ( input_thread_t *p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
55
56
57
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
58

gbazin's avatar
   
gbazin committed
59
60
61
static void ParseOption     ( input_thread_t *p_input,
                              const char *psz_option );

62
/*****************************************************************************
63
 * input_CreateThread: creates a new input thread
64
 *****************************************************************************
65
66
 * This function creates a new input, and returns a pointer
 * to its description. On error, it returns NULL.
67
 *****************************************************************************/
68
input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
69
                                      playlist_item_t *p_item )
Michel Kaempf's avatar
Michel Kaempf committed
70
{
71
    input_thread_t *    p_input;                        /* thread descriptor */
72
    input_info_category_t * p_info;
gbazin's avatar
   
gbazin committed
73
    int i;
74

75
    /* Allocate descriptor */
76
    p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
77
    if( p_input == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
78
    {
79
        msg_Err( p_parent, "out of memory" );
80
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
81
    }
82

gbazin's avatar
   
gbazin committed
83
84
85
86
87
88
    /* 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
89
90
91
92
93
94
95
96
97
    /* 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 );
gbazin's avatar
   
gbazin committed
98

99
    /* Initialize thread properties */
Sam Hocevar's avatar
   
Sam Hocevar committed
100
    p_input->b_eof      = 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
101
102

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

Stéphane Borel's avatar
   
Stéphane Borel committed
105
    /* Demux */
106
107
108
109
    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
110
111

    /* Access */
112
    p_input->p_access = NULL;
113

Sam Hocevar's avatar
   
Sam Hocevar committed
114
115
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;
gbazin's avatar
   
gbazin committed
116
    p_input->i_pts_delay = DEFAULT_PTS_DELAY;
Sam Hocevar's avatar
   
Sam Hocevar committed
117

118
119
120
121
122
123
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

    /* Set locks. */
124
    vlc_mutex_init( p_input, &p_input->stream.stream_lock );
125
    vlc_cond_init( p_input, &p_input->stream.stream_wait );
126
    vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
Michel Kaempf's avatar
Michel Kaempf committed
127

128
    /* Initialize stream description */
Stéphane Borel's avatar
   
Stéphane Borel committed
129
    p_input->stream.b_changed = 0;
130
131
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
132
    p_input->stream.i_pgrm_number = 0;
133
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
134
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
135
    p_input->stream.i_mux_rate = 0;
136
    p_input->stream.b_seekable = 0;
137
    p_input->stream.p_sout = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
138

Sam Hocevar's avatar
   
Sam Hocevar committed
139
140
141
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
   
Stéphane Borel committed
142
143
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
144
    p_input->stream.p_selected_area = NULL;
145
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
146

Sam Hocevar's avatar
   
Sam Hocevar committed
147
148
149
150
    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
151
    /* By default there is one area in a stream */
gbazin's avatar
   
gbazin committed
152
    input_AddArea( p_input, 0, 1 );
153
    p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
Stéphane Borel's avatar
   
Stéphane Borel committed
154

155
156
157
158
    /* Initialize stream control properties. */
    p_input->stream.control.i_status = PLAYING_S;
    p_input->stream.control.i_rate = DEFAULT_RATE;
    p_input->stream.control.b_mute = 0;
159
160
    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );

161
162
163
164
165
166
167
168
169
170
171
    /* 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;

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

174
175
    p_info = input_InfoCategory( p_input, _("General") );
    input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
176
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
   
Sam Hocevar committed
177

Sam Hocevar's avatar
Sam Hocevar committed
178
    /* Create thread and wait for its readiness. */
179
180
    if( vlc_thread_create( p_input, "input", RunThread,
                           VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
Michel Kaempf's avatar
Michel Kaempf committed
181
    {
182
        msg_Err( p_input, "cannot create input thread" );
Michel Kaempf's avatar
Michel Kaempf committed
183
        free( p_input );
184
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
185
    }
186

187
    return p_input;
Michel Kaempf's avatar
Michel Kaempf committed
188
189
}

190
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
191
 * input_StopThread: mark an input thread as zombie
192
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
193
 * This function should not return until the thread is effectively cancelled.
194
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
195
void input_StopThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
196
{
Sam Hocevar's avatar
   
Sam Hocevar committed
197
198
    /* Make the thread exit from a possible vlc_cond_wait() */
    vlc_mutex_lock( &p_input->stream.stream_lock );
199
200
    /* Request thread destruction */
    p_input->b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
201

202
203
    vlc_cond_signal( &p_input->stream.stream_wait );
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
204
205
206
207
208
209
210
211
212
213
}

/*****************************************************************************
 * 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 */
214
    vlc_thread_join( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
215
216
217

    /* Destroy Mutex locks */
    vlc_mutex_destroy( &p_input->stream.control.control_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
218
    vlc_cond_destroy( &p_input->stream.stream_wait );
Sam Hocevar's avatar
   
Sam Hocevar committed
219
    vlc_mutex_destroy( &p_input->stream.stream_lock );
Michel Kaempf's avatar
Michel Kaempf committed
220
221
}

222
/*****************************************************************************
223
 * RunThread: main thread loop
224
 *****************************************************************************
225
 * Thread in charge of processing the network packets and demultiplexing.
226
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
227
static int RunThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
228
{
Sam Hocevar's avatar
Sam Hocevar committed
229
230
231
    /* Signal right now, otherwise we'll get stuck in a peek */
    vlc_thread_ready( p_input );

Sam Hocevar's avatar
   
Sam Hocevar committed
232
233
234
235
236
    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
237
        p_input->b_dead = 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
238
        return 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
239
    }
Michel Kaempf's avatar
Michel Kaempf committed
240

241
    /* initialization is complete */
242
243
244
245
    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_input->stream.b_changed = 1;
    vlc_mutex_unlock( &p_input->stream.stream_lock );

Sam Hocevar's avatar
   
Sam Hocevar committed
246
247
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
248
        unsigned int i, i_count;
249

Sam Hocevar's avatar
   
Sam Hocevar committed
250
        p_input->c_loops++;
Sam Hocevar's avatar
   
Sam Hocevar committed
251

252
        vlc_mutex_lock( &p_input->stream.stream_lock );
253

254
255
256
257
258
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
   
Johan Bilien committed
259
260
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
261
262

                p_input->pf_set_program( p_input,
263
                                         p_input->stream.p_new_program );
264

265
266
267
268
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

269
270
271
272
273
274
275
276
277
278
279
                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;
        }
280

281
282
        if( p_input->stream.p_new_area )
        {
283
284
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
285
                input_AccessReinit( p_input );
286
287
288

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

289
290
291
292
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

293
294
295
296
297
298
299
300
301
                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;
                }
            }
302
303
304
            p_input->stream.p_new_area = NULL;
        }

305
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
   
Sam Hocevar committed
306
        {
307
308
            if( p_input->stream.b_seekable
                 && p_input->pf_seek != NULL )
309
            {
310
311
312
313
314
315
                off_t i_new_pos;

                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );

                i_new_pos = p_input->stream.p_selected_area->i_seek;
Christophe Massiot's avatar
Christophe Massiot committed
316
317
318
                vlc_mutex_unlock( &p_input->stream.stream_lock );
                p_input->pf_seek( p_input, i_new_pos );
                vlc_mutex_lock( &p_input->stream.stream_lock );
319

320
321
322
323
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

324
325
326
327
328
329
330
331
332
                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;
                }
            }
333
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
   
Sam Hocevar committed
334
        }
335

336
337
338
339
340
341
342
343
344
345
346
347
        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;
        }

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

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

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

gbazin's avatar
   
gbazin committed
367
        if( i_count == 0 )
Sam Hocevar's avatar
   
Sam Hocevar committed
368
        {
369
            /* End of file - we do not set b_die because only the
370
             * playlist is allowed to do so. */
371
            msg_Info( p_input, "EOF reached" );
372
373
374
375
376
            p_input->b_eof = 1;
        }
        else if( i_count < 0 )
        {
            p_input->b_error = 1;
377
378
379
        }
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
380
    if( p_input->b_error || p_input->b_eof )
381
382
383
    {
        ErrorThread( p_input );
    }
384

385
    EndThread( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
386

Sam Hocevar's avatar
   
Sam Hocevar committed
387
    return 0;
388
389
}

390
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
391
 * InitThread: init the input Thread
392
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
393
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
394
{
Christophe Massiot's avatar
Christophe Massiot committed
395
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
396
    char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
gbazin's avatar
   
gbazin committed
397
    vlc_value_t val;
gbazin's avatar
   
gbazin committed
398

Christophe Massiot's avatar
Christophe Massiot committed
399
400
401
402
403
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
404
#if defined( WIN32 ) || defined( UNDER_CE )
Christophe Massiot's avatar
Christophe Massiot committed
405
    if( psz_parser - p_input->psz_dupsource == 1 )
gbazin's avatar
   
gbazin committed
406
    {
407
        msg_Warn( p_input, "drive letter %c: found in source string",
Christophe Massiot's avatar
Christophe Massiot committed
408
                           p_input->psz_dupsource[0] ) ;
Stéphane Borel's avatar
   
Stéphane Borel committed
409
        psz_parser = "";
gbazin's avatar
   
gbazin committed
410
411
    }
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
412

Christophe Massiot's avatar
Christophe Massiot committed
413
    if( !*psz_parser )
414
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
415
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
416
        p_input->psz_name = p_input->psz_source;
Christophe Massiot's avatar
Christophe Massiot committed
417
        free( p_input->psz_dupsource );
418
        p_input->psz_dupsource = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
419
    }
Christophe Massiot's avatar
Christophe Massiot committed
420
421
422
    else
    {
        *psz_parser++ = '\0';
423

Xavier Marchesini's avatar
   
Xavier Marchesini committed
424
425
426
427
        /* let's skip '//' */
        if( psz_parser[0] == '/' && psz_parser[1] == '/' )
        {
            psz_parser += 2 ;
428
        }
Xavier Marchesini's avatar
   
Xavier Marchesini committed
429
430

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

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

Stéphane Borel's avatar
   
Stéphane Borel committed
435
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
436
437
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
438
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
439
440
441
442
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
443
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
            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
460

Christophe Massiot's avatar
Christophe Massiot committed
461
462
463
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
   
Sam Hocevar committed
464
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
465
466
467
468
469
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
470
    }
Christophe Massiot's avatar
Christophe Massiot committed
471

472
473
    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
474
475

    if( input_AccessInit( p_input ) == -1 )
476
    {
477
        return -1;
478
    }
Christophe Massiot's avatar
Christophe Massiot committed
479

480
    /* Find and open appropriate access module */
481
482
    p_input->p_access = module_Need( p_input, "access",
                                     p_input->psz_access );
Christophe Massiot's avatar
Christophe Massiot committed
483

484
485
486
487
488
489
490
491
492
493
494
495
496
497
    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 );
    }

498
    if( p_input->p_access == NULL )
499
    {
500
        msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
501
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
502
        return -1;
503
    }
Christophe Massiot's avatar
Christophe Massiot committed
504
505
506

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
507
    {
Christophe Massiot's avatar
Christophe Massiot committed
508
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
509
    }
510
511
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
512
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
513
    }
514

gbazin's avatar
   
gbazin committed
515
516
517
518
519
    /* 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
520
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
521
    {
Christophe Massiot's avatar
Christophe Massiot committed
522
523
        while( !input_FillBuffer( p_input ) )
        {
524
            if( p_input->b_die || p_input->b_error || p_input->b_eof )
Christophe Massiot's avatar
Christophe Massiot committed
525
            {
526
                module_Unneed( p_input, p_input->p_access );
527
                return -1;
Christophe Massiot's avatar
Christophe Massiot committed
528
529
            }
        }
530
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
531

532
    /* Find and open appropriate demux module */
533
534
    p_input->p_demux = module_Need( p_input, "demux",
                                    p_input->psz_demux );
Sam Hocevar's avatar
   
Sam Hocevar committed
535

536
    if( p_input->p_demux == NULL )
537
    {
538
        msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
539
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
540
        module_Unneed( p_input, p_input->p_access );
541
        return -1;
542
543
    }

544
    /* Initialize optional stream output. */
gbazin's avatar
   
gbazin committed
545
    var_Get( p_input, "sout", &val );
gbazin's avatar
   
gbazin committed
546
    if( val.psz_string != NULL )
547
    {
gbazin's avatar
   
gbazin committed
548
549
        if ( *val.psz_string && (p_input->stream.p_sout =
             sout_NewInstance( p_input, val.psz_string )) == NULL )
550
551
        {
            msg_Err( p_input, "cannot start stream output instance, aborting" );
gbazin's avatar
   
gbazin committed
552
            free( val.psz_string );
553
554
555
556
557
            module_Unneed( p_input, p_input->p_access );
            module_Unneed( p_input, p_input->p_demux );
            return -1;
        }

gbazin's avatar
   
gbazin committed
558
        free( val.psz_string );
559
560
    }

561
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
562
563
}

564
/*****************************************************************************
565
 * ErrorThread: RunThread() error loop
566
 *****************************************************************************
567
 * This function is called when an error occured during thread main's loop.
568
 *****************************************************************************/
569
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
570
{
571
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
572
    {
573
574
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
575
576
577
    }
}

578
/*****************************************************************************
579
 * EndThread: end the input thread
580
 *****************************************************************************/
581
static void EndThread( input_thread_t * p_input )
582
{
gbazin's avatar
   
gbazin committed
583
584
    vlc_object_t *p_object;

585
#ifdef HAVE_SYS_TIMES_H
586
587
588
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
   
Sam Hocevar committed
589

590
    msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
591
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
592
#else
593
    msg_Dbg( p_input, "%ld loops", p_input->c_loops );
594
#endif
595

596
597
    input_DumpStream( p_input );

Sam Hocevar's avatar
   
Sam Hocevar committed
598
599
600
    /* Free all ES and destroy all decoder threads */
    input_EndStream( p_input );

601
602
603
604
605
606
    /* Close optional stream output instance */
    if ( p_input->stream.p_sout != NULL )
    {
        sout_DeleteInstance( p_input->stream.p_sout );
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
607
    /* Free demultiplexer's data */
608
    module_Unneed( p_input, p_input->p_demux );
Sam Hocevar's avatar
   
Sam Hocevar committed
609

610
    /* Close the access plug-in */
611
    module_Unneed( p_input, p_input->p_access );
Sam Hocevar's avatar
   
Sam Hocevar committed
612

Christophe Massiot's avatar
Christophe Massiot committed
613
614
    input_AccessEnd( p_input );

615
616
617
618
    /* Free info structures XXX destroy es before 'cause vorbis */
    msg_Dbg( p_input, "freeing info structures...");
    input_DelInfo( p_input );

gbazin's avatar
   
gbazin committed
619
620
    /* Close the video output that should have been re-attached
     * to our object */
621
    while( ( p_object = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD ) ) != NULL )
gbazin's avatar
   
gbazin committed
622
623
624
    {
        vlc_object_detach( p_object );
        vlc_object_release( p_object );
625
        vout_Destroy( (vout_thread_t *)p_object );
gbazin's avatar
   
gbazin committed
626
627
    }

Christophe Massiot's avatar
Christophe Massiot committed
628
    free( p_input->psz_source );
629
    if ( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
630
631
632

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

gbazin's avatar
   
gbazin committed
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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
/*****************************************************************************
 * 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;
}