]> git.sesse.net Git - vlc/blobdiff - plugins/dsp/aout_dsp.c
Some heavy changes today:
[vlc] / plugins / dsp / aout_dsp.c
index 157ce257e4c9ce6a5d1596b014e967af79e74b1f..907994aa347405871f041cdbdf7cb0cd065a61f0 100644 (file)
@@ -1,9 +1,11 @@
 /*****************************************************************************
  * aout_dsp.c : dsp functions library
  *****************************************************************************
- * Copyright (C) 1999, 2000 VideoLAN
+ * Copyright (C) 1999-2001 VideoLAN
+ * $Id: aout_dsp.c,v 1.18 2001/12/30 07:09:54 sam Exp $
  *
- * Authors:
+ * Authors: Michel Kaempf <maxx@via.ecp.fr>
+ *          Samuel Hocevar <sam@zoy.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 /* TODO:
  *
- * - an aout_sysGetFormats() function
+ * - an aout_GetFormats() function
  * - dsp inline/static
  * - make this library portable (see mpg123)
- * - macroify aout_sysPlaySamples &/| aout_sysGetBufInfo ?
  *
  */
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
 #include <errno.h>                                                 /* ENOMEM */
 #include <fcntl.h>                                       /* open(), O_WRONLY */
 #include <sys/ioctl.h>                                            /* ioctl() */
@@ -42,6 +41,8 @@
 #include <stdio.h>                                           /* "intf_msg.h" */
 #include <stdlib.h>                            /* calloc(), malloc(), free() */
 
+#include <videolan/vlc.h>
+
 #ifdef SYS_BSD
 #include <machine/soundcard.h>       /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
                    SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
                    SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
 #endif
 
-#include "config.h"
-#include "common.h"                                     /* boolean_t, byte_t */
-#include "threads.h"
-#include "mtime.h"
-#include "plugins.h"
-
 #include "audio_output.h"                                   /* aout_thread_t */
 
-#include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
-#include "main.h"
-
 /*****************************************************************************
- * vout_dsp_t: dsp audio output method descriptor
+ * aout_sys_t: dsp audio output method descriptor
  *****************************************************************************
  * This structure is part of the audio output thread descriptor.
  * It describes the dsp specific properties of an audio device.
@@ -74,31 +66,83 @@ typedef struct aout_sys_s
 } aout_sys_t;
 
 /*****************************************************************************
- * aout_SysOpen: opens the audio device (the digital sound processor)
+ * Local prototypes.
+ *****************************************************************************/
+static int     aout_Probe       ( probedata_t *p_data );
+static int     aout_Open        ( aout_thread_t *p_aout );
+static int     aout_SetFormat   ( aout_thread_t *p_aout );
+static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
+static void    aout_Play        ( aout_thread_t *p_aout,
+                                  byte_t *buffer, int i_size );
+static void    aout_Close       ( aout_thread_t *p_aout );
+
+/*****************************************************************************
+ * Functions exported as capabilities. They are declared as static so that
+ * we don't pollute the namespace too much.
+ *****************************************************************************/
+void _M( aout_getfunctions )( function_list_t * p_function_list )
+{
+    p_function_list->pf_probe = aout_Probe;
+    p_function_list->functions.aout.pf_open = aout_Open;
+    p_function_list->functions.aout.pf_setformat = aout_SetFormat;
+    p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
+    p_function_list->functions.aout.pf_play = aout_Play;
+    p_function_list->functions.aout.pf_close = aout_Close;
+}
+
+/*****************************************************************************
+ * aout_Probe: probe the audio device and return a score
+ *****************************************************************************
+ * This function tries to open the DSP and returns a score to the plugin
+ * manager so that it can choose the most appropriate one.
+ *****************************************************************************/
+static int aout_Probe( probedata_t *p_data )
+{
+    char * psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
+    int i_fd = open( psz_device, O_WRONLY|O_NONBLOCK );
+
+    /* If we were unable to open the device, there is no way we can use
+     * the plugin. Return a score of 0. */
+    if( i_fd < 0 )
+    {
+        return( 0 );
+    }
+
+    /* Otherwise, there are good chances we can use this plugin, return 100. */
+    close( i_fd );
+
+    return( 100 );
+}
+
+/*****************************************************************************
+ * aout_Open: opens the audio device (the digital sound processor)
  *****************************************************************************
- * - This function opens the dsp as an usual non-blocking write-only file, and
- *   modifies the p_aout->p_sys->i_fd with the file's descriptor.
+ * This function opens the dsp as a usual non-blocking write-only file, and
+ * modifies the p_aout->i_fd with the file's descriptor.
  *****************************************************************************/
