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 *****************************************************************************/
32 #include <vlc_input.h>
33 #include <vlc_es_out.h>
34 #include <vlc_block.h>
37 #include "input_internal.h"
39 #include "vlc_playlist.h"
41 /* FIXME we should find a better way than including that */
42 #include "../text/iso-639_def.h"
44 /*****************************************************************************
46 *****************************************************************************/
52 /* Number of es for this pgrm */
55 vlc_bool_t b_selected;
57 /* Clock for this program */
61 char *psz_now_playing;
71 es_out_pgrm_t *p_pgrm;
74 int64_t i_preroll_end;
76 /* Channel in the track type */
80 char *psz_language_code;
84 /* Fields for Video with CC */
85 vlc_bool_t pb_cc_present[4];
86 es_out_id_t *pp_cc_es[4];
88 /* Field for CC track from a master video */
89 es_out_id_t *p_master;
94 input_thread_t *p_input;
99 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
100 es_out_pgrm_t *p_pgrm; /* Master program */
117 int i_audio_last, i_audio_id;
118 int i_sub_last, i_sub_id;
119 int i_default_sub_id; /* As specified in container; if applicable */
120 char **ppsz_audio_language;
121 char **ppsz_sub_language;
123 /* current main es */
124 es_out_id_t *p_es_audio;
125 es_out_id_t *p_es_video;
126 es_out_id_t *p_es_sub;
129 int64_t i_audio_delay;
133 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
134 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
135 static void EsOutDel ( es_out_t *, es_out_id_t * );
136 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
137 static int EsOutControl( es_out_t *, int i_query, va_list );
139 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
141 static vlc_bool_t EsIsSelected( es_out_t *out, es_out_id_t *es );
142 static void EsSelect( es_out_t *out, es_out_id_t *es );
143 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
144 static char *LanguageGetName( const char *psz_code );
145 static char *LanguageGetCode( const char *psz_lang );
146 static char **LanguageSplit( const char *psz_langs );
147 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
149 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
151 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
152 VLC_FOURCC('c', 'c', '1', ' '),
153 VLC_FOURCC('c', 'c', '2', ' '),
154 VLC_FOURCC('c', 'c', '3', ' '),
155 VLC_FOURCC('c', 'c', '4', ' '),
157 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
160 for( i = 0; i < 4; i++ )
162 if( fcc == EsOutFourccClosedCaptions[i] )
169 /*****************************************************************************
171 *****************************************************************************/
172 es_out_t *input_EsOutNew( input_thread_t *p_input )
174 es_out_t *out = malloc( sizeof( es_out_t ) );
175 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
179 out->pf_add = EsOutAdd;
180 out->pf_send = EsOutSend;
181 out->pf_del = EsOutDel;
182 out->pf_control = EsOutControl;
184 out->b_sout = (p_input->p->p_sout != NULL ? VLC_TRUE : VLC_FALSE);
186 p_sys->p_input = p_input;
188 p_sys->b_active = VLC_FALSE;
189 p_sys->i_mode = ES_OUT_MODE_AUTO;
192 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
193 p_sys->p_pgrm = NULL;
197 TAB_INIT( p_sys->i_es, p_sys->es );
204 var_Get( p_input, "audio-track", &val );
205 p_sys->i_audio_last = val.i_int;
207 var_Get( p_input, "sub-track", &val );
208 p_sys->i_sub_last = val.i_int;
210 p_sys->i_default_sub_id = -1;
212 if( !p_input->b_preparsing )
214 var_Get( p_input, "audio-language", &val );
215 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
216 if( p_sys->ppsz_audio_language )
218 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
219 msg_Dbg( p_input, "selected audio language[%d] %s",
220 i, p_sys->ppsz_audio_language[i] );
222 if( val.psz_string ) free( val.psz_string );
224 var_Get( p_input, "sub-language", &val );
225 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
226 if( p_sys->ppsz_sub_language )
228 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
229 msg_Dbg( p_input, "selected subtitle language[%d] %s",
230 i, p_sys->ppsz_sub_language[i] );
232 if( val.psz_string ) free( val.psz_string );
236 p_sys->ppsz_sub_language = NULL;
237 p_sys->ppsz_audio_language = NULL;
240 var_Get( p_input, "audio-track-id", &val );
241 p_sys->i_audio_id = val.i_int;
243 var_Get( p_input, "sub-track-id", &val );
244 p_sys->i_sub_id = val.i_int;
246 p_sys->p_es_audio = NULL;
247 p_sys->p_es_video = NULL;
248 p_sys->p_es_sub = NULL;
250 p_sys->i_audio_delay= 0;
251 p_sys->i_spu_delay = 0;
256 /*****************************************************************************
258 *****************************************************************************/
259 void input_EsOutDelete( es_out_t *out )
261 es_out_sys_t *p_sys = out->p_sys;
264 for( i = 0; i < p_sys->i_es; i++ )
266 if( p_sys->es[i]->p_dec )
268 input_DecoderDelete( p_sys->es[i]->p_dec );
270 if( p_sys->es[i]->psz_language )
271 free( p_sys->es[i]->psz_language );
272 if( p_sys->es[i]->psz_language_code )
273 free( p_sys->es[i]->psz_language_code );
274 es_format_Clean( &p_sys->es[i]->fmt );
276 free( p_sys->es[i] );
278 if( p_sys->ppsz_audio_language )
280 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
281 free( p_sys->ppsz_audio_language[i] );
282 free( p_sys->ppsz_audio_language );
284 if( p_sys->ppsz_sub_language )
286 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
287 free( p_sys->ppsz_sub_language[i] );
288 free( p_sys->ppsz_sub_language );
294 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
295 for( i = 0; i < p_sys->i_pgrm; i++ )
297 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
298 if( p_pgrm->psz_now_playing )
299 free( p_pgrm->psz_now_playing );
300 if( p_pgrm->psz_publisher )
301 free( p_pgrm->psz_publisher );
302 if( p_pgrm->psz_name )
303 free( p_pgrm->psz_name );
305 vlc_epg_Delete( p_pgrm->p_epg );
309 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
315 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
320 /* Special HACK, -i_id is tha cat of the stream */
321 return (es_out_id_t*)((uint8_t*)NULL-i_id);
324 for( i = 0; i < out->p_sys->i_es; i++ )
326 if( out->p_sys->es[i]->i_id == i_id )
327 return out->p_sys->es[i];
332 static void EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_flush, vlc_bool_t b_audio )
334 es_out_sys_t *p_sys = out->p_sys;
337 for( i = 0; i < p_sys->i_es; i++ )
339 es_out_id_t *es = p_sys->es[i];
341 /* Send a dummy block to let decoder know that
342 * there is a discontinuity */
343 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
344 input_DecoderDiscontinuity( es->p_dec, b_flush );
347 void input_EsOutChangeRate( es_out_t *out )
349 es_out_sys_t *p_sys = out->p_sys;
352 EsOutDiscontinuity( out, VLC_FALSE, VLC_FALSE );
354 for( i = 0; i < p_sys->i_pgrm; i++ )
355 input_ClockSetRate( p_sys->p_input, &p_sys->pgrm[i]->clock );
358 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
360 es_out_sys_t *p_sys = out->p_sys;
362 if( i_cat == AUDIO_ES )
363 p_sys->i_audio_delay = i_delay;
364 else if( i_cat == SPU_ES )
365 p_sys->i_spu_delay = i_delay;
367 void input_EsOutChangeState( es_out_t *out )
369 es_out_sys_t *p_sys = out->p_sys;
370 input_thread_t *p_input = p_sys->p_input;
372 if( p_input->i_state == PAUSE_S )
374 /* Send discontinuity to decoders (it will allow them to flush
375 * * if implemented */
376 EsOutDiscontinuity( out, VLC_FALSE, VLC_FALSE );
380 /* Out of pause, reset pcr */
381 es_out_Control( out, ES_OUT_RESET_PCR );
384 void input_EsOutChangePosition( es_out_t *out )
386 //es_out_sys_t *p_sys = out->p_sys;
388 es_out_Control( out, ES_OUT_RESET_PCR );
389 EsOutDiscontinuity( out, VLC_TRUE, VLC_FALSE );
392 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
394 es_out_sys_t *p_sys = out->p_sys;
397 for( i = 0; i < p_sys->i_es; i++ )
399 es_out_id_t *es = p_sys->es[i];
401 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
407 /*****************************************************************************
409 *****************************************************************************/
410 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
411 vlc_bool_t b_delete )
413 es_out_sys_t *p_sys = out->p_sys;
414 input_thread_t *p_input = p_sys->p_input;
415 vlc_value_t val, text;
419 if( fmt->i_cat == AUDIO_ES )
420 psz_var = "audio-es";
421 else if( fmt->i_cat == VIDEO_ES )
422 psz_var = "video-es";
423 else if( fmt->i_cat == SPU_ES )
431 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
432 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
436 /* Get the number of ES already added */
437 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
442 /* First one, we need to add the "Disable" choice */
443 val2.i_int = -1; text.psz_string = _("Disable");
444 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
448 /* Take care of the ES description */
449 if( fmt->psz_description && *fmt->psz_description )
451 if( psz_language && *psz_language )
453 text.psz_string = malloc( strlen( fmt->psz_description) +
454 strlen( psz_language ) + 10 );
455 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
458 else text.psz_string = strdup( fmt->psz_description );
462 if( psz_language && *psz_language )
465 text.psz_string = malloc( strlen( _("Track %i") )+
466 strlen( psz_language ) + 30 );
467 asprintf( &temp, _("Track %i"), val.i_int );
468 sprintf( text.psz_string, "%s - [%s]", temp, psz_language );
473 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
474 sprintf( text.psz_string, _("Track %i"), val.i_int );
479 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
481 free( text.psz_string );
483 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
486 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
487 vlc_bool_t b_delete )
489 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
492 /* EsOutProgramSelect:
493 * Select a program and update the object variable
495 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
497 es_out_sys_t *p_sys = out->p_sys;
498 input_thread_t *p_input = p_sys->p_input;
502 if( p_sys->p_pgrm == p_pgrm )
503 return; /* Nothing to do */
507 es_out_pgrm_t *old = p_sys->p_pgrm;
508 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
510 for( i = 0; i < p_sys->i_es; i++ )
512 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( out, p_sys->es[i] ) &&
513 p_sys->i_mode != ES_OUT_MODE_ALL )
514 EsUnselect( out, p_sys->es[i], VLC_TRUE );
517 p_sys->p_es_audio = NULL;
518 p_sys->p_es_sub = NULL;
519 p_sys->p_es_video = NULL;
522 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
524 /* Mark it selected */
525 p_pgrm->b_selected = VLC_TRUE;
527 /* Switch master stream */
528 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
530 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
532 p_pgrm->clock.b_master = VLC_TRUE;
533 p_sys->p_pgrm = p_pgrm;
535 /* Update "program" */
536 val.i_int = p_pgrm->i_id;
537 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
540 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
541 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
542 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
543 for( i = 0; i < p_sys->i_es; i++ )
545 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
546 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
547 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
550 /* Update now playing */
551 input_item_SetNowPlaying( p_input->p->input.p_item,
552 p_pgrm->psz_now_playing );
553 input_item_SetPublisher( p_input->p->input.p_item,
554 p_pgrm->psz_publisher );
556 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
562 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
564 es_out_sys_t *p_sys = out->p_sys;
565 input_thread_t *p_input = p_sys->p_input;
568 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
571 p_pgrm->i_id = i_group;
573 p_pgrm->b_selected = VLC_FALSE;
574 p_pgrm->psz_name = NULL;
575 p_pgrm->psz_now_playing = NULL;
576 p_pgrm->psz_publisher = NULL;
577 p_pgrm->p_epg = NULL;
578 input_ClockInit( p_input, &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average );
581 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
583 /* Update "program" variable */
585 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
587 if( i_group == var_GetInteger( p_input, "program" ) )
589 EsOutProgramSelect( out, p_pgrm );
593 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
601 static int EsOutProgramDel( es_out_t *out, int i_group )
603 es_out_sys_t *p_sys = out->p_sys;
604 input_thread_t *p_input = p_sys->p_input;
605 es_out_pgrm_t *p_pgrm = NULL;
609 for( i = 0; i < p_sys->i_pgrm; i++ )
611 if( p_sys->pgrm[i]->i_id == i_group )
613 p_pgrm = p_sys->pgrm[i];
623 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
624 i_group, p_pgrm->i_es );
628 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
630 /* If program is selected we need to unselect it */
631 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
633 if( p_pgrm->psz_name ) free( p_pgrm->psz_name );
634 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
635 if( p_pgrm->psz_publisher ) free( p_pgrm->psz_publisher );
637 vlc_epg_Delete( p_pgrm->p_epg );
640 /* Update "program" variable */
642 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
644 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
651 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
654 if( p_pgrm->psz_name )
655 asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
657 asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
661 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
663 es_out_sys_t *p_sys = out->p_sys;
664 es_out_pgrm_t *p_pgrm = NULL;
665 input_thread_t *p_input = p_sys->p_input;
667 const char *psz_title = NULL;
668 const char *psz_provider = NULL;
671 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
673 /* Check against empty meta data (empty for what we handle) */
674 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
675 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
676 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
677 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
682 for( i = 0; i < p_sys->i_pgrm; i++ )
684 if( p_sys->pgrm[i]->i_id == i_group )
686 p_pgrm = p_sys->pgrm[i];
691 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
694 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
695 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
697 /* Update the description text of the program */
698 if( psz_title && *psz_title )
703 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
705 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
707 /* Remove old entries */
708 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
709 /* TODO update epg name */
712 if( p_pgrm->psz_name ) free( p_pgrm->psz_name );
713 p_pgrm->psz_name = strdup( psz_title );
715 /* ugly but it works */
717 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
719 if( psz_provider && *psz_provider )
721 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
722 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
723 free( text.psz_string );
727 text.psz_string = (char *)psz_title;
728 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
732 psz_cat = EsOutProgramGetMetaName( p_pgrm );
735 if( p_sys->p_pgrm == p_pgrm )
736 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
737 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
739 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
740 for( i = 0; ppsz_all_keys[i]; i++ )
742 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
743 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
744 free( ppsz_all_keys[i] );
746 free( ppsz_all_keys );
751 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
756 for( i = 0; i < p_src->i_event; i++ )
758 vlc_epg_event_t *p_evt = p_src->pp_event[i];
759 vlc_bool_t b_add = VLC_TRUE;
762 for( j = 0; j < p_dst->i_event; j++ )
764 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
769 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
774 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
777 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
778 p_copy->i_start = p_evt->i_start;
779 p_copy->i_duration = p_evt->i_duration;
780 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
781 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
782 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
783 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
787 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
789 /* Keep only 1 old event */
790 if( p_dst->p_current )
792 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
793 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
797 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
799 es_out_sys_t *p_sys = out->p_sys;
800 input_thread_t *p_input = p_sys->p_input;
801 es_out_pgrm_t *p_pgrm = NULL;
806 for( i = 0; i < p_sys->i_pgrm; i++ )
808 if( p_sys->pgrm[i]->i_id == i_group )
810 p_pgrm = p_sys->pgrm[i];
815 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
819 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
820 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
823 psz_cat = EsOutProgramGetMetaName( p_pgrm );
824 #ifdef HAVE_LOCALTIME_R
826 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
828 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
829 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
830 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
832 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
833 time_t t_start = (time_t)p_evt->i_start;
837 localtime_r( &t_start, &tm_start );
839 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
840 if( p_evt->psz_short_description || p_evt->psz_description )
841 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
843 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
844 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
846 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
848 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
852 /* Update now playing */
853 if( p_pgrm->psz_now_playing )
854 free( p_pgrm->psz_now_playing );
855 p_pgrm->psz_now_playing = NULL;
856 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
857 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
859 if( p_pgrm == p_sys->p_pgrm )
860 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
862 if( p_pgrm->psz_now_playing )
864 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
865 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
866 p_pgrm->psz_now_playing );
870 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
871 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
880 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
882 es_out_sys_t *p_sys = out->p_sys;
883 input_thread_t *p_input = p_sys->p_input;
885 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
886 es_out_pgrm_t *p_pgrm = NULL;
889 if( fmt->i_group < 0 )
891 msg_Err( p_input, "invalid group number" );
895 /* Search the program */
896 for( i = 0; i < p_sys->i_pgrm; i++ )
898 if( fmt->i_group == p_sys->pgrm[i]->i_id )
900 p_pgrm = p_sys->pgrm[i];
906 /* Create a new one */
907 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
910 /* Increase ref count for program */
915 fmt->i_id = out->p_sys->i_id;
916 es->i_id = fmt->i_id;
918 es_format_Copy( &es->fmt, fmt );
919 es->i_preroll_end = -1;
925 audio_replay_gain_t rg;
927 es->i_channel = p_sys->i_audio;
929 vlc_mutex_lock( &p_input->p->input.p_item->lock );
930 memset( &rg, 0, sizeof(rg) );
931 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
932 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
934 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
936 if( !es->fmt.audio_replay_gain.pb_peak[i] )
938 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
939 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
941 if( !es->fmt.audio_replay_gain.pb_gain[i] )
943 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
944 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
951 es->i_channel = p_sys->i_video;
952 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
953 vlc_ureduce( &es->fmt.video.i_frame_rate,
954 &es->fmt.video.i_frame_rate_base,
955 fmt->video.i_frame_rate,
956 fmt->video.i_frame_rate_base, 0 );
960 es->i_channel = p_sys->i_sub;
967 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
968 es->psz_language_code = LanguageGetCode( fmt->psz_language );
970 for( i = 0; i < 4; i++ )
971 es->pb_cc_present[i] = VLC_FALSE;
972 es->p_master = VLC_FALSE;
974 if( es->p_pgrm == p_sys->p_pgrm )
975 EsOutESVarUpdate( out, es, VLC_FALSE );
977 /* Select it if needed */
978 EsOutSelect( out, es, VLC_FALSE );
981 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
982 p_sys->i_id++; /* always incremented */
996 EsOutAddInfo( out, es );
1001 static vlc_bool_t EsIsSelected( es_out_t *out, es_out_id_t *es )
1005 vlc_bool_t b_decode = VLC_FALSE;
1006 if( es->p_master->p_dec )
1008 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1009 if( i_channel != -1 )
1010 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1016 return es->p_dec != NULL;
1019 static void EsSelect( es_out_t *out, es_out_id_t *es )
1021 es_out_sys_t *p_sys = out->p_sys;
1022 input_thread_t *p_input = p_sys->p_input;
1024 const char *psz_var;
1026 if( EsIsSelected( out, es ) )
1028 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1035 if( !es->p_master->p_dec )
1038 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1039 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, VLC_TRUE, i_channel ) )
1044 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1046 if( !var_GetBool( p_input, "video" ) ||
1047 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
1049 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1054 else if( es->fmt.i_cat == AUDIO_ES )
1056 var_Get( p_input, "audio", &val );
1057 if( !var_GetBool( p_input, "audio" ) ||
1058 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
1060 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1065 if( es->fmt.i_cat == SPU_ES )
1067 var_Get( p_input, "spu", &val );
1068 if( !var_GetBool( p_input, "spu" ) ||
1069 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
1071 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1077 es->i_preroll_end = -1;
1078 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
1079 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1083 if( es->fmt.i_cat == VIDEO_ES )
1084 psz_var = "video-es";
1085 else if( es->fmt.i_cat == AUDIO_ES )
1086 psz_var = "audio-es";
1087 else if( es->fmt.i_cat == SPU_ES )
1092 /* Mark it as selected */
1093 val.i_int = es->i_id;
1094 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1096 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1099 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
1101 es_out_sys_t *p_sys = out->p_sys;
1102 input_thread_t *p_input = p_sys->p_input;
1104 const char *psz_var;
1106 if( !EsIsSelected( out, es ) )
1108 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1114 if( es->p_master->p_dec )
1116 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1117 if( i_channel != -1 )
1118 input_DecoderSetCcState( es->p_master->p_dec, VLC_FALSE, i_channel );
1123 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1125 for( i = 0; i < 4; i++ )
1127 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1130 if( i_spu_id == es->pp_cc_es[i]->i_id )
1132 /* Force unselection of the CC */
1134 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1136 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1138 EsOutDel( out, es->pp_cc_es[i] );
1140 es->pb_cc_present[i] = VLC_FALSE;
1142 input_DecoderDelete( es->p_dec );
1150 if( es->p_dec == NULL )
1152 if( es->fmt.i_cat == VIDEO_ES )
1153 psz_var = "video-es";
1154 else if( es->fmt.i_cat == AUDIO_ES )
1155 psz_var = "audio-es";
1156 else if( es->fmt.i_cat == SPU_ES )
1161 /* Mark it as unselected */
1163 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1165 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1169 * Select an ES given the current mode
1170 * XXX: you need to take a the lock before (stream.stream_lock)
1172 * \param out The es_out structure
1173 * \param es es_out_id structure
1174 * \param b_force ...
1177 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
1179 es_out_sys_t *p_sys = out->p_sys;
1181 int i_cat = es->fmt.i_cat;
1183 if( !p_sys->b_active ||
1184 ( !b_force && es->fmt.i_priority < 0 ) )
1189 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1191 if( !EsIsSelected( out, es ) )
1192 EsSelect( out, es );
1194 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1198 var_Get( p_sys->p_input, "programs", &val );
1199 for ( i = 0; i < val.p_list->i_count; i++ )
1201 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1203 if( !EsIsSelected( out, es ) )
1204 EsSelect( out, es );
1208 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1210 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1214 if( es->p_pgrm != p_sys->p_pgrm )
1217 if( i_cat == AUDIO_ES )
1219 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1220 es->psz_language_code );
1222 if( p_sys->p_es_audio &&
1223 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1225 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1226 p_sys->p_es_audio->psz_language_code );
1228 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1230 i_wanted = es->i_channel;
1234 /* Select audio if (no audio selected yet)
1235 * - no audio-language
1236 * - no audio code for the ES
1237 * - audio code in the requested list */
1239 !strcmp( es->psz_language_code, "??" ) ||
1240 !p_sys->ppsz_audio_language )
1241 i_wanted = es->i_channel;
1244 if( p_sys->i_audio_last >= 0 )
1245 i_wanted = p_sys->i_audio_last;
1247 if( p_sys->i_audio_id >= 0 )
1249 if( es->i_id == p_sys->i_audio_id )
1250 i_wanted = es->i_channel;
1255 else if( i_cat == SPU_ES )
1257 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1258 es->psz_language_code );
1260 if( p_sys->p_es_sub &&
1261 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1263 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1264 p_sys->p_es_sub->psz_language_code );
1266 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1267 idx1, es->psz_language_code, idx2,
1268 p_sys->p_es_sub->psz_language_code );
1270 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1272 /* We found a SPU that matches our language request */
1273 i_wanted = es->i_channel;
1275 else if( idx1 >= 0 )
1277 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1278 idx1, es->psz_language_code );
1280 i_wanted = es->i_channel;
1282 else if( p_sys->i_default_sub_id >= 0 )
1284 if( es->i_id == p_sys->i_default_sub_id )
1285 i_wanted = es->i_channel;
1288 if( p_sys->i_sub_last >= 0 )
1289 i_wanted = p_sys->i_sub_last;
1291 if( p_sys->i_sub_id >= 0 )
1293 if( es->i_id == p_sys->i_sub_id )
1294 i_wanted = es->i_channel;
1299 else if( i_cat == VIDEO_ES )
1301 i_wanted = es->i_channel;
1304 if( i_wanted == es->i_channel && !EsIsSelected( out, es ) )
1305 EsSelect( out, es );
1308 /* FIXME TODO handle priority here */
1309 if( EsIsSelected( out, es ) )
1311 if( i_cat == AUDIO_ES )
1313 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1314 p_sys->p_es_audio &&
1315 p_sys->p_es_audio != es &&
1316 EsIsSelected( out, p_sys->p_es_audio ) )
1318 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
1320 p_sys->p_es_audio = es;
1322 else if( i_cat == SPU_ES )
1324 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1326 p_sys->p_es_sub != es &&
1327 EsIsSelected( out, p_sys->p_es_sub ) )
1329 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1331 p_sys->p_es_sub = es;
1333 else if( i_cat == VIDEO_ES )
1335 p_sys->p_es_video = es;
1341 * Send a block for the given es_out
1343 * \param out the es_out to send from
1344 * \param es the es_out_id
1345 * \param p_block the data block to send
1347 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1349 es_out_sys_t *p_sys = out->p_sys;
1350 input_thread_t *p_input = p_sys->p_input;
1351 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1355 if( es->fmt.i_cat == AUDIO_ES )
1356 i_delay = p_sys->i_audio_delay;
1357 else if( es->fmt.i_cat == SPU_ES )
1358 i_delay = p_sys->i_spu_delay;
1362 if( p_input->p_libvlc->b_stats )
1364 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1365 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1366 p_block->i_buffer, &i_total );
1367 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1368 (float)i_total, NULL );
1369 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1372 /* Mark preroll blocks */
1373 if( es->i_preroll_end >= 0 )
1375 int64_t i_date = p_block->i_pts;
1377 i_date = p_block->i_dts;
1379 if( i_date < es->i_preroll_end )
1380 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1382 es->i_preroll_end = -1;
1385 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1387 p_block->i_dts += i_delay;
1389 else if( p_block->i_dts > 0 )
1392 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1394 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1396 p_block->i_pts += i_delay;
1398 else if( p_block->i_pts > 0 )
1401 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1403 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1405 mtime_t current_date = mdate();
1407 || p_block->i_pts > current_date + 10000000
1408 || current_date > p_block->i_pts )
1410 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1411 * for teletext streams. */
1412 p_block->i_pts = current_date + 400000
1413 + p_input->i_pts_delay + i_delay;
1417 p_block->i_rate = p_input->p->i_rate;
1419 /* TODO handle mute */
1421 ( es->fmt.i_cat != AUDIO_ES ||
1422 ( p_input->p->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1423 p_input->p->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1425 vlc_bool_t pb_cc[4];
1426 vlc_bool_t b_cc_new = VLC_FALSE;
1428 input_DecoderDecode( es->p_dec, p_block );
1430 /* Check CC status */
1431 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1432 for( i = 0; i < 4; i++ )
1434 static const vlc_fourcc_t fcc[4] = {
1435 VLC_FOURCC('c', 'c', '1', ' '),
1436 VLC_FOURCC('c', 'c', '2', ' '),
1437 VLC_FOURCC('c', 'c', '3', ' '),
1438 VLC_FOURCC('c', 'c', '4', ' '),
1440 static const char *ppsz_description[4] = {
1441 N_("Closed captions 1"),
1442 N_("Closed captions 2"),
1443 N_("Closed captions 3"),
1444 N_("Closed captions 4"),
1448 if( es->pb_cc_present[i] || !pb_cc[i] )
1450 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1452 es_format_Init( &fmt, SPU_ES, fcc[i] );
1453 fmt.i_group = es->fmt.i_group;
1454 fmt.psz_description = strdup( _(ppsz_description[i] ) );
1455 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1456 es->pp_cc_es[i]->p_master = es;
1457 es_format_Clean( &fmt );
1460 es->pb_cc_present[i] = VLC_TRUE;
1461 b_cc_new = VLC_TRUE;
1464 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1468 block_Release( p_block );
1474 /*****************************************************************************
1476 *****************************************************************************/
1477 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1479 es_out_sys_t *p_sys = out->p_sys;
1480 vlc_bool_t b_reselect = VLC_FALSE;
1483 /* We don't try to reselect */
1486 while( !out->p_sys->p_input->b_die && es->p_dec )
1488 if( input_DecoderEmpty( es->p_dec ) )
1492 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1495 if( es->p_pgrm == p_sys->p_pgrm )
1496 EsOutESVarUpdate( out, es, VLC_TRUE );
1498 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1501 if( es->p_pgrm->i_es == 0 )
1503 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1506 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1507 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1509 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1510 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1511 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1513 switch( es->fmt.i_cat )
1526 /* Re-select another track when needed */
1528 for( i = 0; i < p_sys->i_es; i++ )
1530 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1531 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1534 if( es->psz_language )
1535 free( es->psz_language );
1536 if( es->psz_language_code )
1537 free( es->psz_language_code );
1539 es_format_Clean( &es->fmt );
1545 * Control query handler
1547 * \param out the es_out to control
1548 * \param i_query A es_out query as defined in include/ninput.h
1549 * \param args a variable list of arguments for the query
1550 * \return VLC_SUCCESS or an error code
1552 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1554 es_out_sys_t *p_sys = out->p_sys;
1562 case ES_OUT_SET_ES_STATE:
1563 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1564 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1565 if( b && !EsIsSelected( out, es ) )
1567 EsSelect( out, es );
1568 return EsIsSelected( out, es ) ? VLC_SUCCESS : VLC_EGENERIC;
1570 else if( !b && EsIsSelected( out, es ) )
1572 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1577 case ES_OUT_GET_ES_STATE:
1578 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1579 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1581 *pb = EsIsSelected( out, es );
1584 case ES_OUT_SET_ACTIVE:
1586 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1587 p_sys->b_active = b;
1590 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1594 case ES_OUT_GET_ACTIVE:
1595 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1596 *pb = p_sys->b_active;
1599 case ES_OUT_SET_MODE:
1600 i = (int) va_arg( args, int );
1601 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1602 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1606 /* Reapply policy mode */
1607 for( i = 0; i < p_sys->i_es; i++ )
1609 if( EsIsSelected( out, p_sys->es[i] ) )
1611 EsUnselect( out, p_sys->es[i],
1612 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1615 for( i = 0; i < p_sys->i_es; i++ )
1617 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1621 return VLC_EGENERIC;
1623 case ES_OUT_GET_MODE:
1624 pi = (int*) va_arg( args, int* );
1625 *pi = p_sys->i_mode;
1629 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1630 /* Special case NULL, NULL+i_cat */
1633 for( i = 0; i < p_sys->i_es; i++ )
1635 if( EsIsSelected( out, p_sys->es[i] ) )
1636 EsUnselect( out, p_sys->es[i],
1637 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1640 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1642 for( i = 0; i < p_sys->i_es; i++ )
1644 if( p_sys->es[i]->fmt.i_cat == AUDIO_ES &&
1645 EsIsSelected( out, p_sys->es[i] ) )
1646 EsUnselect( out, p_sys->es[i],
1647 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1650 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1652 for( i = 0; i < p_sys->i_es; i++ )
1654 if( p_sys->es[i]->fmt.i_cat == VIDEO_ES &&
1655 EsIsSelected( out, p_sys->es[i] ) )
1656 EsUnselect( out, p_sys->es[i],
1657 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1660 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1662 for( i = 0; i < p_sys->i_es; i++ )
1664 if( p_sys->es[i]->fmt.i_cat == SPU_ES &&
1665 EsIsSelected( out, p_sys->es[i] ) )
1666 EsUnselect( out, p_sys->es[i],
1667 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1672 for( i = 0; i < p_sys->i_es; i++ )
1674 if( es == p_sys->es[i] )
1676 EsOutSelect( out, es, VLC_TRUE );
1682 playlist_t * p_playlist = pl_Yield( p_sys->p_input );
1684 p_playlist->gc_date = mdate();
1685 vlc_object_signal_unlocked( p_playlist );
1687 pl_Release( p_playlist );
1691 case ES_OUT_SET_DEFAULT:
1693 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1697 /*p_sys->i_default_video_id = -1;*/
1698 /*p_sys->i_default_audio_id = -1;*/
1699 p_sys->i_default_sub_id = -1;
1701 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1703 /*p_sys->i_default_video_id = -1;*/
1705 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1707 /*p_sys->i_default_audio_id = -1;*/
1709 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1711 p_sys->i_default_sub_id = -1;
1715 /*if( es->fmt.i_cat == VIDEO_ES )
1716 p_sys->i_default_video_id = es->i_id;
1718 if( es->fmt.i_cat == AUDIO_ES )
1719 p_sys->i_default_audio_id = es->i_id;
1721 if( es->fmt.i_cat == SPU_ES )
1722 p_sys->i_default_sub_id = es->i_id;
1727 case ES_OUT_SET_PCR:
1728 case ES_OUT_SET_GROUP_PCR:
1730 es_out_pgrm_t *p_pgrm = NULL;
1734 if( i_query == ES_OUT_SET_PCR )
1736 p_pgrm = p_sys->p_pgrm;
1741 i_group = (int)va_arg( args, int );
1742 for( i = 0; i < p_sys->i_pgrm; i++ )
1744 if( p_sys->pgrm[i]->i_id == i_group )
1746 p_pgrm = p_sys->pgrm[i];
1751 if( p_pgrm == NULL )
1752 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1754 i_pcr = (int64_t)va_arg( args, int64_t );
1755 /* search program */
1756 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1760 case ES_OUT_RESET_PCR:
1761 for( i = 0; i < p_sys->i_pgrm; i++ )
1762 input_ClockResetPCR( p_sys->p_input, &p_sys->pgrm[i]->clock );
1768 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1769 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1770 *pi_ts = input_ClockGetTS( p_sys->p_input,
1771 &p_sys->p_pgrm->clock, i_ts );
1774 return VLC_EGENERIC;
1776 case ES_OUT_GET_GROUP:
1777 pi = (int*) va_arg( args, int* );
1779 *pi = p_sys->p_pgrm->i_id;
1781 *pi = -1; /* FIXME */
1784 case ES_OUT_SET_GROUP:
1787 i = (int) va_arg( args, int );
1788 for( j = 0; j < p_sys->i_pgrm; j++ )
1790 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1791 if( p_pgrm->i_id == i )
1793 EsOutProgramSelect( out, p_pgrm );
1797 return VLC_EGENERIC;
1800 case ES_OUT_SET_FMT:
1802 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1803 * to update the p_extra data */
1805 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1806 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1807 if( es == NULL ) return VLC_EGENERIC;
1809 if( p_fmt->i_extra )
1811 es->fmt.i_extra = p_fmt->i_extra;
1812 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1813 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1815 if( !es->p_dec ) return VLC_SUCCESS;
1818 input_DecoderDelete( es->p_dec );
1819 es->p_dec = input_DecoderNew( p_sys->p_input,
1820 &es->fmt, VLC_FALSE );
1823 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1824 es->p_dec->fmt_in.p_extra =
1825 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1826 memcpy( es->p_dec->fmt_in.p_extra,
1827 p_fmt->p_extra, p_fmt->i_extra );
1834 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1838 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1839 i_date = (int64_t)va_arg( args, int64_t );
1841 if( !es || !es->p_dec )
1842 return VLC_EGENERIC;
1844 /* XXX We should call input_ClockGetTS but PCR has been reseted
1845 * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1846 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1847 es->i_preroll_end = i_date;
1851 case ES_OUT_SET_GROUP_META:
1853 int i_group = (int)va_arg( args, int );
1854 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1856 EsOutProgramMeta( out, i_group, p_meta );
1859 case ES_OUT_SET_GROUP_EPG:
1861 int i_group = (int)va_arg( args, int );
1862 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1864 EsOutProgramEpg( out, i_group, p_epg );
1867 case ES_OUT_DEL_GROUP:
1869 int i_group = (int)va_arg( args, int );
1871 return EsOutProgramDel( out, i_group );
1875 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1876 return VLC_EGENERIC;
1880 /****************************************************************************
1881 * LanguageGetName: try to expend iso639 into plain name
1882 ****************************************************************************/
1883 static char *LanguageGetName( const char *psz_code )
1885 const iso639_lang_t *pl;
1887 if( psz_code == NULL )
1889 return strdup( "" );
1892 if( strlen( psz_code ) == 2 )
1894 pl = GetLang_1( psz_code );
1896 else if( strlen( psz_code ) == 3 )
1898 pl = GetLang_2B( psz_code );
1899 if( !strcmp( pl->psz_iso639_1, "??" ) )
1901 pl = GetLang_2T( psz_code );
1906 return strdup( psz_code );
1909 if( !strcmp( pl->psz_iso639_1, "??" ) )
1911 return strdup( psz_code );
1915 if( *pl->psz_native_name )
1917 return strdup( pl->psz_native_name );
1919 return strdup( pl->psz_eng_name );
1923 /* Get a 2 char code */
1924 static char *LanguageGetCode( const char *psz_lang )
1926 const iso639_lang_t *pl;
1928 if( psz_lang == NULL || *psz_lang == '\0' )
1929 return strdup("??");
1931 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1933 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1934 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1935 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1936 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1937 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1941 if( pl->psz_iso639_1 != NULL )
1942 return strdup( pl->psz_iso639_1 );
1944 return strdup("??");
1947 static char **LanguageSplit( const char *psz_langs )
1954 if( psz_langs == NULL ) return NULL;
1956 psz_parser = psz_dup = strdup(psz_langs);
1958 while( psz_parser && *psz_parser )
1963 psz = strchr(psz_parser, ',' );
1964 if( psz ) *psz++ = '\0';
1966 psz_code = LanguageGetCode( psz_parser );
1967 if( strcmp( psz_code, "??" ) )
1969 TAB_APPEND( i_psz, ppsz, psz_code );
1977 TAB_APPEND( i_psz, ppsz, NULL );
1984 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1988 if( !ppsz_langs || !psz_lang ) return -1;
1990 for( i = 0; ppsz_langs[i]; i++ )
1991 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1996 /****************************************************************************
1998 * - add meta info to the playlist item
1999 ****************************************************************************/
2000 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2002 es_out_sys_t *p_sys = out->p_sys;
2003 input_thread_t *p_input = p_sys->p_input;
2004 es_format_t *fmt = &es->fmt;
2008 /* Add stream info */
2009 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
2011 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2012 "%.4s", (char*)&fmt->i_codec );
2014 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2015 "%s", es->psz_language );
2017 /* Add information */
2018 switch( fmt->i_cat )
2021 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2022 _("Type"), _("Audio") );
2024 if( fmt->audio.i_channels > 0 )
2025 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2026 "%u", fmt->audio.i_channels );
2028 if( fmt->audio.i_rate > 0 )
2030 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2031 _("%u Hz"), fmt->audio.i_rate );
2032 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2035 if( fmt->audio.i_bitspersample > 0 )
2036 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2037 _("Bits per sample"), "%u",
2038 fmt->audio.i_bitspersample );
2040 if( fmt->i_bitrate > 0 )
2042 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2043 _("%u kb/s"), fmt->i_bitrate / 1000 );
2044 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2049 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2050 _("Type"), _("Video") );
2052 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2053 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2054 _("Resolution"), "%ux%u",
2055 fmt->video.i_width, fmt->video.i_height );
2057 if( fmt->video.i_visible_width > 0 &&
2058 fmt->video.i_visible_height > 0 )
2059 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2060 _("Display resolution"), "%ux%u",
2061 fmt->video.i_visible_width,
2062 fmt->video.i_visible_height);
2063 if( fmt->video.i_frame_rate > 0 &&
2064 fmt->video.i_frame_rate_base > 0 )
2066 div = lldiv( (float)fmt->video.i_frame_rate /
2067 fmt->video.i_frame_rate_base * 1000000,
2069 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2070 _("Frame rate"), I64Fd".%06u",
2071 div.quot, (unsigned int )div.rem );
2076 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2077 _("Type"), _("Subtitle") );