]> git.sesse.net Git - vlc/blob - src/input/es_out.c
Input access locking, part 2.
[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 #include <vlc/vlc.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include <vlc_input.h>
34 #include <vlc_es_out.h>
35 #include <vlc_block.h>
36 #include <vlc_aout.h>
37
38 #include "input_internal.h"
39
40 #include "vlc_playlist.h"
41 #include "iso_lang.h"
42 /* FIXME we should find a better way than including that */
43 #include "../text/iso-639_def.h"
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 typedef struct
49 {
50     /* Program ID */
51     int i_id;
52
53     /* Number of es for this pgrm */
54     int i_es;
55
56     vlc_bool_t b_selected;
57
58     /* Clock for this program */
59     input_clock_t clock;
60
61     char    *psz_name;
62     char    *psz_now_playing;
63     char    *psz_publisher;
64
65     vlc_epg_t *p_epg;
66 } es_out_pgrm_t;
67
68 struct es_out_id_t
69 {
70     /* ES ID */
71     int       i_id;
72     es_out_pgrm_t *p_pgrm;
73
74     /* Misc. */
75     int64_t i_preroll_end;
76
77     /* Channel in the track type */
78     int         i_channel;
79     es_format_t fmt;
80     char        *psz_language;
81     char        *psz_language_code;
82     decoder_t   *p_dec;
83 };
84
85 struct es_out_sys_t
86 {
87     input_thread_t *p_input;
88
89     /* all programs */
90     int           i_pgrm;
91     es_out_pgrm_t **pgrm;
92     es_out_pgrm_t **pp_selected_pgrm; /* --programs */
93     es_out_pgrm_t *p_pgrm;  /* Master program */
94
95     /* all es */
96     int         i_id;
97     int         i_es;
98     es_out_id_t **es;
99
100     /* mode gestion */
101     vlc_bool_t  b_active;
102     int         i_mode;
103
104     /* es count */
105     int         i_audio;
106     int         i_video;
107     int         i_sub;
108
109     /* es to select */
110     int         i_audio_last, i_audio_id;
111     int         i_sub_last, i_sub_id;
112     char        **ppsz_audio_language;
113     char        **ppsz_sub_language;
114
115     /* current main es */
116     es_out_id_t *p_es_audio;
117     es_out_id_t *p_es_video;
118     es_out_id_t *p_es_sub;
119
120     /* delay */
121     int64_t i_audio_delay;
122     int64_t i_spu_delay;
123 };
124
125 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
126 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
127 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
128 static void         EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
129 static int          EsOutControl( es_out_t *, int i_query, va_list );
130
131 static void         EsOutAddInfo( es_out_t *, es_out_id_t *es );
132
133 static void EsSelect( es_out_t *out, es_out_id_t *es );
134 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
135 static char *LanguageGetName( const char *psz_code );
136 static char *LanguageGetCode( const char *psz_lang );
137 static char **LanguageSplit( const char *psz_langs );
138 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
139
140 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
141
142 /*****************************************************************************
143  * input_EsOutNew:
144  *****************************************************************************/
145 es_out_t *input_EsOutNew( input_thread_t *p_input )
146 {
147     es_out_t     *out = malloc( sizeof( es_out_t ) );
148     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
149     vlc_value_t  val;
150     int i;
151
152     out->pf_add     = EsOutAdd;
153     out->pf_send    = EsOutSend;
154     out->pf_del     = EsOutDel;
155     out->pf_control = EsOutControl;
156     out->p_sys      = p_sys;
157     out->b_sout     = (p_input->p->p_sout != NULL ? VLC_TRUE : VLC_FALSE);
158
159     p_sys->p_input = p_input;
160
161     p_sys->b_active = VLC_FALSE;
162     p_sys->i_mode   = ES_OUT_MODE_AUTO;
163
164
165     TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
166     p_sys->p_pgrm   = NULL;
167
168     p_sys->i_id    = 0;
169
170     TAB_INIT( p_sys->i_es, p_sys->es );
171
172     p_sys->i_audio = 0;
173     p_sys->i_video = 0;
174     p_sys->i_sub   = 0;
175
176     /* */
177     var_Get( p_input, "audio-track", &val );
178     p_sys->i_audio_last = val.i_int;
179
180     var_Get( p_input, "sub-track", &val );
181     p_sys->i_sub_last = val.i_int;
182
183     if( !p_input->b_preparsing )
184     {
185         var_Get( p_input, "audio-language", &val );
186         p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
187         if( p_sys->ppsz_audio_language )
188         {
189             for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
190                 msg_Dbg( p_input, "selected audio language[%d] %s",
191                          i, p_sys->ppsz_audio_language[i] );
192         }
193         if( val.psz_string ) free( val.psz_string );
194
195         var_Get( p_input, "sub-language", &val );
196         p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
197         if( p_sys->ppsz_sub_language )
198         {
199             for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
200                 msg_Dbg( p_input, "selected subtitle language[%d] %s",
201                          i, p_sys->ppsz_sub_language[i] );
202         }
203         if( val.psz_string ) free( val.psz_string );
204     }
205     else
206     {
207         p_sys->ppsz_sub_language = NULL;
208         p_sys->ppsz_audio_language = NULL;
209     }
210
211     var_Get( p_input, "audio-track-id", &val );
212     p_sys->i_audio_id = val.i_int;
213
214     var_Get( p_input, "sub-track-id", &val );
215     p_sys->i_sub_id = val.i_int;
216
217     p_sys->p_es_audio = NULL;
218     p_sys->p_es_video = NULL;
219     p_sys->p_es_sub   = NULL;
220
221     p_sys->i_audio_delay= 0;
222     p_sys->i_spu_delay  = 0;
223
224     return out;
225 }
226
227 /*****************************************************************************
228  * input_EsOutDelete:
229  *****************************************************************************/
230 void input_EsOutDelete( es_out_t *out )
231 {
232     es_out_sys_t *p_sys = out->p_sys;
233     int i;
234
235     for( i = 0; i < p_sys->i_es; i++ )
236     {
237         if( p_sys->es[i]->p_dec )
238         {
239             input_DecoderDelete( p_sys->es[i]->p_dec );
240         }
241         if( p_sys->es[i]->psz_language )
242             free( p_sys->es[i]->psz_language );
243         if( p_sys->es[i]->psz_language_code )
244             free( p_sys->es[i]->psz_language_code );
245         es_format_Clean( &p_sys->es[i]->fmt );
246
247         free( p_sys->es[i] );
248     }
249     if( p_sys->ppsz_audio_language )
250     {
251         for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
252             free( p_sys->ppsz_audio_language[i] );
253         free( p_sys->ppsz_audio_language );
254     }
255     if( p_sys->ppsz_sub_language )
256     {
257         for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
258             free( p_sys->ppsz_sub_language[i] );
259         free( p_sys->ppsz_sub_language );
260     }
261
262     if( p_sys->es )
263         free( p_sys->es );
264
265     /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
266     for( i = 0; i < p_sys->i_pgrm; i++ )
267     {
268         es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
269         if( p_pgrm->psz_now_playing )
270             free( p_pgrm->psz_now_playing );
271         if( p_pgrm->psz_publisher )
272             free( p_pgrm->psz_publisher );
273         if( p_pgrm->psz_name )
274             free( p_pgrm->psz_name );
275         if( p_pgrm->p_epg )
276             vlc_epg_Delete( p_pgrm->p_epg );
277
278         free( p_pgrm );
279     }
280     TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
281
282     free( p_sys );
283     free( out );
284 }
285
286 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
287 {
288     int i;
289     if( i_id < 0 )
290     {
291         /* Special HACK, -i_id is tha cat of the stream */
292         return (es_out_id_t*)((uint8_t*)NULL-i_id);
293     }
294
295     for( i = 0; i < out->p_sys->i_es; i++ )
296     {
297         if( out->p_sys->es[i]->i_id == i_id )
298             return out->p_sys->es[i];
299     }
300     return NULL;
301 }
302
303 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_flush, vlc_bool_t b_audio )
304 {
305     es_out_sys_t      *p_sys = out->p_sys;
306     int i;
307
308     for( i = 0; i < p_sys->i_es; i++ )
309     {
310         es_out_id_t *es = p_sys->es[i];
311
312         /* Send a dummy block to let decoder know that
313          * there is a discontinuity */
314         if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
315             input_DecoderDiscontinuity( es->p_dec, b_flush );
316     }
317 }
318 void input_EsOutSetRate( es_out_t *out )
319 {
320     es_out_sys_t      *p_sys = out->p_sys;
321     int i;
322
323     for( i = 0; i < p_sys->i_pgrm; i++ )
324         input_ClockSetRate( p_sys->p_input, &p_sys->pgrm[i]->clock );
325 }
326
327 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
328 {
329     es_out_sys_t *p_sys = out->p_sys;
330
331     if( i_cat == AUDIO_ES )
332         p_sys->i_audio_delay = i_delay;
333     else if( i_cat == SPU_ES )
334         p_sys->i_spu_delay = i_delay;
335 }
336
337 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
338 {
339     es_out_sys_t      *p_sys = out->p_sys;
340     int i;
341
342     for( i = 0; i < p_sys->i_es; i++ )
343     {
344         es_out_id_t *es = p_sys->es[i];
345
346         if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
347             return VLC_FALSE;
348     }
349     return VLC_TRUE;
350 }
351
352 /*****************************************************************************
353  *
354  *****************************************************************************/
355 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
356                               vlc_bool_t b_delete )
357 {
358     es_out_sys_t      *p_sys = out->p_sys;
359     input_thread_t    *p_input = p_sys->p_input;
360     vlc_value_t       val, text;
361
362     const char *psz_var;
363
364     if( es->fmt.i_cat == AUDIO_ES )
365         psz_var = "audio-es";
366     else if( es->fmt.i_cat == VIDEO_ES )
367         psz_var = "video-es";
368     else if( es->fmt.i_cat == SPU_ES )
369         psz_var = "spu-es";
370     else
371         return;
372
373     if( b_delete )
374     {
375         val.i_int = es->i_id;
376         var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
377         var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
378         return;
379     }
380
381     /* Get the number of ES already added */
382     var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
383     if( val.i_int == 0 )
384     {
385         vlc_value_t val2;
386
387         /* First one, we need to add the "Disable" choice */
388         val2.i_int = -1; text.psz_string = _("Disable");
389         var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
390         val.i_int++;
391     }
392
393     /* Take care of the ES description */
394     if( es->fmt.psz_description && *es->fmt.psz_description )
395     {
396         if( es->psz_language && *es->psz_language )
397         {
398             text.psz_string = malloc( strlen( es->fmt.psz_description) +
399                                       strlen( es->psz_language ) + 10 );
400             sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description,
401                                                    es->psz_language );
402         }
403         else text.psz_string = strdup( es->fmt.psz_description );
404     }
405     else
406     {
407         if( es->psz_language && *es->psz_language )
408         {
409             char *temp;
410             text.psz_string = malloc( strlen( _("Track %i") )+
411                                       strlen( es->psz_language ) + 30 );
412             asprintf( &temp,  _("Track %i"), val.i_int );
413             sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
414             free( temp );
415         }
416         else
417         {
418             text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
419             sprintf( text.psz_string, _("Track %i"), val.i_int );
420         }
421     }
422
423     val.i_int = es->i_id;
424     var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
425
426     free( text.psz_string );
427
428     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
429 }
430
431 /* EsOutProgramSelect:
432  *  Select a program and update the object variable
433  */
434 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
435 {
436     es_out_sys_t      *p_sys = out->p_sys;
437     input_thread_t    *p_input = p_sys->p_input;
438     vlc_value_t       val;
439     int               i;
440
441     if( p_sys->p_pgrm == p_pgrm )
442         return; /* Nothing to do */
443
444     if( p_sys->p_pgrm )
445     {
446         es_out_pgrm_t *old = p_sys->p_pgrm;
447         msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
448
449         for( i = 0; i < p_sys->i_es; i++ )
450         {
451             if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
452                 p_sys->i_mode != ES_OUT_MODE_ALL )
453                 EsUnselect( out, p_sys->es[i], VLC_TRUE );
454         }
455
456         p_sys->p_es_audio = NULL;
457         p_sys->p_es_sub = NULL;
458         p_sys->p_es_video = NULL;
459     }
460
461     msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
462
463     /* Mark it selected */
464     p_pgrm->b_selected = VLC_TRUE;
465
466     /* Switch master stream */
467     if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
468     {
469         p_sys->p_pgrm->clock.b_master = VLC_FALSE;
470     }
471     p_pgrm->clock.b_master = VLC_TRUE;
472     p_sys->p_pgrm = p_pgrm;
473
474     /* Update "program" */
475     val.i_int = p_pgrm->i_id;
476     var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
477
478     /* Update "es-*" */
479     var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
480     var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
481     var_Change( p_input, "spu-es",   VLC_VAR_CLEARCHOICES, NULL, NULL );
482     for( i = 0; i < p_sys->i_es; i++ )
483     {
484         if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
485             EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
486         EsOutSelect( out, p_sys->es[i], VLC_FALSE );
487     }
488
489     /* Update now playing */
490     input_item_SetNowPlaying( p_input->p->input.p_item,
491                               p_pgrm->psz_now_playing );
492     input_item_SetPublisher( p_input->p->input.p_item,
493                              p_pgrm->psz_publisher );
494
495     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
496 }
497
498 /* EsOutAddProgram:
499  *  Add a program
500  */
501 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
502 {
503     es_out_sys_t      *p_sys = out->p_sys;
504     input_thread_t    *p_input = p_sys->p_input;
505     vlc_value_t       val;
506
507     es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
508
509     /* Init */
510     p_pgrm->i_id = i_group;
511     p_pgrm->i_es = 0;
512     p_pgrm->b_selected = VLC_FALSE;
513     p_pgrm->psz_name = NULL;
514     p_pgrm->psz_now_playing = NULL;
515     p_pgrm->psz_publisher = NULL;
516     p_pgrm->p_epg = NULL;
517     input_ClockInit( p_input, &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average );
518
519     /* Append it */
520     TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
521
522     /* Update "program" variable */
523     val.i_int = i_group;
524     var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
525
526     if( i_group == var_GetInteger( p_input, "program" ) )
527     {
528         EsOutProgramSelect( out, p_pgrm );
529     }
530     else
531     {
532         var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
533     }
534     return p_pgrm;
535 }
536
537 /* EsOutDelProgram:
538  *  Delete a program
539  */
540 static int EsOutProgramDel( es_out_t *out, int i_group )
541 {
542     es_out_sys_t      *p_sys = out->p_sys;
543     input_thread_t    *p_input = p_sys->p_input;
544     es_out_pgrm_t     *p_pgrm = NULL;
545     vlc_value_t       val;
546     int               i;
547
548     for( i = 0; i < p_sys->i_pgrm; i++ )
549     {
550         if( p_sys->pgrm[i]->i_id == i_group )
551         {
552             p_pgrm = p_sys->pgrm[i];
553             break;
554         }
555     }
556
557     if( p_pgrm == NULL )
558         return VLC_EGENERIC;
559
560     if( p_pgrm->i_es )
561     {
562         msg_Dbg( p_input, "can't delete program %d which still has %i ES",
563                  i_group, p_pgrm->i_es );
564         return VLC_EGENERIC;
565     }
566
567     TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
568
569     /* If program is selected we need to unselect it */
570     if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
571
572     if( p_pgrm->psz_name ) free( p_pgrm->psz_name );
573     if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
574     if( p_pgrm->psz_publisher ) free( p_pgrm->psz_publisher );
575     if( p_pgrm->p_epg )
576         vlc_epg_Delete( p_pgrm->p_epg );
577     free( p_pgrm );
578
579     /* Update "program" variable */
580     val.i_int = i_group;
581     var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
582
583     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
584
585     return VLC_SUCCESS;
586 }
587
588 /* EsOutProgramMeta:
589  */
590 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
591 {
592     char *psz = NULL;
593     if( p_pgrm->psz_name )
594         asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
595     else
596         asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
597     return psz;
598 }
599
600 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
601 {
602     es_out_sys_t      *p_sys = out->p_sys;
603     es_out_pgrm_t     *p_pgrm = NULL;
604     input_thread_t    *p_input = p_sys->p_input;
605     char              *psz_cat;
606     const char        *psz_title = NULL;
607     const char        *psz_provider = NULL;
608     int i;
609
610     msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
611
612     /* Check against empty meta data (empty for what we handle) */
613     if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
614         !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
615         !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
616         vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
617             return;
618     /* Find program */
619     for( i = 0; i < p_sys->i_pgrm; i++ )
620     {
621         if( p_sys->pgrm[i]->i_id == i_group )
622         {
623             p_pgrm = p_sys->pgrm[i];
624             break;
625         }
626     }
627     if( p_pgrm == NULL )
628         p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
629
630     /* */
631     psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
632     psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
633
634     /* Update the description text of the program */
635     if( psz_title && *psz_title )
636     {
637         vlc_value_t val;
638         vlc_value_t text;
639
640         if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
641         {
642             char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
643
644             /* Remove old entries */
645             input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
646             /* TODO update epg name */
647             free( psz_cat );
648         }
649         if( p_pgrm->psz_name ) free( p_pgrm->psz_name );
650         p_pgrm->psz_name = strdup( psz_title );
651
652         /* ugly but it works */
653         val.i_int = i_group;
654         var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
655
656         if( psz_provider && *psz_provider )
657         {
658             asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
659             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
660             free( text.psz_string );
661         }
662         else
663         {
664             text.psz_string = (char *)psz_title;
665             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
666         }
667     }
668
669     psz_cat = EsOutProgramGetMetaName( p_pgrm );
670     if( psz_provider )
671     {
672         if( p_sys->p_pgrm == p_pgrm )
673             input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
674         input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
675     }
676     char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
677     for( i = 0; ppsz_all_keys[i]; i++ )
678     {
679         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
680                        vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
681         free( ppsz_all_keys[i] );
682     }
683     free( ppsz_all_keys );
684
685     free( psz_cat );
686 }
687
688 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
689 {
690     int i;
691
692     /* Add new event */
693     for( i = 0; i < p_src->i_event; i++ )
694     {
695         vlc_epg_event_t *p_evt = p_src->pp_event[i];
696         vlc_bool_t b_add = VLC_TRUE;
697         int j;
698
699         for( j = 0; j < p_dst->i_event; j++ )
700         {
701             if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
702             {
703                 b_add = VLC_FALSE;
704                 break;
705             }
706             if( p_dst->pp_event[j]->i_start > p_evt->i_start )
707                 break;
708         }
709         if( b_add )
710         {
711             vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
712             if( !p_copy )
713                 break;
714             memset( p_copy, 0, sizeof(vlc_epg_event_t) );
715             p_copy->i_start = p_evt->i_start;
716             p_copy->i_duration = p_evt->i_duration;
717             p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
718             p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
719             p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
720             TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
721         }
722     }
723     /* Update current */
724     vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
725
726     /* Keep only 1 old event  */
727     if( p_dst->p_current )
728     {
729         while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
730             TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
731     }
732 }
733
734 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
735 {
736     es_out_sys_t      *p_sys = out->p_sys;
737     input_thread_t    *p_input = p_sys->p_input;
738     es_out_pgrm_t     *p_pgrm = NULL;
739     char *psz_cat;
740     char *psz_epg;
741     int i;
742
743     /* Find program */
744     for( i = 0; i < p_sys->i_pgrm; i++ )
745     {
746         if( p_sys->pgrm[i]->i_id == i_group )
747         {
748             p_pgrm = p_sys->pgrm[i];
749             break;
750         }
751     }
752     if( p_pgrm == NULL )
753         p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
754
755     /* Merge EPG */
756     if( !p_pgrm->p_epg )
757         p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
758     vlc_epg_Merge( p_pgrm->p_epg, p_epg );
759
760     /* Update info */
761     psz_cat = EsOutProgramGetMetaName( p_pgrm );
762 #ifdef HAVE_LOCALTIME_R
763     asprintf( &psz_epg, "EPG %s", psz_cat );
764     input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
765     msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
766     for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
767     {
768         const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
769         time_t t_start = (time_t)p_evt->i_start;
770         struct tm tm_start;
771         char psz_start[128];
772
773         localtime_r( &t_start, &tm_start );
774
775         snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
776         if( p_evt->psz_short_description || p_evt->psz_description )
777             input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
778                            p_evt->psz_name,
779                            p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
780                            p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
781         else
782             input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
783                            p_evt->psz_name,
784                            p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
785     }
786     free( psz_epg );
787 #endif
788     /* Update now playing */
789     if( p_pgrm->psz_now_playing )
790         free( p_pgrm->psz_now_playing );
791     p_pgrm->psz_now_playing = NULL;
792     if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
793         p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
794
795     if( p_pgrm == p_sys->p_pgrm )
796         input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
797
798     if( p_pgrm->psz_now_playing )
799     {
800         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
801             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
802             p_pgrm->psz_now_playing );
803     }
804     else
805     {
806         input_Control( p_input, INPUT_DEL_INFO, psz_cat,
807             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
808     }
809
810     free( psz_cat );
811 }
812
813 /* EsOutAdd:
814  *  Add an es_out
815  */
816 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
817 {
818     es_out_sys_t      *p_sys = out->p_sys;
819     input_thread_t    *p_input = p_sys->p_input;
820
821     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
822     es_out_pgrm_t     *p_pgrm = NULL;
823     int i;
824
825     if( fmt->i_group < 0 )
826     {
827         msg_Err( p_input, "invalid group number" );
828         return NULL;
829     }
830
831     /* Search the program */
832     for( i = 0; i < p_sys->i_pgrm; i++ )
833     {
834         if( fmt->i_group == p_sys->pgrm[i]->i_id )
835         {
836             p_pgrm = p_sys->pgrm[i];
837             break;
838         }
839     }
840     if( p_pgrm == NULL )
841     {
842         /* Create a new one */
843         p_pgrm = EsOutProgramAdd( out, fmt->i_group );
844     }
845
846     /* Increase ref count for program */
847     p_pgrm->i_es++;
848
849     /* Set up ES */
850     if( fmt->i_id < 0 )
851         fmt->i_id = out->p_sys->i_id;
852     es->i_id = fmt->i_id;
853     es->p_pgrm = p_pgrm;
854     es_format_Copy( &es->fmt, fmt );
855     es->i_preroll_end = -1;
856
857     switch( fmt->i_cat )
858     {
859     case AUDIO_ES:
860         es->i_channel = p_sys->i_audio;
861         break;
862
863     case VIDEO_ES:
864         es->i_channel = p_sys->i_video;
865         if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
866             vlc_ureduce( &es->fmt.video.i_frame_rate,
867                          &es->fmt.video.i_frame_rate_base,
868                          fmt->video.i_frame_rate,
869                          fmt->video.i_frame_rate_base, 0 );
870         break;
871
872     case SPU_ES:
873         es->i_channel = p_sys->i_sub;
874         break;
875
876     default:
877         es->i_channel = 0;
878         break;
879     }
880     es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
881     es->psz_language_code = LanguageGetCode( fmt->psz_language );
882     es->p_dec = NULL;
883
884     if( es->p_pgrm == p_sys->p_pgrm )
885         EsOutESVarUpdate( out, es, VLC_FALSE );
886
887     /* Select it if needed */
888     EsOutSelect( out, es, VLC_FALSE );
889
890
891     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
892     p_sys->i_id++;  /* always incremented */
893     switch( fmt->i_cat )
894     {
895         case AUDIO_ES:
896             p_sys->i_audio++;
897             break;
898         case SPU_ES:
899             p_sys->i_sub++;
900             break;
901         case VIDEO_ES:
902             p_sys->i_video++;
903             break;
904     }
905
906     EsOutAddInfo( out, es );
907
908     return es;
909 }
910
911 static void EsSelect( es_out_t *out, es_out_id_t *es )
912 {
913     es_out_sys_t   *p_sys = out->p_sys;
914     input_thread_t *p_input = p_sys->p_input;
915     vlc_value_t    val;
916     const char     *psz_var;
917
918     if( es->p_dec )
919     {
920         msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
921         return;
922     }
923
924     if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
925     {
926         if( !var_GetBool( p_input, "video" ) ||
927             ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
928         {
929             msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
930                      es->i_id );
931             return;
932         }
933     }
934     else if( es->fmt.i_cat == AUDIO_ES )
935     {
936         var_Get( p_input, "audio", &val );
937         if( !var_GetBool( p_input, "audio" ) ||
938             ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
939         {
940             msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
941                      es->i_id );
942             return;
943         }
944     }
945     if( es->fmt.i_cat == SPU_ES )
946     {
947         var_Get( p_input, "spu", &val );
948         if( !var_GetBool( p_input, "spu" ) ||
949             ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
950         {
951             msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
952                      es->i_id );
953             return;
954         }
955     }
956
957     es->i_preroll_end = -1;
958     es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
959     if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
960         return;
961
962     if( es->fmt.i_cat == VIDEO_ES )
963         psz_var = "video-es";
964     else if( es->fmt.i_cat == AUDIO_ES )
965         psz_var = "audio-es";
966     else if( es->fmt.i_cat == SPU_ES )
967         psz_var = "spu-es";
968     else
969         return;
970
971     /* Mark it as selected */
972     val.i_int = es->i_id;
973     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
974
975
976     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
977 }
978
979 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
980 {
981     es_out_sys_t   *p_sys = out->p_sys;
982     input_thread_t *p_input = p_sys->p_input;
983     vlc_value_t    val;
984     const char     *psz_var;
985
986     if( es->p_dec == NULL )
987     {
988         msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
989         return;
990     }
991
992     input_DecoderDelete( es->p_dec );
993     es->p_dec = NULL;
994
995     if( !b_update )
996         return;
997
998     /* Update var */
999     if( es->p_dec == NULL )
1000         return;
1001     if( es->fmt.i_cat == VIDEO_ES )
1002         psz_var = "video-es";
1003     else if( es->fmt.i_cat == AUDIO_ES )
1004         psz_var = "audio-es";
1005     else if( es->fmt.i_cat == SPU_ES )
1006         psz_var = "spu-es";
1007     else
1008         return;
1009
1010     /* Mark it as selected */
1011     val.i_int = -1;
1012     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1013
1014     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1015 }
1016
1017 /**
1018  * Select an ES given the current mode
1019  * XXX: you need to take a the lock before (stream.stream_lock)
1020  *
1021  * \param out The es_out structure
1022  * \param es es_out_id structure
1023  * \param b_force ...
1024  * \return nothing
1025  */
1026 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
1027 {
1028     es_out_sys_t      *p_sys = out->p_sys;
1029
1030     int i_cat = es->fmt.i_cat;
1031
1032     if( !p_sys->b_active ||
1033         ( !b_force && es->fmt.i_priority < 0 ) )
1034     {
1035         return;
1036     }
1037
1038     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1039     {
1040         if( !es->p_dec )
1041             EsSelect( out, es );
1042     }
1043     else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1044     {
1045         vlc_value_t val;
1046         int i;
1047         var_Get( p_sys->p_input, "programs", &val );
1048         for ( i = 0; i < val.p_list->i_count; i++ )
1049         {
1050             if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1051             {
1052                 if( !es->p_dec )
1053                     EsSelect( out, es );
1054                 break;
1055             }
1056         }
1057         var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1058     }
1059     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1060     {
1061         int i_wanted  = -1;
1062
1063         if( es->p_pgrm != p_sys->p_pgrm )
1064             return;
1065
1066         if( i_cat == AUDIO_ES )
1067         {
1068             int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1069                                      es->psz_language_code );
1070
1071             if( p_sys->p_es_audio &&
1072                 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1073             {
1074                 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1075                                          p_sys->p_es_audio->psz_language_code );
1076
1077                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1078                     return;
1079                 i_wanted = es->i_channel;
1080             }
1081             else
1082             {
1083                 /* Select audio if (no audio selected yet)
1084                  * - no audio-language
1085                  * - no audio code for the ES
1086                  * - audio code in the requested list */
1087                 if( idx1 >= 0 ||
1088                     !strcmp( es->psz_language_code, "??" ) ||
1089                     !p_sys->ppsz_audio_language )
1090                     i_wanted = es->i_channel;
1091             }
1092
1093             if( p_sys->i_audio_last >= 0 )
1094                 i_wanted = p_sys->i_audio_last;
1095
1096             if( p_sys->i_audio_id >= 0 )
1097             {
1098                 if( es->i_id == p_sys->i_audio_id )
1099                     i_wanted = es->i_channel;
1100                 else
1101                     return;
1102             }
1103         }
1104         else if( i_cat == SPU_ES )
1105         {
1106             int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1107                                      es->psz_language_code );
1108
1109             if( p_sys->p_es_sub &&
1110                 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1111             {
1112                 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1113                                          p_sys->p_es_sub->psz_language_code );
1114
1115                 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1116                         idx1, es->psz_language_code, idx2,
1117                         p_sys->p_es_sub->psz_language_code );
1118
1119                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1120                     return;
1121                 /* We found a SPU that matches our language request */
1122                 i_wanted  = es->i_channel;
1123             }
1124             else if( idx1 >= 0 )
1125             {
1126                 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1127                         idx1, es->psz_language_code );
1128
1129                 i_wanted  = es->i_channel;
1130             }
1131             if( p_sys->i_sub_last >= 0 )
1132                 i_wanted  = p_sys->i_sub_last;
1133
1134             if( p_sys->i_sub_id >= 0 )
1135             {
1136                 if( es->i_id == p_sys->i_sub_id )
1137                     i_wanted = es->i_channel;
1138                 else
1139                     return;
1140             }
1141         }
1142         else if( i_cat == VIDEO_ES )
1143         {
1144             i_wanted  = es->i_channel;
1145         }
1146
1147         if( i_wanted == es->i_channel && es->p_dec == NULL )
1148             EsSelect( out, es );
1149     }
1150
1151     /* FIXME TODO handle priority here */
1152     if( es->p_dec )
1153     {
1154         if( i_cat == AUDIO_ES )
1155         {
1156             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1157                 p_sys->p_es_audio &&
1158                 p_sys->p_es_audio != es &&
1159                 p_sys->p_es_audio->p_dec )
1160             {
1161                 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
1162             }
1163             p_sys->p_es_audio = es;
1164         }
1165         else if( i_cat == SPU_ES )
1166         {
1167             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1168                 p_sys->p_es_sub &&
1169                 p_sys->p_es_sub != es &&
1170                 p_sys->p_es_sub->p_dec )
1171             {
1172                 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1173             }
1174             p_sys->p_es_sub = es;
1175         }
1176         else if( i_cat == VIDEO_ES )
1177         {
1178             p_sys->p_es_video = es;
1179         }
1180     }
1181 }
1182
1183 /**
1184  * Send a block for the given es_out
1185  *
1186  * \param out the es_out to send from
1187  * \param es the es_out_id
1188  * \param p_block the data block to send
1189  */
1190 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1191 {
1192     es_out_sys_t *p_sys = out->p_sys;
1193     input_thread_t    *p_input = p_sys->p_input;
1194     es_out_pgrm_t *p_pgrm = es->p_pgrm;
1195     int64_t i_delay;
1196     int i_total=0;
1197
1198     if( es->fmt.i_cat == AUDIO_ES )
1199         i_delay = p_sys->i_audio_delay;
1200     else if( es->fmt.i_cat == SPU_ES )
1201         i_delay = p_sys->i_spu_delay;
1202     else
1203         i_delay = 0;
1204
1205     if( p_input->p_libvlc->b_stats )
1206     {
1207         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1208         stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1209                              p_block->i_buffer, &i_total );
1210         stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1211                            (float)i_total, NULL );
1212         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1213     }
1214
1215     /* Mark preroll blocks */
1216     if( es->i_preroll_end >= 0 )
1217     {
1218         int64_t i_date = p_block->i_pts;
1219         if( i_date <= 0 )
1220             i_date = p_block->i_dts;
1221
1222         if( i_date < es->i_preroll_end )
1223             p_block->i_flags |= BLOCK_FLAG_PREROLL;
1224         else
1225             es->i_preroll_end = -1;
1226     }
1227
1228     if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1229     {
1230         p_block->i_dts += i_delay;
1231     }
1232     else if( p_block->i_dts > 0 )
1233     {
1234         p_block->i_dts =
1235             input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1236     }
1237     if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1238     {
1239         p_block->i_pts += i_delay;
1240     }
1241     else if( p_block->i_pts > 0 )
1242     {
1243         p_block->i_pts =
1244             input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1245     }
1246     if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1247     {
1248         mtime_t current_date = mdate();
1249         if( !p_block->i_pts
1250                || p_block->i_pts > current_date + 10000000
1251                || current_date > p_block->i_pts )
1252         {
1253             /* ETSI EN 300 472 Annex A : do not take into account the PTS
1254              * for teletext streams. */
1255             p_block->i_pts = current_date + 400000
1256                                + p_input->i_pts_delay + i_delay;
1257         }
1258     }
1259
1260     p_block->i_rate = p_input->p->i_rate;
1261
1262     /* TODO handle mute */
1263     if( es->p_dec &&
1264         ( es->fmt.i_cat != AUDIO_ES ||
1265           ( p_input->p->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1266             p_input->p->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1267     {
1268         input_DecoderDecode( es->p_dec, p_block );
1269     }
1270     else
1271     {
1272         block_Release( p_block );
1273     }
1274
1275     return VLC_SUCCESS;
1276 }
1277
1278 /*****************************************************************************
1279  * EsOutDel:
1280  *****************************************************************************/
1281 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1282 {
1283     es_out_sys_t *p_sys = out->p_sys;
1284     vlc_bool_t b_reselect = VLC_FALSE;
1285     int i;
1286
1287     /* We don't try to reselect */
1288     if( es->p_dec )
1289     {
1290         while( !out->p_sys->p_input->b_die && es->p_dec )
1291         {
1292             if( input_DecoderEmpty( es->p_dec ) )
1293                 break;
1294             msleep( 20*1000 );
1295         }
1296         EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1297     }
1298
1299     if( es->p_pgrm == p_sys->p_pgrm )
1300         EsOutESVarUpdate( out, es, VLC_TRUE );
1301
1302     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1303
1304     es->p_pgrm->i_es--;
1305     if( es->p_pgrm->i_es == 0 )
1306     {
1307         msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1308     }
1309
1310     if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1311         p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1312
1313     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1314     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1315     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
1316
1317     switch( es->fmt.i_cat )
1318     {
1319         case AUDIO_ES:
1320             p_sys->i_audio--;
1321             break;
1322         case SPU_ES:
1323             p_sys->i_sub--;
1324             break;
1325         case VIDEO_ES:
1326             p_sys->i_video--;
1327             break;
1328     }
1329
1330     /* Re-select another track when needed */
1331     if( b_reselect )
1332         for( i = 0; i < p_sys->i_es; i++ )
1333         {
1334             if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1335                 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1336         }
1337
1338     if( es->psz_language )
1339         free( es->psz_language );
1340     if( es->psz_language_code )
1341         free( es->psz_language_code );
1342
1343     es_format_Clean( &es->fmt );
1344
1345     free( es );
1346 }
1347
1348 /**
1349  * Control query handler
1350  *
1351  * \param out the es_out to control
1352  * \param i_query A es_out query as defined in include/ninput.h
1353  * \param args a variable list of arguments for the query
1354  * \return VLC_SUCCESS or an error code
1355  */
1356 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1357 {
1358     es_out_sys_t *p_sys = out->p_sys;
1359     vlc_bool_t  b, *pb;
1360     int         i, *pi;
1361
1362     es_out_id_t *es;
1363
1364     switch( i_query )
1365     {
1366         case ES_OUT_SET_ES_STATE:
1367             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1368             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1369             if( b && es->p_dec == NULL )
1370             {
1371                 EsSelect( out, es );
1372                 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1373             }
1374             else if( !b && es->p_dec )
1375             {
1376                 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1377                 return VLC_SUCCESS;
1378             }
1379             return VLC_SUCCESS;
1380
1381         case ES_OUT_GET_ES_STATE:
1382             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1383             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1384
1385             *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1386             return VLC_SUCCESS;
1387
1388         case ES_OUT_SET_ACTIVE:
1389         {
1390             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1391             p_sys->b_active = b;
1392             /* Needed ? */
1393             if( b )
1394                 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1395             return VLC_SUCCESS;
1396         }
1397
1398         case ES_OUT_GET_ACTIVE:
1399             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1400             *pb = p_sys->b_active;
1401             return VLC_SUCCESS;
1402
1403         case ES_OUT_SET_MODE:
1404             i = (int) va_arg( args, int );
1405             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1406                 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1407             {
1408                 p_sys->i_mode = i;
1409
1410                 /* Reapply policy mode */
1411                 for( i = 0; i < p_sys->i_es; i++ )
1412                 {
1413                     if( p_sys->es[i]->p_dec )
1414                     {
1415                         EsUnselect( out, p_sys->es[i],
1416                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1417                     }
1418                 }
1419                 for( i = 0; i < p_sys->i_es; i++ )
1420                 {
1421                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1422                 }
1423                 return VLC_SUCCESS;
1424             }
1425             return VLC_EGENERIC;
1426
1427         case ES_OUT_GET_MODE:
1428             pi = (int*) va_arg( args, int* );
1429             *pi = p_sys->i_mode;
1430             return VLC_SUCCESS;
1431
1432         case ES_OUT_SET_ES:
1433             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1434             /* Special case NULL, NULL+i_cat */
1435             if( es == NULL )
1436             {
1437                 for( i = 0; i < p_sys->i_es; i++ )
1438                 {
1439                     if( p_sys->es[i]->p_dec )
1440                         EsUnselect( out, p_sys->es[i],
1441                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1442                 }
1443             }
1444             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1445             {
1446                 for( i = 0; i < p_sys->i_es; i++ )
1447                 {
1448                     if( p_sys->es[i]->p_dec &&
1449                         p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1450                         EsUnselect( out, p_sys->es[i],
1451                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1452                 }
1453             }
1454             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1455             {
1456                 for( i = 0; i < p_sys->i_es; i++ )
1457                 {
1458                     if( p_sys->es[i]->p_dec &&
1459                         p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1460                         EsUnselect( out, p_sys->es[i],
1461                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1462                 }
1463             }
1464             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1465             {
1466                 for( i = 0; i < p_sys->i_es; i++ )
1467                 {
1468                     if( p_sys->es[i]->p_dec &&
1469                         p_sys->es[i]->fmt.i_cat == SPU_ES )
1470                         EsUnselect( out, p_sys->es[i],
1471                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1472                 }
1473             }
1474             else
1475             {
1476                 for( i = 0; i < p_sys->i_es; i++ )
1477                 {
1478                     if( es == p_sys->es[i] )
1479                     {
1480                         EsOutSelect( out, es, VLC_TRUE );
1481                         break;
1482                     }
1483                 }
1484             }
1485             {
1486                 playlist_t * p_playlist = pl_Yield( p_sys->p_input );
1487                 PL_LOCK;
1488                 p_playlist->gc_date = mdate();
1489                 vlc_cond_signal( &p_playlist->object_wait );
1490                 PL_UNLOCK;
1491                 pl_Release( p_playlist );
1492             }
1493             return VLC_SUCCESS;
1494
1495         case ES_OUT_SET_PCR:
1496         case ES_OUT_SET_GROUP_PCR:
1497         {
1498             es_out_pgrm_t *p_pgrm = NULL;
1499             int            i_group = 0;
1500             int64_t        i_pcr;
1501
1502             if( i_query == ES_OUT_SET_PCR )
1503             {
1504                 p_pgrm = p_sys->p_pgrm;
1505             }
1506             else
1507             {
1508                 int i;
1509                 i_group = (int)va_arg( args, int );
1510                 for( i = 0; i < p_sys->i_pgrm; i++ )
1511                 {
1512                     if( p_sys->pgrm[i]->i_id == i_group )
1513                     {
1514                         p_pgrm = p_sys->pgrm[i];
1515                         break;
1516                     }
1517                 }
1518             }
1519             if( p_pgrm == NULL )
1520                 p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
1521
1522             i_pcr = (int64_t)va_arg( args, int64_t );
1523             /* search program */
1524             input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1525             return VLC_SUCCESS;
1526         }
1527
1528         case ES_OUT_RESET_PCR:
1529             for( i = 0; i < p_sys->i_pgrm; i++ )
1530             {
1531                 p_sys->pgrm[i]->clock.i_synchro_state =  SYNCHRO_REINIT;
1532                 p_sys->pgrm[i]->clock.last_pts = 0;
1533             }
1534             return VLC_SUCCESS;
1535
1536         case ES_OUT_GET_TS:
1537             if( p_sys->p_pgrm )
1538             {
1539                 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1540                 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1541                 *pi_ts = input_ClockGetTS( p_sys->p_input,
1542                                            &p_sys->p_pgrm->clock, i_ts );
1543                 return VLC_SUCCESS;
1544             }
1545             return VLC_EGENERIC;
1546
1547         case ES_OUT_GET_GROUP:
1548             pi = (int*) va_arg( args, int* );
1549             if( p_sys->p_pgrm )
1550                 *pi = p_sys->p_pgrm->i_id;
1551             else
1552                 *pi = -1;    /* FIXME */
1553             return VLC_SUCCESS;
1554
1555         case ES_OUT_SET_GROUP:
1556         {
1557             int j;
1558             i = (int) va_arg( args, int );
1559             for( j = 0; j < p_sys->i_pgrm; j++ )
1560             {
1561                 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1562                 if( p_pgrm->i_id == i )
1563                 {
1564                     EsOutProgramSelect( out, p_pgrm );
1565                     return VLC_SUCCESS;
1566                 }
1567             }
1568             return VLC_EGENERIC;
1569         }
1570
1571         case ES_OUT_SET_FMT:
1572         {
1573             /* This ain't pretty but is need by some demuxers (eg. Ogg )
1574              * to update the p_extra data */
1575             es_format_t *p_fmt;
1576             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1577             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1578             if( es == NULL ) return VLC_EGENERIC;
1579
1580             if( p_fmt->i_extra )
1581             {
1582                 es->fmt.i_extra = p_fmt->i_extra;
1583                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1584                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1585
1586                 if( !es->p_dec ) return VLC_SUCCESS;
1587
1588 #if 1
1589                 input_DecoderDelete( es->p_dec );
1590                 es->p_dec = input_DecoderNew( p_sys->p_input,
1591                                               &es->fmt, VLC_FALSE );
1592
1593 #else
1594                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1595                 es->p_dec->fmt_in.p_extra =
1596                     realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1597                 memcpy( es->p_dec->fmt_in.p_extra,
1598                         p_fmt->p_extra, p_fmt->i_extra );
1599 #endif
1600             }
1601
1602             return VLC_SUCCESS;
1603         }
1604
1605         case ES_OUT_SET_NEXT_DISPLAY_TIME:
1606         {
1607             int64_t i_date;
1608
1609             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1610             i_date = (int64_t)va_arg( args, int64_t );
1611
1612             if( !es || !es->p_dec )
1613                 return VLC_EGENERIC;
1614
1615             /* XXX We should call input_ClockGetTS but PCR has been reseted
1616              * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1617              * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1618             es->i_preroll_end = i_date;
1619
1620             return VLC_SUCCESS;
1621         }
1622         case ES_OUT_SET_GROUP_META:
1623         {
1624             int i_group = (int)va_arg( args, int );
1625             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1626
1627             EsOutProgramMeta( out, i_group, p_meta );
1628             return VLC_SUCCESS;
1629         }
1630         case ES_OUT_SET_GROUP_EPG:
1631         {
1632             int i_group = (int)va_arg( args, int );
1633             vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1634
1635             EsOutProgramEpg( out, i_group, p_epg );
1636             return VLC_SUCCESS;
1637         }
1638         case ES_OUT_DEL_GROUP:
1639         {
1640             int i_group = (int)va_arg( args, int );
1641
1642             return EsOutProgramDel( out, i_group );
1643         }
1644
1645         default:
1646             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1647             return VLC_EGENERIC;
1648     }
1649 }
1650
1651 /****************************************************************************
1652  * LanguageGetName: try to expend iso639 into plain name
1653  ****************************************************************************/
1654 static char *LanguageGetName( const char *psz_code )
1655 {
1656     const iso639_lang_t *pl;
1657
1658     if( psz_code == NULL )
1659     {
1660         return strdup( "" );
1661     }
1662
1663     if( strlen( psz_code ) == 2 )
1664     {
1665         pl = GetLang_1( psz_code );
1666     }
1667     else if( strlen( psz_code ) == 3 )
1668     {
1669         pl = GetLang_2B( psz_code );
1670         if( !strcmp( pl->psz_iso639_1, "??" ) )
1671         {
1672             pl = GetLang_2T( psz_code );
1673         }
1674     }
1675     else
1676     {
1677         return strdup( psz_code );
1678     }
1679
1680     if( !strcmp( pl->psz_iso639_1, "??" ) )
1681     {
1682        return strdup( psz_code );
1683     }
1684     else
1685     {
1686         if( *pl->psz_native_name )
1687         {
1688             return strdup( pl->psz_native_name );
1689         }
1690         return strdup( pl->psz_eng_name );
1691     }
1692 }
1693
1694 /* Get a 2 char code */
1695 static char *LanguageGetCode( const char *psz_lang )
1696 {
1697     const iso639_lang_t *pl;
1698
1699     if( psz_lang == NULL || *psz_lang == '\0' )
1700         return strdup("??");
1701
1702     for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1703     {
1704         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1705             !strcasecmp( pl->psz_native_name, psz_lang ) ||
1706             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1707             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1708             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1709             break;
1710     }
1711
1712     if( pl->psz_iso639_1 != NULL )
1713         return strdup( pl->psz_iso639_1 );
1714
1715     return strdup("??");
1716 }
1717
1718 static char **LanguageSplit( const char *psz_langs )
1719 {
1720     char *psz_dup;
1721     char *psz_parser;
1722     char **ppsz = NULL;
1723     int i_psz = 0;
1724
1725     if( psz_langs == NULL ) return NULL;
1726
1727     psz_parser = psz_dup = strdup(psz_langs);
1728
1729     while( psz_parser && *psz_parser )
1730     {
1731         char *psz;
1732         char *psz_code;
1733
1734         psz = strchr(psz_parser, ',' );
1735         if( psz ) *psz++ = '\0';
1736
1737         psz_code = LanguageGetCode( psz_parser );
1738         if( strcmp( psz_code, "??" ) )
1739         {
1740             TAB_APPEND( i_psz, ppsz, psz_code );
1741         }
1742
1743         psz_parser = psz;
1744     }
1745
1746     if( i_psz )
1747     {
1748         TAB_APPEND( i_psz, ppsz, NULL );
1749     }
1750
1751     free( psz_dup );
1752     return ppsz;
1753 }
1754
1755 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1756 {
1757     int i;
1758
1759     if( !ppsz_langs || !psz_lang ) return -1;
1760
1761     for( i = 0; ppsz_langs[i]; i++ )
1762         if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1763
1764     return -1;
1765 }
1766
1767 /****************************************************************************
1768  * EsOutAddInfo:
1769  * - add meta info to the playlist item
1770  ****************************************************************************/
1771 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1772 {
1773     es_out_sys_t   *p_sys = out->p_sys;
1774     input_thread_t *p_input = p_sys->p_input;
1775     es_format_t    *fmt = &es->fmt;
1776     char           *psz_cat;
1777     lldiv_t         div;
1778
1779     /* Add stream info */
1780     asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1781
1782     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1783                    "%.4s", (char*)&fmt->i_codec );
1784
1785     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1786                    "%s", es->psz_language );
1787
1788     /* Add information */
1789     switch( fmt->i_cat )
1790     {
1791     case AUDIO_ES:
1792         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1793                        _("Type"), _("Audio") );
1794
1795         if( fmt->audio.i_channels > 0 )
1796             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1797                            "%u", fmt->audio.i_channels );
1798
1799         if( fmt->audio.i_rate > 0 )
1800         {
1801             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1802                            _("%u Hz"), fmt->audio.i_rate );
1803             var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
1804         }
1805
1806         if( fmt->audio.i_bitspersample > 0 )
1807             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1808                            _("Bits per sample"), "%u",
1809                            fmt->audio.i_bitspersample );
1810
1811         if( fmt->i_bitrate > 0 )
1812         {
1813             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1814                            _("%u kb/s"), fmt->i_bitrate / 1000 );
1815             var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
1816         }
1817         break;
1818
1819     case VIDEO_ES:
1820         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1821                        _("Type"), _("Video") );
1822
1823         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1824             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1825                            _("Resolution"), "%ux%u",
1826                            fmt->video.i_width, fmt->video.i_height );
1827
1828         if( fmt->video.i_visible_width > 0 &&
1829             fmt->video.i_visible_height > 0 )
1830             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1831                            _("Display resolution"), "%ux%u",
1832                            fmt->video.i_visible_width,
1833                            fmt->video.i_visible_height);
1834        if( fmt->video.i_frame_rate > 0 &&
1835            fmt->video.i_frame_rate_base > 0 )
1836        {
1837            div = lldiv( (float)fmt->video.i_frame_rate /
1838                                fmt->video.i_frame_rate_base * 1000000,
1839                                1000000 );
1840            input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1841                           _("Frame rate"), I64Fd".%06u",
1842                           div.quot, (unsigned int )div.rem );
1843        }
1844        break;
1845
1846     case SPU_ES:
1847         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1848                        _("Type"), _("Subtitle") );
1849         break;
1850
1851     default:
1852         break;
1853     }
1854
1855     free( psz_cat );
1856 }