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_common.h>
36 #include <vlc_input.h>
37 #include <vlc_es_out.h>
38 #include <vlc_block.h>
41 #include "input_internal.h"
43 #include <vlc_iso_lang.h>
44 /* FIXME we should find a better way than including that */
45 #include "../text/iso-639_def.h"
47 /*****************************************************************************
49 *****************************************************************************/
55 /* Number of es for this pgrm */
60 /* Clock for this program */
64 char *psz_now_playing;
74 es_out_pgrm_t *p_pgrm;
77 int64_t i_preroll_end;
79 /* Channel in the track type */
83 char *psz_language_code;
87 /* Fields for Video with CC */
88 bool pb_cc_present[4];
89 es_out_id_t *pp_cc_es[4];
91 /* Field for CC track from a master video */
92 es_out_id_t *p_master;
97 input_thread_t *p_input;
101 es_out_pgrm_t **pgrm;
102 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
103 es_out_pgrm_t *p_pgrm; /* Master program */
120 int i_audio_last, i_audio_id;
121 int i_sub_last, i_sub_id;
122 int i_default_sub_id; /* As specified in container; if applicable */
123 char **ppsz_audio_language;
124 char **ppsz_sub_language;
126 /* current main es */
127 es_out_id_t *p_es_audio;
128 es_out_id_t *p_es_video;
129 es_out_id_t *p_es_sub;
132 int64_t i_audio_delay;
135 /* Rate used to rescale ES ts */
139 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
140 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
141 static void EsOutDel ( es_out_t *, es_out_id_t * );
142 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
143 static int EsOutControl( es_out_t *, int i_query, va_list );
145 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
147 static bool EsIsSelected( es_out_id_t *es );
148 static void EsSelect( es_out_t *out, es_out_id_t *es );
149 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
150 static char *LanguageGetName( const char *psz_code );
151 static char *LanguageGetCode( const char *psz_lang );
152 static char **LanguageSplit( const char *psz_langs );
153 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
155 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
157 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
158 VLC_FOURCC('c', 'c', '1', ' '),
159 VLC_FOURCC('c', 'c', '2', ' '),
160 VLC_FOURCC('c', 'c', '3', ' '),
161 VLC_FOURCC('c', 'c', '4', ' '),
163 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
166 for( i = 0; i < 4; i++ )
168 if( fcc == EsOutFourccClosedCaptions[i] )
175 /*****************************************************************************
177 *****************************************************************************/
178 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
183 es_out_t *out = malloc( sizeof( es_out_t ) );
184 if( !out ) return NULL;
186 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
193 out->pf_add = EsOutAdd;
194 out->pf_send = EsOutSend;
195 out->pf_del = EsOutDel;
196 out->pf_control = EsOutControl;
198 out->b_sout = p_input->p->p_sout != NULL;
200 p_sys->p_input = p_input;
202 p_sys->b_active = false;
203 p_sys->i_mode = ES_OUT_MODE_AUTO;
206 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
207 p_sys->p_pgrm = NULL;
211 TAB_INIT( p_sys->i_es, p_sys->es );
218 var_Get( p_input, "audio-track", &val );
219 p_sys->i_audio_last = val.i_int;
221 var_Get( p_input, "sub-track", &val );
222 p_sys->i_sub_last = val.i_int;
224 p_sys->i_default_sub_id = -1;
226 if( !p_input->b_preparsing )
228 var_Get( p_input, "audio-language", &val );
229 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
230 if( p_sys->ppsz_audio_language )
232 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
233 msg_Dbg( p_input, "selected audio language[%d] %s",
234 i, p_sys->ppsz_audio_language[i] );
236 free( val.psz_string );
238 var_Get( p_input, "sub-language", &val );
239 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
240 if( p_sys->ppsz_sub_language )
242 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
243 msg_Dbg( p_input, "selected subtitle language[%d] %s",
244 i, p_sys->ppsz_sub_language[i] );
246 free( val.psz_string );
250 p_sys->ppsz_sub_language = NULL;
251 p_sys->ppsz_audio_language = NULL;
254 var_Get( p_input, "audio-track-id", &val );
255 p_sys->i_audio_id = val.i_int;
257 var_Get( p_input, "sub-track-id", &val );
258 p_sys->i_sub_id = val.i_int;
260 p_sys->p_es_audio = NULL;
261 p_sys->p_es_video = NULL;
262 p_sys->p_es_sub = NULL;
264 p_sys->i_audio_delay= 0;
265 p_sys->i_spu_delay = 0;
267 p_sys->i_rate = i_rate;
272 /*****************************************************************************
274 *****************************************************************************/
275 void input_EsOutDelete( es_out_t *out )
277 es_out_sys_t *p_sys = out->p_sys;
280 for( i = 0; i < p_sys->i_es; i++ )
282 if( p_sys->es[i]->p_dec )
284 input_DecoderDelete( p_sys->es[i]->p_dec );
286 free( p_sys->es[i]->psz_language );
287 free( p_sys->es[i]->psz_language_code );
288 es_format_Clean( &p_sys->es[i]->fmt );
290 free( p_sys->es[i] );
292 if( p_sys->ppsz_audio_language )
294 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
295 free( p_sys->ppsz_audio_language[i] );
296 free( p_sys->ppsz_audio_language );
298 if( p_sys->ppsz_sub_language )
300 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
301 free( p_sys->ppsz_sub_language[i] );
302 free( p_sys->ppsz_sub_language );
307 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
308 for( i = 0; i < p_sys->i_pgrm; i++ )
310 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
311 free( p_pgrm->psz_now_playing );
312 free( p_pgrm->psz_publisher );
313 free( p_pgrm->psz_name );
315 vlc_epg_Delete( p_pgrm->p_epg );
319 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
325 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
330 /* Special HACK, -i_id is tha cat of the stream */
331 return (es_out_id_t*)((uint8_t*)NULL-i_id);
334 for( i = 0; i < out->p_sys->i_es; i++ )
336 if( out->p_sys->es[i]->i_id == i_id )
337 return out->p_sys->es[i];
342 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
344 es_out_sys_t *p_sys = out->p_sys;
347 for( i = 0; i < p_sys->i_es; i++ )
349 es_out_id_t *es = p_sys->es[i];
351 /* Send a dummy block to let decoder know that
352 * there is a discontinuity */
353 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
354 input_DecoderDiscontinuity( es->p_dec, b_flush );
357 void input_EsOutChangeRate( es_out_t *out, int i_rate )
359 es_out_sys_t *p_sys = out->p_sys;
362 p_sys->i_rate = i_rate;
363 EsOutDiscontinuity( out, false, false );
365 for( i = 0; i < p_sys->i_pgrm; i++ )
366 input_ClockSetRate( &p_sys->pgrm[i]->clock, i_rate );
369 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
371 es_out_sys_t *p_sys = out->p_sys;
373 if( i_cat == AUDIO_ES )
374 p_sys->i_audio_delay = i_delay;
375 else if( i_cat == SPU_ES )
376 p_sys->i_spu_delay = i_delay;
378 void input_EsOutChangeState( es_out_t *out )
380 es_out_sys_t *p_sys = out->p_sys;
381 input_thread_t *p_input = p_sys->p_input;
383 if( p_input->i_state == PAUSE_S )
385 /* Send discontinuity to decoders (it will allow them to flush
386 * * if implemented */
387 EsOutDiscontinuity( out, false, false );
391 /* Out of pause, reset pcr */
392 es_out_Control( out, ES_OUT_RESET_PCR );
395 void input_EsOutChangePosition( es_out_t *out )
397 //es_out_sys_t *p_sys = out->p_sys;
399 es_out_Control( out, ES_OUT_RESET_PCR );
400 EsOutDiscontinuity( out, true, false );
403 bool input_EsOutDecodersEmpty( es_out_t *out )
405 es_out_sys_t *p_sys = out->p_sys;
408 for( i = 0; i < p_sys->i_es; i++ )
410 es_out_id_t *es = p_sys->es[i];
412 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
418 /*****************************************************************************
420 *****************************************************************************/
421 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
424 es_out_sys_t *p_sys = out->p_sys;
425 input_thread_t *p_input = p_sys->p_input;
426 const bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
427 vlc_value_t val, text;
431 if( fmt->i_cat == AUDIO_ES )
432 psz_var = "audio-es";
433 else if( fmt->i_cat == VIDEO_ES )
434 psz_var = "video-es";
435 else if( fmt->i_cat == SPU_ES )
443 var_SetInteger( p_sys->p_input, "teletext-es", -1 );
446 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
448 var_SetBool( p_sys->p_input, "intf-change", true );
452 /* Get the number of ES already added */
453 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
458 /* First one, we need to add the "Disable" choice */
459 val2.i_int = -1; text.psz_string = _("Disable");
460 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
464 /* Take care of the ES description */
465 if( fmt->psz_description && *fmt->psz_description )
467 if( psz_language && *psz_language )
469 text.psz_string = malloc( strlen( fmt->psz_description) +
470 strlen( psz_language ) + 10 );
471 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
474 else text.psz_string = strdup( fmt->psz_description );
478 if( psz_language && *psz_language )
480 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
481 text.psz_string = NULL;
485 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
486 text.psz_string = NULL;
491 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
493 free( text.psz_string );
496 var_SetInteger( p_sys->p_input, "teletext-es", i_id );
498 var_SetBool( p_sys->p_input, "intf-change", true );
501 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
504 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
507 /* EsOutProgramSelect:
508 * Select a program and update the object variable
510 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
512 es_out_sys_t *p_sys = out->p_sys;
513 input_thread_t *p_input = p_sys->p_input;
517 if( p_sys->p_pgrm == p_pgrm )
518 return; /* Nothing to do */
522 es_out_pgrm_t *old = p_sys->p_pgrm;
523 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
525 for( i = 0; i < p_sys->i_es; i++ )
527 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
528 p_sys->i_mode != ES_OUT_MODE_ALL )
529 EsUnselect( out, p_sys->es[i], true );
532 p_sys->p_es_audio = NULL;
533 p_sys->p_es_sub = NULL;
534 p_sys->p_es_video = NULL;
537 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
539 /* Mark it selected */
540 p_pgrm->b_selected = true;
542 /* Switch master stream */
543 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
545 p_sys->p_pgrm->clock.b_master = false;
547 p_pgrm->clock.b_master = true;
548 p_sys->p_pgrm = p_pgrm;
550 /* Update "program" */
551 val.i_int = p_pgrm->i_id;
552 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
555 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
556 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
557 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
558 var_SetInteger( p_input, "teletext-es", -1 );
559 for( i = 0; i < p_sys->i_es; i++ )
561 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
562 EsOutESVarUpdate( out, p_sys->es[i], false );
563 EsOutSelect( out, p_sys->es[i], false );
566 /* Update now playing */
567 input_item_SetNowPlaying( p_input->p->input.p_item,
568 p_pgrm->psz_now_playing );
569 input_item_SetPublisher( p_input->p->input.p_item,
570 p_pgrm->psz_publisher );
572 var_SetBool( p_sys->p_input, "intf-change", true );
578 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
580 es_out_sys_t *p_sys = out->p_sys;
581 input_thread_t *p_input = p_sys->p_input;
584 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
585 if( !p_pgrm ) return NULL;
588 p_pgrm->i_id = i_group;
590 p_pgrm->b_selected = false;
591 p_pgrm->psz_name = NULL;
592 p_pgrm->psz_now_playing = NULL;
593 p_pgrm->psz_publisher = NULL;
594 p_pgrm->p_epg = NULL;
595 input_ClockInit( &p_pgrm->clock, false, p_input->p->input.i_cr_average, p_sys->i_rate );
598 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
600 /* Update "program" variable */
602 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
604 if( i_group == var_GetInteger( p_input, "program" ) )
606 EsOutProgramSelect( out, p_pgrm );
610 var_SetBool( p_sys->p_input, "intf-change", true );
618 static int EsOutProgramDel( es_out_t *out, int i_group )
620 es_out_sys_t *p_sys = out->p_sys;
621 input_thread_t *p_input = p_sys->p_input;
622 es_out_pgrm_t *p_pgrm = NULL;
626 for( i = 0; i < p_sys->i_pgrm; i++ )
628 if( p_sys->pgrm[i]->i_id == i_group )
630 p_pgrm = p_sys->pgrm[i];
640 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
641 i_group, p_pgrm->i_es );
645 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
647 /* If program is selected we need to unselect it */
648 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
650 free( p_pgrm->psz_name );
651 free( p_pgrm->psz_now_playing );
652 free( p_pgrm->psz_publisher );
654 vlc_epg_Delete( p_pgrm->p_epg );
657 /* Update "program" variable */
659 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
661 var_SetBool( p_sys->p_input, "intf-change", true );
668 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
671 if( p_pgrm->psz_name )
672 asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
674 asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
678 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
680 es_out_sys_t *p_sys = out->p_sys;
681 es_out_pgrm_t *p_pgrm = NULL;
682 input_thread_t *p_input = p_sys->p_input;
684 const char *psz_title = NULL;
685 const char *psz_provider = NULL;
688 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
690 /* Check against empty meta data (empty for what we handle) */
691 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
692 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
693 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
694 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
699 for( i = 0; i < p_sys->i_pgrm; i++ )
701 if( p_sys->pgrm[i]->i_id == i_group )
703 p_pgrm = p_sys->pgrm[i];
708 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
711 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
712 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
714 /* Update the description text of the program */
715 if( psz_title && *psz_title )
720 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
722 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
724 /* Remove old entries */
725 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
726 /* TODO update epg name */
729 free( p_pgrm->psz_name );
730 p_pgrm->psz_name = strdup( psz_title );
732 /* ugly but it works */
734 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
736 if( psz_provider && *psz_provider )
738 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
739 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
740 free( text.psz_string );
744 text.psz_string = (char *)psz_title;
745 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
749 psz_cat = EsOutProgramGetMetaName( p_pgrm );
752 if( p_sys->p_pgrm == p_pgrm )
753 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
754 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
756 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
757 for( i = 0; ppsz_all_keys[i]; i++ )
759 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
760 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
761 free( ppsz_all_keys[i] );
763 free( ppsz_all_keys );
768 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
773 for( i = 0; i < p_src->i_event; i++ )
775 vlc_epg_event_t *p_evt = p_src->pp_event[i];
779 for( j = 0; j < p_dst->i_event; j++ )
781 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
786 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
791 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
794 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
795 p_copy->i_start = p_evt->i_start;
796 p_copy->i_duration = p_evt->i_duration;
797 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
798 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
799 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
800 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
804 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
806 /* Keep only 1 old event */
807 if( p_dst->p_current )
809 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
810 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
814 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
816 es_out_sys_t *p_sys = out->p_sys;
817 input_thread_t *p_input = p_sys->p_input;
818 es_out_pgrm_t *p_pgrm = NULL;
823 for( i = 0; i < p_sys->i_pgrm; i++ )
825 if( p_sys->pgrm[i]->i_id == i_group )
827 p_pgrm = p_sys->pgrm[i];
832 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
836 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
837 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
840 psz_cat = EsOutProgramGetMetaName( p_pgrm );
841 #ifdef HAVE_LOCALTIME_R
843 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
845 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
846 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
847 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
849 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
850 time_t t_start = (time_t)p_evt->i_start;
854 localtime_r( &t_start, &tm_start );
856 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
857 if( p_evt->psz_short_description || p_evt->psz_description )
858 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
860 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
861 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
863 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
865 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
869 /* Update now playing */
870 free( p_pgrm->psz_now_playing );
871 p_pgrm->psz_now_playing = NULL;
872 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
873 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
875 if( p_pgrm == p_sys->p_pgrm )
876 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
878 if( p_pgrm->psz_now_playing )
880 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
881 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
882 p_pgrm->psz_now_playing );
886 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
887 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
896 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
898 es_out_sys_t *p_sys = out->p_sys;
899 input_thread_t *p_input = p_sys->p_input;
901 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
902 es_out_pgrm_t *p_pgrm = NULL;
905 if( !es ) return NULL;
907 if( fmt->i_group < 0 )
909 msg_Err( p_input, "invalid group number" );
914 /* Search the program */
915 for( i = 0; i < p_sys->i_pgrm; i++ )
917 if( fmt->i_group == p_sys->pgrm[i]->i_id )
919 p_pgrm = p_sys->pgrm[i];
925 /* Create a new one */
926 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
929 /* Increase ref count for program */
934 fmt->i_id = out->p_sys->i_id;
935 es->i_id = fmt->i_id;
937 es_format_Copy( &es->fmt, fmt );
938 es->i_preroll_end = -1;
944 audio_replay_gain_t rg;
946 es->i_channel = p_sys->i_audio;
948 vlc_mutex_lock( &p_input->p->input.p_item->lock );
949 memset( &rg, 0, sizeof(rg) );
950 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
951 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
953 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
955 if( !es->fmt.audio_replay_gain.pb_peak[i] )
957 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
958 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
960 if( !es->fmt.audio_replay_gain.pb_gain[i] )
962 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
963 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
970 es->i_channel = p_sys->i_video;
971 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
972 vlc_ureduce( &es->fmt.video.i_frame_rate,
973 &es->fmt.video.i_frame_rate_base,
974 fmt->video.i_frame_rate,
975 fmt->video.i_frame_rate_base, 0 );
979 es->i_channel = p_sys->i_sub;
986 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
987 es->psz_language_code = LanguageGetCode( fmt->psz_language );
989 for( i = 0; i < 4; i++ )
990 es->pb_cc_present[i] = false;
991 es->p_master = false;
993 if( es->p_pgrm == p_sys->p_pgrm )
994 EsOutESVarUpdate( out, es, false );
996 /* Select it if needed */
997 EsOutSelect( out, es, false );
1000 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1001 p_sys->i_id++; /* always incremented */
1002 switch( fmt->i_cat )
1015 EsOutAddInfo( out, es );
1020 static bool EsIsSelected( es_out_id_t *es )
1024 bool b_decode = false;
1025 if( es->p_master->p_dec )
1027 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1028 if( i_channel != -1 )
1029 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1035 return es->p_dec != NULL;
1038 static void EsSelect( es_out_t *out, es_out_id_t *es )
1040 es_out_sys_t *p_sys = out->p_sys;
1041 input_thread_t *p_input = p_sys->p_input;
1043 const char *psz_var;
1045 if( EsIsSelected( es ) )
1047 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1054 if( !es->p_master->p_dec )
1057 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1058 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1063 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1065 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1067 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1072 else if( es->fmt.i_cat == AUDIO_ES )
1074 var_Get( p_input, "audio", &val );
1075 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1077 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1082 if( es->fmt.i_cat == SPU_ES )
1084 var_Get( p_input, "spu", &val );
1085 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1087 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1093 es->i_preroll_end = -1;
1094 es->p_dec = input_DecoderNew( p_input, &es->fmt, false );
1095 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1099 if( es->fmt.i_cat == VIDEO_ES )
1100 psz_var = "video-es";
1101 else if( es->fmt.i_cat == AUDIO_ES )
1102 psz_var = "audio-es";
1103 else if( es->fmt.i_cat == SPU_ES )
1108 /* Mark it as selected */
1109 val.i_int = es->i_id;
1110 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1112 var_SetBool( p_sys->p_input, "intf-change", true );
1115 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1117 es_out_sys_t *p_sys = out->p_sys;
1118 input_thread_t *p_input = p_sys->p_input;
1120 const char *psz_var;
1122 if( !EsIsSelected( es ) )
1124 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1130 if( es->p_master->p_dec )
1132 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1133 if( i_channel != -1 )
1134 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1139 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1141 for( i = 0; i < 4; i++ )
1143 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1146 if( i_spu_id == es->pp_cc_es[i]->i_id )
1148 /* Force unselection of the CC */
1150 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1152 var_SetBool( p_sys->p_input, "intf-change", true );
1154 EsOutDel( out, es->pp_cc_es[i] );
1156 es->pb_cc_present[i] = false;
1158 input_DecoderDelete( es->p_dec );
1166 if( es->p_dec == NULL )
1168 if( es->fmt.i_cat == VIDEO_ES )
1169 psz_var = "video-es";
1170 else if( es->fmt.i_cat == AUDIO_ES )
1171 psz_var = "audio-es";
1172 else if( es->fmt.i_cat == SPU_ES )
1177 /* Mark it as unselected */
1179 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1181 var_SetBool( p_sys->p_input, "intf-change", true );
1185 * Select an ES given the current mode
1186 * XXX: you need to take a the lock before (stream.stream_lock)
1188 * \param out The es_out structure
1189 * \param es es_out_id structure
1190 * \param b_force ...
1193 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1195 es_out_sys_t *p_sys = out->p_sys;
1197 int i_cat = es->fmt.i_cat;
1199 if( !p_sys->b_active ||
1200 ( !b_force && es->fmt.i_priority < 0 ) )
1205 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1207 if( !EsIsSelected( es ) )
1208 EsSelect( out, es );
1210 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1214 var_Get( p_sys->p_input, "programs", &val );
1215 for ( i = 0; i < val.p_list->i_count; i++ )
1217 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1219 if( !EsIsSelected( es ) )
1220 EsSelect( out, es );
1224 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1226 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1230 if( es->p_pgrm != p_sys->p_pgrm )
1233 if( i_cat == AUDIO_ES )
1235 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1236 es->psz_language_code );
1238 if( p_sys->p_es_audio &&
1239 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1241 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1242 p_sys->p_es_audio->psz_language_code );
1244 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1246 i_wanted = es->i_channel;
1250 /* Select audio if (no audio selected yet)
1251 * - no audio-language
1252 * - no audio code for the ES
1253 * - audio code in the requested list */
1255 !strcmp( es->psz_language_code, "??" ) ||
1256 !p_sys->ppsz_audio_language )
1257 i_wanted = es->i_channel;
1260 if( p_sys->i_audio_last >= 0 )
1261 i_wanted = p_sys->i_audio_last;
1263 if( p_sys->i_audio_id >= 0 )
1265 if( es->i_id == p_sys->i_audio_id )
1266 i_wanted = es->i_channel;
1271 else if( i_cat == SPU_ES )
1273 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1274 es->psz_language_code );
1276 if( p_sys->p_es_sub &&
1277 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1279 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1280 p_sys->p_es_sub->psz_language_code );
1282 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1283 idx1, es->psz_language_code, idx2,
1284 p_sys->p_es_sub->psz_language_code );
1286 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1288 /* We found a SPU that matches our language request */
1289 i_wanted = es->i_channel;
1291 else if( idx1 >= 0 )
1293 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1294 idx1, es->psz_language_code );
1296 i_wanted = es->i_channel;
1298 else if( p_sys->i_default_sub_id >= 0 )
1300 if( es->i_id == p_sys->i_default_sub_id )
1301 i_wanted = es->i_channel;
1304 if( p_sys->i_sub_last >= 0 )
1305 i_wanted = p_sys->i_sub_last;
1307 if( p_sys->i_sub_id >= 0 )
1309 if( es->i_id == p_sys->i_sub_id )
1310 i_wanted = es->i_channel;
1315 else if( i_cat == VIDEO_ES )
1317 i_wanted = es->i_channel;
1320 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1321 EsSelect( out, es );
1324 /* FIXME TODO handle priority here */
1325 if( EsIsSelected( es ) )
1327 if( i_cat == AUDIO_ES )
1329 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1330 p_sys->p_es_audio &&
1331 p_sys->p_es_audio != es &&
1332 EsIsSelected( p_sys->p_es_audio ) )
1334 EsUnselect( out, p_sys->p_es_audio, false );
1336 p_sys->p_es_audio = es;
1338 else if( i_cat == SPU_ES )
1340 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1342 p_sys->p_es_sub != es &&
1343 EsIsSelected( p_sys->p_es_sub ) )
1345 EsUnselect( out, p_sys->p_es_sub, false );
1347 p_sys->p_es_sub = es;
1349 else if( i_cat == VIDEO_ES )
1351 p_sys->p_es_video = es;
1357 * Send a block for the given es_out
1359 * \param out the es_out to send from
1360 * \param es the es_out_id
1361 * \param p_block the data block to send
1363 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1365 es_out_sys_t *p_sys = out->p_sys;
1366 input_thread_t *p_input = p_sys->p_input;
1367 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1371 if( es->fmt.i_cat == AUDIO_ES )
1372 i_delay = p_sys->i_audio_delay;
1373 else if( es->fmt.i_cat == SPU_ES )
1374 i_delay = p_sys->i_spu_delay;
1378 if( libvlc_stats (p_input) )
1380 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1381 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1382 p_block->i_buffer, &i_total );
1383 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1384 (float)i_total, NULL );
1385 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1388 /* Mark preroll blocks */
1389 if( es->i_preroll_end >= 0 )
1391 int64_t i_date = p_block->i_pts;
1393 i_date = p_block->i_dts;
1395 if( i_date < es->i_preroll_end )
1396 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1398 es->i_preroll_end = -1;
1401 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1403 p_block->i_dts += i_delay;
1405 else if( p_block->i_dts > 0 )
1408 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1410 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1412 p_block->i_pts += i_delay;
1414 else if( p_block->i_pts > 0 )
1417 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1419 if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
1420 es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1422 mtime_t current_date = mdate();
1424 || p_block->i_pts > current_date + 10000000
1425 || current_date > p_block->i_pts )
1427 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1428 * for teletext streams. */
1429 p_block->i_pts = current_date + 400000
1430 + p_input->i_pts_delay + i_delay;
1434 p_block->i_rate = p_sys->i_rate;
1436 /* TODO handle mute */
1438 ( es->fmt.i_cat != AUDIO_ES ||
1439 ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1440 p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1443 bool b_cc_new = false;
1445 input_DecoderDecode( es->p_dec, p_block );
1447 /* Check CC status */
1448 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1449 for( i = 0; i < 4; i++ )
1451 static const vlc_fourcc_t fcc[4] = {
1452 VLC_FOURCC('c', 'c', '1', ' '),
1453 VLC_FOURCC('c', 'c', '2', ' '),
1454 VLC_FOURCC('c', 'c', '3', ' '),
1455 VLC_FOURCC('c', 'c', '4', ' '),
1457 static const char ppsz_description[4][18] = {
1458 N_("Closed captions 1"),
1459 N_("Closed captions 2"),
1460 N_("Closed captions 3"),
1461 N_("Closed captions 4"),
1465 if( es->pb_cc_present[i] || !pb_cc[i] )
1467 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1469 es_format_Init( &fmt, SPU_ES, fcc[i] );
1470 fmt.i_group = es->fmt.i_group;
1471 fmt.psz_description = strdup( _(ppsz_description[i] ) );
1472 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1473 es->pp_cc_es[i]->p_master = es;
1474 es_format_Clean( &fmt );
1477 es->pb_cc_present[i] = true;
1481 var_SetBool( p_sys->p_input, "intf-change", true );
1485 block_Release( p_block );
1491 /*****************************************************************************
1493 *****************************************************************************/
1494 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1496 es_out_sys_t *p_sys = out->p_sys;
1497 bool b_reselect = false;
1500 /* We don't try to reselect */
1503 while( !out->p_sys->p_input->b_die && es->p_dec )
1505 if( input_DecoderEmpty( es->p_dec ) )
1509 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1512 if( es->p_pgrm == p_sys->p_pgrm )
1513 EsOutESVarUpdate( out, es, true );
1515 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1518 if( es->p_pgrm->i_es == 0 )
1520 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1523 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1524 p_sys->p_es_sub == es ) b_reselect = true;
1526 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1527 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1528 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1530 switch( es->fmt.i_cat )
1543 /* Re-select another track when needed */
1545 for( i = 0; i < p_sys->i_es; i++ )
1547 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1548 EsOutSelect( out, p_sys->es[i], false );
1551 free( es->psz_language );
1552 free( es->psz_language_code );
1554 es_format_Clean( &es->fmt );
1560 * Control query handler
1562 * \param out the es_out to control
1563 * \param i_query A es_out query as defined in include/ninput.h
1564 * \param args a variable list of arguments for the query
1565 * \return VLC_SUCCESS or an error code
1567 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1569 es_out_sys_t *p_sys = out->p_sys;
1577 case ES_OUT_SET_ES_STATE:
1578 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1579 b = (bool) va_arg( args, int );
1580 if( b && !EsIsSelected( es ) )
1582 EsSelect( out, es );
1583 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1585 else if( !b && EsIsSelected( es ) )
1587 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1592 case ES_OUT_GET_ES_STATE:
1593 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1594 pb = (bool*) va_arg( args, bool * );
1596 *pb = EsIsSelected( es );
1599 case ES_OUT_SET_ACTIVE:
1601 b = (bool) va_arg( args, int );
1602 p_sys->b_active = b;
1605 var_SetBool( p_sys->p_input, "intf-change", true );
1609 case ES_OUT_GET_ACTIVE:
1610 pb = (bool*) va_arg( args, bool * );
1611 *pb = p_sys->b_active;
1614 case ES_OUT_SET_MODE:
1615 i = (int) va_arg( args, int );
1616 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1617 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1621 /* Reapply policy mode */
1622 for( i = 0; i < p_sys->i_es; i++ )
1624 if( EsIsSelected( p_sys->es[i] ) )
1626 EsUnselect( out, p_sys->es[i],
1627 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1630 for( i = 0; i < p_sys->i_es; i++ )
1632 EsOutSelect( out, p_sys->es[i], false );
1636 return VLC_EGENERIC;
1638 case ES_OUT_GET_MODE:
1639 pi = (int*) va_arg( args, int* );
1640 *pi = p_sys->i_mode;
1644 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1645 /* Special case NULL, NULL+i_cat */
1648 for( i = 0; i < p_sys->i_es; i++ )
1650 if( EsIsSelected( p_sys->es[i] ) )
1651 EsUnselect( out, p_sys->es[i],
1652 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1655 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1657 for( i = 0; i < p_sys->i_es; i++ )
1659 if( p_sys->es[i]->fmt.i_cat == AUDIO_ES &&
1660 EsIsSelected( p_sys->es[i] ) )
1661 EsUnselect( out, p_sys->es[i],
1662 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1665 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1667 for( i = 0; i < p_sys->i_es; i++ )
1669 if( p_sys->es[i]->fmt.i_cat == VIDEO_ES &&
1670 EsIsSelected( p_sys->es[i] ) )
1671 EsUnselect( out, p_sys->es[i],
1672 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1675 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1677 for( i = 0; i < p_sys->i_es; i++ )
1679 if( p_sys->es[i]->fmt.i_cat == SPU_ES &&
1680 EsIsSelected( p_sys->es[i] ) )
1681 EsUnselect( out, p_sys->es[i],
1682 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1687 for( i = 0; i < p_sys->i_es; i++ )
1689 if( es == p_sys->es[i] )
1691 EsOutSelect( out, es, true );
1698 event.type = vlc_InputSelectedStreamChanged;
1699 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1703 case ES_OUT_SET_DEFAULT:
1705 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1709 /*p_sys->i_default_video_id = -1;*/
1710 /*p_sys->i_default_audio_id = -1;*/
1711 p_sys->i_default_sub_id = -1;
1713 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1715 /*p_sys->i_default_video_id = -1;*/
1717 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1719 /*p_sys->i_default_audio_id = -1;*/
1721 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1723 p_sys->i_default_sub_id = -1;
1727 /*if( es->fmt.i_cat == VIDEO_ES )
1728 p_sys->i_default_video_id = es->i_id;
1730 if( es->fmt.i_cat == AUDIO_ES )
1731 p_sys->i_default_audio_id = es->i_id;
1733 if( es->fmt.i_cat == SPU_ES )
1734 p_sys->i_default_sub_id = es->i_id;
1739 case ES_OUT_SET_PCR:
1740 case ES_OUT_SET_GROUP_PCR:
1742 es_out_pgrm_t *p_pgrm = NULL;
1746 if( i_query == ES_OUT_SET_PCR )
1748 p_pgrm = p_sys->p_pgrm;
1753 i_group = (int)va_arg( args, int );
1754 for( i = 0; i < p_sys->i_pgrm; i++ )
1756 if( p_sys->pgrm[i]->i_id == i_group )
1758 p_pgrm = p_sys->pgrm[i];
1763 if( p_pgrm == NULL )
1764 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1766 i_pcr = (int64_t)va_arg( args, int64_t );
1767 /* search program */
1768 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1772 case ES_OUT_RESET_PCR:
1773 for( i = 0; i < p_sys->i_pgrm; i++ )
1774 input_ClockResetPCR( &p_sys->pgrm[i]->clock );
1780 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1781 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1782 *pi_ts = input_ClockGetTS( p_sys->p_input,
1783 &p_sys->p_pgrm->clock, i_ts );
1786 return VLC_EGENERIC;
1788 case ES_OUT_GET_GROUP:
1789 pi = (int*) va_arg( args, int* );
1791 *pi = p_sys->p_pgrm->i_id;
1793 *pi = -1; /* FIXME */
1796 case ES_OUT_SET_GROUP:
1799 i = (int) va_arg( args, int );
1800 for( j = 0; j < p_sys->i_pgrm; j++ )
1802 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1803 if( p_pgrm->i_id == i )
1805 EsOutProgramSelect( out, p_pgrm );
1809 return VLC_EGENERIC;
1812 case ES_OUT_SET_FMT:
1814 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1815 * to update the p_extra data */
1817 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1818 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1819 if( es == NULL ) return VLC_EGENERIC;
1821 if( p_fmt->i_extra )
1823 es->fmt.i_extra = p_fmt->i_extra;
1824 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1825 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1827 if( !es->p_dec ) return VLC_SUCCESS;
1830 input_DecoderDelete( es->p_dec );
1831 es->p_dec = input_DecoderNew( p_sys->p_input,
1835 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1836 es->p_dec->fmt_in.p_extra =
1837 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1838 memcpy( es->p_dec->fmt_in.p_extra,
1839 p_fmt->p_extra, p_fmt->i_extra );
1846 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1850 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1851 i_date = (int64_t)va_arg( args, int64_t );
1853 if( !es || !es->p_dec )
1854 return VLC_EGENERIC;
1856 /* XXX We should call input_ClockGetTS but PCR has been reseted
1857 * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1858 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1859 es->i_preroll_end = i_date;
1863 case ES_OUT_SET_GROUP_META:
1865 int i_group = (int)va_arg( args, int );
1866 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1868 EsOutProgramMeta( out, i_group, p_meta );
1871 case ES_OUT_SET_GROUP_EPG:
1873 int i_group = (int)va_arg( args, int );
1874 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1876 EsOutProgramEpg( out, i_group, p_epg );
1879 case ES_OUT_DEL_GROUP:
1881 int i_group = (int)va_arg( args, int );
1883 return EsOutProgramDel( out, i_group );
1887 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1888 return VLC_EGENERIC;
1892 /****************************************************************************
1893 * LanguageGetName: try to expend iso639 into plain name
1894 ****************************************************************************/
1895 static char *LanguageGetName( const char *psz_code )
1897 const iso639_lang_t *pl;
1899 if( psz_code == NULL )
1901 return strdup( "" );
1904 if( strlen( psz_code ) == 2 )
1906 pl = GetLang_1( psz_code );
1908 else if( strlen( psz_code ) == 3 )
1910 pl = GetLang_2B( psz_code );
1911 if( !strcmp( pl->psz_iso639_1, "??" ) )
1913 pl = GetLang_2T( psz_code );
1918 return strdup( psz_code );
1921 if( !strcmp( pl->psz_iso639_1, "??" ) )
1923 return strdup( psz_code );
1927 if( *pl->psz_native_name )
1929 return strdup( pl->psz_native_name );
1931 return strdup( pl->psz_eng_name );
1935 /* Get a 2 char code */
1936 static char *LanguageGetCode( const char *psz_lang )
1938 const iso639_lang_t *pl;
1940 if( psz_lang == NULL || *psz_lang == '\0' )
1941 return strdup("??");
1943 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1945 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1946 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1947 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1948 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1949 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1953 if( pl->psz_iso639_1 != NULL )
1954 return strdup( pl->psz_iso639_1 );
1956 return strdup("??");
1959 static char **LanguageSplit( const char *psz_langs )
1966 if( psz_langs == NULL ) return NULL;
1968 psz_parser = psz_dup = strdup(psz_langs);
1970 while( psz_parser && *psz_parser )
1975 psz = strchr(psz_parser, ',' );
1976 if( psz ) *psz++ = '\0';
1978 if( !strcmp( psz_parser, "any" ) )
1980 TAB_APPEND( i_psz, ppsz, strdup("any") );
1984 psz_code = LanguageGetCode( psz_parser );
1985 if( strcmp( psz_code, "??" ) )
1987 TAB_APPEND( i_psz, ppsz, psz_code );
2000 TAB_APPEND( i_psz, ppsz, NULL );
2007 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2011 if( !ppsz_langs || !psz_lang ) return -1;
2013 for( i = 0; ppsz_langs[i]; i++ )
2015 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2016 !strcasecmp( ppsz_langs[i], "any" ) )
2025 /****************************************************************************
2027 * - add meta info to the playlist item
2028 ****************************************************************************/
2029 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2031 es_out_sys_t *p_sys = out->p_sys;
2032 input_thread_t *p_input = p_sys->p_input;
2033 es_format_t *fmt = &es->fmt;
2037 /* Add stream info */
2038 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
2040 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2041 "%.4s", (char*)&fmt->i_codec );
2043 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2044 "%s", es->psz_language );
2046 /* Add information */
2047 switch( fmt->i_cat )
2050 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2051 _("Type"), _("Audio") );
2053 if( fmt->audio.i_channels > 0 )
2054 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2055 "%u", fmt->audio.i_channels );
2057 if( fmt->audio.i_rate > 0 )
2059 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2060 _("%u Hz"), fmt->audio.i_rate );
2061 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2064 if( fmt->audio.i_bitspersample > 0 )
2065 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2066 _("Bits per sample"), "%u",
2067 fmt->audio.i_bitspersample );
2069 if( fmt->i_bitrate > 0 )
2071 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2072 _("%u kb/s"), fmt->i_bitrate / 1000 );
2073 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2078 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2079 _("Type"), _("Video") );
2081 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2082 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2083 _("Resolution"), "%ux%u",
2084 fmt->video.i_width, fmt->video.i_height );
2086 if( fmt->video.i_visible_width > 0 &&
2087 fmt->video.i_visible_height > 0 )
2088 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2089 _("Display resolution"), "%ux%u",
2090 fmt->video.i_visible_width,
2091 fmt->video.i_visible_height);
2092 if( fmt->video.i_frame_rate > 0 &&
2093 fmt->video.i_frame_rate_base > 0 )
2095 div = lldiv( (float)fmt->video.i_frame_rate /
2096 fmt->video.i_frame_rate_base * 1000000,
2098 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2099 _("Frame rate"), "%"PRId64".%06u",
2100 div.quot, (unsigned int )div.rem );
2105 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2106 _("Type"), _("Subtitle") );