]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/oss.c
* modules/audio_output/oss.c: when now set the fragment size of the OSS device
[vlc] / modules / audio_output / oss.c
index 25e28cbb007161daee5bcfa0d7d699e39f4e46cb..736ebfbb06458ea76a9dc1ff9adab0bd342f6ecb 100644 (file)
@@ -2,7 +2,7 @@
  * oss.c : OSS /dev/dsp module for vlc
  *****************************************************************************
  * Copyright (C) 2000-2002 VideoLAN
- * $Id: oss.c,v 1.5 2002/08/11 01:27:01 massiot Exp $
+ * $Id: oss.c,v 1.32 2002/10/25 15:21:42 gbazin 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;
+    int i_fd;
+    int b_workaround_buggy_driver;
+    int i_fragstotal;
+    mtime_t max_buffer_duration;
 };
 
-#define DEFAULT_FRAME_SIZE 2048
+/* This must be a power of 2. */
+#define FRAME_SIZE 1024
+#define FRAME_COUNT 4
+#define A52_FRAME_NB 1536
 
 /*****************************************************************************
  * Local prototypes
@@ -74,18 +78,28 @@ 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 * );
 
+static mtime_t BufferDuration( aout_instance_t * p_aout );
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
+#define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
+#define BUGGY_LONGTEXT N_( \
+    "Some buggy OSS drivers just don't like when their internal buffers " \
+    "are completely filled (the sound gets heavily hashed). If you have one " \
+    "of these drivers, then you need to enable this option." )
+
 vlc_module_begin();
-    add_category_hint( N_("Audio"), NULL );
-    add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
+    add_category_hint( N_("OSS"), NULL );
+    add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
+              N_("OSS dsp device"), NULL );
+    add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT );
     set_description( _("Linux OSS /dev/dsp module") );
     set_capability( "audio output", 100 );
+    add_shortcut( "oss" );
     set_callbacks( Open, Close );
 vlc_module_end();
 
@@ -106,142 +120,277 @@ static int Open( vlc_object_t *p_this )
     if( p_sys == NULL )
     {
         msg_Err( p_aout, "out of memory" );
-        return 1;
+        return VLC_ENOMEM;
     }
 
-    /* Initialize some variables */
+    /* Get device name */
     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
     {
         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 );
-        free( psz_device );
-        free( p_sys );
-        return -1;
-    }
+    p_sys->i_fd = open( psz_device, O_WRONLY );
     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 ) )
+    if( p_sys->i_fd < 0 )
     {
-        msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
-        free( psz_device );
+        msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
         free( p_sys );
-        return -1;
+        return VLC_EGENERIC;
     }
 
-    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;
+        close( p_sys->i_fd );
+        free( p_sys );
+        return VLC_EGENERIC;
     }
-
+    
     /* Set the output format */
-    if ( AOUT_FMT_IS_SPDIF( &p_aout->output.output ) )
-    {
-        p_aout->output.output.i_format = i_format = AOUT_FMT_SPDIF;
-        p_aout->output.i_nb_samples = 1;
-    }
-    else
+    if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
     {
-        p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
-        p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
-    }
+        int i_format = AFMT_AC3;
 
-    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( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
+             || i_format != AFMT_AC3 )
+        {
+            p_aout->output.output.i_format = AOUT_FMT_S16_NE;
+        }
+        else
+        {
+            p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
+            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 );
+        }
     }
 
-    /* FIXME */
-    if ( p_aout->output.output.i_channels > 2 )
+    if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
     {
-        msg_Warn( p_aout, "only two channels are supported at the moment" );
-        /* Trigger downmixing */
-        p_aout->output.output.i_channels = 2;
-    }
+        int i_format = AFMT_S16_NE;
+        int i_frame_size, i_fragments;
+        int i_rate;
+        int i_nb_channels;
+        audio_buf_info audio_buf;
 
-    /* Set the number of channels */
-    b_stereo = p_aout->output.output.i_channels - 1;
+        if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
+        {
+            msg_Err( p_aout, "cannot set audio output format" );
+            close( p_sys->i_fd );
+            free( p_sys );
+            return VLC_EGENERIC;
+        }
 
