input_programs.c 24.3 KB
Newer Older
1
2
3
/*****************************************************************************
 * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
 *****************************************************************************
4
 * Copyright (C) 1999-2001 VideoLAN
Johan Bilien's avatar
   
Johan Bilien committed
5
 * $Id: input_programs.c,v 1.75 2002/03/19 23:02:29 jobi Exp $
6
 *
7
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * 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.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27
#include <stdlib.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
28
#include <string.h>                                    /* memcpy(), memset() */
Sam Hocevar's avatar
   
Sam Hocevar committed
29
#include <sys/types.h>                                              /* off_t */
30

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

#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
36
#include "input_ext-plugins.h"
37

Sam Hocevar's avatar
   
Sam Hocevar committed
38
39
#include "debug.h"

40
41
42
43
/*
 * NOTICE : all of these functions expect you to have taken the lock on
 * p_input->stream.lock
 */
44
45
46
47

/*****************************************************************************
 * input_InitStream: init the stream descriptor of the given input
 *****************************************************************************/
48
int input_InitStream( input_thread_t * p_input, size_t i_data_len )
49
{
50

51
    p_input->stream.i_stream_id = 0;
52
53
54
55

    /* initialized to 0 since we don't give the signal to the interface
     * before the end of input initialization */
    p_input->stream.b_changed = 0;
56
57
    p_input->stream.pp_es = NULL;
    p_input->stream.pp_selected_es = NULL;
58
59
    p_input->stream.p_removed_es = NULL;
    p_input->stream.p_newly_selected_es = NULL;
60
    p_input->stream.pp_programs = NULL;
61
62
63
    p_input->stream.p_selected_program = NULL;
    p_input->stream.p_new_program = NULL;
    
64
65
    if( i_data_len )
    {
Henri Fallon's avatar
   
Henri Fallon committed
66
67
68
        if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
        {
            intf_ErrMsg( "Unable to allocate memory in input_InitStream");
69
            return 1;
Henri Fallon's avatar
   
Henri Fallon committed
70
        }
71
72
        memset( p_input->stream.p_demux_data, 0, i_data_len );
    }
73
74
75
76
    else
    {
        p_input->stream.p_demux_data = NULL;
    }
77
78

    return 0;
79
80
}

81
82
83
84
85
/*****************************************************************************
 * input_EndStream: free all stream descriptors
 *****************************************************************************/
void input_EndStream( input_thread_t * p_input )
{
86
    /* Free all programs and associated ES, and associated decoders. */
87
    while( p_input->stream.i_pgrm_number )
88
    {
89
90
        input_DelProgram( p_input, p_input->stream.pp_programs[0] );
    }
91

92
    /* Free standalone ES */
93
    while( p_input->stream.i_es_number )
94
95
96
    {
        input_DelES( p_input, p_input->stream.pp_es[0] );
    }
97

98
99
100
101
102
103
    /* Free all areas */
    while( p_input->stream.i_area_nb )
    {
        input_DelArea( p_input, p_input->stream.pp_areas[0] );
    }

104
105
106
107
108
109
    /* Free selected ES */
    if( p_input->stream.pp_selected_es != NULL )
    {
        free( p_input->stream.pp_selected_es );
    }
    
110
111
112
113
    if( p_input->stream.p_demux_data != NULL )
    {
        free( p_input->stream.p_demux_data );
    }
114
115
116
117
118
119
120
121
122
123
124
125
}

/*****************************************************************************
 * input_FindProgram: returns a pointer to a program described by its ID
 *****************************************************************************/
pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
{
    int     i;

    for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
    {
        if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
126
        {
127
            return p_input->stream.pp_programs[i];
128
129
        }
    }
130
131

    return( NULL );
132
133
}

134
135
136
137
138
139
/*****************************************************************************
 * input_AddProgram: add and init a program descriptor
 *****************************************************************************
 * This program descriptor will be referenced in the given stream descriptor
 *****************************************************************************/
pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
140
                                      u16 i_pgrm_id, size_t i_data_len )
141
{
142
143
    /* Where to add the pgrm */
    int i_pgrm_index = p_input->stream.i_pgrm_number;
144
145
146
147
148
149

    /* Add an entry to the list of program associated with the stream */
    p_input->stream.i_pgrm_number++;
    p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
                                           p_input->stream.i_pgrm_number
                                            * sizeof(pgrm_descriptor_t *) );
Henri Fallon's avatar
   
Henri Fallon committed
150
151
152
    if( p_input->stream.pp_programs == NULL )
    {
        intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
Henri Fallon's avatar
   
Henri Fallon committed
153
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
154
155
    }
    
