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_es_out.h>
35 #include <vlc_block.h>
37 #include "input_internal.h"
39 #include "vlc_playlist.h"
41 /* FIXME we should find a better way than including that */
42 #include "../text/iso-639_def.h"
44 /*****************************************************************************
46 *****************************************************************************/
52 /* Number of es for this pgrm */
55 vlc_bool_t b_selected;
57 /* Clock for this program */
60 char *psz_now_playing;
68 es_out_pgrm_t *p_pgrm;
70 /* Signal a discontinuity in the timeline for every PID */
71 vlc_bool_t b_discontinuity;
74 int64_t i_preroll_end;
76 /* Channel in the track type */
80 char *psz_language_code;
86 input_thread_t *p_input;
91 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
92 es_out_pgrm_t *p_pgrm; /* Master program */
109 int i_audio_last, i_audio_id;
110 int i_sub_last, i_sub_id;
111 char **ppsz_audio_language;
112 char **ppsz_sub_language;
114 /* current main es */
115 es_out_id_t *p_es_audio;
116 es_out_id_t *p_es_video;
117 es_out_id_t *p_es_sub;
120 int64_t i_audio_delay;
124 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
125 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
126 static void EsOutDel ( es_out_t *, es_out_id_t * );
127 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
128 static int EsOutControl( es_out_t *, int i_query, va_list );
130 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
132 static void EsSelect( es_out_t *out, es_out_id_t *es );
133 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
134 static char *LanguageGetName( const char *psz_code );
135 static char *LanguageGetCode( const char *psz_lang );
136 static char **LanguageSplit( const char *psz_langs );
137 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
139 /*****************************************************************************
141 *****************************************************************************/
142 es_out_t *input_EsOutNew( input_thread_t *p_input )
144 es_out_t *out = malloc( sizeof( es_out_t ) );
145 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
149 out->pf_add = EsOutAdd;
150 out->pf_send = EsOutSend;
151 out->pf_del = EsOutDel;
152 out->pf_control = EsOutControl;
155 p_sys->p_input = p_input;
157 p_sys->b_active = VLC_FALSE;
158 p_sys->i_mode = ES_OUT_MODE_AUTO;
163 p_sys->p_pgrm = NULL;
174 var_Get( p_input, "audio-track", &val );
175 p_sys->i_audio_last = val.i_int;
177 var_Get( p_input, "sub-track", &val );
178 p_sys->i_sub_last = val.i_int;
180 var_Get( p_input, "audio-language", &val );
181 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
182 if( p_sys->ppsz_audio_language )
184 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
185 msg_Dbg( p_input, "selected audio language[%d] %s",
186 i, p_sys->ppsz_audio_language[i] );
188 if( val.psz_string ) free( val.psz_string );
190 var_Get( p_input, "sub-language", &val );
191 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
192 if( p_sys->ppsz_sub_language )
194 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
195 msg_Dbg( p_input, "selected subtitle language[%d] %s",
196 i, p_sys->ppsz_sub_language[i] );
198 if( val.psz_string ) free( val.psz_string );
200 var_Get( p_input, "audio-track-id", &val );
201 p_sys->i_audio_id = val.i_int;
203 var_Get( p_input, "sub-track-id", &val );
204 p_sys->i_sub_id = val.i_int;
206 p_sys->p_es_audio = NULL;
207 p_sys->p_es_video = NULL;
208 p_sys->p_es_sub = NULL;
210 p_sys->i_audio_delay= 0;
211 p_sys->i_spu_delay = 0;
216 /*****************************************************************************
218 *****************************************************************************/
219 void input_EsOutDelete( es_out_t *out )
221 es_out_sys_t *p_sys = out->p_sys;
224 for( i = 0; i < p_sys->i_es; i++ )
226 if( p_sys->es[i]->p_dec )
228 input_DecoderDelete( p_sys->es[i]->p_dec );
230 if( p_sys->es[i]->psz_language )
231 free( p_sys->es[i]->psz_language );
232 if( p_sys->es[i]->psz_language_code )
233 free( p_sys->es[i]->psz_language_code );
234 es_format_Clean( &p_sys->es[i]->fmt );
236 free( p_sys->es[i] );
238 if( p_sys->ppsz_audio_language )
240 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
241 free( p_sys->ppsz_audio_language[i] );
242 free( p_sys->ppsz_audio_language );
244 if( p_sys->ppsz_sub_language )
246 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
247 free( p_sys->ppsz_sub_language[i] );
248 free( p_sys->ppsz_sub_language );
254 for( i = 0; i < p_sys->i_pgrm; i++ )
256 if( p_sys->pgrm[i]->psz_now_playing )
257 free( p_sys->pgrm[i]->psz_now_playing );
258 free( p_sys->pgrm[i] );
267 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
272 /* Special HACK, -i_id is tha cat of the stream */
273 return (es_out_id_t*)((uint8_t*)NULL-i_id);
276 for( i = 0; i < out->p_sys->i_es; i++ )
278 if( out->p_sys->es[i]->i_id == i_id )
279 return out->p_sys->es[i];
284 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
286 es_out_sys_t *p_sys = out->p_sys;
289 for( i = 0; i < p_sys->i_es; i++ )
291 es_out_id_t *es = p_sys->es[i];
292 es->b_discontinuity = VLC_TRUE; /* signal discontinuity */
294 /* Send a dummy block to let decoder know that
295 * there is a discontinuity */
296 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
298 input_DecoderDiscontinuity( es->p_dec );
303 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
305 es_out_sys_t *p_sys = out->p_sys;
307 if( i_cat == AUDIO_ES )
308 p_sys->i_audio_delay = i_delay;
309 else if( i_cat == SPU_ES )
310 p_sys->i_spu_delay = i_delay;
313 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
315 es_out_sys_t *p_sys = out->p_sys;
318 for( i = 0; i < p_sys->i_es; i++ )
320 es_out_id_t *es = p_sys->es[i];
322 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
328 /*****************************************************************************
330 *****************************************************************************/
331 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
332 vlc_bool_t b_delete )
334 es_out_sys_t *p_sys = out->p_sys;
335 input_thread_t *p_input = p_sys->p_input;
336 vlc_value_t val, text;
340 if( es->fmt.i_cat == AUDIO_ES )
341 psz_var = "audio-es";
342 else if( es->fmt.i_cat == VIDEO_ES )
343 psz_var = "video-es";
344 else if( es->fmt.i_cat == SPU_ES )
351 val.i_int = es->i_id;
352 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
353 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
357 /* Get the number of ES already added */
358 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
363 /* First one, we need to add the "Disable" choice */
364 val2.i_int = -1; text.psz_string = _("Disable");
365 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
369 /* Take care of the ES description */
370 if( es->fmt.psz_description && *es->fmt.psz_description )
372 if( es->psz_language && *es->psz_language )
374 text.psz_string = malloc( strlen( es->fmt.psz_description) +
375 strlen( es->psz_language ) + 10 );
376 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description,
379 else text.psz_string = strdup( es->fmt.psz_description );
383 if( es->psz_language && *es->psz_language )
386 text.psz_string = malloc( strlen( _("Track %i") )+
387 strlen( es->psz_language ) + 30 );
388 asprintf( &temp, _("Track %i"), val.i_int );
389 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
394 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
395 sprintf( text.psz_string, _("Track %i"), val.i_int );
399 val.i_int = es->i_id;
400 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
402 free( text.psz_string );
404 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
407 /* EsOutProgramSelect:
408 * Select a program and update the object variable
410 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
412 es_out_sys_t *p_sys = out->p_sys;
413 input_thread_t *p_input = p_sys->p_input;
417 if( p_sys->p_pgrm == p_pgrm )
418 return; /* Nothing to do */
422 es_out_pgrm_t *old = p_sys->p_pgrm;
423 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
425 for( i = 0; i < p_sys->i_es; i++ )
427 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
428 p_sys->i_mode != ES_OUT_MODE_ALL )
429 EsUnselect( out, p_sys->es[i], VLC_TRUE );
432 p_sys->p_es_audio = NULL;
433 p_sys->p_es_sub = NULL;
434 p_sys->p_es_video = NULL;
437 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
439 /* Mark it selected */
440 p_pgrm->b_selected = VLC_TRUE;
442 /* Switch master stream */
443 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
445 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
447 p_pgrm->clock.b_master = VLC_TRUE;
448 p_sys->p_pgrm = p_pgrm;
450 /* Update "program" */
451 val.i_int = p_pgrm->i_id;
452 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
455 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
456 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
457 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
458 for( i = 0; i < p_sys->i_es; i++ )
460 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
461 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
462 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
465 /* Update now playing if defined per program */
466 if( p_pgrm->psz_now_playing )
468 char *psz_cat = malloc( strlen(_("Program")) + 10 );
470 sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
471 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT),
472 _(VLC_META_NOW_PLAYING), "%s", p_pgrm->psz_now_playing );
477 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
483 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
485 es_out_sys_t *p_sys = out->p_sys;
486 input_thread_t *p_input = p_sys->p_input;
489 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
492 p_pgrm->i_id = i_group;
494 p_pgrm->b_selected = VLC_FALSE;
495 p_pgrm->psz_now_playing = NULL;
496 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average );
499 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
501 /* Update "program" variable */
503 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
505 if( i_group == var_GetInteger( p_input, "program" ) )
507 EsOutProgramSelect( out, p_pgrm );
511 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
519 static int EsOutProgramDel( es_out_t *out, int i_group )
521 es_out_sys_t *p_sys = out->p_sys;
522 input_thread_t *p_input = p_sys->p_input;
523 es_out_pgrm_t *p_pgrm = NULL;
527 for( i = 0; i < p_sys->i_pgrm; i++ )
529 if( p_sys->pgrm[i]->i_id == i_group )
531 p_pgrm = p_sys->pgrm[i];
541 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
542 i_group, p_pgrm->i_es );
546 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
548 /* If program is selected we need to unselect it */
549 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;
551 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
554 /* Update "program" variable */
556 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
558 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
565 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
567 es_out_sys_t *p_sys = out->p_sys;
568 es_out_pgrm_t *p_pgrm = NULL;
569 input_thread_t *p_input = p_sys->p_input;
570 char *psz_cat = malloc( strlen(_("Program")) + 10 );
571 char *psz_title = NULL;
572 char *psz_now_playing = NULL;
573 char *psz_provider = NULL;
576 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
577 sprintf( psz_cat, "%s %d", _("Program"), i_group );
579 if( p_meta->psz_title ) psz_title = p_meta->psz_title;
580 if( p_meta->psz_publisher ) psz_provider = p_meta->psz_publisher;
581 if( p_meta->psz_nowplaying ) psz_now_playing = p_meta->psz_nowplaying;
583 if( !psz_title && !psz_now_playing )
589 for( i = 0; i < p_sys->i_pgrm; i++ )
591 if( p_sys->pgrm[i]->i_id == i_group )
593 p_pgrm = p_sys->pgrm[i];
598 if( p_pgrm == NULL ) {
600 msg_Dbg( p_input, "Trying to add meta for non-existing program" );
604 /* Update the description text of the program */
605 if( psz_title && *psz_title )
610 /* ugly but it works */
612 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
614 if( psz_provider && *psz_provider )
616 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
617 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
618 free( text.psz_string );
622 text.psz_string = psz_title;
623 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
626 if( psz_now_playing )
628 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
629 p_pgrm->psz_now_playing = strdup(psz_now_playing);
631 if( p_sys->p_pgrm == p_pgrm )
633 vlc_meta_SetNowPlaying( p_input->p->input.p_item->p_meta,
643 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
645 es_out_sys_t *p_sys = out->p_sys;
646 input_thread_t *p_input = p_sys->p_input;
648 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
649 es_out_pgrm_t *p_pgrm = NULL;
652 if( fmt->i_group < 0 )
654 msg_Err( p_input, "invalid group number" );
658 /* Search the program */
659 for( i = 0; i < p_sys->i_pgrm; i++ )
661 if( fmt->i_group == p_sys->pgrm[i]->i_id )
663 p_pgrm = p_sys->pgrm[i];
669 /* Create a new one */
670 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
673 /* Increase ref count for program */
678 fmt->i_id = out->p_sys->i_id;
679 es->i_id = fmt->i_id;
681 es_format_Copy( &es->fmt, fmt );
682 es->i_preroll_end = -1;
683 es->b_discontinuity = VLC_FALSE;
688 es->i_channel = p_sys->i_audio;
692 es->i_channel = p_sys->i_video;
693 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
694 vlc_ureduce( &es->fmt.video.i_frame_rate,
695 &es->fmt.video.i_frame_rate_base,
696 fmt->video.i_frame_rate,
697 fmt->video.i_frame_rate_base, 0 );
701 es->i_channel = p_sys->i_sub;
708 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
709 es->psz_language_code = LanguageGetCode( fmt->psz_language );
712 if( es->p_pgrm == p_sys->p_pgrm )
713 EsOutESVarUpdate( out, es, VLC_FALSE );
715 /* Select it if needed */
716 EsOutSelect( out, es, VLC_FALSE );
719 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
720 p_sys->i_id++; /* always incremented */
734 EsOutAddInfo( out, es );
739 static void EsSelect( es_out_t *out, es_out_id_t *es )
741 es_out_sys_t *p_sys = out->p_sys;
742 input_thread_t *p_input = p_sys->p_input;
748 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
752 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
754 if( !var_GetBool( p_input, "video" ) ||
755 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
757 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
762 else if( es->fmt.i_cat == AUDIO_ES )
764 var_Get( p_input, "audio", &val );
765 if( !var_GetBool( p_input, "audio" ) ||
766 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
768 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
773 if( es->fmt.i_cat == SPU_ES )
775 var_Get( p_input, "spu", &val );
776 if( !var_GetBool( p_input, "spu" ) ||
777 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
779 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
785 es->i_preroll_end = -1;
786 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
787 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
790 if( es->fmt.i_cat == VIDEO_ES )
791 psz_var = "video-es";
792 else if( es->fmt.i_cat == AUDIO_ES )
793 psz_var = "audio-es";
794 else if( es->fmt.i_cat == SPU_ES )
799 /* Mark it as selected */
800 val.i_int = es->i_id;
801 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
804 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
807 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
809 es_out_sys_t *p_sys = out->p_sys;
810 input_thread_t *p_input = p_sys->p_input;
814 if( es->p_dec == NULL )
816 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
820 input_DecoderDelete( es->p_dec );
827 if( es->p_dec == NULL )
829 if( es->fmt.i_cat == VIDEO_ES )
830 psz_var = "video-es";
831 else if( es->fmt.i_cat == AUDIO_ES )
832 psz_var = "audio-es";
833 else if( es->fmt.i_cat == SPU_ES )
838 /* Mark it as selected */
840 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
842 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
846 * Select an ES given the current mode
847 * XXX: you need to take a the lock before (stream.stream_lock)
849 * \param out The es_out structure
850 * \param es es_out_id structure
854 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
856 es_out_sys_t *p_sys = out->p_sys;
858 int i_cat = es->fmt.i_cat;
860 if( !p_sys->b_active ||
861 ( !b_force && es->fmt.i_priority < 0 ) )
866 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
871 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
875 var_Get( p_sys->p_input, "programs", &val );
876 for ( i = 0; i < val.p_list->i_count; i++ )
878 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
885 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
887 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
891 if( es->p_pgrm != p_sys->p_pgrm )
894 if( i_cat == AUDIO_ES )
896 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
897 es->psz_language_code );
899 if( p_sys->p_es_audio &&
900 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
902 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
903 p_sys->p_es_audio->psz_language_code );
905 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
907 i_wanted = es->i_channel;
911 /* Select audio if (no audio selected yet)
912 * - no audio-language
913 * - no audio code for the ES
914 * - audio code in the requested list */
916 !strcmp( es->psz_language_code, "??" ) ||
917 !p_sys->ppsz_audio_language )
918 i_wanted = es->i_channel;
921 if( p_sys->i_audio_last >= 0 )
922 i_wanted = p_sys->i_audio_last;
924 if( p_sys->i_audio_id >= 0 )
926 if( es->i_id == p_sys->i_audio_id )
927 i_wanted = es->i_channel;
932 else if( i_cat == SPU_ES )
934 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
935 es->psz_language_code );
937 if( p_sys->p_es_sub &&
938 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
940 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
941 p_sys->p_es_sub->psz_language_code );
943 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
944 idx1, es->psz_language_code, idx2,
945 p_sys->p_es_sub->psz_language_code );
947 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
949 /* We found a SPU that matches our language request */
950 i_wanted = es->i_channel;
954 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
955 idx1, es->psz_language_code );
957 i_wanted = es->i_channel;
959 if( p_sys->i_sub_last >= 0 )
960 i_wanted = p_sys->i_sub_last;
962 if( p_sys->i_sub_id >= 0 )
964 if( es->i_id == p_sys->i_sub_id )
965 i_wanted = es->i_channel;
970 else if( i_cat == VIDEO_ES )
972 i_wanted = es->i_channel;
975 if( i_wanted == es->i_channel && es->p_dec == NULL )
979 /* FIXME TODO handle priority here */
982 if( i_cat == AUDIO_ES )
984 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
986 p_sys->p_es_audio != es &&
987 p_sys->p_es_audio->p_dec )
989 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
991 p_sys->p_es_audio = es;
993 else if( i_cat == SPU_ES )
995 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
997 p_sys->p_es_sub != es &&
998 p_sys->p_es_sub->p_dec )
1000 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1002 p_sys->p_es_sub = es;
1004 else if( i_cat == VIDEO_ES )
1006 p_sys->p_es_video = es;
1012 * Send a block for the given es_out
1014 * \param out the es_out to send from
1015 * \param es the es_out_id
1016 * \param p_block the data block to send
1018 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1020 es_out_sys_t *p_sys = out->p_sys;
1021 input_thread_t *p_input = p_sys->p_input;
1022 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1026 if( es->fmt.i_cat == AUDIO_ES )
1027 i_delay = p_sys->i_audio_delay;
1028 else if( es->fmt.i_cat == SPU_ES )
1029 i_delay = p_sys->i_spu_delay;
1033 if( p_input->p_libvlc->b_stats )
1035 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1036 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1037 p_block->i_buffer, &i_total );
1038 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1039 (float)i_total, NULL );
1040 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1043 /* Mark preroll blocks */
1044 if( es->i_preroll_end >= 0 )
1046 int64_t i_date = p_block->i_pts;
1048 i_date = p_block->i_dts;
1050 if( i_date < es->i_preroll_end )
1051 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1053 es->i_preroll_end = -1;
1056 /* +11 -> avoid null value with non null dts/pts */
1057 if( p_block->i_dts > 0 )
1060 input_ClockGetTS( p_input, &p_pgrm->clock,
1061 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
1063 if( p_block->i_pts > 0 )
1066 input_ClockGetTS( p_input, &p_pgrm->clock,
1067 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
1069 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1071 mtime_t current_date = mdate();
1073 || p_block->i_pts > current_date + 10000000
1074 || current_date > p_block->i_pts )
1076 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1077 * for teletext streams. */
1078 p_block->i_pts = current_date + 400000
1079 + p_input->i_pts_delay + i_delay;
1083 p_block->i_rate = p_input->p->i_rate;
1085 /* TODO handle mute */
1086 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
1087 p_input->p->i_rate == INPUT_RATE_DEFAULT ) )
1089 input_DecoderDecode( es->p_dec, p_block );
1093 block_Release( p_block );
1099 /*****************************************************************************
1101 *****************************************************************************/
1102 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1104 es_out_sys_t *p_sys = out->p_sys;
1105 vlc_bool_t b_reselect = VLC_FALSE;
1108 /* We don't try to reselect */
1110 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1112 if( es->p_pgrm == p_sys->p_pgrm )
1113 EsOutESVarUpdate( out, es, VLC_TRUE );
1115 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1118 if( es->p_pgrm->i_es == 0 )
1120 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1123 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1124 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1126 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1127 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1128 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1130 switch( es->fmt.i_cat )
1143 /* Re-select another track when needed */
1145 for( i = 0; i < p_sys->i_es; i++ )
1147 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1148 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1151 if( es->psz_language )
1152 free( es->psz_language );
1153 if( es->psz_language_code )
1154 free( es->psz_language_code );
1156 es_format_Clean( &es->fmt );
1162 * Control query handler
1164 * \param out the es_out to control
1165 * \param i_query A es_out query as defined in include/ninput.h
1166 * \param args a variable list of arguments for the query
1167 * \return VLC_SUCCESS or an error code
1169 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1171 es_out_sys_t *p_sys = out->p_sys;
1179 case ES_OUT_SET_ES_STATE:
1180 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1181 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1182 if( b && es->p_dec == NULL )
1184 EsSelect( out, es );
1185 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1187 else if( !b && es->p_dec )
1189 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1194 case ES_OUT_GET_ES_STATE:
1195 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1196 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1198 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1201 case ES_OUT_SET_ACTIVE:
1203 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1204 p_sys->b_active = b;
1207 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1211 case ES_OUT_GET_ACTIVE:
1212 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1213 *pb = p_sys->b_active;
1216 case ES_OUT_SET_MODE:
1217 i = (int) va_arg( args, int );
1218 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1219 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1223 /* Reapply policy mode */
1224 for( i = 0; i < p_sys->i_es; i++ )
1226 if( p_sys->es[i]->p_dec )
1228 EsUnselect( out, p_sys->es[i],
1229 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1232 for( i = 0; i < p_sys->i_es; i++ )
1234 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1238 return VLC_EGENERIC;
1240 case ES_OUT_GET_MODE:
1241 pi = (int*) va_arg( args, int* );
1242 *pi = p_sys->i_mode;
1246 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1247 /* Special case NULL, NULL+i_cat */
1250 for( i = 0; i < p_sys->i_es; i++ )
1252 if( p_sys->es[i]->p_dec )
1253 EsUnselect( out, p_sys->es[i],
1254 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1257 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1259 for( i = 0; i < p_sys->i_es; i++ )
1261 if( p_sys->es[i]->p_dec &&
1262 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1263 EsUnselect( out, p_sys->es[i],
1264 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1267 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1269 for( i = 0; i < p_sys->i_es; i++ )
1271 if( p_sys->es[i]->p_dec &&
1272 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1273 EsUnselect( out, p_sys->es[i],
1274 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1277 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1279 for( i = 0; i < p_sys->i_es; i++ )
1281 if( p_sys->es[i]->p_dec &&
1282 p_sys->es[i]->fmt.i_cat == SPU_ES )
1283 EsUnselect( out, p_sys->es[i],
1284 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1289 for( i = 0; i < p_sys->i_es; i++ )
1291 if( es == p_sys->es[i] )
1293 EsOutSelect( out, es, VLC_TRUE );
1300 case ES_OUT_SET_PCR:
1301 case ES_OUT_SET_GROUP_PCR:
1303 es_out_pgrm_t *p_pgrm = NULL;
1307 if( i_query == ES_OUT_SET_PCR )
1309 p_pgrm = p_sys->p_pgrm;
1314 i_group = (int)va_arg( args, int );
1315 for( i = 0; i < p_sys->i_pgrm; i++ )
1317 if( p_sys->pgrm[i]->i_id == i_group )
1319 p_pgrm = p_sys->pgrm[i];
1324 if( p_pgrm == NULL )
1325 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1327 i_pcr = (int64_t)va_arg( args, int64_t );
1328 /* search program */
1329 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1330 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1331 (i_pcr + 11 ) * 9 / 100);
1335 case ES_OUT_RESET_PCR:
1336 for( i = 0; i < p_sys->i_pgrm; i++ )
1338 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1339 p_sys->pgrm[i]->clock.last_pts = 0;
1346 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1347 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1348 *pi_ts = input_ClockGetTS( p_sys->p_input,
1349 &p_sys->p_pgrm->clock,
1350 ( i_ts + 11 ) * 9 / 100 );
1353 return VLC_EGENERIC;
1355 case ES_OUT_GET_GROUP:
1356 pi = (int*) va_arg( args, int* );
1358 *pi = p_sys->p_pgrm->i_id;
1360 *pi = -1; /* FIXME */
1363 case ES_OUT_SET_GROUP:
1366 i = (int) va_arg( args, int );
1367 for( j = 0; j < p_sys->i_pgrm; j++ )
1369 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1370 if( p_pgrm->i_id == i )
1372 EsOutProgramSelect( out, p_pgrm );
1376 return VLC_EGENERIC;
1379 case ES_OUT_SET_FMT:
1381 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1382 * to update the p_extra data */
1384 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1385 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1386 if( es == NULL ) return VLC_EGENERIC;
1388 if( p_fmt->i_extra )
1390 es->fmt.i_extra = p_fmt->i_extra;
1391 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1392 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1394 if( !es->p_dec ) return VLC_SUCCESS;
1397 input_DecoderDelete( es->p_dec );
1398 es->p_dec = input_DecoderNew( p_sys->p_input,
1399 &es->fmt, VLC_FALSE );
1402 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1403 es->p_dec->fmt_in.p_extra =
1404 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1405 memcpy( es->p_dec->fmt_in.p_extra,
1406 p_fmt->p_extra, p_fmt->i_extra );
1413 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1417 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1418 i_date = (int64_t)va_arg( args, int64_t );
1420 if( !es || !es->p_dec )
1421 return VLC_EGENERIC;
1423 es->i_preroll_end = i_date;
1424 input_DecoderPreroll( es->p_dec, i_date );
1428 case ES_OUT_SET_GROUP_META:
1430 int i_group = (int)va_arg( args, int );
1431 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1433 EsOutProgramMeta( out, i_group, p_meta );
1436 case ES_OUT_DEL_GROUP:
1438 int i_group = (int)va_arg( args, int );
1440 return EsOutProgramDel( out, i_group );
1444 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1445 return VLC_EGENERIC;
1449 /****************************************************************************
1450 * LanguageGetName: try to expend iso639 into plain name
1451 ****************************************************************************/
1452 static char *LanguageGetName( const char *psz_code )
1454 const iso639_lang_t *pl;
1456 if( psz_code == NULL )
1458 return strdup( "" );
1461 if( strlen( psz_code ) == 2 )
1463 pl = GetLang_1( psz_code );
1465 else if( strlen( psz_code ) == 3 )
1467 pl = GetLang_2B( psz_code );
1468 if( !strcmp( pl->psz_iso639_1, "??" ) )
1470 pl = GetLang_2T( psz_code );
1475 return strdup( psz_code );
1478 if( !strcmp( pl->psz_iso639_1, "??" ) )
1480 return strdup( psz_code );
1484 if( *pl->psz_native_name )
1486 return strdup( pl->psz_native_name );
1488 return strdup( pl->psz_eng_name );
1492 /* Get a 2 char code */
1493 static char *LanguageGetCode( const char *psz_lang )
1495 const iso639_lang_t *pl;
1497 if( psz_lang == NULL || *psz_lang == '\0' )
1498 return strdup("??");
1500 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1502 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1503 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1504 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1505 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1506 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1510 if( pl->psz_iso639_1 != NULL )
1511 return strdup( pl->psz_iso639_1 );
1513 return strdup("??");
1516 static char **LanguageSplit( const char *psz_langs )
1523 if( psz_langs == NULL ) return NULL;
1525 psz_parser = psz_dup = strdup(psz_langs);
1527 while( psz_parser && *psz_parser )
1532 psz = strchr(psz_parser, ',' );
1533 if( psz ) *psz++ = '\0';
1535 psz_code = LanguageGetCode( psz_parser );
1536 if( strcmp( psz_code, "??" ) )
1538 TAB_APPEND( i_psz, ppsz, psz_code );
1546 TAB_APPEND( i_psz, ppsz, NULL );
1553 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1557 if( !ppsz_langs || !psz_lang ) return -1;
1559 for( i = 0; ppsz_langs[i]; i++ )
1560 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1565 /****************************************************************************
1567 * - add meta info to the playlist item
1568 ****************************************************************************/
1569 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1571 es_out_sys_t *p_sys = out->p_sys;
1572 input_thread_t *p_input = p_sys->p_input;
1573 es_format_t *fmt = &es->fmt;
1577 /* Add stream info */
1578 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1580 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1581 "%.4s", (char*)&fmt->i_codec );
1583 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1584 "%s", es->psz_language );
1586 /* Add information */
1587 switch( fmt->i_cat )
1590 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1591 _("Type"), _("Audio") );
1593 if( fmt->audio.i_channels > 0 )
1594 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1595 "%d", fmt->audio.i_channels );
1597 if( fmt->audio.i_rate > 0 )
1599 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1600 _("%d Hz"), fmt->audio.i_rate );
1601 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
1604 if( fmt->audio.i_bitspersample > 0 )
1605 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1606 _("Bits per sample"), "%d",
1607 fmt->audio.i_bitspersample );
1609 if( fmt->i_bitrate > 0 )
1611 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1612 _("%d kb/s"), fmt->i_bitrate / 1000 );
1613 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
1618 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1619 _("Type"), _("Video") );
1621 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1622 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1623 _("Resolution"), "%dx%d",
1624 fmt->video.i_width, fmt->video.i_height );
1626 if( fmt->video.i_visible_width > 0 &&
1627 fmt->video.i_visible_height > 0 )
1628 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1629 _("Display resolution"), "%dx%d",
1630 fmt->video.i_visible_width,
1631 fmt->video.i_visible_height);
1632 if( fmt->video.i_frame_rate > 0 &&
1633 fmt->video.i_frame_rate_base > 0 )
1635 div = lldiv( (float)fmt->video.i_frame_rate /
1636 fmt->video.i_frame_rate_base * 1000000,
1638 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1639 _("Frame rate"), I64Fd".%06u",
1640 div.quot, (unsigned int )div.rem );
1645 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1646 _("Type"), _("Subtitle") );