]> git.sesse.net Git - vlc/blobdiff - src/input/input_clock.c
Some heavy changes today:
[vlc] / src / input / input_clock.c
index f603ccd252634ea0d81b8725f2f2fd9be43bfdf3..a8b84c2ebd586cd8a3e16713a6a36fe37167611b 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * input_clock.c: Clock/System date convertions, stream management
  *****************************************************************************
- * Copyright (C) 1999, 2000 VideoLAN
- * $Id: input_clock.c,v 1.5 2001/02/08 13:08:03 massiot Exp $
+ * Copyright (C) 1999-2001 VideoLAN
+ * $Id: input_clock.c,v 1.28 2001/12/30 07:09:56 sam Exp $
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
+#include <string.h>                                    /* memcpy(), memset() */
+#include <sys/types.h>                                              /* off_t */
 
-#include "config.h"
-#include "common.h"
-#include "threads.h"
-#include "mtime.h"
-#include "intf_msg.h"
+#include <videolan/vlc.h>
 
 #include "stream_control.h"
 #include "input_ext-intf.h"
 #include "input_ext-dec.h"
-
-#include "input.h"
+#include "input_ext-plugins.h"
 
 /*
  * DISCUSSION : SYNCHRONIZATION METHOD
@@ -82,6 +78,8 @@
 /*****************************************************************************
  * ClockToSysdate: converts a movie clock to system date
  *****************************************************************************/
