]> git.sesse.net Git - vlc/commitdiff
* ./modules/audio_output/oss.c, ./modules/audio_output/sdl.c: improved
authorSam Hocevar <sam@videolan.org>
Sun, 25 Aug 2002 16:55:55 +0000 (16:55 +0000)
committerSam Hocevar <sam@videolan.org>
Sun, 25 Aug 2002 16:55:55 +0000 (16:55 +0000)
    sound synchronization.
  * ./src/audio_output/output.c: reverted another change I previously did :-)

modules/audio_output/oss.c
modules/audio_output/sdl.c
src/audio_output/output.c

index 47d9b071ffc188541d430f667ec8a1793b2ac6cf..111e408e87269c8e1e9356a6a3c3d7e1029f3b2a 100644 (file)
@@ -2,7 +2,7 @@
  * oss.c : OSS /dev/dsp module for vlc
  *****************************************************************************
  * Copyright (C) 2000-2002 VideoLAN
- * $Id: oss.c,v 1.18 2002/08/25 09:40:00 sam Exp $
+ * $Id: oss.c,v 1.19 2002/08/25 16:55:55 sam Exp $
  *
  * Authors: Michel Kaempf <maxx@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
@@ -277,24 +277,31 @@ static void Close( vlc_object_t * p_this )
 
 
 /*****************************************************************************
- * GetBufInfo: buffer status query
+ * BufferDuration: buffer status query
  *****************************************************************************
- * This function fills in the audio_buf_info structure :
- * - returns : number of available fragments (not partially used ones)
- * - int fragstotal : total number of fragments allocated
- * - int fragsize : size of a fragment in bytes
- * - int bytes : available space in bytes (includes partially used fragments)
- * Note! 'bytes' could be more than fragments*fragsize
+ * This function returns the duration in microseconfs of the current buffer.
  *****************************************************************************/
-static int GetBufInfo( aout_instance_t * p_aout )
+static mtime_t BufferDuration( aout_instance_t * p_aout )
 {
     struct aout_sys_t * p_sys = p_aout->output.p_sys;
     audio_buf_info audio_buf;
+    int i_bytes;
 
+    /* Fill the audio_buf_info structure:
+     * - fragstotal: total number of fragments allocated
+     * - fragsize: size of a fragment in bytes
+     * - bytes: available space in bytes (includes partially used fragments)
+     * Note! 'bytes' could be more than fragments*fragsize */
     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
 
-    /* returns the allocated space in bytes */
-    return ( (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes );
+    /* calculate number of available fragments (not partially used ones) */
+    i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
+
+    /* Return the fragment duration */
+    return (mtime_t)i_bytes * 1000000
+            / p_aout->output.output.i_bytes_per_frame
+            / p_aout->output.output.i_rate
+            * p_aout->output.output.i_frame_length;
 }
 
 /*****************************************************************************
@@ -303,6 +310,7 @@ static int GetBufInfo( aout_instance_t * p_aout )
 static int OSSThread( aout_instance_t * p_aout )
 {
     struct aout_sys_t * p_sys = p_aout->output.p_sys;
+    mtime_t next_date = 0;
 
     while ( !p_aout->b_die )
     {
@@ -318,25 +326,30 @@ static int OSSThread( aout_instance_t * p_aout )
 
         if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
         {
-            mtime_t buffered;
+            mtime_t buffered = BufferDuration( p_aout );
 
-            do
+            /* Wait a bit - we don't want our buffer to be full */
+            while( buffered > AOUT_PTS_TOLERANCE * 2 )
             {
-                buffered = (mtime_t)GetBufInfo( p_aout ) * 1000000
-                            / p_aout->output.output.i_bytes_per_frame
-                            / p_aout->output.output.i_rate
-                            * p_aout->output.output.i_frame_length;
-                if( buffered < 50000 )
-                {
-                    break;
-                }
                 msleep( buffered / 2 - 10000 );
+                buffered = BufferDuration( p_aout );
+            }
 
-            } while( VLC_TRUE );
+            if( !next_date )
+            {
+                /* This is the _real_ presentation date */
+                next_date = mdate() + buffered;
+            }
+            else
+            {
+                /* Give a hint to the audio output about our drift, but
+                 * not too much because we want to make it happy with our
+                 * nicely calculated dates. */
+                next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
+            }
 
             /* Next buffer will be played at mdate()+buffered */
