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