]> git.sesse.net Git - vlc/blob - src/input/es_out.c
Moved input_EsOutDecodersIsEmpty to es_out_Control and cleaned its usage.
[vlc] / src / input / es_out.c
1 /*****************************************************************************
2  * es_out.c: Es Out handler for input.
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <assert.h>
34 #include <vlc_common.h>
35
36 #include <vlc_input.h>
37 #include <vlc_es_out.h>
38 #include <vlc_block.h>
39 #include <vlc_aout.h>
40
41 #include "input_internal.h"
42 #include "clock.h"
43 #include "decoder.h"
44 #include "es_out.h"
45
46 #include "../stream_output/stream_output.h"
47
48 #include <vlc_iso_lang.h>
49 /* FIXME we should find a better way than including that */
50 #include "../text/iso-639_def.h"
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 typedef struct
56 {
57     /* Program ID */
58     int i_id;
59
60     /* Number of es for this pgrm */
61     int i_es;
62
63     bool b_selected;
64
65     /* Clock for this program */
66     input_clock_t *p_clock;
67
68     char    *psz_name;
69     char    *psz_now_playing;
70     char    *psz_publisher;
71
72     vlc_epg_t *p_epg;
73 } es_out_pgrm_t;
74
75 struct es_out_id_t
76 {
77     /* ES ID */
78     int       i_id;
79     es_out_pgrm_t *p_pgrm;
80
81     /* Channel in the track type */
82     int         i_channel;
83     es_format_t fmt;
84     char        *psz_language;
85     char        *psz_language_code;
86
87     decoder_t   *p_dec;
88     decoder_t   *p_dec_record;
89
90     /* Fields for Video with CC */
91     bool  pb_cc_present[4];
92     es_out_id_t  *pp_cc_es[4];
93
94     /* Field for CC track from a master video */
95     es_out_id_t *p_master;
96 };
97
98 struct es_out_sys_t
99 {
100     input_thread_t *p_input;
101
102     /* */
103     vlc_mutex_t   lock;
104
105     /* all programs */
106     int           i_pgrm;
107     es_out_pgrm_t **pgrm;
108     es_out_pgrm_t **pp_selected_pgrm; /* --programs */
109     es_out_pgrm_t *p_pgrm;  /* Master program */
110
111     /* all es */
112     int         i_id;
113     int         i_es;
114     es_out_id_t **es;
115
116     /* mode gestion */
117     bool  b_active;
118     int         i_mode;
119
120     /* es count */
121     int         i_audio;
122     int         i_video;
123     int         i_sub;
124
125     /* es to select */
126     int         i_audio_last, i_audio_id;
127     int         i_sub_last, i_sub_id;
128     int         i_default_sub_id;   /* As specified in container; if applicable */
129     char        **ppsz_audio_language;
130     char        **ppsz_sub_language;
131
132     /* current main es */
133     es_out_id_t *p_es_audio;
134     es_out_id_t *p_es_video;
135     es_out_id_t *p_es_sub;
136
137     /* delay */
138     int64_t i_audio_delay;
139     int64_t i_spu_delay;
140
141     /* Rate used for clock */
142     int         i_rate;
143
144     /* */
145     bool        b_paused;
146
147     /* Current preroll */
148     mtime_t     i_preroll_end;
149
150     /* Used for buffering */
151     bool        b_buffering;
152     mtime_t     i_buffering_extra_initial;
153     mtime_t     i_buffering_extra_stream;
154     mtime_t     i_buffering_extra_system;
155
156     /* Record */
157     sout_instance_t *p_sout_record;
158 };
159
160 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
161 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
162 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
163 static int          EsOutControl( es_out_t *, int i_query, va_list );
164 static void         EsOutDelete ( es_out_t *out );
165
166 static void         EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
167 static void         EsOutAddInfo( es_out_t *, es_out_id_t *es );
168
169 static bool EsIsSelected( es_out_id_t *es );
170 static void EsSelect( es_out_t *out, es_out_id_t *es );
171 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
172 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
173 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
174 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
175 static void EsOutProgramsChangeRate( es_out_t *out );
176 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
177 static char *LanguageGetName( const char *psz_code );
178 static char *LanguageGetCode( const char *psz_lang );
179 static char **LanguageSplit( const char *psz_langs );
180 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
181
182 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
183
184 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
185     VLC_FOURCC('c', 'c', '1', ' '),
186     VLC_FOURCC('c', 'c', '2', ' '),
187     VLC_FOURCC('c', 'c', '3', ' '),
188     VLC_FOURCC('c', 'c', '4', ' '),
189 };
190 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
191 {
192     int i;
193     for( i = 0; i < 4; i++ )
194     {
195         if( fcc == EsOutFourccClosedCaptions[i] )
196             return i;
197     }
198     return -1;
199 }
200
201
202 /*****************************************************************************
203  * input_EsOutNew:
204  *****************************************************************************/
205 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
206 {
207     vlc_value_t  val;
208     int i;
209
210     es_out_t     *out = malloc( sizeof( *out ) );
211     if( !out )
212         return NULL;
213
214     es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
215     if( !p_sys )
216     {
217         free( out );
218         return NULL;
219     }
220
221     out->pf_add     = EsOutAdd;
222     out->pf_send    = EsOutSend;
223     out->pf_del     = EsOutDel;
224     out->pf_control = EsOutControl;
225     out->pf_destroy = EsOutDelete;
226     out->p_sys      = p_sys;
227     out->b_sout     = p_input->p->p_sout != NULL;
228
229
230     vlc_mutex_init_recursive( &p_sys->lock );
231     p_sys->p_input = p_input;
232
233     p_sys->b_active = false;
234     p_sys->i_mode   = ES_OUT_MODE_AUTO;
235
236
237     TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
238     p_sys->p_pgrm   = NULL;
239
240     p_sys->i_id    = 0;
241
242     TAB_INIT( p_sys->i_es, p_sys->es );
243
244     p_sys->i_audio = 0;
245     p_sys->i_video = 0;
246     p_sys->i_sub   = 0;
247
248     /* */
249     var_Get( p_input, "audio-track", &val );
250     p_sys->i_audio_last = val.i_int;
251
252     var_Get( p_input, "sub-track", &val );
253     p_sys->i_sub_last = val.i_int;
254
255     p_sys->i_default_sub_id   = -1;
256
257     if( !p_input->b_preparsing )
258     {
259         var_Get( p_input, "audio-language", &val );
260         p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
261         if( p_sys->ppsz_audio_language )
262         {
263             for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
264                 msg_Dbg( p_input, "selected audio language[%d] %s",
265                          i, p_sys->ppsz_audio_language[i] );
266         }
267         free( val.psz_string );
268
269         var_Get( p_input, "sub-language", &val );
270         p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
271         if( p_sys->ppsz_sub_language )
272         {
273             for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
274                 msg_Dbg( p_input, "selected subtitle language[%d] %s",
275                          i, p_sys->ppsz_sub_language[i] );
276         }
277         free( val.psz_string );
278     }
279     else
280     {
281         p_sys->ppsz_sub_language = NULL;
282         p_sys->ppsz_audio_language = NULL;
283     }
284
285     var_Get( p_input, "audio-track-id", &val );
286     p_sys->i_audio_id = val.i_int;
287
288     var_Get( p_input, "sub-track-id", &val );
289     p_sys->i_sub_id = val.i_int;
290
291     p_sys->p_es_audio = NULL;
292     p_sys->p_es_video = NULL;
293     p_sys->p_es_sub   = NULL;
294
295     p_sys->i_audio_delay= 0;
296     p_sys->i_spu_delay  = 0;
297
298     p_sys->b_paused = false;
299
300     p_sys->i_rate = i_rate;
301
302     p_sys->b_buffering = true;
303     p_sys->i_buffering_extra_initial = 0;
304     p_sys->i_buffering_extra_stream = 0;
305     p_sys->i_buffering_extra_system = 0;
306     p_sys->i_preroll_end = -1;
307
308     p_sys->p_sout_record = NULL;
309
310     return out;
311 }
312
313 void input_EsOutChangeRate( es_out_t *out, int i_rate )
314 {
315     es_out_sys_t      *p_sys = out->p_sys;
316
317     p_sys->i_rate = i_rate;
318
319     if( !p_sys->b_paused )
320         EsOutProgramsChangeRate( out );
321 }
322
323 int input_EsOutSetRecord(  es_out_t *out, bool b_record )
324 {
325     es_out_sys_t   *p_sys = out->p_sys;
326     input_thread_t *p_input = p_sys->p_input;
327
328     assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
329
330     if( b_record )
331     {
332         char *psz_path = var_CreateGetString( p_input, "input-record-path" );
333         if( !psz_path || *psz_path == '\0' )
334         {
335             free( psz_path );
336             psz_path = strdup( config_GetHomeDir() );
337         }
338
339         char *psz_sout = NULL;  // TODO conf
340
341         if( !psz_sout && psz_path )
342         {
343             char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
344             if( psz_file )
345             {
346                 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
347                     psz_sout = NULL;
348                 free( psz_file );
349             }
350         }
351         free( psz_path );
352
353         if( !psz_sout )
354             return VLC_EGENERIC;
355
356 #ifdef ENABLE_SOUT
357         p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
358 #endif
359         free( psz_sout );
360
361         if( !p_sys->p_sout_record )
362             return VLC_EGENERIC;
363
364         for( int i = 0; i < p_sys->i_es; i++ )
365         {
366             es_out_id_t *p_es = p_sys->es[i];
367
368             if( !p_es->p_dec || p_es->p_master )
369                 continue;
370
371             p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
372             if( p_es->p_dec_record && p_sys->b_buffering )
373                 input_DecoderStartBuffering( p_es->p_dec_record );
374         }
375     }
376     else
377     {
378         for( int i = 0; i < p_sys->i_es; i++ )
379         {
380             es_out_id_t *p_es = p_sys->es[i];
381
382             if( !p_es->p_dec_record )
383                 continue;
384
385             input_DecoderDelete( p_es->p_dec_record );
386             p_es->p_dec_record = NULL;
387         }
388 #ifdef ENABLE_SOUT
389         sout_DeleteInstance( p_sys->p_sout_record );
390 #endif
391         p_sys->p_sout_record = NULL;
392     }
393
394     return VLC_SUCCESS;
395 }
396 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
397 {
398     es_out_sys_t *p_sys = out->p_sys;
399
400     if( i_cat == AUDIO_ES )
401         p_sys->i_audio_delay = i_delay;
402     else if( i_cat == SPU_ES )
403         p_sys->i_spu_delay = i_delay;
404
405     for( int i = 0; i < p_sys->i_es; i++ )
406         EsOutDecoderChangeDelay( out, p_sys->es[i] );
407 }
408 void input_EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
409 {
410     es_out_sys_t *p_sys = out->p_sys;
411
412     /* XXX the order is important */
413     if( b_paused )
414     {
415         EsOutDecodersChangePause( out, true, i_date );
416         EsOutProgramChangePause( out, true, i_date );
417     }
418     else
419     {
420         if( p_sys->i_buffering_extra_initial > 0 )
421         {
422             mtime_t i_stream_start;
423             mtime_t i_system_start;
424             mtime_t i_stream_duration;
425             mtime_t i_system_duration;
426             int i_ret;
427             i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
428                                           &i_stream_start, &i_system_start,
429                                           &i_stream_duration, &i_system_duration );
430             if( !i_ret )
431             {
432                 /* FIXME pcr != exactly what wanted */
433                 const mtime_t i_used = /*(i_stream_duration - p_sys->p_input->i_pts_delay)*/ p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
434                 i_date -= i_used;
435             }
436             p_sys->i_buffering_extra_initial = 0;
437             p_sys->i_buffering_extra_stream = 0;
438             p_sys->i_buffering_extra_system = 0;
439         }
440         EsOutProgramChangePause( out, false, i_date );
441         EsOutDecodersChangePause( out, false, i_date );
442
443         EsOutProgramsChangeRate( out );
444     }
445     p_sys->b_paused = b_paused;
446 }
447 void input_EsOutChangePosition( es_out_t *out )
448 {
449     es_out_sys_t      *p_sys = out->p_sys;
450
451     for( int i = 0; i < p_sys->i_es; i++ )
452     {
453         es_out_id_t *p_es = p_sys->es[i];
454
455         if( !p_es->p_dec )
456             continue;
457
458         input_DecoderStartBuffering( p_es->p_dec );
459
460         if( p_es->p_dec_record )
461             input_DecoderStartBuffering( p_es->p_dec );
462     }
463
464     for( int i = 0; i < p_sys->i_pgrm; i++ )
465         input_clock_Reset( p_sys->pgrm[i]->p_clock );
466
467     p_sys->b_buffering = true;
468     p_sys->i_buffering_extra_initial = 0;
469     p_sys->i_buffering_extra_stream = 0;
470     p_sys->i_buffering_extra_system = 0;
471     p_sys->i_preroll_end = -1;
472 }
473
474 void input_EsOutFrameNext( es_out_t *out )
475 {
476     es_out_sys_t *p_sys = out->p_sys;
477     es_out_id_t *p_es_video = NULL;
478
479     if( p_sys->b_buffering )
480     {
481         msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
482         return;
483     }
484
485     assert( p_sys->b_paused );
486
487     for( int i = 0; i < p_sys->i_es; i++ )
488     {
489         es_out_id_t *p_es = p_sys->es[i];
490
491         if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
492         {
493             p_es_video = p_es;
494             break;
495         }
496     }
497
498     if( !p_es_video )
499     {
500         msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
501         return;
502     }
503
504     mtime_t i_duration;
505     input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
506
507     msg_Dbg( out->p_sys->p_input, "input_EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
508
509     if( i_duration <= 0 )
510         i_duration = 40*1000;
511
512     /* FIXME it is not a clean way ? */
513     if( p_sys->i_buffering_extra_initial <= 0 )
514     {
515         mtime_t i_stream_start;
516         mtime_t i_system_start;
517         mtime_t i_stream_duration;
518         mtime_t i_system_duration;
519         int i_ret;
520
521         i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
522                                       &i_stream_start, &i_system_start,
523                                       &i_stream_duration, &i_system_duration );
524         if( i_ret )
525             return;
526
527         p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->p_input->i_pts_delay; /* FIXME < 0 ? */
528         p_sys->i_buffering_extra_system =
529         p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
530     }
531
532     const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
533
534     p_sys->b_buffering = true;
535     p_sys->i_buffering_extra_system += i_duration;
536     p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
537                                       ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
538                                                 INPUT_RATE_DEFAULT / i_rate;
539
540     p_sys->i_preroll_end = -1;
541 }
542
543 void input_EsOutLock( es_out_t *out )
544 {
545     vlc_mutex_lock( &out->p_sys->lock );
546 }
547 void input_EsOutUnlock( es_out_t *out )
548 {
549     vlc_mutex_unlock( &out->p_sys->lock );
550 }
551
552 /*****************************************************************************
553  *
554  *****************************************************************************/
555 static void EsOutDelete( es_out_t *out )
556 {
557     es_out_sys_t *p_sys = out->p_sys;
558     int i;
559
560     if( p_sys->p_sout_record )
561         input_EsOutSetRecord( out, false );
562
563     for( i = 0; i < p_sys->i_es; i++ )
564     {
565         if( p_sys->es[i]->p_dec )
566             input_DecoderDelete( p_sys->es[i]->p_dec );
567
568         free( p_sys->es[i]->psz_language );
569         free( p_sys->es[i]->psz_language_code );
570         es_format_Clean( &p_sys->es[i]->fmt );
571
572         free( p_sys->es[i] );
573     }
574     if( p_sys->ppsz_audio_language )
575     {
576         for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
577             free( p_sys->ppsz_audio_language[i] );
578         free( p_sys->ppsz_audio_language );
579     }
580     if( p_sys->ppsz_sub_language )
581     {
582         for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
583             free( p_sys->ppsz_sub_language[i] );
584         free( p_sys->ppsz_sub_language );
585     }
586     free( p_sys->es );
587
588     /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
589     for( i = 0; i < p_sys->i_pgrm; i++ )
590     {
591         es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
592         input_clock_Delete( p_pgrm->p_clock );
593         free( p_pgrm->psz_now_playing );
594         free( p_pgrm->psz_publisher );
595         free( p_pgrm->psz_name );
596         if( p_pgrm->p_epg )
597             vlc_epg_Delete( p_pgrm->p_epg );
598
599         free( p_pgrm );
600     }
601     TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
602     vlc_mutex_destroy( &p_sys->lock );
603
604     free( p_sys );
605     free( out );
606 }
607
608 static mtime_t EsOutGetWakeup( es_out_t *out )
609 {
610     es_out_sys_t   *p_sys = out->p_sys;
611     input_thread_t *p_input = p_sys->p_input;
612
613     if( !p_sys->p_pgrm )
614         return 0;
615
616     /* We do not have a wake up date if the input cannot have its speed
617      * controlled or sout is imposing its own or while buffering
618      *
619      * FIXME for !p_input->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
620     if( !p_input->b_can_pace_control ||
621         p_input->p->b_out_pace_control ||
622         p_sys->b_buffering )
623         return 0;
624
625     return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
626 }
627
628 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
629 {
630     int i;
631     if( i_id < 0 )
632     {
633         /* Special HACK, -i_id is the cat of the stream */
634         return (es_out_id_t*)((uint8_t*)NULL-i_id);
635     }
636
637     for( i = 0; i < out->p_sys->i_es; i++ )
638     {
639         if( out->p_sys->es[i]->i_id == i_id )
640             return out->p_sys->es[i];
641     }
642     return NULL;
643 }
644
645 static bool EsOutDecodersIsEmpty( es_out_t *out )
646 {
647     es_out_sys_t      *p_sys = out->p_sys;
648     int i;
649
650     if( p_sys->b_buffering && p_sys->p_pgrm )
651     {
652         EsOutDecodersStopBuffering( out, true );
653         if( p_sys->b_buffering )
654             return true;
655     }
656
657     for( i = 0; i < p_sys->i_es; i++ )
658     {
659         es_out_id_t *es = p_sys->es[i];
660
661         if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
662             return false;
663         if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
664             return false;
665     }
666     return true;
667 }
668
669
670 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
671 {
672     es_out_sys_t *p_sys = out->p_sys;
673     int i_ret;
674
675     mtime_t i_stream_start;
676     mtime_t i_system_start;
677     mtime_t i_stream_duration;
678     mtime_t i_system_duration;
679     i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
680                                   &i_stream_start, &i_system_start,
681                                   &i_stream_duration, &i_system_duration );
682     assert( !i_ret || b_forced );
683     if( i_ret )
684         return;
685
686     mtime_t i_preroll_duration = 0;
687     if( p_sys->i_preroll_end >= 0 )
688         i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
689
690     const mtime_t i_buffering_duration = p_sys->p_input->i_pts_delay +
691                                          i_preroll_duration +
692                                          p_sys->i_buffering_extra_stream;
693
694     if( i_stream_duration <= i_buffering_duration && !b_forced )
695     {
696         msg_Dbg( p_sys->p_input, "Buffering %d%%",
697                  (int)(100 * i_stream_duration / i_buffering_duration ) );
698         return;
699     }
700
701     msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
702               (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
703     p_sys->b_buffering = false;
704     p_sys->i_preroll_end = -1;
705
706     if( p_sys->i_buffering_extra_initial > 0 )
707     {
708         /* FIXME wrong ? */
709         return;
710     }
711
712     const mtime_t i_decoder_buffering_start = mdate();
713     for( int i = 0; i < p_sys->i_es; i++ )
714     {
715         es_out_id_t *p_es = p_sys->es[i];
716
717         if( !p_es->p_dec )
718             continue;
719         input_DecoderWaitBuffering( p_es->p_dec );
720         if( p_es->p_dec_record )
721             input_DecoderWaitBuffering( p_es->p_dec_record );
722     }
723
724     msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
725               (int)(mdate() - i_decoder_buffering_start)/1000 );
726
727     const mtime_t i_ts_delay = 10*1000 + /* FIXME CLEANUP thread wake up time*/
728                                mdate();
729     //msg_Dbg( p_sys->p_input, "==> %lld", i_ts_delay - p_sys->p_input->i_pts_delay );
730     input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_ts_delay - i_buffering_duration );
731
732     for( int i = 0; i < p_sys->i_es; i++ )
733     {
734         es_out_id_t *p_es = p_sys->es[i];
735
736         if( !p_es->p_dec )
737             continue;
738
739         input_DecoderStopBuffering( p_es->p_dec );
740         if( p_es->p_dec_record )
741             input_DecoderStopBuffering( p_es->p_dec );
742     }
743 }
744 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
745 {
746     es_out_sys_t *p_sys = out->p_sys;
747
748     /* Pause decoders first */
749     for( int i = 0; i < p_sys->i_es; i++ )
750     {
751         es_out_id_t *es = p_sys->es[i];
752
753         if( es->p_dec )
754         {
755             input_DecoderChangePause( es->p_dec, b_paused, i_date );
756             if( es->p_dec_record )
757                 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
758         }
759     }
760 }
761 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
762 {
763     es_out_sys_t *p_sys = out->p_sys;
764
765     for( int i = 0; i < p_sys->i_pgrm; i++ )
766         input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
767 }
768
769 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
770 {
771     es_out_sys_t *p_sys = out->p_sys;
772
773     mtime_t i_delay = 0;
774     if( p_es->fmt.i_cat == AUDIO_ES )
775         i_delay = p_sys->i_audio_delay;
776     else if( p_es->fmt.i_cat == SPU_ES )
777         i_delay = p_sys->i_spu_delay;
778
779     if( i_delay != 0 )
780     {
781         if( p_es->p_dec )
782             input_DecoderChangeDelay( p_es->p_dec, i_delay );
783         if( p_es->p_dec_record )
784             input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
785     }
786 }
787 static void EsOutProgramsChangeRate( es_out_t *out )
788 {
789     es_out_sys_t      *p_sys = out->p_sys;
790
791     for( int i = 0; i < p_sys->i_pgrm; i++ )
792         input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
793 }
794
795 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
796                                      bool b_delete )
797 {
798     es_out_sys_t      *p_sys = out->p_sys;
799     input_thread_t    *p_input = p_sys->p_input;
800     const  bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
801     vlc_value_t       val, text;
802
803     const char *psz_var;
804
805     if( fmt->i_cat == AUDIO_ES )
806         psz_var = "audio-es";
807     else if( fmt->i_cat == VIDEO_ES )
808         psz_var = "video-es";
809     else if( fmt->i_cat == SPU_ES )
810         psz_var = "spu-es";
811     else
812         return;
813
814     if( b_delete )
815     {
816         if( b_teletext )
817             var_SetInteger( p_sys->p_input, "teletext-es", -1 );
818
819         val.i_int = i_id;
820         var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
821
822         var_SetBool( p_sys->p_input, "intf-change", true );
823         return;
824     }
825
826     /* Get the number of ES already added */
827     var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
828     if( val.i_int == 0 )
829     {
830         vlc_value_t val2;
831
832         /* First one, we need to add the "Disable" choice */
833         val2.i_int = -1; text.psz_string = _("Disable");
834         var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
835         val.i_int++;
836     }
837
838     /* Take care of the ES description */
839     if( fmt->psz_description && *fmt->psz_description )
840     {
841         if( psz_language && *psz_language )
842         {
843             text.psz_string = malloc( strlen( fmt->psz_description) +
844                                       strlen( psz_language ) + 10 );
845             sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
846                                                    psz_language );
847         }
848         else text.psz_string = strdup( fmt->psz_description );
849     }
850     else
851     {
852         if( psz_language && *psz_language )
853         {
854             if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
855                 text.psz_string = NULL;
856         }
857         else
858         {
859             if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
860                 text.psz_string = NULL;
861         }
862     }
863
864     val.i_int = i_id;
865     var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
866
867     free( text.psz_string );
868
869     if( b_teletext )
870     {
871         if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
872             var_SetInteger( p_sys->p_input, "teletext-es", i_id );
873     }
874
875     var_SetBool( p_sys->p_input, "intf-change", true );
876 }
877
878 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
879                               bool b_delete )
880 {
881     EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
882 }
883
884 /* EsOutProgramSelect:
885  *  Select a program and update the object variable
886  */
887 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
888 {
889     es_out_sys_t      *p_sys = out->p_sys;
890     input_thread_t    *p_input = p_sys->p_input;
891     vlc_value_t       val;
892     int               i;
893
894     if( p_sys->p_pgrm == p_pgrm )
895         return; /* Nothing to do */
896
897     if( p_sys->p_pgrm )
898     {
899         es_out_pgrm_t *old = p_sys->p_pgrm;
900         msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
901
902         for( i = 0; i < p_sys->i_es; i++ )
903         {
904             if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
905                 p_sys->i_mode != ES_OUT_MODE_ALL )
906                 EsUnselect( out, p_sys->es[i], true );
907         }
908
909         p_sys->p_es_audio = NULL;
910         p_sys->p_es_sub = NULL;
911         p_sys->p_es_video = NULL;
912     }
913
914     msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
915
916     /* Mark it selected */
917     p_pgrm->b_selected = true;
918
919     /* Switch master stream */
920     p_sys->p_pgrm = p_pgrm;
921
922     /* Update "program" */
923     val.i_int = p_pgrm->i_id;
924     var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
925
926     /* Update "es-*" */
927     var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
928     var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
929     var_Change( p_input, "spu-es",   VLC_VAR_CLEARCHOICES, NULL, NULL );
930     var_SetInteger( p_input, "teletext-es", -1 );
931     for( i = 0; i < p_sys->i_es; i++ )
932     {
933         if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
934             EsOutESVarUpdate( out, p_sys->es[i], false );
935         EsOutSelect( out, p_sys->es[i], false );
936     }
937
938     /* Update now playing */
939     input_item_SetNowPlaying( p_input->p->input.p_item,
940                               p_pgrm->psz_now_playing );
941     input_item_SetPublisher( p_input->p->input.p_item,
942                              p_pgrm->psz_publisher );
943
944     var_SetBool( p_sys->p_input, "intf-change", true );
945 }
946
947 /* EsOutAddProgram:
948  *  Add a program
949  */
950 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
951 {
952     es_out_sys_t      *p_sys = out->p_sys;
953     input_thread_t    *p_input = p_sys->p_input;
954     vlc_value_t       val;
955
956     es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
957     if( !p_pgrm )
958         return NULL;
959
960     /* Init */
961     p_pgrm->i_id = i_group;
962     p_pgrm->i_es = 0;
963     p_pgrm->b_selected = false;
964     p_pgrm->psz_name = NULL;
965     p_pgrm->psz_now_playing = NULL;
966     p_pgrm->psz_publisher = NULL;
967     p_pgrm->p_epg = NULL;
968     p_pgrm->p_clock = input_clock_New( p_input->p->input.i_cr_average, p_sys->i_rate );
969     if( !p_pgrm->p_clock )
970     {
971         free( p_pgrm );
972         return NULL;
973     }
974
975     /* Append it */
976     TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
977
978     /* Update "program" variable */
979     val.i_int = i_group;
980     var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
981
982     if( i_group == var_GetInteger( p_input, "program" ) )
983     {
984         EsOutProgramSelect( out, p_pgrm );
985     }
986     else
987     {
988         var_SetBool( p_sys->p_input, "intf-change", true );
989     }
990     return p_pgrm;
991 }
992
993 /* EsOutDelProgram:
994  *  Delete a program
995  */
996 static int EsOutProgramDel( es_out_t *out, int i_group )
997 {
998     es_out_sys_t      *p_sys = out->p_sys;
999     input_thread_t    *p_input = p_sys->p_input;
1000     es_out_pgrm_t     *p_pgrm = NULL;
1001     vlc_value_t       val;
1002     int               i;
1003
1004     for( i = 0; i < p_sys->i_pgrm; i++ )
1005     {
1006         if( p_sys->pgrm[i]->i_id == i_group )
1007         {
1008             p_pgrm = p_sys->pgrm[i];
1009             break;
1010         }
1011     }
1012
1013     if( p_pgrm == NULL )
1014         return VLC_EGENERIC;
1015
1016     if( p_pgrm->i_es )
1017     {
1018         msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1019                  i_group, p_pgrm->i_es );
1020         return VLC_EGENERIC;
1021     }
1022
1023     TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1024
1025     /* If program is selected we need to unselect it */
1026     if( p_sys->p_pgrm == p_pgrm )
1027         p_sys->p_pgrm = NULL;
1028
1029     input_clock_Delete( p_pgrm->p_clock );
1030
1031     free( p_pgrm->psz_name );
1032     free( p_pgrm->psz_now_playing );
1033     free( p_pgrm->psz_publisher );
1034     if( p_pgrm->p_epg )
1035         vlc_epg_Delete( p_pgrm->p_epg );
1036     free( p_pgrm );
1037
1038     /* Update "program" variable */
1039     val.i_int = i_group;
1040     var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
1041
1042     var_SetBool( p_sys->p_input, "intf-change", true );
1043
1044     return VLC_SUCCESS;
1045 }
1046
1047 /* EsOutProgramMeta:
1048  */
1049 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1050 {
1051     char *psz = NULL;
1052     if( p_pgrm->psz_name )
1053     {
1054         if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1055             return NULL;
1056     }
1057     else
1058     {
1059         if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1060             return NULL;
1061     }
1062     return psz;
1063 }
1064
1065 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
1066 {
1067     es_out_sys_t      *p_sys = out->p_sys;
1068     es_out_pgrm_t     *p_pgrm = NULL;
1069     input_thread_t    *p_input = p_sys->p_input;
1070     char              *psz_cat;
1071     const char        *psz_title = NULL;
1072     const char        *psz_provider = NULL;
1073     int i;
1074
1075     msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1076
1077     /* Check against empty meta data (empty for what we handle) */
1078     if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1079         !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1080         !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1081         vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1082     {
1083         return;
1084     }
1085     /* Find program */
1086     for( i = 0; i < p_sys->i_pgrm; i++ )
1087     {
1088         if( p_sys->pgrm[i]->i_id == i_group )
1089         {
1090             p_pgrm = p_sys->pgrm[i];
1091             break;
1092         }
1093     }
1094     if( p_pgrm == NULL )
1095         p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
1096
1097     /* */
1098     psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1099     psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1100
1101     /* Update the description text of the program */
1102     if( psz_title && *psz_title )
1103     {
1104         vlc_value_t val;
1105         vlc_value_t text;
1106
1107         if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1108         {
1109             char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1110
1111             /* Remove old entries */
1112             input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1113             /* TODO update epg name */
1114             free( psz_cat );
1115         }
1116         free( p_pgrm->psz_name );
1117         p_pgrm->psz_name = strdup( psz_title );
1118
1119         /* ugly but it works */
1120         val.i_int = i_group;
1121         var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
1122
1123         if( psz_provider && *psz_provider )
1124         {
1125             if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
1126             {
1127                 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
1128                 free( text.psz_string );
1129             }
1130         }
1131         else
1132         {
1133             text.psz_string = (char *)psz_title;
1134             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
1135         }
1136     }
1137
1138     psz_cat = EsOutProgramGetMetaName( p_pgrm );
1139     if( psz_provider )
1140     {
1141         if( p_sys->p_pgrm == p_pgrm )
1142             input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
1143         input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1144     }
1145     char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1146     for( i = 0; ppsz_all_keys[i]; i++ )
1147     {
1148         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
1149                        vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
1150         free( ppsz_all_keys[i] );
1151     }
1152     free( ppsz_all_keys );
1153
1154     free( psz_cat );
1155 }
1156
1157 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
1158 {
1159     int i;
1160
1161     /* Add new event */
1162     for( i = 0; i < p_src->i_event; i++ )
1163     {
1164         vlc_epg_event_t *p_evt = p_src->pp_event[i];
1165         bool b_add = true;
1166         int j;
1167
1168         for( j = 0; j < p_dst->i_event; j++ )
1169         {
1170             if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
1171             {
1172                 b_add = false;
1173                 break;
1174             }
1175             if( p_dst->pp_event[j]->i_start > p_evt->i_start )
1176                 break;
1177         }
1178         if( b_add )
1179         {
1180             vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
1181             if( !p_copy )
1182                 break;
1183             memset( p_copy, 0, sizeof(vlc_epg_event_t) );
1184             p_copy->i_start = p_evt->i_start;
1185             p_copy->i_duration = p_evt->i_duration;
1186             p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
1187             p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
1188             p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
1189             TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
1190         }
1191     }
1192     /* Update current */
1193     vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
1194
1195     /* Keep only 1 old event  */
1196     if( p_dst->p_current )
1197     {
1198         while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
1199             TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
1200     }
1201 }
1202
1203 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
1204 {
1205     es_out_sys_t      *p_sys = out->p_sys;
1206     input_thread_t    *p_input = p_sys->p_input;
1207     es_out_pgrm_t     *p_pgrm = NULL;
1208     char *psz_cat;
1209     int i;
1210
1211     /* Find program */
1212     for( i = 0; i < p_sys->i_pgrm; i++ )
1213     {
1214         if( p_sys->pgrm[i]->i_id == i_group )
1215         {
1216             p_pgrm = p_sys->pgrm[i];
1217             break;
1218         }
1219     }
1220     if( p_pgrm == NULL )
1221         p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
1222
1223     /* Merge EPG */
1224     if( !p_pgrm->p_epg )
1225         p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1226     vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1227
1228     /* Update info */
1229     psz_cat = EsOutProgramGetMetaName( p_pgrm );
1230 #ifdef HAVE_LOCALTIME_R
1231     char *psz_epg;
1232     if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
1233         psz_epg = NULL;
1234     input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
1235     msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1236     for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
1237     {
1238         const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
1239         time_t t_start = (time_t)p_evt->i_start;
1240         struct tm tm_start;
1241         char psz_start[128];
1242
1243         localtime_r( &t_start, &tm_start );
1244
1245         snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
1246         if( p_evt->psz_short_description || p_evt->psz_description )
1247             input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
1248                            p_evt->psz_name,
1249                            p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
1250                            p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
1251         else
1252             input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
1253                            p_evt->psz_name,
1254                            p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
1255     }
1256     free( psz_epg );
1257 #endif
1258     /* Update now playing */
1259     free( p_pgrm->psz_now_playing );
1260     p_pgrm->psz_now_playing = NULL;
1261     if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
1262         p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
1263
1264     if( p_pgrm == p_sys->p_pgrm )
1265         input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
1266
1267     if( p_pgrm->psz_now_playing )
1268     {
1269         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1270             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1271             p_pgrm->psz_now_playing );
1272     }
1273     else
1274     {
1275         input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1276             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1277     }
1278
1279     free( psz_cat );
1280 }
1281
1282 /* EsOutAdd:
1283  *  Add an es_out
1284  */
1285 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
1286 {
1287     es_out_sys_t      *p_sys = out->p_sys;
1288     input_thread_t    *p_input = p_sys->p_input;
1289
1290     if( fmt->i_group < 0 )
1291     {
1292         msg_Err( p_input, "invalid group number" );
1293         return NULL;
1294     }
1295
1296     es_out_id_t       *es = malloc( sizeof( *es ) );
1297     es_out_pgrm_t     *p_pgrm = NULL;
1298     int i;
1299
1300     if( !es )
1301         return NULL;
1302
1303     vlc_mutex_lock( &p_sys->lock );
1304
1305     /* Search the program */
1306     for( i = 0; i < p_sys->i_pgrm; i++ )
1307     {
1308         if( fmt->i_group == p_sys->pgrm[i]->i_id )
1309         {
1310             p_pgrm = p_sys->pgrm[i];
1311             break;
1312         }
1313     }
1314     if( p_pgrm == NULL )
1315     {
1316         /* Create a new one */
1317         p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1318     }
1319
1320     /* Increase ref count for program */
1321     p_pgrm->i_es++;
1322
1323     /* Set up ES */
1324     if( fmt->i_id < 0 )
1325         fmt->i_id = out->p_sys->i_id;
1326     es->i_id = fmt->i_id;
1327     es->p_pgrm = p_pgrm;
1328     es_format_Copy( &es->fmt, fmt );
1329
1330     switch( fmt->i_cat )
1331     {
1332     case AUDIO_ES:
1333     {
1334         audio_replay_gain_t rg;
1335
1336         es->i_channel = p_sys->i_audio;
1337
1338         memset( &rg, 0, sizeof(rg) );
1339         vlc_mutex_lock( &p_input->p->input.p_item->lock );
1340         vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
1341         vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1342
1343         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1344         {
1345             if( !es->fmt.audio_replay_gain.pb_peak[i] )
1346             {
1347                 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1348                 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1349             }
1350             if( !es->fmt.audio_replay_gain.pb_gain[i] )
1351             {
1352                 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1353                 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1354             }
1355         }
1356         break;
1357     }
1358
1359     case VIDEO_ES:
1360         es->i_channel = p_sys->i_video;
1361         if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
1362             vlc_ureduce( &es->fmt.video.i_frame_rate,
1363                          &es->fmt.video.i_frame_rate_base,
1364                          fmt->video.i_frame_rate,
1365                          fmt->video.i_frame_rate_base, 0 );
1366         break;
1367
1368     case SPU_ES:
1369         es->i_channel = p_sys->i_sub;
1370         break;
1371
1372     default:
1373         es->i_channel = 0;
1374         break;
1375     }
1376     es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
1377     es->psz_language_code = LanguageGetCode( fmt->psz_language );
1378     es->p_dec = NULL;
1379     es->p_dec_record = NULL;
1380     for( i = 0; i < 4; i++ )
1381         es->pb_cc_present[i] = false;
1382     es->p_master = NULL;
1383
1384     if( es->p_pgrm == p_sys->p_pgrm )
1385         EsOutESVarUpdate( out, es, false );
1386
1387     /* Select it if needed */
1388     EsOutSelect( out, es, false );
1389
1390
1391     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1392     p_sys->i_id++;  /* always incremented */
1393     switch( fmt->i_cat )
1394     {
1395         case AUDIO_ES:
1396             p_sys->i_audio++;
1397             break;
1398         case SPU_ES:
1399             p_sys->i_sub++;
1400             break;
1401         case VIDEO_ES:
1402             p_sys->i_video++;
1403             break;
1404     }
1405
1406     EsOutAddInfo( out, es );
1407
1408     vlc_mutex_unlock( &p_sys->lock );
1409
1410     return es;
1411 }
1412
1413 static bool EsIsSelected( es_out_id_t *es )
1414 {
1415     if( es->p_master )
1416     {
1417         bool b_decode = false;
1418         if( es->p_master->p_dec )
1419         {
1420             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1421             if( i_channel != -1 )
1422                 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1423         }
1424         return b_decode;
1425     }
1426     else
1427     {
1428         return es->p_dec != NULL;
1429     }
1430 }
1431 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1432 {
1433     es_out_sys_t   *p_sys = out->p_sys;
1434     input_thread_t *p_input = p_sys->p_input;
1435
1436     p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1437     if( p_es->p_dec )
1438     {
1439         if( p_sys->b_buffering )
1440             input_DecoderStartBuffering( p_es->p_dec );
1441
1442         if( !p_es->p_master && p_sys->p_sout_record )
1443         {
1444             p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1445             if( p_es->p_dec_record && p_sys->b_buffering )
1446                 input_DecoderStartBuffering( p_es->p_dec_record );
1447         }
1448     }
1449
1450     EsOutDecoderChangeDelay( out, p_es );
1451 }
1452 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1453 {
1454     VLC_UNUSED(out);
1455
1456     if( !p_es->p_dec )
1457         return;
1458
1459     input_DecoderDelete( p_es->p_dec );
1460     p_es->p_dec = NULL;
1461
1462     if( p_es->p_dec_record )
1463     {
1464         input_DecoderDelete( p_es->p_dec_record );
1465         p_es->p_dec_record = NULL;
1466     }
1467 }
1468
1469 static void EsSelect( es_out_t *out, es_out_id_t *es )
1470 {
1471     es_out_sys_t   *p_sys = out->p_sys;
1472     input_thread_t *p_input = p_sys->p_input;
1473     vlc_value_t    val;
1474     const char     *psz_var;
1475
1476     if( EsIsSelected( es ) )
1477     {
1478         msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1479         return;
1480     }
1481
1482     if( es->p_master )
1483     {
1484         int i_channel;
1485         if( !es->p_master->p_dec )
1486             return;
1487
1488         i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1489         if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1490             return;
1491     }
1492     else
1493     {
1494         if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1495         {
1496             if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1497             {
1498                 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1499                          es->i_id );
1500                 return;
1501             }
1502         }
1503         else if( es->fmt.i_cat == AUDIO_ES )
1504         {
1505             var_Get( p_input, "audio", &val );
1506             if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1507             {
1508                 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1509                          es->i_id );
1510                 return;
1511             }
1512         }
1513         if( es->fmt.i_cat == SPU_ES )
1514         {
1515             var_Get( p_input, "spu", &val );
1516             if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1517             {
1518                 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1519                          es->i_id );
1520                 return;
1521             }
1522         }
1523
1524         EsCreateDecoder( out, es );
1525
1526         if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1527             return;
1528     }
1529
1530     if( es->fmt.i_cat == VIDEO_ES )
1531         psz_var = "video-es";
1532     else if( es->fmt.i_cat == AUDIO_ES )
1533         psz_var = "audio-es";
1534     else if( es->fmt.i_cat == SPU_ES )
1535         psz_var = "spu-es";
1536     else
1537         return;
1538
1539     /* Mark it as selected */
1540     val.i_int = es->i_id;
1541     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1542
1543     var_SetBool( p_sys->p_input, "intf-change", true );
1544 }
1545
1546 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1547 {
1548     es_out_sys_t   *p_sys = out->p_sys;
1549     input_thread_t *p_input = p_sys->p_input;
1550     vlc_value_t    val;
1551     const char     *psz_var;
1552
1553     if( !EsIsSelected( es ) )
1554     {
1555         msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1556         return;
1557     }
1558
1559     if( es->p_master )
1560     {
1561         if( es->p_master->p_dec )
1562         {
1563             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1564             if( i_channel != -1 )
1565                 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1566         }
1567     }
1568     else
1569     {
1570         const int i_spu_id = var_GetInteger( p_input, "spu-es");
1571         int i;
1572         for( i = 0; i < 4; i++ )
1573         {
1574             if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1575                 continue;
1576
1577             if( i_spu_id == es->pp_cc_es[i]->i_id )
1578             {
1579                 /* Force unselection of the CC */
1580                 val.i_int = -1;
1581                 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1582                 if( !b_update )
1583                     var_SetBool( p_sys->p_input, "intf-change", true );
1584             }
1585             EsOutDel( out, es->pp_cc_es[i] );
1586
1587             es->pb_cc_present[i] = false;
1588         }
1589         EsDestroyDecoder( out, es );
1590     }
1591
1592     if( !b_update )
1593         return;
1594
1595     /* Update var */
1596     if( es->p_dec == NULL )
1597         return;
1598     if( es->fmt.i_cat == VIDEO_ES )
1599         psz_var = "video-es";
1600     else if( es->fmt.i_cat == AUDIO_ES )
1601         psz_var = "audio-es";
1602     else if( es->fmt.i_cat == SPU_ES )
1603         psz_var = "spu-es";
1604     else
1605         return;
1606
1607     /* Mark it as unselected */
1608     val.i_int = -1;
1609     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1610
1611     var_SetBool( p_sys->p_input, "intf-change", true );
1612 }
1613
1614 /**
1615  * Select an ES given the current mode
1616  * XXX: you need to take a the lock before (stream.stream_lock)
1617  *
1618  * \param out The es_out structure
1619  * \param es es_out_id structure
1620  * \param b_force ...
1621  * \return nothing
1622  */
1623 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1624 {
1625     es_out_sys_t      *p_sys = out->p_sys;
1626
1627     int i_cat = es->fmt.i_cat;
1628
1629     if( !p_sys->b_active ||
1630         ( !b_force && es->fmt.i_priority < 0 ) )
1631     {
1632         return;
1633     }
1634
1635     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1636     {
1637         if( !EsIsSelected( es ) )
1638             EsSelect( out, es );
1639     }
1640     else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1641     {
1642         vlc_value_t val;
1643         int i;
1644         var_Get( p_sys->p_input, "programs", &val );
1645         for ( i = 0; i < val.p_list->i_count; i++ )
1646         {
1647             if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1648             {
1649                 if( !EsIsSelected( es ) )
1650                     EsSelect( out, es );
1651                 break;
1652             }
1653         }
1654         var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1655     }
1656     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1657     {
1658         int i_wanted  = -1;
1659
1660         if( es->p_pgrm != p_sys->p_pgrm )
1661             return;
1662
1663         if( i_cat == AUDIO_ES )
1664         {
1665             int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1666                                      es->psz_language_code );
1667
1668             if( p_sys->p_es_audio &&
1669                 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1670             {
1671                 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1672                                          p_sys->p_es_audio->psz_language_code );
1673
1674                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1675                     return;
1676                 i_wanted = es->i_channel;
1677             }
1678             else
1679             {
1680                 /* Select audio if (no audio selected yet)
1681                  * - no audio-language
1682                  * - no audio code for the ES
1683                  * - audio code in the requested list */
1684                 if( idx1 >= 0 ||
1685                     !strcmp( es->psz_language_code, "??" ) ||
1686                     !p_sys->ppsz_audio_language )
1687                     i_wanted = es->i_channel;
1688             }
1689
1690             if( p_sys->i_audio_last >= 0 )
1691                 i_wanted = p_sys->i_audio_last;
1692
1693             if( p_sys->i_audio_id >= 0 )
1694             {
1695                 if( es->i_id == p_sys->i_audio_id )
1696                     i_wanted = es->i_channel;
1697                 else
1698                     return;
1699             }
1700         }
1701         else if( i_cat == SPU_ES )
1702         {
1703             int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1704                                      es->psz_language_code );
1705
1706             if( p_sys->p_es_sub &&
1707                 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1708             {
1709                 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1710                                          p_sys->p_es_sub->psz_language_code );
1711
1712                 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1713                         idx1, es->psz_language_code, idx2,
1714                         p_sys->p_es_sub->psz_language_code );
1715
1716                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1717                     return;
1718                 /* We found a SPU that matches our language request */
1719                 i_wanted  = es->i_channel;
1720             }
1721             else if( idx1 >= 0 )
1722             {
1723                 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1724                         idx1, es->psz_language_code );
1725
1726                 i_wanted  = es->i_channel;
1727             }
1728             else if( p_sys->i_default_sub_id >= 0 )
1729             {
1730                 if( es->i_id == p_sys->i_default_sub_id )
1731                     i_wanted = es->i_channel;
1732             }
1733
1734             if( p_sys->i_sub_last >= 0 )
1735                 i_wanted  = p_sys->i_sub_last;
1736
1737             if( p_sys->i_sub_id >= 0 )
1738             {
1739                 if( es->i_id == p_sys->i_sub_id )
1740                     i_wanted = es->i_channel;
1741                 else
1742                     return;
1743             }
1744         }
1745         else if( i_cat == VIDEO_ES )
1746         {
1747             i_wanted  = es->i_channel;
1748         }
1749
1750         if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1751             EsSelect( out, es );
1752     }
1753
1754     /* FIXME TODO handle priority here */
1755     if( EsIsSelected( es ) )
1756     {
1757         if( i_cat == AUDIO_ES )
1758         {
1759             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1760                 p_sys->p_es_audio &&
1761                 p_sys->p_es_audio != es &&
1762                 EsIsSelected( p_sys->p_es_audio ) )
1763             {
1764                 EsUnselect( out, p_sys->p_es_audio, false );
1765             }
1766             p_sys->p_es_audio = es;
1767         }
1768         else if( i_cat == SPU_ES )
1769         {
1770             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1771                 p_sys->p_es_sub &&
1772                 p_sys->p_es_sub != es &&
1773                 EsIsSelected( p_sys->p_es_sub ) )
1774             {
1775                 EsUnselect( out, p_sys->p_es_sub, false );
1776             }
1777             p_sys->p_es_sub = es;
1778         }
1779         else if( i_cat == VIDEO_ES )
1780         {
1781             p_sys->p_es_video = es;
1782         }
1783     }
1784 }
1785
1786 /**
1787  * Send a block for the given es_out
1788  *
1789  * \param out the es_out to send from
1790  * \param es the es_out_id
1791  * \param p_block the data block to send
1792  */
1793 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1794 {
1795     es_out_sys_t   *p_sys = out->p_sys;
1796     input_thread_t *p_input = p_sys->p_input;
1797     int i_total = 0;
1798
1799     if( libvlc_stats( p_input ) )
1800     {
1801         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1802         stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1803                              p_block->i_buffer, &i_total );
1804         stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1805                            (float)i_total, NULL );
1806         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1807     }
1808
1809     vlc_mutex_lock( &p_sys->lock );
1810
1811     /* Mark preroll blocks */
1812     if( p_sys->i_preroll_end >= 0 )
1813     {
1814         int64_t i_date = p_block->i_pts;
1815         if( i_date <= 0 )
1816             i_date = p_block->i_dts;
1817
1818         if( i_date < p_sys->i_preroll_end )
1819             p_block->i_flags |= BLOCK_FLAG_PREROLL;
1820     }
1821
1822     p_block->i_rate = 0;
1823
1824     if( !es->p_dec )
1825     {
1826         block_Release( p_block );
1827         vlc_mutex_unlock( &p_sys->lock );
1828         return VLC_SUCCESS;
1829     }
1830
1831     /* Decode */
1832     if( es->p_dec_record )
1833     {
1834         block_t *p_dup = block_Duplicate( p_block );
1835         if( p_dup )
1836             input_DecoderDecode( es->p_dec_record, p_dup );
1837     }
1838     input_DecoderDecode( es->p_dec, p_block );
1839
1840     /* Check CC status */
1841     bool pb_cc[4];
1842     bool b_cc_new = false;
1843
1844     input_DecoderIsCcPresent( es->p_dec, pb_cc );
1845     for( int i = 0; i < 4; i++ )
1846     {
1847         static const vlc_fourcc_t fcc[4] = {
1848             VLC_FOURCC('c', 'c', '1', ' '),
1849             VLC_FOURCC('c', 'c', '2', ' '),
1850             VLC_FOURCC('c', 'c', '3', ' '),
1851             VLC_FOURCC('c', 'c', '4', ' '),
1852         };
1853         es_format_t fmt;
1854
1855         if(  es->pb_cc_present[i] || !pb_cc[i] )
1856             continue;
1857         msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1858
1859         es_format_Init( &fmt, SPU_ES, fcc[i] );
1860         fmt.i_group = es->fmt.i_group;
1861         if( asprintf( &fmt.psz_description,
1862                       _("Closed captions %u"), 1 + i ) == -1 )
1863             fmt.psz_description = NULL;
1864         es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1865         es->pp_cc_es[i]->p_master = es;
1866         es_format_Clean( &fmt );
1867
1868         /* */
1869         es->pb_cc_present[i] = true;
1870         b_cc_new = true;
1871     }
1872     if( b_cc_new )
1873         var_SetBool( p_sys->p_input, "intf-change", true );
1874
1875     vlc_mutex_unlock( &p_sys->lock );
1876
1877     return VLC_SUCCESS;
1878 }
1879
1880 /*****************************************************************************
1881  * EsOutDel:
1882  *****************************************************************************/
1883 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1884 {
1885     es_out_sys_t *p_sys = out->p_sys;
1886     bool b_reselect = false;
1887     int i;
1888
1889     vlc_mutex_lock( &p_sys->lock );
1890
1891     /* We don't try to reselect */
1892     if( es->p_dec )
1893     {
1894         while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
1895         {
1896             if( input_DecoderIsEmpty( es->p_dec ) &&
1897                 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
1898                 break;
1899             msleep( 20*1000 );
1900         }
1901         EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1902     }
1903
1904     if( es->p_pgrm == p_sys->p_pgrm )
1905         EsOutESVarUpdate( out, es, true );
1906
1907     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1908
1909     es->p_pgrm->i_es--;
1910     if( es->p_pgrm->i_es == 0 )
1911     {
1912         msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1913     }
1914
1915     if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1916         p_sys->p_es_sub == es ) b_reselect = true;
1917
1918     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1919     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1920     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
1921
1922     switch( es->fmt.i_cat )
1923     {
1924         case AUDIO_ES:
1925             p_sys->i_audio--;
1926             break;
1927         case SPU_ES:
1928             p_sys->i_sub--;
1929             break;
1930         case VIDEO_ES:
1931             p_sys->i_video--;
1932             break;
1933     }
1934
1935     /* Re-select another track when needed */
1936     if( b_reselect )
1937     {
1938         for( i = 0; i < p_sys->i_es; i++ )
1939         {
1940             if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1941                 EsOutSelect( out, p_sys->es[i], false );
1942         }
1943     }
1944
1945     free( es->psz_language );
1946     free( es->psz_language_code );
1947
1948     es_format_Clean( &es->fmt );
1949
1950     vlc_mutex_unlock( &p_sys->lock );
1951
1952     free( es );
1953 }
1954
1955 /**
1956  * Control query handler
1957  *
1958  * \param out the es_out to control
1959  * \param i_query A es_out query as defined in include/ninput.h
1960  * \param args a variable list of arguments for the query
1961  * \return VLC_SUCCESS or an error code
1962  */
1963 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
1964 {
1965     es_out_sys_t *p_sys = out->p_sys;
1966     bool  b, *pb;
1967     int         i, *pi;
1968
1969     es_out_id_t *es;
1970
1971     switch( i_query )
1972     {
1973         case ES_OUT_SET_ES_STATE:
1974             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1975             b = (bool) va_arg( args, int );
1976             if( b && !EsIsSelected( es ) )
1977             {
1978                 EsSelect( out, es );
1979                 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1980             }
1981             else if( !b && EsIsSelected( es ) )
1982             {
1983                 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1984                 return VLC_SUCCESS;
1985             }
1986             return VLC_SUCCESS;
1987
1988         case ES_OUT_GET_ES_STATE:
1989             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1990             pb = (bool*) va_arg( args, bool * );
1991
1992             *pb = EsIsSelected( es );
1993             return VLC_SUCCESS;
1994
1995         case ES_OUT_SET_ACTIVE:
1996         {
1997             b = (bool) va_arg( args, int );
1998             p_sys->b_active = b;
1999             /* Needed ? */
2000             if( b )
2001                 var_SetBool( p_sys->p_input, "intf-change", true );
2002             return VLC_SUCCESS;
2003         }
2004
2005         case ES_OUT_GET_ACTIVE:
2006             pb = (bool*) va_arg( args, bool * );
2007             *pb = p_sys->b_active;
2008             return VLC_SUCCESS;
2009
2010         case ES_OUT_SET_MODE:
2011             i = (int) va_arg( args, int );
2012             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
2013                 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
2014             {
2015                 p_sys->i_mode = i;
2016
2017                 /* Reapply policy mode */
2018                 for( i = 0; i < p_sys->i_es; i++ )
2019                 {
2020                     if( EsIsSelected( p_sys->es[i] ) )
2021                     {
2022                         EsUnselect( out, p_sys->es[i],
2023                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2024                     }
2025                 }
2026                 for( i = 0; i < p_sys->i_es; i++ )
2027                 {
2028                     EsOutSelect( out, p_sys->es[i], false );
2029                 }
2030                 return VLC_SUCCESS;
2031             }
2032             return VLC_EGENERIC;
2033
2034         case ES_OUT_GET_MODE:
2035             pi = (int*) va_arg( args, int* );
2036             *pi = p_sys->i_mode;
2037             return VLC_SUCCESS;
2038
2039         case ES_OUT_SET_ES:
2040         case ES_OUT_RESTART_ES:
2041         {
2042             int i_cat;
2043
2044             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2045
2046             if( es == NULL )
2047                 i_cat = UNKNOWN_ES;
2048             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2049                 i_cat = AUDIO_ES;
2050             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2051                 i_cat = VIDEO_ES;
2052             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2053                 i_cat = SPU_ES;
2054             else
2055                 i_cat = -1;
2056
2057             for( i = 0; i < p_sys->i_es; i++ )
2058             {
2059                 if( i_cat == -1 )
2060                 {
2061                     if( es == p_sys->es[i] )
2062                     {
2063                         EsOutSelect( out, es, true );
2064                         break;
2065                     }
2066                 }
2067                 else
2068                 {
2069                     if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2070                     {
2071                         if( EsIsSelected( p_sys->es[i] ) )
2072                         {
2073                             if( i_query == ES_OUT_RESTART_ES )
2074                             {
2075                                 if( p_sys->es[i]->p_dec )
2076                                 {
2077                                     EsDestroyDecoder( out, p_sys->es[i] );
2078                                     EsCreateDecoder( out, p_sys->es[i] );
2079                                 }
2080                             }
2081                             else
2082                             {
2083                                 EsUnselect( out, p_sys->es[i],
2084                                             p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2085                             }
2086                         }
2087                     }
2088                 }
2089             }
2090             if( i_query == ES_OUT_SET_ES )
2091             {
2092                 vlc_event_t event;
2093                 event.type = vlc_InputSelectedStreamChanged;
2094                 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
2095             }
2096             return VLC_SUCCESS;
2097         }
2098  
2099         case ES_OUT_SET_ES_DEFAULT:
2100         {
2101             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2102
2103             if( es == NULL )
2104             {
2105                 /*p_sys->i_default_video_id = -1;*/
2106                 /*p_sys->i_default_audio_id = -1;*/
2107                 p_sys->i_default_sub_id = -1;
2108             }
2109             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2110             {
2111                 /*p_sys->i_default_video_id = -1;*/
2112             }
2113             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2114             {
2115                 /*p_sys->i_default_audio_id = -1;*/
2116             }
2117             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2118             {
2119                 p_sys->i_default_sub_id = -1;
2120             }
2121             else
2122             {
2123                 /*if( es->fmt.i_cat == VIDEO_ES )
2124                     p_sys->i_default_video_id = es->i_id;
2125                 else
2126                 if( es->fmt.i_cat == AUDIO_ES )
2127                     p_sys->i_default_audio_id = es->i_id;
2128                 else*/
2129                 if( es->fmt.i_cat == SPU_ES )
2130                     p_sys->i_default_sub_id = es->i_id;
2131             }
2132             return VLC_SUCCESS;
2133         }
2134
2135         case ES_OUT_SET_PCR:
2136         case ES_OUT_SET_GROUP_PCR:
2137         {
2138             es_out_pgrm_t *p_pgrm = NULL;
2139             int            i_group = 0;
2140             int64_t        i_pcr;
2141
2142             if( i_query == ES_OUT_SET_PCR )
2143             {
2144                 p_pgrm = p_sys->p_pgrm;
2145             }
2146             else
2147             {
2148                 int i;
2149                 i_group = (int)va_arg( args, int );
2150                 for( i = 0; i < p_sys->i_pgrm; i++ )
2151                 {
2152                     if( p_sys->pgrm[i]->i_id == i_group )
2153                     {
2154                         p_pgrm = p_sys->pgrm[i];
2155                         break;
2156                     }
2157                 }
2158             }
2159             if( p_pgrm == NULL )
2160                 p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
2161
2162             i_pcr = (int64_t)va_arg( args, int64_t );
2163             /* search program
2164              * TODO do not use mdate() but proper stream acquisition date */
2165             input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2166                                 p_sys->p_input->b_can_pace_control, i_pcr, mdate() );
2167             /* Check buffering state on master clock update */
2168             if( p_sys->b_buffering && p_pgrm == p_sys->p_pgrm )
2169                 EsOutDecodersStopBuffering( out, false );
2170
2171             return VLC_SUCCESS;
2172         }
2173
2174         case ES_OUT_RESET_PCR:
2175             msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2176             input_EsOutChangePosition( out );
2177             return VLC_SUCCESS;
2178
2179         case ES_OUT_GET_TS:
2180             if( p_sys->p_pgrm )
2181             {
2182                 int64_t i_ts = (int64_t)va_arg( args, int64_t );
2183                 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
2184                 *pi_ts = input_clock_GetTS( p_sys->p_pgrm->p_clock, NULL,
2185                                             p_sys->p_input->i_pts_delay, i_ts );
2186                 return VLC_SUCCESS;
2187             }
2188             return VLC_EGENERIC;
2189
2190         case ES_OUT_GET_GROUP:
2191             pi = (int*) va_arg( args, int* );
2192             if( p_sys->p_pgrm )
2193                 *pi = p_sys->p_pgrm->i_id;
2194             else
2195                 *pi = -1;    /* FIXME */
2196             return VLC_SUCCESS;
2197
2198         case ES_OUT_SET_GROUP:
2199         {
2200             int j;
2201             i = (int) va_arg( args, int );
2202             for( j = 0; j < p_sys->i_pgrm; j++ )
2203             {
2204                 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2205                 if( p_pgrm->i_id == i )
2206                 {
2207                     EsOutProgramSelect( out, p_pgrm );
2208                     return VLC_SUCCESS;
2209                 }
2210             }
2211             return VLC_EGENERIC;
2212         }
2213
2214         case ES_OUT_SET_FMT:
2215         {
2216             /* This ain't pretty but is need by some demuxers (eg. Ogg )
2217              * to update the p_extra data */
2218             es_format_t *p_fmt;
2219             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2220             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2221             if( es == NULL )
2222                 return VLC_EGENERIC;
2223
2224             if( p_fmt->i_extra )
2225             {
2226                 es->fmt.i_extra = p_fmt->i_extra;
2227                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2228                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2229
2230                 if( !es->p_dec )
2231                     return VLC_SUCCESS;
2232 #if 1
2233                 EsDestroyDecoder( out, es );
2234
2235                 EsCreateDecoder( out, es );
2236 #else
2237                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2238                 es->p_dec->fmt_in.p_extra =
2239                     realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2240                 memcpy( es->p_dec->fmt_in.p_extra,
2241                         p_fmt->p_extra, p_fmt->i_extra );
2242 #endif
2243             }
2244
2245             return VLC_SUCCESS;
2246         }
2247
2248         case ES_OUT_SET_NEXT_DISPLAY_TIME:
2249         {
2250             const int64_t i_date = (int64_t)va_arg( args, int64_t );
2251
2252             if( i_date < 0 )
2253                 return VLC_EGENERIC;
2254
2255             p_sys->i_preroll_end = i_date;
2256
2257             return VLC_SUCCESS;
2258         }
2259         case ES_OUT_SET_GROUP_META:
2260         {
2261             int i_group = (int)va_arg( args, int );
2262             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2263
2264             EsOutProgramMeta( out, i_group, p_meta );
2265             return VLC_SUCCESS;
2266         }
2267         case ES_OUT_SET_GROUP_EPG:
2268         {
2269             int i_group = (int)va_arg( args, int );
2270             vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2271
2272             EsOutProgramEpg( out, i_group, p_epg );
2273             return VLC_SUCCESS;
2274         }
2275         case ES_OUT_DEL_GROUP:
2276         {
2277             int i_group = (int)va_arg( args, int );
2278
2279             return EsOutProgramDel( out, i_group );
2280         }
2281
2282         case ES_OUT_GET_WAKE_UP:
2283         {
2284             mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2285             *pi_wakeup = EsOutGetWakeup( out );
2286             return VLC_SUCCESS;
2287         }
2288
2289         case ES_OUT_SET_ES_BY_ID:
2290         case ES_OUT_RESTART_ES_BY_ID:
2291         case ES_OUT_SET_ES_DEFAULT_BY_ID:
2292         {
2293             const int i_id = (int)va_arg( args, int );
2294             es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2295             int i_new_query;
2296
2297             switch( i_query )
2298             {
2299             case ES_OUT_SET_ES_BY_ID:         i_new_query = ES_OUT_SET_ES; break;
2300             case ES_OUT_RESTART_ES_BY_ID:     i_new_query = ES_OUT_RESTART_ES; break;
2301             case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2302             default:
2303               assert(0);
2304             }
2305             /* TODO if the lock is made non recursive it should be changed */
2306             return es_out_Control( out, i_new_query, p_es );
2307         }
2308
2309         case ES_OUT_GET_BUFFERING:
2310             pb = (bool *)va_arg( args, bool* );
2311             *pb = p_sys->b_buffering;
2312             return VLC_SUCCESS;
2313
2314         case ES_OUT_GET_EMPTY:
2315             pb = (bool *)va_arg( args, bool* );
2316             *pb = EsOutDecodersIsEmpty( out );
2317             return VLC_SUCCESS;
2318
2319         default:
2320             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2321             return VLC_EGENERIC;
2322     }
2323 }
2324 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2325 {
2326     es_out_sys_t *p_sys = out->p_sys;
2327     int i_ret;
2328
2329     vlc_mutex_lock( &p_sys->lock );
2330     i_ret = EsOutControlLocked( out, i_query, args );
2331     vlc_mutex_unlock( &p_sys->lock );
2332
2333     return i_ret;
2334 }
2335
2336 /****************************************************************************
2337  * LanguageGetName: try to expend iso639 into plain name
2338  ****************************************************************************/
2339 static char *LanguageGetName( const char *psz_code )
2340 {
2341     const iso639_lang_t *pl;
2342
2343     if( psz_code == NULL )
2344     {
2345         return strdup( "" );
2346     }
2347
2348     if( strlen( psz_code ) == 2 )
2349     {
2350         pl = GetLang_1( psz_code );
2351     }
2352     else if( strlen( psz_code ) == 3 )
2353     {
2354         pl = GetLang_2B( psz_code );
2355         if( !strcmp( pl->psz_iso639_1, "??" ) )
2356         {
2357             pl = GetLang_2T( psz_code );
2358         }
2359     }
2360     else
2361     {
2362         return strdup( psz_code );
2363     }
2364
2365     if( !strcmp( pl->psz_iso639_1, "??" ) )
2366     {
2367        return strdup( psz_code );
2368     }
2369     else
2370     {
2371         if( *pl->psz_native_name )
2372         {
2373             return strdup( pl->psz_native_name );
2374         }
2375         return strdup( pl->psz_eng_name );
2376     }
2377 }
2378
2379 /* Get a 2 char code */
2380 static char *LanguageGetCode( const char *psz_lang )
2381 {
2382     const iso639_lang_t *pl;
2383
2384     if( psz_lang == NULL || *psz_lang == '\0' )
2385         return strdup("??");
2386
2387     for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2388     {
2389         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2390             !strcasecmp( pl->psz_native_name, psz_lang ) ||
2391             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2392             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2393             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2394             break;
2395     }
2396
2397     if( pl->psz_iso639_1 != NULL )
2398         return strdup( pl->psz_iso639_1 );
2399
2400     return strdup("??");
2401 }
2402
2403 static char **LanguageSplit( const char *psz_langs )
2404 {
2405     char *psz_dup;
2406     char *psz_parser;
2407     char **ppsz = NULL;
2408     int i_psz = 0;
2409
2410     if( psz_langs == NULL ) return NULL;
2411
2412     psz_parser = psz_dup = strdup(psz_langs);
2413
2414     while( psz_parser && *psz_parser )
2415     {
2416         char *psz;
2417         char *psz_code;
2418
2419         psz = strchr(psz_parser, ',' );
2420         if( psz ) *psz++ = '\0';
2421
2422         if( !strcmp( psz_parser, "any" ) )
2423         {
2424             TAB_APPEND( i_psz, ppsz, strdup("any") );
2425         }
2426         else
2427         {
2428             psz_code = LanguageGetCode( psz_parser );
2429             if( strcmp( psz_code, "??" ) )
2430             {
2431                 TAB_APPEND( i_psz, ppsz, psz_code );
2432             }
2433             else
2434             {
2435                 free( psz_code );
2436             }
2437         }
2438
2439         psz_parser = psz;
2440     }
2441
2442     if( i_psz )
2443     {
2444         TAB_APPEND( i_psz, ppsz, NULL );
2445     }
2446
2447     free( psz_dup );
2448     return ppsz;
2449 }
2450
2451 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2452 {
2453     int i;
2454
2455     if( !ppsz_langs || !psz_lang ) return -1;
2456
2457     for( i = 0; ppsz_langs[i]; i++ )
2458     {
2459         if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2460             !strcasecmp( ppsz_langs[i], "any" ) )
2461         {
2462             return i;
2463         }
2464     }
2465
2466     return -1;
2467 }
2468
2469 /****************************************************************************
2470  * EsOutAddInfo:
2471  * - add meta info to the playlist item
2472  ****************************************************************************/
2473 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2474 {
2475     es_out_sys_t   *p_sys = out->p_sys;
2476     input_thread_t *p_input = p_sys->p_input;
2477     es_format_t    *fmt = &es->fmt;
2478     char           *psz_cat;
2479     lldiv_t         div;
2480
2481     /* Add stream info */
2482     if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
2483         return;
2484
2485     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2486                    "%.4s", (char*)&fmt->i_codec );
2487
2488     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2489                    "%s", es->psz_language );
2490
2491     /* Add information */
2492     switch( fmt->i_cat )
2493     {
2494     case AUDIO_ES:
2495         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2496                        _("Type"), _("Audio") );
2497
2498         if( fmt->audio.i_channels > 0 )
2499             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2500                            "%u", fmt->audio.i_channels );
2501
2502         if( fmt->audio.i_rate > 0 )
2503         {
2504             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2505                            _("%u Hz"), fmt->audio.i_rate );
2506             var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2507         }
2508
2509         if( fmt->audio.i_bitspersample > 0 )
2510             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2511                            _("Bits per sample"), "%u",
2512                            fmt->audio.i_bitspersample );
2513
2514         if( fmt->i_bitrate > 0 )
2515         {
2516             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2517                            _("%u kb/s"), fmt->i_bitrate / 1000 );
2518             var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2519         }
2520         break;
2521
2522     case VIDEO_ES:
2523         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2524                        _("Type"), _("Video") );
2525
2526         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2527             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2528                            _("Resolution"), "%ux%u",
2529                            fmt->video.i_width, fmt->video.i_height );
2530
2531         if( fmt->video.i_visible_width > 0 &&
2532             fmt->video.i_visible_height > 0 )
2533             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2534                            _("Display resolution"), "%ux%u",
2535                            fmt->video.i_visible_width,
2536                            fmt->video.i_visible_height);
2537        if( fmt->video.i_frame_rate > 0 &&
2538            fmt->video.i_frame_rate_base > 0 )
2539        {
2540            div = lldiv( (float)fmt->video.i_frame_rate /
2541                                fmt->video.i_frame_rate_base * 1000000,
2542                                1000000 );
2543            input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2544                           _("Frame rate"), "%"PRId64".%06u",
2545                           div.quot, (unsigned int )div.rem );
2546        }
2547        break;
2548
2549     case SPU_ES:
2550         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2551                        _("Type"), _("Subtitle") );
2552         break;
2553
2554     default:
2555         break;
2556     }
2557
2558     free( psz_cat );
2559 }