]> git.sesse.net Git - vlc/blob - src/input/es_out.c
* all: sout have now access to psz_language.
[vlc] / src / input / es_out.c
1 /*****************************************************************************
2  * es_out.c: Es Out handler for input.
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id: es_out.c,v 1.23 2004/01/30 17:49:21 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
32
33 #include "vlc_playlist.h"
34 #include "codecs.h"
35 #include "iso_lang.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 struct es_out_id_t
41 {
42     int             i_channel;
43     es_descriptor_t *p_es;
44 };
45
46 struct es_out_sys_t
47 {
48     input_thread_t *p_input;
49     vlc_bool_t      b_pcr_set;
50
51     /* all es */
52     int         i_id;
53
54     int         i_es;
55     es_out_id_t **es;
56
57     /* mode gestion */
58     vlc_bool_t  b_active;
59     int         i_mode;
60
61     /* es count */
62     int         i_audio;
63     int         i_video;
64     int         i_sub;
65
66     /* es to select */
67     int         i_audio_last;
68     int         i_sub_last;
69
70     /* current main es */
71     es_out_id_t *p_es_audio;
72     es_out_id_t *p_es_video;
73     es_out_id_t *p_es_sub;
74 };
75
76 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
77 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
78 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
79 static int          EsOutControl( es_out_t *, int i_query, va_list );
80
81 static char *LanguageGetName( const char *psz_code );
82
83 /**
84  * Create a new es_out structure
85  *
86  * \param p_input The related input thread
87  * \return the new es_out_t
88  */
89 es_out_t *input_EsOutNew( input_thread_t *p_input )
90 {
91     es_out_t     *out = malloc( sizeof( es_out_t ) );
92     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
93     vlc_value_t  val;
94
95     out->pf_add     = EsOutAdd;
96     out->pf_send    = EsOutSend;
97     out->pf_del     = EsOutDel;
98     out->pf_control = EsOutControl;
99     out->p_sys      = p_sys;
100
101     p_sys->p_input = p_input;
102     p_sys->b_pcr_set = VLC_FALSE;
103
104     p_sys->b_active = VLC_FALSE;
105     p_sys->i_mode   = ES_OUT_MODE_AUTO;
106
107     p_sys->i_id    = 1;
108
109     p_sys->i_es    = 0;
110     p_sys->es      = NULL;
111
112     p_sys->i_audio = 0;
113     p_sys->i_video = 0;
114     p_sys->i_sub   = 0;
115
116     var_Get( p_input, "audio-channel", &val );
117     p_sys->i_audio_last = val.i_int;
118
119     var_Get( p_input, "spu-channel", &val );
120     p_sys->i_sub_last = val.i_int;
121
122     p_sys->p_es_audio = NULL;
123     p_sys->p_es_video = NULL;
124     p_sys->p_es_sub   = NULL;
125
126     return out;
127 }
128
129 /**
130  * Deletes an es_out structure
131  *
132  * \param out  the es_out structure to destroy
133  * \return nothing
134  */
135 void input_EsOutDelete( es_out_t *out )
136 {
137     es_out_sys_t *p_sys = out->p_sys;
138     int i;
139
140     for( i = 0; i < p_sys->i_es; i++ )
141     {
142         free( p_sys->es[i] );
143     }
144     if( p_sys->es )
145     {
146         free( p_sys->es );
147     }
148     free( p_sys );
149     free( out );
150 }
151
152 /**
153  * Add a program
154  *
155  * \param out the es_out
156  * \param i_group ...
157  * \return a program descriptor for the new program
158  */
159 static pgrm_descriptor_t *EsOutAddProgram( es_out_t *out, int i_group )
160 {
161     input_thread_t    *p_input = out->p_sys->p_input;
162     pgrm_descriptor_t *p_prgm;
163     es_descriptor_t   *p_pmt;
164
165     /* FIXME we should use a object variable but a lot of place in src/input
166      * have to be changed */
167     int               i_select = config_GetInt( p_input, "program" );
168
169     /* create it */
170     p_prgm = input_AddProgram( p_input, i_group, 0 );
171
172     /* XXX welcome to kludge, add a dummy es, if you want to understand
173      * why have a look at input_SetProgram. Basicaly, it assume the first
174      * es to be the PMT, how that is stupid, nevertheless it is needed for
175      * the old ts demuxer */
176     p_pmt = input_AddES( p_input, p_prgm, 0, UNKNOWN_ES, NULL, 0 );
177     p_pmt->i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
178
179     /* Select i_select or the first by default */
180     if( p_input->stream.p_selected_program == NULL &&
181         ( i_select <= 0 || i_select == i_group ) )
182     {
183         p_input->stream.p_selected_program = p_prgm;
184     }
185
186     return p_prgm;
187 }
188
189 /**
190  * Select an ES given the current mode
191  * XXX: you need to take a the lock before (stream.stream_lock)
192  *
193  * \param out The es_out structure
194  * \param es es_out_id structure
195  * \param b_force ...
196  * \return nothing
197  */
198 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
199 {
200     es_out_sys_t      *p_sys = out->p_sys;
201     input_thread_t    *p_input = p_sys->p_input;
202
203     int i_cat = es->p_es->i_cat;
204
205     if( !p_sys->b_active ||
206         ( !b_force && es->p_es->fmt.i_priority < 0 ) )
207     {
208         return;
209     }
210
211     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
212     {
213         if( !es->p_es->p_dec )
214         {
215             input_SelectES( p_input, es->p_es );
216         }
217     }
218     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
219     {
220         int i_wanted  = -1;
221
222         if( es->p_es->p_pgrm != NULL &&
223             es->p_es->p_pgrm != p_input->stream.p_selected_program )
224         {
225             return;
226         }
227
228         if( i_cat == AUDIO_ES )
229         {
230             if( p_sys->p_es_audio &&
231                 p_sys->p_es_audio->p_es->fmt.i_priority >=
232                     es->p_es->fmt.i_priority )
233             {
234                 return;
235             }
236             i_wanted  = p_sys->i_audio_last >= 0 ?
237                             p_sys->i_audio_last : es->i_channel;
238         }
239         else if( i_cat == SPU_ES )
240         {
241             if( p_sys->p_es_sub &&
242                 p_sys->p_es_sub->p_es->fmt.i_priority >=
243                     es->p_es->fmt.i_priority )
244             {
245                 return;
246             }
247             i_wanted  = p_sys->i_sub_last;
248         }
249         else if( i_cat == VIDEO_ES )
250         {
251             i_wanted  = es->i_channel;
252         }
253
254         if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
255         {
256             input_SelectES( p_input, es->p_es );
257         }
258     }
259
260     /* FIXME TODO handle priority here */
261     if( es->p_es->p_dec )
262     {
263         if( i_cat == AUDIO_ES )
264         {
265             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
266                 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
267             {
268                 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
269             }
270             p_sys->p_es_audio = es;
271         }
272         else if( i_cat == SPU_ES )
273         {
274             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
275                 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
276             {
277                 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
278             }
279             p_sys->p_es_sub = es;
280         }
281         else if( i_cat == VIDEO_ES )
282         {
283             p_sys->p_es_video = es;
284         }
285     }
286 }
287
288 /**
289  * Add an es_out
290  *
291  * \param out the es_out to add
292  * \param fmt the es_format of the es_out
293  * \return an es_out id
294  */
295 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
296 {
297     es_out_sys_t      *p_sys = out->p_sys;
298     playlist_t        *p_playlist = NULL;
299     input_thread_t    *p_input = p_sys->p_input;
300     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
301     pgrm_descriptor_t *p_prgm = NULL;
302     char              psz_cat[sizeof( _("Stream ") ) + 10];
303     input_info_category_t *p_cat;
304     char              *psz_description;
305
306     vlc_mutex_lock( &p_input->stream.stream_lock );
307     if( fmt->i_group >= 0 )
308     {
309         /* search program */
310         p_prgm = input_FindProgram( p_input, fmt->i_group );
311
312         if( p_prgm == NULL )
313         {
314             /* Create it */
315             p_prgm = EsOutAddProgram( out, fmt->i_group );
316         }
317     }
318     if( fmt->i_id < 0 )
319     {
320         fmt->i_id = out->p_sys->i_id - 1;
321     }
322
323     psz_description = LanguageGetName( fmt->psz_language );
324     es->p_es = input_AddES( p_input,
325                             p_prgm,
326                             fmt->i_id + 1,
327                             fmt->i_cat,
328                             psz_description, 0 );
329     es->p_es->i_stream_id = fmt->i_id;
330     es->p_es->i_fourcc = fmt->i_codec;
331     free( psz_description );
332
333     switch( fmt->i_cat )
334     {
335         case AUDIO_ES:
336         {
337             WAVEFORMATEX *p_wf =
338                 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
339
340             p_wf->wFormatTag        = WAVE_FORMAT_UNKNOWN;
341             p_wf->nChannels         = fmt->audio.i_channels;
342             p_wf->nSamplesPerSec    = fmt->audio.i_rate;
343             p_wf->nAvgBytesPerSec   = fmt->i_bitrate / 8;
344             p_wf->nBlockAlign       = fmt->audio.i_blockalign;
345             p_wf->wBitsPerSample    = fmt->audio.i_bitspersample;
346             p_wf->cbSize            = fmt->i_extra;
347             if( fmt->i_extra > 0 )
348             {
349                 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
350             }
351             es->p_es->p_waveformatex = p_wf;
352
353             es->i_channel = p_sys->i_audio;
354             break;
355         }
356         case VIDEO_ES:
357         {
358             BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
359                                               fmt->i_extra );
360             p_bih->biSize           = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
361             p_bih->biWidth          = fmt->video.i_width;
362             p_bih->biHeight         = fmt->video.i_height;
363             p_bih->biPlanes         = 1;
364             p_bih->biBitCount       = 24;
365             p_bih->biCompression    = fmt->i_codec;
366             p_bih->biSizeImage      = fmt->video.i_width *
367                                           fmt->video.i_height;
368             p_bih->biXPelsPerMeter  = 0;
369             p_bih->biYPelsPerMeter  = 0;
370             p_bih->biClrUsed        = 0;
371             p_bih->biClrImportant   = 0;
372
373             if( fmt->i_extra > 0 )
374             {
375                 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
376             }
377             es->p_es->p_bitmapinfoheader = p_bih;
378
379             es->i_channel = p_sys->i_video;
380             break;
381         }
382         case SPU_ES:
383         {
384             subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
385             memset( p_sub, 0, sizeof( subtitle_data_t ) );
386             if( fmt->i_extra > 0 )
387             {
388                 p_sub->psz_header = malloc( fmt->i_extra  + 1 );
389                 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
390                 /* just to be sure */
391                 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
392             }
393             /* FIXME beuuuuuurk */
394             es->p_es->p_demux_data = p_sub;
395
396             es->i_channel = p_sys->i_sub;
397             break;
398         }
399
400         default:
401             es->i_channel = 0;
402             break;
403     }
404
405     sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
406     /* Get a category and the playlist */
407     if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) &&
408         ( p_playlist = (playlist_t *)vlc_object_find( p_input,
409                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE ) ) )
410     {
411         /* Add information */
412         switch( fmt->i_cat )
413         {
414             case AUDIO_ES:
415                 if( fmt->psz_description )
416                 {
417                     input_AddInfo( p_cat, _("Description"), "%s",
418                                    fmt->psz_description );
419                     playlist_AddInfo( p_playlist, -1, psz_cat,
420                                      _("Description"), "%s",
421                                      fmt->psz_description );
422                 }
423                 input_AddInfo( p_cat, _("Codec"), "%.4s",
424                                (char*)&fmt->i_codec );
425                 playlist_AddInfo( p_playlist, -1, psz_cat,
426                                   _("Codec"),"%.4s",(char*)&fmt->i_codec );
427
428                 input_AddInfo( p_cat, _("Type"), _("Audio") );
429                 playlist_AddInfo( p_playlist, -1, psz_cat,
430                                  _("Type"), _("Audio") );
431
432                 if( fmt->audio.i_channels > 0 )
433                 {
434                     input_AddInfo( p_cat, _("Channels"), "%d",
435                                    fmt->audio.i_channels );
436                     playlist_AddInfo( p_playlist, -1, psz_cat,
437                                       _("Channels"), "%d",                                                            fmt->audio.i_channels );
438                 }
439                 if( fmt->psz_language )
440                 {
441                     input_AddInfo( p_cat, _("Language"), "%s",
442                                    fmt->psz_language );
443                     playlist_AddInfo( p_playlist, -1, psz_cat,
444                                      _("Language"), "%s",
445                                      fmt->psz_language );
446                 }
447                 if( fmt->audio.i_rate > 0 )
448                 {
449                     input_AddInfo( p_cat, _("Sample rate"), _("%d Hz"),
450                                    fmt->audio.i_rate );
451                     playlist_AddInfo( p_playlist, -1, psz_cat,
452                                      _("Sample rate"), _("%d Hz"),
453                                       fmt->audio.i_rate );
454                 }
455                 if( fmt->i_bitrate > 0 )
456                 {
457                     input_AddInfo( p_cat, _("Bitrate"), _("%d bps"),
458                                    fmt->i_bitrate );
459                     playlist_AddInfo( p_playlist, -1, psz_cat,
460                                     _("Bitrate"), _("%d bps"),
461                                      fmt->i_bitrate );
462                 }
463                 if( fmt->audio.i_bitspersample )
464                 {
465                     input_AddInfo( p_cat, _("Bits per sample"), "%d",
466                                    fmt->audio.i_bitspersample );
467                     playlist_AddInfo( p_playlist, -1, psz_cat,
468                                      _("Bits per sample"), "%d",
469                                      fmt->audio.i_bitspersample );
470                 }
471                 break;
472             case VIDEO_ES:
473                 if( fmt->psz_description )
474                 {
475                     input_AddInfo( p_cat, _("Description"), "%s",
476                                    fmt->psz_description );
477                     playlist_AddInfo( p_playlist, -1, psz_cat,
478                                      _("Description"), "%s",
479                                      fmt->psz_description );
480                 }
481                 input_AddInfo( p_cat, _("Type"), _("Video") );
482                 playlist_AddInfo( p_playlist, -1, psz_cat,
483                                 _("Type"), _("Video") );
484
485                 input_AddInfo( p_cat, _("Codec"), "%.4s",
486                                (char*)&fmt->i_codec );
487                 playlist_AddInfo( p_playlist, -1, psz_cat,
488                                  _("Codec"), "%.4s",
489                                  (char*)&fmt->i_codec );
490
491                 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
492                 {
493                     input_AddInfo( p_cat, _("Resolution"), "%dx%d",
494                                    fmt->video.i_width, fmt->video.i_height );
495                     playlist_AddInfo( p_playlist, -1, psz_cat,
496                                     _("Resolution"), "%dx%d",
497                                     fmt->video.i_width, fmt->video.i_height );
498                 }
499                 if( fmt->video.i_visible_width > 0 &&
500                     fmt->video.i_visible_height > 0 )
501                 {
502                     input_AddInfo( p_cat, _("Display resolution"), "%dx%d",
503                                    fmt->video.i_visible_width,
504                                    fmt->video.i_visible_height);
505                      playlist_AddInfo( p_playlist, -1, psz_cat,
506                                        _("Display resolution"), "%dx%d",
507                                        fmt->video.i_visible_width,
508                                        fmt->video.i_visible_height);
509                 }
510                 break;
511             case SPU_ES:
512                 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
513                 playlist_AddInfo( p_playlist, -1, psz_cat,
514                                    _("Type"), _("Subtitle") );
515                 input_AddInfo( p_cat, _("Codec"), "%.4s",
516                                (char*)&fmt->i_codec );
517                 playlist_AddInfo( p_playlist, -1, psz_cat,
518                                  _("Codec"), "%.4s",
519                                  (char*)&fmt->i_codec );
520                 break;
521             default:
522
523                 break;
524         }
525         if( p_playlist ) vlc_object_release( p_playlist );
526     }
527
528     es_format_Copy( &es->p_es->fmt, fmt );
529
530     /* Apply mode
531      * XXX change that when we do group too */
532     if( 1 )
533     {
534         EsOutSelect( out, es, VLC_FALSE );
535     }
536     vlc_mutex_unlock( &p_input->stream.stream_lock );
537
538     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
539     p_sys->i_id++;  /* always incremented */
540     switch( fmt->i_cat )
541     {
542         case AUDIO_ES:
543             p_sys->i_audio++;
544             break;
545         case SPU_ES:
546             p_sys->i_sub++;
547             break;
548         case VIDEO_ES:
549             p_sys->i_video++;
550             break;
551     }
552
553     return es;
554 }
555
556 /**
557  * Send a block for the given es_out
558  *
559  * \param out the es_out to send from
560  * \param es the es_out_id
561  * \param p_block the data block to send
562  */
563 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
564 {
565     es_out_sys_t *p_sys = out->p_sys;
566
567     if( p_sys->b_pcr_set )
568     {
569         pgrm_descriptor_t *p_pgrm = es->p_es->p_pgrm;
570         input_thread_t    *p_input = p_sys->p_input;
571
572         if( p_pgrm == NULL )
573         {
574             p_pgrm = p_sys->p_input->stream.p_selected_program;
575         }
576
577         if( p_block->i_dts > 0 && p_pgrm )
578         {
579             p_block->i_dts =
580                 input_ClockGetTS( p_input, p_pgrm, p_block->i_dts * 9 / 100 );
581         }
582         if( p_block->i_pts > 0 && p_pgrm )
583         {
584             p_block->i_pts =
585                 input_ClockGetTS( p_input, p_pgrm, p_block->i_pts * 9 / 100 );
586         }
587     }
588
589     vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
590     p_block->i_rate = out->p_sys->p_input->stream.control.i_rate;
591     if( es->p_es->p_dec &&
592         (es->p_es->i_cat!=AUDIO_ES || !p_sys->p_input->stream.control.b_mute) )
593     {
594         input_DecodeBlock( es->p_es->p_dec, p_block );
595     }
596     else
597     {
598         block_Release( p_block );
599     }
600     vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
601
602     return VLC_SUCCESS;
603 }
604
605 /*****************************************************************************
606  * EsOutDel:
607  *****************************************************************************/
608 static void EsOutDel( es_out_t *out, es_out_id_t *es )
609 {
610     es_out_sys_t *p_sys = out->p_sys;
611
612     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
613
614     switch( es->p_es->i_cat )
615     {
616         case AUDIO_ES:
617             p_sys->i_audio--;
618             break;
619         case SPU_ES:
620             p_sys->i_sub--;
621             break;
622         case VIDEO_ES:
623             p_sys->i_video--;
624             break;
625     }
626
627     /* We don't try to reselect */
628     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
629     if( es->p_es->p_dec )
630     {
631         input_UnselectES( p_sys->p_input, es->p_es );
632     }
633
634     if( es->p_es->p_waveformatex )
635     {
636         free( es->p_es->p_waveformatex );
637         es->p_es->p_waveformatex = NULL;
638     }
639     if( es->p_es->p_bitmapinfoheader )
640     {
641         free( es->p_es->p_bitmapinfoheader );
642         es->p_es->p_bitmapinfoheader = NULL;
643     }
644     input_DelES( p_sys->p_input, es->p_es );
645
646     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
647     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
648     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
649
650     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
651
652     free( es );
653 }
654
655 /**
656  * Control query handler
657  *
658  * \param out the es_out to control
659  * \param i_query A es_out query as defined in include/ninput.h
660  * \param args a variable list of arguments for the query
661  * \return VLC_SUCCESS or an error code
662  */
663 static int EsOutControl( es_out_t *out, int i_query, va_list args )
664 {
665     es_out_sys_t *p_sys = out->p_sys;
666     vlc_bool_t  b, *pb;
667     int         i, *pi;
668
669     es_out_id_t *es;
670
671     switch( i_query )
672     {
673         case ES_OUT_SET_ES_STATE:
674             vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
675             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
676             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
677             if( b && es->p_es->p_dec == NULL )
678             {
679                 input_SelectES( p_sys->p_input, es->p_es );
680                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
681                 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
682             }
683             else if( !b && es->p_es->p_dec )
684             {
685                 input_UnselectES( p_sys->p_input, es->p_es );
686                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
687                 return VLC_SUCCESS;
688             }
689             vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
690             return VLC_SUCCESS;
691
692         case ES_OUT_GET_ES_STATE:
693             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
694             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
695
696             *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
697             return VLC_SUCCESS;
698
699         case ES_OUT_SET_ACTIVE:
700         {
701             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
702             p_sys->b_active = b;
703
704             if( b )
705             {
706                 vlc_value_t val;
707                 val.b_bool = VLC_TRUE;
708                 var_Set( p_sys->p_input, "intf-change", val );
709             }
710             return VLC_SUCCESS;
711         }
712
713         case ES_OUT_GET_ACTIVE:
714             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
715             *pb = p_sys->b_active;
716             return VLC_SUCCESS;
717
718         case ES_OUT_SET_MODE:
719             i = (int) va_arg( args, int );
720             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
721                 i == ES_OUT_MODE_AUTO )
722             {
723                 vlc_value_t val;
724
725                 p_sys->i_mode = i;
726
727                 /* Reapply policy mode */
728                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
729                 for( i = 0; i < p_sys->i_es; i++ )
730                 {
731                     if( p_sys->es[i]->p_es->p_dec )
732                     {
733                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
734                     }
735                 }
736                 for( i = 0; i < p_sys->i_es; i++ )
737                 {
738                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
739                 }
740                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
741
742                 val.b_bool = VLC_TRUE;
743                 var_Set( p_sys->p_input, "intf-change", val );
744
745                 return VLC_SUCCESS;
746             }
747             return VLC_EGENERIC;
748
749         case ES_OUT_GET_MODE:
750             pi = (int*) va_arg( args, int* );
751             *pi = p_sys->i_mode;
752             return VLC_SUCCESS;
753
754         case ES_OUT_SET_ES:
755             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
756             if( es == NULL )
757             {
758                 for( i = 0; i < p_sys->i_es; i++ )
759                 {
760                     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
761                     if( p_sys->es[i]->p_es->p_dec )
762                     {
763                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
764                     }
765                     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
766                 }
767             }
768             else
769             {
770                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
771                 EsOutSelect( out, es, VLC_TRUE );
772                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
773             }
774             return VLC_SUCCESS;
775
776         case ES_OUT_SET_PCR:
777         case ES_OUT_SET_GROUP_PCR:
778         {
779             pgrm_descriptor_t *p_prgm = NULL;
780             int64_t           i_pcr;
781
782             if( i_query == ES_OUT_SET_PCR )
783             {
784                 p_prgm = p_sys->p_input->stream.p_selected_program;
785             }
786             else
787             {
788                 int i_group = (int)va_arg( args, int );
789                 p_prgm = input_FindProgram( p_sys->p_input, i_group );
790                 if( p_prgm == NULL )
791                 {
792                     /* we create the requested program */
793                     p_prgm = EsOutAddProgram( out, i_group );
794                 }
795             }
796             i_pcr   = (int64_t)va_arg( args, int64_t );
797
798             /* search program */
799             if( p_prgm )
800             {
801                 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr * 9 / 100);
802             }
803             p_sys->b_pcr_set = VLC_TRUE;
804             return VLC_SUCCESS;
805         }
806
807         case ES_OUT_RESET_PCR:
808             for( i = 0; i < p_sys->p_input->stream.i_pgrm_number; i++ )
809             {
810                 p_sys->p_input->stream.pp_programs[i]->i_synchro_state = SYNCHRO_REINIT;
811             }
812             p_sys->b_pcr_set = VLC_TRUE;
813             return VLC_SUCCESS;
814
815         default:
816             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
817             return VLC_EGENERIC;
818     }
819 }
820
821 /****************************************************************************
822  * LanguageGetName: try to expend iso639 into plain name
823  ****************************************************************************/
824 static char *LanguageGetName( const char *psz_code )
825 {
826     const iso639_lang_t *pl;
827
828     if( psz_code == NULL )
829     {
830         return strdup( "" );
831     }
832
833     if( strlen( psz_code ) == 2 )
834     {
835         pl = GetLang_1( psz_code );
836     }
837     else if( strlen( psz_code ) == 3 )
838     {
839         pl = GetLang_2B( psz_code );
840         if( !strcmp( pl->psz_iso639_1, "??" ) )
841         {
842             pl = GetLang_2T( psz_code );
843         }
844     }
845     else
846     {
847         return strdup( psz_code );
848     }
849
850     if( !strcmp( pl->psz_iso639_1, "??" ) )
851     {
852        return strdup( psz_code );
853     }
854     else
855     {
856         if( *pl->psz_native_name )
857         {
858             return strdup( pl->psz_native_name );
859         }
860         return strdup( pl->psz_eng_name );
861     }
862 }