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