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