-    if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
-    {
-        msg_Err( p_aout, "cannot set number of audio channels (%i)",
-                          p_aout->output.output.i_channels );
-        return -1;
-    }
+        switch ( i_format )
+        {
+        case AFMT_U8:
+            p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
+            break;
+        case AFMT_S8:
+            p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
+            break;
+        case AFMT_U16_LE:
+            p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
+            break;
+        case AFMT_S16_LE:
+            p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
+            break;
+        case AFMT_U16_BE:
+            p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
+            break;
+        case AFMT_S16_BE:
+            p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
+            break;
+        default:
+            msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
+                     i_format );
+            close( p_sys->i_fd );
+            free( p_sys );
+            return VLC_EGENERIC;
+        }
 
-    if ( b_stereo + 1 != p_aout->output.output.i_channels )
-    {
-        msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
-                          p_aout->output.output.i_channels,
-                          b_stereo + 1 );
-        p_aout->output.output.i_channels = b_stereo + 1;
-    }
+        /* These cases are desperate because of the OSS API and A/52 spec. */
+        switch ( p_aout->output.output.i_channels )
+        {
+            case AOUT_CHAN_3F:
+            case AOUT_CHAN_2F1R:
+            case AOUT_CHAN_3F1R:
+            case AOUT_CHAN_STEREO | AOUT_CHAN_LFE:
+            case AOUT_CHAN_2F1R | AOUT_CHAN_LFE:
+            case AOUT_CHAN_3F1R | AOUT_CHAN_LFE:
+            case AOUT_CHAN_DOLBY | AOUT_CHAN_LFE:
+                p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
+                break;
+            case AOUT_CHAN_CHANNEL | AOUT_CHAN_LFE:
+            case AOUT_CHAN_CHANNEL1 | AOUT_CHAN_LFE:
+            case AOUT_CHAN_CHANNEL2 | AOUT_CHAN_LFE:
+            case AOUT_CHAN_MONO | AOUT_CHAN_LFE:
+            case AOUT_CHAN_2F2R | AOUT_CHAN_LFE:
+                p_aout->output.output.i_channels &= ~AOUT_CHAN_LFE;
+                break;
+            case AOUT_CHAN_3F2R:
+                p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
+                break;
+        }
+        /* In a nutshell, possible types : AOUT_CHAN_STEREO (and al.),
+           AOUT_CHAN_2F2R, AOUT_CHAN_3F1R | AOUT_CHAN_LFE. */
 
