1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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"
50 #include "../stream_output/stream_output.h"
52 #include <vlc_iso_lang.h>
53 /* FIXME we should find a better way than including that */
54 #include "../text/iso-639_def.h"
56 /*****************************************************************************
58 *****************************************************************************/
64 /* Number of es for this pgrm */
70 /* Clock for this program */
71 input_clock_t *p_clock;
74 char *psz_now_playing;
82 es_out_pgrm_t *p_pgrm;
87 /* Channel in the track type */
91 char *psz_language_code;
94 decoder_t *p_dec_record;
96 /* Fields for Video with CC */
97 bool pb_cc_present[4];
98 es_out_id_t *pp_cc_es[4];
100 /* Field for CC track from a master video */
101 es_out_id_t *p_master;
103 /* ID for the meta data */
109 input_thread_t *p_input;
116 es_out_pgrm_t **pgrm;
117 es_out_pgrm_t *p_pgrm; /* Master program */
133 /* es/group to select */
135 int i_audio_last, i_audio_id;
136 int i_sub_last, i_sub_id;
137 int i_default_sub_id; /* As specified in container; if applicable */
138 char **ppsz_audio_language;
139 char **ppsz_sub_language;
141 /* current main es */
142 es_out_id_t *p_es_audio;
143 es_out_id_t *p_es_video;
144 es_out_id_t *p_es_sub;
147 int64_t i_audio_delay;
150 /* Clock configuration */
152 mtime_t i_pts_jitter;
158 mtime_t i_pause_date;
160 /* Current preroll */
161 mtime_t i_preroll_end;
163 /* Used for buffering */
165 mtime_t i_buffering_extra_initial;
166 mtime_t i_buffering_extra_stream;
167 mtime_t i_buffering_extra_system;
170 sout_instance_t *p_sout_record;
173 static es_out_id_t *EsOutAdd ( es_out_t *, const es_format_t * );
174 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
175 static void EsOutDel ( es_out_t *, es_out_id_t * );
176 static int EsOutControl( es_out_t *, int i_query, va_list );
177 static void EsOutDelete ( es_out_t * );
179 static void EsOutTerminate( es_out_t * );
180 static void EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
181 static void EsOutUpdateInfo( es_out_t *, es_out_id_t *es, const es_format_t *, const vlc_meta_t * );
182 static int EsOutSetRecord( es_out_t *, bool b_record );
184 static bool EsIsSelected( es_out_id_t *es );
185 static void EsSelect( es_out_t *out, es_out_id_t *es );
186 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
187 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
188 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
189 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
190 static void EsOutProgramsChangeRate( es_out_t *out );
191 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
193 static char *LanguageGetName( const char *psz_code );
194 static char *LanguageGetCode( const char *psz_lang );
195 static char **LanguageSplit( const char *psz_langs, bool b_default_any );
196 static int LanguageArrayIndex( char **ppsz_langs, const char *psz_lang );
198 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
200 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
201 VLC_FOURCC('c', 'c', '1', ' '),
202 VLC_FOURCC('c', 'c', '2', ' '),
203 VLC_FOURCC('c', 'c', '3', ' '),
204 VLC_FOURCC('c', 'c', '4', ' '),
206 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
209 for( i = 0; i < 4; i++ )
211 if( fcc == EsOutFourccClosedCaptions[i] )
216 static inline bool EsFmtIsTeletext( const es_format_t *p_fmt )
218 return p_fmt->i_cat == SPU_ES && p_fmt->i_codec == VLC_CODEC_TELETEXT;
221 /*****************************************************************************
223 *****************************************************************************/
224 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
226 es_out_t *out = malloc( sizeof( *out ) );
230 es_out_sys_t *p_sys = calloc( 1, sizeof( *p_sys ) );
237 out->pf_add = EsOutAdd;
238 out->pf_send = EsOutSend;
239 out->pf_del = EsOutDel;
240 out->pf_control = EsOutControl;
241 out->pf_destroy = EsOutDelete;
244 vlc_mutex_init_recursive( &p_sys->lock );
245 p_sys->p_input = p_input;
247 p_sys->b_active = false;
248 p_sys->i_mode = ES_OUT_MODE_NONE;
250 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
252 TAB_INIT( p_sys->i_es, p_sys->es );
255 p_sys->i_group_id = var_GetInteger( p_input, "program" );
256 p_sys->i_audio_last = var_GetInteger( p_input, "audio-track" );
257 p_sys->i_sub_last = var_GetInteger( p_input, "sub-track" );
259 p_sys->i_default_sub_id = -1;
261 if( !p_input->b_preparsing )
265 psz_string = var_GetString( p_input, "audio-language" );
266 p_sys->ppsz_audio_language = LanguageSplit( psz_string, true );
267 if( p_sys->ppsz_audio_language )
269 for( int i = 0; p_sys->ppsz_audio_language[i]; i++ )
270 msg_Dbg( p_input, "selected audio language[%d] %s",
271 i, p_sys->ppsz_audio_language[i] );
275 psz_string = var_GetString( p_input, "sub-language" );
276 p_sys->ppsz_sub_language = LanguageSplit( psz_string, false );
277 if( p_sys->ppsz_sub_language )
279 for( int i = 0; p_sys->ppsz_sub_language[i]; i++ )
280 msg_Dbg( p_input, "selected subtitle language[%d] %s",
281 i, p_sys->ppsz_sub_language[i] );
286 p_sys->i_audio_id = var_GetInteger( p_input, "audio-track-id" );
288 p_sys->i_sub_id = var_GetInteger( p_input, "sub-track-id" );
290 p_sys->i_pause_date = -1;
292 p_sys->i_rate = i_rate;
294 p_sys->b_buffering = true;
295 p_sys->i_preroll_end = -1;
300 int es_out_GetEsState( es_out_t *out, const int i_cat, bool *b_selected, bool *b_error)
302 if( !out && !out->p_sys )
309 p_es = out->p_sys->p_es_video;
312 p_es = out->p_sys->p_es_audio;
315 p_es = out->p_sys->p_es_sub;
324 return es_out_Control( out, ES_OUT_GET_ES_STATE, p_es, b_selected, b_error );
327 /*****************************************************************************
329 *****************************************************************************/
330 static void EsOutDelete( es_out_t *out )
332 es_out_sys_t *p_sys = out->p_sys;
334 assert( !p_sys->i_es && !p_sys->i_pgrm && !p_sys->p_pgrm );
335 if( p_sys->ppsz_audio_language )
337 for( int i = 0; p_sys->ppsz_audio_language[i]; i++ )
338 free( p_sys->ppsz_audio_language[i] );
339 free( p_sys->ppsz_audio_language );
341 if( p_sys->ppsz_sub_language )
343 for( int i = 0; p_sys->ppsz_sub_language[i]; i++ )
344 free( p_sys->ppsz_sub_language[i] );
345 free( p_sys->ppsz_sub_language );
348 vlc_mutex_destroy( &p_sys->lock );
354 static void EsOutTerminate( es_out_t *out )
356 es_out_sys_t *p_sys = out->p_sys;
358 if( p_sys->p_sout_record )
359 EsOutSetRecord( out, false );
361 for( int i = 0; i < p_sys->i_es; i++ )
363 if( p_sys->es[i]->p_dec )
364 input_DecoderDelete( p_sys->es[i]->p_dec );
366 free( p_sys->es[i]->psz_language );
367 free( p_sys->es[i]->psz_language_code );
368 es_format_Clean( &p_sys->es[i]->fmt );
370 free( p_sys->es[i] );
372 TAB_CLEAN( p_sys->i_es, p_sys->es );
374 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
375 for( int i = 0; i < p_sys->i_pgrm; i++ )
377 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
378 input_clock_Delete( p_pgrm->p_clock );
379 free( p_pgrm->psz_now_playing );
380 free( p_pgrm->psz_publisher );
381 free( p_pgrm->psz_name );
385 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
387 p_sys->p_pgrm = NULL;
389 input_item_SetEpgOffline( p_sys->p_input->p->p_item );
390 input_SendEventMetaEpg( p_sys->p_input );
393 static mtime_t EsOutGetWakeup( es_out_t *out )
395 es_out_sys_t *p_sys = out->p_sys;
396 input_thread_t *p_input = p_sys->p_input;
401 /* We do not have a wake up date if the input cannot have its speed
402 * controlled or sout is imposing its own or while buffering
404 * FIXME for !p_input->p->b_can_pace_control a wake-up time is still needed
405 * to avoid too heavy buffering */
406 if( !p_input->p->b_can_pace_control ||
407 p_input->p->b_out_pace_control ||
411 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
414 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
418 /* Special HACK, -i_id is the cat of the stream */
419 return (es_out_id_t*)((uint8_t*)NULL-i_id);
422 for( int i = 0; i < out->p_sys->i_es; i++ )
424 if( out->p_sys->es[i]->i_id == i_id )
425 return out->p_sys->es[i];
430 static bool EsOutDecodersIsEmpty( es_out_t *out )
432 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( int 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( 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_DecoderStartWait( 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_DecoderStartWait( p_es->p_dec );
607 if( p_es->p_dec_record )
608 input_DecoderStartWait( 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;
621 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
623 es_out_sys_t *p_sys = out->p_sys;
626 mtime_t i_stream_start;
627 mtime_t i_system_start;
628 mtime_t i_stream_duration;
629 mtime_t i_system_duration;
630 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
631 &i_stream_start, &i_system_start,
632 &i_stream_duration, &i_system_duration );
633 assert( !i_ret || b_forced );
637 mtime_t i_preroll_duration = 0;
638 if( p_sys->i_preroll_end >= 0 )
639 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
641 const mtime_t i_buffering_duration = p_sys->i_pts_delay +
643 p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
645 if( i_stream_duration <= i_buffering_duration && !b_forced )
647 const double f_level = __MAX( (double)i_stream_duration / i_buffering_duration, 0 );
648 input_SendEventCache( p_sys->p_input, f_level );
650 msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
653 input_SendEventCache( p_sys->p_input, 1.0 );
655 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
656 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
657 p_sys->b_buffering = false;
658 p_sys->i_preroll_end = -1;
660 if( p_sys->i_buffering_extra_initial > 0 )
666 const mtime_t i_decoder_buffering_start = mdate();
667 for( int i = 0; i < p_sys->i_es; i++ )
669 es_out_id_t *p_es = p_sys->es[i];
671 if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
673 input_DecoderWait( p_es->p_dec );
674 if( p_es->p_dec_record )
675 input_DecoderWait( p_es->p_dec_record );
678 msg_Dbg( p_sys->p_input, "Decoder wait done in %d ms",
679 (int)(mdate() - i_decoder_buffering_start)/1000 );
681 /* Here is a good place to destroy unused vout with every demuxer */
682 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
685 const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
686 const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
688 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, true,
689 i_current_date + i_wakeup_delay - i_buffering_duration );
691 for( int i = 0; i < p_sys->i_es; i++ )
693 es_out_id_t *p_es = p_sys->es[i];
698 input_DecoderStopWait( p_es->p_dec );
699 if( p_es->p_dec_record )
700 input_DecoderStopWait( p_es->p_dec_record );
703 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
705 es_out_sys_t *p_sys = out->p_sys;
707 /* Pause decoders first */
708 for( int i = 0; i < p_sys->i_es; i++ )
710 es_out_id_t *es = p_sys->es[i];
714 input_DecoderChangePause( es->p_dec, b_paused, i_date );
715 if( es->p_dec_record )
716 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
721 static bool EsOutIsExtraBufferingAllowed( es_out_t *out )
723 es_out_sys_t *p_sys = out->p_sys;
726 for( int i = 0; i < p_sys->i_es; i++ )
728 es_out_id_t *p_es = p_sys->es[i];
731 i_size += input_DecoderGetFifoSize( p_es->p_dec );
732 if( p_es->p_dec_record )
733 i_size += input_DecoderGetFifoSize( p_es->p_dec_record );
735 //msg_Info( out, "----- EsOutIsExtraBufferingAllowed =% 5d KiB -- ", i_size / 1024 );
737 /* TODO maybe we want to be able to tune it ? */
738 #if defined(OPTIMIZE_MEMORY)
739 const size_t i_level_high = 512*1024; /* 0.5 MiB */
741 const size_t i_level_high = 10*1024*1024; /* 10 MiB */
743 return i_size < i_level_high;
746 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
748 es_out_sys_t *p_sys = out->p_sys;
750 for( int i = 0; i < p_sys->i_pgrm; i++ )
751 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
754 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
756 es_out_sys_t *p_sys = out->p_sys;
759 if( p_es->fmt.i_cat == AUDIO_ES )
760 i_delay = p_sys->i_audio_delay;
761 else if( p_es->fmt.i_cat == SPU_ES )
762 i_delay = p_sys->i_spu_delay;
767 input_DecoderChangeDelay( p_es->p_dec, i_delay );
768 if( p_es->p_dec_record )
769 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
771 static void EsOutProgramsChangeRate( es_out_t *out )
773 es_out_sys_t *p_sys = out->p_sys;
775 for( int i = 0; i < p_sys->i_pgrm; i++ )
776 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
779 static void EsOutFrameNext( es_out_t *out )
781 es_out_sys_t *p_sys = out->p_sys;
782 es_out_id_t *p_es_video = NULL;
784 if( p_sys->b_buffering )
786 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
790 assert( p_sys->b_paused );
792 for( int i = 0; i < p_sys->i_es; i++ )
794 es_out_id_t *p_es = p_sys->es[i];
796 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
805 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
810 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
812 msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
814 if( i_duration <= 0 )
815 i_duration = 40*1000;
817 /* FIXME it is not a clean way ? */
818 if( p_sys->i_buffering_extra_initial <= 0 )
820 mtime_t i_stream_start;
821 mtime_t i_system_start;
822 mtime_t i_stream_duration;
823 mtime_t i_system_duration;
826 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
827 &i_stream_start, &i_system_start,
828 &i_stream_duration, &i_system_duration );
832 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
833 p_sys->i_buffering_extra_system =
834 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
837 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
839 p_sys->b_buffering = true;
840 p_sys->i_buffering_extra_system += i_duration;
841 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
842 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
843 INPUT_RATE_DEFAULT / i_rate;
845 p_sys->i_preroll_end = -1;
847 static mtime_t EsOutGetBuffering( es_out_t *out )
849 es_out_sys_t *p_sys = out->p_sys;
855 mtime_t i_stream_start;
856 mtime_t i_system_start;
857 mtime_t i_stream_duration;
858 mtime_t i_system_duration;
859 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
860 &i_stream_start, &i_system_start,
861 &i_stream_duration, &i_system_duration );
868 if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
870 i_delay = i_stream_duration;
874 mtime_t i_system_duration;
875 if( p_sys->b_paused )
877 i_system_duration = p_sys->i_pause_date - i_system_start;
878 if( p_sys->i_buffering_extra_initial > 0 )
879 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
883 i_system_duration = mdate() - i_system_start;
886 const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
887 i_delay = p_sys->i_pts_delay - i_consumed;
894 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
895 const es_format_t *fmt, const char *psz_language,
898 es_out_sys_t *p_sys = out->p_sys;
899 input_thread_t *p_input = p_sys->p_input;
900 vlc_value_t val, text;
904 if( EsFmtIsTeletext( fmt ) )
905 input_SendEventTeletextDel( p_sys->p_input, i_id );
907 input_SendEventEsDel( p_input, fmt->i_cat, i_id );
911 /* Get the number of ES already added */
913 if( fmt->i_cat == AUDIO_ES )
914 psz_var = "audio-es";
915 else if( fmt->i_cat == VIDEO_ES )
916 psz_var = "video-es";
920 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
925 /* First one, we need to add the "Disable" choice */
926 val2.i_int = -1; text.psz_string = _("Disable");
927 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
931 /* Take care of the ES description */
932 if( fmt->psz_description && *fmt->psz_description )
934 if( psz_language && *psz_language )
936 if( asprintf( &text.psz_string, "%s - [%s]", fmt->psz_description,
937 psz_language ) == -1 )
938 text.psz_string = NULL;
940 else text.psz_string = strdup( fmt->psz_description );
944 if( psz_language && *psz_language )
946 if( asprintf( &text.psz_string, "%s %"PRId64" - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
947 text.psz_string = NULL;
951 if( asprintf( &text.psz_string, "%s %"PRId64, _( "Track" ), val.i_int ) == -1 )
952 text.psz_string = NULL;
956 input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
957 if( EsFmtIsTeletext( fmt ) )
960 snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
961 fmt->subs.teletext.i_magazine,
962 fmt->subs.teletext.i_page );
963 input_SendEventTeletextAdd( p_sys->p_input,
964 i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
967 free( text.psz_string );
970 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
973 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
976 static bool EsOutIsProgramVisible( es_out_t *out, int i_group )
978 return out->p_sys->i_group_id == 0 || out->p_sys->i_group_id == i_group;
981 /* EsOutProgramSelect:
982 * Select a program and update the object variable
984 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
986 es_out_sys_t *p_sys = out->p_sys;
987 input_thread_t *p_input = p_sys->p_input;
990 if( p_sys->p_pgrm == p_pgrm )
991 return; /* Nothing to do */
995 es_out_pgrm_t *old = p_sys->p_pgrm;
996 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
998 for( i = 0; i < p_sys->i_es; i++ )
1000 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
1001 p_sys->i_mode != ES_OUT_MODE_ALL )
1002 EsUnselect( out, p_sys->es[i], true );
1005 p_sys->p_es_audio = NULL;
1006 p_sys->p_es_sub = NULL;
1007 p_sys->p_es_video = NULL;
1010 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
1012 /* Mark it selected */
1013 p_pgrm->b_selected = true;
1015 /* Switch master stream */
1016 p_sys->p_pgrm = p_pgrm;
1018 /* Update "program" */
1019 input_SendEventProgramSelect( p_input, p_pgrm->i_id );
1022 input_SendEventEsDel( p_input, AUDIO_ES, -1 );
1023 input_SendEventEsDel( p_input, VIDEO_ES, -1 );
1024 input_SendEventEsDel( p_input, SPU_ES, -1 );
1025 input_SendEventTeletextDel( p_input, -1 );
1026 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
1029 var_SetInteger( p_input, "teletext-es", -1 );
1031 for( i = 0; i < p_sys->i_es; i++ )
1033 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
1034 EsOutESVarUpdate( out, p_sys->es[i], false );
1035 EsOutSelect( out, p_sys->es[i], false );
1038 /* Update now playing */
1039 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1040 input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
1042 input_SendEventMeta( p_input );
1048 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
1050 es_out_sys_t *p_sys = out->p_sys;
1051 input_thread_t *p_input = p_sys->p_input;
1053 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1058 p_pgrm->i_id = i_group;
1060 p_pgrm->b_selected = false;
1061 p_pgrm->b_scrambled = false;
1062 p_pgrm->psz_name = NULL;
1063 p_pgrm->psz_now_playing = NULL;
1064 p_pgrm->psz_publisher = NULL;
1065 p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1066 if( !p_pgrm->p_clock )
1071 if( p_sys->b_paused )
1072 input_clock_ChangePause( p_pgrm->p_clock, p_sys->b_paused, p_sys->i_pause_date );
1073 input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1076 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1078 /* Update "program" variable */
1079 if( EsOutIsProgramVisible( out, i_group ) )
1080 input_SendEventProgramAdd( p_input, i_group, NULL );
1082 if( i_group == p_sys->i_group_id || ( !p_sys->p_pgrm && p_sys->i_group_id == 0 ) )
1083 EsOutProgramSelect( out, p_pgrm );
1091 static int EsOutProgramDel( es_out_t *out, int i_group )
1093 es_out_sys_t *p_sys = out->p_sys;
1094 input_thread_t *p_input = p_sys->p_input;
1095 es_out_pgrm_t *p_pgrm = NULL;
1098 for( i = 0; i < p_sys->i_pgrm; i++ )
1100 if( p_sys->pgrm[i]->i_id == i_group )
1102 p_pgrm = p_sys->pgrm[i];
1107 if( p_pgrm == NULL )
1108 return VLC_EGENERIC;
1112 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1113 i_group, p_pgrm->i_es );
1114 return VLC_EGENERIC;
1117 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1119 /* If program is selected we need to unselect it */
1120 if( p_sys->p_pgrm == p_pgrm )
1121 p_sys->p_pgrm = NULL;
1123 input_clock_Delete( p_pgrm->p_clock );
1125 free( p_pgrm->psz_name );
1126 free( p_pgrm->psz_now_playing );
1127 free( p_pgrm->psz_publisher );
1130 /* Update "program" variable */
1131 input_SendEventProgramDel( p_input, i_group );
1138 static es_out_pgrm_t *EsOutProgramFind( es_out_t *p_out, int i_group )
1140 es_out_sys_t *p_sys = p_out->p_sys;
1142 for( int i = 0; i < p_sys->i_pgrm; i++ )
1144 if( p_sys->pgrm[i]->i_id == i_group )
1145 return p_sys->pgrm[i];
1147 return EsOutProgramAdd( p_out, i_group );
1150 /* EsOutProgramMeta:
1152 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1155 if( p_pgrm->psz_name )
1157 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1162 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1168 static void EsOutProgramMeta( es_out_t *out, int i_group, const vlc_meta_t *p_meta )
1170 es_out_sys_t *p_sys = out->p_sys;
1171 es_out_pgrm_t *p_pgrm;
1172 input_thread_t *p_input = p_sys->p_input;
1173 const char *psz_title = NULL;
1174 const char *psz_provider = NULL;
1177 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1179 /* Check against empty meta data (empty for what we handle) */
1180 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1181 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1182 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1183 vlc_meta_GetExtraCount( p_meta ) <= 0 )
1188 if( !EsOutIsProgramVisible( out, i_group ) )
1190 p_pgrm = EsOutProgramFind( out, i_group );
1195 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1196 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1198 /* Update the description text of the program */
1199 if( psz_title && *psz_title )
1201 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1203 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1205 /* Remove old entries */
1206 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1207 /* TODO update epg name ?
1208 * TODO update scrambled info name ? */
1211 free( p_pgrm->psz_name );
1212 p_pgrm->psz_name = strdup( psz_title );
1215 if( psz_provider && *psz_provider )
1217 if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1222 psz_text = strdup( psz_title );
1225 /* ugly but it works */
1228 input_SendEventProgramDel( p_input, i_group );
1229 input_SendEventProgramAdd( p_input, i_group, psz_text );
1230 if( p_sys->p_pgrm == p_pgrm )
1231 input_SendEventProgramSelect( p_input, i_group );
1237 char **ppsz_all_keys = vlc_meta_CopyExtraNames(p_meta );
1239 info_category_t *p_cat = NULL;
1240 if( psz_provider || ( ppsz_all_keys[0] && *ppsz_all_keys[0] ) )
1242 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1244 p_cat = info_category_New( psz_cat );
1248 for( i = 0; ppsz_all_keys[i]; i++ )
1251 info_category_AddInfo( p_cat, vlc_gettext(ppsz_all_keys[i]), "%s",
1252 vlc_meta_GetExtra( p_meta, ppsz_all_keys[i] ) );
1253 free( ppsz_all_keys[i] );
1255 free( ppsz_all_keys );
1259 if( p_sys->p_pgrm == p_pgrm )
1261 input_item_SetPublisher( p_input->p->p_item, psz_provider );
1262 input_SendEventMeta( p_input );
1265 info_category_AddInfo( p_cat, vlc_meta_TypeToLocalizedString(vlc_meta_Publisher),
1266 "%s",psz_provider );
1269 input_Control( p_input, INPUT_MERGE_INFOS, p_cat );
1272 static void EsOutProgramEpg( es_out_t *out, int i_group, const vlc_epg_t *p_epg )
1274 es_out_sys_t *p_sys = out->p_sys;
1275 input_thread_t *p_input = p_sys->p_input;
1276 input_item_t *p_item = p_input->p->p_item;
1277 es_out_pgrm_t *p_pgrm;
1281 if( !EsOutIsProgramVisible( out, i_group ) )
1283 p_pgrm = EsOutProgramFind( out, i_group );
1288 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1289 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, psz_cat );
1295 epg.psz_name = psz_cat;
1297 input_item_SetEpg( p_item, &epg );
1298 input_SendEventMetaEpg( p_sys->p_input );
1300 /* Update now playing */
1301 free( p_pgrm->psz_now_playing );
1302 p_pgrm->psz_now_playing = NULL;
1304 vlc_mutex_lock( &p_item->lock );
1305 for( int i = 0; i < p_item->i_epg; i++ )
1307 const vlc_epg_t *p_tmp = p_item->pp_epg[i];
1309 if( p_tmp->psz_name && !strcmp(p_tmp->psz_name, psz_cat) )
1311 if( p_tmp->p_current && p_tmp->p_current->psz_name && *p_tmp->p_current->psz_name )
1312 p_pgrm->psz_now_playing = strdup( p_tmp->p_current->psz_name );
1316 vlc_mutex_unlock( &p_item->lock );
1318 if( p_pgrm == p_sys->p_pgrm )
1320 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1321 input_SendEventMeta( p_input );
1324 if( p_pgrm->psz_now_playing )
1326 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1327 vlc_meta_TypeToLocalizedString(vlc_meta_NowPlaying),
1328 p_pgrm->psz_now_playing );
1332 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1333 vlc_meta_TypeToLocalizedString(vlc_meta_NowPlaying) );
1339 static void EsOutProgramUpdateScrambled( es_out_t *p_out, es_out_pgrm_t *p_pgrm )
1341 es_out_sys_t *p_sys = p_out->p_sys;
1342 input_thread_t *p_input = p_sys->p_input;
1343 bool b_scrambled = false;
1345 for( int i = 0; i < p_sys->i_es; i++ )
1347 if( p_sys->es[i]->p_pgrm == p_pgrm && p_sys->es[i]->b_scrambled )
1353 if( !p_pgrm->b_scrambled == !b_scrambled )
1356 p_pgrm->b_scrambled = b_scrambled;
1357 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1360 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Scrambled"), _("Yes") );
1362 input_Control( p_input, INPUT_DEL_INFO, psz_cat, _("Scrambled") );
1364 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, b_scrambled );
1367 static void EsOutMeta( es_out_t *p_out, const vlc_meta_t *p_meta )
1369 es_out_sys_t *p_sys = p_out->p_sys;
1370 input_thread_t *p_input = p_sys->p_input;
1372 input_item_t *p_item = input_GetItem( p_input );
1374 char *psz_title = NULL;
1375 char *psz_arturl = input_item_GetArtURL( p_item );
1377 vlc_mutex_lock( &p_item->lock );
1379 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
1380 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
1382 vlc_meta_Merge( p_item->p_meta, p_meta );
1384 if( !psz_arturl || *psz_arturl == '\0' )
1386 const char *psz_tmp = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
1388 psz_arturl = strdup( psz_tmp );
1390 vlc_mutex_unlock( &p_item->lock );
1392 if( psz_arturl && *psz_arturl )
1394 input_item_SetArtURL( p_item, psz_arturl );
1396 if( !strncmp( psz_arturl, "attachment://", 13 ) )
1398 /* Don't look for art cover if sout
1399 * XXX It can change when sout has meta data support */
1400 if( p_input->p->p_sout && !p_input->b_preparsing )
1401 input_item_SetArtURL( p_item, "" );
1403 input_ExtractAttachmentAndCacheArt( p_input );
1410 input_item_SetName( p_item, psz_title );
1413 input_item_SetPreparsed( p_item, true );
1415 input_SendEventMeta( p_input );
1416 /* TODO handle sout meta ? */
1422 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1424 es_out_sys_t *p_sys = out->p_sys;
1425 input_thread_t *p_input = p_sys->p_input;
1427 if( fmt->i_group < 0 )
1429 msg_Err( p_input, "invalid group number" );
1433 es_out_id_t *es = malloc( sizeof( *es ) );
1434 es_out_pgrm_t *p_pgrm;
1440 vlc_mutex_lock( &p_sys->lock );
1442 /* Search the program */
1443 p_pgrm = EsOutProgramFind( out, fmt->i_group );
1446 vlc_mutex_unlock( &p_sys->lock );
1451 /* Increase ref count for program */
1455 es->p_pgrm = p_pgrm;
1456 es_format_Copy( &es->fmt, fmt );
1457 if( es->fmt.i_id < 0 )
1458 es->fmt.i_id = out->p_sys->i_id;
1459 if( !es->fmt.i_original_fourcc )
1460 es->fmt.i_original_fourcc = es->fmt.i_codec;
1461 if( es->fmt.i_cat == AUDIO_ES )
1462 es->fmt.i_codec = vlc_fourcc_GetCodecAudio( es->fmt.i_codec,
1463 es->fmt.audio.i_bitspersample );
1465 es->fmt.i_codec = vlc_fourcc_GetCodec( es->fmt.i_cat,
1468 es->i_id = es->fmt.i_id;
1469 es->i_meta_id = out->p_sys->i_id;
1470 es->b_scrambled = false;
1472 switch( es->fmt.i_cat )
1476 audio_replay_gain_t rg;
1478 es->i_channel = p_sys->i_audio;
1480 memset( &rg, 0, sizeof(rg) );
1481 vlc_mutex_lock( &p_input->p->p_item->lock );
1482 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1483 vlc_mutex_unlock( &p_input->p->p_item->lock );
1485 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1487 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1489 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1490 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1492 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1494 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1495 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1502 es->i_channel = p_sys->i_video;
1503 if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1504 vlc_ureduce( &es->fmt.video.i_frame_rate,
1505 &es->fmt.video.i_frame_rate_base,
1506 es->fmt.video.i_frame_rate,
1507 es->fmt.video.i_frame_rate_base, 0 );
1511 es->i_channel = p_sys->i_sub;
1518 es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1519 es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1521 es->p_dec_record = NULL;
1522 for( i = 0; i < 4; i++ )
1523 es->pb_cc_present[i] = false;
1524 es->p_master = NULL;
1526 if( es->p_pgrm == p_sys->p_pgrm )
1527 EsOutESVarUpdate( out, es, false );
1529 /* Select it if needed */
1530 EsOutSelect( out, es, false );
1533 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1534 p_sys->i_id++; /* always incremented */
1535 switch( es->fmt.i_cat )
1548 EsOutUpdateInfo( out, es, &es->fmt, NULL );
1550 if( es->b_scrambled )
1551 EsOutProgramUpdateScrambled( out, es->p_pgrm );
1553 vlc_mutex_unlock( &p_sys->lock );
1558 static bool EsIsSelected( es_out_id_t *es )
1562 bool b_decode = false;
1563 if( es->p_master->p_dec )
1565 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1566 if( i_channel != -1 )
1567 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1573 return es->p_dec != NULL;
1576 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1578 es_out_sys_t *p_sys = out->p_sys;
1579 input_thread_t *p_input = p_sys->p_input;
1581 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1584 if( p_sys->b_buffering )
1585 input_DecoderStartWait( p_es->p_dec );
1587 if( !p_es->p_master && p_sys->p_sout_record )
1589 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1590 if( p_es->p_dec_record && p_sys->b_buffering )
1591 input_DecoderStartWait( p_es->p_dec_record );
1595 EsOutDecoderChangeDelay( out, p_es );
1598 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1605 input_DecoderDelete( p_es->p_dec );
1608 if( p_es->p_dec_record )
1610 input_DecoderDelete( p_es->p_dec_record );
1611 p_es->p_dec_record = NULL;
1615 static void EsSelect( es_out_t *out, es_out_id_t *es )
1617 es_out_sys_t *p_sys = out->p_sys;
1618 input_thread_t *p_input = p_sys->p_input;
1620 if( EsIsSelected( es ) )
1622 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1629 if( !es->p_master->p_dec )
1632 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1633 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1638 const bool b_sout = p_input->p->p_sout != NULL;
1639 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1641 if( !var_GetBool( p_input, b_sout ? "sout-video" : "video" ) )
1643 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1648 else if( es->fmt.i_cat == AUDIO_ES )
1650 if( !var_GetBool( p_input, b_sout ? "sout-audio" : "audio" ) )
1652 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1657 if( es->fmt.i_cat == SPU_ES )
1659 if( !var_GetBool( p_input, b_sout ? "sout-spu" : "spu" ) )
1661 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1667 EsCreateDecoder( out, es );
1669 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1673 /* Mark it as selected */
1674 input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1675 input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1678 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1680 es_out_sys_t *p_sys = out->p_sys;
1681 input_thread_t *p_input = p_sys->p_input;
1683 if( !EsIsSelected( es ) )
1685 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1691 if( es->p_master->p_dec )
1693 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1694 if( i_channel != -1 )
1695 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1700 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1702 for( i = 0; i < 4; i++ )
1704 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1707 if( i_spu_id == es->pp_cc_es[i]->i_id )
1709 /* Force unselection of the CC */
1710 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1712 EsOutDel( out, es->pp_cc_es[i] );
1714 es->pb_cc_present[i] = false;
1716 EsDestroyDecoder( out, es );
1722 /* Mark it as unselected */
1723 input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1724 if( EsFmtIsTeletext( &es->fmt ) )
1725 input_SendEventTeletextSelect( p_input, -1 );
1729 * Select an ES given the current mode
1730 * XXX: you need to take a the lock before (stream.stream_lock)
1732 * \param out The es_out structure
1733 * \param es es_out_id structure
1734 * \param b_force ...
1737 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1739 es_out_sys_t *p_sys = out->p_sys;
1741 int i_cat = es->fmt.i_cat;
1743 if( !p_sys->b_active ||
1744 ( !b_force && es->fmt.i_priority < ES_PRIORITY_SELECTABLE_MIN ) )
1749 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1751 if( !EsIsSelected( es ) )
1752 EsSelect( out, es );
1754 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1756 char *prgms = var_GetNonEmptyString( p_sys->p_input, "programs" );
1761 for ( const char *prgm = strtok_r( prgms, ",", &buf );
1763 prgm = strtok_r( NULL, ",", &buf ) )
1765 if( atoi( prgm ) == es->p_pgrm->i_id || b_force )
1767 if( !EsIsSelected( es ) )
1768 EsSelect( out, es );
1775 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1779 if( es->p_pgrm != p_sys->p_pgrm )
1782 if( i_cat == AUDIO_ES )
1784 if( p_sys->ppsz_audio_language )
1786 int es_idx = LanguageArrayIndex( p_sys->ppsz_audio_language,
1787 es->psz_language_code );
1788 if( !p_sys->p_es_audio )
1790 /* Only select the language if it's in the list */
1792 i_wanted = es->i_channel;
1796 int selected_es_idx =
1797 LanguageArrayIndex( p_sys->ppsz_audio_language,
1798 p_sys->p_es_audio->psz_language_code );
1800 ( selected_es_idx < 0 || es_idx < selected_es_idx ||
1801 ( es_idx == selected_es_idx &&
1802 p_sys->p_es_audio->fmt.i_priority < es->fmt.i_priority ) ) )
1803 i_wanted = es->i_channel;
1808 /* Select the first one if there is no selected audio yet
1809 * then choose by ES priority */
1810 if( !p_sys->p_es_audio ||
1811 p_sys->p_es_audio->fmt.i_priority < es->fmt.i_priority )
1812 i_wanted = es->i_channel;
1815 if( p_sys->i_audio_last >= 0 )
1816 i_wanted = p_sys->i_audio_last;
1818 if( p_sys->i_audio_id >= 0 )
1820 if( es->i_id == p_sys->i_audio_id )
1821 i_wanted = es->i_channel;
1826 else if( i_cat == SPU_ES )
1828 if( p_sys->ppsz_sub_language )
1830 int es_idx = LanguageArrayIndex( p_sys->ppsz_sub_language,
1831 es->psz_language_code );
1832 if( !p_sys->p_es_sub )
1834 /* Select the language if it's in the list */
1836 /*FIXME: Should default subtitle not in the list be
1837 * displayed if not forbidden by none? */
1838 ( p_sys->i_default_sub_id >= 0 &&
1839 /* check if the subtitle isn't forbidden by none */
1840 LanguageArrayIndex( p_sys->ppsz_sub_language, "none" ) < 0 &&
1841 es->i_id == p_sys->i_default_sub_id ) )
1842 i_wanted = es->i_channel;
1846 int selected_es_idx =
1847 LanguageArrayIndex( p_sys->ppsz_sub_language,
1848 p_sys->p_es_sub->psz_language_code );
1851 ( selected_es_idx < 0 || es_idx < selected_es_idx ||
1852 ( es_idx == selected_es_idx &&
1853 p_sys->p_es_sub->fmt.i_priority < es->fmt.i_priority ) ) )
1854 i_wanted = es->i_channel;
1859 /* If there is no user preference, select the default subtitle
1860 * or adapt by ES priority */
1861 if( ( !p_sys->p_es_sub &&
1862 ( p_sys->i_default_sub_id >= 0 &&
1863 es->i_id == p_sys->i_default_sub_id ) ) ||
1864 ( p_sys->p_es_sub &&
1865 p_sys->p_es_sub->fmt.i_priority < es->fmt.i_priority ) )
1866 i_wanted = es->i_channel;
1869 if( p_sys->i_sub_last >= 0 )
1870 i_wanted = p_sys->i_sub_last;
1872 if( p_sys->i_sub_id >= 0 )
1874 if( es->i_id == p_sys->i_sub_id )
1875 i_wanted = es->i_channel;
1880 else if( i_cat == VIDEO_ES )
1882 i_wanted = es->i_channel;
1885 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1886 EsSelect( out, es );
1889 /* FIXME TODO handle priority here */
1890 if( EsIsSelected( es ) )
1892 if( i_cat == AUDIO_ES )
1894 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1895 p_sys->p_es_audio &&
1896 p_sys->p_es_audio != es &&
1897 EsIsSelected( p_sys->p_es_audio ) )
1899 EsUnselect( out, p_sys->p_es_audio, false );
1901 p_sys->p_es_audio = es;
1903 else if( i_cat == SPU_ES )
1905 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1907 p_sys->p_es_sub != es &&
1908 EsIsSelected( p_sys->p_es_sub ) )
1910 EsUnselect( out, p_sys->p_es_sub, false );
1912 p_sys->p_es_sub = es;
1914 else if( i_cat == VIDEO_ES )
1916 p_sys->p_es_video = es;
1922 * Send a block for the given es_out
1924 * \param out the es_out to send from
1925 * \param es the es_out_id
1926 * \param p_block the data block to send
1928 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1930 es_out_sys_t *p_sys = out->p_sys;
1931 input_thread_t *p_input = p_sys->p_input;
1933 if( libvlc_stats( p_input ) )
1937 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1938 stats_Update( p_input->p->counters.p_demux_read,
1939 p_block->i_buffer, &i_total );
1940 stats_Update( p_input->p->counters.p_demux_bitrate, i_total, NULL );
1942 /* Update number of corrupted data packats */
1943 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
1945 stats_Update( p_input->p->counters.p_demux_corrupted, 1, NULL );
1947 /* Update number of discontinuities */
1948 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1950 stats_Update( p_input->p->counters.p_demux_discontinuity, 1, NULL );
1952 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1955 vlc_mutex_lock( &p_sys->lock );
1957 /* Mark preroll blocks */
1958 if( p_sys->i_preroll_end >= 0 )
1960 int64_t i_date = p_block->i_pts;
1961 if( p_block->i_pts <= VLC_TS_INVALID )
1962 i_date = p_block->i_dts;
1964 if( i_date < p_sys->i_preroll_end )
1965 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1970 block_Release( p_block );
1971 vlc_mutex_unlock( &p_sys->lock );
1975 /* Check for sout mode */
1976 if( p_input->p->p_sout )
1978 /* FIXME review this, proper lock may be missing */
1979 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 &&
1980 p_input->p->b_out_pace_control )
1982 msg_Dbg( p_input, "switching to sync mode" );
1983 p_input->p->b_out_pace_control = false;
1985 else if( p_input->p->p_sout->i_out_pace_nocontrol <= 0 &&
1986 !p_input->p->b_out_pace_control )
1988 msg_Dbg( p_input, "switching to async mode" );
1989 p_input->p->b_out_pace_control = true;
1994 if( es->p_dec_record )
1996 block_t *p_dup = block_Duplicate( p_block );
1998 input_DecoderDecode( es->p_dec_record, p_dup,
1999 p_input->p->b_out_pace_control );
2001 input_DecoderDecode( es->p_dec, p_block,
2002 p_input->p->b_out_pace_control );
2004 es_format_t fmt_dsc;
2005 vlc_meta_t *p_meta_dsc;
2006 if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
2008 EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
2010 es_format_Clean( &fmt_dsc );
2012 vlc_meta_Delete( p_meta_dsc );
2015 /* Check CC status */
2018 input_DecoderIsCcPresent( es->p_dec, pb_cc );
2019 for( int i = 0; i < 4; i++ )
2023 if( es->pb_cc_present[i] || !pb_cc[i] )
2025 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
2027 es_format_Init( &fmt, SPU_ES, EsOutFourccClosedCaptions[i] );
2028 fmt.i_group = es->fmt.i_group;
2029 if( asprintf( &fmt.psz_description,
2030 _("Closed captions %u"), 1 + i ) == -1 )
2031 fmt.psz_description = NULL;
2032 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
2033 es->pp_cc_es[i]->p_master = es;
2034 es_format_Clean( &fmt );
2037 es->pb_cc_present[i] = true;
2040 vlc_mutex_unlock( &p_sys->lock );
2045 /*****************************************************************************
2047 *****************************************************************************/
2048 static void EsOutDel( es_out_t *out, es_out_id_t *es )
2050 es_out_sys_t *p_sys = out->p_sys;
2051 bool b_reselect = false;
2054 vlc_mutex_lock( &p_sys->lock );
2056 /* We don't try to reselect */
2059 while( vlc_object_alive(p_sys->p_input) && !p_sys->b_buffering && es->p_dec )
2061 if( input_DecoderIsEmpty( es->p_dec ) &&
2062 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
2064 /* FIXME there should be a way to have auto deleted es, but there will be
2065 * a problem when another codec of the same type is created (mainly video) */
2068 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2071 if( es->p_pgrm == p_sys->p_pgrm )
2072 EsOutESVarUpdate( out, es, true );
2074 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
2076 /* Update program */
2078 if( es->p_pgrm->i_es == 0 )
2079 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
2081 if( es->b_scrambled )
2082 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2085 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
2086 p_sys->p_es_sub == es ) b_reselect = true;
2088 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
2089 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
2090 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
2092 switch( es->fmt.i_cat )
2105 /* Re-select another track when needed */
2108 for( i = 0; i < p_sys->i_es; i++ )
2110 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
2111 EsOutSelect( out, p_sys->es[i], false );
2115 free( es->psz_language );
2116 free( es->psz_language_code );
2118 es_format_Clean( &es->fmt );
2120 vlc_mutex_unlock( &p_sys->lock );
2126 * Control query handler
2128 * \param out the es_out to control
2129 * \param i_query A es_out query as defined in include/ninput.h
2130 * \param args a variable list of arguments for the query
2131 * \return VLC_SUCCESS or an error code
2133 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2135 es_out_sys_t *p_sys = out->p_sys;
2139 case ES_OUT_SET_ES_STATE:
2141 es_out_id_t *es = va_arg( args, es_out_id_t * );
2142 bool b = va_arg( args, int );
2143 if( b && !EsIsSelected( es ) )
2145 EsSelect( out, es );
2146 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2148 else if( !b && EsIsSelected( es ) )
2150 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2156 case ES_OUT_GET_ES_STATE:
2158 es_out_id_t *es = va_arg( args, es_out_id_t * );
2159 bool *pb = va_arg( args, bool * );
2160 bool *pb_error = va_arg( args, bool *);
2162 *pb = EsIsSelected( es );
2163 *pb_error = (es->p_dec ? atomic_load( &es->p_dec->b_error ) : false);
2167 case ES_OUT_GET_GROUP_FORCED:
2169 int *pi_group = va_arg( args, int * );
2170 *pi_group = p_sys->i_group_id;
2174 case ES_OUT_SET_MODE:
2176 const int i_mode = va_arg( args, int );
2177 assert( i_mode == ES_OUT_MODE_NONE || i_mode == ES_OUT_MODE_ALL ||
2178 i_mode == ES_OUT_MODE_AUTO || i_mode == ES_OUT_MODE_PARTIAL ||
2179 i_mode == ES_OUT_MODE_END );
2181 if( i_mode != ES_OUT_MODE_NONE && !p_sys->b_active && p_sys->i_es > 0 )
2183 /* XXX Terminate vout if there are tracks but no video one.
2184 * This one is not mandatory but is he earliest place where it
2187 for( i = 0; i < p_sys->i_es; i++ )
2189 es_out_id_t *p_es = p_sys->es[i];
2190 if( p_es->fmt.i_cat == VIDEO_ES )
2193 if( i >= p_sys->i_es )
2194 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2196 p_sys->b_active = i_mode != ES_OUT_MODE_NONE;
2197 p_sys->i_mode = i_mode;
2199 /* Reapply policy mode */
2200 for( int i = 0; i < p_sys->i_es; i++ )
2202 if( EsIsSelected( p_sys->es[i] ) )
2203 EsUnselect( out, p_sys->es[i],
2204 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2206 for( int i = 0; i < p_sys->i_es; i++ )
2207 EsOutSelect( out, p_sys->es[i], false );
2208 if( i_mode == ES_OUT_MODE_END )
2209 EsOutTerminate( out );
2214 case ES_OUT_RESTART_ES:
2216 es_out_id_t *es = va_arg( args, es_out_id_t * );
2221 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2223 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2225 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2230 for( int i = 0; i < p_sys->i_es; i++ )
2234 if( es == p_sys->es[i] )
2236 EsOutSelect( out, es, true );
2242 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2244 if( EsIsSelected( p_sys->es[i] ) )
2246 if( i_query == ES_OUT_RESTART_ES )
2248 if( p_sys->es[i]->p_dec )
2250 EsDestroyDecoder( out, p_sys->es[i] );
2251 EsCreateDecoder( out, p_sys->es[i] );
2256 EsUnselect( out, p_sys->es[i],
2257 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2266 case ES_OUT_SET_ES_DEFAULT:
2268 es_out_id_t *es = va_arg( args, es_out_id_t * );
2272 /*p_sys->i_default_video_id = -1;*/
2273 /*p_sys->i_default_audio_id = -1;*/
2274 p_sys->i_default_sub_id = -1;
2276 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2278 /*p_sys->i_default_video_id = -1;*/
2280 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2282 /*p_sys->i_default_audio_id = -1;*/
2284 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2286 p_sys->i_default_sub_id = -1;
2290 /*if( es->fmt.i_cat == VIDEO_ES )
2291 p_sys->i_default_video_id = es->i_id;
2293 if( es->fmt.i_cat == AUDIO_ES )
2294 p_sys->i_default_audio_id = es->i_id;
2296 if( es->fmt.i_cat == SPU_ES )
2297 p_sys->i_default_sub_id = es->i_id;
2302 case ES_OUT_SET_PCR:
2303 case ES_OUT_SET_GROUP_PCR:
2305 es_out_pgrm_t *p_pgrm = NULL;
2309 /* Search program */
2310 if( i_query == ES_OUT_SET_PCR )
2312 p_pgrm = p_sys->p_pgrm;
2314 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2318 i_group = (int)va_arg( args, int );
2319 p_pgrm = EsOutProgramFind( out, i_group );
2322 return VLC_EGENERIC;
2324 i_pcr = (int64_t)va_arg( args, int64_t );
2325 if( i_pcr <= VLC_TS_INVALID )
2327 msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
2328 return VLC_EGENERIC;
2331 /* TODO do not use mdate() but proper stream acquisition date */
2333 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2335 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering,
2336 EsOutIsExtraBufferingAllowed( out ),
2339 if( !p_sys->p_pgrm )
2342 if( p_sys->b_buffering )
2344 /* Check buffering state on master clock update */
2345 EsOutDecodersStopBuffering( out, false );
2347 else if( p_pgrm == p_sys->p_pgrm )
2349 if( b_late && ( !p_sys->p_input->p->p_sout ||
2350 !p_sys->p_input->p->b_out_pace_control ) )
2352 const mtime_t i_pts_delay_base = p_sys->i_pts_delay - p_sys->i_pts_jitter;
2353 mtime_t i_pts_delay = input_clock_GetJitter( p_pgrm->p_clock );
2355 /* Avoid dangerously high value */
2356 const mtime_t i_jitter_max = INT64_C(1000) * var_InheritInteger( p_sys->p_input, "clock-jitter" );
2357 if( i_pts_delay > __MIN( i_pts_delay_base + i_jitter_max, INPUT_PTS_DELAY_MAX ) )
2359 msg_Err( p_sys->p_input,
2360 "ES_OUT_SET_(GROUP_)PCR is called too late (jitter of %d ms ignored)",
2361 (int)(i_pts_delay - i_pts_delay_base) / 1000 );
2362 i_pts_delay = p_sys->i_pts_delay;
2365 for( int i = 0; i < p_sys->i_pgrm; i++ )
2366 input_clock_Reset( p_sys->pgrm[i]->p_clock );
2370 msg_Err( p_sys->p_input,
2371 "ES_OUT_SET_(GROUP_)PCR is called too late (pts_delay increased to %d ms)",
2372 (int)(i_pts_delay/1000) );
2374 /* Force a rebufferization when we are too late */
2376 /* It is not really good, as we throw away already buffered data
2377 * TODO have a mean to correctly reenter bufferization */
2378 es_out_Control( out, ES_OUT_RESET_PCR );
2381 es_out_SetJitter( out, i_pts_delay_base, i_pts_delay - i_pts_delay_base, p_sys->i_cr_average );
2387 case ES_OUT_RESET_PCR:
2388 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2389 EsOutChangePosition( out );
2392 case ES_OUT_SET_GROUP:
2394 int i = va_arg( args, int );
2395 for( int j = 0; j < p_sys->i_pgrm; j++ )
2397 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2398 if( p_pgrm->i_id == i )
2400 EsOutProgramSelect( out, p_pgrm );
2404 return VLC_EGENERIC;
2407 case ES_OUT_SET_ES_FMT:
2409 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2410 * to update the p_extra data */
2411 es_out_id_t *es = va_arg( args, es_out_id_t * );
2412 es_format_t *p_fmt = va_arg( args, es_format_t * );
2414 return VLC_EGENERIC;
2416 if( p_fmt->i_extra )
2418 es->fmt.i_extra = p_fmt->i_extra;
2419 es->fmt.p_extra = xrealloc( es->fmt.p_extra, p_fmt->i_extra );
2420 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2425 EsDestroyDecoder( out, es );
2427 EsCreateDecoder( out, es );
2429 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2430 es->p_dec->fmt_in.p_extra =
2431 xrealloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2432 memcpy( es->p_dec->fmt_in.p_extra,
2433 p_fmt->p_extra, p_fmt->i_extra );
2440 case ES_OUT_SET_ES_SCRAMBLED_STATE:
2442 es_out_id_t *es = va_arg( args, es_out_id_t * );
2443 bool b_scrambled = (bool)va_arg( args, int );
2445 if( !es->b_scrambled != !b_scrambled )
2447 es->b_scrambled = b_scrambled;
2448 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2453 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2455 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2458 return VLC_EGENERIC;
2460 p_sys->i_preroll_end = i_date;
2464 case ES_OUT_SET_GROUP_META:
2466 int i_group = (int)va_arg( args, int );
2467 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2469 EsOutProgramMeta( out, i_group, p_meta );
2472 case ES_OUT_SET_GROUP_EPG:
2474 int i_group = (int)va_arg( args, int );
2475 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
2477 EsOutProgramEpg( out, i_group, p_epg );
2481 case ES_OUT_DEL_GROUP:
2483 int i_group = (int)va_arg( args, int );
2485 return EsOutProgramDel( out, i_group );
2488 case ES_OUT_SET_META:
2490 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
2492 EsOutMeta( out, p_meta );
2496 case ES_OUT_GET_WAKE_UP:
2498 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2499 *pi_wakeup = EsOutGetWakeup( out );
2503 case ES_OUT_SET_ES_BY_ID:
2504 case ES_OUT_RESTART_ES_BY_ID:
2505 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2507 const int i_id = (int)va_arg( args, int );
2508 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2513 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2514 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2515 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2519 /* TODO if the lock is made non recursive it should be changed */
2520 int i_ret = es_out_Control( out, i_new_query, p_es );
2522 /* Clean up vout after user action (in active mode only).
2523 * FIXME it does not work well with multiple video windows */
2524 if( p_sys->b_active )
2525 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2529 case ES_OUT_GET_ES_OBJECTS_BY_ID:
2531 const int i_id = va_arg( args, int );
2532 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2534 return VLC_EGENERIC;
2536 vlc_object_t **pp_decoder = va_arg( args, vlc_object_t ** );
2537 vout_thread_t **pp_vout = va_arg( args, vout_thread_t ** );
2538 audio_output_t **pp_aout = va_arg( args, audio_output_t ** );
2542 *pp_decoder = vlc_object_hold( p_es->p_dec );
2543 input_DecoderGetObjects( p_es->p_dec, pp_vout, pp_aout );
2557 case ES_OUT_GET_BUFFERING:
2559 bool *pb = va_arg( args, bool* );
2560 *pb = p_sys->b_buffering;
2564 case ES_OUT_GET_EMPTY:
2566 bool *pb = va_arg( args, bool* );
2567 *pb = EsOutDecodersIsEmpty( out );
2571 case ES_OUT_SET_DELAY:
2573 const int i_cat = (int)va_arg( args, int );
2574 const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2575 EsOutSetDelay( out, i_cat, i_delay );
2579 case ES_OUT_SET_RECORD_STATE:
2581 bool b = va_arg( args, int );
2582 return EsOutSetRecord( out, b );
2585 case ES_OUT_SET_PAUSE_STATE:
2587 const bool b_source_paused = (bool)va_arg( args, int );
2588 const bool b_paused = (bool)va_arg( args, int );
2589 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2591 assert( !b_source_paused == !b_paused );
2592 EsOutChangePause( out, b_paused, i_date );
2597 case ES_OUT_SET_RATE:
2599 const int i_src_rate = (int)va_arg( args, int );
2600 const int i_rate = (int)va_arg( args, int );
2602 assert( i_src_rate == i_rate );
2603 EsOutChangeRate( out, i_rate );
2608 case ES_OUT_SET_TIME:
2610 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2612 assert( i_date == -1 );
2613 EsOutChangePosition( out );
2618 case ES_OUT_SET_FRAME_NEXT:
2619 EsOutFrameNext( out );
2622 case ES_OUT_SET_TIMES:
2624 double f_position = (double)va_arg( args, double );
2625 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2626 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2628 input_SendEventLength( p_sys->p_input, i_length );
2630 if( !p_sys->b_buffering )
2634 /* Fix for buffering delay */
2635 if( !p_sys->p_input->p->p_sout ||
2636 !p_sys->p_input->p->b_out_pace_control )
2637 i_delay = EsOutGetBuffering( out );
2646 f_position -= (double)i_delay / i_length;
2647 if( f_position < 0 )
2650 input_SendEventPosition( p_sys->p_input, f_position, i_time );
2654 case ES_OUT_SET_JITTER:
2656 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2657 mtime_t i_pts_jitter = (mtime_t)va_arg( args, mtime_t );
2658 int i_cr_average = (int)va_arg( args, int );
2660 bool b_change_clock =
2661 i_pts_delay + i_pts_jitter != p_sys->i_pts_delay ||
2662 i_cr_average != p_sys->i_cr_average;
2664 assert( i_pts_jitter >= 0 );
2665 p_sys->i_pts_delay = i_pts_delay + i_pts_jitter;
2666 p_sys->i_pts_jitter = i_pts_jitter;
2667 p_sys->i_cr_average = i_cr_average;
2669 for( int i = 0; i < p_sys->i_pgrm && b_change_clock; i++ )
2670 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2671 i_pts_delay + i_pts_jitter, i_cr_average );
2675 case ES_OUT_GET_PCR_SYSTEM:
2677 if( p_sys->b_buffering )
2678 return VLC_EGENERIC;
2680 es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
2682 return VLC_EGENERIC;
2684 mtime_t *pi_system = va_arg( args, mtime_t *);
2685 mtime_t *pi_delay = va_arg( args, mtime_t *);
2686 input_clock_GetSystemOrigin( p_pgrm->p_clock, pi_system, pi_delay );
2690 case ES_OUT_MODIFY_PCR_SYSTEM:
2692 if( p_sys->b_buffering )
2693 return VLC_EGENERIC;
2695 es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
2697 return VLC_EGENERIC;
2699 const bool b_absolute = va_arg( args, int );
2700 const mtime_t i_system = va_arg( args, mtime_t );
2701 input_clock_ChangeSystemOrigin( p_pgrm->p_clock, b_absolute, i_system );
2704 case ES_OUT_SET_EOS:
2706 for (int i = 0; i < p_sys->i_es; i++) {
2707 es_out_id_t *id = p_sys->es[i];
2708 decoder_t *p_dec = id->p_dec;
2711 block_t *p_block = block_Alloc(0);
2715 p_block->i_flags |= BLOCK_FLAG_CORE_EOS;
2716 input_DecoderDecode(p_dec, p_block, false);
2722 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2723 return VLC_EGENERIC;
2727 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2729 es_out_sys_t *p_sys = out->p_sys;
2732 vlc_mutex_lock( &p_sys->lock );
2733 i_ret = EsOutControlLocked( out, i_query, args );
2734 vlc_mutex_unlock( &p_sys->lock );
2739 /****************************************************************************
2740 * LanguageGetName: try to expend iso639 into plain name
2741 ****************************************************************************/
2742 static char *LanguageGetName( const char *psz_code )
2744 const iso639_lang_t *pl;
2746 if( psz_code == NULL || !strcmp( psz_code, "und" ) )
2748 return strdup( "" );
2751 if( strlen( psz_code ) == 2 )
2753 pl = GetLang_1( psz_code );
2755 else if( strlen( psz_code ) == 3 )
2757 pl = GetLang_2B( psz_code );
2758 if( !strcmp( pl->psz_iso639_1, "??" ) )
2760 pl = GetLang_2T( psz_code );
2765 return strdup( psz_code );
2768 if( !strcmp( pl->psz_iso639_1, "??" ) )
2770 return strdup( psz_code );
2774 return strdup( vlc_gettext(pl->psz_eng_name) );
2778 /* Get a 2 char code */
2779 static char *LanguageGetCode( const char *psz_lang )
2781 const iso639_lang_t *pl;
2783 if( psz_lang == NULL || *psz_lang == '\0' )
2784 return strdup("??");
2786 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
2788 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2789 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2790 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2791 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2792 return strdup( pl->psz_iso639_1 );
2795 return strdup("??");
2798 static char **LanguageSplit( const char *psz_langs, bool b_default_any )
2805 if( psz_langs == NULL ) return NULL;
2807 psz_parser = psz_dup = strdup(psz_langs);
2809 while( psz_parser && *psz_parser )
2814 psz = strchr(psz_parser, ',' );
2815 if( psz ) *psz++ = '\0';
2817 if( !strcmp( psz_parser, "any" ) )
2819 TAB_APPEND( i_psz, ppsz, strdup("any") );
2821 else if( !strcmp( psz_parser, "none" ) )
2823 TAB_APPEND( i_psz, ppsz, strdup("none") );
2827 psz_code = LanguageGetCode( psz_parser );
2828 if( strcmp( psz_code, "??" ) )
2830 TAB_APPEND( i_psz, ppsz, psz_code );
2843 if( b_default_any && strcmp( ppsz[i_psz - 1], "none" ) )
2844 TAB_APPEND( i_psz, ppsz, strdup("any") );
2845 TAB_APPEND( i_psz, ppsz, NULL );
2852 static int LanguageArrayIndex( char **ppsz_langs, const char *psz_lang )
2854 if( !ppsz_langs || !psz_lang )
2857 for( int i = 0; ppsz_langs[i]; i++ )
2859 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2860 !strcasecmp( ppsz_langs[i], "any" ) )
2862 if( !strcasecmp( ppsz_langs[i], "none" ) )
2869 /****************************************************************************
2871 * - add meta info to the playlist item
2872 ****************************************************************************/
2873 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2875 es_out_sys_t *p_sys = out->p_sys;
2876 input_thread_t *p_input = p_sys->p_input;
2877 const es_format_t *p_fmt_es = &es->fmt;
2880 if( es->fmt.i_cat == fmt->i_cat )
2882 es_format_t update = *fmt;
2883 update.i_id = es->i_meta_id;
2884 update.i_codec = es->fmt.i_codec;
2885 update.i_original_fourcc = es->fmt.i_original_fourcc;
2886 input_item_UpdateTracksInfo(input_GetItem(p_input), &update);
2889 /* Create category */
2891 snprintf( psz_cat, sizeof(psz_cat),_("Stream %d"), es->i_meta_id );
2892 info_category_t *p_cat = info_category_New( psz_cat );
2896 /* Add information */
2897 const char *psz_type;
2898 switch( fmt->i_cat )
2901 psz_type = _("Audio");
2904 psz_type = _("Video");
2907 psz_type = _("Subtitle");
2915 info_category_AddInfo( p_cat, _("Type"), "%s", psz_type );
2917 if( es->i_meta_id != es->i_id )
2918 info_category_AddInfo( p_cat, _("Original ID"),
2921 const char *psz_codec_description =
2922 vlc_fourcc_GetDescription( p_fmt_es->i_cat, p_fmt_es->i_codec );
2923 const vlc_fourcc_t i_codec_fourcc = ( p_fmt_es->i_original_fourcc )?
2924 p_fmt_es->i_original_fourcc : p_fmt_es->i_codec;
2925 if( psz_codec_description && *psz_codec_description )
2926 info_category_AddInfo( p_cat, _("Codec"), "%s (%.4s)",
2927 psz_codec_description, (char*)&i_codec_fourcc );
2928 else if ( i_codec_fourcc != VLC_FOURCC(0,0,0,0) )
2929 info_category_AddInfo( p_cat, _("Codec"), "%.4s",
2930 (char*)&i_codec_fourcc );
2932 if( es->psz_language && *es->psz_language )
2933 info_category_AddInfo( p_cat, _("Language"), "%s",
2935 if( fmt->psz_description && *fmt->psz_description )
2936 info_category_AddInfo( p_cat, _("Description"), "%s",
2937 fmt->psz_description );
2939 switch( fmt->i_cat )
2942 info_category_AddInfo( p_cat, _("Type"), _("Audio") );
2944 if( fmt->audio.i_physical_channels )
2945 info_category_AddInfo( p_cat, _("Channels"), "%s",
2946 _( aout_FormatPrintChannels( &fmt->audio ) ) );
2948 if( fmt->audio.i_rate > 0 )
2950 info_category_AddInfo( p_cat, _("Sample rate"), _("%u Hz"),
2951 fmt->audio.i_rate );
2952 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2953 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2956 unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2957 if( i_bitspersample <= 0 )
2958 i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2959 if( i_bitspersample > 0 )
2960 info_category_AddInfo( p_cat, _("Bits per sample"), "%u",
2963 if( fmt->i_bitrate > 0 )
2965 info_category_AddInfo( p_cat, _("Bitrate"), _("%u kb/s"),
2966 fmt->i_bitrate / 1000 );
2967 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2968 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2970 for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2972 const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2973 if( !p_rg->pb_gain[i] )
2975 const char *psz_name;
2976 if( i == AUDIO_REPLAY_GAIN_TRACK )
2977 psz_name = _("Track replay gain");
2979 psz_name = _("Album replay gain");
2980 info_category_AddInfo( p_cat, psz_name, _("%.2f dB"),
2986 info_category_AddInfo( p_cat, _("Type"), _("Video") );
2988 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2989 info_category_AddInfo( p_cat, _("Resolution"), "%ux%u",
2990 fmt->video.i_width, fmt->video.i_height );
2992 if( fmt->video.i_visible_width > 0 &&
2993 fmt->video.i_visible_height > 0 )
2994 info_category_AddInfo( p_cat, _("Display resolution"), "%ux%u",
2995 fmt->video.i_visible_width,
2996 fmt->video.i_visible_height);
2997 if( fmt->video.i_frame_rate > 0 &&
2998 fmt->video.i_frame_rate_base > 0 )
3000 div = lldiv( (float)fmt->video.i_frame_rate /
3001 fmt->video.i_frame_rate_base * 1000000,
3004 info_category_AddInfo( p_cat, _("Frame rate"), "%"PRId64".%06u",
3005 div.quot, (unsigned int )div.rem );
3007 info_category_AddInfo( p_cat, _("Frame rate"), "%"PRId64,
3010 if( fmt->i_codec != p_fmt_es->i_codec )
3012 const char *psz_chroma_description =
3013 vlc_fourcc_GetDescription( VIDEO_ES, fmt->i_codec );
3014 if( psz_chroma_description )
3015 info_category_AddInfo( p_cat, _("Decoded format"), "%s",
3016 psz_chroma_description );
3022 info_category_AddInfo( p_cat, _("Type"), _("Subtitle") );
3029 /* Append generic meta */
3032 char **ppsz_all_keys = vlc_meta_CopyExtraNames( p_meta );
3033 for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
3035 char *psz_key = ppsz_all_keys[i];
3036 const char *psz_value = vlc_meta_GetExtra( p_meta, psz_key );
3039 info_category_AddInfo( p_cat, vlc_gettext(psz_key), "%s",
3040 vlc_gettext(psz_value) );
3043 free( ppsz_all_keys );
3046 input_Control( p_input, INPUT_REPLACE_INFOS, p_cat );