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_common.h>
36 #include <vlc_input.h>
37 #include <vlc_es_out.h>
38 #include <vlc_block.h>
41 #include "input_internal.h"
43 #include <vlc_iso_lang.h>
44 /* FIXME we should find a better way than including that */
45 #include "../text/iso-639_def.h"
47 /*****************************************************************************
49 *****************************************************************************/
55 /* Number of es for this pgrm */
60 /* Clock for this program */
64 char *psz_now_playing;
74 es_out_pgrm_t *p_pgrm;
77 int64_t i_preroll_end;
79 /* Channel in the track type */
83 char *psz_language_code;
87 /* Fields for Video with CC */
88 bool pb_cc_present[4];
89 es_out_id_t *pp_cc_es[4];
91 /* Field for CC track from a master video */
92 es_out_id_t *p_master;
97 input_thread_t *p_input;
101 es_out_pgrm_t **pgrm;
102 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
103 es_out_pgrm_t *p_pgrm; /* Master program */
120 int i_audio_last, i_audio_id;
121 int i_sub_last, i_sub_id;
122 int i_default_sub_id; /* As specified in container; if applicable */
123 char **ppsz_audio_language;
124 char **ppsz_sub_language;
126 /* current main es */
127 es_out_id_t *p_es_audio;
128 es_out_id_t *p_es_video;
129 es_out_id_t *p_es_sub;
132 int64_t i_audio_delay;
135 /* Rate used to rescale ES ts */
139 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
140 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
141 static void EsOutDel ( es_out_t *, es_out_id_t * );
142 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
143 static int EsOutControl( es_out_t *, int i_query, va_list );
145 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
147 static bool EsIsSelected( es_out_id_t *es );
148 static void EsSelect( es_out_t *out, es_out_id_t *es );
149 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
150 static char *LanguageGetName( const char *psz_code );
151 static char *LanguageGetCode( const char *psz_lang );
152 static char **LanguageSplit( const char *psz_langs );
153 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
155 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
157 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
158 VLC_FOURCC('c', 'c', '1', ' '),
159 VLC_FOURCC('c', 'c', '2', ' '),
160 VLC_FOURCC('c', 'c', '3', ' '),
161 VLC_FOURCC('c', 'c', '4', ' '),
163 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
166 for( i = 0; i < 4; i++ )
168 if( fcc == EsOutFourccClosedCaptions[i] )
175 /*****************************************************************************
177 *****************************************************************************/
178 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
180 es_out_t *out = malloc( sizeof( es_out_t ) );
181 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
185 if( !out ) return NULL;
192 out->pf_add = EsOutAdd;
193 out->pf_send = EsOutSend;
194 out->pf_del = EsOutDel;
195 out->pf_control = EsOutControl;
197 out->b_sout = p_input->p->p_sout != NULL;
199 p_sys->p_input = p_input;
201 p_sys->b_active = false;
202 p_sys->i_mode = ES_OUT_MODE_AUTO;
205 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
206 p_sys->p_pgrm = NULL;
210 TAB_INIT( p_sys->i_es, p_sys->es );
217 var_Get( p_input, "audio-track", &val );
218 p_sys->i_audio_last = val.i_int;
220 var_Get( p_input, "sub-track", &val );
221 p_sys->i_sub_last = val.i_int;
223 p_sys->i_default_sub_id = -1;
225 if( !p_input->b_preparsing )
227 var_Get( p_input, "audio-language", &val );
228 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
229 if( p_sys->ppsz_audio_language )
231 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
232 msg_Dbg( p_input, "selected audio language[%d] %s",
233 i, p_sys->ppsz_audio_language[i] );
235 free( val.psz_string );
237 var_Get( p_input, "sub-language", &val );
238 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
239 if( p_sys->ppsz_sub_language )
241 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
242 msg_Dbg( p_input, "selected subtitle language[%d] %s",
243 i, p_sys->ppsz_sub_language[i] );
245 free( val.psz_string );
249 p_sys->ppsz_sub_language = NULL;
250 p_sys->ppsz_audio_language = NULL;
253 var_Get( p_input, "audio-track-id", &val );
254 p_sys->i_audio_id = val.i_int;
256 var_Get( p_input, "sub-track-id", &val );
257 p_sys->i_sub_id = val.i_int;
259 p_sys->p_es_audio = NULL;
260 p_sys->p_es_video = NULL;
261 p_sys->p_es_sub = NULL;
263 p_sys->i_audio_delay= 0;
264 p_sys->i_spu_delay = 0;
266 p_sys->i_rate = i_rate;
271 /*****************************************************************************
273 *****************************************************************************/
274 void input_EsOutDelete( es_out_t *out )
276 es_out_sys_t *p_sys = out->p_sys;
279 for( i = 0; i < p_sys->i_es; i++ )
281 if( p_sys->es[i]->p_dec )
283 input_DecoderDelete( p_sys->es[i]->p_dec );
285 free( p_sys->es[i]->psz_language );
286 free( p_sys->es[i]->psz_language_code );
287 es_format_Clean( &p_sys->es[i]->fmt );
289 free( p_sys->es[i] );
291 if( p_sys->ppsz_audio_language )
293 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
294 free( p_sys->ppsz_audio_language[i] );
295 free( p_sys->ppsz_audio_language );
297 if( p_sys->ppsz_sub_language )
299 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
300 free( p_sys->ppsz_sub_language[i] );
301 free( p_sys->ppsz_sub_language );
306 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
307 for( i = 0; i < p_sys->i_pgrm; i++ )
309 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
310 free( p_pgrm->psz_now_playing );
311 free( p_pgrm->psz_publisher );
312 free( p_pgrm->psz_name );
314 vlc_epg_Delete( p_pgrm->p_epg );
318 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
324 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
329 /* Special HACK, -i_id is tha cat of the stream */
330 return (es_out_id_t*)((uint8_t*)NULL-i_id);
333 for( i = 0; i < out->p_sys->i_es; i++ )
335 if( out->p_sys->es[i]->i_id == i_id )
336 return out->p_sys->es[i];
341 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
343 es_out_sys_t *p_sys = out->p_sys;
346 for( i = 0; i < p_sys->i_es; i++ )
348 es_out_id_t *es = p_sys->es[i];
350 /* Send a dummy block to let decoder know that
351 * there is a discontinuity */
352 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
353 input_DecoderDiscontinuity( es->p_dec, b_flush );
356 void input_EsOutChangeRate( es_out_t *out, int i_rate )
358 es_out_sys_t *p_sys = out->p_sys;
361 p_sys->i_rate = i_rate;
362 EsOutDiscontinuity( out, false, false );
364 for( i = 0; i < p_sys->i_pgrm; i++ )
365 input_ClockSetRate( &p_sys->pgrm[i]->clock, i_rate );
368 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
370 es_out_sys_t *p_sys = out->p_sys;
372 if( i_cat == AUDIO_ES )
373 p_sys->i_audio_delay = i_delay;
374 else if( i_cat == SPU_ES )
375 p_sys->i_spu_delay = i_delay;
377 void input_EsOutChangeState( es_out_t *out )
379 es_out_sys_t *p_sys = out->p_sys;
380 input_thread_t *p_input = p_sys->p_input;
382 if( p_input->i_state == PAUSE_S )
384 /* Send discontinuity to decoders (it will allow them to flush
385 * * if implemented */
386 EsOutDiscontinuity( out, false, false );
390 /* Out of pause, reset pcr */
391 es_out_Control( out, ES_OUT_RESET_PCR );
394 void input_EsOutChangePosition( es_out_t *out )
396 //es_out_sys_t *p_sys = out->p_sys;
398 es_out_Control( out, ES_OUT_RESET_PCR );
399 EsOutDiscontinuity( out, true, false );
402 bool input_EsOutDecodersEmpty( es_out_t *out )
404 es_out_sys_t *p_sys = out->p_sys;
407 for( i = 0; i < p_sys->i_es; i++ )
409 es_out_id_t *es = p_sys->es[i];
411 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
417 /*****************************************************************************
419 *****************************************************************************/
420 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
423 es_out_sys_t *p_sys = out->p_sys;
424 input_thread_t *p_input = p_sys->p_input;
425 const bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
426 vlc_value_t val, text;
430 if( fmt->i_cat == AUDIO_ES )
431 psz_var = "audio-es";
432 else if( fmt->i_cat == VIDEO_ES )
433 psz_var = "video-es";
434 else if( fmt->i_cat == SPU_ES )
442 var_SetInteger( p_sys->p_input, "teletext-es", -1 );
445 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
447 var_SetBool( p_sys->p_input, "intf-change", true );
451 /* Get the number of ES already added */
452 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
457 /* First one, we need to add the "Disable" choice */
458 val2.i_int = -1; text.psz_string = _("Disable");
459 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
463 /* Take care of the ES description */
464 if( fmt->psz_description && *fmt->psz_description )
466 if( psz_language && *psz_language )
468 text.psz_string = malloc( strlen( fmt->psz_description) +
469 strlen( psz_language ) + 10 );
470 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
473 else text.psz_string = strdup( fmt->psz_description );
477 if( psz_language && *psz_language )
480 text.psz_string = malloc( strlen( _("Track %i") )+
481 strlen( psz_language ) + 30 );
482 asprintf( &temp, _("Track %i"), val.i_int );
483 sprintf( text.psz_string, "%s - [%s]", temp, psz_language );
488 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
489 sprintf( text.psz_string, _("Track %i"), val.i_int );
494 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
496 free( text.psz_string );
499 var_SetInteger( p_sys->p_input, "teletext-es", i_id );
501 var_SetBool( p_sys->p_input, "intf-change", true );
504 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
507 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
510 /* EsOutProgramSelect:
511 * Select a program and update the object variable
513 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
515 es_out_sys_t *p_sys = out->p_sys;
516 input_thread_t *p_input = p_sys->p_input;
520 if( p_sys->p_pgrm == p_pgrm )
521 return; /* Nothing to do */
525 es_out_pgrm_t *old = p_sys->p_pgrm;
526 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
528 for( i = 0; i < p_sys->i_es; i++ )
530 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
531 p_sys->i_mode != ES_OUT_MODE_ALL )
532 EsUnselect( out, p_sys->es[i], true );
535 p_sys->p_es_audio = NULL;
536 p_sys->p_es_sub = NULL;
537 p_sys->p_es_video = NULL;
540 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
542 /* Mark it selected */
543 p_pgrm->b_selected = true;
545 /* Switch master stream */
546 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
548 p_sys->p_pgrm->clock.b_master = false;
550 p_pgrm->clock.b_master = true;
551 p_sys->p_pgrm = p_pgrm;
553 /* Update "program" */
554 val.i_int = p_pgrm->i_id;
555 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
558 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
559 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
560 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
561 var_SetInteger( p_input, "teletext-es", -1 );
562 for( i = 0; i < p_sys->i_es; i++ )
564 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
565 EsOutESVarUpdate( out, p_sys->es[i], false );
566 EsOutSelect( out, p_sys->es[i], false );
569 /* Update now playing */
570 input_item_SetNowPlaying( p_input->p->input.p_item,
571 p_pgrm->psz_now_playing );
572 input_item_SetPublisher( p_input->p->input.p_item,
573 p_pgrm->psz_publisher );
575 var_SetBool( p_sys->p_input, "intf-change", true );
581 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
583 es_out_sys_t *p_sys = out->p_sys;
584 input_thread_t *p_input = p_sys->p_input;
587 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
588 if( !p_pgrm ) return NULL;
591 p_pgrm->i_id = i_group;
593 p_pgrm->b_selected = false;
594 p_pgrm->psz_name = NULL;
595 p_pgrm->psz_now_playing = NULL;
596 p_pgrm->psz_publisher = NULL;
597 p_pgrm->p_epg = NULL;
598 input_ClockInit( &p_pgrm->clock, false, p_input->p->input.i_cr_average, p_sys->i_rate );
601 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
603 /* Update "program" variable */
605 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
607 if( i_group == var_GetInteger( p_input, "program" ) )
609 EsOutProgramSelect( out, p_pgrm );
613 var_SetBool( p_sys->p_input, "intf-change", true );
621 static int EsOutProgramDel( es_out_t *out, int i_group )
623 es_out_sys_t *p_sys = out->p_sys;
624 input_thread_t *p_input = p_sys->p_input;
625 es_out_pgrm_t *p_pgrm = NULL;
629 for( i = 0; i < p_sys->i_pgrm; i++ )
631 if( p_sys->pgrm[i]->i_id == i_group )
633 p_pgrm = p_sys->pgrm[i];
643 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
644 i_group, p_pgrm->i_es );
648 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
650 /* If program is selected we need to unselect it */
651 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
653 free( p_pgrm->psz_name );
654 free( p_pgrm->psz_now_playing );
655 free( p_pgrm->psz_publisher );
657 vlc_epg_Delete( p_pgrm->p_epg );
660 /* Update "program" variable */
662 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
664 var_SetBool( p_sys->p_input, "intf-change", true );
671 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
674 if( p_pgrm->psz_name )
675 asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
677 asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
681 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
683 es_out_sys_t *p_sys = out->p_sys;
684 es_out_pgrm_t *p_pgrm = NULL;
685 input_thread_t *p_input = p_sys->p_input;
687 const char *psz_title = NULL;
688 const char *psz_provider = NULL;
691 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
693 /* Check against empty meta data (empty for what we handle) */
694 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
695 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
696 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
697 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
702 for( i = 0; i < p_sys->i_pgrm; i++ )
704 if( p_sys->pgrm[i]->i_id == i_group )
706 p_pgrm = p_sys->pgrm[i];
711 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
714 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
715 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
717 /* Update the description text of the program */
718 if( psz_title && *psz_title )
723 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
725 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
727 /* Remove old entries */
728 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
729 /* TODO update epg name */
732 free( p_pgrm->psz_name );
733 p_pgrm->psz_name = strdup( psz_title );
735 /* ugly but it works */
737 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
739 if( psz_provider && *psz_provider )
741 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
742 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
743 free( text.psz_string );
747 text.psz_string = (char *)psz_title;
748 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
752 psz_cat = EsOutProgramGetMetaName( p_pgrm );
755 if( p_sys->p_pgrm == p_pgrm )
756 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
757 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
759 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
760 for( i = 0; ppsz_all_keys[i]; i++ )
762 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
763 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
764 free( ppsz_all_keys[i] );
766 free( ppsz_all_keys );
771 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
776 for( i = 0; i < p_src->i_event; i++ )
778 vlc_epg_event_t *p_evt = p_src->pp_event[i];
782 for( j = 0; j < p_dst->i_event; j++ )
784 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
789 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
794 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
797 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
798 p_copy->i_start = p_evt->i_start;
799 p_copy->i_duration = p_evt->i_duration;
800 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
801 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
802 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
803 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
807 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
809 /* Keep only 1 old event */
810 if( p_dst->p_current )
812 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
813 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
817 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
819 es_out_sys_t *p_sys = out->p_sys;
820 input_thread_t *p_input = p_sys->p_input;
821 es_out_pgrm_t *p_pgrm = NULL;
826 for( i = 0; i < p_sys->i_pgrm; i++ )
828 if( p_sys->pgrm[i]->i_id == i_group )
830 p_pgrm = p_sys->pgrm[i];
835 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
839 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
840 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
843 psz_cat = EsOutProgramGetMetaName( p_pgrm );
844 #ifdef HAVE_LOCALTIME_R
846 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
848 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
849 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
850 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
852 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
853 time_t t_start = (time_t)p_evt->i_start;
857 localtime_r( &t_start, &tm_start );
859 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
860 if( p_evt->psz_short_description || p_evt->psz_description )
861 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
863 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
864 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
866 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
868 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
872 /* Update now playing */
873 free( p_pgrm->psz_now_playing );
874 p_pgrm->psz_now_playing = NULL;
875 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
876 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
878 if( p_pgrm == p_sys->p_pgrm )
879 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
881 if( p_pgrm->psz_now_playing )
883 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
884 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
885 p_pgrm->psz_now_playing );
889 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
890 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
899 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
901 es_out_sys_t *p_sys = out->p_sys;
902 input_thread_t *p_input = p_sys->p_input;
904 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
905 es_out_pgrm_t *p_pgrm = NULL;
908 if( !es ) return NULL;
910 if( fmt->i_group < 0 )
912 msg_Err( p_input, "invalid group number" );
917 /* Search the program */
918 for( i = 0; i < p_sys->i_pgrm; i++ )
920 if( fmt->i_group == p_sys->pgrm[i]->i_id )
922 p_pgrm = p_sys->pgrm[i];
928 /* Create a new one */
929 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
932 /* Increase ref count for program */
937 fmt->i_id = out->p_sys->i_id;
938 es->i_id = fmt->i_id;
940 es_format_Copy( &es->fmt, fmt );
941 es->i_preroll_end = -1;
947 audio_replay_gain_t rg;
949 es->i_channel = p_sys->i_audio;
951 vlc_mutex_lock( &p_input->p->input.p_item->lock );
952 memset( &rg, 0, sizeof(rg) );
953 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
954 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
956 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
958 if( !es->fmt.audio_replay_gain.pb_peak[i] )
960 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
961 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
963 if( !es->fmt.audio_replay_gain.pb_gain[i] )
965 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
966 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
973 es->i_channel = p_sys->i_video;
974 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
975 vlc_ureduce( &es->fmt.video.i_frame_rate,
976 &es->fmt.video.i_frame_rate_base,
977 fmt->video.i_frame_rate,
978 fmt->video.i_frame_rate_base, 0 );
982 es->i_channel = p_sys->i_sub;
989 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
990 es->psz_language_code = LanguageGetCode( fmt->psz_language );
992 for( i = 0; i < 4; i++ )
993 es->pb_cc_present[i] = false;
994 es->p_master = false;
996 if( es->p_pgrm == p_sys->p_pgrm )
997 EsOutESVarUpdate( out, es, false );
999 /* Select it if needed */
1000 EsOutSelect( out, es, false );
1003 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1004 p_sys->i_id++; /* always incremented */
1005 switch( fmt->i_cat )
1018 EsOutAddInfo( out, es );
1023 static bool EsIsSelected( es_out_id_t *es )
1027 bool b_decode = false;
1028 if( es->p_master->p_dec )
1030 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1031 if( i_channel != -1 )
1032 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1038 return es->p_dec != NULL;
1041 static void EsSelect( es_out_t *out, es_out_id_t *es )
1043 es_out_sys_t *p_sys = out->p_sys;
1044 input_thread_t *p_input = p_sys->p_input;
1046 const char *psz_var;
1048 if( EsIsSelected( es ) )
1050 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1057 if( !es->p_master->p_dec )
1060 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1061 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1066 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1068 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1070 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1075 else if( es->fmt.i_cat == AUDIO_ES )
1077 var_Get( p_input, "audio", &val );
1078 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1080 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1085 if( es->fmt.i_cat == SPU_ES )
1087 var_Get( p_input, "spu", &val );
1088 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1090 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1096 es->i_preroll_end = -1;
1097 es->p_dec = input_DecoderNew( p_input, &es->fmt, false );
1098 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1102 if( es->fmt.i_cat == VIDEO_ES )
1103 psz_var = "video-es";
1104 else if( es->fmt.i_cat == AUDIO_ES )
1105 psz_var = "audio-es";
1106 else if( es->fmt.i_cat == SPU_ES )
1111 /* Mark it as selected */
1112 val.i_int = es->i_id;
1113 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1115 var_SetBool( p_sys->p_input, "intf-change", true );
1118 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1120 es_out_sys_t *p_sys = out->p_sys;
1121 input_thread_t *p_input = p_sys->p_input;
1123 const char *psz_var;
1125 if( !EsIsSelected( es ) )
1127 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1133 if( es->p_master->p_dec )
1135 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1136 if( i_channel != -1 )
1137 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1142 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1144 for( i = 0; i < 4; i++ )
1146 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1149 if( i_spu_id == es->pp_cc_es[i]->i_id )
1151 /* Force unselection of the CC */
1153 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1155 var_SetBool( p_sys->p_input, "intf-change", true );
1157 EsOutDel( out, es->pp_cc_es[i] );
1159 es->pb_cc_present[i] = false;
1161 input_DecoderDelete( es->p_dec );
1169 if( es->p_dec == NULL )
1171 if( es->fmt.i_cat == VIDEO_ES )
1172 psz_var = "video-es";
1173 else if( es->fmt.i_cat == AUDIO_ES )
1174 psz_var = "audio-es";
1175 else if( es->fmt.i_cat == SPU_ES )
1180 /* Mark it as unselected */
1182 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1184 var_SetBool( p_sys->p_input, "intf-change", true );
1188 * Select an ES given the current mode
1189 * XXX: you need to take a the lock before (stream.stream_lock)
1191 * \param out The es_out structure
1192 * \param es es_out_id structure
1193 * \param b_force ...
1196 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1198 es_out_sys_t *p_sys = out->p_sys;
1200 int i_cat = es->fmt.i_cat;
1202 if( !p_sys->b_active ||
1203 ( !b_force && es->fmt.i_priority < 0 ) )
1208 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1210 if( !EsIsSelected( es ) )
1211 EsSelect( out, es );
1213 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1217 var_Get( p_sys->p_input, "programs", &val );
1218 for ( i = 0; i < val.p_list->i_count; i++ )
1220 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1222 if( !EsIsSelected( es ) )
1223 EsSelect( out, es );
1227 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1229 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1233 if( es->p_pgrm != p_sys->p_pgrm )
1236 if( i_cat == AUDIO_ES )
1238 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1239 es->psz_language_code );
1241 if( p_sys->p_es_audio &&
1242 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1244 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1245 p_sys->p_es_audio->psz_language_code );
1247 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1249 i_wanted = es->i_channel;
1253 /* Select audio if (no audio selected yet)
1254 * - no audio-language
1255 * - no audio code for the ES
1256 * - audio code in the requested list */
1258 !strcmp( es->psz_language_code, "??" ) ||
1259 !p_sys->ppsz_audio_language )
1260 i_wanted = es->i_channel;
1263 if( p_sys->i_audio_last >= 0 )
1264 i_wanted = p_sys->i_audio_last;
1266 if( p_sys->i_audio_id >= 0 )
1268 if( es->i_id == p_sys->i_audio_id )
1269 i_wanted = es->i_channel;
1274 else if( i_cat == SPU_ES )
1276 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1277 es->psz_language_code );
1279 if( p_sys->p_es_sub &&
1280 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1282 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1283 p_sys->p_es_sub->psz_language_code );
1285 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1286 idx1, es->psz_language_code, idx2,
1287 p_sys->p_es_sub->psz_language_code );
1289 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1291 /* We found a SPU that matches our language request */
1292 i_wanted = es->i_channel;
1294 else if( idx1 >= 0 )
1296 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1297 idx1, es->psz_language_code );
1299 i_wanted = es->i_channel;
1301 else if( p_sys->i_default_sub_id >= 0 )
1303 if( es->i_id == p_sys->i_default_sub_id )
1304 i_wanted = es->i_channel;
1307 if( p_sys->i_sub_last >= 0 )
1308 i_wanted = p_sys->i_sub_last;
1310 if( p_sys->i_sub_id >= 0 )
1312 if( es->i_id == p_sys->i_sub_id )
1313 i_wanted = es->i_channel;
1318 else if( i_cat == VIDEO_ES )
1320 i_wanted = es->i_channel;
1323 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1324 EsSelect( out, es );
1327 /* FIXME TODO handle priority here */
1328 if( EsIsSelected( es ) )
1330 if( i_cat == AUDIO_ES )
1332 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1333 p_sys->p_es_audio &&
1334 p_sys->p_es_audio != es &&
1335 EsIsSelected( p_sys->p_es_audio ) )
1337 EsUnselect( out, p_sys->p_es_audio, false );
1339 p_sys->p_es_audio = es;
1341 else if( i_cat == SPU_ES )
1343 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1345 p_sys->p_es_sub != es &&
1346 EsIsSelected( p_sys->p_es_sub ) )
1348 EsUnselect( out, p_sys->p_es_sub, false );
1350 p_sys->p_es_sub = es;
1352 else if( i_cat == VIDEO_ES )
1354 p_sys->p_es_video = es;
1360 * Send a block for the given es_out
1362 * \param out the es_out to send from
1363 * \param es the es_out_id
1364 * \param p_block the data block to send
1366 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1368 es_out_sys_t *p_sys = out->p_sys;
1369 input_thread_t *p_input = p_sys->p_input;
1370 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1374 if( es->fmt.i_cat == AUDIO_ES )
1375 i_delay = p_sys->i_audio_delay;
1376 else if( es->fmt.i_cat == SPU_ES )
1377 i_delay = p_sys->i_spu_delay;
1381 if( libvlc_stats (p_input) )
1383 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1384 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1385 p_block->i_buffer, &i_total );
1386 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1387 (float)i_total, NULL );
1388 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1391 /* Mark preroll blocks */
1392 if( es->i_preroll_end >= 0 )
1394 int64_t i_date = p_block->i_pts;
1396 i_date = p_block->i_dts;
1398 if( i_date < es->i_preroll_end )
1399 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1401 es->i_preroll_end = -1;
1404 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1406 p_block->i_dts += i_delay;
1408 else if( p_block->i_dts > 0 )
1411 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1413 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1415 p_block->i_pts += i_delay;
1417 else if( p_block->i_pts > 0 )
1420 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1422 if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
1423 es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1425 mtime_t current_date = mdate();
1427 || p_block->i_pts > current_date + 10000000
1428 || current_date > p_block->i_pts )
1430 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1431 * for teletext streams. */
1432 p_block->i_pts = current_date + 400000
1433 + p_input->i_pts_delay + i_delay;
1437 p_block->i_rate = p_sys->i_rate;
1439 /* TODO handle mute */
1441 ( es->fmt.i_cat != AUDIO_ES ||
1442 ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1443 p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1446 bool b_cc_new = false;
1448 input_DecoderDecode( es->p_dec, p_block );
1450 /* Check CC status */
1451 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1452 for( i = 0; i < 4; i++ )
1454 static const vlc_fourcc_t fcc[4] = {
1455 VLC_FOURCC('c', 'c', '1', ' '),
1456 VLC_FOURCC('c', 'c', '2', ' '),
1457 VLC_FOURCC('c', 'c', '3', ' '),
1458 VLC_FOURCC('c', 'c', '4', ' '),
1460 static const char ppsz_description[4][18] = {
1461 N_("Closed captions 1"),
1462 N_("Closed captions 2"),
1463 N_("Closed captions 3"),
1464 N_("Closed captions 4"),
1468 if( es->pb_cc_present[i] || !pb_cc[i] )
1470 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1472 es_format_Init( &fmt, SPU_ES, fcc[i] );
1473 fmt.i_group = es->fmt.i_group;
1474 fmt.psz_description = strdup( _(ppsz_description[i] ) );
1475 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1476 es->pp_cc_es[i]->p_master = es;
1477 es_format_Clean( &fmt );
1480 es->pb_cc_present[i] = true;
1484 var_SetBool( p_sys->p_input, "intf-change", true );
1488 block_Release( p_block );
1494 /*****************************************************************************
1496 *****************************************************************************/
1497 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1499 es_out_sys_t *p_sys = out->p_sys;
1500 bool b_reselect = false;
1503 /* We don't try to reselect */
1506 while( !out->p_sys->p_input->b_die && es->p_dec )
1508 if( input_DecoderEmpty( es->p_dec ) )
1512 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1515 if( es->p_pgrm == p_sys->p_pgrm )
1516 EsOutESVarUpdate( out, es, true );
1518 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1521 if( es->p_pgrm->i_es == 0 )
1523 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1526 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1527 p_sys->p_es_sub == es ) b_reselect = true;
1529 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1530 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1531 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1533 switch( es->fmt.i_cat )
1546 /* Re-select another track when needed */
1548 for( i = 0; i < p_sys->i_es; i++ )
1550 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1551 EsOutSelect( out, p_sys->es[i], false );
1554 free( es->psz_language );
1555 free( es->psz_language_code );
1557 es_format_Clean( &es->fmt );
1563 * Control query handler
1565 * \param out the es_out to control
1566 * \param i_query A es_out query as defined in include/ninput.h
1567 * \param args a variable list of arguments for the query
1568 * \return VLC_SUCCESS or an error code
1570 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1572 es_out_sys_t *p_sys = out->p_sys;
1580 case ES_OUT_SET_ES_STATE:
1581 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1582 b = (bool) va_arg( args, int );
1583 if( b && !EsIsSelected( es ) )
1585 EsSelect( out, es );
1586 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1588 else if( !b && EsIsSelected( es ) )
1590 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1595 case ES_OUT_GET_ES_STATE:
1596 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1597 pb = (bool*) va_arg( args, bool * );
1599 *pb = EsIsSelected( es );
1602 case ES_OUT_SET_ACTIVE:
1604 b = (bool) va_arg( args, int );
1605 p_sys->b_active = b;
1608 var_SetBool( p_sys->p_input, "intf-change", true );
1612 case ES_OUT_GET_ACTIVE:
1613 pb = (bool*) va_arg( args, bool * );
1614 *pb = p_sys->b_active;
1617 case ES_OUT_SET_MODE:
1618 i = (int) va_arg( args, int );
1619 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1620 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1624 /* Reapply policy mode */
1625 for( i = 0; i < p_sys->i_es; i++ )
1627 if( EsIsSelected( p_sys->es[i] ) )
1629 EsUnselect( out, p_sys->es[i],
1630 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1633 for( i = 0; i < p_sys->i_es; i++ )
1635 EsOutSelect( out, p_sys->es[i], false );
1639 return VLC_EGENERIC;
1641 case ES_OUT_GET_MODE:
1642 pi = (int*) va_arg( args, int* );
1643 *pi = p_sys->i_mode;
1647 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1648 /* Special case NULL, NULL+i_cat */
1651 for( i = 0; i < p_sys->i_es; i++ )
1653 if( EsIsSelected( p_sys->es[i] ) )
1654 EsUnselect( out, p_sys->es[i],
1655 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1658 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1660 for( i = 0; i < p_sys->i_es; i++ )
1662 if( p_sys->es[i]->fmt.i_cat == AUDIO_ES &&
1663 EsIsSelected( p_sys->es[i] ) )
1664 EsUnselect( out, p_sys->es[i],
1665 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1668 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1670 for( i = 0; i < p_sys->i_es; i++ )
1672 if( p_sys->es[i]->fmt.i_cat == VIDEO_ES &&
1673 EsIsSelected( p_sys->es[i] ) )
1674 EsUnselect( out, p_sys->es[i],
1675 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1678 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1680 for( i = 0; i < p_sys->i_es; i++ )
1682 if( p_sys->es[i]->fmt.i_cat == SPU_ES &&
1683 EsIsSelected( p_sys->es[i] ) )
1684 EsUnselect( out, p_sys->es[i],
1685 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1690 for( i = 0; i < p_sys->i_es; i++ )
1692 if( es == p_sys->es[i] )
1694 EsOutSelect( out, es, true );
1701 event.type = vlc_InputSelectedStreamChanged;
1702 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1706 case ES_OUT_SET_DEFAULT:
1708 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1712 /*p_sys->i_default_video_id = -1;*/
1713 /*p_sys->i_default_audio_id = -1;*/
1714 p_sys->i_default_sub_id = -1;
1716 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1718 /*p_sys->i_default_video_id = -1;*/
1720 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1722 /*p_sys->i_default_audio_id = -1;*/
1724 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1726 p_sys->i_default_sub_id = -1;
1730 /*if( es->fmt.i_cat == VIDEO_ES )
1731 p_sys->i_default_video_id = es->i_id;
1733 if( es->fmt.i_cat == AUDIO_ES )
1734 p_sys->i_default_audio_id = es->i_id;
1736 if( es->fmt.i_cat == SPU_ES )
1737 p_sys->i_default_sub_id = es->i_id;
1742 case ES_OUT_SET_PCR:
1743 case ES_OUT_SET_GROUP_PCR:
1745 es_out_pgrm_t *p_pgrm = NULL;
1749 if( i_query == ES_OUT_SET_PCR )
1751 p_pgrm = p_sys->p_pgrm;
1756 i_group = (int)va_arg( args, int );
1757 for( i = 0; i < p_sys->i_pgrm; i++ )
1759 if( p_sys->pgrm[i]->i_id == i_group )
1761 p_pgrm = p_sys->pgrm[i];
1766 if( p_pgrm == NULL )
1767 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1769 i_pcr = (int64_t)va_arg( args, int64_t );
1770 /* search program */
1771 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1775 case ES_OUT_RESET_PCR:
1776 for( i = 0; i < p_sys->i_pgrm; i++ )
1777 input_ClockResetPCR( &p_sys->pgrm[i]->clock );
1783 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1784 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1785 *pi_ts = input_ClockGetTS( p_sys->p_input,
1786 &p_sys->p_pgrm->clock, i_ts );
1789 return VLC_EGENERIC;
1791 case ES_OUT_GET_GROUP:
1792 pi = (int*) va_arg( args, int* );
1794 *pi = p_sys->p_pgrm->i_id;
1796 *pi = -1; /* FIXME */
1799 case ES_OUT_SET_GROUP:
1802 i = (int) va_arg( args, int );
1803 for( j = 0; j < p_sys->i_pgrm; j++ )
1805 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1806 if( p_pgrm->i_id == i )
1808 EsOutProgramSelect( out, p_pgrm );
1812 return VLC_EGENERIC;
1815 case ES_OUT_SET_FMT:
1817 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1818 * to update the p_extra data */
1820 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1821 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1822 if( es == NULL ) return VLC_EGENERIC;
1824 if( p_fmt->i_extra )
1826 es->fmt.i_extra = p_fmt->i_extra;
1827 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1828 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1830 if( !es->p_dec ) return VLC_SUCCESS;
1833 input_DecoderDelete( es->p_dec );
1834 es->p_dec = input_DecoderNew( p_sys->p_input,
1838 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1839 es->p_dec->fmt_in.p_extra =
1840 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1841 memcpy( es->p_dec->fmt_in.p_extra,
1842 p_fmt->p_extra, p_fmt->i_extra );
1849 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1853 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1854 i_date = (int64_t)va_arg( args, int64_t );
1856 if( !es || !es->p_dec )
1857 return VLC_EGENERIC;
1859 /* XXX We should call input_ClockGetTS but PCR has been reseted
1860 * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1861 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1862 es->i_preroll_end = i_date;
1866 case ES_OUT_SET_GROUP_META:
1868 int i_group = (int)va_arg( args, int );
1869 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1871 EsOutProgramMeta( out, i_group, p_meta );
1874 case ES_OUT_SET_GROUP_EPG:
1876 int i_group = (int)va_arg( args, int );
1877 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1879 EsOutProgramEpg( out, i_group, p_epg );
1882 case ES_OUT_DEL_GROUP:
1884 int i_group = (int)va_arg( args, int );
1886 return EsOutProgramDel( out, i_group );
1890 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1891 return VLC_EGENERIC;
1895 /****************************************************************************
1896 * LanguageGetName: try to expend iso639 into plain name
1897 ****************************************************************************/
1898 static char *LanguageGetName( const char *psz_code )
1900 const iso639_lang_t *pl;
1902 if( psz_code == NULL )
1904 return strdup( "" );
1907 if( strlen( psz_code ) == 2 )
1909 pl = GetLang_1( psz_code );
1911 else if( strlen( psz_code ) == 3 )
1913 pl = GetLang_2B( psz_code );
1914 if( !strcmp( pl->psz_iso639_1, "??" ) )
1916 pl = GetLang_2T( psz_code );
1921 return strdup( psz_code );
1924 if( !strcmp( pl->psz_iso639_1, "??" ) )
1926 return strdup( psz_code );
1930 if( *pl->psz_native_name )
1932 return strdup( pl->psz_native_name );
1934 return strdup( pl->psz_eng_name );
1938 /* Get a 2 char code */
1939 static char *LanguageGetCode( const char *psz_lang )
1941 const iso639_lang_t *pl;
1943 if( psz_lang == NULL || *psz_lang == '\0' )
1944 return strdup("??");
1946 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1948 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1949 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1950 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1951 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1952 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1956 if( pl->psz_iso639_1 != NULL )
1957 return strdup( pl->psz_iso639_1 );
1959 return strdup("??");
1962 static char **LanguageSplit( const char *psz_langs )
1969 if( psz_langs == NULL ) return NULL;
1971 psz_parser = psz_dup = strdup(psz_langs);
1973 while( psz_parser && *psz_parser )
1978 psz = strchr(psz_parser, ',' );
1979 if( psz ) *psz++ = '\0';
1981 if( !strcmp( psz_parser, "any" ) )
1983 TAB_APPEND( i_psz, ppsz, strdup("any") );
1987 psz_code = LanguageGetCode( psz_parser );
1988 if( strcmp( psz_code, "??" ) )
1990 TAB_APPEND( i_psz, ppsz, psz_code );
2003 TAB_APPEND( i_psz, ppsz, NULL );
2010 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2014 if( !ppsz_langs || !psz_lang ) return -1;
2016 for( i = 0; ppsz_langs[i]; i++ )
2018 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2019 !strcasecmp( ppsz_langs[i], "any" ) )
2028 /****************************************************************************
2030 * - add meta info to the playlist item
2031 ****************************************************************************/
2032 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2034 es_out_sys_t *p_sys = out->p_sys;
2035 input_thread_t *p_input = p_sys->p_input;
2036 es_format_t *fmt = &es->fmt;
2040 /* Add stream info */
2041 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
2043 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2044 "%.4s", (char*)&fmt->i_codec );
2046 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2047 "%s", es->psz_language );
2049 /* Add information */
2050 switch( fmt->i_cat )
2053 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2054 _("Type"), _("Audio") );
2056 if( fmt->audio.i_channels > 0 )
2057 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2058 "%u", fmt->audio.i_channels );
2060 if( fmt->audio.i_rate > 0 )
2062 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2063 _("%u Hz"), fmt->audio.i_rate );
2064 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2067 if( fmt->audio.i_bitspersample > 0 )
2068 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2069 _("Bits per sample"), "%u",
2070 fmt->audio.i_bitspersample );
2072 if( fmt->i_bitrate > 0 )
2074 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2075 _("%u kb/s"), fmt->i_bitrate / 1000 );
2076 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2081 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2082 _("Type"), _("Video") );
2084 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2085 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2086 _("Resolution"), "%ux%u",
2087 fmt->video.i_width, fmt->video.i_height );
2089 if( fmt->video.i_visible_width > 0 &&
2090 fmt->video.i_visible_height > 0 )
2091 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2092 _("Display resolution"), "%ux%u",
2093 fmt->video.i_visible_width,
2094 fmt->video.i_visible_height);
2095 if( fmt->video.i_frame_rate > 0 &&
2096 fmt->video.i_frame_rate_base > 0 )
2098 div = lldiv( (float)fmt->video.i_frame_rate /
2099 fmt->video.i_frame_rate_base * 1000000,
2101 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2102 _("Frame rate"), "%"PRId64".%06u",
2103 div.quot, (unsigned int )div.rem );
2108 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2109 _("Type"), _("Subtitle") );