]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/oss.c
* ./bootstrap : Fixed an issue with old shell versions
[vlc] / modules / audio_output / oss.c
index 7e1f67d2054c0fd4d968e2b2f489da2750e18109..4cc278ed855af7e51ba48aa60f6aed94a0cbc487 100644 (file)
@@ -2,7 +2,7 @@
  * oss.c : OSS /dev/dsp module for vlc
  *****************************************************************************
  * Copyright (C) 2000-2002 VideoLAN
- * $Id: oss.c,v 1.9 2002/08/14 00:23:59 massiot Exp $
+ * $Id: oss.c,v 1.20 2002/08/29 23:53:22 massiot Exp $
  *
  * Authors: Michel Kaempf <maxx@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
@@ -65,7 +65,8 @@ struct aout_sys_t
     volatile vlc_bool_t   b_initialized;
 };
 
-#define FRAME_SIZE 2048
+#define FRAME_SIZE 1024
+#define FRAME_COUNT 8
 #define A52_FRAME_NB 1536
 
 /*****************************************************************************
@@ -75,17 +76,18 @@ 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();
 
@@ -106,7 +108,7 @@ 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 */
@@ -114,7 +116,7 @@ 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 */
@@ -123,24 +125,26 @@ static int Open( vlc_object_t *p_this )
         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_initialized = VLC_FALSE;
-    if( vlc_thread_create( p_aout, "aout", OSSThread, 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 -1;
+        return VLC_ETHREAD;
     }
 
     p_aout->output.pf_setformat = SetFormat;
     p_aout->output.pf_play = Play;
 
-    return 0;
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
@@ -156,6 +160,7 @@ 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;
@@ -164,15 +169,24 @@ static int SetFormat( aout_instance_t *p_aout )
     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_fragments = FRAME_COUNT << 16 | FRAME_SIZE;
+    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 ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
     {
+        i_format = AOUT_FMT_SPDIF;
         p_aout->output.i_nb_samples = A52_FRAME_NB;
-        p_aout->output.output.i_bytes_per_sec = p_aout->output.output.i_rate
-                                     * AOUT_SPDIF_SIZE / A52_FRAME_NB;
+        p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
+        p_aout->output.output.i_frame_length = A52_FRAME_NB;
     }
     else
     {
@@ -183,9 +197,8 @@ static int SetFormat( aout_instance_t *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;
+        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 )
@@ -205,7 +218,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 )
@@ -222,7 +235,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 )
@@ -235,15 +248,14 @@ static int SetFormat( aout_instance_t *p_aout )
 
     p_sys->b_initialized = VLC_TRUE;
 
-    return 0;
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
  * Play: queue a buffer for playing by OSSThread
  *****************************************************************************/
-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 );
 }
 
 /*****************************************************************************
@@ -254,33 +266,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_aout->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 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;
 }
 
 /*****************************************************************************
@@ -289,11 +311,11 @@ 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 )
     {
         aout_buffer_t * p_buffer;
-        mtime_t next_date = 0;
         int i_tmp, i_size;
         byte_t * p_bytes;
 
@@ -305,29 +327,51 @@ static int OSSThread( aout_instance_t * p_aout )
 
         if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
         {
-            /* 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 );
-            next_date += mdate();
+            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 );
+        }
+        else
+        {
+            p_buffer = aout_OutputNextBuffer( p_aout, 0, VLC_TRUE );
         }
-
-        p_buffer = aout_OutputNextBuffer( p_aout, next_date );
 
         if ( p_buffer != NULL )
         {
             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
         {
-            i_size = aout_FormatToByterate( &p_aout->output.output )
-                      * FRAME_SIZE
-                      / p_aout->output.output.i_rate;
-            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 +385,11 @@ static int OSSThread( aout_instance_t * p_aout )
         {
             aout_BufferFree( p_buffer );
         }
+        else
+        {
+            free( p_bytes );
+        }
     }
 
-    return 0;
+    return VLC_SUCCESS;
 }