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>
41 #include "input_internal.h"
47 #include "../stream_output/stream_output.h"
49 #include <vlc_iso_lang.h>
50 /* FIXME we should find a better way than including that */
51 #include "../text/iso-639_def.h"
53 /*****************************************************************************
55 *****************************************************************************/
61 /* Number of es for this pgrm */
67 /* Clock for this program */
68 input_clock_t *p_clock;
71 char *psz_now_playing;
81 es_out_pgrm_t *p_pgrm;
86 /* Channel in the track type */
90 char *psz_language_code;
93 decoder_t *p_dec_record;
95 /* Fields for Video with CC */
96 bool pb_cc_present[4];
97 es_out_id_t *pp_cc_es[4];
99 /* Field for CC track from a master video */
100 es_out_id_t *p_master;
102 /* ID for the meta data */
108 input_thread_t *p_input;
115 es_out_pgrm_t **pgrm;
116 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
117 es_out_pgrm_t *p_pgrm; /* Master program */
134 int i_audio_last, i_audio_id;
135 int i_sub_last, i_sub_id;
136 int i_default_sub_id; /* As specified in container; if applicable */
137 char **ppsz_audio_language;
138 char **ppsz_sub_language;
140 /* current main es */
141 es_out_id_t *p_es_audio;
142 es_out_id_t *p_es_video;
143 es_out_id_t *p_es_sub;
146 int64_t i_audio_delay;
149 /* Clock configuration */
156 mtime_t i_pause_date;
158 /* Current preroll */
159 mtime_t i_preroll_end;
161 /* Used for buffering */
163 mtime_t i_buffering_extra_initial;
164 mtime_t i_buffering_extra_stream;
165 mtime_t i_buffering_extra_system;
168 sout_instance_t *p_sout_record;
171 static es_out_id_t *EsOutAdd ( es_out_t *, const es_format_t * );
172 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
173 static void EsOutDel ( es_out_t *, es_out_id_t * );
174 static int EsOutControl( es_out_t *, int i_query, va_list );
175 static void EsOutDelete ( es_out_t * );
177 static void EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
178 static void EsOutUpdateInfo( es_out_t *, es_out_id_t *es, const es_format_t *, const vlc_meta_t * );
179 static int EsOutSetRecord( es_out_t *, bool b_record );
181 static bool EsIsSelected( es_out_id_t *es );
182 static void EsSelect( es_out_t *out, es_out_id_t *es );
183 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
184 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
185 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
186 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
187 static void EsOutProgramsChangeRate( es_out_t *out );
188 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_FOURCC( 't', 'e', 'l', 'x' );
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 );
371 vlc_epg_Delete( p_pgrm->p_epg );
375 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
376 vlc_mutex_destroy( &p_sys->lock );
382 static mtime_t EsOutGetWakeup( es_out_t *out )
384 es_out_sys_t *p_sys = out->p_sys;
385 input_thread_t *p_input = p_sys->p_input;
390 /* We do not have a wake up date if the input cannot have its speed
391 * controlled or sout is imposing its own or while buffering
393 * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
394 if( !p_input->p->b_can_pace_control ||
395 p_input->p->b_out_pace_control ||
399 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
402 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
407 /* Special HACK, -i_id is the cat of the stream */
408 return (es_out_id_t*)((uint8_t*)NULL-i_id);
411 for( i = 0; i < out->p_sys->i_es; i++ )
413 if( out->p_sys->es[i]->i_id == i_id )
414 return out->p_sys->es[i];
419 static bool EsOutDecodersIsEmpty( es_out_t *out )
421 es_out_sys_t *p_sys = out->p_sys;
424 if( p_sys->b_buffering && p_sys->p_pgrm )
426 EsOutDecodersStopBuffering( out, true );
427 if( p_sys->b_buffering )
431 for( i = 0; i < p_sys->i_es; i++ )
433 es_out_id_t *es = p_sys->es[i];
435 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
437 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
443 static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
445 es_out_sys_t *p_sys = out->p_sys;
447 if( i_cat == AUDIO_ES )
448 p_sys->i_audio_delay = i_delay;
449 else if( i_cat == SPU_ES )
450 p_sys->i_spu_delay = i_delay;
452 for( int i = 0; i < p_sys->i_es; i++ )
453 EsOutDecoderChangeDelay( out, p_sys->es[i] );
456 static int EsOutSetRecord( es_out_t *out, bool b_record )
458 es_out_sys_t *p_sys = out->p_sys;
459 input_thread_t *p_input = p_sys->p_input;
461 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
465 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
466 if( !psz_path || *psz_path == '\0' )
469 psz_path = strdup( config_GetHomeDir() );
472 char *psz_sout = NULL; // TODO conf
474 if( !psz_sout && psz_path )
476 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
479 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
490 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
494 if( !p_sys->p_sout_record )
497 for( int i = 0; i < p_sys->i_es; i++ )
499 es_out_id_t *p_es = p_sys->es[i];
501 if( !p_es->p_dec || p_es->p_master )
504 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
505 if( p_es->p_dec_record && p_sys->b_buffering )
506 input_DecoderStartBuffering( p_es->p_dec_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_record )
518 input_DecoderDelete( p_es->p_dec_record );
519 p_es->p_dec_record = NULL;
522 sout_DeleteInstance( p_sys->p_sout_record );
524 p_sys->p_sout_record = NULL;
529 static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
531 es_out_sys_t *p_sys = out->p_sys;
533 /* XXX the order is important */
536 EsOutDecodersChangePause( out, true, i_date );
537 EsOutProgramChangePause( out, true, i_date );
541 if( p_sys->i_buffering_extra_initial > 0 )
543 mtime_t i_stream_start;
544 mtime_t i_system_start;
545 mtime_t i_stream_duration;
546 mtime_t i_system_duration;
548 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
549 &i_stream_start, &i_system_start,
550 &i_stream_duration, &i_system_duration );
553 /* FIXME pcr != exactly what wanted */
554 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;
557 p_sys->i_buffering_extra_initial = 0;
558 p_sys->i_buffering_extra_stream = 0;
559 p_sys->i_buffering_extra_system = 0;
561 EsOutProgramChangePause( out, false, i_date );
562 EsOutDecodersChangePause( out, false, i_date );
564 EsOutProgramsChangeRate( out );
566 p_sys->b_paused = b_paused;
567 p_sys->i_pause_date = i_date;
570 static void EsOutChangeRate( es_out_t *out, int i_rate )
572 es_out_sys_t *p_sys = out->p_sys;
574 p_sys->i_rate = i_rate;
576 if( !p_sys->b_paused )
577 EsOutProgramsChangeRate( out );
580 static void EsOutChangePosition( es_out_t *out )
582 es_out_sys_t *p_sys = out->p_sys;
584 input_SendEventCache( p_sys->p_input, 0.0 );
586 for( int i = 0; i < p_sys->i_es; i++ )
588 es_out_id_t *p_es = p_sys->es[i];
593 input_DecoderStartBuffering( p_es->p_dec );
595 if( p_es->p_dec_record )
596 input_DecoderStartBuffering( p_es->p_dec_record );
599 for( int i = 0; i < p_sys->i_pgrm; i++ )
600 input_clock_Reset( p_sys->pgrm[i]->p_clock );
602 p_sys->b_buffering = true;
603 p_sys->i_buffering_extra_initial = 0;
604 p_sys->i_buffering_extra_stream = 0;
605 p_sys->i_buffering_extra_system = 0;
606 p_sys->i_preroll_end = -1;
611 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
613 es_out_sys_t *p_sys = out->p_sys;
616 mtime_t i_stream_start;
617 mtime_t i_system_start;
618 mtime_t i_stream_duration;
619 mtime_t i_system_duration;
620 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
621 &i_stream_start, &i_system_start,
622 &i_stream_duration, &i_system_duration );
623 assert( !i_ret || b_forced );
627 mtime_t i_preroll_duration = 0;
628 if( p_sys->i_preroll_end >= 0 )
629 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
631 const mtime_t i_buffering_duration = p_sys->i_pts_delay +
633 p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
635 if( i_stream_duration <= i_buffering_duration && !b_forced )
637 const double f_level = (double)i_stream_duration / i_buffering_duration;
638 input_SendEventCache( p_sys->p_input, f_level );
640 msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
643 input_SendEventCache( p_sys->p_input, 1.0 );
645 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
646 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
647 p_sys->b_buffering = false;
648 p_sys->i_preroll_end = -1;
650 if( p_sys->i_buffering_extra_initial > 0 )
656 const mtime_t i_decoder_buffering_start = mdate();
657 for( int i = 0; i < p_sys->i_es; i++ )
659 es_out_id_t *p_es = p_sys->es[i];
661 if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
663 input_DecoderWaitBuffering( p_es->p_dec );
664 if( p_es->p_dec_record )
665 input_DecoderWaitBuffering( p_es->p_dec_record );
668 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
669 (int)(mdate() - i_decoder_buffering_start)/1000 );
671 /* Here is a good place to destroy unused vout with every demuxer */
672 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
675 const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
676 const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
678 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_current_date + i_wakeup_delay - i_buffering_duration );
680 for( int i = 0; i < p_sys->i_es; i++ )
682 es_out_id_t *p_es = p_sys->es[i];
687 input_DecoderStopBuffering( p_es->p_dec );
688 if( p_es->p_dec_record )
689 input_DecoderStopBuffering( p_es->p_dec_record );
692 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
694 es_out_sys_t *p_sys = out->p_sys;
696 /* Pause decoders first */
697 for( int i = 0; i < p_sys->i_es; i++ )
699 es_out_id_t *es = p_sys->es[i];
703 input_DecoderChangePause( es->p_dec, b_paused, i_date );
704 if( es->p_dec_record )
705 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
709 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
711 es_out_sys_t *p_sys = out->p_sys;
713 for( int i = 0; i < p_sys->i_pgrm; i++ )
714 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
717 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
719 es_out_sys_t *p_sys = out->p_sys;
722 if( p_es->fmt.i_cat == AUDIO_ES )
723 i_delay = p_sys->i_audio_delay;
724 else if( p_es->fmt.i_cat == SPU_ES )
725 i_delay = p_sys->i_spu_delay;
730 input_DecoderChangeDelay( p_es->p_dec, i_delay );
731 if( p_es->p_dec_record )
732 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
735 static void EsOutProgramsChangeRate( es_out_t *out )
737 es_out_sys_t *p_sys = out->p_sys;
739 for( int i = 0; i < p_sys->i_pgrm; i++ )
740 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
743 static void EsOutFrameNext( es_out_t *out )
745 es_out_sys_t *p_sys = out->p_sys;
746 es_out_id_t *p_es_video = NULL;
748 if( p_sys->b_buffering )
750 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
754 assert( p_sys->b_paused );
756 for( int i = 0; i < p_sys->i_es; i++ )
758 es_out_id_t *p_es = p_sys->es[i];
760 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
769 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
774 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
776 msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
778 if( i_duration <= 0 )
779 i_duration = 40*1000;
781 /* FIXME it is not a clean way ? */
782 if( p_sys->i_buffering_extra_initial <= 0 )
784 mtime_t i_stream_start;
785 mtime_t i_system_start;
786 mtime_t i_stream_duration;
787 mtime_t i_system_duration;
790 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
791 &i_stream_start, &i_system_start,
792 &i_stream_duration, &i_system_duration );
796 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
797 p_sys->i_buffering_extra_system =
798 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
801 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
803 p_sys->b_buffering = true;
804 p_sys->i_buffering_extra_system += i_duration;
805 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
806 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
807 INPUT_RATE_DEFAULT / i_rate;
809 p_sys->i_preroll_end = -1;
811 static mtime_t EsOutGetBuffering( es_out_t *out )
813 es_out_sys_t *p_sys = out->p_sys;
819 mtime_t i_stream_start;
820 mtime_t i_system_start;
821 mtime_t i_stream_duration;
822 mtime_t i_system_duration;
823 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
824 &i_stream_start, &i_system_start,
825 &i_stream_duration, &i_system_duration );
832 if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
834 i_delay = i_stream_duration;
838 mtime_t i_system_duration;
839 if( p_sys->b_paused )
841 i_system_duration = p_sys->i_pause_date - i_system_start;
842 if( p_sys->i_buffering_extra_initial > 0 )
843 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
847 i_system_duration = mdate() - i_system_start;
850 const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
851 i_delay = p_sys->i_pts_delay - i_consumed;
858 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
859 const es_format_t *fmt, const char *psz_language,
862 es_out_sys_t *p_sys = out->p_sys;
863 input_thread_t *p_input = p_sys->p_input;
864 vlc_value_t val, text;
868 if( EsFmtIsTeletext( fmt ) )
869 input_SendEventTeletextDel( p_sys->p_input, i_id );
871 input_SendEventEsDel( p_input, fmt->i_cat, i_id );
875 /* Get the number of ES already added */
877 if( fmt->i_cat == AUDIO_ES )
878 psz_var = "audio-es";
879 else if( fmt->i_cat == VIDEO_ES )
880 psz_var = "video-es";
884 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
889 /* First one, we need to add the "Disable" choice */
890 val2.i_int = -1; text.psz_string = _("Disable");
891 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
895 /* Take care of the ES description */
896 if( fmt->psz_description && *fmt->psz_description )
898 if( psz_language && *psz_language )
900 text.psz_string = malloc( strlen( fmt->psz_description) +
901 strlen( psz_language ) + 10 );
902 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
905 else text.psz_string = strdup( fmt->psz_description );
909 if( psz_language && *psz_language )
911 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
912 text.psz_string = NULL;
916 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
917 text.psz_string = NULL;
921 input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
922 if( EsFmtIsTeletext( fmt ) )
925 snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
926 fmt->subs.teletext.i_magazine,
927 fmt->subs.teletext.i_page );
928 input_SendEventTeletextAdd( p_sys->p_input,
929 i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
932 free( text.psz_string );
935 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
938 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
941 /* EsOutProgramSelect:
942 * Select a program and update the object variable
944 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
946 es_out_sys_t *p_sys = out->p_sys;
947 input_thread_t *p_input = p_sys->p_input;
950 if( p_sys->p_pgrm == p_pgrm )
951 return; /* Nothing to do */
955 es_out_pgrm_t *old = p_sys->p_pgrm;
956 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
958 for( i = 0; i < p_sys->i_es; i++ )
960 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
961 p_sys->i_mode != ES_OUT_MODE_ALL )
962 EsUnselect( out, p_sys->es[i], true );
965 p_sys->p_es_audio = NULL;
966 p_sys->p_es_sub = NULL;
967 p_sys->p_es_video = NULL;
970 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
972 /* Mark it selected */
973 p_pgrm->b_selected = true;
975 /* Switch master stream */
976 p_sys->p_pgrm = p_pgrm;
978 /* Update "program" */
979 input_SendEventProgramSelect( p_input, p_pgrm->i_id );
982 input_SendEventEsDel( p_input, AUDIO_ES, -1 );
983 input_SendEventEsDel( p_input, VIDEO_ES, -1 );
984 input_SendEventEsDel( p_input, SPU_ES, -1 );
985 input_SendEventTeletextDel( p_input, -1 );
986 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
989 var_SetInteger( p_input, "teletext-es", -1 );
991 for( i = 0; i < p_sys->i_es; i++ )
993 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
994 EsOutESVarUpdate( out, p_sys->es[i], false );
995 EsOutSelect( out, p_sys->es[i], false );
998 /* Update now playing */
999 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1000 input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
1002 input_SendEventMeta( p_input );
1008 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
1010 es_out_sys_t *p_sys = out->p_sys;
1011 input_thread_t *p_input = p_sys->p_input;
1013 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1018 p_pgrm->i_id = i_group;
1020 p_pgrm->b_selected = false;
1021 p_pgrm->b_scrambled = false;
1022 p_pgrm->psz_name = NULL;
1023 p_pgrm->psz_now_playing = NULL;
1024 p_pgrm->psz_publisher = NULL;
1025 p_pgrm->p_epg = NULL;
1026 p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1027 if( !p_pgrm->p_clock )
1032 input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1036 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1038 /* Update "program" variable */
1039 input_SendEventProgramAdd( p_input, i_group, NULL );
1041 if( i_group == var_GetInteger( p_input, "program" ) )
1042 EsOutProgramSelect( out, p_pgrm );
1050 static int EsOutProgramDel( es_out_t *out, int i_group )
1052 es_out_sys_t *p_sys = out->p_sys;
1053 input_thread_t *p_input = p_sys->p_input;
1054 es_out_pgrm_t *p_pgrm = NULL;
1057 for( i = 0; i < p_sys->i_pgrm; i++ )
1059 if( p_sys->pgrm[i]->i_id == i_group )
1061 p_pgrm = p_sys->pgrm[i];
1066 if( p_pgrm == NULL )
1067 return VLC_EGENERIC;
1071 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1072 i_group, p_pgrm->i_es );
1073 return VLC_EGENERIC;
1076 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1078 /* If program is selected we need to unselect it */
1079 if( p_sys->p_pgrm == p_pgrm )
1080 p_sys->p_pgrm = NULL;
1082 input_clock_Delete( p_pgrm->p_clock );
1084 free( p_pgrm->psz_name );
1085 free( p_pgrm->psz_now_playing );
1086 free( p_pgrm->psz_publisher );
1088 vlc_epg_Delete( p_pgrm->p_epg );
1091 /* Update "program" variable */
1092 input_SendEventProgramDel( p_input, i_group );
1099 static es_out_pgrm_t *EsOutProgramFind( es_out_t *p_out, int i_group )
1101 es_out_sys_t *p_sys = p_out->p_sys;
1103 for( int i = 0; i < p_sys->i_pgrm; i++ )
1105 if( p_sys->pgrm[i]->i_id == i_group )
1106 return p_sys->pgrm[i];
1108 return EsOutProgramAdd( p_out, i_group );
1111 /* EsOutProgramMeta:
1113 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1116 if( p_pgrm->psz_name )
1118 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1123 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1129 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
1131 es_out_sys_t *p_sys = out->p_sys;
1132 es_out_pgrm_t *p_pgrm;
1133 input_thread_t *p_input = p_sys->p_input;
1135 const char *psz_title = NULL;
1136 const char *psz_provider = NULL;
1139 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1141 /* Check against empty meta data (empty for what we handle) */
1142 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1143 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1144 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1145 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1150 p_pgrm = EsOutProgramFind( out, i_group );
1155 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1156 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1158 /* Update the description text of the program */
1159 if( psz_title && *psz_title )
1161 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1163 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1165 /* Remove old entries */
1166 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1167 /* TODO update epg name ?
1168 * TODO update scrambled info name ? */
1171 free( p_pgrm->psz_name );
1172 p_pgrm->psz_name = strdup( psz_title );
1175 if( psz_provider && *psz_provider )
1177 if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1182 psz_text = strdup( psz_title );
1185 /* ugly but it works */
1188 input_SendEventProgramDel( p_input, i_group );
1189 input_SendEventProgramAdd( p_input, i_group, psz_text );
1195 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1198 if( p_sys->p_pgrm == p_pgrm )
1200 input_item_SetPublisher( p_input->p->p_item, psz_provider );
1201 input_SendEventMeta( p_input );
1203 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1205 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1206 for( i = 0; ppsz_all_keys[i]; i++ )
1208 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
1209 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
1210 free( ppsz_all_keys[i] );
1212 free( ppsz_all_keys );
1217 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
1222 for( i = 0; i < p_src->i_event; i++ )
1224 vlc_epg_event_t *p_evt = p_src->pp_event[i];
1228 for( j = 0; j < p_dst->i_event; j++ )
1230 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
1235 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
1240 vlc_epg_event_t *p_copy = calloc( 1, sizeof(vlc_epg_event_t) );
1243 p_copy->i_start = p_evt->i_start;
1244 p_copy->i_duration = p_evt->i_duration;
1245 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
1246 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
1247 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
1248 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
1251 /* Update current */
1252 if( p_src->p_current )
1253 vlc_epg_SetCurrent( p_dst, p_src->p_current->i_start );
1255 /* Keep only 1 old event */
1256 if( p_dst->p_current )
1258 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
1259 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
1263 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
1265 es_out_sys_t *p_sys = out->p_sys;
1266 input_thread_t *p_input = p_sys->p_input;
1267 es_out_pgrm_t *p_pgrm;
1271 p_pgrm = EsOutProgramFind( out, i_group );
1276 if( !p_pgrm->p_epg )
1277 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1278 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1281 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1283 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1286 if( asprintf( &psz_epg, "EPG %s", psz_cat ) >= 0 )
1288 input_item_SetEpg( p_input->p->p_item, psz_epg, p_pgrm->p_epg );
1292 /* Update now playing */
1293 free( p_pgrm->psz_now_playing );
1294 p_pgrm->psz_now_playing = NULL;
1295 if( p_pgrm->p_epg->p_current && p_pgrm->p_epg->p_current->psz_name && *p_pgrm->p_epg->p_current->psz_name )
1296 p_pgrm->psz_now_playing = strdup( p_pgrm->p_epg->p_current->psz_name );
1298 if( p_pgrm == p_sys->p_pgrm )
1300 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1301 input_SendEventMeta( p_input );
1304 if( p_pgrm->psz_now_playing )
1306 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1307 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1308 p_pgrm->psz_now_playing );
1312 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1313 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1319 static void EsOutProgramUpdateScrambled( es_out_t *p_out, es_out_pgrm_t *p_pgrm )
1321 es_out_sys_t *p_sys = p_out->p_sys;
1322 input_thread_t *p_input = p_sys->p_input;
1323 bool b_scrambled = false;
1325 for( int i = 0; i < p_sys->i_es; i++ )
1327 if( p_sys->es[i]->p_pgrm == p_pgrm && p_sys->es[i]->b_scrambled )
1333 if( !p_pgrm->b_scrambled == !b_scrambled )
1336 p_pgrm->b_scrambled = b_scrambled;
1337 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1340 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Scrambled"), _("Yes") );
1342 input_Control( p_input, INPUT_DEL_INFO, psz_cat, _("Scrambled") );
1344 input_SendEventProgramScrambled( p_input, p_pgrm->i_id, b_scrambled );
1350 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1352 es_out_sys_t *p_sys = out->p_sys;
1353 input_thread_t *p_input = p_sys->p_input;
1355 if( fmt->i_group < 0 )
1357 msg_Err( p_input, "invalid group number" );
1361 es_out_id_t *es = malloc( sizeof( *es ) );
1362 es_out_pgrm_t *p_pgrm;
1368 vlc_mutex_lock( &p_sys->lock );
1370 /* Search the program */
1371 p_pgrm = EsOutProgramFind( out, fmt->i_group );
1374 vlc_mutex_unlock( &p_sys->lock );
1379 /* Increase ref count for program */
1383 es->p_pgrm = p_pgrm;
1384 es_format_Copy( &es->fmt, fmt );
1385 if( es->fmt.i_id < 0 )
1386 es->fmt.i_id = out->p_sys->i_id;
1387 es->i_id = es->fmt.i_id;
1388 es->i_meta_id = out->p_sys->i_id;
1389 es->b_scrambled = false;
1391 switch( es->fmt.i_cat )
1395 audio_replay_gain_t rg;
1397 es->i_channel = p_sys->i_audio;
1399 memset( &rg, 0, sizeof(rg) );
1400 vlc_mutex_lock( &p_input->p->p_item->lock );
1401 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1402 vlc_mutex_unlock( &p_input->p->p_item->lock );
1404 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1406 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1408 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1409 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1411 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1413 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1414 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1421 es->i_channel = p_sys->i_video;
1422 if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1423 vlc_ureduce( &es->fmt.video.i_frame_rate,
1424 &es->fmt.video.i_frame_rate_base,
1425 es->fmt.video.i_frame_rate,
1426 es->fmt.video.i_frame_rate_base, 0 );
1430 es->i_channel = p_sys->i_sub;
1437 es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1438 es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1440 es->p_dec_record = NULL;
1441 for( i = 0; i < 4; i++ )
1442 es->pb_cc_present[i] = false;
1443 es->p_master = NULL;
1445 if( es->p_pgrm == p_sys->p_pgrm )
1446 EsOutESVarUpdate( out, es, false );
1448 /* Select it if needed */
1449 EsOutSelect( out, es, false );
1452 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1453 p_sys->i_id++; /* always incremented */
1454 switch( es->fmt.i_cat )
1467 EsOutUpdateInfo( out, es, &es->fmt, NULL );
1469 if( es->b_scrambled )
1470 EsOutProgramUpdateScrambled( out, es->p_pgrm );
1472 vlc_mutex_unlock( &p_sys->lock );
1477 static bool EsIsSelected( es_out_id_t *es )
1481 bool b_decode = false;
1482 if( es->p_master->p_dec )
1484 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1485 if( i_channel != -1 )
1486 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1492 return es->p_dec != NULL;
1495 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1497 es_out_sys_t *p_sys = out->p_sys;
1498 input_thread_t *p_input = p_sys->p_input;
1500 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1503 if( p_sys->b_buffering )
1504 input_DecoderStartBuffering( p_es->p_dec );
1506 if( !p_es->p_master && p_sys->p_sout_record )
1508 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1509 if( p_es->p_dec_record && p_sys->b_buffering )
1510 input_DecoderStartBuffering( p_es->p_dec_record );
1514 EsOutDecoderChangeDelay( out, p_es );
1516 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1523 input_DecoderDelete( p_es->p_dec );
1526 if( p_es->p_dec_record )
1528 input_DecoderDelete( p_es->p_dec_record );
1529 p_es->p_dec_record = NULL;
1533 static void EsSelect( es_out_t *out, es_out_id_t *es )
1535 es_out_sys_t *p_sys = out->p_sys;
1536 input_thread_t *p_input = p_sys->p_input;
1538 if( EsIsSelected( es ) )
1540 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1547 if( !es->p_master->p_dec )
1550 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1551 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1556 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1558 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1560 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1565 else if( es->fmt.i_cat == AUDIO_ES )
1567 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1569 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1574 if( es->fmt.i_cat == SPU_ES )
1576 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1578 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1584 EsCreateDecoder( out, es );
1586 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1590 /* Mark it as selected */
1591 input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1592 input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1595 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1597 es_out_sys_t *p_sys = out->p_sys;
1598 input_thread_t *p_input = p_sys->p_input;
1600 if( !EsIsSelected( es ) )
1602 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1608 if( es->p_master->p_dec )
1610 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1611 if( i_channel != -1 )
1612 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1617 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1619 for( i = 0; i < 4; i++ )
1621 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1624 if( i_spu_id == es->pp_cc_es[i]->i_id )
1626 /* Force unselection of the CC */
1627 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1629 EsOutDel( out, es->pp_cc_es[i] );
1631 es->pb_cc_present[i] = false;
1633 EsDestroyDecoder( out, es );
1639 /* Mark it as unselected */
1640 input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1641 if( EsFmtIsTeletext( &es->fmt ) )
1642 input_SendEventTeletextSelect( p_input, -1 );
1646 * Select an ES given the current mode
1647 * XXX: you need to take a the lock before (stream.stream_lock)
1649 * \param out The es_out structure
1650 * \param es es_out_id structure
1651 * \param b_force ...
1654 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1656 es_out_sys_t *p_sys = out->p_sys;
1658 int i_cat = es->fmt.i_cat;
1660 if( !p_sys->b_active ||
1661 ( !b_force && es->fmt.i_priority < 0 ) )
1666 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1668 if( !EsIsSelected( es ) )
1669 EsSelect( out, es );
1671 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1675 var_Get( p_sys->p_input, "programs", &val );
1676 for ( i = 0; i < val.p_list->i_count; i++ )
1678 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1680 if( !EsIsSelected( es ) )
1681 EsSelect( out, es );
1685 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1687 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1691 if( es->p_pgrm != p_sys->p_pgrm )
1694 if( i_cat == AUDIO_ES )
1696 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1697 es->psz_language_code );
1699 if( p_sys->p_es_audio &&
1700 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1702 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1703 p_sys->p_es_audio->psz_language_code );
1705 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1707 i_wanted = es->i_channel;
1711 /* Select audio if (no audio selected yet)
1712 * - no audio-language
1713 * - no audio code for the ES
1714 * - audio code in the requested list */
1716 !strcmp( es->psz_language_code, "??" ) ||
1717 !p_sys->ppsz_audio_language )
1718 i_wanted = es->i_channel;
1721 if( p_sys->i_audio_last >= 0 )
1722 i_wanted = p_sys->i_audio_last;
1724 if( p_sys->i_audio_id >= 0 )
1726 if( es->i_id == p_sys->i_audio_id )
1727 i_wanted = es->i_channel;
1732 else if( i_cat == SPU_ES )
1734 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1735 es->psz_language_code );
1737 if( p_sys->p_es_sub &&
1738 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1740 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1741 p_sys->p_es_sub->psz_language_code );
1743 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1744 idx1, es->psz_language_code, idx2,
1745 p_sys->p_es_sub->psz_language_code );
1747 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1749 /* We found a SPU that matches our language request */
1750 i_wanted = es->i_channel;
1752 else if( idx1 >= 0 )
1754 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1755 idx1, es->psz_language_code );
1757 i_wanted = es->i_channel;
1759 else if( p_sys->i_default_sub_id >= 0 )
1761 if( es->i_id == p_sys->i_default_sub_id )
1762 i_wanted = es->i_channel;
1765 if( p_sys->i_sub_last >= 0 )
1766 i_wanted = p_sys->i_sub_last;
1768 if( p_sys->i_sub_id >= 0 )
1770 if( es->i_id == p_sys->i_sub_id )
1771 i_wanted = es->i_channel;
1776 else if( i_cat == VIDEO_ES )
1778 i_wanted = es->i_channel;
1781 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1782 EsSelect( out, es );
1785 /* FIXME TODO handle priority here */
1786 if( EsIsSelected( es ) )
1788 if( i_cat == AUDIO_ES )
1790 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1791 p_sys->p_es_audio &&
1792 p_sys->p_es_audio != es &&
1793 EsIsSelected( p_sys->p_es_audio ) )
1795 EsUnselect( out, p_sys->p_es_audio, false );
1797 p_sys->p_es_audio = es;
1799 else if( i_cat == SPU_ES )
1801 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1803 p_sys->p_es_sub != es &&
1804 EsIsSelected( p_sys->p_es_sub ) )
1806 EsUnselect( out, p_sys->p_es_sub, false );
1808 p_sys->p_es_sub = es;
1810 else if( i_cat == VIDEO_ES )
1812 p_sys->p_es_video = es;
1818 * Send a block for the given es_out
1820 * \param out the es_out to send from
1821 * \param es the es_out_id
1822 * \param p_block the data block to send
1824 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1826 es_out_sys_t *p_sys = out->p_sys;
1827 input_thread_t *p_input = p_sys->p_input;
1830 if( libvlc_stats( p_input ) )
1832 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1833 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1834 p_block->i_buffer, &i_total );
1835 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1836 (float)i_total, NULL );
1838 /* Update number of corrupted data packats */
1839 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
1841 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_corrupted,
1844 /* Update number of discontinuities */
1845 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1847 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_discontinuity,
1850 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1853 vlc_mutex_lock( &p_sys->lock );
1855 /* Mark preroll blocks */
1856 if( p_sys->i_preroll_end >= 0 )
1858 int64_t i_date = p_block->i_pts;
1860 i_date = p_block->i_dts;
1862 if( i_date < p_sys->i_preroll_end )
1863 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1866 p_block->i_rate = 0;
1870 block_Release( p_block );
1871 vlc_mutex_unlock( &p_sys->lock );
1876 if( es->p_dec_record )
1878 block_t *p_dup = block_Duplicate( p_block );
1880 input_DecoderDecode( es->p_dec_record, p_dup );
1882 input_DecoderDecode( es->p_dec, p_block );
1884 es_format_t fmt_dsc;
1885 vlc_meta_t *p_meta_dsc;
1886 if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
1888 EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
1890 es_format_Clean( &fmt_dsc );
1892 vlc_meta_Delete( p_meta_dsc );
1895 /* Check CC status */
1898 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1899 for( int i = 0; i < 4; i++ )
1901 static const vlc_fourcc_t fcc[4] = {
1902 VLC_FOURCC('c', 'c', '1', ' '),
1903 VLC_FOURCC('c', 'c', '2', ' '),
1904 VLC_FOURCC('c', 'c', '3', ' '),
1905 VLC_FOURCC('c', 'c', '4', ' '),
1909 if( es->pb_cc_present[i] || !pb_cc[i] )
1911 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1913 es_format_Init( &fmt, SPU_ES, fcc[i] );
1914 fmt.i_group = es->fmt.i_group;
1915 if( asprintf( &fmt.psz_description,
1916 _("Closed captions %u"), 1 + i ) == -1 )
1917 fmt.psz_description = NULL;
1918 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1919 es->pp_cc_es[i]->p_master = es;
1920 es_format_Clean( &fmt );
1923 es->pb_cc_present[i] = true;
1926 vlc_mutex_unlock( &p_sys->lock );
1931 /*****************************************************************************
1933 *****************************************************************************/
1934 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1936 es_out_sys_t *p_sys = out->p_sys;
1937 bool b_reselect = false;
1940 vlc_mutex_lock( &p_sys->lock );
1942 /* We don't try to reselect */
1945 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
1947 if( input_DecoderIsEmpty( es->p_dec ) &&
1948 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
1950 /* FIXME there should be a way to have auto deleted es, but there will be
1951 * a problem when another codec of the same type is created (mainly video) */
1954 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1957 if( es->p_pgrm == p_sys->p_pgrm )
1958 EsOutESVarUpdate( out, es, true );
1960 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1962 /* Update program */
1964 if( es->p_pgrm->i_es == 0 )
1965 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1967 if( es->b_scrambled )
1968 EsOutProgramUpdateScrambled( out, es->p_pgrm );
1971 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1972 p_sys->p_es_sub == es ) b_reselect = true;
1974 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1975 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1976 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1978 switch( es->fmt.i_cat )
1991 /* Re-select another track when needed */
1994 for( i = 0; i < p_sys->i_es; i++ )
1996 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1997 EsOutSelect( out, p_sys->es[i], false );
2001 free( es->psz_language );
2002 free( es->psz_language_code );
2004 es_format_Clean( &es->fmt );
2006 vlc_mutex_unlock( &p_sys->lock );
2012 * Control query handler
2014 * \param out the es_out to control
2015 * \param i_query A es_out query as defined in include/ninput.h
2016 * \param args a variable list of arguments for the query
2017 * \return VLC_SUCCESS or an error code
2019 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2021 es_out_sys_t *p_sys = out->p_sys;
2029 case ES_OUT_SET_ES_STATE:
2030 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2031 b = (bool) va_arg( args, int );
2032 if( b && !EsIsSelected( es ) )
2034 EsSelect( out, es );
2035 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2037 else if( !b && EsIsSelected( es ) )
2039 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2044 case ES_OUT_GET_ES_STATE:
2045 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2046 pb = (bool*) va_arg( args, bool * );
2048 *pb = EsIsSelected( es );
2051 case ES_OUT_SET_ACTIVE:
2053 b = (bool) va_arg( args, int );
2054 if( b && !p_sys->b_active && p_sys->i_es > 0 )
2056 /* XXX Terminate vout if there are tracks but no video one.
2057 * This one is not mandatory but is he earliest place where it
2059 for( i = 0; i < p_sys->i_es; i++ )
2061 es_out_id_t *p_es = p_sys->es[i];
2062 if( p_es->fmt.i_cat == VIDEO_ES )
2065 if( i >= p_sys->i_es )
2066 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2068 p_sys->b_active = b;
2072 case ES_OUT_SET_MODE:
2073 i = (int) va_arg( args, int );
2074 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
2075 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
2079 /* Reapply policy mode */
2080 for( i = 0; i < p_sys->i_es; i++ )
2082 if( EsIsSelected( p_sys->es[i] ) )
2084 EsUnselect( out, p_sys->es[i],
2085 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2088 for( i = 0; i < p_sys->i_es; i++ )
2090 EsOutSelect( out, p_sys->es[i], false );
2094 return VLC_EGENERIC;
2097 case ES_OUT_RESTART_ES:
2101 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2105 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2107 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2109 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2114 for( i = 0; i < p_sys->i_es; i++ )
2118 if( es == p_sys->es[i] )
2120 EsOutSelect( out, es, true );
2126 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2128 if( EsIsSelected( p_sys->es[i] ) )
2130 if( i_query == ES_OUT_RESTART_ES )
2132 if( p_sys->es[i]->p_dec )
2134 EsDestroyDecoder( out, p_sys->es[i] );
2135 EsCreateDecoder( out, p_sys->es[i] );
2140 EsUnselect( out, p_sys->es[i],
2141 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2150 case ES_OUT_SET_ES_DEFAULT:
2152 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2156 /*p_sys->i_default_video_id = -1;*/
2157 /*p_sys->i_default_audio_id = -1;*/
2158 p_sys->i_default_sub_id = -1;
2160 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2162 /*p_sys->i_default_video_id = -1;*/
2164 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2166 /*p_sys->i_default_audio_id = -1;*/
2168 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2170 p_sys->i_default_sub_id = -1;
2174 /*if( es->fmt.i_cat == VIDEO_ES )
2175 p_sys->i_default_video_id = es->i_id;
2177 if( es->fmt.i_cat == AUDIO_ES )
2178 p_sys->i_default_audio_id = es->i_id;
2180 if( es->fmt.i_cat == SPU_ES )
2181 p_sys->i_default_sub_id = es->i_id;
2186 case ES_OUT_SET_PCR:
2187 case ES_OUT_SET_GROUP_PCR:
2189 es_out_pgrm_t *p_pgrm = NULL;
2193 if( i_query == ES_OUT_SET_PCR )
2195 p_pgrm = p_sys->p_pgrm;
2197 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2201 i_group = (int)va_arg( args, int );
2202 p_pgrm = EsOutProgramFind( out, i_group );
2205 return VLC_EGENERIC;
2207 i_pcr = (int64_t)va_arg( args, int64_t );
2209 * TODO do not use mdate() but proper stream acquisition date */
2210 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2211 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering, i_pcr, mdate() );
2212 /* Check buffering state on master clock update */
2213 if( p_sys->b_buffering && p_pgrm == p_sys->p_pgrm )
2214 EsOutDecodersStopBuffering( out, false );
2219 case ES_OUT_RESET_PCR:
2220 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2221 EsOutChangePosition( out );
2224 case ES_OUT_SET_GROUP:
2227 i = (int) va_arg( args, int );
2228 for( j = 0; j < p_sys->i_pgrm; j++ )
2230 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2231 if( p_pgrm->i_id == i )
2233 EsOutProgramSelect( out, p_pgrm );
2237 return VLC_EGENERIC;
2240 case ES_OUT_SET_ES_FMT:
2242 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2243 * to update the p_extra data */
2245 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2246 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2248 return VLC_EGENERIC;
2250 if( p_fmt->i_extra )
2252 es->fmt.i_extra = p_fmt->i_extra;
2253 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2254 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2259 EsDestroyDecoder( out, es );
2261 EsCreateDecoder( out, es );
2263 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2264 es->p_dec->fmt_in.p_extra =
2265 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2266 memcpy( es->p_dec->fmt_in.p_extra,
2267 p_fmt->p_extra, p_fmt->i_extra );
2274 case ES_OUT_SET_ES_SCRAMBLED_STATE:
2276 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2277 bool b_scrambled = (bool)va_arg( args, int );
2279 if( !es->b_scrambled != !b_scrambled )
2281 es->b_scrambled = b_scrambled;
2282 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2287 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2289 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2292 return VLC_EGENERIC;
2294 p_sys->i_preroll_end = i_date;
2298 case ES_OUT_SET_GROUP_META:
2300 int i_group = (int)va_arg( args, int );
2301 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2303 EsOutProgramMeta( out, i_group, p_meta );
2306 case ES_OUT_SET_GROUP_EPG:
2308 int i_group = (int)va_arg( args, int );
2309 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2311 EsOutProgramEpg( out, i_group, p_epg );
2315 case ES_OUT_DEL_GROUP:
2317 int i_group = (int)va_arg( args, int );
2319 return EsOutProgramDel( out, i_group );
2322 case ES_OUT_GET_WAKE_UP:
2324 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2325 *pi_wakeup = EsOutGetWakeup( out );
2329 case ES_OUT_SET_ES_BY_ID:
2330 case ES_OUT_RESTART_ES_BY_ID:
2331 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2333 const int i_id = (int)va_arg( args, int );
2334 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2339 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2340 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2341 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2345 /* TODO if the lock is made non recursive it should be changed */
2346 int i_ret = es_out_Control( out, i_new_query, p_es );
2348 /* Clean up vout after user action (in active mode only).
2349 * FIXME it does not work well with multiple video windows */
2350 if( p_sys->b_active )
2351 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2355 case ES_OUT_GET_BUFFERING:
2356 pb = (bool *)va_arg( args, bool* );
2357 *pb = p_sys->b_buffering;
2360 case ES_OUT_GET_EMPTY:
2361 pb = (bool *)va_arg( args, bool* );
2362 *pb = EsOutDecodersIsEmpty( out );
2365 case ES_OUT_SET_DELAY:
2367 const int i_cat = (int)va_arg( args, int );
2368 const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2369 EsOutSetDelay( out, i_cat, i_delay );
2373 case ES_OUT_SET_RECORD_STATE:
2374 b = (bool) va_arg( args, int );
2375 return EsOutSetRecord( out, b );
2377 case ES_OUT_SET_PAUSE_STATE:
2379 const bool b_source_paused = (bool)va_arg( args, int );
2380 const bool b_paused = (bool)va_arg( args, int );
2381 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2383 assert( !b_source_paused == !b_paused );
2384 EsOutChangePause( out, b_paused, i_date );
2389 case ES_OUT_SET_RATE:
2391 const int i_src_rate = (int)va_arg( args, int );
2392 const int i_rate = (int)va_arg( args, int );
2394 assert( i_src_rate == i_rate );
2395 EsOutChangeRate( out, i_rate );
2400 case ES_OUT_SET_TIME:
2402 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2404 assert( i_date == -1 );
2405 EsOutChangePosition( out );
2410 case ES_OUT_SET_FRAME_NEXT:
2411 EsOutFrameNext( out );
2414 case ES_OUT_SET_TIMES:
2416 double f_position = (double)va_arg( args, double );
2417 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2418 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2420 /* Fix for buffering delay */
2421 const mtime_t i_delay = EsOutGetBuffering( out );
2428 f_position -= (double)i_delay / i_length;
2429 if( f_position < 0 )
2432 if( !p_sys->b_buffering )
2433 input_SendEventTimes( p_sys->p_input, f_position, i_time, i_length );
2436 case ES_OUT_SET_JITTER:
2438 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2439 int i_cr_average = (int)va_arg( args, int );
2441 if( i_pts_delay == p_sys->i_pts_delay &&
2442 i_cr_average == p_sys->i_cr_average )
2445 p_sys->i_pts_delay = i_pts_delay;
2446 p_sys->i_cr_average = i_cr_average;
2448 for( int i = 0; i < p_sys->i_pgrm; i++ )
2449 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2450 i_pts_delay, i_cr_average );
2455 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2456 return VLC_EGENERIC;
2459 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2461 es_out_sys_t *p_sys = out->p_sys;
2464 vlc_mutex_lock( &p_sys->lock );
2465 i_ret = EsOutControlLocked( out, i_query, args );
2466 vlc_mutex_unlock( &p_sys->lock );
2471 /****************************************************************************
2472 * LanguageGetName: try to expend iso639 into plain name
2473 ****************************************************************************/
2474 static char *LanguageGetName( const char *psz_code )
2476 const iso639_lang_t *pl;
2478 if( psz_code == NULL )
2480 return strdup( "" );
2483 if( strlen( psz_code ) == 2 )
2485 pl = GetLang_1( psz_code );
2487 else if( strlen( psz_code ) == 3 )
2489 pl = GetLang_2B( psz_code );
2490 if( !strcmp( pl->psz_iso639_1, "??" ) )
2492 pl = GetLang_2T( psz_code );
2497 return strdup( psz_code );
2500 if( !strcmp( pl->psz_iso639_1, "??" ) )
2502 return strdup( psz_code );
2506 if( *pl->psz_native_name )
2508 return strdup( pl->psz_native_name );
2510 return strdup( pl->psz_eng_name );
2514 /* Get a 2 char code */
2515 static char *LanguageGetCode( const char *psz_lang )
2517 const iso639_lang_t *pl;
2519 if( psz_lang == NULL || *psz_lang == '\0' )
2520 return strdup("??");
2522 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2524 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2525 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2526 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2527 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2528 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2532 if( pl->psz_iso639_1 != NULL )
2533 return strdup( pl->psz_iso639_1 );
2535 return strdup("??");
2538 static char **LanguageSplit( const char *psz_langs )
2545 if( psz_langs == NULL ) return NULL;
2547 psz_parser = psz_dup = strdup(psz_langs);
2549 while( psz_parser && *psz_parser )
2554 psz = strchr(psz_parser, ',' );
2555 if( psz ) *psz++ = '\0';
2557 if( !strcmp( psz_parser, "any" ) )
2559 TAB_APPEND( i_psz, ppsz, strdup("any") );
2563 psz_code = LanguageGetCode( psz_parser );
2564 if( strcmp( psz_code, "??" ) )
2566 TAB_APPEND( i_psz, ppsz, psz_code );
2579 TAB_APPEND( i_psz, ppsz, NULL );
2586 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2590 if( !ppsz_langs || !psz_lang ) return -1;
2592 for( i = 0; ppsz_langs[i]; i++ )
2594 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2595 !strcasecmp( ppsz_langs[i], "any" ) )
2604 /****************************************************************************
2606 * - add meta info to the playlist item
2607 ****************************************************************************/
2608 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2610 es_out_sys_t *p_sys = out->p_sys;
2611 input_thread_t *p_input = p_sys->p_input;
2612 const es_format_t *p_fmt_es = &es->fmt;
2616 /* Create category name */
2617 if( asprintf( &psz_cat, _("Stream %d"), es->i_meta_id ) == -1 )
2620 /* Remove previous information */
2621 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
2623 /* Add informations */
2624 const char *psz_type;
2625 switch( fmt->i_cat )
2628 psz_type = _("Audio");
2631 psz_type = _("Video");
2634 psz_type = _("Subtitle");
2642 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Type"), psz_type );
2644 if( es->i_meta_id != es->i_id )
2645 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Original ID"),
2648 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2649 "%.4s", (char*)&p_fmt_es->i_codec );
2651 if( es->psz_language && *es->psz_language )
2652 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2653 "%s", es->psz_language );
2654 if( fmt->psz_description && *fmt->psz_description )
2655 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
2656 "%s", fmt->psz_description );
2658 switch( fmt->i_cat )
2661 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2662 _("Type"), _("Audio") );
2664 if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
2665 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2666 "%s", _( aout_FormatPrintChannels( &fmt->audio ) ) );
2667 else if( fmt->audio.i_channels > 0 )
2668 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2669 "%u", fmt->audio.i_channels );
2671 if( fmt->audio.i_rate > 0 )
2673 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2674 _("%u Hz"), fmt->audio.i_rate );
2675 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2676 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2679 unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2680 if( i_bitspersample <= 0 )
2681 i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2682 if( i_bitspersample > 0 )
2683 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2684 _("Bits per sample"), "%u",
2687 if( fmt->i_bitrate > 0 )
2689 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2690 _("%u kb/s"), fmt->i_bitrate / 1000 );
2691 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2692 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2694 for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2696 const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2697 if( !p_rg->pb_gain[i] )
2699 const char *psz_name;
2700 if( i == AUDIO_REPLAY_GAIN_TRACK )
2701 psz_name = _("Track replay gain");
2703 psz_name = _("Album replay gain");
2704 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2705 psz_name, _("%.2f dB"), p_rg->pf_gain[i] );
2710 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2711 _("Type"), _("Video") );
2713 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2714 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2715 _("Resolution"), "%ux%u",
2716 fmt->video.i_width, fmt->video.i_height );
2718 if( fmt->video.i_visible_width > 0 &&
2719 fmt->video.i_visible_height > 0 )
2720 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2721 _("Display resolution"), "%ux%u",
2722 fmt->video.i_visible_width,
2723 fmt->video.i_visible_height);
2724 if( fmt->video.i_frame_rate > 0 &&
2725 fmt->video.i_frame_rate_base > 0 )
2727 div = lldiv( (float)fmt->video.i_frame_rate /
2728 fmt->video.i_frame_rate_base * 1000000,
2731 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2732 _("Frame rate"), "%"PRId64".%06u",
2733 div.quot, (unsigned int )div.rem );
2735 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2736 _("Frame rate"), "%"PRId64, div.quot );
2741 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2742 _("Type"), _("Subtitle") );
2749 /* Append generic meta */
2752 char **ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2753 for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
2755 char *psz_key = ppsz_all_keys[i];
2756 char *psz_value = vlc_dictionary_value_for_key( &p_meta->extra_tags, psz_key );
2759 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(psz_key), _(psz_value) );
2762 free( ppsz_all_keys );