+static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
+                         mtime_t i_clock, mtime_t i_sysdate );
 static mtime_t ClockToSysdate( input_thread_t * p_input,
                                pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
 {
@@ -89,12 +87,12 @@ static mtime_t ClockToSysdate( input_thread_t * p_input,
 
     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
     {
-        i_sysdate = (i_clock - p_pgrm->cr_ref) 
-                        * p_input->stream.control.i_rate
-                        * 300
-                        / 27
-                        / DEFAULT_RATE
-                        + p_pgrm->sysdate_ref;
+        i_sysdate = (mtime_t)(i_clock - p_pgrm->cr_ref) 
+                        * (mtime_t)p_input->stream.control.i_rate
+                        * (mtime_t)300;
+        i_sysdate /= 27;
+        i_sysdate /= 1000;
+        i_sysdate += (mtime_t)p_pgrm->sysdate_ref;
     }
 
     return( i_sysdate );
@@ -114,66 +112,105 @@ static mtime_t ClockCurrent( input_thread_t * p_input,
 }
 
 /*****************************************************************************
- * input_ClockNewRef: writes a new clock reference
+ * ClockNewRef: writes a new clock reference
  *****************************************************************************/
-void input_ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
-                        mtime_t i_clock, mtime_t i_sysdate )
+static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
+                         mtime_t i_clock, mtime_t i_sysdate )
 {
-    intf_WarnMsg( 1, "Old ref: %lld/%lld, New ref: %lld/%lld", p_pgrm->cr_ref,
-              p_pgrm->sysdate_ref, i_clock, i_sysdate );
     p_pgrm->cr_ref = i_clock;
-    p_pgrm->sysdate_ref = i_sysdate;
+    /* this is actually a kludge, but it gives better results when scr
+    * is zero in DVDs: we are 3-4 ms in advance instead of sometimes
+    * 100ms late  */
+    p_pgrm->sysdate_ref = ( p_pgrm->last_syscr && !i_clock )
+                          ? p_pgrm->last_syscr
+                          : i_sysdate ;
 }
 
 /*****************************************************************************
- * EscapeDiscontinuity: send a NULL packet to the decoders
+ * input_ClockInit: reinitializes the clock reference after a stream
+ *                  discontinuity
  *****************************************************************************/
-static void EscapeDiscontinuity( input_thread_t * p_input,
-                                 pgrm_descriptor_t * p_pgrm )
+void input_ClockInit( pgrm_descriptor_t * p_pgrm )
 {
-    int     i_es;
-
-    for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
-    {
-        es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
-
-        if( p_es->p_decoder_fifo != NULL )
-        {
-            input_NullPacket( p_input, p_es );
-        }
-    }
+    p_pgrm->last_cr = 0;
+    p_pgrm->last_syscr = 0;
+    p_pgrm->cr_ref = 0;
+    p_pgrm->sysdate_ref = 0;
+    p_pgrm->delta_cr = 0;
+    p_pgrm->c_average_count = 0;
 }
 
 /*****************************************************************************
- * EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
+ * input_ClockManageControl: handles the messages from the interface
+ *****************************************************************************
+ * Returns UNDEF_S if nothing happened, PAUSE_S if the stream was paused
  *****************************************************************************/
-static void EscapeAudioDiscontinuity( input_thread_t * p_input,
-                                      pgrm_descriptor_t * p_pgrm )
+int input_ClockManageControl( input_thread_t * p_input,
+                               pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
 {
-    int     i_es;
+    int i_return_value = UNDEF_S;
+
+    vlc_mutex_lock( &p_input->stream.stream_lock );
+
+    if( p_input->stream.i_new_status == PAUSE_S )
+    {
+        int i_old_status;
+        vlc_mutex_lock( &p_input->stream.control.control_lock );
+        i_old_status = p_input->stream.control.i_status;
+
+        p_input->stream.control.i_status = PAUSE_S;
+        vlc_cond_wait( &p_input->stream.stream_wait,
+                       &p_input->stream.stream_lock );
+        p_pgrm->last_syscr = 0;
+        ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
+
+        if( p_input->stream.i_new_status == PAUSE_S )
+        { 
+            /* PAUSE_S undoes the pause state: Return to old state. */
+            p_input->stream.control.i_status = i_old_status;
+            p_input->stream.i_new_status = UNDEF_S;
+            p_input->stream.i_new_rate = UNDEF_S;
+        }
+
+        /* We handle i_new_status != PAUSE_S below... */
+        vlc_mutex_unlock( &p_input->stream.control.control_lock );
+
+        i_return_value = PAUSE_S;
+    }
 
-    for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
+    if( p_input->stream.i_new_status != UNDEF_S )
     {
-        es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
+        vlc_mutex_lock( &p_input->stream.control.control_lock );
 
-        if( p_es->p_decoder_fifo != NULL && p_es->b_audio )
+        p_input->stream.control.i_status = p_input->stream.i_new_status;
+
+        ClockNewRef( p_input, p_pgrm, i_clock,
+                     ClockToSysdate( p_input, p_pgrm, i_clock ) );
+
+        if( p_input->stream.control.i_status == PLAYING_S )
+        {
+            p_input->stream.control.i_rate = DEFAULT_RATE;
+            p_input->stream.control.b_mute = 0;
+        }
+        else
         {
-            input_NullPacket( p_input, p_es );
+            p_input->stream.control.i_rate = p_input->stream.i_new_rate;
+            p_input->stream.control.b_mute = 1;
+
+            /* Feed the audio decoders with a NULL packet to avoid
+             * discontinuities. */
+            input_EscapeAudioDiscontinuity( p_input );
         }
+
+        p_input->stream.i_new_status = UNDEF_S;
+        p_input->stream.i_new_rate = UNDEF_S;
+
+        vlc_mutex_unlock( &p_input->stream.control.control_lock );
     }
-}
 
-/*****************************************************************************
- * input_ClockInit: reinitializes the clock reference after a stream
- *                  discontinuity
- *****************************************************************************/
-void input_ClockInit( pgrm_descriptor_t * p_pgrm )
-{
-    p_pgrm->last_cr = 0;
-    p_pgrm->cr_ref = 0;
-    p_pgrm->sysdate_ref = 0;
-    p_pgrm->delta_cr = 0;
-    p_pgrm->c_average_count = 0;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+
+    return( i_return_value );
 }
 
 /*****************************************************************************
@@ -182,11 +219,25 @@ void input_ClockInit( pgrm_descriptor_t * p_pgrm )
 void input_ClockManageRef( input_thread_t * p_input,
                            pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
 {
-    if( p_pgrm->i_synchro_state != SYNCHRO_OK )
+    if( ( p_pgrm->i_synchro_state != SYNCHRO_OK ) || ( i_clock == 0 ) )
     {
         /* Feed synchro with a new reference point. */
-        input_ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
+        ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
         p_pgrm->i_synchro_state = SYNCHRO_OK;
+
+        if( p_input->stream.b_pace_control
+             && p_input->stream.p_selected_program == p_pgrm )
+        {
+            p_pgrm->last_cr = i_clock;
+            mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
+        }
+        else
+        {
+            p_pgrm->last_cr = 0;
+            p_pgrm->last_syscr = 0;
+            p_pgrm->delta_cr = 0;
+            p_pgrm->c_average_count = 0;
+        }
     }
     else
     {
@@ -197,62 +248,29 @@ void input_ClockManageRef( input_thread_t * p_input,
             /* Stream discontinuity, for which we haven't received a
              * warning from the stream control facilities (dd-edited
              * stream ?). */
-            intf_WarnMsg( 3, "Clock gap, unexpected stream discontinuity" );
+            intf_WarnMsg( 1, "Clock gap, unexpected stream discontinuity" );
             input_ClockInit( p_pgrm );
             p_pgrm->i_synchro_state = SYNCHRO_START;
-            EscapeDiscontinuity( p_input, p_pgrm );
+            input_EscapeDiscontinuity( p_input, p_pgrm );
         }
 
         p_pgrm->last_cr = i_clock;
 
         if( p_input->stream.b_pace_control
-             && p_input->stream.pp_programs[0] == p_pgrm )
+             && p_input->stream.p_selected_program == p_pgrm )
         {
+            /* We remember the last system date to be able to restart
+             * the synchro we statistically better continuity, after 
+             * a zero scr */
+            p_pgrm->last_syscr = ClockToSysdate( p_input, p_pgrm, i_clock );
+            
             /* Wait a while before delivering the packets to the decoder.
              * In case of multiple programs, we arbitrarily follow the
              * clock of the first program. */
-            mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
+            mwait( p_pgrm->last_syscr );
 
             /* Now take into account interface changes. */
-            vlc_mutex_lock( &p_input->stream.stream_lock );
-            if( p_input->stream.i_new_status != UNDEF_S )
-            {
-                if( p_input->stream.i_new_status == PAUSE_S )
-                {
-                    vlc_cond_wait( &p_input->stream.stream_wait,
-                                   &p_input->stream.stream_lock );
-                    input_ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
-                }
-                else
-                {
-                    input_ClockNewRef( p_input, p_pgrm, i_clock,
-                               ClockToSysdate( p_input, p_pgrm, i_clock ) );
-                }
-
-                vlc_mutex_lock( &p_input->stream.control.control_lock );
-                p_input->stream.control.i_status = p_input->stream.i_new_status;
-
-                if( p_input->stream.control.i_status != PLAYING_S
-                     && p_input->stream.control.i_status != PAUSE_S )
-                {
-                    p_input->stream.control.i_rate = p_input->stream.i_new_rate;
-                    p_input->stream.control.b_mute = 1;
-
-                    /* Feed the audio decoders with a NULL packet to avoid
-                     * discontinuities. */
-                    EscapeAudioDiscontinuity( p_input, p_pgrm );
-                }
-                else
-                {
-                    p_input->stream.control.i_rate = DEFAULT_RATE;
-                    p_input->stream.control.b_mute = 0;
-                }
-                vlc_mutex_unlock( &p_input->stream.control.control_lock );
-
-                p_input->stream.i_new_status = UNDEF_S;
-                p_input->stream.i_new_rate = UNDEF_S;
-            }
-            vlc_mutex_unlock( &p_input->stream.stream_lock );
+            input_ClockManageControl( p_input, p_pgrm, i_clock );
         }
         else
         {
@@ -264,14 +282,14 @@ void input_ClockManageRef( input_thread_t * p_input,
             {
                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
                                         * (CR_MAX_AVERAGE_COUNTER - 1)
-                                      + i_extrapoled_clock )
+                                      + ( i_extrapoled_clock - i_clock ) )
                                     / CR_MAX_AVERAGE_COUNTER;
             }
             else
             {
                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
                                         * p_pgrm->c_average_count
-                                      + i_extrapoled_clock )
+                                      + ( i_extrapoled_clock - i_clock ) )
                                     / (p_pgrm->c_average_count + 1);
                 p_pgrm->c_average_count++;
             }
@@ -288,7 +306,8 @@ mtime_t input_ClockGetTS( input_thread_t * p_input,
     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
     {
         return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
-                 + DEFAULT_PTS_DELAY );
+                 + DEFAULT_PTS_DELAY
+                 + (p_main->i_desync > 0 ? p_main->i_desync : 0) );
     }
     else
     {