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"
47 #include "../stream_output/stream_output.h"
49 #include <vlc_iso_lang.h>
50 /* FIXME we should find a better way than including that */
51 #include "../text/iso-639_def.h"
53 /*****************************************************************************
55 *****************************************************************************/
61 /* Number of es for this pgrm */
66 /* Clock for this program */
67 input_clock_t *p_clock;
70 char *psz_now_playing;
80 es_out_pgrm_t *p_pgrm;
82 /* Channel in the track type */
86 char *psz_language_code;
89 decoder_t *p_dec_record;
91 /* Fields for Video with CC */
92 bool pb_cc_present[4];
93 es_out_id_t *pp_cc_es[4];
95 /* Field for CC track from a master video */
96 es_out_id_t *p_master;
101 input_thread_t *p_input;
108 es_out_pgrm_t **pgrm;
109 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
110 es_out_pgrm_t *p_pgrm; /* Master program */
127 int i_audio_last, i_audio_id;
128 int i_sub_last, i_sub_id;
129 int i_default_sub_id; /* As specified in container; if applicable */
130 char **ppsz_audio_language;
131 char **ppsz_sub_language;
133 /* current main es */
134 es_out_id_t *p_es_audio;
135 es_out_id_t *p_es_video;
136 es_out_id_t *p_es_sub;
139 int64_t i_audio_delay;
142 /* Rate used for clock */
148 /* Current preroll */
149 mtime_t i_preroll_end;
151 /* Used for buffering */
153 mtime_t i_buffering_extra_initial;
154 mtime_t i_buffering_extra_stream;
155 mtime_t i_buffering_extra_system;
158 sout_instance_t *p_sout_record;
161 static es_out_id_t *EsOutAdd ( es_out_t *, const es_format_t * );
162 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
163 static void EsOutDel ( es_out_t *, es_out_id_t * );
164 static int EsOutControl( es_out_t *, int i_query, va_list );
165 static void EsOutDelete ( es_out_t * );
167 static void EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
168 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
169 static int EsOutSetRecord( es_out_t *, bool b_record );
171 static bool EsIsSelected( es_out_id_t *es );
172 static void EsSelect( es_out_t *out, es_out_id_t *es );
173 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
174 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
175 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
176 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
177 static void EsOutProgramsChangeRate( es_out_t *out );
178 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
179 static char *LanguageGetName( const char *psz_code );
180 static char *LanguageGetCode( const char *psz_lang );
181 static char **LanguageSplit( const char *psz_langs );
182 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
184 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
186 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
187 VLC_FOURCC('c', 'c', '1', ' '),
188 VLC_FOURCC('c', 'c', '2', ' '),
189 VLC_FOURCC('c', 'c', '3', ' '),
190 VLC_FOURCC('c', 'c', '4', ' '),
192 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
195 for( i = 0; i < 4; i++ )
197 if( fcc == EsOutFourccClosedCaptions[i] )
204 /*****************************************************************************
206 *****************************************************************************/
207 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
212 es_out_t *out = malloc( sizeof( *out ) );
216 es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
223 out->pf_add = EsOutAdd;
224 out->pf_send = EsOutSend;
225 out->pf_del = EsOutDel;
226 out->pf_control = EsOutControl;
227 out->pf_destroy = EsOutDelete;
229 out->b_sout = p_input->p->p_sout != NULL;
232 vlc_mutex_init_recursive( &p_sys->lock );
233 p_sys->p_input = p_input;
235 p_sys->b_active = false;
236 p_sys->i_mode = ES_OUT_MODE_AUTO;
239 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
240 p_sys->p_pgrm = NULL;
244 TAB_INIT( p_sys->i_es, p_sys->es );
251 var_Get( p_input, "audio-track", &val );
252 p_sys->i_audio_last = val.i_int;
254 var_Get( p_input, "sub-track", &val );
255 p_sys->i_sub_last = val.i_int;
257 p_sys->i_default_sub_id = -1;
259 if( !p_input->b_preparsing )
261 var_Get( p_input, "audio-language", &val );
262 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
263 if( p_sys->ppsz_audio_language )
265 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
266 msg_Dbg( p_input, "selected audio language[%d] %s",
267 i, p_sys->ppsz_audio_language[i] );
269 free( val.psz_string );
271 var_Get( p_input, "sub-language", &val );
272 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
273 if( p_sys->ppsz_sub_language )
275 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
276 msg_Dbg( p_input, "selected subtitle language[%d] %s",
277 i, p_sys->ppsz_sub_language[i] );
279 free( val.psz_string );
283 p_sys->ppsz_sub_language = NULL;
284 p_sys->ppsz_audio_language = NULL;
287 var_Get( p_input, "audio-track-id", &val );
288 p_sys->i_audio_id = val.i_int;
290 var_Get( p_input, "sub-track-id", &val );
291 p_sys->i_sub_id = val.i_int;
293 p_sys->p_es_audio = NULL;
294 p_sys->p_es_video = NULL;
295 p_sys->p_es_sub = NULL;
297 p_sys->i_audio_delay= 0;
298 p_sys->i_spu_delay = 0;
300 p_sys->b_paused = false;
302 p_sys->i_rate = i_rate;
304 p_sys->b_buffering = true;
305 p_sys->i_buffering_extra_initial = 0;
306 p_sys->i_buffering_extra_stream = 0;
307 p_sys->i_buffering_extra_system = 0;
308 p_sys->i_preroll_end = -1;
310 p_sys->p_sout_record = NULL;
315 /*****************************************************************************
317 *****************************************************************************/
318 static void EsOutDelete( es_out_t *out )
320 es_out_sys_t *p_sys = out->p_sys;
323 if( p_sys->p_sout_record )
324 EsOutSetRecord( out, false );
326 for( i = 0; i < p_sys->i_es; i++ )
328 if( p_sys->es[i]->p_dec )
329 input_DecoderDelete( p_sys->es[i]->p_dec );
331 free( p_sys->es[i]->psz_language );
332 free( p_sys->es[i]->psz_language_code );
333 es_format_Clean( &p_sys->es[i]->fmt );
335 free( p_sys->es[i] );
337 if( p_sys->ppsz_audio_language )
339 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
340 free( p_sys->ppsz_audio_language[i] );
341 free( p_sys->ppsz_audio_language );
343 if( p_sys->ppsz_sub_language )
345 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
346 free( p_sys->ppsz_sub_language[i] );
347 free( p_sys->ppsz_sub_language );
351 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
352 for( i = 0; i < p_sys->i_pgrm; i++ )
354 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
355 input_clock_Delete( p_pgrm->p_clock );
356 free( p_pgrm->psz_now_playing );
357 free( p_pgrm->psz_publisher );
358 free( p_pgrm->psz_name );
360 vlc_epg_Delete( p_pgrm->p_epg );
364 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
365 vlc_mutex_destroy( &p_sys->lock );
371 static mtime_t EsOutGetWakeup( es_out_t *out )
373 es_out_sys_t *p_sys = out->p_sys;
374 input_thread_t *p_input = p_sys->p_input;
379 /* We do not have a wake up date if the input cannot have its speed
380 * controlled or sout is imposing its own or while buffering
382 * FIXME for !p_input->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
383 if( !p_input->b_can_pace_control ||
384 p_input->p->b_out_pace_control ||
388 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
391 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
396 /* Special HACK, -i_id is the cat of the stream */
397 return (es_out_id_t*)((uint8_t*)NULL-i_id);
400 for( i = 0; i < out->p_sys->i_es; i++ )
402 if( out->p_sys->es[i]->i_id == i_id )
403 return out->p_sys->es[i];
408 static bool EsOutDecodersIsEmpty( es_out_t *out )
410 es_out_sys_t *p_sys = out->p_sys;
413 if( p_sys->b_buffering && p_sys->p_pgrm )
415 EsOutDecodersStopBuffering( out, true );
416 if( p_sys->b_buffering )
420 for( i = 0; i < p_sys->i_es; i++ )
422 es_out_id_t *es = p_sys->es[i];
424 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
426 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
432 static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
434 es_out_sys_t *p_sys = out->p_sys;
436 if( i_cat == AUDIO_ES )
437 p_sys->i_audio_delay = i_delay;
438 else if( i_cat == SPU_ES )
439 p_sys->i_spu_delay = i_delay;
441 for( int i = 0; i < p_sys->i_es; i++ )
442 EsOutDecoderChangeDelay( out, p_sys->es[i] );
445 static int EsOutSetRecord( es_out_t *out, bool b_record )
447 es_out_sys_t *p_sys = out->p_sys;
448 input_thread_t *p_input = p_sys->p_input;
450 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
454 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
455 if( !psz_path || *psz_path == '\0' )
458 psz_path = strdup( config_GetHomeDir() );
461 char *psz_sout = NULL; // TODO conf
463 if( !psz_sout && psz_path )
465 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
468 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
479 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
483 if( !p_sys->p_sout_record )
486 for( int i = 0; i < p_sys->i_es; i++ )
488 es_out_id_t *p_es = p_sys->es[i];
490 if( !p_es->p_dec || p_es->p_master )
493 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
494 if( p_es->p_dec_record && p_sys->b_buffering )
495 input_DecoderStartBuffering( p_es->p_dec_record );
500 for( int i = 0; i < p_sys->i_es; i++ )
502 es_out_id_t *p_es = p_sys->es[i];
504 if( !p_es->p_dec_record )
507 input_DecoderDelete( p_es->p_dec_record );
508 p_es->p_dec_record = NULL;
511 sout_DeleteInstance( p_sys->p_sout_record );
513 p_sys->p_sout_record = NULL;
518 static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
520 es_out_sys_t *p_sys = out->p_sys;
522 /* XXX the order is important */
525 EsOutDecodersChangePause( out, true, i_date );
526 EsOutProgramChangePause( out, true, i_date );
530 if( p_sys->i_buffering_extra_initial > 0 )
532 mtime_t i_stream_start;
533 mtime_t i_system_start;
534 mtime_t i_stream_duration;
535 mtime_t i_system_duration;
537 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
538 &i_stream_start, &i_system_start,
539 &i_stream_duration, &i_system_duration );
542 /* FIXME pcr != exactly what wanted */
543 const mtime_t i_used = /*(i_stream_duration - p_sys->p_input->i_pts_delay)*/ p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
546 p_sys->i_buffering_extra_initial = 0;
547 p_sys->i_buffering_extra_stream = 0;
548 p_sys->i_buffering_extra_system = 0;
550 EsOutProgramChangePause( out, false, i_date );
551 EsOutDecodersChangePause( out, false, i_date );
553 EsOutProgramsChangeRate( out );
555 p_sys->b_paused = b_paused;
558 static void EsOutChangeRate( es_out_t *out, int i_rate )
560 es_out_sys_t *p_sys = out->p_sys;
562 p_sys->i_rate = i_rate;
564 if( !p_sys->b_paused )
565 EsOutProgramsChangeRate( out );
568 static void EsOutChangePosition( es_out_t *out )
570 es_out_sys_t *p_sys = out->p_sys;
572 for( int i = 0; i < p_sys->i_es; i++ )
574 es_out_id_t *p_es = p_sys->es[i];
579 input_DecoderStartBuffering( p_es->p_dec );
581 if( p_es->p_dec_record )
582 input_DecoderStartBuffering( p_es->p_dec );
585 for( int i = 0; i < p_sys->i_pgrm; i++ )
586 input_clock_Reset( p_sys->pgrm[i]->p_clock );
588 p_sys->b_buffering = true;
589 p_sys->i_buffering_extra_initial = 0;
590 p_sys->i_buffering_extra_stream = 0;
591 p_sys->i_buffering_extra_system = 0;
592 p_sys->i_preroll_end = -1;
597 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
599 es_out_sys_t *p_sys = out->p_sys;
602 mtime_t i_stream_start;
603 mtime_t i_system_start;
604 mtime_t i_stream_duration;
605 mtime_t i_system_duration;
606 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
607 &i_stream_start, &i_system_start,
608 &i_stream_duration, &i_system_duration );
609 assert( !i_ret || b_forced );
613 mtime_t i_preroll_duration = 0;
614 if( p_sys->i_preroll_end >= 0 )
615 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
617 const mtime_t i_buffering_duration = p_sys->p_input->i_pts_delay +
619 p_sys->i_buffering_extra_stream;
621 if( i_stream_duration <= i_buffering_duration && !b_forced )
623 msg_Dbg( p_sys->p_input, "Buffering %d%%",
624 (int)(100 * i_stream_duration / i_buffering_duration ) );
628 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
629 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
630 p_sys->b_buffering = false;
631 p_sys->i_preroll_end = -1;
633 if( p_sys->i_buffering_extra_initial > 0 )
639 const mtime_t i_decoder_buffering_start = mdate();
640 for( int i = 0; i < p_sys->i_es; i++ )
642 es_out_id_t *p_es = p_sys->es[i];
646 input_DecoderWaitBuffering( p_es->p_dec );
647 if( p_es->p_dec_record )
648 input_DecoderWaitBuffering( p_es->p_dec_record );
651 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
652 (int)(mdate() - i_decoder_buffering_start)/1000 );
654 const mtime_t i_ts_delay = 10*1000 + /* FIXME CLEANUP thread wake up time*/
656 //msg_Dbg( p_sys->p_input, "==> %lld", i_ts_delay - p_sys->p_input->i_pts_delay );
657 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_ts_delay - i_buffering_duration );
659 for( int i = 0; i < p_sys->i_es; i++ )
661 es_out_id_t *p_es = p_sys->es[i];
666 input_DecoderStopBuffering( p_es->p_dec );
667 if( p_es->p_dec_record )
668 input_DecoderStopBuffering( p_es->p_dec );
671 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
673 es_out_sys_t *p_sys = out->p_sys;
675 /* Pause decoders first */
676 for( int i = 0; i < p_sys->i_es; i++ )
678 es_out_id_t *es = p_sys->es[i];
682 input_DecoderChangePause( es->p_dec, b_paused, i_date );
683 if( es->p_dec_record )
684 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
688 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
690 es_out_sys_t *p_sys = out->p_sys;
692 for( int i = 0; i < p_sys->i_pgrm; i++ )
693 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
696 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
698 es_out_sys_t *p_sys = out->p_sys;
701 if( p_es->fmt.i_cat == AUDIO_ES )
702 i_delay = p_sys->i_audio_delay;
703 else if( p_es->fmt.i_cat == SPU_ES )
704 i_delay = p_sys->i_spu_delay;
709 input_DecoderChangeDelay( p_es->p_dec, i_delay );
710 if( p_es->p_dec_record )
711 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
714 static void EsOutProgramsChangeRate( es_out_t *out )
716 es_out_sys_t *p_sys = out->p_sys;
718 for( int i = 0; i < p_sys->i_pgrm; i++ )
719 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
722 static void EsOutFrameNext( es_out_t *out )
724 es_out_sys_t *p_sys = out->p_sys;
725 es_out_id_t *p_es_video = NULL;
727 if( p_sys->b_buffering )
729 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
733 assert( p_sys->b_paused );
735 for( int i = 0; i < p_sys->i_es; i++ )
737 es_out_id_t *p_es = p_sys->es[i];
739 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
748 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
753 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
755 msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
757 if( i_duration <= 0 )
758 i_duration = 40*1000;
760 /* FIXME it is not a clean way ? */
761 if( p_sys->i_buffering_extra_initial <= 0 )
763 mtime_t i_stream_start;
764 mtime_t i_system_start;
765 mtime_t i_stream_duration;
766 mtime_t i_system_duration;
769 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
770 &i_stream_start, &i_system_start,
771 &i_stream_duration, &i_system_duration );
775 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->p_input->i_pts_delay; /* FIXME < 0 ? */
776 p_sys->i_buffering_extra_system =
777 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
780 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
782 p_sys->b_buffering = true;
783 p_sys->i_buffering_extra_system += i_duration;
784 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
785 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
786 INPUT_RATE_DEFAULT / i_rate;
788 p_sys->i_preroll_end = -1;
793 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
796 es_out_sys_t *p_sys = out->p_sys;
797 input_thread_t *p_input = p_sys->p_input;
798 const bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
799 vlc_value_t val, text;
805 var_SetInteger( p_sys->p_input, "teletext-es", -1 );
807 input_SendEventEsDel( p_input, SPU_ES, i_id );
811 /* Get the number of ES already added */
813 if( fmt->i_cat == AUDIO_ES )
814 psz_var = "audio-es";
815 else if( fmt->i_cat == VIDEO_ES )
816 psz_var = "video-es";
820 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
825 /* First one, we need to add the "Disable" choice */
826 val2.i_int = -1; text.psz_string = _("Disable");
827 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
831 /* Take care of the ES description */
832 if( fmt->psz_description && *fmt->psz_description )
834 if( psz_language && *psz_language )
836 text.psz_string = malloc( strlen( fmt->psz_description) +
837 strlen( psz_language ) + 10 );
838 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
841 else text.psz_string = strdup( fmt->psz_description );
845 if( psz_language && *psz_language )
847 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
848 text.psz_string = NULL;
852 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
853 text.psz_string = NULL;
857 input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
859 free( text.psz_string );
864 if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
865 var_SetInteger( p_sys->p_input, "teletext-es", i_id );
869 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
872 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
875 /* EsOutProgramSelect:
876 * Select a program and update the object variable
878 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
880 es_out_sys_t *p_sys = out->p_sys;
881 input_thread_t *p_input = p_sys->p_input;
884 if( p_sys->p_pgrm == p_pgrm )
885 return; /* Nothing to do */
889 es_out_pgrm_t *old = p_sys->p_pgrm;
890 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
892 for( i = 0; i < p_sys->i_es; i++ )
894 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
895 p_sys->i_mode != ES_OUT_MODE_ALL )
896 EsUnselect( out, p_sys->es[i], true );
899 p_sys->p_es_audio = NULL;
900 p_sys->p_es_sub = NULL;
901 p_sys->p_es_video = NULL;
904 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
906 /* Mark it selected */
907 p_pgrm->b_selected = true;
909 /* Switch master stream */
910 p_sys->p_pgrm = p_pgrm;
912 /* Update "program" */
913 input_SendEventProgramSelect( p_input, p_pgrm->i_id );
916 input_SendEventEsDel( p_input, AUDIO_ES, -1 );
917 input_SendEventEsDel( p_input, VIDEO_ES, -1 );
918 input_SendEventEsDel( p_input, SPU_ES, -1 );
921 var_SetInteger( p_input, "teletext-es", -1 );
923 for( i = 0; i < p_sys->i_es; i++ )
925 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
926 EsOutESVarUpdate( out, p_sys->es[i], false );
927 EsOutSelect( out, p_sys->es[i], false );
930 /* Update now playing */
931 input_item_SetNowPlaying( p_input->p->input.p_item,
932 p_pgrm->psz_now_playing );
933 input_item_SetPublisher( p_input->p->input.p_item,
934 p_pgrm->psz_publisher );
936 input_SendEventMeta( p_input );
942 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
944 es_out_sys_t *p_sys = out->p_sys;
945 input_thread_t *p_input = p_sys->p_input;
947 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
952 p_pgrm->i_id = i_group;
954 p_pgrm->b_selected = false;
955 p_pgrm->psz_name = NULL;
956 p_pgrm->psz_now_playing = NULL;
957 p_pgrm->psz_publisher = NULL;
958 p_pgrm->p_epg = NULL;
959 p_pgrm->p_clock = input_clock_New( p_input->p->input.i_cr_average, p_sys->i_rate );
960 if( !p_pgrm->p_clock )
967 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
969 /* Update "program" variable */
970 input_SendEventProgramAdd( p_input, i_group, NULL );
972 if( i_group == var_GetInteger( p_input, "program" ) )
973 EsOutProgramSelect( out, p_pgrm );
981 static int EsOutProgramDel( es_out_t *out, int i_group )
983 es_out_sys_t *p_sys = out->p_sys;
984 input_thread_t *p_input = p_sys->p_input;
985 es_out_pgrm_t *p_pgrm = NULL;
988 for( i = 0; i < p_sys->i_pgrm; i++ )
990 if( p_sys->pgrm[i]->i_id == i_group )
992 p_pgrm = p_sys->pgrm[i];
1002 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1003 i_group, p_pgrm->i_es );
1004 return VLC_EGENERIC;
1007 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1009 /* If program is selected we need to unselect it */
1010 if( p_sys->p_pgrm == p_pgrm )
1011 p_sys->p_pgrm = NULL;
1013 input_clock_Delete( p_pgrm->p_clock );
1015 free( p_pgrm->psz_name );
1016 free( p_pgrm->psz_now_playing );
1017 free( p_pgrm->psz_publisher );
1019 vlc_epg_Delete( p_pgrm->p_epg );
1022 /* Update "program" variable */
1023 input_SendEventProgramDel( p_input, i_group );
1028 /* EsOutProgramMeta:
1030 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1033 if( p_pgrm->psz_name )
1035 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1040 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1046 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
1048 es_out_sys_t *p_sys = out->p_sys;
1049 es_out_pgrm_t *p_pgrm = NULL;
1050 input_thread_t *p_input = p_sys->p_input;
1052 const char *psz_title = NULL;
1053 const char *psz_provider = NULL;
1056 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1058 /* Check against empty meta data (empty for what we handle) */
1059 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1060 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1061 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1062 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1067 for( i = 0; i < p_sys->i_pgrm; i++ )
1069 if( p_sys->pgrm[i]->i_id == i_group )
1071 p_pgrm = p_sys->pgrm[i];
1075 if( p_pgrm == NULL )
1076 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1079 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1080 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1082 /* Update the description text of the program */
1083 if( psz_title && *psz_title )
1085 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1087 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1089 /* Remove old entries */
1090 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1091 /* TODO update epg name */
1094 free( p_pgrm->psz_name );
1095 p_pgrm->psz_name = strdup( psz_title );
1098 if( psz_provider && *psz_provider )
1100 if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1105 psz_text = strdup( psz_title );
1108 /* ugly but it works */
1111 input_SendEventProgramDel( p_input, i_group );
1112 input_SendEventProgramAdd( p_input, i_group, psz_text );
1118 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1121 if( p_sys->p_pgrm == p_pgrm )
1123 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
1124 input_SendEventMeta( p_input );
1126 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1128 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1129 for( i = 0; ppsz_all_keys[i]; i++ )
1131 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
1132 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
1133 free( ppsz_all_keys[i] );
1135 free( ppsz_all_keys );
1140 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
1145 for( i = 0; i < p_src->i_event; i++ )
1147 vlc_epg_event_t *p_evt = p_src->pp_event[i];
1151 for( j = 0; j < p_dst->i_event; j++ )
1153 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
1158 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
1163 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
1166 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
1167 p_copy->i_start = p_evt->i_start;
1168 p_copy->i_duration = p_evt->i_duration;
1169 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
1170 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
1171 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
1172 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
1175 /* Update current */
1176 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
1178 /* Keep only 1 old event */
1179 if( p_dst->p_current )
1181 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
1182 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
1186 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
1188 es_out_sys_t *p_sys = out->p_sys;
1189 input_thread_t *p_input = p_sys->p_input;
1190 es_out_pgrm_t *p_pgrm = NULL;
1195 for( i = 0; i < p_sys->i_pgrm; i++ )
1197 if( p_sys->pgrm[i]->i_id == i_group )
1199 p_pgrm = p_sys->pgrm[i];
1203 if( p_pgrm == NULL )
1204 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1207 if( !p_pgrm->p_epg )
1208 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1209 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1212 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1213 #ifdef HAVE_LOCALTIME_R
1215 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
1217 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
1218 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1219 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
1221 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
1222 time_t t_start = (time_t)p_evt->i_start;
1224 char psz_start[128];
1226 localtime_r( &t_start, &tm_start );
1228 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
1229 if( p_evt->psz_short_description || p_evt->psz_description )
1230 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
1232 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
1233 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
1235 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
1237 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
1241 /* Update now playing */
1242 free( p_pgrm->psz_now_playing );
1243 p_pgrm->psz_now_playing = NULL;
1244 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
1245 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
1247 if( p_pgrm == p_sys->p_pgrm )
1249 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
1250 input_SendEventMeta( p_input );
1253 if( p_pgrm->psz_now_playing )
1255 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1256 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1257 p_pgrm->psz_now_playing );
1261 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1262 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1271 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1273 es_out_sys_t *p_sys = out->p_sys;
1274 input_thread_t *p_input = p_sys->p_input;
1276 if( fmt->i_group < 0 )
1278 msg_Err( p_input, "invalid group number" );
1282 es_out_id_t *es = malloc( sizeof( *es ) );
1283 es_out_pgrm_t *p_pgrm = NULL;
1289 vlc_mutex_lock( &p_sys->lock );
1291 /* Search the program */
1292 for( i = 0; i < p_sys->i_pgrm; i++ )
1294 if( fmt->i_group == p_sys->pgrm[i]->i_id )
1296 p_pgrm = p_sys->pgrm[i];
1300 if( p_pgrm == NULL )
1302 /* Create a new one */
1303 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1306 /* Increase ref count for program */
1310 es->p_pgrm = p_pgrm;
1311 es_format_Copy( &es->fmt, fmt );
1312 if( es->fmt.i_id < 0 )
1313 es->fmt.i_id = out->p_sys->i_id;
1314 es->i_id = es->fmt.i_id;
1316 switch( es->fmt.i_cat )
1320 audio_replay_gain_t rg;
1322 es->i_channel = p_sys->i_audio;
1324 memset( &rg, 0, sizeof(rg) );
1325 vlc_mutex_lock( &p_input->p->input.p_item->lock );
1326 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
1327 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1329 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1331 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1333 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1334 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1336 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1338 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1339 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1346 es->i_channel = p_sys->i_video;
1347 if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1348 vlc_ureduce( &es->fmt.video.i_frame_rate,
1349 &es->fmt.video.i_frame_rate_base,
1350 es->fmt.video.i_frame_rate,
1351 es->fmt.video.i_frame_rate_base, 0 );
1355 es->i_channel = p_sys->i_sub;
1362 es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1363 es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1365 es->p_dec_record = NULL;
1366 for( i = 0; i < 4; i++ )
1367 es->pb_cc_present[i] = false;
1368 es->p_master = NULL;
1370 if( es->p_pgrm == p_sys->p_pgrm )
1371 EsOutESVarUpdate( out, es, false );
1373 /* Select it if needed */
1374 EsOutSelect( out, es, false );
1377 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1378 p_sys->i_id++; /* always incremented */
1379 switch( es->fmt.i_cat )
1392 EsOutAddInfo( out, es );
1394 vlc_mutex_unlock( &p_sys->lock );
1399 static bool EsIsSelected( es_out_id_t *es )
1403 bool b_decode = false;
1404 if( es->p_master->p_dec )
1406 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1407 if( i_channel != -1 )
1408 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1414 return es->p_dec != NULL;
1417 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1419 es_out_sys_t *p_sys = out->p_sys;
1420 input_thread_t *p_input = p_sys->p_input;
1422 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1425 if( p_sys->b_buffering )
1426 input_DecoderStartBuffering( p_es->p_dec );
1428 if( !p_es->p_master && p_sys->p_sout_record )
1430 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1431 if( p_es->p_dec_record && p_sys->b_buffering )
1432 input_DecoderStartBuffering( p_es->p_dec_record );
1436 EsOutDecoderChangeDelay( out, p_es );
1438 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1445 input_DecoderDelete( p_es->p_dec );
1448 if( p_es->p_dec_record )
1450 input_DecoderDelete( p_es->p_dec_record );
1451 p_es->p_dec_record = NULL;
1455 static void EsSelect( es_out_t *out, es_out_id_t *es )
1457 es_out_sys_t *p_sys = out->p_sys;
1458 input_thread_t *p_input = p_sys->p_input;
1461 if( EsIsSelected( es ) )
1463 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1470 if( !es->p_master->p_dec )
1473 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1474 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1479 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1481 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1483 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1488 else if( es->fmt.i_cat == AUDIO_ES )
1490 var_Get( p_input, "audio", &val );
1491 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1493 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1498 if( es->fmt.i_cat == SPU_ES )
1500 var_Get( p_input, "spu", &val );
1501 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1503 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1509 EsCreateDecoder( out, es );
1511 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1515 /* Mark it as selected */
1516 input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1519 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1521 es_out_sys_t *p_sys = out->p_sys;
1522 input_thread_t *p_input = p_sys->p_input;
1524 if( !EsIsSelected( es ) )
1526 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1532 if( es->p_master->p_dec )
1534 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1535 if( i_channel != -1 )
1536 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1541 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1543 for( i = 0; i < 4; i++ )
1545 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1548 if( i_spu_id == es->pp_cc_es[i]->i_id )
1550 /* Force unselection of the CC */
1551 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1553 EsOutDel( out, es->pp_cc_es[i] );
1555 es->pb_cc_present[i] = false;
1557 EsDestroyDecoder( out, es );
1564 if( es->p_dec == NULL )
1567 /* Mark it as unselected */
1568 input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1572 * Select an ES given the current mode
1573 * XXX: you need to take a the lock before (stream.stream_lock)
1575 * \param out The es_out structure
1576 * \param es es_out_id structure
1577 * \param b_force ...
1580 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1582 es_out_sys_t *p_sys = out->p_sys;
1584 int i_cat = es->fmt.i_cat;
1586 if( !p_sys->b_active ||
1587 ( !b_force && es->fmt.i_priority < 0 ) )
1592 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1594 if( !EsIsSelected( es ) )
1595 EsSelect( out, es );
1597 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1601 var_Get( p_sys->p_input, "programs", &val );
1602 for ( i = 0; i < val.p_list->i_count; i++ )
1604 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1606 if( !EsIsSelected( es ) )
1607 EsSelect( out, es );
1611 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1613 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1617 if( es->p_pgrm != p_sys->p_pgrm )
1620 if( i_cat == AUDIO_ES )
1622 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1623 es->psz_language_code );
1625 if( p_sys->p_es_audio &&
1626 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1628 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1629 p_sys->p_es_audio->psz_language_code );
1631 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1633 i_wanted = es->i_channel;
1637 /* Select audio if (no audio selected yet)
1638 * - no audio-language
1639 * - no audio code for the ES
1640 * - audio code in the requested list */
1642 !strcmp( es->psz_language_code, "??" ) ||
1643 !p_sys->ppsz_audio_language )
1644 i_wanted = es->i_channel;
1647 if( p_sys->i_audio_last >= 0 )
1648 i_wanted = p_sys->i_audio_last;
1650 if( p_sys->i_audio_id >= 0 )
1652 if( es->i_id == p_sys->i_audio_id )
1653 i_wanted = es->i_channel;
1658 else if( i_cat == SPU_ES )
1660 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1661 es->psz_language_code );
1663 if( p_sys->p_es_sub &&
1664 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1666 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1667 p_sys->p_es_sub->psz_language_code );
1669 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1670 idx1, es->psz_language_code, idx2,
1671 p_sys->p_es_sub->psz_language_code );
1673 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1675 /* We found a SPU that matches our language request */
1676 i_wanted = es->i_channel;
1678 else if( idx1 >= 0 )
1680 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1681 idx1, es->psz_language_code );
1683 i_wanted = es->i_channel;
1685 else if( p_sys->i_default_sub_id >= 0 )
1687 if( es->i_id == p_sys->i_default_sub_id )
1688 i_wanted = es->i_channel;
1691 if( p_sys->i_sub_last >= 0 )
1692 i_wanted = p_sys->i_sub_last;
1694 if( p_sys->i_sub_id >= 0 )
1696 if( es->i_id == p_sys->i_sub_id )
1697 i_wanted = es->i_channel;
1702 else if( i_cat == VIDEO_ES )
1704 i_wanted = es->i_channel;
1707 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1708 EsSelect( out, es );
1711 /* FIXME TODO handle priority here */
1712 if( EsIsSelected( es ) )
1714 if( i_cat == AUDIO_ES )
1716 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1717 p_sys->p_es_audio &&
1718 p_sys->p_es_audio != es &&
1719 EsIsSelected( p_sys->p_es_audio ) )
1721 EsUnselect( out, p_sys->p_es_audio, false );
1723 p_sys->p_es_audio = es;
1725 else if( i_cat == SPU_ES )
1727 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1729 p_sys->p_es_sub != es &&
1730 EsIsSelected( p_sys->p_es_sub ) )
1732 EsUnselect( out, p_sys->p_es_sub, false );
1734 p_sys->p_es_sub = es;
1736 else if( i_cat == VIDEO_ES )
1738 p_sys->p_es_video = es;
1744 * Send a block for the given es_out
1746 * \param out the es_out to send from
1747 * \param es the es_out_id
1748 * \param p_block the data block to send
1750 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1752 es_out_sys_t *p_sys = out->p_sys;
1753 input_thread_t *p_input = p_sys->p_input;
1756 if( libvlc_stats( p_input ) )
1758 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1759 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1760 p_block->i_buffer, &i_total );
1761 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1762 (float)i_total, NULL );
1763 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1766 vlc_mutex_lock( &p_sys->lock );
1768 /* Mark preroll blocks */
1769 if( p_sys->i_preroll_end >= 0 )
1771 int64_t i_date = p_block->i_pts;
1773 i_date = p_block->i_dts;
1775 if( i_date < p_sys->i_preroll_end )
1776 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1779 p_block->i_rate = 0;
1783 block_Release( p_block );
1784 vlc_mutex_unlock( &p_sys->lock );
1789 if( es->p_dec_record )
1791 block_t *p_dup = block_Duplicate( p_block );
1793 input_DecoderDecode( es->p_dec_record, p_dup );
1795 input_DecoderDecode( es->p_dec, p_block );
1797 /* Check CC status */
1800 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1801 for( int i = 0; i < 4; i++ )
1803 static const vlc_fourcc_t fcc[4] = {
1804 VLC_FOURCC('c', 'c', '1', ' '),
1805 VLC_FOURCC('c', 'c', '2', ' '),
1806 VLC_FOURCC('c', 'c', '3', ' '),
1807 VLC_FOURCC('c', 'c', '4', ' '),
1811 if( es->pb_cc_present[i] || !pb_cc[i] )
1813 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1815 es_format_Init( &fmt, SPU_ES, fcc[i] );
1816 fmt.i_group = es->fmt.i_group;
1817 if( asprintf( &fmt.psz_description,
1818 _("Closed captions %u"), 1 + i ) == -1 )
1819 fmt.psz_description = NULL;
1820 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1821 es->pp_cc_es[i]->p_master = es;
1822 es_format_Clean( &fmt );
1825 es->pb_cc_present[i] = true;
1828 vlc_mutex_unlock( &p_sys->lock );
1833 /*****************************************************************************
1835 *****************************************************************************/
1836 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1838 es_out_sys_t *p_sys = out->p_sys;
1839 bool b_reselect = false;
1842 vlc_mutex_lock( &p_sys->lock );
1844 /* We don't try to reselect */
1847 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
1849 if( input_DecoderIsEmpty( es->p_dec ) &&
1850 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
1852 /* FIXME there should be a way to have auto deleted es, but there will be
1853 * a problem when another codec of the same type is created (mainly video) */
1856 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1859 if( es->p_pgrm == p_sys->p_pgrm )
1860 EsOutESVarUpdate( out, es, true );
1862 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1865 if( es->p_pgrm->i_es == 0 )
1867 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1870 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1871 p_sys->p_es_sub == es ) b_reselect = true;
1873 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1874 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1875 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1877 switch( es->fmt.i_cat )
1890 /* Re-select another track when needed */
1893 for( i = 0; i < p_sys->i_es; i++ )
1895 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1896 EsOutSelect( out, p_sys->es[i], false );
1900 free( es->psz_language );
1901 free( es->psz_language_code );
1903 es_format_Clean( &es->fmt );
1905 vlc_mutex_unlock( &p_sys->lock );
1911 * Control query handler
1913 * \param out the es_out to control
1914 * \param i_query A es_out query as defined in include/ninput.h
1915 * \param args a variable list of arguments for the query
1916 * \return VLC_SUCCESS or an error code
1918 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
1920 es_out_sys_t *p_sys = out->p_sys;
1928 case ES_OUT_SET_ES_STATE:
1929 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1930 b = (bool) va_arg( args, int );
1931 if( b && !EsIsSelected( es ) )
1933 EsSelect( out, es );
1934 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1936 else if( !b && EsIsSelected( es ) )
1938 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1943 case ES_OUT_GET_ES_STATE:
1944 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1945 pb = (bool*) va_arg( args, bool * );
1947 *pb = EsIsSelected( es );
1950 case ES_OUT_SET_ACTIVE:
1952 b = (bool) va_arg( args, int );
1953 p_sys->b_active = b;
1957 case ES_OUT_SET_MODE:
1958 i = (int) va_arg( args, int );
1959 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1960 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1964 /* Reapply policy mode */
1965 for( i = 0; i < p_sys->i_es; i++ )
1967 if( EsIsSelected( p_sys->es[i] ) )
1969 EsUnselect( out, p_sys->es[i],
1970 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1973 for( i = 0; i < p_sys->i_es; i++ )
1975 EsOutSelect( out, p_sys->es[i], false );
1979 return VLC_EGENERIC;
1982 case ES_OUT_RESTART_ES:
1986 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1990 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1992 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1994 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1999 for( i = 0; i < p_sys->i_es; i++ )
2003 if( es == p_sys->es[i] )
2005 EsOutSelect( out, es, true );
2011 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2013 if( EsIsSelected( p_sys->es[i] ) )
2015 if( i_query == ES_OUT_RESTART_ES )
2017 if( p_sys->es[i]->p_dec )
2019 EsDestroyDecoder( out, p_sys->es[i] );
2020 EsCreateDecoder( out, p_sys->es[i] );
2025 EsUnselect( out, p_sys->es[i],
2026 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2035 case ES_OUT_SET_ES_DEFAULT:
2037 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2041 /*p_sys->i_default_video_id = -1;*/
2042 /*p_sys->i_default_audio_id = -1;*/
2043 p_sys->i_default_sub_id = -1;
2045 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2047 /*p_sys->i_default_video_id = -1;*/
2049 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2051 /*p_sys->i_default_audio_id = -1;*/
2053 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2055 p_sys->i_default_sub_id = -1;
2059 /*if( es->fmt.i_cat == VIDEO_ES )
2060 p_sys->i_default_video_id = es->i_id;
2062 if( es->fmt.i_cat == AUDIO_ES )
2063 p_sys->i_default_audio_id = es->i_id;
2065 if( es->fmt.i_cat == SPU_ES )
2066 p_sys->i_default_sub_id = es->i_id;
2071 case ES_OUT_SET_PCR:
2072 case ES_OUT_SET_GROUP_PCR:
2074 es_out_pgrm_t *p_pgrm = NULL;
2078 if( i_query == ES_OUT_SET_PCR )
2080 p_pgrm = p_sys->p_pgrm;
2085 i_group = (int)va_arg( args, int );
2086 for( i = 0; i < p_sys->i_pgrm; i++ )
2088 if( p_sys->pgrm[i]->i_id == i_group )
2090 p_pgrm = p_sys->pgrm[i];
2095 if( p_pgrm == NULL )
2096 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2098 i_pcr = (int64_t)va_arg( args, int64_t );
2100 * TODO do not use mdate() but proper stream acquisition date */
2101 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2102 p_sys->p_input->b_can_pace_control || p_sys->b_buffering, i_pcr, mdate() );
2103 /* Check buffering state on master clock update */
2104 if( p_sys->b_buffering && p_pgrm == p_sys->p_pgrm )
2105 EsOutDecodersStopBuffering( out, false );
2110 case ES_OUT_RESET_PCR:
2111 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2112 EsOutChangePosition( out );
2118 int64_t i_ts = (int64_t)va_arg( args, int64_t );
2119 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
2120 *pi_ts = input_clock_GetTS( p_sys->p_pgrm->p_clock, NULL,
2121 p_sys->p_input->i_pts_delay, i_ts );
2124 return VLC_EGENERIC;
2126 case ES_OUT_SET_GROUP:
2129 i = (int) va_arg( args, int );
2130 for( j = 0; j < p_sys->i_pgrm; j++ )
2132 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2133 if( p_pgrm->i_id == i )
2135 EsOutProgramSelect( out, p_pgrm );
2139 return VLC_EGENERIC;
2142 case ES_OUT_SET_ES_FMT:
2144 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2145 * to update the p_extra data */
2147 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2148 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2150 return VLC_EGENERIC;
2152 if( p_fmt->i_extra )
2154 es->fmt.i_extra = p_fmt->i_extra;
2155 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2156 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2161 EsDestroyDecoder( out, es );
2163 EsCreateDecoder( out, es );
2165 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2166 es->p_dec->fmt_in.p_extra =
2167 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2168 memcpy( es->p_dec->fmt_in.p_extra,
2169 p_fmt->p_extra, p_fmt->i_extra );
2176 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2178 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2181 return VLC_EGENERIC;
2183 p_sys->i_preroll_end = i_date;
2187 case ES_OUT_SET_GROUP_META:
2189 int i_group = (int)va_arg( args, int );
2190 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2192 EsOutProgramMeta( out, i_group, p_meta );
2195 case ES_OUT_SET_GROUP_EPG:
2197 int i_group = (int)va_arg( args, int );
2198 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2200 EsOutProgramEpg( out, i_group, p_epg );
2203 case ES_OUT_DEL_GROUP:
2205 int i_group = (int)va_arg( args, int );
2207 return EsOutProgramDel( out, i_group );
2210 case ES_OUT_GET_WAKE_UP:
2212 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2213 *pi_wakeup = EsOutGetWakeup( out );
2217 case ES_OUT_SET_ES_BY_ID:
2218 case ES_OUT_RESTART_ES_BY_ID:
2219 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2221 const int i_id = (int)va_arg( args, int );
2222 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2227 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2228 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2229 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2233 /* TODO if the lock is made non recursive it should be changed */
2234 return es_out_Control( out, i_new_query, p_es );
2237 case ES_OUT_GET_BUFFERING:
2238 pb = (bool *)va_arg( args, bool* );
2239 *pb = p_sys->b_buffering;
2242 case ES_OUT_GET_EMPTY:
2243 pb = (bool *)va_arg( args, bool* );
2244 *pb = EsOutDecodersIsEmpty( out );
2247 case ES_OUT_SET_DELAY:
2249 const int i_cat = (int)va_arg( args, int );
2250 const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2251 EsOutSetDelay( out, i_cat, i_delay );
2255 case ES_OUT_SET_RECORD_STATE:
2256 b = (bool) va_arg( args, int );
2257 return EsOutSetRecord( out, b );
2259 case ES_OUT_SET_PAUSE_STATE:
2261 const bool b_source_paused = (bool)va_arg( args, int );
2262 const bool b_paused = (bool)va_arg( args, int );
2263 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2265 assert( !b_source_paused == !b_paused );
2266 EsOutChangePause( out, b_paused, i_date );
2271 case ES_OUT_SET_RATE:
2273 const int i_src_rate = (int)va_arg( args, int );
2274 const int i_rate = (int)va_arg( args, int );
2276 assert( i_src_rate == i_rate );
2277 EsOutChangeRate( out, i_rate );
2282 case ES_OUT_SET_TIME:
2284 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2286 assert( i_date == -1 );
2287 EsOutChangePosition( out );
2292 case ES_OUT_SET_FRAME_NEXT:
2293 EsOutFrameNext( out );
2297 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2298 return VLC_EGENERIC;
2301 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2303 es_out_sys_t *p_sys = out->p_sys;
2306 vlc_mutex_lock( &p_sys->lock );
2307 i_ret = EsOutControlLocked( out, i_query, args );
2308 vlc_mutex_unlock( &p_sys->lock );
2313 /****************************************************************************
2314 * LanguageGetName: try to expend iso639 into plain name
2315 ****************************************************************************/
2316 static char *LanguageGetName( const char *psz_code )
2318 const iso639_lang_t *pl;
2320 if( psz_code == NULL )
2322 return strdup( "" );
2325 if( strlen( psz_code ) == 2 )
2327 pl = GetLang_1( psz_code );
2329 else if( strlen( psz_code ) == 3 )
2331 pl = GetLang_2B( psz_code );
2332 if( !strcmp( pl->psz_iso639_1, "??" ) )
2334 pl = GetLang_2T( psz_code );
2339 return strdup( psz_code );
2342 if( !strcmp( pl->psz_iso639_1, "??" ) )
2344 return strdup( psz_code );
2348 if( *pl->psz_native_name )
2350 return strdup( pl->psz_native_name );
2352 return strdup( pl->psz_eng_name );
2356 /* Get a 2 char code */
2357 static char *LanguageGetCode( const char *psz_lang )
2359 const iso639_lang_t *pl;
2361 if( psz_lang == NULL || *psz_lang == '\0' )
2362 return strdup("??");
2364 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2366 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2367 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2368 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2369 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2370 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2374 if( pl->psz_iso639_1 != NULL )
2375 return strdup( pl->psz_iso639_1 );
2377 return strdup("??");
2380 static char **LanguageSplit( const char *psz_langs )
2387 if( psz_langs == NULL ) return NULL;
2389 psz_parser = psz_dup = strdup(psz_langs);
2391 while( psz_parser && *psz_parser )
2396 psz = strchr(psz_parser, ',' );
2397 if( psz ) *psz++ = '\0';
2399 if( !strcmp( psz_parser, "any" ) )
2401 TAB_APPEND( i_psz, ppsz, strdup("any") );
2405 psz_code = LanguageGetCode( psz_parser );
2406 if( strcmp( psz_code, "??" ) )
2408 TAB_APPEND( i_psz, ppsz, psz_code );
2421 TAB_APPEND( i_psz, ppsz, NULL );
2428 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2432 if( !ppsz_langs || !psz_lang ) return -1;
2434 for( i = 0; ppsz_langs[i]; i++ )
2436 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2437 !strcasecmp( ppsz_langs[i], "any" ) )
2446 /****************************************************************************
2448 * - add meta info to the playlist item
2449 ****************************************************************************/
2450 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2452 es_out_sys_t *p_sys = out->p_sys;
2453 input_thread_t *p_input = p_sys->p_input;
2454 es_format_t *fmt = &es->fmt;
2458 /* Add stream info */
2459 if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
2462 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2463 "%.4s", (char*)&fmt->i_codec );
2465 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2466 "%s", es->psz_language );
2468 /* Add information */
2469 switch( fmt->i_cat )
2472 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2473 _("Type"), _("Audio") );
2475 if( fmt->audio.i_channels > 0 )
2476 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2477 "%u", fmt->audio.i_channels );
2479 if( fmt->audio.i_rate > 0 )
2481 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2482 _("%u Hz"), fmt->audio.i_rate );
2483 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2484 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2487 if( fmt->audio.i_bitspersample > 0 )
2488 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2489 _("Bits per sample"), "%u",
2490 fmt->audio.i_bitspersample );
2492 if( fmt->i_bitrate > 0 )
2494 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2495 _("%u kb/s"), fmt->i_bitrate / 1000 );
2496 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2497 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2502 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2503 _("Type"), _("Video") );
2505 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2506 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2507 _("Resolution"), "%ux%u",
2508 fmt->video.i_width, fmt->video.i_height );
2510 if( fmt->video.i_visible_width > 0 &&
2511 fmt->video.i_visible_height > 0 )
2512 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2513 _("Display resolution"), "%ux%u",
2514 fmt->video.i_visible_width,
2515 fmt->video.i_visible_height);
2516 if( fmt->video.i_frame_rate > 0 &&
2517 fmt->video.i_frame_rate_base > 0 )
2519 div = lldiv( (float)fmt->video.i_frame_rate /
2520 fmt->video.i_frame_rate_base * 1000000,
2522 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2523 _("Frame rate"), "%"PRId64".%06u",
2524 div.quot, (unsigned int )div.rem );
2529 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2530 _("Type"), _("Subtitle") );