input.c 19.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
Gildas Bazin's avatar
 
Gildas Bazin committed
7
 * $Id: input.c,v 1.229 2003/05/05 16:09:35 gbazin 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

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
 * This function creates a new input, and returns a pointer
 * to its description. On error, it returns NULL.
64
 *****************************************************************************/
65
input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
66
                                      playlist_item_t *p_item )
Michel Kaempf's avatar
Michel Kaempf committed
67
{
68
    input_thread_t *    p_input;                        /* thread descriptor */
69
    input_info_category_t * p_info;
70

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

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

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

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

    /* Access */
89
    p_input->p_access = NULL;
90

Sam Hocevar's avatar
 
Sam Hocevar committed
91 92
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
93
    p_input->i_pts_delay = DEFAULT_PTS_DELAY;
Sam Hocevar's avatar
 
Sam Hocevar committed
94

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

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

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

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

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

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

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

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

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

151 152
    p_info = input_InfoCategory( p_input, _("General") );
    input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
153
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
 
Sam Hocevar committed
154

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

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

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

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

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

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

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

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

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

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

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

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

Johan Bilien's avatar
 
Johan Bilien committed
236 237
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
238 239

                p_input->pf_set_program( p_input,
240
                                         p_input->stream.p_new_program );
241

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

246 247 248 249 250 251 252 253 254 255 256
                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;
        }
257

258 259
        if( p_input->stream.p_new_area )
        {
260 261
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
262
                input_AccessReinit( p_input );
263 264 265

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

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

270 271 272 273 274 275 276 277 278
                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;
                }
            }
279 280 281
            p_input->stream.p_new_area = NULL;
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

460 461 462 463 464 465 466 467 468 469 470 471 472 473
    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 );
    }

474
    if( p_input->p_access == NULL )
475
    {
476
        msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
477
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
478
        return -1;
479
    }
Christophe Massiot's avatar
Christophe Massiot committed
480 481 482

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
483
    {
Christophe Massiot's avatar
Christophe Massiot committed
484
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
485
    }
486 487
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
488
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
489
    }
490

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

503
    /* Find and open appropriate demux module */
504 505
    p_input->p_demux = module_Need( p_input, "demux",
                                    p_input->psz_demux );
Sam Hocevar's avatar
 
Sam Hocevar committed
506

507
    if( p_input->p_demux == NULL )
508
    {
509
        msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
510
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
511
        module_Unneed( p_input, p_input->p_access );
512
        return -1;
513 514
    }

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

533
    return 0;
Michel Kaempf's avatar
Michel Kaempf committed
534 535
}

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

550
/*****************************************************************************
551
 * EndThread: end the input thread
552
 *****************************************************************************/
553
static void EndThread( input_thread_t * p_input )
554
{
Gildas Bazin's avatar
 
Gildas Bazin committed
555 556
    vlc_object_t *p_object;

557
#ifdef HAVE_SYS_TIMES_H
558 559 560
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
 
Sam Hocevar committed
561

562
    msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
563
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
564
#else
565
    msg_Dbg( p_input, "%ld loops", p_input->c_loops );
566
#endif
567

568 569 570
    /* Free info structures */
    msg_Dbg( p_input, "freeing info structures...");
    input_DelInfo( p_input );
571

572 573
    input_DumpStream( p_input );

Sam Hocevar's avatar
 
Sam Hocevar committed
574 575 576
    /* Free all ES and destroy all decoder threads */
    input_EndStream( p_input );

577 578 579 580 581 582
    /* 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
583
    /* Free demultiplexer's data */
584
    module_Unneed( p_input, p_input->p_demux );
Sam Hocevar's avatar
 
Sam Hocevar committed
585

586
    /* Close the access plug-in */
587
    module_Unneed( p_input, p_input->p_access );
Sam Hocevar's avatar
 
Sam Hocevar committed
588

Christophe Massiot's avatar
Christophe Massiot committed
589 590
    input_AccessEnd( p_input );

Gildas Bazin's avatar
 
Gildas Bazin committed
591 592 593 594 595 596 597
    /* Close the video output that should have been re-attached
     * to our object */
    p_object = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
    if( p_object )
    {
        vlc_object_detach( p_object );
        vlc_object_release( p_object );
598
        vout_Destroy( (vout_thread_t *)p_object );
Gildas Bazin's avatar
 
Gildas Bazin committed
599 600
    }

Christophe Massiot's avatar
Christophe Massiot committed
601
    free( p_input->psz_source );
602
    if ( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
603 604 605

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