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