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"
49 #include "../stream_output/stream_output.h"
51 #include <vlc_iso_lang.h>
52 /* FIXME we should find a better way than including that */
53 #include "../text/iso-639_def.h"
55 /*****************************************************************************
57 *****************************************************************************/
63 /* Number of es for this pgrm */
69 /* Clock for this program */
70 input_clock_t *p_clock;
73 char *psz_now_playing;
81 es_out_pgrm_t *p_pgrm;
86 /* Channel in the track type */
90 char *psz_language_code;
93 decoder_t *p_dec_record;
95 /* Fields for Video with CC */
96 bool pb_cc_present[4];
97 es_out_id_t *pp_cc_es[4];
99 /* Field for CC track from a master video */
100 es_out_id_t *p_master;
102 /* ID for the meta data */
108 input_thread_t *p_input;
115 es_out_pgrm_t **pgrm;
116 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
117 es_out_pgrm_t *p_pgrm; /* Master program */
134 int i_audio_last, i_audio_id;
135 int i_sub_last, i_sub_id;
136 int i_default_sub_id; /* As specified in container; if applicable */
137 char **ppsz_audio_language;
138 char **ppsz_sub_language;
140 /* current main es */
141 es_out_id_t *p_es_audio;
142 es_out_id_t *p_es_video;
143 es_out_id_t *p_es_sub;
146 int64_t i_audio_delay;
149 /* Clock configuration */
151 mtime_t i_pts_jitter;
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 EsOutTerminate( es_out_t * );
179 static void EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
180 static void EsOutUpdateInfo( es_out_t *, es_out_id_t *es, const es_format_t *, const vlc_meta_t * );
181 static int EsOutSetRecord( es_out_t *, bool b_record );
183 static bool EsIsSelected( es_out_id_t *es );
184 static void EsSelect( es_out_t *out, es_out_id_t *es );
185 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
186 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
187 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
188 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
189 static void EsOutProgramsChangeRate( es_out_t *out );
190 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
192 static char *LanguageGetName( const char *psz_code );
193 static char *LanguageGetCode( const char *psz_lang );
194 static char **LanguageSplit( const char *psz_langs, bool b_default_any );
195 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
197 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
199 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
200 VLC_FOURCC('c', 'c', '1', ' '),
201 VLC_FOURCC('c', 'c', '2', ' '),
202 VLC_FOURCC('c', 'c', '3', ' '),
203 VLC_FOURCC('c', 'c', '4', ' '),
205 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
208 for( i = 0; i < 4; i++ )
210 if( fcc == EsOutFourccClosedCaptions[i] )
215 static inline bool EsFmtIsTeletext( const es_format_t *p_fmt )
217 return p_fmt->i_cat == SPU_ES && p_fmt->i_codec == VLC_CODEC_TELETEXT;
220 /*****************************************************************************
222 *****************************************************************************/
223 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
225 es_out_t *out = malloc( sizeof( *out ) );
229 es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
236 out->pf_add = EsOutAdd;
237 out->pf_send = EsOutSend;
238 out->pf_del = EsOutDel;
239 out->pf_control = EsOutControl;
240 out->pf_destroy = EsOutDelete;
243 vlc_mutex_init_recursive( &p_sys->lock );
244 p_sys->p_input = p_input;
246 p_sys->b_active = false;
247 p_sys->i_mode = ES_OUT_MODE_NONE;
250 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
251 p_sys->p_pgrm = NULL;
255 TAB_INIT( p_sys->i_es, p_sys->es );
262 p_sys->i_audio_last = var_GetInteger( p_input, "audio-track" );
264 p_sys->i_sub_last = var_GetInteger( p_input, "sub-track" );
266 p_sys->i_default_sub_id = -1;
268 if( !p_input->b_preparsing )
272 psz_string = var_GetString( p_input, "audio-language" );
273 p_sys->ppsz_audio_language = LanguageSplit( psz_string, true );
274 if( p_sys->ppsz_audio_language )
276 for( int i = 0; p_sys->ppsz_audio_language[i]; i++ )
277 msg_Dbg( p_input, "selected audio language[%d] %s",
278 i, p_sys->ppsz_audio_language[i] );
282 psz_string = var_GetString( p_input, "sub-language" );
283 p_sys->ppsz_sub_language = LanguageSplit( psz_string, false );
284 if( p_sys->ppsz_sub_language )
286 for( int i = 0; p_sys->ppsz_sub_language[i]; i++ )
287 msg_Dbg( p_input, "selected subtitle language[%d] %s",
288 i, p_sys->ppsz_sub_language[i] );
294 p_sys->ppsz_sub_language = NULL;
295 p_sys->ppsz_audio_language = NULL;
298 p_sys->i_audio_id = var_GetInteger( p_input, "audio-track-id" );
300 p_sys->i_sub_id = var_GetInteger( p_input, "sub-track-id" );
302 p_sys->p_es_audio = NULL;
303 p_sys->p_es_video = NULL;
304 p_sys->p_es_sub = NULL;
306 p_sys->i_audio_delay= 0;
307 p_sys->i_spu_delay = 0;
309 p_sys->b_paused = false;
310 p_sys->i_pause_date = -1;
312 p_sys->i_rate = i_rate;
313 p_sys->i_pts_delay = 0;
314 p_sys->i_pts_jitter = 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;
335 assert( !p_sys->i_es && !p_sys->i_pgrm && !p_sys->p_pgrm );
336 if( p_sys->ppsz_audio_language )
338 for( int i = 0; p_sys->ppsz_audio_language[i]; i++ )
339 free( p_sys->ppsz_audio_language[i] );
340 free( p_sys->ppsz_audio_language );
342 if( p_sys->ppsz_sub_language )
344 for( int i = 0; p_sys->ppsz_sub_language[i]; i++ )
345 free( p_sys->ppsz_sub_language[i] );
346 free( p_sys->ppsz_sub_language );
349 vlc_mutex_destroy( &p_sys->lock );
355 static void EsOutTerminate( es_out_t *out )
357 es_out_sys_t *p_sys = out->p_sys;
359 if( p_sys->p_sout_record )
360 EsOutSetRecord( out, false );
362 for( int i = 0; i < p_sys->i_es; i++ )
364 if( p_sys->es[i]->p_dec )
365 input_DecoderDelete( p_sys->es[i]->p_dec );
367 free( p_sys->es[i]->psz_language );
368 free( p_sys->es[i]->psz_language_code );
369 es_format_Clean( &p_sys->es[i]->fmt );
371 free( p_sys->es[i] );
373 TAB_CLEAN( p_sys->i_es, p_sys->es );
375 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
376 for( int i = 0; i < p_sys->i_pgrm; i++ )
378 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
379 input_clock_Delete( p_pgrm->p_clock );
380 free( p_pgrm->psz_now_playing );
381 free( p_pgrm->psz_publisher );
382 free( p_pgrm->psz_name );
386 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
388 p_sys->p_pgrm = NULL;
390 input_item_SetEpgOffline( p_sys->p_input->p->p_item );
391 input_SendEventMetaEpg( p_sys->p_input );
394 static mtime_t EsOutGetWakeup( es_out_t *out )
396 es_out_sys_t *p_sys = out->p_sys;
397 input_thread_t *p_input = p_sys->p_input;
402 /* We do not have a wake up date if the input cannot have its speed
403 * controlled or sout is imposing its own or while buffering
405 * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
406 if( !p_input->p->b_can_pace_control ||
407 p_input->p->b_out_pace_control ||
411 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
414 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
419 /* Special HACK, -i_id is the cat of the stream */
420 return (es_out_id_t*)((uint8_t*)NULL-i_id);
423 for( i = 0; i < out->p_sys->i_es; i++ )
425 if( out->p_sys->es[i]->i_id == i_id )
426 return out->p_sys->es[i];
431 static bool EsOutDecodersIsEmpty( es_out_t *out )
433 es_out_sys_t *p_sys = out->p_sys;
436 if( p_sys->b_buffering && p_sys->p_pgrm )
438 EsOutDecodersStopBuffering( out, true );
439 if( p_sys->b_buffering )
443 for( i = 0; i < p_sys->i_es; i++ )
445 es_out_id_t *es = p_sys->es[i];
447 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
449 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
455 static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
457 es_out_sys_t *p_sys = out->p_sys;
459 if( i_cat == AUDIO_ES )
460 p_sys->i_audio_delay = i_delay;
461 else if( i_cat == SPU_ES )
462 p_sys->i_spu_delay = i_delay;
464 for( int i = 0; i < p_sys->i_es; i++ )
465 EsOutDecoderChangeDelay( out, p_sys->es[i] );
468 static int EsOutSetRecord( es_out_t *out, bool b_record )
470 es_out_sys_t *p_sys = out->p_sys;
471 input_thread_t *p_input = p_sys->p_input;
473 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
477 char *psz_path = var_CreateGetNonEmptyString( p_input, "input-record-path" );
480 if( var_CountChoices( p_input, "video-es" ) )
481 psz_path = config_GetUserDir( VLC_VIDEOS_DIR );
482 else if( var_CountChoices( p_input, "audio-es" ) )
483 psz_path = config_GetUserDir( VLC_MUSIC_DIR );
485 psz_path = config_GetUserDir( VLC_DOWNLOAD_DIR );
488 char *psz_sout = NULL; // TODO conf
490 if( !psz_sout && psz_path )
492 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
495 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
506 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
510 if( !p_sys->p_sout_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 || p_es->p_master )
520 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
521 if( p_es->p_dec_record && p_sys->b_buffering )
522 input_DecoderStartBuffering( p_es->p_dec_record );
527 for( int i = 0; i < p_sys->i_es; i++ )
529 es_out_id_t *p_es = p_sys->es[i];
531 if( !p_es->p_dec_record )
534 input_DecoderDelete( p_es->p_dec_record );
535 p_es->p_dec_record = NULL;
538 sout_DeleteInstance( p_sys->p_sout_record );
540 p_sys->p_sout_record = NULL;
545 static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
547 es_out_sys_t *p_sys = out->p_sys;
549 /* XXX the order is important */
552 EsOutDecodersChangePause( out, true, i_date );
553 EsOutProgramChangePause( out, true, i_date );
557 if( p_sys->i_buffering_extra_initial > 0 )
559 mtime_t i_stream_start;
560 mtime_t i_system_start;
561 mtime_t i_stream_duration;
562 mtime_t i_system_duration;
564 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
565 &i_stream_start, &i_system_start,
566 &i_stream_duration, &i_system_duration );
569 /* FIXME pcr != exactly what wanted */
570 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;
573 p_sys->i_buffering_extra_initial = 0;
574 p_sys->i_buffering_extra_stream = 0;
575 p_sys->i_buffering_extra_system = 0;
577 EsOutProgramChangePause( out, false, i_date );
578 EsOutDecodersChangePause( out, false, i_date );
580 EsOutProgramsChangeRate( out );
582 p_sys->b_paused = b_paused;
583 p_sys->i_pause_date = i_date;
586 static void EsOutChangeRate( es_out_t *out, int i_rate )
588 es_out_sys_t *p_sys = out->p_sys;
590 p_sys->i_rate = i_rate;
591 EsOutProgramsChangeRate( out );
594 static void EsOutChangePosition( es_out_t *out )
596 es_out_sys_t *p_sys = out->p_sys;
598 input_SendEventCache( p_sys->p_input, 0.0 );
600 for( int i = 0; i < p_sys->i_es; i++ )
602 es_out_id_t *p_es = p_sys->es[i];
607 input_DecoderStartBuffering( p_es->p_dec );
609 if( p_es->p_dec_record )
610 input_DecoderStartBuffering( p_es->p_dec_record );
613 for( int i = 0; i < p_sys->i_pgrm; i++ )
614 input_clock_Reset( p_sys->pgrm[i]->p_clock );
616 p_sys->b_buffering = true;
617 p_sys->i_buffering_extra_initial = 0;
618 p_sys->i_buffering_extra_stream = 0;
619 p_sys->i_buffering_extra_system = 0;
620 p_sys->i_preroll_end = -1;
625 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
627 es_out_sys_t *p_sys = out->p_sys;
630 mtime_t i_stream_start;
631 mtime_t i_system_start;
632 mtime_t i_stream_duration;
633 mtime_t i_system_duration;
634 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
635 &i_stream_start, &i_system_start,
636 &i_stream_duration, &i_system_duration );
637 assert( !i_ret || b_forced );
641 mtime_t i_preroll_duration = 0;
642 if( p_sys->i_preroll_end >= 0 )
643 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
645 const mtime_t i_buffering_duration = p_sys->i_pts_delay +
647 p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
649 if( i_stream_duration <= i_buffering_duration && !b_forced )
651 const double f_level = __MAX( (double)i_stream_duration / i_buffering_duration, 0 );
652 input_SendEventCache( p_sys->p_input, f_level );
654 msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
657 input_SendEventCache( p_sys->p_input, 1.0 );
659 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
660 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
661 p_sys->b_buffering = false;
662 p_sys->i_preroll_end = -1;
664 if( p_sys->i_buffering_extra_initial > 0 )
670 const mtime_t i_decoder_buffering_start = mdate();
671 for( int i = 0; i < p_sys->i_es; i++ )
673 es_out_id_t *p_es = p_sys->es[i];
675 if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
677 input_DecoderWaitBuffering( p_es->p_dec );
678 if( p_es->p_dec_record )
679 input_DecoderWaitBuffering( p_es->p_dec_record );
682 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
683 (int)(mdate() - i_decoder_buffering_start)/1000 );
685 /* Here is a good place to destroy unused vout with every demuxer */
686 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
689 const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
690 const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
692 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, true,
693 i_current_date + i_wakeup_delay - i_buffering_duration );
695 for( int i = 0; i < p_sys->i_es; i++ )
697 es_out_id_t *p_es = p_sys->es[i];
702 input_DecoderStopBuffering( p_es->p_dec );
703 if( p_es->p_dec_record )
704 input_DecoderStopBuffering( p_es->p_dec_record );
707 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
709 es_out_sys_t *p_sys = out->p_sys;
711 /* Pause decoders first */
712 for( int i = 0; i < p_sys->i_es; i++ )
714 es_out_id_t *es = p_sys->es[i];
718 input_DecoderChangePause( es->p_dec, b_paused, i_date );
719 if( es->p_dec_record )
720 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
725 static bool EsOutIsExtraBufferingAllowed( es_out_t *out )
727 es_out_sys_t *p_sys = out->p_sys;
730 for( int i = 0; i < p_sys->i_es; i++ )
732 es_out_id_t *p_es = p_sys->es[i];
735 i_size += input_DecoderGetFifoSize( p_es->p_dec );
736 if( p_es->p_dec_record )
737 i_size += input_DecoderGetFifoSize( p_es->p_dec_record );
739 //msg_Info( out, "----- EsOutIsExtraBufferingAllowed =% 5d KiB -- ", i_size / 1024 );
741 /* TODO maybe we want to be able to tune it ? */
742 #if defined(OPTIMIZE_MEMORY)
743 const size_t i_level_high = 512*1024; /* 0.5 MiB */
745 const size_t i_level_high = 10*1024*1024; /* 10 MiB */
747 return i_size < i_level_high;
750 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
752 es_out_sys_t *p_sys = out->p_sys;
754 for( int i = 0; i < p_sys->i_pgrm; i++ )
755 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
758 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
760 es_out_sys_t *p_sys = out->p_sys;
763 if( p_es->fmt.i_cat == AUDIO_ES )
764 i_delay = p_sys->i_audio_delay;
765 else if( p_es->fmt.i_cat == SPU_ES )
766 i_delay = p_sys->i_spu_delay;
771 input_DecoderChangeDelay( p_es->p_dec, i_delay );
772 if( p_es->p_dec_record )
773 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
776 static void EsOutProgramsChangeRate( es_out_t *out )
778 es_out_sys_t *p_sys = out->p_sys;
780 for( int i = 0; i < p_sys->i_pgrm; i++ )
781 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
784 static void EsOutFrameNext( es_out_t *out )
786 es_out_sys_t *p_sys = out->p_sys;
787 es_out_id_t *p_es_video = NULL;
789 if( p_sys->b_buffering )
791 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
795 assert( p_sys->b_paused );
797 for( int i = 0; i < p_sys->i_es; i++ )
799 es_out_id_t *p_es = p_sys->es[i];
801 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
810 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
815 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
817 msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
819 if( i_duration <= 0 )
820 i_duration = 40*1000;
822 /* FIXME it is not a clean way ? */
823 if( p_sys->i_buffering_extra_initial <= 0 )
825 mtime_t i_stream_start;
826 mtime_t i_system_start;
827 mtime_t i_stream_duration;
828 mtime_t i_system_duration;
831 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
832 &i_stream_start, &i_system_start,
833 &i_stream_duration, &i_system_duration );
837 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
838 p_sys->i_buffering_extra_system =
839 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
842 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
844 p_sys->b_buffering = true;
845 p_sys->i_buffering_extra_system += i_duration;
846 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
847 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
848 INPUT_RATE_DEFAULT / i_rate;
850 p_sys->i_preroll_end = -1;
852 static mtime_t EsOutGetBuffering( es_out_t *out )
854 es_out_sys_t *p_sys = out->p_sys;
860 mtime_t i_stream_start;
861 mtime_t i_system_start;
862 mtime_t i_stream_duration;
863 mtime_t i_system_duration;
864 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
865 &i_stream_start, &i_system_start,
866 &i_stream_duration, &i_system_duration );
873 if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
875 i_delay = i_stream_duration;
879 mtime_t i_system_duration;
880 if( p_sys->b_paused )
882 i_system_duration = p_sys->i_pause_date - i_system_start;
883 if( p_sys->i_buffering_extra_initial > 0 )
884 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
888 i_system_duration = mdate() - i_system_start;
891 const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
892 i_delay = p_sys->i_pts_delay - i_consumed;
899 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
900 const es_format_t *fmt, const char *psz_language,
903 es_out_sys_t *p_sys = out->p_sys;
904 input_thread_t *p_input = p_sys->p_input;
905 vlc_value_t val, text;
909 if( EsFmtIsTeletext( fmt ) )
910 input_SendEventTeletextDel( p_sys->p_input, i_id );
912 input_SendEventEsDel( p_input, fmt->i_cat, i_id );
916 /* Get the number of ES already added */
918 if( fmt->i_cat == AUDIO_ES )
919 psz_var = "audio-es";
920 else if( fmt->i_cat == VIDEO_ES )
921 psz_var = "video-es";
925 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
930 /* First one, we need to add the "Disable" choice */
931 val2.i_int = -1; text.psz_string = _("Disable");
932 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
936 /* Take care of the ES description */
937 if( fmt->psz_description && *fmt->psz_description )
939 if( psz_language && *psz_language )
941 if( asprintf( &text.psz_string, "%s - [%s]", fmt->psz_description,
942 psz_language ) == -1 )
943 text.psz_string = NULL;
945 else text.psz_string = strdup( fmt->psz_description );
949 if( psz_language && *psz_language )
951 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
952 text.psz_string = NULL;
956 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
957 text.psz_string = NULL;
961 input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
962 if( EsFmtIsTeletext( fmt ) )
965 snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
966 fmt->subs.teletext.i_magazine,
967 fmt->subs.teletext.i_page );
968 input_SendEventTeletextAdd( p_sys->p_input,
969 i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
972 free( text.psz_string );
975 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
978 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
981 /* EsOutProgramSelect:
982 * Select a program and update the object variable
984 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
986 es_out_sys_t *p_sys = out->p_sys;
987 input_thread_t *p_input = p_sys->p_input;
990 if( p_sys->p_pgrm == p_pgrm )
991 return; /* Nothing to do */
995 es_out_pgrm_t *old = p_sys->p_pgrm;
996 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
998 for( i = 0; i < p_sys->i_es; i++ )
1000 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
1001 p_sys->i_mode != ES_OUT_MODE_ALL )
1002 EsUnselect( out, p_sys->es[i], true );
1005 p_sys->p_es_audio = NULL;
1006 p_sys->p_es_sub = NULL;
1007 p_sys->p_es_video = NULL;
1010 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
1012 /* Mark it selected */
1013 p_pgrm->b_selected = true;
1015 /* Switch master stream */
1016 p_sys->p_pgrm = p_pgrm;
1018 /* Update "program" */
1019 input_SendEventProgramSelect( p_input, p_pgrm->i_id );
1022 input_SendEventEsDel( p_input, AUDIO_ES, -1 );
1023 input_SendEventEsDel( p_input, VIDEO_ES, -1 );
1024 input_SendEventEsDel( p_input, SPU_ES, -1 );
1025 input_SendEventTeletextDel( p_input, -1 );
1026 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
1029 var_SetInteger( p_input, "teletext-es", -1 );
1031 for( i = 0; i < p_sys->i_es; i++ )
1033 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
1034 EsOutESVarUpdate( out, p_sys->es[i], false );
1035 EsOutSelect( out, p_sys->es[i], false );
1038 /* Update now playing */
1039 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1040 input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
1042 input_SendEventMeta( p_input );
1048 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
1050 es_out_sys_t *p_sys = out->p_sys;
1051 input_thread_t *p_input = p_sys->p_input;
1053 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1058 p_pgrm->i_id = i_group;
1060 p_pgrm->b_selected = false;
1061 p_pgrm->b_scrambled = false;
1062 p_pgrm->psz_name = NULL;
1063 p_pgrm->psz_now_playing = NULL;
1064 p_pgrm->psz_publisher = NULL;
1065 p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1066 if( !p_pgrm->p_clock )
1071 if( p_sys->b_paused )
1072 input_clock_ChangePause( p_pgrm->p_clock, p_sys->b_paused, p_sys->i_pause_date );
1073 input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1076 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1078 /* Update "program" variable */
1079 input_SendEventProgramAdd( p_input, i_group, NULL );
1081 if( i_group == var_GetInteger( p_input, "program" ) )
1082 EsOutProgramSelect( out, p_pgrm );
1090 static int EsOutProgramDel( es_out_t *out, int i_group )
1092 es_out_sys_t *p_sys = out->p_sys;
1093 input_thread_t *p_input = p_sys->p_input;
1094 es_out_pgrm_t *p_pgrm = NULL;
1097 for( i = 0; i < p_sys->i_pgrm; i++ )
1099 if( p_sys->pgrm[i]->i_id == i_group )
1101 p_pgrm = p_sys->pgrm[i];
1106 if( p_pgrm == NULL )
1107 return VLC_EGENERIC;
1111 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1112 i_group, p_pgrm->i_es );
1113 return VLC_EGENERIC;
1116 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1118 /* If program is selected we need to unselect it */
1119 if( p_sys->p_pgrm == p_pgrm )
1120 p_sys->p_pgrm = NULL;
1122 input_clock_Delete( p_pgrm->p_clock );
1124 free( p_pgrm->psz_name );
1125 free( p_pgrm->psz_now_playing );
1126 free( p_pgrm->psz_publisher );
1129 /* Update "program" variable */
1130 input_SendEventProgramDel( p_input, i_group );
1137 static es_out_pgrm_t *EsOutProgramFind( es_out_t *p_out, int i_group )
1139 es_out_sys_t *p_sys = p_out->p_sys;
1141 for( int i = 0; i < p_sys->i_pgrm; i++ )
1143 if( p_sys->pgrm[i]->i_id == i_group )
1144 return p_sys->pgrm[i];
1146 return EsOutProgramAdd( p_out, i_group );
1149 /* EsOutProgramMeta:
1151 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1154 if( p_pgrm->psz_name )
1156 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1161 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1167 static void EsOutProgramMeta( es_out_t *out, int i_group, const vlc_meta_t *p_meta )
1169 es_out_sys_t *p_sys = out->p_sys;
1170 es_out_pgrm_t *p_pgrm;
1171 input_thread_t *p_input = p_sys->p_input;
1172 const char *psz_title = NULL;
1173 const char *psz_provider = NULL;
1176 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1178 /* Check against empty meta data (empty for what we handle) */
1179 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1180 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1181 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1182 vlc_meta_GetExtraCount( p_meta ) <= 0 )
1187 p_pgrm = EsOutProgramFind( out, i_group );
1192 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1193 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1195 /* Update the description text of the program */
1196 if( psz_title && *psz_title )
1198 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1200 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1202 /* Remove old entries */
1203 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1204 /* TODO update epg name ?
1205 * TODO update scrambled info name ? */
1208 free( p_pgrm->psz_name );
1209 p_pgrm->psz_name = strdup( psz_title );
1212 if( psz_provider && *psz_provider )
1214 if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1219 psz_text = strdup( psz_title );
1222 /* ugly but it works */
1225 input_SendEventProgramDel( p_input, i_group );
1226 input_SendEventProgramAdd( p_input, i_group, psz_text );
1227 if( p_sys->p_pgrm == p_pgrm )
1228 input_SendEventProgramSelect( p_input, i_group );
1234 char **ppsz_all_keys = vlc_meta_CopyExtraNames(p_meta );
1236 info_category_t *p_cat = NULL;
1237 if( psz_provider || ( ppsz_all_keys[0] && *ppsz_all_keys[0] ) )
1239 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1241 p_cat = info_category_New( psz_cat );
1245 for( i = 0; ppsz_all_keys[i]; i++ )
1248 info_category_AddInfo( p_cat, vlc_gettext(ppsz_all_keys[i]), "%s",
1249 vlc_meta_GetExtra( p_meta, ppsz_all_keys[i] ) );
1250 free( ppsz_all_keys[i] );
1252 free( ppsz_all_keys );
1256 if( p_sys->p_pgrm == p_pgrm )
1258 input_item_SetPublisher( p_input->p->p_item, psz_provider );
1259 input_SendEventMeta( p_input );
1262 info_category_AddInfo( p_cat, vlc_meta_TypeToLocalizedString(vlc_meta_Publisher),
1263 "%s",psz_provider );
1266 input_Control( p_input, INPUT_MERGE_INFOS, p_cat );
1269 static void EsOutProgramEpg( es_out_t *out, int i_group, const vlc_epg_t *p_epg )
1271 es_out_sys_t *p_sys = out->p_sys;
1272 input_thread_t *p_input = p_sys->p_input;
1273 input_item_t *p_item = p_input->p->p_item;
1274 es_out_pgrm_t *p_pgrm;
1278 p_pgrm = EsOutProgramFind( out, i_group );
1283 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1284 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, psz_cat );
1290 epg.psz_name = psz_cat;
1292 input_item_SetEpg( p_item, &epg );
1293 input_SendEventMetaEpg( p_sys->p_input );
1295 /* Update now playing */
1296 free( p_pgrm->psz_now_playing );
1297 p_pgrm->psz_now_playing = NULL;
1299 vlc_mutex_lock( &p_item->lock );
1300 for( int i = 0; i < p_item->i_epg; i++ )
1302 const vlc_epg_t *p_tmp = p_item->pp_epg[i];
1304 if( p_tmp->psz_name && !strcmp(p_tmp->psz_name, psz_cat) )
1306 if( p_tmp->p_current && p_tmp->p_current->psz_name && *p_tmp->p_current->psz_name )
1307 p_pgrm->psz_now_playing = strdup( p_tmp->p_current->psz_name );
1311 vlc_mutex_unlock( &p_item->lock );
1313 if( p_pgrm == p_sys->p_pgrm )
1315 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1316 input_SendEventMeta( p_input );
1319 if( p_pgrm->psz_now_playing )
1321 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1322 vlc_meta_TypeToLocalizedString(vlc_meta_NowPlaying),
1323 p_pgrm->psz_now_playing );
1327 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1328 vlc_meta_TypeToLocalizedString(vlc_meta_NowPlaying) );
1334 static void EsOutProgramUpdateScrambled( es_out_t *p_out, es_out_pgrm_t *p_pgrm )
1336 es_out_sys_t *p_sys = p_out->p_sys;
1337 input_thread_t *p_input = p_sys->p_input;
1338 bool b_scrambled = false;
1340 for( int i = 0; i < p_sys->i_es; i++ )
1342 if( p_sys->es[i]->p_pgrm == p_pgrm && p_sys->es[i]->b_scrambled )
1348 if( !p_pgrm->b_scrambled == !b_scrambled )
1351 p_pgrm->b_scrambled = b_scrambled;
1352 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1355 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Scrambled"), _("Yes") );
1357 input_Control( p_input, INPUT_DEL_INFO, psz_cat, _("Scrambled") );
1359 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, b_scrambled );
1362 static void EsOutMeta( es_out_t *p_out, const vlc_meta_t *p_meta )
1364 es_out_sys_t *p_sys = p_out->p_sys;
1365 input_thread_t *p_input = p_sys->p_input;
1367 input_item_t *p_item = input_GetItem( p_input );
1369 char *psz_title = NULL;
1370 char *psz_arturl = input_item_GetArtURL( p_item );
1372 vlc_mutex_lock( &p_item->lock );
1374 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
1375 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
1377 vlc_meta_Merge( p_item->p_meta, p_meta );
1379 if( !psz_arturl || *psz_arturl == '\0' )
1381 const char *psz_tmp = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
1383 psz_arturl = strdup( psz_tmp );
1385 vlc_mutex_unlock( &p_item->lock );
1387 if( psz_arturl && *psz_arturl )
1389 input_item_SetArtURL( p_item, psz_arturl );
1391 if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
1393 /* Don't look for art cover if sout
1394 * XXX It can change when sout has meta data support */
1395 if( p_input->p->p_sout && !p_input->b_preparsing )
1396 input_item_SetArtURL( p_item, "" );
1398 input_ExtractAttachmentAndCacheArt( p_input );
1405 input_item_SetName( p_item, psz_title );
1408 input_item_SetPreparsed( p_item, true );
1410 input_SendEventMeta( p_input );
1411 /* TODO handle sout meta ? */
1417 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1419 es_out_sys_t *p_sys = out->p_sys;
1420 input_thread_t *p_input = p_sys->p_input;
1422 if( fmt->i_group < 0 )
1424 msg_Err( p_input, "invalid group number" );
1428 es_out_id_t *es = malloc( sizeof( *es ) );
1429 es_out_pgrm_t *p_pgrm;
1435 vlc_mutex_lock( &p_sys->lock );
1437 /* Search the program */
1438 p_pgrm = EsOutProgramFind( out, fmt->i_group );
1441 vlc_mutex_unlock( &p_sys->lock );
1446 /* Increase ref count for program */
1450 es->p_pgrm = p_pgrm;
1451 es_format_Copy( &es->fmt, fmt );
1452 if( es->fmt.i_id < 0 )
1453 es->fmt.i_id = out->p_sys->i_id;
1454 if( !es->fmt.i_original_fourcc )
1455 es->fmt.i_original_fourcc = es->fmt.i_codec;
1456 if( es->fmt.i_cat == AUDIO_ES )
1457 es->fmt.i_codec = vlc_fourcc_GetCodecAudio( es->fmt.i_codec,
1458 es->fmt.audio.i_bitspersample );
1460 es->fmt.i_codec = vlc_fourcc_GetCodec( es->fmt.i_cat,
1463 es->i_id = es->fmt.i_id;
1464 es->i_meta_id = out->p_sys->i_id;
1465 es->b_scrambled = false;
1467 switch( es->fmt.i_cat )
1471 audio_replay_gain_t rg;
1473 es->i_channel = p_sys->i_audio;
1475 memset( &rg, 0, sizeof(rg) );
1476 vlc_mutex_lock( &p_input->p->p_item->lock );
1477 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1478 vlc_mutex_unlock( &p_input->p->p_item->lock );
1480 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1482 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1484 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1485 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1487 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1489 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1490 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1497 es->i_channel = p_sys->i_video;
1498 if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1499 vlc_ureduce( &es->fmt.video.i_frame_rate,
1500 &es->fmt.video.i_frame_rate_base,
1501 es->fmt.video.i_frame_rate,
1502 es->fmt.video.i_frame_rate_base, 0 );
1506 es->i_channel = p_sys->i_sub;
1513 es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1514 es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1516 es->p_dec_record = NULL;
1517 for( i = 0; i < 4; i++ )
1518 es->pb_cc_present[i] = false;
1519 es->p_master = NULL;
1521 if( es->p_pgrm == p_sys->p_pgrm )
1522 EsOutESVarUpdate( out, es, false );
1524 /* Select it if needed */
1525 EsOutSelect( out, es, false );
1528 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1529 p_sys->i_id++; /* always incremented */
1530 switch( es->fmt.i_cat )
1543 EsOutUpdateInfo( out, es, &es->fmt, NULL );
1545 if( es->b_scrambled )
1546 EsOutProgramUpdateScrambled( out, es->p_pgrm );
1548 vlc_mutex_unlock( &p_sys->lock );
1553 static bool EsIsSelected( es_out_id_t *es )
1557 bool b_decode = false;
1558 if( es->p_master->p_dec )
1560 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1561 if( i_channel != -1 )
1562 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1568 return es->p_dec != NULL;
1571 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1573 es_out_sys_t *p_sys = out->p_sys;
1574 input_thread_t *p_input = p_sys->p_input;
1576 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1579 if( p_sys->b_buffering )
1580 input_DecoderStartBuffering( p_es->p_dec );
1582 if( !p_es->p_master && p_sys->p_sout_record )
1584 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1585 if( p_es->p_dec_record && p_sys->b_buffering )
1586 input_DecoderStartBuffering( p_es->p_dec_record );
1590 EsOutDecoderChangeDelay( out, p_es );
1592 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1599 input_DecoderDelete( p_es->p_dec );
1602 if( p_es->p_dec_record )
1604 input_DecoderDelete( p_es->p_dec_record );
1605 p_es->p_dec_record = NULL;
1609 static void EsSelect( es_out_t *out, es_out_id_t *es )
1611 es_out_sys_t *p_sys = out->p_sys;
1612 input_thread_t *p_input = p_sys->p_input;
1614 if( EsIsSelected( es ) )
1616 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1623 if( !es->p_master->p_dec )
1626 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1627 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1632 const bool b_sout = p_input->p->p_sout != NULL;
1633 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1635 if( !var_GetBool( p_input, b_sout ? "sout-video" : "video" ) )
1637 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1642 else if( es->fmt.i_cat == AUDIO_ES )
1644 if( !var_GetBool( p_input, b_sout ? "sout-audio" : "audio" ) )
1646 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1651 if( es->fmt.i_cat == SPU_ES )
1653 if( !var_GetBool( p_input, b_sout ? "sout-spu" : "spu" ) )
1655 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1661 EsCreateDecoder( out, es );
1663 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1667 /* Mark it as selected */
1668 input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1669 input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1672 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1674 es_out_sys_t *p_sys = out->p_sys;
1675 input_thread_t *p_input = p_sys->p_input;
1677 if( !EsIsSelected( es ) )
1679 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1685 if( es->p_master->p_dec )
1687 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1688 if( i_channel != -1 )
1689 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1694 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1696 for( i = 0; i < 4; i++ )
1698 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1701 if( i_spu_id == es->pp_cc_es[i]->i_id )
1703 /* Force unselection of the CC */
1704 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1706 EsOutDel( out, es->pp_cc_es[i] );
1708 es->pb_cc_present[i] = false;
1710 EsDestroyDecoder( out, es );
1716 /* Mark it as unselected */
1717 input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1718 if( EsFmtIsTeletext( &es->fmt ) )
1719 input_SendEventTeletextSelect( p_input, -1 );
1723 * Select an ES given the current mode
1724 * XXX: you need to take a the lock before (stream.stream_lock)
1726 * \param out The es_out structure
1727 * \param es es_out_id structure
1728 * \param b_force ...
1731 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1733 es_out_sys_t *p_sys = out->p_sys;
1735 int i_cat = es->fmt.i_cat;
1737 if( !p_sys->b_active ||
1738 ( !b_force && es->fmt.i_priority < 0 ) )
1743 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1745 if( !EsIsSelected( es ) )
1746 EsSelect( out, es );
1748 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1750 char *prgms = var_GetNonEmptyString( p_sys->p_input, "programs" );
1755 for ( const char *prgm = strtok_r( prgms, ",", &buf );
1757 prgm = strtok_r( NULL, ",", &buf ) )
1759 if( atoi( prgm ) == es->p_pgrm->i_id || b_force )
1761 if( !EsIsSelected( es ) )
1762 EsSelect( out, es );
1769 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1773 if( es->p_pgrm != p_sys->p_pgrm )
1776 if( i_cat == AUDIO_ES )
1778 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1779 es->psz_language_code );
1781 if( p_sys->p_es_audio &&
1782 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1784 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1785 p_sys->p_es_audio->psz_language_code );
1787 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1789 i_wanted = es->i_channel;
1793 /* Select audio if (no audio selected yet)
1794 * - no audio-language
1795 * - no audio code for the ES
1796 * - audio code in the requested list */
1798 !strcmp( es->psz_language_code, "??" ) ||
1799 !p_sys->ppsz_audio_language )
1800 i_wanted = es->i_channel;
1803 if( p_sys->i_audio_last >= 0 )
1804 i_wanted = p_sys->i_audio_last;
1806 if( p_sys->i_audio_id >= 0 )
1808 if( es->i_id == p_sys->i_audio_id )
1809 i_wanted = es->i_channel;
1814 else if( i_cat == SPU_ES )
1816 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1817 es->psz_language_code );
1819 if( p_sys->p_es_sub &&
1820 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1822 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1823 p_sys->p_es_sub->psz_language_code );
1825 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1826 idx1, es->psz_language_code, idx2,
1827 p_sys->p_es_sub->psz_language_code );
1829 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1831 /* We found a SPU that matches our language request */
1832 i_wanted = es->i_channel;
1834 else if( idx1 >= 0 )
1836 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1837 idx1, es->psz_language_code );
1839 i_wanted = es->i_channel;
1841 else if( p_sys->i_default_sub_id >= 0 )
1843 if( es->i_id == p_sys->i_default_sub_id )
1844 i_wanted = es->i_channel;
1847 if( p_sys->i_sub_last >= 0 )
1848 i_wanted = p_sys->i_sub_last;
1850 if( p_sys->i_sub_id >= 0 )
1852 if( es->i_id == p_sys->i_sub_id )
1853 i_wanted = es->i_channel;
1858 else if( i_cat == VIDEO_ES )
1860 i_wanted = es->i_channel;
1863 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1864 EsSelect( out, es );
1867 /* FIXME TODO handle priority here */
1868 if( EsIsSelected( es ) )
1870 if( i_cat == AUDIO_ES )
1872 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1873 p_sys->p_es_audio &&
1874 p_sys->p_es_audio != es &&
1875 EsIsSelected( p_sys->p_es_audio ) )
1877 EsUnselect( out, p_sys->p_es_audio, false );
1879 p_sys->p_es_audio = es;
1881 else if( i_cat == SPU_ES )
1883 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1885 p_sys->p_es_sub != es &&
1886 EsIsSelected( p_sys->p_es_sub ) )
1888 EsUnselect( out, p_sys->p_es_sub, false );
1890 p_sys->p_es_sub = es;
1892 else if( i_cat == VIDEO_ES )
1894 p_sys->p_es_video = es;
1900 * Send a block for the given es_out
1902 * \param out the es_out to send from
1903 * \param es the es_out_id
1904 * \param p_block the data block to send
1906 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1908 es_out_sys_t *p_sys = out->p_sys;
1909 input_thread_t *p_input = p_sys->p_input;
1912 if( libvlc_stats( p_input ) )
1914 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1915 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1916 p_block->i_buffer, &i_total );
1917 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1918 (float)i_total, NULL );
1920 /* Update number of corrupted data packats */
1921 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
1923 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_corrupted,
1926 /* Update number of discontinuities */
1927 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1929 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_discontinuity,
1932 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1935 vlc_mutex_lock( &p_sys->lock );
1937 /* Mark preroll blocks */
1938 if( p_sys->i_preroll_end >= 0 )
1940 int64_t i_date = p_block->i_pts;
1941 if( p_block->i_pts <= VLC_TS_INVALID )
1942 i_date = p_block->i_dts;
1944 if( i_date < p_sys->i_preroll_end )
1945 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1948 p_block->i_rate = 0;
1952 block_Release( p_block );
1953 vlc_mutex_unlock( &p_sys->lock );
1957 /* Check for sout mode */
1958 if( p_input->p->p_sout )
1960 /* FIXME review this, proper lock may be missing */
1961 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 &&
1962 p_input->p->b_out_pace_control )
1964 msg_Dbg( p_input, "switching to sync mode" );
1965 p_input->p->b_out_pace_control = false;
1967 else if( p_input->p->p_sout->i_out_pace_nocontrol <= 0 &&
1968 !p_input->p->b_out_pace_control )
1970 msg_Dbg( p_input, "switching to async mode" );
1971 p_input->p->b_out_pace_control = true;
1976 if( es->p_dec_record )
1978 block_t *p_dup = block_Duplicate( p_block );
1980 input_DecoderDecode( es->p_dec_record, p_dup,
1981 p_input->p->b_out_pace_control );
1983 input_DecoderDecode( es->p_dec, p_block,
1984 p_input->p->b_out_pace_control );
1986 es_format_t fmt_dsc;
1987 vlc_meta_t *p_meta_dsc;
1988 if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
1990 EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
1992 es_format_Clean( &fmt_dsc );
1994 vlc_meta_Delete( p_meta_dsc );
1997 /* Check CC status */
2000 input_DecoderIsCcPresent( es->p_dec, pb_cc );
2001 for( int i = 0; i < 4; i++ )
2005 if( es->pb_cc_present[i] || !pb_cc[i] )
2007 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
2009 es_format_Init( &fmt, SPU_ES, EsOutFourccClosedCaptions[i] );
2010 fmt.i_group = es->fmt.i_group;
2011 if( asprintf( &fmt.psz_description,
2012 _("Closed captions %u"), 1 + i ) == -1 )
2013 fmt.psz_description = NULL;
2014 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
2015 es->pp_cc_es[i]->p_master = es;
2016 es_format_Clean( &fmt );
2019 es->pb_cc_present[i] = true;
2022 vlc_mutex_unlock( &p_sys->lock );
2027 /*****************************************************************************
2029 *****************************************************************************/
2030 static void EsOutDel( es_out_t *out, es_out_id_t *es )
2032 es_out_sys_t *p_sys = out->p_sys;
2033 bool b_reselect = false;
2036 vlc_mutex_lock( &p_sys->lock );
2038 /* We don't try to reselect */
2041 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
2043 if( input_DecoderIsEmpty( es->p_dec ) &&
2044 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
2046 /* FIXME there should be a way to have auto deleted es, but there will be
2047 * a problem when another codec of the same type is created (mainly video) */
2050 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2053 if( es->p_pgrm == p_sys->p_pgrm )
2054 EsOutESVarUpdate( out, es, true );
2056 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
2058 /* Update program */
2060 if( es->p_pgrm->i_es == 0 )
2061 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
2063 if( es->b_scrambled )
2064 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2067 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
2068 p_sys->p_es_sub == es ) b_reselect = true;
2070 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
2071 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
2072 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
2074 switch( es->fmt.i_cat )
2087 /* Re-select another track when needed */
2090 for( i = 0; i < p_sys->i_es; i++ )
2092 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
2093 EsOutSelect( out, p_sys->es[i], false );
2097 free( es->psz_language );
2098 free( es->psz_language_code );
2100 es_format_Clean( &es->fmt );
2102 vlc_mutex_unlock( &p_sys->lock );
2108 * Control query handler
2110 * \param out the es_out to control
2111 * \param i_query A es_out query as defined in include/ninput.h
2112 * \param args a variable list of arguments for the query
2113 * \return VLC_SUCCESS or an error code
2115 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2117 es_out_sys_t *p_sys = out->p_sys;
2121 case ES_OUT_SET_ES_STATE:
2123 es_out_id_t *es = va_arg( args, es_out_id_t * );
2124 bool b = va_arg( args, int );
2125 if( b && !EsIsSelected( es ) )
2127 EsSelect( out, es );
2128 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2130 else if( !b && EsIsSelected( es ) )
2132 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2138 case ES_OUT_GET_ES_STATE:
2140 es_out_id_t *es = va_arg( args, es_out_id_t * );
2141 bool *pb = va_arg( args, bool * );
2143 *pb = EsIsSelected( es );
2147 case ES_OUT_SET_MODE:
2149 const int i_mode = va_arg( args, int );
2150 assert( i_mode == ES_OUT_MODE_NONE || i_mode == ES_OUT_MODE_ALL ||
2151 i_mode == ES_OUT_MODE_AUTO || i_mode == ES_OUT_MODE_PARTIAL ||
2152 i_mode == ES_OUT_MODE_END );
2154 if( i_mode != ES_OUT_MODE_NONE && !p_sys->b_active && p_sys->i_es > 0 )
2156 /* XXX Terminate vout if there are tracks but no video one.
2157 * This one is not mandatory but is he earliest place where it
2160 for( i = 0; i < p_sys->i_es; i++ )
2162 es_out_id_t *p_es = p_sys->es[i];
2163 if( p_es->fmt.i_cat == VIDEO_ES )
2166 if( i >= p_sys->i_es )
2167 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2169 p_sys->b_active = i_mode != ES_OUT_MODE_NONE;
2170 p_sys->i_mode = i_mode;
2172 /* Reapply policy mode */
2173 for( int i = 0; i < p_sys->i_es; i++ )
2175 if( EsIsSelected( p_sys->es[i] ) )
2176 EsUnselect( out, p_sys->es[i],
2177 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2179 for( int i = 0; i < p_sys->i_es; i++ )
2180 EsOutSelect( out, p_sys->es[i], false );
2181 if( i_mode == ES_OUT_MODE_END )
2182 EsOutTerminate( out );
2187 case ES_OUT_RESTART_ES:
2189 es_out_id_t *es = va_arg( args, es_out_id_t * );
2194 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2196 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2198 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2203 for( int i = 0; i < p_sys->i_es; i++ )
2207 if( es == p_sys->es[i] )
2209 EsOutSelect( out, es, true );
2215 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2217 if( EsIsSelected( p_sys->es[i] ) )
2219 if( i_query == ES_OUT_RESTART_ES )
2221 if( p_sys->es[i]->p_dec )
2223 EsDestroyDecoder( out, p_sys->es[i] );
2224 EsCreateDecoder( out, p_sys->es[i] );
2229 EsUnselect( out, p_sys->es[i],
2230 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2239 case ES_OUT_SET_ES_DEFAULT:
2241 es_out_id_t *es = va_arg( args, es_out_id_t * );
2245 /*p_sys->i_default_video_id = -1;*/
2246 /*p_sys->i_default_audio_id = -1;*/
2247 p_sys->i_default_sub_id = -1;
2249 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2251 /*p_sys->i_default_video_id = -1;*/
2253 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2255 /*p_sys->i_default_audio_id = -1;*/
2257 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2259 p_sys->i_default_sub_id = -1;
2263 /*if( es->fmt.i_cat == VIDEO_ES )
2264 p_sys->i_default_video_id = es->i_id;
2266 if( es->fmt.i_cat == AUDIO_ES )
2267 p_sys->i_default_audio_id = es->i_id;
2269 if( es->fmt.i_cat == SPU_ES )
2270 p_sys->i_default_sub_id = es->i_id;
2275 case ES_OUT_SET_PCR:
2276 case ES_OUT_SET_GROUP_PCR:
2278 es_out_pgrm_t *p_pgrm = NULL;
2282 /* Search program */
2283 if( i_query == ES_OUT_SET_PCR )
2285 p_pgrm = p_sys->p_pgrm;
2287 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2291 i_group = (int)va_arg( args, int );
2292 p_pgrm = EsOutProgramFind( out, i_group );
2295 return VLC_EGENERIC;
2297 i_pcr = (int64_t)va_arg( args, int64_t );
2298 if( i_pcr <= VLC_TS_INVALID )
2300 msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
2301 return VLC_EGENERIC;
2304 /* TODO do not use mdate() but proper stream acquisition date */
2306 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2308 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering,
2309 EsOutIsExtraBufferingAllowed( out ),
2312 if( p_pgrm == p_sys->p_pgrm )
2314 if( p_sys->b_buffering )
2316 /* Check buffering state on master clock update */
2317 EsOutDecodersStopBuffering( out, false );
2319 else if( b_late && ( !p_sys->p_input->p->p_sout ||
2320 !p_sys->p_input->p->b_out_pace_control ) )
2322 const mtime_t i_pts_delay_base = p_sys->i_pts_delay - p_sys->i_pts_jitter;
2323 mtime_t i_pts_delay = input_clock_GetJitter( p_pgrm->p_clock );
2325 /* Avoid dangerously high value */
2326 const mtime_t i_jitter_max = INT64_C(1000) * var_InheritInteger( p_sys->p_input, "clock-jitter" );
2327 if( i_pts_delay > __MIN( i_pts_delay_base + i_jitter_max, INPUT_PTS_DELAY_MAX ) )
2329 msg_Err( p_sys->p_input,
2330 "ES_OUT_SET_(GROUP_)PCR is called too late (jitter of %d ms ignored)",
2331 (int)(i_pts_delay - i_pts_delay_base) / 1000 );
2332 i_pts_delay = p_sys->i_pts_delay;
2336 msg_Err( p_sys->p_input,
2337 "ES_OUT_SET_(GROUP_)PCR is called too late (pts_delay increased to %d ms)",
2338 (int)(i_pts_delay/1000) );
2341 /* Force a rebufferization when we are too late */
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_SetJitter( out, i_pts_delay_base, i_pts_delay - i_pts_delay_base, 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:
2360 int i = va_arg( args, int );
2361 for( int j = 0; j < p_sys->i_pgrm; j++ )
2363 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2364 if( p_pgrm->i_id == i )
2366 EsOutProgramSelect( out, p_pgrm );
2370 return VLC_EGENERIC;
2373 case ES_OUT_SET_ES_FMT:
2375 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2376 * to update the p_extra data */
2377 es_out_id_t *es = va_arg( args, es_out_id_t * );
2378 es_format_t *p_fmt = va_arg( args, es_format_t * );
2380 return VLC_EGENERIC;
2382 if( p_fmt->i_extra )
2384 es->fmt.i_extra = p_fmt->i_extra;
2385 es->fmt.p_extra = xrealloc( es->fmt.p_extra, p_fmt->i_extra );
2386 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2391 EsDestroyDecoder( out, es );
2393 EsCreateDecoder( out, es );
2395 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2396 es->p_dec->fmt_in.p_extra =
2397 xrealloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2398 memcpy( es->p_dec->fmt_in.p_extra,
2399 p_fmt->p_extra, p_fmt->i_extra );
2406 case ES_OUT_SET_ES_SCRAMBLED_STATE:
2408 es_out_id_t *es = va_arg( args, es_out_id_t * );
2409 bool b_scrambled = (bool)va_arg( args, int );
2411 if( !es->b_scrambled != !b_scrambled )
2413 es->b_scrambled = b_scrambled;
2414 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2419 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2421 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2424 return VLC_EGENERIC;
2426 p_sys->i_preroll_end = i_date;
2430 case ES_OUT_SET_GROUP_META:
2432 int i_group = (int)va_arg( args, int );
2433 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2435 EsOutProgramMeta( out, i_group, p_meta );
2438 case ES_OUT_SET_GROUP_EPG:
2440 int i_group = (int)va_arg( args, int );
2441 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
2443 EsOutProgramEpg( out, i_group, p_epg );
2447 case ES_OUT_DEL_GROUP:
2449 int i_group = (int)va_arg( args, int );
2451 return EsOutProgramDel( out, i_group );
2454 case ES_OUT_SET_META:
2456 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2458 EsOutMeta( out, p_meta );
2462 case ES_OUT_GET_WAKE_UP:
2464 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2465 *pi_wakeup = EsOutGetWakeup( out );
2469 case ES_OUT_SET_ES_BY_ID:
2470 case ES_OUT_RESTART_ES_BY_ID:
2471 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2473 const int i_id = (int)va_arg( args, int );
2474 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2479 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2480 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2481 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2485 /* TODO if the lock is made non recursive it should be changed */
2486 int i_ret = es_out_Control( out, i_new_query, p_es );
2488 /* Clean up vout after user action (in active mode only).
2489 * FIXME it does not work well with multiple video windows */
2490 if( p_sys->b_active )
2491 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2495 case ES_OUT_GET_ES_OBJECTS_BY_ID:
2497 const int i_id = va_arg( args, int );
2498 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2500 return VLC_EGENERIC;
2502 vlc_object_t **pp_decoder = va_arg( args, vlc_object_t ** );
2503 vout_thread_t **pp_vout = va_arg( args, vout_thread_t ** );
2504 aout_instance_t **pp_aout = va_arg( args, aout_instance_t ** );
2508 *pp_decoder = vlc_object_hold( p_es->p_dec );
2509 input_DecoderGetObjects( p_es->p_dec, pp_vout, pp_aout );
2523 case ES_OUT_GET_BUFFERING:
2525 bool *pb = va_arg( args, bool* );
2526 *pb = p_sys->b_buffering;
2530 case ES_OUT_GET_EMPTY:
2532 bool *pb = va_arg( args, bool* );
2533 *pb = EsOutDecodersIsEmpty( out );
2537 case ES_OUT_SET_DELAY:
2539 const int i_cat = (int)va_arg( args, int );
2540 const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2541 EsOutSetDelay( out, i_cat, i_delay );
2545 case ES_OUT_SET_RECORD_STATE:
2547 bool b = va_arg( args, int );
2548 return EsOutSetRecord( out, b );
2551 case ES_OUT_SET_PAUSE_STATE:
2553 const bool b_source_paused = (bool)va_arg( args, int );
2554 const bool b_paused = (bool)va_arg( args, int );
2555 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2557 assert( !b_source_paused == !b_paused );
2558 EsOutChangePause( out, b_paused, i_date );
2563 case ES_OUT_SET_RATE:
2565 const int i_src_rate = (int)va_arg( args, int );
2566 const int i_rate = (int)va_arg( args, int );
2568 assert( i_src_rate == i_rate );
2569 EsOutChangeRate( out, i_rate );
2574 case ES_OUT_SET_TIME:
2576 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2578 assert( i_date == -1 );
2579 EsOutChangePosition( out );
2584 case ES_OUT_SET_FRAME_NEXT:
2585 EsOutFrameNext( out );
2588 case ES_OUT_SET_TIMES:
2590 double f_position = (double)va_arg( args, double );
2591 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2592 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2594 input_SendEventLength( p_sys->p_input, i_length );
2596 if( !p_sys->b_buffering )
2600 /* Fix for buffering delay */
2601 if( !p_sys->p_input->p->p_sout ||
2602 !p_sys->p_input->p->b_out_pace_control )
2603 i_delay = EsOutGetBuffering( out );
2612 f_position -= (double)i_delay / i_length;
2613 if( f_position < 0 )
2616 input_SendEventPosition( p_sys->p_input, f_position, i_time );
2620 case ES_OUT_SET_JITTER:
2622 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2623 mtime_t i_pts_jitter = (mtime_t)va_arg( args, mtime_t );
2624 int i_cr_average = (int)va_arg( args, int );
2626 bool b_change_clock =
2627 i_pts_delay + i_pts_jitter != p_sys->i_pts_delay ||
2628 i_cr_average != p_sys->i_cr_average;
2630 assert( i_pts_jitter >= 0 );
2631 p_sys->i_pts_delay = i_pts_delay + i_pts_jitter;
2632 p_sys->i_pts_jitter = i_pts_jitter;
2633 p_sys->i_cr_average = i_cr_average;
2635 for( int i = 0; i < p_sys->i_pgrm && b_change_clock; i++ )
2636 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2637 i_pts_delay + i_pts_jitter, i_cr_average );
2641 case ES_OUT_GET_PCR_SYSTEM:
2643 if( p_sys->b_buffering )
2644 return VLC_EGENERIC;
2646 es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
2648 return VLC_EGENERIC;
2650 mtime_t *pi_system = va_arg( args, mtime_t *);
2651 mtime_t *pi_delay = va_arg( args, mtime_t *);
2652 input_clock_GetSystemOrigin( p_pgrm->p_clock, pi_system, pi_delay );
2656 case ES_OUT_MODIFY_PCR_SYSTEM:
2658 if( p_sys->b_buffering )
2659 return VLC_EGENERIC;
2661 es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
2663 return VLC_EGENERIC;
2665 const bool b_absolute = va_arg( args, int );
2666 const mtime_t i_system = va_arg( args, mtime_t );
2667 input_clock_ChangeSystemOrigin( p_pgrm->p_clock, b_absolute, i_system );
2672 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2673 return VLC_EGENERIC;
2676 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2678 es_out_sys_t *p_sys = out->p_sys;
2681 vlc_mutex_lock( &p_sys->lock );
2682 i_ret = EsOutControlLocked( out, i_query, args );
2683 vlc_mutex_unlock( &p_sys->lock );
2688 /****************************************************************************
2689 * LanguageGetName: try to expend iso639 into plain name
2690 ****************************************************************************/
2691 static char *LanguageGetName( const char *psz_code )
2693 const iso639_lang_t *pl;
2695 if( psz_code == NULL )
2697 return strdup( "" );
2700 if( strlen( psz_code ) == 2 )
2702 pl = GetLang_1( psz_code );
2704 else if( strlen( psz_code ) == 3 )
2706 pl = GetLang_2B( psz_code );
2707 if( !strcmp( pl->psz_iso639_1, "??" ) )
2709 pl = GetLang_2T( psz_code );
2714 return strdup( psz_code );
2717 if( !strcmp( pl->psz_iso639_1, "??" ) )
2719 return strdup( psz_code );
2723 if( *pl->psz_native_name )
2725 return strdup( pl->psz_native_name );
2727 return strdup( pl->psz_eng_name );
2731 /* Get a 2 char code */
2732 static char *LanguageGetCode( const char *psz_lang )
2734 const iso639_lang_t *pl;
2736 if( psz_lang == NULL || *psz_lang == '\0' )
2737 return strdup("??");
2739 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
2741 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2742 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2743 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2744 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2745 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2749 if( pl->psz_eng_name != NULL )
2750 return strdup( pl->psz_iso639_1 );
2752 return strdup("??");
2755 static char **LanguageSplit( const char *psz_langs, bool b_default_any )
2762 if( psz_langs == NULL ) return NULL;
2764 psz_parser = psz_dup = strdup(psz_langs);
2766 while( psz_parser && *psz_parser )
2771 psz = strchr(psz_parser, ',' );
2772 if( psz ) *psz++ = '\0';
2774 if( !strcmp( psz_parser, "any" ) )
2776 TAB_APPEND( i_psz, ppsz, strdup("any") );
2778 else if( !strcmp( psz_parser, "none" ) )
2780 TAB_APPEND( i_psz, ppsz, strdup("none") );
2784 psz_code = LanguageGetCode( psz_parser );
2785 if( strcmp( psz_code, "??" ) )
2787 TAB_APPEND( i_psz, ppsz, psz_code );
2800 if( b_default_any && strcmp( ppsz[i_psz - 1], "none" ) )
2801 TAB_APPEND( i_psz, ppsz, strdup("any") );
2802 TAB_APPEND( i_psz, ppsz, NULL );
2809 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2813 if( !ppsz_langs || !psz_lang ) return -1;
2815 for( i = 0; ppsz_langs[i]; i++ )
2817 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2818 !strcasecmp( ppsz_langs[i], "any" ) )
2820 if( !strcasecmp( ppsz_langs[i], "none" ) )
2827 /****************************************************************************
2829 * - add meta info to the playlist item
2830 ****************************************************************************/
2831 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2833 es_out_sys_t *p_sys = out->p_sys;
2834 input_thread_t *p_input = p_sys->p_input;
2835 const es_format_t *p_fmt_es = &es->fmt;
2838 /* Create category */
2840 snprintf( psz_cat, sizeof(psz_cat),_("Stream %d"), es->i_meta_id );
2841 info_category_t *p_cat = info_category_New( psz_cat );
2845 /* Add informations */
2846 const char *psz_type;
2847 switch( fmt->i_cat )
2850 psz_type = _("Audio");
2853 psz_type = _("Video");
2856 psz_type = _("Subtitle");
2864 info_category_AddInfo( p_cat, _("Type"), "%s", psz_type );
2866 if( es->i_meta_id != es->i_id )
2867 info_category_AddInfo( p_cat, _("Original ID"),
2870 const char *psz_codec_description =
2871 vlc_fourcc_GetDescription( p_fmt_es->i_cat, p_fmt_es->i_codec );
2872 const vlc_fourcc_t i_codec_fourcc = ( p_fmt_es->i_original_fourcc )?
2873 p_fmt_es->i_original_fourcc : p_fmt_es->i_codec;
2874 if( psz_codec_description )
2875 info_category_AddInfo( p_cat, _("Codec"), "%s (%.4s)",
2876 psz_codec_description, (char*)&i_codec_fourcc );
2878 info_category_AddInfo( p_cat, _("Codec"), "%.4s",
2879 (char*)&i_codec_fourcc );
2881 if( es->psz_language && *es->psz_language )
2882 info_category_AddInfo( p_cat, _("Language"), "%s",
2884 if( fmt->psz_description && *fmt->psz_description )
2885 info_category_AddInfo( p_cat, _("Description"), "%s",
2886 fmt->psz_description );
2888 switch( fmt->i_cat )
2891 info_category_AddInfo( p_cat, _("Type"), _("Audio") );
2893 if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
2894 info_category_AddInfo( p_cat, _("Channels"), "%s",
2895 _( aout_FormatPrintChannels( &fmt->audio ) ) );
2896 else if( fmt->audio.i_channels > 0 )
2897 info_category_AddInfo( p_cat, _("Channels"), "%u",
2898 fmt->audio.i_channels );
2900 if( fmt->audio.i_rate > 0 )
2902 info_category_AddInfo( p_cat, _("Sample rate"), _("%u Hz"),
2903 fmt->audio.i_rate );
2904 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2905 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2908 unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2909 if( i_bitspersample <= 0 )
2910 i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2911 if( i_bitspersample > 0 )
2912 info_category_AddInfo( p_cat, _("Bits per sample"), "%u",
2915 if( fmt->i_bitrate > 0 )
2917 info_category_AddInfo( p_cat, _("Bitrate"), _("%u kb/s"),
2918 fmt->i_bitrate / 1000 );
2919 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2920 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2922 for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2924 const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2925 if( !p_rg->pb_gain[i] )
2927 const char *psz_name;
2928 if( i == AUDIO_REPLAY_GAIN_TRACK )
2929 psz_name = _("Track replay gain");
2931 psz_name = _("Album replay gain");
2932 info_category_AddInfo( p_cat, psz_name, _("%.2f dB"),
2938 info_category_AddInfo( p_cat, _("Type"), _("Video") );
2940 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2941 info_category_AddInfo( p_cat, _("Resolution"), "%ux%u",
2942 fmt->video.i_width, fmt->video.i_height );
2944 if( fmt->video.i_visible_width > 0 &&
2945 fmt->video.i_visible_height > 0 )
2946 info_category_AddInfo( p_cat, _("Display resolution"), "%ux%u",
2947 fmt->video.i_visible_width,
2948 fmt->video.i_visible_height);
2949 if( fmt->video.i_frame_rate > 0 &&
2950 fmt->video.i_frame_rate_base > 0 )
2952 div = lldiv( (float)fmt->video.i_frame_rate /
2953 fmt->video.i_frame_rate_base * 1000000,
2956 info_category_AddInfo( p_cat, _("Frame rate"), "%"PRId64".%06u",
2957 div.quot, (unsigned int )div.rem );
2959 info_category_AddInfo( p_cat, _("Frame rate"), "%"PRId64,
2965 info_category_AddInfo( p_cat, _("Type"), _("Subtitle") );
2972 /* Append generic meta */
2975 char **ppsz_all_keys = vlc_meta_CopyExtraNames( p_meta );
2976 for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
2978 char *psz_key = ppsz_all_keys[i];
2979 const char *psz_value = vlc_meta_GetExtra( p_meta, psz_key );
2982 info_category_AddInfo( p_cat, vlc_gettext(psz_key), "%s",
2983 vlc_gettext(psz_value) );
2986 free( ppsz_all_keys );
2989 input_Control( p_input, INPUT_REPLACE_INFOS, p_cat );