input.c 18.3 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.215 2002/11/11 14:39:12 sam 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

Sam Hocevar's avatar
 
Sam Hocevar committed
39
#include "netutils.h"
40
#include "vlc_playlist.h"
41

42 43 44
#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
45
#include "input_ext-plugins.h"
Michel Lespinasse's avatar
Yop,  
Michel Lespinasse committed
46

47 48
#include "stream_output.h"

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

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

59
/*****************************************************************************
60
 * input_CreateThread: creates a new input thread
61
 *****************************************************************************
62 63 64 65
 * This function creates a new input, and returns a pointer
 * to its description. On error, it returns NULL.
 * If pi_status is NULL, then the function will block until the thread is ready.
 * If not, it will be updated using one of the THREAD_* constants.
66
 *****************************************************************************/
67 68
input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
                                      playlist_item_t *p_item, int *pi_status )
Michel Kaempf's avatar
Michel Kaempf committed
69
{
70
    input_thread_t *    p_input;                        /* thread descriptor */
71
    input_info_category_t * p_info;
72

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

    /* Initialize thread properties */
Sam Hocevar's avatar
 
Sam Hocevar committed
82
    p_input->b_eof      = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
83 84

    /* Set target */
Christophe Massiot's avatar
Christophe Massiot committed
85
    p_input->psz_source = strdup( p_item->psz_name );
Sam Hocevar's avatar
 
Sam Hocevar committed
86

Stéphane Borel's avatar
 
Stéphane Borel committed
87
    /* Demux */
88
    p_input->p_demux = NULL;
Stéphane Borel's avatar
 
Stéphane Borel committed
89 90

    /* Access */
91
    p_input->p_access = NULL;
92
    
Sam Hocevar's avatar
 
Sam Hocevar committed
93 94 95
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;

96 97 98 99 100 101
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

    /* Set locks. */
102
    vlc_mutex_init( p_input, &p_input->stream.stream_lock );
103
    vlc_cond_init( p_input, &p_input->stream.stream_wait );
104
    vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
Michel Kaempf's avatar
Michel Kaempf committed
105

106
    /* Initialize stream description */
Stéphane Borel's avatar
 
Stéphane Borel committed
107
    p_input->stream.b_changed = 0;
108 109
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
110
    p_input->stream.i_pgrm_number = 0;
111
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
112
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
113
    p_input->stream.i_mux_rate = 0;
114
    p_input->stream.b_seekable = 0;
115
    p_input->stream.p_sout = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
116

Sam Hocevar's avatar
 
Sam Hocevar committed
117 118 119
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
 
Stéphane Borel committed
120 121
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
122
    p_input->stream.p_selected_area = NULL;
123
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
124

Sam Hocevar's avatar
 
Sam Hocevar committed
125 126 127 128
    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
129
    /* By default there is one area in a stream */
Stéphane Borel's avatar
 
Stéphane Borel committed
130
    input_AddArea( p_input );
131
    p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
Stéphane Borel's avatar
 
Stéphane Borel committed
132

133 134 135 136
    /* 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;
137 138
    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );

139 140 141 142 143 144 145 146 147 148 149
    /* 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;

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

152 153
    p_info = input_InfoCategory( p_input, "General" );
    input_AddInfo( p_info, "playlist item", p_input->psz_source );
154
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
 
Sam Hocevar committed
155

Sam Hocevar's avatar
Sam Hocevar committed
156
    /* Create thread and wait for its readiness. */
157 158
    if( vlc_thread_create( p_input, "input", RunThread,
                           VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
Michel Kaempf's avatar
Michel Kaempf committed
159
    {
160
        msg_Err( p_input, "cannot create input thread" );
Michel Kaempf's avatar
Michel Kaempf committed
161
        free( p_input );
162
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
163
    }
164

165
    return p_input;
Michel Kaempf's avatar
Michel Kaempf committed
166 167
}

168
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
169
 * input_StopThread: mark an input thread as zombie
170
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
171
 * This function should not return until the thread is effectively cancelled.
172
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
173
void input_StopThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
174
{
Sam Hocevar's avatar
 
Sam Hocevar committed
175 176
    /* Make the thread exit from a possible vlc_cond_wait() */
    vlc_mutex_lock( &p_input->stream.stream_lock );
177 178
    /* Request thread destruction */
    p_input->b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
179

180 181
    vlc_cond_signal( &p_input->stream.stream_wait );
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
182 183 184 185 186 187 188 189 190 191
}

/*****************************************************************************
 * 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 */
192
    vlc_thread_join( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
193 194 195

    /* Destroy Mutex locks */
    vlc_mutex_destroy( &p_input->stream.control.control_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
196
    vlc_cond_destroy( &p_input->stream.stream_wait );
Sam Hocevar's avatar
 
Sam Hocevar committed
197
    vlc_mutex_destroy( &p_input->stream.stream_lock );
Michel Kaempf's avatar
Michel Kaempf committed
198 199
}

200
/*****************************************************************************
201
 * RunThread: main thread loop
202
 *****************************************************************************
203
 * Thread in charge of processing the network packets and demultiplexing.
204
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
205
static int RunThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
206
{
Sam Hocevar's avatar
Sam Hocevar committed
207 208 209
    /* Signal right now, otherwise we'll get stuck in a peek */
    vlc_thread_ready( p_input );

Sam Hocevar's avatar
 
Sam Hocevar committed
210 211 212 213 214
    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
215
        p_input->b_dead = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
216
        return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
217
    }
Michel Kaempf's avatar
Michel Kaempf committed
218

219
    /* initialization is complete */
220 221 222 223
    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
224 225
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
Christophe Massiot's avatar
Christophe Massiot committed
226
        int i, i_count;
227

Sam Hocevar's avatar
 
Sam Hocevar committed
228
        p_input->c_loops++;
Sam Hocevar's avatar
 
Sam Hocevar committed
229

230
        vlc_mutex_lock( &p_input->stream.stream_lock );
231

232 233 234 235 236
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
 
Johan Bilien committed
237 238 239
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
                
240
                p_input->pf_set_program( p_input, 
241
                                         p_input->stream.p_new_program );
242

243 244 245 246
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

247 248 249 250 251 252 253 254 255 256 257 258
                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;
        }
        
259 260
        if( p_input->stream.p_new_area )
        {
261 262
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
263
                input_AccessReinit( p_input );
264 265 266

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

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

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;
                }
            }
