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 );
485 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
487 es_out_sys_t *p_sys = out->p_sys;
488 input_thread_t *p_input = p_sys->p_input;
490 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
491 es_out_pgrm_t *p_pgrm = NULL;
494 if( fmt->i_group < 0 )
496 msg_Err( p_input, "invalid group number" );
500 /* Search the program */
501 for( i = 0; i < p_sys->i_pgrm; i++ )
503 if( fmt->i_group == p_sys->pgrm[i]->i_id )
505 p_pgrm = p_sys->pgrm[i];
511 /* Create a new one */
512 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
515 /* Increase ref count for program */
520 fmt->i_id = out->p_sys->i_id;
521 es->i_id = fmt->i_id;
523 es_format_Copy( &es->fmt, fmt );
524 es->i_preroll_end = -1;
529 es->i_channel = p_sys->i_audio;
533 es->i_channel = p_sys->i_video;
537 es->i_channel = p_sys->i_sub;
544 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
545 es->psz_language_code = LanguageGetCode( fmt->psz_language );
548 if( es->p_pgrm == p_sys->p_pgrm )
549 EsOutESVarUpdate( out, es, VLC_FALSE );
551 /* Select it if needed */
552 EsOutSelect( out, es, VLC_FALSE );
555 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
556 p_sys->i_id++; /* always incremented */
570 EsOutAddInfo( out, es );
575 static void EsSelect( es_out_t *out, es_out_id_t *es )
577 es_out_sys_t *p_sys = out->p_sys;
578 input_thread_t *p_input = p_sys->p_input;
584 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
588 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
590 if( !var_GetBool( p_input, "video" ) ||
591 ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
593 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
598 else if( es->fmt.i_cat == AUDIO_ES )
600 var_Get( p_input, "audio", &val );
601 if( !var_GetBool( p_input, "audio" ) ||
602 ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
604 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
610 es->i_preroll_end = -1;
611 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
612 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
615 if( es->fmt.i_cat == VIDEO_ES )
616 psz_var = "video-es";
617 else if( es->fmt.i_cat == AUDIO_ES )
618 psz_var = "audio-es";
619 else if( es->fmt.i_cat == SPU_ES )
624 /* Mark it as selected */
625 val.i_int = es->i_id;
626 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
629 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
632 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
634 es_out_sys_t *p_sys = out->p_sys;
635 input_thread_t *p_input = p_sys->p_input;
639 if( es->p_dec == NULL )
641 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
645 input_DecoderDelete( es->p_dec );
652 if( es->p_dec == NULL )
654 if( es->fmt.i_cat == VIDEO_ES )
655 psz_var = "video-es";
656 else if( es->fmt.i_cat == AUDIO_ES )
657 psz_var = "audio-es";
658 else if( es->fmt.i_cat == SPU_ES )
663 /* Mark it as selected */
665 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
667 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
671 * Select an ES given the current mode
672 * XXX: you need to take a the lock before (stream.stream_lock)
674 * \param out The es_out structure
675 * \param es es_out_id structure
679 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
681 es_out_sys_t *p_sys = out->p_sys;
683 int i_cat = es->fmt.i_cat;
685 if( !p_sys->b_active ||
686 ( !b_force && es->fmt.i_priority < 0 ) )
691 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
696 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
700 var_Get( p_sys->p_input, "programs", &val );
701 for ( i = 0; i < val.p_list->i_count; i++ )
703 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
710 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
712 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
716 if( es->p_pgrm != p_sys->p_pgrm )
719 if( i_cat == AUDIO_ES )
721 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
722 es->psz_language_code );
724 if( p_sys->p_es_audio &&
725 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
727 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
728 p_sys->p_es_audio->psz_language_code );
730 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
732 i_wanted = es->i_channel;
736 /* Select audio if (no audio selected yet)
737 * - no audio-language
738 * - no audio code for the ES
739 * - audio code in the requested list */
741 !strcmp( es->psz_language_code, "??" ) ||
742 !p_sys->ppsz_audio_language )
743 i_wanted = es->i_channel;
746 if( p_sys->i_audio_last >= 0 )
747 i_wanted = p_sys->i_audio_last;
749 else if( i_cat == SPU_ES )
751 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
752 es->psz_language_code );
754 if( p_sys->p_es_sub &&
755 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
757 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
758 p_sys->p_es_sub->psz_language_code );
760 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
761 idx1, es->psz_language_code, idx2,
762 p_sys->p_es_sub->psz_language_code );
764 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
766 /* We found a SPU that matches our language request */
767 i_wanted = es->i_channel;
771 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
772 idx1, es->psz_language_code );
774 i_wanted = es->i_channel;
776 if( p_sys->i_sub_last >= 0 )
777 i_wanted = p_sys->i_sub_last;
779 else if( i_cat == VIDEO_ES )
781 i_wanted = es->i_channel;
784 if( i_wanted == es->i_channel && es->p_dec == NULL )
788 /* FIXME TODO handle priority here */
791 if( i_cat == AUDIO_ES )
793 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
795 p_sys->p_es_audio != es &&
796 p_sys->p_es_audio->p_dec )
798 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
800 p_sys->p_es_audio = es;
802 else if( i_cat == SPU_ES )
804 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
806 p_sys->p_es_sub != es &&
807 p_sys->p_es_sub->p_dec )
809 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
811 p_sys->p_es_sub = es;
813 else if( i_cat == VIDEO_ES )
815 p_sys->p_es_video = es;
821 * Send a block for the given es_out
823 * \param out the es_out to send from
824 * \param es the es_out_id
825 * \param p_block the data block to send
827 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
829 es_out_sys_t *p_sys = out->p_sys;
830 input_thread_t *p_input = p_sys->p_input;
831 es_out_pgrm_t *p_pgrm = es->p_pgrm;
834 if( es->fmt.i_cat == AUDIO_ES )
835 i_delay = p_sys->i_audio_delay;
836 else if( es->fmt.i_cat == SPU_ES )
837 i_delay = p_sys->i_spu_delay;
841 /* Mark preroll blocks */
842 if( es->i_preroll_end >= 0 )
844 int64_t i_date = p_block->i_pts;
846 i_date = p_block->i_dts;
848 if( i_date < es->i_preroll_end )
849 p_block->i_flags |= BLOCK_FLAG_PREROLL;
851 es->i_preroll_end = -1;
854 /* +11 -> avoid null value with non null dts/pts */
855 if( p_block->i_dts > 0 )
858 input_ClockGetTS( p_input, &p_pgrm->clock,
859 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
861 if( p_block->i_pts > 0 )
864 input_ClockGetTS( p_input, &p_pgrm->clock,
865 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
867 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
869 mtime_t current_date = mdate();
871 || p_block->i_pts > current_date + 10000000
872 || current_date > p_block->i_pts )
874 /* ETSI EN 300 472 Annex A : do not take into account the PTS
875 * for teletext streams. */
876 p_block->i_pts = current_date + 400000
877 + p_input->i_pts_delay + i_delay;
881 p_block->i_rate = p_input->i_rate;
883 /* TODO handle mute */
884 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
885 p_input->i_rate == INPUT_RATE_DEFAULT ) )
887 input_DecoderDecode( es->p_dec, p_block );
891 block_Release( p_block );
897 /*****************************************************************************
899 *****************************************************************************/
900 static void EsOutDel( es_out_t *out, es_out_id_t *es )
902 es_out_sys_t *p_sys = out->p_sys;
904 /* We don't try to reselect */
906 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
908 if( es->p_pgrm == p_sys->p_pgrm )
909 EsOutESVarUpdate( out, es, VLC_TRUE );
911 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
914 if( es->p_pgrm->i_es == 0 )
916 msg_Warn( p_sys->p_input, "Program doesn't contain anymore ES, "
920 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
921 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
922 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
924 switch( es->fmt.i_cat )
937 if( es->psz_language )
938 free( es->psz_language );
939 if( es->psz_language_code )
940 free( es->psz_language_code );
942 es_format_Clean( &es->fmt );
948 * Control query handler
950 * \param out the es_out to control
951 * \param i_query A es_out query as defined in include/ninput.h
952 * \param args a variable list of arguments for the query
953 * \return VLC_SUCCESS or an error code
955 static int EsOutControl( es_out_t *out, int i_query, va_list args )
957 es_out_sys_t *p_sys = out->p_sys;
965 case ES_OUT_SET_ES_STATE:
966 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
967 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
968 if( b && es->p_dec == NULL )
971 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
973 else if( !b && es->p_dec )
975 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
980 case ES_OUT_GET_ES_STATE:
981 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
982 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
984 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
987 case ES_OUT_SET_ACTIVE:
989 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
993 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
997 case ES_OUT_GET_ACTIVE:
998 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
999 *pb = p_sys->b_active;
1002 case ES_OUT_SET_MODE:
1003 i = (int) va_arg( args, int );
1004 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1005 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1009 /* Reapply policy mode */
1010 for( i = 0; i < p_sys->i_es; i++ )
1012 if( p_sys->es[i]->p_dec )
1014 EsUnselect( out, p_sys->es[i],
1015 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1018 for( i = 0; i < p_sys->i_es; i++ )
1020 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1024 return VLC_EGENERIC;
1026 case ES_OUT_GET_MODE:
1027 pi = (int*) va_arg( args, int* );
1028 *pi = p_sys->i_mode;
1032 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1033 /* Special case NULL, NULL+i_cat */
1036 for( i = 0; i < p_sys->i_es; i++ )
1038 if( p_sys->es[i]->p_dec )
1039 EsUnselect( out, p_sys->es[i],
1040 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1043 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1045 for( i = 0; i < p_sys->i_es; i++ )
1047 if( p_sys->es[i]->p_dec &&
1048 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1049 EsUnselect( out, p_sys->es[i],
1050 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1053 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1055 for( i = 0; i < p_sys->i_es; i++ )
1057 if( p_sys->es[i]->p_dec &&
1058 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1059 EsUnselect( out, p_sys->es[i],
1060 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1063 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1065 for( i = 0; i < p_sys->i_es; i++ )
1067 if( p_sys->es[i]->p_dec &&
1068 p_sys->es[i]->fmt.i_cat == SPU_ES )
1069 EsUnselect( out, p_sys->es[i],
1070 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1075 for( i = 0; i < p_sys->i_es; i++ )
1077 if( es == p_sys->es[i] )
1079 EsOutSelect( out, es, VLC_TRUE );
1086 case ES_OUT_SET_PCR:
1087 case ES_OUT_SET_GROUP_PCR:
1089 es_out_pgrm_t *p_pgrm = NULL;
1093 if( i_query == ES_OUT_SET_PCR )
1095 p_pgrm = p_sys->p_pgrm;
1100 i_group = (int)va_arg( args, int );
1101 for( i = 0; i < p_sys->i_pgrm; i++ )
1103 if( p_sys->pgrm[i]->i_id == i_group )
1105 p_pgrm = p_sys->pgrm[i];
1110 if( p_pgrm == NULL )
1111 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1113 i_pcr = (int64_t)va_arg( args, int64_t );
1114 /* search program */
1115 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1116 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1117 (i_pcr + 11 ) * 9 / 100);
1121 case ES_OUT_RESET_PCR:
1122 for( i = 0; i < p_sys->i_pgrm; i++ )
1124 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1125 p_sys->pgrm[i]->clock.last_pts = 0;
1132 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1133 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1134 *pi_ts = input_ClockGetTS( p_sys->p_input,
1135 &p_sys->p_pgrm->clock,
1136 ( i_ts + 11 ) * 9 / 100 );
1139 return VLC_EGENERIC;
1141 case ES_OUT_GET_GROUP:
1142 pi = (int*) va_arg( args, int* );
1144 *pi = p_sys->p_pgrm->i_id;
1146 *pi = -1; /* FIXME */
1149 case ES_OUT_SET_GROUP:
1152 i = (int) va_arg( args, int );
1153 for( j = 0; j < p_sys->i_pgrm; j++ )
1155 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1156 if( p_pgrm->i_id == i )
1158 EsOutProgramSelect( out, p_pgrm );
1162 return VLC_EGENERIC;
1165 case ES_OUT_SET_FMT:
1167 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1168 * to update the p_extra data */
1170 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1171 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1172 if( es == NULL ) return VLC_EGENERIC;
1174 if( p_fmt->i_extra )
1176 es->fmt.i_extra = p_fmt->i_extra;
1177 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1178 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1180 if( !es->p_dec ) return VLC_SUCCESS;
1183 input_DecoderDelete( es->p_dec );
1184 es->p_dec = input_DecoderNew( p_sys->p_input,
1185 &es->fmt, VLC_FALSE );
1188 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1189 es->p_dec->fmt_in.p_extra =
1190 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1191 memcpy( es->p_dec->fmt_in.p_extra,
1192 p_fmt->p_extra, p_fmt->i_extra );
1199 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1203 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1204 i_date = (int64_t)va_arg( args, int64_t );
1206 if( !es || !es->p_dec )
1207 return VLC_EGENERIC;
1209 es->i_preroll_end = i_date;
1210 input_DecoderPreroll( es->p_dec, i_date );
1216 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1217 return VLC_EGENERIC;
1221 /****************************************************************************
1222 * LanguageGetName: try to expend iso639 into plain name
1223 ****************************************************************************/
1224 static char *LanguageGetName( const char *psz_code )
1226 const iso639_lang_t *pl;
1228 if( psz_code == NULL )
1230 return strdup( "" );
1233 if( strlen( psz_code ) == 2 )
1235 pl = GetLang_1( psz_code );
1237 else if( strlen( psz_code ) == 3 )
1239 pl = GetLang_2B( psz_code );
1240 if( !strcmp( pl->psz_iso639_1, "??" ) )
1242 pl = GetLang_2T( psz_code );
1247 return strdup( psz_code );
1250 if( !strcmp( pl->psz_iso639_1, "??" ) )
1252 return strdup( psz_code );
1256 if( *pl->psz_native_name )
1258 return strdup( pl->psz_native_name );
1260 return strdup( pl->psz_eng_name );
1264 /* Get a 2 char code */
1265 static char *LanguageGetCode( const char *psz_lang )
1267 const iso639_lang_t *pl;
1269 if( psz_lang == NULL || *psz_lang == '\0' )
1270 return strdup("??");
1272 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1274 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1275 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1276 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1277 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1278 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1282 if( pl->psz_iso639_1 != NULL )
1283 return strdup( pl->psz_iso639_1 );
1285 return strdup("??");
1288 static char **LanguageSplit( const char *psz_langs )
1295 if( psz_langs == NULL ) return NULL;
1297 psz_parser = psz_dup = strdup(psz_langs);
1299 while( psz_parser && *psz_parser )
1304 psz = strchr(psz_parser, ',' );
1305 if( psz ) *psz++ = '\0';
1307 psz_code = LanguageGetCode( psz_parser );
1308 if( strcmp( psz_code, "??" ) )
1310 TAB_APPEND( i_psz, ppsz, psz_code );
1318 TAB_APPEND( i_psz, ppsz, NULL );
1325 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1329 if( !ppsz_langs || !psz_lang ) return -1;
1331 for( i = 0; ppsz_langs[i]; i++ )
1332 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1337 /****************************************************************************
1339 * - add meta info to the playlist item
1340 ****************************************************************************/
1341 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1343 es_out_sys_t *p_sys = out->p_sys;
1344 input_thread_t *p_input = p_sys->p_input;
1345 es_format_t *fmt = &es->fmt;
1348 /* Add stream info */
1349 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1351 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1352 "%.4s", (char*)&fmt->i_codec );
1354 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1355 "%s", es->psz_language );
1357 /* Add information */
1358 switch( fmt->i_cat )
1361 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1362 _("Type"), _("Audio") );
1364 if( fmt->audio.i_channels > 0 )
1365 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1366 "%d", fmt->audio.i_channels );
1368 if( fmt->audio.i_rate > 0 )
1369 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1370 _("%d Hz"), fmt->audio.i_rate );
1372 if( fmt->audio.i_bitspersample > 0 )
1373 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1374 _("Bits per sample"), "%d",
1375 fmt->audio.i_bitspersample );
1377 if( fmt->i_bitrate > 0 )
1378 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1379 _("%d kb/s"), fmt->i_bitrate / 1000 );
1383 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1384 _("Type"), _("Video") );
1386 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1387 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1388 _("Resolution"), "%dx%d",
1389 fmt->video.i_width, fmt->video.i_height );
1391 if( fmt->video.i_visible_width > 0 &&
1392 fmt->video.i_visible_height > 0 )
1393 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1394 _("Display resolution"), "%dx%d",
1395 fmt->video.i_visible_width,
1396 fmt->video.i_visible_height);
1400 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1401 _("Type"), _("Subtitle") );