-            p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
-                                              VLC_FALSE );
+            p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
         }
         else
         {
@@ -347,6 +360,9 @@ static int OSSThread( aout_instance_t * p_aout )
         {
             p_bytes = p_buffer->p_buffer;
             i_size = p_buffer->i_nb_bytes;
+            /* This is theoretical ... we'll see next iteration whether
+             * we're drifting */
+            next_date += p_buffer->end_date - p_buffer->start_date;
         }
         else
         {
@@ -354,6 +370,7 @@ static int OSSThread( aout_instance_t * p_aout )
                       * p_aout->output.output.i_bytes_per_frame;
             p_bytes = malloc( i_size );
             memset( p_bytes, 0, i_size );
+            next_date = 0;
         }
 
         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
index 0d63ee235dfeaa9ed16a3cf2267a8899f8c26664..54a6e86b74f7247eb899b67046eb8e7ff7cea92f 100644 (file)
@@ -2,7 +2,7 @@
  * sdl.c : SDL audio output plugin for vlc
  *****************************************************************************
  * Copyright (C) 2000-2002 VideoLAN
- * $Id: sdl.c,v 1.6 2002/08/25 09:40:00 sam Exp $
+ * $Id: sdl.c,v 1.7 2002/08/25 16:55:55 sam Exp $
  *
  * Authors: Michel Kaempf <maxx@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
@@ -48,7 +48,7 @@
  *****************************************************************************/
 struct aout_sys_t
 {   
-    mtime_t call_time;
+    mtime_t next_date;
     mtime_t buffer_time;
 };
 
@@ -78,7 +78,6 @@ vlc_module_end();
 static int Open ( vlc_object_t *p_this )
 {
     aout_instance_t *p_aout = (aout_instance_t *)p_this;
-    aout_sys_t * p_sys;
 
     Uint32 i_flags = SDL_INIT_AUDIO;
 
@@ -87,14 +86,6 @@ static int Open ( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
-    /* Allocate structure */
-    p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
-    if( p_sys == NULL )
-    {
-        msg_Err( p_aout, "out of memory" );
-        return VLC_ENOMEM;
-    }
-
     p_aout->output.pf_setformat = SetFormat;
     p_aout->output.pf_play = Play;
 
@@ -112,7 +103,6 @@ static int Open ( vlc_object_t *p_this )
     if( SDL_Init( i_flags ) < 0 )
     {
         msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
-        free( p_sys );
         return VLC_EGENERIC;
     }
 
@@ -124,8 +114,6 @@ static int Open ( vlc_object_t *p_this )
  *****************************************************************************/
 static int SetFormat( aout_instance_t *p_aout )
 {
-    aout_sys_t * p_sys = p_aout->output.p_sys;
-
     /* TODO: finish and clean this */
     SDL_AudioSpec desired;
 
@@ -145,10 +133,6 @@ static int SetFormat( aout_instance_t *p_aout )
     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
     p_aout->output.i_nb_samples = FRAME_SIZE;
 
-    p_sys->call_time = 0;
-    p_sys->buffer_time = (mtime_t)FRAME_SIZE * 1000000
-                          / p_aout->output.output.i_rate;
-
     SDL_PauseAudio( 0 );
 
     return VLC_SUCCESS;
@@ -166,14 +150,9 @@ static void Play( aout_instance_t * p_aout )
  *****************************************************************************/
 static void Close ( vlc_object_t *p_this )
 {
-    aout_instance_t *p_aout = (aout_instance_t *)p_this;
-    aout_sys_t * p_sys = p_aout->output.p_sys;
-
     SDL_PauseAudio( 1 );
     SDL_CloseAudio();
     SDL_QuitSubSystem( SDL_INIT_AUDIO );
-
-    free( p_sys );
 }
 
 /*****************************************************************************
@@ -182,35 +161,15 @@ static void Close ( vlc_object_t *p_this )
 static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
 {
     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
-    aout_sys_t * p_sys = p_aout->output.p_sys;
+    aout_buffer_t *   p_buffer;
 
-    aout_buffer_t * p_buffer;
+    /* SDL is unable to call us at regular times, or tell us its current
+     * hardware latency, or the buffer state. So we just pop data and throw
+     * it at SDL's face. Nah. */
 
-    /* We try to stay around call_time + buffer_time/2. This is kludgy but
-     * unavoidable because SDL is completely unable to 1. tell us about its
-     * latency, and 2. call SDLCallback at regular intervals. */
-    if( mdate() < p_sys->call_time + p_sys->buffer_time / 2 )
-    {
-        /* We can't wait too much, because SDL will be lost, and we can't
-         * wait too little, because we are not sure that there will be
-         * samples in the queue. */
-        mwait( p_sys->call_time + p_sys->buffer_time / 4 );
-        p_sys->call_time += p_sys->buffer_time;
-    }
-    else
-    {
-        p_sys->call_time = mdate() + p_sys->buffer_time / 4;
-    }
-
-    /* Tell the output we're playing samples at call_time + 2*buffer_time */
-    p_buffer = aout_OutputNextBuffer( p_aout, p_sys->call_time
-                                       + 2 * p_sys->buffer_time, VLC_TRUE );
-
-    if ( i_len != FRAME_SIZE * sizeof(s16)
-                    * p_aout->output.output.i_channels )
-    {
-        msg_Err( p_aout, "SDL doesn't know its buffer size (%d)", i_len );
-    }
+    vlc_mutex_lock( &p_aout->mixer_lock );
+    p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
+    vlc_mutex_unlock( &p_aout->mixer_lock );
 
     if ( p_buffer != NULL )
     {
index 9368de723a39ddd35bacc29654e15c70fc535fec..e356a005db7f8e63aabcd89c9d800ee3c3bab52a 100644 (file)
@@ -2,7 +2,7 @@
  * output.c : internal management of output streams for the audio output
  *****************************************************************************
  * Copyright (C) 2002 VideoLAN
- * $Id: output.c,v 1.11 2002/08/25 09:40:00 sam Exp $
+ * $Id: output.c,v 1.12 2002/08/25 16:55:55 sam Exp $
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
@@ -171,7 +171,7 @@ aout_buffer_t * aout_OutputNextBuffer( aout_instance_t * p_aout,
     vlc_mutex_lock( &p_aout->mixer_lock );
 
     p_buffer = p_aout->output.fifo.p_first;
-    while ( p_buffer && p_buffer->start_date < start_date && p_buffer->p_next )
+    while ( p_buffer && p_buffer->start_date < start_date )
     {
         msg_Dbg( p_aout, "audio output is too slow (%lld), trashing %lldus",
                  start_date - p_buffer->start_date,