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