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 */
66 /* Clock for this program */
67 input_clock_t *p_clock;
70 char *psz_now_playing;
80 es_out_pgrm_t *p_pgrm;
82 /* Channel in the track type */
86 char *psz_language_code;
89 decoder_t *p_dec_record;
91 /* Fields for Video with CC */
92 bool pb_cc_present[4];
93 es_out_id_t *pp_cc_es[4];
95 /* Field for CC track from a master video */
96 es_out_id_t *p_master;
98 /* ID for the meta data */
104 input_thread_t *p_input;
111 es_out_pgrm_t **pgrm;
112 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
113 es_out_pgrm_t *p_pgrm; /* Master program */
130 int i_audio_last, i_audio_id;
131 int i_sub_last, i_sub_id;
132 int i_default_sub_id; /* As specified in container; if applicable */
133 char **ppsz_audio_language;
134 char **ppsz_sub_language;
136 /* current main es */
137 es_out_id_t *p_es_audio;
138 es_out_id_t *p_es_video;
139 es_out_id_t *p_es_sub;
142 int64_t i_audio_delay;
145 /* Clock configuration */
152 mtime_t i_pause_date;
154 /* Current preroll */
155 mtime_t i_preroll_end;
157 /* Used for buffering */
159 mtime_t i_buffering_extra_initial;
160 mtime_t i_buffering_extra_stream;
161 mtime_t i_buffering_extra_system;
164 sout_instance_t *p_sout_record;
167 static es_out_id_t *EsOutAdd ( es_out_t *, const es_format_t * );
168 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
169 static void EsOutDel ( es_out_t *, es_out_id_t * );
170 static int EsOutControl( es_out_t *, int i_query, va_list );
171 static void EsOutDelete ( es_out_t * );
173 static void EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
174 static void EsOutUpdateInfo( es_out_t *, es_out_id_t *es, const es_format_t *, const vlc_meta_t * );
175 static int EsOutSetRecord( es_out_t *, bool b_record );
177 static bool EsIsSelected( es_out_id_t *es );
178 static void EsSelect( es_out_t *out, es_out_id_t *es );
179 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
180 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
181 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
182 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
183 static void EsOutProgramsChangeRate( es_out_t *out );
184 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
185 static char *LanguageGetName( const char *psz_code );
186 static char *LanguageGetCode( const char *psz_lang );
187 static char **LanguageSplit( const char *psz_langs );
188 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
190 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
192 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
193 VLC_FOURCC('c', 'c', '1', ' '),
194 VLC_FOURCC('c', 'c', '2', ' '),
195 VLC_FOURCC('c', 'c', '3', ' '),
196 VLC_FOURCC('c', 'c', '4', ' '),
198 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
201 for( i = 0; i < 4; i++ )
203 if( fcc == EsOutFourccClosedCaptions[i] )
208 static inline bool EsFmtIsTeletext( const es_format_t *p_fmt )
210 return p_fmt->i_cat == SPU_ES && p_fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
213 /*****************************************************************************
215 *****************************************************************************/
216 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
221 es_out_t *out = malloc( sizeof( *out ) );
225 es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
232 out->pf_add = EsOutAdd;
233 out->pf_send = EsOutSend;
234 out->pf_del = EsOutDel;
235 out->pf_control = EsOutControl;
236 out->pf_destroy = EsOutDelete;
238 out->b_sout = p_input->p->p_sout != NULL;
241 vlc_mutex_init_recursive( &p_sys->lock );
242 p_sys->p_input = p_input;
244 p_sys->b_active = false;
245 p_sys->i_mode = ES_OUT_MODE_AUTO;
248 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
249 p_sys->p_pgrm = NULL;
253 TAB_INIT( p_sys->i_es, p_sys->es );
260 var_Get( p_input, "audio-track", &val );
261 p_sys->i_audio_last = val.i_int;
263 var_Get( p_input, "sub-track", &val );
264 p_sys->i_sub_last = val.i_int;
266 p_sys->i_default_sub_id = -1;
268 if( !p_input->b_preparsing )
270 var_Get( p_input, "audio-language", &val );
271 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
272 if( p_sys->ppsz_audio_language )
274 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
275 msg_Dbg( p_input, "selected audio language[%d] %s",
276 i, p_sys->ppsz_audio_language[i] );
278 free( val.psz_string );
280 var_Get( p_input, "sub-language", &val );
281 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
282 if( p_sys->ppsz_sub_language )
284 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
285 msg_Dbg( p_input, "selected subtitle language[%d] %s",
286 i, p_sys->ppsz_sub_language[i] );
288 free( val.psz_string );
292 p_sys->ppsz_sub_language = NULL;
293 p_sys->ppsz_audio_language = NULL;
296 var_Get( p_input, "audio-track-id", &val );
297 p_sys->i_audio_id = val.i_int;
299 var_Get( p_input, "sub-track-id", &val );
300 p_sys->i_sub_id = val.i_int;
302 p_sys->p_es_audio = NULL;
303 p_sys->p_es_video = NULL;
304 p_sys->p_es_sub = NULL;
306 p_sys->i_audio_delay= 0;
307 p_sys->i_spu_delay = 0;
309 p_sys->b_paused = false;
310 p_sys->i_pause_date = -1;
312 p_sys->i_rate = i_rate;
313 p_sys->i_pts_delay = 0;
314 p_sys->i_cr_average = 0;
316 p_sys->b_buffering = true;
317 p_sys->i_buffering_extra_initial = 0;
318 p_sys->i_buffering_extra_stream = 0;
319 p_sys->i_buffering_extra_system = 0;
320 p_sys->i_preroll_end = -1;
322 p_sys->p_sout_record = NULL;
327 /*****************************************************************************
329 *****************************************************************************/
330 static void EsOutDelete( es_out_t *out )
332 es_out_sys_t *p_sys = out->p_sys;
335 if( p_sys->p_sout_record )
336 EsOutSetRecord( out, false );
338 for( i = 0; i < p_sys->i_es; i++ )
340 if( p_sys->es[i]->p_dec )
341 input_DecoderDelete( p_sys->es[i]->p_dec );
343 free( p_sys->es[i]->psz_language );
344 free( p_sys->es[i]->psz_language_code );
345 es_format_Clean( &p_sys->es[i]->fmt );
347 free( p_sys->es[i] );
349 if( p_sys->ppsz_audio_language )
351 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
352 free( p_sys->ppsz_audio_language[i] );
353 free( p_sys->ppsz_audio_language );
355 if( p_sys->ppsz_sub_language )
357 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
358 free( p_sys->ppsz_sub_language[i] );
359 free( p_sys->ppsz_sub_language );
363 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
364 for( i = 0; i < p_sys->i_pgrm; i++ )
366 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
367 input_clock_Delete( p_pgrm->p_clock );
368 free( p_pgrm->psz_now_playing );
369 free( p_pgrm->psz_publisher );
370 free( p_pgrm->psz_name );
372 vlc_epg_Delete( p_pgrm->p_epg );
376 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
377 vlc_mutex_destroy( &p_sys->lock );
383 static mtime_t EsOutGetWakeup( es_out_t *out )
385 es_out_sys_t *p_sys = out->p_sys;
386 input_thread_t *p_input = p_sys->p_input;
391 /* We do not have a wake up date if the input cannot have its speed
392 * controlled or sout is imposing its own or while buffering
394 * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
395 if( !p_input->p->b_can_pace_control ||
396 p_input->p->b_out_pace_control ||
400 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
403 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
408 /* Special HACK, -i_id is the cat of the stream */
409 return (es_out_id_t*)((uint8_t*)NULL-i_id);
412 for( i = 0; i < out->p_sys->i_es; i++ )
414 if( out->p_sys->es[i]->i_id == i_id )
415 return out->p_sys->es[i];
420 static bool EsOutDecodersIsEmpty( es_out_t *out )
422 es_out_sys_t *p_sys = out->p_sys;
425 if( p_sys->b_buffering && p_sys->p_pgrm )
427 EsOutDecodersStopBuffering( out, true );
428 if( p_sys->b_buffering )
432 for( i = 0; i < p_sys->i_es; i++ )
434 es_out_id_t *es = p_sys->es[i];
436 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
438 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
444 static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
446 es_out_sys_t *p_sys = out->p_sys;
448 if( i_cat == AUDIO_ES )
449 p_sys->i_audio_delay = i_delay;
450 else if( i_cat == SPU_ES )
451 p_sys->i_spu_delay = i_delay;
453 for( int i = 0; i < p_sys->i_es; i++ )
454 EsOutDecoderChangeDelay( out, p_sys->es[i] );
457 static int EsOutSetRecord( es_out_t *out, bool b_record )
459 es_out_sys_t *p_sys = out->p_sys;
460 input_thread_t *p_input = p_sys->p_input;
462 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
466 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
467 if( !psz_path || *psz_path == '\0' )
470 psz_path = strdup( config_GetHomeDir() );
473 char *psz_sout = NULL; // TODO conf
475 if( !psz_sout && psz_path )
477 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
480 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
491 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
495 if( !p_sys->p_sout_record )
498 for( int i = 0; i < p_sys->i_es; i++ )
500 es_out_id_t *p_es = p_sys->es[i];
502 if( !p_es->p_dec || p_es->p_master )
505 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
506 if( p_es->p_dec_record && p_sys->b_buffering )
507 input_DecoderStartBuffering( p_es->p_dec_record );
512 for( int i = 0; i < p_sys->i_es; i++ )
514 es_out_id_t *p_es = p_sys->es[i];
516 if( !p_es->p_dec_record )
519 input_DecoderDelete( p_es->p_dec_record );
520 p_es->p_dec_record = NULL;
523 sout_DeleteInstance( p_sys->p_sout_record );
525 p_sys->p_sout_record = NULL;
530 static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
532 es_out_sys_t *p_sys = out->p_sys;
534 /* XXX the order is important */
537 EsOutDecodersChangePause( out, true, i_date );
538 EsOutProgramChangePause( out, true, i_date );
542 if( p_sys->i_buffering_extra_initial > 0 )
544 mtime_t i_stream_start;
545 mtime_t i_system_start;
546 mtime_t i_stream_duration;
547 mtime_t i_system_duration;
549 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
550 &i_stream_start, &i_system_start,
551 &i_stream_duration, &i_system_duration );
554 /* FIXME pcr != exactly what wanted */
555 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;
558 p_sys->i_buffering_extra_initial = 0;
559 p_sys->i_buffering_extra_stream = 0;
560 p_sys->i_buffering_extra_system = 0;
562 EsOutProgramChangePause( out, false, i_date );
563 EsOutDecodersChangePause( out, false, i_date );
565 EsOutProgramsChangeRate( out );
567 p_sys->b_paused = b_paused;
568 p_sys->i_pause_date = i_date;
571 static void EsOutChangeRate( es_out_t *out, int i_rate )
573 es_out_sys_t *p_sys = out->p_sys;
575 p_sys->i_rate = i_rate;
577 if( !p_sys->b_paused )
578 EsOutProgramsChangeRate( out );
581 static void EsOutChangePosition( es_out_t *out )
583 es_out_sys_t *p_sys = out->p_sys;
585 input_SendEventCache( p_sys->p_input, 0.0 );
587 for( int i = 0; i < p_sys->i_es; i++ )
589 es_out_id_t *p_es = p_sys->es[i];
594 input_DecoderStartBuffering( p_es->p_dec );
596 if( p_es->p_dec_record )
597 input_DecoderStartBuffering( p_es->p_dec_record );
600 for( int i = 0; i < p_sys->i_pgrm; i++ )
601 input_clock_Reset( p_sys->pgrm[i]->p_clock );
603 p_sys->b_buffering = true;
604 p_sys->i_buffering_extra_initial = 0;
605 p_sys->i_buffering_extra_stream = 0;
606 p_sys->i_buffering_extra_system = 0;
607 p_sys->i_preroll_end = -1;
612 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
614 es_out_sys_t *p_sys = out->p_sys;
617 mtime_t i_stream_start;
618 mtime_t i_system_start;
619 mtime_t i_stream_duration;
620 mtime_t i_system_duration;
621 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
622 &i_stream_start, &i_system_start,
623 &i_stream_duration, &i_system_duration );
624 assert( !i_ret || b_forced );
628 mtime_t i_preroll_duration = 0;
629 if( p_sys->i_preroll_end >= 0 )
630 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
632 const mtime_t i_buffering_duration = p_sys->i_pts_delay +
634 p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
636 if( i_stream_duration <= i_buffering_duration && !b_forced )
638 const double f_level = (double)i_stream_duration / i_buffering_duration;
639 input_SendEventCache( p_sys->p_input, f_level );
641 msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
644 input_SendEventCache( p_sys->p_input, 1.0 );
646 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
647 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
648 p_sys->b_buffering = false;
649 p_sys->i_preroll_end = -1;
651 if( p_sys->i_buffering_extra_initial > 0 )
657 const mtime_t i_decoder_buffering_start = mdate();
658 for( int i = 0; i < p_sys->i_es; i++ )
660 es_out_id_t *p_es = p_sys->es[i];
662 if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
664 input_DecoderWaitBuffering( p_es->p_dec );
665 if( p_es->p_dec_record )
666 input_DecoderWaitBuffering( p_es->p_dec_record );
669 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
670 (int)(mdate() - i_decoder_buffering_start)/1000 );
672 /* Here is a good place to destroy unused vout with every demuxer */
673 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
676 const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
677 const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
679 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_current_date + i_wakeup_delay - i_buffering_duration );
681 for( int i = 0; i < p_sys->i_es; i++ )
683 es_out_id_t *p_es = p_sys->es[i];
688 input_DecoderStopBuffering( p_es->p_dec );
689 if( p_es->p_dec_record )
690 input_DecoderStopBuffering( p_es->p_dec_record );
693 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
695 es_out_sys_t *p_sys = out->p_sys;
697 /* Pause decoders first */
698 for( int i = 0; i < p_sys->i_es; i++ )
700 es_out_id_t *es = p_sys->es[i];
704 input_DecoderChangePause( es->p_dec, b_paused, i_date );
705 if( es->p_dec_record )
706 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
710 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
712 es_out_sys_t *p_sys = out->p_sys;
714 for( int i = 0; i < p_sys->i_pgrm; i++ )
715 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
718 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
720 es_out_sys_t *p_sys = out->p_sys;
723 if( p_es->fmt.i_cat == AUDIO_ES )
724 i_delay = p_sys->i_audio_delay;
725 else if( p_es->fmt.i_cat == SPU_ES )
726 i_delay = p_sys->i_spu_delay;
731 input_DecoderChangeDelay( p_es->p_dec, i_delay );
732 if( p_es->p_dec_record )
733 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
736 static void EsOutProgramsChangeRate( es_out_t *out )
738 es_out_sys_t *p_sys = out->p_sys;
740 for( int i = 0; i < p_sys->i_pgrm; i++ )
741 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
744 static void EsOutFrameNext( es_out_t *out )
746 es_out_sys_t *p_sys = out->p_sys;
747 es_out_id_t *p_es_video = NULL;
749 if( p_sys->b_buffering )
751 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
755 assert( p_sys->b_paused );
757 for( int i = 0; i < p_sys->i_es; i++ )
759 es_out_id_t *p_es = p_sys->es[i];
761 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
770 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
775 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
777 msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
779 if( i_duration <= 0 )
780 i_duration = 40*1000;
782 /* FIXME it is not a clean way ? */
783 if( p_sys->i_buffering_extra_initial <= 0 )
785 mtime_t i_stream_start;
786 mtime_t i_system_start;
787 mtime_t i_stream_duration;
788 mtime_t i_system_duration;
791 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
792 &i_stream_start, &i_system_start,
793 &i_stream_duration, &i_system_duration );
797 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
798 p_sys->i_buffering_extra_system =
799 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
802 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
804 p_sys->b_buffering = true;
805 p_sys->i_buffering_extra_system += i_duration;
806 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
807 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
808 INPUT_RATE_DEFAULT / i_rate;
810 p_sys->i_preroll_end = -1;
812 static mtime_t EsOutGetBuffering( es_out_t *out )
814 es_out_sys_t *p_sys = out->p_sys;
820 mtime_t i_stream_start;
821 mtime_t i_system_start;
822 mtime_t i_stream_duration;
823 mtime_t i_system_duration;
824 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
825 &i_stream_start, &i_system_start,
826 &i_stream_duration, &i_system_duration );
833 if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
835 i_delay = i_stream_duration;
839 mtime_t i_system_duration;
840 if( p_sys->b_paused )
842 i_system_duration = p_sys->i_pause_date - i_system_start;
843 if( p_sys->i_buffering_extra_initial > 0 )
844 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
848 i_system_duration = mdate() - i_system_start;
851 const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
852 i_delay = p_sys->i_pts_delay - i_consumed;
859 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
860 const es_format_t *fmt, const char *psz_language,
863 es_out_sys_t *p_sys = out->p_sys;
864 input_thread_t *p_input = p_sys->p_input;
865 vlc_value_t val, text;
869 if( EsFmtIsTeletext( fmt ) )
870 input_SendEventTeletextDel( p_sys->p_input, i_id );
872 input_SendEventEsDel( p_input, fmt->i_cat, i_id );
876 /* Get the number of ES already added */
878 if( fmt->i_cat == AUDIO_ES )
879 psz_var = "audio-es";
880 else if( fmt->i_cat == VIDEO_ES )
881 psz_var = "video-es";
885 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
890 /* First one, we need to add the "Disable" choice */
891 val2.i_int = -1; text.psz_string = _("Disable");
892 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
896 /* Take care of the ES description */
897 if( fmt->psz_description && *fmt->psz_description )
899 if( psz_language && *psz_language )
901 text.psz_string = malloc( strlen( fmt->psz_description) +
902 strlen( psz_language ) + 10 );
903 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
906 else text.psz_string = strdup( fmt->psz_description );
910 if( psz_language && *psz_language )
912 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
913 text.psz_string = NULL;
917 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
918 text.psz_string = NULL;
922 input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
923 if( EsFmtIsTeletext( fmt ) )
926 snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
927 fmt->subs.teletext.i_magazine,
928 fmt->subs.teletext.i_page );
929 input_SendEventTeletextAdd( p_sys->p_input,
930 i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
933 free( text.psz_string );
936 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
939 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
942 /* EsOutProgramSelect:
943 * Select a program and update the object variable
945 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
947 es_out_sys_t *p_sys = out->p_sys;
948 input_thread_t *p_input = p_sys->p_input;
951 if( p_sys->p_pgrm == p_pgrm )
952 return; /* Nothing to do */
956 es_out_pgrm_t *old = p_sys->p_pgrm;
957 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
959 for( i = 0; i < p_sys->i_es; i++ )
961 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
962 p_sys->i_mode != ES_OUT_MODE_ALL )
963 EsUnselect( out, p_sys->es[i], true );
966 p_sys->p_es_audio = NULL;
967 p_sys->p_es_sub = NULL;
968 p_sys->p_es_video = NULL;
971 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
973 /* Mark it selected */
974 p_pgrm->b_selected = true;
976 /* Switch master stream */
977 p_sys->p_pgrm = p_pgrm;
979 /* Update "program" */
980 input_SendEventProgramSelect( p_input, p_pgrm->i_id );
983 input_SendEventEsDel( p_input, AUDIO_ES, -1 );
984 input_SendEventEsDel( p_input, VIDEO_ES, -1 );
985 input_SendEventEsDel( p_input, SPU_ES, -1 );
986 input_SendEventTeletextDel( p_input, -1 );
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->psz_name = NULL;
1022 p_pgrm->psz_now_playing = NULL;
1023 p_pgrm->psz_publisher = NULL;
1024 p_pgrm->p_epg = NULL;
1025 p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1026 if( !p_pgrm->p_clock )
1031 input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1035 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1037 /* Update "program" variable */
1038 input_SendEventProgramAdd( p_input, i_group, NULL );
1040 if( i_group == var_GetInteger( p_input, "program" ) )
1041 EsOutProgramSelect( out, p_pgrm );
1049 static int EsOutProgramDel( es_out_t *out, int i_group )
1051 es_out_sys_t *p_sys = out->p_sys;
1052 input_thread_t *p_input = p_sys->p_input;
1053 es_out_pgrm_t *p_pgrm = NULL;
1056 for( i = 0; i < p_sys->i_pgrm; i++ )
1058 if( p_sys->pgrm[i]->i_id == i_group )
1060 p_pgrm = p_sys->pgrm[i];
1065 if( p_pgrm == NULL )
1066 return VLC_EGENERIC;
1070 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1071 i_group, p_pgrm->i_es );
1072 return VLC_EGENERIC;
1075 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1077 /* If program is selected we need to unselect it */
1078 if( p_sys->p_pgrm == p_pgrm )
1079 p_sys->p_pgrm = NULL;
1081 input_clock_Delete( p_pgrm->p_clock );
1083 free( p_pgrm->psz_name );
1084 free( p_pgrm->psz_now_playing );
1085 free( p_pgrm->psz_publisher );
1087 vlc_epg_Delete( p_pgrm->p_epg );
1090 /* Update "program" variable */
1091 input_SendEventProgramDel( p_input, i_group );
1096 /* EsOutProgramMeta:
1098 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1101 if( p_pgrm->psz_name )
1103 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1108 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1114 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
1116 es_out_sys_t *p_sys = out->p_sys;
1117 es_out_pgrm_t *p_pgrm = NULL;
1118 input_thread_t *p_input = p_sys->p_input;
1120 const char *psz_title = NULL;
1121 const char *psz_provider = NULL;
1124 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1126 /* Check against empty meta data (empty for what we handle) */
1127 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1128 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1129 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1130 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1135 for( i = 0; i < p_sys->i_pgrm; i++ )
1137 if( p_sys->pgrm[i]->i_id == i_group )
1139 p_pgrm = p_sys->pgrm[i];
1143 if( p_pgrm == NULL )
1144 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1147 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1148 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1150 /* Update the description text of the program */
1151 if( psz_title && *psz_title )
1153 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1155 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1157 /* Remove old entries */
1158 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1159 /* TODO update epg name */
1162 free( p_pgrm->psz_name );
1163 p_pgrm->psz_name = strdup( psz_title );
1166 if( psz_provider && *psz_provider )
1168 if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1173 psz_text = strdup( psz_title );
1176 /* ugly but it works */
1179 input_SendEventProgramDel( p_input, i_group );
1180 input_SendEventProgramAdd( p_input, i_group, psz_text );
1186 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1189 if( p_sys->p_pgrm == p_pgrm )
1191 input_item_SetPublisher( p_input->p->p_item, psz_provider );
1192 input_SendEventMeta( p_input );
1194 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1196 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1197 for( i = 0; ppsz_all_keys[i]; i++ )
1199 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
1200 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
1201 free( ppsz_all_keys[i] );
1203 free( ppsz_all_keys );
1208 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
1213 for( i = 0; i < p_src->i_event; i++ )
1215 vlc_epg_event_t *p_evt = p_src->pp_event[i];
1219 for( j = 0; j < p_dst->i_event; j++ )
1221 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
1226 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
1231 vlc_epg_event_t *p_copy = calloc( 1, sizeof(vlc_epg_event_t) );
1234 p_copy->i_start = p_evt->i_start;
1235 p_copy->i_duration = p_evt->i_duration;
1236 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
1237 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
1238 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
1239 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
1242 /* Update current */
1243 if( p_src->p_current )
1244 vlc_epg_SetCurrent( p_dst, p_src->p_current->i_start );
1246 /* Keep only 1 old event */
1247 if( p_dst->p_current )
1249 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
1250 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
1254 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
1256 es_out_sys_t *p_sys = out->p_sys;
1257 input_thread_t *p_input = p_sys->p_input;
1258 es_out_pgrm_t *p_pgrm = NULL;
1263 for( i = 0; i < p_sys->i_pgrm; i++ )
1265 if( p_sys->pgrm[i]->i_id == i_group )
1267 p_pgrm = p_sys->pgrm[i];
1271 if( p_pgrm == NULL )
1272 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1275 if( !p_pgrm->p_epg )
1276 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1277 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1280 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1281 #ifdef HAVE_LOCALTIME_R
1283 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
1285 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
1286 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1287 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
1289 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
1290 time_t t_start = (time_t)p_evt->i_start;
1292 char psz_start[128];
1294 localtime_r( &t_start, &tm_start );
1296 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
1297 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
1298 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
1299 if( p_evt->psz_short_description || p_evt->psz_description )
1300 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
1302 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
1303 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
1305 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
1307 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
1311 /* Update now playing */
1312 free( p_pgrm->psz_now_playing );
1313 p_pgrm->psz_now_playing = NULL;
1314 if( p_pgrm->p_epg->p_current && p_pgrm->p_epg->p_current->psz_name && *p_pgrm->p_epg->p_current->psz_name )
1315 p_pgrm->psz_now_playing = strdup( p_pgrm->p_epg->p_current->psz_name );
1317 if( p_pgrm == p_sys->p_pgrm )
1319 input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1320 input_SendEventMeta( p_input );
1323 if( p_pgrm->psz_now_playing )
1325 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1326 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1327 p_pgrm->psz_now_playing );
1331 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1332 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1341 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1343 es_out_sys_t *p_sys = out->p_sys;
1344 input_thread_t *p_input = p_sys->p_input;
1346 if( fmt->i_group < 0 )
1348 msg_Err( p_input, "invalid group number" );
1352 es_out_id_t *es = malloc( sizeof( *es ) );
1353 es_out_pgrm_t *p_pgrm = NULL;
1359 vlc_mutex_lock( &p_sys->lock );
1361 /* Search the program */
1362 for( i = 0; i < p_sys->i_pgrm; i++ )
1364 if( fmt->i_group == p_sys->pgrm[i]->i_id )
1366 p_pgrm = p_sys->pgrm[i];
1370 if( p_pgrm == NULL )
1372 /* Create a new one */
1373 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1376 /* Increase ref count for program */
1380 es->p_pgrm = p_pgrm;
1381 es_format_Copy( &es->fmt, fmt );
1382 if( es->fmt.i_id < 0 )
1383 es->fmt.i_id = out->p_sys->i_id;
1384 es->i_id = es->fmt.i_id;
1385 es->i_meta_id = out->p_sys->i_id;
1387 switch( es->fmt.i_cat )
1391 audio_replay_gain_t rg;
1393 es->i_channel = p_sys->i_audio;
1395 memset( &rg, 0, sizeof(rg) );
1396 vlc_mutex_lock( &p_input->p->p_item->lock );
1397 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1398 vlc_mutex_unlock( &p_input->p->p_item->lock );
1400 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1402 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1404 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1405 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1407 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1409 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1410 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1417 es->i_channel = p_sys->i_video;
1418 if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1419 vlc_ureduce( &es->fmt.video.i_frame_rate,
1420 &es->fmt.video.i_frame_rate_base,
1421 es->fmt.video.i_frame_rate,
1422 es->fmt.video.i_frame_rate_base, 0 );
1426 es->i_channel = p_sys->i_sub;
1433 es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1434 es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1436 es->p_dec_record = NULL;
1437 for( i = 0; i < 4; i++ )
1438 es->pb_cc_present[i] = false;
1439 es->p_master = NULL;
1441 if( es->p_pgrm == p_sys->p_pgrm )
1442 EsOutESVarUpdate( out, es, false );
1444 /* Select it if needed */
1445 EsOutSelect( out, es, false );
1448 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1449 p_sys->i_id++; /* always incremented */
1450 switch( es->fmt.i_cat )
1463 EsOutUpdateInfo( out, es, &es->fmt, NULL );
1465 vlc_mutex_unlock( &p_sys->lock );
1470 static bool EsIsSelected( es_out_id_t *es )
1474 bool b_decode = false;
1475 if( es->p_master->p_dec )
1477 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1478 if( i_channel != -1 )
1479 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1485 return es->p_dec != NULL;
1488 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1490 es_out_sys_t *p_sys = out->p_sys;
1491 input_thread_t *p_input = p_sys->p_input;
1493 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1496 if( p_sys->b_buffering )
1497 input_DecoderStartBuffering( p_es->p_dec );
1499 if( !p_es->p_master && p_sys->p_sout_record )
1501 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1502 if( p_es->p_dec_record && p_sys->b_buffering )
1503 input_DecoderStartBuffering( p_es->p_dec_record );
1507 EsOutDecoderChangeDelay( out, p_es );
1509 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1516 input_DecoderDelete( p_es->p_dec );
1519 if( p_es->p_dec_record )
1521 input_DecoderDelete( p_es->p_dec_record );
1522 p_es->p_dec_record = NULL;
1526 static void EsSelect( es_out_t *out, es_out_id_t *es )
1528 es_out_sys_t *p_sys = out->p_sys;
1529 input_thread_t *p_input = p_sys->p_input;
1532 if( EsIsSelected( es ) )
1534 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1541 if( !es->p_master->p_dec )
1544 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1545 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1550 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1552 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1554 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1559 else if( es->fmt.i_cat == AUDIO_ES )
1561 var_Get( p_input, "audio", &val );
1562 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1564 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1569 if( es->fmt.i_cat == SPU_ES )
1571 var_Get( p_input, "spu", &val );
1572 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1574 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1580 EsCreateDecoder( out, es );
1582 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1586 /* Mark it as selected */
1587 input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1588 input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1591 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1593 es_out_sys_t *p_sys = out->p_sys;
1594 input_thread_t *p_input = p_sys->p_input;
1596 if( !EsIsSelected( es ) )
1598 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1604 if( es->p_master->p_dec )
1606 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1607 if( i_channel != -1 )
1608 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1613 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1615 for( i = 0; i < 4; i++ )
1617 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1620 if( i_spu_id == es->pp_cc_es[i]->i_id )
1622 /* Force unselection of the CC */
1623 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1625 EsOutDel( out, es->pp_cc_es[i] );
1627 es->pb_cc_present[i] = false;
1629 EsDestroyDecoder( out, es );
1635 /* Mark it as unselected */
1636 input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1637 if( EsFmtIsTeletext( &es->fmt ) )
1638 input_SendEventTeletextSelect( p_input, -1 );
1642 * Select an ES given the current mode
1643 * XXX: you need to take a the lock before (stream.stream_lock)
1645 * \param out The es_out structure
1646 * \param es es_out_id structure
1647 * \param b_force ...
1650 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1652 es_out_sys_t *p_sys = out->p_sys;
1654 int i_cat = es->fmt.i_cat;
1656 if( !p_sys->b_active ||
1657 ( !b_force && es->fmt.i_priority < 0 ) )
1662 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1664 if( !EsIsSelected( es ) )
1665 EsSelect( out, es );
1667 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1671 var_Get( p_sys->p_input, "programs", &val );
1672 for ( i = 0; i < val.p_list->i_count; i++ )
1674 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1676 if( !EsIsSelected( es ) )
1677 EsSelect( out, es );
1681 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1683 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1687 if( es->p_pgrm != p_sys->p_pgrm )
1690 if( i_cat == AUDIO_ES )
1692 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1693 es->psz_language_code );
1695 if( p_sys->p_es_audio &&
1696 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1698 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1699 p_sys->p_es_audio->psz_language_code );
1701 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1703 i_wanted = es->i_channel;
1707 /* Select audio if (no audio selected yet)
1708 * - no audio-language
1709 * - no audio code for the ES
1710 * - audio code in the requested list */
1712 !strcmp( es->psz_language_code, "??" ) ||
1713 !p_sys->ppsz_audio_language )
1714 i_wanted = es->i_channel;
1717 if( p_sys->i_audio_last >= 0 )
1718 i_wanted = p_sys->i_audio_last;
1720 if( p_sys->i_audio_id >= 0 )
1722 if( es->i_id == p_sys->i_audio_id )
1723 i_wanted = es->i_channel;
1728 else if( i_cat == SPU_ES )
1730 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1731 es->psz_language_code );
1733 if( p_sys->p_es_sub &&
1734 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1736 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1737 p_sys->p_es_sub->psz_language_code );
1739 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1740 idx1, es->psz_language_code, idx2,
1741 p_sys->p_es_sub->psz_language_code );
1743 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1745 /* We found a SPU that matches our language request */
1746 i_wanted = es->i_channel;
1748 else if( idx1 >= 0 )
1750 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1751 idx1, es->psz_language_code );
1753 i_wanted = es->i_channel;
1755 else if( p_sys->i_default_sub_id >= 0 )
1757 if( es->i_id == p_sys->i_default_sub_id )
1758 i_wanted = es->i_channel;
1761 if( p_sys->i_sub_last >= 0 )
1762 i_wanted = p_sys->i_sub_last;
1764 if( p_sys->i_sub_id >= 0 )
1766 if( es->i_id == p_sys->i_sub_id )
1767 i_wanted = es->i_channel;
1772 else if( i_cat == VIDEO_ES )
1774 i_wanted = es->i_channel;
1777 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1778 EsSelect( out, es );
1781 /* FIXME TODO handle priority here */
1782 if( EsIsSelected( es ) )
1784 if( i_cat == AUDIO_ES )
1786 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1787 p_sys->p_es_audio &&
1788 p_sys->p_es_audio != es &&
1789 EsIsSelected( p_sys->p_es_audio ) )
1791 EsUnselect( out, p_sys->p_es_audio, false );
1793 p_sys->p_es_audio = es;
1795 else if( i_cat == SPU_ES )
1797 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1799 p_sys->p_es_sub != es &&
1800 EsIsSelected( p_sys->p_es_sub ) )
1802 EsUnselect( out, p_sys->p_es_sub, false );
1804 p_sys->p_es_sub = es;
1806 else if( i_cat == VIDEO_ES )
1808 p_sys->p_es_video = es;
1814 * Send a block for the given es_out
1816 * \param out the es_out to send from
1817 * \param es the es_out_id
1818 * \param p_block the data block to send
1820 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1822 es_out_sys_t *p_sys = out->p_sys;
1823 input_thread_t *p_input = p_sys->p_input;
1826 if( libvlc_stats( p_input ) )
1828 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1829 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1830 p_block->i_buffer, &i_total );
1831 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1832 (float)i_total, NULL );
1833 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1836 vlc_mutex_lock( &p_sys->lock );
1838 /* Mark preroll blocks */
1839 if( p_sys->i_preroll_end >= 0 )
1841 int64_t i_date = p_block->i_pts;
1843 i_date = p_block->i_dts;
1845 if( i_date < p_sys->i_preroll_end )
1846 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1849 p_block->i_rate = 0;
1853 block_Release( p_block );
1854 vlc_mutex_unlock( &p_sys->lock );
1859 if( es->p_dec_record )
1861 block_t *p_dup = block_Duplicate( p_block );
1863 input_DecoderDecode( es->p_dec_record, p_dup );
1865 input_DecoderDecode( es->p_dec, p_block );
1867 es_format_t fmt_dsc;
1868 vlc_meta_t *p_meta_dsc;
1869 if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
1871 EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
1873 es_format_Clean( &fmt_dsc );
1875 vlc_meta_Delete( p_meta_dsc );
1878 /* Check CC status */
1881 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1882 for( int i = 0; i < 4; i++ )
1884 static const vlc_fourcc_t fcc[4] = {
1885 VLC_FOURCC('c', 'c', '1', ' '),
1886 VLC_FOURCC('c', 'c', '2', ' '),
1887 VLC_FOURCC('c', 'c', '3', ' '),
1888 VLC_FOURCC('c', 'c', '4', ' '),
1892 if( es->pb_cc_present[i] || !pb_cc[i] )
1894 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1896 es_format_Init( &fmt, SPU_ES, fcc[i] );
1897 fmt.i_group = es->fmt.i_group;
1898 if( asprintf( &fmt.psz_description,
1899 _("Closed captions %u"), 1 + i ) == -1 )
1900 fmt.psz_description = NULL;
1901 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1902 es->pp_cc_es[i]->p_master = es;
1903 es_format_Clean( &fmt );
1906 es->pb_cc_present[i] = true;
1909 vlc_mutex_unlock( &p_sys->lock );
1914 /*****************************************************************************
1916 *****************************************************************************/
1917 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1919 es_out_sys_t *p_sys = out->p_sys;
1920 bool b_reselect = false;
1923 vlc_mutex_lock( &p_sys->lock );
1925 /* We don't try to reselect */
1928 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
1930 if( input_DecoderIsEmpty( es->p_dec ) &&
1931 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
1933 /* FIXME there should be a way to have auto deleted es, but there will be
1934 * a problem when another codec of the same type is created (mainly video) */
1937 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1940 if( es->p_pgrm == p_sys->p_pgrm )
1941 EsOutESVarUpdate( out, es, true );
1943 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1946 if( es->p_pgrm->i_es == 0 )
1948 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1951 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1952 p_sys->p_es_sub == es ) b_reselect = true;
1954 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1955 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1956 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1958 switch( es->fmt.i_cat )
1971 /* Re-select another track when needed */
1974 for( i = 0; i < p_sys->i_es; i++ )
1976 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1977 EsOutSelect( out, p_sys->es[i], false );
1981 free( es->psz_language );
1982 free( es->psz_language_code );
1984 es_format_Clean( &es->fmt );
1986 vlc_mutex_unlock( &p_sys->lock );
1992 * Control query handler
1994 * \param out the es_out to control
1995 * \param i_query A es_out query as defined in include/ninput.h
1996 * \param args a variable list of arguments for the query
1997 * \return VLC_SUCCESS or an error code
1999 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2001 es_out_sys_t *p_sys = out->p_sys;
2009 case ES_OUT_SET_ES_STATE:
2010 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2011 b = (bool) va_arg( args, int );
2012 if( b && !EsIsSelected( es ) )
2014 EsSelect( out, es );
2015 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2017 else if( !b && EsIsSelected( es ) )
2019 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2024 case ES_OUT_GET_ES_STATE:
2025 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2026 pb = (bool*) va_arg( args, bool * );
2028 *pb = EsIsSelected( es );
2031 case ES_OUT_SET_ACTIVE:
2033 b = (bool) va_arg( args, int );
2034 if( b && !p_sys->b_active && p_sys->i_es > 0 )
2036 /* XXX Terminate vout if there are tracks but no video one.
2037 * This one is not mandatory but is he earliest place where it
2039 for( i = 0; i < p_sys->i_es; i++ )
2041 es_out_id_t *p_es = p_sys->es[i];
2042 if( p_es->fmt.i_cat == VIDEO_ES )
2045 if( i >= p_sys->i_es )
2046 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2048 p_sys->b_active = b;
2052 case ES_OUT_SET_MODE:
2053 i = (int) va_arg( args, int );
2054 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
2055 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
2059 /* Reapply policy mode */
2060 for( i = 0; i < p_sys->i_es; i++ )
2062 if( EsIsSelected( p_sys->es[i] ) )
2064 EsUnselect( out, p_sys->es[i],
2065 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2068 for( i = 0; i < p_sys->i_es; i++ )
2070 EsOutSelect( out, p_sys->es[i], false );
2074 return VLC_EGENERIC;
2077 case ES_OUT_RESTART_ES:
2081 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2085 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2087 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2089 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2094 for( i = 0; i < p_sys->i_es; i++ )
2098 if( es == p_sys->es[i] )
2100 EsOutSelect( out, es, true );
2106 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2108 if( EsIsSelected( p_sys->es[i] ) )
2110 if( i_query == ES_OUT_RESTART_ES )
2112 if( p_sys->es[i]->p_dec )
2114 EsDestroyDecoder( out, p_sys->es[i] );
2115 EsCreateDecoder( out, p_sys->es[i] );
2120 EsUnselect( out, p_sys->es[i],
2121 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2130 case ES_OUT_SET_ES_DEFAULT:
2132 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2136 /*p_sys->i_default_video_id = -1;*/
2137 /*p_sys->i_default_audio_id = -1;*/
2138 p_sys->i_default_sub_id = -1;
2140 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2142 /*p_sys->i_default_video_id = -1;*/
2144 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2146 /*p_sys->i_default_audio_id = -1;*/
2148 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2150 p_sys->i_default_sub_id = -1;
2154 /*if( es->fmt.i_cat == VIDEO_ES )
2155 p_sys->i_default_video_id = es->i_id;
2157 if( es->fmt.i_cat == AUDIO_ES )
2158 p_sys->i_default_audio_id = es->i_id;
2160 if( es->fmt.i_cat == SPU_ES )
2161 p_sys->i_default_sub_id = es->i_id;
2166 case ES_OUT_SET_PCR:
2167 case ES_OUT_SET_GROUP_PCR:
2169 es_out_pgrm_t *p_pgrm = NULL;
2173 if( i_query == ES_OUT_SET_PCR )
2175 p_pgrm = p_sys->p_pgrm;
2180 i_group = (int)va_arg( args, int );
2181 for( i = 0; i < p_sys->i_pgrm; i++ )
2183 if( p_sys->pgrm[i]->i_id == i_group )
2185 p_pgrm = p_sys->pgrm[i];
2190 if( p_pgrm == NULL )
2191 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2193 i_pcr = (int64_t)va_arg( args, int64_t );
2195 * TODO do not use mdate() but proper stream acquisition date */
2196 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2197 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering, i_pcr, mdate() );
2198 /* Check buffering state on master clock update */
2199 if( p_sys->b_buffering && p_pgrm == p_sys->p_pgrm )
2200 EsOutDecodersStopBuffering( out, false );
2205 case ES_OUT_RESET_PCR:
2206 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2207 EsOutChangePosition( out );
2210 case ES_OUT_SET_GROUP:
2213 i = (int) va_arg( args, int );
2214 for( j = 0; j < p_sys->i_pgrm; j++ )
2216 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2217 if( p_pgrm->i_id == i )
2219 EsOutProgramSelect( out, p_pgrm );
2223 return VLC_EGENERIC;
2226 case ES_OUT_SET_ES_FMT:
2228 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2229 * to update the p_extra data */
2231 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2232 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2234 return VLC_EGENERIC;
2236 if( p_fmt->i_extra )
2238 es->fmt.i_extra = p_fmt->i_extra;
2239 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2240 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2245 EsDestroyDecoder( out, es );
2247 EsCreateDecoder( out, es );
2249 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2250 es->p_dec->fmt_in.p_extra =
2251 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2252 memcpy( es->p_dec->fmt_in.p_extra,
2253 p_fmt->p_extra, p_fmt->i_extra );
2260 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2262 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2265 return VLC_EGENERIC;
2267 p_sys->i_preroll_end = i_date;
2271 case ES_OUT_SET_GROUP_META:
2273 int i_group = (int)va_arg( args, int );
2274 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2276 EsOutProgramMeta( out, i_group, p_meta );
2279 case ES_OUT_SET_GROUP_EPG:
2281 int i_group = (int)va_arg( args, int );
2282 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2284 EsOutProgramEpg( out, i_group, p_epg );
2287 case ES_OUT_DEL_GROUP:
2289 int i_group = (int)va_arg( args, int );
2291 return EsOutProgramDel( out, i_group );
2294 case ES_OUT_GET_WAKE_UP:
2296 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2297 *pi_wakeup = EsOutGetWakeup( out );
2301 case ES_OUT_SET_ES_BY_ID:
2302 case ES_OUT_RESTART_ES_BY_ID:
2303 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2305 const int i_id = (int)va_arg( args, int );
2306 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2311 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2312 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2313 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2317 /* TODO if the lock is made non recursive it should be changed */
2318 int i_ret = es_out_Control( out, i_new_query, p_es );
2320 /* Clean up vout after user action (in active mode only).
2321 * FIXME it does not work well with multiple video windows */
2322 if( p_sys->b_active )
2323 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2327 case ES_OUT_GET_BUFFERING:
2328 pb = (bool *)va_arg( args, bool* );
2329 *pb = p_sys->b_buffering;
2332 case ES_OUT_GET_EMPTY:
2333 pb = (bool *)va_arg( args, bool* );
2334 *pb = EsOutDecodersIsEmpty( out );
2337 case ES_OUT_SET_DELAY:
2339 const int i_cat = (int)va_arg( args, int );
2340 const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2341 EsOutSetDelay( out, i_cat, i_delay );
2345 case ES_OUT_SET_RECORD_STATE:
2346 b = (bool) va_arg( args, int );
2347 return EsOutSetRecord( out, b );
2349 case ES_OUT_SET_PAUSE_STATE:
2351 const bool b_source_paused = (bool)va_arg( args, int );
2352 const bool b_paused = (bool)va_arg( args, int );
2353 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2355 assert( !b_source_paused == !b_paused );
2356 EsOutChangePause( out, b_paused, i_date );
2361 case ES_OUT_SET_RATE:
2363 const int i_src_rate = (int)va_arg( args, int );
2364 const int i_rate = (int)va_arg( args, int );
2366 assert( i_src_rate == i_rate );
2367 EsOutChangeRate( out, i_rate );
2372 case ES_OUT_SET_TIME:
2374 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2376 assert( i_date == -1 );
2377 EsOutChangePosition( out );
2382 case ES_OUT_SET_FRAME_NEXT:
2383 EsOutFrameNext( out );
2386 case ES_OUT_SET_TIMES:
2388 double f_position = (double)va_arg( args, double );
2389 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2390 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2392 /* Fix for buffering delay */
2393 const mtime_t i_delay = EsOutGetBuffering( out );
2400 f_position -= (double)i_delay / i_length;
2401 if( f_position < 0 )
2404 if( !p_sys->b_buffering )
2405 input_SendEventTimes( p_sys->p_input, f_position, i_time, i_length );
2408 case ES_OUT_SET_JITTER:
2410 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2411 int i_cr_average = (int)va_arg( args, int );
2413 if( i_pts_delay == p_sys->i_pts_delay &&
2414 i_cr_average == p_sys->i_cr_average )
2417 p_sys->i_pts_delay = i_pts_delay;
2418 p_sys->i_cr_average = i_cr_average;
2420 for( int i = 0; i < p_sys->i_pgrm; i++ )
2421 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2422 i_pts_delay, i_cr_average );
2427 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2428 return VLC_EGENERIC;
2431 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2433 es_out_sys_t *p_sys = out->p_sys;
2436 vlc_mutex_lock( &p_sys->lock );
2437 i_ret = EsOutControlLocked( out, i_query, args );
2438 vlc_mutex_unlock( &p_sys->lock );
2443 /****************************************************************************
2444 * LanguageGetName: try to expend iso639 into plain name
2445 ****************************************************************************/
2446 static char *LanguageGetName( const char *psz_code )
2448 const iso639_lang_t *pl;
2450 if( psz_code == NULL )
2452 return strdup( "" );
2455 if( strlen( psz_code ) == 2 )
2457 pl = GetLang_1( psz_code );
2459 else if( strlen( psz_code ) == 3 )
2461 pl = GetLang_2B( psz_code );
2462 if( !strcmp( pl->psz_iso639_1, "??" ) )
2464 pl = GetLang_2T( psz_code );
2469 return strdup( psz_code );
2472 if( !strcmp( pl->psz_iso639_1, "??" ) )
2474 return strdup( psz_code );
2478 if( *pl->psz_native_name )
2480 return strdup( pl->psz_native_name );
2482 return strdup( pl->psz_eng_name );
2486 /* Get a 2 char code */
2487 static char *LanguageGetCode( const char *psz_lang )
2489 const iso639_lang_t *pl;
2491 if( psz_lang == NULL || *psz_lang == '\0' )
2492 return strdup("??");
2494 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2496 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2497 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2498 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2499 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2500 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2504 if( pl->psz_iso639_1 != NULL )
2505 return strdup( pl->psz_iso639_1 );
2507 return strdup("??");
2510 static char **LanguageSplit( const char *psz_langs )
2517 if( psz_langs == NULL ) return NULL;
2519 psz_parser = psz_dup = strdup(psz_langs);
2521 while( psz_parser && *psz_parser )
2526 psz = strchr(psz_parser, ',' );
2527 if( psz ) *psz++ = '\0';
2529 if( !strcmp( psz_parser, "any" ) )
2531 TAB_APPEND( i_psz, ppsz, strdup("any") );
2535 psz_code = LanguageGetCode( psz_parser );
2536 if( strcmp( psz_code, "??" ) )
2538 TAB_APPEND( i_psz, ppsz, psz_code );
2551 TAB_APPEND( i_psz, ppsz, NULL );
2558 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2562 if( !ppsz_langs || !psz_lang ) return -1;
2564 for( i = 0; ppsz_langs[i]; i++ )
2566 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2567 !strcasecmp( ppsz_langs[i], "any" ) )
2576 /****************************************************************************
2578 * - add meta info to the playlist item
2579 ****************************************************************************/
2580 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2582 es_out_sys_t *p_sys = out->p_sys;
2583 input_thread_t *p_input = p_sys->p_input;
2584 const es_format_t *p_fmt_es = &es->fmt;
2588 /* Create category name */
2589 if( asprintf( &psz_cat, _("Stream %d"), es->i_meta_id ) == -1 )
2592 /* Remove previous information */
2593 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
2595 /* Add informations */
2596 const char *psz_type;
2597 switch( fmt->i_cat )
2600 psz_type = _("Audio");
2603 psz_type = _("Video");
2606 psz_type = _("Subtitle");
2614 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Type"), psz_type );
2616 if( es->i_meta_id != es->i_id )
2617 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Original ID"),
2620 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2621 "%.4s", (char*)&p_fmt_es->i_codec );
2623 if( es->psz_language && *es->psz_language )
2624 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2625 "%s", es->psz_language );
2626 if( fmt->psz_description && *fmt->psz_description )
2627 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
2628 "%s", fmt->psz_description );
2630 switch( fmt->i_cat )
2633 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2634 _("Type"), _("Audio") );
2636 if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
2637 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2638 "%s", _( aout_FormatPrintChannels( &fmt->audio ) ) );
2639 else if( fmt->audio.i_channels > 0 )
2640 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2641 "%u", fmt->audio.i_channels );
2643 if( fmt->audio.i_rate > 0 )
2645 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2646 _("%u Hz"), fmt->audio.i_rate );
2647 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2648 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2651 unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2652 if( i_bitspersample <= 0 )
2653 i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2654 if( i_bitspersample > 0 )
2655 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2656 _("Bits per sample"), "%u",
2659 if( fmt->i_bitrate > 0 )
2661 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2662 _("%u kb/s"), fmt->i_bitrate / 1000 );
2663 /* FIXME that should be removed or improved ! (used by text/strings.c) */
2664 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2666 for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2668 const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2669 if( !p_rg->pb_gain[i] )
2671 const char *psz_name;
2672 if( i == AUDIO_REPLAY_GAIN_TRACK )
2673 psz_name = _("Track replay gain");
2675 psz_name = _("Album replay gain");
2676 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2677 psz_name, _("%.2f dB"), p_rg->pf_gain[i] );
2682 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2683 _("Type"), _("Video") );
2685 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2686 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2687 _("Resolution"), "%ux%u",
2688 fmt->video.i_width, fmt->video.i_height );
2690 if( fmt->video.i_visible_width > 0 &&
2691 fmt->video.i_visible_height > 0 )
2692 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2693 _("Display resolution"), "%ux%u",
2694 fmt->video.i_visible_width,
2695 fmt->video.i_visible_height);
2696 if( fmt->video.i_frame_rate > 0 &&
2697 fmt->video.i_frame_rate_base > 0 )
2699 div = lldiv( (float)fmt->video.i_frame_rate /
2700 fmt->video.i_frame_rate_base * 1000000,
2703 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2704 _("Frame rate"), "%"PRId64".%06u",
2705 div.quot, (unsigned int )div.rem );
2707 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2708 _("Frame rate"), "%"PRId64, div.quot );
2713 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2714 _("Type"), _("Subtitle") );
2721 /* Append generic meta */
2724 char **ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2725 for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
2727 char *psz_key = ppsz_all_keys[i];
2728 char *psz_value = vlc_dictionary_value_for_key( &p_meta->extra_tags, psz_key );
2731 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(psz_key), _(psz_value) );
2734 free( ppsz_all_keys );