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