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"
46 #include "../stream_output/stream_output.h"
48 #include <vlc_iso_lang.h>
49 /* FIXME we should find a better way than including that */
50 #include "../text/iso-639_def.h"
52 /*****************************************************************************
54 *****************************************************************************/
60 /* Number of es for this pgrm */
65 /* Clock for this program */
66 input_clock_t *p_clock;
69 char *psz_now_playing;
79 es_out_pgrm_t *p_pgrm;
81 /* Channel in the track type */
85 char *psz_language_code;
88 decoder_t *p_dec_record;
90 /* Fields for Video with CC */
91 bool pb_cc_present[4];
92 es_out_id_t *pp_cc_es[4];
94 /* Field for CC track from a master video */
95 es_out_id_t *p_master;
100 input_thread_t *p_input;
107 es_out_pgrm_t **pgrm;
108 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
109 es_out_pgrm_t *p_pgrm; /* Master program */
126 int i_audio_last, i_audio_id;
127 int i_sub_last, i_sub_id;
128 int i_default_sub_id; /* As specified in container; if applicable */
129 char **ppsz_audio_language;
130 char **ppsz_sub_language;
132 /* current main es */
133 es_out_id_t *p_es_audio;
134 es_out_id_t *p_es_video;
135 es_out_id_t *p_es_sub;
138 int64_t i_audio_delay;
141 /* Rate used for clock */
147 /* Current preroll */
148 mtime_t i_preroll_end;
150 /* Used for buffering */
152 mtime_t i_buffering_extra_initial;
153 mtime_t i_buffering_extra_stream;
154 mtime_t i_buffering_extra_system;
157 sout_instance_t *p_sout_record;
160 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
161 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
162 static void EsOutDel ( es_out_t *, es_out_id_t * );
163 static int EsOutControl( es_out_t *, int i_query, va_list );
164 static void EsOutDelete ( es_out_t *out );
166 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
167 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
169 static bool EsIsSelected( es_out_id_t *es );
170 static void EsSelect( es_out_t *out, es_out_id_t *es );
171 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
172 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
173 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
174 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
175 static void EsOutProgramsChangeRate( es_out_t *out );
176 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
177 static char *LanguageGetName( const char *psz_code );
178 static char *LanguageGetCode( const char *psz_lang );
179 static char **LanguageSplit( const char *psz_langs );
180 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
182 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
184 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
185 VLC_FOURCC('c', 'c', '1', ' '),
186 VLC_FOURCC('c', 'c', '2', ' '),
187 VLC_FOURCC('c', 'c', '3', ' '),
188 VLC_FOURCC('c', 'c', '4', ' '),
190 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
193 for( i = 0; i < 4; i++ )
195 if( fcc == EsOutFourccClosedCaptions[i] )
202 /*****************************************************************************
204 *****************************************************************************/
205 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
210 es_out_t *out = malloc( sizeof( *out ) );
214 es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
221 out->pf_add = EsOutAdd;
222 out->pf_send = EsOutSend;
223 out->pf_del = EsOutDel;
224 out->pf_control = EsOutControl;
225 out->pf_destroy = EsOutDelete;
227 out->b_sout = p_input->p->p_sout != NULL;
230 vlc_mutex_init_recursive( &p_sys->lock );
231 p_sys->p_input = p_input;
233 p_sys->b_active = false;
234 p_sys->i_mode = ES_OUT_MODE_AUTO;
237 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
238 p_sys->p_pgrm = NULL;
242 TAB_INIT( p_sys->i_es, p_sys->es );
249 var_Get( p_input, "audio-track", &val );
250 p_sys->i_audio_last = val.i_int;
252 var_Get( p_input, "sub-track", &val );
253 p_sys->i_sub_last = val.i_int;
255 p_sys->i_default_sub_id = -1;
257 if( !p_input->b_preparsing )
259 var_Get( p_input, "audio-language", &val );
260 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
261 if( p_sys->ppsz_audio_language )
263 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
264 msg_Dbg( p_input, "selected audio language[%d] %s",
265 i, p_sys->ppsz_audio_language[i] );
267 free( val.psz_string );
269 var_Get( p_input, "sub-language", &val );
270 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
271 if( p_sys->ppsz_sub_language )
273 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
274 msg_Dbg( p_input, "selected subtitle language[%d] %s",
275 i, p_sys->ppsz_sub_language[i] );
277 free( val.psz_string );
281 p_sys->ppsz_sub_language = NULL;
282 p_sys->ppsz_audio_language = NULL;
285 var_Get( p_input, "audio-track-id", &val );
286 p_sys->i_audio_id = val.i_int;
288 var_Get( p_input, "sub-track-id", &val );
289 p_sys->i_sub_id = val.i_int;
291 p_sys->p_es_audio = NULL;
292 p_sys->p_es_video = NULL;
293 p_sys->p_es_sub = NULL;
295 p_sys->i_audio_delay= 0;
296 p_sys->i_spu_delay = 0;
298 p_sys->b_paused = false;
300 p_sys->i_rate = i_rate;
302 p_sys->b_buffering = true;
303 p_sys->i_buffering_extra_initial = 0;
304 p_sys->i_buffering_extra_stream = 0;
305 p_sys->i_buffering_extra_system = 0;
306 p_sys->i_preroll_end = -1;
308 p_sys->p_sout_record = NULL;
313 void input_EsOutChangeRate( es_out_t *out, int i_rate )
315 es_out_sys_t *p_sys = out->p_sys;
317 p_sys->i_rate = i_rate;
319 if( !p_sys->b_paused )
320 EsOutProgramsChangeRate( out );
323 int input_EsOutSetRecord( es_out_t *out, bool b_record )
325 es_out_sys_t *p_sys = out->p_sys;
326 input_thread_t *p_input = p_sys->p_input;
328 assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
332 char *psz_path = var_CreateGetString( p_input, "input-record-path" );
333 if( !psz_path || *psz_path == '\0' )
336 psz_path = strdup( config_GetHomeDir() );
339 char *psz_sout = NULL; // TODO conf
341 if( !psz_sout && psz_path )
343 char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
346 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
357 p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
361 if( !p_sys->p_sout_record )
364 for( int i = 0; i < p_sys->i_es; i++ )
366 es_out_id_t *p_es = p_sys->es[i];
368 if( !p_es->p_dec || p_es->p_master )
371 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
372 if( p_es->p_dec_record && p_sys->b_buffering )
373 input_DecoderStartBuffering( p_es->p_dec_record );
378 for( int i = 0; i < p_sys->i_es; i++ )
380 es_out_id_t *p_es = p_sys->es[i];
382 if( !p_es->p_dec_record )
385 input_DecoderDelete( p_es->p_dec_record );
386 p_es->p_dec_record = NULL;
389 sout_DeleteInstance( p_sys->p_sout_record );
391 p_sys->p_sout_record = NULL;
396 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
398 es_out_sys_t *p_sys = out->p_sys;
400 if( i_cat == AUDIO_ES )
401 p_sys->i_audio_delay = i_delay;
402 else if( i_cat == SPU_ES )
403 p_sys->i_spu_delay = i_delay;
405 for( int i = 0; i < p_sys->i_es; i++ )
406 EsOutDecoderChangeDelay( out, p_sys->es[i] );
408 void input_EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
410 es_out_sys_t *p_sys = out->p_sys;
412 /* XXX the order is important */
415 EsOutDecodersChangePause( out, true, i_date );
416 EsOutProgramChangePause( out, true, i_date );
420 if( p_sys->i_buffering_extra_initial > 0 )
422 mtime_t i_stream_start;
423 mtime_t i_system_start;
424 mtime_t i_stream_duration;
425 mtime_t i_system_duration;
427 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
428 &i_stream_start, &i_system_start,
429 &i_stream_duration, &i_system_duration );
432 /* FIXME pcr != exactly what wanted */
433 const mtime_t i_used = /*(i_stream_duration - p_sys->p_input->i_pts_delay)*/ p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
436 p_sys->i_buffering_extra_initial = 0;
437 p_sys->i_buffering_extra_stream = 0;
438 p_sys->i_buffering_extra_system = 0;
440 EsOutProgramChangePause( out, false, i_date );
441 EsOutDecodersChangePause( out, false, i_date );
443 EsOutProgramsChangeRate( out );
445 p_sys->b_paused = b_paused;
447 void input_EsOutChangePosition( es_out_t *out )
449 es_out_sys_t *p_sys = out->p_sys;
451 for( int i = 0; i < p_sys->i_es; i++ )
453 es_out_id_t *p_es = p_sys->es[i];
458 input_DecoderStartBuffering( p_es->p_dec );
460 if( p_es->p_dec_record )
461 input_DecoderStartBuffering( p_es->p_dec );
464 for( int i = 0; i < p_sys->i_pgrm; i++ )
465 input_clock_Reset( p_sys->pgrm[i]->p_clock );
467 p_sys->b_buffering = true;
468 p_sys->i_buffering_extra_initial = 0;
469 p_sys->i_buffering_extra_stream = 0;
470 p_sys->i_buffering_extra_system = 0;
471 p_sys->i_preroll_end = -1;
474 void input_EsOutFrameNext( es_out_t *out )
476 es_out_sys_t *p_sys = out->p_sys;
477 es_out_id_t *p_es_video = NULL;
479 if( p_sys->b_buffering )
481 msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
485 assert( p_sys->b_paused );
487 for( int i = 0; i < p_sys->i_es; i++ )
489 es_out_id_t *p_es = p_sys->es[i];
491 if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
500 msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
505 input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
507 msg_Dbg( out->p_sys->p_input, "input_EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
509 if( i_duration <= 0 )
510 i_duration = 40*1000;
512 /* FIXME it is not a clean way ? */
513 if( p_sys->i_buffering_extra_initial <= 0 )
515 mtime_t i_stream_start;
516 mtime_t i_system_start;
517 mtime_t i_stream_duration;
518 mtime_t i_system_duration;
521 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
522 &i_stream_start, &i_system_start,
523 &i_stream_duration, &i_system_duration );
527 p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->p_input->i_pts_delay; /* FIXME < 0 ? */
528 p_sys->i_buffering_extra_system =
529 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
532 const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
534 p_sys->b_buffering = true;
535 p_sys->i_buffering_extra_system += i_duration;
536 p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
537 ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
538 INPUT_RATE_DEFAULT / i_rate;
540 p_sys->i_preroll_end = -1;
543 void input_EsOutLock( es_out_t *out )
545 vlc_mutex_lock( &out->p_sys->lock );
547 void input_EsOutUnlock( es_out_t *out )
549 vlc_mutex_unlock( &out->p_sys->lock );
552 /*****************************************************************************
554 *****************************************************************************/
555 static void EsOutDelete( es_out_t *out )
557 es_out_sys_t *p_sys = out->p_sys;
560 if( p_sys->p_sout_record )
561 input_EsOutSetRecord( out, false );
563 for( i = 0; i < p_sys->i_es; i++ )
565 if( p_sys->es[i]->p_dec )
566 input_DecoderDelete( p_sys->es[i]->p_dec );
568 free( p_sys->es[i]->psz_language );
569 free( p_sys->es[i]->psz_language_code );
570 es_format_Clean( &p_sys->es[i]->fmt );
572 free( p_sys->es[i] );
574 if( p_sys->ppsz_audio_language )
576 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
577 free( p_sys->ppsz_audio_language[i] );
578 free( p_sys->ppsz_audio_language );
580 if( p_sys->ppsz_sub_language )
582 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
583 free( p_sys->ppsz_sub_language[i] );
584 free( p_sys->ppsz_sub_language );
588 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
589 for( i = 0; i < p_sys->i_pgrm; i++ )
591 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
592 input_clock_Delete( p_pgrm->p_clock );
593 free( p_pgrm->psz_now_playing );
594 free( p_pgrm->psz_publisher );
595 free( p_pgrm->psz_name );
597 vlc_epg_Delete( p_pgrm->p_epg );
601 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
602 vlc_mutex_destroy( &p_sys->lock );
608 static mtime_t EsOutGetWakeup( es_out_t *out )
610 es_out_sys_t *p_sys = out->p_sys;
611 input_thread_t *p_input = p_sys->p_input;
616 /* We do not have a wake up date if the input cannot have its speed
617 * controlled or sout is imposing its own or while buffering
619 * FIXME for !p_input->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
620 if( !p_input->b_can_pace_control ||
621 p_input->p->b_out_pace_control ||
625 return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
628 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
633 /* Special HACK, -i_id is the cat of the stream */
634 return (es_out_id_t*)((uint8_t*)NULL-i_id);
637 for( i = 0; i < out->p_sys->i_es; i++ )
639 if( out->p_sys->es[i]->i_id == i_id )
640 return out->p_sys->es[i];
645 static bool EsOutDecodersIsEmpty( es_out_t *out )
647 es_out_sys_t *p_sys = out->p_sys;
650 if( p_sys->b_buffering && p_sys->p_pgrm )
652 EsOutDecodersStopBuffering( out, true );
653 if( p_sys->b_buffering )
657 for( i = 0; i < p_sys->i_es; i++ )
659 es_out_id_t *es = p_sys->es[i];
661 if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
663 if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
670 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
672 es_out_sys_t *p_sys = out->p_sys;
675 mtime_t i_stream_start;
676 mtime_t i_system_start;
677 mtime_t i_stream_duration;
678 mtime_t i_system_duration;
679 i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
680 &i_stream_start, &i_system_start,
681 &i_stream_duration, &i_system_duration );
682 assert( !i_ret || b_forced );
686 mtime_t i_preroll_duration = 0;
687 if( p_sys->i_preroll_end >= 0 )
688 i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
690 const mtime_t i_buffering_duration = p_sys->p_input->i_pts_delay +
692 p_sys->i_buffering_extra_stream;
694 if( i_stream_duration <= i_buffering_duration && !b_forced )
696 msg_Dbg( p_sys->p_input, "Buffering %d%%",
697 (int)(100 * i_stream_duration / i_buffering_duration ) );
701 msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
702 (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
703 p_sys->b_buffering = false;
704 p_sys->i_preroll_end = -1;
706 if( p_sys->i_buffering_extra_initial > 0 )
712 const mtime_t i_decoder_buffering_start = mdate();
713 for( int i = 0; i < p_sys->i_es; i++ )
715 es_out_id_t *p_es = p_sys->es[i];
719 input_DecoderWaitBuffering( p_es->p_dec );
720 if( p_es->p_dec_record )
721 input_DecoderWaitBuffering( p_es->p_dec_record );
724 msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
725 (int)(mdate() - i_decoder_buffering_start)/1000 );
727 const mtime_t i_ts_delay = 10*1000 + /* FIXME CLEANUP thread wake up time*/
729 //msg_Dbg( p_sys->p_input, "==> %lld", i_ts_delay - p_sys->p_input->i_pts_delay );
730 input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_ts_delay - i_buffering_duration );
732 for( int i = 0; i < p_sys->i_es; i++ )
734 es_out_id_t *p_es = p_sys->es[i];
739 input_DecoderStopBuffering( p_es->p_dec );
740 if( p_es->p_dec_record )
741 input_DecoderStopBuffering( p_es->p_dec );
744 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
746 es_out_sys_t *p_sys = out->p_sys;
748 /* Pause decoders first */
749 for( int i = 0; i < p_sys->i_es; i++ )
751 es_out_id_t *es = p_sys->es[i];
755 input_DecoderChangePause( es->p_dec, b_paused, i_date );
756 if( es->p_dec_record )
757 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
761 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
763 es_out_sys_t *p_sys = out->p_sys;
765 for( int i = 0; i < p_sys->i_pgrm; i++ )
766 input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
769 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
771 es_out_sys_t *p_sys = out->p_sys;
774 if( p_es->fmt.i_cat == AUDIO_ES )
775 i_delay = p_sys->i_audio_delay;
776 else if( p_es->fmt.i_cat == SPU_ES )
777 i_delay = p_sys->i_spu_delay;
782 input_DecoderChangeDelay( p_es->p_dec, i_delay );
783 if( p_es->p_dec_record )
784 input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
787 static void EsOutProgramsChangeRate( es_out_t *out )
789 es_out_sys_t *p_sys = out->p_sys;
791 for( int i = 0; i < p_sys->i_pgrm; i++ )
792 input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
795 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
798 es_out_sys_t *p_sys = out->p_sys;
799 input_thread_t *p_input = p_sys->p_input;
800 const bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
801 vlc_value_t val, text;
805 if( fmt->i_cat == AUDIO_ES )
806 psz_var = "audio-es";
807 else if( fmt->i_cat == VIDEO_ES )
808 psz_var = "video-es";
809 else if( fmt->i_cat == SPU_ES )
817 var_SetInteger( p_sys->p_input, "teletext-es", -1 );
820 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
822 var_SetBool( p_sys->p_input, "intf-change", true );
826 /* Get the number of ES already added */
827 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
832 /* First one, we need to add the "Disable" choice */
833 val2.i_int = -1; text.psz_string = _("Disable");
834 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
838 /* Take care of the ES description */
839 if( fmt->psz_description && *fmt->psz_description )
841 if( psz_language && *psz_language )
843 text.psz_string = malloc( strlen( fmt->psz_description) +
844 strlen( psz_language ) + 10 );
845 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
848 else text.psz_string = strdup( fmt->psz_description );
852 if( psz_language && *psz_language )
854 if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
855 text.psz_string = NULL;
859 if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
860 text.psz_string = NULL;
865 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
867 free( text.psz_string );
871 if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
872 var_SetInteger( p_sys->p_input, "teletext-es", i_id );
875 var_SetBool( p_sys->p_input, "intf-change", true );
878 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
881 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
884 /* EsOutProgramSelect:
885 * Select a program and update the object variable
887 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
889 es_out_sys_t *p_sys = out->p_sys;
890 input_thread_t *p_input = p_sys->p_input;
894 if( p_sys->p_pgrm == p_pgrm )
895 return; /* Nothing to do */
899 es_out_pgrm_t *old = p_sys->p_pgrm;
900 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
902 for( i = 0; i < p_sys->i_es; i++ )
904 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
905 p_sys->i_mode != ES_OUT_MODE_ALL )
906 EsUnselect( out, p_sys->es[i], true );
909 p_sys->p_es_audio = NULL;
910 p_sys->p_es_sub = NULL;
911 p_sys->p_es_video = NULL;
914 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
916 /* Mark it selected */
917 p_pgrm->b_selected = true;
919 /* Switch master stream */
920 p_sys->p_pgrm = p_pgrm;
922 /* Update "program" */
923 val.i_int = p_pgrm->i_id;
924 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
927 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
928 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
929 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
930 var_SetInteger( p_input, "teletext-es", -1 );
931 for( i = 0; i < p_sys->i_es; i++ )
933 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
934 EsOutESVarUpdate( out, p_sys->es[i], false );
935 EsOutSelect( out, p_sys->es[i], false );
938 /* Update now playing */
939 input_item_SetNowPlaying( p_input->p->input.p_item,
940 p_pgrm->psz_now_playing );
941 input_item_SetPublisher( p_input->p->input.p_item,
942 p_pgrm->psz_publisher );
944 var_SetBool( p_sys->p_input, "intf-change", true );
950 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
952 es_out_sys_t *p_sys = out->p_sys;
953 input_thread_t *p_input = p_sys->p_input;
956 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
961 p_pgrm->i_id = i_group;
963 p_pgrm->b_selected = false;
964 p_pgrm->psz_name = NULL;
965 p_pgrm->psz_now_playing = NULL;
966 p_pgrm->psz_publisher = NULL;
967 p_pgrm->p_epg = NULL;
968 p_pgrm->p_clock = input_clock_New( p_input->p->input.i_cr_average, p_sys->i_rate );
969 if( !p_pgrm->p_clock )
976 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
978 /* Update "program" variable */
980 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
982 if( i_group == var_GetInteger( p_input, "program" ) )
984 EsOutProgramSelect( out, p_pgrm );
988 var_SetBool( p_sys->p_input, "intf-change", true );
996 static int EsOutProgramDel( es_out_t *out, int i_group )
998 es_out_sys_t *p_sys = out->p_sys;
999 input_thread_t *p_input = p_sys->p_input;
1000 es_out_pgrm_t *p_pgrm = NULL;
1004 for( i = 0; i < p_sys->i_pgrm; i++ )
1006 if( p_sys->pgrm[i]->i_id == i_group )
1008 p_pgrm = p_sys->pgrm[i];
1013 if( p_pgrm == NULL )
1014 return VLC_EGENERIC;
1018 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1019 i_group, p_pgrm->i_es );
1020 return VLC_EGENERIC;
1023 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1025 /* If program is selected we need to unselect it */
1026 if( p_sys->p_pgrm == p_pgrm )
1027 p_sys->p_pgrm = NULL;
1029 input_clock_Delete( p_pgrm->p_clock );
1031 free( p_pgrm->psz_name );
1032 free( p_pgrm->psz_now_playing );
1033 free( p_pgrm->psz_publisher );
1035 vlc_epg_Delete( p_pgrm->p_epg );
1038 /* Update "program" variable */
1039 val.i_int = i_group;
1040 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
1042 var_SetBool( p_sys->p_input, "intf-change", true );
1047 /* EsOutProgramMeta:
1049 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1052 if( p_pgrm->psz_name )
1054 if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1059 if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1065 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
1067 es_out_sys_t *p_sys = out->p_sys;
1068 es_out_pgrm_t *p_pgrm = NULL;
1069 input_thread_t *p_input = p_sys->p_input;
1071 const char *psz_title = NULL;
1072 const char *psz_provider = NULL;
1075 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1077 /* Check against empty meta data (empty for what we handle) */
1078 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1079 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1080 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1081 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1086 for( i = 0; i < p_sys->i_pgrm; i++ )
1088 if( p_sys->pgrm[i]->i_id == i_group )
1090 p_pgrm = p_sys->pgrm[i];
1094 if( p_pgrm == NULL )
1095 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1098 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1099 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1101 /* Update the description text of the program */
1102 if( psz_title && *psz_title )
1107 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1109 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1111 /* Remove old entries */
1112 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1113 /* TODO update epg name */
1116 free( p_pgrm->psz_name );
1117 p_pgrm->psz_name = strdup( psz_title );
1119 /* ugly but it works */
1120 val.i_int = i_group;
1121 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
1123 if( psz_provider && *psz_provider )
1125 if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
1127 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
1128 free( text.psz_string );
1133 text.psz_string = (char *)psz_title;
1134 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
1138 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1141 if( p_sys->p_pgrm == p_pgrm )
1142 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
1143 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1145 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1146 for( i = 0; ppsz_all_keys[i]; i++ )
1148 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
1149 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
1150 free( ppsz_all_keys[i] );
1152 free( ppsz_all_keys );
1157 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
1162 for( i = 0; i < p_src->i_event; i++ )
1164 vlc_epg_event_t *p_evt = p_src->pp_event[i];
1168 for( j = 0; j < p_dst->i_event; j++ )
1170 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
1175 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
1180 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
1183 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
1184 p_copy->i_start = p_evt->i_start;
1185 p_copy->i_duration = p_evt->i_duration;
1186 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
1187 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
1188 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
1189 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
1192 /* Update current */
1193 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
1195 /* Keep only 1 old event */
1196 if( p_dst->p_current )
1198 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
1199 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
1203 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
1205 es_out_sys_t *p_sys = out->p_sys;
1206 input_thread_t *p_input = p_sys->p_input;
1207 es_out_pgrm_t *p_pgrm = NULL;
1212 for( i = 0; i < p_sys->i_pgrm; i++ )
1214 if( p_sys->pgrm[i]->i_id == i_group )
1216 p_pgrm = p_sys->pgrm[i];
1220 if( p_pgrm == NULL )
1221 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1224 if( !p_pgrm->p_epg )
1225 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1226 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1229 psz_cat = EsOutProgramGetMetaName( p_pgrm );
1230 #ifdef HAVE_LOCALTIME_R
1232 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
1234 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
1235 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1236 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
1238 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
1239 time_t t_start = (time_t)p_evt->i_start;
1241 char psz_start[128];
1243 localtime_r( &t_start, &tm_start );
1245 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
1246 if( p_evt->psz_short_description || p_evt->psz_description )
1247 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
1249 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
1250 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
1252 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
1254 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
1258 /* Update now playing */
1259 free( p_pgrm->psz_now_playing );
1260 p_pgrm->psz_now_playing = NULL;
1261 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
1262 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
1264 if( p_pgrm == p_sys->p_pgrm )
1265 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
1267 if( p_pgrm->psz_now_playing )
1269 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1270 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1271 p_pgrm->psz_now_playing );
1275 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1276 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1285 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
1287 es_out_sys_t *p_sys = out->p_sys;
1288 input_thread_t *p_input = p_sys->p_input;
1290 if( fmt->i_group < 0 )
1292 msg_Err( p_input, "invalid group number" );
1296 es_out_id_t *es = malloc( sizeof( *es ) );
1297 es_out_pgrm_t *p_pgrm = NULL;
1303 vlc_mutex_lock( &p_sys->lock );
1305 /* Search the program */
1306 for( i = 0; i < p_sys->i_pgrm; i++ )
1308 if( fmt->i_group == p_sys->pgrm[i]->i_id )
1310 p_pgrm = p_sys->pgrm[i];
1314 if( p_pgrm == NULL )
1316 /* Create a new one */
1317 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1320 /* Increase ref count for program */
1325 fmt->i_id = out->p_sys->i_id;
1326 es->i_id = fmt->i_id;
1327 es->p_pgrm = p_pgrm;
1328 es_format_Copy( &es->fmt, fmt );
1330 switch( fmt->i_cat )
1334 audio_replay_gain_t rg;
1336 es->i_channel = p_sys->i_audio;
1338 memset( &rg, 0, sizeof(rg) );
1339 vlc_mutex_lock( &p_input->p->input.p_item->lock );
1340 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
1341 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1343 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1345 if( !es->fmt.audio_replay_gain.pb_peak[i] )
1347 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1348 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1350 if( !es->fmt.audio_replay_gain.pb_gain[i] )
1352 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1353 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1360 es->i_channel = p_sys->i_video;
1361 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
1362 vlc_ureduce( &es->fmt.video.i_frame_rate,
1363 &es->fmt.video.i_frame_rate_base,
1364 fmt->video.i_frame_rate,
1365 fmt->video.i_frame_rate_base, 0 );
1369 es->i_channel = p_sys->i_sub;
1376 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
1377 es->psz_language_code = LanguageGetCode( fmt->psz_language );
1379 es->p_dec_record = NULL;
1380 for( i = 0; i < 4; i++ )
1381 es->pb_cc_present[i] = false;
1382 es->p_master = NULL;
1384 if( es->p_pgrm == p_sys->p_pgrm )
1385 EsOutESVarUpdate( out, es, false );
1387 /* Select it if needed */
1388 EsOutSelect( out, es, false );
1391 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1392 p_sys->i_id++; /* always incremented */
1393 switch( fmt->i_cat )
1406 EsOutAddInfo( out, es );
1408 vlc_mutex_unlock( &p_sys->lock );
1413 static bool EsIsSelected( es_out_id_t *es )
1417 bool b_decode = false;
1418 if( es->p_master->p_dec )
1420 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1421 if( i_channel != -1 )
1422 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1428 return es->p_dec != NULL;
1431 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1433 es_out_sys_t *p_sys = out->p_sys;
1434 input_thread_t *p_input = p_sys->p_input;
1436 p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1439 if( p_sys->b_buffering )
1440 input_DecoderStartBuffering( p_es->p_dec );
1442 if( !p_es->p_master && p_sys->p_sout_record )
1444 p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1445 if( p_es->p_dec_record && p_sys->b_buffering )
1446 input_DecoderStartBuffering( p_es->p_dec_record );
1450 EsOutDecoderChangeDelay( out, p_es );
1452 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1459 input_DecoderDelete( p_es->p_dec );
1462 if( p_es->p_dec_record )
1464 input_DecoderDelete( p_es->p_dec_record );
1465 p_es->p_dec_record = NULL;
1469 static void EsSelect( es_out_t *out, es_out_id_t *es )
1471 es_out_sys_t *p_sys = out->p_sys;
1472 input_thread_t *p_input = p_sys->p_input;
1474 const char *psz_var;
1476 if( EsIsSelected( es ) )
1478 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1485 if( !es->p_master->p_dec )
1488 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1489 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1494 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1496 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1498 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1503 else if( es->fmt.i_cat == AUDIO_ES )
1505 var_Get( p_input, "audio", &val );
1506 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1508 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1513 if( es->fmt.i_cat == SPU_ES )
1515 var_Get( p_input, "spu", &val );
1516 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1518 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1524 EsCreateDecoder( out, es );
1526 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1530 if( es->fmt.i_cat == VIDEO_ES )
1531 psz_var = "video-es";
1532 else if( es->fmt.i_cat == AUDIO_ES )
1533 psz_var = "audio-es";
1534 else if( es->fmt.i_cat == SPU_ES )
1539 /* Mark it as selected */
1540 val.i_int = es->i_id;
1541 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1543 var_SetBool( p_sys->p_input, "intf-change", true );
1546 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1548 es_out_sys_t *p_sys = out->p_sys;
1549 input_thread_t *p_input = p_sys->p_input;
1551 const char *psz_var;
1553 if( !EsIsSelected( es ) )
1555 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1561 if( es->p_master->p_dec )
1563 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1564 if( i_channel != -1 )
1565 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1570 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1572 for( i = 0; i < 4; i++ )
1574 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1577 if( i_spu_id == es->pp_cc_es[i]->i_id )
1579 /* Force unselection of the CC */
1581 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1583 var_SetBool( p_sys->p_input, "intf-change", true );
1585 EsOutDel( out, es->pp_cc_es[i] );
1587 es->pb_cc_present[i] = false;
1589 EsDestroyDecoder( out, es );
1596 if( es->p_dec == NULL )
1598 if( es->fmt.i_cat == VIDEO_ES )
1599 psz_var = "video-es";
1600 else if( es->fmt.i_cat == AUDIO_ES )
1601 psz_var = "audio-es";
1602 else if( es->fmt.i_cat == SPU_ES )
1607 /* Mark it as unselected */
1609 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1611 var_SetBool( p_sys->p_input, "intf-change", true );
1615 * Select an ES given the current mode
1616 * XXX: you need to take a the lock before (stream.stream_lock)
1618 * \param out The es_out structure
1619 * \param es es_out_id structure
1620 * \param b_force ...
1623 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1625 es_out_sys_t *p_sys = out->p_sys;
1627 int i_cat = es->fmt.i_cat;
1629 if( !p_sys->b_active ||
1630 ( !b_force && es->fmt.i_priority < 0 ) )
1635 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1637 if( !EsIsSelected( es ) )
1638 EsSelect( out, es );
1640 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1644 var_Get( p_sys->p_input, "programs", &val );
1645 for ( i = 0; i < val.p_list->i_count; i++ )
1647 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1649 if( !EsIsSelected( es ) )
1650 EsSelect( out, es );
1654 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1656 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1660 if( es->p_pgrm != p_sys->p_pgrm )
1663 if( i_cat == AUDIO_ES )
1665 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1666 es->psz_language_code );
1668 if( p_sys->p_es_audio &&
1669 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1671 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1672 p_sys->p_es_audio->psz_language_code );
1674 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1676 i_wanted = es->i_channel;
1680 /* Select audio if (no audio selected yet)
1681 * - no audio-language
1682 * - no audio code for the ES
1683 * - audio code in the requested list */
1685 !strcmp( es->psz_language_code, "??" ) ||
1686 !p_sys->ppsz_audio_language )
1687 i_wanted = es->i_channel;
1690 if( p_sys->i_audio_last >= 0 )
1691 i_wanted = p_sys->i_audio_last;
1693 if( p_sys->i_audio_id >= 0 )
1695 if( es->i_id == p_sys->i_audio_id )
1696 i_wanted = es->i_channel;
1701 else if( i_cat == SPU_ES )
1703 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1704 es->psz_language_code );
1706 if( p_sys->p_es_sub &&
1707 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1709 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1710 p_sys->p_es_sub->psz_language_code );
1712 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1713 idx1, es->psz_language_code, idx2,
1714 p_sys->p_es_sub->psz_language_code );
1716 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1718 /* We found a SPU that matches our language request */
1719 i_wanted = es->i_channel;
1721 else if( idx1 >= 0 )
1723 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1724 idx1, es->psz_language_code );
1726 i_wanted = es->i_channel;
1728 else if( p_sys->i_default_sub_id >= 0 )
1730 if( es->i_id == p_sys->i_default_sub_id )
1731 i_wanted = es->i_channel;
1734 if( p_sys->i_sub_last >= 0 )
1735 i_wanted = p_sys->i_sub_last;
1737 if( p_sys->i_sub_id >= 0 )
1739 if( es->i_id == p_sys->i_sub_id )
1740 i_wanted = es->i_channel;
1745 else if( i_cat == VIDEO_ES )
1747 i_wanted = es->i_channel;
1750 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1751 EsSelect( out, es );
1754 /* FIXME TODO handle priority here */
1755 if( EsIsSelected( es ) )
1757 if( i_cat == AUDIO_ES )
1759 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1760 p_sys->p_es_audio &&
1761 p_sys->p_es_audio != es &&
1762 EsIsSelected( p_sys->p_es_audio ) )
1764 EsUnselect( out, p_sys->p_es_audio, false );
1766 p_sys->p_es_audio = es;
1768 else if( i_cat == SPU_ES )
1770 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1772 p_sys->p_es_sub != es &&
1773 EsIsSelected( p_sys->p_es_sub ) )
1775 EsUnselect( out, p_sys->p_es_sub, false );
1777 p_sys->p_es_sub = es;
1779 else if( i_cat == VIDEO_ES )
1781 p_sys->p_es_video = es;
1787 * Send a block for the given es_out
1789 * \param out the es_out to send from
1790 * \param es the es_out_id
1791 * \param p_block the data block to send
1793 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1795 es_out_sys_t *p_sys = out->p_sys;
1796 input_thread_t *p_input = p_sys->p_input;
1799 if( libvlc_stats( p_input ) )
1801 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1802 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1803 p_block->i_buffer, &i_total );
1804 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1805 (float)i_total, NULL );
1806 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1809 vlc_mutex_lock( &p_sys->lock );
1811 /* Mark preroll blocks */
1812 if( p_sys->i_preroll_end >= 0 )
1814 int64_t i_date = p_block->i_pts;
1816 i_date = p_block->i_dts;
1818 if( i_date < p_sys->i_preroll_end )
1819 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1822 p_block->i_rate = 0;
1826 block_Release( p_block );
1827 vlc_mutex_unlock( &p_sys->lock );
1832 if( es->p_dec_record )
1834 block_t *p_dup = block_Duplicate( p_block );
1836 input_DecoderDecode( es->p_dec_record, p_dup );
1838 input_DecoderDecode( es->p_dec, p_block );
1840 /* Check CC status */
1842 bool b_cc_new = false;
1844 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1845 for( int i = 0; i < 4; i++ )
1847 static const vlc_fourcc_t fcc[4] = {
1848 VLC_FOURCC('c', 'c', '1', ' '),
1849 VLC_FOURCC('c', 'c', '2', ' '),
1850 VLC_FOURCC('c', 'c', '3', ' '),
1851 VLC_FOURCC('c', 'c', '4', ' '),
1855 if( es->pb_cc_present[i] || !pb_cc[i] )
1857 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1859 es_format_Init( &fmt, SPU_ES, fcc[i] );
1860 fmt.i_group = es->fmt.i_group;
1861 if( asprintf( &fmt.psz_description,
1862 _("Closed captions %u"), 1 + i ) == -1 )
1863 fmt.psz_description = NULL;
1864 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1865 es->pp_cc_es[i]->p_master = es;
1866 es_format_Clean( &fmt );
1869 es->pb_cc_present[i] = true;
1873 var_SetBool( p_sys->p_input, "intf-change", true );
1875 vlc_mutex_unlock( &p_sys->lock );
1880 /*****************************************************************************
1882 *****************************************************************************/
1883 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1885 es_out_sys_t *p_sys = out->p_sys;
1886 bool b_reselect = false;
1889 vlc_mutex_lock( &p_sys->lock );
1891 /* We don't try to reselect */
1894 while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
1896 if( input_DecoderIsEmpty( es->p_dec ) &&
1897 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
1901 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1904 if( es->p_pgrm == p_sys->p_pgrm )
1905 EsOutESVarUpdate( out, es, true );
1907 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1910 if( es->p_pgrm->i_es == 0 )
1912 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1915 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1916 p_sys->p_es_sub == es ) b_reselect = true;
1918 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1919 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1920 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1922 switch( es->fmt.i_cat )
1935 /* Re-select another track when needed */
1938 for( i = 0; i < p_sys->i_es; i++ )
1940 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1941 EsOutSelect( out, p_sys->es[i], false );
1945 free( es->psz_language );
1946 free( es->psz_language_code );
1948 es_format_Clean( &es->fmt );
1950 vlc_mutex_unlock( &p_sys->lock );
1956 * Control query handler
1958 * \param out the es_out to control
1959 * \param i_query A es_out query as defined in include/ninput.h
1960 * \param args a variable list of arguments for the query
1961 * \return VLC_SUCCESS or an error code
1963 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
1965 es_out_sys_t *p_sys = out->p_sys;
1973 case ES_OUT_SET_ES_STATE:
1974 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1975 b = (bool) va_arg( args, int );
1976 if( b && !EsIsSelected( es ) )
1978 EsSelect( out, es );
1979 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1981 else if( !b && EsIsSelected( es ) )
1983 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1988 case ES_OUT_GET_ES_STATE:
1989 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1990 pb = (bool*) va_arg( args, bool * );
1992 *pb = EsIsSelected( es );
1995 case ES_OUT_SET_ACTIVE:
1997 b = (bool) va_arg( args, int );
1998 p_sys->b_active = b;
2001 var_SetBool( p_sys->p_input, "intf-change", true );
2005 case ES_OUT_GET_ACTIVE:
2006 pb = (bool*) va_arg( args, bool * );
2007 *pb = p_sys->b_active;
2010 case ES_OUT_SET_MODE:
2011 i = (int) va_arg( args, int );
2012 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
2013 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
2017 /* Reapply policy mode */
2018 for( i = 0; i < p_sys->i_es; i++ )
2020 if( EsIsSelected( p_sys->es[i] ) )
2022 EsUnselect( out, p_sys->es[i],
2023 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2026 for( i = 0; i < p_sys->i_es; i++ )
2028 EsOutSelect( out, p_sys->es[i], false );
2032 return VLC_EGENERIC;
2034 case ES_OUT_GET_MODE:
2035 pi = (int*) va_arg( args, int* );
2036 *pi = p_sys->i_mode;
2040 case ES_OUT_RESTART_ES:
2044 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2048 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2050 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2052 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2057 for( i = 0; i < p_sys->i_es; i++ )
2061 if( es == p_sys->es[i] )
2063 EsOutSelect( out, es, true );
2069 if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2071 if( EsIsSelected( p_sys->es[i] ) )
2073 if( i_query == ES_OUT_RESTART_ES )
2075 if( p_sys->es[i]->p_dec )
2077 EsDestroyDecoder( out, p_sys->es[i] );
2078 EsCreateDecoder( out, p_sys->es[i] );
2083 EsUnselect( out, p_sys->es[i],
2084 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2090 if( i_query == ES_OUT_SET_ES )
2093 event.type = vlc_InputSelectedStreamChanged;
2094 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
2099 case ES_OUT_SET_ES_DEFAULT:
2101 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2105 /*p_sys->i_default_video_id = -1;*/
2106 /*p_sys->i_default_audio_id = -1;*/
2107 p_sys->i_default_sub_id = -1;
2109 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2111 /*p_sys->i_default_video_id = -1;*/
2113 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2115 /*p_sys->i_default_audio_id = -1;*/
2117 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2119 p_sys->i_default_sub_id = -1;
2123 /*if( es->fmt.i_cat == VIDEO_ES )
2124 p_sys->i_default_video_id = es->i_id;
2126 if( es->fmt.i_cat == AUDIO_ES )
2127 p_sys->i_default_audio_id = es->i_id;
2129 if( es->fmt.i_cat == SPU_ES )
2130 p_sys->i_default_sub_id = es->i_id;
2135 case ES_OUT_SET_PCR:
2136 case ES_OUT_SET_GROUP_PCR:
2138 es_out_pgrm_t *p_pgrm = NULL;
2142 if( i_query == ES_OUT_SET_PCR )
2144 p_pgrm = p_sys->p_pgrm;
2149 i_group = (int)va_arg( args, int );
2150 for( i = 0; i < p_sys->i_pgrm; i++ )
2152 if( p_sys->pgrm[i]->i_id == i_group )
2154 p_pgrm = p_sys->pgrm[i];
2159 if( p_pgrm == NULL )
2160 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
2162 i_pcr = (int64_t)va_arg( args, int64_t );
2164 * TODO do not use mdate() but proper stream acquisition date */
2165 input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2166 p_sys->p_input->b_can_pace_control, i_pcr, mdate() );
2167 /* Check buffering state on master clock update */
2168 if( p_sys->b_buffering && p_pgrm == p_sys->p_pgrm )
2169 EsOutDecodersStopBuffering( out, false );
2174 case ES_OUT_RESET_PCR:
2175 msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2176 input_EsOutChangePosition( out );
2182 int64_t i_ts = (int64_t)va_arg( args, int64_t );
2183 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
2184 *pi_ts = input_clock_GetTS( p_sys->p_pgrm->p_clock, NULL,
2185 p_sys->p_input->i_pts_delay, i_ts );
2188 return VLC_EGENERIC;
2190 case ES_OUT_GET_GROUP:
2191 pi = (int*) va_arg( args, int* );
2193 *pi = p_sys->p_pgrm->i_id;
2195 *pi = -1; /* FIXME */
2198 case ES_OUT_SET_GROUP:
2201 i = (int) va_arg( args, int );
2202 for( j = 0; j < p_sys->i_pgrm; j++ )
2204 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2205 if( p_pgrm->i_id == i )
2207 EsOutProgramSelect( out, p_pgrm );
2211 return VLC_EGENERIC;
2214 case ES_OUT_SET_FMT:
2216 /* This ain't pretty but is need by some demuxers (eg. Ogg )
2217 * to update the p_extra data */
2219 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2220 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2222 return VLC_EGENERIC;
2224 if( p_fmt->i_extra )
2226 es->fmt.i_extra = p_fmt->i_extra;
2227 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2228 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2233 EsDestroyDecoder( out, es );
2235 EsCreateDecoder( out, es );
2237 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2238 es->p_dec->fmt_in.p_extra =
2239 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2240 memcpy( es->p_dec->fmt_in.p_extra,
2241 p_fmt->p_extra, p_fmt->i_extra );
2248 case ES_OUT_SET_NEXT_DISPLAY_TIME:
2250 const int64_t i_date = (int64_t)va_arg( args, int64_t );
2253 return VLC_EGENERIC;
2255 p_sys->i_preroll_end = i_date;
2259 case ES_OUT_SET_GROUP_META:
2261 int i_group = (int)va_arg( args, int );
2262 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2264 EsOutProgramMeta( out, i_group, p_meta );
2267 case ES_OUT_SET_GROUP_EPG:
2269 int i_group = (int)va_arg( args, int );
2270 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2272 EsOutProgramEpg( out, i_group, p_epg );
2275 case ES_OUT_DEL_GROUP:
2277 int i_group = (int)va_arg( args, int );
2279 return EsOutProgramDel( out, i_group );
2282 case ES_OUT_GET_WAKE_UP:
2284 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2285 *pi_wakeup = EsOutGetWakeup( out );
2289 case ES_OUT_SET_ES_BY_ID:
2290 case ES_OUT_RESTART_ES_BY_ID:
2291 case ES_OUT_SET_ES_DEFAULT_BY_ID:
2293 const int i_id = (int)va_arg( args, int );
2294 es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2299 case ES_OUT_SET_ES_BY_ID: i_new_query = ES_OUT_SET_ES; break;
2300 case ES_OUT_RESTART_ES_BY_ID: i_new_query = ES_OUT_RESTART_ES; break;
2301 case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2305 /* TODO if the lock is made non recursive it should be changed */
2306 return es_out_Control( out, i_new_query, p_es );
2309 case ES_OUT_GET_BUFFERING:
2310 pb = (bool *)va_arg( args, bool* );
2311 *pb = p_sys->b_buffering;
2314 case ES_OUT_GET_EMPTY:
2315 pb = (bool *)va_arg( args, bool* );
2316 *pb = EsOutDecodersIsEmpty( out );
2320 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2321 return VLC_EGENERIC;
2324 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2326 es_out_sys_t *p_sys = out->p_sys;
2329 vlc_mutex_lock( &p_sys->lock );
2330 i_ret = EsOutControlLocked( out, i_query, args );
2331 vlc_mutex_unlock( &p_sys->lock );
2336 /****************************************************************************
2337 * LanguageGetName: try to expend iso639 into plain name
2338 ****************************************************************************/
2339 static char *LanguageGetName( const char *psz_code )
2341 const iso639_lang_t *pl;
2343 if( psz_code == NULL )
2345 return strdup( "" );
2348 if( strlen( psz_code ) == 2 )
2350 pl = GetLang_1( psz_code );
2352 else if( strlen( psz_code ) == 3 )
2354 pl = GetLang_2B( psz_code );
2355 if( !strcmp( pl->psz_iso639_1, "??" ) )
2357 pl = GetLang_2T( psz_code );
2362 return strdup( psz_code );
2365 if( !strcmp( pl->psz_iso639_1, "??" ) )
2367 return strdup( psz_code );
2371 if( *pl->psz_native_name )
2373 return strdup( pl->psz_native_name );
2375 return strdup( pl->psz_eng_name );
2379 /* Get a 2 char code */
2380 static char *LanguageGetCode( const char *psz_lang )
2382 const iso639_lang_t *pl;
2384 if( psz_lang == NULL || *psz_lang == '\0' )
2385 return strdup("??");
2387 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2389 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2390 !strcasecmp( pl->psz_native_name, psz_lang ) ||
2391 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2392 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2393 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2397 if( pl->psz_iso639_1 != NULL )
2398 return strdup( pl->psz_iso639_1 );
2400 return strdup("??");
2403 static char **LanguageSplit( const char *psz_langs )
2410 if( psz_langs == NULL ) return NULL;
2412 psz_parser = psz_dup = strdup(psz_langs);
2414 while( psz_parser && *psz_parser )
2419 psz = strchr(psz_parser, ',' );
2420 if( psz ) *psz++ = '\0';
2422 if( !strcmp( psz_parser, "any" ) )
2424 TAB_APPEND( i_psz, ppsz, strdup("any") );
2428 psz_code = LanguageGetCode( psz_parser );
2429 if( strcmp( psz_code, "??" ) )
2431 TAB_APPEND( i_psz, ppsz, psz_code );
2444 TAB_APPEND( i_psz, ppsz, NULL );
2451 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2455 if( !ppsz_langs || !psz_lang ) return -1;
2457 for( i = 0; ppsz_langs[i]; i++ )
2459 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2460 !strcasecmp( ppsz_langs[i], "any" ) )
2469 /****************************************************************************
2471 * - add meta info to the playlist item
2472 ****************************************************************************/
2473 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2475 es_out_sys_t *p_sys = out->p_sys;
2476 input_thread_t *p_input = p_sys->p_input;
2477 es_format_t *fmt = &es->fmt;
2481 /* Add stream info */
2482 if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
2485 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2486 "%.4s", (char*)&fmt->i_codec );
2488 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2489 "%s", es->psz_language );
2491 /* Add information */
2492 switch( fmt->i_cat )
2495 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2496 _("Type"), _("Audio") );
2498 if( fmt->audio.i_channels > 0 )
2499 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2500 "%u", fmt->audio.i_channels );
2502 if( fmt->audio.i_rate > 0 )
2504 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2505 _("%u Hz"), fmt->audio.i_rate );
2506 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2509 if( fmt->audio.i_bitspersample > 0 )
2510 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2511 _("Bits per sample"), "%u",
2512 fmt->audio.i_bitspersample );
2514 if( fmt->i_bitrate > 0 )
2516 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2517 _("%u kb/s"), fmt->i_bitrate / 1000 );
2518 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2523 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2524 _("Type"), _("Video") );
2526 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2527 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2528 _("Resolution"), "%ux%u",
2529 fmt->video.i_width, fmt->video.i_height );
2531 if( fmt->video.i_visible_width > 0 &&
2532 fmt->video.i_visible_height > 0 )
2533 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2534 _("Display resolution"), "%ux%u",
2535 fmt->video.i_visible_width,
2536 fmt->video.i_visible_height);
2537 if( fmt->video.i_frame_rate > 0 &&
2538 fmt->video.i_frame_rate_base > 0 )
2540 div = lldiv( (float)fmt->video.i_frame_rate /
2541 fmt->video.i_frame_rate_base * 1000000,
2543 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2544 _("Frame rate"), "%"PRId64".%06u",
2545 div.quot, (unsigned int )div.rem );
2550 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2551 _("Type"), _("Subtitle") );