input.c 18.5 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.214 2002/11/10 18:04:23 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 34 35 36 37 38 39 40 41 42
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif

43
#include <string.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
44

45 46
#ifdef HAVE_SYS_TIMES_H
#   include <sys/times.h>
47
#endif
48

Sam Hocevar's avatar
 
Sam Hocevar committed
49
#include "netutils.h"
50
#include "vlc_playlist.h"
51

52 53 54
#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
55
#include "input_ext-plugins.h"
Michel Lespinasse's avatar
Yop,  
Michel Lespinasse committed
56

57 58
#include "stream_output.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
59 60
#include "interface.h"

61
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
62
 * Local prototypes
63
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
64
static  int RunThread       ( input_thread_t *p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
65 66 67
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
68

69
/*****************************************************************************
70
 * input_CreateThread: creates a new input thread
71
 *****************************************************************************
72 73 74 75
 * 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.
76
 *****************************************************************************/
77 78
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
79
{
80
    input_thread_t *    p_input;                        /* thread descriptor */
81
    input_info_category_t * p_info;
82

83
    /* Allocate descriptor */
84
    p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
85
    if( p_input == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
86
    {
87
        msg_Err( p_parent, "out of memory" );
88
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
89
    }
90 91

    /* Initialize thread properties */
Sam Hocevar's avatar
 
Sam Hocevar committed
92
    p_input->b_eof      = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
93 94

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

Stéphane Borel's avatar
 
Stéphane Borel committed
97
    /* Demux */
98
    p_input->p_demux = NULL;
Stéphane Borel's avatar
 
Stéphane Borel committed
99 100

    /* Access */
101
    p_input->p_access = NULL;
102
    
Sam Hocevar's avatar
 
Sam Hocevar committed
103 104 105
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;

106 107 108 109 110 111
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

    /* Set locks. */
112
    vlc_mutex_init( p_input, &p_input->stream.stream_lock );
113
    vlc_cond_init( p_input, &p_input->stream.stream_wait );
114
    vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
Michel Kaempf's avatar
Michel Kaempf committed
115

116
    /* Initialize stream description */
Stéphane Borel's avatar
 
Stéphane Borel committed
117
    p_input->stream.b_changed = 0;
118 119
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
120
    p_input->stream.i_pgrm_number = 0;
121
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
122
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
123
    p_input->stream.i_mux_rate = 0;
124
    p_input->stream.b_seekable = 0;
125
    p_input->stream.p_sout = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
126

Sam Hocevar's avatar
 
Sam Hocevar committed
127 128 129
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
 
Stéphane Borel committed
130 131
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
132
    p_input->stream.p_selected_area = NULL;
133
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
134

Sam Hocevar's avatar
 
Sam Hocevar committed
135 136 137 138
    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
139
    /* By default there is one area in a stream */
Stéphane Borel's avatar
 
Stéphane Borel committed
140
    input_AddArea( p_input );
141
    p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
Stéphane Borel's avatar
 
Stéphane Borel committed
142

143 144 145 146
    /* 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;
147 148
    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );

149 150 151 152 153 154 155 156 157 158 159
    /* 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;

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

162 163
    p_info = input_InfoCategory( p_input, "General" );
    input_AddInfo( p_info, "playlist item", p_input->psz_source );
164
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
 
Sam Hocevar committed
165

Sam Hocevar's avatar
Sam Hocevar committed
166
    /* Create thread and wait for its readiness. */
167 168
    if( vlc_thread_create( p_input, "input", RunThread,
                           VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
Michel Kaempf's avatar
Michel Kaempf committed
169
    {
170
        msg_Err( p_input, "cannot create input thread" );
Michel Kaempf's avatar
Michel Kaempf committed
171
        free( p_input );
172
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
173
    }
174

175
    return p_input;
Michel Kaempf's avatar
Michel Kaempf committed
176 177
}

178
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
179
 * input_StopThread: mark an input thread as zombie
180
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
181
 * This function should not return until the thread is effectively cancelled.
182
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
183
void input_StopThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
184
{
Sam Hocevar's avatar
 
Sam Hocevar committed
185 186
    /* Make the thread exit from a possible vlc_cond_wait() */
    vlc_mutex_lock( &p_input->stream.stream_lock );
187 188
    /* Request thread destruction */
    p_input->b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
189

190 191
    vlc_cond_signal( &p_input->stream.stream_wait );
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
192 193 194 195 196 197 198 199 200 201
}

/*****************************************************************************
 * 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 */
202
    vlc_thread_join( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
203 204 205

    /* Destroy Mutex locks */
    vlc_mutex_destroy( &p_input->stream.control.control_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
206
    vlc_cond_destroy( &p_input->stream.stream_wait );
Sam Hocevar's avatar
 
Sam Hocevar committed
207
    vlc_mutex_destroy( &p_input->stream.stream_lock );
Michel Kaempf's avatar
Michel Kaempf committed
208 209
}

210
/*****************************************************************************
211
 * RunThread: main thread loop
212
 *****************************************************************************
213
 * Thread in charge of processing the network packets and demultiplexing.
214
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
215
static int RunThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
216
{
Sam Hocevar's avatar
Sam Hocevar committed
217 218 219
    /* Signal right now, otherwise we'll get stuck in a peek */
    vlc_thread_ready( p_input );

Sam Hocevar's avatar
 
Sam Hocevar committed
220 221 222 223 224
    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
225
        p_input->b_dead = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
226
        return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
227
    }
Michel Kaempf's avatar
Michel Kaempf committed
228

229
    /* initialization is complete */
230 231 232 233
    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
234 235
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
Christophe Massiot's avatar
Christophe Massiot committed
236
        int i, i_count;
237

Sam Hocevar's avatar
 
Sam Hocevar committed
238
        p_input->c_loops++;
Sam Hocevar's avatar
 
Sam Hocevar committed
239

240
        vlc_mutex_lock( &p_input->stream.stream_lock );
241

242 243 244 245 246
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
 
Johan Bilien committed
247 248 249
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
                
250
                p_input->pf_set_program( p_input, 
251
                                         p_input->stream.p_new_program );
252

253 254 255 256
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

257 258 259 260 261 262 263 264 265 266 267 268
                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;
        }
        
269 270
        if( p_input->stream.p_new_area )
        {
271 272
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
273
                input_AccessReinit( p_input );
274 275 276

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

277 278 279 280
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

281 282 283 284 285 286 287 288 289
                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;
                }
            }
290 291 292
            p_input->stream.p_new_area = NULL;
        }

293
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
 
Sam Hocevar committed
294
        {
295 296
            if( p_input->stream.b_seekable
                 && p_input->pf_seek != NULL )
297
            {
298 299 300 301 302 303
                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
304 305 306
                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 );
307

308 309 310 311
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

312 313 314 315 316 317 318 319 320
                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;
                }
            }
