]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/oss.c
* ALL: More hooks for audio volume management.
[vlc] / modules / audio_output / oss.c
index 28189db1f67c1eded7d6ef019a53072b3a07e3af..c3c3582a32e4ad836b1d2b99dd9917bd659c503f 100644 (file)
@@ -2,7 +2,7 @@
  * oss.c : OSS /dev/dsp module for vlc
  *****************************************************************************
  * Copyright (C) 2000-2002 VideoLAN
- * $Id: oss.c,v 1.7 2002/08/12 09:34:15 sam Exp $
+ * $Id: oss.c,v 1.26 2002/09/18 21:21:23 massiot Exp $
  *
  * Authors: Michel Kaempf <maxx@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
 struct aout_sys_t
 {
     int                   i_fd;
-    volatile vlc_bool_t   b_die;
-    volatile vlc_bool_t   b_initialized;
 };
 
-#define DEFAULT_FRAME_SIZE 2048
+#define FRAME_SIZE 1024
+#define FRAME_COUNT 8
+#define A52_FRAME_NB 1536
 
 /*****************************************************************************
  * Local prototypes
@@ -74,18 +74,18 @@ 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 *, aout_buffer_t * );
+static void Play         ( aout_instance_t * );
 static int  OSSThread    ( aout_instance_t * );
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin();
-    add_category_hint( N_("Audio"), NULL );
+    add_category_hint( N_("OSS"), NULL );
     add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
     set_description( _("Linux OSS /dev/dsp module") );
     set_capability( "audio output", 100 );
+    add_shortcut( "dsp" );
     set_callbacks( Open, Close );
 vlc_module_end();
 
@@ -100,13 +100,18 @@ 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 ) );
     if( p_sys == NULL )
     {
         msg_Err( p_aout, "out of memory" );
-        return 1;
+        return VLC_ENOMEM;
     }
 
     /* Initialize some variables */
@@ -114,82 +119,87 @@ static int Open( vlc_object_t *p_this )
     {
         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
         free( p_sys );
-        return -1;
+        return VLC_EGENERIC;
     }
 
     /* Open the sound device */
     if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
     {
-        msg_Err( p_aout, "cannot open audio device (%s)",
-                          psz_device );
+        msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
         free( psz_device );
         free( p_sys );
-        return -1;
+        return VLC_EGENERIC;
     }
     free( psz_device );
 
-    /* Create OSS thread and wait for its readiness. */
-    p_sys->b_die = 0;
-    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) );
-        free( psz_device );
-        free( p_sys );
-        return -1;
-    }
-
-    p_aout->output.pf_setformat = SetFormat;
     p_aout->output.pf_play = Play;
 
-    return 0;
-}
-
-/*****************************************************************************
- * 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;
-    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 -1;
+        return VLC_EGENERIC;
+    }
+    
+    /* 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 );
+        return VLC_EGENERIC;
     }
 
     /* Set the output format */
-    if ( AOUT_FMT_IS_SPDIF( &p_aout->output.output ) )
+    if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
     {
-        p_aout->output.output.i_format = i_format = AOUT_FMT_SPDIF;
-        p_aout->output.i_nb_samples = 1;
+        i_format = AOUT_FMT_SPDIF;
+        p_aout->output.i_nb_samples = A52_FRAME_NB;
+        p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
+        p_aout->output.output.i_frame_length = A52_FRAME_NB;
+
+        aout_VolumeNoneInit( p_aout );
     }
     else
     {
         p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
-        p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
+        p_aout->output.i_nb_samples = FRAME_SIZE;
+
+        aout_VolumeSoftInit( p_aout );
     }
 
     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
          || i_format != p_aout->output.output.i_format )
     {
-        msg_Err( p_aout, "cannot set audio output format (%i)",
-                          i_format );
-        return -1;
+        if ( i_format == AOUT_FMT_SPDIF )
+        {
+            /* Retry with S16 */
+            msg_Warn( p_aout, "cannot set audio output format (%i)", i_format );
+            p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
+            p_aout->output.i_nb_samples = FRAME_SIZE;
+            if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
+                 || i_format != p_aout->output.output.i_format )
+            {
+                msg_Err( p_aout, "cannot set audio output format (%i)",
+                         i_format );
+                return VLC_EGENERIC;
+            }
+        }
+        else
+        {
+            msg_Err( p_aout, "cannot set audio output format (%i)", i_format );
+            return VLC_EGENERIC;
+        }
     }
 
