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"
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 */
59 vlc_bool_t b_selected;
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 vlc_bool_t 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, vlc_bool_t 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 vlc_bool_t 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, vlc_bool_t 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 ? VLC_TRUE : VLC_FALSE);
193 p_sys->p_input = p_input;
195 p_sys->b_active = VLC_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 msg_Dbg( out, "deleting es out" );
275 for( i = 0; i < p_sys->i_es; i++ )
277 if( p_sys->es[i]->p_dec )
279 input_DecoderDelete( p_sys->es[i]->p_dec );
281 free( p_sys->es[i]->psz_language );
282 free( p_sys->es[i]->psz_language_code );
283 es_format_Clean( &p_sys->es[i]->fmt );
285 free( p_sys->es[i] );
287 if( p_sys->ppsz_audio_language )
289 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
290 free( p_sys->ppsz_audio_language[i] );
291 free( p_sys->ppsz_audio_language );
293 if( p_sys->ppsz_sub_language )
295 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
296 free( p_sys->ppsz_sub_language[i] );
297 free( p_sys->ppsz_sub_language );
302 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
303 for( i = 0; i < p_sys->i_pgrm; i++ )
305 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
306 free( p_pgrm->psz_now_playing );
307 free( p_pgrm->psz_publisher );
308 free( p_pgrm->psz_name );
310 vlc_epg_Delete( p_pgrm->p_epg );
314 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
320 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
325 /* Special HACK, -i_id is tha cat of the stream */
326 return (es_out_id_t*)((uint8_t*)NULL-i_id);
329 for( i = 0; i < out->p_sys->i_es; i++ )
331 if( out->p_sys->es[i]->i_id == i_id )
332 return out->p_sys->es[i];
337 static void EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_flush, vlc_bool_t b_audio )
339 es_out_sys_t *p_sys = out->p_sys;
342 for( i = 0; i < p_sys->i_es; i++ )
344 es_out_id_t *es = p_sys->es[i];
346 /* Send a dummy block to let decoder know that
347 * there is a discontinuity */
348 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
349 input_DecoderDiscontinuity( es->p_dec, b_flush );
352 void input_EsOutChangeRate( es_out_t *out, int i_rate )
354 es_out_sys_t *p_sys = out->p_sys;
357 p_sys->i_rate = i_rate;
358 EsOutDiscontinuity( out, VLC_FALSE, VLC_FALSE );
360 for( i = 0; i < p_sys->i_pgrm; i++ )
361 input_ClockSetRate( &p_sys->pgrm[i]->clock, i_rate );
364 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
366 es_out_sys_t *p_sys = out->p_sys;
368 if( i_cat == AUDIO_ES )
369 p_sys->i_audio_delay = i_delay;
370 else if( i_cat == SPU_ES )
371 p_sys->i_spu_delay = i_delay;
373 void input_EsOutChangeState( es_out_t *out )
375 es_out_sys_t *p_sys = out->p_sys;
376 input_thread_t *p_input = p_sys->p_input;
378 if( p_input->i_state == PAUSE_S )
380 /* Send discontinuity to decoders (it will allow them to flush
381 * * if implemented */
382 EsOutDiscontinuity( out, VLC_FALSE, VLC_FALSE );
386 /* Out of pause, reset pcr */
387 es_out_Control( out, ES_OUT_RESET_PCR );
390 void input_EsOutChangePosition( es_out_t *out )
392 //es_out_sys_t *p_sys = out->p_sys;
394 es_out_Control( out, ES_OUT_RESET_PCR );
395 EsOutDiscontinuity( out, VLC_TRUE, VLC_FALSE );
398 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
400 es_out_sys_t *p_sys = out->p_sys;
403 for( i = 0; i < p_sys->i_es; i++ )
405 es_out_id_t *es = p_sys->es[i];
407 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
413 /*****************************************************************************
415 *****************************************************************************/
416 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
417 vlc_bool_t b_delete )
419 es_out_sys_t *p_sys = out->p_sys;
420 input_thread_t *p_input = p_sys->p_input;
421 vlc_value_t val, text;
425 if( fmt->i_cat == AUDIO_ES )
426 psz_var = "audio-es";
427 else if( fmt->i_cat == VIDEO_ES )
428 psz_var = "video-es";
429 else if( fmt->i_cat == SPU_ES )
437 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
438 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
442 /* Get the number of ES already added */
443 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
448 /* First one, we need to add the "Disable" choice */
449 val2.i_int = -1; text.psz_string = _("Disable");
450 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
454 /* Take care of the ES description */
455 if( fmt->psz_description && *fmt->psz_description )
457 if( psz_language && *psz_language )
459 text.psz_string = malloc( strlen( fmt->psz_description) +
460 strlen( psz_language ) + 10 );
461 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
464 else text.psz_string = strdup( fmt->psz_description );
468 if( psz_language && *psz_language )
471 text.psz_string = malloc( strlen( _("Track %i") )+
472 strlen( psz_language ) + 30 );
473 asprintf( &temp, _("Track %i"), val.i_int );
474 sprintf( text.psz_string, "%s - [%s]", temp, psz_language );
479 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
480 sprintf( text.psz_string, _("Track %i"), val.i_int );
485 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
487 free( text.psz_string );
489 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
492 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
493 vlc_bool_t b_delete )
495 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
498 /* EsOutProgramSelect:
499 * Select a program and update the object variable
501 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
503 es_out_sys_t *p_sys = out->p_sys;
504 input_thread_t *p_input = p_sys->p_input;
508 if( p_sys->p_pgrm == p_pgrm )
509 return; /* Nothing to do */
513 es_out_pgrm_t *old = p_sys->p_pgrm;
514 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
516 for( i = 0; i < p_sys->i_es; i++ )
518 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
519 p_sys->i_mode != ES_OUT_MODE_ALL )
520 EsUnselect( out, p_sys->es[i], VLC_TRUE );
523 p_sys->p_es_audio = NULL;
524 p_sys->p_es_sub = NULL;
525 p_sys->p_es_video = NULL;
528 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
530 /* Mark it selected */
531 p_pgrm->b_selected = VLC_TRUE;
533 /* Switch master stream */
534 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
536 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
538 p_pgrm->clock.b_master = VLC_TRUE;
539 p_sys->p_pgrm = p_pgrm;
541 /* Update "program" */
542 val.i_int = p_pgrm->i_id;
543 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
546 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
547 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
548 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
549 for( i = 0; i < p_sys->i_es; i++ )
551 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
552 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
553 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
556 /* Update now playing */
557 input_item_SetNowPlaying( p_input->p->input.p_item,
558 p_pgrm->psz_now_playing );
559 input_item_SetPublisher( p_input->p->input.p_item,
560 p_pgrm->psz_publisher );
562 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
568 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
570 es_out_sys_t *p_sys = out->p_sys;
571 input_thread_t *p_input = p_sys->p_input;
574 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
577 p_pgrm->i_id = i_group;
579 p_pgrm->b_selected = VLC_FALSE;
580 p_pgrm->psz_name = NULL;
581 p_pgrm->psz_now_playing = NULL;
582 p_pgrm->psz_publisher = NULL;
583 p_pgrm->p_epg = NULL;
584 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average, p_sys->i_rate );
587 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
589 /* Update "program" variable */
591 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
593 if( i_group == var_GetInteger( p_input, "program" ) )
595 EsOutProgramSelect( out, p_pgrm );
599 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
607 static int EsOutProgramDel( es_out_t *out, int i_group )
609 es_out_sys_t *p_sys = out->p_sys;
610 input_thread_t *p_input = p_sys->p_input;
611 es_out_pgrm_t *p_pgrm = NULL;
615 for( i = 0; i < p_sys->i_pgrm; i++ )
617 if( p_sys->pgrm[i]->i_id == i_group )
619 p_pgrm = p_sys->pgrm[i];
629 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
630 i_group, p_pgrm->i_es );
634 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
636 /* If program is selected we need to unselect it */
637 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
639 free( p_pgrm->psz_name );
640 free( p_pgrm->psz_now_playing );
641 free( p_pgrm->psz_publisher );
643 vlc_epg_Delete( p_pgrm->p_epg );
646 /* Update "program" variable */
648 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
650 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
657 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
660 if( p_pgrm->psz_name )
661 asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
663 asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
667 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
669 es_out_sys_t *p_sys = out->p_sys;
670 es_out_pgrm_t *p_pgrm = NULL;
671 input_thread_t *p_input = p_sys->p_input;
673 const char *psz_title = NULL;
674 const char *psz_provider = NULL;
677 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
679 /* Check against empty meta data (empty for what we handle) */
680 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
681 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
682 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
683 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
688 for( i = 0; i < p_sys->i_pgrm; i++ )
690 if( p_sys->pgrm[i]->i_id == i_group )
692 p_pgrm = p_sys->pgrm[i];
697 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
700 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
701 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
703 /* Update the description text of the program */
704 if( psz_title && *psz_title )
709 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
711 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
713 /* Remove old entries */
714 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
715 /* TODO update epg name */
718 free( p_pgrm->psz_name );
719 p_pgrm->psz_name = strdup( psz_title );
721 /* ugly but it works */
723 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
725 if( psz_provider && *psz_provider )
727 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
728 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
729 free( text.psz_string );
733 text.psz_string = (char *)psz_title;
734 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
738 psz_cat = EsOutProgramGetMetaName( p_pgrm );
741 if( p_sys->p_pgrm == p_pgrm )
742 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
743 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
745 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
746 for( i = 0; ppsz_all_keys[i]; i++ )
748 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
749 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
750 free( ppsz_all_keys[i] );
752 free( ppsz_all_keys );
757 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
762 for( i = 0; i < p_src->i_event; i++ )
764 vlc_epg_event_t *p_evt = p_src->pp_event[i];
765 vlc_bool_t b_add = VLC_TRUE;
768 for( j = 0; j < p_dst->i_event; j++ )
770 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
775 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
780 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
783 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
784 p_copy->i_start = p_evt->i_start;
785 p_copy->i_duration = p_evt->i_duration;
786 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
787 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
788 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
789 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
793 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
795 /* Keep only 1 old event */
796 if( p_dst->p_current )
798 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
799 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
803 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
805 es_out_sys_t *p_sys = out->p_sys;
806 input_thread_t *p_input = p_sys->p_input;
807 es_out_pgrm_t *p_pgrm = NULL;
812 for( i = 0; i < p_sys->i_pgrm; i++ )
814 if( p_sys->pgrm[i]->i_id == i_group )
816 p_pgrm = p_sys->pgrm[i];
821 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
825 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
826 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
829 psz_cat = EsOutProgramGetMetaName( p_pgrm );
830 #ifdef HAVE_LOCALTIME_R
832 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
834 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
835 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
836 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
838 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
839 time_t t_start = (time_t)p_evt->i_start;
843 localtime_r( &t_start, &tm_start );
845 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
846 if( p_evt->psz_short_description || p_evt->psz_description )
847 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
849 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
850 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
852 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
854 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
858 /* Update now playing */
859 free( p_pgrm->psz_now_playing );
860 p_pgrm->psz_now_playing = NULL;
861 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
862 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
864 if( p_pgrm == p_sys->p_pgrm )
865 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
867 if( p_pgrm->psz_now_playing )
869 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
870 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
871 p_pgrm->psz_now_playing );
875 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
876 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
885 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
887 es_out_sys_t *p_sys = out->p_sys;
888 input_thread_t *p_input = p_sys->p_input;
890 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
891 es_out_pgrm_t *p_pgrm = NULL;
894 if( fmt->i_group < 0 )
896 msg_Err( p_input, "invalid group number" );
900 /* Search the program */
901 for( i = 0; i < p_sys->i_pgrm; i++ )
903 if( fmt->i_group == p_sys->pgrm[i]->i_id )
905 p_pgrm = p_sys->pgrm[i];
911 /* Create a new one */
912 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
915 /* Increase ref count for program */
920 fmt->i_id = out->p_sys->i_id;
921 es->i_id = fmt->i_id;
923 es_format_Copy( &es->fmt, fmt );
924 es->i_preroll_end = -1;
930 audio_replay_gain_t rg;
932 es->i_channel = p_sys->i_audio;
934 vlc_mutex_lock( &p_input->p->input.p_item->lock );
935 memset( &rg, 0, sizeof(rg) );
936 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
937 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
939 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
941 if( !es->fmt.audio_replay_gain.pb_peak[i] )
943 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
944 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
946 if( !es->fmt.audio_replay_gain.pb_gain[i] )
948 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
949 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
956 es->i_channel = p_sys->i_video;
957 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
958 vlc_ureduce( &es->fmt.video.i_frame_rate,
959 &es->fmt.video.i_frame_rate_base,
960 fmt->video.i_frame_rate,
961 fmt->video.i_frame_rate_base, 0 );
965 es->i_channel = p_sys->i_sub;
972 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
973 es->psz_language_code = LanguageGetCode( fmt->psz_language );
975 for( i = 0; i < 4; i++ )
976 es->pb_cc_present[i] = VLC_FALSE;
977 es->p_master = VLC_FALSE;
979 if( es->p_pgrm == p_sys->p_pgrm )
980 EsOutESVarUpdate( out, es, VLC_FALSE );
982 /* Select it if needed */
983 EsOutSelect( out, es, VLC_FALSE );
986 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
987 p_sys->i_id++; /* always incremented */
1001 EsOutAddInfo( out, es );
1006 static vlc_bool_t EsIsSelected( es_out_id_t *es )
1010 vlc_bool_t b_decode = VLC_FALSE;
1011 if( es->p_master->p_dec )
1013 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1014 if( i_channel != -1 )
1015 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1021 return es->p_dec != NULL;
1024 static void EsSelect( es_out_t *out, es_out_id_t *es )
1026 es_out_sys_t *p_sys = out->p_sys;
1027 input_thread_t *p_input = p_sys->p_input;
1029 const char *psz_var;
1031 if( EsIsSelected( es ) )
1033 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1040 if( !es->p_master->p_dec )
1043 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1044 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, VLC_TRUE, i_channel ) )
1049 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1051 if( !var_GetBool( p_input, "video" ) ||
1052 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
1054 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1059 else if( es->fmt.i_cat == AUDIO_ES )
1061 var_Get( p_input, "audio", &val );
1062 if( !var_GetBool( p_input, "audio" ) ||
1063 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
1065 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1070 if( es->fmt.i_cat == SPU_ES )
1072 var_Get( p_input, "spu", &val );
1073 if( !var_GetBool( p_input, "spu" ) ||
1074 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
1076 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1082 es->i_preroll_end = -1;
1083 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
1084 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1088 if( es->fmt.i_cat == VIDEO_ES )
1089 psz_var = "video-es";
1090 else if( es->fmt.i_cat == AUDIO_ES )
1091 psz_var = "audio-es";
1092 else if( es->fmt.i_cat == SPU_ES )
1097 /* Mark it as selected */
1098 val.i_int = es->i_id;
1099 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1101 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1104 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
1106 es_out_sys_t *p_sys = out->p_sys;
1107 input_thread_t *p_input = p_sys->p_input;
1109 const char *psz_var;
1111 if( !EsIsSelected( es ) )
1113 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1119 if( es->p_master->p_dec )
1121 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1122 if( i_channel != -1 )
1123 input_DecoderSetCcState( es->p_master->p_dec, VLC_FALSE, i_channel );
1128 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1130 for( i = 0; i < 4; i++ )
1132 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1135 if( i_spu_id == es->pp_cc_es[i]->i_id )
1137 /* Force unselection of the CC */
1139 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1141 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1143 EsOutDel( out, es->pp_cc_es[i] );
1145 es->pb_cc_present[i] = VLC_FALSE;
1147 input_DecoderDelete( es->p_dec );
1155 if( es->p_dec == NULL )
1157 if( es->fmt.i_cat == VIDEO_ES )
1158 psz_var = "video-es";
1159 else if( es->fmt.i_cat == AUDIO_ES )
1160 psz_var = "audio-es";
1161 else if( es->fmt.i_cat == SPU_ES )
1166 /* Mark it as unselected */
1168 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1170 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1174 * Select an ES given the current mode
1175 * XXX: you need to take a the lock before (stream.stream_lock)
1177 * \param out The es_out structure
1178 * \param es es_out_id structure
1179 * \param b_force ...
1182 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
1184 es_out_sys_t *p_sys = out->p_sys;
1186 int i_cat = es->fmt.i_cat;
1188 if( !p_sys->b_active ||
1189 ( !b_force && es->fmt.i_priority < 0 ) )
1194 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1196 if( !EsIsSelected( es ) )
1197 EsSelect( out, es );
1199 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1203 var_Get( p_sys->p_input, "programs", &val );
1204 for ( i = 0; i < val.p_list->i_count; i++ )
1206 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1208 if( !EsIsSelected( es ) )
1209 EsSelect( out, es );
1213 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1215 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1219 if( es->p_pgrm != p_sys->p_pgrm )
1222 if( i_cat == AUDIO_ES )
1224 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1225 es->psz_language_code );
1227 if( p_sys->p_es_audio &&
1228 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1230 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1231 p_sys->p_es_audio->psz_language_code );
1233 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1235 i_wanted = es->i_channel;
1239 /* Select audio if (no audio selected yet)
1240 * - no audio-language
1241 * - no audio code for the ES
1242 * - audio code in the requested list */
1244 !strcmp( es->psz_language_code, "??" ) ||
1245 !p_sys->ppsz_audio_language )
1246 i_wanted = es->i_channel;
1249 if( p_sys->i_audio_last >= 0 )
1250 i_wanted = p_sys->i_audio_last;
1252 if( p_sys->i_audio_id >= 0 )
1254 if( es->i_id == p_sys->i_audio_id )
1255 i_wanted = es->i_channel;
1260 else if( i_cat == SPU_ES )
1262 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1263 es->psz_language_code );
1265 if( p_sys->p_es_sub &&
1266 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1268 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1269 p_sys->p_es_sub->psz_language_code );
1271 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1272 idx1, es->psz_language_code, idx2,
1273 p_sys->p_es_sub->psz_language_code );
1275 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1277 /* We found a SPU that matches our language request */
1278 i_wanted = es->i_channel;
1280 else if( idx1 >= 0 )
1282 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1283 idx1, es->psz_language_code );
1285 i_wanted = es->i_channel;
1287 else if( p_sys->i_default_sub_id >= 0 )
1289 if( es->i_id == p_sys->i_default_sub_id )
1290 i_wanted = es->i_channel;
1293 if( p_sys->i_sub_last >= 0 )
1294 i_wanted = p_sys->i_sub_last;
1296 if( p_sys->i_sub_id >= 0 )
1298 if( es->i_id == p_sys->i_sub_id )
1299 i_wanted = es->i_channel;
1304 else if( i_cat == VIDEO_ES )
1306 i_wanted = es->i_channel;
1309 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1310 EsSelect( out, es );
1313 /* FIXME TODO handle priority here */
1314 if( EsIsSelected( es ) )
1316 if( i_cat == AUDIO_ES )
1318 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1319 p_sys->p_es_audio &&
1320 p_sys->p_es_audio != es &&
1321 EsIsSelected( p_sys->p_es_audio ) )
1323 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
1325 p_sys->p_es_audio = es;
1327 else if( i_cat == SPU_ES )
1329 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1331 p_sys->p_es_sub != es &&
1332 EsIsSelected( p_sys->p_es_sub ) )
1334 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1336 p_sys->p_es_sub = es;
1338 else if( i_cat == VIDEO_ES )
1340 p_sys->p_es_video = es;
1346 * Send a block for the given es_out
1348 * \param out the es_out to send from
1349 * \param es the es_out_id
1350 * \param p_block the data block to send
1352 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1354 es_out_sys_t *p_sys = out->p_sys;
1355 input_thread_t *p_input = p_sys->p_input;
1356 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1360 if( es->fmt.i_cat == AUDIO_ES )
1361 i_delay = p_sys->i_audio_delay;
1362 else if( es->fmt.i_cat == SPU_ES )
1363 i_delay = p_sys->i_spu_delay;
1367 if( p_input->p_libvlc->b_stats )
1369 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1370 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1371 p_block->i_buffer, &i_total );
1372 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1373 (float)i_total, NULL );
1374 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1377 /* Mark preroll blocks */
1378 if( es->i_preroll_end >= 0 )
1380 int64_t i_date = p_block->i_pts;
1382 i_date = p_block->i_dts;
1384 if( i_date < es->i_preroll_end )
1385 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1387 es->i_preroll_end = -1;
1390 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1392 p_block->i_dts += i_delay;
1394 else if( p_block->i_dts > 0 )
1397 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1399 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1401 p_block->i_pts += i_delay;
1403 else if( p_block->i_pts > 0 )
1406 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1408 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1410 mtime_t current_date = mdate();
1412 || p_block->i_pts > current_date + 10000000
1413 || current_date > p_block->i_pts )
1415 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1416 * for teletext streams. */
1417 p_block->i_pts = current_date + 400000
1418 + p_input->i_pts_delay + i_delay;
1422 p_block->i_rate = p_sys->i_rate;
1424 /* TODO handle mute */
1426 ( es->fmt.i_cat != AUDIO_ES ||
1427 ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1428 p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1430 vlc_bool_t pb_cc[4];
1431 vlc_bool_t b_cc_new = VLC_FALSE;
1433 input_DecoderDecode( es->p_dec, p_block );
1435 /* Check CC status */
1436 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1437 for( i = 0; i < 4; i++ )
1439 static const vlc_fourcc_t fcc[4] = {
1440 VLC_FOURCC('c', 'c', '1', ' '),
1441 VLC_FOURCC('c', 'c', '2', ' '),
1442 VLC_FOURCC('c', 'c', '3', ' '),
1443 VLC_FOURCC('c', 'c', '4', ' '),
1445 static const char *ppsz_description[4] = {
1446 N_("Closed captions 1"),
1447 N_("Closed captions 2"),
1448 N_("Closed captions 3"),
1449 N_("Closed captions 4"),
1453 if( es->pb_cc_present[i] || !pb_cc[i] )
1455 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1457 es_format_Init( &fmt, SPU_ES, fcc[i] );
1458 fmt.i_group = es->fmt.i_group;
1459 fmt.psz_description = strdup( _(ppsz_description[i] ) );
1460 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1461 es->pp_cc_es[i]->p_master = es;
1462 es_format_Clean( &fmt );
1465 es->pb_cc_present[i] = VLC_TRUE;
1466 b_cc_new = VLC_TRUE;
1469 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1473 block_Release( p_block );
1479 /*****************************************************************************
1481 *****************************************************************************/
1482 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1484 es_out_sys_t *p_sys = out->p_sys;
1485 vlc_bool_t b_reselect = VLC_FALSE;
1488 /* We don't try to reselect */
1491 while( !out->p_sys->p_input->b_die && es->p_dec )
1493 if( input_DecoderEmpty( es->p_dec ) )
1497 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1500 if( es->p_pgrm == p_sys->p_pgrm )
1501 EsOutESVarUpdate( out, es, VLC_TRUE );
1503 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1506 if( es->p_pgrm->i_es == 0 )
1508 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1511 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1512 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1514 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1515 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1516 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1518 switch( es->fmt.i_cat )
1531 /* Re-select another track when needed */
1533 for( i = 0; i < p_sys->i_es; i++ )
1535 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1536 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1539 free( es->psz_language );
1540 free( es->psz_language_code );
1542 es_format_Clean( &es->fmt );
1548 * Control query handler
1550 * \param out the es_out to control
1551 * \param i_query A es_out query as defined in include/ninput.h
1552 * \param args a variable list of arguments for the query
1553 * \return VLC_SUCCESS or an error code
1555 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1557 es_out_sys_t *p_sys = out->p_sys;
1565 case ES_OUT_SET_ES_STATE:
1566 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1567 b = (bool) va_arg( args, int );
1568 if( b && !EsIsSelected( es ) )
1570 EsSelect( out, es );
1571 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1573 else if( !b && EsIsSelected( es ) )
1575 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1580 case ES_OUT_GET_ES_STATE:
1581 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1582 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1584 *pb = EsIsSelected( es );
1587 case ES_OUT_SET_ACTIVE:
1589 b = (bool) va_arg( args, int );
1590 p_sys->b_active = b;
1593 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1597 case ES_OUT_GET_ACTIVE:
1598 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1599 *pb = p_sys->b_active;
1602 case ES_OUT_SET_MODE:
1603 i = (int) va_arg( args, int );
1604 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1605 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1609 /* Reapply policy mode */
1610 for( i = 0; i < p_sys->i_es; i++ )
1612 if( EsIsSelected( p_sys->es[i] ) )
1614 EsUnselect( out, p_sys->es[i],
1615 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1618 for( i = 0; i < p_sys->i_es; i++ )
1620 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1624 return VLC_EGENERIC;
1626 case ES_OUT_GET_MODE:
1627 pi = (int*) va_arg( args, int* );
1628 *pi = p_sys->i_mode;
1632 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1633 /* Special case NULL, NULL+i_cat */
1636 for( i = 0; i < p_sys->i_es; i++ )
1638 if( EsIsSelected( p_sys->es[i] ) )
1639 EsUnselect( out, p_sys->es[i],
1640 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1643 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1645 for( i = 0; i < p_sys->i_es; i++ )
1647 if( p_sys->es[i]->fmt.i_cat == AUDIO_ES &&
1648 EsIsSelected( p_sys->es[i] ) )
1649 EsUnselect( out, p_sys->es[i],
1650 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1653 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1655 for( i = 0; i < p_sys->i_es; i++ )
1657 if( p_sys->es[i]->fmt.i_cat == VIDEO_ES &&
1658 EsIsSelected( p_sys->es[i] ) )
1659 EsUnselect( out, p_sys->es[i],
1660 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1663 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1665 for( i = 0; i < p_sys->i_es; i++ )
1667 if( p_sys->es[i]->fmt.i_cat == SPU_ES &&
1668 EsIsSelected( p_sys->es[i] ) )
1669 EsUnselect( out, p_sys->es[i],
1670 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1675 for( i = 0; i < p_sys->i_es; i++ )
1677 if( es == p_sys->es[i] )
1679 EsOutSelect( out, es, VLC_TRUE );
1685 playlist_t * p_playlist = pl_Yield( p_sys->p_input );
1687 p_playlist->gc_date = mdate();
1688 vlc_object_signal_unlocked( p_playlist );
1690 pl_Release( p_playlist );
1694 case ES_OUT_SET_DEFAULT:
1696 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1700 /*p_sys->i_default_video_id = -1;*/
1701 /*p_sys->i_default_audio_id = -1;*/
1702 p_sys->i_default_sub_id = -1;
1704 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1706 /*p_sys->i_default_video_id = -1;*/
1708 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1710 /*p_sys->i_default_audio_id = -1;*/
1712 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1714 p_sys->i_default_sub_id = -1;
1718 /*if( es->fmt.i_cat == VIDEO_ES )
1719 p_sys->i_default_video_id = es->i_id;
1721 if( es->fmt.i_cat == AUDIO_ES )
1722 p_sys->i_default_audio_id = es->i_id;
1724 if( es->fmt.i_cat == SPU_ES )
1725 p_sys->i_default_sub_id = es->i_id;
1730 case ES_OUT_SET_PCR:
1731 case ES_OUT_SET_GROUP_PCR:
1733 es_out_pgrm_t *p_pgrm = NULL;
1737 if( i_query == ES_OUT_SET_PCR )
1739 p_pgrm = p_sys->p_pgrm;
1744 i_group = (int)va_arg( args, int );
1745 for( i = 0; i < p_sys->i_pgrm; i++ )
1747 if( p_sys->pgrm[i]->i_id == i_group )
1749 p_pgrm = p_sys->pgrm[i];
1754 if( p_pgrm == NULL )
1755 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1757 i_pcr = (int64_t)va_arg( args, int64_t );
1758 /* search program */
1759 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1763 case ES_OUT_RESET_PCR:
1764 for( i = 0; i < p_sys->i_pgrm; i++ )
1765 input_ClockResetPCR( &p_sys->pgrm[i]->clock );
1771 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1772 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1773 *pi_ts = input_ClockGetTS( p_sys->p_input,
1774 &p_sys->p_pgrm->clock, i_ts );
1777 return VLC_EGENERIC;
1779 case ES_OUT_GET_GROUP:
1780 pi = (int*) va_arg( args, int* );
1782 *pi = p_sys->p_pgrm->i_id;
1784 *pi = -1; /* FIXME */
1787 case ES_OUT_SET_GROUP:
1790 i = (int) va_arg( args, int );
1791 for( j = 0; j < p_sys->i_pgrm; j++ )
1793 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1794 if( p_pgrm->i_id == i )
1796 EsOutProgramSelect( out, p_pgrm );
1800 return VLC_EGENERIC;
1803 case ES_OUT_SET_FMT:
1805 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1806 * to update the p_extra data */
1808 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1809 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1810 if( es == NULL ) return VLC_EGENERIC;
1812 if( p_fmt->i_extra )
1814 es->fmt.i_extra = p_fmt->i_extra;
1815 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1816 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1818 if( !es->p_dec ) return VLC_SUCCESS;
1821 input_DecoderDelete( es->p_dec );
1822 es->p_dec = input_DecoderNew( p_sys->p_input,
1823 &es->fmt, VLC_FALSE );
1826 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1827 es->p_dec->fmt_in.p_extra =
1828 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1829 memcpy( es->p_dec->fmt_in.p_extra,
1830 p_fmt->p_extra, p_fmt->i_extra );
1837 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1841 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1842 i_date = (int64_t)va_arg( args, int64_t );
1844 if( !es || !es->p_dec )
1845 return VLC_EGENERIC;
1847 /* XXX We should call input_ClockGetTS but PCR has been reseted
1848 * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1849 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1850 es->i_preroll_end = i_date;
1854 case ES_OUT_SET_GROUP_META:
1856 int i_group = (int)va_arg( args, int );
1857 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1859 EsOutProgramMeta( out, i_group, p_meta );
1862 case ES_OUT_SET_GROUP_EPG:
1864 int i_group = (int)va_arg( args, int );
1865 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1867 EsOutProgramEpg( out, i_group, p_epg );
1870 case ES_OUT_DEL_GROUP:
1872 int i_group = (int)va_arg( args, int );
1874 return EsOutProgramDel( out, i_group );
1878 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1879 return VLC_EGENERIC;
1883 /****************************************************************************
1884 * LanguageGetName: try to expend iso639 into plain name
1885 ****************************************************************************/
1886 static char *LanguageGetName( const char *psz_code )
1888 const iso639_lang_t *pl;
1890 if( psz_code == NULL )
1892 return strdup( "" );
1895 if( strlen( psz_code ) == 2 )
1897 pl = GetLang_1( psz_code );
1899 else if( strlen( psz_code ) == 3 )
1901 pl = GetLang_2B( psz_code );
1902 if( !strcmp( pl->psz_iso639_1, "??" ) )
1904 pl = GetLang_2T( psz_code );
1909 return strdup( psz_code );
1912 if( !strcmp( pl->psz_iso639_1, "??" ) )
1914 return strdup( psz_code );
1918 if( *pl->psz_native_name )
1920 return strdup( pl->psz_native_name );
1922 return strdup( pl->psz_eng_name );
1926 /* Get a 2 char code */
1927 static char *LanguageGetCode( const char *psz_lang )
1929 const iso639_lang_t *pl;
1931 if( psz_lang == NULL || *psz_lang == '\0' )
1932 return strdup("??");
1934 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1936 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1937 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1938 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1939 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1940 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1944 if( pl->psz_iso639_1 != NULL )
1945 return strdup( pl->psz_iso639_1 );
1947 return strdup("??");
1950 static char **LanguageSplit( const char *psz_langs )
1957 if( psz_langs == NULL ) return NULL;
1959 psz_parser = psz_dup = strdup(psz_langs);
1961 while( psz_parser && *psz_parser )
1966 psz = strchr(psz_parser, ',' );
1967 if( psz ) *psz++ = '\0';
1969 if( !strcmp( psz_parser, "any" ) )
1971 TAB_APPEND( i_psz, ppsz, strdup("any") );
1975 psz_code = LanguageGetCode( psz_parser );
1976 if( strcmp( psz_code, "??" ) )
1978 TAB_APPEND( i_psz, ppsz, psz_code );
1991 TAB_APPEND( i_psz, ppsz, NULL );
1998 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2002 if( !ppsz_langs || !psz_lang ) return -1;
2004 for( i = 0; ppsz_langs[i]; i++ )
2006 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2007 !strcasecmp( ppsz_langs[i], "any" ) )
2016 /****************************************************************************
2018 * - add meta info to the playlist item
2019 ****************************************************************************/
2020 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2022 es_out_sys_t *p_sys = out->p_sys;
2023 input_thread_t *p_input = p_sys->p_input;
2024 es_format_t *fmt = &es->fmt;
2028 /* Add stream info */
2029 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
2031 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2032 "%.4s", (char*)&fmt->i_codec );
2034 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2035 "%s", es->psz_language );
2037 /* Add information */
2038 switch( fmt->i_cat )
2041 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2042 _("Type"), _("Audio") );
2044 if( fmt->audio.i_channels > 0 )
2045 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2046 "%u", fmt->audio.i_channels );
2048 if( fmt->audio.i_rate > 0 )
2050 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2051 _("%u Hz"), fmt->audio.i_rate );
2052 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2055 if( fmt->audio.i_bitspersample > 0 )
2056 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2057 _("Bits per sample"), "%u",
2058 fmt->audio.i_bitspersample );
2060 if( fmt->i_bitrate > 0 )
2062 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2063 _("%u kb/s"), fmt->i_bitrate / 1000 );
2064 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2069 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2070 _("Type"), _("Video") );
2072 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2073 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2074 _("Resolution"), "%ux%u",
2075 fmt->video.i_width, fmt->video.i_height );
2077 if( fmt->video.i_visible_width > 0 &&
2078 fmt->video.i_visible_height > 0 )
2079 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2080 _("Display resolution"), "%ux%u",
2081 fmt->video.i_visible_width,
2082 fmt->video.i_visible_height);
2083 if( fmt->video.i_frame_rate > 0 &&
2084 fmt->video.i_frame_rate_base > 0 )
2086 div = lldiv( (float)fmt->video.i_frame_rate /
2087 fmt->video.i_frame_rate_base * 1000000,
2089 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2090 _("Frame rate"), I64Fd".%06u",
2091 div.quot, (unsigned int )div.rem );
2096 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2097 _("Type"), _("Subtitle") );