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 *****************************************************************************/
33 #include <vlc/input.h>
34 #include <vlc/decoder.h>
36 #include "input_internal.h"
38 #include "vlc_playlist.h"
40 /* FIXME we should find a better way than including that */
41 #include "../misc/iso-639_def.h"
43 /*****************************************************************************
45 *****************************************************************************/
51 /* Number of es for this pgrm */
54 vlc_bool_t b_selected;
56 /* Clock for this program */
59 char *psz_now_playing;
67 es_out_pgrm_t *p_pgrm;
69 /* Signal a discontinuity in the timeline for every PID */
70 vlc_bool_t b_discontinuity;
73 int64_t i_preroll_end;
75 /* Channel in the track type */
79 char *psz_language_code;
85 input_thread_t *p_input;
90 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
91 es_out_pgrm_t *p_pgrm; /* Master program */
108 int i_audio_last, i_audio_id;
109 int i_sub_last, i_sub_id;
110 char **ppsz_audio_language;
111 char **ppsz_sub_language;
113 /* current main es */
114 es_out_id_t *p_es_audio;
115 es_out_id_t *p_es_video;
116 es_out_id_t *p_es_sub;
119 int64_t i_audio_delay;
123 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
124 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
125 static void EsOutDel ( es_out_t *, es_out_id_t * );
126 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
127 static int EsOutControl( es_out_t *, int i_query, va_list );
129 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
131 static void EsSelect( es_out_t *out, es_out_id_t *es );
132 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
133 static char *LanguageGetName( const char *psz_code );
134 static char *LanguageGetCode( const char *psz_lang );
135 static char **LanguageSplit( const char *psz_langs );
136 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
138 /*****************************************************************************
140 *****************************************************************************/
141 es_out_t *input_EsOutNew( input_thread_t *p_input )
143 es_out_t *out = malloc( sizeof( es_out_t ) );
144 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
148 out->pf_add = EsOutAdd;
149 out->pf_send = EsOutSend;
150 out->pf_del = EsOutDel;
151 out->pf_control = EsOutControl;
154 p_sys->p_input = p_input;
156 p_sys->b_active = VLC_FALSE;
157 p_sys->i_mode = ES_OUT_MODE_AUTO;
162 p_sys->p_pgrm = NULL;
173 var_Get( p_input, "audio-track", &val );
174 p_sys->i_audio_last = val.i_int;
176 var_Get( p_input, "sub-track", &val );
177 p_sys->i_sub_last = val.i_int;
179 var_Get( p_input, "audio-language", &val );
180 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
181 if( p_sys->ppsz_audio_language )
183 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
184 msg_Dbg( p_input, "selected audio language[%d] %s",
185 i, p_sys->ppsz_audio_language[i] );
187 if( val.psz_string ) free( val.psz_string );
189 var_Get( p_input, "sub-language", &val );
190 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
191 if( p_sys->ppsz_sub_language )
193 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
194 msg_Dbg( p_input, "selected subtitle language[%d] %s",
195 i, p_sys->ppsz_sub_language[i] );
197 if( val.psz_string ) free( val.psz_string );
199 var_Get( p_input, "audio-track-id", &val );
200 p_sys->i_audio_id = val.i_int;
202 var_Get( p_input, "sub-track-id", &val );
203 p_sys->i_sub_id = val.i_int;
205 p_sys->p_es_audio = NULL;
206 p_sys->p_es_video = NULL;
207 p_sys->p_es_sub = NULL;
209 p_sys->i_audio_delay= 0;
210 p_sys->i_spu_delay = 0;
215 /*****************************************************************************
217 *****************************************************************************/
218 void input_EsOutDelete( es_out_t *out )
220 es_out_sys_t *p_sys = out->p_sys;
223 for( i = 0; i < p_sys->i_es; i++ )
225 if( p_sys->es[i]->p_dec )
227 input_DecoderDelete( p_sys->es[i]->p_dec );
229 if( p_sys->es[i]->psz_language )
230 free( p_sys->es[i]->psz_language );
231 if( p_sys->es[i]->psz_language_code )
232 free( p_sys->es[i]->psz_language_code );
233 es_format_Clean( &p_sys->es[i]->fmt );
235 free( p_sys->es[i] );
237 if( p_sys->ppsz_audio_language )
239 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
240 free( p_sys->ppsz_audio_language[i] );
241 free( p_sys->ppsz_audio_language );
243 if( p_sys->ppsz_sub_language )
245 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
246 free( p_sys->ppsz_sub_language[i] );
247 free( p_sys->ppsz_sub_language );
253 for( i = 0; i < p_sys->i_pgrm; i++ )
255 if( p_sys->pgrm[i]->psz_now_playing )
256 free( p_sys->pgrm[i]->psz_now_playing );
257 free( p_sys->pgrm[i] );
266 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
271 /* Special HACK, -i_id is tha cat of the stream */
272 return (es_out_id_t*)((uint8_t*)NULL-i_id);
275 for( i = 0; i < out->p_sys->i_es; i++ )
277 if( out->p_sys->es[i]->i_id == i_id )
278 return out->p_sys->es[i];
283 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
285 es_out_sys_t *p_sys = out->p_sys;
288 for( i = 0; i < p_sys->i_es; i++ )
290 es_out_id_t *es = p_sys->es[i];
291 es->b_discontinuity = VLC_TRUE; /* signal discontinuity */
293 /* Send a dummy block to let decoder know that
294 * there is a discontinuity */
295 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
297 input_DecoderDiscontinuity( es->p_dec );
302 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
304 es_out_sys_t *p_sys = out->p_sys;
306 if( i_cat == AUDIO_ES )
307 p_sys->i_audio_delay = i_delay;
308 else if( i_cat == SPU_ES )
309 p_sys->i_spu_delay = i_delay;
312 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
314 es_out_sys_t *p_sys = out->p_sys;
317 for( i = 0; i < p_sys->i_es; i++ )
319 es_out_id_t *es = p_sys->es[i];
321 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
327 /*****************************************************************************
329 *****************************************************************************/
330 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
331 vlc_bool_t b_delete )
333 es_out_sys_t *p_sys = out->p_sys;
334 input_thread_t *p_input = p_sys->p_input;
335 vlc_value_t val, text;
339 if( es->fmt.i_cat == AUDIO_ES )
340 psz_var = "audio-es";
341 else if( es->fmt.i_cat == VIDEO_ES )
342 psz_var = "video-es";
343 else if( es->fmt.i_cat == SPU_ES )
350 val.i_int = es->i_id;
351 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
352 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
356 /* Get the number of ES already added */
357 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
362 /* First one, we need to add the "Disable" choice */
363 val2.i_int = -1; text.psz_string = _("Disable");
364 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
368 /* Take care of the ES description */
369 if( es->fmt.psz_description && *es->fmt.psz_description )
371 if( es->psz_language && *es->psz_language )
373 text.psz_string = malloc( strlen( es->fmt.psz_description) +
374 strlen( es->psz_language ) + 10 );
375 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description,
378 else text.psz_string = strdup( es->fmt.psz_description );
382 if( es->psz_language && *es->psz_language )
385 text.psz_string = malloc( strlen( _("Track %i") )+
386 strlen( es->psz_language ) + 30 );
387 asprintf( &temp, _("Track %i"), val.i_int );
388 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
393 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
394 sprintf( text.psz_string, _("Track %i"), val.i_int );
398 val.i_int = es->i_id;
399 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
401 free( text.psz_string );
403 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
406 /* EsOutProgramSelect:
407 * Select a program and update the object variable
409 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
411 es_out_sys_t *p_sys = out->p_sys;
412 input_thread_t *p_input = p_sys->p_input;
416 if( p_sys->p_pgrm == p_pgrm )
417 return; /* Nothing to do */
421 es_out_pgrm_t *old = p_sys->p_pgrm;
422 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
424 for( i = 0; i < p_sys->i_es; i++ )
426 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
427 p_sys->i_mode != ES_OUT_MODE_ALL )
428 EsUnselect( out, p_sys->es[i], VLC_TRUE );
431 p_sys->p_es_audio = NULL;
432 p_sys->p_es_sub = NULL;
433 p_sys->p_es_video = NULL;
436 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
438 /* Mark it selected */
439 p_pgrm->b_selected = VLC_TRUE;
441 /* Switch master stream */
442 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
444 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
446 p_pgrm->clock.b_master = VLC_TRUE;
447 p_sys->p_pgrm = p_pgrm;
449 /* Update "program" */
450 val.i_int = p_pgrm->i_id;
451 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
454 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
455 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
456 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
457 for( i = 0; i < p_sys->i_es; i++ )
459 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
460 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
461 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
464 /* Update now playing if defined per program */
465 if( p_pgrm->psz_now_playing )
467 char *psz_cat = malloc( strlen(_("Program")) + 10 );
469 sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
470 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT),
471 _(VLC_META_NOW_PLAYING), "%s", p_pgrm->psz_now_playing );
476 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
482 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
484 es_out_sys_t *p_sys = out->p_sys;
485 input_thread_t *p_input = p_sys->p_input;
488 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
491 p_pgrm->i_id = i_group;
493 p_pgrm->b_selected = VLC_FALSE;
494 p_pgrm->psz_now_playing = NULL;
495 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->input.i_cr_average );
498 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
500 /* Update "program" variable */
502 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
504 if( i_group == var_GetInteger( p_input, "program" ) )
506 EsOutProgramSelect( out, p_pgrm );
510 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
518 static int EsOutProgramDel( es_out_t *out, int i_group )
520 es_out_sys_t *p_sys = out->p_sys;
521 input_thread_t *p_input = p_sys->p_input;
522 es_out_pgrm_t *p_pgrm = NULL;
526 for( i = 0; i < p_sys->i_pgrm; i++ )
528 if( p_sys->pgrm[i]->i_id == i_group )
530 p_pgrm = p_sys->pgrm[i];
540 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
541 i_group, p_pgrm->i_es );
545 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
547 /* If program is selected we need to unselect it */
548 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;
550 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
553 /* Update "program" variable */
555 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
557 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
564 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
566 es_out_sys_t *p_sys = out->p_sys;
567 es_out_pgrm_t *p_pgrm = NULL;
568 input_thread_t *p_input = p_sys->p_input;
569 char *psz_cat = malloc( strlen(_("Program")) + 10 );
570 char *psz_title = NULL;
571 char *psz_now_playing = NULL;
572 char *psz_provider = NULL;
575 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
576 sprintf( psz_cat, "%s %d", _("Program"), i_group );
578 if( p_meta->psz_title ) psz_title = p_meta->psz_title;
579 if( p_meta->psz_publisher ) psz_provider = p_meta->psz_publisher;
580 if( p_meta->psz_nowplaying ) psz_now_playing = p_meta->psz_nowplaying;
582 if( !psz_title && !psz_now_playing )
588 for( i = 0; i < p_sys->i_pgrm; i++ )
590 if( p_sys->pgrm[i]->i_id == i_group )
592 p_pgrm = p_sys->pgrm[i];
598 p_pgrm = EsOutProgramAdd( out, i_group );
600 /* Update the description text of the program */
601 if( psz_title && *psz_title )
606 /* ugly but it works */
608 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
610 if( psz_provider && *psz_provider )
612 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
613 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
614 free( text.psz_string );
618 text.psz_string = psz_title;
619 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
622 if( psz_now_playing )
624 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
625 p_pgrm->psz_now_playing = strdup(psz_now_playing);
627 if( p_sys->p_pgrm == p_pgrm )
629 vlc_meta_SetNowPlaying( p_input->input.p_item->p_meta,
639 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
641 es_out_sys_t *p_sys = out->p_sys;
642 input_thread_t *p_input = p_sys->p_input;
644 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
645 es_out_pgrm_t *p_pgrm = NULL;
648 if( fmt->i_group < 0 )
650 msg_Err( p_input, "invalid group number" );
654 /* Search the program */
655 for( i = 0; i < p_sys->i_pgrm; i++ )
657 if( fmt->i_group == p_sys->pgrm[i]->i_id )
659 p_pgrm = p_sys->pgrm[i];
665 /* Create a new one */
666 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
669 /* Increase ref count for program */
674 fmt->i_id = out->p_sys->i_id;
675 es->i_id = fmt->i_id;
677 es_format_Copy( &es->fmt, fmt );
678 es->i_preroll_end = -1;
679 es->b_discontinuity = VLC_FALSE;
684 es->i_channel = p_sys->i_audio;
688 es->i_channel = p_sys->i_video;
689 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
690 vlc_ureduce( &es->fmt.video.i_frame_rate,
691 &es->fmt.video.i_frame_rate_base,
692 fmt->video.i_frame_rate,
693 fmt->video.i_frame_rate_base, 0 );
697 es->i_channel = p_sys->i_sub;
704 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
705 es->psz_language_code = LanguageGetCode( fmt->psz_language );
708 if( es->p_pgrm == p_sys->p_pgrm )
709 EsOutESVarUpdate( out, es, VLC_FALSE );
711 /* Select it if needed */
712 EsOutSelect( out, es, VLC_FALSE );
715 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
716 p_sys->i_id++; /* always incremented */
730 EsOutAddInfo( out, es );
735 static void EsSelect( es_out_t *out, es_out_id_t *es )
737 es_out_sys_t *p_sys = out->p_sys;
738 input_thread_t *p_input = p_sys->p_input;
744 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
748 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
750 if( !var_GetBool( p_input, "video" ) ||
751 ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
753 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
758 else if( es->fmt.i_cat == AUDIO_ES )
760 var_Get( p_input, "audio", &val );
761 if( !var_GetBool( p_input, "audio" ) ||
762 ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
764 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
769 if( es->fmt.i_cat == SPU_ES )
771 var_Get( p_input, "spu", &val );
772 if( !var_GetBool( p_input, "spu" ) ||
773 ( p_input->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
775 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
781 es->i_preroll_end = -1;
782 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
783 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
786 if( es->fmt.i_cat == VIDEO_ES )
787 psz_var = "video-es";
788 else if( es->fmt.i_cat == AUDIO_ES )
789 psz_var = "audio-es";
790 else if( es->fmt.i_cat == SPU_ES )
795 /* Mark it as selected */
796 val.i_int = es->i_id;
797 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
800 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
803 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
805 es_out_sys_t *p_sys = out->p_sys;
806 input_thread_t *p_input = p_sys->p_input;
810 if( es->p_dec == NULL )
812 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
816 input_DecoderDelete( es->p_dec );
823 if( es->p_dec == NULL )
825 if( es->fmt.i_cat == VIDEO_ES )
826 psz_var = "video-es";
827 else if( es->fmt.i_cat == AUDIO_ES )
828 psz_var = "audio-es";
829 else if( es->fmt.i_cat == SPU_ES )
834 /* Mark it as selected */
836 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
838 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
842 * Select an ES given the current mode
843 * XXX: you need to take a the lock before (stream.stream_lock)
845 * \param out The es_out structure
846 * \param es es_out_id structure
850 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
852 es_out_sys_t *p_sys = out->p_sys;
854 int i_cat = es->fmt.i_cat;
856 if( !p_sys->b_active ||
857 ( !b_force && es->fmt.i_priority < 0 ) )
862 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
867 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
871 var_Get( p_sys->p_input, "programs", &val );
872 for ( i = 0; i < val.p_list->i_count; i++ )
874 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
881 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
883 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
887 if( es->p_pgrm != p_sys->p_pgrm )
890 if( i_cat == AUDIO_ES )
892 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
893 es->psz_language_code );
895 if( p_sys->p_es_audio &&
896 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
898 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
899 p_sys->p_es_audio->psz_language_code );
901 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
903 i_wanted = es->i_channel;
907 /* Select audio if (no audio selected yet)
908 * - no audio-language
909 * - no audio code for the ES
910 * - audio code in the requested list */
912 !strcmp( es->psz_language_code, "??" ) ||
913 !p_sys->ppsz_audio_language )
914 i_wanted = es->i_channel;
917 if( p_sys->i_audio_last >= 0 )
918 i_wanted = p_sys->i_audio_last;
920 if( p_sys->i_audio_id >= 0 )
922 if( es->i_id == p_sys->i_audio_id )
923 i_wanted = es->i_channel;
928 else if( i_cat == SPU_ES )
930 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
931 es->psz_language_code );
933 if( p_sys->p_es_sub &&
934 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
936 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
937 p_sys->p_es_sub->psz_language_code );
939 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
940 idx1, es->psz_language_code, idx2,
941 p_sys->p_es_sub->psz_language_code );
943 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
945 /* We found a SPU that matches our language request */
946 i_wanted = es->i_channel;
950 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
951 idx1, es->psz_language_code );
953 i_wanted = es->i_channel;
955 if( p_sys->i_sub_last >= 0 )
956 i_wanted = p_sys->i_sub_last;
958 if( p_sys->i_sub_id >= 0 )
960 if( es->i_id == p_sys->i_sub_id )
961 i_wanted = es->i_channel;
966 else if( i_cat == VIDEO_ES )
968 i_wanted = es->i_channel;
971 if( i_wanted == es->i_channel && es->p_dec == NULL )
975 /* FIXME TODO handle priority here */
978 if( i_cat == AUDIO_ES )
980 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
982 p_sys->p_es_audio != es &&
983 p_sys->p_es_audio->p_dec )
985 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
987 p_sys->p_es_audio = es;
989 else if( i_cat == SPU_ES )
991 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
993 p_sys->p_es_sub != es &&
994 p_sys->p_es_sub->p_dec )
996 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
998 p_sys->p_es_sub = es;
1000 else if( i_cat == VIDEO_ES )
1002 p_sys->p_es_video = es;
1008 * Send a block for the given es_out
1010 * \param out the es_out to send from
1011 * \param es the es_out_id
1012 * \param p_block the data block to send
1014 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1016 es_out_sys_t *p_sys = out->p_sys;
1017 input_thread_t *p_input = p_sys->p_input;
1018 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1022 if( es->fmt.i_cat == AUDIO_ES )
1023 i_delay = p_sys->i_audio_delay;
1024 else if( es->fmt.i_cat == SPU_ES )
1025 i_delay = p_sys->i_spu_delay;
1029 if( p_input->p_libvlc_global->b_stats )
1031 vlc_mutex_lock( &p_input->counters.counters_lock );
1032 stats_UpdateInteger( p_input, p_input->counters.p_demux_read,
1033 p_block->i_buffer, &i_total );
1034 stats_UpdateFloat( p_input , p_input->counters.p_demux_bitrate,
1035 (float)i_total, NULL );
1036 vlc_mutex_unlock( &p_input->counters.counters_lock );
1039 /* Mark preroll blocks */
1040 if( es->i_preroll_end >= 0 )
1042 int64_t i_date = p_block->i_pts;
1044 i_date = p_block->i_dts;
1046 if( i_date < es->i_preroll_end )
1047 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1049 es->i_preroll_end = -1;
1052 /* +11 -> avoid null value with non null dts/pts */
1053 if( p_block->i_dts > 0 )
1056 input_ClockGetTS( p_input, &p_pgrm->clock,
1057 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
1059 if( p_block->i_pts > 0 )
1062 input_ClockGetTS( p_input, &p_pgrm->clock,
1063 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
1065 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1067 mtime_t current_date = mdate();
1069 || p_block->i_pts > current_date + 10000000
1070 || current_date > p_block->i_pts )
1072 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1073 * for teletext streams. */
1074 p_block->i_pts = current_date + 400000
1075 + p_input->i_pts_delay + i_delay;
1079 p_block->i_rate = p_input->i_rate;
1081 /* TODO handle mute */
1082 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
1083 p_input->i_rate == INPUT_RATE_DEFAULT ) )
1085 input_DecoderDecode( es->p_dec, p_block );
1089 block_Release( p_block );
1095 /*****************************************************************************
1097 *****************************************************************************/
1098 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1100 es_out_sys_t *p_sys = out->p_sys;
1101 vlc_bool_t b_reselect = VLC_FALSE;
1104 /* We don't try to reselect */
1106 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1108 if( es->p_pgrm == p_sys->p_pgrm )
1109 EsOutESVarUpdate( out, es, VLC_TRUE );
1111 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1114 if( es->p_pgrm->i_es == 0 )
1116 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1119 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1120 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1122 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1123 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1124 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1126 switch( es->fmt.i_cat )
1139 /* Re-select another track when needed */
1141 for( i = 0; i < p_sys->i_es; i++ )
1143 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1144 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1147 if( es->psz_language )
1148 free( es->psz_language );
1149 if( es->psz_language_code )
1150 free( es->psz_language_code );
1152 es_format_Clean( &es->fmt );
1158 * Control query handler
1160 * \param out the es_out to control
1161 * \param i_query A es_out query as defined in include/ninput.h
1162 * \param args a variable list of arguments for the query
1163 * \return VLC_SUCCESS or an error code
1165 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1167 es_out_sys_t *p_sys = out->p_sys;
1175 case ES_OUT_SET_ES_STATE:
1176 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1177 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1178 if( b && es->p_dec == NULL )
1180 EsSelect( out, es );
1181 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1183 else if( !b && es->p_dec )
1185 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1190 case ES_OUT_GET_ES_STATE:
1191 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1192 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1194 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1197 case ES_OUT_SET_ACTIVE:
1199 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1200 p_sys->b_active = b;
1203 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1207 case ES_OUT_GET_ACTIVE:
1208 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1209 *pb = p_sys->b_active;
1212 case ES_OUT_SET_MODE:
1213 i = (int) va_arg( args, int );
1214 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1215 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1219 /* Reapply policy mode */
1220 for( i = 0; i < p_sys->i_es; i++ )
1222 if( p_sys->es[i]->p_dec )
1224 EsUnselect( out, p_sys->es[i],
1225 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1228 for( i = 0; i < p_sys->i_es; i++ )
1230 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1234 return VLC_EGENERIC;
1236 case ES_OUT_GET_MODE:
1237 pi = (int*) va_arg( args, int* );
1238 *pi = p_sys->i_mode;
1242 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1243 /* Special case NULL, NULL+i_cat */
1246 for( i = 0; i < p_sys->i_es; i++ )
1248 if( p_sys->es[i]->p_dec )
1249 EsUnselect( out, p_sys->es[i],
1250 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1253 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1255 for( i = 0; i < p_sys->i_es; i++ )
1257 if( p_sys->es[i]->p_dec &&
1258 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1259 EsUnselect( out, p_sys->es[i],
1260 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1263 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1265 for( i = 0; i < p_sys->i_es; i++ )
1267 if( p_sys->es[i]->p_dec &&
1268 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1269 EsUnselect( out, p_sys->es[i],
1270 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1273 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1275 for( i = 0; i < p_sys->i_es; i++ )
1277 if( p_sys->es[i]->p_dec &&
1278 p_sys->es[i]->fmt.i_cat == SPU_ES )
1279 EsUnselect( out, p_sys->es[i],
1280 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1285 for( i = 0; i < p_sys->i_es; i++ )
1287 if( es == p_sys->es[i] )
1289 EsOutSelect( out, es, VLC_TRUE );
1296 case ES_OUT_SET_PCR:
1297 case ES_OUT_SET_GROUP_PCR:
1299 es_out_pgrm_t *p_pgrm = NULL;
1303 if( i_query == ES_OUT_SET_PCR )
1305 p_pgrm = p_sys->p_pgrm;
1310 i_group = (int)va_arg( args, int );
1311 for( i = 0; i < p_sys->i_pgrm; i++ )
1313 if( p_sys->pgrm[i]->i_id == i_group )
1315 p_pgrm = p_sys->pgrm[i];
1320 if( p_pgrm == NULL )
1321 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1323 i_pcr = (int64_t)va_arg( args, int64_t );
1324 /* search program */
1325 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1326 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1327 (i_pcr + 11 ) * 9 / 100);
1331 case ES_OUT_RESET_PCR:
1332 for( i = 0; i < p_sys->i_pgrm; i++ )
1334 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1335 p_sys->pgrm[i]->clock.last_pts = 0;
1342 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1343 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1344 *pi_ts = input_ClockGetTS( p_sys->p_input,
1345 &p_sys->p_pgrm->clock,
1346 ( i_ts + 11 ) * 9 / 100 );
1349 return VLC_EGENERIC;
1351 case ES_OUT_GET_GROUP:
1352 pi = (int*) va_arg( args, int* );
1354 *pi = p_sys->p_pgrm->i_id;
1356 *pi = -1; /* FIXME */
1359 case ES_OUT_SET_GROUP:
1362 i = (int) va_arg( args, int );
1363 for( j = 0; j < p_sys->i_pgrm; j++ )
1365 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1366 if( p_pgrm->i_id == i )
1368 EsOutProgramSelect( out, p_pgrm );
1372 return VLC_EGENERIC;
1375 case ES_OUT_SET_FMT:
1377 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1378 * to update the p_extra data */
1380 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1381 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1382 if( es == NULL ) return VLC_EGENERIC;
1384 if( p_fmt->i_extra )
1386 es->fmt.i_extra = p_fmt->i_extra;
1387 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1388 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1390 if( !es->p_dec ) return VLC_SUCCESS;
1393 input_DecoderDelete( es->p_dec );
1394 es->p_dec = input_DecoderNew( p_sys->p_input,
1395 &es->fmt, VLC_FALSE );
1398 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1399 es->p_dec->fmt_in.p_extra =
1400 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1401 memcpy( es->p_dec->fmt_in.p_extra,
1402 p_fmt->p_extra, p_fmt->i_extra );
1409 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1413 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1414 i_date = (int64_t)va_arg( args, int64_t );
1416 if( !es || !es->p_dec )
1417 return VLC_EGENERIC;
1419 es->i_preroll_end = i_date;
1420 input_DecoderPreroll( es->p_dec, i_date );
1424 case ES_OUT_SET_GROUP_META:
1426 int i_group = (int)va_arg( args, int );
1427 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1429 EsOutProgramMeta( out, i_group, p_meta );
1432 case ES_OUT_DEL_GROUP:
1434 int i_group = (int)va_arg( args, int );
1436 return EsOutProgramDel( out, i_group );
1440 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1441 return VLC_EGENERIC;
1445 /****************************************************************************
1446 * LanguageGetName: try to expend iso639 into plain name
1447 ****************************************************************************/
1448 static char *LanguageGetName( const char *psz_code )
1450 const iso639_lang_t *pl;
1452 if( psz_code == NULL )
1454 return strdup( "" );
1457 if( strlen( psz_code ) == 2 )
1459 pl = GetLang_1( psz_code );
1461 else if( strlen( psz_code ) == 3 )
1463 pl = GetLang_2B( psz_code );
1464 if( !strcmp( pl->psz_iso639_1, "??" ) )
1466 pl = GetLang_2T( psz_code );
1471 return strdup( psz_code );
1474 if( !strcmp( pl->psz_iso639_1, "??" ) )
1476 return strdup( psz_code );
1480 if( *pl->psz_native_name )
1482 return strdup( pl->psz_native_name );
1484 return strdup( pl->psz_eng_name );
1488 /* Get a 2 char code */
1489 static char *LanguageGetCode( const char *psz_lang )
1491 const iso639_lang_t *pl;
1493 if( psz_lang == NULL || *psz_lang == '\0' )
1494 return strdup("??");
1496 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1498 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1499 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1500 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1501 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1502 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1506 if( pl->psz_iso639_1 != NULL )
1507 return strdup( pl->psz_iso639_1 );
1509 return strdup("??");
1512 static char **LanguageSplit( const char *psz_langs )
1519 if( psz_langs == NULL ) return NULL;
1521 psz_parser = psz_dup = strdup(psz_langs);
1523 while( psz_parser && *psz_parser )
1528 psz = strchr(psz_parser, ',' );
1529 if( psz ) *psz++ = '\0';
1531 psz_code = LanguageGetCode( psz_parser );
1532 if( strcmp( psz_code, "??" ) )
1534 TAB_APPEND( i_psz, ppsz, psz_code );
1542 TAB_APPEND( i_psz, ppsz, NULL );
1549 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1553 if( !ppsz_langs || !psz_lang ) return -1;
1555 for( i = 0; ppsz_langs[i]; i++ )
1556 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1561 /****************************************************************************
1563 * - add meta info to the playlist item
1564 ****************************************************************************/
1565 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1567 es_out_sys_t *p_sys = out->p_sys;
1568 input_thread_t *p_input = p_sys->p_input;
1569 es_format_t *fmt = &es->fmt;
1573 /* Add stream info */
1574 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1576 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1577 "%.4s", (char*)&fmt->i_codec );
1579 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1580 "%s", es->psz_language );
1582 /* Add information */
1583 switch( fmt->i_cat )
1586 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1587 _("Type"), _("Audio") );
1589 if( fmt->audio.i_channels > 0 )
1590 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1591 "%d", fmt->audio.i_channels );
1593 if( fmt->audio.i_rate > 0 )
1595 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1596 _("%d Hz"), fmt->audio.i_rate );
1597 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
1600 if( fmt->audio.i_bitspersample > 0 )
1601 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1602 _("Bits per sample"), "%d",
1603 fmt->audio.i_bitspersample );
1605 if( fmt->i_bitrate > 0 )
1607 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1608 _("%d kb/s"), fmt->i_bitrate / 1000 );
1609 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
1614 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1615 _("Type"), _("Video") );
1617 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1618 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1619 _("Resolution"), "%dx%d",
1620 fmt->video.i_width, fmt->video.i_height );
1622 if( fmt->video.i_visible_width > 0 &&
1623 fmt->video.i_visible_height > 0 )
1624 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1625 _("Display resolution"), "%dx%d",
1626 fmt->video.i_visible_width,
1627 fmt->video.i_visible_height);
1628 if( fmt->video.i_frame_rate > 0 &&
1629 fmt->video.i_frame_rate_base > 0 )
1631 div = lldiv( (float)fmt->video.i_frame_rate /
1632 fmt->video.i_frame_rate_base * 1000000,
1634 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1635 _("Frame rate"), I64Fd".%06u",
1636 div.quot, (unsigned int )div.rem );
1641 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1642 _("Type"), _("Subtitle") );