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;
358 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock, p_sys->p_input );
361 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
363 es_out_sys_t *p_sys = out->p_sys;
366 for( i = 0; i < p_sys->i_es; i++ )
368 es_out_id_t *es = p_sys->es[i];
370 /* Send a dummy block to let decoder know that
371 * there is a discontinuity */
372 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
374 input_DecoderDiscontinuity( es->p_dec, b_flush );
375 if( es->p_dec_record )
376 input_DecoderDiscontinuity( es->p_dec_record, b_flush );
380 void input_EsOutChangeRate( es_out_t *out, int i_rate )
382 es_out_sys_t *p_sys = out->p_sys;
385 p_sys->i_rate = i_rate;
386 EsOutDiscontinuity( out, false, false );
388 for( i = 0; i < p_sys->i_pgrm; i++ )
389 input_clock_SetRate( p_sys->pgrm[i]->p_clock, i_rate );
392 int input_EsOutSetRecord( es_out_t *out, bool b_record )
394 es_out_sys_t *p_sys = out->p_sys;
395 input_thread_t *p_input = p_sys->p_input;
397 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
401 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
402 if( !psz_path || *psz_path == '\0' )
405 psz_path = strdup( config_GetHomeDir() );
408 char *psz_sout = NULL; // TODO conf
410 if( !psz_sout && psz_path )
412 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
415 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
426 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
430 if( !p_sys->p_sout_record )
433 for( int i = 0; i < p_sys->i_es; i++ )
435 es_out_id_t *p_es = p_sys->es[i];
437 if( !p_es->p_dec || p_es->p_master )
440 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
445 for( int i = 0; i < p_sys->i_es; i++ )
447 es_out_id_t *p_es = p_sys->es[i];
449 if( !p_es->p_dec_record )
452 input_DecoderDelete( p_es->p_dec_record );
453 p_es->p_dec_record = NULL;
456 sout_DeleteInstance( p_sys->p_sout_record );
458 p_sys->p_sout_record = NULL;
463 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
465 es_out_sys_t *p_sys = out->p_sys;
467 if( i_cat == AUDIO_ES )
468 p_sys->i_audio_delay = i_delay;
469 else if( i_cat == SPU_ES )
470 p_sys->i_spu_delay = i_delay;
472 void input_EsOutChangeState( es_out_t *out )
474 es_out_sys_t *p_sys = out->p_sys;
475 input_thread_t *p_input = p_sys->p_input;
477 if( p_input->i_state == PAUSE_S )
479 /* Send discontinuity to decoders (it will allow them to flush
480 * * if implemented */
481 EsOutDiscontinuity( out, false, false );
485 /* Out of pause, reset pcr */
486 es_out_Control( out, ES_OUT_RESET_PCR );
489 void input_EsOutChangePosition( es_out_t *out )
491 //es_out_sys_t *p_sys = out->p_sys;
493 es_out_Control( out, ES_OUT_RESET_PCR );
494 EsOutDiscontinuity( out, true, false );
497 bool input_EsOutDecodersEmpty( es_out_t *out )
499 es_out_sys_t *p_sys = out->p_sys;
502 for( i = 0; i < p_sys->i_es; i++ )
504 es_out_id_t *es = p_sys->es[i];
506 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
508 if( es->p_dec_record && !input_DecoderEmpty( es->p_dec_record ) )
514 /*****************************************************************************
516 *****************************************************************************/
517 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
520 es_out_sys_t *p_sys = out->p_sys;
521 input_thread_t *p_input = p_sys->p_input;
522 const bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
523 vlc_value_t val, text;
527 if( fmt->i_cat == AUDIO_ES )
528 psz_var = "audio-es";
529 else if( fmt->i_cat == VIDEO_ES )
530 psz_var = "video-es";
531 else if( fmt->i_cat == SPU_ES )
539 var_SetInteger( p_sys->p_input, "teletext-es", -1 );
542 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
544 var_SetBool( p_sys->p_input, "intf-change", true );
548 /* Get the number of ES already added */
549 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
554 /* First one, we need to add the "Disable" choice */
555 val2.i_int = -1; text.psz_string = _("Disable");
556 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
560 /* Take care of the ES description */
561 if( fmt->psz_description && *fmt->psz_description )
563 if( psz_language && *psz_language )
565 text.psz_string = malloc( strlen( fmt->psz_description) +
566 strlen( psz_language ) + 10 );
567 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
570 else text.psz_string = strdup( fmt->psz_description );
574 if( psz_language && *psz_language )
576 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
577 text.psz_string = NULL;
581 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
582 text.psz_string = NULL;
587 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
589 free( text.psz_string );
593 if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
594 var_SetInteger( p_sys->p_input, "teletext-es", i_id );
597 var_SetBool( p_sys->p_input, "intf-change", true );
600 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
603 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
606 /* EsOutProgramSelect:
607 * Select a program and update the object variable
609 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
611 es_out_sys_t *p_sys = out->p_sys;
612 input_thread_t *p_input = p_sys->p_input;
616 if( p_sys->p_pgrm == p_pgrm )
617 return; /* Nothing to do */
621 es_out_pgrm_t *old = p_sys->p_pgrm;
622 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
624 for( i = 0; i < p_sys->i_es; i++ )
626 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
627 p_sys->i_mode != ES_OUT_MODE_ALL )
628 EsUnselect( out, p_sys->es[i], true );
631 p_sys->p_es_audio = NULL;
632 p_sys->p_es_sub = NULL;
633 p_sys->p_es_video = NULL;
636 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
638 /* Mark it selected */
639 p_pgrm->b_selected = true;
641 /* Switch master stream */
643 input_clock_SetMaster( p_sys->p_pgrm->p_clock, false );
644 input_clock_SetMaster( p_pgrm->p_clock, true );
645 p_sys->p_pgrm = p_pgrm;
647 /* Update "program" */
648 val.i_int = p_pgrm->i_id;
649 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
652 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
653 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
654 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
655 var_SetInteger( p_input, "teletext-es", -1 );
656 for( i = 0; i < p_sys->i_es; i++ )
658 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
659 EsOutESVarUpdate( out, p_sys->es[i], false );
660 EsOutSelect( out, p_sys->es[i], false );
663 /* Update now playing */
664 input_item_SetNowPlaying( p_input->p->input.p_item,
665 p_pgrm->psz_now_playing );
666 input_item_SetPublisher( p_input->p->input.p_item,
667 p_pgrm->psz_publisher );
669 var_SetBool( p_sys->p_input, "intf-change", true );
675 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
677 es_out_sys_t *p_sys = out->p_sys;
678 input_thread_t *p_input = p_sys->p_input;
681 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
686 p_pgrm->i_id = i_group;
688 p_pgrm->b_selected = false;
689 p_pgrm->psz_name = NULL;
690 p_pgrm->psz_now_playing = NULL;
691 p_pgrm->psz_publisher = NULL;
692 p_pgrm->p_epg = NULL;
693 p_pgrm->p_clock = input_clock_New( false, p_input->p->input.i_cr_average, p_sys->i_rate );
694 if( !p_pgrm->p_clock )
701 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
703 /* Update "program" variable */
705 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
707 if( i_group == var_GetInteger( p_input, "program" ) )
709 EsOutProgramSelect( out, p_pgrm );
713 var_SetBool( p_sys->p_input, "intf-change", true );
721 static int EsOutProgramDel( es_out_t *out, int i_group )
723 es_out_sys_t *p_sys = out->p_sys;
724 input_thread_t *p_input = p_sys->p_input;
725 es_out_pgrm_t *p_pgrm = NULL;
729 for( i = 0; i < p_sys->i_pgrm; i++ )
731 if( p_sys->pgrm[i]->i_id == i_group )
733 p_pgrm = p_sys->pgrm[i];
743 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
744 i_group, p_pgrm->i_es );
748 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
750 /* If program is selected we need to unselect it */
751 if( p_sys->p_pgrm == p_pgrm )
752 p_sys->p_pgrm = NULL;
754 input_clock_Delete( p_pgrm->p_clock );
756 free( p_pgrm->psz_name );
757 free( p_pgrm->psz_now_playing );
758 free( p_pgrm->psz_publisher );
760 vlc_epg_Delete( p_pgrm->p_epg );
763 /* Update "program" variable */
765 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
767 var_SetBool( p_sys->p_input, "intf-change", true );
774 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
777 if( p_pgrm->psz_name )
779 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
784 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
790 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
792 es_out_sys_t *p_sys = out->p_sys;
793 es_out_pgrm_t *p_pgrm = NULL;
794 input_thread_t *p_input = p_sys->p_input;
796 const char *psz_title = NULL;
797 const char *psz_provider = NULL;
800 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
802 /* Check against empty meta data (empty for what we handle) */
803 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
804 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
805 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
806 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
811 for( i = 0; i < p_sys->i_pgrm; i++ )
813 if( p_sys->pgrm[i]->i_id == i_group )
815 p_pgrm = p_sys->pgrm[i];
820 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
823 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
824 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
826 /* Update the description text of the program */
827 if( psz_title && *psz_title )
832 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
834 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
836 /* Remove old entries */
837 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
838 /* TODO update epg name */
841 free( p_pgrm->psz_name );
842 p_pgrm->psz_name = strdup( psz_title );
844 /* ugly but it works */
846 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
848 if( psz_provider && *psz_provider )
850 if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
852 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
853 free( text.psz_string );
858 text.psz_string = (char *)psz_title;
859 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
863 psz_cat = EsOutProgramGetMetaName( p_pgrm );
866 if( p_sys->p_pgrm == p_pgrm )
867 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
868 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
870 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
871 for( i = 0; ppsz_all_keys[i]; i++ )
873 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
874 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
875 free( ppsz_all_keys[i] );
877 free( ppsz_all_keys );
882 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
887 for( i = 0; i < p_src->i_event; i++ )
889 vlc_epg_event_t *p_evt = p_src->pp_event[i];
893 for( j = 0; j < p_dst->i_event; j++ )
895 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
900 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
905 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
908 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
909 p_copy->i_start = p_evt->i_start;
910 p_copy->i_duration = p_evt->i_duration;
911 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
912 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
913 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
914 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
918 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
920 /* Keep only 1 old event */
921 if( p_dst->p_current )
923 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
924 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
928 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
930 es_out_sys_t *p_sys = out->p_sys;
931 input_thread_t *p_input = p_sys->p_input;
932 es_out_pgrm_t *p_pgrm = NULL;
937 for( i = 0; i < p_sys->i_pgrm; i++ )
939 if( p_sys->pgrm[i]->i_id == i_group )
941 p_pgrm = p_sys->pgrm[i];
946 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
950 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
951 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
954 psz_cat = EsOutProgramGetMetaName( p_pgrm );
955 #ifdef HAVE_LOCALTIME_R
957 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
959 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
960 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
961 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
963 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
964 time_t t_start = (time_t)p_evt->i_start;
968 localtime_r( &t_start, &tm_start );
970 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
971 if( p_evt->psz_short_description || p_evt->psz_description )
972 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
974 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
975 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
977 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
979 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
983 /* Update now playing */
984 free( p_pgrm->psz_now_playing );
985 p_pgrm->psz_now_playing = NULL;
986 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
987 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
989 if( p_pgrm == p_sys->p_pgrm )
990 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
992 if( p_pgrm->psz_now_playing )
994 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
995 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
996 p_pgrm->psz_now_playing );
1000 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1001 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1010 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
1012 es_out_sys_t *p_sys = out->p_sys;
1013 input_thread_t *p_input = p_sys->p_input;
1015 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
1016 es_out_pgrm_t *p_pgrm = NULL;
1019 if( !es ) return NULL;
1021 if( fmt->i_group < 0 )
1023 msg_Err( p_input, "invalid group number" );
1028 /* Search the program */
1029 for( i = 0; i < p_sys->i_pgrm; i++ )
1031 if( fmt->i_group == p_sys->pgrm[i]->i_id )
1033 p_pgrm = p_sys->pgrm[i];
1037 if( p_pgrm == NULL )
1039 /* Create a new one */
1040 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1043 /* Increase ref count for program */
1048 fmt->i_id = out->p_sys->i_id;
1049 es->i_id = fmt->i_id;
1050 es->p_pgrm = p_pgrm;
1051 es_format_Copy( &es->fmt, fmt );
1052 es->i_preroll_end = -1;
1054 switch( fmt->i_cat )
1058 audio_replay_gain_t rg;
1060 es->i_channel = p_sys->i_audio;
1062 memset( &rg, 0, sizeof(rg) );
1063 vlc_mutex_lock( &p_input->p->input.p_item->lock );
1064 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
1065 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1067 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1069 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1071 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1072 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1074 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1076 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1077 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1084 es->i_channel = p_sys->i_video;
1085 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
1086 vlc_ureduce( &es->fmt.video.i_frame_rate,
1087 &es->fmt.video.i_frame_rate_base,
1088 fmt->video.i_frame_rate,
1089 fmt->video.i_frame_rate_base, 0 );
1093 es->i_channel = p_sys->i_sub;
1100 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
1101 es->psz_language_code = LanguageGetCode( fmt->psz_language );
1103 es->p_dec_record = NULL;
1104 for( i = 0; i < 4; i++ )
1105 es->pb_cc_present[i] = false;
1106 es->p_master = NULL;
1108 if( es->p_pgrm == p_sys->p_pgrm )
1109 EsOutESVarUpdate( out, es, false );
1111 /* Select it if needed */
1112 EsOutSelect( out, es, false );
1115 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1116 p_sys->i_id++; /* always incremented */
1117 switch( fmt->i_cat )
1130 EsOutAddInfo( out, es );
1135 static bool EsIsSelected( es_out_id_t *es )
1139 bool b_decode = false;
1140 if( es->p_master->p_dec )
1142 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1143 if( i_channel != -1 )
1144 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1150 return es->p_dec != NULL;
1153 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1155 es_out_sys_t *p_sys = out->p_sys;
1156 input_thread_t *p_input = p_sys->p_input;
1158 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_input->p->p_sout );
1159 if( p_es->p_dec && !p_es->p_master && p_sys->p_sout_record )
1160 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
1162 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1169 input_DecoderDelete( p_es->p_dec );
1172 if( p_es->p_dec_record )
1174 input_DecoderDelete( p_es->p_dec_record );
1175 p_es->p_dec_record = NULL;
1179 static void EsSelect( es_out_t *out, es_out_id_t *es )
1181 es_out_sys_t *p_sys = out->p_sys;
1182 input_thread_t *p_input = p_sys->p_input;
1184 const char *psz_var;
1186 if( EsIsSelected( es ) )
1188 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1195 if( !es->p_master->p_dec )
1198 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1199 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1204 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1206 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1208 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1213 else if( es->fmt.i_cat == AUDIO_ES )
1215 var_Get( p_input, "audio", &val );
1216 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1218 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1223 if( es->fmt.i_cat == SPU_ES )
1225 var_Get( p_input, "spu", &val );
1226 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1228 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1234 es->i_preroll_end = -1;
1235 EsCreateDecoder( out, es );
1237 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1241 if( es->fmt.i_cat == VIDEO_ES )
1242 psz_var = "video-es";
1243 else if( es->fmt.i_cat == AUDIO_ES )
1244 psz_var = "audio-es";
1245 else if( es->fmt.i_cat == SPU_ES )
1250 /* Mark it as selected */
1251 val.i_int = es->i_id;
1252 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1254 var_SetBool( p_sys->p_input, "intf-change", true );
1257 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1259 es_out_sys_t *p_sys = out->p_sys;
1260 input_thread_t *p_input = p_sys->p_input;
1262 const char *psz_var;
1264 if( !EsIsSelected( es ) )
1266 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1272 if( es->p_master->p_dec )
1274 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1275 if( i_channel != -1 )
1276 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1281 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1283 for( i = 0; i < 4; i++ )
1285 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1288 if( i_spu_id == es->pp_cc_es[i]->i_id )
1290 /* Force unselection of the CC */
1292 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1294 var_SetBool( p_sys->p_input, "intf-change", true );
1296 EsOutDel( out, es->pp_cc_es[i] );
1298 es->pb_cc_present[i] = false;
1300 EsDestroyDecoder( out, es );
1307 if( es->p_dec == NULL )
1309 if( es->fmt.i_cat == VIDEO_ES )
1310 psz_var = "video-es";
1311 else if( es->fmt.i_cat == AUDIO_ES )
1312 psz_var = "audio-es";
1313 else if( es->fmt.i_cat == SPU_ES )
1318 /* Mark it as unselected */
1320 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1322 var_SetBool( p_sys->p_input, "intf-change", true );
1326 * Select an ES given the current mode
1327 * XXX: you need to take a the lock before (stream.stream_lock)
1329 * \param out The es_out structure
1330 * \param es es_out_id structure
1331 * \param b_force ...
1334 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1336 es_out_sys_t *p_sys = out->p_sys;
1338 int i_cat = es->fmt.i_cat;
1340 if( !p_sys->b_active ||
1341 ( !b_force && es->fmt.i_priority < 0 ) )
1346 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1348 if( !EsIsSelected( es ) )
1349 EsSelect( out, es );
1351 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1355 var_Get( p_sys->p_input, "programs", &val );
1356 for ( i = 0; i < val.p_list->i_count; i++ )
1358 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1360 if( !EsIsSelected( es ) )
1361 EsSelect( out, es );
1365 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1367 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1371 if( es->p_pgrm != p_sys->p_pgrm )
1374 if( i_cat == AUDIO_ES )
1376 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1377 es->psz_language_code );
1379 if( p_sys->p_es_audio &&
1380 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1382 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1383 p_sys->p_es_audio->psz_language_code );
1385 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1387 i_wanted = es->i_channel;
1391 /* Select audio if (no audio selected yet)
1392 * - no audio-language
1393 * - no audio code for the ES
1394 * - audio code in the requested list */
1396 !strcmp( es->psz_language_code, "??" ) ||
1397 !p_sys->ppsz_audio_language )
1398 i_wanted = es->i_channel;
1401 if( p_sys->i_audio_last >= 0 )
1402 i_wanted = p_sys->i_audio_last;
1404 if( p_sys->i_audio_id >= 0 )
1406 if( es->i_id == p_sys->i_audio_id )
1407 i_wanted = es->i_channel;
1412 else if( i_cat == SPU_ES )
1414 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1415 es->psz_language_code );
1417 if( p_sys->p_es_sub &&
1418 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1420 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1421 p_sys->p_es_sub->psz_language_code );
1423 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1424 idx1, es->psz_language_code, idx2,
1425 p_sys->p_es_sub->psz_language_code );
1427 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1429 /* We found a SPU that matches our language request */
1430 i_wanted = es->i_channel;
1432 else if( idx1 >= 0 )
1434 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1435 idx1, es->psz_language_code );
1437 i_wanted = es->i_channel;
1439 else if( p_sys->i_default_sub_id >= 0 )
1441 if( es->i_id == p_sys->i_default_sub_id )
1442 i_wanted = es->i_channel;
1445 if( p_sys->i_sub_last >= 0 )
1446 i_wanted = p_sys->i_sub_last;
1448 if( p_sys->i_sub_id >= 0 )
1450 if( es->i_id == p_sys->i_sub_id )
1451 i_wanted = es->i_channel;
1456 else if( i_cat == VIDEO_ES )
1458 i_wanted = es->i_channel;
1461 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1462 EsSelect( out, es );
1465 /* FIXME TODO handle priority here */
1466 if( EsIsSelected( es ) )
1468 if( i_cat == AUDIO_ES )
1470 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1471 p_sys->p_es_audio &&
1472 p_sys->p_es_audio != es &&
1473 EsIsSelected( p_sys->p_es_audio ) )
1475 EsUnselect( out, p_sys->p_es_audio, false );
1477 p_sys->p_es_audio = es;
1479 else if( i_cat == SPU_ES )
1481 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1483 p_sys->p_es_sub != es &&
1484 EsIsSelected( p_sys->p_es_sub ) )
1486 EsUnselect( out, p_sys->p_es_sub, false );
1488 p_sys->p_es_sub = es;
1490 else if( i_cat == VIDEO_ES )
1492 p_sys->p_es_video = es;
1498 * Send a block for the given es_out
1500 * \param out the es_out to send from
1501 * \param es the es_out_id
1502 * \param p_block the data block to send
1504 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1506 es_out_sys_t *p_sys = out->p_sys;
1507 input_thread_t *p_input = p_sys->p_input;
1508 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1512 if( es->fmt.i_cat == AUDIO_ES )
1513 i_delay = p_sys->i_audio_delay;
1514 else if( es->fmt.i_cat == SPU_ES )
1515 i_delay = p_sys->i_spu_delay;
1519 if( libvlc_stats (p_input) )
1521 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1522 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1523 p_block->i_buffer, &i_total );
1524 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1525 (float)i_total, NULL );
1526 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1529 /* Mark preroll blocks */
1530 if( es->i_preroll_end >= 0 )
1532 int64_t i_date = p_block->i_pts;
1534 i_date = p_block->i_dts;
1536 if( i_date < es->i_preroll_end )
1537 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1539 es->i_preroll_end = -1;
1542 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1544 p_block->i_dts += i_delay;
1546 else if( p_block->i_dts > 0 )
1549 input_clock_GetTS( p_pgrm->p_clock, p_input, p_block->i_dts ) + i_delay;
1551 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1553 p_block->i_pts += i_delay;
1555 else if( p_block->i_pts > 0 )
1558 input_clock_GetTS( p_pgrm->p_clock, p_input, p_block->i_pts ) + i_delay;
1560 if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
1561 es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1563 mtime_t current_date = mdate();
1565 || p_block->i_pts > current_date + 10000000
1566 || current_date > p_block->i_pts )
1568 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1569 * for teletext streams. */
1570 p_block->i_pts = current_date + 400000
1571 + p_input->i_pts_delay + i_delay;
1575 p_block->i_rate = p_sys->i_rate;
1577 /* TODO handle mute */
1579 ( es->fmt.i_cat != AUDIO_ES ||
1580 ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1581 p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1584 bool b_cc_new = false;
1586 if( es->p_dec_record )
1588 block_t *p_dup = block_Duplicate( p_block );
1590 input_DecoderDecode( es->p_dec_record, p_dup );
1592 input_DecoderDecode( es->p_dec, p_block );
1594 /* Check CC status */
1595 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1596 for( i = 0; i < 4; i++ )
1598 static const vlc_fourcc_t fcc[4] = {
1599 VLC_FOURCC('c', 'c', '1', ' '),
1600 VLC_FOURCC('c', 'c', '2', ' '),
1601 VLC_FOURCC('c', 'c', '3', ' '),
1602 VLC_FOURCC('c', 'c', '4', ' '),
1606 if( es->pb_cc_present[i] || !pb_cc[i] )
1608 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1610 es_format_Init( &fmt, SPU_ES, fcc[i] );
1611 fmt.i_group = es->fmt.i_group;
1612 if( asprintf( &fmt.psz_description,
1613 _("Closed captions %u"), 1 + i ) == -1 )
1614 fmt.psz_description = NULL;
1615 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1616 es->pp_cc_es[i]->p_master = es;
1617 es_format_Clean( &fmt );
1620 es->pb_cc_present[i] = true;
1624 var_SetBool( p_sys->p_input, "intf-change", true );
1628 block_Release( p_block );
1634 /*****************************************************************************
1636 *****************************************************************************/
1637 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1639 es_out_sys_t *p_sys = out->p_sys;
1640 bool b_reselect = false;
1643 /* We don't try to reselect */
1646 while( !out->p_sys->p_input->b_die && es->p_dec )
1648 if( input_DecoderEmpty( es->p_dec ) &&
1649 ( !es->p_dec_record || input_DecoderEmpty( es->p_dec_record ) ))
1653 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1656 if( es->p_pgrm == p_sys->p_pgrm )
1657 EsOutESVarUpdate( out, es, true );
1659 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1662 if( es->p_pgrm->i_es == 0 )
1664 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1667 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1668 p_sys->p_es_sub == es ) b_reselect = true;
1670 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1671 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1672 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1674 switch( es->fmt.i_cat )
1687 /* Re-select another track when needed */
1689 for( i = 0; i < p_sys->i_es; i++ )
1691 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1692 EsOutSelect( out, p_sys->es[i], false );
1695 free( es->psz_language );
1696 free( es->psz_language_code );
1698 es_format_Clean( &es->fmt );
1704 * Control query handler
1706 * \param out the es_out to control
1707 * \param i_query A es_out query as defined in include/ninput.h
1708 * \param args a variable list of arguments for the query
1709 * \return VLC_SUCCESS or an error code
1711 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1713 es_out_sys_t *p_sys = out->p_sys;
1721 case ES_OUT_SET_ES_STATE:
1722 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1723 b = (bool) va_arg( args, int );
1724 if( b && !EsIsSelected( es ) )
1726 EsSelect( out, es );
1727 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1729 else if( !b && EsIsSelected( es ) )
1731 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1736 case ES_OUT_GET_ES_STATE:
1737 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1738 pb = (bool*) va_arg( args, bool * );
1740 *pb = EsIsSelected( es );
1743 case ES_OUT_SET_ACTIVE:
1745 b = (bool) va_arg( args, int );
1746 p_sys->b_active = b;
1749 var_SetBool( p_sys->p_input, "intf-change", true );
1753 case ES_OUT_GET_ACTIVE:
1754 pb = (bool*) va_arg( args, bool * );
1755 *pb = p_sys->b_active;
1758 case ES_OUT_SET_MODE:
1759 i = (int) va_arg( args, int );
1760 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1761 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1765 /* Reapply policy mode */
1766 for( i = 0; i < p_sys->i_es; i++ )
1768 if( EsIsSelected( p_sys->es[i] ) )
1770 EsUnselect( out, p_sys->es[i],
1771 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1774 for( i = 0; i < p_sys->i_es; i++ )
1776 EsOutSelect( out, p_sys->es[i], false );
1780 return VLC_EGENERIC;
1782 case ES_OUT_GET_MODE:
1783 pi = (int*) va_arg( args, int* );
1784 *pi = p_sys->i_mode;
1788 case ES_OUT_RESTART_ES:
1792 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1796 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1798 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1800 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1805 for( i = 0; i < p_sys->i_es; i++ )
1809 if( es == p_sys->es[i] )
1811 EsOutSelect( out, es, true );
1817 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
1819 if( EsIsSelected( p_sys->es[i] ) )
1821 if( i_query == ES_OUT_RESTART_ES )
1823 if( p_sys->es[i]->p_dec )
1825 EsDestroyDecoder( out, p_sys->es[i] );
1826 EsCreateDecoder( out, p_sys->es[i] );
1831 EsUnselect( out, p_sys->es[i],
1832 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1838 if( i_query == ES_OUT_SET_ES )
1841 event.type = vlc_InputSelectedStreamChanged;
1842 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1847 case ES_OUT_SET_DEFAULT:
1849 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1853 /*p_sys->i_default_video_id = -1;*/
1854 /*p_sys->i_default_audio_id = -1;*/
1855 p_sys->i_default_sub_id = -1;
1857 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1859 /*p_sys->i_default_video_id = -1;*/
1861 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1863 /*p_sys->i_default_audio_id = -1;*/
1865 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1867 p_sys->i_default_sub_id = -1;
1871 /*if( es->fmt.i_cat == VIDEO_ES )
1872 p_sys->i_default_video_id = es->i_id;
1874 if( es->fmt.i_cat == AUDIO_ES )
1875 p_sys->i_default_audio_id = es->i_id;
1877 if( es->fmt.i_cat == SPU_ES )
1878 p_sys->i_default_sub_id = es->i_id;
1883 case ES_OUT_SET_PCR:
1884 case ES_OUT_SET_GROUP_PCR:
1886 es_out_pgrm_t *p_pgrm = NULL;
1890 if( i_query == ES_OUT_SET_PCR )
1892 p_pgrm = p_sys->p_pgrm;
1897 i_group = (int)va_arg( args, int );
1898 for( i = 0; i < p_sys->i_pgrm; i++ )
1900 if( p_sys->pgrm[i]->i_id == i_group )
1902 p_pgrm = p_sys->pgrm[i];
1907 if( p_pgrm == NULL )
1908 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1910 i_pcr = (int64_t)va_arg( args, int64_t );
1912 * TODO do not use mdate() but proper stream acquisition date */
1913 input_clock_SetPCR( p_pgrm->p_clock, p_sys->p_input, i_pcr, mdate() );
1917 case ES_OUT_RESET_PCR:
1918 for( i = 0; i < p_sys->i_pgrm; i++ )
1919 input_clock_ResetPCR( p_sys->pgrm[i]->p_clock );
1925 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1926 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1927 *pi_ts = input_clock_GetTS( p_sys->p_pgrm->p_clock,
1928 p_sys->p_input, i_ts );
1931 return VLC_EGENERIC;
1933 case ES_OUT_GET_GROUP:
1934 pi = (int*) va_arg( args, int* );
1936 *pi = p_sys->p_pgrm->i_id;
1938 *pi = -1; /* FIXME */
1941 case ES_OUT_SET_GROUP:
1944 i = (int) va_arg( args, int );
1945 for( j = 0; j < p_sys->i_pgrm; j++ )
1947 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1948 if( p_pgrm->i_id == i )
1950 EsOutProgramSelect( out, p_pgrm );
1954 return VLC_EGENERIC;
1957 case ES_OUT_SET_FMT:
1959 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1960 * to update the p_extra data */
1962 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1963 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1965 return VLC_EGENERIC;
1967 if( p_fmt->i_extra )
1969 es->fmt.i_extra = p_fmt->i_extra;
1970 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1971 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1976 EsDestroyDecoder( out, es );
1978 EsCreateDecoder( out, es );
1980 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1981 es->p_dec->fmt_in.p_extra =
1982 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1983 memcpy( es->p_dec->fmt_in.p_extra,
1984 p_fmt->p_extra, p_fmt->i_extra );
1991 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1995 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1996 i_date = (int64_t)va_arg( args, int64_t );
1998 if( !es || !es->p_dec )
1999 return VLC_EGENERIC;
2001 /* XXX We should call input_clock_GetTS but PCR has been reseted
2002 * and it will return 0, so we won't call input_clock_GetTS on all preroll samples
2003 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
2004 es->i_preroll_end = i_date;
2008 case ES_OUT_SET_GROUP_META:
2010 int i_group = (int)va_arg( args, int );
2011 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2013 EsOutProgramMeta( out, i_group, p_meta );
2016 case ES_OUT_SET_GROUP_EPG:
2018 int i_group = (int)va_arg( args, int );
2019 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2021 EsOutProgramEpg( out, i_group, p_epg );
2024 case ES_OUT_DEL_GROUP:
2026 int i_group = (int)va_arg( args, int );
2028 return EsOutProgramDel( out, i_group );
2032 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2033 return VLC_EGENERIC;
2037 /****************************************************************************
2038 * LanguageGetName: try to expend iso639 into plain name
2039 ****************************************************************************/
2040 static char *LanguageGetName( const char *psz_code )
2042 const iso639_lang_t *pl;
2044 if( psz_code == NULL )
2046 return strdup( "" );
2049 if( strlen( psz_code ) == 2 )
2051 pl = GetLang_1( psz_code );
2053 else if( strlen( psz_code ) == 3 )
2055 pl = GetLang_2B( psz_code );
2056 if( !strcmp( pl->psz_iso639_1, "??" ) )
2058 pl = GetLang_2T( psz_code );
2063 return strdup( psz_code );
2066 if( !strcmp( pl->psz_iso639_1, "??" ) )
2068 return strdup( psz_code );
2072 if( *pl->psz_native_name )
2074 return strdup( pl->psz_native_name );
2076 return strdup( pl->psz_eng_name );
2080 /* Get a 2 char code */
2081 static char *LanguageGetCode( const char *psz_lang )
2083 const iso639_lang_t *pl;
2085 if( psz_lang == NULL || *psz_lang == '\0' )
2086 return strdup("??");
2088 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2090 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2091 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2092 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2093 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2094 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2098 if( pl->psz_iso639_1 != NULL )
2099 return strdup( pl->psz_iso639_1 );
2101 return strdup("??");
2104 static char **LanguageSplit( const char *psz_langs )
2111 if( psz_langs == NULL ) return NULL;
2113 psz_parser = psz_dup = strdup(psz_langs);
2115 while( psz_parser && *psz_parser )
2120 psz = strchr(psz_parser, ',' );
2121 if( psz ) *psz++ = '\0';
2123 if( !strcmp( psz_parser, "any" ) )
2125 TAB_APPEND( i_psz, ppsz, strdup("any") );
2129 psz_code = LanguageGetCode( psz_parser );
2130 if( strcmp( psz_code, "??" ) )
2132 TAB_APPEND( i_psz, ppsz, psz_code );
2145 TAB_APPEND( i_psz, ppsz, NULL );
2152 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2156 if( !ppsz_langs || !psz_lang ) return -1;
2158 for( i = 0; ppsz_langs[i]; i++ )
2160 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2161 !strcasecmp( ppsz_langs[i], "any" ) )
2170 /****************************************************************************
2172 * - add meta info to the playlist item
2173 ****************************************************************************/
2174 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2176 es_out_sys_t *p_sys = out->p_sys;
2177 input_thread_t *p_input = p_sys->p_input;
2178 es_format_t *fmt = &es->fmt;
2182 /* Add stream info */
2183 if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
2186 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2187 "%.4s", (char*)&fmt->i_codec );
2189 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2190 "%s", es->psz_language );
2192 /* Add information */
2193 switch( fmt->i_cat )
2196 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2197 _("Type"), _("Audio") );
2199 if( fmt->audio.i_channels > 0 )
2200 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2201 "%u", fmt->audio.i_channels );
2203 if( fmt->audio.i_rate > 0 )
2205 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2206 _("%u Hz"), fmt->audio.i_rate );
2207 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2210 if( fmt->audio.i_bitspersample > 0 )
2211 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2212 _("Bits per sample"), "%u",
2213 fmt->audio.i_bitspersample );
2215 if( fmt->i_bitrate > 0 )
2217 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2218 _("%u kb/s"), fmt->i_bitrate / 1000 );
2219 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2224 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2225 _("Type"), _("Video") );
2227 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2228 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2229 _("Resolution"), "%ux%u",
2230 fmt->video.i_width, fmt->video.i_height );
2232 if( fmt->video.i_visible_width > 0 &&
2233 fmt->video.i_visible_height > 0 )
2234 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2235 _("Display resolution"), "%ux%u",
2236 fmt->video.i_visible_width,
2237 fmt->video.i_visible_height);
2238 if( fmt->video.i_frame_rate > 0 &&
2239 fmt->video.i_frame_rate_base > 0 )
2241 div = lldiv( (float)fmt->video.i_frame_rate /
2242 fmt->video.i_frame_rate_base * 1000000,
2244 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2245 _("Frame rate"), "%"PRId64".%06u",
2246 div.quot, (unsigned int )div.rem );
2251 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2252 _("Type"), _("Subtitle") );