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;
336 if( p_sys->p_sout_record )
337 EsOutSetRecord( out, false );
339 for( i = 0; i < p_sys->i_es; i++ )
341 if( p_sys->es[i]->p_dec )
342 input_DecoderDelete( p_sys->es[i]->p_dec );
344 free( p_sys->es[i]->psz_language );
345 free( p_sys->es[i]->psz_language_code );
346 es_format_Clean( &p_sys->es[i]->fmt );
348 free( p_sys->es[i] );
350 if( p_sys->ppsz_audio_language )
352 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
353 free( p_sys->ppsz_audio_language[i] );
354 free( p_sys->ppsz_audio_language );
356 if( p_sys->ppsz_sub_language )
358 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
359 free( p_sys->ppsz_sub_language[i] );
360 free( p_sys->ppsz_sub_language );
364 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
365 for( i = 0; i < p_sys->i_pgrm; i++ )
367 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
368 input_clock_Delete( p_pgrm->p_clock );
369 free( p_pgrm->psz_now_playing );
370 free( p_pgrm->psz_publisher );
371 free( p_pgrm->psz_name );
373 vlc_epg_Delete( p_pgrm->p_epg );
377 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
378 vlc_mutex_destroy( &p_sys->lock );
384 static mtime_t EsOutGetWakeup( es_out_t *out )
386 es_out_sys_t *p_sys = out->p_sys;
387 input_thread_t *p_input = p_sys->p_input;
392 /* We do not have a wake up date if the input cannot have its speed
393 * controlled or sout is imposing its own or while buffering
395 * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
396 if( !p_input->p->b_can_pace_control ||
397 p_input->p->b_out_pace_control ||
401 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
404 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
409 /* Special HACK, -i_id is the cat of the stream */
410 return (es_out_id_t*)((uint8_t*)NULL-i_id);
413 for( i = 0; i < out->p_sys->i_es; i++ )
415 if( out->p_sys->es[i]->i_id == i_id )
416 return out->p_sys->es[i];
421 static bool EsOutDecodersIsEmpty( es_out_t *out )
423 es_out_sys_t *p_sys = out->p_sys;
426 if( p_sys->b_buffering && p_sys->p_pgrm )
428 EsOutDecodersStopBuffering( out, true );
429 if( p_sys->b_buffering )
433 for( i = 0; i < p_sys->i_es; i++ )
435 es_out_id_t *es = p_sys->es[i];
437 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
439 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
445 static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
447 es_out_sys_t *p_sys = out->p_sys;
449 if( i_cat == AUDIO_ES )
450 p_sys->i_audio_delay = i_delay;
451 else if( i_cat == SPU_ES )
452 p_sys->i_spu_delay = i_delay;
454 for( int i = 0; i < p_sys->i_es; i++ )
455 EsOutDecoderChangeDelay( out, p_sys->es[i] );
458 static int EsOutSetRecord( es_out_t *out, bool b_record )
460 es_out_sys_t *p_sys = out->p_sys;
461 input_thread_t *p_input = p_sys->p_input;
463 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
467 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
468 if( !psz_path || *psz_path == '\0' )
471 psz_path = strdup( config_GetHomeDir() );
474 char *psz_sout = NULL; // TODO conf
476 if( !psz_sout && psz_path )
478 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
481 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
492 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
496 if( !p_sys->p_sout_record )
499 for( int i = 0; i < p_sys->i_es; i++ )
501 es_out_id_t *p_es = p_sys->es[i];
503 if( !p_es->p_dec || p_es->p_master )
506 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
507 if( p_es->p_dec_record && p_sys->b_buffering )
508 input_DecoderStartBuffering( p_es->p_dec_record );
513 for( int i = 0; i < p_sys->i_es; i++ )
515 es_out_id_t *p_es = p_sys->es[i];
517 if( !p_es->p_dec_record )
520 input_DecoderDelete( p_es->p_dec_record );
521 p_es->p_dec_record = NULL;
524 sout_DeleteInstance( p_sys->p_sout_record );
526 p_sys->p_sout_record = NULL;
531 static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
533 es_out_sys_t *p_sys = out->p_sys;
535 /* XXX the order is important */
538 EsOutDecodersChangePause( out, true, i_date );
539 EsOutProgramChangePause( out, true, i_date );
543 if( p_sys->i_buffering_extra_initial > 0 )
545 mtime_t i_stream_start;
546 mtime_t i_system_start;
547 mtime_t i_stream_duration;
548 mtime_t i_system_duration;
550 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
551 &i_stream_start, &i_system_start,
552 &i_stream_duration, &i_system_duration );
555 /* FIXME pcr != exactly what wanted */
556 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;
559 p_sys->i_buffering_extra_initial = 0;
560 p_sys->i_buffering_extra_stream = 0;
561 p_sys->i_buffering_extra_system = 0;
563 EsOutProgramChangePause( out, false, i_date );
564 EsOutDecodersChangePause( out, false, i_date );
566 EsOutProgramsChangeRate( out );
568 p_sys->b_paused = b_paused;
569 p_sys->i_pause_date = i_date;
572 static void EsOutChangeRate( es_out_t *out, int i_rate )
574 es_out_sys_t *p_sys = out->p_sys;
576 p_sys->i_rate = i_rate;
577 EsOutProgramsChangeRate( out );
580 static void EsOutChangePosition( es_out_t *out )
582 es_out_sys_t *p_sys = out->p_sys;
584 input_SendEventCache( p_sys->p_input, 0.0 );
586 for( int i = 0; i < p_sys->i_es; i++ )
588 es_out_id_t *p_es = p_sys->es[i];
593 input_DecoderStartBuffering( p_es->p_dec );
595 if( p_es->p_dec_record )
596 input_DecoderStartBuffering( p_es->p_dec_record );
599 for( int i = 0; i < p_sys->i_pgrm; i++ )
600 input_clock_Reset( p_sys->pgrm[i]->p_clock );
602 p_sys->b_buffering = true;
603 p_sys->i_buffering_extra_initial = 0;
604 p_sys->i_buffering_extra_stream = 0;
605 p_sys->i_buffering_extra_system = 0;
606 p_sys->i_preroll_end = -1;
611 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
613 es_out_sys_t *p_sys = out->p_sys;
616 mtime_t i_stream_start;
617 mtime_t i_system_start;
618 mtime_t i_stream_duration;
619 mtime_t i_system_duration;
620 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
621 &i_stream_start, &i_system_start,
622 &i_stream_duration, &i_system_duration );
623 assert( !i_ret || b_forced );
627 mtime_t i_preroll_duration = 0;
628 if( p_sys->i_preroll_end >= 0 )
629 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
631 const mtime_t i_buffering_duration = p_sys->i_pts_delay +
633 p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
635 if( i_stream_duration <= i_buffering_duration && !b_forced )
637 const double f_level = (double)i_stream_duration / i_buffering_duration;
638 input_SendEventCache( p_sys->p_input, f_level );
640 msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
643 input_SendEventCache( p_sys->p_input, 1.0 );
645 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
646 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
647 p_sys->b_buffering = false;
648 p_sys->i_preroll_end = -1;
650 if( p_sys->i_buffering_extra_initial > 0 )
656 const mtime_t i_decoder_buffering_start = mdate();
657 for( int i = 0; i < p_sys->i_es; i++ )
659 es_out_id_t *p_es = p_sys->es[i];
661 if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
663 input_DecoderWaitBuffering( p_es->p_dec );
664 if( p_es->p_dec_record )
665 input_DecoderWaitBuffering( p_es->p_dec_record );
668 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
669 (int)(mdate() - i_decoder_buffering_start)/1000 );
671 /* Here is a good place to destroy unused vout with every demuxer */
672 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
675 const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
676 const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
678 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_current_date + i_wakeup_delay - i_buffering_duration );
680 for( int i = 0; i < p_sys->i_es; i++ )
682 es_out_id_t *p_es = p_sys->es[i];
687 input_DecoderStopBuffering( p_es->p_dec );
688 if( p_es->p_dec_record )
689 input_DecoderStopBuffering( p_es->p_dec_record );
692 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
694 es_out_sys_t *p_sys = out->p_sys;
696 /* Pause decoders first */
697 for( int i = 0; i < p_sys->i_es; i++ )
699 es_out_id_t *es = p_sys->es[i];
703 input_DecoderChangePause( es->p_dec, b_paused, i_date );
704 if( es->p_dec_record )
705 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
710 static bool EsOutIsExtraBufferingAllowed( es_out_t *out )
712 es_out_sys_t *p_sys = out->p_sys;
715 for( int i = 0; i < p_sys->i_es; i++ )
717 es_out_id_t *p_es = p_sys->es[i];
720 i_size += input_DecoderGetFifoSize( p_es->p_dec );
721 if( p_es->p_dec_record )
722 i_size += input_DecoderGetFifoSize( p_es->p_dec_record );
724 //fprintf( stderr, "----- EsOutIsExtraBufferingAllowed =% 5d kbytes -- ", i_size / 1024 );
726 /* TODO maybe we want to be able to tune it ? */
727 #if defined(OPTIMIZE_MEMORY)
728 const size_t i_level_high = 500000; /* 0.5 Mbytes */
730 const size_t i_level_high = 10000000; /* 10 Mbytes */
732 return i_size < i_level_high;
735 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
737 es_out_sys_t *p_sys = out->p_sys;
739 for( int i = 0; i < p_sys->i_pgrm; i++ )
740 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
743 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
745 es_out_sys_t *p_sys = out->p_sys;
748 if( p_es->fmt.i_cat == AUDIO_ES )
749 i_delay = p_sys->i_audio_delay;
750 else if( p_es->fmt.i_cat == SPU_ES )
751 i_delay = p_sys->i_spu_delay;
756 input_DecoderChangeDelay( p_es->p_dec, i_delay );
757 if( p_es->p_dec_record )
758 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
761 static void EsOutProgramsChangeRate( es_out_t *out )
763 es_out_sys_t *p_sys = out->p_sys;
765 for( int i = 0; i < p_sys->i_pgrm; i++ )
766 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
769 static void EsOutFrameNext( es_out_t *out )
771 es_out_sys_t *p_sys = out->p_sys;
772 es_out_id_t *p_es_video = NULL;
774 if( p_sys->b_buffering )
776 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
780 assert( p_sys->b_paused );
782 for( int i = 0; i < p_sys->i_es; i++ )
784 es_out_id_t *p_es = p_sys->es[i];
786 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
795 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
800 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
802 msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
804 if( i_duration <= 0 )
805 i_duration = 40*1000;
807 /* FIXME it is not a clean way ? */
808 if( p_sys->i_buffering_extra_initial <= 0 )
810 mtime_t i_stream_start;
811 mtime_t i_system_start;
812 mtime_t i_stream_duration;
813 mtime_t i_system_duration;
816 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
817 &i_stream_start, &i_system_start,
818 &i_stream_duration, &i_system_duration );
822 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
823 p_sys->i_buffering_extra_system =
824 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
827 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
829 p_sys->b_buffering = true;
830 p_sys->i_buffering_extra_system += i_duration;
831 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
832 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
833 INPUT_RATE_DEFAULT / i_rate;
835 p_sys->i_preroll_end = -1;
837 static mtime_t EsOutGetBuffering( es_out_t *out )
839 es_out_sys_t *p_sys = out->p_sys;
845 mtime_t i_stream_start;
846 mtime_t i_system_start;
847 mtime_t i_stream_duration;
848 mtime_t i_system_duration;
849 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
850 &i_stream_start, &i_system_start,
851 &i_stream_duration, &i_system_duration );
858 if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
860 i_delay = i_stream_duration;
864 mtime_t i_system_duration;
865 if( p_sys->b_paused )
867 i_system_duration = p_sys->i_pause_date - i_system_start;
868 if( p_sys->i_buffering_extra_initial > 0 )
869 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
873 i_system_duration = mdate() - i_system_start;
876 const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
877 i_delay = p_sys->i_pts_delay - i_consumed;
884 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
885 const es_format_t *fmt, const char *psz_language,
888 es_out_sys_t *p_sys = out->p_sys;
889 input_thread_t *p_input = p_sys->p_input;
890 vlc_value_t val, text;
894 if( EsFmtIsTeletext( fmt ) )
895 input_SendEventTeletextDel( p_sys->p_input, i_id );
897 input_SendEventEsDel( p_input, fmt->i_cat, i_id );
901 /* Get the number of ES already added */
903 if( fmt->i_cat == AUDIO_ES )
904 psz_var = "audio-es";
905 else if( fmt->i_cat == VIDEO_ES )
906 psz_var = "video-es";
910 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
915 /* First one, we need to add the "Disable" choice */
916 val2.i_int = -1; text.psz_string = _("Disable");
917 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
921 /* Take care of the ES description */
922 if( fmt->psz_description && *fmt->psz_description )
924 if( psz_language && *psz_language )
926 text.psz_string = malloc( strlen( fmt->psz_description) +
927 strlen( psz_language ) + 10 );
928 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
931 else text.psz_string = strdup( fmt->psz_description );
935 if( psz_language && *psz_language )
937 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
938 text.psz_string = NULL;
942 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
943 text.psz_string = NULL;
947 input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
948 if( EsFmtIsTeletext( fmt ) )
951 snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
952 fmt->subs.teletext.i_magazine,
953 fmt->subs.teletext.i_page );
954 input_SendEventTeletextAdd( p_sys->p_input,
955 i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
958 free( text.psz_string );
961 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
964 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
967 /* EsOutProgramSelect:
968 * Select a program and update the object variable
970 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
972 es_out_sys_t *p_sys = out->p_sys;
973 input_thread_t *p_input = p_sys->p_input;
976 if( p_sys->p_pgrm == p_pgrm )
977 return; /* Nothing to do */
981 es_out_pgrm_t *old = p_sys->p_pgrm;
982 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
984 for( i = 0; i < p_sys->i_es; i++ )
986 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
987 p_sys->i_mode != ES_OUT_MODE_ALL )
988 EsUnselect( out, p_sys->es[i], true );
991 p_sys->p_es_audio = NULL;
992 p_sys->p_es_sub = NULL;
993 p_sys->p_es_video = NULL;
996 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
998 /* Mark it selected */
999 p_pgrm->b_selected = true;
1001 /* Switch master stream */
1002 p_sys->p_pgrm = p_pgrm;
1004 /* Update "program" */
1005 input_SendEventProgramSelect( p_input, p_pgrm->i_id );
1008 input_SendEventEsDel( p_input, AUDIO_ES, -1 );
1009 input_SendEventEsDel( p_input, VIDEO_ES, -1 );
1010 input_SendEventEsDel( p_input, SPU_ES, -1 );
1011 input_SendEventTeletextDel( p_input, -1 );
1012 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
1015 var_SetInteger( p_input, "teletext-es", -1 );
1017 for( i = 0; i < p_sys->i_es; i++ )
1019 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
1020 EsOutESVarUpdate( out, p_sys->es[i], false );
1021 EsOutSelect( out, p_sys->es[i], false );
1024 /* Update now playing */
1025 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1026 input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
1028 input_SendEventMeta( p_input );
1034 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
1036 es_out_sys_t *p_sys = out->p_sys;
1037 input_thread_t *p_input = p_sys->p_input;
1039 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1044 p_pgrm->i_id = i_group;
1046 p_pgrm->b_selected = false;
1047 p_pgrm->b_scrambled = false;
1048 p_pgrm->psz_name = NULL;
1049 p_pgrm->psz_now_playing = NULL;
1050 p_pgrm->psz_publisher = NULL;
1051 p_pgrm->p_epg = NULL;
1052 p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1053 if( !p_pgrm->p_clock )
1058 if( p_sys->b_paused )
1059 input_clock_ChangePause( p_pgrm->p_clock, p_sys->b_paused, p_sys->i_pause_date );
1060 input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1063 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1065 /* Update "program" variable */
1066 input_SendEventProgramAdd( p_input, i_group, NULL );
1068 if( i_group == var_GetInteger( p_input, "program" ) )
1069 EsOutProgramSelect( out, p_pgrm );
1077 static int EsOutProgramDel( es_out_t *out, int i_group )
1079 es_out_sys_t *p_sys = out->p_sys;
1080 input_thread_t *p_input = p_sys->p_input;
1081 es_out_pgrm_t *p_pgrm = NULL;
1084 for( i = 0; i < p_sys->i_pgrm; i++ )
1086 if( p_sys->pgrm[i]->i_id == i_group )
1088 p_pgrm = p_sys->pgrm[i];
1093 if( p_pgrm == NULL )
1094 return VLC_EGENERIC;
1098 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1099 i_group, p_pgrm->i_es );
1100 return VLC_EGENERIC;
1103 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1105 /* If program is selected we need to unselect it */
1106 if( p_sys->p_pgrm == p_pgrm )
1107 p_sys->p_pgrm = NULL;
1109 input_clock_Delete( p_pgrm->p_clock );
1111 free( p_pgrm->psz_name );
1112 free( p_pgrm->psz_now_playing );
1113 free( p_pgrm->psz_publisher );
1115 vlc_epg_Delete( p_pgrm->p_epg );
1118 /* Update "program" variable */
1119 input_SendEventProgramDel( p_input, i_group );
1126 static es_out_pgrm_t *EsOutProgramFind( es_out_t *p_out, int i_group )
1128 es_out_sys_t *p_sys = p_out->p_sys;
1130 for( int i = 0; i < p_sys->i_pgrm; i++ )
1132 if( p_sys->pgrm[i]->i_id == i_group )
1133 return p_sys->pgrm[i];
1135 return EsOutProgramAdd( p_out, i_group );
1138 /* EsOutProgramMeta:
1140 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1143 if( p_pgrm->psz_name )
1145 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1150 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1156 static void EsOutProgramMeta( es_out_t *out, int i_group, const vlc_meta_t *p_meta )
1158 es_out_sys_t *p_sys = out->p_sys;
1159 es_out_pgrm_t *p_pgrm;
1160 input_thread_t *p_input = p_sys->p_input;
1162 const char *psz_title = NULL;
1163 const char *psz_provider = NULL;
1166 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1168 /* Check against empty meta data (empty for what we handle) */
1169 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1170 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1171 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1172 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1177 p_pgrm = EsOutProgramFind( out, i_group );
1182 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1183 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1185 /* Update the description text of the program */
1186 if( psz_title && *psz_title )
1188 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1190 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1192 /* Remove old entries */
1193 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1194 /* TODO update epg name ?
1195 * TODO update scrambled info name ? */
1198 free( p_pgrm->psz_name );
1199 p_pgrm->psz_name = strdup( psz_title );
1202 if( psz_provider && *psz_provider )
1204 if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1209 psz_text = strdup( psz_title );
1212 /* ugly but it works */
1215 input_SendEventProgramDel( p_input, i_group );
1216 input_SendEventProgramAdd( p_input, i_group, psz_text );
1222 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1225 if( p_sys->p_pgrm == p_pgrm )
1227 input_item_SetPublisher( p_input->p->p_item, psz_provider );
1228 input_SendEventMeta( p_input );
1230 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1232 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1233 for( i = 0; ppsz_all_keys[i]; i++ )
1235 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1236 vlc_gettext(ppsz_all_keys[i]),
1237 vlc_dictionary_value_for_key( &p_meta->extra_tags,
1238 ppsz_all_keys[i] ) );
1239 free( ppsz_all_keys[i] );
1241 free( ppsz_all_keys );
1246 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
1251 for( i = 0; i < p_src->i_event; i++ )
1253 vlc_epg_event_t *p_evt = p_src->pp_event[i];
1257 for( j = 0; j < p_dst->i_event; j++ )
1259 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
1264 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
1269 vlc_epg_event_t *p_copy = calloc( 1, sizeof(vlc_epg_event_t) );
1272 p_copy->i_start = p_evt->i_start;
1273 p_copy->i_duration = p_evt->i_duration;
1274 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
1275 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
1276 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
1277 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
1280 /* Update current */
1281 if( p_src->p_current )
1282 vlc_epg_SetCurrent( p_dst, p_src->p_current->i_start );
1284 /* Keep only 1 old event */
1285 if( p_dst->p_current )
1287 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
1288 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
1292 static void EsOutProgramEpg( es_out_t *out, int i_group, const vlc_epg_t *p_epg )
1294 es_out_sys_t *p_sys = out->p_sys;
1295 input_thread_t *p_input = p_sys->p_input;
1296 es_out_pgrm_t *p_pgrm;
1300 p_pgrm = EsOutProgramFind( out, i_group );
1305 if( !p_pgrm->p_epg )
1306 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1307 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1310 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1312 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1315 if( asprintf( &psz_epg, "EPG %s", psz_cat ) >= 0 )
1317 input_item_SetEpg( p_input->p->p_item, psz_epg, p_pgrm->p_epg );
1321 /* Update now playing */
1322 free( p_pgrm->psz_now_playing );
1323 p_pgrm->psz_now_playing = NULL;
1324 if( p_pgrm->p_epg->p_current && p_pgrm->p_epg->p_current->psz_name && *p_pgrm->p_epg->p_current->psz_name )
1325 p_pgrm->psz_now_playing = strdup( p_pgrm->p_epg->p_current->psz_name );
1327 if( p_pgrm == p_sys->p_pgrm )
1329 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1330 input_SendEventMeta( p_input );
1333 if( p_pgrm->psz_now_playing )
1335 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1336 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1337 p_pgrm->psz_now_playing );
1341 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1342 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1348 static void EsOutProgramUpdateScrambled( es_out_t *p_out, es_out_pgrm_t *p_pgrm )
1350 es_out_sys_t *p_sys = p_out->p_sys;
1351 input_thread_t *p_input = p_sys->p_input;
1352 bool b_scrambled = false;
1354 for( int i = 0; i < p_sys->i_es; i++ )
1356 if( p_sys->es[i]->p_pgrm == p_pgrm && p_sys->es[i]->b_scrambled )
1362 if( !p_pgrm->b_scrambled == !b_scrambled )
1365 p_pgrm->b_scrambled = b_scrambled;
1366 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1369 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Scrambled"), _("Yes") );
1371 input_Control( p_input, INPUT_DEL_INFO, psz_cat, _("Scrambled") );
1373 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, b_scrambled );
1376 static void EsOutMeta( es_out_t *p_out, const vlc_meta_t *p_meta )
1378 es_out_sys_t *p_sys = p_out->p_sys;
1379 input_thread_t *p_input = p_sys->p_input;
1381 input_item_t *p_item = input_GetItem( p_input );
1383 char *psz_title = NULL;
1384 char *psz_arturl = input_item_GetArtURL( p_item );
1386 vlc_mutex_lock( &p_item->lock );
1388 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
1389 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
1391 vlc_meta_Merge( p_item->p_meta, p_meta );
1393 if( !psz_arturl || *psz_arturl == '\0' )
1395 const char *psz_tmp = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
1397 psz_arturl = strdup( psz_tmp );
1399 vlc_mutex_unlock( &p_item->lock );
1401 if( psz_arturl && *psz_arturl )
1403 input_item_SetArtURL( p_item, psz_arturl );
1405 if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
1407 /* Don't look for art cover if sout
1408 * XXX It can change when sout has meta data support */
1409 if( p_out->b_sout && !p_input->b_preparsing )
1410 input_item_SetArtURL( p_item, "" );
1412 input_ExtractAttachmentAndCacheArt( p_input );
1419 input_item_SetName( p_item, psz_title );
1422 input_item_SetPreparsed( p_item, true );
1424 input_SendEventMeta( p_input );
1425 /* TODO handle sout meta ? */
1431 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1433 es_out_sys_t *p_sys = out->p_sys;
1434 input_thread_t *p_input = p_sys->p_input;
1436 if( fmt->i_group < 0 )
1438 msg_Err( p_input, "invalid group number" );
1442 es_out_id_t *es = malloc( sizeof( *es ) );
1443 es_out_pgrm_t *p_pgrm;
1449 vlc_mutex_lock( &p_sys->lock );
1451 /* Search the program */
1452 p_pgrm = EsOutProgramFind( out, fmt->i_group );
1455 vlc_mutex_unlock( &p_sys->lock );
1460 /* Increase ref count for program */
1464 es->p_pgrm = p_pgrm;
1465 es_format_Copy( &es->fmt, fmt );
1466 if( es->fmt.i_id < 0 )
1467 es->fmt.i_id = out->p_sys->i_id;
1468 if( !es->fmt.i_original_fourcc )
1469 es->fmt.i_original_fourcc = es->fmt.i_codec;
1470 es->fmt.i_codec = vlc_fourcc_GetCodec( es->fmt.i_cat, es->fmt.i_codec );
1472 es->i_id = es->fmt.i_id;
1473 es->i_meta_id = out->p_sys->i_id;
1474 es->b_scrambled = false;
1476 switch( es->fmt.i_cat )
1480 audio_replay_gain_t rg;
1482 es->i_channel = p_sys->i_audio;
1484 memset( &rg, 0, sizeof(rg) );
1485 vlc_mutex_lock( &p_input->p->p_item->lock );
1486 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1487 vlc_mutex_unlock( &p_input->p->p_item->lock );
1489 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1491 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1493 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1494 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1496 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1498 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1499 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1506 es->i_channel = p_sys->i_video;
1507 if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1508 vlc_ureduce( &es->fmt.video.i_frame_rate,
1509 &es->fmt.video.i_frame_rate_base,
1510 es->fmt.video.i_frame_rate,
1511 es->fmt.video.i_frame_rate_base, 0 );
1515 es->i_channel = p_sys->i_sub;
1522 es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1523 es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1525 es->p_dec_record = NULL;
1526 for( i = 0; i < 4; i++ )
1527 es->pb_cc_present[i] = false;
1528 es->p_master = NULL;
1530 if( es->p_pgrm == p_sys->p_pgrm )
1531 EsOutESVarUpdate( out, es, false );
1533 /* Select it if needed */
1534 EsOutSelect( out, es, false );
1537 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1538 p_sys->i_id++; /* always incremented */
1539 switch( es->fmt.i_cat )
1552 EsOutUpdateInfo( out, es, &es->fmt, NULL );
1554 if( es->b_scrambled )
1555 EsOutProgramUpdateScrambled( out, es->p_pgrm );
1557 vlc_mutex_unlock( &p_sys->lock );
1562 static bool EsIsSelected( es_out_id_t *es )
1566 bool b_decode = false;
1567 if( es->p_master->p_dec )
1569 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1570 if( i_channel != -1 )
1571 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1577 return es->p_dec != NULL;
1580 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1582 es_out_sys_t *p_sys = out->p_sys;
1583 input_thread_t *p_input = p_sys->p_input;
1585 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1588 if( p_sys->b_buffering )
1589 input_DecoderStartBuffering( p_es->p_dec );
1591 if( !p_es->p_master && p_sys->p_sout_record )
1593 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1594 if( p_es->p_dec_record && p_sys->b_buffering )
1595 input_DecoderStartBuffering( p_es->p_dec_record );
1599 EsOutDecoderChangeDelay( out, p_es );
1601 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1608 input_DecoderDelete( p_es->p_dec );
1611 if( p_es->p_dec_record )
1613 input_DecoderDelete( p_es->p_dec_record );
1614 p_es->p_dec_record = NULL;
1618 static void EsSelect( es_out_t *out, es_out_id_t *es )
1620 es_out_sys_t *p_sys = out->p_sys;
1621 input_thread_t *p_input = p_sys->p_input;
1623 if( EsIsSelected( es ) )
1625 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1632 if( !es->p_master->p_dec )
1635 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1636 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1641 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1643 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1645 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1650 else if( es->fmt.i_cat == AUDIO_ES )
1652 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1654 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1659 if( es->fmt.i_cat == SPU_ES )
1661 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1663 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1669 EsCreateDecoder( out, es );
1671 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1675 /* Mark it as selected */
1676 input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1677 input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1680 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1682 es_out_sys_t *p_sys = out->p_sys;
1683 input_thread_t *p_input = p_sys->p_input;
1685 if( !EsIsSelected( es ) )
1687 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1693 if( es->p_master->p_dec )
1695 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1696 if( i_channel != -1 )
1697 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1702 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1704 for( i = 0; i < 4; i++ )
1706 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1709 if( i_spu_id == es->pp_cc_es[i]->i_id )
1711 /* Force unselection of the CC */
1712 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1714 EsOutDel( out, es->pp_cc_es[i] );
1716 es->pb_cc_present[i] = false;
1718 EsDestroyDecoder( out, es );
1724 /* Mark it as unselected */
1725 input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1726 if( EsFmtIsTeletext( &es->fmt ) )
1727 input_SendEventTeletextSelect( p_input, -1 );
1731 * Select an ES given the current mode
1732 * XXX: you need to take a the lock before (stream.stream_lock)
1734 * \param out The es_out structure
1735 * \param es es_out_id structure
1736 * \param b_force ...
1739 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1741 es_out_sys_t *p_sys = out->p_sys;
1743 int i_cat = es->fmt.i_cat;
1745 if( !p_sys->b_active ||
1746 ( !b_force && es->fmt.i_priority < 0 ) )
1751 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1753 if( !EsIsSelected( es ) )
1754 EsSelect( out, es );
1756 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1760 var_Get( p_sys->p_input, "programs", &val );
1761 for ( i = 0; i < val.p_list->i_count; i++ )
1763 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1765 if( !EsIsSelected( es ) )
1766 EsSelect( out, es );
1770 var_FreeList( &val, NULL );
1772 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1776 if( es->p_pgrm != p_sys->p_pgrm )
1779 if( i_cat == AUDIO_ES )
1781 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1782 es->psz_language_code );
1784 if( p_sys->p_es_audio &&
1785 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1787 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1788 p_sys->p_es_audio->psz_language_code );
1790 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1792 i_wanted = es->i_channel;
1796 /* Select audio if (no audio selected yet)
1797 * - no audio-language
1798 * - no audio code for the ES
1799 * - audio code in the requested list */
1801 !strcmp( es->psz_language_code, "??" ) ||
1802 !p_sys->ppsz_audio_language )
1803 i_wanted = es->i_channel;
1806 if( p_sys->i_audio_last >= 0 )
1807 i_wanted = p_sys->i_audio_last;
1809 if( p_sys->i_audio_id >= 0 )
1811 if( es->i_id == p_sys->i_audio_id )
1812 i_wanted = es->i_channel;
1817 else if( i_cat == SPU_ES )
1819 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1820 es->psz_language_code );
1822 if( p_sys->p_es_sub &&
1823 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1825 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1826 p_sys->p_es_sub->psz_language_code );
1828 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1829 idx1, es->psz_language_code, idx2,
1830 p_sys->p_es_sub->psz_language_code );
1832 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1834 /* We found a SPU that matches our language request */
1835 i_wanted = es->i_channel;
1837 else if( idx1 >= 0 )
1839 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1840 idx1, es->psz_language_code );
1842 i_wanted = es->i_channel;
1844 else if( p_sys->i_default_sub_id >= 0 )
1846 if( es->i_id == p_sys->i_default_sub_id )
1847 i_wanted = es->i_channel;
1850 if( p_sys->i_sub_last >= 0 )
1851 i_wanted = p_sys->i_sub_last;
1853 if( p_sys->i_sub_id >= 0 )
1855 if( es->i_id == p_sys->i_sub_id )
1856 i_wanted = es->i_channel;
1861 else if( i_cat == VIDEO_ES )
1863 i_wanted = es->i_channel;
1866 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1867 EsSelect( out, es );
1870 /* FIXME TODO handle priority here */
1871 if( EsIsSelected( es ) )
1873 if( i_cat == AUDIO_ES )
1875 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1876 p_sys->p_es_audio &&
1877 p_sys->p_es_audio != es &&
1878 EsIsSelected( p_sys->p_es_audio ) )
1880 EsUnselect( out, p_sys->p_es_audio, false );
1882 p_sys->p_es_audio = es;
1884 else if( i_cat == SPU_ES )
1886 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1888 p_sys->p_es_sub != es &&
1889 EsIsSelected( p_sys->p_es_sub ) )
1891 EsUnselect( out, p_sys->p_es_sub, false );
1893 p_sys->p_es_sub = es;
1895 else if( i_cat == VIDEO_ES )
1897 p_sys->p_es_video = es;
1903 * Send a block for the given es_out
1905 * \param out the es_out to send from
1906 * \param es the es_out_id
1907 * \param p_block the data block to send
1909 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1911 es_out_sys_t *p_sys = out->p_sys;
1912 input_thread_t *p_input = p_sys->p_input;
1915 if( libvlc_stats( p_input ) )
1917 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1918 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1919 p_block->i_buffer, &i_total );
1920 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1921 (float)i_total, NULL );
1923 /* Update number of corrupted data packats */
1924 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
1926 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_corrupted,
1929 /* Update number of discontinuities */
1930 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1932 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_discontinuity,
1935 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1938 vlc_mutex_lock( &p_sys->lock );
1940 /* Mark preroll blocks */
1941 if( p_sys->i_preroll_end >= 0 )
1943 int64_t i_date = p_block->i_pts;
1945 i_date = p_block->i_dts;
1947 if( i_date < p_sys->i_preroll_end )
1948 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1951 p_block->i_rate = 0;
1955 block_Release( p_block );
1956 vlc_mutex_unlock( &p_sys->lock );
1960 /* Check for sout mode */
1963 /* FIXME review this, proper lock may be missing */
1964 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 &&
1965 p_input->p->b_out_pace_control )
1967 msg_Dbg( p_input, "switching to sync mode" );
1968 p_input->p->b_out_pace_control = false;
1970 else if( p_input->p->p_sout->i_out_pace_nocontrol <= 0 &&
1971 !p_input->p->b_out_pace_control )
1973 msg_Dbg( p_input, "switching to async mode" );
1974 p_input->p->b_out_pace_control = true;
1979 if( es->p_dec_record )
1981 block_t *p_dup = block_Duplicate( p_block );
1983 input_DecoderDecode( es->p_dec_record, p_dup,
1984 p_input->p->b_out_pace_control );
1986 input_DecoderDecode( es->p_dec, p_block,
1987 p_input->p->b_out_pace_control );
1989 es_format_t fmt_dsc;
1990 vlc_meta_t *p_meta_dsc;
1991 if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
1993 EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
1995 es_format_Clean( &fmt_dsc );
1997 vlc_meta_Delete( p_meta_dsc );
2000 /* Check CC status */
2003 input_DecoderIsCcPresent( es->p_dec, pb_cc );
2004 for( int i = 0; i < 4; i++ )
2008 if( es->pb_cc_present[i] || !pb_cc[i] )
2010 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
2012 es_format_Init( &fmt, SPU_ES, EsOutFourccClosedCaptions[i] );
2013 fmt.i_group = es->fmt.i_group;
2014 if( asprintf( &fmt.psz_description,
2015 _("Closed captions %u"), 1 + i ) == -1 )
2016 fmt.psz_description = NULL;
2017 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
2018 es->pp_cc_es[i]->p_master = es;
2019 es_format_Clean( &fmt );
2022 es->pb_cc_present[i] = true;
2025 vlc_mutex_unlock( &p_sys->lock );
2030 /*****************************************************************************
2032 *****************************************************************************/
2033 static void EsOutDel( es_out_t *out, es_out_id_t *es )
2035 es_out_sys_t *p_sys = out->p_sys;
2036 bool b_reselect = false;
2039 vlc_mutex_lock( &p_sys->lock );
2041 /* We don't try to reselect */
2044 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
2046 if( input_DecoderIsEmpty( es->p_dec ) &&
2047 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
2049 /* FIXME there should be a way to have auto deleted es, but there will be
2050 * a problem when another codec of the same type is created (mainly video) */
2053 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2056 if( es->p_pgrm == p_sys->p_pgrm )
2057 EsOutESVarUpdate( out, es, true );
2059 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
2061 /* Update program */
2063 if( es->p_pgrm->i_es == 0 )
2064 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
2066 if( es->b_scrambled )
2067 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2070 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
2071 p_sys->p_es_sub == es ) b_reselect = true;
2073 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
2074 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
2075 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
2077 switch( es->fmt.i_cat )
2090 /* Re-select another track when needed */
2093 for( i = 0; i < p_sys->i_es; i++ )
2095 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
2096 EsOutSelect( out, p_sys->es[i], false );
2100 free( es->psz_language );
2101 free( es->psz_language_code );
2103 es_format_Clean( &es->fmt );
2105 vlc_mutex_unlock( &p_sys->lock );
2111 * Control query handler
2113 * \param out the es_out to control
2114 * \param i_query A es_out query as defined in include/ninput.h
2115 * \param args a variable list of arguments for the query
2116 * \return VLC_SUCCESS or an error code
2118 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2120 es_out_sys_t *p_sys = out->p_sys;
2128 case ES_OUT_SET_ES_STATE:
2129 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2130 b = (bool) va_arg( args, int );
2131 if( b && !EsIsSelected( es ) )
2133 EsSelect( out, es );
2134 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2136 else if( !b && EsIsSelected( es ) )
2138 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2143 case ES_OUT_GET_ES_STATE:
2144 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2145 pb = (bool*) va_arg( args, bool * );
2147 *pb = EsIsSelected( es );
2150 case ES_OUT_SET_ACTIVE:
2152 b = (bool) va_arg( args, int );
2153 if( b && !p_sys->b_active && p_sys->i_es > 0 )
2155 /* XXX Terminate vout if there are tracks but no video one.
2156 * This one is not mandatory but is he earliest place where it
2158 for( i = 0; i < p_sys->i_es; i++ )
2160 es_out_id_t *p_es = p_sys->es[i];
2161 if( p_es->fmt.i_cat == VIDEO_ES )
2164 if( i >= p_sys->i_es )
2165 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2167 p_sys->b_active = b;
2171 case ES_OUT_SET_MODE:
2172 i = (int) va_arg( args, int );
2173 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
2174 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
2178 /* Reapply policy mode */
2179 for( i = 0; i < p_sys->i_es; i++ )
2181 if( EsIsSelected( p_sys->es[i] ) )
2183 EsUnselect( out, p_sys->es[i],
2184 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2187 for( i = 0; i < p_sys->i_es; i++ )
2189 EsOutSelect( out, p_sys->es[i], false );
2193 return VLC_EGENERIC;
2196 case ES_OUT_RESTART_ES:
2200 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2204 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2206 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2208 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2213 for( i = 0; i < p_sys->i_es; i++ )
2217 if( es == p_sys->es[i] )
2219 EsOutSelect( out, es, true );
2225 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2227 if( EsIsSelected( p_sys->es[i] ) )
2229 if( i_query == ES_OUT_RESTART_ES )
2231 if( p_sys->es[i]->p_dec )
2233 EsDestroyDecoder( out, p_sys->es[i] );
2234 EsCreateDecoder( out, p_sys->es[i] );
2239 EsUnselect( out, p_sys->es[i],
2240 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2249 case ES_OUT_SET_ES_DEFAULT:
2251 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2255 /*p_sys->i_default_video_id = -1;*/
2256 /*p_sys->i_default_audio_id = -1;*/
2257 p_sys->i_default_sub_id = -1;
2259 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2261 /*p_sys->i_default_video_id = -1;*/
2263 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2265 /*p_sys->i_default_audio_id = -1;*/
2267 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2269 p_sys->i_default_sub_id = -1;
2273 /*if( es->fmt.i_cat == VIDEO_ES )
2274 p_sys->i_default_video_id = es->i_id;
2276 if( es->fmt.i_cat == AUDIO_ES )
2277 p_sys->i_default_audio_id = es->i_id;
2279 if( es->fmt.i_cat == SPU_ES )
2280 p_sys->i_default_sub_id = es->i_id;
2285 case ES_OUT_SET_PCR:
2286 case ES_OUT_SET_GROUP_PCR:
2288 es_out_pgrm_t *p_pgrm = NULL;
2292 /* Search program */
2293 if( i_query == ES_OUT_SET_PCR )
2295 p_pgrm = p_sys->p_pgrm;
2297 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2301 i_group = (int)va_arg( args, int );
2302 p_pgrm = EsOutProgramFind( out, i_group );
2305 return VLC_EGENERIC;
2307 i_pcr = (int64_t)va_arg( args, int64_t );
2308 if( i_pcr <= VLC_TS_INVALID )
2310 msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
2311 return VLC_EGENERIC;
2314 /* TODO do not use mdate() but proper stream acquisition date */
2316 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2318 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering,
2319 EsOutIsExtraBufferingAllowed( out ),
2322 if( p_pgrm == p_sys->p_pgrm )
2324 if( p_sys->b_buffering )
2326 /* Check buffering state on master clock update */
2327 EsOutDecodersStopBuffering( out, false );
2331 mtime_t i_pts_delay = input_clock_GetJitter( p_pgrm->p_clock );
2333 /* Avoid dangerously high value */
2334 const mtime_t i_pts_delay_max = 30000000;
2335 if( i_pts_delay > i_pts_delay_max )
2336 i_pts_delay = __MAX( i_pts_delay_max, p_sys->i_pts_delay );
2338 /* Force a rebufferization when we are too late */
2339 msg_Err( p_sys->p_input,
2340 "ES_OUT_SET_(GROUP_)PCR is called too late, increasing pts_delay to %d ms",
2341 (int)(i_pts_delay/1000) );
2343 /* It is not really good, as we throw away already buffered data
2344 * TODO have a mean to correctly reenter bufferization */
2345 es_out_Control( out, ES_OUT_RESET_PCR );
2347 es_out_Control( out, ES_OUT_SET_JITTER, i_pts_delay, p_sys->i_cr_average );
2353 case ES_OUT_RESET_PCR:
2354 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2355 EsOutChangePosition( out );
2358 case ES_OUT_SET_GROUP:
2361 i = (int) va_arg( args, int );
2362 for( j = 0; j < p_sys->i_pgrm; j++ )
2364 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2365 if( p_pgrm->i_id == i )
2367 EsOutProgramSelect( out, p_pgrm );
2371 return VLC_EGENERIC;
2374 case ES_OUT_SET_ES_FMT:
2376 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2377 * to update the p_extra data */
2379 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2380 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2382 return VLC_EGENERIC;
2384 if( p_fmt->i_extra )
2386 es->fmt.i_extra = p_fmt->i_extra;
2387 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2388 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2393 EsDestroyDecoder( out, es );
2395 EsCreateDecoder( out, es );
2397 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2398 es->p_dec->fmt_in.p_extra =
2399 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2400 memcpy( es->p_dec->fmt_in.p_extra,
2401 p_fmt->p_extra, p_fmt->i_extra );
2408 case ES_OUT_SET_ES_SCRAMBLED_STATE:
2410 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2411 bool b_scrambled = (bool)va_arg( args, int );
2413 if( !es->b_scrambled != !b_scrambled )
2415 es->b_scrambled = b_scrambled;
2416 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2421 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2423 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2426 return VLC_EGENERIC;
2428 p_sys->i_preroll_end = i_date;
2432 case ES_OUT_SET_GROUP_META:
2434 int i_group = (int)va_arg( args, int );
2435 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2437 EsOutProgramMeta( out, i_group, p_meta );
2440 case ES_OUT_SET_GROUP_EPG:
2442 int i_group = (int)va_arg( args, int );
2443 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
2445 EsOutProgramEpg( out, i_group, p_epg );
2449 case ES_OUT_DEL_GROUP:
2451 int i_group = (int)va_arg( args, int );
2453 return EsOutProgramDel( out, i_group );
2456 case ES_OUT_SET_META:
2458 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2460 EsOutMeta( out, p_meta );
2464 case ES_OUT_GET_WAKE_UP:
2466 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2467 *pi_wakeup = EsOutGetWakeup( out );
2471 case ES_OUT_SET_ES_BY_ID:
2472 case ES_OUT_RESTART_ES_BY_ID:
2473 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2475 const int i_id = (int)va_arg( args, int );
2476 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2481 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2482 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2483 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2487 /* TODO if the lock is made non recursive it should be changed */
2488 int i_ret = es_out_Control( out, i_new_query, p_es );
2490 /* Clean up vout after user action (in active mode only).
2491 * FIXME it does not work well with multiple video windows */
2492 if( p_sys->b_active )
2493 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2497 case ES_OUT_GET_BUFFERING:
2498 pb = (bool *)va_arg( args, bool* );
2499 *pb = p_sys->b_buffering;
2502 case ES_OUT_GET_EMPTY:
2503 pb = (bool *)va_arg( args, bool* );
2504 *pb = EsOutDecodersIsEmpty( out );
2507 case ES_OUT_SET_DELAY:
2509 const int i_cat = (int)va_arg( args, int );
2510 const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2511 EsOutSetDelay( out, i_cat, i_delay );
2515 case ES_OUT_SET_RECORD_STATE:
2516 b = (bool) va_arg( args, int );
2517 return EsOutSetRecord( out, b );
2519 case ES_OUT_SET_PAUSE_STATE:
2521 const bool b_source_paused = (bool)va_arg( args, int );
2522 const bool b_paused = (bool)va_arg( args, int );
2523 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2525 assert( !b_source_paused == !b_paused );
2526 EsOutChangePause( out, b_paused, i_date );
2531 case ES_OUT_SET_RATE:
2533 const int i_src_rate = (int)va_arg( args, int );
2534 const int i_rate = (int)va_arg( args, int );
2536 assert( i_src_rate == i_rate );
2537 EsOutChangeRate( out, i_rate );
2542 case ES_OUT_SET_TIME:
2544 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2546 assert( i_date == -1 );
2547 EsOutChangePosition( out );
2552 case ES_OUT_SET_FRAME_NEXT:
2553 EsOutFrameNext( out );
2556 case ES_OUT_SET_TIMES:
2558 double f_position = (double)va_arg( args, double );
2559 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2560 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2562 input_SendEventLength( p_sys->p_input, i_length );
2564 if( !p_sys->b_buffering )
2566 /* Fix for buffering delay */
2567 const mtime_t i_delay = EsOutGetBuffering( out );
2574 f_position -= (double)i_delay / i_length;
2575 if( f_position < 0 )
2578 input_SendEventPosition( p_sys->p_input, f_position, i_time );
2582 case ES_OUT_SET_JITTER:
2584 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2585 int i_cr_average = (int)va_arg( args, int );
2587 if( i_pts_delay == p_sys->i_pts_delay &&
2588 i_cr_average == p_sys->i_cr_average )
2591 p_sys->i_pts_delay = i_pts_delay;
2592 p_sys->i_cr_average = i_cr_average;
2594 for( int i = 0; i < p_sys->i_pgrm; i++ )
2595 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2596 i_pts_delay, i_cr_average );
2601 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2602 return VLC_EGENERIC;
2605 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2607 es_out_sys_t *p_sys = out->p_sys;
2610 vlc_mutex_lock( &p_sys->lock );
2611 i_ret = EsOutControlLocked( out, i_query, args );
2612 vlc_mutex_unlock( &p_sys->lock );
2617 /****************************************************************************
2618 * LanguageGetName: try to expend iso639 into plain name
2619 ****************************************************************************/
2620 static char *LanguageGetName( const char *psz_code )
2622 const iso639_lang_t *pl;
2624 if( psz_code == NULL )
2626 return strdup( "" );
2629 if( strlen( psz_code ) == 2 )
2631 pl = GetLang_1( psz_code );
2633 else if( strlen( psz_code ) == 3 )
2635 pl = GetLang_2B( psz_code );
2636 if( !strcmp( pl->psz_iso639_1, "??" ) )
2638 pl = GetLang_2T( psz_code );
2643 return strdup( psz_code );
2646 if( !strcmp( pl->psz_iso639_1, "??" ) )
2648 return strdup( psz_code );
2652 if( *pl->psz_native_name )
2654 return strdup( pl->psz_native_name );
2656 return strdup( pl->psz_eng_name );
2660 /* Get a 2 char code */
2661 static char *LanguageGetCode( const char *psz_lang )
2663 const iso639_lang_t *pl;
2665 if( psz_lang == NULL || *psz_lang == '\0' )
2666 return strdup("??");
2668 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
2670 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2671 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2672 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2673 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2674 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2678 if( pl->psz_eng_name != NULL )
2679 return strdup( pl->psz_iso639_1 );
2681 return strdup("??");
2684 static char **LanguageSplit( const char *psz_langs )
2691 if( psz_langs == NULL ) return NULL;
2693 psz_parser = psz_dup = strdup(psz_langs);
2695 while( psz_parser && *psz_parser )
2700 psz = strchr(psz_parser, ',' );
2701 if( psz ) *psz++ = '\0';
2703 if( !strcmp( psz_parser, "any" ) )
2705 TAB_APPEND( i_psz, ppsz, strdup("any") );
2709 psz_code = LanguageGetCode( psz_parser );
2710 if( strcmp( psz_code, "??" ) )
2712 TAB_APPEND( i_psz, ppsz, psz_code );
2725 TAB_APPEND( i_psz, ppsz, NULL );
2732 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2736 if( !ppsz_langs || !psz_lang ) return -1;
2738 for( i = 0; ppsz_langs[i]; i++ )
2740 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2741 !strcasecmp( ppsz_langs[i], "any" ) )
2750 /****************************************************************************
2752 * - add meta info to the playlist item
2753 ****************************************************************************/
2754 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2756 es_out_sys_t *p_sys = out->p_sys;
2757 input_thread_t *p_input = p_sys->p_input;
2758 const es_format_t *p_fmt_es = &es->fmt;
2762 /* Create category name */
2763 if( asprintf( &psz_cat, _("Stream %d"), es->i_meta_id ) == -1 )
2766 /* Remove previous information */
2767 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
2769 /* Add informations */
2770 const char *psz_type;
2771 switch( fmt->i_cat )
2774 psz_type = _("Audio");
2777 psz_type = _("Video");
2780 psz_type = _("Subtitle");
2788 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Type"), psz_type );
2790 if( es->i_meta_id != es->i_id )
2791 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Original ID"),
2794 const char *psz_codec_description =
2795 vlc_fourcc_GetDescription( p_fmt_es->i_cat, p_fmt_es->i_codec );
2796 const vlc_fourcc_t i_codec_fourcc = p_fmt_es->i_original_fourcc ?: p_fmt_es->i_codec;
2797 if( psz_codec_description )
2798 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2799 "%s (%.4s)", psz_codec_description, (char*)&i_codec_fourcc );
2801 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2802 "%.4s", (char*)&i_codec_fourcc );
2804 if( es->psz_language && *es->psz_language )
2805 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2806 "%s", es->psz_language );
2807 if( fmt->psz_description && *fmt->psz_description )
2808 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
2809 "%s", fmt->psz_description );
2811 switch( fmt->i_cat )
2814 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2815 _("Type"), _("Audio") );
2817 if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
2818 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2819 "%s", _( aout_FormatPrintChannels( &fmt->audio ) ) );
2820 else if( fmt->audio.i_channels > 0 )
2821 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2822 "%u", fmt->audio.i_channels );
2824 if( fmt->audio.i_rate > 0 )
2826 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2827 _("%u Hz"), fmt->audio.i_rate );
2828 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2829 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2832 unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2833 if( i_bitspersample <= 0 )
2834 i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2835 if( i_bitspersample > 0 )
2836 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2837 _("Bits per sample"), "%u",
2840 if( fmt->i_bitrate > 0 )
2842 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2843 _("%u kb/s"), fmt->i_bitrate / 1000 );
2844 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2845 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2847 for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2849 const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2850 if( !p_rg->pb_gain[i] )
2852 const char *psz_name;
2853 if( i == AUDIO_REPLAY_GAIN_TRACK )
2854 psz_name = _("Track replay gain");
2856 psz_name = _("Album replay gain");
2857 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2858 psz_name, _("%.2f dB"), p_rg->pf_gain[i] );
2863 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2864 _("Type"), _("Video") );
2866 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2867 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2868 _("Resolution"), "%ux%u",
2869 fmt->video.i_width, fmt->video.i_height );
2871 if( fmt->video.i_visible_width > 0 &&
2872 fmt->video.i_visible_height > 0 )
2873 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2874 _("Display resolution"), "%ux%u",
2875 fmt->video.i_visible_width,
2876 fmt->video.i_visible_height);
2877 if( fmt->video.i_frame_rate > 0 &&
2878 fmt->video.i_frame_rate_base > 0 )
2880 div = lldiv( (float)fmt->video.i_frame_rate /
2881 fmt->video.i_frame_rate_base * 1000000,
2884 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2885 _("Frame rate"), "%"PRId64".%06u",
2886 div.quot, (unsigned int )div.rem );
2888 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2889 _("Frame rate"), "%"PRId64, div.quot );
2894 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2895 _("Type"), _("Subtitle") );
2902 /* Append generic meta */
2905 char **ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2906 for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
2908 char *psz_key = ppsz_all_keys[i];
2909 char *psz_value = vlc_dictionary_value_for_key( &p_meta->extra_tags, psz_key );
2912 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2913 vlc_gettext(psz_key), vlc_gettext(psz_value) );
2916 free( ppsz_all_keys );