input.c 20.9 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.195 2002/05/14 21:23:44 massiot 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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
45
#include "intf_playlist.h"
46

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*****************************************************************************
 * input_InitBank: initialize the input bank.
 *****************************************************************************/
void input_InitBank ( void )
{
    p_input_bank->i_count = 0;

    /* XXX: Workaround for old interface modules */
    p_input_bank->pp_input[0] = NULL;

    vlc_mutex_init( &p_input_bank->lock );
}

/*****************************************************************************
 * input_EndBank: empty the input bank.
 *****************************************************************************
 * This function ends all unused inputs and empties the bank in
 * case of success.
 *****************************************************************************/
void input_EndBank ( void )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
85 86
    int i_input;

Sam Hocevar's avatar
 
Sam Hocevar committed
87
    /* Ask all remaining video outputs to die */
Sam Hocevar's avatar
 
Sam Hocevar committed
88
    for( i_input = 0; i_input < p_input_bank->i_count; i_input++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
89
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
90 91
        input_StopThread(
                p_input_bank->pp_input[ i_input ], NULL );
Sam Hocevar's avatar
 
Sam Hocevar committed
92
        input_DestroyThread(
Sam Hocevar's avatar
 
Sam Hocevar committed
93
                p_input_bank->pp_input[ i_input ] );
Sam Hocevar's avatar
 
Sam Hocevar committed
94 95 96 97 98
    }

    vlc_mutex_destroy( &p_input_bank->lock );
}

99
/*****************************************************************************
100
 * input_CreateThread: creates a new input thread
101
 *****************************************************************************
102 103 104 105
 * 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.
106
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
107
input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
Michel Kaempf's avatar
Michel Kaempf committed
108
{
109 110
    input_thread_t *    p_input;                        /* thread descriptor */

111 112 113
    /* Allocate descriptor */
    p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
    if( p_input == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
114
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
115 116
        intf_ErrMsg( "input error: can't allocate input thread (%s)",
                     strerror(errno) );
Michel Kaempf's avatar
Michel Kaempf committed
117 118
        return( NULL );
    }
119 120

    /* Initialize thread properties */
Sam Hocevar's avatar
 
Sam Hocevar committed
121 122 123
    p_input->b_die      = 0;
    p_input->b_error    = 0;
    p_input->b_eof      = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
124 125

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

Sam Hocevar's avatar
 
Sam Hocevar committed
128 129
    /* Set status */
    p_input->i_status   = THREAD_CREATE;
Stéphane Borel's avatar
 
Stéphane Borel committed
130 131

    /* Demux */
Stéphane Borel's avatar
 
Stéphane Borel committed
132
    p_input->p_demux_module = NULL;
Stéphane Borel's avatar
 
Stéphane Borel committed
133 134 135 136 137 138
    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
139
    p_input->p_access_module = NULL;
Stéphane Borel's avatar
 
Stéphane Borel committed
140 141 142 143 144 145
    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;
146
    
Sam Hocevar's avatar
 
Sam Hocevar committed
147 148 149
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;

150 151 152 153 154 155 156 157 158
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

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

160
    /* Initialize stream description */
Stéphane Borel's avatar
 
Stéphane Borel committed
161
    p_input->stream.b_changed = 0;
162 163
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
164
    p_input->stream.i_pgrm_number = 0;
165
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
166
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
167
    p_input->stream.i_mux_rate = 0;
Michel Kaempf's avatar
Michel Kaempf committed
168

Sam Hocevar's avatar
 
Sam Hocevar committed
169 170 171
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
 
Stéphane Borel committed
172 173
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
174
    p_input->stream.p_selected_area = NULL;
175
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
176

Sam Hocevar's avatar
 
Sam Hocevar committed
177 178 179 180
    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
181
    /* By default there is one area in a stream */
Stéphane Borel's avatar
 
Stéphane Borel committed
182
    input_AddArea( p_input );
183
    p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
Stéphane Borel's avatar
 
Stéphane Borel committed
184