321
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
 
Sam Hocevar committed
322
        }
323

324 325 326 327 328 329 330 331 332 333 334 335
        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;
        }

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

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

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

355
        if( i_count == 0 && p_input->stream.b_seekable )
Sam Hocevar's avatar
 
Sam Hocevar committed
356
        {
357
            /* End of file - we do not set b_die because only the
358
             * playlist is allowed to do so. */
359
            msg_Info( p_input, "EOF reached" );
360 361 362 363 364
            p_input->b_eof = 1;
        }
        else if( i_count < 0 )
        {
            p_input->b_error = 1;
365 366 367
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
368
    if( p_input->b_error || p_input->b_eof )
369 370 371
    {
        ErrorThread( p_input );
    }
372

373
    EndThread( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
374

Sam Hocevar's avatar
 
Sam Hocevar committed
375
    return 0;
376 377
}

378
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
379
 * InitThread: init the input Thread
380
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
381
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
382
{
Christophe Massiot's avatar
Christophe Massiot committed
383 384
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
    char * psz_parser = p_input->psz_source;
Gildas Bazin's avatar
 
Gildas Bazin committed
385

Christophe Massiot's avatar
Christophe Massiot committed
386 387 388 389 390
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
391
#ifdef WIN32
Gildas Bazin's avatar
 
Gildas Bazin committed
392
    if( psz_parser - p_input->psz_source == 1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
393
    {
394 395
        msg_Warn( p_input, "drive letter %c: found in source string",
                           p_input->psz_source ) ;
Stéphane Borel's avatar
 
Stéphane Borel committed
396
        psz_parser = "";
Gildas Bazin's avatar
 
Gildas Bazin committed
397 398
    }
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
399

Christophe Massiot's avatar
Christophe Massiot committed
400
    if( !*psz_parser )
401
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
402
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
403
        p_input->psz_name = p_input->psz_source;
Michel Kaempf's avatar
Michel Kaempf committed
404
    }
Christophe Massiot's avatar
Christophe Massiot committed
405 406 407
    else
    {
        *psz_parser++ = '\0';
408

Xavier Marchesini's avatar
 
Xavier Marchesini committed
409 410 411 412 413 414 415
        /* 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
416

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

Stéphane Borel's avatar
 
Stéphane Borel committed
420
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
421 422
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
423
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
424 425 426 427
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
428
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
            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
445

Christophe Massiot's avatar
Christophe Massiot committed
446 447 448
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
 
Sam Hocevar committed
449
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
450 451 452 453 454
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
455
    }
Christophe Massiot's avatar
Christophe Massiot committed
456

457 458
    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
459 460

    if( input_AccessInit( p_input ) == -1 )
461
    {
462
        return -1;
463
    }
Christophe Massiot's avatar
Christophe Massiot committed
464

465
    /* Find and open appropriate access module */
466 467
    p_input->p_access = module_Need( p_input, "access",
                                     p_input->psz_access );
Christophe Massiot's avatar
Christophe Massiot committed
468

469
    if( p_input->p_access == NULL )
470
    {
471
        msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
472
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
473
        return -1;
474
    }
Christophe Massiot's avatar
Christophe Massiot committed
475 476 477

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
478
    {
Christophe Massiot's avatar
Christophe Massiot committed
479
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
480
    }
481 482
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
483
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
484
    }
485

Sam Hocevar's avatar
 
Sam Hocevar committed
486
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
487
    {
Christophe Massiot's avatar
Christophe Massiot committed
488 489
        while( !input_FillBuffer( p_input ) )
        {
490
            if( p_input->b_die || p_input->b_error || p_input->b_eof )
Christophe Massiot's avatar
Christophe Massiot committed
491
            {
492
                module_Unneed( p_input, p_input->p_access );
493
                return -1;
Christophe Massiot's avatar
Christophe Massiot committed
494 495
            }
        }
496
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
497

498
    /* Find and open appropriate demux module */
499 500
    p_input->p_demux = module_Need( p_input, "demux",
                                    p_input->psz_demux );
Sam Hocevar's avatar
 
Sam Hocevar committed
501

502
    if( p_input->p_demux== NULL )
503
    {
504
        msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
505
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
506
        module_Unneed( p_input, p_input->p_access );
507
        return -1;
508 509
    }

510 511 512 513
    /* Initialize optional stream output. */
    psz_parser = config_GetPsz( p_input, "sout" );
    if ( psz_parser != NULL )
    {
514 515 516
        if ( *psz_parser &&
             (p_input->stream.p_sout = sout_NewInstance( p_input, psz_parser ))
               == NULL )
517 518 519 520 521 522 523 524 525 526 527
        {
            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 );
    }

528
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
529 530
}

531
/*****************************************************************************
532
 * ErrorThread: RunThread() error loop
533
 *****************************************************************************
534
 * This function is called when an error occured during thread main's loop.
535
 *****************************************************************************/
536
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
537
{
538
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
539
    {
540 541
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
542 543 544
    }
}

545
/*****************************************************************************
546
 * EndThread: end the input thread
547
 *****************************************************************************/
548
static void EndThread( input_thread_t * p_input )
549
{
550
#ifdef HAVE_SYS_TIMES_H
551 552 553
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
 
Sam Hocevar committed
554

555 556
    msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
557
#else
558
    msg_Dbg( p_input, "%d loops", p_input->c_loops );
559
#endif
560

561 562 563 564
    /* Free info structures */
    msg_Dbg( p_input, "freeing info structures...");
    input_DelInfo( p_input );
    
565 566
    input_DumpStream( p_input );

Sam Hocevar's avatar
 
Sam Hocevar committed
567 568 569
    /* Free all ES and destroy all decoder threads */
    input_EndStream( p_input );

570 571 572 573 574 575
    /* 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
576
    /* Free demultiplexer's data */
577
    module_Unneed( p_input, p_input->p_demux );
Sam Hocevar's avatar
 
Sam Hocevar committed
578

579
    /* Close the access plug-in */
580
    module_Unneed( p_input, p_input->p_access );
Sam Hocevar's avatar
 
Sam Hocevar committed
581

Christophe Massiot's avatar
Christophe Massiot committed
582 583 584
    input_AccessEnd( p_input );

    free( p_input->psz_source );
585 586 587

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