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 */
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 free( p_pgrm->psz_now_playing );
321 free( p_pgrm->psz_publisher );
322 free( p_pgrm->psz_name );
324 vlc_epg_Delete( p_pgrm->p_epg );
328 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
334 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
339 /* Special HACK, -i_id is tha cat of the stream */
340 return (es_out_id_t*)((uint8_t*)NULL-i_id);
343 for( i = 0; i < out->p_sys->i_es; i++ )
345 if( out->p_sys->es[i]->i_id == i_id )
346 return out->p_sys->es[i];
351 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
353 es_out_sys_t *p_sys = out->p_sys;
356 for( i = 0; i < p_sys->i_es; i++ )
358 es_out_id_t *es = p_sys->es[i];
360 /* Send a dummy block to let decoder know that
361 * there is a discontinuity */
362 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
364 input_DecoderDiscontinuity( es->p_dec, b_flush );
365 if( es->p_dec_record )
366 input_DecoderDiscontinuity( es->p_dec_record, b_flush );
370 void input_EsOutChangeRate( es_out_t *out, int i_rate )
372 es_out_sys_t *p_sys = out->p_sys;
375 p_sys->i_rate = i_rate;
376 EsOutDiscontinuity( out, false, false );
378 for( i = 0; i < p_sys->i_pgrm; i++ )
379 input_ClockSetRate( &p_sys->pgrm[i]->clock, i_rate );
382 int input_EsOutSetRecord( es_out_t *out, bool b_record )
384 es_out_sys_t *p_sys = out->p_sys;
385 input_thread_t *p_input = p_sys->p_input;
387 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
391 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
392 if( !psz_path || *psz_path == '\0' )
395 psz_path = strdup( config_GetHomeDir() );
398 char *psz_sout = NULL; // TODO conf
400 if( !psz_sout && psz_path )
402 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
405 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
416 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
420 if( !p_sys->p_sout_record )
423 for( int i = 0; i < p_sys->i_es; i++ )
425 es_out_id_t *p_es = p_sys->es[i];
427 if( !p_es->p_dec || p_es->p_master )
430 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
435 for( int i = 0; i < p_sys->i_es; i++ )
437 es_out_id_t *p_es = p_sys->es[i];
439 if( !p_es->p_dec_record )
442 input_DecoderDelete( p_es->p_dec_record );
443 p_es->p_dec_record = NULL;
446 sout_DeleteInstance( p_sys->p_sout_record );
448 p_sys->p_sout_record = NULL;
453 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
455 es_out_sys_t *p_sys = out->p_sys;
457 if( i_cat == AUDIO_ES )
458 p_sys->i_audio_delay = i_delay;
459 else if( i_cat == SPU_ES )
460 p_sys->i_spu_delay = i_delay;
462 void input_EsOutChangeState( es_out_t *out )
464 es_out_sys_t *p_sys = out->p_sys;
465 input_thread_t *p_input = p_sys->p_input;
467 if( p_input->i_state == PAUSE_S )
469 /* Send discontinuity to decoders (it will allow them to flush
470 * * if implemented */
471 EsOutDiscontinuity( out, false, false );
475 /* Out of pause, reset pcr */
476 es_out_Control( out, ES_OUT_RESET_PCR );
479 void input_EsOutChangePosition( es_out_t *out )
481 //es_out_sys_t *p_sys = out->p_sys;
483 es_out_Control( out, ES_OUT_RESET_PCR );
484 EsOutDiscontinuity( out, true, false );
487 bool input_EsOutDecodersEmpty( es_out_t *out )
489 es_out_sys_t *p_sys = out->p_sys;
492 for( i = 0; i < p_sys->i_es; i++ )
494 es_out_id_t *es = p_sys->es[i];
496 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
498 if( es->p_dec_record && !input_DecoderEmpty( es->p_dec_record ) )
504 /*****************************************************************************
506 *****************************************************************************/
507 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
510 es_out_sys_t *p_sys = out->p_sys;
511 input_thread_t *p_input = p_sys->p_input;
512 const bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
513 vlc_value_t val, text;
517 if( fmt->i_cat == AUDIO_ES )
518 psz_var = "audio-es";
519 else if( fmt->i_cat == VIDEO_ES )
520 psz_var = "video-es";
521 else if( fmt->i_cat == SPU_ES )
529 var_SetInteger( p_sys->p_input, "teletext-es", -1 );
532 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
534 var_SetBool( p_sys->p_input, "intf-change", true );
538 /* Get the number of ES already added */
539 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
544 /* First one, we need to add the "Disable" choice */
545 val2.i_int = -1; text.psz_string = _("Disable");
546 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
550 /* Take care of the ES description */
551 if( fmt->psz_description && *fmt->psz_description )
553 if( psz_language && *psz_language )
555 text.psz_string = malloc( strlen( fmt->psz_description) +
556 strlen( psz_language ) + 10 );
557 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
560 else text.psz_string = strdup( fmt->psz_description );
564 if( psz_language && *psz_language )
566 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
567 text.psz_string = NULL;
571 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
572 text.psz_string = NULL;
577 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
579 free( text.psz_string );
582 var_SetInteger( p_sys->p_input, "teletext-es", i_id );
584 var_SetBool( p_sys->p_input, "intf-change", true );
587 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
590 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
593 /* EsOutProgramSelect:
594 * Select a program and update the object variable
596 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
598 es_out_sys_t *p_sys = out->p_sys;
599 input_thread_t *p_input = p_sys->p_input;
603 if( p_sys->p_pgrm == p_pgrm )
604 return; /* Nothing to do */
608 es_out_pgrm_t *old = p_sys->p_pgrm;
609 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
611 for( i = 0; i < p_sys->i_es; i++ )
613 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
614 p_sys->i_mode != ES_OUT_MODE_ALL )
615 EsUnselect( out, p_sys->es[i], true );
618 p_sys->p_es_audio = NULL;
619 p_sys->p_es_sub = NULL;
620 p_sys->p_es_video = NULL;
623 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
625 /* Mark it selected */
626 p_pgrm->b_selected = true;
628 /* Switch master stream */
629 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
631 p_sys->p_pgrm->clock.b_master = false;
633 p_pgrm->clock.b_master = true;
634 p_sys->p_pgrm = p_pgrm;
636 /* Update "program" */
637 val.i_int = p_pgrm->i_id;
638 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
641 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
642 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
643 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
644 var_SetInteger( p_input, "teletext-es", -1 );
645 for( i = 0; i < p_sys->i_es; i++ )
647 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
648 EsOutESVarUpdate( out, p_sys->es[i], false );
649 EsOutSelect( out, p_sys->es[i], false );
652 /* Update now playing */
653 input_item_SetNowPlaying( p_input->p->input.p_item,
654 p_pgrm->psz_now_playing );
655 input_item_SetPublisher( p_input->p->input.p_item,
656 p_pgrm->psz_publisher );
658 var_SetBool( p_sys->p_input, "intf-change", true );
664 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
666 es_out_sys_t *p_sys = out->p_sys;
667 input_thread_t *p_input = p_sys->p_input;
670 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
671 if( !p_pgrm ) return NULL;
674 p_pgrm->i_id = i_group;
676 p_pgrm->b_selected = false;
677 p_pgrm->psz_name = NULL;
678 p_pgrm->psz_now_playing = NULL;
679 p_pgrm->psz_publisher = NULL;
680 p_pgrm->p_epg = NULL;
681 input_ClockInit( &p_pgrm->clock, false, p_input->p->input.i_cr_average, p_sys->i_rate );
684 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
686 /* Update "program" variable */
688 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
690 if( i_group == var_GetInteger( p_input, "program" ) )
692 EsOutProgramSelect( out, p_pgrm );
696 var_SetBool( p_sys->p_input, "intf-change", true );
704 static int EsOutProgramDel( es_out_t *out, int i_group )
706 es_out_sys_t *p_sys = out->p_sys;
707 input_thread_t *p_input = p_sys->p_input;
708 es_out_pgrm_t *p_pgrm = NULL;
712 for( i = 0; i < p_sys->i_pgrm; i++ )
714 if( p_sys->pgrm[i]->i_id == i_group )
716 p_pgrm = p_sys->pgrm[i];
726 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
727 i_group, p_pgrm->i_es );
731 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
733 /* If program is selected we need to unselect it */
734 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
736 free( p_pgrm->psz_name );
737 free( p_pgrm->psz_now_playing );
738 free( p_pgrm->psz_publisher );
740 vlc_epg_Delete( p_pgrm->p_epg );
743 /* Update "program" variable */
745 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
747 var_SetBool( p_sys->p_input, "intf-change", true );
754 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
757 if( p_pgrm->psz_name )
759 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
764 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
770 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
772 es_out_sys_t *p_sys = out->p_sys;
773 es_out_pgrm_t *p_pgrm = NULL;
774 input_thread_t *p_input = p_sys->p_input;
776 const char *psz_title = NULL;
777 const char *psz_provider = NULL;
780 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
782 /* Check against empty meta data (empty for what we handle) */
783 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
784 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
785 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
786 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
791 for( i = 0; i < p_sys->i_pgrm; i++ )
793 if( p_sys->pgrm[i]->i_id == i_group )
795 p_pgrm = p_sys->pgrm[i];
800 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
803 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
804 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
806 /* Update the description text of the program */
807 if( psz_title && *psz_title )
812 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
814 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
816 /* Remove old entries */
817 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
818 /* TODO update epg name */
821 free( p_pgrm->psz_name );
822 p_pgrm->psz_name = strdup( psz_title );
824 /* ugly but it works */
826 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
828 if( psz_provider && *psz_provider )
830 if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
832 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
833 free( text.psz_string );
838 text.psz_string = (char *)psz_title;
839 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
843 psz_cat = EsOutProgramGetMetaName( p_pgrm );
846 if( p_sys->p_pgrm == p_pgrm )
847 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
848 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
850 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
851 for( i = 0; ppsz_all_keys[i]; i++ )
853 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
854 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
855 free( ppsz_all_keys[i] );
857 free( ppsz_all_keys );
862 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
867 for( i = 0; i < p_src->i_event; i++ )
869 vlc_epg_event_t *p_evt = p_src->pp_event[i];
873 for( j = 0; j < p_dst->i_event; j++ )
875 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
880 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
885 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
888 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
889 p_copy->i_start = p_evt->i_start;
890 p_copy->i_duration = p_evt->i_duration;
891 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
892 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
893 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
894 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
898 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
900 /* Keep only 1 old event */
901 if( p_dst->p_current )
903 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
904 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
908 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
910 es_out_sys_t *p_sys = out->p_sys;
911 input_thread_t *p_input = p_sys->p_input;
912 es_out_pgrm_t *p_pgrm = NULL;
917 for( i = 0; i < p_sys->i_pgrm; i++ )
919 if( p_sys->pgrm[i]->i_id == i_group )
921 p_pgrm = p_sys->pgrm[i];
926 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
930 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
931 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
934 psz_cat = EsOutProgramGetMetaName( p_pgrm );
935 #ifdef HAVE_LOCALTIME_R
937 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
939 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
940 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
941 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
943 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
944 time_t t_start = (time_t)p_evt->i_start;
948 localtime_r( &t_start, &tm_start );
950 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
951 if( p_evt->psz_short_description || p_evt->psz_description )
952 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
954 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
955 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
957 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
959 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
963 /* Update now playing */
964 free( p_pgrm->psz_now_playing );
965 p_pgrm->psz_now_playing = NULL;
966 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
967 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
969 if( p_pgrm == p_sys->p_pgrm )
970 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
972 if( p_pgrm->psz_now_playing )
974 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
975 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
976 p_pgrm->psz_now_playing );
980 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
981 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
990 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
992 es_out_sys_t *p_sys = out->p_sys;
993 input_thread_t *p_input = p_sys->p_input;
995 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
996 es_out_pgrm_t *p_pgrm = NULL;
999 if( !es ) return NULL;
1001 if( fmt->i_group < 0 )
1003 msg_Err( p_input, "invalid group number" );
1008 /* Search the program */
1009 for( i = 0; i < p_sys->i_pgrm; i++ )
1011 if( fmt->i_group == p_sys->pgrm[i]->i_id )
1013 p_pgrm = p_sys->pgrm[i];
1017 if( p_pgrm == NULL )
1019 /* Create a new one */
1020 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1023 /* Increase ref count for program */
1028 fmt->i_id = out->p_sys->i_id;
1029 es->i_id = fmt->i_id;
1030 es->p_pgrm = p_pgrm;
1031 es_format_Copy( &es->fmt, fmt );
1032 es->i_preroll_end = -1;
1034 switch( fmt->i_cat )
1038 audio_replay_gain_t rg;
1040 es->i_channel = p_sys->i_audio;
1042 memset( &rg, 0, sizeof(rg) );
1043 vlc_mutex_lock( &p_input->p->input.p_item->lock );
1044 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
1045 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1047 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1049 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1051 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1052 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1054 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1056 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1057 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1064 es->i_channel = p_sys->i_video;
1065 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
1066 vlc_ureduce( &es->fmt.video.i_frame_rate,
1067 &es->fmt.video.i_frame_rate_base,
1068 fmt->video.i_frame_rate,
1069 fmt->video.i_frame_rate_base, 0 );
1073 es->i_channel = p_sys->i_sub;
1080 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
1081 es->psz_language_code = LanguageGetCode( fmt->psz_language );
1083 es->p_dec_record = NULL;
1084 for( i = 0; i < 4; i++ )
1085 es->pb_cc_present[i] = false;
1086 es->p_master = NULL;
1088 if( es->p_pgrm == p_sys->p_pgrm )
1089 EsOutESVarUpdate( out, es, false );
1091 /* Select it if needed */
1092 EsOutSelect( out, es, false );
1095 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1096 p_sys->i_id++; /* always incremented */
1097 switch( fmt->i_cat )
1110 EsOutAddInfo( out, es );
1115 static bool EsIsSelected( es_out_id_t *es )
1119 bool b_decode = false;
1120 if( es->p_master->p_dec )
1122 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1123 if( i_channel != -1 )
1124 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1130 return es->p_dec != NULL;
1133 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1135 es_out_sys_t *p_sys = out->p_sys;
1136 input_thread_t *p_input = p_sys->p_input;
1138 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_input->p->p_sout );
1139 if( p_es->p_dec && !p_es->p_master && p_sys->p_sout_record )
1140 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
1142 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1144 es_out_sys_t *p_sys = out->p_sys;
1149 input_DecoderDelete( p_es->p_dec );
1152 if( p_es->p_dec_record )
1154 input_DecoderDelete( p_es->p_dec_record );
1155 p_es->p_dec_record = NULL;
1159 static void EsSelect( es_out_t *out, es_out_id_t *es )
1161 es_out_sys_t *p_sys = out->p_sys;
1162 input_thread_t *p_input = p_sys->p_input;
1164 const char *psz_var;
1166 if( EsIsSelected( es ) )
1168 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1175 if( !es->p_master->p_dec )
1178 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1179 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1184 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1186 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1188 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1193 else if( es->fmt.i_cat == AUDIO_ES )
1195 var_Get( p_input, "audio", &val );
1196 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1198 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1203 if( es->fmt.i_cat == SPU_ES )
1205 var_Get( p_input, "spu", &val );
1206 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1208 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1214 es->i_preroll_end = -1;
1215 EsCreateDecoder( out, es );
1217 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1221 if( es->fmt.i_cat == VIDEO_ES )
1222 psz_var = "video-es";
1223 else if( es->fmt.i_cat == AUDIO_ES )
1224 psz_var = "audio-es";
1225 else if( es->fmt.i_cat == SPU_ES )
1230 /* Mark it as selected */
1231 val.i_int = es->i_id;
1232 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1234 var_SetBool( p_sys->p_input, "intf-change", true );
1237 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1239 es_out_sys_t *p_sys = out->p_sys;
1240 input_thread_t *p_input = p_sys->p_input;
1242 const char *psz_var;
1244 if( !EsIsSelected( es ) )
1246 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1252 if( es->p_master->p_dec )
1254 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1255 if( i_channel != -1 )
1256 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1261 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1263 for( i = 0; i < 4; i++ )
1265 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1268 if( i_spu_id == es->pp_cc_es[i]->i_id )
1270 /* Force unselection of the CC */
1272 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1274 var_SetBool( p_sys->p_input, "intf-change", true );
1276 EsOutDel( out, es->pp_cc_es[i] );
1278 es->pb_cc_present[i] = false;
1280 EsDestroyDecoder( out, es );
1287 if( es->p_dec == NULL )
1289 if( es->fmt.i_cat == VIDEO_ES )
1290 psz_var = "video-es";
1291 else if( es->fmt.i_cat == AUDIO_ES )
1292 psz_var = "audio-es";
1293 else if( es->fmt.i_cat == SPU_ES )
1298 /* Mark it as unselected */
1300 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1302 var_SetBool( p_sys->p_input, "intf-change", true );
1306 * Select an ES given the current mode
1307 * XXX: you need to take a the lock before (stream.stream_lock)
1309 * \param out The es_out structure
1310 * \param es es_out_id structure
1311 * \param b_force ...
1314 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1316 es_out_sys_t *p_sys = out->p_sys;
1318 int i_cat = es->fmt.i_cat;
1320 if( !p_sys->b_active ||
1321 ( !b_force && es->fmt.i_priority < 0 ) )
1326 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1328 if( !EsIsSelected( es ) )
1329 EsSelect( out, es );
1331 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1335 var_Get( p_sys->p_input, "programs", &val );
1336 for ( i = 0; i < val.p_list->i_count; i++ )
1338 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1340 if( !EsIsSelected( es ) )
1341 EsSelect( out, es );
1345 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1347 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1351 if( es->p_pgrm != p_sys->p_pgrm )
1354 if( i_cat == AUDIO_ES )
1356 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1357 es->psz_language_code );
1359 if( p_sys->p_es_audio &&
1360 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1362 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1363 p_sys->p_es_audio->psz_language_code );
1365 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1367 i_wanted = es->i_channel;
1371 /* Select audio if (no audio selected yet)
1372 * - no audio-language
1373 * - no audio code for the ES
1374 * - audio code in the requested list */
1376 !strcmp( es->psz_language_code, "??" ) ||
1377 !p_sys->ppsz_audio_language )
1378 i_wanted = es->i_channel;
1381 if( p_sys->i_audio_last >= 0 )
1382 i_wanted = p_sys->i_audio_last;
1384 if( p_sys->i_audio_id >= 0 )
1386 if( es->i_id == p_sys->i_audio_id )
1387 i_wanted = es->i_channel;
1392 else if( i_cat == SPU_ES )
1394 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1395 es->psz_language_code );
1397 if( p_sys->p_es_sub &&
1398 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1400 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1401 p_sys->p_es_sub->psz_language_code );
1403 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1404 idx1, es->psz_language_code, idx2,
1405 p_sys->p_es_sub->psz_language_code );
1407 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1409 /* We found a SPU that matches our language request */
1410 i_wanted = es->i_channel;
1412 else if( idx1 >= 0 )
1414 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1415 idx1, es->psz_language_code );
1417 i_wanted = es->i_channel;
1419 else if( p_sys->i_default_sub_id >= 0 )
1421 if( es->i_id == p_sys->i_default_sub_id )
1422 i_wanted = es->i_channel;
1425 if( p_sys->i_sub_last >= 0 )
1426 i_wanted = p_sys->i_sub_last;
1428 if( p_sys->i_sub_id >= 0 )
1430 if( es->i_id == p_sys->i_sub_id )
1431 i_wanted = es->i_channel;
1436 else if( i_cat == VIDEO_ES )
1438 i_wanted = es->i_channel;
1441 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1442 EsSelect( out, es );
1445 /* FIXME TODO handle priority here */
1446 if( EsIsSelected( es ) )
1448 if( i_cat == AUDIO_ES )
1450 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1451 p_sys->p_es_audio &&
1452 p_sys->p_es_audio != es &&
1453 EsIsSelected( p_sys->p_es_audio ) )
1455 EsUnselect( out, p_sys->p_es_audio, false );
1457 p_sys->p_es_audio = es;
1459 else if( i_cat == SPU_ES )
1461 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1463 p_sys->p_es_sub != es &&
1464 EsIsSelected( p_sys->p_es_sub ) )
1466 EsUnselect( out, p_sys->p_es_sub, false );
1468 p_sys->p_es_sub = es;
1470 else if( i_cat == VIDEO_ES )
1472 p_sys->p_es_video = es;
1478 * Send a block for the given es_out
1480 * \param out the es_out to send from
1481 * \param es the es_out_id
1482 * \param p_block the data block to send
1484 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1486 es_out_sys_t *p_sys = out->p_sys;
1487 input_thread_t *p_input = p_sys->p_input;
1488 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1492 if( es->fmt.i_cat == AUDIO_ES )
1493 i_delay = p_sys->i_audio_delay;
1494 else if( es->fmt.i_cat == SPU_ES )
1495 i_delay = p_sys->i_spu_delay;
1499 if( libvlc_stats (p_input) )
1501 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1502 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1503 p_block->i_buffer, &i_total );
1504 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1505 (float)i_total, NULL );
1506 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1509 /* Mark preroll blocks */
1510 if( es->i_preroll_end >= 0 )
1512 int64_t i_date = p_block->i_pts;
1514 i_date = p_block->i_dts;
1516 if( i_date < es->i_preroll_end )
1517 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1519 es->i_preroll_end = -1;
1522 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1524 p_block->i_dts += i_delay;
1526 else if( p_block->i_dts > 0 )
1529 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1531 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1533 p_block->i_pts += i_delay;
1535 else if( p_block->i_pts > 0 )
1538 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1540 if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
1541 es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1543 mtime_t current_date = mdate();
1545 || p_block->i_pts > current_date + 10000000
1546 || current_date > p_block->i_pts )
1548 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1549 * for teletext streams. */
1550 p_block->i_pts = current_date + 400000
1551 + p_input->i_pts_delay + i_delay;
1555 p_block->i_rate = p_sys->i_rate;
1557 /* TODO handle mute */
1559 ( es->fmt.i_cat != AUDIO_ES ||
1560 ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1561 p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1564 bool b_cc_new = false;
1566 if( es->p_dec_record )
1568 block_t *p_dup = block_Duplicate( p_block );
1570 input_DecoderDecode( es->p_dec_record, p_dup );
1572 input_DecoderDecode( es->p_dec, p_block );
1574 /* Check CC status */
1575 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1576 for( i = 0; i < 4; i++ )
1578 static const vlc_fourcc_t fcc[4] = {
1579 VLC_FOURCC('c', 'c', '1', ' '),
1580 VLC_FOURCC('c', 'c', '2', ' '),
1581 VLC_FOURCC('c', 'c', '3', ' '),
1582 VLC_FOURCC('c', 'c', '4', ' '),
1584 static const char ppsz_description[4][18] = {
1585 N_("Closed captions 1"),
1586 N_("Closed captions 2"),
1587 N_("Closed captions 3"),
1588 N_("Closed captions 4"),
1592 if( es->pb_cc_present[i] || !pb_cc[i] )
1594 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1596 es_format_Init( &fmt, SPU_ES, fcc[i] );
1597 fmt.i_group = es->fmt.i_group;
1598 fmt.psz_description = strdup( _(ppsz_description[i] ) );
1599 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1600 es->pp_cc_es[i]->p_master = es;
1601 es_format_Clean( &fmt );
1604 es->pb_cc_present[i] = true;
1608 var_SetBool( p_sys->p_input, "intf-change", true );
1612 block_Release( p_block );
1618 /*****************************************************************************
1620 *****************************************************************************/
1621 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1623 es_out_sys_t *p_sys = out->p_sys;
1624 bool b_reselect = false;
1627 /* We don't try to reselect */
1630 while( !out->p_sys->p_input->b_die && es->p_dec )
1632 if( input_DecoderEmpty( es->p_dec ) &&
1633 ( !es->p_dec_record || input_DecoderEmpty( es->p_dec_record ) ))
1637 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1640 if( es->p_pgrm == p_sys->p_pgrm )
1641 EsOutESVarUpdate( out, es, true );
1643 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1646 if( es->p_pgrm->i_es == 0 )
1648 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1651 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1652 p_sys->p_es_sub == es ) b_reselect = true;
1654 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1655 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1656 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1658 switch( es->fmt.i_cat )
1671 /* Re-select another track when needed */
1673 for( i = 0; i < p_sys->i_es; i++ )
1675 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1676 EsOutSelect( out, p_sys->es[i], false );
1679 free( es->psz_language );
1680 free( es->psz_language_code );
1682 es_format_Clean( &es->fmt );
1688 * Control query handler
1690 * \param out the es_out to control
1691 * \param i_query A es_out query as defined in include/ninput.h
1692 * \param args a variable list of arguments for the query
1693 * \return VLC_SUCCESS or an error code
1695 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1697 es_out_sys_t *p_sys = out->p_sys;
1705 case ES_OUT_SET_ES_STATE:
1706 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1707 b = (bool) va_arg( args, int );
1708 if( b && !EsIsSelected( es ) )
1710 EsSelect( out, es );
1711 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1713 else if( !b && EsIsSelected( es ) )
1715 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1720 case ES_OUT_GET_ES_STATE:
1721 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1722 pb = (bool*) va_arg( args, bool * );
1724 *pb = EsIsSelected( es );
1727 case ES_OUT_SET_ACTIVE:
1729 b = (bool) va_arg( args, int );
1730 p_sys->b_active = b;
1733 var_SetBool( p_sys->p_input, "intf-change", true );
1737 case ES_OUT_GET_ACTIVE:
1738 pb = (bool*) va_arg( args, bool * );
1739 *pb = p_sys->b_active;
1742 case ES_OUT_SET_MODE:
1743 i = (int) va_arg( args, int );
1744 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1745 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1749 /* Reapply policy mode */
1750 for( i = 0; i < p_sys->i_es; i++ )
1752 if( EsIsSelected( p_sys->es[i] ) )
1754 EsUnselect( out, p_sys->es[i],
1755 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1758 for( i = 0; i < p_sys->i_es; i++ )
1760 EsOutSelect( out, p_sys->es[i], false );
1764 return VLC_EGENERIC;
1766 case ES_OUT_GET_MODE:
1767 pi = (int*) va_arg( args, int* );
1768 *pi = p_sys->i_mode;
1772 case ES_OUT_RESTART_ES:
1776 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1780 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1782 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1784 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1789 for( i = 0; i < p_sys->i_es; i++ )
1793 if( es == p_sys->es[i] )
1795 EsOutSelect( out, es, true );
1801 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
1803 if( EsIsSelected( p_sys->es[i] ) )
1805 if( i_query == ES_OUT_RESTART_ES )
1807 if( p_sys->es[i]->p_dec )
1809 EsDestroyDecoder( out, p_sys->es[i] );
1810 EsCreateDecoder( out, p_sys->es[i] );
1815 EsUnselect( out, p_sys->es[i],
1816 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1822 if( i_query == ES_OUT_SET_ES )
1825 event.type = vlc_InputSelectedStreamChanged;
1826 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1831 case ES_OUT_SET_DEFAULT:
1833 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1837 /*p_sys->i_default_video_id = -1;*/
1838 /*p_sys->i_default_audio_id = -1;*/
1839 p_sys->i_default_sub_id = -1;
1841 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1843 /*p_sys->i_default_video_id = -1;*/
1845 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1847 /*p_sys->i_default_audio_id = -1;*/
1849 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1851 p_sys->i_default_sub_id = -1;
1855 /*if( es->fmt.i_cat == VIDEO_ES )
1856 p_sys->i_default_video_id = es->i_id;
1858 if( es->fmt.i_cat == AUDIO_ES )
1859 p_sys->i_default_audio_id = es->i_id;
1861 if( es->fmt.i_cat == SPU_ES )
1862 p_sys->i_default_sub_id = es->i_id;
1867 case ES_OUT_SET_PCR:
1868 case ES_OUT_SET_GROUP_PCR:
1870 es_out_pgrm_t *p_pgrm = NULL;
1874 if( i_query == ES_OUT_SET_PCR )
1876 p_pgrm = p_sys->p_pgrm;
1881 i_group = (int)va_arg( args, int );
1882 for( i = 0; i < p_sys->i_pgrm; i++ )
1884 if( p_sys->pgrm[i]->i_id == i_group )
1886 p_pgrm = p_sys->pgrm[i];
1891 if( p_pgrm == NULL )
1892 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1894 i_pcr = (int64_t)va_arg( args, int64_t );
1895 /* search program */
1896 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1900 case ES_OUT_RESET_PCR:
1901 for( i = 0; i < p_sys->i_pgrm; i++ )
1902 input_ClockResetPCR( &p_sys->pgrm[i]->clock );
1908 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1909 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1910 *pi_ts = input_ClockGetTS( p_sys->p_input,
1911 &p_sys->p_pgrm->clock, i_ts );
1914 return VLC_EGENERIC;
1916 case ES_OUT_GET_GROUP:
1917 pi = (int*) va_arg( args, int* );
1919 *pi = p_sys->p_pgrm->i_id;
1921 *pi = -1; /* FIXME */
1924 case ES_OUT_SET_GROUP:
1927 i = (int) va_arg( args, int );
1928 for( j = 0; j < p_sys->i_pgrm; j++ )
1930 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1931 if( p_pgrm->i_id == i )
1933 EsOutProgramSelect( out, p_pgrm );
1937 return VLC_EGENERIC;
1940 case ES_OUT_SET_FMT:
1942 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1943 * to update the p_extra data */
1945 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1946 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1948 return VLC_EGENERIC;
1950 if( p_fmt->i_extra )
1952 es->fmt.i_extra = p_fmt->i_extra;
1953 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1954 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1959 EsDestroyDecoder( out, es );
1961 EsCreateDecoder( out, es );
1963 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1964 es->p_dec->fmt_in.p_extra =
1965 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1966 memcpy( es->p_dec->fmt_in.p_extra,
1967 p_fmt->p_extra, p_fmt->i_extra );
1974 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1978 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1979 i_date = (int64_t)va_arg( args, int64_t );
1981 if( !es || !es->p_dec )
1982 return VLC_EGENERIC;
1984 /* XXX We should call input_ClockGetTS but PCR has been reseted
1985 * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1986 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1987 es->i_preroll_end = i_date;
1991 case ES_OUT_SET_GROUP_META:
1993 int i_group = (int)va_arg( args, int );
1994 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1996 EsOutProgramMeta( out, i_group, p_meta );
1999 case ES_OUT_SET_GROUP_EPG:
2001 int i_group = (int)va_arg( args, int );
2002 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2004 EsOutProgramEpg( out, i_group, p_epg );
2007 case ES_OUT_DEL_GROUP:
2009 int i_group = (int)va_arg( args, int );
2011 return EsOutProgramDel( out, i_group );
2015 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2016 return VLC_EGENERIC;
2020 /****************************************************************************
2021 * LanguageGetName: try to expend iso639 into plain name
2022 ****************************************************************************/
2023 static char *LanguageGetName( const char *psz_code )
2025 const iso639_lang_t *pl;
2027 if( psz_code == NULL )
2029 return strdup( "" );
2032 if( strlen( psz_code ) == 2 )
2034 pl = GetLang_1( psz_code );
2036 else if( strlen( psz_code ) == 3 )
2038 pl = GetLang_2B( psz_code );
2039 if( !strcmp( pl->psz_iso639_1, "??" ) )
2041 pl = GetLang_2T( psz_code );
2046 return strdup( psz_code );
2049 if( !strcmp( pl->psz_iso639_1, "??" ) )
2051 return strdup( psz_code );
2055 if( *pl->psz_native_name )
2057 return strdup( pl->psz_native_name );
2059 return strdup( pl->psz_eng_name );
2063 /* Get a 2 char code */
2064 static char *LanguageGetCode( const char *psz_lang )
2066 const iso639_lang_t *pl;
2068 if( psz_lang == NULL || *psz_lang == '\0' )
2069 return strdup("??");
2071 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2073 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2074 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2075 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2076 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2077 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2081 if( pl->psz_iso639_1 != NULL )
2082 return strdup( pl->psz_iso639_1 );
2084 return strdup("??");
2087 static char **LanguageSplit( const char *psz_langs )
2094 if( psz_langs == NULL ) return NULL;
2096 psz_parser = psz_dup = strdup(psz_langs);
2098 while( psz_parser && *psz_parser )
2103 psz = strchr(psz_parser, ',' );
2104 if( psz ) *psz++ = '\0';
2106 if( !strcmp( psz_parser, "any" ) )
2108 TAB_APPEND( i_psz, ppsz, strdup("any") );
2112 psz_code = LanguageGetCode( psz_parser );
2113 if( strcmp( psz_code, "??" ) )
2115 TAB_APPEND( i_psz, ppsz, psz_code );
2128 TAB_APPEND( i_psz, ppsz, NULL );
2135 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2139 if( !ppsz_langs || !psz_lang ) return -1;
2141 for( i = 0; ppsz_langs[i]; i++ )
2143 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2144 !strcasecmp( ppsz_langs[i], "any" ) )
2153 /****************************************************************************
2155 * - add meta info to the playlist item
2156 ****************************************************************************/
2157 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2159 es_out_sys_t *p_sys = out->p_sys;
2160 input_thread_t *p_input = p_sys->p_input;
2161 es_format_t *fmt = &es->fmt;
2165 /* Add stream info */
2166 if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
2169 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2170 "%.4s", (char*)&fmt->i_codec );
2172 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2173 "%s", es->psz_language );
2175 /* Add information */
2176 switch( fmt->i_cat )
2179 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2180 _("Type"), _("Audio") );
2182 if( fmt->audio.i_channels > 0 )
2183 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2184 "%u", fmt->audio.i_channels );
2186 if( fmt->audio.i_rate > 0 )
2188 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2189 _("%u Hz"), fmt->audio.i_rate );
2190 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2193 if( fmt->audio.i_bitspersample > 0 )
2194 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2195 _("Bits per sample"), "%u",
2196 fmt->audio.i_bitspersample );
2198 if( fmt->i_bitrate > 0 )
2200 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2201 _("%u kb/s"), fmt->i_bitrate / 1000 );
2202 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2207 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2208 _("Type"), _("Video") );
2210 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2211 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2212 _("Resolution"), "%ux%u",
2213 fmt->video.i_width, fmt->video.i_height );
2215 if( fmt->video.i_visible_width > 0 &&
2216 fmt->video.i_visible_height > 0 )
2217 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2218 _("Display resolution"), "%ux%u",
2219 fmt->video.i_visible_width,
2220 fmt->video.i_visible_height);
2221 if( fmt->video.i_frame_rate > 0 &&
2222 fmt->video.i_frame_rate_base > 0 )
2224 div = lldiv( (float)fmt->video.i_frame_rate /
2225 fmt->video.i_frame_rate_base * 1000000,
2227 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2228 _("Frame rate"), "%"PRId64".%06u",
2229 div.quot, (unsigned int )div.rem );
2234 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2235 _("Type"), _("Subtitle") );