156
157
158
    /* Allocate the structure to store this description */
    p_input->stream.pp_programs[i_pgrm_index] =
                                        malloc( sizeof(pgrm_descriptor_t) );
Henri Fallon's avatar
   
Henri Fallon committed
159
160
161
    if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
    {
        intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
Henri Fallon's avatar
   
Henri Fallon committed
162
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
163
164
    }
    
165
166
167
    /* Init this entry */
    p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
    p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
168
    p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
169
170
171
172

    p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
    p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;

173
174
    input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );

175
    p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
176
                                                = SYNCHRO_START;
177

178
179
180
181
    if( i_data_len )
    {
        p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
            malloc( i_data_len );
Henri Fallon's avatar
   
Henri Fallon committed
182
183
184
        if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
        {
            intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
Henri Fallon's avatar
   
Henri Fallon committed
185
            return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
186
        }
187
188
189
        memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
                i_data_len );
    }
190
191
192
193
    else
    {
        p_input->stream.pp_programs[i_pgrm_index]->p_demux_data = NULL;
    }
194

195
196
197
198
199
200
201
202
    return p_input->stream.pp_programs[i_pgrm_index];
}

/*****************************************************************************
 * input_DelProgram: destroy a program descriptor
 *****************************************************************************
 * All ES descriptions referenced in the descriptor will be deleted.
 *****************************************************************************/
203
void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
204
{
205
    int i_pgrm_index;
206

207
    ASSERT( p_pgrm );
208
209

    /* Free the structures that describe the es that belongs to that program */
210
    while( p_pgrm->i_es_number )
211
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
212
        input_DelES( p_input, p_pgrm->pp_es[0] );
213
214
    }

215
216
217
218
219
220
    /* Free the demux data */
    if( p_pgrm->p_demux_data != NULL )
    {
        free( p_pgrm->p_demux_data );
    }

221
222
223
224
225
226
227
    /* Find the program in the programs table */
    for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
         i_pgrm_index++ )
    {
        if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
            break;
    }
228
229
230

    /* Remove this program from the stream's list of programs */
    p_input->stream.i_pgrm_number--;
231

232
233
234
235
236
    p_input->stream.pp_programs[i_pgrm_index] =
        p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
    p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
                                           p_input->stream.i_pgrm_number
                                            * sizeof(pgrm_descriptor_t *) );
237

238
    if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
Henri Fallon's avatar
   
Henri Fallon committed
239
    {
240
241
        intf_ErrMsg( "input error: unable to realloc program list"
                     " in input_DelProgram" );
Henri Fallon's avatar
   
Henri Fallon committed
242
    }
243

244
245
246
247
    /* Free the description of this program */
    free( p_pgrm );
}

Stéphane Borel's avatar
   
Stéphane Borel committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*****************************************************************************
 * input_AddArea: add and init an area descriptor
 *****************************************************************************
 * This area descriptor will be referenced in the given stream descriptor
 *****************************************************************************/
input_area_t * input_AddArea( input_thread_t * p_input )
{
    /* Where to add the pgrm */
    int i_area_index = p_input->stream.i_area_nb;

    /* Add an entry to the list of program associated with the stream */
    p_input->stream.i_area_nb++;
    p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
                                        p_input->stream.i_area_nb
                                            * sizeof(input_area_t *) );
    if( p_input->stream.pp_areas == NULL )
    {
        intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
        return( NULL );
    }
    
    /* Allocate the structure to store this description */
    p_input->stream.pp_areas[i_area_index] =
                                        malloc( sizeof(input_area_t) );
    if( p_input->stream.pp_areas[i_area_index] == NULL )
    {
        intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
        return( NULL );
    }
    
    /* Init this entry */
    p_input->stream.pp_areas[i_area_index]->i_id = 0;
    p_input->stream.pp_areas[i_area_index]->i_start = 0;
    p_input->stream.pp_areas[i_area_index]->i_size = 0;
    p_input->stream.pp_areas[i_area_index]->i_tell = 0;
283
    p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
284
    p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
Stéphane Borel's avatar
   
Stéphane Borel committed
285
286
287
288
289
    p_input->stream.pp_areas[i_area_index]->i_part= 0;

    return p_input->stream.pp_areas[i_area_index];
}

290
291
292
293
294
295
/*****************************************************************************
 * input_SetProgram: changes the current program
 *****************************************************************************/
