input.c 19.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-2001 VideoLAN
7
 * $Id: input.c,v 1.198 2002/06/01 12:32:01 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 30 31 32
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
33

34
#include <vlc/vlc.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
35

36
#include <string.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
37 38
#include <errno.h>

39 40
#ifdef HAVE_SYS_TIMES_H
#   include <sys/times.h>
41
#endif
42

Sam Hocevar's avatar
 
Sam Hocevar committed
43
#include "netutils.h"
44
#include "playlist.h"
45

46 47 48
#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
49
#include "input_ext-plugins.h"
Michel Lespinasse's avatar
Yop,  
Michel Lespinasse committed
50

Sam Hocevar's avatar
 
Sam Hocevar committed
51 52
#include "interface.h"

53
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
54
 * Local prototypes
55
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
56
static  int RunThread       ( input_thread_t *p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
57 58
static  int InitThread      ( input_thread_t *p_input );
static void ErrorThread     ( input_thread_t *p_input );
59
static void CloseThread     ( input_thread_t *p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
60 61
static void DestroyThread   ( input_thread_t *p_input );
static void EndThread       ( input_thread_t *p_input );
Michel Kaempf's avatar
Michel Kaempf committed
62

63
/*****************************************************************************
64
 * input_CreateThread: creates a new input thread
65
 *****************************************************************************
66 67 68 69
 * 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.
70
 *****************************************************************************/
71 72
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
73
{
74 75
    input_thread_t *    p_input;                        /* thread descriptor */

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

    /* Initialize thread properties */
Sam Hocevar's avatar
 
Sam Hocevar committed
85
    p_input->b_eof      = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
86 87

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

Sam Hocevar's avatar
 
Sam Hocevar committed
90 91
    /* Set status */
    p_input->i_status   = THREAD_CREATE;
Stéphane Borel's avatar
 
Stéphane Borel committed
92 93

    /* Demux */
Stéphane Borel's avatar
 
Stéphane Borel committed
94
    p_input->p_demux_module = NULL;
Stéphane Borel's avatar
 
Stéphane Borel committed
95 96 97 98 99 100
    p_input->pf_init    = NULL;
    p_input->pf_end     = NULL;
    p_input->pf_demux   = NULL;
    p_input->pf_rewind  = NULL;

    /* Access */
Stéphane Borel's avatar
 
Stéphane Borel committed
101
    p_input->p_access_module = NULL;
Stéphane Borel's avatar
 
Stéphane Borel committed
102 103 104 105 106 107
    p_input->pf_open        = NULL;
    p_input->pf_close       = NULL;
    p_input->pf_read        = NULL;
    p_input->pf_seek        = NULL;
    p_input->pf_set_area    = NULL;
    p_input->pf_set_program = NULL;
108
    
Sam Hocevar's avatar
 
Sam Hocevar committed
109 110 111
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;

112 113 114 115 116 117
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

    /* Set locks. */
118
    vlc_mutex_init( p_input, &p_input->stream.stream_lock );
119
    vlc_cond_init( &p_input->stream.stream_wait );
120
    vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
Michel Kaempf's avatar
Michel Kaempf committed
121

122
    /* Initialize stream description */
Stéphane Borel's avatar
 
Stéphane Borel committed
123
    p_input->stream.b_changed = 0;
124 125
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
126
    p_input->stream.i_pgrm_number = 0;
127
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
128
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
129
    p_input->stream.i_mux_rate = 0;
130
    p_input->stream.b_seekable = 0;
Michel Kaempf's avatar
Michel Kaempf committed
131

Sam Hocevar's avatar
 
Sam Hocevar committed
132 133 134
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
 
Stéphane Borel committed
135 136
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
137
    p_input->stream.p_selected_area = NULL;
138
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
139

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

148 149 150 151
    /* 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;
152 153 154 155
    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
    p_input->stream.control.i_smp = config_GetInt( p_input, "vdec-smp" );

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

157
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
 
Sam Hocevar committed
158

Sam Hocevar's avatar
 
Sam Hocevar committed
159
    /* Create thread. */
160
    if( vlc_thread_create( p_input, "input", RunThread, 1 ) )
Michel Kaempf's avatar
Michel Kaempf committed
161
    {
162
        msg_Err( p_input, "cannot create input thread (%s)", strerror(errno) );
Michel Kaempf's avatar
Michel Kaempf committed
163
        free( p_input );
164
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
165
    }
166

Sam Hocevar's avatar
 
Sam Hocevar committed
167
#if 0
168 169 170 171
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
172
        {
173
            msleep( THREAD_SLEEP );
174
        } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
Sam Hocevar's avatar
 
Sam Hocevar committed
175
                && (i_status != THREAD_FATAL) );
176
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
177 178
#endif

179
    return p_input;
Michel Kaempf's avatar
Michel Kaempf committed
180 181
}

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

194 195 196
    vlc_cond_signal( &p_input->stream.stream_wait );
    vlc_mutex_unlock( &p_input->stream.stream_lock );

197
    /* If status is NULL, wait until thread has been destroyed */
Sam Hocevar's avatar
 
Sam Hocevar committed
198
#if 0
199 200 201 202 203
    if( pi_status == NULL )
    {
        do
        {
            msleep( THREAD_SLEEP );
204 205
        } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
                  && (i_status != THREAD_FATAL) );
206
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
207 208 209 210 211 212 213 214 215 216 217
#endif
}

