]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/oss.c
*It seems that the setting of the fragment size was buggy.
[vlc] / modules / audio_output / oss.c
index 47d9b071ffc188541d430f667ec8a1793b2ac6cf..9633e24af1276d82f09b37a5017a13e4ed489ecc 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.22 2002/08/31 22:10:25 stef Exp $
  *
  * Authors: Michel Kaempf <maxx@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
@@ -62,7 +62,6 @@
 struct aout_sys_t
 {
     int                   i_fd;
-    volatile vlc_bool_t   b_initialized;
 };
 
 #define FRAME_SIZE 1024
@@ -75,7 +74,6 @@ struct aout_sys_t
 static int  Open         ( vlc_object_t * );
 static void Close        ( vlc_object_t * );
 
-static int  SetFormat    ( aout_instance_t * );
 static void Play         ( aout_instance_t * );
 static int  OSSThread    ( aout_instance_t * );
 
@@ -102,6 +100,11 @@ static int Open( vlc_object_t *p_this )
     aout_instance_t * p_aout = (aout_instance_t *)p_this;
     struct aout_sys_t * p_sys;
     char * psz_device;
+    int i_format;
+    int i_rate;
+    int i_frame_size;
+    int i_fragments;
+    vlc_bool_t b_stereo;
 
     /* Allocate structure */
     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
@@ -129,50 +132,25 @@ static int Open( vlc_object_t *p_this )
     }
     free( psz_device );
 
-    /* Create OSS thread and wait for its readiness. */
-    p_sys->b_initialized = VLC_FALSE;
-    if( vlc_thread_create( p_aout, "aout", OSSThread, VLC_FALSE ) )
-    {
-        msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
-        close( p_sys->i_fd );
-        free( psz_device );
-        free( p_sys );
-        return VLC_ETHREAD;
-    }
-
-    p_aout->output.pf_setformat = SetFormat;
     p_aout->output.pf_play = Play;
 
-    return VLC_SUCCESS;
-}
-
-/*****************************************************************************
- * SetFormat: reset the dsp and set its format
- *****************************************************************************
- * This functions resets the DSP device, tries to initialize the output
- * format with the value contained in the dsp structure, and if this value
- * could not be set, the default value returned by ioctl is set. It then
- * does the same for the stereo mode, and for the output rate.
- *****************************************************************************/
-static int SetFormat( aout_instance_t *p_aout )
-{
-    struct aout_sys_t * p_sys = p_aout->output.p_sys;
-    int i_format;
-    int i_rate;
-    int i_fragments;
-    vlc_bool_t b_stereo;
-
-    p_sys->b_initialized = VLC_FALSE;
-
     /* Reset the DSP device */
     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
     {
         msg_Err( p_aout, "cannot reset OSS audio device" );
         return VLC_EGENERIC;
     }
-
-    /* Set the fragment size */
-    i_fragments = FRAME_COUNT << 16 | FRAME_SIZE;
+    
+    /* Set the fragment size
+     * i_fragment = xxxxyyyy where: xxxx        is fragtotal
+     *                              1 << yyyy   is fragsize */
+    i_fragments = 0;
+    i_frame_size = FRAME_SIZE;
+    while( i_frame_size >>= 1 )
+    {
+        ++i_fragments;
+    }
+    i_fragments |= FRAME_COUNT << 16;
     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
     {
         msg_Err( p_aout, "cannot set fragment size (%.8x)", i_fragments );
@@ -245,13 +223,22 @@ static int SetFormat( aout_instance_t *p_aout )
         }
     }
 
-    p_sys->b_initialized = VLC_TRUE;
+    /* Create OSS thread and wait for its readiness. */
+    if( vlc_thread_create( p_aout, "aout", OSSThread,
+                           VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
+    {
+        msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
+        close( p_sys->i_fd );
+        free( psz_device );
+        free( p_sys );
+        return VLC_ETHREAD;
+    }
 
     return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * Play: queue a buffer for playing by OSSThread
+ * Play: nothing to do
  *****************************************************************************/
 static void Play( aout_instance_t *p_aout )
 {
@@ -277,24 +264,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 microseconds 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 +297,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 )
     {
@@ -310,33 +305,32 @@ static int OSSThread( aout_instance_t * p_aout )
         int i_tmp, i_size;
         byte_t * p_bytes;
 
-        if( !p_sys->b_initialized )
-        {
-            msleep( THREAD_SLEEP );
-            continue;
-        }
-
         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 +341,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 +351,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 );