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 */
56 char *psz_now_playing;
64 es_out_pgrm_t *p_pgrm;
67 int64_t i_preroll_end;
69 /* Channel in the track type */
73 char *psz_language_code;
79 input_thread_t *p_input;
84 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
85 es_out_pgrm_t *p_pgrm; /* Master program */
104 char **ppsz_audio_language;
105 char **ppsz_sub_language;
107 /* current main es */
108 es_out_id_t *p_es_audio;
109 es_out_id_t *p_es_video;
110 es_out_id_t *p_es_sub;
113 int64_t i_audio_delay;
117 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
118 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
119 static void EsOutDel ( es_out_t *, es_out_id_t * );
120 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
121 static int EsOutControl( es_out_t *, int i_query, va_list );
123 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
125 static void EsSelect( es_out_t *out, es_out_id_t *es );
126 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
127 static char *LanguageGetName( const char *psz_code );
128 static char *LanguageGetCode( const char *psz_lang );
129 static char **LanguageSplit( const char *psz_langs );
130 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
132 /*****************************************************************************
134 *****************************************************************************/
135 es_out_t *input_EsOutNew( input_thread_t *p_input )
137 es_out_t *out = malloc( sizeof( es_out_t ) );
138 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
142 out->pf_add = EsOutAdd;
143 out->pf_send = EsOutSend;
144 out->pf_del = EsOutDel;
145 out->pf_control = EsOutControl;
148 p_sys->p_input = p_input;
150 p_sys->b_active = VLC_FALSE;
151 p_sys->i_mode = ES_OUT_MODE_AUTO;
156 p_sys->p_pgrm = NULL;
167 var_Get( p_input, "audio-track", &val );
168 p_sys->i_audio_last = val.i_int;
170 var_Get( p_input, "sub-track", &val );
171 p_sys->i_sub_last = val.i_int;
173 var_Get( p_input, "audio-language", &val );
174 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
175 if( p_sys->ppsz_audio_language )
177 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
178 msg_Dbg( p_input, "select audio in language[%d] %s",
179 i, p_sys->ppsz_audio_language[i] );
181 if( val.psz_string ) free( val.psz_string );
183 var_Get( p_input, "sub-language", &val );
184 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
185 if( p_sys->ppsz_sub_language )
187 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
188 msg_Dbg( p_input, "select subtitle in language[%d] %s",
189 i, p_sys->ppsz_sub_language[i] );
191 if( val.psz_string ) free( val.psz_string );
193 p_sys->p_es_audio = NULL;
194 p_sys->p_es_video = NULL;
195 p_sys->p_es_sub = NULL;
197 p_sys->i_audio_delay= 0;
198 p_sys->i_spu_delay = 0;
203 /*****************************************************************************
205 *****************************************************************************/
206 void input_EsOutDelete( es_out_t *out )
208 es_out_sys_t *p_sys = out->p_sys;
211 for( i = 0; i < p_sys->i_es; i++ )
213 if( p_sys->es[i]->p_dec )
215 input_DecoderDelete( p_sys->es[i]->p_dec );
217 if( p_sys->es[i]->psz_language )
218 free( p_sys->es[i]->psz_language );
219 if( p_sys->es[i]->psz_language_code )
220 free( p_sys->es[i]->psz_language_code );
221 es_format_Clean( &p_sys->es[i]->fmt );
223 free( p_sys->es[i] );
225 if( p_sys->ppsz_audio_language )
227 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
228 free( p_sys->ppsz_audio_language[i] );
229 free( p_sys->ppsz_audio_language );
231 if( p_sys->ppsz_sub_language )
233 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
234 free( p_sys->ppsz_sub_language[i] );
235 free( p_sys->ppsz_sub_language );
241 for( i = 0; i < p_sys->i_pgrm; i++ )
243 if( p_sys->pgrm[i]->psz_now_playing )
244 free( p_sys->pgrm[i]->psz_now_playing );
245 free( p_sys->pgrm[i] );
254 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
259 /* Special HACK, -i_id is tha cat of the stream */
260 return (es_out_id_t*)((uint8_t*)NULL-i_id);
263 for( i = 0; i < out->p_sys->i_es; i++ )
265 if( out->p_sys->es[i]->i_id == i_id )
266 return out->p_sys->es[i];
271 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
273 es_out_sys_t *p_sys = out->p_sys;
276 for( i = 0; i < p_sys->i_es; i++ )
278 es_out_id_t *es = p_sys->es[i];
280 /* Send a dummy block to let decoder know that
281 * there is a discontinuity */
282 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
284 input_DecoderDiscontinuity( es->p_dec );
289 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
291 es_out_sys_t *p_sys = out->p_sys;
293 if( i_cat == AUDIO_ES )
294 p_sys->i_audio_delay = i_delay;
295 else if( i_cat == SPU_ES )
296 p_sys->i_spu_delay = i_delay;
299 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
301 es_out_sys_t *p_sys = out->p_sys;
304 for( i = 0; i < p_sys->i_es; i++ )
306 es_out_id_t *es = p_sys->es[i];
308 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
314 /*****************************************************************************
316 *****************************************************************************/
317 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
318 vlc_bool_t b_delete )
320 es_out_sys_t *p_sys = out->p_sys;
321 input_thread_t *p_input = p_sys->p_input;
322 vlc_value_t val, text;
326 if( es->fmt.i_cat == AUDIO_ES )
327 psz_var = "audio-es";
328 else if( es->fmt.i_cat == VIDEO_ES )
329 psz_var = "video-es";
330 else if( es->fmt.i_cat == SPU_ES )
337 val.i_int = es->i_id;
338 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
339 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
343 /* Get the number of ES already added */
344 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
349 /* First one, we need to add the "Disable" choice */
350 val2.i_int = -1; text.psz_string = _("Disable");
351 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
355 /* Take care of the ES description */
356 if( es->fmt.psz_description && *es->fmt.psz_description )
358 if( es->psz_language && *es->psz_language )
360 text.psz_string = malloc( strlen( es->fmt.psz_description) + strlen( es->psz_language ) + 10 );
361 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description, es->psz_language );
363 else text.psz_string = strdup( es->fmt.psz_description );
367 if( es->psz_language && *es->psz_language )
370 text.psz_string = malloc( strlen( _("Track %i") )+ strlen( es->psz_language ) + 30 );
371 asprintf( &temp, _("Track %i"), val.i_int );
372 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
377 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
378 sprintf( text.psz_string, _("Track %i"), val.i_int );
382 val.i_int = es->i_id;
383 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
385 free( text.psz_string );
387 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
390 /* EsOutProgramSelect:
391 * Select a program and update the object variable
393 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
395 es_out_sys_t *p_sys = out->p_sys;
396 input_thread_t *p_input = p_sys->p_input;
400 if( p_sys->p_pgrm == p_pgrm )
401 return; /* Nothing to do */
405 es_out_pgrm_t *old = p_sys->p_pgrm;
406 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
408 for( i = 0; i < p_sys->i_es; i++ )
410 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
411 p_sys->i_mode != ES_OUT_MODE_ALL )
412 EsUnselect( out, p_sys->es[i], VLC_TRUE );
415 p_sys->p_es_audio = NULL;
416 p_sys->p_es_sub = NULL;
417 p_sys->p_es_video = NULL;
420 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
422 /* Mark it selected */
423 p_pgrm->b_selected = VLC_TRUE;
425 /* Switch master stream */
426 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
428 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
430 p_pgrm->clock.b_master = VLC_TRUE;
431 p_sys->p_pgrm = p_pgrm;
433 /* Update "program" */
434 val.i_int = p_pgrm->i_id;
435 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
438 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
439 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
440 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
441 for( i = 0; i < p_sys->i_es; i++ )
443 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
444 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
445 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
448 /* Update now playing if defined per program */
449 if( p_pgrm->psz_now_playing )
451 char *psz_cat = malloc( strlen(_("Program")) + 10 );
453 sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
454 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
455 VLC_META_NOW_PLAYING, "%s", p_pgrm->psz_now_playing );
460 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
466 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
468 es_out_sys_t *p_sys = out->p_sys;
469 input_thread_t *p_input = p_sys->p_input;
472 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
475 p_pgrm->i_id = i_group;
477 p_pgrm->b_selected = VLC_FALSE;
478 p_pgrm->psz_now_playing = NULL;
479 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->input.i_cr_average );
482 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
484 /* Update "program" variable */
486 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
488 if( i_group == var_GetInteger( p_input, "program" ) )
490 EsOutProgramSelect( out, p_pgrm );
494 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
502 static void EsOutProgramDel( es_out_t *out, int i_group )
504 es_out_sys_t *p_sys = out->p_sys;
505 input_thread_t *p_input = p_sys->p_input;
506 es_out_pgrm_t *p_pgrm = NULL;
510 for( i = 0; i < p_sys->i_pgrm; i++ )
512 if( p_sys->pgrm[i]->i_id == i_group )
514 p_pgrm = p_sys->pgrm[i];
519 if( p_pgrm == NULL ) return;
523 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
524 i_group, p_pgrm->i_es );
528 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
530 /* If program is selected we need to unselect it */
531 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;
533 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
536 /* Update "program" variable */
538 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
540 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
545 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
547 es_out_sys_t *p_sys = out->p_sys;
548 es_out_pgrm_t *p_pgrm = NULL;
549 input_thread_t *p_input = p_sys->p_input;
550 char *psz_cat = malloc( strlen(_("Program")) + 10 );
551 char *psz_name = NULL;
552 char *psz_now_playing = NULL;
553 char *psz_provider = NULL;
556 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
557 sprintf( psz_cat, "%s %d", _("Program"), i_group );
559 for( i = 0; i < p_meta->i_meta; i++ )
561 msg_Dbg( p_input, " - %s = %s", p_meta->name[i], p_meta->value[i] );
563 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
564 _(p_meta->name[i]), "%s", p_meta->value[i] );
565 if( !strcasecmp( p_meta->name[i], "Name" ) )
566 psz_name = p_meta->value[i];
567 else if( !strcasecmp( p_meta->name[i], "Provider" ) )
568 psz_provider = p_meta->value[i];
569 else if( !strcasecmp( p_meta->name[i], VLC_META_NOW_PLAYING ) )
570 psz_now_playing = p_meta->value[i];
573 if( !psz_name && !psz_now_playing )
579 for( i = 0; i < p_sys->i_pgrm; i++ )
581 if( p_sys->pgrm[i]->i_id == i_group )
583 p_pgrm = p_sys->pgrm[i];
589 p_pgrm = EsOutProgramAdd( out, i_group );
591 /* Update the description text of the program */
592 if( psz_name && *psz_name )
597 /* ugly but it works */
599 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
601 if( psz_provider && *psz_provider )
603 asprintf( &text.psz_string, "%s [%s]", psz_name, psz_provider );
604 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
605 free( text.psz_string );
609 text.psz_string = psz_name;
610 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
613 if( psz_now_playing )
615 p_pgrm->psz_now_playing = strdup(psz_now_playing);
617 if( p_sys->p_pgrm == p_pgrm )
619 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
620 VLC_META_NOW_PLAYING, "%s", psz_now_playing );
629 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
631 es_out_sys_t *p_sys = out->p_sys;
632 input_thread_t *p_input = p_sys->p_input;
634 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
635 es_out_pgrm_t *p_pgrm = NULL;
638 if( fmt->i_group < 0 )
640 msg_Err( p_input, "invalid group number" );
644 /* Search the program */
645 for( i = 0; i < p_sys->i_pgrm; i++ )
647 if( fmt->i_group == p_sys->pgrm[i]->i_id )
649 p_pgrm = p_sys->pgrm[i];
655 /* Create a new one */
656 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
659 /* Increase ref count for program */
664 fmt->i_id = out->p_sys->i_id;
665 es->i_id = fmt->i_id;
667 es_format_Copy( &es->fmt, fmt );
668 es->i_preroll_end = -1;
673 es->i_channel = p_sys->i_audio;
677 es->i_channel = p_sys->i_video;
681 es->i_channel = p_sys->i_sub;
688 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
689 es->psz_language_code = LanguageGetCode( fmt->psz_language );
692 if( es->p_pgrm == p_sys->p_pgrm )
693 EsOutESVarUpdate( out, es, VLC_FALSE );
695 /* Select it if needed */
696 EsOutSelect( out, es, VLC_FALSE );
699 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
700 p_sys->i_id++; /* always incremented */
714 EsOutAddInfo( out, es );
719 static void EsSelect( es_out_t *out, es_out_id_t *es )
721 es_out_sys_t *p_sys = out->p_sys;
722 input_thread_t *p_input = p_sys->p_input;
728 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
732 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
734 if( !var_GetBool( p_input, "video" ) ||
735 ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
737 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
742 else if( es->fmt.i_cat == AUDIO_ES )
744 var_Get( p_input, "audio", &val );
745 if( !var_GetBool( p_input, "audio" ) ||
746 ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
748 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
754 es->i_preroll_end = -1;
755 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
756 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
759 if( es->fmt.i_cat == VIDEO_ES )
760 psz_var = "video-es";
761 else if( es->fmt.i_cat == AUDIO_ES )
762 psz_var = "audio-es";
763 else if( es->fmt.i_cat == SPU_ES )
768 /* Mark it as selected */
769 val.i_int = es->i_id;
770 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
773 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
776 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
778 es_out_sys_t *p_sys = out->p_sys;
779 input_thread_t *p_input = p_sys->p_input;
783 if( es->p_dec == NULL )
785 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
789 input_DecoderDelete( es->p_dec );
796 if( es->p_dec == NULL )
798 if( es->fmt.i_cat == VIDEO_ES )
799 psz_var = "video-es";
800 else if( es->fmt.i_cat == AUDIO_ES )
801 psz_var = "audio-es";
802 else if( es->fmt.i_cat == SPU_ES )
807 /* Mark it as selected */
809 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
811 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
815 * Select an ES given the current mode
816 * XXX: you need to take a the lock before (stream.stream_lock)
818 * \param out The es_out structure
819 * \param es es_out_id structure
823 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
825 es_out_sys_t *p_sys = out->p_sys;
827 int i_cat = es->fmt.i_cat;
829 if( !p_sys->b_active ||
830 ( !b_force && es->fmt.i_priority < 0 ) )
835 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
840 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
844 var_Get( p_sys->p_input, "programs", &val );
845 for ( i = 0; i < val.p_list->i_count; i++ )
847 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
854 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
856 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
860 if( es->p_pgrm != p_sys->p_pgrm )
863 if( i_cat == AUDIO_ES )
865 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
866 es->psz_language_code );
868 if( p_sys->p_es_audio &&
869 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
871 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
872 p_sys->p_es_audio->psz_language_code );
874 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
876 i_wanted = es->i_channel;
880 /* Select audio if (no audio selected yet)
881 * - no audio-language
882 * - no audio code for the ES
883 * - audio code in the requested list */
885 !strcmp( es->psz_language_code, "??" ) ||
886 !p_sys->ppsz_audio_language )
887 i_wanted = es->i_channel;
890 if( p_sys->i_audio_last >= 0 )
891 i_wanted = p_sys->i_audio_last;
893 else if( i_cat == SPU_ES )
895 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
896 es->psz_language_code );
898 if( p_sys->p_es_sub &&
899 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
901 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
902 p_sys->p_es_sub->psz_language_code );
904 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
905 idx1, es->psz_language_code, idx2,
906 p_sys->p_es_sub->psz_language_code );
908 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
910 /* We found a SPU that matches our language request */
911 i_wanted = es->i_channel;
915 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
916 idx1, es->psz_language_code );
918 i_wanted = es->i_channel;
920 if( p_sys->i_sub_last >= 0 )
921 i_wanted = p_sys->i_sub_last;
923 else if( i_cat == VIDEO_ES )
925 i_wanted = es->i_channel;
928 if( i_wanted == es->i_channel && es->p_dec == NULL )
932 /* FIXME TODO handle priority here */
935 if( i_cat == AUDIO_ES )
937 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
939 p_sys->p_es_audio != es &&
940 p_sys->p_es_audio->p_dec )
942 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
944 p_sys->p_es_audio = es;
946 else if( i_cat == SPU_ES )
948 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
950 p_sys->p_es_sub != es &&
951 p_sys->p_es_sub->p_dec )
953 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
955 p_sys->p_es_sub = es;
957 else if( i_cat == VIDEO_ES )
959 p_sys->p_es_video = es;
965 * Send a block for the given es_out
967 * \param out the es_out to send from
968 * \param es the es_out_id
969 * \param p_block the data block to send
971 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
973 es_out_sys_t *p_sys = out->p_sys;
974 input_thread_t *p_input = p_sys->p_input;
975 es_out_pgrm_t *p_pgrm = es->p_pgrm;
978 if( es->fmt.i_cat == AUDIO_ES )
979 i_delay = p_sys->i_audio_delay;
980 else if( es->fmt.i_cat == SPU_ES )
981 i_delay = p_sys->i_spu_delay;
985 /* Mark preroll blocks */
986 if( es->i_preroll_end >= 0 )
988 int64_t i_date = p_block->i_pts;
990 i_date = p_block->i_dts;
992 if( i_date < es->i_preroll_end )
993 p_block->i_flags |= BLOCK_FLAG_PREROLL;
995 es->i_preroll_end = -1;
998 /* +11 -> avoid null value with non null dts/pts */
999 if( p_block->i_dts > 0 )
1002 input_ClockGetTS( p_input, &p_pgrm->clock,
1003 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
1005 if( p_block->i_pts > 0 )
1008 input_ClockGetTS( p_input, &p_pgrm->clock,
1009 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
1011 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1013 mtime_t current_date = mdate();
1015 || p_block->i_pts > current_date + 10000000
1016 || current_date > p_block->i_pts )
1018 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1019 * for teletext streams. */
1020 p_block->i_pts = current_date + 400000
1021 + p_input->i_pts_delay + i_delay;
1025 p_block->i_rate = p_input->i_rate;
1027 /* TODO handle mute */
1028 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
1029 p_input->i_rate == INPUT_RATE_DEFAULT ) )
1031 input_DecoderDecode( es->p_dec, p_block );
1035 block_Release( p_block );
1041 /*****************************************************************************
1043 *****************************************************************************/
1044 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1046 es_out_sys_t *p_sys = out->p_sys;
1048 /* We don't try to reselect */
1050 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1052 if( es->p_pgrm == p_sys->p_pgrm )
1053 EsOutESVarUpdate( out, es, VLC_TRUE );
1055 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1058 if( es->p_pgrm->i_es == 0 )
1060 msg_Warn( p_sys->p_input, "Program doesn't contain anymore ES, "
1061 "TODO cleaning ?" );
1064 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1065 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1066 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1068 switch( es->fmt.i_cat )
1081 if( es->psz_language )
1082 free( es->psz_language );
1083 if( es->psz_language_code )
1084 free( es->psz_language_code );
1086 es_format_Clean( &es->fmt );
1092 * Control query handler
1094 * \param out the es_out to control
1095 * \param i_query A es_out query as defined in include/ninput.h
1096 * \param args a variable list of arguments for the query
1097 * \return VLC_SUCCESS or an error code
1099 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1101 es_out_sys_t *p_sys = out->p_sys;
1109 case ES_OUT_SET_ES_STATE:
1110 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1111 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1112 if( b && es->p_dec == NULL )
1114 EsSelect( out, es );
1115 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1117 else if( !b && es->p_dec )
1119 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1124 case ES_OUT_GET_ES_STATE:
1125 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1126 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1128 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1131 case ES_OUT_SET_ACTIVE:
1133 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1134 p_sys->b_active = b;
1137 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1141 case ES_OUT_GET_ACTIVE:
1142 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1143 *pb = p_sys->b_active;
1146 case ES_OUT_SET_MODE:
1147 i = (int) va_arg( args, int );
1148 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1149 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1153 /* Reapply policy mode */
1154 for( i = 0; i < p_sys->i_es; i++ )
1156 if( p_sys->es[i]->p_dec )
1158 EsUnselect( out, p_sys->es[i],
1159 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1162 for( i = 0; i < p_sys->i_es; i++ )
1164 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1168 return VLC_EGENERIC;
1170 case ES_OUT_GET_MODE:
1171 pi = (int*) va_arg( args, int* );
1172 *pi = p_sys->i_mode;
1176 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1177 /* Special case NULL, NULL+i_cat */
1180 for( i = 0; i < p_sys->i_es; i++ )
1182 if( p_sys->es[i]->p_dec )
1183 EsUnselect( out, p_sys->es[i],
1184 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1187 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1189 for( i = 0; i < p_sys->i_es; i++ )
1191 if( p_sys->es[i]->p_dec &&
1192 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1193 EsUnselect( out, p_sys->es[i],
1194 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1197 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1199 for( i = 0; i < p_sys->i_es; i++ )
1201 if( p_sys->es[i]->p_dec &&
1202 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1203 EsUnselect( out, p_sys->es[i],
1204 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1207 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1209 for( i = 0; i < p_sys->i_es; i++ )
1211 if( p_sys->es[i]->p_dec &&
1212 p_sys->es[i]->fmt.i_cat == SPU_ES )
1213 EsUnselect( out, p_sys->es[i],
1214 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1219 for( i = 0; i < p_sys->i_es; i++ )
1221 if( es == p_sys->es[i] )
1223 EsOutSelect( out, es, VLC_TRUE );
1230 case ES_OUT_SET_PCR:
1231 case ES_OUT_SET_GROUP_PCR:
1233 es_out_pgrm_t *p_pgrm = NULL;
1237 if( i_query == ES_OUT_SET_PCR )
1239 p_pgrm = p_sys->p_pgrm;
1244 i_group = (int)va_arg( args, int );
1245 for( i = 0; i < p_sys->i_pgrm; i++ )
1247 if( p_sys->pgrm[i]->i_id == i_group )
1249 p_pgrm = p_sys->pgrm[i];
1254 if( p_pgrm == NULL )
1255 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1257 i_pcr = (int64_t)va_arg( args, int64_t );
1258 /* search program */
1259 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1260 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1261 (i_pcr + 11 ) * 9 / 100);
1265 case ES_OUT_RESET_PCR:
1266 for( i = 0; i < p_sys->i_pgrm; i++ )
1268 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1269 p_sys->pgrm[i]->clock.last_pts = 0;
1276 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1277 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1278 *pi_ts = input_ClockGetTS( p_sys->p_input,
1279 &p_sys->p_pgrm->clock,
1280 ( i_ts + 11 ) * 9 / 100 );
1283 return VLC_EGENERIC;
1285 case ES_OUT_GET_GROUP:
1286 pi = (int*) va_arg( args, int* );
1288 *pi = p_sys->p_pgrm->i_id;
1290 *pi = -1; /* FIXME */
1293 case ES_OUT_SET_GROUP:
1296 i = (int) va_arg( args, int );
1297 for( j = 0; j < p_sys->i_pgrm; j++ )
1299 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1300 if( p_pgrm->i_id == i )
1302 EsOutProgramSelect( out, p_pgrm );
1306 return VLC_EGENERIC;
1309 case ES_OUT_SET_FMT:
1311 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1312 * to update the p_extra data */
1314 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1315 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1316 if( es == NULL ) return VLC_EGENERIC;
1318 if( p_fmt->i_extra )
1320 es->fmt.i_extra = p_fmt->i_extra;
1321 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1322 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1324 if( !es->p_dec ) return VLC_SUCCESS;
1327 input_DecoderDelete( es->p_dec );
1328 es->p_dec = input_DecoderNew( p_sys->p_input,
1329 &es->fmt, VLC_FALSE );
1332 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1333 es->p_dec->fmt_in.p_extra =
1334 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1335 memcpy( es->p_dec->fmt_in.p_extra,
1336 p_fmt->p_extra, p_fmt->i_extra );
1343 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1347 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1348 i_date = (int64_t)va_arg( args, int64_t );
1350 if( !es || !es->p_dec )
1351 return VLC_EGENERIC;
1353 es->i_preroll_end = i_date;
1354 input_DecoderPreroll( es->p_dec, i_date );
1358 case ES_OUT_SET_GROUP_META:
1360 int i_group = (int)va_arg( args, int );
1361 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1363 EsOutProgramMeta( out, i_group, p_meta );
1366 case ES_OUT_DEL_GROUP:
1368 int i_group = (int)va_arg( args, int );
1370 return EsOutProgramDel( out, i_group );
1374 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1375 return VLC_EGENERIC;
1379 /****************************************************************************
1380 * LanguageGetName: try to expend iso639 into plain name
1381 ****************************************************************************/
1382 static char *LanguageGetName( const char *psz_code )
1384 const iso639_lang_t *pl;
1386 if( psz_code == NULL )
1388 return strdup( "" );
1391 if( strlen( psz_code ) == 2 )
1393 pl = GetLang_1( psz_code );
1395 else if( strlen( psz_code ) == 3 )
1397 pl = GetLang_2B( psz_code );
1398 if( !strcmp( pl->psz_iso639_1, "??" ) )
1400 pl = GetLang_2T( psz_code );
1405 return strdup( psz_code );
1408 if( !strcmp( pl->psz_iso639_1, "??" ) )
1410 return strdup( psz_code );
1414 if( *pl->psz_native_name )
1416 return strdup( pl->psz_native_name );
1418 return strdup( pl->psz_eng_name );
1422 /* Get a 2 char code */
1423 static char *LanguageGetCode( const char *psz_lang )
1425 const iso639_lang_t *pl;
1427 if( psz_lang == NULL || *psz_lang == '\0' )
1428 return strdup("??");
1430 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1432 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1433 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1434 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1435 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1436 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1440 if( pl->psz_iso639_1 != NULL )
1441 return strdup( pl->psz_iso639_1 );
1443 return strdup("??");
1446 static char **LanguageSplit( const char *psz_langs )
1453 if( psz_langs == NULL ) return NULL;
1455 psz_parser = psz_dup = strdup(psz_langs);
1457 while( psz_parser && *psz_parser )
1462 psz = strchr(psz_parser, ',' );
1463 if( psz ) *psz++ = '\0';
1465 psz_code = LanguageGetCode( psz_parser );
1466 if( strcmp( psz_code, "??" ) )
1468 TAB_APPEND( i_psz, ppsz, psz_code );
1476 TAB_APPEND( i_psz, ppsz, NULL );
1483 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1487 if( !ppsz_langs || !psz_lang ) return -1;
1489 for( i = 0; ppsz_langs[i]; i++ )
1490 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1495 /****************************************************************************
1497 * - add meta info to the playlist item
1498 ****************************************************************************/
1499 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1501 es_out_sys_t *p_sys = out->p_sys;
1502 input_thread_t *p_input = p_sys->p_input;
1503 es_format_t *fmt = &es->fmt;
1506 /* Add stream info */
1507 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1509 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1510 "%.4s", (char*)&fmt->i_codec );
1512 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1513 "%s", es->psz_language );
1515 /* Add information */
1516 switch( fmt->i_cat )
1519 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1520 _("Type"), _("Audio") );
1522 if( fmt->audio.i_channels > 0 )
1523 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1524 "%d", fmt->audio.i_channels );
1526 if( fmt->audio.i_rate > 0 )
1527 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1528 _("%d Hz"), fmt->audio.i_rate );
1530 if( fmt->audio.i_bitspersample > 0 )
1531 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1532 _("Bits per sample"), "%d",
1533 fmt->audio.i_bitspersample );
1535 if( fmt->i_bitrate > 0 )
1536 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1537 _("%d kb/s"), fmt->i_bitrate / 1000 );
1541 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1542 _("Type"), _("Video") );
1544 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1545 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1546 _("Resolution"), "%dx%d",
1547 fmt->video.i_width, fmt->video.i_height );
1549 if( fmt->video.i_visible_width > 0 &&
1550 fmt->video.i_visible_height > 0 )
1551 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1552 _("Display resolution"), "%dx%d",
1553 fmt->video.i_visible_width,
1554 fmt->video.i_visible_height);
1555 if( fmt->video.i_frame_rate > 0 &&
1556 fmt->video.i_frame_rate_base > 0 )
1557 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1558 _("Frame rate"), "%f",
1559 (float)fmt->video.i_frame_rate /
1560 fmt->video.i_frame_rate_base );
1564 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1565 _("Type"), _("Subtitle") );