/*****************************************************************************
 * 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 */
218
    vlc_thread_join( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
219 220 221

    /* Destroy Mutex locks */
    vlc_mutex_destroy( &p_input->stream.control.control_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
222
    vlc_cond_destroy( &p_input->stream.stream_wait );
Sam Hocevar's avatar
 
Sam Hocevar committed
223 224 225 226
    vlc_mutex_destroy( &p_input->stream.stream_lock );
    
    /* Free input structure */
    free( p_input );
Michel Kaempf's avatar
Michel Kaempf committed
227 228
}

229
/*****************************************************************************
230
 * RunThread: main thread loop
231
 *****************************************************************************
232
 * Thread in charge of processing the network packets and demultiplexing.
233
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
234
static int RunThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
235
{
Sam Hocevar's avatar
 
Sam Hocevar committed
236 237 238
    if( InitThread( p_input ) )
    {
        /* If we failed, wait before we are killed, and exit */
Sam Hocevar's avatar
 
Sam Hocevar committed
239
        p_input->i_status = THREAD_ERROR;
Sam Hocevar's avatar
 
Sam Hocevar committed
240
        p_input->b_error = 1;
241
        vlc_thread_ready( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
242 243
        ErrorThread( p_input );
        DestroyThread( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
244
        return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
245
    }
Michel Kaempf's avatar
Michel Kaempf committed
246

247 248
    vlc_thread_ready( p_input );

Sam Hocevar's avatar
 
Sam Hocevar committed
249 250
    p_input->i_status = THREAD_READY;

251
    /* initialization is complete */
252 253 254 255
    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
256 257
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
Christophe Massiot's avatar
Christophe Massiot committed
258
        int i, i_count;
259

Sam Hocevar's avatar
 
Sam Hocevar committed
260
        p_input->c_loops++;
Sam Hocevar's avatar
 
Sam Hocevar committed
261

262
        vlc_mutex_lock( &p_input->stream.stream_lock );
263

264 265 266 267 268
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
 
Johan Bilien committed
269 270 271
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
                
272 273 274
                p_input->pf_set_program( p_input, 
                        p_input->stream.p_new_program );

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

279 280 281 282 283 284 285 286 287 288 289 290
                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;
        }
        
291 292
        if( p_input->stream.p_new_area )
        {
293 294
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
295
                input_AccessReinit( p_input );
296 297 298

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

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

303 304 305 306 307 308 309 310 311
                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;
                }
            }
312 313 314
            p_input->stream.p_new_area = NULL;
        }

315
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
 
Sam Hocevar committed
316
        {
317 318
            if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
            {
319 320 321 322 323 324
                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
325 326 327
                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 );
328

329 330 331 332
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

333 334 335 336 337 338 339 340 341
                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;
                }
            }
342
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
 
Sam Hocevar committed
343
        }
344

345 346 347 348 349 350 351 352 353 354 355 356
        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;
        }

357 358 359 360 361 362 363 364 365 366 367 368 369 370
        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;
        }

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

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

376
        if( i_count == 0 && p_input->stream.b_seekable )
Sam Hocevar's avatar
 
