]> git.sesse.net Git - vlc/blobdiff - src/audio_output/dec.c
* modules/gui/wxwidgets: small code cleanup.
[vlc] / src / audio_output / dec.c
index 9eb8ba362b585c5ff07bb145a6546e5a3d9ad6e8..c14a9a4f14de93c3cca0742c5bc6a6886b81b5c7 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * dec.c : audio output API towards decoders
  *****************************************************************************
- * Copyright (C) 2002 VideoLAN
- * $Id: dec.c,v 1.7 2003/02/23 01:25:26 massiot Exp $
+ * Copyright (C) 2002-2004 the VideoLAN team
+ * $Id$
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
@@ -18,7 +18,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -35,6 +35,7 @@
 
 #include "audio_output.h"
 #include "aout_internal.h"
+#include <vlc/input.h>                 /* for input_thread_t and i_pts_delay */
 
 /*
  * Creation/Deletion
 /*****************************************************************************
  * aout_DecNew : create a decoder
  *****************************************************************************/
-static aout_input_t * DecNew( aout_instance_t * p_aout,
+static aout_input_t * DecNew( vlc_object_t * p_this, aout_instance_t * p_aout,
                               audio_sample_format_t * p_format )
 {
     aout_input_t * p_input;
+    input_thread_t * p_input_thread;
+    vlc_value_t val;
 
     /* We can only be called by the decoder, so no need to lock
      * p_input->lock. */
@@ -80,14 +83,8 @@ static aout_input_t * DecNew( aout_instance_t * p_aout,
     {
         int i;
 
-        if ( var_Type( p_aout, "audio-device" ) != 0 )
-        {
-            var_Destroy( p_aout, "audio-device" );
-        }
-        if ( var_Type( p_aout, "audio-channels" ) != 0 )
-        {
-            var_Destroy( p_aout, "audio-channels" );
-        }
+        var_Destroy( p_aout, "audio-device" );
+        var_Destroy( p_aout, "audio-channels" );
 
         /* Recreate the output using the new format. */
         if ( aout_OutputNew( p_aout, p_format ) < 0 )
@@ -123,12 +120,30 @@ static aout_input_t * DecNew( aout_instance_t * p_aout,
         return NULL;
     }
 
-    aout_MixerNew( p_aout );
-
     aout_InputNew( p_aout, p_input );
 
     vlc_mutex_unlock( &p_aout->mixer_lock );
 
+    var_Create( p_this, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
+    var_Get( p_this, "audio-desync", &val );
+    p_input->i_desync = val.i_int * 1000;
+
+    p_input_thread = (input_thread_t *)vlc_object_find( p_this,
+                                           VLC_OBJECT_INPUT, FIND_PARENT );
+    if( p_input_thread )
+    {
+        p_input->i_pts_delay = p_input_thread->i_pts_delay;
+        p_input->i_pts_delay += p_input->i_desync;
+        p_input->p_input_thread = p_input_thread;
+        vlc_object_release( p_input_thread );
+    }
+    else
+    {
+        p_input->i_pts_delay = DEFAULT_PTS_DELAY;
+        p_input->i_pts_delay += p_input->i_desync;
+        p_input->p_input_thread = NULL;
+    }
+
     return p_input;
 }
 
@@ -151,6 +166,7 @@ aout_input_t * __aout_DecNew( vlc_object_t * p_this,
             {
                 return NULL;
             }
+            vlc_object_attach( *pp_aout, p_this->p_libvlc );
         }
         else
         {
@@ -158,7 +174,7 @@ aout_input_t * __aout_DecNew( vlc_object_t * p_this,
         }
     }
 
-    return DecNew( *pp_aout, p_format );
+    return DecNew( p_this, *pp_aout, p_format );
 }
 
 /*****************************************************************************
@@ -285,6 +301,27 @@ int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
         return -1;
     }
 
+    /* Apply the desynchronisation requested by the user */
+    p_buffer->start_date += p_input->i_desync;
+    p_buffer->end_date += p_input->i_desync;
+
+    if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
+         AOUT_MAX_ADVANCE_TIME )
+    {
+        msg_Warn( p_aout, "received buffer in the future ("I64Fd")",
+                  p_buffer->start_date - mdate());
+        if( p_input->p_input_thread )
+        {
+            vlc_mutex_lock( &p_input->p_input_thread->counters.counters_lock);
+            stats_UpdateInteger( p_aout,
+                           p_input->p_input_thread->counters.p_lost_abuffers,
+                           1, NULL );
+            vlc_mutex_unlock( &p_input->p_input_thread->counters.counters_lock);
+        }
+        aout_BufferFree( p_buffer );
+        return -1;
+    }
+
     p_buffer->end_date = p_buffer->start_date
                             + (mtime_t)(p_buffer->i_nb_samples * 1000000)
                                 / p_input->input.i_rate;
@@ -306,7 +343,7 @@ int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
                             / p_input->input.i_rate;
 
         aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
-        p_aout->p_vlc->pf_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
+        p_aout->p_libvlc->pf_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
                                   p_buffer->i_nb_bytes );
         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
@@ -331,8 +368,15 @@ int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
     /* Run the mixer if it is able to run. */
     vlc_mutex_lock( &p_aout->mixer_lock );
     aout_MixerRun( p_aout );
+    if( p_input->p_input_thread )
+    {
+        vlc_mutex_lock( &p_input->p_input_thread->counters.counters_lock);
+        stats_UpdateInteger( p_aout,
+                             p_input->p_input_thread->counters.p_played_abuffers,
+                             1, NULL );
+        vlc_mutex_unlock( &p_input->p_input_thread->counters.counters_lock);
+    }
     vlc_mutex_unlock( &p_aout->mixer_lock );
 
     return 0;
 }
-