]> git.sesse.net Git - vlc/blobdiff - src/input/clock.c
System clock is given to input_ClockSetPCR.
[vlc] / src / input / clock.c
index 6c724b82d7effdd654203c6c4479ebe65f954e09..81fffc5f6398c8d0a96127bae60d2d9089c3e054 100644 (file)
@@ -28,7 +28,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 
 #include "input_internal.h"
 
  * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
  */
 
+enum /* Synchro states */
+{
+    SYNCHRO_OK     = 0,
+    SYNCHRO_START  = 1,
+    SYNCHRO_REINIT = 2,
+};
 
 static void ClockNewRef( input_clock_t * p_pgrm,
                          mtime_t i_clock, mtime_t i_sysdate );
@@ -75,7 +81,7 @@ static void ClockNewRef( input_clock_t * p_pgrm,
  *****************************************************************************/
 
 /* Maximum gap allowed between two CRs. */
-#define CR_MAX_GAP (I64C(2000000)*100/9)
+#define CR_MAX_GAP (INT64_C(2000000)*100/9)
 
 /* Latency introduced on DVDs with CR == 0 on chapter change - this is from
  * my dice --Meuuh */
@@ -138,19 +144,22 @@ void input_ClockInit( input_clock_t *cl, bool b_master, int i_cr_average, int i_
 
 /*****************************************************************************
  * input_ClockSetPCR: manages a clock reference
+ *
+ *  i_ck_stream: date in stream clock
+ *  i_ck_system: date in system clock
  *****************************************************************************/
 void input_ClockSetPCR( input_thread_t *p_input,
-                        input_clock_t *cl, mtime_t i_clock )
+                        input_clock_t *cl,
+                        mtime_t i_ck_stream, mtime_t i_ck_system )
 {
     const bool b_synchronize = p_input->b_can_pace_control && cl->b_master;
-    const mtime_t i_mdate = mdate();
 
     if( ( cl->i_synchro_state != SYNCHRO_OK ) ||
-        ( i_clock == 0 && cl->last_cr != 0 ) )
+        ( i_ck_stream == 0 && cl->last_cr != 0 ) )
     {
         /* Feed synchro with a new reference point. */
-        ClockNewRef( cl, i_clock,
-                         __MAX( cl->last_pts + CR_MEAN_PTS_GAP, i_mdate ) );
+        ClockNewRef( cl, i_ck_stream,
+                         __MAX( cl->last_pts + CR_MEAN_PTS_GAP, i_ck_system ) );
         cl->i_synchro_state = SYNCHRO_OK;
 
         if( !b_synchronize )
@@ -161,8 +170,8 @@ void input_ClockSetPCR( input_thread_t *p_input,
         }
     }
     else if ( cl->last_cr != 0 &&
-              ( (cl->last_cr - i_clock) > CR_MAX_GAP ||
-                (cl->last_cr - i_clock) < - CR_MAX_GAP ) )
+              ( (cl->last_cr - i_ck_stream) > CR_MAX_GAP ||
+                (cl->last_cr - i_ck_stream) < - CR_MAX_GAP ) )
     {
         /* Stream discontinuity, for which we haven't received a
          * warning from the stream control facilities (dd-edited
@@ -171,43 +180,27 @@ void input_ClockSetPCR( input_thread_t *p_input,
         input_ClockInit( cl, cl->b_master, cl->i_cr_average, cl->i_rate );
         /* Feed synchro with a new reference point. */
         msg_Warn( p_input, "feeding synchro with a new reference point trying to recover from clock gap" );
-        ClockNewRef( cl, i_clock,
-                         __MAX( cl->last_pts + CR_MEAN_PTS_GAP, i_mdate ) );
+        ClockNewRef( cl, i_ck_stream,
+                         __MAX( cl->last_pts + CR_MEAN_PTS_GAP, i_ck_system ) );
         cl->i_synchro_state = SYNCHRO_OK;
     }
 
-    cl->last_cr = i_clock;
-    cl->last_sysdate = i_mdate;
+    cl->last_cr = i_ck_stream;
+    cl->last_sysdate = i_ck_system;
 
-    if( b_synchronize )
-    {
-        /* Wait a while before delivering the packets to the decoder.
-         * In case of multiple programs, we arbitrarily follow the
-         * clock of the selected program. */
-        if( !p_input->p->b_out_pace_control )
-        {
-            mtime_t i_wakeup = ClockToSysdate( cl, i_clock );
-            while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
-            {
-                msleep( CLOCK_FREQ );
-                if( p_input->b_die ) i_wakeup = mdate();
-            }
-            mwait( i_wakeup );
-        }
-    }
-    else if ( i_mdate - cl->last_update > 200000 )
+    if( !b_synchronize && i_ck_system - cl->last_update > 200000 )
     {
         /* Smooth clock reference variations. */
         const mtime_t i_extrapoled_clock = ClockCurrent( cl );
         /* Bresenham algorithm to smooth variations. */
         const mtime_t i_tmp = cl->delta_cr * (cl->i_cr_average - 1) +
-                              ( i_extrapoled_clock - i_clock ) * 1  +
+                              ( i_extrapoled_clock - i_ck_stream ) * 1  +
                               cl->i_delta_cr_residue;
 
         cl->i_delta_cr_residue = i_tmp % cl->i_cr_average;
         cl->delta_cr           = i_tmp / cl->i_cr_average;
 
-        cl->last_update = i_mdate;
+        cl->last_update = i_ck_system;
     }
 }
 
@@ -245,3 +238,22 @@ void input_ClockSetRate( input_clock_t *cl, int i_rate )
     cl->i_rate = i_rate;
 }
 
+/*****************************************************************************
+ * input_ClockGetWakeup
+ *****************************************************************************/
+mtime_t input_ClockGetWakeup( input_thread_t *p_input, input_clock_t *cl )
+{
+    /* Not synchronized, we cannot wait */
+    if( cl->i_synchro_state != SYNCHRO_OK )
+        return 0;
+
+    /* We must not wait if not pace controled, or we are not the
+     * master clock */
+    if( !p_input->b_can_pace_control || !cl->b_master ||
+        p_input->p->b_out_pace_control )
+        return 0;
+
+    /* */
+    return ClockToSysdate( cl, cl->last_cr );
+}
+