1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
33 #include "input_internal.h"
35 #include "vlc_playlist.h"
37 /* FIXME we should find a better way than including that */
38 #include "../misc/iso-639_def.h"
40 /*****************************************************************************
42 *****************************************************************************/
48 /* Number of es for this pgrm */
51 vlc_bool_t b_selected;
53 /* Clock for this program */
62 es_out_pgrm_t *p_pgrm;
65 int64_t i_preroll_end;
67 /* Channel in the track type */
71 char *psz_language_code;
77 input_thread_t *p_input;
82 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
83 es_out_pgrm_t *p_pgrm; /* Master program */
102 char **ppsz_audio_language;
103 char **ppsz_sub_language;
105 /* current main es */
106 es_out_id_t *p_es_audio;
107 es_out_id_t *p_es_video;
108 es_out_id_t *p_es_sub;
111 int64_t i_audio_delay;
115 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
116 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
117 static void EsOutDel ( es_out_t *, es_out_id_t * );
118 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
119 static int EsOutControl( es_out_t *, int i_query, va_list );
121 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
123 static void EsSelect( es_out_t *out, es_out_id_t *es );
124 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
125 static char *LanguageGetName( const char *psz_code );
126 static char *LanguageGetCode( const char *psz_lang );
127 static char **LanguageSplit( const char *psz_langs );
128 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
130 /*****************************************************************************
132 *****************************************************************************/
133 es_out_t *input_EsOutNew( input_thread_t *p_input )
135 es_out_t *out = malloc( sizeof( es_out_t ) );
136 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
140 out->pf_add = EsOutAdd;
141 out->pf_send = EsOutSend;
142 out->pf_del = EsOutDel;
143 out->pf_control = EsOutControl;
146 p_sys->p_input = p_input;
148 p_sys->b_active = VLC_FALSE;
149 p_sys->i_mode = ES_OUT_MODE_AUTO;
154 p_sys->p_pgrm = NULL;
165 var_Get( p_input, "audio-track", &val );
166 p_sys->i_audio_last = val.i_int;
168 var_Get( p_input, "sub-track", &val );
169 p_sys->i_sub_last = val.i_int;
171 var_Get( p_input, "audio-language", &val );
172 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
173 if( p_sys->ppsz_audio_language )
175 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
176 msg_Dbg( p_input, "select audio in language[%d] %s",
177 i, p_sys->ppsz_audio_language[i] );
179 if( val.psz_string ) free( val.psz_string );
181 var_Get( p_input, "sub-language", &val );
182 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
183 if( p_sys->ppsz_sub_language )
185 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
186 msg_Dbg( p_input, "select subtitle in language[%d] %s",
187 i, p_sys->ppsz_sub_language[i] );
189 if( val.psz_string ) free( val.psz_string );
191 p_sys->p_es_audio = NULL;
192 p_sys->p_es_video = NULL;
193 p_sys->p_es_sub = NULL;
195 p_sys->i_audio_delay= 0;
196 p_sys->i_spu_delay = 0;
201 /*****************************************************************************
203 *****************************************************************************/
204 void input_EsOutDelete( es_out_t *out )
206 es_out_sys_t *p_sys = out->p_sys;
209 for( i = 0; i < p_sys->i_es; i++ )
211 if( p_sys->es[i]->p_dec )
213 input_DecoderDelete( p_sys->es[i]->p_dec );
215 if( p_sys->es[i]->psz_language )
216 free( p_sys->es[i]->psz_language );
217 if( p_sys->es[i]->psz_language_code )
218 free( p_sys->es[i]->psz_language_code );
219 es_format_Clean( &p_sys->es[i]->fmt );
221 free( p_sys->es[i] );
223 if( p_sys->ppsz_audio_language )
225 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
226 free( p_sys->ppsz_audio_language[i] );
227 free( p_sys->ppsz_audio_language );
229 if( p_sys->ppsz_sub_language )
231 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
232 free( p_sys->ppsz_sub_language[i] );
233 free( p_sys->ppsz_sub_language );
239 for( i = 0; i < p_sys->i_pgrm; i++ )
241 free( p_sys->pgrm[i] );
250 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
255 /* Special HACK, -i_id is tha cat of the stream */
256 return (es_out_id_t*)((uint8_t*)NULL-i_id);
259 for( i = 0; i < out->p_sys->i_es; i++ )
261 if( out->p_sys->es[i]->i_id == i_id )
262 return out->p_sys->es[i];
267 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
269 es_out_sys_t *p_sys = out->p_sys;
272 for( i = 0; i < p_sys->i_es; i++ )
274 es_out_id_t *es = p_sys->es[i];
276 /* Send a dummy block to let decoder know that
277 * there is a discontinuity */
278 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
280 input_DecoderDiscontinuity( es->p_dec );
285 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
287 es_out_sys_t *p_sys = out->p_sys;
289 if( i_cat == AUDIO_ES )
290 p_sys->i_audio_delay = i_delay;
291 else if( i_cat == SPU_ES )
292 p_sys->i_spu_delay = i_delay;
295 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
297 es_out_sys_t *p_sys = out->p_sys;
300 for( i = 0; i < p_sys->i_es; i++ )
302 es_out_id_t *es = p_sys->es[i];
304 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
310 /*****************************************************************************
312 *****************************************************************************/
313 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
314 vlc_bool_t b_delete )
316 es_out_sys_t *p_sys = out->p_sys;
317 input_thread_t *p_input = p_sys->p_input;
318 vlc_value_t val, text;
322 if( es->fmt.i_cat == AUDIO_ES )
323 psz_var = "audio-es";
324 else if( es->fmt.i_cat == VIDEO_ES )
325 psz_var = "video-es";
326 else if( es->fmt.i_cat == SPU_ES )
333 val.i_int = es->i_id;
334 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
335 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
339 /* Get the number of ES already added */
340 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
345 /* First one, we need to add the "Disable" choice */
346 val2.i_int = -1; text.psz_string = _("Disable");
347 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
351 /* Take care of the ES description */
352 if( es->fmt.psz_description && *es->fmt.psz_description )
354 if( es->psz_language && *es->psz_language )
356 text.psz_string = malloc( strlen( es->fmt.psz_description) + strlen( es->psz_language ) + 10 );
357 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description, es->psz_language );
359 else text.psz_string = strdup( es->fmt.psz_description );
363 if( es->psz_language && *es->psz_language )
366 text.psz_string = malloc( strlen( _("Track %i") )+ strlen( es->psz_language ) + 30 );
367 asprintf( &temp, _("Track %i"), val.i_int );
368 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
373 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
374 sprintf( text.psz_string, _("Track %i"), val.i_int );
378 val.i_int = es->i_id;
379 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
381 free( text.psz_string );
383 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
386 /* EsOutProgramSelect:
387 * Select a program and update the object variable
389 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
391 es_out_sys_t *p_sys = out->p_sys;
392 input_thread_t *p_input = p_sys->p_input;
396 if( p_sys->p_pgrm == p_pgrm )
397 return; /* Nothing to do */
401 es_out_pgrm_t *old = p_sys->p_pgrm;
402 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
404 for( i = 0; i < p_sys->i_es; i++ )
406 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
407 p_sys->i_mode != ES_OUT_MODE_ALL )
408 EsUnselect( out, p_sys->es[i], VLC_TRUE );
411 p_sys->p_es_audio = NULL;
412 p_sys->p_es_sub = NULL;
413 p_sys->p_es_video = NULL;
416 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
418 /* Mark it selected */
419 p_pgrm->b_selected = VLC_TRUE;
421 /* Switch master stream */
422 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
424 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
426 p_pgrm->clock.b_master = VLC_TRUE;
427 p_sys->p_pgrm = p_pgrm;
429 /* Update "program" */
430 val.i_int = p_pgrm->i_id;
431 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
434 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
435 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
436 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
437 for( i = 0; i < p_sys->i_es; i++ )
439 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
440 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
441 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
444 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
450 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
452 es_out_sys_t *p_sys = out->p_sys;
453 input_thread_t *p_input = p_sys->p_input;
456 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
459 p_pgrm->i_id = i_group;
461 p_pgrm->b_selected = VLC_FALSE;
462 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->input.i_cr_average );
465 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
467 /* Update "program" variable */
469 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
471 if( i_group == var_GetInteger( p_input, "program" ) )
473 EsOutProgramSelect( out, p_pgrm );
477 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
484 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
486 es_out_sys_t *p_sys = out->p_sys;
487 input_thread_t *p_input = p_sys->p_input;
488 char *psz_cat = malloc( strlen(_("Program")) + 10 );
491 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
492 sprintf( psz_cat, "%s %d", _("Program"), i_group );
494 for( i = 0; i < p_meta->i_meta; i++ )
496 msg_Dbg( p_input, " - %s = %s", p_meta->name[i], p_meta->value[i] );
498 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
499 _(p_meta->name[i]), "%s", p_meta->value[i] );
507 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
509 es_out_sys_t *p_sys = out->p_sys;
510 input_thread_t *p_input = p_sys->p_input;
512 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
513 es_out_pgrm_t *p_pgrm = NULL;
516 if( fmt->i_group < 0 )
518 msg_Err( p_input, "invalid group number" );
522 /* Search the program */
523 for( i = 0; i < p_sys->i_pgrm; i++ )
525 if( fmt->i_group == p_sys->pgrm[i]->i_id )
527 p_pgrm = p_sys->pgrm[i];
533 /* Create a new one */
534 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
537 /* Increase ref count for program */
542 fmt->i_id = out->p_sys->i_id;
543 es->i_id = fmt->i_id;
545 es_format_Copy( &es->fmt, fmt );
546 es->i_preroll_end = -1;
551 es->i_channel = p_sys->i_audio;
555 es->i_channel = p_sys->i_video;
559 es->i_channel = p_sys->i_sub;
566 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
567 es->psz_language_code = LanguageGetCode( fmt->psz_language );
570 if( es->p_pgrm == p_sys->p_pgrm )
571 EsOutESVarUpdate( out, es, VLC_FALSE );
573 /* Select it if needed */
574 EsOutSelect( out, es, VLC_FALSE );
577 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
578 p_sys->i_id++; /* always incremented */
592 EsOutAddInfo( out, es );
597 static void EsSelect( es_out_t *out, es_out_id_t *es )
599 es_out_sys_t *p_sys = out->p_sys;
600 input_thread_t *p_input = p_sys->p_input;
606 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
610 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
612 if( !var_GetBool( p_input, "video" ) ||
613 ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
615 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
620 else if( es->fmt.i_cat == AUDIO_ES )
622 var_Get( p_input, "audio", &val );
623 if( !var_GetBool( p_input, "audio" ) ||
624 ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
626 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
632 es->i_preroll_end = -1;
633 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
634 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
637 if( es->fmt.i_cat == VIDEO_ES )
638 psz_var = "video-es";
639 else if( es->fmt.i_cat == AUDIO_ES )
640 psz_var = "audio-es";
641 else if( es->fmt.i_cat == SPU_ES )
646 /* Mark it as selected */
647 val.i_int = es->i_id;
648 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
651 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
654 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
656 es_out_sys_t *p_sys = out->p_sys;
657 input_thread_t *p_input = p_sys->p_input;
661 if( es->p_dec == NULL )
663 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
667 input_DecoderDelete( es->p_dec );
674 if( es->p_dec == NULL )
676 if( es->fmt.i_cat == VIDEO_ES )
677 psz_var = "video-es";
678 else if( es->fmt.i_cat == AUDIO_ES )
679 psz_var = "audio-es";
680 else if( es->fmt.i_cat == SPU_ES )
685 /* Mark it as selected */
687 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
689 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
693 * Select an ES given the current mode
694 * XXX: you need to take a the lock before (stream.stream_lock)
696 * \param out The es_out structure
697 * \param es es_out_id structure
701 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
703 es_out_sys_t *p_sys = out->p_sys;
705 int i_cat = es->fmt.i_cat;
707 if( !p_sys->b_active ||
708 ( !b_force && es->fmt.i_priority < 0 ) )
713 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
718 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
722 var_Get( p_sys->p_input, "programs", &val );
723 for ( i = 0; i < val.p_list->i_count; i++ )
725 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
732 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
734 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
738 if( es->p_pgrm != p_sys->p_pgrm )
741 if( i_cat == AUDIO_ES )
743 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
744 es->psz_language_code );
746 if( p_sys->p_es_audio &&
747 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
749 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
750 p_sys->p_es_audio->psz_language_code );
752 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
754 i_wanted = es->i_channel;
758 /* Select audio if (no audio selected yet)
759 * - no audio-language
760 * - no audio code for the ES
761 * - audio code in the requested list */
763 !strcmp( es->psz_language_code, "??" ) ||
764 !p_sys->ppsz_audio_language )
765 i_wanted = es->i_channel;
768 if( p_sys->i_audio_last >= 0 )
769 i_wanted = p_sys->i_audio_last;
771 else if( i_cat == SPU_ES )
773 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
774 es->psz_language_code );
776 if( p_sys->p_es_sub &&
777 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
779 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
780 p_sys->p_es_sub->psz_language_code );
782 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
783 idx1, es->psz_language_code, idx2,
784 p_sys->p_es_sub->psz_language_code );
786 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
788 /* We found a SPU that matches our language request */
789 i_wanted = es->i_channel;
793 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
794 idx1, es->psz_language_code );
796 i_wanted = es->i_channel;
798 if( p_sys->i_sub_last >= 0 )
799 i_wanted = p_sys->i_sub_last;
801 else if( i_cat == VIDEO_ES )
803 i_wanted = es->i_channel;
806 if( i_wanted == es->i_channel && es->p_dec == NULL )
810 /* FIXME TODO handle priority here */
813 if( i_cat == AUDIO_ES )
815 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
817 p_sys->p_es_audio != es &&
818 p_sys->p_es_audio->p_dec )
820 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
822 p_sys->p_es_audio = es;
824 else if( i_cat == SPU_ES )
826 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
828 p_sys->p_es_sub != es &&
829 p_sys->p_es_sub->p_dec )
831 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
833 p_sys->p_es_sub = es;
835 else if( i_cat == VIDEO_ES )
837 p_sys->p_es_video = es;
843 * Send a block for the given es_out
845 * \param out the es_out to send from
846 * \param es the es_out_id
847 * \param p_block the data block to send
849 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
851 es_out_sys_t *p_sys = out->p_sys;
852 input_thread_t *p_input = p_sys->p_input;
853 es_out_pgrm_t *p_pgrm = es->p_pgrm;
856 if( es->fmt.i_cat == AUDIO_ES )
857 i_delay = p_sys->i_audio_delay;
858 else if( es->fmt.i_cat == SPU_ES )
859 i_delay = p_sys->i_spu_delay;
863 /* Mark preroll blocks */
864 if( es->i_preroll_end >= 0 )
866 int64_t i_date = p_block->i_pts;
868 i_date = p_block->i_dts;
870 if( i_date < es->i_preroll_end )
871 p_block->i_flags |= BLOCK_FLAG_PREROLL;
873 es->i_preroll_end = -1;
876 /* +11 -> avoid null value with non null dts/pts */
877 if( p_block->i_dts > 0 )
880 input_ClockGetTS( p_input, &p_pgrm->clock,
881 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
883 if( p_block->i_pts > 0 )
886 input_ClockGetTS( p_input, &p_pgrm->clock,
887 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
889 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
891 mtime_t current_date = mdate();
893 || p_block->i_pts > current_date + 10000000
894 || current_date > p_block->i_pts )
896 /* ETSI EN 300 472 Annex A : do not take into account the PTS
897 * for teletext streams. */
898 p_block->i_pts = current_date + 400000
899 + p_input->i_pts_delay + i_delay;
903 p_block->i_rate = p_input->i_rate;
905 /* TODO handle mute */
906 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
907 p_input->i_rate == INPUT_RATE_DEFAULT ) )
909 input_DecoderDecode( es->p_dec, p_block );
913 block_Release( p_block );
919 /*****************************************************************************
921 *****************************************************************************/
922 static void EsOutDel( es_out_t *out, es_out_id_t *es )
924 es_out_sys_t *p_sys = out->p_sys;
926 /* We don't try to reselect */
928 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
930 if( es->p_pgrm == p_sys->p_pgrm )
931 EsOutESVarUpdate( out, es, VLC_TRUE );
933 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
936 if( es->p_pgrm->i_es == 0 )
938 msg_Warn( p_sys->p_input, "Program doesn't contain anymore ES, "
942 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
943 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
944 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
946 switch( es->fmt.i_cat )
959 if( es->psz_language )
960 free( es->psz_language );
961 if( es->psz_language_code )
962 free( es->psz_language_code );
964 es_format_Clean( &es->fmt );
970 * Control query handler
972 * \param out the es_out to control
973 * \param i_query A es_out query as defined in include/ninput.h
974 * \param args a variable list of arguments for the query
975 * \return VLC_SUCCESS or an error code
977 static int EsOutControl( es_out_t *out, int i_query, va_list args )
979 es_out_sys_t *p_sys = out->p_sys;
987 case ES_OUT_SET_ES_STATE:
988 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
989 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
990 if( b && es->p_dec == NULL )
993 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
995 else if( !b && es->p_dec )
997 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1002 case ES_OUT_GET_ES_STATE:
1003 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1004 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1006 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1009 case ES_OUT_SET_ACTIVE:
1011 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1012 p_sys->b_active = b;
1015 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1019 case ES_OUT_GET_ACTIVE:
1020 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1021 *pb = p_sys->b_active;
1024 case ES_OUT_SET_MODE:
1025 i = (int) va_arg( args, int );
1026 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1027 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1031 /* Reapply policy mode */
1032 for( i = 0; i < p_sys->i_es; i++ )
1034 if( p_sys->es[i]->p_dec )
1036 EsUnselect( out, p_sys->es[i],
1037 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1040 for( i = 0; i < p_sys->i_es; i++ )
1042 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1046 return VLC_EGENERIC;
1048 case ES_OUT_GET_MODE:
1049 pi = (int*) va_arg( args, int* );
1050 *pi = p_sys->i_mode;
1054 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1055 /* Special case NULL, NULL+i_cat */
1058 for( i = 0; i < p_sys->i_es; i++ )
1060 if( p_sys->es[i]->p_dec )
1061 EsUnselect( out, p_sys->es[i],
1062 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1065 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1067 for( i = 0; i < p_sys->i_es; i++ )
1069 if( p_sys->es[i]->p_dec &&
1070 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1071 EsUnselect( out, p_sys->es[i],
1072 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1075 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1077 for( i = 0; i < p_sys->i_es; i++ )
1079 if( p_sys->es[i]->p_dec &&
1080 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1081 EsUnselect( out, p_sys->es[i],
1082 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1085 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1087 for( i = 0; i < p_sys->i_es; i++ )
1089 if( p_sys->es[i]->p_dec &&
1090 p_sys->es[i]->fmt.i_cat == SPU_ES )
1091 EsUnselect( out, p_sys->es[i],
1092 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1097 for( i = 0; i < p_sys->i_es; i++ )
1099 if( es == p_sys->es[i] )
1101 EsOutSelect( out, es, VLC_TRUE );
1108 case ES_OUT_SET_PCR:
1109 case ES_OUT_SET_GROUP_PCR:
1111 es_out_pgrm_t *p_pgrm = NULL;
1115 if( i_query == ES_OUT_SET_PCR )
1117 p_pgrm = p_sys->p_pgrm;
1122 i_group = (int)va_arg( args, int );
1123 for( i = 0; i < p_sys->i_pgrm; i++ )
1125 if( p_sys->pgrm[i]->i_id == i_group )
1127 p_pgrm = p_sys->pgrm[i];
1132 if( p_pgrm == NULL )
1133 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1135 i_pcr = (int64_t)va_arg( args, int64_t );
1136 /* search program */
1137 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1138 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1139 (i_pcr + 11 ) * 9 / 100);
1143 case ES_OUT_RESET_PCR:
1144 for( i = 0; i < p_sys->i_pgrm; i++ )
1146 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1147 p_sys->pgrm[i]->clock.last_pts = 0;
1154 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1155 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1156 *pi_ts = input_ClockGetTS( p_sys->p_input,
1157 &p_sys->p_pgrm->clock,
1158 ( i_ts + 11 ) * 9 / 100 );
1161 return VLC_EGENERIC;
1163 case ES_OUT_GET_GROUP:
1164 pi = (int*) va_arg( args, int* );
1166 *pi = p_sys->p_pgrm->i_id;
1168 *pi = -1; /* FIXME */
1171 case ES_OUT_SET_GROUP:
1174 i = (int) va_arg( args, int );
1175 for( j = 0; j < p_sys->i_pgrm; j++ )
1177 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1178 if( p_pgrm->i_id == i )
1180 EsOutProgramSelect( out, p_pgrm );
1184 return VLC_EGENERIC;
1187 case ES_OUT_SET_FMT:
1189 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1190 * to update the p_extra data */
1192 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1193 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1194 if( es == NULL ) return VLC_EGENERIC;
1196 if( p_fmt->i_extra )
1198 es->fmt.i_extra = p_fmt->i_extra;
1199 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1200 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1202 if( !es->p_dec ) return VLC_SUCCESS;
1205 input_DecoderDelete( es->p_dec );
1206 es->p_dec = input_DecoderNew( p_sys->p_input,
1207 &es->fmt, VLC_FALSE );
1210 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1211 es->p_dec->fmt_in.p_extra =
1212 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1213 memcpy( es->p_dec->fmt_in.p_extra,
1214 p_fmt->p_extra, p_fmt->i_extra );
1221 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1225 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1226 i_date = (int64_t)va_arg( args, int64_t );
1228 if( !es || !es->p_dec )
1229 return VLC_EGENERIC;
1231 es->i_preroll_end = i_date;
1232 input_DecoderPreroll( es->p_dec, i_date );
1236 case ES_OUT_SET_GROUP_META:
1238 int i_group = (int)va_arg( args, int );
1239 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1241 EsOutProgramMeta( out, i_group, p_meta );
1246 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1247 return VLC_EGENERIC;
1251 /****************************************************************************
1252 * LanguageGetName: try to expend iso639 into plain name
1253 ****************************************************************************/
1254 static char *LanguageGetName( const char *psz_code )
1256 const iso639_lang_t *pl;
1258 if( psz_code == NULL )
1260 return strdup( "" );
1263 if( strlen( psz_code ) == 2 )
1265 pl = GetLang_1( psz_code );
1267 else if( strlen( psz_code ) == 3 )
1269 pl = GetLang_2B( psz_code );
1270 if( !strcmp( pl->psz_iso639_1, "??" ) )
1272 pl = GetLang_2T( psz_code );
1277 return strdup( psz_code );
1280 if( !strcmp( pl->psz_iso639_1, "??" ) )
1282 return strdup( psz_code );
1286 if( *pl->psz_native_name )
1288 return strdup( pl->psz_native_name );
1290 return strdup( pl->psz_eng_name );
1294 /* Get a 2 char code */
1295 static char *LanguageGetCode( const char *psz_lang )
1297 const iso639_lang_t *pl;
1299 if( psz_lang == NULL || *psz_lang == '\0' )
1300 return strdup("??");
1302 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1304 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1305 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1306 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1307 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1308 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1312 if( pl->psz_iso639_1 != NULL )
1313 return strdup( pl->psz_iso639_1 );
1315 return strdup("??");
1318 static char **LanguageSplit( const char *psz_langs )
1325 if( psz_langs == NULL ) return NULL;
1327 psz_parser = psz_dup = strdup(psz_langs);
1329 while( psz_parser && *psz_parser )
1334 psz = strchr(psz_parser, ',' );
1335 if( psz ) *psz++ = '\0';
1337 psz_code = LanguageGetCode( psz_parser );
1338 if( strcmp( psz_code, "??" ) )
1340 TAB_APPEND( i_psz, ppsz, psz_code );
1348 TAB_APPEND( i_psz, ppsz, NULL );
1355 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1359 if( !ppsz_langs || !psz_lang ) return -1;
1361 for( i = 0; ppsz_langs[i]; i++ )
1362 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1367 /****************************************************************************
1369 * - add meta info to the playlist item
1370 ****************************************************************************/
1371 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1373 es_out_sys_t *p_sys = out->p_sys;
1374 input_thread_t *p_input = p_sys->p_input;
1375 es_format_t *fmt = &es->fmt;
1378 /* Add stream info */
1379 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1381 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1382 "%.4s", (char*)&fmt->i_codec );
1384 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1385 "%s", es->psz_language );
1387 /* Add information */
1388 switch( fmt->i_cat )
1391 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1392 _("Type"), _("Audio") );
1394 if( fmt->audio.i_channels > 0 )
1395 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1396 "%d", fmt->audio.i_channels );
1398 if( fmt->audio.i_rate > 0 )
1399 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1400 _("%d Hz"), fmt->audio.i_rate );
1402 if( fmt->audio.i_bitspersample > 0 )
1403 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1404 _("Bits per sample"), "%d",
1405 fmt->audio.i_bitspersample );
1407 if( fmt->i_bitrate > 0 )
1408 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1409 _("%d kb/s"), fmt->i_bitrate / 1000 );
1413 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1414 _("Type"), _("Video") );
1416 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1417 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1418 _("Resolution"), "%dx%d",
1419 fmt->video.i_width, fmt->video.i_height );
1421 if( fmt->video.i_visible_width > 0 &&
1422 fmt->video.i_visible_height > 0 )
1423 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1424 _("Display resolution"), "%dx%d",
1425 fmt->video.i_visible_width,
1426 fmt->video.i_visible_height);
1430 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1431 _("Type"), _("Subtitle") );