[AD] auto detection of audio driver

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Attached is a patch that changes the behavior of auto detection for
audio drivers. Instead of trying to load exactly one driver known to
exist at compile time, it attempts to install each of them one at a
time. It installs the first one that succeeds.

Audio drivers typically fail quickly (immediately in most cases), so
this shouldn't introduce any undesirable delays. However, the patch as
implemented does remove the verbose error message that details the
name of the driver that failed. To me, that's a non-issue, as now the
auto detection tries them all, and if you specify a single one, you
know which one failed.

This new method would allow Linux systems to try PulseAudio first, and
if that fails, go on to OSS or Alsa.

--
Matthew Leverton
Index: audio.c
===================================================================
--- audio.c	(revision 12599)
+++ audio.c	(working copy)
@@ -41,6 +41,18 @@
    extern struct ALLEGRO_AUDIO_DRIVER _al_kcm_pulseaudio_driver;
 #endif
 
+/* The following list is used when autodetecting the audio driver to use.
+   The first one that returns success is used, hence order is important. */
+static const ALLEGRO_AUDIO_DRIVER_ENUM audio_drivers[] =
+{
+   ALLEGRO_AUDIO_DRIVER_DSOUND,
+   ALLEGRO_AUDIO_DRIVER_AQUEUE,
+   ALLEGRO_AUDIO_DRIVER_OPENAL,
+   ALLEGRO_AUDIO_DRIVER_PULSEAUDIO,
+   ALLEGRO_AUDIO_DRIVER_ALSA,
+   ALLEGRO_AUDIO_DRIVER_OSS
+};
+
 /* Channel configuration helpers */
 
 size_t al_get_channel_count(ALLEGRO_CHANNEL_CONF conf)
@@ -121,8 +133,6 @@
  */
 static bool do_install_audio(ALLEGRO_AUDIO_DRIVER_ENUM mode)
 {
-   bool retVal;
-
    /* check to see if a driver is already installed and running */
    if (_al_kcm_driver) {
       _al_set_error(ALLEGRO_GENERIC_ERROR, "A driver already running");
@@ -135,45 +145,18 @@
 
    switch (mode) {
       case ALLEGRO_AUDIO_DRIVER_AUTODETECT:
-#if defined(ALLEGRO_CFG_KCM_AQUEUE)
-         retVal = al_install_audio(ALLEGRO_AUDIO_DRIVER_AQUEUE);
-         if (retVal)
-            return retVal;
-#endif
-/* If a PA server is running, we should use it by default as it will
- * hijack ALSA and OSS and using those then just means extra lag.
- * 
- * FIXME: Detect if no PA server is running and in that case prefer
- * ALSA and OSS first.
- */
-#if defined(ALLEGRO_CFG_KCM_PULSEAUDIO)
-         retVal = al_install_audio(ALLEGRO_AUDIO_DRIVER_PULSEAUDIO);
-         if (retVal)
-            return retVal;
-#endif
-#if defined(ALLEGRO_CFG_KCM_ALSA)
-         retVal = al_install_audio(ALLEGRO_AUDIO_DRIVER_ALSA);
-         if (retVal)
-            return retVal;
-#endif
-#if defined(ALLEGRO_CFG_KCM_DSOUND)
-         retVal = al_install_audio(ALLEGRO_AUDIO_DRIVER_DSOUND);
-         if (retVal)
-            return retVal;
-#endif
-#if defined(ALLEGRO_CFG_KCM_OSS)
-         retVal = al_install_audio(ALLEGRO_AUDIO_DRIVER_OSS);
-         if (retVal)
-            return retVal;
-#endif
-#if defined(ALLEGRO_CFG_KCM_OPENAL)
-         retVal = al_install_audio(ALLEGRO_AUDIO_DRIVER_OPENAL);
-         if (retVal)
-            return retVal;
-#endif
-         _al_set_error(ALLEGRO_INVALID_PARAM, "No audio driver can be used.");
+      {
+         size_t i;
+         for (i=0; i<sizeof(audio_drivers) / sizeof(ALLEGRO_AUDIO_DRIVER_ENUM); ++i)
+         {
+            if (do_install_audio(audio_drivers[i]))
+               return true;
+         }
+
          _al_kcm_driver = NULL;
+
          return false;
+      }
 
       case ALLEGRO_AUDIO_DRIVER_AQUEUE:
          #if defined(ALLEGRO_CFG_KCM_AQUEUE)
@@ -182,11 +165,8 @@
                _al_kcm_driver = &_al_kcm_aqueue_driver;
                return true;
             }
-            return false;
-         #else
-            _al_set_error(ALLEGRO_INVALID_PARAM, "Audio Queue driver not available on this platform");
-            return false;
          #endif
+         return false;
 
       case ALLEGRO_AUDIO_DRIVER_OPENAL:
          #if defined(ALLEGRO_CFG_KCM_OPENAL)
@@ -195,11 +175,8 @@
                _al_kcm_driver = &_al_kcm_openal_driver;
                return true;
             }
-            return false;
-         #else
-            _al_set_error(ALLEGRO_INVALID_PARAM, "OpenAL not available on this platform");
-            return false;
          #endif
+         return false;
 
       case ALLEGRO_AUDIO_DRIVER_ALSA:
          #if defined(ALLEGRO_CFG_KCM_ALSA)
@@ -208,12 +185,10 @@
                _al_kcm_driver = &_al_kcm_alsa_driver;
                return true;
             }
-            return false;
-         #else
-            _al_set_error(ALLEGRO_INVALID_PARAM, "ALSA not available on this platform");
-            return false;
          #endif
+         return false;
 
+
       case ALLEGRO_AUDIO_DRIVER_OSS:
          #if defined(ALLEGRO_CFG_KCM_OSS)
             if (_al_kcm_oss_driver.open() == 0) {
@@ -221,11 +196,8 @@
                _al_kcm_driver = &_al_kcm_oss_driver;
                return true;
             }
-            return false;
-         #else
-            _al_set_error(ALLEGRO_INVALID_PARAM, "OSS not available on this platform");
-            return false;
          #endif
+         return false;
 
       case ALLEGRO_AUDIO_DRIVER_PULSEAUDIO:
          #if defined(ALLEGRO_CFG_KCM_PULSEAUDIO)
@@ -234,11 +206,8 @@
                _al_kcm_driver = &_al_kcm_pulseaudio_driver;
                return true;
             }
-            return false;
-         #else
-            _al_set_error(ALLEGRO_INVALID_PARAM, "PulseAudio not available on this platform");
-            return false;
          #endif
+         return false;
 
       case ALLEGRO_AUDIO_DRIVER_DSOUND:
          #if defined(ALLEGRO_CFG_KCM_DSOUND)
@@ -247,11 +216,8 @@
                _al_kcm_driver = &_al_kcm_dsound_driver;
                return true;
             }
-            return false;
-         #else
-            _al_set_error(ALLEGRO_INVALID_PARAM, "DirectSound not available on this platform");
-            return false;
          #endif
+         return false;
 
       default:
          _al_set_error(ALLEGRO_INVALID_PARAM, "Invalid audio driver");
@@ -267,6 +233,9 @@
    if (ret) {
       _al_kcm_init_destructors();
    }
+   else {
+      _al_set_error(ALLEGRO_INVALID_PARAM, "Unable to install audio driver.");
+   }
    return ret;
 }
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/