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