1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #include <vlc_input.h>
37 #include <vlc_es_out.h>
38 #include <vlc_block.h>
41 #include "input_internal.h"
43 #include "vlc_playlist.h"
44 #include <vlc_iso_lang.h>
45 /* FIXME we should find a better way than including that */
46 #include "../text/iso-639_def.h"
48 /*****************************************************************************
50 *****************************************************************************/
56 /* Number of es for this pgrm */
61 /* Clock for this program */
65 char *psz_now_playing;
75 es_out_pgrm_t *p_pgrm;
78 int64_t i_preroll_end;
80 /* Channel in the track type */
84 char *psz_language_code;
88 /* Fields for Video with CC */
89 bool pb_cc_present[4];
90 es_out_id_t *pp_cc_es[4];
92 /* Field for CC track from a master video */
93 es_out_id_t *p_master;
98 input_thread_t *p_input;
102 es_out_pgrm_t **pgrm;
103 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
104 es_out_pgrm_t *p_pgrm; /* Master program */
121 int i_audio_last, i_audio_id;
122 int i_sub_last, i_sub_id;
123 int i_default_sub_id; /* As specified in container; if applicable */
124 char **ppsz_audio_language;
125 char **ppsz_sub_language;
127 /* current main es */
128 es_out_id_t *p_es_audio;
129 es_out_id_t *p_es_video;
130 es_out_id_t *p_es_sub;
133 int64_t i_audio_delay;
136 /* Rate used to rescale ES ts */
140 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
141 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
142 static void EsOutDel ( es_out_t *, es_out_id_t * );
143 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
144 static int EsOutControl( es_out_t *, int i_query, va_list );
146 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
148 static bool EsIsSelected( es_out_id_t *es );
149 static void EsSelect( es_out_t *out, es_out_id_t *es );
150 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
151 static char *LanguageGetName( const char *psz_code );
152 static char *LanguageGetCode( const char *psz_lang );
153 static char **LanguageSplit( const char *psz_langs );
154 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
156 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
158 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
159 VLC_FOURCC('c', 'c', '1', ' '),
160 VLC_FOURCC('c', 'c', '2', ' '),
161 VLC_FOURCC('c', 'c', '3', ' '),
162 VLC_FOURCC('c', 'c', '4', ' '),
164 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
167 for( i = 0; i < 4; i++ )
169 if( fcc == EsOutFourccClosedCaptions[i] )
176 /*****************************************************************************
178 *****************************************************************************/
179 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
181 es_out_t *out = malloc( sizeof( es_out_t ) );
182 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
186 out->pf_add = EsOutAdd;
187 out->pf_send = EsOutSend;
188 out->pf_del = EsOutDel;
189 out->pf_control = EsOutControl;
191 out->b_sout = (p_input->p->p_sout != NULL ? true : false);
193 p_sys->p_input = p_input;
195 p_sys->b_active = false;
196 p_sys->i_mode = ES_OUT_MODE_AUTO;
199 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
200 p_sys->p_pgrm = NULL;
204 TAB_INIT( p_sys->i_es, p_sys->es );
211 var_Get( p_input, "audio-track", &val );
212 p_sys->i_audio_last = val.i_int;
214 var_Get( p_input, "sub-track", &val );
215 p_sys->i_sub_last = val.i_int;
217 p_sys->i_default_sub_id = -1;
219 if( !p_input->b_preparsing )
221 var_Get( p_input, "audio-language", &val );
222 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
223 if( p_sys->ppsz_audio_language )
225 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
226 msg_Dbg( p_input, "selected audio language[%d] %s",
227 i, p_sys->ppsz_audio_language[i] );
229 free( val.psz_string );
231 var_Get( p_input, "sub-language", &val );
232 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
233 if( p_sys->ppsz_sub_language )
235 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
236 msg_Dbg( p_input, "selected subtitle language[%d] %s",
237 i, p_sys->ppsz_sub_language[i] );
239 free( val.psz_string );
243 p_sys->ppsz_sub_language = NULL;
244 p_sys->ppsz_audio_language = NULL;
247 var_Get( p_input, "audio-track-id", &val );
248 p_sys->i_audio_id = val.i_int;
250 var_Get( p_input, "sub-track-id", &val );
251 p_sys->i_sub_id = val.i_int;
253 p_sys->p_es_audio = NULL;
254 p_sys->p_es_video = NULL;
255 p_sys->p_es_sub = NULL;
257 p_sys->i_audio_delay= 0;
258 p_sys->i_spu_delay = 0;
260 p_sys->i_rate = i_rate;
265 /*****************************************************************************
267 *****************************************************************************/
268 void input_EsOutDelete( es_out_t *out )
270 es_out_sys_t *p_sys = out->p_sys;
273 for( i = 0; i < p_sys->i_es; i++ )
275 if( p_sys->es[i]->p_dec )
277 input_DecoderDelete( p_sys->es[i]->p_dec );
279 free( p_sys->es[i]->psz_language );
280 free( p_sys->es[i]->psz_language_code );
281 es_format_Clean( &p_sys->es[i]->fmt );
283 free( p_sys->es[i] );
285 if( p_sys->ppsz_audio_language )
287 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
288 free( p_sys->ppsz_audio_language[i] );
289 free( p_sys->ppsz_audio_language );
291 if( p_sys->ppsz_sub_language )
293 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
294 free( p_sys->ppsz_sub_language[i] );
295 free( p_sys->ppsz_sub_language );
300 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
301 for( i = 0; i < p_sys->i_pgrm; i++ )
303 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
304 free( p_pgrm->psz_now_playing );
305 free( p_pgrm->psz_publisher );
306 free( p_pgrm->psz_name );
308 vlc_epg_Delete( p_pgrm->p_epg );
312 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
318 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
323 /* Special HACK, -i_id is tha cat of the stream */
324 return (es_out_id_t*)((uint8_t*)NULL-i_id);
327 for( i = 0; i < out->p_sys->i_es; i++ )
329 if( out->p_sys->es[i]->i_id == i_id )
330 return out->p_sys->es[i];
335 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
337 es_out_sys_t *p_sys = out->p_sys;
340 for( i = 0; i < p_sys->i_es; i++ )
342 es_out_id_t *es = p_sys->es[i];
344 /* Send a dummy block to let decoder know that
345 * there is a discontinuity */
346 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
347 input_DecoderDiscontinuity( es->p_dec, b_flush );
350 void input_EsOutChangeRate( es_out_t *out, int i_rate )
352 es_out_sys_t *p_sys = out->p_sys;
355 p_sys->i_rate = i_rate;
356 EsOutDiscontinuity( out, false, false );
358 for( i = 0; i < p_sys->i_pgrm; i++ )
359 input_ClockSetRate( &p_sys->pgrm[i]->clock, i_rate );
362 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
364 es_out_sys_t *p_sys = out->p_sys;
366 if( i_cat == AUDIO_ES )
367 p_sys->i_audio_delay = i_delay;
368 else if( i_cat == SPU_ES )
369 p_sys->i_spu_delay = i_delay;
371 void input_EsOutChangeState( es_out_t *out )
373 es_out_sys_t *p_sys = out->p_sys;
374 input_thread_t *p_input = p_sys->p_input;
376 if( p_input->i_state == PAUSE_S )
378 /* Send discontinuity to decoders (it will allow them to flush
379 * * if implemented */
380 EsOutDiscontinuity( out, false, false );
384 /* Out of pause, reset pcr */
385 es_out_Control( out, ES_OUT_RESET_PCR );
388 void input_EsOutChangePosition( es_out_t *out )
390 //es_out_sys_t *p_sys = out->p_sys;
392 es_out_Control( out, ES_OUT_RESET_PCR );
393 EsOutDiscontinuity( out, true, false );
396 bool input_EsOutDecodersEmpty( es_out_t *out )
398 es_out_sys_t *p_sys = out->p_sys;
401 for( i = 0; i < p_sys->i_es; i++ )
403 es_out_id_t *es = p_sys->es[i];
405 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
411 /*****************************************************************************
413 *****************************************************************************/
414 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
417 es_out_sys_t *p_sys = out->p_sys;
418 input_thread_t *p_input = p_sys->p_input;
419 vlc_value_t val, text;
423 if( fmt->i_cat == AUDIO_ES )
424 psz_var = "audio-es";
425 else if( fmt->i_cat == VIDEO_ES )
426 psz_var = "video-es";
427 else if( fmt->i_cat == SPU_ES )
435 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
436 var_SetBool( p_sys->p_input, "intf-change", true );
440 /* Get the number of ES already added */
441 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
446 /* First one, we need to add the "Disable" choice */
447 val2.i_int = -1; text.psz_string = _("Disable");
448 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
452 /* Take care of the ES description */
453 if( fmt->psz_description && *fmt->psz_description )
455 if( psz_language && *psz_language )
457 text.psz_string = malloc( strlen( fmt->psz_description) +
458 strlen( psz_language ) + 10 );
459 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
462 else text.psz_string = strdup( fmt->psz_description );
466 if( psz_language && *psz_language )
469 text.psz_string = malloc( strlen( _("Track %i") )+
470 strlen( psz_language ) + 30 );
471 asprintf( &temp, _("Track %i"), val.i_int );
472 sprintf( text.psz_string, "%s - [%s]", temp, psz_language );
477 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
478 sprintf( text.psz_string, _("Track %i"), val.i_int );
483 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
485 free( text.psz_string );
487 var_SetBool( p_sys->p_input, "intf-change", true );
490 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
493 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
496 /* EsOutProgramSelect:
497 * Select a program and update the object variable
499 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
501 es_out_sys_t *p_sys = out->p_sys;
502 input_thread_t *p_input = p_sys->p_input;
506 if( p_sys->p_pgrm == p_pgrm )
507 return; /* Nothing to do */
511 es_out_pgrm_t *old = p_sys->p_pgrm;
512 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
514 for( i = 0; i < p_sys->i_es; i++ )
516 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
517 p_sys->i_mode != ES_OUT_MODE_ALL )
518 EsUnselect( out, p_sys->es[i], true );
521 p_sys->p_es_audio = NULL;
522 p_sys->p_es_sub = NULL;
523 p_sys->p_es_video = NULL;
526 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
528 /* Mark it selected */
529 p_pgrm->b_selected = true;
531 /* Switch master stream */
532 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
534 p_sys->p_pgrm->clock.b_master = false;
536 p_pgrm->clock.b_master = true;
537 p_sys->p_pgrm = p_pgrm;
539 /* Update "program" */
540 val.i_int = p_pgrm->i_id;
541 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
544 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
545 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
546 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
547 for( i = 0; i < p_sys->i_es; i++ )
549 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
550 EsOutESVarUpdate( out, p_sys->es[i], false );
551 EsOutSelect( out, p_sys->es[i], false );
554 /* Update now playing */
555 input_item_SetNowPlaying( p_input->p->input.p_item,
556 p_pgrm->psz_now_playing );
557 input_item_SetPublisher( p_input->p->input.p_item,
558 p_pgrm->psz_publisher );
560 var_SetBool( p_sys->p_input, "intf-change", true );
566 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
568 es_out_sys_t *p_sys = out->p_sys;
569 input_thread_t *p_input = p_sys->p_input;
572 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
575 p_pgrm->i_id = i_group;
577 p_pgrm->b_selected = false;
578 p_pgrm->psz_name = NULL;
579 p_pgrm->psz_now_playing = NULL;
580 p_pgrm->psz_publisher = NULL;
581 p_pgrm->p_epg = NULL;
582 input_ClockInit( &p_pgrm->clock, false, p_input->p->input.i_cr_average, p_sys->i_rate );
585 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
587 /* Update "program" variable */
589 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
591 if( i_group == var_GetInteger( p_input, "program" ) )
593 EsOutProgramSelect( out, p_pgrm );
597 var_SetBool( p_sys->p_input, "intf-change", true );
605 static int EsOutProgramDel( es_out_t *out, int i_group )
607 es_out_sys_t *p_sys = out->p_sys;
608 input_thread_t *p_input = p_sys->p_input;
609 es_out_pgrm_t *p_pgrm = NULL;
613 for( i = 0; i < p_sys->i_pgrm; i++ )
615 if( p_sys->pgrm[i]->i_id == i_group )
617 p_pgrm = p_sys->pgrm[i];
627 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
628 i_group, p_pgrm->i_es );
632 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
634 /* If program is selected we need to unselect it */
635 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
637 free( p_pgrm->psz_name );
638 free( p_pgrm->psz_now_playing );
639 free( p_pgrm->psz_publisher );
641 vlc_epg_Delete( p_pgrm->p_epg );
644 /* Update "program" variable */
646 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
648 var_SetBool( p_sys->p_input, "intf-change", true );
655 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
658 if( p_pgrm->psz_name )
659 asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
661 asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
665 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
667 es_out_sys_t *p_sys = out->p_sys;
668 es_out_pgrm_t *p_pgrm = NULL;
669 input_thread_t *p_input = p_sys->p_input;
671 const char *psz_title = NULL;
672 const char *psz_provider = NULL;
675 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
677 /* Check against empty meta data (empty for what we handle) */
678 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
679 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
680 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
681 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
686 for( i = 0; i < p_sys->i_pgrm; i++ )
688 if( p_sys->pgrm[i]->i_id == i_group )
690 p_pgrm = p_sys->pgrm[i];
695 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
698 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
699 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
701 /* Update the description text of the program */
702 if( psz_title && *psz_title )
707 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
709 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
711 /* Remove old entries */
712 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
713 /* TODO update epg name */
716 free( p_pgrm->psz_name );
717 p_pgrm->psz_name = strdup( psz_title );
719 /* ugly but it works */
721 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
723 if( psz_provider && *psz_provider )
725 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
726 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
727 free( text.psz_string );
731 text.psz_string = (char *)psz_title;
732 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
736 psz_cat = EsOutProgramGetMetaName( p_pgrm );
739 if( p_sys->p_pgrm == p_pgrm )
740 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
741 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
743 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
744 for( i = 0; ppsz_all_keys[i]; i++ )
746 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
747 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
748 free( ppsz_all_keys[i] );
750 free( ppsz_all_keys );
755 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
760 for( i = 0; i < p_src->i_event; i++ )
762 vlc_epg_event_t *p_evt = p_src->pp_event[i];
766 for( j = 0; j < p_dst->i_event; j++ )
768 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
773 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
778 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
781 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
782 p_copy->i_start = p_evt->i_start;
783 p_copy->i_duration = p_evt->i_duration;
784 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
785 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
786 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
787 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
791 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
793 /* Keep only 1 old event */
794 if( p_dst->p_current )
796 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
797 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
801 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
803 es_out_sys_t *p_sys = out->p_sys;
804 input_thread_t *p_input = p_sys->p_input;
805 es_out_pgrm_t *p_pgrm = NULL;
810 for( i = 0; i < p_sys->i_pgrm; i++ )
812 if( p_sys->pgrm[i]->i_id == i_group )
814 p_pgrm = p_sys->pgrm[i];
819 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
823 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
824 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
827 psz_cat = EsOutProgramGetMetaName( p_pgrm );
828 #ifdef HAVE_LOCALTIME_R
830 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
832 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
833 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
834 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
836 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
837 time_t t_start = (time_t)p_evt->i_start;
841 localtime_r( &t_start, &tm_start );
843 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
844 if( p_evt->psz_short_description || p_evt->psz_description )
845 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
847 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
848 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
850 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
852 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
856 /* Update now playing */
857 free( p_pgrm->psz_now_playing );
858 p_pgrm->psz_now_playing = NULL;
859 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
860 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
862 if( p_pgrm == p_sys->p_pgrm )
863 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
865 if( p_pgrm->psz_now_playing )
867 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
868 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
869 p_pgrm->psz_now_playing );
873 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
874 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
883 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
885 es_out_sys_t *p_sys = out->p_sys;
886 input_thread_t *p_input = p_sys->p_input;
888 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
889 es_out_pgrm_t *p_pgrm = NULL;
892 if( fmt->i_group < 0 )
894 msg_Err( p_input, "invalid group number" );
898 /* Search the program */
899 for( i = 0; i < p_sys->i_pgrm; i++ )
901 if( fmt->i_group == p_sys->pgrm[i]->i_id )
903 p_pgrm = p_sys->pgrm[i];
909 /* Create a new one */
910 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
913 /* Increase ref count for program */
918 fmt->i_id = out->p_sys->i_id;
919 es->i_id = fmt->i_id;
921 es_format_Copy( &es->fmt, fmt );
922 es->i_preroll_end = -1;
928 audio_replay_gain_t rg;
930 es->i_channel = p_sys->i_audio;
932 vlc_mutex_lock( &p_input->p->input.p_item->lock );
933 memset( &rg, 0, sizeof(rg) );
934 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
935 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
937 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
939 if( !es->fmt.audio_replay_gain.pb_peak[i] )
941 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
942 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
944 if( !es->fmt.audio_replay_gain.pb_gain[i] )
946 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
947 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
954 es->i_channel = p_sys->i_video;
955 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
956 vlc_ureduce( &es->fmt.video.i_frame_rate,
957 &es->fmt.video.i_frame_rate_base,
958 fmt->video.i_frame_rate,
959 fmt->video.i_frame_rate_base, 0 );
963 es->i_channel = p_sys->i_sub;
970 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
971 es->psz_language_code = LanguageGetCode( fmt->psz_language );
973 for( i = 0; i < 4; i++ )
974 es->pb_cc_present[i] = false;
975 es->p_master = false;
977 if( es->p_pgrm == p_sys->p_pgrm )
978 EsOutESVarUpdate( out, es, false );
980 /* Select it if needed */
981 EsOutSelect( out, es, false );
984 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
985 p_sys->i_id++; /* always incremented */
999 EsOutAddInfo( out, es );
1004 static bool EsIsSelected( es_out_id_t *es )
1008 bool b_decode = false;
1009 if( es->p_master->p_dec )
1011 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1012 if( i_channel != -1 )
1013 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1019 return es->p_dec != NULL;
1022 static void EsSelect( es_out_t *out, es_out_id_t *es )
1024 es_out_sys_t *p_sys = out->p_sys;
1025 input_thread_t *p_input = p_sys->p_input;
1027 const char *psz_var;
1029 if( EsIsSelected( es ) )
1031 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1038 if( !es->p_master->p_dec )
1041 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1042 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1047 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1049 if( !var_GetBool( p_input, "video" ) ||
1050 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
1052 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1057 else if( es->fmt.i_cat == AUDIO_ES )
1059 var_Get( p_input, "audio", &val );
1060 if( !var_GetBool( p_input, "audio" ) ||
1061 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
1063 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1068 if( es->fmt.i_cat == SPU_ES )
1070 var_Get( p_input, "spu", &val );
1071 if( !var_GetBool( p_input, "spu" ) ||
1072 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
1074 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1080 es->i_preroll_end = -1;
1081 es->p_dec = input_DecoderNew( p_input, &es->fmt, false );
1082 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1086 if( es->fmt.i_cat == VIDEO_ES )
1087 psz_var = "video-es";
1088 else if( es->fmt.i_cat == AUDIO_ES )
1089 psz_var = "audio-es";
1090 else if( es->fmt.i_cat == SPU_ES )
1095 /* Mark it as selected */
1096 val.i_int = es->i_id;
1097 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1099 var_SetBool( p_sys->p_input, "intf-change", true );
1102 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1104 es_out_sys_t *p_sys = out->p_sys;
1105 input_thread_t *p_input = p_sys->p_input;
1107 const char *psz_var;
1109 if( !EsIsSelected( es ) )
1111 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1117 if( es->p_master->p_dec )
1119 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1120 if( i_channel != -1 )
1121 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1126 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1128 for( i = 0; i < 4; i++ )
1130 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1133 if( i_spu_id == es->pp_cc_es[i]->i_id )
1135 /* Force unselection of the CC */
1137 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1139 var_SetBool( p_sys->p_input, "intf-change", true );
1141 EsOutDel( out, es->pp_cc_es[i] );
1143 es->pb_cc_present[i] = false;
1145 input_DecoderDelete( es->p_dec );
1153 if( es->p_dec == NULL )
1155 if( es->fmt.i_cat == VIDEO_ES )
1156 psz_var = "video-es";
1157 else if( es->fmt.i_cat == AUDIO_ES )
1158 psz_var = "audio-es";
1159 else if( es->fmt.i_cat == SPU_ES )
1164 /* Mark it as unselected */
1166 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1168 var_SetBool( p_sys->p_input, "intf-change", true );
1172 * Select an ES given the current mode
1173 * XXX: you need to take a the lock before (stream.stream_lock)
1175 * \param out The es_out structure
1176 * \param es es_out_id structure
1177 * \param b_force ...
1180 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1182 es_out_sys_t *p_sys = out->p_sys;
1184 int i_cat = es->fmt.i_cat;
1186 if( !p_sys->b_active ||
1187 ( !b_force && es->fmt.i_priority < 0 ) )
1192 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1194 if( !EsIsSelected( es ) )
1195 EsSelect( out, es );
1197 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1201 var_Get( p_sys->p_input, "programs", &val );
1202 for ( i = 0; i < val.p_list->i_count; i++ )
1204 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1206 if( !EsIsSelected( es ) )
1207 EsSelect( out, es );
1211 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1213 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1217 if( es->p_pgrm != p_sys->p_pgrm )
1220 if( i_cat == AUDIO_ES )
1222 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1223 es->psz_language_code );
1225 if( p_sys->p_es_audio &&
1226 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1228 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1229 p_sys->p_es_audio->psz_language_code );
1231 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1233 i_wanted = es->i_channel;
1237 /* Select audio if (no audio selected yet)
1238 * - no audio-language
1239 * - no audio code for the ES
1240 * - audio code in the requested list */
1242 !strcmp( es->psz_language_code, "??" ) ||
1243 !p_sys->ppsz_audio_language )
1244 i_wanted = es->i_channel;
1247 if( p_sys->i_audio_last >= 0 )
1248 i_wanted = p_sys->i_audio_last;
1250 if( p_sys->i_audio_id >= 0 )
1252 if( es->i_id == p_sys->i_audio_id )
1253 i_wanted = es->i_channel;
1258 else if( i_cat == SPU_ES )
1260 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1261 es->psz_language_code );
1263 if( p_sys->p_es_sub &&
1264 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1266 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1267 p_sys->p_es_sub->psz_language_code );
1269 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1270 idx1, es->psz_language_code, idx2,
1271 p_sys->p_es_sub->psz_language_code );
1273 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1275 /* We found a SPU that matches our language request */
1276 i_wanted = es->i_channel;
1278 else if( idx1 >= 0 )
1280 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1281 idx1, es->psz_language_code );
1283 i_wanted = es->i_channel;
1285 else if( p_sys->i_default_sub_id >= 0 )
1287 if( es->i_id == p_sys->i_default_sub_id )
1288 i_wanted = es->i_channel;
1291 if( p_sys->i_sub_last >= 0 )
1292 i_wanted = p_sys->i_sub_last;
1294 if( p_sys->i_sub_id >= 0 )
1296 if( es->i_id == p_sys->i_sub_id )
1297 i_wanted = es->i_channel;
1302 else if( i_cat == VIDEO_ES )
1304 i_wanted = es->i_channel;
1307 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1308 EsSelect( out, es );
1311 /* FIXME TODO handle priority here */
1312 if( EsIsSelected( es ) )
1314 if( i_cat == AUDIO_ES )
1316 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1317 p_sys->p_es_audio &&
1318 p_sys->p_es_audio != es &&
1319 EsIsSelected( p_sys->p_es_audio ) )
1321 EsUnselect( out, p_sys->p_es_audio, false );
1323 p_sys->p_es_audio = es;
1325 else if( i_cat == SPU_ES )
1327 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1329 p_sys->p_es_sub != es &&
1330 EsIsSelected( p_sys->p_es_sub ) )
1332 EsUnselect( out, p_sys->p_es_sub, false );
1334 p_sys->p_es_sub = es;
1336 else if( i_cat == VIDEO_ES )
1338 p_sys->p_es_video = es;
1344 * Send a block for the given es_out
1346 * \param out the es_out to send from
1347 * \param es the es_out_id
1348 * \param p_block the data block to send
1350 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1352 es_out_sys_t *p_sys = out->p_sys;
1353 input_thread_t *p_input = p_sys->p_input;
1354 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1358 if( es->fmt.i_cat == AUDIO_ES )
1359 i_delay = p_sys->i_audio_delay;
1360 else if( es->fmt.i_cat == SPU_ES )
1361 i_delay = p_sys->i_spu_delay;
1365 if( libvlc_stats (p_input) )
1367 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1368 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1369 p_block->i_buffer, &i_total );
1370 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1371 (float)i_total, NULL );
1372 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1375 /* Mark preroll blocks */
1376 if( es->i_preroll_end >= 0 )
1378 int64_t i_date = p_block->i_pts;
1380 i_date = p_block->i_dts;
1382 if( i_date < es->i_preroll_end )
1383 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1385 es->i_preroll_end = -1;
1388 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1390 p_block->i_dts += i_delay;
1392 else if( p_block->i_dts > 0 )
1395 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1397 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1399 p_block->i_pts += i_delay;
1401 else if( p_block->i_pts > 0 )
1404 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1406 if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
1407 es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1409 mtime_t current_date = mdate();
1411 || p_block->i_pts > current_date + 10000000
1412 || current_date > p_block->i_pts )
1414 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1415 * for teletext streams. */
1416 p_block->i_pts = current_date + 400000
1417 + p_input->i_pts_delay + i_delay;
1421 p_block->i_rate = p_sys->i_rate;
1423 /* TODO handle mute */
1425 ( es->fmt.i_cat != AUDIO_ES ||
1426 ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1427 p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1430 bool b_cc_new = false;
1432 input_DecoderDecode( es->p_dec, p_block );
1434 /* Check CC status */
1435 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1436 for( i = 0; i < 4; i++ )
1438 static const vlc_fourcc_t fcc[4] = {
1439 VLC_FOURCC('c', 'c', '1', ' '),
1440 VLC_FOURCC('c', 'c', '2', ' '),
1441 VLC_FOURCC('c', 'c', '3', ' '),
1442 VLC_FOURCC('c', 'c', '4', ' '),
1444 static const char *ppsz_description[4] = {
1445 N_("Closed captions 1"),
1446 N_("Closed captions 2"),
1447 N_("Closed captions 3"),
1448 N_("Closed captions 4"),
1452 if( es->pb_cc_present[i] || !pb_cc[i] )
1454 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1456 es_format_Init( &fmt, SPU_ES, fcc[i] );
1457 fmt.i_group = es->fmt.i_group;
1458 fmt.psz_description = strdup( _(ppsz_description[i] ) );
1459 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1460 es->pp_cc_es[i]->p_master = es;
1461 es_format_Clean( &fmt );
1464 es->pb_cc_present[i] = true;
1468 var_SetBool( p_sys->p_input, "intf-change", true );
1472 block_Release( p_block );
1478 /*****************************************************************************
1480 *****************************************************************************/
1481 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1483 es_out_sys_t *p_sys = out->p_sys;
1484 bool b_reselect = false;
1487 /* We don't try to reselect */
1490 while( !out->p_sys->p_input->b_die && es->p_dec )
1492 if( input_DecoderEmpty( es->p_dec ) )
1496 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1499 if( es->p_pgrm == p_sys->p_pgrm )
1500 EsOutESVarUpdate( out, es, true );
1502 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1505 if( es->p_pgrm->i_es == 0 )
1507 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1510 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1511 p_sys->p_es_sub == es ) b_reselect = true;
1513 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1514 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1515 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1517 switch( es->fmt.i_cat )
1530 /* Re-select another track when needed */
1532 for( i = 0; i < p_sys->i_es; i++ )
1534 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1535 EsOutSelect( out, p_sys->es[i], false );
1538 free( es->psz_language );
1539 free( es->psz_language_code );
1541 es_format_Clean( &es->fmt );
1547 * Control query handler
1549 * \param out the es_out to control
1550 * \param i_query A es_out query as defined in include/ninput.h
1551 * \param args a variable list of arguments for the query
1552 * \return VLC_SUCCESS or an error code
1554 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1556 es_out_sys_t *p_sys = out->p_sys;
1564 case ES_OUT_SET_ES_STATE:
1565 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1566 b = (bool) va_arg( args, int );
1567 if( b && !EsIsSelected( es ) )
1569 EsSelect( out, es );
1570 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1572 else if( !b && EsIsSelected( es ) )
1574 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1579 case ES_OUT_GET_ES_STATE:
1580 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1581 pb = (bool*) va_arg( args, bool * );
1583 *pb = EsIsSelected( es );
1586 case ES_OUT_SET_ACTIVE:
1588 b = (bool) va_arg( args, int );
1589 p_sys->b_active = b;
1592 var_SetBool( p_sys->p_input, "intf-change", true );
1596 case ES_OUT_GET_ACTIVE:
1597 pb = (bool*) va_arg( args, bool * );
1598 *pb = p_sys->b_active;
1601 case ES_OUT_SET_MODE:
1602 i = (int) va_arg( args, int );
1603 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1604 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1608 /* Reapply policy mode */
1609 for( i = 0; i < p_sys->i_es; i++ )
1611 if( EsIsSelected( p_sys->es[i] ) )
1613 EsUnselect( out, p_sys->es[i],
1614 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1617 for( i = 0; i < p_sys->i_es; i++ )
1619 EsOutSelect( out, p_sys->es[i], false );
1623 return VLC_EGENERIC;
1625 case ES_OUT_GET_MODE:
1626 pi = (int*) va_arg( args, int* );
1627 *pi = p_sys->i_mode;
1631 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1632 /* Special case NULL, NULL+i_cat */
1635 for( i = 0; i < p_sys->i_es; i++ )
1637 if( EsIsSelected( p_sys->es[i] ) )
1638 EsUnselect( out, p_sys->es[i],
1639 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1642 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1644 for( i = 0; i < p_sys->i_es; i++ )
1646 if( p_sys->es[i]->fmt.i_cat == AUDIO_ES &&
1647 EsIsSelected( p_sys->es[i] ) )
1648 EsUnselect( out, p_sys->es[i],
1649 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1652 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1654 for( i = 0; i < p_sys->i_es; i++ )
1656 if( p_sys->es[i]->fmt.i_cat == VIDEO_ES &&
1657 EsIsSelected( p_sys->es[i] ) )
1658 EsUnselect( out, p_sys->es[i],
1659 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1662 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1664 for( i = 0; i < p_sys->i_es; i++ )
1666 if( p_sys->es[i]->fmt.i_cat == SPU_ES &&
1667 EsIsSelected( p_sys->es[i] ) )
1668 EsUnselect( out, p_sys->es[i],
1669 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1674 for( i = 0; i < p_sys->i_es; i++ )
1676 if( es == p_sys->es[i] )
1678 EsOutSelect( out, es, true );
1684 /* FIXME: we don't want to depend on the playlist */
1685 playlist_t * p_playlist = vlc_object_find( p_sys->p_input,
1686 VLC_OBJECT_PLAYLIST, FIND_PARENT );
1690 p_playlist->gc_date = mdate();
1691 vlc_object_signal_unlocked( p_playlist );
1693 vlc_object_release( p_playlist );
1698 case ES_OUT_SET_DEFAULT:
1700 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1704 /*p_sys->i_default_video_id = -1;*/
1705 /*p_sys->i_default_audio_id = -1;*/
1706 p_sys->i_default_sub_id = -1;
1708 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1710 /*p_sys->i_default_video_id = -1;*/
1712 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1714 /*p_sys->i_default_audio_id = -1;*/
1716 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1718 p_sys->i_default_sub_id = -1;
1722 /*if( es->fmt.i_cat == VIDEO_ES )
1723 p_sys->i_default_video_id = es->i_id;
1725 if( es->fmt.i_cat == AUDIO_ES )
1726 p_sys->i_default_audio_id = es->i_id;
1728 if( es->fmt.i_cat == SPU_ES )
1729 p_sys->i_default_sub_id = es->i_id;
1734 case ES_OUT_SET_PCR:
1735 case ES_OUT_SET_GROUP_PCR:
1737 es_out_pgrm_t *p_pgrm = NULL;
1741 if( i_query == ES_OUT_SET_PCR )
1743 p_pgrm = p_sys->p_pgrm;
1748 i_group = (int)va_arg( args, int );
1749 for( i = 0; i < p_sys->i_pgrm; i++ )
1751 if( p_sys->pgrm[i]->i_id == i_group )
1753 p_pgrm = p_sys->pgrm[i];
1758 if( p_pgrm == NULL )
1759 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1761 i_pcr = (int64_t)va_arg( args, int64_t );
1762 /* search program */
1763 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1767 case ES_OUT_RESET_PCR:
1768 for( i = 0; i < p_sys->i_pgrm; i++ )
1769 input_ClockResetPCR( &p_sys->pgrm[i]->clock );
1775 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1776 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1777 *pi_ts = input_ClockGetTS( p_sys->p_input,
1778 &p_sys->p_pgrm->clock, i_ts );
1781 return VLC_EGENERIC;
1783 case ES_OUT_GET_GROUP:
1784 pi = (int*) va_arg( args, int* );
1786 *pi = p_sys->p_pgrm->i_id;
1788 *pi = -1; /* FIXME */
1791 case ES_OUT_SET_GROUP:
1794 i = (int) va_arg( args, int );
1795 for( j = 0; j < p_sys->i_pgrm; j++ )
1797 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1798 if( p_pgrm->i_id == i )
1800 EsOutProgramSelect( out, p_pgrm );
1804 return VLC_EGENERIC;
1807 case ES_OUT_SET_FMT:
1809 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1810 * to update the p_extra data */
1812 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1813 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1814 if( es == NULL ) return VLC_EGENERIC;
1816 if( p_fmt->i_extra )
1818 es->fmt.i_extra = p_fmt->i_extra;
1819 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1820 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1822 if( !es->p_dec ) return VLC_SUCCESS;
1825 input_DecoderDelete( es->p_dec );
1826 es->p_dec = input_DecoderNew( p_sys->p_input,
1830 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1831 es->p_dec->fmt_in.p_extra =
1832 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1833 memcpy( es->p_dec->fmt_in.p_extra,
1834 p_fmt->p_extra, p_fmt->i_extra );
1841 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1845 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1846 i_date = (int64_t)va_arg( args, int64_t );
1848 if( !es || !es->p_dec )
1849 return VLC_EGENERIC;
1851 /* XXX We should call input_ClockGetTS but PCR has been reseted
1852 * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1853 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1854 es->i_preroll_end = i_date;
1858 case ES_OUT_SET_GROUP_META:
1860 int i_group = (int)va_arg( args, int );
1861 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1863 EsOutProgramMeta( out, i_group, p_meta );
1866 case ES_OUT_SET_GROUP_EPG:
1868 int i_group = (int)va_arg( args, int );
1869 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1871 EsOutProgramEpg( out, i_group, p_epg );
1874 case ES_OUT_DEL_GROUP:
1876 int i_group = (int)va_arg( args, int );
1878 return EsOutProgramDel( out, i_group );
1882 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1883 return VLC_EGENERIC;
1887 /****************************************************************************
1888 * LanguageGetName: try to expend iso639 into plain name
1889 ****************************************************************************/
1890 static char *LanguageGetName( const char *psz_code )
1892 const iso639_lang_t *pl;
1894 if( psz_code == NULL )
1896 return strdup( "" );
1899 if( strlen( psz_code ) == 2 )
1901 pl = GetLang_1( psz_code );
1903 else if( strlen( psz_code ) == 3 )
1905 pl = GetLang_2B( psz_code );
1906 if( !strcmp( pl->psz_iso639_1, "??" ) )
1908 pl = GetLang_2T( psz_code );
1913 return strdup( psz_code );
1916 if( !strcmp( pl->psz_iso639_1, "??" ) )
1918 return strdup( psz_code );
1922 if( *pl->psz_native_name )
1924 return strdup( pl->psz_native_name );
1926 return strdup( pl->psz_eng_name );
1930 /* Get a 2 char code */
1931 static char *LanguageGetCode( const char *psz_lang )
1933 const iso639_lang_t *pl;
1935 if( psz_lang == NULL || *psz_lang == '\0' )
1936 return strdup("??");
1938 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1940 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1941 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1942 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1943 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1944 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1948 if( pl->psz_iso639_1 != NULL )
1949 return strdup( pl->psz_iso639_1 );
1951 return strdup("??");
1954 static char **LanguageSplit( const char *psz_langs )
1961 if( psz_langs == NULL ) return NULL;
1963 psz_parser = psz_dup = strdup(psz_langs);
1965 while( psz_parser && *psz_parser )
1970 psz = strchr(psz_parser, ',' );
1971 if( psz ) *psz++ = '\0';
1973 if( !strcmp( psz_parser, "any" ) )
1975 TAB_APPEND( i_psz, ppsz, strdup("any") );
1979 psz_code = LanguageGetCode( psz_parser );
1980 if( strcmp( psz_code, "??" ) )
1982 TAB_APPEND( i_psz, ppsz, psz_code );
1995 TAB_APPEND( i_psz, ppsz, NULL );
2002 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2006 if( !ppsz_langs || !psz_lang ) return -1;
2008 for( i = 0; ppsz_langs[i]; i++ )
2010 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2011 !strcasecmp( ppsz_langs[i], "any" ) )
2020 /****************************************************************************
2022 * - add meta info to the playlist item
2023 ****************************************************************************/
2024 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2026 es_out_sys_t *p_sys = out->p_sys;
2027 input_thread_t *p_input = p_sys->p_input;
2028 es_format_t *fmt = &es->fmt;
2032 /* Add stream info */
2033 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
2035 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2036 "%.4s", (char*)&fmt->i_codec );
2038 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2039 "%s", es->psz_language );
2041 /* Add information */
2042 switch( fmt->i_cat )
2045 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2046 _("Type"), _("Audio") );
2048 if( fmt->audio.i_channels > 0 )
2049 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2050 "%u", fmt->audio.i_channels );
2052 if( fmt->audio.i_rate > 0 )
2054 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2055 _("%u Hz"), fmt->audio.i_rate );
2056 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2059 if( fmt->audio.i_bitspersample > 0 )
2060 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2061 _("Bits per sample"), "%u",
2062 fmt->audio.i_bitspersample );
2064 if( fmt->i_bitrate > 0 )
2066 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2067 _("%u kb/s"), fmt->i_bitrate / 1000 );
2068 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2073 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2074 _("Type"), _("Video") );
2076 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2077 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2078 _("Resolution"), "%ux%u",
2079 fmt->video.i_width, fmt->video.i_height );
2081 if( fmt->video.i_visible_width > 0 &&
2082 fmt->video.i_visible_height > 0 )
2083 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2084 _("Display resolution"), "%ux%u",
2085 fmt->video.i_visible_width,
2086 fmt->video.i_visible_height);
2087 if( fmt->video.i_frame_rate > 0 &&
2088 fmt->video.i_frame_rate_base > 0 )
2090 div = lldiv( (float)fmt->video.i_frame_rate /
2091 fmt->video.i_frame_rate_base * 1000000,
2093 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2094 _("Frame rate"), "%"PRId64".%06u",
2095 div.quot, (unsigned int )div.rem );
2100 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2101 _("Type"), _("Subtitle") );