185 186 187 188
    /* 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;
Gildas Bazin's avatar
 
Gildas Bazin committed
189
    p_input->stream.control.b_grayscale = config_GetIntVariable( "grayscale" );
190
    p_input->stream.control.i_smp = config_GetIntVariable( "vdec-smp" );
Michel Kaempf's avatar
Michel Kaempf committed
191

Christophe Massiot's avatar
Christophe Massiot committed
192
    intf_WarnMsg( 1, "input: playlist item `%s'", p_input->psz_source );
Sam Hocevar's avatar
 
Sam Hocevar committed
193

Sam Hocevar's avatar
 
Sam Hocevar committed
194
    /* Create thread. */
Sam Hocevar's avatar
 
Sam Hocevar committed
195 196
    if( vlc_thread_create( &p_input->thread_id, "input",
                           (vlc_thread_func_t)RunThread, (void *) p_input ) )
Michel Kaempf's avatar
Michel Kaempf committed
197
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
198 199
        intf_ErrMsg( "input error: can't create input thread (%s)",
                     strerror(errno) );
Michel Kaempf's avatar
Michel Kaempf committed
200 201 202
        free( p_input );
        return( NULL );
    }
203

Sam Hocevar's avatar
 
Sam Hocevar committed
204
#if 0
205 206 207 208
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
209
        {
210
            msleep( THREAD_SLEEP );
211
        } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
Sam Hocevar's avatar
 
Sam Hocevar committed
212
                && (i_status != THREAD_FATAL) );
213
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
214 215
#endif

Michel Kaempf's avatar
Michel Kaempf committed
216 217 218
    return( p_input );
}

219
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
220
 * input_StopThread: mark an input thread as zombie
221
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
222
 * This function should not return until the thread is effectively cancelled.
223
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
224
void input_StopThread( input_thread_t *p_input, int *pi_status )
Michel Kaempf's avatar
Michel Kaempf committed
225
{
Sam Hocevar's avatar
 
Sam Hocevar committed
226 227
    /* Make the thread exit from a possible vlc_cond_wait() */
    vlc_mutex_lock( &p_input->stream.stream_lock );
228

229 230
    /* Request thread destruction */
    p_input->b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
231

232 233 234
    vlc_cond_signal( &p_input->stream.stream_wait );
    vlc_mutex_unlock( &p_input->stream.stream_lock );

235
    /* If status is NULL, wait until thread has been destroyed */
Sam Hocevar's avatar
 
Sam Hocevar committed
236
#if 0
237 238 239 240 241
    if( pi_status == NULL )
    {
        do
        {
            msleep( THREAD_SLEEP );
242 243
        } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
                  && (i_status != THREAD_FATAL) );
244
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
#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 */
    vlc_thread_join( p_input->thread_id );

    /* Destroy Mutex locks */
    vlc_mutex_destroy( &p_input->stream.control.control_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
260
    vlc_cond_destroy( &p_input->stream.stream_wait );
Sam Hocevar's avatar
 
Sam Hocevar committed
261 262 263 264
    vlc_mutex_destroy( &p_input->stream.stream_lock );
    
    /* Free input structure */
    free( p_input );
Michel Kaempf's avatar
Michel Kaempf committed
265 266
}

267
/*****************************************************************************
268
 * RunThread: main thread loop
269
 *****************************************************************************
270
 * Thread in charge of processing the network packets and demultiplexing.
271
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
272
static int RunThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
273
{
Sam Hocevar's avatar
 
Sam Hocevar committed
274 275 276
    if( InitThread( p_input ) )
    {
        /* If we failed, wait before we are killed, and exit */
Sam Hocevar's avatar
 
Sam Hocevar committed
277
        p_input->i_status = THREAD_ERROR;
Sam Hocevar's avatar
 
Sam Hocevar committed
278 279 280
        p_input->b_error = 1;
        ErrorThread( p_input );
        DestroyThread( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
281
        return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
282
    }
Michel Kaempf's avatar
Michel Kaempf committed
283

Sam Hocevar's avatar
 
Sam Hocevar committed
284 285
    p_input->i_status = THREAD_READY;

286
    /* initialization is complete */
287 288 289 290
    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
291 292
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
Christophe Massiot's avatar
Christophe Massiot committed
293
        int i, i_count;
294

Sam Hocevar's avatar
 
Sam Hocevar committed
295
        p_input->c_loops++;
Sam Hocevar's avatar
 
Sam Hocevar committed
296

297
        vlc_mutex_lock( &p_input->stream.stream_lock );
298

299 300 301 302 303
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
 
Johan Bilien committed
304 305 306
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
                
307 308 309
                p_input->pf_set_program( p_input, 
                        p_input->stream.p_new_program );

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

314 315 316 317 318 319 320 321 322 323 324 325
                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;
        }
        
