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 *****************************************************************************/
32 #include <vlc_input.h>
33 #include <vlc_es_out.h>
34 #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 */
61 char *psz_now_playing;
71 es_out_pgrm_t *p_pgrm;
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 int i_default_sub_id; /* As specified in container; if applicable */
112 char **ppsz_audio_language;
113 char **ppsz_sub_language;
115 /* current main es */
116 es_out_id_t *p_es_audio;
117 es_out_id_t *p_es_video;
118 es_out_id_t *p_es_sub;
121 int64_t i_audio_delay;
125 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
126 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
127 static void EsOutDel ( es_out_t *, es_out_id_t * );
128 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
129 static int EsOutControl( es_out_t *, int i_query, va_list );
131 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
133 static void EsSelect( es_out_t *out, es_out_id_t *es );
134 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
135 static char *LanguageGetName( const char *psz_code );
136 static char *LanguageGetCode( const char *psz_lang );
137 static char **LanguageSplit( const char *psz_langs );
138 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
140 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
142 /*****************************************************************************
144 *****************************************************************************/
145 es_out_t *input_EsOutNew( input_thread_t *p_input )
147 es_out_t *out = malloc( sizeof( es_out_t ) );
148 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
152 out->pf_add = EsOutAdd;
153 out->pf_send = EsOutSend;
154 out->pf_del = EsOutDel;
155 out->pf_control = EsOutControl;
157 out->b_sout = (p_input->p->p_sout != NULL ? VLC_TRUE : VLC_FALSE);
159 p_sys->p_input = p_input;
161 p_sys->b_active = VLC_FALSE;
162 p_sys->i_mode = ES_OUT_MODE_AUTO;
165 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
166 p_sys->p_pgrm = NULL;
170 TAB_INIT( p_sys->i_es, p_sys->es );
177 var_Get( p_input, "audio-track", &val );
178 p_sys->i_audio_last = val.i_int;
180 var_Get( p_input, "sub-track", &val );
181 p_sys->i_sub_last = val.i_int;
183 p_sys->i_default_sub_id = -1;
185 if( !p_input->b_preparsing )
187 var_Get( p_input, "audio-language", &val );
188 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
189 if( p_sys->ppsz_audio_language )
191 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
192 msg_Dbg( p_input, "selected audio language[%d] %s",
193 i, p_sys->ppsz_audio_language[i] );
195 if( val.psz_string ) free( val.psz_string );
197 var_Get( p_input, "sub-language", &val );
198 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
199 if( p_sys->ppsz_sub_language )
201 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
202 msg_Dbg( p_input, "selected subtitle language[%d] %s",
203 i, p_sys->ppsz_sub_language[i] );
205 if( val.psz_string ) free( val.psz_string );
209 p_sys->ppsz_sub_language = NULL;
210 p_sys->ppsz_audio_language = NULL;
213 var_Get( p_input, "audio-track-id", &val );
214 p_sys->i_audio_id = val.i_int;
216 var_Get( p_input, "sub-track-id", &val );
217 p_sys->i_sub_id = val.i_int;
219 p_sys->p_es_audio = NULL;
220 p_sys->p_es_video = NULL;
221 p_sys->p_es_sub = NULL;
223 p_sys->i_audio_delay= 0;
224 p_sys->i_spu_delay = 0;
229 /*****************************************************************************
231 *****************************************************************************/
232 void input_EsOutDelete( es_out_t *out )
234 es_out_sys_t *p_sys = out->p_sys;
237 for( i = 0; i < p_sys->i_es; i++ )
239 if( p_sys->es[i]->p_dec )
241 input_DecoderDelete( p_sys->es[i]->p_dec );
243 if( p_sys->es[i]->psz_language )
244 free( p_sys->es[i]->psz_language );
245 if( p_sys->es[i]->psz_language_code )
246 free( p_sys->es[i]->psz_language_code );
247 es_format_Clean( &p_sys->es[i]->fmt );
249 free( p_sys->es[i] );
251 if( p_sys->ppsz_audio_language )
253 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
254 free( p_sys->ppsz_audio_language[i] );
255 free( p_sys->ppsz_audio_language );
257 if( p_sys->ppsz_sub_language )
259 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
260 free( p_sys->ppsz_sub_language[i] );
261 free( p_sys->ppsz_sub_language );
267 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
268 for( i = 0; i < p_sys->i_pgrm; i++ )
270 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
271 if( p_pgrm->psz_now_playing )
272 free( p_pgrm->psz_now_playing );
273 if( p_pgrm->psz_publisher )
274 free( p_pgrm->psz_publisher );
275 if( p_pgrm->psz_name )
276 free( p_pgrm->psz_name );
278 vlc_epg_Delete( p_pgrm->p_epg );
282 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
288 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
293 /* Special HACK, -i_id is tha cat of the stream */
294 return (es_out_id_t*)((uint8_t*)NULL-i_id);
297 for( i = 0; i < out->p_sys->i_es; i++ )
299 if( out->p_sys->es[i]->i_id == i_id )
300 return out->p_sys->es[i];
305 static void EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_flush, vlc_bool_t b_audio )
307 es_out_sys_t *p_sys = out->p_sys;
310 for( i = 0; i < p_sys->i_es; i++ )
312 es_out_id_t *es = p_sys->es[i];
314 /* Send a dummy block to let decoder know that
315 * there is a discontinuity */
316 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
317 input_DecoderDiscontinuity( es->p_dec, b_flush );
320 void input_EsOutChangeRate( es_out_t *out )
322 es_out_sys_t *p_sys = out->p_sys;
325 EsOutDiscontinuity( out, VLC_FALSE, VLC_FALSE );
327 for( i = 0; i < p_sys->i_pgrm; i++ )
328 input_ClockSetRate( p_sys->p_input, &p_sys->pgrm[i]->clock );
331 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
333 es_out_sys_t *p_sys = out->p_sys;
335 if( i_cat == AUDIO_ES )
336 p_sys->i_audio_delay = i_delay;
337 else if( i_cat == SPU_ES )
338 p_sys->i_spu_delay = i_delay;
340 void input_EsOutChangeState( es_out_t *out )
342 es_out_sys_t *p_sys = out->p_sys;
343 input_thread_t *p_input = p_sys->p_input;
345 if( p_input->i_state == PAUSE_S )
347 /* Send discontinuity to decoders (it will allow them to flush
348 * * if implemented */
349 EsOutDiscontinuity( out, VLC_FALSE, VLC_FALSE );
353 /* Out of pause, reset pcr */
354 es_out_Control( out, ES_OUT_RESET_PCR );
357 void input_EsOutChangePosition( es_out_t *out )
359 //es_out_sys_t *p_sys = out->p_sys;
361 es_out_Control( out, ES_OUT_RESET_PCR );
362 EsOutDiscontinuity( out, VLC_TRUE, VLC_FALSE );
365 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
367 es_out_sys_t *p_sys = out->p_sys;
370 for( i = 0; i < p_sys->i_es; i++ )
372 es_out_id_t *es = p_sys->es[i];
374 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
380 /*****************************************************************************
382 *****************************************************************************/
383 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
384 vlc_bool_t b_delete )
386 es_out_sys_t *p_sys = out->p_sys;
387 input_thread_t *p_input = p_sys->p_input;
388 vlc_value_t val, text;
392 if( es->fmt.i_cat == AUDIO_ES )
393 psz_var = "audio-es";
394 else if( es->fmt.i_cat == VIDEO_ES )
395 psz_var = "video-es";
396 else if( es->fmt.i_cat == SPU_ES )
403 val.i_int = es->i_id;
404 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
405 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
409 /* Get the number of ES already added */
410 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
415 /* First one, we need to add the "Disable" choice */
416 val2.i_int = -1; text.psz_string = _("Disable");
417 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
421 /* Take care of the ES description */
422 if( es->fmt.psz_description && *es->fmt.psz_description )
424 if( es->psz_language && *es->psz_language )
426 text.psz_string = malloc( strlen( es->fmt.psz_description) +
427 strlen( es->psz_language ) + 10 );
428 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description,
431 else text.psz_string = strdup( es->fmt.psz_description );
435 if( es->psz_language && *es->psz_language )
438 text.psz_string = malloc( strlen( _("Track %i") )+
439 strlen( es->psz_language ) + 30 );
440 asprintf( &temp, _("Track %i"), val.i_int );
441 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
446 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
447 sprintf( text.psz_string, _("Track %i"), val.i_int );
451 val.i_int = es->i_id;
452 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
454 free( text.psz_string );
456 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
459 /* EsOutProgramSelect:
460 * Select a program and update the object variable
462 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
464 es_out_sys_t *p_sys = out->p_sys;
465 input_thread_t *p_input = p_sys->p_input;
469 if( p_sys->p_pgrm == p_pgrm )
470 return; /* Nothing to do */
474 es_out_pgrm_t *old = p_sys->p_pgrm;
475 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
477 for( i = 0; i < p_sys->i_es; i++ )
479 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
480 p_sys->i_mode != ES_OUT_MODE_ALL )
481 EsUnselect( out, p_sys->es[i], VLC_TRUE );
484 p_sys->p_es_audio = NULL;
485 p_sys->p_es_sub = NULL;
486 p_sys->p_es_video = NULL;
489 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
491 /* Mark it selected */
492 p_pgrm->b_selected = VLC_TRUE;
494 /* Switch master stream */
495 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
497 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
499 p_pgrm->clock.b_master = VLC_TRUE;
500 p_sys->p_pgrm = p_pgrm;
502 /* Update "program" */
503 val.i_int = p_pgrm->i_id;
504 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
507 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
508 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
509 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
510 for( i = 0; i < p_sys->i_es; i++ )
512 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
513 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
514 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
517 /* Update now playing */
518 input_item_SetNowPlaying( p_input->p->input.p_item,
519 p_pgrm->psz_now_playing );
520 input_item_SetPublisher( p_input->p->input.p_item,
521 p_pgrm->psz_publisher );
523 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
529 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
531 es_out_sys_t *p_sys = out->p_sys;
532 input_thread_t *p_input = p_sys->p_input;
535 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
538 p_pgrm->i_id = i_group;
540 p_pgrm->b_selected = VLC_FALSE;
541 p_pgrm->psz_name = NULL;
542 p_pgrm->psz_now_playing = NULL;
543 p_pgrm->psz_publisher = NULL;
544 p_pgrm->p_epg = NULL;
545 input_ClockInit( p_input, &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average );
548 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
550 /* Update "program" variable */
552 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
554 if( i_group == var_GetInteger( p_input, "program" ) )
556 EsOutProgramSelect( out, p_pgrm );
560 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
568 static int EsOutProgramDel( es_out_t *out, int i_group )
570 es_out_sys_t *p_sys = out->p_sys;
571 input_thread_t *p_input = p_sys->p_input;
572 es_out_pgrm_t *p_pgrm = NULL;
576 for( i = 0; i < p_sys->i_pgrm; i++ )
578 if( p_sys->pgrm[i]->i_id == i_group )
580 p_pgrm = p_sys->pgrm[i];
590 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
591 i_group, p_pgrm->i_es );
595 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
597 /* If program is selected we need to unselect it */
598 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
600 if( p_pgrm->psz_name ) free( p_pgrm->psz_name );
601 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
602 if( p_pgrm->psz_publisher ) free( p_pgrm->psz_publisher );
604 vlc_epg_Delete( p_pgrm->p_epg );
607 /* Update "program" variable */
609 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
611 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
618 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
621 if( p_pgrm->psz_name )
622 asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
624 asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
628 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
630 es_out_sys_t *p_sys = out->p_sys;
631 es_out_pgrm_t *p_pgrm = NULL;
632 input_thread_t *p_input = p_sys->p_input;
634 const char *psz_title = NULL;
635 const char *psz_provider = NULL;
638 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
640 /* Check against empty meta data (empty for what we handle) */
641 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
642 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
643 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
644 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
647 for( i = 0; i < p_sys->i_pgrm; i++ )
649 if( p_sys->pgrm[i]->i_id == i_group )
651 p_pgrm = p_sys->pgrm[i];
656 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
659 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
660 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
662 /* Update the description text of the program */
663 if( psz_title && *psz_title )
668 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
670 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
672 /* Remove old entries */
673 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
674 /* TODO update epg name */
677 if( p_pgrm->psz_name ) free( p_pgrm->psz_name );
678 p_pgrm->psz_name = strdup( psz_title );
680 /* ugly but it works */
682 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
684 if( psz_provider && *psz_provider )
686 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
687 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
688 free( text.psz_string );
692 text.psz_string = (char *)psz_title;
693 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
697 psz_cat = EsOutProgramGetMetaName( p_pgrm );
700 if( p_sys->p_pgrm == p_pgrm )
701 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
702 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
704 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
705 for( i = 0; ppsz_all_keys[i]; i++ )
707 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
708 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
709 free( ppsz_all_keys[i] );
711 free( ppsz_all_keys );
716 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
721 for( i = 0; i < p_src->i_event; i++ )
723 vlc_epg_event_t *p_evt = p_src->pp_event[i];
724 vlc_bool_t b_add = VLC_TRUE;
727 for( j = 0; j < p_dst->i_event; j++ )
729 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
734 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
739 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
742 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
743 p_copy->i_start = p_evt->i_start;
744 p_copy->i_duration = p_evt->i_duration;
745 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
746 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
747 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
748 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
752 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
754 /* Keep only 1 old event */
755 if( p_dst->p_current )
757 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
758 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
762 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
764 es_out_sys_t *p_sys = out->p_sys;
765 input_thread_t *p_input = p_sys->p_input;
766 es_out_pgrm_t *p_pgrm = NULL;
771 for( i = 0; i < p_sys->i_pgrm; i++ )
773 if( p_sys->pgrm[i]->i_id == i_group )
775 p_pgrm = p_sys->pgrm[i];
780 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
784 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
785 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
788 psz_cat = EsOutProgramGetMetaName( p_pgrm );
789 #ifdef HAVE_LOCALTIME_R
791 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
793 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
794 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
795 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
797 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
798 time_t t_start = (time_t)p_evt->i_start;
802 localtime_r( &t_start, &tm_start );
804 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
805 if( p_evt->psz_short_description || p_evt->psz_description )
806 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
808 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
809 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
811 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
813 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
817 /* Update now playing */
818 if( p_pgrm->psz_now_playing )
819 free( p_pgrm->psz_now_playing );
820 p_pgrm->psz_now_playing = NULL;
821 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
822 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
824 if( p_pgrm == p_sys->p_pgrm )
825 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
827 if( p_pgrm->psz_now_playing )
829 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
830 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
831 p_pgrm->psz_now_playing );
835 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
836 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
845 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
847 es_out_sys_t *p_sys = out->p_sys;
848 input_thread_t *p_input = p_sys->p_input;
850 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
851 es_out_pgrm_t *p_pgrm = NULL;
854 if( fmt->i_group < 0 )
856 msg_Err( p_input, "invalid group number" );
860 /* Search the program */
861 for( i = 0; i < p_sys->i_pgrm; i++ )
863 if( fmt->i_group == p_sys->pgrm[i]->i_id )
865 p_pgrm = p_sys->pgrm[i];
871 /* Create a new one */
872 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
875 /* Increase ref count for program */
880 fmt->i_id = out->p_sys->i_id;
881 es->i_id = fmt->i_id;
883 es_format_Copy( &es->fmt, fmt );
884 es->i_preroll_end = -1;
889 es->i_channel = p_sys->i_audio;
893 es->i_channel = p_sys->i_video;
894 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
895 vlc_ureduce( &es->fmt.video.i_frame_rate,
896 &es->fmt.video.i_frame_rate_base,
897 fmt->video.i_frame_rate,
898 fmt->video.i_frame_rate_base, 0 );
902 es->i_channel = p_sys->i_sub;
909 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
910 es->psz_language_code = LanguageGetCode( fmt->psz_language );
913 if( es->p_pgrm == p_sys->p_pgrm )
914 EsOutESVarUpdate( out, es, VLC_FALSE );
916 /* Select it if needed */
917 EsOutSelect( out, es, VLC_FALSE );
920 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
921 p_sys->i_id++; /* always incremented */
935 EsOutAddInfo( out, es );
940 static void EsSelect( es_out_t *out, es_out_id_t *es )
942 es_out_sys_t *p_sys = out->p_sys;
943 input_thread_t *p_input = p_sys->p_input;
949 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
953 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
955 if( !var_GetBool( p_input, "video" ) ||
956 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
958 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
963 else if( es->fmt.i_cat == AUDIO_ES )
965 var_Get( p_input, "audio", &val );
966 if( !var_GetBool( p_input, "audio" ) ||
967 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
969 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
974 if( es->fmt.i_cat == SPU_ES )
976 var_Get( p_input, "spu", &val );
977 if( !var_GetBool( p_input, "spu" ) ||
978 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
980 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
986 es->i_preroll_end = -1;
987 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
988 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
991 if( es->fmt.i_cat == VIDEO_ES )
992 psz_var = "video-es";
993 else if( es->fmt.i_cat == AUDIO_ES )
994 psz_var = "audio-es";
995 else if( es->fmt.i_cat == SPU_ES )
1000 /* Mark it as selected */
1001 val.i_int = es->i_id;
1002 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1005 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1008 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
1010 es_out_sys_t *p_sys = out->p_sys;
1011 input_thread_t *p_input = p_sys->p_input;
1013 const char *psz_var;
1015 if( es->p_dec == NULL )
1017 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1021 input_DecoderDelete( es->p_dec );
1028 if( es->p_dec == NULL )
1030 if( es->fmt.i_cat == VIDEO_ES )
1031 psz_var = "video-es";
1032 else if( es->fmt.i_cat == AUDIO_ES )
1033 psz_var = "audio-es";
1034 else if( es->fmt.i_cat == SPU_ES )
1039 /* Mark it as selected */
1041 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1043 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1047 * Select an ES given the current mode
1048 * XXX: you need to take a the lock before (stream.stream_lock)
1050 * \param out The es_out structure
1051 * \param es es_out_id structure
1052 * \param b_force ...
1055 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
1057 es_out_sys_t *p_sys = out->p_sys;
1059 int i_cat = es->fmt.i_cat;
1061 if( !p_sys->b_active ||
1062 ( !b_force && es->fmt.i_priority < 0 ) )
1067 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1070 EsSelect( out, es );
1072 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1076 var_Get( p_sys->p_input, "programs", &val );
1077 for ( i = 0; i < val.p_list->i_count; i++ )
1079 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1082 EsSelect( out, es );
1086 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1088 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1092 if( es->p_pgrm != p_sys->p_pgrm )
1095 if( i_cat == AUDIO_ES )
1097 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1098 es->psz_language_code );
1100 if( p_sys->p_es_audio &&
1101 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1103 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1104 p_sys->p_es_audio->psz_language_code );
1106 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1108 i_wanted = es->i_channel;
1112 /* Select audio if (no audio selected yet)
1113 * - no audio-language
1114 * - no audio code for the ES
1115 * - audio code in the requested list */
1117 !strcmp( es->psz_language_code, "??" ) ||
1118 !p_sys->ppsz_audio_language )
1119 i_wanted = es->i_channel;
1122 if( p_sys->i_audio_last >= 0 )
1123 i_wanted = p_sys->i_audio_last;
1125 if( p_sys->i_audio_id >= 0 )
1127 if( es->i_id == p_sys->i_audio_id )
1128 i_wanted = es->i_channel;
1133 else if( i_cat == SPU_ES )
1135 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1136 es->psz_language_code );
1138 if( p_sys->p_es_sub &&
1139 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1141 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1142 p_sys->p_es_sub->psz_language_code );
1144 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1145 idx1, es->psz_language_code, idx2,
1146 p_sys->p_es_sub->psz_language_code );
1148 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1150 /* We found a SPU that matches our language request */
1151 i_wanted = es->i_channel;
1153 else if( idx1 >= 0 )
1155 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1156 idx1, es->psz_language_code );
1158 i_wanted = es->i_channel;
1160 else if( p_sys->i_default_sub_id >= 0 )
1162 if( es->i_id == p_sys->i_default_sub_id )
1163 i_wanted = es->i_channel;
1166 if( p_sys->i_sub_last >= 0 )
1167 i_wanted = p_sys->i_sub_last;
1169 if( p_sys->i_sub_id >= 0 )
1171 if( es->i_id == p_sys->i_sub_id )
1172 i_wanted = es->i_channel;
1177 else if( i_cat == VIDEO_ES )
1179 i_wanted = es->i_channel;
1182 if( i_wanted == es->i_channel && es->p_dec == NULL )
1183 EsSelect( out, es );
1186 /* FIXME TODO handle priority here */
1189 if( i_cat == AUDIO_ES )
1191 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1192 p_sys->p_es_audio &&
1193 p_sys->p_es_audio != es &&
1194 p_sys->p_es_audio->p_dec )
1196 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
1198 p_sys->p_es_audio = es;
1200 else if( i_cat == SPU_ES )
1202 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1204 p_sys->p_es_sub != es &&
1205 p_sys->p_es_sub->p_dec )
1207 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1209 p_sys->p_es_sub = es;
1211 else if( i_cat == VIDEO_ES )
1213 p_sys->p_es_video = es;
1219 * Send a block for the given es_out
1221 * \param out the es_out to send from
1222 * \param es the es_out_id
1223 * \param p_block the data block to send
1225 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1227 es_out_sys_t *p_sys = out->p_sys;
1228 input_thread_t *p_input = p_sys->p_input;
1229 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1233 if( es->fmt.i_cat == AUDIO_ES )
1234 i_delay = p_sys->i_audio_delay;
1235 else if( es->fmt.i_cat == SPU_ES )
1236 i_delay = p_sys->i_spu_delay;
1240 if( p_input->p_libvlc->b_stats )
1242 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1243 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1244 p_block->i_buffer, &i_total );
1245 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1246 (float)i_total, NULL );
1247 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1250 /* Mark preroll blocks */
1251 if( es->i_preroll_end >= 0 )
1253 int64_t i_date = p_block->i_pts;
1255 i_date = p_block->i_dts;
1257 if( i_date < es->i_preroll_end )
1258 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1260 es->i_preroll_end = -1;
1263 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1265 p_block->i_dts += i_delay;
1267 else if( p_block->i_dts > 0 )
1270 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1272 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1274 p_block->i_pts += i_delay;
1276 else if( p_block->i_pts > 0 )
1279 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1281 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1283 mtime_t current_date = mdate();
1285 || p_block->i_pts > current_date + 10000000
1286 || current_date > p_block->i_pts )
1288 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1289 * for teletext streams. */
1290 p_block->i_pts = current_date + 400000
1291 + p_input->i_pts_delay + i_delay;
1295 p_block->i_rate = p_input->p->i_rate;
1297 /* TODO handle mute */
1299 ( es->fmt.i_cat != AUDIO_ES ||
1300 ( p_input->p->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1301 p_input->p->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1303 input_DecoderDecode( es->p_dec, p_block );
1307 block_Release( p_block );
1313 /*****************************************************************************
1315 *****************************************************************************/
1316 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1318 es_out_sys_t *p_sys = out->p_sys;
1319 vlc_bool_t b_reselect = VLC_FALSE;
1322 /* We don't try to reselect */
1325 while( !out->p_sys->p_input->b_die && es->p_dec )
1327 if( input_DecoderEmpty( es->p_dec ) )
1331 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1334 if( es->p_pgrm == p_sys->p_pgrm )
1335 EsOutESVarUpdate( out, es, VLC_TRUE );
1337 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1340 if( es->p_pgrm->i_es == 0 )
1342 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1345 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1346 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1348 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1349 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1350 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1352 switch( es->fmt.i_cat )
1365 /* Re-select another track when needed */
1367 for( i = 0; i < p_sys->i_es; i++ )
1369 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1370 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1373 if( es->psz_language )
1374 free( es->psz_language );
1375 if( es->psz_language_code )
1376 free( es->psz_language_code );
1378 es_format_Clean( &es->fmt );
1384 * Control query handler
1386 * \param out the es_out to control
1387 * \param i_query A es_out query as defined in include/ninput.h
1388 * \param args a variable list of arguments for the query
1389 * \return VLC_SUCCESS or an error code
1391 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1393 es_out_sys_t *p_sys = out->p_sys;
1401 case ES_OUT_SET_ES_STATE:
1402 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1403 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1404 if( b && es->p_dec == NULL )
1406 EsSelect( out, es );
1407 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1409 else if( !b && es->p_dec )
1411 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1416 case ES_OUT_GET_ES_STATE:
1417 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1418 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1420 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1423 case ES_OUT_SET_ACTIVE:
1425 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1426 p_sys->b_active = b;
1429 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1433 case ES_OUT_GET_ACTIVE:
1434 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1435 *pb = p_sys->b_active;
1438 case ES_OUT_SET_MODE:
1439 i = (int) va_arg( args, int );
1440 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1441 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1445 /* Reapply policy mode */
1446 for( i = 0; i < p_sys->i_es; i++ )
1448 if( p_sys->es[i]->p_dec )
1450 EsUnselect( out, p_sys->es[i],
1451 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1454 for( i = 0; i < p_sys->i_es; i++ )
1456 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1460 return VLC_EGENERIC;
1462 case ES_OUT_GET_MODE:
1463 pi = (int*) va_arg( args, int* );
1464 *pi = p_sys->i_mode;
1468 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1469 /* Special case NULL, NULL+i_cat */
1472 for( i = 0; i < p_sys->i_es; i++ )
1474 if( p_sys->es[i]->p_dec )
1475 EsUnselect( out, p_sys->es[i],
1476 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1479 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1481 for( i = 0; i < p_sys->i_es; i++ )
1483 if( p_sys->es[i]->p_dec &&
1484 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1485 EsUnselect( out, p_sys->es[i],
1486 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1489 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1491 for( i = 0; i < p_sys->i_es; i++ )
1493 if( p_sys->es[i]->p_dec &&
1494 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1495 EsUnselect( out, p_sys->es[i],
1496 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1499 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1501 for( i = 0; i < p_sys->i_es; i++ )
1503 if( p_sys->es[i]->p_dec &&
1504 p_sys->es[i]->fmt.i_cat == SPU_ES )
1505 EsUnselect( out, p_sys->es[i],
1506 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1511 for( i = 0; i < p_sys->i_es; i++ )
1513 if( es == p_sys->es[i] )
1515 EsOutSelect( out, es, VLC_TRUE );
1521 playlist_t * p_playlist = pl_Yield( p_sys->p_input );
1523 p_playlist->gc_date = mdate();
1524 vlc_object_signal_unlocked( p_playlist );
1526 pl_Release( p_playlist );
1530 case ES_OUT_SET_DEFAULT:
1532 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1536 /*p_sys->i_default_video_id = -1;*/
1537 /*p_sys->i_default_audio_id = -1;*/
1538 p_sys->i_default_sub_id = -1;
1540 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1542 /*p_sys->i_default_video_id = -1;*/
1544 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1546 /*p_sys->i_default_audio_id = -1;*/
1548 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1550 p_sys->i_default_sub_id = -1;
1554 /*if( es->fmt.i_cat == VIDEO_ES )
1555 p_sys->i_default_video_id = es->i_id;
1557 if( es->fmt.i_cat == AUDIO_ES )
1558 p_sys->i_default_audio_id = es->i_id;
1560 if( es->fmt.i_cat == SPU_ES )
1561 p_sys->i_default_sub_id = es->i_id;
1566 case ES_OUT_SET_PCR:
1567 case ES_OUT_SET_GROUP_PCR:
1569 es_out_pgrm_t *p_pgrm = NULL;
1573 if( i_query == ES_OUT_SET_PCR )
1575 p_pgrm = p_sys->p_pgrm;
1580 i_group = (int)va_arg( args, int );
1581 for( i = 0; i < p_sys->i_pgrm; i++ )
1583 if( p_sys->pgrm[i]->i_id == i_group )
1585 p_pgrm = p_sys->pgrm[i];
1590 if( p_pgrm == NULL )
1591 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1593 i_pcr = (int64_t)va_arg( args, int64_t );
1594 /* search program */
1595 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1599 case ES_OUT_RESET_PCR:
1600 for( i = 0; i < p_sys->i_pgrm; i++ )
1602 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1603 p_sys->pgrm[i]->clock.last_pts = 0;
1610 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1611 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1612 *pi_ts = input_ClockGetTS( p_sys->p_input,
1613 &p_sys->p_pgrm->clock, i_ts );
1616 return VLC_EGENERIC;
1618 case ES_OUT_GET_GROUP:
1619 pi = (int*) va_arg( args, int* );
1621 *pi = p_sys->p_pgrm->i_id;
1623 *pi = -1; /* FIXME */
1626 case ES_OUT_SET_GROUP:
1629 i = (int) va_arg( args, int );
1630 for( j = 0; j < p_sys->i_pgrm; j++ )
1632 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1633 if( p_pgrm->i_id == i )
1635 EsOutProgramSelect( out, p_pgrm );
1639 return VLC_EGENERIC;
1642 case ES_OUT_SET_FMT:
1644 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1645 * to update the p_extra data */
1647 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1648 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1649 if( es == NULL ) return VLC_EGENERIC;
1651 if( p_fmt->i_extra )
1653 es->fmt.i_extra = p_fmt->i_extra;
1654 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1655 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1657 if( !es->p_dec ) return VLC_SUCCESS;
1660 input_DecoderDelete( es->p_dec );
1661 es->p_dec = input_DecoderNew( p_sys->p_input,
1662 &es->fmt, VLC_FALSE );
1665 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1666 es->p_dec->fmt_in.p_extra =
1667 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1668 memcpy( es->p_dec->fmt_in.p_extra,
1669 p_fmt->p_extra, p_fmt->i_extra );
1676 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1680 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1681 i_date = (int64_t)va_arg( args, int64_t );
1683 if( !es || !es->p_dec )
1684 return VLC_EGENERIC;
1686 /* XXX We should call input_ClockGetTS but PCR has been reseted
1687 * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1688 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1689 es->i_preroll_end = i_date;
1693 case ES_OUT_SET_GROUP_META:
1695 int i_group = (int)va_arg( args, int );
1696 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1698 EsOutProgramMeta( out, i_group, p_meta );
1701 case ES_OUT_SET_GROUP_EPG:
1703 int i_group = (int)va_arg( args, int );
1704 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1706 EsOutProgramEpg( out, i_group, p_epg );
1709 case ES_OUT_DEL_GROUP:
1711 int i_group = (int)va_arg( args, int );
1713 return EsOutProgramDel( out, i_group );
1717 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1718 return VLC_EGENERIC;
1722 /****************************************************************************
1723 * LanguageGetName: try to expend iso639 into plain name
1724 ****************************************************************************/
1725 static char *LanguageGetName( const char *psz_code )
1727 const iso639_lang_t *pl;
1729 if( psz_code == NULL )
1731 return strdup( "" );
1734 if( strlen( psz_code ) == 2 )
1736 pl = GetLang_1( psz_code );
1738 else if( strlen( psz_code ) == 3 )
1740 pl = GetLang_2B( psz_code );
1741 if( !strcmp( pl->psz_iso639_1, "??" ) )
1743 pl = GetLang_2T( psz_code );
1748 return strdup( psz_code );
1751 if( !strcmp( pl->psz_iso639_1, "??" ) )
1753 return strdup( psz_code );
1757 if( *pl->psz_native_name )
1759 return strdup( pl->psz_native_name );
1761 return strdup( pl->psz_eng_name );
1765 /* Get a 2 char code */
1766 static char *LanguageGetCode( const char *psz_lang )
1768 const iso639_lang_t *pl;
1770 if( psz_lang == NULL || *psz_lang == '\0' )
1771 return strdup("??");
1773 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1775 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1776 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1777 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1778 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1779 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1783 if( pl->psz_iso639_1 != NULL )
1784 return strdup( pl->psz_iso639_1 );
1786 return strdup("??");
1789 static char **LanguageSplit( const char *psz_langs )
1796 if( psz_langs == NULL ) return NULL;
1798 psz_parser = psz_dup = strdup(psz_langs);
1800 while( psz_parser && *psz_parser )
1805 psz = strchr(psz_parser, ',' );
1806 if( psz ) *psz++ = '\0';
1808 psz_code = LanguageGetCode( psz_parser );
1809 if( strcmp( psz_code, "??" ) )
1811 TAB_APPEND( i_psz, ppsz, psz_code );
1819 TAB_APPEND( i_psz, ppsz, NULL );
1826 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1830 if( !ppsz_langs || !psz_lang ) return -1;
1832 for( i = 0; ppsz_langs[i]; i++ )
1833 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1838 /****************************************************************************
1840 * - add meta info to the playlist item
1841 ****************************************************************************/
1842 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1844 es_out_sys_t *p_sys = out->p_sys;
1845 input_thread_t *p_input = p_sys->p_input;
1846 es_format_t *fmt = &es->fmt;
1850 /* Add stream info */
1851 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1853 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1854 "%.4s", (char*)&fmt->i_codec );
1856 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1857 "%s", es->psz_language );
1859 /* Add information */
1860 switch( fmt->i_cat )
1863 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1864 _("Type"), _("Audio") );
1866 if( fmt->audio.i_channels > 0 )
1867 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1868 "%u", fmt->audio.i_channels );
1870 if( fmt->audio.i_rate > 0 )
1872 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1873 _("%u Hz"), fmt->audio.i_rate );
1874 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
1877 if( fmt->audio.i_bitspersample > 0 )
1878 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1879 _("Bits per sample"), "%u",
1880 fmt->audio.i_bitspersample );
1882 if( fmt->i_bitrate > 0 )
1884 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1885 _("%u kb/s"), fmt->i_bitrate / 1000 );
1886 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
1891 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1892 _("Type"), _("Video") );
1894 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1895 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1896 _("Resolution"), "%ux%u",
1897 fmt->video.i_width, fmt->video.i_height );
1899 if( fmt->video.i_visible_width > 0 &&
1900 fmt->video.i_visible_height > 0 )
1901 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1902 _("Display resolution"), "%ux%u",
1903 fmt->video.i_visible_width,
1904 fmt->video.i_visible_height);
1905 if( fmt->video.i_frame_rate > 0 &&
1906 fmt->video.i_frame_rate_base > 0 )
1908 div = lldiv( (float)fmt->video.i_frame_rate /
1909 fmt->video.i_frame_rate_base * 1000000,
1911 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1912 _("Frame rate"), I64Fd".%06u",
1913 div.quot, (unsigned int )div.rem );
1918 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1919 _("Type"), _("Subtitle") );