-    if ( !AOUT_FMT_IS_SPDIF( &p_aout->output.output ) )
+    if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
     {
         /* FIXME */
         if ( p_aout->output.output.i_channels > 2 )
@@ -206,7 +216,7 @@ static int SetFormat( aout_instance_t *p_aout )
         {
             msg_Err( p_aout, "cannot set number of audio channels (%i)",
                               p_aout->output.output.i_channels );
-            return -1;
+            return VLC_EGENERIC;
         }
 
         if ( b_stereo + 1 != p_aout->output.output.i_channels )
@@ -223,7 +233,7 @@ static int SetFormat( aout_instance_t *p_aout )
         {
             msg_Err( p_aout, "cannot set audio output rate (%i)",
                              p_aout->output.output.i_rate );
-            return -1;
+            return VLC_EGENERIC;
         }
 
         if( i_rate != p_aout->output.output.i_rate )
@@ -234,17 +244,25 @@ 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 0;
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * Play: queue a buffer for playing by OSSThread
+ * Play: nothing to do
  *****************************************************************************/
-static void Play( aout_instance_t *p_aout, aout_buffer_t * p_buffer )
+static void Play( aout_instance_t *p_aout )
 {
-    aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
 }
 
 /*****************************************************************************
@@ -255,33 +273,43 @@ static void Close( vlc_object_t * p_this )
     aout_instance_t *p_aout = (aout_instance_t *)p_this;
     struct aout_sys_t * p_sys = p_aout->output.p_sys;
 
-    p_sys->b_die = 1;
+    p_aout->b_die = VLC_TRUE;
     vlc_thread_join( p_aout );
+    p_aout->b_die = VLC_FALSE;
 
+    ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
     close( p_sys->i_fd );
+
     free( p_sys );
 }
 
 
 /*****************************************************************************
- * 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;
 }
 
 /*****************************************************************************
@@ -290,46 +318,81 @@ 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_sys->b_die )
+    while ( !p_aout->b_die )
     {
-        aout_buffer_t * p_buffer;
-        mtime_t next_date = 0;
+        aout_buffer_t * p_buffer = NULL;
         int i_tmp, i_size;
         byte_t * p_bytes;
 
-        if( !p_sys->b_initialized )
+        if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
         {
-            msleep( THREAD_SLEEP );
-            continue;
+            mtime_t buffered = BufferDuration( p_aout );
+
+            /* Wait a bit - we don't want our buffer to be full */
+            while( buffered > AOUT_PTS_TOLERANCE * 2 )
+            {
+                msleep( buffered / 2 - 10000 );
+                buffered = BufferDuration( p_aout );
+            }
+
+            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, next_date, VLC_FALSE );
         }
-
-        if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
+        else
         {
-            /* Get the presentation date of the next write() operation. It
-             * is equal to the current date + duration of buffered samples.
-             * Order is important here, since GetBufInfo is believed to take
-             * more time than mdate(). */
-            next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
-                      / aout_FormatToByterate( &p_aout->output.output,
-                                               p_aout->output.output.i_rate );
-            next_date += mdate();
+            /* emu10k1 driver does not report Buffer Duration correctly in
+             * passthrough mode so we have to cheat */
+            if( !next_date )
+            {
+                next_date = mdate();
+            }
+            else
+            {
+                mtime_t delay = next_date - mdate();
+                if( delay > AOUT_PTS_TOLERANCE )
+                {
+                    msleep( delay / 2 );
+                }
+            }
+            
+            while( !p_aout->b_die && ! ( p_buffer =
+                aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
+            {
+                msleep( 1000 );
+                next_date = mdate();
+            }
         }
 
-        p_buffer = aout_OutputNextBuffer( p_aout, next_date );
-
         if ( p_buffer != NULL )
         {
             p_bytes = p_buffer->p_buffer;
-            i_size = aout_FormatToSize( &p_aout->output.output,
-                                        p_buffer->i_nb_samples );
+            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
         {
-            i_size = aout_FormatToSize( &p_aout->output.output,
-                                        DEFAULT_FRAME_SIZE );
-            p_bytes = alloca( i_size );
+            i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
+                      * 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 );
@@ -343,7 +406,11 @@ static int OSSThread( aout_instance_t * p_aout )
         {
             aout_BufferFree( p_buffer );
         }
+        else
+        {
+            free( p_bytes );
+        }
     }
 
-    return 0;
+    return VLC_SUCCESS;
 }