-    /* Set the output rate */
-    i_rate = p_aout->output.output.i_rate;
-    if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
-    {
-        msg_Err( p_aout, "cannot set audio output rate (%i)",
-                         p_aout->output.output.i_rate );
-        return -1;
+        i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
+
+        if ( i_nb_channels > 2 )
+        {
+            /* Check that the device supports this. */
+
+#ifdef SNDCTL_DSP_GETCHANNELMASK
+            int i_chanmask;
+            if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
+                        &i_chanmask ) == 0 )
+            {
+                if ( !(i_chanmask & DSP_BIND_FRONT) )
+                {
+                    msg_Err( p_aout, "No front channels ! (%x)",
+                             i_chanmask );
+                    close( p_sys->i_fd );
+                    free( p_sys );
+                    return VLC_EGENERIC;
+                }
+
+                if ( !(i_chanmask & DSP_BIND_SURR) )
+                {
+                    p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
+                    i_nb_channels = 2;
+                }
+                if ( p_aout->output.output.i_channels ==
+                         (AOUT_CHAN_3F2R | AOUT_CHAN_LFE)
+                      && !(i_chanmask & DSP_BIND_CENTER_LFE) )
+                {
+                    p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
+                    i_nb_channels = 4;
+                }
+            }
+            else
+#endif
+            {
+                /* The driver doesn't support this call, assume it is stereo. */
+                p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
+                i_nb_channels = 2;
+            }
+        }
+
+        /* Set the number of channels */
+        if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 )
+        {
+            msg_Err( p_aout, "cannot set number of audio channels (%i)",
+                              p_aout->output.output.i_channels );
+            close( p_sys->i_fd );
+            free( p_sys );
+            return VLC_EGENERIC;
+        }
+
+        if ( i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
+        {
+            switch ( i_nb_channels )
+            {
+            case 1: p_aout->output.output.i_channels = AOUT_CHAN_MONO; break;
+            case 2: p_aout->output.output.i_channels = AOUT_CHAN_STEREO; break;
+            case 4: p_aout->output.output.i_channels = AOUT_CHAN_2F2R; break;
+            default:
+                msg_Err( p_aout, "Unsupported downmixing (%d)", i_nb_channels );
+                close( p_sys->i_fd );
+                free( p_sys );
+                return VLC_EGENERIC;
+            }
+        }
+
+        /* Set the output rate */
+        i_rate = p_aout->output.output.i_rate;
+        if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
+        {
+            msg_Err( p_aout, "cannot set audio output rate (%i)",
+                             p_aout->output.output.i_rate );
+            close( p_sys->i_fd );
+            free( p_sys );
+            return VLC_EGENERIC;
+        }
+
+        if( i_rate != p_aout->output.output.i_rate )
+        {
+            p_aout->output.output.i_rate = i_rate;
+        }
+
+        /* Set the fragment size */
+        aout_FormatPrepare( &p_aout->output.output );
+
+        /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
+         *                              1 << yyyy   is fragsize */
+        i_fragments = 0;
+        i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
+        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_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
+        }
+
+        if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
+        {
+            msg_Warn( p_aout, "cannot get fragment size" );
+            close( p_sys->i_fd );
+            free( p_sys );
+            return VLC_EGENERIC;
+        }
+       else
+        {
+            /* Number of fragments actually allocated */
+            p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
+
+            /* Maximum duration the soundcard's buffer can hold */
+            p_aout->output.p_sys->max_buffer_duration =
+                (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
+                / p_aout->output.output.i_bytes_per_frame
+                / p_aout->output.output.i_rate
+                * p_aout->output.output.i_frame_length;
+
+            p_aout->output.i_nb_samples = audio_buf.fragsize /
+                p_aout->output.output.i_bytes_per_frame;
+        }
+
+        aout_VolumeSoftInit( p_aout );
     }
 
-    if( i_rate != p_aout->output.output.i_rate )
+    /* Create OSS thread and wait for its readiness. */
+    if( vlc_thread_create( p_aout, "aout", OSSThread,
+                           VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
     {
-        msg_Warn( p_aout, "driver forced resampling %li->%li",
-                          p_aout->output.output.i_rate, i_rate );
-        p_aout->output.output.i_rate = i_rate;
+        msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
+        close( p_sys->i_fd );
+        free( p_sys );
+        return VLC_ETHREAD;
     }
 
-    p_sys->b_initialized = VLC_TRUE;
+    p_aout->output.p_sys->b_workaround_buggy_driver =
+        config_GetInt( p_aout, "oss-buggy" );
 
-    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 );
 }
 
 /*****************************************************************************
@@ -252,33 +401,42 @@ 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;
 }
 
 /*****************************************************************************
@@ -287,47 +445,88 @@ 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 )
     {
-        int i_bytes_per_sample;
-        aout_buffer_t * p_buffer;
-        mtime_t next_date;
+        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 != VLC_FOURCC('s','p','d','i') )
         {
-            msleep( THREAD_SLEEP );
-            continue;
+            mtime_t buffered = BufferDuration( p_aout );
+
+            if( p_aout->output.p_sys->b_workaround_buggy_driver )
+            {
+#define i_fragstotal p_aout->output.p_sys->i_fragstotal
+                /* Wait a bit - we don't want our buffer to be full */
+                if( buffered > (p_aout->output.p_sys->max_buffer_duration
+                                / i_fragstotal * (i_fragstotal - 1)) )
+                {
+                    msleep((p_aout->output.p_sys->max_buffer_duration
+                                / i_fragstotal ));
+                    buffered = BufferDuration( p_aout );
+                }
+#undef i_fragstotal
+            }
+
+            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 );
@@ -341,7 +540,11 @@ static int OSSThread( aout_instance_t * p_aout )
         {
             aout_BufferFree( p_buffer );
         }
+        else
+        {
+            free( p_bytes );
+        }
     }
 
-    return 0;
+    return VLC_SUCCESS;
 }