]> 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 4cc278ed855af7e51ba48aa60f6aed94a0cbc487..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.20 2002/08/29 23:53:22 massiot 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>
@@ -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,51 +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_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;
-    }
-
-    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 );
@@ -187,18 +164,39 @@ static int SetFormat( aout_instance_t *p_aout )
         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 = 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 VLC_EGENERIC;
+        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 ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
@@ -246,13 +244,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 )
 {
@@ -280,7 +287,7 @@ static void Close( vlc_object_t * p_this )
 /*****************************************************************************
  * BufferDuration: buffer status query
  *****************************************************************************
- * This function returns the duration in microseconfs of the current buffer.
+ * This function returns the duration in microseconds of the current buffer.
  *****************************************************************************/
 static mtime_t BufferDuration( aout_instance_t * p_aout )
 {
@@ -315,16 +322,10 @@ static int OSSThread( aout_instance_t * p_aout )
 
     while ( !p_aout->b_die )
     {
-        aout_buffer_t * p_buffer;
+        aout_buffer_t * p_buffer = NULL;
         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 = BufferDuration( p_aout );
@@ -354,7 +355,27 @@ static int OSSThread( aout_instance_t * p_aout )
         }
         else
         {
-            p_buffer = aout_OutputNextBuffer( p_aout, 0, VLC_TRUE );
+            /* 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();
+            }
         }
 
         if ( p_buffer != NULL )