Sam Hocevar committed
377
        {
378 379
            /* End of file - we do not set b_die because only the
             * interface is allowed to do so. */
380
            msg_Info( p_input, "EOF reached" );
381 382 383 384 385
            p_input->b_eof = 1;
        }
        else if( i_count < 0 )
        {
            p_input->b_error = 1;
386 387 388
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
389
    if( p_input->b_error || p_input->b_eof )
390 391 392
    {
        ErrorThread( p_input );
    }
393

394
    EndThread( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
395 396

    DestroyThread( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
397 398

    return 0;
399 400
}

401
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
402
 * InitThread: init the input Thread
403
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
404
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
405
{
Christophe Massiot's avatar
Christophe Massiot committed
406 407
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
    char * psz_parser = p_input->psz_source;
Gildas Bazin's avatar
 
Gildas Bazin committed
408

Christophe Massiot's avatar
Christophe Massiot committed
409 410 411 412 413
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
414
#ifdef WIN32
Gildas Bazin's avatar
 
Gildas Bazin committed
415
    if( psz_parser - p_input->psz_source == 1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
416
    {
417 418
        msg_Warn( p_input, "drive letter %c: found in source string",
                           p_input->psz_source ) ;
Stéphane Borel's avatar
 
Stéphane Borel committed
419
        psz_parser = "";
Gildas Bazin's avatar
 
Gildas Bazin committed
420 421
    }
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
422

Christophe Massiot's avatar
Christophe Massiot committed
423
    if( !*psz_parser )
424
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
425
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
426
        p_input->psz_name = p_input->psz_source;
Michel Kaempf's avatar
Michel Kaempf committed
427
    }
Christophe Massiot's avatar
Christophe Massiot committed
428 429 430
    else
    {
        *psz_parser++ = '\0';
431

Xavier Marchesini's avatar
 
Xavier Marchesini committed
432 433 434 435 436 437 438
        /* 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
439

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

Stéphane Borel's avatar
 
Stéphane Borel committed
443
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
444 445
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
446
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
447 448 449 450
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
451
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
            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
468

Christophe Massiot's avatar
Christophe Massiot committed
469 470 471
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
 
Sam Hocevar committed
472
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
473 474 475 476 477
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
478
    }
Christophe Massiot's avatar
Christophe Massiot committed
479

480 481
    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
482 483

    if( input_AccessInit( p_input ) == -1 )
484
    {
Christophe Massiot's avatar
Christophe Massiot committed
485
        return( -1 );
486
    }
Christophe Massiot's avatar
Christophe Massiot committed
487

488 489 490 491
    /* Find and open appropriate access module */
    p_input->p_access_module =
        module_Need( p_input, MODULE_CAPABILITY_ACCESS,
                     p_input->psz_access, (void *)p_input );
Christophe Massiot's avatar
Christophe Massiot committed
492 493

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

#define f p_input->p_access_module->p_functions->access.functions.access
    p_input->pf_open          = f.pf_open;
    p_input->pf_close         = f.pf_close;
    p_input->pf_read          = f.pf_read;
    p_input->pf_set_area      = f.pf_set_area;
    p_input->pf_set_program   = f.pf_set_program;
    p_input->pf_seek          = f.pf_seek;
#undef f

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
511
    {
Christophe Massiot's avatar
Christophe Massiot committed
512
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
513
    }
514 515
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
516
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
517
    }
518

Sam Hocevar's avatar
 
Sam Hocevar committed
519
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
520
    {
Christophe Massiot's avatar
Christophe Massiot committed
521 522
        while( !input_FillBuffer( p_input ) )
        {
523
            if( p_input->b_die || p_input->b_error || p_input->b_eof )
Christophe Massiot's avatar
Christophe Massiot committed
524 525 526 527 528
            {
                module_Unneed( p_input->p_access_module );
                return( -1 );
            }
        }
529
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
530

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

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

Christophe Massiot's avatar
Christophe Massiot committed
544 545 546 547 548 549 550
#define f p_input->p_demux_module->p_functions->demux.functions.demux
    p_input->pf_init          = f.pf_init;
    p_input->pf_end           = f.pf_end;
    p_input->pf_demux         = f.pf_demux;
    p_input->pf_rewind        = f.pf_rewind;
#undef f

Sam Hocevar's avatar
 
Sam Hocevar committed
551
    return( 0 );
Michel Kaempf's avatar
Michel Kaempf committed
552 553
}

554
/*****************************************************************************
555
 * ErrorThread: RunThread() error loop
556
 *****************************************************************************
557
 * This function is called when an error occured during thread main's loop.
558
 *****************************************************************************/
559
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
560
{
561
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
562
    {
563 564
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
565 566 567
    }
}

568
/*****************************************************************************
569
 * EndThread: end the input thread
570
 *****************************************************************************/
571
static void EndThread( input_thread_t * p_input )
572
{
573
#ifdef HAVE_SYS_TIMES_H
574 575 576
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
 
Sam Hocevar committed
577

578 579
    msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
580
#else
581
    msg_Dbg( p_input, "%d loops", p_input->c_loops );
582
#endif
583

584 585 586 587
    input_DumpStream( p_input );

    /* Store status */
    p_input->i_status = THREAD_END;
Sam Hocevar's avatar
 
Sam Hocevar committed
588 589 590 591 592 593

    /* Free all ES and destroy all decoder threads */
    input_EndStream( p_input );

    /* Free demultiplexer's data */
    p_input->pf_end( p_input );
Christophe Massiot's avatar
Christophe Massiot committed
594
    module_Unneed( p_input->p_demux_module );
Sam Hocevar's avatar
 
Sam Hocevar committed
595

Christophe Massiot's avatar
Christophe Massiot committed
596
    /* Close the access plug-in */
Sam Hocevar's avatar
 
Sam Hocevar committed
597 598 599 600 601 602 603 604
    CloseThread( p_input );
}

/*****************************************************************************
 * CloseThread: close the target
 *****************************************************************************/
static void CloseThread( input_thread_t * p_input )
{
Christophe Massiot's avatar
Christophe Massiot committed
605 606
    p_input->pf_close( p_input );
    module_Unneed( p_input->p_access_module );
Sam Hocevar's avatar
 
Sam Hocevar committed
607

Christophe Massiot's avatar
Christophe Massiot committed
608 609 610
    input_AccessEnd( p_input );

    free( p_input->psz_source );
Sam Hocevar's avatar
 
Sam Hocevar committed
611 612 613 614 615 616 617
}

/*****************************************************************************
 * DestroyThread: destroy the input thread
 *****************************************************************************/
static void DestroyThread( input_thread_t * p_input )
{
618
    /* Update status */
Sam Hocevar's avatar
 
Sam Hocevar committed
619
    p_input->i_status = THREAD_OVER;
Sam Hocevar's avatar
Sam Hocevar committed
620
}
621