280 281 282
            p_input->stream.p_new_area = NULL;
        }

283
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
 
Sam Hocevar committed
284
        {
285 286
            if( p_input->stream.b_seekable
                 && p_input->pf_seek != NULL )
287
            {
288 289 290 291 292 293
                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
294 295 296
                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 );
297

298 299 300 301
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

302 303 304 305 306 307 308 309 310
                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;
                }
            }
311
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
 
Sam Hocevar committed
312
        }
313

314 315 316 317 318 319 320 321 322 323 324 325
        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;
        }

326 327 328 329 330 331 332 333 334 335 336 337 338 339
        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;
        }

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

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

345
        if( i_count == 0 && p_input->stream.b_seekable )
Sam Hocevar's avatar
 
Sam Hocevar committed
346
        {
347
            /* End of file - we do not set b_die because only the
348
             * playlist is allowed to do so. */
349
            msg_Info( p_input, "EOF reached" );
350 351 352 353 354
            p_input->b_eof = 1;
        }
        else if( i_count < 0 )
        {
            p_input->b_error = 1;
355 356 357
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
358
    if( p_input->b_error || p_input->b_eof )
359 360 361
    {
        ErrorThread( p_input );
    }
362

363
    EndThread( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
364

Sam Hocevar's avatar
 
Sam Hocevar committed
365
    return 0;
366 367
}

368
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
369
 * InitThread: init the input Thread
370
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
371
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
372
{
Christophe Massiot's avatar
Christophe Massiot committed
373 374
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
    char * psz_parser = p_input->psz_source;
Gildas Bazin's avatar
 
Gildas Bazin committed
375

Christophe Massiot's avatar
Christophe Massiot committed
376 377 378 379 380
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
381
#if defined( WIN32 ) || defined( UNDER_CE )
Gildas Bazin's avatar
 
Gildas Bazin committed
382
    if( psz_parser - p_input->psz_source == 1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
383
    {
384
        msg_Warn( p_input, "drive letter %c: found in source string",
385
                           p_input->psz_source[0] ) ;
Stéphane Borel's avatar
 
Stéphane Borel committed
386
        psz_parser = "";
Gildas Bazin's avatar
 
Gildas Bazin committed
387 388
    }
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
389

Christophe Massiot's avatar
Christophe Massiot committed
390
    if( !*psz_parser )
391
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
392
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
393
        p_input->psz_name = p_input->psz_source;
Michel Kaempf's avatar
Michel Kaempf committed
394
    }
Christophe Massiot's avatar
Christophe Massiot committed
395 396 397
    else
    {
        *psz_parser++ = '\0';
398

Xavier Marchesini's avatar
 
Xavier Marchesini committed
399 400 401 402 403 404 405
        /* let's skip '//' */
        if( psz_parser[0] == '/' && psz_parser[1] == '/' )
        {
            psz_parser += 2 ;
        } 

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

Christophe Massiot's avatar
Christophe Massiot committed
407 408
        /* Come back to parse the access and demux plug-ins */
        psz_parser = p_input->psz_source;
Gildas Bazin's avatar
 
Gildas Bazin committed
409

Stéphane Borel's avatar
 
Stéphane Borel committed
410
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
411 412
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
413
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
414 415 416 417
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
418
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
            psz_parser++;
        }
        else
        {
            p_input->psz_access = psz_parser;

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

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

Christophe Massiot's avatar
Christophe Massiot committed
436 437 438
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
 
Sam Hocevar committed
439
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
440 441 442 443 444
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
445
    }
Christophe Massiot's avatar
Christophe Massiot committed
446

447 448
    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
449 450

    if( input_AccessInit( p_input ) == -1 )
451
    {
452
        return -1;
453
    }
Christophe Massiot's avatar
Christophe Massiot committed
454

455
    /* Find and open appropriate access module */
456 457
    p_input->p_access = module_Need( p_input, "access",
                                     p_input->psz_access );
Christophe Massiot's avatar
Christophe Massiot committed
458

459
    if( p_input->p_access == NULL )
460
    {
461
        msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
462
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
463
        return -1;
464
    }
Christophe Massiot's avatar
Christophe Massiot committed
465 466 467

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
468
    {
Christophe Massiot's avatar
Christophe Massiot committed
469
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
470
    }
471 472
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
473
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
474
    }
475

Sam Hocevar's avatar
 
Sam Hocevar committed
476
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
477
    {
Christophe Massiot's avatar
Christophe Massiot committed
478 479
        while( !input_FillBuffer( p_input ) )
        {
480
            if( p_input->b_die || p_input->b_error || p_input->b_eof )
Christophe Massiot's avatar
Christophe Massiot committed
481
            {
482
                module_Unneed( p_input, p_input->p_access );
483
                return -1;
Christophe Massiot's avatar
Christophe Massiot committed
484 485
            }
        }
486
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
487

488
    /* Find and open appropriate demux module */
489 490
    p_input->p_demux = module_Need( p_input, "demux",
                                    p_input->psz_demux );
Sam Hocevar's avatar
 
Sam Hocevar committed
491

492
    if( p_input->p_demux== NULL )
493
    {
494
        msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
495
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
496
        module_Unneed( p_input, p_input->p_access );
497
        return -1;
498 499
    }

500 501 502 503
    /* Initialize optional stream output. */
    psz_parser = config_GetPsz( p_input, "sout" );
    if ( psz_parser != NULL )
    {
504 505 506
        if ( *psz_parser &&
             (p_input->stream.p_sout = sout_NewInstance( p_input, psz_parser ))
               == NULL )
507 508 509 510 511 512 513 514 515 516 517
        {
            msg_Err( p_input, "cannot start stream output instance, aborting" );
            free( psz_parser );
            module_Unneed( p_input, p_input->p_access );
            module_Unneed( p_input, p_input->p_demux );
            return -1;
        }

        free( psz_parser );
    }

518
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
519 520
}

521
/*****************************************************************************
522
 * ErrorThread: RunThread() error loop
523
 *****************************************************************************
524
 * This function is called when an error occured during thread main's loop.
525
 *****************************************************************************/
526
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
527
{
528
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
529
    {
530 531
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
532 533 534
    }
}

535
/*****************************************************************************
536
 * EndThread: end the input thread
537
 *****************************************************************************/
538
static void EndThread( input_thread_t * p_input )
539
{
540
#ifdef HAVE_SYS_TIMES_H
541 542 543
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
 
Sam Hocevar committed
544

545 546
    msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
547
#else
548
    msg_Dbg( p_input, "%d loops", p_input->c_loops );
549
#endif
550

551 552 553 554
    /* Free info structures */
    msg_Dbg( p_input, "freeing info structures...");
    input_DelInfo( p_input );
    
555 556
    input_DumpStream( p_input );

Sam Hocevar's avatar
 
Sam Hocevar committed
557 558 559
    /* Free all ES and destroy all decoder threads */
    input_EndStream( p_input );

560 561 562 563 564 565
    /* 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
566
    /* Free demultiplexer's data */
567
    module_Unneed( p_input, p_input->p_demux );
Sam Hocevar's avatar
 
Sam Hocevar committed
568

569
    /* Close the access plug-in */
570
    module_Unneed( p_input, p_input->p_access );
Sam Hocevar's avatar
 
Sam Hocevar committed
571

Christophe Massiot's avatar
Christophe Massiot committed
572 573 574
    input_AccessEnd( p_input );

    free( p_input->psz_source );
575 576 577

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