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 *****************************************************************************/
34 #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"
42 #include "input_clock.h"
43 #include "input_decoder.h"
45 #include "../stream_output/stream_output.h"
47 #include <vlc_iso_lang.h>
48 /* FIXME we should find a better way than including that */
49 #include "../text/iso-639_def.h"
51 /*****************************************************************************
53 *****************************************************************************/
59 /* Number of es for this pgrm */
64 /* Clock for this program */
65 input_clock_t *p_clock;
68 char *psz_now_playing;
78 es_out_pgrm_t *p_pgrm;
80 /* Channel in the track type */
84 char *psz_language_code;
87 decoder_t *p_dec_record;
89 /* Fields for Video with CC */
90 bool pb_cc_present[4];
91 es_out_id_t *pp_cc_es[4];
93 /* Field for CC track from a master video */
94 es_out_id_t *p_master;
99 input_thread_t *p_input;
103 es_out_pgrm_t **pgrm;
104 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
105 es_out_pgrm_t *p_pgrm; /* Master program */
122 int i_audio_last, i_audio_id;
123 int i_sub_last, i_sub_id;
124 int i_default_sub_id; /* As specified in container; if applicable */
125 char **ppsz_audio_language;
126 char **ppsz_sub_language;
128 /* current main es */
129 es_out_id_t *p_es_audio;
130 es_out_id_t *p_es_video;
131 es_out_id_t *p_es_sub;
134 int64_t i_audio_delay;
137 /* Rate used for clock */
140 /* Current preroll */
141 int64_t i_preroll_end;
143 /* Used for buffering */
147 sout_instance_t *p_sout_record;
150 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
151 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
152 static void EsOutDel ( es_out_t *, es_out_id_t * );
153 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
154 static int EsOutControl( es_out_t *, int i_query, va_list );
156 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
158 static bool EsIsSelected( es_out_id_t *es );
159 static void EsSelect( es_out_t *out, es_out_id_t *es );
160 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
161 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
162 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
163 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
164 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
165 static char *LanguageGetName( const char *psz_code );
166 static char *LanguageGetCode( const char *psz_lang );
167 static char **LanguageSplit( const char *psz_langs );
168 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
170 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
172 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
173 VLC_FOURCC('c', 'c', '1', ' '),
174 VLC_FOURCC('c', 'c', '2', ' '),
175 VLC_FOURCC('c', 'c', '3', ' '),
176 VLC_FOURCC('c', 'c', '4', ' '),
178 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
181 for( i = 0; i < 4; i++ )
183 if( fcc == EsOutFourccClosedCaptions[i] )
190 /*****************************************************************************
192 *****************************************************************************/
193 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
198 es_out_t *out = malloc( sizeof( es_out_t ) );
199 if( !out ) return NULL;
201 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
208 out->pf_add = EsOutAdd;
209 out->pf_send = EsOutSend;
210 out->pf_del = EsOutDel;
211 out->pf_control = EsOutControl;
213 out->b_sout = p_input->p->p_sout != NULL;
215 p_sys->p_input = p_input;
217 p_sys->b_active = false;
218 p_sys->i_mode = ES_OUT_MODE_AUTO;
221 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
222 p_sys->p_pgrm = NULL;
226 TAB_INIT( p_sys->i_es, p_sys->es );
233 var_Get( p_input, "audio-track", &val );
234 p_sys->i_audio_last = val.i_int;
236 var_Get( p_input, "sub-track", &val );
237 p_sys->i_sub_last = val.i_int;
239 p_sys->i_default_sub_id = -1;
241 if( !p_input->b_preparsing )
243 var_Get( p_input, "audio-language", &val );
244 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
245 if( p_sys->ppsz_audio_language )
247 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
248 msg_Dbg( p_input, "selected audio language[%d] %s",
249 i, p_sys->ppsz_audio_language[i] );
251 free( val.psz_string );
253 var_Get( p_input, "sub-language", &val );
254 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
255 if( p_sys->ppsz_sub_language )
257 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
258 msg_Dbg( p_input, "selected subtitle language[%d] %s",
259 i, p_sys->ppsz_sub_language[i] );
261 free( val.psz_string );
265 p_sys->ppsz_sub_language = NULL;
266 p_sys->ppsz_audio_language = NULL;
269 var_Get( p_input, "audio-track-id", &val );
270 p_sys->i_audio_id = val.i_int;
272 var_Get( p_input, "sub-track-id", &val );
273 p_sys->i_sub_id = val.i_int;
275 p_sys->p_es_audio = NULL;
276 p_sys->p_es_video = NULL;
277 p_sys->p_es_sub = NULL;
279 p_sys->i_audio_delay= 0;
280 p_sys->i_spu_delay = 0;
282 p_sys->i_rate = i_rate;
284 p_sys->b_buffering = true;
285 p_sys->i_preroll_end = -1;
287 p_sys->p_sout_record = NULL;
292 /*****************************************************************************
294 *****************************************************************************/
295 void input_EsOutDelete( es_out_t *out )
297 es_out_sys_t *p_sys = out->p_sys;
300 if( p_sys->p_sout_record )
301 input_EsOutSetRecord( out, false );
303 for( i = 0; i < p_sys->i_es; i++ )
305 if( p_sys->es[i]->p_dec )
306 input_DecoderDelete( p_sys->es[i]->p_dec );
308 free( p_sys->es[i]->psz_language );
309 free( p_sys->es[i]->psz_language_code );
310 es_format_Clean( &p_sys->es[i]->fmt );
312 free( p_sys->es[i] );
314 if( p_sys->ppsz_audio_language )
316 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
317 free( p_sys->ppsz_audio_language[i] );
318 free( p_sys->ppsz_audio_language );
320 if( p_sys->ppsz_sub_language )
322 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
323 free( p_sys->ppsz_sub_language[i] );
324 free( p_sys->ppsz_sub_language );
328 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
329 for( i = 0; i < p_sys->i_pgrm; i++ )
331 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
332 input_clock_Delete( p_pgrm->p_clock );
333 free( p_pgrm->psz_now_playing );
334 free( p_pgrm->psz_publisher );
335 free( p_pgrm->psz_name );
337 vlc_epg_Delete( p_pgrm->p_epg );
341 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
347 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
352 /* Special HACK, -i_id is tha cat of the stream */
353 return (es_out_id_t*)((uint8_t*)NULL-i_id);
356 for( i = 0; i < out->p_sys->i_es; i++ )
358 if( out->p_sys->es[i]->i_id == i_id )
359 return out->p_sys->es[i];
364 mtime_t input_EsOutGetWakeup( es_out_t *out )
366 es_out_sys_t *p_sys = out->p_sys;
367 input_thread_t *p_input = p_sys->p_input;
372 /* We do not have a wake up date if the input cannot have its speed
373 * controlled or sout is imposing its own or while buffering
375 * FIXME for !p_input->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
376 if( !p_input->b_can_pace_control ||
377 p_input->p->b_out_pace_control ||
381 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
384 void input_EsOutChangeRate( es_out_t *out, int i_rate )
386 es_out_sys_t *p_sys = out->p_sys;
389 p_sys->i_rate = i_rate;
391 for( i = 0; i < p_sys->i_pgrm; i++ )
392 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, i_rate );
395 int input_EsOutSetRecord( es_out_t *out, bool b_record )
397 es_out_sys_t *p_sys = out->p_sys;
398 input_thread_t *p_input = p_sys->p_input;
400 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
404 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
405 if( !psz_path || *psz_path == '\0' )
408 psz_path = strdup( config_GetHomeDir() );
411 char *psz_sout = NULL; // TODO conf
413 if( !psz_sout && psz_path )
415 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
418 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
429 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
433 if( !p_sys->p_sout_record )
436 for( int i = 0; i < p_sys->i_es; i++ )
438 es_out_id_t *p_es = p_sys->es[i];
440 if( !p_es->p_dec || p_es->p_master )
443 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
444 if( p_es->p_dec_record && p_sys->b_buffering )
445 input_DecoderStartBuffering( p_es->p_dec_record );
450 for( int i = 0; i < p_sys->i_es; i++ )
452 es_out_id_t *p_es = p_sys->es[i];
454 if( !p_es->p_dec_record )
457 input_DecoderDelete( p_es->p_dec_record );
458 p_es->p_dec_record = NULL;
461 sout_DeleteInstance( p_sys->p_sout_record );
463 p_sys->p_sout_record = NULL;
468 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
470 es_out_sys_t *p_sys = out->p_sys;
472 if( i_cat == AUDIO_ES )
473 p_sys->i_audio_delay = i_delay;
474 else if( i_cat == SPU_ES )
475 p_sys->i_spu_delay = i_delay;
477 for( int i = 0; i < p_sys->i_es; i++ )
478 EsOutDecoderChangeDelay( out, p_sys->es[i] );
480 void input_EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
482 /* XXX the order is important */
485 EsOutDecodersChangePause( out, true, i_date );
486 EsOutProgramChangePause( out, true, i_date );
490 EsOutProgramChangePause( out, false, i_date );
491 EsOutDecodersChangePause( out, false, i_date );
494 void input_EsOutChangePosition( es_out_t *out )
496 es_out_sys_t *p_sys = out->p_sys;
498 for( int i = 0; i < p_sys->i_es; i++ )
500 es_out_id_t *p_es = p_sys->es[i];
505 input_DecoderStartBuffering( p_es->p_dec );
507 if( p_es->p_dec_record )
508 input_DecoderStartBuffering( p_es->p_dec );
511 for( int i = 0; i < p_sys->i_pgrm; i++ )
512 input_clock_Reset( p_sys->pgrm[i]->p_clock );
514 p_sys->b_buffering = true;
515 p_sys->i_preroll_end = -1;
518 bool input_EsOutDecodersIsEmpty( es_out_t *out )
520 es_out_sys_t *p_sys = out->p_sys;
523 if( p_sys->b_buffering && p_sys->p_pgrm )
525 EsOutDecodersStopBuffering( out, true );
526 if( p_sys->b_buffering )
530 for( i = 0; i < p_sys->i_es; i++ )
532 es_out_id_t *es = p_sys->es[i];
534 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
536 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
542 bool input_EsOutIsBuffering( es_out_t *out )
544 return out->p_sys->b_buffering;
547 /*****************************************************************************
549 *****************************************************************************/
550 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
552 es_out_sys_t *p_sys = out->p_sys;
555 mtime_t i_stream_start;
556 mtime_t i_system_start;
557 mtime_t i_stream_duration;
558 mtime_t i_system_duration;
559 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
560 &i_stream_start, &i_system_start,
561 &i_stream_duration, &i_system_duration );
562 assert( !i_ret || b_forced );
566 mtime_t i_preroll_duration = 0;
567 if( p_sys->i_preroll_end >= 0 )
568 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
570 if( i_stream_duration <= p_sys->p_input->i_pts_delay + i_preroll_duration && !b_forced )
572 msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * i_stream_duration / ( p_sys->p_input->i_pts_delay + i_preroll_duration )) );
576 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
577 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
578 p_sys->b_buffering = false;
579 p_sys->i_preroll_end = -1;
581 const mtime_t i_decoder_buffering_start = mdate();
582 for( int i = 0; i < p_sys->i_es; i++ )
584 es_out_id_t *p_es = p_sys->es[i];
588 input_DecoderWaitBuffering( p_es->p_dec );
589 if( p_es->p_dec_record )
590 input_DecoderWaitBuffering( p_es->p_dec_record );
593 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
594 (int)(mdate() - i_decoder_buffering_start)/1000 );
596 const mtime_t i_ts_delay = 10*1000 + /* FIXME CLEANUP thread wake up time*/
598 //msg_Dbg( p_sys->p_input, "==> %lld", i_ts_delay - p_sys->p_input->i_pts_delay );
599 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_ts_delay - p_sys->p_input->i_pts_delay - i_preroll_duration );
601 for( int i = 0; i < p_sys->i_es; i++ )
603 es_out_id_t *p_es = p_sys->es[i];
608 input_DecoderStopBuffering( p_es->p_dec );
609 if( p_es->p_dec_record )
610 input_DecoderStopBuffering( p_es->p_dec );
613 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
615 es_out_sys_t *p_sys = out->p_sys;
617 /* Pause decoders first */
618 for( int i = 0; i < p_sys->i_es; i++ )
620 es_out_id_t *es = p_sys->es[i];
622 /* Send a dummy block to let decoder know that
623 * there is a discontinuity */
626 input_DecoderChangePause( es->p_dec, b_paused, i_date );
627 if( es->p_dec_record )
628 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
632 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
634 es_out_sys_t *p_sys = out->p_sys;
636 for( int i = 0; i < p_sys->i_pgrm; i++ )
637 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
640 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
642 es_out_sys_t *p_sys = out->p_sys;
645 if( p_es->fmt.i_cat == AUDIO_ES )
646 i_delay = p_sys->i_audio_delay;
647 else if( p_es->fmt.i_cat == SPU_ES )
648 i_delay = p_sys->i_spu_delay;
653 input_DecoderChangeDelay( p_es->p_dec, i_delay );
654 if( p_es->p_dec_record )
655 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
660 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
663 es_out_sys_t *p_sys = out->p_sys;
664 input_thread_t *p_input = p_sys->p_input;
665 const bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
666 vlc_value_t val, text;
670 if( fmt->i_cat == AUDIO_ES )
671 psz_var = "audio-es";
672 else if( fmt->i_cat == VIDEO_ES )
673 psz_var = "video-es";
674 else if( fmt->i_cat == SPU_ES )
682 var_SetInteger( p_sys->p_input, "teletext-es", -1 );
685 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
687 var_SetBool( p_sys->p_input, "intf-change", true );
691 /* Get the number of ES already added */
692 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
697 /* First one, we need to add the "Disable" choice */
698 val2.i_int = -1; text.psz_string = _("Disable");
699 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
703 /* Take care of the ES description */
704 if( fmt->psz_description && *fmt->psz_description )
706 if( psz_language && *psz_language )
708 text.psz_string = malloc( strlen( fmt->psz_description) +
709 strlen( psz_language ) + 10 );
710 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
713 else text.psz_string = strdup( fmt->psz_description );
717 if( psz_language && *psz_language )
719 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
720 text.psz_string = NULL;
724 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
725 text.psz_string = NULL;
730 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
732 free( text.psz_string );
736 if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
737 var_SetInteger( p_sys->p_input, "teletext-es", i_id );
740 var_SetBool( p_sys->p_input, "intf-change", true );
743 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
746 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
749 /* EsOutProgramSelect:
750 * Select a program and update the object variable
752 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
754 es_out_sys_t *p_sys = out->p_sys;
755 input_thread_t *p_input = p_sys->p_input;
759 if( p_sys->p_pgrm == p_pgrm )
760 return; /* Nothing to do */
764 es_out_pgrm_t *old = p_sys->p_pgrm;
765 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
767 for( i = 0; i < p_sys->i_es; i++ )
769 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
770 p_sys->i_mode != ES_OUT_MODE_ALL )
771 EsUnselect( out, p_sys->es[i], true );
774 p_sys->p_es_audio = NULL;
775 p_sys->p_es_sub = NULL;
776 p_sys->p_es_video = NULL;
779 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
781 /* Mark it selected */
782 p_pgrm->b_selected = true;
784 /* Switch master stream */
785 p_sys->p_pgrm = p_pgrm;
787 /* Update "program" */
788 val.i_int = p_pgrm->i_id;
789 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
792 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
793 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
794 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
795 var_SetInteger( p_input, "teletext-es", -1 );
796 for( i = 0; i < p_sys->i_es; i++ )
798 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
799 EsOutESVarUpdate( out, p_sys->es[i], false );
800 EsOutSelect( out, p_sys->es[i], false );
803 /* Update now playing */
804 input_item_SetNowPlaying( p_input->p->input.p_item,
805 p_pgrm->psz_now_playing );
806 input_item_SetPublisher( p_input->p->input.p_item,
807 p_pgrm->psz_publisher );
809 var_SetBool( p_sys->p_input, "intf-change", true );
815 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
817 es_out_sys_t *p_sys = out->p_sys;
818 input_thread_t *p_input = p_sys->p_input;
821 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
826 p_pgrm->i_id = i_group;
828 p_pgrm->b_selected = false;
829 p_pgrm->psz_name = NULL;
830 p_pgrm->psz_now_playing = NULL;
831 p_pgrm->psz_publisher = NULL;
832 p_pgrm->p_epg = NULL;
833 p_pgrm->p_clock = input_clock_New( p_input->p->input.i_cr_average, p_sys->i_rate );
834 if( !p_pgrm->p_clock )
841 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
843 /* Update "program" variable */
845 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
847 if( i_group == var_GetInteger( p_input, "program" ) )
849 EsOutProgramSelect( out, p_pgrm );
853 var_SetBool( p_sys->p_input, "intf-change", true );
861 static int EsOutProgramDel( es_out_t *out, int i_group )
863 es_out_sys_t *p_sys = out->p_sys;
864 input_thread_t *p_input = p_sys->p_input;
865 es_out_pgrm_t *p_pgrm = NULL;
869 for( i = 0; i < p_sys->i_pgrm; i++ )
871 if( p_sys->pgrm[i]->i_id == i_group )
873 p_pgrm = p_sys->pgrm[i];
883 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
884 i_group, p_pgrm->i_es );
888 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
890 /* If program is selected we need to unselect it */
891 if( p_sys->p_pgrm == p_pgrm )
892 p_sys->p_pgrm = NULL;
894 input_clock_Delete( p_pgrm->p_clock );
896 free( p_pgrm->psz_name );
897 free( p_pgrm->psz_now_playing );
898 free( p_pgrm->psz_publisher );
900 vlc_epg_Delete( p_pgrm->p_epg );
903 /* Update "program" variable */
905 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
907 var_SetBool( p_sys->p_input, "intf-change", true );
914 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
917 if( p_pgrm->psz_name )
919 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
924 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
930 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
932 es_out_sys_t *p_sys = out->p_sys;
933 es_out_pgrm_t *p_pgrm = NULL;
934 input_thread_t *p_input = p_sys->p_input;
936 const char *psz_title = NULL;
937 const char *psz_provider = NULL;
940 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
942 /* Check against empty meta data (empty for what we handle) */
943 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
944 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
945 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
946 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
951 for( i = 0; i < p_sys->i_pgrm; i++ )
953 if( p_sys->pgrm[i]->i_id == i_group )
955 p_pgrm = p_sys->pgrm[i];
960 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
963 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
964 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
966 /* Update the description text of the program */
967 if( psz_title && *psz_title )
972 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
974 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
976 /* Remove old entries */
977 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
978 /* TODO update epg name */
981 free( p_pgrm->psz_name );
982 p_pgrm->psz_name = strdup( psz_title );
984 /* ugly but it works */
986 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
988 if( psz_provider && *psz_provider )
990 if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
992 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
993 free( text.psz_string );
998 text.psz_string = (char *)psz_title;
999 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
1003 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1006 if( p_sys->p_pgrm == p_pgrm )
1007 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
1008 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1010 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1011 for( i = 0; ppsz_all_keys[i]; i++ )
1013 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
1014 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
1015 free( ppsz_all_keys[i] );
1017 free( ppsz_all_keys );
1022 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
1027 for( i = 0; i < p_src->i_event; i++ )
1029 vlc_epg_event_t *p_evt = p_src->pp_event[i];
1033 for( j = 0; j < p_dst->i_event; j++ )
1035 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
1040 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
1045 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
1048 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
1049 p_copy->i_start = p_evt->i_start;
1050 p_copy->i_duration = p_evt->i_duration;
1051 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
1052 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
1053 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
1054 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
1057 /* Update current */
1058 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
1060 /* Keep only 1 old event */
1061 if( p_dst->p_current )
1063 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
1064 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
1068 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
1070 es_out_sys_t *p_sys = out->p_sys;
1071 input_thread_t *p_input = p_sys->p_input;
1072 es_out_pgrm_t *p_pgrm = NULL;
1077 for( i = 0; i < p_sys->i_pgrm; i++ )
1079 if( p_sys->pgrm[i]->i_id == i_group )
1081 p_pgrm = p_sys->pgrm[i];
1085 if( p_pgrm == NULL )
1086 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1089 if( !p_pgrm->p_epg )
1090 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1091 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1094 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1095 #ifdef HAVE_LOCALTIME_R
1097 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
1099 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
1100 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1101 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
1103 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
1104 time_t t_start = (time_t)p_evt->i_start;
1106 char psz_start[128];
1108 localtime_r( &t_start, &tm_start );
1110 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
1111 if( p_evt->psz_short_description || p_evt->psz_description )
1112 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
1114 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
1115 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
1117 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
1119 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
1123 /* Update now playing */
1124 free( p_pgrm->psz_now_playing );
1125 p_pgrm->psz_now_playing = NULL;
1126 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
1127 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
1129 if( p_pgrm == p_sys->p_pgrm )
1130 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
1132 if( p_pgrm->psz_now_playing )
1134 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1135 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1136 p_pgrm->psz_now_playing );
1140 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1141 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1150 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
1152 es_out_sys_t *p_sys = out->p_sys;
1153 input_thread_t *p_input = p_sys->p_input;
1155 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
1156 es_out_pgrm_t *p_pgrm = NULL;
1159 if( !es ) return NULL;
1161 if( fmt->i_group < 0 )
1163 msg_Err( p_input, "invalid group number" );
1168 /* Search the program */
1169 for( i = 0; i < p_sys->i_pgrm; i++ )
1171 if( fmt->i_group == p_sys->pgrm[i]->i_id )
1173 p_pgrm = p_sys->pgrm[i];
1177 if( p_pgrm == NULL )
1179 /* Create a new one */
1180 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1183 /* Increase ref count for program */
1188 fmt->i_id = out->p_sys->i_id;
1189 es->i_id = fmt->i_id;
1190 es->p_pgrm = p_pgrm;
1191 es_format_Copy( &es->fmt, fmt );
1193 switch( fmt->i_cat )
1197 audio_replay_gain_t rg;
1199 es->i_channel = p_sys->i_audio;
1201 memset( &rg, 0, sizeof(rg) );
1202 vlc_mutex_lock( &p_input->p->input.p_item->lock );
1203 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
1204 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1206 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1208 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1210 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1211 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1213 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1215 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1216 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1223 es->i_channel = p_sys->i_video;
1224 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
1225 vlc_ureduce( &es->fmt.video.i_frame_rate,
1226 &es->fmt.video.i_frame_rate_base,
1227 fmt->video.i_frame_rate,
1228 fmt->video.i_frame_rate_base, 0 );
1232 es->i_channel = p_sys->i_sub;
1239 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
1240 es->psz_language_code = LanguageGetCode( fmt->psz_language );
1242 es->p_dec_record = NULL;
1243 for( i = 0; i < 4; i++ )
1244 es->pb_cc_present[i] = false;
1245 es->p_master = NULL;
1247 if( es->p_pgrm == p_sys->p_pgrm )
1248 EsOutESVarUpdate( out, es, false );
1250 /* Select it if needed */
1251 EsOutSelect( out, es, false );
1254 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1255 p_sys->i_id++; /* always incremented */
1256 switch( fmt->i_cat )
1269 EsOutAddInfo( out, es );
1274 static bool EsIsSelected( es_out_id_t *es )
1278 bool b_decode = false;
1279 if( es->p_master->p_dec )
1281 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1282 if( i_channel != -1 )
1283 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1289 return es->p_dec != NULL;
1292 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1294 es_out_sys_t *p_sys = out->p_sys;
1295 input_thread_t *p_input = p_sys->p_input;
1297 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1300 if( p_sys->b_buffering )
1301 input_DecoderStartBuffering( p_es->p_dec );
1303 if( !p_es->p_master && p_sys->p_sout_record )
1305 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1306 if( p_es->p_dec_record && p_sys->b_buffering )
1307 input_DecoderStartBuffering( p_es->p_dec_record );
1311 EsOutDecoderChangeDelay( out, p_es );
1313 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1320 input_DecoderDelete( p_es->p_dec );
1323 if( p_es->p_dec_record )
1325 input_DecoderDelete( p_es->p_dec_record );
1326 p_es->p_dec_record = NULL;
1330 static void EsSelect( es_out_t *out, es_out_id_t *es )
1332 es_out_sys_t *p_sys = out->p_sys;
1333 input_thread_t *p_input = p_sys->p_input;
1335 const char *psz_var;
1337 if( EsIsSelected( es ) )
1339 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1346 if( !es->p_master->p_dec )
1349 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1350 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1355 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1357 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1359 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1364 else if( es->fmt.i_cat == AUDIO_ES )
1366 var_Get( p_input, "audio", &val );
1367 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1369 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1374 if( es->fmt.i_cat == SPU_ES )
1376 var_Get( p_input, "spu", &val );
1377 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1379 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1385 EsCreateDecoder( out, es );
1387 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1391 if( es->fmt.i_cat == VIDEO_ES )
1392 psz_var = "video-es";
1393 else if( es->fmt.i_cat == AUDIO_ES )
1394 psz_var = "audio-es";
1395 else if( es->fmt.i_cat == SPU_ES )
1400 /* Mark it as selected */
1401 val.i_int = es->i_id;
1402 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1404 var_SetBool( p_sys->p_input, "intf-change", true );
1407 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1409 es_out_sys_t *p_sys = out->p_sys;
1410 input_thread_t *p_input = p_sys->p_input;
1412 const char *psz_var;
1414 if( !EsIsSelected( es ) )
1416 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1422 if( es->p_master->p_dec )
1424 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1425 if( i_channel != -1 )
1426 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1431 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1433 for( i = 0; i < 4; i++ )
1435 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1438 if( i_spu_id == es->pp_cc_es[i]->i_id )
1440 /* Force unselection of the CC */
1442 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1444 var_SetBool( p_sys->p_input, "intf-change", true );
1446 EsOutDel( out, es->pp_cc_es[i] );
1448 es->pb_cc_present[i] = false;
1450 EsDestroyDecoder( out, es );
1457 if( es->p_dec == NULL )
1459 if( es->fmt.i_cat == VIDEO_ES )
1460 psz_var = "video-es";
1461 else if( es->fmt.i_cat == AUDIO_ES )
1462 psz_var = "audio-es";
1463 else if( es->fmt.i_cat == SPU_ES )
1468 /* Mark it as unselected */
1470 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1472 var_SetBool( p_sys->p_input, "intf-change", true );
1476 * Select an ES given the current mode
1477 * XXX: you need to take a the lock before (stream.stream_lock)
1479 * \param out The es_out structure
1480 * \param es es_out_id structure
1481 * \param b_force ...
1484 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1486 es_out_sys_t *p_sys = out->p_sys;
1488 int i_cat = es->fmt.i_cat;
1490 if( !p_sys->b_active ||
1491 ( !b_force && es->fmt.i_priority < 0 ) )
1496 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1498 if( !EsIsSelected( es ) )
1499 EsSelect( out, es );
1501 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1505 var_Get( p_sys->p_input, "programs", &val );
1506 for ( i = 0; i < val.p_list->i_count; i++ )
1508 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1510 if( !EsIsSelected( es ) )
1511 EsSelect( out, es );
1515 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1517 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1521 if( es->p_pgrm != p_sys->p_pgrm )
1524 if( i_cat == AUDIO_ES )
1526 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1527 es->psz_language_code );
1529 if( p_sys->p_es_audio &&
1530 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1532 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1533 p_sys->p_es_audio->psz_language_code );
1535 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1537 i_wanted = es->i_channel;
1541 /* Select audio if (no audio selected yet)
1542 * - no audio-language
1543 * - no audio code for the ES
1544 * - audio code in the requested list */
1546 !strcmp( es->psz_language_code, "??" ) ||
1547 !p_sys->ppsz_audio_language )
1548 i_wanted = es->i_channel;
1551 if( p_sys->i_audio_last >= 0 )
1552 i_wanted = p_sys->i_audio_last;
1554 if( p_sys->i_audio_id >= 0 )
1556 if( es->i_id == p_sys->i_audio_id )
1557 i_wanted = es->i_channel;
1562 else if( i_cat == SPU_ES )
1564 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1565 es->psz_language_code );
1567 if( p_sys->p_es_sub &&
1568 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1570 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1571 p_sys->p_es_sub->psz_language_code );
1573 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1574 idx1, es->psz_language_code, idx2,
1575 p_sys->p_es_sub->psz_language_code );
1577 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1579 /* We found a SPU that matches our language request */
1580 i_wanted = es->i_channel;
1582 else if( idx1 >= 0 )
1584 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1585 idx1, es->psz_language_code );
1587 i_wanted = es->i_channel;
1589 else if( p_sys->i_default_sub_id >= 0 )
1591 if( es->i_id == p_sys->i_default_sub_id )
1592 i_wanted = es->i_channel;
1595 if( p_sys->i_sub_last >= 0 )
1596 i_wanted = p_sys->i_sub_last;
1598 if( p_sys->i_sub_id >= 0 )
1600 if( es->i_id == p_sys->i_sub_id )
1601 i_wanted = es->i_channel;
1606 else if( i_cat == VIDEO_ES )
1608 i_wanted = es->i_channel;
1611 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1612 EsSelect( out, es );
1615 /* FIXME TODO handle priority here */
1616 if( EsIsSelected( es ) )
1618 if( i_cat == AUDIO_ES )
1620 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1621 p_sys->p_es_audio &&
1622 p_sys->p_es_audio != es &&
1623 EsIsSelected( p_sys->p_es_audio ) )
1625 EsUnselect( out, p_sys->p_es_audio, false );
1627 p_sys->p_es_audio = es;
1629 else if( i_cat == SPU_ES )
1631 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1633 p_sys->p_es_sub != es &&
1634 EsIsSelected( p_sys->p_es_sub ) )
1636 EsUnselect( out, p_sys->p_es_sub, false );
1638 p_sys->p_es_sub = es;
1640 else if( i_cat == VIDEO_ES )
1642 p_sys->p_es_video = es;
1648 * Send a block for the given es_out
1650 * \param out the es_out to send from
1651 * \param es the es_out_id
1652 * \param p_block the data block to send
1654 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1656 es_out_sys_t *p_sys = out->p_sys;
1657 input_thread_t *p_input = p_sys->p_input;
1660 if( libvlc_stats( p_input ) )
1662 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1663 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1664 p_block->i_buffer, &i_total );
1665 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1666 (float)i_total, NULL );
1667 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1670 /* Mark preroll blocks */
1671 if( p_sys->i_preroll_end >= 0 )
1673 int64_t i_date = p_block->i_pts;
1675 i_date = p_block->i_dts;
1677 if( i_date < p_sys->i_preroll_end )
1678 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1681 p_block->i_rate = 0;
1685 block_Release( p_block );
1690 if( es->p_dec_record )
1692 block_t *p_dup = block_Duplicate( p_block );
1694 input_DecoderDecode( es->p_dec_record, p_dup );
1696 input_DecoderDecode( es->p_dec, p_block );
1698 /* Check CC status */
1700 bool b_cc_new = false;
1702 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1703 for( int i = 0; i < 4; i++ )
1705 static const vlc_fourcc_t fcc[4] = {
1706 VLC_FOURCC('c', 'c', '1', ' '),
1707 VLC_FOURCC('c', 'c', '2', ' '),
1708 VLC_FOURCC('c', 'c', '3', ' '),
1709 VLC_FOURCC('c', 'c', '4', ' '),
1713 if( es->pb_cc_present[i] || !pb_cc[i] )
1715 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1717 es_format_Init( &fmt, SPU_ES, fcc[i] );
1718 fmt.i_group = es->fmt.i_group;
1719 if( asprintf( &fmt.psz_description,
1720 _("Closed captions %u"), 1 + i ) == -1 )
1721 fmt.psz_description = NULL;
1722 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1723 es->pp_cc_es[i]->p_master = es;
1724 es_format_Clean( &fmt );
1727 es->pb_cc_present[i] = true;
1731 var_SetBool( p_sys->p_input, "intf-change", true );
1736 /*****************************************************************************
1738 *****************************************************************************/
1739 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1741 es_out_sys_t *p_sys = out->p_sys;
1742 bool b_reselect = false;
1745 /* We don't try to reselect */
1748 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
1750 if( input_DecoderIsEmpty( es->p_dec ) &&
1751 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
1755 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1758 if( es->p_pgrm == p_sys->p_pgrm )
1759 EsOutESVarUpdate( out, es, true );
1761 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1764 if( es->p_pgrm->i_es == 0 )
1766 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1769 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1770 p_sys->p_es_sub == es ) b_reselect = true;
1772 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1773 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1774 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1776 switch( es->fmt.i_cat )
1789 /* Re-select another track when needed */
1791 for( i = 0; i < p_sys->i_es; i++ )
1793 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1794 EsOutSelect( out, p_sys->es[i], false );
1797 free( es->psz_language );
1798 free( es->psz_language_code );
1800 es_format_Clean( &es->fmt );
1806 * Control query handler
1808 * \param out the es_out to control
1809 * \param i_query A es_out query as defined in include/ninput.h
1810 * \param args a variable list of arguments for the query
1811 * \return VLC_SUCCESS or an error code
1813 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1815 es_out_sys_t *p_sys = out->p_sys;
1823 case ES_OUT_SET_ES_STATE:
1824 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1825 b = (bool) va_arg( args, int );
1826 if( b && !EsIsSelected( es ) )
1828 EsSelect( out, es );
1829 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1831 else if( !b && EsIsSelected( es ) )
1833 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1838 case ES_OUT_GET_ES_STATE:
1839 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1840 pb = (bool*) va_arg( args, bool * );
1842 *pb = EsIsSelected( es );
1845 case ES_OUT_SET_ACTIVE:
1847 b = (bool) va_arg( args, int );
1848 p_sys->b_active = b;
1851 var_SetBool( p_sys->p_input, "intf-change", true );
1855 case ES_OUT_GET_ACTIVE:
1856 pb = (bool*) va_arg( args, bool * );
1857 *pb = p_sys->b_active;
1860 case ES_OUT_SET_MODE:
1861 i = (int) va_arg( args, int );
1862 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1863 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1867 /* Reapply policy mode */
1868 for( i = 0; i < p_sys->i_es; i++ )
1870 if( EsIsSelected( p_sys->es[i] ) )
1872 EsUnselect( out, p_sys->es[i],
1873 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1876 for( i = 0; i < p_sys->i_es; i++ )
1878 EsOutSelect( out, p_sys->es[i], false );
1882 return VLC_EGENERIC;
1884 case ES_OUT_GET_MODE:
1885 pi = (int*) va_arg( args, int* );
1886 *pi = p_sys->i_mode;
1890 case ES_OUT_RESTART_ES:
1894 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1898 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1900 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1902 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1907 for( i = 0; i < p_sys->i_es; i++ )
1911 if( es == p_sys->es[i] )
1913 EsOutSelect( out, es, true );
1919 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
1921 if( EsIsSelected( p_sys->es[i] ) )
1923 if( i_query == ES_OUT_RESTART_ES )
1925 if( p_sys->es[i]->p_dec )
1927 EsDestroyDecoder( out, p_sys->es[i] );
1928 EsCreateDecoder( out, p_sys->es[i] );
1933 EsUnselect( out, p_sys->es[i],
1934 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1940 if( i_query == ES_OUT_SET_ES )
1943 event.type = vlc_InputSelectedStreamChanged;
1944 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1949 case ES_OUT_SET_DEFAULT:
1951 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1955 /*p_sys->i_default_video_id = -1;*/
1956 /*p_sys->i_default_audio_id = -1;*/
1957 p_sys->i_default_sub_id = -1;
1959 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1961 /*p_sys->i_default_video_id = -1;*/
1963 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1965 /*p_sys->i_default_audio_id = -1;*/
1967 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1969 p_sys->i_default_sub_id = -1;
1973 /*if( es->fmt.i_cat == VIDEO_ES )
1974 p_sys->i_default_video_id = es->i_id;
1976 if( es->fmt.i_cat == AUDIO_ES )
1977 p_sys->i_default_audio_id = es->i_id;
1979 if( es->fmt.i_cat == SPU_ES )
1980 p_sys->i_default_sub_id = es->i_id;
1985 case ES_OUT_SET_PCR:
1986 case ES_OUT_SET_GROUP_PCR:
1988 es_out_pgrm_t *p_pgrm = NULL;
1992 if( i_query == ES_OUT_SET_PCR )
1994 p_pgrm = p_sys->p_pgrm;
1999 i_group = (int)va_arg( args, int );
2000 for( i = 0; i < p_sys->i_pgrm; i++ )
2002 if( p_sys->pgrm[i]->i_id == i_group )
2004 p_pgrm = p_sys->pgrm[i];
2009 if( p_pgrm == NULL )
2010 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2012 i_pcr = (int64_t)va_arg( args, int64_t );
2014 * TODO do not use mdate() but proper stream acquisition date */
2015 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2016 p_sys->p_input->b_can_pace_control, i_pcr, mdate() );
2017 /* Check buffering state on master clock update */
2018 if( p_sys->b_buffering && p_pgrm == p_sys->p_pgrm )
2019 EsOutDecodersStopBuffering( out, false );
2024 case ES_OUT_RESET_PCR:
2025 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2026 input_EsOutChangePosition( out );
2032 int64_t i_ts = (int64_t)va_arg( args, int64_t );
2033 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
2034 *pi_ts = input_clock_GetTS( p_sys->p_pgrm->p_clock, NULL,
2035 p_sys->p_input->i_pts_delay, i_ts );
2038 return VLC_EGENERIC;
2040 case ES_OUT_GET_GROUP:
2041 pi = (int*) va_arg( args, int* );
2043 *pi = p_sys->p_pgrm->i_id;
2045 *pi = -1; /* FIXME */
2048 case ES_OUT_SET_GROUP:
2051 i = (int) va_arg( args, int );
2052 for( j = 0; j < p_sys->i_pgrm; j++ )
2054 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2055 if( p_pgrm->i_id == i )
2057 EsOutProgramSelect( out, p_pgrm );
2061 return VLC_EGENERIC;
2064 case ES_OUT_SET_FMT:
2066 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2067 * to update the p_extra data */
2069 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2070 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2072 return VLC_EGENERIC;
2074 if( p_fmt->i_extra )
2076 es->fmt.i_extra = p_fmt->i_extra;
2077 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2078 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2083 EsDestroyDecoder( out, es );
2085 EsCreateDecoder( out, es );
2087 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2088 es->p_dec->fmt_in.p_extra =
2089 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2090 memcpy( es->p_dec->fmt_in.p_extra,
2091 p_fmt->p_extra, p_fmt->i_extra );
2098 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2100 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2103 return VLC_EGENERIC;
2105 p_sys->i_preroll_end = i_date;
2109 case ES_OUT_SET_GROUP_META:
2111 int i_group = (int)va_arg( args, int );
2112 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2114 EsOutProgramMeta( out, i_group, p_meta );
2117 case ES_OUT_SET_GROUP_EPG:
2119 int i_group = (int)va_arg( args, int );
2120 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2122 EsOutProgramEpg( out, i_group, p_epg );
2125 case ES_OUT_DEL_GROUP:
2127 int i_group = (int)va_arg( args, int );
2129 return EsOutProgramDel( out, i_group );
2133 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2134 return VLC_EGENERIC;
2138 /****************************************************************************
2139 * LanguageGetName: try to expend iso639 into plain name
2140 ****************************************************************************/
2141 static char *LanguageGetName( const char *psz_code )
2143 const iso639_lang_t *pl;
2145 if( psz_code == NULL )
2147 return strdup( "" );
2150 if( strlen( psz_code ) == 2 )
2152 pl = GetLang_1( psz_code );
2154 else if( strlen( psz_code ) == 3 )
2156 pl = GetLang_2B( psz_code );
2157 if( !strcmp( pl->psz_iso639_1, "??" ) )
2159 pl = GetLang_2T( psz_code );
2164 return strdup( psz_code );
2167 if( !strcmp( pl->psz_iso639_1, "??" ) )
2169 return strdup( psz_code );
2173 if( *pl->psz_native_name )
2175 return strdup( pl->psz_native_name );
2177 return strdup( pl->psz_eng_name );
2181 /* Get a 2 char code */
2182 static char *LanguageGetCode( const char *psz_lang )
2184 const iso639_lang_t *pl;
2186 if( psz_lang == NULL || *psz_lang == '\0' )
2187 return strdup("??");
2189 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2191 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2192 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2193 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2194 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2195 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2199 if( pl->psz_iso639_1 != NULL )
2200 return strdup( pl->psz_iso639_1 );
2202 return strdup("??");
2205 static char **LanguageSplit( const char *psz_langs )
2212 if( psz_langs == NULL ) return NULL;
2214 psz_parser = psz_dup = strdup(psz_langs);
2216 while( psz_parser && *psz_parser )
2221 psz = strchr(psz_parser, ',' );
2222 if( psz ) *psz++ = '\0';
2224 if( !strcmp( psz_parser, "any" ) )
2226 TAB_APPEND( i_psz, ppsz, strdup("any") );
2230 psz_code = LanguageGetCode( psz_parser );
2231 if( strcmp( psz_code, "??" ) )
2233 TAB_APPEND( i_psz, ppsz, psz_code );
2246 TAB_APPEND( i_psz, ppsz, NULL );
2253 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2257 if( !ppsz_langs || !psz_lang ) return -1;
2259 for( i = 0; ppsz_langs[i]; i++ )
2261 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2262 !strcasecmp( ppsz_langs[i], "any" ) )
2271 /****************************************************************************
2273 * - add meta info to the playlist item
2274 ****************************************************************************/
2275 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2277 es_out_sys_t *p_sys = out->p_sys;
2278 input_thread_t *p_input = p_sys->p_input;
2279 es_format_t *fmt = &es->fmt;
2283 /* Add stream info */
2284 if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
2287 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2288 "%.4s", (char*)&fmt->i_codec );
2290 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2291 "%s", es->psz_language );
2293 /* Add information */
2294 switch( fmt->i_cat )
2297 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2298 _("Type"), _("Audio") );
2300 if( fmt->audio.i_channels > 0 )
2301 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2302 "%u", fmt->audio.i_channels );
2304 if( fmt->audio.i_rate > 0 )
2306 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2307 _("%u Hz"), fmt->audio.i_rate );
2308 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2311 if( fmt->audio.i_bitspersample > 0 )
2312 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2313 _("Bits per sample"), "%u",
2314 fmt->audio.i_bitspersample );
2316 if( fmt->i_bitrate > 0 )
2318 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2319 _("%u kb/s"), fmt->i_bitrate / 1000 );
2320 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2325 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2326 _("Type"), _("Video") );
2328 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2329 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2330 _("Resolution"), "%ux%u",
2331 fmt->video.i_width, fmt->video.i_height );
2333 if( fmt->video.i_visible_width > 0 &&
2334 fmt->video.i_visible_height > 0 )
2335 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2336 _("Display resolution"), "%ux%u",
2337 fmt->video.i_visible_width,
2338 fmt->video.i_visible_height);
2339 if( fmt->video.i_frame_rate > 0 &&
2340 fmt->video.i_frame_rate_base > 0 )
2342 div = lldiv( (float)fmt->video.i_frame_rate /
2343 fmt->video.i_frame_rate_base * 1000000,
2345 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2346 _("Frame rate"), "%"PRId64".%06u",
2347 div.quot, (unsigned int )div.rem );
2352 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2353 _("Type"), _("Subtitle") );