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 *****************************************************************************/
34 #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 "../stream_output/stream_output.h"
45 #include <vlc_iso_lang.h>
46 /* FIXME we should find a better way than including that */
47 #include "../text/iso-639_def.h"
49 /*****************************************************************************
51 *****************************************************************************/
57 /* Number of es for this pgrm */
62 /* Clock for this program */
63 input_clock_t *p_clock;
66 char *psz_now_playing;
76 es_out_pgrm_t *p_pgrm;
79 int64_t i_preroll_end;
81 /* Channel in the track type */
85 char *psz_language_code;
88 decoder_t *p_dec_record;
90 /* Fields for Video with CC */
91 bool pb_cc_present[4];
92 es_out_id_t *pp_cc_es[4];
94 /* Field for CC track from a master video */
95 es_out_id_t *p_master;
100 input_thread_t *p_input;
104 es_out_pgrm_t **pgrm;
105 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
106 es_out_pgrm_t *p_pgrm; /* Master program */
123 int i_audio_last, i_audio_id;
124 int i_sub_last, i_sub_id;
125 int i_default_sub_id; /* As specified in container; if applicable */
126 char **ppsz_audio_language;
127 char **ppsz_sub_language;
129 /* current main es */
130 es_out_id_t *p_es_audio;
131 es_out_id_t *p_es_video;
132 es_out_id_t *p_es_sub;
135 int64_t i_audio_delay;
138 /* Rate used to rescale ES ts */
142 sout_instance_t *p_sout_record;
145 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
146 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
147 static void EsOutDel ( es_out_t *, es_out_id_t * );
148 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
149 static int EsOutControl( es_out_t *, int i_query, va_list );
151 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
153 static bool EsIsSelected( es_out_id_t *es );
154 static void EsSelect( es_out_t *out, es_out_id_t *es );
155 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
156 static char *LanguageGetName( const char *psz_code );
157 static char *LanguageGetCode( const char *psz_lang );
158 static char **LanguageSplit( const char *psz_langs );
159 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
161 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
163 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
164 VLC_FOURCC('c', 'c', '1', ' '),
165 VLC_FOURCC('c', 'c', '2', ' '),
166 VLC_FOURCC('c', 'c', '3', ' '),
167 VLC_FOURCC('c', 'c', '4', ' '),
169 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
172 for( i = 0; i < 4; i++ )
174 if( fcc == EsOutFourccClosedCaptions[i] )
181 /*****************************************************************************
183 *****************************************************************************/
184 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
189 es_out_t *out = malloc( sizeof( es_out_t ) );
190 if( !out ) return NULL;
192 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
199 out->pf_add = EsOutAdd;
200 out->pf_send = EsOutSend;
201 out->pf_del = EsOutDel;
202 out->pf_control = EsOutControl;
204 out->b_sout = p_input->p->p_sout != NULL;
206 p_sys->p_input = p_input;
208 p_sys->b_active = false;
209 p_sys->i_mode = ES_OUT_MODE_AUTO;
212 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
213 p_sys->p_pgrm = NULL;
217 TAB_INIT( p_sys->i_es, p_sys->es );
224 var_Get( p_input, "audio-track", &val );
225 p_sys->i_audio_last = val.i_int;
227 var_Get( p_input, "sub-track", &val );
228 p_sys->i_sub_last = val.i_int;
230 p_sys->i_default_sub_id = -1;
232 if( !p_input->b_preparsing )
234 var_Get( p_input, "audio-language", &val );
235 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
236 if( p_sys->ppsz_audio_language )
238 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
239 msg_Dbg( p_input, "selected audio language[%d] %s",
240 i, p_sys->ppsz_audio_language[i] );
242 free( val.psz_string );
244 var_Get( p_input, "sub-language", &val );
245 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
246 if( p_sys->ppsz_sub_language )
248 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
249 msg_Dbg( p_input, "selected subtitle language[%d] %s",
250 i, p_sys->ppsz_sub_language[i] );
252 free( val.psz_string );
256 p_sys->ppsz_sub_language = NULL;
257 p_sys->ppsz_audio_language = NULL;
260 var_Get( p_input, "audio-track-id", &val );
261 p_sys->i_audio_id = val.i_int;
263 var_Get( p_input, "sub-track-id", &val );
264 p_sys->i_sub_id = val.i_int;
266 p_sys->p_es_audio = NULL;
267 p_sys->p_es_video = NULL;
268 p_sys->p_es_sub = NULL;
270 p_sys->i_audio_delay= 0;
271 p_sys->i_spu_delay = 0;
273 p_sys->i_rate = i_rate;
275 p_sys->p_sout_record = NULL;
280 /*****************************************************************************
282 *****************************************************************************/
283 void input_EsOutDelete( es_out_t *out )
285 es_out_sys_t *p_sys = out->p_sys;
288 if( p_sys->p_sout_record )
289 input_EsOutSetRecord( out, false );
291 for( i = 0; i < p_sys->i_es; i++ )
293 if( p_sys->es[i]->p_dec )
294 input_DecoderDelete( p_sys->es[i]->p_dec );
296 free( p_sys->es[i]->psz_language );
297 free( p_sys->es[i]->psz_language_code );
298 es_format_Clean( &p_sys->es[i]->fmt );
300 free( p_sys->es[i] );
302 if( p_sys->ppsz_audio_language )
304 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
305 free( p_sys->ppsz_audio_language[i] );
306 free( p_sys->ppsz_audio_language );
308 if( p_sys->ppsz_sub_language )
310 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
311 free( p_sys->ppsz_sub_language[i] );
312 free( p_sys->ppsz_sub_language );
316 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
317 for( i = 0; i < p_sys->i_pgrm; i++ )
319 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
320 input_clock_Delete( p_pgrm->p_clock );
321 free( p_pgrm->psz_now_playing );
322 free( p_pgrm->psz_publisher );
323 free( p_pgrm->psz_name );
325 vlc_epg_Delete( p_pgrm->p_epg );
329 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
335 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
340 /* Special HACK, -i_id is tha cat of the stream */
341 return (es_out_id_t*)((uint8_t*)NULL-i_id);
344 for( i = 0; i < out->p_sys->i_es; i++ )
346 if( out->p_sys->es[i]->i_id == i_id )
347 return out->p_sys->es[i];
352 mtime_t input_EsOutGetWakeup( es_out_t *out )
354 es_out_sys_t *p_sys = out->p_sys;
355 input_thread_t *p_input = p_sys->p_input;
360 /* We do not have a wake up date if the input cannot have its speed
361 * controlled or sout is imposing its own */
362 if( !p_input->b_can_pace_control || p_input->p->b_out_pace_control )
365 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
368 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
370 es_out_sys_t *p_sys = out->p_sys;
373 for( i = 0; i < p_sys->i_es; i++ )
375 es_out_id_t *es = p_sys->es[i];
377 /* Send a dummy block to let decoder know that
378 * there is a discontinuity */
379 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
381 input_DecoderDiscontinuity( es->p_dec, b_flush );
382 if( es->p_dec_record )
383 input_DecoderDiscontinuity( es->p_dec_record, b_flush );
387 void input_EsOutChangeRate( es_out_t *out, int i_rate )
389 es_out_sys_t *p_sys = out->p_sys;
392 p_sys->i_rate = i_rate;
393 EsOutDiscontinuity( out, false, false );
395 for( i = 0; i < p_sys->i_pgrm; i++ )
396 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, i_rate );
399 int input_EsOutSetRecord( es_out_t *out, bool b_record )
401 es_out_sys_t *p_sys = out->p_sys;
402 input_thread_t *p_input = p_sys->p_input;
404 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
408 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
409 if( !psz_path || *psz_path == '\0' )
412 psz_path = strdup( config_GetHomeDir() );
415 char *psz_sout = NULL; // TODO conf
417 if( !psz_sout && psz_path )
419 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
422 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
433 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
437 if( !p_sys->p_sout_record )
440 for( int i = 0; i < p_sys->i_es; i++ )
442 es_out_id_t *p_es = p_sys->es[i];
444 if( !p_es->p_dec || p_es->p_master )
447 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
452 for( int i = 0; i < p_sys->i_es; i++ )
454 es_out_id_t *p_es = p_sys->es[i];
456 if( !p_es->p_dec_record )
459 input_DecoderDelete( p_es->p_dec_record );
460 p_es->p_dec_record = NULL;
463 sout_DeleteInstance( p_sys->p_sout_record );
465 p_sys->p_sout_record = NULL;
470 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
472 es_out_sys_t *p_sys = out->p_sys;
474 if( i_cat == AUDIO_ES )
475 p_sys->i_audio_delay = i_delay;
476 else if( i_cat == SPU_ES )
477 p_sys->i_spu_delay = i_delay;
479 void input_EsOutChangeState( es_out_t *out )
481 es_out_sys_t *p_sys = out->p_sys;
482 input_thread_t *p_input = p_sys->p_input;
484 if( p_input->i_state == PAUSE_S )
486 /* Send discontinuity to decoders (it will allow them to flush
487 * * if implemented */
488 EsOutDiscontinuity( out, false, false );
492 /* Out of pause, reset pcr */
493 es_out_Control( out, ES_OUT_RESET_PCR );
496 void input_EsOutChangePosition( es_out_t *out )
498 //es_out_sys_t *p_sys = out->p_sys;
500 es_out_Control( out, ES_OUT_RESET_PCR );
501 EsOutDiscontinuity( out, true, false );
504 bool input_EsOutDecodersEmpty( es_out_t *out )
506 es_out_sys_t *p_sys = out->p_sys;
509 for( i = 0; i < p_sys->i_es; i++ )
511 es_out_id_t *es = p_sys->es[i];
513 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
515 if( es->p_dec_record && !input_DecoderEmpty( es->p_dec_record ) )
521 /*****************************************************************************
523 *****************************************************************************/
524 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
527 es_out_sys_t *p_sys = out->p_sys;
528 input_thread_t *p_input = p_sys->p_input;
529 const bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
530 vlc_value_t val, text;
534 if( fmt->i_cat == AUDIO_ES )
535 psz_var = "audio-es";
536 else if( fmt->i_cat == VIDEO_ES )
537 psz_var = "video-es";
538 else if( fmt->i_cat == SPU_ES )
546 var_SetInteger( p_sys->p_input, "teletext-es", -1 );
549 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
551 var_SetBool( p_sys->p_input, "intf-change", true );
555 /* Get the number of ES already added */
556 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
561 /* First one, we need to add the "Disable" choice */
562 val2.i_int = -1; text.psz_string = _("Disable");
563 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
567 /* Take care of the ES description */
568 if( fmt->psz_description && *fmt->psz_description )
570 if( psz_language && *psz_language )
572 text.psz_string = malloc( strlen( fmt->psz_description) +
573 strlen( psz_language ) + 10 );
574 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
577 else text.psz_string = strdup( fmt->psz_description );
581 if( psz_language && *psz_language )
583 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
584 text.psz_string = NULL;
588 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
589 text.psz_string = NULL;
594 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
596 free( text.psz_string );
600 if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
601 var_SetInteger( p_sys->p_input, "teletext-es", i_id );
604 var_SetBool( p_sys->p_input, "intf-change", true );
607 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
610 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
613 /* EsOutProgramSelect:
614 * Select a program and update the object variable
616 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
618 es_out_sys_t *p_sys = out->p_sys;
619 input_thread_t *p_input = p_sys->p_input;
623 if( p_sys->p_pgrm == p_pgrm )
624 return; /* Nothing to do */
628 es_out_pgrm_t *old = p_sys->p_pgrm;
629 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
631 for( i = 0; i < p_sys->i_es; i++ )
633 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
634 p_sys->i_mode != ES_OUT_MODE_ALL )
635 EsUnselect( out, p_sys->es[i], true );
638 p_sys->p_es_audio = NULL;
639 p_sys->p_es_sub = NULL;
640 p_sys->p_es_video = NULL;
643 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
645 /* Mark it selected */
646 p_pgrm->b_selected = true;
648 /* Switch master stream */
650 input_clock_ChangeMaster( p_sys->p_pgrm->p_clock, false );
651 input_clock_ChangeMaster( p_pgrm->p_clock, true );
652 p_sys->p_pgrm = p_pgrm;
654 /* Update "program" */
655 val.i_int = p_pgrm->i_id;
656 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
659 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
660 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
661 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
662 var_SetInteger( p_input, "teletext-es", -1 );
663 for( i = 0; i < p_sys->i_es; i++ )
665 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
666 EsOutESVarUpdate( out, p_sys->es[i], false );
667 EsOutSelect( out, p_sys->es[i], false );
670 /* Update now playing */
671 input_item_SetNowPlaying( p_input->p->input.p_item,
672 p_pgrm->psz_now_playing );
673 input_item_SetPublisher( p_input->p->input.p_item,
674 p_pgrm->psz_publisher );
676 var_SetBool( p_sys->p_input, "intf-change", true );
682 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
684 es_out_sys_t *p_sys = out->p_sys;
685 input_thread_t *p_input = p_sys->p_input;
688 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
693 p_pgrm->i_id = i_group;
695 p_pgrm->b_selected = false;
696 p_pgrm->psz_name = NULL;
697 p_pgrm->psz_now_playing = NULL;
698 p_pgrm->psz_publisher = NULL;
699 p_pgrm->p_epg = NULL;
700 p_pgrm->p_clock = input_clock_New( false, p_input->p->input.i_cr_average, p_sys->i_rate );
701 if( !p_pgrm->p_clock )
708 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
710 /* Update "program" variable */
712 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
714 if( i_group == var_GetInteger( p_input, "program" ) )
716 EsOutProgramSelect( out, p_pgrm );
720 var_SetBool( p_sys->p_input, "intf-change", true );
728 static int EsOutProgramDel( es_out_t *out, int i_group )
730 es_out_sys_t *p_sys = out->p_sys;
731 input_thread_t *p_input = p_sys->p_input;
732 es_out_pgrm_t *p_pgrm = NULL;
736 for( i = 0; i < p_sys->i_pgrm; i++ )
738 if( p_sys->pgrm[i]->i_id == i_group )
740 p_pgrm = p_sys->pgrm[i];
750 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
751 i_group, p_pgrm->i_es );
755 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
757 /* If program is selected we need to unselect it */
758 if( p_sys->p_pgrm == p_pgrm )
759 p_sys->p_pgrm = NULL;
761 input_clock_Delete( p_pgrm->p_clock );
763 free( p_pgrm->psz_name );
764 free( p_pgrm->psz_now_playing );
765 free( p_pgrm->psz_publisher );
767 vlc_epg_Delete( p_pgrm->p_epg );
770 /* Update "program" variable */
772 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
774 var_SetBool( p_sys->p_input, "intf-change", true );
781 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
784 if( p_pgrm->psz_name )
786 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
791 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
797 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
799 es_out_sys_t *p_sys = out->p_sys;
800 es_out_pgrm_t *p_pgrm = NULL;
801 input_thread_t *p_input = p_sys->p_input;
803 const char *psz_title = NULL;
804 const char *psz_provider = NULL;
807 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
809 /* Check against empty meta data (empty for what we handle) */
810 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
811 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
812 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
813 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
818 for( i = 0; i < p_sys->i_pgrm; i++ )
820 if( p_sys->pgrm[i]->i_id == i_group )
822 p_pgrm = p_sys->pgrm[i];
827 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
830 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
831 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
833 /* Update the description text of the program */
834 if( psz_title && *psz_title )
839 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
841 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
843 /* Remove old entries */
844 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
845 /* TODO update epg name */
848 free( p_pgrm->psz_name );
849 p_pgrm->psz_name = strdup( psz_title );
851 /* ugly but it works */
853 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
855 if( psz_provider && *psz_provider )
857 if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
859 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
860 free( text.psz_string );
865 text.psz_string = (char *)psz_title;
866 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
870 psz_cat = EsOutProgramGetMetaName( p_pgrm );
873 if( p_sys->p_pgrm == p_pgrm )
874 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
875 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
877 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
878 for( i = 0; ppsz_all_keys[i]; i++ )
880 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
881 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
882 free( ppsz_all_keys[i] );
884 free( ppsz_all_keys );
889 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
894 for( i = 0; i < p_src->i_event; i++ )
896 vlc_epg_event_t *p_evt = p_src->pp_event[i];
900 for( j = 0; j < p_dst->i_event; j++ )
902 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
907 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
912 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
915 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
916 p_copy->i_start = p_evt->i_start;
917 p_copy->i_duration = p_evt->i_duration;
918 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
919 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
920 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
921 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
925 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
927 /* Keep only 1 old event */
928 if( p_dst->p_current )
930 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
931 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
935 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
937 es_out_sys_t *p_sys = out->p_sys;
938 input_thread_t *p_input = p_sys->p_input;
939 es_out_pgrm_t *p_pgrm = NULL;
944 for( i = 0; i < p_sys->i_pgrm; i++ )
946 if( p_sys->pgrm[i]->i_id == i_group )
948 p_pgrm = p_sys->pgrm[i];
953 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
957 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
958 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
961 psz_cat = EsOutProgramGetMetaName( p_pgrm );
962 #ifdef HAVE_LOCALTIME_R
964 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
966 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
967 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
968 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
970 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
971 time_t t_start = (time_t)p_evt->i_start;
975 localtime_r( &t_start, &tm_start );
977 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
978 if( p_evt->psz_short_description || p_evt->psz_description )
979 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
981 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
982 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
984 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
986 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
990 /* Update now playing */
991 free( p_pgrm->psz_now_playing );
992 p_pgrm->psz_now_playing = NULL;
993 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
994 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
996 if( p_pgrm == p_sys->p_pgrm )
997 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
999 if( p_pgrm->psz_now_playing )
1001 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1002 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1003 p_pgrm->psz_now_playing );
1007 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1008 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1017 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
1019 es_out_sys_t *p_sys = out->p_sys;
1020 input_thread_t *p_input = p_sys->p_input;
1022 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
1023 es_out_pgrm_t *p_pgrm = NULL;
1026 if( !es ) return NULL;
1028 if( fmt->i_group < 0 )
1030 msg_Err( p_input, "invalid group number" );
1035 /* Search the program */
1036 for( i = 0; i < p_sys->i_pgrm; i++ )
1038 if( fmt->i_group == p_sys->pgrm[i]->i_id )
1040 p_pgrm = p_sys->pgrm[i];
1044 if( p_pgrm == NULL )
1046 /* Create a new one */
1047 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1050 /* Increase ref count for program */
1055 fmt->i_id = out->p_sys->i_id;
1056 es->i_id = fmt->i_id;
1057 es->p_pgrm = p_pgrm;
1058 es_format_Copy( &es->fmt, fmt );
1059 es->i_preroll_end = -1;
1061 switch( fmt->i_cat )
1065 audio_replay_gain_t rg;
1067 es->i_channel = p_sys->i_audio;
1069 memset( &rg, 0, sizeof(rg) );
1070 vlc_mutex_lock( &p_input->p->input.p_item->lock );
1071 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
1072 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1074 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1076 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1078 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1079 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1081 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1083 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1084 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1091 es->i_channel = p_sys->i_video;
1092 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
1093 vlc_ureduce( &es->fmt.video.i_frame_rate,
1094 &es->fmt.video.i_frame_rate_base,
1095 fmt->video.i_frame_rate,
1096 fmt->video.i_frame_rate_base, 0 );
1100 es->i_channel = p_sys->i_sub;
1107 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
1108 es->psz_language_code = LanguageGetCode( fmt->psz_language );
1110 es->p_dec_record = NULL;
1111 for( i = 0; i < 4; i++ )
1112 es->pb_cc_present[i] = false;
1113 es->p_master = NULL;
1115 if( es->p_pgrm == p_sys->p_pgrm )
1116 EsOutESVarUpdate( out, es, false );
1118 /* Select it if needed */
1119 EsOutSelect( out, es, false );
1122 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1123 p_sys->i_id++; /* always incremented */
1124 switch( fmt->i_cat )
1137 EsOutAddInfo( out, es );
1142 static bool EsIsSelected( es_out_id_t *es )
1146 bool b_decode = false;
1147 if( es->p_master->p_dec )
1149 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1150 if( i_channel != -1 )
1151 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1157 return es->p_dec != NULL;
1160 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1162 es_out_sys_t *p_sys = out->p_sys;
1163 input_thread_t *p_input = p_sys->p_input;
1165 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_input->p->p_sout );
1166 if( p_es->p_dec && !p_es->p_master && p_sys->p_sout_record )
1167 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
1169 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1176 input_DecoderDelete( p_es->p_dec );
1179 if( p_es->p_dec_record )
1181 input_DecoderDelete( p_es->p_dec_record );
1182 p_es->p_dec_record = NULL;
1186 static void EsSelect( es_out_t *out, es_out_id_t *es )
1188 es_out_sys_t *p_sys = out->p_sys;
1189 input_thread_t *p_input = p_sys->p_input;
1191 const char *psz_var;
1193 if( EsIsSelected( es ) )
1195 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1202 if( !es->p_master->p_dec )
1205 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1206 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1211 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1213 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1215 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1220 else if( es->fmt.i_cat == AUDIO_ES )
1222 var_Get( p_input, "audio", &val );
1223 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1225 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1230 if( es->fmt.i_cat == SPU_ES )
1232 var_Get( p_input, "spu", &val );
1233 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1235 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1241 es->i_preroll_end = -1;
1242 EsCreateDecoder( out, es );
1244 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1248 if( es->fmt.i_cat == VIDEO_ES )
1249 psz_var = "video-es";
1250 else if( es->fmt.i_cat == AUDIO_ES )
1251 psz_var = "audio-es";
1252 else if( es->fmt.i_cat == SPU_ES )
1257 /* Mark it as selected */
1258 val.i_int = es->i_id;
1259 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1261 var_SetBool( p_sys->p_input, "intf-change", true );
1264 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1266 es_out_sys_t *p_sys = out->p_sys;
1267 input_thread_t *p_input = p_sys->p_input;
1269 const char *psz_var;
1271 if( !EsIsSelected( es ) )
1273 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1279 if( es->p_master->p_dec )
1281 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1282 if( i_channel != -1 )
1283 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1288 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1290 for( i = 0; i < 4; i++ )
1292 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1295 if( i_spu_id == es->pp_cc_es[i]->i_id )
1297 /* Force unselection of the CC */
1299 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1301 var_SetBool( p_sys->p_input, "intf-change", true );
1303 EsOutDel( out, es->pp_cc_es[i] );
1305 es->pb_cc_present[i] = false;
1307 EsDestroyDecoder( out, es );
1314 if( es->p_dec == NULL )
1316 if( es->fmt.i_cat == VIDEO_ES )
1317 psz_var = "video-es";
1318 else if( es->fmt.i_cat == AUDIO_ES )
1319 psz_var = "audio-es";
1320 else if( es->fmt.i_cat == SPU_ES )
1325 /* Mark it as unselected */
1327 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1329 var_SetBool( p_sys->p_input, "intf-change", true );
1333 * Select an ES given the current mode
1334 * XXX: you need to take a the lock before (stream.stream_lock)
1336 * \param out The es_out structure
1337 * \param es es_out_id structure
1338 * \param b_force ...
1341 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1343 es_out_sys_t *p_sys = out->p_sys;
1345 int i_cat = es->fmt.i_cat;
1347 if( !p_sys->b_active ||
1348 ( !b_force && es->fmt.i_priority < 0 ) )
1353 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1355 if( !EsIsSelected( es ) )
1356 EsSelect( out, es );
1358 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1362 var_Get( p_sys->p_input, "programs", &val );
1363 for ( i = 0; i < val.p_list->i_count; i++ )
1365 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1367 if( !EsIsSelected( es ) )
1368 EsSelect( out, es );
1372 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1374 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1378 if( es->p_pgrm != p_sys->p_pgrm )
1381 if( i_cat == AUDIO_ES )
1383 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1384 es->psz_language_code );
1386 if( p_sys->p_es_audio &&
1387 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1389 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1390 p_sys->p_es_audio->psz_language_code );
1392 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1394 i_wanted = es->i_channel;
1398 /* Select audio if (no audio selected yet)
1399 * - no audio-language
1400 * - no audio code for the ES
1401 * - audio code in the requested list */
1403 !strcmp( es->psz_language_code, "??" ) ||
1404 !p_sys->ppsz_audio_language )
1405 i_wanted = es->i_channel;
1408 if( p_sys->i_audio_last >= 0 )
1409 i_wanted = p_sys->i_audio_last;
1411 if( p_sys->i_audio_id >= 0 )
1413 if( es->i_id == p_sys->i_audio_id )
1414 i_wanted = es->i_channel;
1419 else if( i_cat == SPU_ES )
1421 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1422 es->psz_language_code );
1424 if( p_sys->p_es_sub &&
1425 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1427 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1428 p_sys->p_es_sub->psz_language_code );
1430 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1431 idx1, es->psz_language_code, idx2,
1432 p_sys->p_es_sub->psz_language_code );
1434 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1436 /* We found a SPU that matches our language request */
1437 i_wanted = es->i_channel;
1439 else if( idx1 >= 0 )
1441 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1442 idx1, es->psz_language_code );
1444 i_wanted = es->i_channel;
1446 else if( p_sys->i_default_sub_id >= 0 )
1448 if( es->i_id == p_sys->i_default_sub_id )
1449 i_wanted = es->i_channel;
1452 if( p_sys->i_sub_last >= 0 )
1453 i_wanted = p_sys->i_sub_last;
1455 if( p_sys->i_sub_id >= 0 )
1457 if( es->i_id == p_sys->i_sub_id )
1458 i_wanted = es->i_channel;
1463 else if( i_cat == VIDEO_ES )
1465 i_wanted = es->i_channel;
1468 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1469 EsSelect( out, es );
1472 /* FIXME TODO handle priority here */
1473 if( EsIsSelected( es ) )
1475 if( i_cat == AUDIO_ES )
1477 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1478 p_sys->p_es_audio &&
1479 p_sys->p_es_audio != es &&
1480 EsIsSelected( p_sys->p_es_audio ) )
1482 EsUnselect( out, p_sys->p_es_audio, false );
1484 p_sys->p_es_audio = es;
1486 else if( i_cat == SPU_ES )
1488 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1490 p_sys->p_es_sub != es &&
1491 EsIsSelected( p_sys->p_es_sub ) )
1493 EsUnselect( out, p_sys->p_es_sub, false );
1495 p_sys->p_es_sub = es;
1497 else if( i_cat == VIDEO_ES )
1499 p_sys->p_es_video = es;
1505 * Send a block for the given es_out
1507 * \param out the es_out to send from
1508 * \param es the es_out_id
1509 * \param p_block the data block to send
1511 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1513 es_out_sys_t *p_sys = out->p_sys;
1514 input_thread_t *p_input = p_sys->p_input;
1515 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1519 if( es->fmt.i_cat == AUDIO_ES )
1520 i_delay = p_sys->i_audio_delay;
1521 else if( es->fmt.i_cat == SPU_ES )
1522 i_delay = p_sys->i_spu_delay;
1526 if( libvlc_stats (p_input) )
1528 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1529 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1530 p_block->i_buffer, &i_total );
1531 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1532 (float)i_total, NULL );
1533 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1536 /* Mark preroll blocks */
1537 if( es->i_preroll_end >= 0 )
1539 int64_t i_date = p_block->i_pts;
1541 i_date = p_block->i_dts;
1543 if( i_date < es->i_preroll_end )
1544 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1546 es->i_preroll_end = -1;
1549 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1551 p_block->i_dts += i_delay;
1553 else if( p_block->i_dts > 0 )
1556 input_clock_GetTS( p_pgrm->p_clock, p_input->i_pts_delay, p_block->i_dts ) + i_delay;
1558 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1560 p_block->i_pts += i_delay;
1562 else if( p_block->i_pts > 0 )
1565 input_clock_GetTS( p_pgrm->p_clock, p_input->i_pts_delay, p_block->i_pts ) + i_delay;
1567 if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
1568 es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1570 mtime_t current_date = mdate();
1572 || p_block->i_pts > current_date + 10000000
1573 || current_date > p_block->i_pts )
1575 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1576 * for teletext streams. */
1577 p_block->i_pts = current_date + 400000
1578 + p_input->i_pts_delay + i_delay;
1582 p_block->i_rate = p_sys->i_rate;
1584 /* TODO handle mute */
1586 ( es->fmt.i_cat != AUDIO_ES ||
1587 ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1588 p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1591 bool b_cc_new = false;
1593 if( es->p_dec_record )
1595 block_t *p_dup = block_Duplicate( p_block );
1597 input_DecoderDecode( es->p_dec_record, p_dup );
1599 input_DecoderDecode( es->p_dec, p_block );
1601 /* Check CC status */
1602 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1603 for( i = 0; i < 4; i++ )
1605 static const vlc_fourcc_t fcc[4] = {
1606 VLC_FOURCC('c', 'c', '1', ' '),
1607 VLC_FOURCC('c', 'c', '2', ' '),
1608 VLC_FOURCC('c', 'c', '3', ' '),
1609 VLC_FOURCC('c', 'c', '4', ' '),
1613 if( es->pb_cc_present[i] || !pb_cc[i] )
1615 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1617 es_format_Init( &fmt, SPU_ES, fcc[i] );
1618 fmt.i_group = es->fmt.i_group;
1619 if( asprintf( &fmt.psz_description,
1620 _("Closed captions %u"), 1 + i ) == -1 )
1621 fmt.psz_description = NULL;
1622 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1623 es->pp_cc_es[i]->p_master = es;
1624 es_format_Clean( &fmt );
1627 es->pb_cc_present[i] = true;
1631 var_SetBool( p_sys->p_input, "intf-change", true );
1635 block_Release( p_block );
1641 /*****************************************************************************
1643 *****************************************************************************/
1644 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1646 es_out_sys_t *p_sys = out->p_sys;
1647 bool b_reselect = false;
1650 /* We don't try to reselect */
1653 while( !out->p_sys->p_input->b_die && es->p_dec )
1655 if( input_DecoderEmpty( es->p_dec ) &&
1656 ( !es->p_dec_record || input_DecoderEmpty( es->p_dec_record ) ))
1660 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1663 if( es->p_pgrm == p_sys->p_pgrm )
1664 EsOutESVarUpdate( out, es, true );
1666 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1669 if( es->p_pgrm->i_es == 0 )
1671 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1674 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1675 p_sys->p_es_sub == es ) b_reselect = true;
1677 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1678 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1679 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1681 switch( es->fmt.i_cat )
1694 /* Re-select another track when needed */
1696 for( i = 0; i < p_sys->i_es; i++ )
1698 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1699 EsOutSelect( out, p_sys->es[i], false );
1702 free( es->psz_language );
1703 free( es->psz_language_code );
1705 es_format_Clean( &es->fmt );
1711 * Control query handler
1713 * \param out the es_out to control
1714 * \param i_query A es_out query as defined in include/ninput.h
1715 * \param args a variable list of arguments for the query
1716 * \return VLC_SUCCESS or an error code
1718 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1720 es_out_sys_t *p_sys = out->p_sys;
1728 case ES_OUT_SET_ES_STATE:
1729 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1730 b = (bool) va_arg( args, int );
1731 if( b && !EsIsSelected( es ) )
1733 EsSelect( out, es );
1734 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1736 else if( !b && EsIsSelected( es ) )
1738 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1743 case ES_OUT_GET_ES_STATE:
1744 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1745 pb = (bool*) va_arg( args, bool * );
1747 *pb = EsIsSelected( es );
1750 case ES_OUT_SET_ACTIVE:
1752 b = (bool) va_arg( args, int );
1753 p_sys->b_active = b;
1756 var_SetBool( p_sys->p_input, "intf-change", true );
1760 case ES_OUT_GET_ACTIVE:
1761 pb = (bool*) va_arg( args, bool * );
1762 *pb = p_sys->b_active;
1765 case ES_OUT_SET_MODE:
1766 i = (int) va_arg( args, int );
1767 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1768 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1772 /* Reapply policy mode */
1773 for( i = 0; i < p_sys->i_es; i++ )
1775 if( EsIsSelected( p_sys->es[i] ) )
1777 EsUnselect( out, p_sys->es[i],
1778 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1781 for( i = 0; i < p_sys->i_es; i++ )
1783 EsOutSelect( out, p_sys->es[i], false );
1787 return VLC_EGENERIC;
1789 case ES_OUT_GET_MODE:
1790 pi = (int*) va_arg( args, int* );
1791 *pi = p_sys->i_mode;
1795 case ES_OUT_RESTART_ES:
1799 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1803 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1805 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1807 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1812 for( i = 0; i < p_sys->i_es; i++ )
1816 if( es == p_sys->es[i] )
1818 EsOutSelect( out, es, true );
1824 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
1826 if( EsIsSelected( p_sys->es[i] ) )
1828 if( i_query == ES_OUT_RESTART_ES )
1830 if( p_sys->es[i]->p_dec )
1832 EsDestroyDecoder( out, p_sys->es[i] );
1833 EsCreateDecoder( out, p_sys->es[i] );
1838 EsUnselect( out, p_sys->es[i],
1839 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1845 if( i_query == ES_OUT_SET_ES )
1848 event.type = vlc_InputSelectedStreamChanged;
1849 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1854 case ES_OUT_SET_DEFAULT:
1856 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1860 /*p_sys->i_default_video_id = -1;*/
1861 /*p_sys->i_default_audio_id = -1;*/
1862 p_sys->i_default_sub_id = -1;
1864 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1866 /*p_sys->i_default_video_id = -1;*/
1868 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1870 /*p_sys->i_default_audio_id = -1;*/
1872 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1874 p_sys->i_default_sub_id = -1;
1878 /*if( es->fmt.i_cat == VIDEO_ES )
1879 p_sys->i_default_video_id = es->i_id;
1881 if( es->fmt.i_cat == AUDIO_ES )
1882 p_sys->i_default_audio_id = es->i_id;
1884 if( es->fmt.i_cat == SPU_ES )
1885 p_sys->i_default_sub_id = es->i_id;
1890 case ES_OUT_SET_PCR:
1891 case ES_OUT_SET_GROUP_PCR:
1893 es_out_pgrm_t *p_pgrm = NULL;
1897 if( i_query == ES_OUT_SET_PCR )
1899 p_pgrm = p_sys->p_pgrm;
1904 i_group = (int)va_arg( args, int );
1905 for( i = 0; i < p_sys->i_pgrm; i++ )
1907 if( p_sys->pgrm[i]->i_id == i_group )
1909 p_pgrm = p_sys->pgrm[i];
1914 if( p_pgrm == NULL )
1915 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1917 i_pcr = (int64_t)va_arg( args, int64_t );
1919 * TODO do not use mdate() but proper stream acquisition date */
1920 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
1921 p_sys->p_input->b_can_pace_control, i_pcr, mdate() );
1925 case ES_OUT_RESET_PCR:
1926 for( i = 0; i < p_sys->i_pgrm; i++ )
1927 input_clock_Reset( p_sys->pgrm[i]->p_clock );
1933 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1934 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1935 *pi_ts = input_clock_GetTS( p_sys->p_pgrm->p_clock,
1936 p_sys->p_input->i_pts_delay, i_ts );
1939 return VLC_EGENERIC;
1941 case ES_OUT_GET_GROUP:
1942 pi = (int*) va_arg( args, int* );
1944 *pi = p_sys->p_pgrm->i_id;
1946 *pi = -1; /* FIXME */
1949 case ES_OUT_SET_GROUP:
1952 i = (int) va_arg( args, int );
1953 for( j = 0; j < p_sys->i_pgrm; j++ )
1955 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1956 if( p_pgrm->i_id == i )
1958 EsOutProgramSelect( out, p_pgrm );
1962 return VLC_EGENERIC;
1965 case ES_OUT_SET_FMT:
1967 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1968 * to update the p_extra data */
1970 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1971 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1973 return VLC_EGENERIC;
1975 if( p_fmt->i_extra )
1977 es->fmt.i_extra = p_fmt->i_extra;
1978 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1979 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1984 EsDestroyDecoder( out, es );
1986 EsCreateDecoder( out, es );
1988 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1989 es->p_dec->fmt_in.p_extra =
1990 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1991 memcpy( es->p_dec->fmt_in.p_extra,
1992 p_fmt->p_extra, p_fmt->i_extra );
1999 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2003 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2004 i_date = (int64_t)va_arg( args, int64_t );
2006 if( !es || !es->p_dec )
2007 return VLC_EGENERIC;
2009 /* XXX We should call input_clock_GetTS but PCR has been reseted
2010 * and it will return 0, so we won't call input_clock_GetTS on all preroll samples
2011 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
2012 es->i_preroll_end = i_date;
2016 case ES_OUT_SET_GROUP_META:
2018 int i_group = (int)va_arg( args, int );
2019 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2021 EsOutProgramMeta( out, i_group, p_meta );
2024 case ES_OUT_SET_GROUP_EPG:
2026 int i_group = (int)va_arg( args, int );
2027 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2029 EsOutProgramEpg( out, i_group, p_epg );
2032 case ES_OUT_DEL_GROUP:
2034 int i_group = (int)va_arg( args, int );
2036 return EsOutProgramDel( out, i_group );
2040 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2041 return VLC_EGENERIC;
2045 /****************************************************************************
2046 * LanguageGetName: try to expend iso639 into plain name
2047 ****************************************************************************/
2048 static char *LanguageGetName( const char *psz_code )
2050 const iso639_lang_t *pl;
2052 if( psz_code == NULL )
2054 return strdup( "" );
2057 if( strlen( psz_code ) == 2 )
2059 pl = GetLang_1( psz_code );
2061 else if( strlen( psz_code ) == 3 )
2063 pl = GetLang_2B( psz_code );
2064 if( !strcmp( pl->psz_iso639_1, "??" ) )
2066 pl = GetLang_2T( psz_code );
2071 return strdup( psz_code );
2074 if( !strcmp( pl->psz_iso639_1, "??" ) )
2076 return strdup( psz_code );
2080 if( *pl->psz_native_name )
2082 return strdup( pl->psz_native_name );
2084 return strdup( pl->psz_eng_name );
2088 /* Get a 2 char code */
2089 static char *LanguageGetCode( const char *psz_lang )
2091 const iso639_lang_t *pl;
2093 if( psz_lang == NULL || *psz_lang == '\0' )
2094 return strdup("??");
2096 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2098 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2099 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2100 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2101 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2102 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2106 if( pl->psz_iso639_1 != NULL )
2107 return strdup( pl->psz_iso639_1 );
2109 return strdup("??");
2112 static char **LanguageSplit( const char *psz_langs )
2119 if( psz_langs == NULL ) return NULL;
2121 psz_parser = psz_dup = strdup(psz_langs);
2123 while( psz_parser && *psz_parser )
2128 psz = strchr(psz_parser, ',' );
2129 if( psz ) *psz++ = '\0';
2131 if( !strcmp( psz_parser, "any" ) )
2133 TAB_APPEND( i_psz, ppsz, strdup("any") );
2137 psz_code = LanguageGetCode( psz_parser );
2138 if( strcmp( psz_code, "??" ) )
2140 TAB_APPEND( i_psz, ppsz, psz_code );
2153 TAB_APPEND( i_psz, ppsz, NULL );
2160 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2164 if( !ppsz_langs || !psz_lang ) return -1;
2166 for( i = 0; ppsz_langs[i]; i++ )
2168 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2169 !strcasecmp( ppsz_langs[i], "any" ) )
2178 /****************************************************************************
2180 * - add meta info to the playlist item
2181 ****************************************************************************/
2182 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2184 es_out_sys_t *p_sys = out->p_sys;
2185 input_thread_t *p_input = p_sys->p_input;
2186 es_format_t *fmt = &es->fmt;
2190 /* Add stream info */
2191 if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
2194 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2195 "%.4s", (char*)&fmt->i_codec );
2197 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2198 "%s", es->psz_language );
2200 /* Add information */
2201 switch( fmt->i_cat )
2204 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2205 _("Type"), _("Audio") );
2207 if( fmt->audio.i_channels > 0 )
2208 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2209 "%u", fmt->audio.i_channels );
2211 if( fmt->audio.i_rate > 0 )
2213 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2214 _("%u Hz"), fmt->audio.i_rate );
2215 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2218 if( fmt->audio.i_bitspersample > 0 )
2219 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2220 _("Bits per sample"), "%u",
2221 fmt->audio.i_bitspersample );
2223 if( fmt->i_bitrate > 0 )
2225 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2226 _("%u kb/s"), fmt->i_bitrate / 1000 );
2227 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2232 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2233 _("Type"), _("Video") );
2235 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2236 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2237 _("Resolution"), "%ux%u",
2238 fmt->video.i_width, fmt->video.i_height );
2240 if( fmt->video.i_visible_width > 0 &&
2241 fmt->video.i_visible_height > 0 )
2242 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2243 _("Display resolution"), "%ux%u",
2244 fmt->video.i_visible_width,
2245 fmt->video.i_visible_height);
2246 if( fmt->video.i_frame_rate > 0 &&
2247 fmt->video.i_frame_rate_base > 0 )
2249 div = lldiv( (float)fmt->video.i_frame_rate /
2250 fmt->video.i_frame_rate_base * 1000000,
2252 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2253 _("Frame rate"), "%"PRId64".%06u",
2254 div.quot, (unsigned int )div.rem );
2259 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2260 _("Type"), _("Subtitle") );