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"
42 #include "input_clock.h"
43 #include "input_decoder.h"
45 #include "../stream_output/stream_output.h"
47 #include <vlc_iso_lang.h>
48 /* FIXME we should find a better way than including that */
49 #include "../text/iso-639_def.h"
51 /*****************************************************************************
53 *****************************************************************************/
59 /* Number of es for this pgrm */
64 /* Clock for this program */
65 input_clock_t *p_clock;
68 char *psz_now_playing;
78 es_out_pgrm_t *p_pgrm;
81 int64_t i_preroll_end;
83 /* Channel in the track type */
87 char *psz_language_code;
90 decoder_t *p_dec_record;
92 /* Fields for Video with CC */
93 bool pb_cc_present[4];
94 es_out_id_t *pp_cc_es[4];
96 /* Field for CC track from a master video */
97 es_out_id_t *p_master;
102 input_thread_t *p_input;
106 es_out_pgrm_t **pgrm;
107 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
108 es_out_pgrm_t *p_pgrm; /* Master program */
125 int i_audio_last, i_audio_id;
126 int i_sub_last, i_sub_id;
127 int i_default_sub_id; /* As specified in container; if applicable */
128 char **ppsz_audio_language;
129 char **ppsz_sub_language;
131 /* current main es */
132 es_out_id_t *p_es_audio;
133 es_out_id_t *p_es_video;
134 es_out_id_t *p_es_sub;
137 int64_t i_audio_delay;
140 /* Rate used for clock */
144 sout_instance_t *p_sout_record;
147 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
148 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
149 static void EsOutDel ( es_out_t *, es_out_id_t * );
150 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
151 static int EsOutControl( es_out_t *, int i_query, va_list );
153 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
155 static bool EsIsSelected( es_out_id_t *es );
156 static void EsSelect( es_out_t *out, es_out_id_t *es );
157 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
158 static char *LanguageGetName( const char *psz_code );
159 static char *LanguageGetCode( const char *psz_lang );
160 static char **LanguageSplit( const char *psz_langs );
161 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
163 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
165 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
166 VLC_FOURCC('c', 'c', '1', ' '),
167 VLC_FOURCC('c', 'c', '2', ' '),
168 VLC_FOURCC('c', 'c', '3', ' '),
169 VLC_FOURCC('c', 'c', '4', ' '),
171 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
174 for( i = 0; i < 4; i++ )
176 if( fcc == EsOutFourccClosedCaptions[i] )
183 /*****************************************************************************
185 *****************************************************************************/
186 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
191 es_out_t *out = malloc( sizeof( es_out_t ) );
192 if( !out ) return NULL;
194 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
201 out->pf_add = EsOutAdd;
202 out->pf_send = EsOutSend;
203 out->pf_del = EsOutDel;
204 out->pf_control = EsOutControl;
206 out->b_sout = p_input->p->p_sout != NULL;
208 p_sys->p_input = p_input;
210 p_sys->b_active = false;
211 p_sys->i_mode = ES_OUT_MODE_AUTO;
214 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
215 p_sys->p_pgrm = NULL;
219 TAB_INIT( p_sys->i_es, p_sys->es );
226 var_Get( p_input, "audio-track", &val );
227 p_sys->i_audio_last = val.i_int;
229 var_Get( p_input, "sub-track", &val );
230 p_sys->i_sub_last = val.i_int;
232 p_sys->i_default_sub_id = -1;
234 if( !p_input->b_preparsing )
236 var_Get( p_input, "audio-language", &val );
237 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
238 if( p_sys->ppsz_audio_language )
240 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
241 msg_Dbg( p_input, "selected audio language[%d] %s",
242 i, p_sys->ppsz_audio_language[i] );
244 free( val.psz_string );
246 var_Get( p_input, "sub-language", &val );
247 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
248 if( p_sys->ppsz_sub_language )
250 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
251 msg_Dbg( p_input, "selected subtitle language[%d] %s",
252 i, p_sys->ppsz_sub_language[i] );
254 free( val.psz_string );
258 p_sys->ppsz_sub_language = NULL;
259 p_sys->ppsz_audio_language = NULL;
262 var_Get( p_input, "audio-track-id", &val );
263 p_sys->i_audio_id = val.i_int;
265 var_Get( p_input, "sub-track-id", &val );
266 p_sys->i_sub_id = val.i_int;
268 p_sys->p_es_audio = NULL;
269 p_sys->p_es_video = NULL;
270 p_sys->p_es_sub = NULL;
272 p_sys->i_audio_delay= 0;
273 p_sys->i_spu_delay = 0;
275 p_sys->i_rate = i_rate;
277 p_sys->p_sout_record = NULL;
282 /*****************************************************************************
284 *****************************************************************************/
285 void input_EsOutDelete( es_out_t *out )
287 es_out_sys_t *p_sys = out->p_sys;
290 if( p_sys->p_sout_record )
291 input_EsOutSetRecord( out, false );
293 for( i = 0; i < p_sys->i_es; i++ )
295 if( p_sys->es[i]->p_dec )
296 input_DecoderDelete( p_sys->es[i]->p_dec );
298 free( p_sys->es[i]->psz_language );
299 free( p_sys->es[i]->psz_language_code );
300 es_format_Clean( &p_sys->es[i]->fmt );
302 free( p_sys->es[i] );
304 if( p_sys->ppsz_audio_language )
306 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
307 free( p_sys->ppsz_audio_language[i] );
308 free( p_sys->ppsz_audio_language );
310 if( p_sys->ppsz_sub_language )
312 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
313 free( p_sys->ppsz_sub_language[i] );
314 free( p_sys->ppsz_sub_language );
318 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
319 for( i = 0; i < p_sys->i_pgrm; i++ )
321 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
322 input_clock_Delete( p_pgrm->p_clock );
323 free( p_pgrm->psz_now_playing );
324 free( p_pgrm->psz_publisher );
325 free( p_pgrm->psz_name );
327 vlc_epg_Delete( p_pgrm->p_epg );
331 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
337 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
342 /* Special HACK, -i_id is tha cat of the stream */
343 return (es_out_id_t*)((uint8_t*)NULL-i_id);
346 for( i = 0; i < out->p_sys->i_es; i++ )
348 if( out->p_sys->es[i]->i_id == i_id )
349 return out->p_sys->es[i];
354 mtime_t input_EsOutGetWakeup( es_out_t *out )
356 es_out_sys_t *p_sys = out->p_sys;
357 input_thread_t *p_input = p_sys->p_input;
362 /* We do not have a wake up date if the input cannot have its speed
363 * controlled or sout is imposing its own */
364 if( !p_input->b_can_pace_control || p_input->p->b_out_pace_control )
367 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
370 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
372 es_out_sys_t *p_sys = out->p_sys;
375 for( i = 0; i < p_sys->i_es; i++ )
377 es_out_id_t *es = p_sys->es[i];
379 /* Send a dummy block to let decoder know that
380 * there is a discontinuity */
381 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
383 input_DecoderDiscontinuity( es->p_dec, b_flush );
384 if( es->p_dec_record )
385 input_DecoderDiscontinuity( es->p_dec_record, b_flush );
389 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
391 es_out_sys_t *p_sys = out->p_sys;
393 /* Pause decoders first */
394 for( int i = 0; i < p_sys->i_es; i++ )
396 es_out_id_t *es = p_sys->es[i];
398 /* Send a dummy block to let decoder know that
399 * there is a discontinuity */
402 input_DecoderChangePause( es->p_dec, b_paused, i_date );
403 if( es->p_dec_record )
404 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
408 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
410 es_out_sys_t *p_sys = out->p_sys;
412 for( int i = 0; i < p_sys->i_pgrm; i++ )
413 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
416 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
418 es_out_sys_t *p_sys = out->p_sys;
421 if( p_es->fmt.i_cat == AUDIO_ES )
422 i_delay = p_sys->i_audio_delay;
423 else if( p_es->fmt.i_cat == SPU_ES )
424 i_delay = p_sys->i_spu_delay;
429 input_DecoderChangeDelay( p_es->p_dec, i_delay );
430 if( p_es->p_dec_record )
431 input_DecoderChangeDelay( p_es->p_dec, i_delay );
435 void input_EsOutChangeRate( es_out_t *out, int i_rate )
437 es_out_sys_t *p_sys = out->p_sys;
440 p_sys->i_rate = i_rate;
442 for( i = 0; i < p_sys->i_pgrm; i++ )
443 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, i_rate );
446 int input_EsOutSetRecord( es_out_t *out, bool b_record )
448 es_out_sys_t *p_sys = out->p_sys;
449 input_thread_t *p_input = p_sys->p_input;
451 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
455 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
456 if( !psz_path || *psz_path == '\0' )
459 psz_path = strdup( config_GetHomeDir() );
462 char *psz_sout = NULL; // TODO conf
464 if( !psz_sout && psz_path )
466 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
469 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
480 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
484 if( !p_sys->p_sout_record )
487 for( int i = 0; i < p_sys->i_es; i++ )
489 es_out_id_t *p_es = p_sys->es[i];
491 if( !p_es->p_dec || p_es->p_master )
494 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
499 for( int i = 0; i < p_sys->i_es; i++ )
501 es_out_id_t *p_es = p_sys->es[i];
503 if( !p_es->p_dec_record )
506 input_DecoderDelete( p_es->p_dec_record );
507 p_es->p_dec_record = NULL;
510 sout_DeleteInstance( p_sys->p_sout_record );
512 p_sys->p_sout_record = NULL;
517 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
519 es_out_sys_t *p_sys = out->p_sys;
521 if( i_cat == AUDIO_ES )
522 p_sys->i_audio_delay = i_delay;
523 else if( i_cat == AUDIO_ES )
524 p_sys->i_spu_delay = i_delay;
526 for( int i = 0; i < p_sys->i_es; i++ )
527 EsOutDecoderChangeDelay( out, p_sys->es[i] );
529 void input_EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
531 /* XXX the order is important */
534 EsOutDecodersChangePause( out, true, i_date );
535 EsOutProgramChangePause( out, true, i_date );
539 EsOutProgramChangePause( out, false, i_date );
540 EsOutDecodersChangePause( out, false, i_date );
543 void input_EsOutChangePosition( es_out_t *out )
545 //es_out_sys_t *p_sys = out->p_sys;
547 es_out_Control( out, ES_OUT_RESET_PCR );
548 EsOutDiscontinuity( out, true, false );
551 bool input_EsOutDecodersEmpty( es_out_t *out )
553 es_out_sys_t *p_sys = out->p_sys;
556 for( i = 0; i < p_sys->i_es; i++ )
558 es_out_id_t *es = p_sys->es[i];
560 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
562 if( es->p_dec_record && !input_DecoderEmpty( es->p_dec_record ) )
568 /*****************************************************************************
570 *****************************************************************************/
571 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
574 es_out_sys_t *p_sys = out->p_sys;
575 input_thread_t *p_input = p_sys->p_input;
576 const bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
577 vlc_value_t val, text;
581 if( fmt->i_cat == AUDIO_ES )
582 psz_var = "audio-es";
583 else if( fmt->i_cat == VIDEO_ES )
584 psz_var = "video-es";
585 else if( fmt->i_cat == SPU_ES )
593 var_SetInteger( p_sys->p_input, "teletext-es", -1 );
596 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
598 var_SetBool( p_sys->p_input, "intf-change", true );
602 /* Get the number of ES already added */
603 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
608 /* First one, we need to add the "Disable" choice */
609 val2.i_int = -1; text.psz_string = _("Disable");
610 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
614 /* Take care of the ES description */
615 if( fmt->psz_description && *fmt->psz_description )
617 if( psz_language && *psz_language )
619 text.psz_string = malloc( strlen( fmt->psz_description) +
620 strlen( psz_language ) + 10 );
621 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
624 else text.psz_string = strdup( fmt->psz_description );
628 if( psz_language && *psz_language )
630 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
631 text.psz_string = NULL;
635 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
636 text.psz_string = NULL;
641 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
643 free( text.psz_string );
647 if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
648 var_SetInteger( p_sys->p_input, "teletext-es", i_id );
651 var_SetBool( p_sys->p_input, "intf-change", true );
654 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
657 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
660 /* EsOutProgramSelect:
661 * Select a program and update the object variable
663 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
665 es_out_sys_t *p_sys = out->p_sys;
666 input_thread_t *p_input = p_sys->p_input;
670 if( p_sys->p_pgrm == p_pgrm )
671 return; /* Nothing to do */
675 es_out_pgrm_t *old = p_sys->p_pgrm;
676 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
678 for( i = 0; i < p_sys->i_es; i++ )
680 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
681 p_sys->i_mode != ES_OUT_MODE_ALL )
682 EsUnselect( out, p_sys->es[i], true );
685 p_sys->p_es_audio = NULL;
686 p_sys->p_es_sub = NULL;
687 p_sys->p_es_video = NULL;
690 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
692 /* Mark it selected */
693 p_pgrm->b_selected = true;
695 /* Switch master stream */
696 p_sys->p_pgrm = p_pgrm;
698 /* Update "program" */
699 val.i_int = p_pgrm->i_id;
700 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
703 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
704 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
705 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
706 var_SetInteger( p_input, "teletext-es", -1 );
707 for( i = 0; i < p_sys->i_es; i++ )
709 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
710 EsOutESVarUpdate( out, p_sys->es[i], false );
711 EsOutSelect( out, p_sys->es[i], false );
714 /* Update now playing */
715 input_item_SetNowPlaying( p_input->p->input.p_item,
716 p_pgrm->psz_now_playing );
717 input_item_SetPublisher( p_input->p->input.p_item,
718 p_pgrm->psz_publisher );
720 var_SetBool( p_sys->p_input, "intf-change", true );
726 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
728 es_out_sys_t *p_sys = out->p_sys;
729 input_thread_t *p_input = p_sys->p_input;
732 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
737 p_pgrm->i_id = i_group;
739 p_pgrm->b_selected = false;
740 p_pgrm->psz_name = NULL;
741 p_pgrm->psz_now_playing = NULL;
742 p_pgrm->psz_publisher = NULL;
743 p_pgrm->p_epg = NULL;
744 p_pgrm->p_clock = input_clock_New( p_input->p->input.i_cr_average, p_sys->i_rate );
745 if( !p_pgrm->p_clock )
752 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
754 /* Update "program" variable */
756 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
758 if( i_group == var_GetInteger( p_input, "program" ) )
760 EsOutProgramSelect( out, p_pgrm );
764 var_SetBool( p_sys->p_input, "intf-change", true );
772 static int EsOutProgramDel( es_out_t *out, int i_group )
774 es_out_sys_t *p_sys = out->p_sys;
775 input_thread_t *p_input = p_sys->p_input;
776 es_out_pgrm_t *p_pgrm = NULL;
780 for( i = 0; i < p_sys->i_pgrm; i++ )
782 if( p_sys->pgrm[i]->i_id == i_group )
784 p_pgrm = p_sys->pgrm[i];
794 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
795 i_group, p_pgrm->i_es );
799 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
801 /* If program is selected we need to unselect it */
802 if( p_sys->p_pgrm == p_pgrm )
803 p_sys->p_pgrm = NULL;
805 input_clock_Delete( p_pgrm->p_clock );
807 free( p_pgrm->psz_name );
808 free( p_pgrm->psz_now_playing );
809 free( p_pgrm->psz_publisher );
811 vlc_epg_Delete( p_pgrm->p_epg );
814 /* Update "program" variable */
816 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
818 var_SetBool( p_sys->p_input, "intf-change", true );
825 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
828 if( p_pgrm->psz_name )
830 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
835 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
841 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
843 es_out_sys_t *p_sys = out->p_sys;
844 es_out_pgrm_t *p_pgrm = NULL;
845 input_thread_t *p_input = p_sys->p_input;
847 const char *psz_title = NULL;
848 const char *psz_provider = NULL;
851 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
853 /* Check against empty meta data (empty for what we handle) */
854 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
855 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
856 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
857 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
862 for( i = 0; i < p_sys->i_pgrm; i++ )
864 if( p_sys->pgrm[i]->i_id == i_group )
866 p_pgrm = p_sys->pgrm[i];
871 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
874 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
875 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
877 /* Update the description text of the program */
878 if( psz_title && *psz_title )
883 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
885 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
887 /* Remove old entries */
888 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
889 /* TODO update epg name */
892 free( p_pgrm->psz_name );
893 p_pgrm->psz_name = strdup( psz_title );
895 /* ugly but it works */
897 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
899 if( psz_provider && *psz_provider )
901 if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
903 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
904 free( text.psz_string );
909 text.psz_string = (char *)psz_title;
910 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
914 psz_cat = EsOutProgramGetMetaName( p_pgrm );
917 if( p_sys->p_pgrm == p_pgrm )
918 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
919 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
921 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
922 for( i = 0; ppsz_all_keys[i]; i++ )
924 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
925 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
926 free( ppsz_all_keys[i] );
928 free( ppsz_all_keys );
933 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
938 for( i = 0; i < p_src->i_event; i++ )
940 vlc_epg_event_t *p_evt = p_src->pp_event[i];
944 for( j = 0; j < p_dst->i_event; j++ )
946 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
951 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
956 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
959 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
960 p_copy->i_start = p_evt->i_start;
961 p_copy->i_duration = p_evt->i_duration;
962 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
963 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
964 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
965 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
969 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
971 /* Keep only 1 old event */
972 if( p_dst->p_current )
974 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
975 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
979 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
981 es_out_sys_t *p_sys = out->p_sys;
982 input_thread_t *p_input = p_sys->p_input;
983 es_out_pgrm_t *p_pgrm = NULL;
988 for( i = 0; i < p_sys->i_pgrm; i++ )
990 if( p_sys->pgrm[i]->i_id == i_group )
992 p_pgrm = p_sys->pgrm[i];
997 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1000 if( !p_pgrm->p_epg )
1001 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1002 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1005 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1006 #ifdef HAVE_LOCALTIME_R
1008 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
1010 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
1011 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1012 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
1014 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
1015 time_t t_start = (time_t)p_evt->i_start;
1017 char psz_start[128];
1019 localtime_r( &t_start, &tm_start );
1021 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
1022 if( p_evt->psz_short_description || p_evt->psz_description )
1023 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
1025 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
1026 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
1028 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
1030 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
1034 /* Update now playing */
1035 free( p_pgrm->psz_now_playing );
1036 p_pgrm->psz_now_playing = NULL;
1037 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
1038 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
1040 if( p_pgrm == p_sys->p_pgrm )
1041 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
1043 if( p_pgrm->psz_now_playing )
1045 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1046 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1047 p_pgrm->psz_now_playing );
1051 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1052 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1061 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
1063 es_out_sys_t *p_sys = out->p_sys;
1064 input_thread_t *p_input = p_sys->p_input;
1066 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
1067 es_out_pgrm_t *p_pgrm = NULL;
1070 if( !es ) return NULL;
1072 if( fmt->i_group < 0 )
1074 msg_Err( p_input, "invalid group number" );
1079 /* Search the program */
1080 for( i = 0; i < p_sys->i_pgrm; i++ )
1082 if( fmt->i_group == p_sys->pgrm[i]->i_id )
1084 p_pgrm = p_sys->pgrm[i];
1088 if( p_pgrm == NULL )
1090 /* Create a new one */
1091 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1094 /* Increase ref count for program */
1099 fmt->i_id = out->p_sys->i_id;
1100 es->i_id = fmt->i_id;
1101 es->p_pgrm = p_pgrm;
1102 es_format_Copy( &es->fmt, fmt );
1103 es->i_preroll_end = -1;
1105 switch( fmt->i_cat )
1109 audio_replay_gain_t rg;
1111 es->i_channel = p_sys->i_audio;
1113 memset( &rg, 0, sizeof(rg) );
1114 vlc_mutex_lock( &p_input->p->input.p_item->lock );
1115 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
1116 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1118 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1120 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1122 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1123 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1125 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1127 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1128 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1135 es->i_channel = p_sys->i_video;
1136 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
1137 vlc_ureduce( &es->fmt.video.i_frame_rate,
1138 &es->fmt.video.i_frame_rate_base,
1139 fmt->video.i_frame_rate,
1140 fmt->video.i_frame_rate_base, 0 );
1144 es->i_channel = p_sys->i_sub;
1151 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
1152 es->psz_language_code = LanguageGetCode( fmt->psz_language );
1154 es->p_dec_record = NULL;
1155 for( i = 0; i < 4; i++ )
1156 es->pb_cc_present[i] = false;
1157 es->p_master = NULL;
1159 if( es->p_pgrm == p_sys->p_pgrm )
1160 EsOutESVarUpdate( out, es, false );
1162 /* Select it if needed */
1163 EsOutSelect( out, es, false );
1166 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1167 p_sys->i_id++; /* always incremented */
1168 switch( fmt->i_cat )
1181 EsOutAddInfo( out, es );
1186 static bool EsIsSelected( es_out_id_t *es )
1190 bool b_decode = false;
1191 if( es->p_master->p_dec )
1193 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1194 if( i_channel != -1 )
1195 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1201 return es->p_dec != NULL;
1204 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1206 es_out_sys_t *p_sys = out->p_sys;
1207 input_thread_t *p_input = p_sys->p_input;
1209 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1210 if( p_es->p_dec && !p_es->p_master && p_sys->p_sout_record )
1211 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1213 EsOutDecoderChangeDelay( out, p_es );
1215 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1222 input_DecoderDelete( p_es->p_dec );
1225 if( p_es->p_dec_record )
1227 input_DecoderDelete( p_es->p_dec_record );
1228 p_es->p_dec_record = NULL;
1232 static void EsSelect( es_out_t *out, es_out_id_t *es )
1234 es_out_sys_t *p_sys = out->p_sys;
1235 input_thread_t *p_input = p_sys->p_input;
1237 const char *psz_var;
1239 if( EsIsSelected( es ) )
1241 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1248 if( !es->p_master->p_dec )
1251 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1252 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1257 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1259 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1261 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1266 else if( es->fmt.i_cat == AUDIO_ES )
1268 var_Get( p_input, "audio", &val );
1269 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1271 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1276 if( es->fmt.i_cat == SPU_ES )
1278 var_Get( p_input, "spu", &val );
1279 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1281 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1287 es->i_preroll_end = -1;
1288 EsCreateDecoder( out, es );
1290 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1294 if( es->fmt.i_cat == VIDEO_ES )
1295 psz_var = "video-es";
1296 else if( es->fmt.i_cat == AUDIO_ES )
1297 psz_var = "audio-es";
1298 else if( es->fmt.i_cat == SPU_ES )
1303 /* Mark it as selected */
1304 val.i_int = es->i_id;
1305 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1307 var_SetBool( p_sys->p_input, "intf-change", true );
1310 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1312 es_out_sys_t *p_sys = out->p_sys;
1313 input_thread_t *p_input = p_sys->p_input;
1315 const char *psz_var;
1317 if( !EsIsSelected( es ) )
1319 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1325 if( es->p_master->p_dec )
1327 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1328 if( i_channel != -1 )
1329 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1334 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1336 for( i = 0; i < 4; i++ )
1338 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1341 if( i_spu_id == es->pp_cc_es[i]->i_id )
1343 /* Force unselection of the CC */
1345 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1347 var_SetBool( p_sys->p_input, "intf-change", true );
1349 EsOutDel( out, es->pp_cc_es[i] );
1351 es->pb_cc_present[i] = false;
1353 EsDestroyDecoder( out, es );
1360 if( es->p_dec == NULL )
1362 if( es->fmt.i_cat == VIDEO_ES )
1363 psz_var = "video-es";
1364 else if( es->fmt.i_cat == AUDIO_ES )
1365 psz_var = "audio-es";
1366 else if( es->fmt.i_cat == SPU_ES )
1371 /* Mark it as unselected */
1373 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1375 var_SetBool( p_sys->p_input, "intf-change", true );
1379 * Select an ES given the current mode
1380 * XXX: you need to take a the lock before (stream.stream_lock)
1382 * \param out The es_out structure
1383 * \param es es_out_id structure
1384 * \param b_force ...
1387 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1389 es_out_sys_t *p_sys = out->p_sys;
1391 int i_cat = es->fmt.i_cat;
1393 if( !p_sys->b_active ||
1394 ( !b_force && es->fmt.i_priority < 0 ) )
1399 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1401 if( !EsIsSelected( es ) )
1402 EsSelect( out, es );
1404 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1408 var_Get( p_sys->p_input, "programs", &val );
1409 for ( i = 0; i < val.p_list->i_count; i++ )
1411 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1413 if( !EsIsSelected( es ) )
1414 EsSelect( out, es );
1418 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1420 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1424 if( es->p_pgrm != p_sys->p_pgrm )
1427 if( i_cat == AUDIO_ES )
1429 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1430 es->psz_language_code );
1432 if( p_sys->p_es_audio &&
1433 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1435 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1436 p_sys->p_es_audio->psz_language_code );
1438 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1440 i_wanted = es->i_channel;
1444 /* Select audio if (no audio selected yet)
1445 * - no audio-language
1446 * - no audio code for the ES
1447 * - audio code in the requested list */
1449 !strcmp( es->psz_language_code, "??" ) ||
1450 !p_sys->ppsz_audio_language )
1451 i_wanted = es->i_channel;
1454 if( p_sys->i_audio_last >= 0 )
1455 i_wanted = p_sys->i_audio_last;
1457 if( p_sys->i_audio_id >= 0 )
1459 if( es->i_id == p_sys->i_audio_id )
1460 i_wanted = es->i_channel;
1465 else if( i_cat == SPU_ES )
1467 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1468 es->psz_language_code );
1470 if( p_sys->p_es_sub &&
1471 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1473 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1474 p_sys->p_es_sub->psz_language_code );
1476 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1477 idx1, es->psz_language_code, idx2,
1478 p_sys->p_es_sub->psz_language_code );
1480 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1482 /* We found a SPU that matches our language request */
1483 i_wanted = es->i_channel;
1485 else if( idx1 >= 0 )
1487 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1488 idx1, es->psz_language_code );
1490 i_wanted = es->i_channel;
1492 else if( p_sys->i_default_sub_id >= 0 )
1494 if( es->i_id == p_sys->i_default_sub_id )
1495 i_wanted = es->i_channel;
1498 if( p_sys->i_sub_last >= 0 )
1499 i_wanted = p_sys->i_sub_last;
1501 if( p_sys->i_sub_id >= 0 )
1503 if( es->i_id == p_sys->i_sub_id )
1504 i_wanted = es->i_channel;
1509 else if( i_cat == VIDEO_ES )
1511 i_wanted = es->i_channel;
1514 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1515 EsSelect( out, es );
1518 /* FIXME TODO handle priority here */
1519 if( EsIsSelected( es ) )
1521 if( i_cat == AUDIO_ES )
1523 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1524 p_sys->p_es_audio &&
1525 p_sys->p_es_audio != es &&
1526 EsIsSelected( p_sys->p_es_audio ) )
1528 EsUnselect( out, p_sys->p_es_audio, false );
1530 p_sys->p_es_audio = es;
1532 else if( i_cat == SPU_ES )
1534 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1536 p_sys->p_es_sub != es &&
1537 EsIsSelected( p_sys->p_es_sub ) )
1539 EsUnselect( out, p_sys->p_es_sub, false );
1541 p_sys->p_es_sub = es;
1543 else if( i_cat == VIDEO_ES )
1545 p_sys->p_es_video = es;
1551 * Send a block for the given es_out
1553 * \param out the es_out to send from
1554 * \param es the es_out_id
1555 * \param p_block the data block to send
1557 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1559 es_out_sys_t *p_sys = out->p_sys;
1560 input_thread_t *p_input = p_sys->p_input;
1561 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1564 if( libvlc_stats( p_input ) )
1566 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1567 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1568 p_block->i_buffer, &i_total );
1569 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1570 (float)i_total, NULL );
1571 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1574 /* Mark preroll blocks */
1575 if( es->i_preroll_end >= 0 )
1577 int64_t i_date = p_block->i_pts;
1579 i_date = p_block->i_dts;
1581 if( i_date < es->i_preroll_end )
1582 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1584 es->i_preroll_end = -1;
1587 p_block->i_rate = 0;
1591 block_Release( p_block );
1596 if( es->p_dec_record )
1598 block_t *p_dup = block_Duplicate( p_block );
1600 input_DecoderDecode( es->p_dec_record, p_dup );
1602 input_DecoderDecode( es->p_dec, p_block );
1604 /* Check CC status */
1606 bool b_cc_new = false;
1608 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1609 for( int i = 0; i < 4; i++ )
1611 static const vlc_fourcc_t fcc[4] = {
1612 VLC_FOURCC('c', 'c', '1', ' '),
1613 VLC_FOURCC('c', 'c', '2', ' '),
1614 VLC_FOURCC('c', 'c', '3', ' '),
1615 VLC_FOURCC('c', 'c', '4', ' '),
1619 if( es->pb_cc_present[i] || !pb_cc[i] )
1621 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1623 es_format_Init( &fmt, SPU_ES, fcc[i] );
1624 fmt.i_group = es->fmt.i_group;
1625 if( asprintf( &fmt.psz_description,
1626 _("Closed captions %u"), 1 + i ) == -1 )
1627 fmt.psz_description = NULL;
1628 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1629 es->pp_cc_es[i]->p_master = es;
1630 es_format_Clean( &fmt );
1633 es->pb_cc_present[i] = true;
1637 var_SetBool( p_sys->p_input, "intf-change", true );
1642 /*****************************************************************************
1644 *****************************************************************************/
1645 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1647 es_out_sys_t *p_sys = out->p_sys;
1648 bool b_reselect = false;
1651 /* We don't try to reselect */
1654 while( !out->p_sys->p_input->b_die && es->p_dec )
1656 if( input_DecoderEmpty( es->p_dec ) &&
1657 ( !es->p_dec_record || input_DecoderEmpty( es->p_dec_record ) ))
1661 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1664 if( es->p_pgrm == p_sys->p_pgrm )
1665 EsOutESVarUpdate( out, es, true );
1667 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1670 if( es->p_pgrm->i_es == 0 )
1672 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1675 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1676 p_sys->p_es_sub == es ) b_reselect = true;
1678 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1679 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1680 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1682 switch( es->fmt.i_cat )
1695 /* Re-select another track when needed */
1697 for( i = 0; i < p_sys->i_es; i++ )
1699 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1700 EsOutSelect( out, p_sys->es[i], false );
1703 free( es->psz_language );
1704 free( es->psz_language_code );
1706 es_format_Clean( &es->fmt );
1712 * Control query handler
1714 * \param out the es_out to control
1715 * \param i_query A es_out query as defined in include/ninput.h
1716 * \param args a variable list of arguments for the query
1717 * \return VLC_SUCCESS or an error code
1719 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1721 es_out_sys_t *p_sys = out->p_sys;
1729 case ES_OUT_SET_ES_STATE:
1730 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1731 b = (bool) va_arg( args, int );
1732 if( b && !EsIsSelected( es ) )
1734 EsSelect( out, es );
1735 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1737 else if( !b && EsIsSelected( es ) )
1739 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1744 case ES_OUT_GET_ES_STATE:
1745 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1746 pb = (bool*) va_arg( args, bool * );
1748 *pb = EsIsSelected( es );
1751 case ES_OUT_SET_ACTIVE:
1753 b = (bool) va_arg( args, int );
1754 p_sys->b_active = b;
1757 var_SetBool( p_sys->p_input, "intf-change", true );
1761 case ES_OUT_GET_ACTIVE:
1762 pb = (bool*) va_arg( args, bool * );
1763 *pb = p_sys->b_active;
1766 case ES_OUT_SET_MODE:
1767 i = (int) va_arg( args, int );
1768 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1769 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1773 /* Reapply policy mode */
1774 for( i = 0; i < p_sys->i_es; i++ )
1776 if( EsIsSelected( p_sys->es[i] ) )
1778 EsUnselect( out, p_sys->es[i],
1779 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1782 for( i = 0; i < p_sys->i_es; i++ )
1784 EsOutSelect( out, p_sys->es[i], false );
1788 return VLC_EGENERIC;
1790 case ES_OUT_GET_MODE:
1791 pi = (int*) va_arg( args, int* );
1792 *pi = p_sys->i_mode;
1796 case ES_OUT_RESTART_ES:
1800 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1804 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1806 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1808 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1813 for( i = 0; i < p_sys->i_es; i++ )
1817 if( es == p_sys->es[i] )
1819 EsOutSelect( out, es, true );
1825 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
1827 if( EsIsSelected( p_sys->es[i] ) )
1829 if( i_query == ES_OUT_RESTART_ES )
1831 if( p_sys->es[i]->p_dec )
1833 EsDestroyDecoder( out, p_sys->es[i] );
1834 EsCreateDecoder( out, p_sys->es[i] );
1839 EsUnselect( out, p_sys->es[i],
1840 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1846 if( i_query == ES_OUT_SET_ES )
1849 event.type = vlc_InputSelectedStreamChanged;
1850 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1855 case ES_OUT_SET_DEFAULT:
1857 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1861 /*p_sys->i_default_video_id = -1;*/
1862 /*p_sys->i_default_audio_id = -1;*/
1863 p_sys->i_default_sub_id = -1;
1865 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1867 /*p_sys->i_default_video_id = -1;*/
1869 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1871 /*p_sys->i_default_audio_id = -1;*/
1873 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1875 p_sys->i_default_sub_id = -1;
1879 /*if( es->fmt.i_cat == VIDEO_ES )
1880 p_sys->i_default_video_id = es->i_id;
1882 if( es->fmt.i_cat == AUDIO_ES )
1883 p_sys->i_default_audio_id = es->i_id;
1885 if( es->fmt.i_cat == SPU_ES )
1886 p_sys->i_default_sub_id = es->i_id;
1891 case ES_OUT_SET_PCR:
1892 case ES_OUT_SET_GROUP_PCR:
1894 es_out_pgrm_t *p_pgrm = NULL;
1898 if( i_query == ES_OUT_SET_PCR )
1900 p_pgrm = p_sys->p_pgrm;
1905 i_group = (int)va_arg( args, int );
1906 for( i = 0; i < p_sys->i_pgrm; i++ )
1908 if( p_sys->pgrm[i]->i_id == i_group )
1910 p_pgrm = p_sys->pgrm[i];
1915 if( p_pgrm == NULL )
1916 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1918 i_pcr = (int64_t)va_arg( args, int64_t );
1920 * TODO do not use mdate() but proper stream acquisition date */
1921 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
1922 p_sys->p_input->b_can_pace_control, i_pcr, mdate() );
1926 case ES_OUT_RESET_PCR:
1927 for( i = 0; i < p_sys->i_pgrm; i++ )
1928 input_clock_Reset( p_sys->pgrm[i]->p_clock );
1934 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1935 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1936 *pi_ts = input_clock_GetTS( p_sys->p_pgrm->p_clock,
1937 p_sys->p_input->i_pts_delay, i_ts );
1940 return VLC_EGENERIC;
1942 case ES_OUT_GET_GROUP:
1943 pi = (int*) va_arg( args, int* );
1945 *pi = p_sys->p_pgrm->i_id;
1947 *pi = -1; /* FIXME */
1950 case ES_OUT_SET_GROUP:
1953 i = (int) va_arg( args, int );
1954 for( j = 0; j < p_sys->i_pgrm; j++ )
1956 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1957 if( p_pgrm->i_id == i )
1959 EsOutProgramSelect( out, p_pgrm );
1963 return VLC_EGENERIC;
1966 case ES_OUT_SET_FMT:
1968 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1969 * to update the p_extra data */
1971 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1972 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1974 return VLC_EGENERIC;
1976 if( p_fmt->i_extra )
1978 es->fmt.i_extra = p_fmt->i_extra;
1979 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1980 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1985 EsDestroyDecoder( out, es );
1987 EsCreateDecoder( out, es );
1989 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1990 es->p_dec->fmt_in.p_extra =
1991 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1992 memcpy( es->p_dec->fmt_in.p_extra,
1993 p_fmt->p_extra, p_fmt->i_extra );
2000 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2004 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2005 i_date = (int64_t)va_arg( args, int64_t );
2007 if( !es || !es->p_dec )
2008 return VLC_EGENERIC;
2010 es->i_preroll_end = i_date;
2014 case ES_OUT_SET_GROUP_META:
2016 int i_group = (int)va_arg( args, int );
2017 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2019 EsOutProgramMeta( out, i_group, p_meta );
2022 case ES_OUT_SET_GROUP_EPG:
2024 int i_group = (int)va_arg( args, int );
2025 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2027 EsOutProgramEpg( out, i_group, p_epg );
2030 case ES_OUT_DEL_GROUP:
2032 int i_group = (int)va_arg( args, int );
2034 return EsOutProgramDel( out, i_group );
2038 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2039 return VLC_EGENERIC;
2043 /****************************************************************************
2044 * LanguageGetName: try to expend iso639 into plain name
2045 ****************************************************************************/
2046 static char *LanguageGetName( const char *psz_code )
2048 const iso639_lang_t *pl;
2050 if( psz_code == NULL )
2052 return strdup( "" );
2055 if( strlen( psz_code ) == 2 )
2057 pl = GetLang_1( psz_code );
2059 else if( strlen( psz_code ) == 3 )
2061 pl = GetLang_2B( psz_code );
2062 if( !strcmp( pl->psz_iso639_1, "??" ) )
2064 pl = GetLang_2T( psz_code );
2069 return strdup( psz_code );
2072 if( !strcmp( pl->psz_iso639_1, "??" ) )
2074 return strdup( psz_code );
2078 if( *pl->psz_native_name )
2080 return strdup( pl->psz_native_name );
2082 return strdup( pl->psz_eng_name );
2086 /* Get a 2 char code */
2087 static char *LanguageGetCode( const char *psz_lang )
2089 const iso639_lang_t *pl;
2091 if( psz_lang == NULL || *psz_lang == '\0' )
2092 return strdup("??");
2094 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2096 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2097 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2098 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2099 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2100 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2104 if( pl->psz_iso639_1 != NULL )
2105 return strdup( pl->psz_iso639_1 );
2107 return strdup("??");
2110 static char **LanguageSplit( const char *psz_langs )
2117 if( psz_langs == NULL ) return NULL;
2119 psz_parser = psz_dup = strdup(psz_langs);
2121 while( psz_parser && *psz_parser )
2126 psz = strchr(psz_parser, ',' );
2127 if( psz ) *psz++ = '\0';
2129 if( !strcmp( psz_parser, "any" ) )
2131 TAB_APPEND( i_psz, ppsz, strdup("any") );
2135 psz_code = LanguageGetCode( psz_parser );
2136 if( strcmp( psz_code, "??" ) )
2138 TAB_APPEND( i_psz, ppsz, psz_code );
2151 TAB_APPEND( i_psz, ppsz, NULL );
2158 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2162 if( !ppsz_langs || !psz_lang ) return -1;
2164 for( i = 0; ppsz_langs[i]; i++ )
2166 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2167 !strcasecmp( ppsz_langs[i], "any" ) )
2176 /****************************************************************************
2178 * - add meta info to the playlist item
2179 ****************************************************************************/
2180 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2182 es_out_sys_t *p_sys = out->p_sys;
2183 input_thread_t *p_input = p_sys->p_input;
2184 es_format_t *fmt = &es->fmt;
2188 /* Add stream info */
2189 if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
2192 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2193 "%.4s", (char*)&fmt->i_codec );
2195 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2196 "%s", es->psz_language );
2198 /* Add information */
2199 switch( fmt->i_cat )
2202 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2203 _("Type"), _("Audio") );
2205 if( fmt->audio.i_channels > 0 )
2206 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2207 "%u", fmt->audio.i_channels );
2209 if( fmt->audio.i_rate > 0 )
2211 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2212 _("%u Hz"), fmt->audio.i_rate );
2213 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2216 if( fmt->audio.i_bitspersample > 0 )
2217 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2218 _("Bits per sample"), "%u",
2219 fmt->audio.i_bitspersample );
2221 if( fmt->i_bitrate > 0 )
2223 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2224 _("%u kb/s"), fmt->i_bitrate / 1000 );
2225 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2230 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2231 _("Type"), _("Video") );
2233 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2234 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2235 _("Resolution"), "%ux%u",
2236 fmt->video.i_width, fmt->video.i_height );
2238 if( fmt->video.i_visible_width > 0 &&
2239 fmt->video.i_visible_height > 0 )
2240 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2241 _("Display resolution"), "%ux%u",
2242 fmt->video.i_visible_width,
2243 fmt->video.i_visible_height);
2244 if( fmt->video.i_frame_rate > 0 &&
2245 fmt->video.i_frame_rate_base > 0 )
2247 div = lldiv( (float)fmt->video.i_frame_rate /
2248 fmt->video.i_frame_rate_base * 1000000,
2250 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2251 _("Frame rate"), "%"PRId64".%06u",
2252 div.quot, (unsigned int )div.rem );
2257 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2258 _("Type"), _("Subtitle") );