+ p_sys->i_rate = i_rate;
+
+ if( !p_sys->b_paused )
+ EsOutProgramsChangeRate( out );
+}
+
+static void EsOutChangePosition( es_out_t *out )
+{
+ es_out_sys_t *p_sys = out->p_sys;
+
+ input_SendEventCache( p_sys->p_input, 0.0 );
+
+ for( int i = 0; i < p_sys->i_es; i++ )
+ {
+ es_out_id_t *p_es = p_sys->es[i];
+
+ if( !p_es->p_dec )
+ continue;
+
+ input_DecoderStartBuffering( p_es->p_dec );
+
+ if( p_es->p_dec_record )
+ input_DecoderStartBuffering( p_es->p_dec );
+ }
+
+ for( int i = 0; i < p_sys->i_pgrm; i++ )
+ input_clock_Reset( p_sys->pgrm[i]->p_clock );
+
+ p_sys->b_buffering = true;
+ p_sys->i_buffering_extra_initial = 0;
+ p_sys->i_buffering_extra_stream = 0;
+ p_sys->i_buffering_extra_system = 0;
+ p_sys->i_preroll_end = -1;
+}
+
+
+
+static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
+{
+ es_out_sys_t *p_sys = out->p_sys;
+ int i_ret;
+
+ mtime_t i_stream_start;
+ mtime_t i_system_start;
+ mtime_t i_stream_duration;
+ mtime_t i_system_duration;
+ i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
+ &i_stream_start, &i_system_start,
+ &i_stream_duration, &i_system_duration );
+ assert( !i_ret || b_forced );
+ if( i_ret )
+ return;
+
+ mtime_t i_preroll_duration = 0;
+ if( p_sys->i_preroll_end >= 0 )
+ i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
+
+ const mtime_t i_buffering_duration = p_sys->p_input->i_pts_delay +
+ i_preroll_duration +
+ p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
+
+ if( i_stream_duration <= i_buffering_duration && !b_forced )
+ {
+ const double f_level = (double)i_stream_duration / i_buffering_duration;
+ input_SendEventCache( p_sys->p_input, f_level );
+
+ msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
+ return;
+ }
+ input_SendEventCache( p_sys->p_input, 1.0 );
+
+ msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
+ (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
+ p_sys->b_buffering = false;
+ p_sys->i_preroll_end = -1;
+
+ if( p_sys->i_buffering_extra_initial > 0 )
+ {
+ /* FIXME wrong ? */
+ return;
+ }
+
+ const mtime_t i_decoder_buffering_start = mdate();
+ for( int i = 0; i < p_sys->i_es; i++ )
+ {
+ es_out_id_t *p_es = p_sys->es[i];
+
+ if( !p_es->p_dec )
+ continue;
+ input_DecoderWaitBuffering( p_es->p_dec );
+ if( p_es->p_dec_record )
+ input_DecoderWaitBuffering( p_es->p_dec_record );
+ }
+
+ msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
+ (int)(mdate() - i_decoder_buffering_start)/1000 );
+
+ const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
+ const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
+
+ input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_current_date + i_wakeup_delay - i_buffering_duration );
+
+ for( int i = 0; i < p_sys->i_es; i++ )
+ {
+ es_out_id_t *p_es = p_sys->es[i];
+
+ if( !p_es->p_dec )
+ continue;
+
+ input_DecoderStopBuffering( p_es->p_dec );
+ if( p_es->p_dec_record )
+ input_DecoderStopBuffering( p_es->p_dec );
+ }
+}
+static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
+{
+ es_out_sys_t *p_sys = out->p_sys;
+
+ /* Pause decoders first */
+ for( int i = 0; i < p_sys->i_es; i++ )