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 */
156 mtime_t i_pause_date;
158 /* Current preroll */
159 mtime_t i_preroll_end;
161 /* Used for buffering */
163 mtime_t i_buffering_extra_initial;
164 mtime_t i_buffering_extra_stream;
165 mtime_t i_buffering_extra_system;
168 sout_instance_t *p_sout_record;
171 static es_out_id_t *EsOutAdd ( es_out_t *, const es_format_t * );
172 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
173 static void EsOutDel ( es_out_t *, es_out_id_t * );
174 static int EsOutControl( es_out_t *, int i_query, va_list );
175 static void EsOutDelete ( es_out_t * );
177 static void EsOutTerminate( 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;
242 vlc_mutex_init_recursive( &p_sys->lock );
243 p_sys->p_input = p_input;
245 p_sys->b_active = false;
246 p_sys->i_mode = ES_OUT_MODE_NONE;
249 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
250 p_sys->p_pgrm = NULL;
254 TAB_INIT( p_sys->i_es, p_sys->es );
261 p_sys->i_audio_last = var_GetInteger( p_input, "audio-track" );
263 p_sys->i_sub_last = var_GetInteger( p_input, "sub-track" );
265 p_sys->i_default_sub_id = -1;
267 if( !p_input->b_preparsing )
271 psz_string = var_GetString( p_input, "audio-language" );
272 p_sys->ppsz_audio_language = LanguageSplit( psz_string );
273 if( p_sys->ppsz_audio_language )
275 for( int i = 0; p_sys->ppsz_audio_language[i]; i++ )
276 msg_Dbg( p_input, "selected audio language[%d] %s",
277 i, p_sys->ppsz_audio_language[i] );
281 psz_string = var_GetString( p_input, "sub-language" );
282 p_sys->ppsz_sub_language = LanguageSplit( psz_string );
283 if( p_sys->ppsz_sub_language )
285 for( int i = 0; p_sys->ppsz_sub_language[i]; i++ )
286 msg_Dbg( p_input, "selected subtitle language[%d] %s",
287 i, p_sys->ppsz_sub_language[i] );
293 p_sys->ppsz_sub_language = NULL;
294 p_sys->ppsz_audio_language = NULL;
297 p_sys->i_audio_id = var_GetInteger( p_input, "audio-track-id" );
299 p_sys->i_sub_id = var_GetInteger( p_input, "sub-track-id" );
301 p_sys->p_es_audio = NULL;
302 p_sys->p_es_video = NULL;
303 p_sys->p_es_sub = NULL;
305 p_sys->i_audio_delay= 0;
306 p_sys->i_spu_delay = 0;
308 p_sys->b_paused = false;
309 p_sys->i_pause_date = -1;
311 p_sys->i_rate = i_rate;
312 p_sys->i_pts_delay = 0;
313 p_sys->i_cr_average = 0;
315 p_sys->b_buffering = true;
316 p_sys->i_buffering_extra_initial = 0;
317 p_sys->i_buffering_extra_stream = 0;
318 p_sys->i_buffering_extra_system = 0;
319 p_sys->i_preroll_end = -1;
321 p_sys->p_sout_record = NULL;
326 /*****************************************************************************
328 *****************************************************************************/
329 static void EsOutDelete( es_out_t *out )
331 es_out_sys_t *p_sys = out->p_sys;
333 assert( !p_sys->i_es && !p_sys->i_pgrm && !p_sys->p_pgrm );
334 if( p_sys->ppsz_audio_language )
336 for( int i = 0; p_sys->ppsz_audio_language[i]; i++ )
337 free( p_sys->ppsz_audio_language[i] );
338 free( p_sys->ppsz_audio_language );
340 if( p_sys->ppsz_sub_language )
342 for( int i = 0; p_sys->ppsz_sub_language[i]; i++ )
343 free( p_sys->ppsz_sub_language[i] );
344 free( p_sys->ppsz_sub_language );
347 vlc_mutex_destroy( &p_sys->lock );
353 static void EsOutTerminate( es_out_t *out )
355 es_out_sys_t *p_sys = out->p_sys;
357 if( p_sys->p_sout_record )
358 EsOutSetRecord( out, false );
360 for( int i = 0; i < p_sys->i_es; i++ )
362 if( p_sys->es[i]->p_dec )
363 input_DecoderDelete( p_sys->es[i]->p_dec );
365 free( p_sys->es[i]->psz_language );
366 free( p_sys->es[i]->psz_language_code );
367 es_format_Clean( &p_sys->es[i]->fmt );
369 free( p_sys->es[i] );
371 TAB_CLEAN( p_sys->i_es, p_sys->es );
373 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
374 for( int i = 0; i < p_sys->i_pgrm; i++ )
376 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
377 input_clock_Delete( p_pgrm->p_clock );
378 free( p_pgrm->psz_now_playing );
379 free( p_pgrm->psz_publisher );
380 free( p_pgrm->psz_name );
384 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
386 p_sys->p_pgrm = NULL;
388 input_item_SetEpgOffline( p_sys->p_input->p->p_item );
389 input_SendEventMetaEpg( p_sys->p_input );
392 static mtime_t EsOutGetWakeup( es_out_t *out )
394 es_out_sys_t *p_sys = out->p_sys;
395 input_thread_t *p_input = p_sys->p_input;
400 /* We do not have a wake up date if the input cannot have its speed
401 * controlled or sout is imposing its own or while buffering
403 * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
404 if( !p_input->p->b_can_pace_control ||
405 p_input->p->b_out_pace_control ||
409 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
412 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
417 /* Special HACK, -i_id is the cat of the stream */
418 return (es_out_id_t*)((uint8_t*)NULL-i_id);
421 for( i = 0; i < out->p_sys->i_es; i++ )
423 if( out->p_sys->es[i]->i_id == i_id )
424 return out->p_sys->es[i];
429 static bool EsOutDecodersIsEmpty( es_out_t *out )
431 es_out_sys_t *p_sys = out->p_sys;
434 if( p_sys->b_buffering && p_sys->p_pgrm )
436 EsOutDecodersStopBuffering( out, true );
437 if( p_sys->b_buffering )
441 for( i = 0; i < p_sys->i_es; i++ )
443 es_out_id_t *es = p_sys->es[i];
445 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
447 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
453 static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
455 es_out_sys_t *p_sys = out->p_sys;
457 if( i_cat == AUDIO_ES )
458 p_sys->i_audio_delay = i_delay;
459 else if( i_cat == SPU_ES )
460 p_sys->i_spu_delay = i_delay;
462 for( int i = 0; i < p_sys->i_es; i++ )
463 EsOutDecoderChangeDelay( out, p_sys->es[i] );
466 static int EsOutSetRecord( es_out_t *out, bool b_record )
468 es_out_sys_t *p_sys = out->p_sys;
469 input_thread_t *p_input = p_sys->p_input;
471 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
475 char *psz_path = var_CreateGetNonEmptyString( p_input, "input-record-path" );
478 if( var_CountChoices( p_input, "video-es" ) )
479 psz_path = config_GetUserDir( VLC_VIDEOS_DIR );
480 else if( var_CountChoices( p_input, "audio-es" ) )
481 psz_path = config_GetUserDir( VLC_MUSIC_DIR );
483 psz_path = config_GetUserDir( VLC_DOWNLOAD_DIR );
486 char *psz_sout = NULL; // TODO conf
488 if( !psz_sout && psz_path )
490 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
493 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
504 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
508 if( !p_sys->p_sout_record )
511 for( int i = 0; i < p_sys->i_es; i++ )
513 es_out_id_t *p_es = p_sys->es[i];
515 if( !p_es->p_dec || p_es->p_master )
518 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
519 if( p_es->p_dec_record && p_sys->b_buffering )
520 input_DecoderStartBuffering( p_es->p_dec_record );
525 for( int i = 0; i < p_sys->i_es; i++ )
527 es_out_id_t *p_es = p_sys->es[i];
529 if( !p_es->p_dec_record )
532 input_DecoderDelete( p_es->p_dec_record );
533 p_es->p_dec_record = NULL;
536 sout_DeleteInstance( p_sys->p_sout_record );
538 p_sys->p_sout_record = NULL;
543 static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
545 es_out_sys_t *p_sys = out->p_sys;
547 /* XXX the order is important */
550 EsOutDecodersChangePause( out, true, i_date );
551 EsOutProgramChangePause( out, true, i_date );
555 if( p_sys->i_buffering_extra_initial > 0 )
557 mtime_t i_stream_start;
558 mtime_t i_system_start;
559 mtime_t i_stream_duration;
560 mtime_t i_system_duration;
562 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
563 &i_stream_start, &i_system_start,
564 &i_stream_duration, &i_system_duration );
567 /* FIXME pcr != exactly what wanted */
568 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;
571 p_sys->i_buffering_extra_initial = 0;
572 p_sys->i_buffering_extra_stream = 0;
573 p_sys->i_buffering_extra_system = 0;
575 EsOutProgramChangePause( out, false, i_date );
576 EsOutDecodersChangePause( out, false, i_date );
578 EsOutProgramsChangeRate( out );
580 p_sys->b_paused = b_paused;
581 p_sys->i_pause_date = i_date;
584 static void EsOutChangeRate( es_out_t *out, int i_rate )
586 es_out_sys_t *p_sys = out->p_sys;
588 p_sys->i_rate = i_rate;
589 EsOutProgramsChangeRate( out );
592 static void EsOutChangePosition( es_out_t *out )
594 es_out_sys_t *p_sys = out->p_sys;
596 input_SendEventCache( p_sys->p_input, 0.0 );
598 for( int i = 0; i < p_sys->i_es; i++ )
600 es_out_id_t *p_es = p_sys->es[i];
605 input_DecoderStartBuffering( p_es->p_dec );
607 if( p_es->p_dec_record )
608 input_DecoderStartBuffering( p_es->p_dec_record );
611 for( int i = 0; i < p_sys->i_pgrm; i++ )
612 input_clock_Reset( p_sys->pgrm[i]->p_clock );
614 p_sys->b_buffering = true;
615 p_sys->i_buffering_extra_initial = 0;
616 p_sys->i_buffering_extra_stream = 0;
617 p_sys->i_buffering_extra_system = 0;
618 p_sys->i_preroll_end = -1;
623 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
625 es_out_sys_t *p_sys = out->p_sys;
628 mtime_t i_stream_start;
629 mtime_t i_system_start;
630 mtime_t i_stream_duration;
631 mtime_t i_system_duration;
632 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
633 &i_stream_start, &i_system_start,
634 &i_stream_duration, &i_system_duration );
635 assert( !i_ret || b_forced );
639 mtime_t i_preroll_duration = 0;
640 if( p_sys->i_preroll_end >= 0 )
641 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
643 const mtime_t i_buffering_duration = p_sys->i_pts_delay +
645 p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
647 if( i_stream_duration <= i_buffering_duration && !b_forced )
649 const double f_level = (double)i_stream_duration / i_buffering_duration;
650 input_SendEventCache( p_sys->p_input, f_level );
652 msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
655 input_SendEventCache( p_sys->p_input, 1.0 );
657 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
658 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
659 p_sys->b_buffering = false;
660 p_sys->i_preroll_end = -1;
662 if( p_sys->i_buffering_extra_initial > 0 )
668 const mtime_t i_decoder_buffering_start = mdate();
669 for( int i = 0; i < p_sys->i_es; i++ )
671 es_out_id_t *p_es = p_sys->es[i];
673 if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
675 input_DecoderWaitBuffering( p_es->p_dec );
676 if( p_es->p_dec_record )
677 input_DecoderWaitBuffering( p_es->p_dec_record );
680 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
681 (int)(mdate() - i_decoder_buffering_start)/1000 );
683 /* Here is a good place to destroy unused vout with every demuxer */
684 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
687 const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
688 const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
690 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, true,
691 i_current_date + i_wakeup_delay - i_buffering_duration );
693 for( int i = 0; i < p_sys->i_es; i++ )
695 es_out_id_t *p_es = p_sys->es[i];
700 input_DecoderStopBuffering( p_es->p_dec );
701 if( p_es->p_dec_record )
702 input_DecoderStopBuffering( p_es->p_dec_record );
705 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
707 es_out_sys_t *p_sys = out->p_sys;
709 /* Pause decoders first */
710 for( int i = 0; i < p_sys->i_es; i++ )
712 es_out_id_t *es = p_sys->es[i];
716 input_DecoderChangePause( es->p_dec, b_paused, i_date );
717 if( es->p_dec_record )
718 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
723 static bool EsOutIsExtraBufferingAllowed( es_out_t *out )
725 es_out_sys_t *p_sys = out->p_sys;
728 for( int i = 0; i < p_sys->i_es; i++ )
730 es_out_id_t *p_es = p_sys->es[i];
733 i_size += input_DecoderGetFifoSize( p_es->p_dec );
734 if( p_es->p_dec_record )
735 i_size += input_DecoderGetFifoSize( p_es->p_dec_record );
737 //fprintf( stderr, "----- EsOutIsExtraBufferingAllowed =% 5d kbytes -- ", i_size / 1024 );
739 /* TODO maybe we want to be able to tune it ? */
740 #if defined(OPTIMIZE_MEMORY)
741 const size_t i_level_high = 500000; /* 0.5 Mbytes */
743 const size_t i_level_high = 10000000; /* 10 Mbytes */
745 return i_size < i_level_high;
748 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
750 es_out_sys_t *p_sys = out->p_sys;
752 for( int i = 0; i < p_sys->i_pgrm; i++ )
753 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
756 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
758 es_out_sys_t *p_sys = out->p_sys;
761 if( p_es->fmt.i_cat == AUDIO_ES )
762 i_delay = p_sys->i_audio_delay;
763 else if( p_es->fmt.i_cat == SPU_ES )
764 i_delay = p_sys->i_spu_delay;
769 input_DecoderChangeDelay( p_es->p_dec, i_delay );
770 if( p_es->p_dec_record )
771 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
774 static void EsOutProgramsChangeRate( es_out_t *out )
776 es_out_sys_t *p_sys = out->p_sys;
778 for( int i = 0; i < p_sys->i_pgrm; i++ )
779 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
782 static void EsOutFrameNext( es_out_t *out )
784 es_out_sys_t *p_sys = out->p_sys;
785 es_out_id_t *p_es_video = NULL;
787 if( p_sys->b_buffering )
789 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
793 assert( p_sys->b_paused );
795 for( int i = 0; i < p_sys->i_es; i++ )
797 es_out_id_t *p_es = p_sys->es[i];
799 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
808 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
813 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
815 msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
817 if( i_duration <= 0 )
818 i_duration = 40*1000;
820 /* FIXME it is not a clean way ? */
821 if( p_sys->i_buffering_extra_initial <= 0 )
823 mtime_t i_stream_start;
824 mtime_t i_system_start;
825 mtime_t i_stream_duration;
826 mtime_t i_system_duration;
829 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
830 &i_stream_start, &i_system_start,
831 &i_stream_duration, &i_system_duration );
835 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
836 p_sys->i_buffering_extra_system =
837 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
840 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
842 p_sys->b_buffering = true;
843 p_sys->i_buffering_extra_system += i_duration;
844 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
845 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
846 INPUT_RATE_DEFAULT / i_rate;
848 p_sys->i_preroll_end = -1;
850 static mtime_t EsOutGetBuffering( es_out_t *out )
852 es_out_sys_t *p_sys = out->p_sys;
858 mtime_t i_stream_start;
859 mtime_t i_system_start;
860 mtime_t i_stream_duration;
861 mtime_t i_system_duration;
862 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
863 &i_stream_start, &i_system_start,
864 &i_stream_duration, &i_system_duration );
871 if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
873 i_delay = i_stream_duration;
877 mtime_t i_system_duration;
878 if( p_sys->b_paused )
880 i_system_duration = p_sys->i_pause_date - i_system_start;
881 if( p_sys->i_buffering_extra_initial > 0 )
882 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
886 i_system_duration = mdate() - i_system_start;
889 const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
890 i_delay = p_sys->i_pts_delay - i_consumed;
897 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
898 const es_format_t *fmt, const char *psz_language,
901 es_out_sys_t *p_sys = out->p_sys;
902 input_thread_t *p_input = p_sys->p_input;
903 vlc_value_t val, text;
907 if( EsFmtIsTeletext( fmt ) )
908 input_SendEventTeletextDel( p_sys->p_input, i_id );
910 input_SendEventEsDel( p_input, fmt->i_cat, i_id );
914 /* Get the number of ES already added */
916 if( fmt->i_cat == AUDIO_ES )
917 psz_var = "audio-es";
918 else if( fmt->i_cat == VIDEO_ES )
919 psz_var = "video-es";
923 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
928 /* First one, we need to add the "Disable" choice */
929 val2.i_int = -1; text.psz_string = _("Disable");
930 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
934 /* Take care of the ES description */
935 if( fmt->psz_description && *fmt->psz_description )
937 if( psz_language && *psz_language )
939 if( asprintf( &text.psz_string, "%s - [%s]", fmt->psz_description,
940 psz_language ) == -1 )
941 text.psz_string = NULL;
943 else text.psz_string = strdup( fmt->psz_description );
947 if( psz_language && *psz_language )
949 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
950 text.psz_string = NULL;
954 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
955 text.psz_string = NULL;
959 input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
960 if( EsFmtIsTeletext( fmt ) )
963 snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
964 fmt->subs.teletext.i_magazine,
965 fmt->subs.teletext.i_page );
966 input_SendEventTeletextAdd( p_sys->p_input,
967 i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
970 free( text.psz_string );
973 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
976 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
979 /* EsOutProgramSelect:
980 * Select a program and update the object variable
982 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
984 es_out_sys_t *p_sys = out->p_sys;
985 input_thread_t *p_input = p_sys->p_input;
988 if( p_sys->p_pgrm == p_pgrm )
989 return; /* Nothing to do */
993 es_out_pgrm_t *old = p_sys->p_pgrm;
994 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
996 for( i = 0; i < p_sys->i_es; i++ )
998 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
999 p_sys->i_mode != ES_OUT_MODE_ALL )
1000 EsUnselect( out, p_sys->es[i], true );
1003 p_sys->p_es_audio = NULL;
1004 p_sys->p_es_sub = NULL;
1005 p_sys->p_es_video = NULL;
1008 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
1010 /* Mark it selected */
1011 p_pgrm->b_selected = true;
1013 /* Switch master stream */
1014 p_sys->p_pgrm = p_pgrm;
1016 /* Update "program" */
1017 input_SendEventProgramSelect( p_input, p_pgrm->i_id );
1020 input_SendEventEsDel( p_input, AUDIO_ES, -1 );
1021 input_SendEventEsDel( p_input, VIDEO_ES, -1 );
1022 input_SendEventEsDel( p_input, SPU_ES, -1 );
1023 input_SendEventTeletextDel( p_input, -1 );
1024 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
1027 var_SetInteger( p_input, "teletext-es", -1 );
1029 for( i = 0; i < p_sys->i_es; i++ )
1031 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
1032 EsOutESVarUpdate( out, p_sys->es[i], false );
1033 EsOutSelect( out, p_sys->es[i], false );
1036 /* Update now playing */
1037 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1038 input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
1040 input_SendEventMeta( p_input );
1046 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
1048 es_out_sys_t *p_sys = out->p_sys;
1049 input_thread_t *p_input = p_sys->p_input;
1051 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1056 p_pgrm->i_id = i_group;
1058 p_pgrm->b_selected = false;
1059 p_pgrm->b_scrambled = false;
1060 p_pgrm->psz_name = NULL;
1061 p_pgrm->psz_now_playing = NULL;
1062 p_pgrm->psz_publisher = NULL;
1063 p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1064 if( !p_pgrm->p_clock )
1069 if( p_sys->b_paused )
1070 input_clock_ChangePause( p_pgrm->p_clock, p_sys->b_paused, p_sys->i_pause_date );
1071 input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1074 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1076 /* Update "program" variable */
1077 input_SendEventProgramAdd( p_input, i_group, NULL );
1079 if( i_group == var_GetInteger( p_input, "program" ) )
1080 EsOutProgramSelect( out, p_pgrm );
1088 static int EsOutProgramDel( es_out_t *out, int i_group )
1090 es_out_sys_t *p_sys = out->p_sys;
1091 input_thread_t *p_input = p_sys->p_input;
1092 es_out_pgrm_t *p_pgrm = NULL;
1095 for( i = 0; i < p_sys->i_pgrm; i++ )
1097 if( p_sys->pgrm[i]->i_id == i_group )
1099 p_pgrm = p_sys->pgrm[i];
1104 if( p_pgrm == NULL )
1105 return VLC_EGENERIC;
1109 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1110 i_group, p_pgrm->i_es );
1111 return VLC_EGENERIC;
1114 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1116 /* If program is selected we need to unselect it */
1117 if( p_sys->p_pgrm == p_pgrm )
1118 p_sys->p_pgrm = NULL;
1120 input_clock_Delete( p_pgrm->p_clock );
1122 free( p_pgrm->psz_name );
1123 free( p_pgrm->psz_now_playing );
1124 free( p_pgrm->psz_publisher );
1127 /* Update "program" variable */
1128 input_SendEventProgramDel( p_input, i_group );
1135 static es_out_pgrm_t *EsOutProgramFind( es_out_t *p_out, int i_group )
1137 es_out_sys_t *p_sys = p_out->p_sys;
1139 for( int i = 0; i < p_sys->i_pgrm; i++ )
1141 if( p_sys->pgrm[i]->i_id == i_group )
1142 return p_sys->pgrm[i];
1144 return EsOutProgramAdd( p_out, i_group );
1147 /* EsOutProgramMeta:
1149 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1152 if( p_pgrm->psz_name )
1154 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1159 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1165 static void EsOutProgramMeta( es_out_t *out, int i_group, const vlc_meta_t *p_meta )
1167 es_out_sys_t *p_sys = out->p_sys;
1168 es_out_pgrm_t *p_pgrm;
1169 input_thread_t *p_input = p_sys->p_input;
1170 const char *psz_title = NULL;
1171 const char *psz_provider = NULL;
1174 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1176 /* Check against empty meta data (empty for what we handle) */
1177 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1178 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1179 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1180 vlc_meta_GetExtraCount( p_meta ) <= 0 )
1185 p_pgrm = EsOutProgramFind( out, i_group );
1190 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1191 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1193 /* Update the description text of the program */
1194 if( psz_title && *psz_title )
1196 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1198 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1200 /* Remove old entries */
1201 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1202 /* TODO update epg name ?
1203 * TODO update scrambled info name ? */
1206 free( p_pgrm->psz_name );
1207 p_pgrm->psz_name = strdup( psz_title );
1210 if( psz_provider && *psz_provider )
1212 if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1217 psz_text = strdup( psz_title );
1220 /* ugly but it works */
1223 input_SendEventProgramDel( p_input, i_group );
1224 input_SendEventProgramAdd( p_input, i_group, psz_text );
1225 if( p_sys->p_pgrm == p_pgrm )
1226 input_SendEventProgramSelect( p_input, i_group );
1232 char **ppsz_all_keys = vlc_meta_CopyExtraNames(p_meta );
1234 info_category_t *p_cat = NULL;
1235 if( psz_provider || *ppsz_all_keys[0] )
1237 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1239 p_cat = info_category_New( psz_cat );
1243 for( i = 0; ppsz_all_keys[i]; i++ )
1246 info_category_AddInfo( p_cat, vlc_gettext(ppsz_all_keys[i]), "%s",
1247 vlc_meta_GetExtra( p_meta, ppsz_all_keys[i] ) );
1248 free( ppsz_all_keys[i] );
1250 free( ppsz_all_keys );
1254 if( p_sys->p_pgrm == p_pgrm )
1256 input_item_SetPublisher( p_input->p->p_item, psz_provider );
1257 input_SendEventMeta( p_input );
1260 info_category_AddInfo( p_cat, vlc_meta_TypeToLocalizedString(vlc_meta_Publisher),
1261 "%s",psz_provider );
1264 input_Control( p_input, INPUT_MERGE_INFOS, p_cat );
1267 static void EsOutProgramEpg( es_out_t *out, int i_group, const vlc_epg_t *p_epg )
1269 es_out_sys_t *p_sys = out->p_sys;
1270 input_thread_t *p_input = p_sys->p_input;
1271 input_item_t *p_item = p_input->p->p_item;
1272 es_out_pgrm_t *p_pgrm;
1276 p_pgrm = EsOutProgramFind( out, i_group );
1281 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1282 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, psz_cat );
1288 epg.psz_name = psz_cat;
1290 input_item_SetEpg( p_item, &epg );
1291 input_SendEventMetaEpg( p_sys->p_input );
1293 /* Update now playing */
1294 free( p_pgrm->psz_now_playing );
1295 p_pgrm->psz_now_playing = NULL;
1297 vlc_mutex_lock( &p_item->lock );
1298 for( int i = 0; i < p_item->i_epg; i++ )
1300 const vlc_epg_t *p_tmp = p_item->pp_epg[i];
1302 if( p_tmp->psz_name && !strcmp(p_tmp->psz_name, psz_cat) )
1304 if( p_tmp->p_current && p_tmp->p_current->psz_name && *p_tmp->p_current->psz_name )
1305 p_pgrm->psz_now_playing = strdup( p_tmp->p_current->psz_name );
1309 vlc_mutex_unlock( &p_item->lock );
1311 if( p_pgrm == p_sys->p_pgrm )
1313 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1314 input_SendEventMeta( p_input );
1317 if( p_pgrm->psz_now_playing )
1319 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1320 vlc_meta_TypeToLocalizedString(vlc_meta_NowPlaying),
1321 p_pgrm->psz_now_playing );
1325 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1326 vlc_meta_TypeToLocalizedString(vlc_meta_NowPlaying) );
1332 static void EsOutProgramUpdateScrambled( es_out_t *p_out, es_out_pgrm_t *p_pgrm )
1334 es_out_sys_t *p_sys = p_out->p_sys;
1335 input_thread_t *p_input = p_sys->p_input;
1336 bool b_scrambled = false;
1338 for( int i = 0; i < p_sys->i_es; i++ )
1340 if( p_sys->es[i]->p_pgrm == p_pgrm && p_sys->es[i]->b_scrambled )
1346 if( !p_pgrm->b_scrambled == !b_scrambled )
1349 p_pgrm->b_scrambled = b_scrambled;
1350 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1353 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Scrambled"), _("Yes") );
1355 input_Control( p_input, INPUT_DEL_INFO, psz_cat, _("Scrambled") );
1357 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, b_scrambled );
1360 static void EsOutMeta( es_out_t *p_out, const vlc_meta_t *p_meta )
1362 es_out_sys_t *p_sys = p_out->p_sys;
1363 input_thread_t *p_input = p_sys->p_input;
1365 input_item_t *p_item = input_GetItem( p_input );
1367 char *psz_title = NULL;
1368 char *psz_arturl = input_item_GetArtURL( p_item );
1370 vlc_mutex_lock( &p_item->lock );
1372 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
1373 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
1375 vlc_meta_Merge( p_item->p_meta, p_meta );
1377 if( !psz_arturl || *psz_arturl == '\0' )
1379 const char *psz_tmp = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
1381 psz_arturl = strdup( psz_tmp );
1383 vlc_mutex_unlock( &p_item->lock );
1385 if( psz_arturl && *psz_arturl )
1387 input_item_SetArtURL( p_item, psz_arturl );
1389 if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
1391 /* Don't look for art cover if sout
1392 * XXX It can change when sout has meta data support */
1393 if( p_input->p->p_sout && !p_input->b_preparsing )
1394 input_item_SetArtURL( p_item, "" );
1396 input_ExtractAttachmentAndCacheArt( p_input );
1403 input_item_SetName( p_item, psz_title );
1406 input_item_SetPreparsed( p_item, true );
1408 input_SendEventMeta( p_input );
1409 /* TODO handle sout meta ? */
1415 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1417 es_out_sys_t *p_sys = out->p_sys;
1418 input_thread_t *p_input = p_sys->p_input;
1420 if( fmt->i_group < 0 )
1422 msg_Err( p_input, "invalid group number" );
1426 es_out_id_t *es = malloc( sizeof( *es ) );
1427 es_out_pgrm_t *p_pgrm;
1433 vlc_mutex_lock( &p_sys->lock );
1435 /* Search the program */
1436 p_pgrm = EsOutProgramFind( out, fmt->i_group );
1439 vlc_mutex_unlock( &p_sys->lock );
1444 /* Increase ref count for program */
1448 es->p_pgrm = p_pgrm;
1449 es_format_Copy( &es->fmt, fmt );
1450 if( es->fmt.i_id < 0 )
1451 es->fmt.i_id = out->p_sys->i_id;
1452 if( !es->fmt.i_original_fourcc )
1453 es->fmt.i_original_fourcc = es->fmt.i_codec;
1454 es->fmt.i_codec = vlc_fourcc_GetCodec( es->fmt.i_cat, es->fmt.i_codec );
1456 es->i_id = es->fmt.i_id;
1457 es->i_meta_id = out->p_sys->i_id;
1458 es->b_scrambled = false;
1460 switch( es->fmt.i_cat )
1464 audio_replay_gain_t rg;
1466 es->i_channel = p_sys->i_audio;
1468 memset( &rg, 0, sizeof(rg) );
1469 vlc_mutex_lock( &p_input->p->p_item->lock );
1470 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1471 vlc_mutex_unlock( &p_input->p->p_item->lock );
1473 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1475 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1477 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1478 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1480 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1482 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1483 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1490 es->i_channel = p_sys->i_video;
1491 if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1492 vlc_ureduce( &es->fmt.video.i_frame_rate,
1493 &es->fmt.video.i_frame_rate_base,
1494 es->fmt.video.i_frame_rate,
1495 es->fmt.video.i_frame_rate_base, 0 );
1499 es->i_channel = p_sys->i_sub;
1506 es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1507 es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1509 es->p_dec_record = NULL;
1510 for( i = 0; i < 4; i++ )
1511 es->pb_cc_present[i] = false;
1512 es->p_master = NULL;
1514 if( es->p_pgrm == p_sys->p_pgrm )
1515 EsOutESVarUpdate( out, es, false );
1517 /* Select it if needed */
1518 EsOutSelect( out, es, false );
1521 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1522 p_sys->i_id++; /* always incremented */
1523 switch( es->fmt.i_cat )
1536 EsOutUpdateInfo( out, es, &es->fmt, NULL );
1538 if( es->b_scrambled )
1539 EsOutProgramUpdateScrambled( out, es->p_pgrm );
1541 vlc_mutex_unlock( &p_sys->lock );
1546 static bool EsIsSelected( es_out_id_t *es )
1550 bool b_decode = false;
1551 if( es->p_master->p_dec )
1553 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1554 if( i_channel != -1 )
1555 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1561 return es->p_dec != NULL;
1564 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1566 es_out_sys_t *p_sys = out->p_sys;
1567 input_thread_t *p_input = p_sys->p_input;
1569 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1572 if( p_sys->b_buffering )
1573 input_DecoderStartBuffering( p_es->p_dec );
1575 if( !p_es->p_master && p_sys->p_sout_record )
1577 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1578 if( p_es->p_dec_record && p_sys->b_buffering )
1579 input_DecoderStartBuffering( p_es->p_dec_record );
1583 EsOutDecoderChangeDelay( out, p_es );
1585 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1592 input_DecoderDelete( p_es->p_dec );
1595 if( p_es->p_dec_record )
1597 input_DecoderDelete( p_es->p_dec_record );
1598 p_es->p_dec_record = NULL;
1602 static void EsSelect( es_out_t *out, es_out_id_t *es )
1604 es_out_sys_t *p_sys = out->p_sys;
1605 input_thread_t *p_input = p_sys->p_input;
1607 if( EsIsSelected( es ) )
1609 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1616 if( !es->p_master->p_dec )
1619 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1620 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1625 const bool b_sout = p_input->p->p_sout != NULL;
1626 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1628 if( !var_GetBool( p_input, b_sout ? "sout-video" : "video" ) )
1630 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1635 else if( es->fmt.i_cat == AUDIO_ES )
1637 if( !var_GetBool( p_input, b_sout ? "sout-audio" : "audio" ) )
1639 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1644 if( es->fmt.i_cat == SPU_ES )
1646 if( !var_GetBool( p_input, b_sout ? "sout-spu" : "spu" ) )
1648 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1654 EsCreateDecoder( out, es );
1656 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1660 /* Mark it as selected */
1661 input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1662 input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1665 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1667 es_out_sys_t *p_sys = out->p_sys;
1668 input_thread_t *p_input = p_sys->p_input;
1670 if( !EsIsSelected( es ) )
1672 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1678 if( es->p_master->p_dec )
1680 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1681 if( i_channel != -1 )
1682 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1687 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1689 for( i = 0; i < 4; i++ )
1691 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1694 if( i_spu_id == es->pp_cc_es[i]->i_id )
1696 /* Force unselection of the CC */
1697 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1699 EsOutDel( out, es->pp_cc_es[i] );
1701 es->pb_cc_present[i] = false;
1703 EsDestroyDecoder( out, es );
1709 /* Mark it as unselected */
1710 input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1711 if( EsFmtIsTeletext( &es->fmt ) )
1712 input_SendEventTeletextSelect( p_input, -1 );
1716 * Select an ES given the current mode
1717 * XXX: you need to take a the lock before (stream.stream_lock)
1719 * \param out The es_out structure
1720 * \param es es_out_id structure
1721 * \param b_force ...
1724 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1726 es_out_sys_t *p_sys = out->p_sys;
1728 int i_cat = es->fmt.i_cat;
1730 if( !p_sys->b_active ||
1731 ( !b_force && es->fmt.i_priority < 0 ) )
1736 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1738 if( !EsIsSelected( es ) )
1739 EsSelect( out, es );
1741 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1745 var_Get( p_sys->p_input, "programs", &val );
1746 for ( i = 0; i < val.p_list->i_count; i++ )
1748 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1750 if( !EsIsSelected( es ) )
1751 EsSelect( out, es );
1755 var_FreeList( &val, NULL );
1757 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1761 if( es->p_pgrm != p_sys->p_pgrm )
1764 if( i_cat == AUDIO_ES )
1766 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1767 es->psz_language_code );
1769 if( p_sys->p_es_audio &&
1770 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1772 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1773 p_sys->p_es_audio->psz_language_code );
1775 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1777 i_wanted = es->i_channel;
1781 /* Select audio if (no audio selected yet)
1782 * - no audio-language
1783 * - no audio code for the ES
1784 * - audio code in the requested list */
1786 !strcmp( es->psz_language_code, "??" ) ||
1787 !p_sys->ppsz_audio_language )
1788 i_wanted = es->i_channel;
1791 if( p_sys->i_audio_last >= 0 )
1792 i_wanted = p_sys->i_audio_last;
1794 if( p_sys->i_audio_id >= 0 )
1796 if( es->i_id == p_sys->i_audio_id )
1797 i_wanted = es->i_channel;
1802 else if( i_cat == SPU_ES )
1804 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1805 es->psz_language_code );
1807 if( p_sys->p_es_sub &&
1808 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1810 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1811 p_sys->p_es_sub->psz_language_code );
1813 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1814 idx1, es->psz_language_code, idx2,
1815 p_sys->p_es_sub->psz_language_code );
1817 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1819 /* We found a SPU that matches our language request */
1820 i_wanted = es->i_channel;
1822 else if( idx1 >= 0 )
1824 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1825 idx1, es->psz_language_code );
1827 i_wanted = es->i_channel;
1829 else if( p_sys->i_default_sub_id >= 0 )
1831 if( es->i_id == p_sys->i_default_sub_id )
1832 i_wanted = es->i_channel;
1835 if( p_sys->i_sub_last >= 0 )
1836 i_wanted = p_sys->i_sub_last;
1838 if( p_sys->i_sub_id >= 0 )
1840 if( es->i_id == p_sys->i_sub_id )
1841 i_wanted = es->i_channel;
1846 else if( i_cat == VIDEO_ES )
1848 i_wanted = es->i_channel;
1851 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1852 EsSelect( out, es );
1855 /* FIXME TODO handle priority here */
1856 if( EsIsSelected( es ) )
1858 if( i_cat == AUDIO_ES )
1860 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1861 p_sys->p_es_audio &&
1862 p_sys->p_es_audio != es &&
1863 EsIsSelected( p_sys->p_es_audio ) )
1865 EsUnselect( out, p_sys->p_es_audio, false );
1867 p_sys->p_es_audio = es;
1869 else if( i_cat == SPU_ES )
1871 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1873 p_sys->p_es_sub != es &&
1874 EsIsSelected( p_sys->p_es_sub ) )
1876 EsUnselect( out, p_sys->p_es_sub, false );
1878 p_sys->p_es_sub = es;
1880 else if( i_cat == VIDEO_ES )
1882 p_sys->p_es_video = es;
1888 * Send a block for the given es_out
1890 * \param out the es_out to send from
1891 * \param es the es_out_id
1892 * \param p_block the data block to send
1894 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1896 es_out_sys_t *p_sys = out->p_sys;
1897 input_thread_t *p_input = p_sys->p_input;
1900 if( libvlc_stats( p_input ) )
1902 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1903 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1904 p_block->i_buffer, &i_total );
1905 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1906 (float)i_total, NULL );
1908 /* Update number of corrupted data packats */
1909 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
1911 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_corrupted,
1914 /* Update number of discontinuities */
1915 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1917 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_discontinuity,
1920 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1923 vlc_mutex_lock( &p_sys->lock );
1925 /* Mark preroll blocks */
1926 if( p_sys->i_preroll_end >= 0 )
1928 int64_t i_date = p_block->i_pts;
1929 if( p_block->i_pts <= VLC_TS_INVALID )
1930 i_date = p_block->i_dts;
1932 if( i_date < p_sys->i_preroll_end )
1933 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1936 p_block->i_rate = 0;
1940 block_Release( p_block );
1941 vlc_mutex_unlock( &p_sys->lock );
1945 /* Check for sout mode */
1946 if( p_input->p->p_sout )
1948 /* FIXME review this, proper lock may be missing */
1949 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 &&
1950 p_input->p->b_out_pace_control )
1952 msg_Dbg( p_input, "switching to sync mode" );
1953 p_input->p->b_out_pace_control = false;
1955 else if( p_input->p->p_sout->i_out_pace_nocontrol <= 0 &&
1956 !p_input->p->b_out_pace_control )
1958 msg_Dbg( p_input, "switching to async mode" );
1959 p_input->p->b_out_pace_control = true;
1964 if( es->p_dec_record )
1966 block_t *p_dup = block_Duplicate( p_block );
1968 input_DecoderDecode( es->p_dec_record, p_dup,
1969 p_input->p->b_out_pace_control );
1971 input_DecoderDecode( es->p_dec, p_block,
1972 p_input->p->b_out_pace_control );
1974 es_format_t fmt_dsc;
1975 vlc_meta_t *p_meta_dsc;
1976 if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
1978 EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
1980 es_format_Clean( &fmt_dsc );
1982 vlc_meta_Delete( p_meta_dsc );
1985 /* Check CC status */
1988 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1989 for( int i = 0; i < 4; i++ )
1993 if( es->pb_cc_present[i] || !pb_cc[i] )
1995 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1997 es_format_Init( &fmt, SPU_ES, EsOutFourccClosedCaptions[i] );
1998 fmt.i_group = es->fmt.i_group;
1999 if( asprintf( &fmt.psz_description,
2000 _("Closed captions %u"), 1 + i ) == -1 )
2001 fmt.psz_description = NULL;
2002 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
2003 es->pp_cc_es[i]->p_master = es;
2004 es_format_Clean( &fmt );
2007 es->pb_cc_present[i] = true;
2010 vlc_mutex_unlock( &p_sys->lock );
2015 /*****************************************************************************
2017 *****************************************************************************/
2018 static void EsOutDel( es_out_t *out, es_out_id_t *es )
2020 es_out_sys_t *p_sys = out->p_sys;
2021 bool b_reselect = false;
2024 vlc_mutex_lock( &p_sys->lock );
2026 /* We don't try to reselect */
2029 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
2031 if( input_DecoderIsEmpty( es->p_dec ) &&
2032 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
2034 /* FIXME there should be a way to have auto deleted es, but there will be
2035 * a problem when another codec of the same type is created (mainly video) */
2038 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2041 if( es->p_pgrm == p_sys->p_pgrm )
2042 EsOutESVarUpdate( out, es, true );
2044 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
2046 /* Update program */
2048 if( es->p_pgrm->i_es == 0 )
2049 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
2051 if( es->b_scrambled )
2052 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2055 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
2056 p_sys->p_es_sub == es ) b_reselect = true;
2058 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
2059 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
2060 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
2062 switch( es->fmt.i_cat )
2075 /* Re-select another track when needed */
2078 for( i = 0; i < p_sys->i_es; i++ )
2080 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
2081 EsOutSelect( out, p_sys->es[i], false );
2085 free( es->psz_language );
2086 free( es->psz_language_code );
2088 es_format_Clean( &es->fmt );
2090 vlc_mutex_unlock( &p_sys->lock );
2096 * Control query handler
2098 * \param out the es_out to control
2099 * \param i_query A es_out query as defined in include/ninput.h
2100 * \param args a variable list of arguments for the query
2101 * \return VLC_SUCCESS or an error code
2103 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2105 es_out_sys_t *p_sys = out->p_sys;
2109 case ES_OUT_SET_ES_STATE:
2111 es_out_id_t *es = va_arg( args, es_out_id_t * );
2112 bool b = va_arg( args, int );
2113 if( b && !EsIsSelected( es ) )
2115 EsSelect( out, es );
2116 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2118 else if( !b && EsIsSelected( es ) )
2120 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2126 case ES_OUT_GET_ES_STATE:
2128 es_out_id_t *es = va_arg( args, es_out_id_t * );
2129 bool *pb = va_arg( args, bool * );
2131 *pb = EsIsSelected( es );
2135 case ES_OUT_SET_MODE:
2137 const int i_mode = va_arg( args, int );
2138 assert( i_mode == ES_OUT_MODE_NONE || i_mode == ES_OUT_MODE_ALL ||
2139 i_mode == ES_OUT_MODE_AUTO || i_mode == ES_OUT_MODE_PARTIAL ||
2140 i_mode == ES_OUT_MODE_END );
2142 if( i_mode != ES_OUT_MODE_NONE && !p_sys->b_active && p_sys->i_es > 0 )
2144 /* XXX Terminate vout if there are tracks but no video one.
2145 * This one is not mandatory but is he earliest place where it
2148 for( i = 0; i < p_sys->i_es; i++ )
2150 es_out_id_t *p_es = p_sys->es[i];
2151 if( p_es->fmt.i_cat == VIDEO_ES )
2154 if( i >= p_sys->i_es )
2155 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2157 p_sys->b_active = i_mode != ES_OUT_MODE_NONE;
2158 p_sys->i_mode = i_mode;
2160 /* Reapply policy mode */
2161 for( int i = 0; i < p_sys->i_es; i++ )
2163 if( EsIsSelected( p_sys->es[i] ) )
2164 EsUnselect( out, p_sys->es[i],
2165 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2167 for( int i = 0; i < p_sys->i_es; i++ )
2168 EsOutSelect( out, p_sys->es[i], false );
2169 if( i_mode == ES_OUT_MODE_END )
2170 EsOutTerminate( out );
2175 case ES_OUT_RESTART_ES:
2177 es_out_id_t *es = va_arg( args, es_out_id_t * );
2182 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2184 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2186 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2191 for( int i = 0; i < p_sys->i_es; i++ )
2195 if( es == p_sys->es[i] )
2197 EsOutSelect( out, es, true );
2203 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2205 if( EsIsSelected( p_sys->es[i] ) )
2207 if( i_query == ES_OUT_RESTART_ES )
2209 if( p_sys->es[i]->p_dec )
2211 EsDestroyDecoder( out, p_sys->es[i] );
2212 EsCreateDecoder( out, p_sys->es[i] );
2217 EsUnselect( out, p_sys->es[i],
2218 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2227 case ES_OUT_SET_ES_DEFAULT:
2229 es_out_id_t *es = va_arg( args, es_out_id_t * );
2233 /*p_sys->i_default_video_id = -1;*/
2234 /*p_sys->i_default_audio_id = -1;*/
2235 p_sys->i_default_sub_id = -1;
2237 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2239 /*p_sys->i_default_video_id = -1;*/
2241 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2243 /*p_sys->i_default_audio_id = -1;*/
2245 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2247 p_sys->i_default_sub_id = -1;
2251 /*if( es->fmt.i_cat == VIDEO_ES )
2252 p_sys->i_default_video_id = es->i_id;
2254 if( es->fmt.i_cat == AUDIO_ES )
2255 p_sys->i_default_audio_id = es->i_id;
2257 if( es->fmt.i_cat == SPU_ES )
2258 p_sys->i_default_sub_id = es->i_id;
2263 case ES_OUT_SET_PCR:
2264 case ES_OUT_SET_GROUP_PCR:
2266 es_out_pgrm_t *p_pgrm = NULL;
2270 /* Search program */
2271 if( i_query == ES_OUT_SET_PCR )
2273 p_pgrm = p_sys->p_pgrm;
2275 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2279 i_group = (int)va_arg( args, int );
2280 p_pgrm = EsOutProgramFind( out, i_group );
2283 return VLC_EGENERIC;
2285 i_pcr = (int64_t)va_arg( args, int64_t );
2286 if( i_pcr <= VLC_TS_INVALID )
2288 msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
2289 return VLC_EGENERIC;
2292 /* TODO do not use mdate() but proper stream acquisition date */
2294 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2296 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering,
2297 EsOutIsExtraBufferingAllowed( out ),
2300 if( p_pgrm == p_sys->p_pgrm )
2302 if( p_sys->b_buffering )
2304 /* Check buffering state on master clock update */
2305 EsOutDecodersStopBuffering( out, false );
2307 else if( b_late && ( !p_sys->p_input->p->p_sout ||
2308 !p_sys->p_input->p->b_out_pace_control ) )
2310 mtime_t i_pts_delay = input_clock_GetJitter( p_pgrm->p_clock );
2312 /* Avoid dangerously high value */
2313 const mtime_t i_pts_delay_max = 30000000;
2314 if( i_pts_delay > i_pts_delay_max )
2315 i_pts_delay = __MAX( i_pts_delay_max, p_sys->i_pts_delay );
2317 /* Force a rebufferization when we are too late */
2318 msg_Err( p_sys->p_input,
2319 "ES_OUT_SET_(GROUP_)PCR is called too late, increasing pts_delay to %d ms",
2320 (int)(i_pts_delay/1000) );
2322 /* It is not really good, as we throw away already buffered data
2323 * TODO have a mean to correctly reenter bufferization */
2324 es_out_Control( out, ES_OUT_RESET_PCR );
2326 es_out_Control( out, ES_OUT_SET_JITTER, i_pts_delay, p_sys->i_cr_average );
2332 case ES_OUT_RESET_PCR:
2333 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2334 EsOutChangePosition( out );
2337 case ES_OUT_SET_GROUP:
2339 int i = va_arg( args, int );
2340 for( int j = 0; j < p_sys->i_pgrm; j++ )
2342 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2343 if( p_pgrm->i_id == i )
2345 EsOutProgramSelect( out, p_pgrm );
2349 return VLC_EGENERIC;
2352 case ES_OUT_SET_ES_FMT:
2354 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2355 * to update the p_extra data */
2356 es_out_id_t *es = va_arg( args, es_out_id_t * );
2357 es_format_t *p_fmt = va_arg( args, es_format_t * );
2359 return VLC_EGENERIC;
2361 if( p_fmt->i_extra )
2363 es->fmt.i_extra = p_fmt->i_extra;
2364 es->fmt.p_extra = xrealloc( es->fmt.p_extra, p_fmt->i_extra );
2365 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2370 EsDestroyDecoder( out, es );
2372 EsCreateDecoder( out, es );
2374 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2375 es->p_dec->fmt_in.p_extra =
2376 xrealloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2377 memcpy( es->p_dec->fmt_in.p_extra,
2378 p_fmt->p_extra, p_fmt->i_extra );
2385 case ES_OUT_SET_ES_SCRAMBLED_STATE:
2387 es_out_id_t *es = va_arg( args, es_out_id_t * );
2388 bool b_scrambled = (bool)va_arg( args, int );
2390 if( !es->b_scrambled != !b_scrambled )
2392 es->b_scrambled = b_scrambled;
2393 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2398 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2400 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2403 return VLC_EGENERIC;
2405 p_sys->i_preroll_end = i_date;
2409 case ES_OUT_SET_GROUP_META:
2411 int i_group = (int)va_arg( args, int );
2412 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2414 EsOutProgramMeta( out, i_group, p_meta );
2417 case ES_OUT_SET_GROUP_EPG:
2419 int i_group = (int)va_arg( args, int );
2420 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
2422 EsOutProgramEpg( out, i_group, p_epg );
2426 case ES_OUT_DEL_GROUP:
2428 int i_group = (int)va_arg( args, int );
2430 return EsOutProgramDel( out, i_group );
2433 case ES_OUT_SET_META:
2435 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2437 EsOutMeta( out, p_meta );
2441 case ES_OUT_GET_WAKE_UP:
2443 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2444 *pi_wakeup = EsOutGetWakeup( out );
2448 case ES_OUT_SET_ES_BY_ID:
2449 case ES_OUT_RESTART_ES_BY_ID:
2450 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2452 const int i_id = (int)va_arg( args, int );
2453 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2458 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2459 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2460 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2464 /* TODO if the lock is made non recursive it should be changed */
2465 int i_ret = es_out_Control( out, i_new_query, p_es );
2467 /* Clean up vout after user action (in active mode only).
2468 * FIXME it does not work well with multiple video windows */
2469 if( p_sys->b_active )
2470 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2474 case ES_OUT_GET_ES_OBJECTS_BY_ID:
2476 const int i_id = va_arg( args, int );
2477 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2479 return VLC_EGENERIC;
2481 vlc_object_t **pp_decoder = va_arg( args, vlc_object_t ** );
2482 vout_thread_t **pp_vout = va_arg( args, vout_thread_t ** );
2483 aout_instance_t **pp_aout = va_arg( args, aout_instance_t ** );
2487 *pp_decoder = vlc_object_hold( p_es->p_dec );
2488 input_DecoderGetObjects( p_es->p_dec, pp_vout, pp_aout );
2502 case ES_OUT_GET_BUFFERING:
2504 bool *pb = va_arg( args, bool* );
2505 *pb = p_sys->b_buffering;
2509 case ES_OUT_GET_EMPTY:
2511 bool *pb = va_arg( args, bool* );
2512 *pb = EsOutDecodersIsEmpty( out );
2516 case ES_OUT_SET_DELAY:
2518 const int i_cat = (int)va_arg( args, int );
2519 const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2520 EsOutSetDelay( out, i_cat, i_delay );
2524 case ES_OUT_SET_RECORD_STATE:
2526 bool b = va_arg( args, int );
2527 return EsOutSetRecord( out, b );
2530 case ES_OUT_SET_PAUSE_STATE:
2532 const bool b_source_paused = (bool)va_arg( args, int );
2533 const bool b_paused = (bool)va_arg( args, int );
2534 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2536 assert( !b_source_paused == !b_paused );
2537 EsOutChangePause( out, b_paused, i_date );
2542 case ES_OUT_SET_RATE:
2544 const int i_src_rate = (int)va_arg( args, int );
2545 const int i_rate = (int)va_arg( args, int );
2547 assert( i_src_rate == i_rate );
2548 EsOutChangeRate( out, i_rate );
2553 case ES_OUT_SET_TIME:
2555 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2557 assert( i_date == -1 );
2558 EsOutChangePosition( out );
2563 case ES_OUT_SET_FRAME_NEXT:
2564 EsOutFrameNext( out );
2567 case ES_OUT_SET_TIMES:
2569 double f_position = (double)va_arg( args, double );
2570 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2571 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2573 input_SendEventLength( p_sys->p_input, i_length );
2575 if( !p_sys->b_buffering )
2579 /* Fix for buffering delay */
2580 if( !p_sys->p_input->p->p_sout ||
2581 !p_sys->p_input->p->b_out_pace_control )
2582 i_delay = EsOutGetBuffering( out );
2591 f_position -= (double)i_delay / i_length;
2592 if( f_position < 0 )
2595 input_SendEventPosition( p_sys->p_input, f_position, i_time );
2599 case ES_OUT_SET_JITTER:
2601 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2602 int i_cr_average = (int)va_arg( args, int );
2604 if( i_pts_delay == p_sys->i_pts_delay &&
2605 i_cr_average == p_sys->i_cr_average )
2608 p_sys->i_pts_delay = i_pts_delay;
2609 p_sys->i_cr_average = i_cr_average;
2611 for( int i = 0; i < p_sys->i_pgrm; i++ )
2612 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2613 i_pts_delay, i_cr_average );
2617 case ES_OUT_GET_PCR_SYSTEM:
2619 if( p_sys->b_buffering )
2620 return VLC_EGENERIC;
2622 es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
2624 return VLC_EGENERIC;
2626 mtime_t *pi_system = va_arg( args, mtime_t *);
2627 mtime_t *pi_delay = va_arg( args, mtime_t *);
2628 input_clock_GetSystemOrigin( p_pgrm->p_clock, pi_system, pi_delay );
2632 case ES_OUT_MODIFY_PCR_SYSTEM:
2634 if( p_sys->b_buffering )
2635 return VLC_EGENERIC;
2637 es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
2639 return VLC_EGENERIC;
2641 const bool b_absolute = va_arg( args, int );
2642 const mtime_t i_system = va_arg( args, mtime_t );
2643 input_clock_ChangeSystemOrigin( p_pgrm->p_clock, b_absolute, i_system );
2648 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2649 return VLC_EGENERIC;
2652 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2654 es_out_sys_t *p_sys = out->p_sys;
2657 vlc_mutex_lock( &p_sys->lock );
2658 i_ret = EsOutControlLocked( out, i_query, args );
2659 vlc_mutex_unlock( &p_sys->lock );
2664 /****************************************************************************
2665 * LanguageGetName: try to expend iso639 into plain name
2666 ****************************************************************************/
2667 static char *LanguageGetName( const char *psz_code )
2669 const iso639_lang_t *pl;
2671 if( psz_code == NULL )
2673 return strdup( "" );
2676 if( strlen( psz_code ) == 2 )
2678 pl = GetLang_1( psz_code );
2680 else if( strlen( psz_code ) == 3 )
2682 pl = GetLang_2B( psz_code );
2683 if( !strcmp( pl->psz_iso639_1, "??" ) )
2685 pl = GetLang_2T( psz_code );
2690 return strdup( psz_code );
2693 if( !strcmp( pl->psz_iso639_1, "??" ) )
2695 return strdup( psz_code );
2699 if( *pl->psz_native_name )
2701 return strdup( pl->psz_native_name );
2703 return strdup( pl->psz_eng_name );
2707 /* Get a 2 char code */
2708 static char *LanguageGetCode( const char *psz_lang )
2710 const iso639_lang_t *pl;
2712 if( psz_lang == NULL || *psz_lang == '\0' )
2713 return strdup("??");
2715 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
2717 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2718 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2719 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2720 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2721 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2725 if( pl->psz_eng_name != NULL )
2726 return strdup( pl->psz_iso639_1 );
2728 return strdup("??");
2731 static char **LanguageSplit( const char *psz_langs )
2738 if( psz_langs == NULL ) return NULL;
2740 psz_parser = psz_dup = strdup(psz_langs);
2742 while( psz_parser && *psz_parser )
2747 psz = strchr(psz_parser, ',' );
2748 if( psz ) *psz++ = '\0';
2750 if( !strcmp( psz_parser, "any" ) )
2752 TAB_APPEND( i_psz, ppsz, strdup("any") );
2756 psz_code = LanguageGetCode( psz_parser );
2757 if( strcmp( psz_code, "??" ) )
2759 TAB_APPEND( i_psz, ppsz, psz_code );
2772 TAB_APPEND( i_psz, ppsz, NULL );
2779 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2783 if( !ppsz_langs || !psz_lang ) return -1;
2785 for( i = 0; ppsz_langs[i]; i++ )
2787 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2788 !strcasecmp( ppsz_langs[i], "any" ) )
2797 /****************************************************************************
2799 * - add meta info to the playlist item
2800 ****************************************************************************/
2801 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2803 es_out_sys_t *p_sys = out->p_sys;
2804 input_thread_t *p_input = p_sys->p_input;
2805 const es_format_t *p_fmt_es = &es->fmt;
2808 /* Create category */
2810 snprintf( psz_cat, sizeof(psz_cat),_("Stream %d"), es->i_meta_id );
2811 info_category_t *p_cat = info_category_New( psz_cat );
2815 /* Add informations */
2816 const char *psz_type;
2817 switch( fmt->i_cat )
2820 psz_type = _("Audio");
2823 psz_type = _("Video");
2826 psz_type = _("Subtitle");
2834 info_category_AddInfo( p_cat, _("Type"), "%s", psz_type );
2836 if( es->i_meta_id != es->i_id )
2837 info_category_AddInfo( p_cat, _("Original ID"),
2840 const char *psz_codec_description =
2841 vlc_fourcc_GetDescription( p_fmt_es->i_cat, p_fmt_es->i_codec );
2842 const vlc_fourcc_t i_codec_fourcc = p_fmt_es->i_original_fourcc ?: p_fmt_es->i_codec;
2843 if( psz_codec_description )
2844 info_category_AddInfo( p_cat, _("Codec"), "%s (%.4s)",
2845 psz_codec_description, (char*)&i_codec_fourcc );
2847 info_category_AddInfo( p_cat, _("Codec"), "%.4s",
2848 (char*)&i_codec_fourcc );
2850 if( es->psz_language && *es->psz_language )
2851 info_category_AddInfo( p_cat, _("Language"), "%s",
2853 if( fmt->psz_description && *fmt->psz_description )
2854 info_category_AddInfo( p_cat, _("Description"), "%s",
2855 fmt->psz_description );
2857 switch( fmt->i_cat )
2860 info_category_AddInfo( p_cat, _("Type"), _("Audio") );
2862 if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
2863 info_category_AddInfo( p_cat, _("Channels"), "%s",
2864 _( aout_FormatPrintChannels( &fmt->audio ) ) );
2865 else if( fmt->audio.i_channels > 0 )
2866 info_category_AddInfo( p_cat, _("Channels"), "%u",
2867 fmt->audio.i_channels );
2869 if( fmt->audio.i_rate > 0 )
2871 info_category_AddInfo( p_cat, _("Sample rate"), _("%u Hz"),
2872 fmt->audio.i_rate );
2873 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2874 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2877 unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2878 if( i_bitspersample <= 0 )
2879 i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2880 if( i_bitspersample > 0 )
2881 info_category_AddInfo( p_cat, _("Bits per sample"), "%u",
2884 if( fmt->i_bitrate > 0 )
2886 info_category_AddInfo( p_cat, _("Bitrate"), _("%u kb/s"),
2887 fmt->i_bitrate / 1000 );
2888 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2889 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2891 for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2893 const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2894 if( !p_rg->pb_gain[i] )
2896 const char *psz_name;
2897 if( i == AUDIO_REPLAY_GAIN_TRACK )
2898 psz_name = _("Track replay gain");
2900 psz_name = _("Album replay gain");
2901 info_category_AddInfo( p_cat, psz_name, _("%.2f dB"),
2907 info_category_AddInfo( p_cat, _("Type"), _("Video") );
2909 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2910 info_category_AddInfo( p_cat, _("Resolution"), "%ux%u",
2911 fmt->video.i_width, fmt->video.i_height );
2913 if( fmt->video.i_visible_width > 0 &&
2914 fmt->video.i_visible_height > 0 )
2915 info_category_AddInfo( p_cat, _("Display resolution"), "%ux%u",
2916 fmt->video.i_visible_width,
2917 fmt->video.i_visible_height);
2918 if( fmt->video.i_frame_rate > 0 &&
2919 fmt->video.i_frame_rate_base > 0 )
2921 div = lldiv( (float)fmt->video.i_frame_rate /
2922 fmt->video.i_frame_rate_base * 1000000,
2925 info_category_AddInfo( p_cat, _("Frame rate"), "%"PRId64".%06u",
2926 div.quot, (unsigned int )div.rem );
2928 info_category_AddInfo( p_cat, _("Frame rate"), "%"PRId64,
2934 info_category_AddInfo( p_cat, _("Type"), _("Subtitle") );
2941 /* Append generic meta */
2944 char **ppsz_all_keys = vlc_meta_CopyExtraNames( p_meta );
2945 for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
2947 char *psz_key = ppsz_all_keys[i];
2948 const char *psz_value = vlc_meta_GetExtra( p_meta, psz_key );
2951 info_category_AddInfo( p_cat, vlc_gettext(psz_key), "%s",
2952 vlc_gettext(psz_value) );
2955 free( ppsz_all_keys );
2958 input_Control( p_input, INPUT_REPLACE_INFOS, p_cat );