-int aout_SysOpen( aout_thread_t *p_aout )
+static int aout_Open( aout_thread_t *p_aout )
 {
     /* Allocate structure */
     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
     if( p_aout->p_sys == NULL )
     {
-        intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
+        intf_ErrMsg("aout error: %s", strerror(ENOMEM) );
         return( 1 );
     }
 
     /* Initialize some variables */
-    p_aout->i_format = AOUT_DEFAULT_FORMAT;
     p_aout->psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
-    p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
-    p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
+    p_aout->i_format   = AOUT_FORMAT_DEFAULT;
+    p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
+                                                  AOUT_STEREO_DEFAULT );
+    p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR,
+                                              AOUT_RATE_DEFAULT );
 
     /* Open the sound device */
-    if ( (p_aout->i_fd = open( p_aout->psz_device, O_WRONLY )) < 0 )
+    if( (p_aout->i_fd = open( p_aout->psz_device, O_WRONLY )) < 0 )
     {
-        intf_ErrMsg( "aout error: can't open audio device (%s)\n", p_aout->psz_device );
+        intf_ErrMsg( "aout error: can't open audio device (%s)",
+                     p_aout->psz_device );
         return( -1 );
     }
 
@@ -106,100 +150,82 @@ int aout_SysOpen( aout_thread_t *p_aout )
 }
 
 /*****************************************************************************
- * aout_SysReset: resets the dsp
- *****************************************************************************/
-int aout_SysReset( aout_thread_t *p_aout )
-{
-    if ( ioctl( p_aout->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
-    {
-        intf_ErrMsg( "aout error: can't reset audio device (%s)\n", p_aout->psz_device );
-    return( -1 );
-    }
-
-    return( 0 );
-}
-
-/*****************************************************************************
- * aout_SysSetFormat: sets the dsp output format
+ * aout_SetFormat: resets the dsp and sets its format
  *****************************************************************************
- * This functions tries to initialize the dsp 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.
+ * 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.
  *****************************************************************************/
-int aout_SysSetFormat( aout_thread_t *p_aout )
+static int aout_SetFormat( aout_thread_t *p_aout )
 {
     int i_format;
+    long l_rate;
+    boolean_t b_stereo = p_aout->b_stereo;
+
+    /* Reset the DSP device */
+    if( ioctl( p_aout->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
+    {
+        intf_ErrMsg( "aout error: can't reset audio device (%s)",
+                     p_aout->psz_device );
+        return( -1 );
+    }
 
+    /* Set the output format */
     i_format = p_aout->i_format;
-    if ( ioctl( p_aout->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
+    if( ioctl( p_aout->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
     {
-        intf_ErrMsg( "aout error: can't set audio output format (%i)\n", p_aout->i_format );
+        intf_ErrMsg( "aout error: can't set audio output format (%i)",
+                     p_aout->i_format );
         return( -1 );
     }
 
-    if ( i_format != p_aout->i_format )
+    if( i_format != p_aout->i_format )
     {
-        intf_DbgMsg( "aout debug: audio output format not supported (%i)\n", p_aout->i_format );
+        intf_WarnMsg( 2, "aout warning: audio output format not supported (%i)",
+                      p_aout->i_format );
         p_aout->i_format = i_format;
     }
 
-    return( 0 );
-}
-
-/*****************************************************************************
- * aout_SysSetChannels: sets the dsp's stereo or mono mode
- *****************************************************************************
- * This function acts just like the previous one...
- *****************************************************************************/
-int aout_SysSetChannels( aout_thread_t *p_aout )
-{
-    boolean_t b_stereo = p_aout->b_stereo;
-
-    if ( ioctl( p_aout->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
+    /* Set the number of channels */
+    if( ioctl( p_aout->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
     {
-        intf_ErrMsg( "aout error: can't set number of audio channels (%i)\n", p_aout->i_channels );
+        intf_ErrMsg( "aout error: can't set number of audio channels (%i)",
+                     p_aout->i_channels );
         return( -1 );
     }
 
-    if ( b_stereo != p_aout->b_stereo )
+    if( b_stereo != p_aout->b_stereo )
     {
-        intf_DbgMsg( "aout debug: number of audio channels not supported (%i)\n", p_aout->i_channels );
+        intf_WarnMsg( 2, "aout warning: number of audio channels not supported (%i)",
+                      p_aout->i_channels );
         p_aout->b_stereo = b_stereo;
         p_aout->i_channels = 1 + b_stereo;
     }
 
-    return( 0 );
-}
-
-/*****************************************************************************
- * aout_SysSetRate: sets the dsp's audio output rate
- *****************************************************************************
- * This function tries to initialize the dsp with the rate contained in the
- * dsp structure, but if the dsp doesn't support this value, the function uses
- * the value returned by ioctl...
- *****************************************************************************/
-int aout_SysSetRate( aout_thread_t *p_aout )
-{
-    long l_rate;
-
+    /* Set the output rate */
     l_rate = p_aout->l_rate;
-    if ( ioctl( p_aout->i_fd, SNDCTL_DSP_SPEED, &l_rate ) < 0 )
+    if( ioctl( p_aout->i_fd, SNDCTL_DSP_SPEED, &l_rate ) < 0 )
     {
-        intf_ErrMsg( "aout error: can't set audio output rate (%li)\n", p_aout->l_rate );
+        intf_ErrMsg( "aout error: can't set audio output rate (%li)",
+                     p_aout->l_rate );
         return( -1 );
     }
 
-    if ( l_rate != p_aout->l_rate )
+    if( l_rate != p_aout->l_rate )
     {
-        intf_DbgMsg( "aout debug: audio output rate not supported (%li)\n", p_aout->l_rate );
+        intf_WarnMsg( 1, "aout warning: audio output rate not supported (%li)",
+                      p_aout->l_rate );
         p_aout->l_rate = l_rate;
     }
 
+    p_aout->i_latency = 0;
+    
     return( 0 );
 }
 
 /*****************************************************************************
- * aout_SysGetBufInfo: buffer status query
+ * aout_GetBufInfo: buffer status query
  *****************************************************************************
  * This function fills in the audio_buf_info structure :
  * - int fragments : number of available fragments (partially usend ones not
@@ -209,7 +235,7 @@ int aout_SysSetRate( aout_thread_t *p_aout )
  * - int bytes : available space in bytes (includes partially used fragments)
  * Note! 'bytes' could be more than fragments*fragsize
  *****************************************************************************/
-long aout_SysGetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
+static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
 {
     ioctl( p_aout->i_fd, SNDCTL_DSP_GETOSPACE, &p_aout->p_sys->audio_buf );
 
@@ -220,11 +246,11 @@ long aout_SysGetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
 }
 
 /*****************************************************************************
- * aout_SysPlaySamples: plays a sound samples buffer
+ * aout_Play: plays a sound samples buffer
  *****************************************************************************
  * This function writes a buffer of i_length bytes in the dsp
  *****************************************************************************/
-void aout_SysPlaySamples( aout_thread_t *p_aout, byte_t *buffer, int i_size )
+static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
 {
     if( p_aout->b_active )
     {
@@ -233,9 +259,9 @@ void aout_SysPlaySamples( aout_thread_t *p_aout, byte_t *buffer, int i_size )
 }
 
 /*****************************************************************************
- * aout_SysClose: closes the dsp audio device
+ * aout_Close: closes the dsp audio device
  *****************************************************************************/
-void aout_SysClose( aout_thread_t *p_aout )
+static void aout_Close( aout_thread_t *p_aout )
 {
     close( p_aout->i_fd );
 }