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 if( val.psz_string ) 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 if( val.psz_string ) free( val.psz_string );
243 p_sys->ppsz_sub_language = NULL;
244 p_sys->ppsz_audio_language = NULL;
247 var_Get( p_input, "audio-track-id", &val );
248 p_sys->i_audio_id = val.i_int;
250 var_Get( p_input, "sub-track-id", &val );
251 p_sys->i_sub_id = val.i_int;
253 p_sys->p_es_audio = NULL;
254 p_sys->p_es_video = NULL;
255 p_sys->p_es_sub = NULL;
257 p_sys->i_audio_delay= 0;
258 p_sys->i_spu_delay = 0;
260 p_sys->i_rate = i_rate;
265 /*****************************************************************************
267 *****************************************************************************/
268 void input_EsOutDelete( es_out_t *out )
270 es_out_sys_t *p_sys = out->p_sys;
273 for( i = 0; i < p_sys->i_es; i++ )
275 if( p_sys->es[i]->p_dec )
277 input_DecoderDelete( p_sys->es[i]->p_dec );
279 if( p_sys->es[i]->psz_language )
280 free( p_sys->es[i]->psz_language );
281 if( p_sys->es[i]->psz_language_code )
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 );
303 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
304 for( i = 0; i < p_sys->i_pgrm; i++ )
306 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
307 if( p_pgrm->psz_now_playing )
308 free( p_pgrm->psz_now_playing );
309 if( p_pgrm->psz_publisher )
310 free( p_pgrm->psz_publisher );
311 if( p_pgrm->psz_name )
312 free( p_pgrm->psz_name );
314 vlc_epg_Delete( p_pgrm->p_epg );
318 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
324 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
329 /* Special HACK, -i_id is tha cat of the stream */
330 return (es_out_id_t*)((uint8_t*)NULL-i_id);
333 for( i = 0; i < out->p_sys->i_es; i++ )
335 if( out->p_sys->es[i]->i_id == i_id )
336 return out->p_sys->es[i];
341 static void EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_flush, vlc_bool_t b_audio )
343 es_out_sys_t *p_sys = out->p_sys;
346 for( i = 0; i < p_sys->i_es; i++ )
348 es_out_id_t *es = p_sys->es[i];
350 /* Send a dummy block to let decoder know that
351 * there is a discontinuity */
352 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
353 input_DecoderDiscontinuity( es->p_dec, b_flush );
356 void input_EsOutChangeRate( es_out_t *out, int i_rate )
358 es_out_sys_t *p_sys = out->p_sys;
361 p_sys->i_rate = i_rate;
362 EsOutDiscontinuity( out, VLC_FALSE, VLC_FALSE );
364 for( i = 0; i < p_sys->i_pgrm; i++ )
365 input_ClockSetRate( &p_sys->pgrm[i]->clock, i_rate );
368 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
370 es_out_sys_t *p_sys = out->p_sys;
372 if( i_cat == AUDIO_ES )
373 p_sys->i_audio_delay = i_delay;
374 else if( i_cat == SPU_ES )
375 p_sys->i_spu_delay = i_delay;
377 void input_EsOutChangeState( es_out_t *out )
379 es_out_sys_t *p_sys = out->p_sys;
380 input_thread_t *p_input = p_sys->p_input;
382 if( p_input->i_state == PAUSE_S )
384 /* Send discontinuity to decoders (it will allow them to flush
385 * * if implemented */
386 EsOutDiscontinuity( out, VLC_FALSE, VLC_FALSE );
390 /* Out of pause, reset pcr */
391 es_out_Control( out, ES_OUT_RESET_PCR );
394 void input_EsOutChangePosition( es_out_t *out )
396 //es_out_sys_t *p_sys = out->p_sys;
398 es_out_Control( out, ES_OUT_RESET_PCR );
399 EsOutDiscontinuity( out, VLC_TRUE, VLC_FALSE );
402 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
404 es_out_sys_t *p_sys = out->p_sys;
407 for( i = 0; i < p_sys->i_es; i++ )
409 es_out_id_t *es = p_sys->es[i];
411 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
417 /*****************************************************************************
419 *****************************************************************************/
420 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
421 vlc_bool_t b_delete )
423 es_out_sys_t *p_sys = out->p_sys;
424 input_thread_t *p_input = p_sys->p_input;
425 vlc_value_t val, text;
429 if( fmt->i_cat == AUDIO_ES )
430 psz_var = "audio-es";
431 else if( fmt->i_cat == VIDEO_ES )
432 psz_var = "video-es";
433 else if( fmt->i_cat == SPU_ES )
441 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
442 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
446 /* Get the number of ES already added */
447 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
452 /* First one, we need to add the "Disable" choice */
453 val2.i_int = -1; text.psz_string = _("Disable");
454 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
458 /* Take care of the ES description */
459 if( fmt->psz_description && *fmt->psz_description )
461 if( psz_language && *psz_language )
463 text.psz_string = malloc( strlen( fmt->psz_description) +
464 strlen( psz_language ) + 10 );
465 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
468 else text.psz_string = strdup( fmt->psz_description );
472 if( psz_language && *psz_language )
475 text.psz_string = malloc( strlen( _("Track %i") )+
476 strlen( psz_language ) + 30 );
477 asprintf( &temp, _("Track %i"), val.i_int );
478 sprintf( text.psz_string, "%s - [%s]", temp, psz_language );
483 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
484 sprintf( text.psz_string, _("Track %i"), val.i_int );
489 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
491 free( text.psz_string );
493 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
496 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
497 vlc_bool_t b_delete )
499 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
502 /* EsOutProgramSelect:
503 * Select a program and update the object variable
505 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
507 es_out_sys_t *p_sys = out->p_sys;
508 input_thread_t *p_input = p_sys->p_input;
512 if( p_sys->p_pgrm == p_pgrm )
513 return; /* Nothing to do */
517 es_out_pgrm_t *old = p_sys->p_pgrm;
518 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
520 for( i = 0; i < p_sys->i_es; i++ )
522 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
523 p_sys->i_mode != ES_OUT_MODE_ALL )
524 EsUnselect( out, p_sys->es[i], VLC_TRUE );
527 p_sys->p_es_audio = NULL;
528 p_sys->p_es_sub = NULL;
529 p_sys->p_es_video = NULL;
532 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
534 /* Mark it selected */
535 p_pgrm->b_selected = VLC_TRUE;
537 /* Switch master stream */
538 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
540 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
542 p_pgrm->clock.b_master = VLC_TRUE;
543 p_sys->p_pgrm = p_pgrm;
545 /* Update "program" */
546 val.i_int = p_pgrm->i_id;
547 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
550 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
551 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
552 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
553 for( i = 0; i < p_sys->i_es; i++ )
555 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
556 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
557 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
560 /* Update now playing */
561 input_item_SetNowPlaying( p_input->p->input.p_item,
562 p_pgrm->psz_now_playing );
563 input_item_SetPublisher( p_input->p->input.p_item,
564 p_pgrm->psz_publisher );
566 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
572 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
574 es_out_sys_t *p_sys = out->p_sys;
575 input_thread_t *p_input = p_sys->p_input;
578 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
581 p_pgrm->i_id = i_group;
583 p_pgrm->b_selected = VLC_FALSE;
584 p_pgrm->psz_name = NULL;
585 p_pgrm->psz_now_playing = NULL;
586 p_pgrm->psz_publisher = NULL;
587 p_pgrm->p_epg = NULL;
588 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average, p_sys->i_rate );
591 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
593 /* Update "program" variable */
595 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
597 if( i_group == var_GetInteger( p_input, "program" ) )
599 EsOutProgramSelect( out, p_pgrm );
603 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
611 static int EsOutProgramDel( es_out_t *out, int i_group )
613 es_out_sys_t *p_sys = out->p_sys;
614 input_thread_t *p_input = p_sys->p_input;
615 es_out_pgrm_t *p_pgrm = NULL;
619 for( i = 0; i < p_sys->i_pgrm; i++ )
621 if( p_sys->pgrm[i]->i_id == i_group )
623 p_pgrm = p_sys->pgrm[i];
633 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
634 i_group, p_pgrm->i_es );
638 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
640 /* If program is selected we need to unselect it */
641 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
643 if( p_pgrm->psz_name ) free( p_pgrm->psz_name );
644 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
645 if( p_pgrm->psz_publisher ) free( p_pgrm->psz_publisher );
647 vlc_epg_Delete( p_pgrm->p_epg );
650 /* Update "program" variable */
652 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
654 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
661 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
664 if( p_pgrm->psz_name )
665 asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
667 asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
671 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
673 es_out_sys_t *p_sys = out->p_sys;
674 es_out_pgrm_t *p_pgrm = NULL;
675 input_thread_t *p_input = p_sys->p_input;
677 const char *psz_title = NULL;
678 const char *psz_provider = NULL;
681 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
683 /* Check against empty meta data (empty for what we handle) */
684 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
685 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
686 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
687 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
692 for( i = 0; i < p_sys->i_pgrm; i++ )
694 if( p_sys->pgrm[i]->i_id == i_group )
696 p_pgrm = p_sys->pgrm[i];
701 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
704 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
705 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
707 /* Update the description text of the program */
708 if( psz_title && *psz_title )
713 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
715 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
717 /* Remove old entries */
718 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
719 /* TODO update epg name */
722 if( p_pgrm->psz_name ) free( p_pgrm->psz_name );
723 p_pgrm->psz_name = strdup( psz_title );
725 /* ugly but it works */
727 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
729 if( psz_provider && *psz_provider )
731 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
732 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
733 free( text.psz_string );
737 text.psz_string = (char *)psz_title;
738 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
742 psz_cat = EsOutProgramGetMetaName( p_pgrm );
745 if( p_sys->p_pgrm == p_pgrm )
746 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
747 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
749 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
750 for( i = 0; ppsz_all_keys[i]; i++ )
752 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
753 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
754 free( ppsz_all_keys[i] );
756 free( ppsz_all_keys );
761 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
766 for( i = 0; i < p_src->i_event; i++ )
768 vlc_epg_event_t *p_evt = p_src->pp_event[i];
769 vlc_bool_t b_add = VLC_TRUE;
772 for( j = 0; j < p_dst->i_event; j++ )
774 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
779 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
784 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
787 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
788 p_copy->i_start = p_evt->i_start;
789 p_copy->i_duration = p_evt->i_duration;
790 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
791 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
792 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
793 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
797 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
799 /* Keep only 1 old event */
800 if( p_dst->p_current )
802 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
803 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
807 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
809 es_out_sys_t *p_sys = out->p_sys;
810 input_thread_t *p_input = p_sys->p_input;
811 es_out_pgrm_t *p_pgrm = NULL;
816 for( i = 0; i < p_sys->i_pgrm; i++ )
818 if( p_sys->pgrm[i]->i_id == i_group )
820 p_pgrm = p_sys->pgrm[i];
825 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
829 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
830 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
833 psz_cat = EsOutProgramGetMetaName( p_pgrm );
834 #ifdef HAVE_LOCALTIME_R
836 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
838 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
839 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
840 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
842 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
843 time_t t_start = (time_t)p_evt->i_start;
847 localtime_r( &t_start, &tm_start );
849 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
850 if( p_evt->psz_short_description || p_evt->psz_description )
851 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
853 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
854 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
856 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
858 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
862 /* Update now playing */
863 if( p_pgrm->psz_now_playing )
864 free( p_pgrm->psz_now_playing );
865 p_pgrm->psz_now_playing = NULL;
866 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
867 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
869 if( p_pgrm == p_sys->p_pgrm )
870 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
872 if( p_pgrm->psz_now_playing )
874 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
875 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
876 p_pgrm->psz_now_playing );
880 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
881 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
890 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
892 es_out_sys_t *p_sys = out->p_sys;
893 input_thread_t *p_input = p_sys->p_input;
895 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
896 es_out_pgrm_t *p_pgrm = NULL;
899 if( fmt->i_group < 0 )
901 msg_Err( p_input, "invalid group number" );
905 /* Search the program */
906 for( i = 0; i < p_sys->i_pgrm; i++ )
908 if( fmt->i_group == p_sys->pgrm[i]->i_id )
910 p_pgrm = p_sys->pgrm[i];
916 /* Create a new one */
917 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
920 /* Increase ref count for program */
925 fmt->i_id = out->p_sys->i_id;
926 es->i_id = fmt->i_id;
928 es_format_Copy( &es->fmt, fmt );
929 es->i_preroll_end = -1;
935 audio_replay_gain_t rg;
937 es->i_channel = p_sys->i_audio;
939 vlc_mutex_lock( &p_input->p->input.p_item->lock );
940 memset( &rg, 0, sizeof(rg) );
941 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
942 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
944 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
946 if( !es->fmt.audio_replay_gain.pb_peak[i] )
948 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
949 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
951 if( !es->fmt.audio_replay_gain.pb_gain[i] )
953 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
954 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
961 es->i_channel = p_sys->i_video;
962 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
963 vlc_ureduce( &es->fmt.video.i_frame_rate,
964 &es->fmt.video.i_frame_rate_base,
965 fmt->video.i_frame_rate,
966 fmt->video.i_frame_rate_base, 0 );
970 es->i_channel = p_sys->i_sub;
977 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
978 es->psz_language_code = LanguageGetCode( fmt->psz_language );
980 for( i = 0; i < 4; i++ )
981 es->pb_cc_present[i] = VLC_FALSE;
982 es->p_master = VLC_FALSE;
984 if( es->p_pgrm == p_sys->p_pgrm )
985 EsOutESVarUpdate( out, es, VLC_FALSE );
987 /* Select it if needed */
988 EsOutSelect( out, es, VLC_FALSE );
991 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
992 p_sys->i_id++; /* always incremented */
1006 EsOutAddInfo( out, es );
1011 static vlc_bool_t EsIsSelected( es_out_id_t *es )
1015 vlc_bool_t b_decode = VLC_FALSE;
1016 if( es->p_master->p_dec )
1018 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1019 if( i_channel != -1 )
1020 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1026 return es->p_dec != NULL;
1029 static void EsSelect( es_out_t *out, es_out_id_t *es )
1031 es_out_sys_t *p_sys = out->p_sys;
1032 input_thread_t *p_input = p_sys->p_input;
1034 const char *psz_var;
1036 if( EsIsSelected( es ) )
1038 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1045 if( !es->p_master->p_dec )
1048 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1049 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, VLC_TRUE, i_channel ) )
1054 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1056 if( !var_GetBool( p_input, "video" ) ||
1057 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
1059 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1064 else if( es->fmt.i_cat == AUDIO_ES )
1066 var_Get( p_input, "audio", &val );
1067 if( !var_GetBool( p_input, "audio" ) ||
1068 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
1070 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1075 if( es->fmt.i_cat == SPU_ES )
1077 var_Get( p_input, "spu", &val );
1078 if( !var_GetBool( p_input, "spu" ) ||
1079 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
1081 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1087 es->i_preroll_end = -1;
1088 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
1089 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1093 if( es->fmt.i_cat == VIDEO_ES )
1094 psz_var = "video-es";
1095 else if( es->fmt.i_cat == AUDIO_ES )
1096 psz_var = "audio-es";
1097 else if( es->fmt.i_cat == SPU_ES )
1102 /* Mark it as selected */
1103 val.i_int = es->i_id;
1104 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1106 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1109 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
1111 es_out_sys_t *p_sys = out->p_sys;
1112 input_thread_t *p_input = p_sys->p_input;
1114 const char *psz_var;
1116 if( !EsIsSelected( es ) )
1118 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1124 if( es->p_master->p_dec )
1126 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1127 if( i_channel != -1 )
1128 input_DecoderSetCcState( es->p_master->p_dec, VLC_FALSE, i_channel );
1133 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1135 for( i = 0; i < 4; i++ )
1137 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1140 if( i_spu_id == es->pp_cc_es[i]->i_id )
1142 /* Force unselection of the CC */
1144 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1146 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1148 EsOutDel( out, es->pp_cc_es[i] );
1150 es->pb_cc_present[i] = VLC_FALSE;
1152 input_DecoderDelete( es->p_dec );
1160 if( es->p_dec == NULL )
1162 if( es->fmt.i_cat == VIDEO_ES )
1163 psz_var = "video-es";
1164 else if( es->fmt.i_cat == AUDIO_ES )
1165 psz_var = "audio-es";
1166 else if( es->fmt.i_cat == SPU_ES )
1171 /* Mark it as unselected */
1173 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1175 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1179 * Select an ES given the current mode
1180 * XXX: you need to take a the lock before (stream.stream_lock)
1182 * \param out The es_out structure
1183 * \param es es_out_id structure
1184 * \param b_force ...
1187 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
1189 es_out_sys_t *p_sys = out->p_sys;
1191 int i_cat = es->fmt.i_cat;
1193 if( !p_sys->b_active ||
1194 ( !b_force && es->fmt.i_priority < 0 ) )
1199 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1201 if( !EsIsSelected( es ) )
1202 EsSelect( out, es );
1204 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1208 var_Get( p_sys->p_input, "programs", &val );
1209 for ( i = 0; i < val.p_list->i_count; i++ )
1211 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1213 if( !EsIsSelected( es ) )
1214 EsSelect( out, es );
1218 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1220 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1224 if( es->p_pgrm != p_sys->p_pgrm )
1227 if( i_cat == AUDIO_ES )
1229 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1230 es->psz_language_code );
1232 if( p_sys->p_es_audio &&
1233 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1235 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1236 p_sys->p_es_audio->psz_language_code );
1238 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1240 i_wanted = es->i_channel;
1244 /* Select audio if (no audio selected yet)
1245 * - no audio-language
1246 * - no audio code for the ES
1247 * - audio code in the requested list */
1249 !strcmp( es->psz_language_code, "??" ) ||
1250 !p_sys->ppsz_audio_language )
1251 i_wanted = es->i_channel;
1254 if( p_sys->i_audio_last >= 0 )
1255 i_wanted = p_sys->i_audio_last;
1257 if( p_sys->i_audio_id >= 0 )
1259 if( es->i_id == p_sys->i_audio_id )
1260 i_wanted = es->i_channel;
1265 else if( i_cat == SPU_ES )
1267 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1268 es->psz_language_code );
1270 if( p_sys->p_es_sub &&
1271 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1273 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1274 p_sys->p_es_sub->psz_language_code );
1276 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1277 idx1, es->psz_language_code, idx2,
1278 p_sys->p_es_sub->psz_language_code );
1280 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1282 /* We found a SPU that matches our language request */
1283 i_wanted = es->i_channel;
1285 else if( idx1 >= 0 )
1287 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1288 idx1, es->psz_language_code );
1290 i_wanted = es->i_channel;
1292 else if( p_sys->i_default_sub_id >= 0 )
1294 if( es->i_id == p_sys->i_default_sub_id )
1295 i_wanted = es->i_channel;
1298 if( p_sys->i_sub_last >= 0 )
1299 i_wanted = p_sys->i_sub_last;
1301 if( p_sys->i_sub_id >= 0 )
1303 if( es->i_id == p_sys->i_sub_id )
1304 i_wanted = es->i_channel;
1309 else if( i_cat == VIDEO_ES )
1311 i_wanted = es->i_channel;
1314 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1315 EsSelect( out, es );
1318 /* FIXME TODO handle priority here */
1319 if( EsIsSelected( es ) )
1321 if( i_cat == AUDIO_ES )
1323 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1324 p_sys->p_es_audio &&
1325 p_sys->p_es_audio != es &&
1326 EsIsSelected( p_sys->p_es_audio ) )
1328 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
1330 p_sys->p_es_audio = es;
1332 else if( i_cat == SPU_ES )
1334 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1336 p_sys->p_es_sub != es &&
1337 EsIsSelected( p_sys->p_es_sub ) )
1339 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1341 p_sys->p_es_sub = es;
1343 else if( i_cat == VIDEO_ES )
1345 p_sys->p_es_video = es;
1351 * Send a block for the given es_out
1353 * \param out the es_out to send from
1354 * \param es the es_out_id
1355 * \param p_block the data block to send
1357 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1359 es_out_sys_t *p_sys = out->p_sys;
1360 input_thread_t *p_input = p_sys->p_input;
1361 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1365 if( es->fmt.i_cat == AUDIO_ES )
1366 i_delay = p_sys->i_audio_delay;
1367 else if( es->fmt.i_cat == SPU_ES )
1368 i_delay = p_sys->i_spu_delay;
1372 if( p_input->p_libvlc->b_stats )
1374 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1375 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1376 p_block->i_buffer, &i_total );
1377 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1378 (float)i_total, NULL );
1379 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1382 /* Mark preroll blocks */
1383 if( es->i_preroll_end >= 0 )
1385 int64_t i_date = p_block->i_pts;
1387 i_date = p_block->i_dts;
1389 if( i_date < es->i_preroll_end )
1390 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1392 es->i_preroll_end = -1;
1395 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1397 p_block->i_dts += i_delay;
1399 else if( p_block->i_dts > 0 )
1402 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1404 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1406 p_block->i_pts += i_delay;
1408 else if( p_block->i_pts > 0 )
1411 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1413 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1415 mtime_t current_date = mdate();
1417 || p_block->i_pts > current_date + 10000000
1418 || current_date > p_block->i_pts )
1420 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1421 * for teletext streams. */
1422 p_block->i_pts = current_date + 400000
1423 + p_input->i_pts_delay + i_delay;
1427 p_block->i_rate = p_sys->i_rate;
1429 /* TODO handle mute */
1431 ( es->fmt.i_cat != AUDIO_ES ||
1432 ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1433 p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1435 vlc_bool_t pb_cc[4];
1436 vlc_bool_t b_cc_new = VLC_FALSE;
1438 input_DecoderDecode( es->p_dec, p_block );
1440 /* Check CC status */
1441 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1442 for( i = 0; i < 4; i++ )
1444 static const vlc_fourcc_t fcc[4] = {
1445 VLC_FOURCC('c', 'c', '1', ' '),
1446 VLC_FOURCC('c', 'c', '2', ' '),
1447 VLC_FOURCC('c', 'c', '3', ' '),
1448 VLC_FOURCC('c', 'c', '4', ' '),
1450 static const char *ppsz_description[4] = {
1451 N_("Closed captions 1"),
1452 N_("Closed captions 2"),
1453 N_("Closed captions 3"),
1454 N_("Closed captions 4"),
1458 if( es->pb_cc_present[i] || !pb_cc[i] )
1460 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1462 es_format_Init( &fmt, SPU_ES, fcc[i] );
1463 fmt.i_group = es->fmt.i_group;
1464 fmt.psz_description = strdup( _(ppsz_description[i] ) );
1465 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1466 es->pp_cc_es[i]->p_master = es;
1467 es_format_Clean( &fmt );
1470 es->pb_cc_present[i] = VLC_TRUE;
1471 b_cc_new = VLC_TRUE;
1474 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1478 block_Release( p_block );
1484 /*****************************************************************************
1486 *****************************************************************************/
1487 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1489 es_out_sys_t *p_sys = out->p_sys;
1490 vlc_bool_t b_reselect = VLC_FALSE;
1493 /* We don't try to reselect */
1496 while( !out->p_sys->p_input->b_die && es->p_dec )
1498 if( input_DecoderEmpty( es->p_dec ) )
1502 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1505 if( es->p_pgrm == p_sys->p_pgrm )
1506 EsOutESVarUpdate( out, es, VLC_TRUE );
1508 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1511 if( es->p_pgrm->i_es == 0 )
1513 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1516 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1517 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1519 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1520 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1521 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1523 switch( es->fmt.i_cat )
1536 /* Re-select another track when needed */
1538 for( i = 0; i < p_sys->i_es; i++ )
1540 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1541 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1544 if( es->psz_language )
1545 free( es->psz_language );
1546 if( es->psz_language_code )
1547 free( es->psz_language_code );
1549 es_format_Clean( &es->fmt );
1555 * Control query handler
1557 * \param out the es_out to control
1558 * \param i_query A es_out query as defined in include/ninput.h
1559 * \param args a variable list of arguments for the query
1560 * \return VLC_SUCCESS or an error code
1562 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1564 es_out_sys_t *p_sys = out->p_sys;
1572 case ES_OUT_SET_ES_STATE:
1573 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1574 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1575 if( b && !EsIsSelected( es ) )
1577 EsSelect( out, es );
1578 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1580 else if( !b && EsIsSelected( es ) )
1582 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1587 case ES_OUT_GET_ES_STATE:
1588 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1589 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1591 *pb = EsIsSelected( es );
1594 case ES_OUT_SET_ACTIVE:
1596 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1597 p_sys->b_active = b;
1600 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1604 case ES_OUT_GET_ACTIVE:
1605 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1606 *pb = p_sys->b_active;
1609 case ES_OUT_SET_MODE:
1610 i = (int) va_arg( args, int );
1611 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1612 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1616 /* Reapply policy mode */
1617 for( i = 0; i < p_sys->i_es; i++ )
1619 if( EsIsSelected( p_sys->es[i] ) )
1621 EsUnselect( out, p_sys->es[i],
1622 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1625 for( i = 0; i < p_sys->i_es; i++ )
1627 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1631 return VLC_EGENERIC;
1633 case ES_OUT_GET_MODE:
1634 pi = (int*) va_arg( args, int* );
1635 *pi = p_sys->i_mode;
1639 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1640 /* Special case NULL, NULL+i_cat */
1643 for( i = 0; i < p_sys->i_es; i++ )
1645 if( EsIsSelected( 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+AUDIO_ES) )
1652 for( i = 0; i < p_sys->i_es; i++ )
1654 if( p_sys->es[i]->fmt.i_cat == AUDIO_ES &&
1655 EsIsSelected( 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+VIDEO_ES) )
1662 for( i = 0; i < p_sys->i_es; i++ )
1664 if( p_sys->es[i]->fmt.i_cat == VIDEO_ES &&
1665 EsIsSelected( p_sys->es[i] ) )
1666 EsUnselect( out, p_sys->es[i],
1667 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1670 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1672 for( i = 0; i < p_sys->i_es; i++ )
1674 if( p_sys->es[i]->fmt.i_cat == SPU_ES &&
1675 EsIsSelected( p_sys->es[i] ) )
1676 EsUnselect( out, p_sys->es[i],
1677 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1682 for( i = 0; i < p_sys->i_es; i++ )
1684 if( es == p_sys->es[i] )
1686 EsOutSelect( out, es, VLC_TRUE );
1692 playlist_t * p_playlist = pl_Yield( p_sys->p_input );
1694 p_playlist->gc_date = mdate();
1695 vlc_object_signal_unlocked( p_playlist );
1697 pl_Release( p_playlist );
1701 case ES_OUT_SET_DEFAULT:
1703 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1707 /*p_sys->i_default_video_id = -1;*/
1708 /*p_sys->i_default_audio_id = -1;*/
1709 p_sys->i_default_sub_id = -1;
1711 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1713 /*p_sys->i_default_video_id = -1;*/
1715 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1717 /*p_sys->i_default_audio_id = -1;*/
1719 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1721 p_sys->i_default_sub_id = -1;
1725 /*if( es->fmt.i_cat == VIDEO_ES )
1726 p_sys->i_default_video_id = es->i_id;
1728 if( es->fmt.i_cat == AUDIO_ES )
1729 p_sys->i_default_audio_id = es->i_id;
1731 if( es->fmt.i_cat == SPU_ES )
1732 p_sys->i_default_sub_id = es->i_id;
1737 case ES_OUT_SET_PCR:
1738 case ES_OUT_SET_GROUP_PCR:
1740 es_out_pgrm_t *p_pgrm = NULL;
1744 if( i_query == ES_OUT_SET_PCR )
1746 p_pgrm = p_sys->p_pgrm;
1751 i_group = (int)va_arg( args, int );
1752 for( i = 0; i < p_sys->i_pgrm; i++ )
1754 if( p_sys->pgrm[i]->i_id == i_group )
1756 p_pgrm = p_sys->pgrm[i];
1761 if( p_pgrm == NULL )
1762 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1764 i_pcr = (int64_t)va_arg( args, int64_t );
1765 /* search program */
1766 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1770 case ES_OUT_RESET_PCR:
1771 for( i = 0; i < p_sys->i_pgrm; i++ )
1772 input_ClockResetPCR( &p_sys->pgrm[i]->clock );
1778 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1779 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1780 *pi_ts = input_ClockGetTS( p_sys->p_input,
1781 &p_sys->p_pgrm->clock, i_ts );
1784 return VLC_EGENERIC;
1786 case ES_OUT_GET_GROUP:
1787 pi = (int*) va_arg( args, int* );
1789 *pi = p_sys->p_pgrm->i_id;
1791 *pi = -1; /* FIXME */
1794 case ES_OUT_SET_GROUP:
1797 i = (int) va_arg( args, int );
1798 for( j = 0; j < p_sys->i_pgrm; j++ )
1800 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1801 if( p_pgrm->i_id == i )
1803 EsOutProgramSelect( out, p_pgrm );
1807 return VLC_EGENERIC;
1810 case ES_OUT_SET_FMT:
1812 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1813 * to update the p_extra data */
1815 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1816 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1817 if( es == NULL ) return VLC_EGENERIC;
1819 if( p_fmt->i_extra )
1821 es->fmt.i_extra = p_fmt->i_extra;
1822 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1823 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1825 if( !es->p_dec ) return VLC_SUCCESS;
1828 input_DecoderDelete( es->p_dec );
1829 es->p_dec = input_DecoderNew( p_sys->p_input,
1830 &es->fmt, VLC_FALSE );
1833 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1834 es->p_dec->fmt_in.p_extra =
1835 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1836 memcpy( es->p_dec->fmt_in.p_extra,
1837 p_fmt->p_extra, p_fmt->i_extra );
1844 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1848 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1849 i_date = (int64_t)va_arg( args, int64_t );
1851 if( !es || !es->p_dec )
1852 return VLC_EGENERIC;
1854 /* XXX We should call input_ClockGetTS but PCR has been reseted
1855 * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1856 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1857 es->i_preroll_end = i_date;
1861 case ES_OUT_SET_GROUP_META:
1863 int i_group = (int)va_arg( args, int );
1864 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1866 EsOutProgramMeta( out, i_group, p_meta );
1869 case ES_OUT_SET_GROUP_EPG:
1871 int i_group = (int)va_arg( args, int );
1872 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1874 EsOutProgramEpg( out, i_group, p_epg );
1877 case ES_OUT_DEL_GROUP:
1879 int i_group = (int)va_arg( args, int );
1881 return EsOutProgramDel( out, i_group );
1885 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1886 return VLC_EGENERIC;
1890 /****************************************************************************
1891 * LanguageGetName: try to expend iso639 into plain name
1892 ****************************************************************************/
1893 static char *LanguageGetName( const char *psz_code )
1895 const iso639_lang_t *pl;
1897 if( psz_code == NULL )
1899 return strdup( "" );
1902 if( strlen( psz_code ) == 2 )
1904 pl = GetLang_1( psz_code );
1906 else if( strlen( psz_code ) == 3 )
1908 pl = GetLang_2B( psz_code );
1909 if( !strcmp( pl->psz_iso639_1, "??" ) )
1911 pl = GetLang_2T( psz_code );
1916 return strdup( psz_code );
1919 if( !strcmp( pl->psz_iso639_1, "??" ) )
1921 return strdup( psz_code );
1925 if( *pl->psz_native_name )
1927 return strdup( pl->psz_native_name );
1929 return strdup( pl->psz_eng_name );
1933 /* Get a 2 char code */
1934 static char *LanguageGetCode( const char *psz_lang )
1936 const iso639_lang_t *pl;
1938 if( psz_lang == NULL || *psz_lang == '\0' )
1939 return strdup("??");
1941 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1943 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1944 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1945 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1946 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1947 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1951 if( pl->psz_iso639_1 != NULL )
1952 return strdup( pl->psz_iso639_1 );
1954 return strdup("??");
1957 static char **LanguageSplit( const char *psz_langs )
1964 if( psz_langs == NULL ) return NULL;
1966 psz_parser = psz_dup = strdup(psz_langs);
1968 while( psz_parser && *psz_parser )
1973 psz = strchr(psz_parser, ',' );
1974 if( psz ) *psz++ = '\0';
1976 if( !strcmp( psz_parser, "any" ) )
1978 TAB_APPEND( i_psz, ppsz, strdup("any") );
1982 psz_code = LanguageGetCode( psz_parser );
1983 if( strcmp( psz_code, "??" ) )
1985 TAB_APPEND( i_psz, ppsz, psz_code );
1998 TAB_APPEND( i_psz, ppsz, NULL );
2005 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2009 if( !ppsz_langs || !psz_lang ) return -1;
2011 for( i = 0; ppsz_langs[i]; i++ )
2013 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2014 !strcasecmp( ppsz_langs[i], "any" ) )
2023 /****************************************************************************
2025 * - add meta info to the playlist item
2026 ****************************************************************************/
2027 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2029 es_out_sys_t *p_sys = out->p_sys;
2030 input_thread_t *p_input = p_sys->p_input;
2031 es_format_t *fmt = &es->fmt;
2035 /* Add stream info */
2036 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
2038 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2039 "%.4s", (char*)&fmt->i_codec );
2041 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2042 "%s", es->psz_language );
2044 /* Add information */
2045 switch( fmt->i_cat )
2048 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2049 _("Type"), _("Audio") );
2051 if( fmt->audio.i_channels > 0 )
2052 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2053 "%u", fmt->audio.i_channels );
2055 if( fmt->audio.i_rate > 0 )
2057 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2058 _("%u Hz"), fmt->audio.i_rate );
2059 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2062 if( fmt->audio.i_bitspersample > 0 )
2063 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2064 _("Bits per sample"), "%u",
2065 fmt->audio.i_bitspersample );
2067 if( fmt->i_bitrate > 0 )
2069 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2070 _("%u kb/s"), fmt->i_bitrate / 1000 );
2071 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2076 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2077 _("Type"), _("Video") );
2079 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2080 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2081 _("Resolution"), "%ux%u",
2082 fmt->video.i_width, fmt->video.i_height );
2084 if( fmt->video.i_visible_width > 0 &&
2085 fmt->video.i_visible_height > 0 )
2086 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2087 _("Display resolution"), "%ux%u",
2088 fmt->video.i_visible_width,
2089 fmt->video.i_visible_height);
2090 if( fmt->video.i_frame_rate > 0 &&
2091 fmt->video.i_frame_rate_base > 0 )
2093 div = lldiv( (float)fmt->video.i_frame_rate /
2094 fmt->video.i_frame_rate_base * 1000000,
2096 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2097 _("Frame rate"), I64Fd".%06u",
2098 div.quot, (unsigned int )div.rem );
2103 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2104 _("Type"), _("Subtitle") );