1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_input.h>
37 #include <vlc_es_out.h>
38 #include <vlc_block.h>
40 #include <vlc_fourcc.h>
42 #include "input_internal.h"
48 #include "../stream_output/stream_output.h"
50 #include <vlc_iso_lang.h>
51 /* FIXME we should find a better way than including that */
52 #include "../text/iso-639_def.h"
54 /*****************************************************************************
56 *****************************************************************************/
62 /* Number of es for this pgrm */
68 /* Clock for this program */
69 input_clock_t *p_clock;
72 char *psz_now_playing;
80 es_out_pgrm_t *p_pgrm;
85 /* Channel in the track type */
89 char *psz_language_code;
92 decoder_t *p_dec_record;
94 /* Fields for Video with CC */
95 bool pb_cc_present[4];
96 es_out_id_t *pp_cc_es[4];
98 /* Field for CC track from a master video */
99 es_out_id_t *p_master;
101 /* ID for the meta data */
107 input_thread_t *p_input;
114 es_out_pgrm_t **pgrm;
115 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
116 es_out_pgrm_t *p_pgrm; /* Master program */
133 int i_audio_last, i_audio_id;
134 int i_sub_last, i_sub_id;
135 int i_default_sub_id; /* As specified in container; if applicable */
136 char **ppsz_audio_language;
137 char **ppsz_sub_language;
139 /* current main es */
140 es_out_id_t *p_es_audio;
141 es_out_id_t *p_es_video;
142 es_out_id_t *p_es_sub;
145 int64_t i_audio_delay;
148 /* Clock configuration */
155 mtime_t i_pause_date;
157 /* Current preroll */
158 mtime_t i_preroll_end;
160 /* Used for buffering */
162 mtime_t i_buffering_extra_initial;
163 mtime_t i_buffering_extra_stream;
164 mtime_t i_buffering_extra_system;
167 sout_instance_t *p_sout_record;
170 static es_out_id_t *EsOutAdd ( es_out_t *, const es_format_t * );
171 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
172 static void EsOutDel ( es_out_t *, es_out_id_t * );
173 static int EsOutControl( es_out_t *, int i_query, va_list );
174 static void EsOutDelete ( es_out_t * );
176 static void EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
177 static void EsOutUpdateInfo( es_out_t *, es_out_id_t *es, const es_format_t *, const vlc_meta_t * );
178 static int EsOutSetRecord( es_out_t *, bool b_record );
180 static bool EsIsSelected( es_out_id_t *es );
181 static void EsSelect( es_out_t *out, es_out_id_t *es );
182 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
183 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
184 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
185 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
186 static void EsOutProgramsChangeRate( es_out_t *out );
187 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
189 static char *LanguageGetName( const char *psz_code );
190 static char *LanguageGetCode( const char *psz_lang );
191 static char **LanguageSplit( const char *psz_langs );
192 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
194 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
196 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
197 VLC_FOURCC('c', 'c', '1', ' '),
198 VLC_FOURCC('c', 'c', '2', ' '),
199 VLC_FOURCC('c', 'c', '3', ' '),
200 VLC_FOURCC('c', 'c', '4', ' '),
202 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
205 for( i = 0; i < 4; i++ )
207 if( fcc == EsOutFourccClosedCaptions[i] )
212 static inline bool EsFmtIsTeletext( const es_format_t *p_fmt )
214 return p_fmt->i_cat == SPU_ES && p_fmt->i_codec == VLC_CODEC_TELETEXT;
217 /*****************************************************************************
219 *****************************************************************************/
220 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
222 es_out_t *out = malloc( sizeof( *out ) );
226 es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
233 out->pf_add = EsOutAdd;
234 out->pf_send = EsOutSend;
235 out->pf_del = EsOutDel;
236 out->pf_control = EsOutControl;
237 out->pf_destroy = EsOutDelete;
239 out->b_sout = p_input->p->p_sout != NULL;
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_AUTO;
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;
334 if( p_sys->p_sout_record )
335 EsOutSetRecord( out, false );
337 for( i = 0; i < p_sys->i_es; i++ )
339 if( p_sys->es[i]->p_dec )
340 input_DecoderDelete( p_sys->es[i]->p_dec );
342 free( p_sys->es[i]->psz_language );
343 free( p_sys->es[i]->psz_language_code );
344 es_format_Clean( &p_sys->es[i]->fmt );
346 free( p_sys->es[i] );
348 if( p_sys->ppsz_audio_language )
350 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
351 free( p_sys->ppsz_audio_language[i] );
352 free( p_sys->ppsz_audio_language );
354 if( p_sys->ppsz_sub_language )
356 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
357 free( p_sys->ppsz_sub_language[i] );
358 free( p_sys->ppsz_sub_language );
362 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
363 for( i = 0; i < p_sys->i_pgrm; i++ )
365 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
366 input_clock_Delete( p_pgrm->p_clock );
367 free( p_pgrm->psz_now_playing );
368 free( p_pgrm->psz_publisher );
369 free( p_pgrm->psz_name );
373 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
375 input_item_SetEpgOffline( p_sys->p_input->p->p_item );
377 vlc_mutex_destroy( &p_sys->lock );
383 static mtime_t EsOutGetWakeup( es_out_t *out )
385 es_out_sys_t *p_sys = out->p_sys;
386 input_thread_t *p_input = p_sys->p_input;
391 /* We do not have a wake up date if the input cannot have its speed
392 * controlled or sout is imposing its own or while buffering
394 * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
395 if( !p_input->p->b_can_pace_control ||
396 p_input->p->b_out_pace_control ||
400 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
403 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
408 /* Special HACK, -i_id is the cat of the stream */
409 return (es_out_id_t*)((uint8_t*)NULL-i_id);
412 for( i = 0; i < out->p_sys->i_es; i++ )
414 if( out->p_sys->es[i]->i_id == i_id )
415 return out->p_sys->es[i];
420 static bool EsOutDecodersIsEmpty( es_out_t *out )
422 es_out_sys_t *p_sys = out->p_sys;
425 if( p_sys->b_buffering && p_sys->p_pgrm )
427 EsOutDecodersStopBuffering( out, true );
428 if( p_sys->b_buffering )
432 for( i = 0; i < p_sys->i_es; i++ )
434 es_out_id_t *es = p_sys->es[i];
436 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
438 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
444 static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
446 es_out_sys_t *p_sys = out->p_sys;
448 if( i_cat == AUDIO_ES )
449 p_sys->i_audio_delay = i_delay;
450 else if( i_cat == SPU_ES )
451 p_sys->i_spu_delay = i_delay;
453 for( int i = 0; i < p_sys->i_es; i++ )
454 EsOutDecoderChangeDelay( out, p_sys->es[i] );
457 static int EsOutSetRecord( es_out_t *out, bool b_record )
459 es_out_sys_t *p_sys = out->p_sys;
460 input_thread_t *p_input = p_sys->p_input;
462 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
466 char *psz_path = var_CreateGetNonEmptyString( p_input, "input-record-path" );
469 if( var_CountChoices( p_input, "video-es" ) )
470 psz_path = config_GetUserDir( VLC_VIDEOS_DIR );
471 else if( var_CountChoices( p_input, "audio-es" ) )
472 psz_path = config_GetUserDir( VLC_MUSIC_DIR );
474 psz_path = config_GetUserDir( VLC_DOWNLOAD_DIR );
477 char *psz_sout = NULL; // TODO conf
479 if( !psz_sout && psz_path )
481 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
484 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
495 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
499 if( !p_sys->p_sout_record )
502 for( int i = 0; i < p_sys->i_es; i++ )
504 es_out_id_t *p_es = p_sys->es[i];
506 if( !p_es->p_dec || p_es->p_master )
509 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
510 if( p_es->p_dec_record && p_sys->b_buffering )
511 input_DecoderStartBuffering( p_es->p_dec_record );
516 for( int i = 0; i < p_sys->i_es; i++ )
518 es_out_id_t *p_es = p_sys->es[i];
520 if( !p_es->p_dec_record )
523 input_DecoderDelete( p_es->p_dec_record );
524 p_es->p_dec_record = NULL;
527 sout_DeleteInstance( p_sys->p_sout_record );
529 p_sys->p_sout_record = NULL;
534 static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
536 es_out_sys_t *p_sys = out->p_sys;
538 /* XXX the order is important */
541 EsOutDecodersChangePause( out, true, i_date );
542 EsOutProgramChangePause( out, true, i_date );
546 if( p_sys->i_buffering_extra_initial > 0 )
548 mtime_t i_stream_start;
549 mtime_t i_system_start;
550 mtime_t i_stream_duration;
551 mtime_t i_system_duration;
553 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
554 &i_stream_start, &i_system_start,
555 &i_stream_duration, &i_system_duration );
558 /* FIXME pcr != exactly what wanted */
559 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;
562 p_sys->i_buffering_extra_initial = 0;
563 p_sys->i_buffering_extra_stream = 0;
564 p_sys->i_buffering_extra_system = 0;
566 EsOutProgramChangePause( out, false, i_date );
567 EsOutDecodersChangePause( out, false, i_date );
569 EsOutProgramsChangeRate( out );
571 p_sys->b_paused = b_paused;
572 p_sys->i_pause_date = i_date;
575 static void EsOutChangeRate( es_out_t *out, int i_rate )
577 es_out_sys_t *p_sys = out->p_sys;
579 p_sys->i_rate = i_rate;
580 EsOutProgramsChangeRate( out );
583 static void EsOutChangePosition( es_out_t *out )
585 es_out_sys_t *p_sys = out->p_sys;
587 input_SendEventCache( p_sys->p_input, 0.0 );
589 for( int i = 0; i < p_sys->i_es; i++ )
591 es_out_id_t *p_es = p_sys->es[i];
596 input_DecoderStartBuffering( p_es->p_dec );
598 if( p_es->p_dec_record )
599 input_DecoderStartBuffering( p_es->p_dec_record );
602 for( int i = 0; i < p_sys->i_pgrm; i++ )
603 input_clock_Reset( p_sys->pgrm[i]->p_clock );
605 p_sys->b_buffering = true;
606 p_sys->i_buffering_extra_initial = 0;
607 p_sys->i_buffering_extra_stream = 0;
608 p_sys->i_buffering_extra_system = 0;
609 p_sys->i_preroll_end = -1;
614 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
616 es_out_sys_t *p_sys = out->p_sys;
619 mtime_t i_stream_start;
620 mtime_t i_system_start;
621 mtime_t i_stream_duration;
622 mtime_t i_system_duration;
623 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
624 &i_stream_start, &i_system_start,
625 &i_stream_duration, &i_system_duration );
626 assert( !i_ret || b_forced );
630 mtime_t i_preroll_duration = 0;
631 if( p_sys->i_preroll_end >= 0 )
632 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
634 const mtime_t i_buffering_duration = p_sys->i_pts_delay +
636 p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
638 if( i_stream_duration <= i_buffering_duration && !b_forced )
640 const double f_level = (double)i_stream_duration / i_buffering_duration;
641 input_SendEventCache( p_sys->p_input, f_level );
643 msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
646 input_SendEventCache( p_sys->p_input, 1.0 );
648 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
649 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
650 p_sys->b_buffering = false;
651 p_sys->i_preroll_end = -1;
653 if( p_sys->i_buffering_extra_initial > 0 )
659 const mtime_t i_decoder_buffering_start = mdate();
660 for( int i = 0; i < p_sys->i_es; i++ )
662 es_out_id_t *p_es = p_sys->es[i];
664 if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
666 input_DecoderWaitBuffering( p_es->p_dec );
667 if( p_es->p_dec_record )
668 input_DecoderWaitBuffering( p_es->p_dec_record );
671 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
672 (int)(mdate() - i_decoder_buffering_start)/1000 );
674 /* Here is a good place to destroy unused vout with every demuxer */
675 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
678 const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
679 const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
681 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_current_date + i_wakeup_delay - i_buffering_duration );
683 for( int i = 0; i < p_sys->i_es; i++ )
685 es_out_id_t *p_es = p_sys->es[i];
690 input_DecoderStopBuffering( p_es->p_dec );
691 if( p_es->p_dec_record )
692 input_DecoderStopBuffering( p_es->p_dec_record );
695 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
697 es_out_sys_t *p_sys = out->p_sys;
699 /* Pause decoders first */
700 for( int i = 0; i < p_sys->i_es; i++ )
702 es_out_id_t *es = p_sys->es[i];
706 input_DecoderChangePause( es->p_dec, b_paused, i_date );
707 if( es->p_dec_record )
708 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
713 static bool EsOutIsExtraBufferingAllowed( es_out_t *out )
715 es_out_sys_t *p_sys = out->p_sys;
718 for( int i = 0; i < p_sys->i_es; i++ )
720 es_out_id_t *p_es = p_sys->es[i];
723 i_size += input_DecoderGetFifoSize( p_es->p_dec );
724 if( p_es->p_dec_record )
725 i_size += input_DecoderGetFifoSize( p_es->p_dec_record );
727 //fprintf( stderr, "----- EsOutIsExtraBufferingAllowed =% 5d kbytes -- ", i_size / 1024 );
729 /* TODO maybe we want to be able to tune it ? */
730 #if defined(OPTIMIZE_MEMORY)
731 const size_t i_level_high = 500000; /* 0.5 Mbytes */
733 const size_t i_level_high = 10000000; /* 10 Mbytes */
735 return i_size < i_level_high;
738 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
740 es_out_sys_t *p_sys = out->p_sys;
742 for( int i = 0; i < p_sys->i_pgrm; i++ )
743 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
746 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
748 es_out_sys_t *p_sys = out->p_sys;
751 if( p_es->fmt.i_cat == AUDIO_ES )
752 i_delay = p_sys->i_audio_delay;
753 else if( p_es->fmt.i_cat == SPU_ES )
754 i_delay = p_sys->i_spu_delay;
759 input_DecoderChangeDelay( p_es->p_dec, i_delay );
760 if( p_es->p_dec_record )
761 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
764 static void EsOutProgramsChangeRate( es_out_t *out )
766 es_out_sys_t *p_sys = out->p_sys;
768 for( int i = 0; i < p_sys->i_pgrm; i++ )
769 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
772 static void EsOutFrameNext( es_out_t *out )
774 es_out_sys_t *p_sys = out->p_sys;
775 es_out_id_t *p_es_video = NULL;
777 if( p_sys->b_buffering )
779 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
783 assert( p_sys->b_paused );
785 for( int i = 0; i < p_sys->i_es; i++ )
787 es_out_id_t *p_es = p_sys->es[i];
789 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
798 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
803 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
805 msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
807 if( i_duration <= 0 )
808 i_duration = 40*1000;
810 /* FIXME it is not a clean way ? */
811 if( p_sys->i_buffering_extra_initial <= 0 )
813 mtime_t i_stream_start;
814 mtime_t i_system_start;
815 mtime_t i_stream_duration;
816 mtime_t i_system_duration;
819 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
820 &i_stream_start, &i_system_start,
821 &i_stream_duration, &i_system_duration );
825 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
826 p_sys->i_buffering_extra_system =
827 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
830 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
832 p_sys->b_buffering = true;
833 p_sys->i_buffering_extra_system += i_duration;
834 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
835 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
836 INPUT_RATE_DEFAULT / i_rate;
838 p_sys->i_preroll_end = -1;
840 static mtime_t EsOutGetBuffering( es_out_t *out )
842 es_out_sys_t *p_sys = out->p_sys;
848 mtime_t i_stream_start;
849 mtime_t i_system_start;
850 mtime_t i_stream_duration;
851 mtime_t i_system_duration;
852 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
853 &i_stream_start, &i_system_start,
854 &i_stream_duration, &i_system_duration );
861 if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
863 i_delay = i_stream_duration;
867 mtime_t i_system_duration;
868 if( p_sys->b_paused )
870 i_system_duration = p_sys->i_pause_date - i_system_start;
871 if( p_sys->i_buffering_extra_initial > 0 )
872 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
876 i_system_duration = mdate() - i_system_start;
879 const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
880 i_delay = p_sys->i_pts_delay - i_consumed;
887 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
888 const es_format_t *fmt, const char *psz_language,
891 es_out_sys_t *p_sys = out->p_sys;
892 input_thread_t *p_input = p_sys->p_input;
893 vlc_value_t val, text;
897 if( EsFmtIsTeletext( fmt ) )
898 input_SendEventTeletextDel( p_sys->p_input, i_id );
900 input_SendEventEsDel( p_input, fmt->i_cat, i_id );
904 /* Get the number of ES already added */
906 if( fmt->i_cat == AUDIO_ES )
907 psz_var = "audio-es";
908 else if( fmt->i_cat == VIDEO_ES )
909 psz_var = "video-es";
913 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
918 /* First one, we need to add the "Disable" choice */
919 val2.i_int = -1; text.psz_string = _("Disable");
920 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
924 /* Take care of the ES description */
925 if( fmt->psz_description && *fmt->psz_description )
927 if( psz_language && *psz_language )
929 text.psz_string = malloc( strlen( fmt->psz_description) +
930 strlen( psz_language ) + 10 );
931 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
934 else text.psz_string = strdup( fmt->psz_description );
938 if( psz_language && *psz_language )
940 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
941 text.psz_string = NULL;
945 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
946 text.psz_string = NULL;
950 input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
951 if( EsFmtIsTeletext( fmt ) )
954 snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
955 fmt->subs.teletext.i_magazine,
956 fmt->subs.teletext.i_page );
957 input_SendEventTeletextAdd( p_sys->p_input,
958 i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
961 free( text.psz_string );
964 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
967 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
970 /* EsOutProgramSelect:
971 * Select a program and update the object variable
973 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
975 es_out_sys_t *p_sys = out->p_sys;
976 input_thread_t *p_input = p_sys->p_input;
979 if( p_sys->p_pgrm == p_pgrm )
980 return; /* Nothing to do */
984 es_out_pgrm_t *old = p_sys->p_pgrm;
985 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
987 for( i = 0; i < p_sys->i_es; i++ )
989 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
990 p_sys->i_mode != ES_OUT_MODE_ALL )
991 EsUnselect( out, p_sys->es[i], true );
994 p_sys->p_es_audio = NULL;
995 p_sys->p_es_sub = NULL;
996 p_sys->p_es_video = NULL;
999 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
1001 /* Mark it selected */
1002 p_pgrm->b_selected = true;
1004 /* Switch master stream */
1005 p_sys->p_pgrm = p_pgrm;
1007 /* Update "program" */
1008 input_SendEventProgramSelect( p_input, p_pgrm->i_id );
1011 input_SendEventEsDel( p_input, AUDIO_ES, -1 );
1012 input_SendEventEsDel( p_input, VIDEO_ES, -1 );
1013 input_SendEventEsDel( p_input, SPU_ES, -1 );
1014 input_SendEventTeletextDel( p_input, -1 );
1015 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
1018 var_SetInteger( p_input, "teletext-es", -1 );
1020 for( i = 0; i < p_sys->i_es; i++ )
1022 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
1023 EsOutESVarUpdate( out, p_sys->es[i], false );
1024 EsOutSelect( out, p_sys->es[i], false );
1027 /* Update now playing */
1028 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1029 input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
1031 input_SendEventMeta( p_input );
1037 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
1039 es_out_sys_t *p_sys = out->p_sys;
1040 input_thread_t *p_input = p_sys->p_input;
1042 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1047 p_pgrm->i_id = i_group;
1049 p_pgrm->b_selected = false;
1050 p_pgrm->b_scrambled = false;
1051 p_pgrm->psz_name = NULL;
1052 p_pgrm->psz_now_playing = NULL;
1053 p_pgrm->psz_publisher = NULL;
1054 p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1055 if( !p_pgrm->p_clock )
1060 if( p_sys->b_paused )
1061 input_clock_ChangePause( p_pgrm->p_clock, p_sys->b_paused, p_sys->i_pause_date );
1062 input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1065 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1067 /* Update "program" variable */
1068 input_SendEventProgramAdd( p_input, i_group, NULL );
1070 if( i_group == var_GetInteger( p_input, "program" ) )
1071 EsOutProgramSelect( out, p_pgrm );
1079 static int EsOutProgramDel( es_out_t *out, int i_group )
1081 es_out_sys_t *p_sys = out->p_sys;
1082 input_thread_t *p_input = p_sys->p_input;
1083 es_out_pgrm_t *p_pgrm = NULL;
1086 for( i = 0; i < p_sys->i_pgrm; i++ )
1088 if( p_sys->pgrm[i]->i_id == i_group )
1090 p_pgrm = p_sys->pgrm[i];
1095 if( p_pgrm == NULL )
1096 return VLC_EGENERIC;
1100 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1101 i_group, p_pgrm->i_es );
1102 return VLC_EGENERIC;
1105 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1107 /* If program is selected we need to unselect it */
1108 if( p_sys->p_pgrm == p_pgrm )
1109 p_sys->p_pgrm = NULL;
1111 input_clock_Delete( p_pgrm->p_clock );
1113 free( p_pgrm->psz_name );
1114 free( p_pgrm->psz_now_playing );
1115 free( p_pgrm->psz_publisher );
1118 /* Update "program" variable */
1119 input_SendEventProgramDel( p_input, i_group );
1126 static es_out_pgrm_t *EsOutProgramFind( es_out_t *p_out, int i_group )
1128 es_out_sys_t *p_sys = p_out->p_sys;
1130 for( int i = 0; i < p_sys->i_pgrm; i++ )
1132 if( p_sys->pgrm[i]->i_id == i_group )
1133 return p_sys->pgrm[i];
1135 return EsOutProgramAdd( p_out, i_group );
1138 /* EsOutProgramMeta:
1140 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1143 if( p_pgrm->psz_name )
1145 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1150 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1156 static void EsOutProgramMeta( es_out_t *out, int i_group, const vlc_meta_t *p_meta )
1158 es_out_sys_t *p_sys = out->p_sys;
1159 es_out_pgrm_t *p_pgrm;
1160 input_thread_t *p_input = p_sys->p_input;
1162 const char *psz_title = NULL;
1163 const char *psz_provider = NULL;
1166 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1168 /* Check against empty meta data (empty for what we handle) */
1169 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1170 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1171 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1172 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1177 p_pgrm = EsOutProgramFind( out, i_group );
1182 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1183 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1185 /* Update the description text of the program */
1186 if( psz_title && *psz_title )
1188 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1190 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1192 /* Remove old entries */
1193 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1194 /* TODO update epg name ?
1195 * TODO update scrambled info name ? */
1198 free( p_pgrm->psz_name );
1199 p_pgrm->psz_name = strdup( psz_title );
1202 if( psz_provider && *psz_provider )
1204 if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1209 psz_text = strdup( psz_title );
1212 /* ugly but it works */
1215 input_SendEventProgramDel( p_input, i_group );
1216 input_SendEventProgramAdd( p_input, i_group, psz_text );
1222 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1225 if( p_sys->p_pgrm == p_pgrm )
1227 input_item_SetPublisher( p_input->p->p_item, psz_provider );
1228 input_SendEventMeta( p_input );
1230 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1232 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1233 for( i = 0; ppsz_all_keys[i]; i++ )
1235 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1236 vlc_gettext(ppsz_all_keys[i]),
1237 vlc_dictionary_value_for_key( &p_meta->extra_tags,
1238 ppsz_all_keys[i] ) );
1239 free( ppsz_all_keys[i] );
1241 free( ppsz_all_keys );
1246 static void EsOutProgramEpg( es_out_t *out, int i_group, const vlc_epg_t *p_epg )
1248 es_out_sys_t *p_sys = out->p_sys;
1249 input_thread_t *p_input = p_sys->p_input;
1250 input_item_t *p_item = p_input->p->p_item;
1251 es_out_pgrm_t *p_pgrm;
1255 p_pgrm = EsOutProgramFind( out, i_group );
1260 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1261 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, psz_cat );
1267 epg.psz_name = psz_cat;
1269 input_item_SetEpg( p_item, &epg );
1271 /* Update now playing */
1272 free( p_pgrm->psz_now_playing );
1273 p_pgrm->psz_now_playing = NULL;
1275 vlc_mutex_lock( &p_item->lock );
1276 for( int i = 0; i < p_item->i_epg; i++ )
1278 const vlc_epg_t *p_tmp = p_item->pp_epg[i];
1280 if( p_tmp->psz_name && !strcmp(p_tmp->psz_name, psz_cat) )
1282 if( p_tmp->p_current && p_tmp->p_current->psz_name && *p_tmp->p_current->psz_name )
1283 p_pgrm->psz_now_playing = strdup( p_tmp->p_current->psz_name );
1287 vlc_mutex_unlock( &p_item->lock );
1289 if( p_pgrm == p_sys->p_pgrm )
1291 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1292 input_SendEventMeta( p_input );
1295 if( p_pgrm->psz_now_playing )
1297 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1298 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1299 p_pgrm->psz_now_playing );
1303 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1304 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1310 static void EsOutProgramUpdateScrambled( es_out_t *p_out, es_out_pgrm_t *p_pgrm )
1312 es_out_sys_t *p_sys = p_out->p_sys;
1313 input_thread_t *p_input = p_sys->p_input;
1314 bool b_scrambled = false;
1316 for( int i = 0; i < p_sys->i_es; i++ )
1318 if( p_sys->es[i]->p_pgrm == p_pgrm && p_sys->es[i]->b_scrambled )
1324 if( !p_pgrm->b_scrambled == !b_scrambled )
1327 p_pgrm->b_scrambled = b_scrambled;
1328 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1331 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Scrambled"), _("Yes") );
1333 input_Control( p_input, INPUT_DEL_INFO, psz_cat, _("Scrambled") );
1335 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, b_scrambled );
1338 static void EsOutMeta( es_out_t *p_out, const vlc_meta_t *p_meta )
1340 es_out_sys_t *p_sys = p_out->p_sys;
1341 input_thread_t *p_input = p_sys->p_input;
1343 input_item_t *p_item = input_GetItem( p_input );
1345 char *psz_title = NULL;
1346 char *psz_arturl = input_item_GetArtURL( p_item );
1348 vlc_mutex_lock( &p_item->lock );
1350 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
1351 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
1353 vlc_meta_Merge( p_item->p_meta, p_meta );
1355 if( !psz_arturl || *psz_arturl == '\0' )
1357 const char *psz_tmp = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
1359 psz_arturl = strdup( psz_tmp );
1361 vlc_mutex_unlock( &p_item->lock );
1363 if( psz_arturl && *psz_arturl )
1365 input_item_SetArtURL( p_item, psz_arturl );
1367 if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
1369 /* Don't look for art cover if sout
1370 * XXX It can change when sout has meta data support */
1371 if( p_out->b_sout && !p_input->b_preparsing )
1372 input_item_SetArtURL( p_item, "" );
1374 input_ExtractAttachmentAndCacheArt( p_input );
1381 input_item_SetName( p_item, psz_title );
1384 input_item_SetPreparsed( p_item, true );
1386 input_SendEventMeta( p_input );
1387 /* TODO handle sout meta ? */
1393 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1395 es_out_sys_t *p_sys = out->p_sys;
1396 input_thread_t *p_input = p_sys->p_input;
1398 if( fmt->i_group < 0 )
1400 msg_Err( p_input, "invalid group number" );
1404 es_out_id_t *es = malloc( sizeof( *es ) );
1405 es_out_pgrm_t *p_pgrm;
1411 vlc_mutex_lock( &p_sys->lock );
1413 /* Search the program */
1414 p_pgrm = EsOutProgramFind( out, fmt->i_group );
1417 vlc_mutex_unlock( &p_sys->lock );
1422 /* Increase ref count for program */
1426 es->p_pgrm = p_pgrm;
1427 es_format_Copy( &es->fmt, fmt );
1428 if( es->fmt.i_id < 0 )
1429 es->fmt.i_id = out->p_sys->i_id;
1430 if( !es->fmt.i_original_fourcc )
1431 es->fmt.i_original_fourcc = es->fmt.i_codec;
1432 es->fmt.i_codec = vlc_fourcc_GetCodec( es->fmt.i_cat, es->fmt.i_codec );
1434 es->i_id = es->fmt.i_id;
1435 es->i_meta_id = out->p_sys->i_id;
1436 es->b_scrambled = false;
1438 switch( es->fmt.i_cat )
1442 audio_replay_gain_t rg;
1444 es->i_channel = p_sys->i_audio;
1446 memset( &rg, 0, sizeof(rg) );
1447 vlc_mutex_lock( &p_input->p->p_item->lock );
1448 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1449 vlc_mutex_unlock( &p_input->p->p_item->lock );
1451 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1453 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1455 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1456 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1458 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1460 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1461 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1468 es->i_channel = p_sys->i_video;
1469 if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1470 vlc_ureduce( &es->fmt.video.i_frame_rate,
1471 &es->fmt.video.i_frame_rate_base,
1472 es->fmt.video.i_frame_rate,
1473 es->fmt.video.i_frame_rate_base, 0 );
1477 es->i_channel = p_sys->i_sub;
1484 es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1485 es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1487 es->p_dec_record = NULL;
1488 for( i = 0; i < 4; i++ )
1489 es->pb_cc_present[i] = false;
1490 es->p_master = NULL;
1492 if( es->p_pgrm == p_sys->p_pgrm )
1493 EsOutESVarUpdate( out, es, false );
1495 /* Select it if needed */
1496 EsOutSelect( out, es, false );
1499 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1500 p_sys->i_id++; /* always incremented */
1501 switch( es->fmt.i_cat )
1514 EsOutUpdateInfo( out, es, &es->fmt, NULL );
1516 if( es->b_scrambled )
1517 EsOutProgramUpdateScrambled( out, es->p_pgrm );
1519 vlc_mutex_unlock( &p_sys->lock );
1524 static bool EsIsSelected( es_out_id_t *es )
1528 bool b_decode = false;
1529 if( es->p_master->p_dec )
1531 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1532 if( i_channel != -1 )
1533 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1539 return es->p_dec != NULL;
1542 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1544 es_out_sys_t *p_sys = out->p_sys;
1545 input_thread_t *p_input = p_sys->p_input;
1547 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1550 if( p_sys->b_buffering )
1551 input_DecoderStartBuffering( p_es->p_dec );
1553 if( !p_es->p_master && p_sys->p_sout_record )
1555 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1556 if( p_es->p_dec_record && p_sys->b_buffering )
1557 input_DecoderStartBuffering( p_es->p_dec_record );
1561 EsOutDecoderChangeDelay( out, p_es );
1563 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1570 input_DecoderDelete( p_es->p_dec );
1573 if( p_es->p_dec_record )
1575 input_DecoderDelete( p_es->p_dec_record );
1576 p_es->p_dec_record = NULL;
1580 static void EsSelect( es_out_t *out, es_out_id_t *es )
1582 es_out_sys_t *p_sys = out->p_sys;
1583 input_thread_t *p_input = p_sys->p_input;
1585 if( EsIsSelected( es ) )
1587 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1594 if( !es->p_master->p_dec )
1597 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1598 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1603 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1605 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1607 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1612 else if( es->fmt.i_cat == AUDIO_ES )
1614 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1616 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1621 if( es->fmt.i_cat == SPU_ES )
1623 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1625 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1631 EsCreateDecoder( out, es );
1633 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1637 /* Mark it as selected */
1638 input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1639 input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1642 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1644 es_out_sys_t *p_sys = out->p_sys;
1645 input_thread_t *p_input = p_sys->p_input;
1647 if( !EsIsSelected( es ) )
1649 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1655 if( es->p_master->p_dec )
1657 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1658 if( i_channel != -1 )
1659 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1664 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1666 for( i = 0; i < 4; i++ )
1668 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1671 if( i_spu_id == es->pp_cc_es[i]->i_id )
1673 /* Force unselection of the CC */
1674 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1676 EsOutDel( out, es->pp_cc_es[i] );
1678 es->pb_cc_present[i] = false;
1680 EsDestroyDecoder( out, es );
1686 /* Mark it as unselected */
1687 input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1688 if( EsFmtIsTeletext( &es->fmt ) )
1689 input_SendEventTeletextSelect( p_input, -1 );
1693 * Select an ES given the current mode
1694 * XXX: you need to take a the lock before (stream.stream_lock)
1696 * \param out The es_out structure
1697 * \param es es_out_id structure
1698 * \param b_force ...
1701 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1703 es_out_sys_t *p_sys = out->p_sys;
1705 int i_cat = es->fmt.i_cat;
1707 if( !p_sys->b_active ||
1708 ( !b_force && es->fmt.i_priority < 0 ) )
1713 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1715 if( !EsIsSelected( es ) )
1716 EsSelect( out, es );
1718 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1722 var_Get( p_sys->p_input, "programs", &val );
1723 for ( i = 0; i < val.p_list->i_count; i++ )
1725 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1727 if( !EsIsSelected( es ) )
1728 EsSelect( out, es );
1732 var_FreeList( &val, NULL );
1734 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1738 if( es->p_pgrm != p_sys->p_pgrm )
1741 if( i_cat == AUDIO_ES )
1743 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1744 es->psz_language_code );
1746 if( p_sys->p_es_audio &&
1747 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1749 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1750 p_sys->p_es_audio->psz_language_code );
1752 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1754 i_wanted = es->i_channel;
1758 /* Select audio if (no audio selected yet)
1759 * - no audio-language
1760 * - no audio code for the ES
1761 * - audio code in the requested list */
1763 !strcmp( es->psz_language_code, "??" ) ||
1764 !p_sys->ppsz_audio_language )
1765 i_wanted = es->i_channel;
1768 if( p_sys->i_audio_last >= 0 )
1769 i_wanted = p_sys->i_audio_last;
1771 if( p_sys->i_audio_id >= 0 )
1773 if( es->i_id == p_sys->i_audio_id )
1774 i_wanted = es->i_channel;
1779 else if( i_cat == SPU_ES )
1781 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1782 es->psz_language_code );
1784 if( p_sys->p_es_sub &&
1785 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1787 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1788 p_sys->p_es_sub->psz_language_code );
1790 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1791 idx1, es->psz_language_code, idx2,
1792 p_sys->p_es_sub->psz_language_code );
1794 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1796 /* We found a SPU that matches our language request */
1797 i_wanted = es->i_channel;
1799 else if( idx1 >= 0 )
1801 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1802 idx1, es->psz_language_code );
1804 i_wanted = es->i_channel;
1806 else if( p_sys->i_default_sub_id >= 0 )
1808 if( es->i_id == p_sys->i_default_sub_id )
1809 i_wanted = es->i_channel;
1812 if( p_sys->i_sub_last >= 0 )
1813 i_wanted = p_sys->i_sub_last;
1815 if( p_sys->i_sub_id >= 0 )
1817 if( es->i_id == p_sys->i_sub_id )
1818 i_wanted = es->i_channel;
1823 else if( i_cat == VIDEO_ES )
1825 i_wanted = es->i_channel;
1828 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1829 EsSelect( out, es );
1832 /* FIXME TODO handle priority here */
1833 if( EsIsSelected( es ) )
1835 if( i_cat == AUDIO_ES )
1837 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1838 p_sys->p_es_audio &&
1839 p_sys->p_es_audio != es &&
1840 EsIsSelected( p_sys->p_es_audio ) )
1842 EsUnselect( out, p_sys->p_es_audio, false );
1844 p_sys->p_es_audio = es;
1846 else if( i_cat == SPU_ES )
1848 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1850 p_sys->p_es_sub != es &&
1851 EsIsSelected( p_sys->p_es_sub ) )
1853 EsUnselect( out, p_sys->p_es_sub, false );
1855 p_sys->p_es_sub = es;
1857 else if( i_cat == VIDEO_ES )
1859 p_sys->p_es_video = es;
1865 * Send a block for the given es_out
1867 * \param out the es_out to send from
1868 * \param es the es_out_id
1869 * \param p_block the data block to send
1871 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1873 es_out_sys_t *p_sys = out->p_sys;
1874 input_thread_t *p_input = p_sys->p_input;
1877 if( libvlc_stats( p_input ) )
1879 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1880 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1881 p_block->i_buffer, &i_total );
1882 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1883 (float)i_total, NULL );
1885 /* Update number of corrupted data packats */
1886 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
1888 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_corrupted,
1891 /* Update number of discontinuities */
1892 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1894 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_discontinuity,
1897 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1900 vlc_mutex_lock( &p_sys->lock );
1902 /* Mark preroll blocks */
1903 if( p_sys->i_preroll_end >= 0 )
1905 int64_t i_date = p_block->i_pts;
1907 i_date = p_block->i_dts;
1909 if( i_date < p_sys->i_preroll_end )
1910 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1913 p_block->i_rate = 0;
1917 block_Release( p_block );
1918 vlc_mutex_unlock( &p_sys->lock );
1922 /* Check for sout mode */
1925 /* FIXME review this, proper lock may be missing */
1926 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 &&
1927 p_input->p->b_out_pace_control )
1929 msg_Dbg( p_input, "switching to sync mode" );
1930 p_input->p->b_out_pace_control = false;
1932 else if( p_input->p->p_sout->i_out_pace_nocontrol <= 0 &&
1933 !p_input->p->b_out_pace_control )
1935 msg_Dbg( p_input, "switching to async mode" );
1936 p_input->p->b_out_pace_control = true;
1941 if( es->p_dec_record )
1943 block_t *p_dup = block_Duplicate( p_block );
1945 input_DecoderDecode( es->p_dec_record, p_dup,
1946 p_input->p->b_out_pace_control );
1948 input_DecoderDecode( es->p_dec, p_block,
1949 p_input->p->b_out_pace_control );
1951 es_format_t fmt_dsc;
1952 vlc_meta_t *p_meta_dsc;
1953 if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
1955 EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
1957 es_format_Clean( &fmt_dsc );
1959 vlc_meta_Delete( p_meta_dsc );
1962 /* Check CC status */
1965 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1966 for( int i = 0; i < 4; i++ )
1970 if( es->pb_cc_present[i] || !pb_cc[i] )
1972 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1974 es_format_Init( &fmt, SPU_ES, EsOutFourccClosedCaptions[i] );
1975 fmt.i_group = es->fmt.i_group;
1976 if( asprintf( &fmt.psz_description,
1977 _("Closed captions %u"), 1 + i ) == -1 )
1978 fmt.psz_description = NULL;
1979 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1980 es->pp_cc_es[i]->p_master = es;
1981 es_format_Clean( &fmt );
1984 es->pb_cc_present[i] = true;
1987 vlc_mutex_unlock( &p_sys->lock );
1992 /*****************************************************************************
1994 *****************************************************************************/
1995 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1997 es_out_sys_t *p_sys = out->p_sys;
1998 bool b_reselect = false;
2001 vlc_mutex_lock( &p_sys->lock );
2003 /* We don't try to reselect */
2006 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
2008 if( input_DecoderIsEmpty( es->p_dec ) &&
2009 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
2011 /* FIXME there should be a way to have auto deleted es, but there will be
2012 * a problem when another codec of the same type is created (mainly video) */
2015 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2018 if( es->p_pgrm == p_sys->p_pgrm )
2019 EsOutESVarUpdate( out, es, true );
2021 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
2023 /* Update program */
2025 if( es->p_pgrm->i_es == 0 )
2026 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
2028 if( es->b_scrambled )
2029 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2032 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
2033 p_sys->p_es_sub == es ) b_reselect = true;
2035 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
2036 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
2037 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
2039 switch( es->fmt.i_cat )
2052 /* Re-select another track when needed */
2055 for( i = 0; i < p_sys->i_es; i++ )
2057 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
2058 EsOutSelect( out, p_sys->es[i], false );
2062 free( es->psz_language );
2063 free( es->psz_language_code );
2065 es_format_Clean( &es->fmt );
2067 vlc_mutex_unlock( &p_sys->lock );
2073 * Control query handler
2075 * \param out the es_out to control
2076 * \param i_query A es_out query as defined in include/ninput.h
2077 * \param args a variable list of arguments for the query
2078 * \return VLC_SUCCESS or an error code
2080 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2082 es_out_sys_t *p_sys = out->p_sys;
2090 case ES_OUT_SET_ES_STATE:
2091 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2092 b = (bool) va_arg( args, int );
2093 if( b && !EsIsSelected( es ) )
2095 EsSelect( out, es );
2096 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2098 else if( !b && EsIsSelected( es ) )
2100 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2105 case ES_OUT_GET_ES_STATE:
2106 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2107 pb = (bool*) va_arg( args, bool * );
2109 *pb = EsIsSelected( es );
2112 case ES_OUT_SET_ACTIVE:
2114 b = (bool) va_arg( args, int );
2115 if( b && !p_sys->b_active && p_sys->i_es > 0 )
2117 /* XXX Terminate vout if there are tracks but no video one.
2118 * This one is not mandatory but is he earliest place where it
2120 for( i = 0; i < p_sys->i_es; i++ )
2122 es_out_id_t *p_es = p_sys->es[i];
2123 if( p_es->fmt.i_cat == VIDEO_ES )
2126 if( i >= p_sys->i_es )
2127 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2129 p_sys->b_active = b;
2133 case ES_OUT_SET_MODE:
2134 i = (int) va_arg( args, int );
2135 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
2136 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
2140 /* Reapply policy mode */
2141 for( i = 0; i < p_sys->i_es; i++ )
2143 if( EsIsSelected( p_sys->es[i] ) )
2145 EsUnselect( out, p_sys->es[i],
2146 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2149 for( i = 0; i < p_sys->i_es; i++ )
2151 EsOutSelect( out, p_sys->es[i], false );
2155 return VLC_EGENERIC;
2158 case ES_OUT_RESTART_ES:
2162 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2166 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2168 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2170 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2175 for( i = 0; i < p_sys->i_es; i++ )
2179 if( es == p_sys->es[i] )
2181 EsOutSelect( out, es, true );
2187 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2189 if( EsIsSelected( p_sys->es[i] ) )
2191 if( i_query == ES_OUT_RESTART_ES )
2193 if( p_sys->es[i]->p_dec )
2195 EsDestroyDecoder( out, p_sys->es[i] );
2196 EsCreateDecoder( out, p_sys->es[i] );
2201 EsUnselect( out, p_sys->es[i],
2202 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2211 case ES_OUT_SET_ES_DEFAULT:
2213 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2217 /*p_sys->i_default_video_id = -1;*/
2218 /*p_sys->i_default_audio_id = -1;*/
2219 p_sys->i_default_sub_id = -1;
2221 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2223 /*p_sys->i_default_video_id = -1;*/
2225 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2227 /*p_sys->i_default_audio_id = -1;*/
2229 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2231 p_sys->i_default_sub_id = -1;
2235 /*if( es->fmt.i_cat == VIDEO_ES )
2236 p_sys->i_default_video_id = es->i_id;
2238 if( es->fmt.i_cat == AUDIO_ES )
2239 p_sys->i_default_audio_id = es->i_id;
2241 if( es->fmt.i_cat == SPU_ES )
2242 p_sys->i_default_sub_id = es->i_id;
2247 case ES_OUT_SET_PCR:
2248 case ES_OUT_SET_GROUP_PCR:
2250 es_out_pgrm_t *p_pgrm = NULL;
2254 /* Search program */
2255 if( i_query == ES_OUT_SET_PCR )
2257 p_pgrm = p_sys->p_pgrm;
2259 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2263 i_group = (int)va_arg( args, int );
2264 p_pgrm = EsOutProgramFind( out, i_group );
2267 return VLC_EGENERIC;
2269 i_pcr = (int64_t)va_arg( args, int64_t );
2270 if( i_pcr <= VLC_TS_INVALID )
2272 msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
2273 return VLC_EGENERIC;
2276 /* TODO do not use mdate() but proper stream acquisition date */
2278 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2280 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering,
2281 EsOutIsExtraBufferingAllowed( out ),
2284 if( p_pgrm == p_sys->p_pgrm )
2286 if( p_sys->b_buffering )
2288 /* Check buffering state on master clock update */
2289 EsOutDecodersStopBuffering( out, false );
2293 mtime_t i_pts_delay = input_clock_GetJitter( p_pgrm->p_clock );
2295 /* Avoid dangerously high value */
2296 const mtime_t i_pts_delay_max = 30000000;
2297 if( i_pts_delay > i_pts_delay_max )
2298 i_pts_delay = __MAX( i_pts_delay_max, p_sys->i_pts_delay );
2300 /* Force a rebufferization when we are too late */
2301 msg_Err( p_sys->p_input,
2302 "ES_OUT_SET_(GROUP_)PCR is called too late, increasing pts_delay to %d ms",
2303 (int)(i_pts_delay/1000) );
2305 /* It is not really good, as we throw away already buffered data
2306 * TODO have a mean to correctly reenter bufferization */
2307 es_out_Control( out, ES_OUT_RESET_PCR );
2309 es_out_Control( out, ES_OUT_SET_JITTER, i_pts_delay, p_sys->i_cr_average );
2315 case ES_OUT_RESET_PCR:
2316 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2317 EsOutChangePosition( out );
2320 case ES_OUT_SET_GROUP:
2323 i = (int) va_arg( args, int );
2324 for( j = 0; j < p_sys->i_pgrm; j++ )
2326 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2327 if( p_pgrm->i_id == i )
2329 EsOutProgramSelect( out, p_pgrm );
2333 return VLC_EGENERIC;
2336 case ES_OUT_SET_ES_FMT:
2338 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2339 * to update the p_extra data */
2341 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2342 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2344 return VLC_EGENERIC;
2346 if( p_fmt->i_extra )
2348 es->fmt.i_extra = p_fmt->i_extra;
2349 es->fmt.p_extra = xrealloc( es->fmt.p_extra, p_fmt->i_extra );
2350 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2355 EsDestroyDecoder( out, es );
2357 EsCreateDecoder( out, es );
2359 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2360 es->p_dec->fmt_in.p_extra =
2361 xrealloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2362 memcpy( es->p_dec->fmt_in.p_extra,
2363 p_fmt->p_extra, p_fmt->i_extra );
2370 case ES_OUT_SET_ES_SCRAMBLED_STATE:
2372 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2373 bool b_scrambled = (bool)va_arg( args, int );
2375 if( !es->b_scrambled != !b_scrambled )
2377 es->b_scrambled = b_scrambled;
2378 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2383 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2385 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2388 return VLC_EGENERIC;
2390 p_sys->i_preroll_end = i_date;
2394 case ES_OUT_SET_GROUP_META:
2396 int i_group = (int)va_arg( args, int );
2397 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2399 EsOutProgramMeta( out, i_group, p_meta );
2402 case ES_OUT_SET_GROUP_EPG:
2404 int i_group = (int)va_arg( args, int );
2405 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
2407 EsOutProgramEpg( out, i_group, p_epg );
2411 case ES_OUT_DEL_GROUP:
2413 int i_group = (int)va_arg( args, int );
2415 return EsOutProgramDel( out, i_group );
2418 case ES_OUT_SET_META:
2420 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2422 EsOutMeta( out, p_meta );
2426 case ES_OUT_GET_WAKE_UP:
2428 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2429 *pi_wakeup = EsOutGetWakeup( out );
2433 case ES_OUT_SET_ES_BY_ID:
2434 case ES_OUT_RESTART_ES_BY_ID:
2435 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2437 const int i_id = (int)va_arg( args, int );
2438 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2443 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2444 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2445 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2449 /* TODO if the lock is made non recursive it should be changed */
2450 int i_ret = es_out_Control( out, i_new_query, p_es );
2452 /* Clean up vout after user action (in active mode only).
2453 * FIXME it does not work well with multiple video windows */
2454 if( p_sys->b_active )
2455 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2459 case ES_OUT_GET_BUFFERING:
2460 pb = (bool *)va_arg( args, bool* );
2461 *pb = p_sys->b_buffering;
2464 case ES_OUT_GET_EMPTY:
2465 pb = (bool *)va_arg( args, bool* );
2466 *pb = EsOutDecodersIsEmpty( out );
2469 case ES_OUT_SET_DELAY:
2471 const int i_cat = (int)va_arg( args, int );
2472 const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2473 EsOutSetDelay( out, i_cat, i_delay );
2477 case ES_OUT_SET_RECORD_STATE:
2478 b = (bool) va_arg( args, int );
2479 return EsOutSetRecord( out, b );
2481 case ES_OUT_SET_PAUSE_STATE:
2483 const bool b_source_paused = (bool)va_arg( args, int );
2484 const bool b_paused = (bool)va_arg( args, int );
2485 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2487 assert( !b_source_paused == !b_paused );
2488 EsOutChangePause( out, b_paused, i_date );
2493 case ES_OUT_SET_RATE:
2495 const int i_src_rate = (int)va_arg( args, int );
2496 const int i_rate = (int)va_arg( args, int );
2498 assert( i_src_rate == i_rate );
2499 EsOutChangeRate( out, i_rate );
2504 case ES_OUT_SET_TIME:
2506 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2508 assert( i_date == -1 );
2509 EsOutChangePosition( out );
2514 case ES_OUT_SET_FRAME_NEXT:
2515 EsOutFrameNext( out );
2518 case ES_OUT_SET_TIMES:
2520 double f_position = (double)va_arg( args, double );
2521 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2522 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2524 input_SendEventLength( p_sys->p_input, i_length );
2526 if( !p_sys->b_buffering )
2528 /* Fix for buffering delay */
2529 const mtime_t i_delay = EsOutGetBuffering( out );
2536 f_position -= (double)i_delay / i_length;
2537 if( f_position < 0 )
2540 input_SendEventPosition( p_sys->p_input, f_position, i_time );
2544 case ES_OUT_SET_JITTER:
2546 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2547 int i_cr_average = (int)va_arg( args, int );
2549 if( i_pts_delay == p_sys->i_pts_delay &&
2550 i_cr_average == p_sys->i_cr_average )
2553 p_sys->i_pts_delay = i_pts_delay;
2554 p_sys->i_cr_average = i_cr_average;
2556 for( int i = 0; i < p_sys->i_pgrm; i++ )
2557 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2558 i_pts_delay, i_cr_average );
2563 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2564 return VLC_EGENERIC;
2567 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2569 es_out_sys_t *p_sys = out->p_sys;
2572 vlc_mutex_lock( &p_sys->lock );
2573 i_ret = EsOutControlLocked( out, i_query, args );
2574 vlc_mutex_unlock( &p_sys->lock );
2579 /****************************************************************************
2580 * LanguageGetName: try to expend iso639 into plain name
2581 ****************************************************************************/
2582 static char *LanguageGetName( const char *psz_code )
2584 const iso639_lang_t *pl;
2586 if( psz_code == NULL )
2588 return strdup( "" );
2591 if( strlen( psz_code ) == 2 )
2593 pl = GetLang_1( psz_code );
2595 else if( strlen( psz_code ) == 3 )
2597 pl = GetLang_2B( psz_code );
2598 if( !strcmp( pl->psz_iso639_1, "??" ) )
2600 pl = GetLang_2T( psz_code );
2605 return strdup( psz_code );
2608 if( !strcmp( pl->psz_iso639_1, "??" ) )
2610 return strdup( psz_code );
2614 if( *pl->psz_native_name )
2616 return strdup( pl->psz_native_name );
2618 return strdup( pl->psz_eng_name );
2622 /* Get a 2 char code */
2623 static char *LanguageGetCode( const char *psz_lang )
2625 const iso639_lang_t *pl;
2627 if( psz_lang == NULL || *psz_lang == '\0' )
2628 return strdup("??");
2630 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
2632 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2633 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2634 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2635 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2636 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2640 if( pl->psz_eng_name != NULL )
2641 return strdup( pl->psz_iso639_1 );
2643 return strdup("??");
2646 static char **LanguageSplit( const char *psz_langs )
2653 if( psz_langs == NULL ) return NULL;
2655 psz_parser = psz_dup = strdup(psz_langs);
2657 while( psz_parser && *psz_parser )
2662 psz = strchr(psz_parser, ',' );
2663 if( psz ) *psz++ = '\0';
2665 if( !strcmp( psz_parser, "any" ) )
2667 TAB_APPEND( i_psz, ppsz, strdup("any") );
2671 psz_code = LanguageGetCode( psz_parser );
2672 if( strcmp( psz_code, "??" ) )
2674 TAB_APPEND( i_psz, ppsz, psz_code );
2687 TAB_APPEND( i_psz, ppsz, NULL );
2694 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2698 if( !ppsz_langs || !psz_lang ) return -1;
2700 for( i = 0; ppsz_langs[i]; i++ )
2702 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2703 !strcasecmp( ppsz_langs[i], "any" ) )
2712 /****************************************************************************
2714 * - add meta info to the playlist item
2715 ****************************************************************************/
2716 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2718 es_out_sys_t *p_sys = out->p_sys;
2719 input_thread_t *p_input = p_sys->p_input;
2720 const es_format_t *p_fmt_es = &es->fmt;
2724 /* Create category name */
2725 if( asprintf( &psz_cat, _("Stream %d"), es->i_meta_id ) == -1 )
2728 /* Remove previous information */
2729 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
2731 /* Add informations */
2732 const char *psz_type;
2733 switch( fmt->i_cat )
2736 psz_type = _("Audio");
2739 psz_type = _("Video");
2742 psz_type = _("Subtitle");
2750 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Type"), psz_type );
2752 if( es->i_meta_id != es->i_id )
2753 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Original ID"),
2756 const char *psz_codec_description =
2757 vlc_fourcc_GetDescription( p_fmt_es->i_cat, p_fmt_es->i_codec );
2758 const vlc_fourcc_t i_codec_fourcc = p_fmt_es->i_original_fourcc ?: p_fmt_es->i_codec;
2759 if( psz_codec_description )
2760 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2761 "%s (%.4s)", psz_codec_description, (char*)&i_codec_fourcc );
2763 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2764 "%.4s", (char*)&i_codec_fourcc );
2766 if( es->psz_language && *es->psz_language )
2767 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2768 "%s", es->psz_language );
2769 if( fmt->psz_description && *fmt->psz_description )
2770 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
2771 "%s", fmt->psz_description );
2773 switch( fmt->i_cat )
2776 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2777 _("Type"), _("Audio") );
2779 if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
2780 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2781 "%s", _( aout_FormatPrintChannels( &fmt->audio ) ) );
2782 else if( fmt->audio.i_channels > 0 )
2783 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2784 "%u", fmt->audio.i_channels );
2786 if( fmt->audio.i_rate > 0 )
2788 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2789 _("%u Hz"), fmt->audio.i_rate );
2790 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2791 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2794 unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2795 if( i_bitspersample <= 0 )
2796 i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2797 if( i_bitspersample > 0 )
2798 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2799 _("Bits per sample"), "%u",
2802 if( fmt->i_bitrate > 0 )
2804 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2805 _("%u kb/s"), fmt->i_bitrate / 1000 );
2806 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2807 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2809 for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2811 const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2812 if( !p_rg->pb_gain[i] )
2814 const char *psz_name;
2815 if( i == AUDIO_REPLAY_GAIN_TRACK )
2816 psz_name = _("Track replay gain");
2818 psz_name = _("Album replay gain");
2819 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2820 psz_name, _("%.2f dB"), p_rg->pf_gain[i] );
2825 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2826 _("Type"), _("Video") );
2828 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2829 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2830 _("Resolution"), "%ux%u",
2831 fmt->video.i_width, fmt->video.i_height );
2833 if( fmt->video.i_visible_width > 0 &&
2834 fmt->video.i_visible_height > 0 )
2835 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2836 _("Display resolution"), "%ux%u",
2837 fmt->video.i_visible_width,
2838 fmt->video.i_visible_height);
2839 if( fmt->video.i_frame_rate > 0 &&
2840 fmt->video.i_frame_rate_base > 0 )
2842 div = lldiv( (float)fmt->video.i_frame_rate /
2843 fmt->video.i_frame_rate_base * 1000000,
2846 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2847 _("Frame rate"), "%"PRId64".%06u",
2848 div.quot, (unsigned int )div.rem );
2850 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2851 _("Frame rate"), "%"PRId64, div.quot );
2856 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2857 _("Type"), _("Subtitle") );
2864 /* Append generic meta */
2867 char **ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2868 for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
2870 char *psz_key = ppsz_all_keys[i];
2871 char *psz_value = vlc_dictionary_value_for_key( &p_meta->extra_tags, psz_key );
2874 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2875 vlc_gettext(psz_key), vlc_gettext(psz_value) );
2878 free( ppsz_all_keys );