int input_SetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_new_prg )
{
    int i_es_index;
Johan Bilien's avatar
   
Johan Bilien committed
296
297
    
    if ( p_input->stream.p_selected_program )
298
    {
Johan Bilien's avatar
   
Johan Bilien committed
299
300
301
302
303
304
305
306
307
        for ( i_es_index = 0 ; 
                i_es_index < p_input->stream.i_selected_es_number; 
                i_es_index ++ )
        {
            intf_WarnMsg( 4, "Unselecting ES %d", 
                    p_input->stream.pp_selected_es[i_es_index]->i_id );
            input_UnselectES( p_input , 
                    p_input->stream.pp_selected_es[i_es_index] );
        }
308
    }
Johan Bilien's avatar
   
Johan Bilien committed
309

310
311
    for (i_es_index = 0 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
    {
Johan Bilien's avatar
   
Johan Bilien committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
        int i_required_audio_es;
        int i_required_spu_es;
        int i_audio_es = 0;
        int i_spu_es = 0;

        /* Get the number of the required audio stream */
        if( p_main->b_audio )
        {
            /* Default is the first one */
            i_required_audio_es = config_GetIntVariable( "input_channel" );
            if( i_required_audio_es < 0 )
            {
                i_required_audio_es = 1;
            }
        }
        else
        {
            i_required_audio_es = 0;
        }

        /* Same thing for subtitles */
        if( p_main->b_video )
        {
            /* for spu, default is none */
            i_required_spu_es = config_GetIntVariable( "input_subtitle" );
            if( i_required_spu_es < 0 )
            {
                i_required_spu_es = 0;
            }
        }
        else
        {
            i_required_spu_es = 0;
        }
        switch( p_new_prg->pp_es[i_es_index]->i_cat )
                    {
                        case MPEG1_VIDEO_ES:
                        case MPEG2_VIDEO_ES:
                            intf_WarnMsg( 4, "Selecting ES %x", 
                                    p_new_prg->pp_es[i_es_index]->i_id );
                            input_SelectES( p_input, 
                                   p_new_prg->pp_es[i_es_index] );
                            break;
                        case MPEG1_AUDIO_ES:
                        case MPEG2_AUDIO_ES:
                            i_audio_es += 1;
                            if( i_audio_es == i_required_audio_es )
                            {
                                intf_WarnMsg( 4, "Selecting ES %x", 
                                    p_new_prg->pp_es[i_es_index]->i_id );   
                                input_SelectES( p_input, 
                                    p_new_prg->pp_es[i_es_index]);
                            }
                            break;
                        case LPCM_AUDIO_ES :
                        case AC3_AUDIO_ES :
                            i_audio_es += 1;
                            if( i_audio_es == i_required_audio_es )
                            {
                                intf_WarnMsg( 4, "Selecting ES %x", 
                                    p_new_prg->pp_es[i_es_index]->i_id );
                                input_SelectES( p_input, 
                                    p_new_prg->pp_es[i_es_index] );
                            }
                            break;
                        /* Not sure this one is fully specification-compliant */
                        case DVD_SPU_ES :
                            i_spu_es += 1;
                            if( i_spu_es == i_required_spu_es )
                            {
                                intf_WarnMsg( 4, "Selecting ES %x", 
                                    p_new_prg->pp_es[i_es_index]->i_id );
                                input_SelectES( p_input, 
                                    p_new_prg->pp_es[i_es_index] );
                            }
                            break;
                        default :
                            break;
                    }

392
    }
Johan Bilien's avatar
   
Johan Bilien committed
393
    
394
395
396
397
398
399
400

    p_input->stream.p_selected_program = p_new_prg;

    return( 0 );
}


Stéphane Borel's avatar
   
Stéphane Borel committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*****************************************************************************
 * input_DelArea: destroy a area descriptor
 *****************************************************************************
 * All ES descriptions referenced in the descriptor will be deleted.
 *****************************************************************************/
void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
{
    int i_area_index;

    ASSERT( p_area );

    /* Find the area in the areas table */
    for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
         i_area_index++ )
    {
        if( p_input->stream.pp_areas[i_area_index] == p_area )
            break;
    }

    /* Remove this area from the stream's list of areas */
    p_input->stream.i_area_nb--;

    p_input->stream.pp_areas[i_area_index] =
        p_input->stream.pp_areas[p_input->stream.i_area_nb];
    p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
                                           p_input->stream.i_area_nb
                                            * sizeof(input_area_t *) );

    if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
    {
        intf_ErrMsg( "input error: unable to realloc area list"
432
                     " in input_DelArea" );
Stéphane Borel's avatar
   
Stéphane Borel committed
433
434
435
436
437
438
439
    }

    /* Free the description of this area */
    free( p_area );
}