326 327
        if( p_input->stream.p_new_area )
        {
328 329
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
330
                input_AccessReinit( p_input );
331 332 333

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

334 335 336 337
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

338 339 340 341 342 343 344 345 346
                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;
                }
            }
347 348 349
            p_input->stream.p_new_area = NULL;
        }

350
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
 
Sam Hocevar committed
351
        {
352 353
            if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
            {
354 355 356 357 358 359
                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
360 361 362
                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 );
363

364 365 366 367
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

368 369 370 371 372 373 374 375 376
                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;
                }
            }
377
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
 
Sam Hocevar committed
378
        }
379

380 381 382 383 384 385 386 387 388 389 390 391
        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;
        }

392 393 394 395 396 397 398 399 400 401 402 403 404 405
        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;
        }

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

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

411
        if( i_count == 0 && p_input->stream.b_seekable )
Sam Hocevar's avatar
 
Sam Hocevar committed
412
        {
413 414 415 416 417 418 419 420
            /* End of file - we do not set b_die because only the
             * interface is allowed to do so. */
            intf_WarnMsg( 3, "input: EOF reached" );
            p_input->b_eof = 1;
        }
        else if( i_count < 0 )
        {
            p_input->b_error = 1;
421 422 423
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
424
    if( p_input->b_error || p_input->b_eof )
425 426 427
    {
        ErrorThread( p_input );
    }
428

429
    EndThread( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
430 431

    DestroyThread( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
432 433

    return 0;
434 435
}

436
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
437
 * InitThread: init the input Thread
438
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
439
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
440
{
Christophe Massiot's avatar
Christophe Massiot committed
441 442
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
    char * psz_parser = p_input->psz_source;
Gildas Bazin's avatar
 
Gildas Bazin committed
443

Christophe Massiot's avatar
Christophe Massiot committed
444 445 446 447 448
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
449
#ifdef WIN32
Gildas Bazin's avatar
 
Gildas Bazin committed
450
    if( psz_parser - p_input->psz_source == 1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
451 452 453
    {
        intf_WarnMsg( 2, "Drive letter %c: specified in source string",
                      p_input->psz_source ) ;
Stéphane Borel's avatar
 
Stéphane Borel committed
454
        psz_parser = "";
Gildas Bazin's avatar
 
Gildas Bazin committed
455 456
    }
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
457

Christophe Massiot's avatar
Christophe Massiot committed
458
    if( !*psz_parser )
459
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
460
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
461
        p_input->psz_name = p_input->psz_source;
Michel Kaempf's avatar
Michel Kaempf committed
462
    }
Christophe Massiot's avatar
Christophe Massiot committed
463 464 465
    else
    {
        *psz_parser++ = '\0';
466

Xavier Marchesini's avatar
 
Xavier Marchesini committed
467 468 469 470 471 472 473
        /* 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
474

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

Stéphane Borel's avatar
 
Stéphane Borel committed
478
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
479 480
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
481
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
482 483 484 485
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
 
Sam Hocevar committed
486
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
            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
503

Christophe Massiot's avatar
Christophe Massiot committed
504 505 506
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
 
Sam Hocevar committed
507
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
508 509 510 511 512
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
513
    }
Christophe Massiot's avatar
Christophe Massiot committed
514

Sam Hocevar's avatar
 
Sam Hocevar committed
515
    intf_WarnMsg( 2, "input: access `%s', demux `%s', name `%s'",
Christophe Massiot's avatar
Christophe Massiot committed
516 517 518 519
                  p_input->psz_access, p_input->psz_demux,
                  p_input->psz_name );

    if( input_AccessInit( p_input ) == -1 )
520
    {
Christophe Massiot's avatar
Christophe Massiot committed
521
        return( -1 );
522
    }
Christophe Massiot's avatar
Christophe Massiot committed
523 524 525 526 527 528 529

    /* Find and open appropriate access plug-in. */
    p_input->p_access_module = module_Need( MODULE_CAPABILITY_ACCESS,
                                 p_input->psz_access,
                                 (void *)p_input );

    if( p_input->p_access_module == NULL )
530
    {
Christophe Massiot's avatar
Christophe Massiot committed
531 532 533 534
        intf_ErrMsg( "input error: no suitable access plug-in for `%s/%s:%s'",
                     p_input->psz_access, p_input->psz_demux,
                     p_input->psz_name );
        return( -1 );
535
    }
Christophe Massiot's avatar
Christophe Massiot committed
536 537 538 539 540 541 542 543 544 545 546 547

#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
548
    {
Christophe Massiot's avatar
Christophe Massiot committed
549
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
550
    }
551 552
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
553
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
554
    }
555

Sam Hocevar's avatar
 
Sam Hocevar committed
556
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
557
    {
Christophe Massiot's avatar
Christophe Massiot committed
558 559 560 561 562 563 564 565
        while( !input_FillBuffer( p_input ) )
        {
            if( p_input->b_die || p_input->b_error )
            {
                module_Unneed( p_input->p_access_module );
                return( -1 );
            }
        }
566
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
567

Christophe Massiot's avatar
Christophe Massiot committed
568 569 570 571
    /* Find and open appropriate demux plug-in. */
    p_input->p_demux_module = module_Need( MODULE_CAPABILITY_DEMUX,
                                 p_input->psz_demux,
                                 (void *)p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
572

Christophe Massiot's avatar
Christophe Massiot committed
573
    if( p_input->p_demux_module == NULL )
574
    {
Christophe Massiot's avatar
Christophe Massiot committed
575 576 577 578
        intf_ErrMsg( "input error: no suitable demux plug-in for `%s/%s:%s'",
                     p_input->psz_access, p_input->psz_demux,
                     p_input->psz_name );
        module_Unneed( p_input->p_access_module );
579 580 581
        return( -1 );
    }

Christophe Massiot's avatar
Christophe Massiot committed
582 583 584 585 586 587 588
#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
589
    return( 0 );
Michel Kaempf's avatar
Michel Kaempf committed
590 591
}

592
/*****************************************************************************
593
 * ErrorThread: RunThread() error loop
594
 *****************************************************************************
595
 * This function is called when an error occured during thread main's loop.
596
 *****************************************************************************/
597
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
598
{
599
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
600
    {
601 602
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
603 604 605
    }
}

606
/*****************************************************************************
607
 * EndThread: end the input thread
608
 *****************************************************************************/
609
static void EndThread( input_thread_t * p_input )
610
{
611
    /* Store status */
Sam Hocevar's avatar
 
Sam Hocevar committed
612
    p_input->i_status = THREAD_END;
613

614
    if( p_main->b_stats )
Sam Hocevar's avatar
 
Sam Hocevar committed
615
    {
616
#ifdef HAVE_SYS_TIMES_H
617 618
        /* Display statistics */
        struct tms  cpu_usage;
Sam Hocevar's avatar
 
Sam Hocevar committed
619 620
        times( &cpu_usage );

621 622 623
        intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
                      p_input->c_loops,
                      cpu_usage.tms_utime, cpu_usage.tms_stime );
624 625 626
#else
        intf_StatMsg( "input stats: %d loops", p_input->c_loops );
#endif
627 628

        input_DumpStream( p_input );
Sam Hocevar's avatar
 
Sam Hocevar committed
629 630 631 632 633 634 635
    }

    /* 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
636
    module_Unneed( p_input->p_demux_module );
Sam Hocevar's avatar
 
Sam Hocevar committed
637

Christophe Massiot's avatar
Christophe Massiot committed
638
    /* Close the access plug-in */
Sam Hocevar's avatar
 
Sam Hocevar committed
639 640 641 642 643 644 645 646
    CloseThread( p_input );
}

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

Christophe Massiot's avatar
Christophe Massiot committed
650 651 652
    input_AccessEnd( p_input );

    free( p_input->psz_source );
Sam Hocevar's avatar
 
Sam Hocevar committed
653 654 655 656 657 658 659
}

/*****************************************************************************
 * DestroyThread: destroy the input thread
 *****************************************************************************/
static void DestroyThread( input_thread_t * p_input )
{
660
    /* Update status */
Sam Hocevar's avatar
 
Sam Hocevar committed
661
    p_input->i_status = THREAD_OVER;
662
}
663