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>
40 #include <vlc_fourcc.h>
42 #include "input_internal.h"
48 #include "../stream_output/stream_output.h"
50 #include <vlc_iso_lang.h>
51 /* FIXME we should find a better way than including that */
52 #include "../text/iso-639_def.h"
54 /*****************************************************************************
56 *****************************************************************************/
62 /* Number of es for this pgrm */
68 /* Clock for this program */
69 input_clock_t *p_clock;
72 char *psz_now_playing;
82 es_out_pgrm_t *p_pgrm;
87 /* Channel in the track type */
91 char *psz_language_code;
94 decoder_t *p_dec_record;
96 /* Fields for Video with CC */
97 bool pb_cc_present[4];
98 es_out_id_t *pp_cc_es[4];
100 /* Field for CC track from a master video */
101 es_out_id_t *p_master;
103 /* ID for the meta data */
109 input_thread_t *p_input;
116 es_out_pgrm_t **pgrm;
117 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
118 es_out_pgrm_t *p_pgrm; /* Master program */
135 int i_audio_last, i_audio_id;
136 int i_sub_last, i_sub_id;
137 int i_default_sub_id; /* As specified in container; if applicable */
138 char **ppsz_audio_language;
139 char **ppsz_sub_language;
141 /* current main es */
142 es_out_id_t *p_es_audio;
143 es_out_id_t *p_es_video;
144 es_out_id_t *p_es_sub;
147 int64_t i_audio_delay;
150 /* Clock configuration */
157 mtime_t i_pause_date;
159 /* Current preroll */
160 mtime_t i_preroll_end;
162 /* Used for buffering */
164 mtime_t i_buffering_extra_initial;
165 mtime_t i_buffering_extra_stream;
166 mtime_t i_buffering_extra_system;
169 sout_instance_t *p_sout_record;
172 static es_out_id_t *EsOutAdd ( es_out_t *, const es_format_t * );
173 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
174 static void EsOutDel ( es_out_t *, es_out_id_t * );
175 static int EsOutControl( es_out_t *, int i_query, va_list );
176 static void EsOutDelete ( es_out_t * );
178 static void EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
179 static void EsOutUpdateInfo( es_out_t *, es_out_id_t *es, const es_format_t *, const vlc_meta_t * );
180 static int EsOutSetRecord( es_out_t *, bool b_record );
182 static bool EsIsSelected( es_out_id_t *es );
183 static void EsSelect( es_out_t *out, es_out_id_t *es );
184 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
185 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
186 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
187 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
188 static void EsOutProgramsChangeRate( es_out_t *out );
189 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
191 static char *LanguageGetName( const char *psz_code );
192 static char *LanguageGetCode( const char *psz_lang );
193 static char **LanguageSplit( const char *psz_langs );
194 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
196 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
198 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
199 VLC_FOURCC('c', 'c', '1', ' '),
200 VLC_FOURCC('c', 'c', '2', ' '),
201 VLC_FOURCC('c', 'c', '3', ' '),
202 VLC_FOURCC('c', 'c', '4', ' '),
204 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
207 for( i = 0; i < 4; i++ )
209 if( fcc == EsOutFourccClosedCaptions[i] )
214 static inline bool EsFmtIsTeletext( const es_format_t *p_fmt )
216 return p_fmt->i_cat == SPU_ES && p_fmt->i_codec == VLC_CODEC_TELETEXT;
219 /*****************************************************************************
221 *****************************************************************************/
222 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
224 es_out_t *out = malloc( sizeof( *out ) );
228 es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
235 out->pf_add = EsOutAdd;
236 out->pf_send = EsOutSend;
237 out->pf_del = EsOutDel;
238 out->pf_control = EsOutControl;
239 out->pf_destroy = EsOutDelete;
241 out->b_sout = p_input->p->p_sout != NULL;
244 vlc_mutex_init_recursive( &p_sys->lock );
245 p_sys->p_input = p_input;
247 p_sys->b_active = false;
248 p_sys->i_mode = ES_OUT_MODE_AUTO;
251 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
252 p_sys->p_pgrm = NULL;
256 TAB_INIT( p_sys->i_es, p_sys->es );
263 p_sys->i_audio_last = var_GetInteger( p_input, "audio-track" );
265 p_sys->i_sub_last = var_GetInteger( p_input, "sub-track" );
267 p_sys->i_default_sub_id = -1;
269 if( !p_input->b_preparsing )
273 psz_string = var_GetString( p_input, "audio-language" );
274 p_sys->ppsz_audio_language = LanguageSplit( psz_string );
275 if( p_sys->ppsz_audio_language )
277 for( int i = 0; p_sys->ppsz_audio_language[i]; i++ )
278 msg_Dbg( p_input, "selected audio language[%d] %s",
279 i, p_sys->ppsz_audio_language[i] );
283 psz_string = var_GetString( p_input, "sub-language" );
284 p_sys->ppsz_sub_language = LanguageSplit( psz_string );
285 if( p_sys->ppsz_sub_language )
287 for( int i = 0; p_sys->ppsz_sub_language[i]; i++ )
288 msg_Dbg( p_input, "selected subtitle language[%d] %s",
289 i, p_sys->ppsz_sub_language[i] );
295 p_sys->ppsz_sub_language = NULL;
296 p_sys->ppsz_audio_language = NULL;
299 p_sys->i_audio_id = var_GetInteger( p_input, "audio-track-id" );
301 p_sys->i_sub_id = var_GetInteger( p_input, "sub-track-id" );
303 p_sys->p_es_audio = NULL;
304 p_sys->p_es_video = NULL;
305 p_sys->p_es_sub = NULL;
307 p_sys->i_audio_delay= 0;
308 p_sys->i_spu_delay = 0;
310 p_sys->b_paused = false;
311 p_sys->i_pause_date = -1;
313 p_sys->i_rate = i_rate;
314 p_sys->i_pts_delay = 0;
315 p_sys->i_cr_average = 0;
317 p_sys->b_buffering = true;
318 p_sys->i_buffering_extra_initial = 0;
319 p_sys->i_buffering_extra_stream = 0;
320 p_sys->i_buffering_extra_system = 0;
321 p_sys->i_preroll_end = -1;
323 p_sys->p_sout_record = NULL;
328 /*****************************************************************************
330 *****************************************************************************/
331 static void EsOutDelete( es_out_t *out )
333 es_out_sys_t *p_sys = out->p_sys;
334 input_thread_t *p_input = p_sys->p_input;
337 if( p_sys->p_sout_record )
338 EsOutSetRecord( out, false );
340 for( i = 0; i < p_sys->i_es; i++ )
342 if( p_sys->es[i]->p_dec )
343 input_DecoderDelete( p_sys->es[i]->p_dec );
345 free( p_sys->es[i]->psz_language );
346 free( p_sys->es[i]->psz_language_code );
347 es_format_Clean( &p_sys->es[i]->fmt );
349 free( p_sys->es[i] );
351 if( p_sys->ppsz_audio_language )
353 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
354 free( p_sys->ppsz_audio_language[i] );
355 free( p_sys->ppsz_audio_language );
357 if( p_sys->ppsz_sub_language )
359 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
360 free( p_sys->ppsz_sub_language[i] );
361 free( p_sys->ppsz_sub_language );
365 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
366 for( i = 0; i < p_sys->i_pgrm; i++ )
368 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
369 input_clock_Delete( p_pgrm->p_clock );
371 /* Remove SDT and EPG entries */
372 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
373 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
375 if( asprintf( &psz_epg, "EPG %s", psz_cat ) >= 0 )
377 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
382 free( p_pgrm->psz_now_playing );
383 free( p_pgrm->psz_publisher );
384 free( p_pgrm->psz_name );
386 vlc_epg_Delete( p_pgrm->p_epg );
390 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
391 vlc_mutex_destroy( &p_sys->lock );
397 static mtime_t EsOutGetWakeup( es_out_t *out )
399 es_out_sys_t *p_sys = out->p_sys;
400 input_thread_t *p_input = p_sys->p_input;
405 /* We do not have a wake up date if the input cannot have its speed
406 * controlled or sout is imposing its own or while buffering
408 * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
409 if( !p_input->p->b_can_pace_control ||
410 p_input->p->b_out_pace_control ||
414 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
417 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
422 /* Special HACK, -i_id is the cat of the stream */
423 return (es_out_id_t*)((uint8_t*)NULL-i_id);
426 for( i = 0; i < out->p_sys->i_es; i++ )
428 if( out->p_sys->es[i]->i_id == i_id )
429 return out->p_sys->es[i];
434 static bool EsOutDecodersIsEmpty( es_out_t *out )
436 es_out_sys_t *p_sys = out->p_sys;
439 if( p_sys->b_buffering && p_sys->p_pgrm )
441 EsOutDecodersStopBuffering( out, true );
442 if( p_sys->b_buffering )
446 for( i = 0; i < p_sys->i_es; i++ )
448 es_out_id_t *es = p_sys->es[i];
450 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
452 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
458 static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
460 es_out_sys_t *p_sys = out->p_sys;
462 if( i_cat == AUDIO_ES )
463 p_sys->i_audio_delay = i_delay;
464 else if( i_cat == SPU_ES )
465 p_sys->i_spu_delay = i_delay;
467 for( int i = 0; i < p_sys->i_es; i++ )
468 EsOutDecoderChangeDelay( out, p_sys->es[i] );
471 static int EsOutSetRecord( es_out_t *out, bool b_record )
473 es_out_sys_t *p_sys = out->p_sys;
474 input_thread_t *p_input = p_sys->p_input;
476 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
480 char *psz_path = var_CreateGetNonEmptyString( p_input, "input-record-path" );
483 if( var_CountChoices( p_input, "video-es" ) )
484 psz_path = config_GetUserDir( VLC_VIDEOS_DIR );
485 else if( var_CountChoices( p_input, "audio-es" ) )
486 psz_path = config_GetUserDir( VLC_MUSIC_DIR );
488 psz_path = config_GetUserDir( VLC_DOWNLOAD_DIR );
491 char *psz_sout = NULL; // TODO conf
493 if( !psz_sout && psz_path )
495 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
498 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
509 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
513 if( !p_sys->p_sout_record )
516 for( int i = 0; i < p_sys->i_es; i++ )
518 es_out_id_t *p_es = p_sys->es[i];
520 if( !p_es->p_dec || p_es->p_master )
523 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
524 if( p_es->p_dec_record && p_sys->b_buffering )
525 input_DecoderStartBuffering( p_es->p_dec_record );
530 for( int i = 0; i < p_sys->i_es; i++ )
532 es_out_id_t *p_es = p_sys->es[i];
534 if( !p_es->p_dec_record )
537 input_DecoderDelete( p_es->p_dec_record );
538 p_es->p_dec_record = NULL;
541 sout_DeleteInstance( p_sys->p_sout_record );
543 p_sys->p_sout_record = NULL;
548 static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
550 es_out_sys_t *p_sys = out->p_sys;
552 /* XXX the order is important */
555 EsOutDecodersChangePause( out, true, i_date );
556 EsOutProgramChangePause( out, true, i_date );
560 if( p_sys->i_buffering_extra_initial > 0 )
562 mtime_t i_stream_start;
563 mtime_t i_system_start;
564 mtime_t i_stream_duration;
565 mtime_t i_system_duration;
567 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
568 &i_stream_start, &i_system_start,
569 &i_stream_duration, &i_system_duration );
572 /* FIXME pcr != exactly what wanted */
573 const mtime_t i_used = /*(i_stream_duration - p_sys->p_input->p->i_pts_delay)*/ p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
576 p_sys->i_buffering_extra_initial = 0;
577 p_sys->i_buffering_extra_stream = 0;
578 p_sys->i_buffering_extra_system = 0;
580 EsOutProgramChangePause( out, false, i_date );
581 EsOutDecodersChangePause( out, false, i_date );
583 EsOutProgramsChangeRate( out );
585 p_sys->b_paused = b_paused;
586 p_sys->i_pause_date = i_date;
589 static void EsOutChangeRate( es_out_t *out, int i_rate )
591 es_out_sys_t *p_sys = out->p_sys;
593 p_sys->i_rate = i_rate;
594 EsOutProgramsChangeRate( out );
597 static void EsOutChangePosition( es_out_t *out )
599 es_out_sys_t *p_sys = out->p_sys;
601 input_SendEventCache( p_sys->p_input, 0.0 );
603 for( int i = 0; i < p_sys->i_es; i++ )
605 es_out_id_t *p_es = p_sys->es[i];
610 input_DecoderStartBuffering( p_es->p_dec );
612 if( p_es->p_dec_record )
613 input_DecoderStartBuffering( p_es->p_dec_record );
616 for( int i = 0; i < p_sys->i_pgrm; i++ )
617 input_clock_Reset( p_sys->pgrm[i]->p_clock );
619 p_sys->b_buffering = true;
620 p_sys->i_buffering_extra_initial = 0;
621 p_sys->i_buffering_extra_stream = 0;
622 p_sys->i_buffering_extra_system = 0;
623 p_sys->i_preroll_end = -1;
628 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
630 es_out_sys_t *p_sys = out->p_sys;
633 mtime_t i_stream_start;
634 mtime_t i_system_start;
635 mtime_t i_stream_duration;
636 mtime_t i_system_duration;
637 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
638 &i_stream_start, &i_system_start,
639 &i_stream_duration, &i_system_duration );
640 assert( !i_ret || b_forced );
644 mtime_t i_preroll_duration = 0;
645 if( p_sys->i_preroll_end >= 0 )
646 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
648 const mtime_t i_buffering_duration = p_sys->i_pts_delay +
650 p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
652 if( i_stream_duration <= i_buffering_duration && !b_forced )
654 const double f_level = (double)i_stream_duration / i_buffering_duration;
655 input_SendEventCache( p_sys->p_input, f_level );
657 msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
660 input_SendEventCache( p_sys->p_input, 1.0 );
662 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
663 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
664 p_sys->b_buffering = false;
665 p_sys->i_preroll_end = -1;
667 if( p_sys->i_buffering_extra_initial > 0 )
673 const mtime_t i_decoder_buffering_start = mdate();
674 for( int i = 0; i < p_sys->i_es; i++ )
676 es_out_id_t *p_es = p_sys->es[i];
678 if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
680 input_DecoderWaitBuffering( p_es->p_dec );
681 if( p_es->p_dec_record )
682 input_DecoderWaitBuffering( p_es->p_dec_record );
685 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
686 (int)(mdate() - i_decoder_buffering_start)/1000 );
688 /* Here is a good place to destroy unused vout with every demuxer */
689 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
692 const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
693 const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
695 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_current_date + i_wakeup_delay - i_buffering_duration );
697 for( int i = 0; i < p_sys->i_es; i++ )
699 es_out_id_t *p_es = p_sys->es[i];
704 input_DecoderStopBuffering( p_es->p_dec );
705 if( p_es->p_dec_record )
706 input_DecoderStopBuffering( p_es->p_dec_record );
709 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
711 es_out_sys_t *p_sys = out->p_sys;
713 /* Pause decoders first */
714 for( int i = 0; i < p_sys->i_es; i++ )
716 es_out_id_t *es = p_sys->es[i];
720 input_DecoderChangePause( es->p_dec, b_paused, i_date );
721 if( es->p_dec_record )
722 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
727 static bool EsOutIsExtraBufferingAllowed( es_out_t *out )
729 es_out_sys_t *p_sys = out->p_sys;
732 for( int i = 0; i < p_sys->i_es; i++ )
734 es_out_id_t *p_es = p_sys->es[i];
737 i_size += input_DecoderGetFifoSize( p_es->p_dec );
738 if( p_es->p_dec_record )
739 i_size += input_DecoderGetFifoSize( p_es->p_dec_record );
741 //fprintf( stderr, "----- EsOutIsExtraBufferingAllowed =% 5d kbytes -- ", i_size / 1024 );
743 /* TODO maybe we want to be able to tune it ? */
744 #if defined(OPTIMIZE_MEMORY)
745 const size_t i_level_high = 500000; /* 0.5 Mbytes */
747 const size_t i_level_high = 10000000; /* 10 Mbytes */
749 return i_size < i_level_high;
752 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
754 es_out_sys_t *p_sys = out->p_sys;
756 for( int i = 0; i < p_sys->i_pgrm; i++ )
757 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
760 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
762 es_out_sys_t *p_sys = out->p_sys;
765 if( p_es->fmt.i_cat == AUDIO_ES )
766 i_delay = p_sys->i_audio_delay;
767 else if( p_es->fmt.i_cat == SPU_ES )
768 i_delay = p_sys->i_spu_delay;
773 input_DecoderChangeDelay( p_es->p_dec, i_delay );
774 if( p_es->p_dec_record )
775 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
778 static void EsOutProgramsChangeRate( es_out_t *out )
780 es_out_sys_t *p_sys = out->p_sys;
782 for( int i = 0; i < p_sys->i_pgrm; i++ )
783 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
786 static void EsOutFrameNext( es_out_t *out )
788 es_out_sys_t *p_sys = out->p_sys;
789 es_out_id_t *p_es_video = NULL;
791 if( p_sys->b_buffering )
793 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
797 assert( p_sys->b_paused );
799 for( int i = 0; i < p_sys->i_es; i++ )
801 es_out_id_t *p_es = p_sys->es[i];
803 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
812 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
817 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
819 msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
821 if( i_duration <= 0 )
822 i_duration = 40*1000;
824 /* FIXME it is not a clean way ? */
825 if( p_sys->i_buffering_extra_initial <= 0 )
827 mtime_t i_stream_start;
828 mtime_t i_system_start;
829 mtime_t i_stream_duration;
830 mtime_t i_system_duration;
833 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
834 &i_stream_start, &i_system_start,
835 &i_stream_duration, &i_system_duration );
839 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
840 p_sys->i_buffering_extra_system =
841 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
844 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
846 p_sys->b_buffering = true;
847 p_sys->i_buffering_extra_system += i_duration;
848 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
849 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
850 INPUT_RATE_DEFAULT / i_rate;
852 p_sys->i_preroll_end = -1;
854 static mtime_t EsOutGetBuffering( es_out_t *out )
856 es_out_sys_t *p_sys = out->p_sys;
862 mtime_t i_stream_start;
863 mtime_t i_system_start;
864 mtime_t i_stream_duration;
865 mtime_t i_system_duration;
866 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
867 &i_stream_start, &i_system_start,
868 &i_stream_duration, &i_system_duration );
875 if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
877 i_delay = i_stream_duration;
881 mtime_t i_system_duration;
882 if( p_sys->b_paused )
884 i_system_duration = p_sys->i_pause_date - i_system_start;
885 if( p_sys->i_buffering_extra_initial > 0 )
886 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
890 i_system_duration = mdate() - i_system_start;
893 const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
894 i_delay = p_sys->i_pts_delay - i_consumed;
901 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
902 const es_format_t *fmt, const char *psz_language,
905 es_out_sys_t *p_sys = out->p_sys;
906 input_thread_t *p_input = p_sys->p_input;
907 vlc_value_t val, text;
911 if( EsFmtIsTeletext( fmt ) )
912 input_SendEventTeletextDel( p_sys->p_input, i_id );
914 input_SendEventEsDel( p_input, fmt->i_cat, i_id );
918 /* Get the number of ES already added */
920 if( fmt->i_cat == AUDIO_ES )
921 psz_var = "audio-es";
922 else if( fmt->i_cat == VIDEO_ES )
923 psz_var = "video-es";
927 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
932 /* First one, we need to add the "Disable" choice */
933 val2.i_int = -1; text.psz_string = _("Disable");
934 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
938 /* Take care of the ES description */
939 if( fmt->psz_description && *fmt->psz_description )
941 if( psz_language && *psz_language )
943 text.psz_string = malloc( strlen( fmt->psz_description) +
944 strlen( psz_language ) + 10 );
945 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
948 else text.psz_string = strdup( fmt->psz_description );
952 if( psz_language && *psz_language )
954 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
955 text.psz_string = NULL;
959 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
960 text.psz_string = NULL;
964 input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
965 if( EsFmtIsTeletext( fmt ) )
968 snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
969 fmt->subs.teletext.i_magazine,
970 fmt->subs.teletext.i_page );
971 input_SendEventTeletextAdd( p_sys->p_input,
972 i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
975 free( text.psz_string );
978 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
981 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
984 /* EsOutProgramSelect:
985 * Select a program and update the object variable
987 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
989 es_out_sys_t *p_sys = out->p_sys;
990 input_thread_t *p_input = p_sys->p_input;
993 if( p_sys->p_pgrm == p_pgrm )
994 return; /* Nothing to do */
998 es_out_pgrm_t *old = p_sys->p_pgrm;
999 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
1001 for( i = 0; i < p_sys->i_es; i++ )
1003 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
1004 p_sys->i_mode != ES_OUT_MODE_ALL )
1005 EsUnselect( out, p_sys->es[i], true );
1008 p_sys->p_es_audio = NULL;
1009 p_sys->p_es_sub = NULL;
1010 p_sys->p_es_video = NULL;
1013 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
1015 /* Mark it selected */
1016 p_pgrm->b_selected = true;
1018 /* Switch master stream */
1019 p_sys->p_pgrm = p_pgrm;
1021 /* Update "program" */
1022 input_SendEventProgramSelect( p_input, p_pgrm->i_id );
1025 input_SendEventEsDel( p_input, AUDIO_ES, -1 );
1026 input_SendEventEsDel( p_input, VIDEO_ES, -1 );
1027 input_SendEventEsDel( p_input, SPU_ES, -1 );
1028 input_SendEventTeletextDel( p_input, -1 );
1029 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
1032 var_SetInteger( p_input, "teletext-es", -1 );
1034 for( i = 0; i < p_sys->i_es; i++ )
1036 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
1037 EsOutESVarUpdate( out, p_sys->es[i], false );
1038 EsOutSelect( out, p_sys->es[i], false );
1041 /* Update now playing */
1042 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1043 input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
1045 input_SendEventMeta( p_input );
1051 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
1053 es_out_sys_t *p_sys = out->p_sys;
1054 input_thread_t *p_input = p_sys->p_input;
1056 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1061 p_pgrm->i_id = i_group;
1063 p_pgrm->b_selected = false;
1064 p_pgrm->b_scrambled = false;
1065 p_pgrm->psz_name = NULL;
1066 p_pgrm->psz_now_playing = NULL;
1067 p_pgrm->psz_publisher = NULL;
1068 p_pgrm->p_epg = NULL;
1069 p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1070 if( !p_pgrm->p_clock )
1075 if( p_sys->b_paused )
1076 input_clock_ChangePause( p_pgrm->p_clock, p_sys->b_paused, p_sys->i_pause_date );
1077 input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1080 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1082 /* Update "program" variable */
1083 input_SendEventProgramAdd( p_input, i_group, NULL );
1085 if( i_group == var_GetInteger( p_input, "program" ) )
1086 EsOutProgramSelect( out, p_pgrm );
1094 static int EsOutProgramDel( es_out_t *out, int i_group )
1096 es_out_sys_t *p_sys = out->p_sys;
1097 input_thread_t *p_input = p_sys->p_input;
1098 es_out_pgrm_t *p_pgrm = NULL;
1101 for( i = 0; i < p_sys->i_pgrm; i++ )
1103 if( p_sys->pgrm[i]->i_id == i_group )
1105 p_pgrm = p_sys->pgrm[i];
1110 if( p_pgrm == NULL )
1111 return VLC_EGENERIC;
1115 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1116 i_group, p_pgrm->i_es );
1117 return VLC_EGENERIC;
1120 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1122 /* If program is selected we need to unselect it */
1123 if( p_sys->p_pgrm == p_pgrm )
1124 p_sys->p_pgrm = NULL;
1126 input_clock_Delete( p_pgrm->p_clock );
1128 free( p_pgrm->psz_name );
1129 free( p_pgrm->psz_now_playing );
1130 free( p_pgrm->psz_publisher );
1132 vlc_epg_Delete( p_pgrm->p_epg );
1135 /* Update "program" variable */
1136 input_SendEventProgramDel( p_input, i_group );
1143 static es_out_pgrm_t *EsOutProgramFind( es_out_t *p_out, int i_group )
1145 es_out_sys_t *p_sys = p_out->p_sys;
1147 for( int i = 0; i < p_sys->i_pgrm; i++ )
1149 if( p_sys->pgrm[i]->i_id == i_group )
1150 return p_sys->pgrm[i];
1152 return EsOutProgramAdd( p_out, i_group );
1155 /* EsOutProgramMeta:
1157 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1160 if( p_pgrm->psz_name )
1162 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1167 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1173 static void EsOutProgramMeta( es_out_t *out, int i_group, const vlc_meta_t *p_meta )
1175 es_out_sys_t *p_sys = out->p_sys;
1176 es_out_pgrm_t *p_pgrm;
1177 input_thread_t *p_input = p_sys->p_input;
1179 const char *psz_title = NULL;
1180 const char *psz_provider = NULL;
1183 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1185 /* Check against empty meta data (empty for what we handle) */
1186 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1187 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1188 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1189 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1194 p_pgrm = EsOutProgramFind( out, i_group );
1199 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1200 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1202 /* Update the description text of the program */
1203 if( psz_title && *psz_title )
1205 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1207 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1209 /* Remove old entries */
1210 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1211 /* TODO update epg name ?
1212 * TODO update scrambled info name ? */
1215 free( p_pgrm->psz_name );
1216 p_pgrm->psz_name = strdup( psz_title );
1219 if( psz_provider && *psz_provider )
1221 if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1226 psz_text = strdup( psz_title );
1229 /* ugly but it works */
1232 input_SendEventProgramDel( p_input, i_group );
1233 input_SendEventProgramAdd( p_input, i_group, psz_text );
1239 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1242 if( p_sys->p_pgrm == p_pgrm )
1244 input_item_SetPublisher( p_input->p->p_item, psz_provider );
1245 input_SendEventMeta( p_input );
1247 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1249 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1250 for( i = 0; ppsz_all_keys[i]; i++ )
1252 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1253 vlc_gettext(ppsz_all_keys[i]),
1254 vlc_dictionary_value_for_key( &p_meta->extra_tags,
1255 ppsz_all_keys[i] ) );
1256 free( ppsz_all_keys[i] );
1258 free( ppsz_all_keys );
1263 static void EsOutProgramEpg( es_out_t *out, int i_group, const vlc_epg_t *p_epg )
1265 es_out_sys_t *p_sys = out->p_sys;
1266 input_thread_t *p_input = p_sys->p_input;
1267 es_out_pgrm_t *p_pgrm;
1271 p_pgrm = EsOutProgramFind( out, i_group );
1276 if( !p_pgrm->p_epg )
1277 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1278 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1281 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1283 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1286 if( asprintf( &psz_epg, "EPG %s", psz_cat ) >= 0 )
1288 input_item_SetEpg( p_input->p->p_item, psz_epg, p_pgrm->p_epg );
1292 /* Update now playing */
1293 free( p_pgrm->psz_now_playing );
1294 p_pgrm->psz_now_playing = NULL;
1295 if( p_pgrm->p_epg->p_current && p_pgrm->p_epg->p_current->psz_name && *p_pgrm->p_epg->p_current->psz_name )
1296 p_pgrm->psz_now_playing = strdup( p_pgrm->p_epg->p_current->psz_name );
1298 if( p_pgrm == p_sys->p_pgrm )
1300 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1301 input_SendEventMeta( p_input );
1304 if( p_pgrm->psz_now_playing )
1306 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1307 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1308 p_pgrm->psz_now_playing );
1312 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1313 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1319 static void EsOutProgramUpdateScrambled( es_out_t *p_out, es_out_pgrm_t *p_pgrm )
1321 es_out_sys_t *p_sys = p_out->p_sys;
1322 input_thread_t *p_input = p_sys->p_input;
1323 bool b_scrambled = false;
1325 for( int i = 0; i < p_sys->i_es; i++ )
1327 if( p_sys->es[i]->p_pgrm == p_pgrm && p_sys->es[i]->b_scrambled )
1333 if( !p_pgrm->b_scrambled == !b_scrambled )
1336 p_pgrm->b_scrambled = b_scrambled;
1337 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1340 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Scrambled"), _("Yes") );
1342 input_Control( p_input, INPUT_DEL_INFO, psz_cat, _("Scrambled") );
1344 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, b_scrambled );
1347 static void EsOutMeta( es_out_t *p_out, const vlc_meta_t *p_meta )
1349 es_out_sys_t *p_sys = p_out->p_sys;
1350 input_thread_t *p_input = p_sys->p_input;
1352 input_item_t *p_item = input_GetItem( p_input );
1354 char *psz_title = NULL;
1355 char *psz_arturl = input_item_GetArtURL( p_item );
1357 vlc_mutex_lock( &p_item->lock );
1359 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
1360 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
1362 vlc_meta_Merge( p_item->p_meta, p_meta );
1364 if( !psz_arturl || *psz_arturl == '\0' )
1366 const char *psz_tmp = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
1368 psz_arturl = strdup( psz_tmp );
1370 vlc_mutex_unlock( &p_item->lock );
1372 if( psz_arturl && *psz_arturl )
1374 input_item_SetArtURL( p_item, psz_arturl );
1376 if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
1378 /* Don't look for art cover if sout
1379 * XXX It can change when sout has meta data support */
1380 if( p_out->b_sout && !p_input->b_preparsing )
1381 input_item_SetArtURL( p_item, "" );
1383 input_ExtractAttachmentAndCacheArt( p_input );
1390 input_item_SetName( p_item, psz_title );
1393 input_item_SetPreparsed( p_item, true );
1395 input_SendEventMeta( p_input );
1396 /* TODO handle sout meta ? */
1402 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1404 es_out_sys_t *p_sys = out->p_sys;
1405 input_thread_t *p_input = p_sys->p_input;
1407 if( fmt->i_group < 0 )
1409 msg_Err( p_input, "invalid group number" );
1413 es_out_id_t *es = malloc( sizeof( *es ) );
1414 es_out_pgrm_t *p_pgrm;
1420 vlc_mutex_lock( &p_sys->lock );
1422 /* Search the program */
1423 p_pgrm = EsOutProgramFind( out, fmt->i_group );
1426 vlc_mutex_unlock( &p_sys->lock );
1431 /* Increase ref count for program */
1435 es->p_pgrm = p_pgrm;
1436 es_format_Copy( &es->fmt, fmt );
1437 if( es->fmt.i_id < 0 )
1438 es->fmt.i_id = out->p_sys->i_id;
1439 if( !es->fmt.i_original_fourcc )
1440 es->fmt.i_original_fourcc = es->fmt.i_codec;
1441 es->fmt.i_codec = vlc_fourcc_GetCodec( es->fmt.i_cat, es->fmt.i_codec );
1443 es->i_id = es->fmt.i_id;
1444 es->i_meta_id = out->p_sys->i_id;
1445 es->b_scrambled = false;
1447 switch( es->fmt.i_cat )
1451 audio_replay_gain_t rg;
1453 es->i_channel = p_sys->i_audio;
1455 memset( &rg, 0, sizeof(rg) );
1456 vlc_mutex_lock( &p_input->p->p_item->lock );
1457 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1458 vlc_mutex_unlock( &p_input->p->p_item->lock );
1460 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1462 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1464 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1465 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1467 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1469 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1470 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1477 es->i_channel = p_sys->i_video;
1478 if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1479 vlc_ureduce( &es->fmt.video.i_frame_rate,
1480 &es->fmt.video.i_frame_rate_base,
1481 es->fmt.video.i_frame_rate,
1482 es->fmt.video.i_frame_rate_base, 0 );
1486 es->i_channel = p_sys->i_sub;
1493 es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1494 es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1496 es->p_dec_record = NULL;
1497 for( i = 0; i < 4; i++ )
1498 es->pb_cc_present[i] = false;
1499 es->p_master = NULL;
1501 if( es->p_pgrm == p_sys->p_pgrm )
1502 EsOutESVarUpdate( out, es, false );
1504 /* Select it if needed */
1505 EsOutSelect( out, es, false );
1508 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1509 p_sys->i_id++; /* always incremented */
1510 switch( es->fmt.i_cat )
1523 EsOutUpdateInfo( out, es, &es->fmt, NULL );
1525 if( es->b_scrambled )
1526 EsOutProgramUpdateScrambled( out, es->p_pgrm );
1528 vlc_mutex_unlock( &p_sys->lock );
1533 static bool EsIsSelected( es_out_id_t *es )
1537 bool b_decode = false;
1538 if( es->p_master->p_dec )
1540 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1541 if( i_channel != -1 )
1542 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1548 return es->p_dec != NULL;
1551 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1553 es_out_sys_t *p_sys = out->p_sys;
1554 input_thread_t *p_input = p_sys->p_input;
1556 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1559 if( p_sys->b_buffering )
1560 input_DecoderStartBuffering( p_es->p_dec );
1562 if( !p_es->p_master && p_sys->p_sout_record )
1564 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1565 if( p_es->p_dec_record && p_sys->b_buffering )
1566 input_DecoderStartBuffering( p_es->p_dec_record );
1570 EsOutDecoderChangeDelay( out, p_es );
1572 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1579 input_DecoderDelete( p_es->p_dec );
1582 if( p_es->p_dec_record )
1584 input_DecoderDelete( p_es->p_dec_record );
1585 p_es->p_dec_record = NULL;
1589 static void EsSelect( es_out_t *out, es_out_id_t *es )
1591 es_out_sys_t *p_sys = out->p_sys;
1592 input_thread_t *p_input = p_sys->p_input;
1594 if( EsIsSelected( es ) )
1596 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1603 if( !es->p_master->p_dec )
1606 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1607 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1612 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1614 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1616 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1621 else if( es->fmt.i_cat == AUDIO_ES )
1623 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1625 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1630 if( es->fmt.i_cat == SPU_ES )
1632 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1634 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1640 EsCreateDecoder( out, es );
1642 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1646 /* Mark it as selected */
1647 input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1648 input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1651 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1653 es_out_sys_t *p_sys = out->p_sys;
1654 input_thread_t *p_input = p_sys->p_input;
1656 if( !EsIsSelected( es ) )
1658 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1664 if( es->p_master->p_dec )
1666 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1667 if( i_channel != -1 )
1668 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1673 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1675 for( i = 0; i < 4; i++ )
1677 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1680 if( i_spu_id == es->pp_cc_es[i]->i_id )
1682 /* Force unselection of the CC */
1683 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1685 EsOutDel( out, es->pp_cc_es[i] );
1687 es->pb_cc_present[i] = false;
1689 EsDestroyDecoder( out, es );
1695 /* Mark it as unselected */
1696 input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1697 if( EsFmtIsTeletext( &es->fmt ) )
1698 input_SendEventTeletextSelect( p_input, -1 );
1702 * Select an ES given the current mode
1703 * XXX: you need to take a the lock before (stream.stream_lock)
1705 * \param out The es_out structure
1706 * \param es es_out_id structure
1707 * \param b_force ...
1710 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1712 es_out_sys_t *p_sys = out->p_sys;
1714 int i_cat = es->fmt.i_cat;
1716 if( !p_sys->b_active ||
1717 ( !b_force && es->fmt.i_priority < 0 ) )
1722 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1724 if( !EsIsSelected( es ) )
1725 EsSelect( out, es );
1727 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1731 var_Get( p_sys->p_input, "programs", &val );
1732 for ( i = 0; i < val.p_list->i_count; i++ )
1734 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1736 if( !EsIsSelected( es ) )
1737 EsSelect( out, es );
1741 var_FreeList( &val, NULL );
1743 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1747 if( es->p_pgrm != p_sys->p_pgrm )
1750 if( i_cat == AUDIO_ES )
1752 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1753 es->psz_language_code );
1755 if( p_sys->p_es_audio &&
1756 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1758 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1759 p_sys->p_es_audio->psz_language_code );
1761 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1763 i_wanted = es->i_channel;
1767 /* Select audio if (no audio selected yet)
1768 * - no audio-language
1769 * - no audio code for the ES
1770 * - audio code in the requested list */
1772 !strcmp( es->psz_language_code, "??" ) ||
1773 !p_sys->ppsz_audio_language )
1774 i_wanted = es->i_channel;
1777 if( p_sys->i_audio_last >= 0 )
1778 i_wanted = p_sys->i_audio_last;
1780 if( p_sys->i_audio_id >= 0 )
1782 if( es->i_id == p_sys->i_audio_id )
1783 i_wanted = es->i_channel;
1788 else if( i_cat == SPU_ES )
1790 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1791 es->psz_language_code );
1793 if( p_sys->p_es_sub &&
1794 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1796 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1797 p_sys->p_es_sub->psz_language_code );
1799 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1800 idx1, es->psz_language_code, idx2,
1801 p_sys->p_es_sub->psz_language_code );
1803 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1805 /* We found a SPU that matches our language request */
1806 i_wanted = es->i_channel;
1808 else if( idx1 >= 0 )
1810 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1811 idx1, es->psz_language_code );
1813 i_wanted = es->i_channel;
1815 else if( p_sys->i_default_sub_id >= 0 )
1817 if( es->i_id == p_sys->i_default_sub_id )
1818 i_wanted = es->i_channel;
1821 if( p_sys->i_sub_last >= 0 )
1822 i_wanted = p_sys->i_sub_last;
1824 if( p_sys->i_sub_id >= 0 )
1826 if( es->i_id == p_sys->i_sub_id )
1827 i_wanted = es->i_channel;
1832 else if( i_cat == VIDEO_ES )
1834 i_wanted = es->i_channel;
1837 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1838 EsSelect( out, es );
1841 /* FIXME TODO handle priority here */
1842 if( EsIsSelected( es ) )
1844 if( i_cat == AUDIO_ES )
1846 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1847 p_sys->p_es_audio &&
1848 p_sys->p_es_audio != es &&
1849 EsIsSelected( p_sys->p_es_audio ) )
1851 EsUnselect( out, p_sys->p_es_audio, false );
1853 p_sys->p_es_audio = es;
1855 else if( i_cat == SPU_ES )
1857 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1859 p_sys->p_es_sub != es &&
1860 EsIsSelected( p_sys->p_es_sub ) )
1862 EsUnselect( out, p_sys->p_es_sub, false );
1864 p_sys->p_es_sub = es;
1866 else if( i_cat == VIDEO_ES )
1868 p_sys->p_es_video = es;
1874 * Send a block for the given es_out
1876 * \param out the es_out to send from
1877 * \param es the es_out_id
1878 * \param p_block the data block to send
1880 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1882 es_out_sys_t *p_sys = out->p_sys;
1883 input_thread_t *p_input = p_sys->p_input;
1886 if( libvlc_stats( p_input ) )
1888 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1889 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1890 p_block->i_buffer, &i_total );
1891 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1892 (float)i_total, NULL );
1894 /* Update number of corrupted data packats */
1895 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
1897 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_corrupted,
1900 /* Update number of discontinuities */
1901 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1903 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_discontinuity,
1906 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1909 vlc_mutex_lock( &p_sys->lock );
1911 /* Mark preroll blocks */
1912 if( p_sys->i_preroll_end >= 0 )
1914 int64_t i_date = p_block->i_pts;
1916 i_date = p_block->i_dts;
1918 if( i_date < p_sys->i_preroll_end )
1919 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1922 p_block->i_rate = 0;
1926 block_Release( p_block );
1927 vlc_mutex_unlock( &p_sys->lock );
1931 /* Check for sout mode */
1934 /* FIXME review this, proper lock may be missing */
1935 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 &&
1936 p_input->p->b_out_pace_control )
1938 msg_Dbg( p_input, "switching to sync mode" );
1939 p_input->p->b_out_pace_control = false;
1941 else if( p_input->p->p_sout->i_out_pace_nocontrol <= 0 &&
1942 !p_input->p->b_out_pace_control )
1944 msg_Dbg( p_input, "switching to async mode" );
1945 p_input->p->b_out_pace_control = true;
1950 if( es->p_dec_record )
1952 block_t *p_dup = block_Duplicate( p_block );
1954 input_DecoderDecode( es->p_dec_record, p_dup,
1955 p_input->p->b_out_pace_control );
1957 input_DecoderDecode( es->p_dec, p_block,
1958 p_input->p->b_out_pace_control );
1960 es_format_t fmt_dsc;
1961 vlc_meta_t *p_meta_dsc;
1962 if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
1964 EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
1966 es_format_Clean( &fmt_dsc );
1968 vlc_meta_Delete( p_meta_dsc );
1971 /* Check CC status */
1974 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1975 for( int i = 0; i < 4; i++ )
1979 if( es->pb_cc_present[i] || !pb_cc[i] )
1981 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1983 es_format_Init( &fmt, SPU_ES, EsOutFourccClosedCaptions[i] );
1984 fmt.i_group = es->fmt.i_group;
1985 if( asprintf( &fmt.psz_description,
1986 _("Closed captions %u"), 1 + i ) == -1 )
1987 fmt.psz_description = NULL;
1988 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1989 es->pp_cc_es[i]->p_master = es;
1990 es_format_Clean( &fmt );
1993 es->pb_cc_present[i] = true;
1996 vlc_mutex_unlock( &p_sys->lock );
2001 /*****************************************************************************
2003 *****************************************************************************/
2004 static void EsOutDel( es_out_t *out, es_out_id_t *es )
2006 es_out_sys_t *p_sys = out->p_sys;
2007 bool b_reselect = false;
2010 vlc_mutex_lock( &p_sys->lock );
2012 /* We don't try to reselect */
2015 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
2017 if( input_DecoderIsEmpty( es->p_dec ) &&
2018 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
2020 /* FIXME there should be a way to have auto deleted es, but there will be
2021 * a problem when another codec of the same type is created (mainly video) */
2024 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2027 if( es->p_pgrm == p_sys->p_pgrm )
2028 EsOutESVarUpdate( out, es, true );
2030 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
2032 /* Update program */
2034 if( es->p_pgrm->i_es == 0 )
2035 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
2037 if( es->b_scrambled )
2038 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2041 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
2042 p_sys->p_es_sub == es ) b_reselect = true;
2044 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
2045 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
2046 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
2048 switch( es->fmt.i_cat )
2061 /* Re-select another track when needed */
2064 for( i = 0; i < p_sys->i_es; i++ )
2066 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
2067 EsOutSelect( out, p_sys->es[i], false );
2071 free( es->psz_language );
2072 free( es->psz_language_code );
2074 es_format_Clean( &es->fmt );
2076 vlc_mutex_unlock( &p_sys->lock );
2082 * Control query handler
2084 * \param out the es_out to control
2085 * \param i_query A es_out query as defined in include/ninput.h
2086 * \param args a variable list of arguments for the query
2087 * \return VLC_SUCCESS or an error code
2089 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2091 es_out_sys_t *p_sys = out->p_sys;
2099 case ES_OUT_SET_ES_STATE:
2100 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2101 b = (bool) va_arg( args, int );
2102 if( b && !EsIsSelected( es ) )
2104 EsSelect( out, es );
2105 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2107 else if( !b && EsIsSelected( es ) )
2109 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2114 case ES_OUT_GET_ES_STATE:
2115 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2116 pb = (bool*) va_arg( args, bool * );
2118 *pb = EsIsSelected( es );
2121 case ES_OUT_SET_ACTIVE:
2123 b = (bool) va_arg( args, int );
2124 if( b && !p_sys->b_active && p_sys->i_es > 0 )
2126 /* XXX Terminate vout if there are tracks but no video one.
2127 * This one is not mandatory but is he earliest place where it
2129 for( i = 0; i < p_sys->i_es; i++ )
2131 es_out_id_t *p_es = p_sys->es[i];
2132 if( p_es->fmt.i_cat == VIDEO_ES )
2135 if( i >= p_sys->i_es )
2136 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2138 p_sys->b_active = b;
2142 case ES_OUT_SET_MODE:
2143 i = (int) va_arg( args, int );
2144 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
2145 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
2149 /* Reapply policy mode */
2150 for( i = 0; i < p_sys->i_es; i++ )
2152 if( EsIsSelected( p_sys->es[i] ) )
2154 EsUnselect( out, p_sys->es[i],
2155 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2158 for( i = 0; i < p_sys->i_es; i++ )
2160 EsOutSelect( out, p_sys->es[i], false );
2164 return VLC_EGENERIC;
2167 case ES_OUT_RESTART_ES:
2171 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2175 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2177 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2179 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2184 for( i = 0; i < p_sys->i_es; i++ )
2188 if( es == p_sys->es[i] )
2190 EsOutSelect( out, es, true );
2196 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2198 if( EsIsSelected( p_sys->es[i] ) )
2200 if( i_query == ES_OUT_RESTART_ES )
2202 if( p_sys->es[i]->p_dec )
2204 EsDestroyDecoder( out, p_sys->es[i] );
2205 EsCreateDecoder( out, p_sys->es[i] );
2210 EsUnselect( out, p_sys->es[i],
2211 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2220 case ES_OUT_SET_ES_DEFAULT:
2222 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2226 /*p_sys->i_default_video_id = -1;*/
2227 /*p_sys->i_default_audio_id = -1;*/
2228 p_sys->i_default_sub_id = -1;
2230 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2232 /*p_sys->i_default_video_id = -1;*/
2234 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2236 /*p_sys->i_default_audio_id = -1;*/
2238 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2240 p_sys->i_default_sub_id = -1;
2244 /*if( es->fmt.i_cat == VIDEO_ES )
2245 p_sys->i_default_video_id = es->i_id;
2247 if( es->fmt.i_cat == AUDIO_ES )
2248 p_sys->i_default_audio_id = es->i_id;
2250 if( es->fmt.i_cat == SPU_ES )
2251 p_sys->i_default_sub_id = es->i_id;
2256 case ES_OUT_SET_PCR:
2257 case ES_OUT_SET_GROUP_PCR:
2259 es_out_pgrm_t *p_pgrm = NULL;
2263 /* Search program */
2264 if( i_query == ES_OUT_SET_PCR )
2266 p_pgrm = p_sys->p_pgrm;
2268 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2272 i_group = (int)va_arg( args, int );
2273 p_pgrm = EsOutProgramFind( out, i_group );
2276 return VLC_EGENERIC;
2278 i_pcr = (int64_t)va_arg( args, int64_t );
2279 if( i_pcr <= VLC_TS_INVALID )
2281 msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
2282 return VLC_EGENERIC;
2285 /* TODO do not use mdate() but proper stream acquisition date */
2287 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2289 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering,
2290 EsOutIsExtraBufferingAllowed( out ),
2293 if( p_pgrm == p_sys->p_pgrm )
2295 if( p_sys->b_buffering )
2297 /* Check buffering state on master clock update */
2298 EsOutDecodersStopBuffering( out, false );
2302 mtime_t i_pts_delay = input_clock_GetJitter( p_pgrm->p_clock );
2304 /* Avoid dangerously high value */
2305 const mtime_t i_pts_delay_max = 30000000;
2306 if( i_pts_delay > i_pts_delay_max )
2307 i_pts_delay = __MAX( i_pts_delay_max, p_sys->i_pts_delay );
2309 /* Force a rebufferization when we are too late */
2310 msg_Err( p_sys->p_input,
2311 "ES_OUT_SET_(GROUP_)PCR is called too late, increasing pts_delay to %d ms",
2312 (int)(i_pts_delay/1000) );
2314 /* It is not really good, as we throw away already buffered data
2315 * TODO have a mean to correctly reenter bufferization */
2316 es_out_Control( out, ES_OUT_RESET_PCR );
2318 es_out_Control( out, ES_OUT_SET_JITTER, i_pts_delay, p_sys->i_cr_average );
2324 case ES_OUT_RESET_PCR:
2325 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2326 EsOutChangePosition( out );
2329 case ES_OUT_SET_GROUP:
2332 i = (int) va_arg( args, int );
2333 for( j = 0; j < p_sys->i_pgrm; j++ )
2335 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2336 if( p_pgrm->i_id == i )
2338 EsOutProgramSelect( out, p_pgrm );
2342 return VLC_EGENERIC;
2345 case ES_OUT_SET_ES_FMT:
2347 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2348 * to update the p_extra data */
2350 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2351 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2353 return VLC_EGENERIC;
2355 if( p_fmt->i_extra )
2357 es->fmt.i_extra = p_fmt->i_extra;
2358 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2359 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2364 EsDestroyDecoder( out, es );
2366 EsCreateDecoder( out, es );
2368 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2369 es->p_dec->fmt_in.p_extra =
2370 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2371 memcpy( es->p_dec->fmt_in.p_extra,
2372 p_fmt->p_extra, p_fmt->i_extra );
2379 case ES_OUT_SET_ES_SCRAMBLED_STATE:
2381 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2382 bool b_scrambled = (bool)va_arg( args, int );
2384 if( !es->b_scrambled != !b_scrambled )
2386 es->b_scrambled = b_scrambled;
2387 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2392 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2394 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2397 return VLC_EGENERIC;
2399 p_sys->i_preroll_end = i_date;
2403 case ES_OUT_SET_GROUP_META:
2405 int i_group = (int)va_arg( args, int );
2406 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2408 EsOutProgramMeta( out, i_group, p_meta );
2411 case ES_OUT_SET_GROUP_EPG:
2413 int i_group = (int)va_arg( args, int );
2414 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
2416 EsOutProgramEpg( out, i_group, p_epg );
2420 case ES_OUT_DEL_GROUP:
2422 int i_group = (int)va_arg( args, int );
2424 return EsOutProgramDel( out, i_group );
2427 case ES_OUT_SET_META:
2429 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2431 EsOutMeta( out, p_meta );
2435 case ES_OUT_GET_WAKE_UP:
2437 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2438 *pi_wakeup = EsOutGetWakeup( out );
2442 case ES_OUT_SET_ES_BY_ID:
2443 case ES_OUT_RESTART_ES_BY_ID:
2444 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2446 const int i_id = (int)va_arg( args, int );
2447 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2452 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2453 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2454 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2458 /* TODO if the lock is made non recursive it should be changed */
2459 int i_ret = es_out_Control( out, i_new_query, p_es );
2461 /* Clean up vout after user action (in active mode only).
2462 * FIXME it does not work well with multiple video windows */
2463 if( p_sys->b_active )
2464 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2468 case ES_OUT_GET_BUFFERING:
2469 pb = (bool *)va_arg( args, bool* );
2470 *pb = p_sys->b_buffering;
2473 case ES_OUT_GET_EMPTY:
2474 pb = (bool *)va_arg( args, bool* );
2475 *pb = EsOutDecodersIsEmpty( out );
2478 case ES_OUT_SET_DELAY:
2480 const int i_cat = (int)va_arg( args, int );
2481 const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2482 EsOutSetDelay( out, i_cat, i_delay );
2486 case ES_OUT_SET_RECORD_STATE:
2487 b = (bool) va_arg( args, int );
2488 return EsOutSetRecord( out, b );
2490 case ES_OUT_SET_PAUSE_STATE:
2492 const bool b_source_paused = (bool)va_arg( args, int );
2493 const bool b_paused = (bool)va_arg( args, int );
2494 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2496 assert( !b_source_paused == !b_paused );
2497 EsOutChangePause( out, b_paused, i_date );
2502 case ES_OUT_SET_RATE:
2504 const int i_src_rate = (int)va_arg( args, int );
2505 const int i_rate = (int)va_arg( args, int );
2507 assert( i_src_rate == i_rate );
2508 EsOutChangeRate( out, i_rate );
2513 case ES_OUT_SET_TIME:
2515 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2517 assert( i_date == -1 );
2518 EsOutChangePosition( out );
2523 case ES_OUT_SET_FRAME_NEXT:
2524 EsOutFrameNext( out );
2527 case ES_OUT_SET_TIMES:
2529 double f_position = (double)va_arg( args, double );
2530 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2531 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2533 input_SendEventLength( p_sys->p_input, i_length );
2535 if( !p_sys->b_buffering )
2537 /* Fix for buffering delay */
2538 const mtime_t i_delay = EsOutGetBuffering( out );
2545 f_position -= (double)i_delay / i_length;
2546 if( f_position < 0 )
2549 input_SendEventPosition( p_sys->p_input, f_position, i_time );
2553 case ES_OUT_SET_JITTER:
2555 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2556 int i_cr_average = (int)va_arg( args, int );
2558 if( i_pts_delay == p_sys->i_pts_delay &&
2559 i_cr_average == p_sys->i_cr_average )
2562 p_sys->i_pts_delay = i_pts_delay;
2563 p_sys->i_cr_average = i_cr_average;
2565 for( int i = 0; i < p_sys->i_pgrm; i++ )
2566 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2567 i_pts_delay, i_cr_average );
2572 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2573 return VLC_EGENERIC;
2576 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2578 es_out_sys_t *p_sys = out->p_sys;
2581 vlc_mutex_lock( &p_sys->lock );
2582 i_ret = EsOutControlLocked( out, i_query, args );
2583 vlc_mutex_unlock( &p_sys->lock );
2588 /****************************************************************************
2589 * LanguageGetName: try to expend iso639 into plain name
2590 ****************************************************************************/
2591 static char *LanguageGetName( const char *psz_code )
2593 const iso639_lang_t *pl;
2595 if( psz_code == NULL )
2597 return strdup( "" );
2600 if( strlen( psz_code ) == 2 )
2602 pl = GetLang_1( psz_code );
2604 else if( strlen( psz_code ) == 3 )
2606 pl = GetLang_2B( psz_code );
2607 if( !strcmp( pl->psz_iso639_1, "??" ) )
2609 pl = GetLang_2T( psz_code );
2614 return strdup( psz_code );
2617 if( !strcmp( pl->psz_iso639_1, "??" ) )
2619 return strdup( psz_code );
2623 if( *pl->psz_native_name )
2625 return strdup( pl->psz_native_name );
2627 return strdup( pl->psz_eng_name );
2631 /* Get a 2 char code */
2632 static char *LanguageGetCode( const char *psz_lang )
2634 const iso639_lang_t *pl;
2636 if( psz_lang == NULL || *psz_lang == '\0' )
2637 return strdup("??");
2639 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
2641 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2642 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2643 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2644 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2645 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2649 if( pl->psz_eng_name != NULL )
2650 return strdup( pl->psz_iso639_1 );
2652 return strdup("??");
2655 static char **LanguageSplit( const char *psz_langs )
2662 if( psz_langs == NULL ) return NULL;
2664 psz_parser = psz_dup = strdup(psz_langs);
2666 while( psz_parser && *psz_parser )
2671 psz = strchr(psz_parser, ',' );
2672 if( psz ) *psz++ = '\0';
2674 if( !strcmp( psz_parser, "any" ) )
2676 TAB_APPEND( i_psz, ppsz, strdup("any") );
2680 psz_code = LanguageGetCode( psz_parser );
2681 if( strcmp( psz_code, "??" ) )
2683 TAB_APPEND( i_psz, ppsz, psz_code );
2696 TAB_APPEND( i_psz, ppsz, NULL );
2703 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2707 if( !ppsz_langs || !psz_lang ) return -1;
2709 for( i = 0; ppsz_langs[i]; i++ )
2711 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2712 !strcasecmp( ppsz_langs[i], "any" ) )
2721 /****************************************************************************
2723 * - add meta info to the playlist item
2724 ****************************************************************************/
2725 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2727 es_out_sys_t *p_sys = out->p_sys;
2728 input_thread_t *p_input = p_sys->p_input;
2729 const es_format_t *p_fmt_es = &es->fmt;
2733 /* Create category name */
2734 if( asprintf( &psz_cat, _("Stream %d"), es->i_meta_id ) == -1 )
2737 /* Remove previous information */
2738 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
2740 /* Add informations */
2741 const char *psz_type;
2742 switch( fmt->i_cat )
2745 psz_type = _("Audio");
2748 psz_type = _("Video");
2751 psz_type = _("Subtitle");
2759 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Type"), psz_type );
2761 if( es->i_meta_id != es->i_id )
2762 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Original ID"),
2765 const char *psz_codec_description =
2766 vlc_fourcc_GetDescription( p_fmt_es->i_cat, p_fmt_es->i_codec );
2767 const vlc_fourcc_t i_codec_fourcc = p_fmt_es->i_original_fourcc ?: p_fmt_es->i_codec;
2768 if( psz_codec_description )
2769 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2770 "%s (%.4s)", psz_codec_description, (char*)&i_codec_fourcc );
2772 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2773 "%.4s", (char*)&i_codec_fourcc );
2775 if( es->psz_language && *es->psz_language )
2776 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2777 "%s", es->psz_language );
2778 if( fmt->psz_description && *fmt->psz_description )
2779 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
2780 "%s", fmt->psz_description );
2782 switch( fmt->i_cat )
2785 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2786 _("Type"), _("Audio") );
2788 if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
2789 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2790 "%s", _( aout_FormatPrintChannels( &fmt->audio ) ) );
2791 else if( fmt->audio.i_channels > 0 )
2792 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2793 "%u", fmt->audio.i_channels );
2795 if( fmt->audio.i_rate > 0 )
2797 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2798 _("%u Hz"), fmt->audio.i_rate );
2799 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2800 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2803 unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2804 if( i_bitspersample <= 0 )
2805 i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2806 if( i_bitspersample > 0 )
2807 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2808 _("Bits per sample"), "%u",
2811 if( fmt->i_bitrate > 0 )
2813 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2814 _("%u kb/s"), fmt->i_bitrate / 1000 );
2815 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2816 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2818 for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2820 const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2821 if( !p_rg->pb_gain[i] )
2823 const char *psz_name;
2824 if( i == AUDIO_REPLAY_GAIN_TRACK )
2825 psz_name = _("Track replay gain");
2827 psz_name = _("Album replay gain");
2828 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2829 psz_name, _("%.2f dB"), p_rg->pf_gain[i] );
2834 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2835 _("Type"), _("Video") );
2837 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2838 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2839 _("Resolution"), "%ux%u",
2840 fmt->video.i_width, fmt->video.i_height );
2842 if( fmt->video.i_visible_width > 0 &&
2843 fmt->video.i_visible_height > 0 )
2844 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2845 _("Display resolution"), "%ux%u",
2846 fmt->video.i_visible_width,
2847 fmt->video.i_visible_height);
2848 if( fmt->video.i_frame_rate > 0 &&
2849 fmt->video.i_frame_rate_base > 0 )
2851 div = lldiv( (float)fmt->video.i_frame_rate /
2852 fmt->video.i_frame_rate_base * 1000000,
2855 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2856 _("Frame rate"), "%"PRId64".%06u",
2857 div.quot, (unsigned int )div.rem );
2859 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2860 _("Frame rate"), "%"PRId64, div.quot );
2865 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2866 _("Type"), _("Subtitle") );
2873 /* Append generic meta */
2876 char **ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2877 for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
2879 char *psz_key = ppsz_all_keys[i];
2880 char *psz_value = vlc_dictionary_value_for_key( &p_meta->extra_tags, psz_key );
2883 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2884 vlc_gettext(psz_key), vlc_gettext(psz_value) );
2887 free( ppsz_all_keys );