440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*****************************************************************************
 * input_FindES: returns a pointer to an ES described by its ID
 *****************************************************************************/
es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
{
    int     i;

    for( i = 0; i < p_input->stream.i_es_number; i++ )
    {
        if( p_input->stream.pp_es[i]->i_id == i_es_id )
        {
            return p_input->stream.pp_es[i];
        }
    }

    return( NULL );
456
457
458
459
460
461
462
463
464
465
}

/*****************************************************************************
 * input_AddES:
 *****************************************************************************
 * Reserve a slot in the table of ES descriptors for the ES and add it to the
 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
 * alone (PSI ?)
 *****************************************************************************/
es_descriptor_t * input_AddES( input_thread_t * p_input,
466
467
                               pgrm_descriptor_t * p_pgrm, u16 i_es_id,
                               size_t i_data_len )
468
{
469
    es_descriptor_t * p_es;
470

471
    p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
Henri Fallon's avatar
   
Henri Fallon committed
472
473
474
    if( p_es == NULL )
    {
        intf_ErrMsg( "Unable to allocate memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
475
        return( NULL);
Henri Fallon's avatar
   
Henri Fallon committed
476
    }
477
478
479
480
    p_input->stream.i_es_number++;
    p_input->stream.pp_es = realloc( p_input->stream.pp_es,
                                     p_input->stream.i_es_number
                                      * sizeof(es_descriptor_t *) );
Henri Fallon's avatar
   
Henri Fallon committed
481
482
483
    if( p_input->stream.pp_es == NULL )
    {
        intf_ErrMsg( "Unable to realloc memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
484
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
485
    }
486
    p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
487

488
    /* Init its values */
489
    p_es->i_id = i_es_id;
490
    p_es->psz_desc[0] = '\0';
491
492
    p_es->p_pes = NULL;
    p_es->p_decoder_fifo = NULL;
493
    p_es->b_audio = 0;
Stéphane Borel's avatar
Stéphane Borel committed
494
    p_es->i_cat = UNKNOWN_ES;
495
496

    if( i_data_len )
497
    {
498
        p_es->p_demux_data = malloc( i_data_len );
Henri Fallon's avatar
   
Henri Fallon committed
499
500
501
        if( p_es->p_demux_data == NULL )
        {
            intf_ErrMsg( "Unable to allocate memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
502
            return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
503
        }
504
505
        memset( p_es->p_demux_data, 0, i_data_len );
    }
506
507
508
509
    else
    {
        p_es->p_demux_data = NULL;
    }
510
511
512
513
514
515
516
517

    /* Add this ES to the program definition if one is given */
    if( p_pgrm )
    {
        p_pgrm->i_es_number++;
        p_pgrm->pp_es = realloc( p_pgrm->pp_es,
                                 p_pgrm->i_es_number
                                  * sizeof(es_descriptor_t *) );
Henri Fallon's avatar
   
Henri Fallon committed
518
519
520
        if( p_pgrm->pp_es == NULL )
        {
            intf_ErrMsg( "Unable to realloc memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
521
            return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
522
        }
523
524
        p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
        p_es->p_pgrm = p_pgrm;
525
526
527
    }
    else
    {
528
        p_es->p_pgrm = NULL;
529
530
531
532
533
534
535
536
    }

    return p_es;
}

/*****************************************************************************
 * input_DelES:
 *****************************************************************************/
537
void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
538
{
539
540
    int                     i_index, i_es_index;
    pgrm_descriptor_t *     p_pgrm;
541

542
543
544
545
546
    ASSERT( p_es );
    p_pgrm = p_es->p_pgrm;

    /* Kill associated decoder, if any. */
    if( p_es->p_decoder_fifo != NULL )
547
    {
548
        input_EndDecoder( p_input, p_es );
549
550
551
552
553
554
    }

    /* Remove this ES from the description of the program if it is associated to
     * one */
    if( p_pgrm )
    {
555
        for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
556
        {
557
            if( p_pgrm->pp_es[i_index] == p_es )
558
559
            {
                p_pgrm->i_es_number--;
560
                p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
561
562
                p_pgrm->pp_es = realloc( p_pgrm->pp_es,
                                         p_pgrm->i_es_number
563
                                          * sizeof(es_descriptor_t *));
564
                if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
Henri Fallon's avatar
   
Henri Fallon committed
565
566
567
                {
                    intf_ErrMsg( "Unable to realloc memory in input_DelES" );
                }
568
569
570
571
572
                break;
            }
        }
    }

573
574
575
576
577
    /* Free the demux data */
    if( p_es->p_demux_data != NULL )
    {
        free( p_es->p_demux_data );
    }
578

579
580
581
582
583
584
585
586
    /* Find the ES in the ES table */
    for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
         i_es_index++ )
    {
        if( p_input->stream.pp_es[i_es_index] == p_es )
            break;
    }

587
588
    /* Free the ES */
    free( p_es );
589
590
591
592
593
594
    p_input->stream.i_es_number--;
    p_input->stream.pp_es[i_es_index] =
                    p_input->stream.pp_es[p_input->stream.i_es_number];
    p_input->stream.pp_es = realloc( p_input->stream.pp_es,
                                     p_input->stream.i_es_number
                                      * sizeof(es_descriptor_t *));
595
    if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
Henri Fallon's avatar
   
Henri Fallon committed
596
597
598
599
    {
        intf_ErrMsg( "Unable to realloc memory in input_DelES" );
    }
    
600
601
602
603
}

/*****************************************************************************
 * input_SelectES: selects an ES and spawns the associated decoder
604
605
606
 *****************************************************************************
 * Remember we are still supposed to have stream_lock when entering this
 * function ?
607
608
609
 *****************************************************************************/
int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
{
610
611
    if( p_es == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
612
        intf_ErrMsg( "input error: nothing to do in input_SelectES" );
613
614
615
        return -1;
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
616
    intf_WarnMsg( 4, "input: selecting ES 0x%x", p_es->i_id );
617
618
619

    if( p_es->p_decoder_fifo != NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
620
        intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
621
622
623
        return( -1 );
    }

gbazin's avatar
   
gbazin committed
624
625
    p_es->thread_id = 0;

626
627
    switch( p_es->i_type )
    {
Henri Fallon's avatar
   
Henri Fallon committed
628
    case AC3_AUDIO_ES:
629
630
    case MPEG1_AUDIO_ES:
    case MPEG2_AUDIO_ES:
Henri Fallon's avatar
   
Henri Fallon committed
631
    case LPCM_AUDIO_ES:
632
633
        if( p_main->b_audio )
        {
634
635
636
            /* Release the lock, not to block the input thread during
             * the creation of the thread. */
            vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
637
            p_es->thread_id = input_RunDecoder( p_input, p_es );
638
            vlc_mutex_lock( &p_input->stream.stream_lock );
639
        }
640
641
642
643
        break;

    case MPEG1_VIDEO_ES:
    case MPEG2_VIDEO_ES:
644
    case DVD_SPU_ES:
645
646
        if( p_main->b_video )
        {
647
648
649
            /* Release the lock, not to block the input thread during
             * the creation of the thread. */
            vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
650
            p_es->thread_id = input_RunDecoder( p_input, p_es );
651
            vlc_mutex_lock( &p_input->stream.stream_lock );
652
        }
653
654
        break;

655
    default:
Sam Hocevar's avatar
   
Sam Hocevar committed
656
        intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
657
658
659
660
        return( -1 );
        break;
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
661
662
663
664
665
    if( p_es->thread_id == 0 )
    {
        return( -1 );
    }

666
    return( 0 );
667
}
668
669

/*****************************************************************************
670
 * input_UnselectES: removes an ES from the list of selected ES
671
 *****************************************************************************/
672
int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
673
674
675
676
{

    int     i_index = 0;

677
678
679
680
681
682
    if( p_es == NULL )
    {
        intf_ErrMsg( "Nothing to do in input_UnselectES" );
        return -1;
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
683
    intf_WarnMsg( 4, "input: unselecting ES 0x%x", p_es->i_id );
684
685
686

    if( p_es->p_decoder_fifo == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
687
        intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
688
689
690
691
692
        return( -1 );
    }

    input_EndDecoder( p_input, p_es );

693
694
    if( ( p_es->p_decoder_fifo == NULL ) &&
        ( p_input->stream.i_selected_es_number > 0 ) )
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
    {
        p_input->stream.i_selected_es_number--;

        while( ( i_index < p_input->stream.i_selected_es_number ) &&
               ( p_input->stream.pp_selected_es[i_index] != p_es ) )
        {
            i_index++;
        }

        p_input->stream.pp_selected_es[i_index] = 
          p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];

        p_input->stream.pp_selected_es = realloc(
                                           p_input->stream.pp_selected_es,
                                           p_input->stream.i_selected_es_number
                                            * sizeof(es_descriptor_t *) );
711

712
713
        if( p_input->stream.pp_selected_es == NULL )
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
714
            intf_WarnMsg( 4, "input: no more selected ES in input_UnselectES" );
715
            return( 1 );
716
717
        }
    }
718

719
720
    return( 0 );
}