src/audio/nas/SDL_nasaudio.c
branchSDL-ryan-multiple-audio-device
changeset 3825 76c5a414b996
parent 3822 748707e2ddd1
child 3846 66fb40445587
     1.1 --- a/src/audio/nas/SDL_nasaudio.c	Sat Oct 07 07:23:52 2006 +0000
     1.2 +++ b/src/audio/nas/SDL_nasaudio.c	Sat Oct 07 07:25:30 2006 +0000
     1.3 @@ -32,6 +32,7 @@
     1.4  
     1.5  #include "SDL_timer.h"
     1.6  #include "SDL_audio.h"
     1.7 +#include "SDL_loadso.h"
     1.8  #include "../SDL_audiomem.h"
     1.9  #include "../SDL_audio_c.h"
    1.10  #include "SDL_nasaudio.h"
    1.11 @@ -41,17 +42,128 @@
    1.12  
    1.13  static struct SDL_PrivateAudioData *this2 = NULL;
    1.14  
    1.15 -/* !!! FIXME: dynamic loading? */
    1.16 +
    1.17 +static void (*NAS_AuCloseServer)(AuServer *);
    1.18 +static void (*NAS_AuNextEvent)(AuServer *, AuBool, AuEvent *);
    1.19 +static AuBool (*NAS_AuDispatchEvent)(AuServer *, AuEvent *);
    1.20 +static AuFlowID (*NAS_AuCreateFlow)(AuServer *, AuStatus *);
    1.21 +static void (*NAS_AuStartFlow)(AuServer *, AuFlowID, AuStatus *);
    1.22 +static void (*NAS_AuSetElements)
    1.23 +    (AuServer *, AuFlowID, AuBool, int, AuElement *, AuStatus *);
    1.24 +static void (*NAS_AuWriteElement)
    1.25 +    (AuServer *, AuFlowID, int, AuUint32, AuPointer, AuBool, AuStatus *);
    1.26 +static AuServer *(*NAS_AuOpenServer)
    1.27 +    (_AuConst char *, int, _AuConst char *, int, _AuConst char *, char **);
    1.28 +static AuEventHandlerRec *(*NAS_AuRegisterEventHandler)
    1.29 +    (AuServer *, AuMask, int, AuID, AuEventHandlerCallback, AuPointer);
    1.30 +
    1.31 +
    1.32 +#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
    1.33 +
    1.34 +static const char *nas_library = SDL_AUDIO_DRIVER_NAS_DYNAMIC;
    1.35 +static void *nas_handle = NULL;
    1.36 +
    1.37 +static int
    1.38 +load_nas_sym(const char *fn, void **addr)
    1.39 +{
    1.40 +    *addr = SDL_LoadFunction(nas_handle, fn);
    1.41 +    if (*addr == NULL) {
    1.42 +        return 0;
    1.43 +    }
    1.44 +    return 1;
    1.45 +}
    1.46 +
    1.47 +/* cast funcs to char* first, to please GCC's strict aliasing rules. */
    1.48 +#define SDL_NAS_SYM(x) \
    1.49 +    if (!load_nas_sym(#x, (void **) (char *) &NAS_##x)) return -1
    1.50 +#else
    1.51 +#define SDL_NAS_SYM(x) NAS_##x = x
    1.52 +#endif
    1.53 +
    1.54 +static int load_nas_syms(void)
    1.55 +{
    1.56 +    SDL_NAS_SYM(AuCloseServer);
    1.57 +    SDL_NAS_SYM(AuNextEvent);
    1.58 +    SDL_NAS_SYM(AuDispatchEvent);
    1.59 +    SDL_NAS_SYM(AuCreateFlow);
    1.60 +    SDL_NAS_SYM(AuStartFlow);
    1.61 +    SDL_NAS_SYM(AuSetElements);
    1.62 +    SDL_NAS_SYM(AuWriteElement);
    1.63 +    SDL_NAS_SYM(AuOpenServer);
    1.64 +    SDL_NAS_SYM(AuRegisterEventHandler);
    1.65 +    return 0;
    1.66 +}
    1.67 +#undef SDL_NAS_SYM
    1.68 +
    1.69 +#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
    1.70 +
    1.71 +static int library_load_count = 0;
    1.72 +
    1.73 +static void
    1.74 +UnloadNASLibrary(void)
    1.75 +{
    1.76 +    if ((nas_handle != NULL) && (--library_load_count == 0)) {
    1.77 +        SDL_UnloadObject(nas_handle);
    1.78 +        nas_handle = NULL;
    1.79 +    }
    1.80 +}
    1.81 +
    1.82 +static int
    1.83 +LoadNASLibrary(void)
    1.84 +{
    1.85 +    int retval = 0;
    1.86 +    if (library_load_count++ == 0) {
    1.87 +        nas_handle = SDL_LoadObject(nas_library);
    1.88 +        if (nas_handle == NULL) {
    1.89 +            /* Copy error string so we can use it in a new SDL_SetError(). */
    1.90 +            char *origerr = SDL_GetError();
    1.91 +            size_t len = SDL_strlen(origerr) + 1;
    1.92 +            char *err = (char *) alloca(len);
    1.93 +            SDL_strlcpy(err, origerr, len);
    1.94 +
    1.95 +            library_load_count--;
    1.96 +            retval = -1;
    1.97 +            SDL_SetError("NAS: SDL_LoadObject('%s') failed: %s\n",
    1.98 +                          nas_library, err);
    1.99 +        } else {
   1.100 +            retval = load_nas_syms();
   1.101 +            if (retval < 0) {
   1.102 +                UnloadNASLibrary();
   1.103 +            }
   1.104 +        }
   1.105 +    }
   1.106 +    return retval;
   1.107 +}
   1.108 +
   1.109 +#else
   1.110 +
   1.111 +static void
   1.112 +UnloadNASLibrary(void)
   1.113 +{
   1.114 +}
   1.115 +
   1.116 +static int
   1.117 +LoadNASLibrary(void)
   1.118 +{
   1.119 +    load_nas_syms();
   1.120 +    return 0;
   1.121 +}
   1.122 +
   1.123 +#endif /* SDL_AUDIO_DRIVER_NAS_DYNAMIC */
   1.124  
   1.125  static int
   1.126  NAS_Available(void)
   1.127  {
   1.128 -    AuServer *aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
   1.129 -    if (!aud)
   1.130 -        return 0;
   1.131 -
   1.132 -    AuCloseServer(aud);
   1.133 -    return 1;
   1.134 +    int available = 0;
   1.135 +    if (LoadNASLibrary() >= 0) {
   1.136 +        AuServer *aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
   1.137 +        if (aud != NULL) {
   1.138 +            available = 1;
   1.139 +            NAS_AuCloseServer(aud);
   1.140 +        }
   1.141 +        UnloadNASLibrary();
   1.142 +    }
   1.143 +    return available;
   1.144  }
   1.145  
   1.146  /* This function waits until it is possible to write a full sound buffer */
   1.147 @@ -60,8 +172,8 @@
   1.148  {
   1.149      while (this->hidden->buf_free < this->hidden->mixlen) {
   1.150          AuEvent ev;
   1.151 -        AuNextEvent(this->hidden->aud, AuTrue, &ev);
   1.152 -        AuDispatchEvent(this->hidden->aud, &ev);
   1.153 +        NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
   1.154 +        NAS_AuDispatchEvent(this->hidden->aud, &ev);
   1.155      }
   1.156  }
   1.157  
   1.158 @@ -75,13 +187,13 @@
   1.159           *  of the buffer is free now than what we think.
   1.160           */
   1.161          AuEvent ev;
   1.162 -        AuNextEvent(this->hidden->aud, AuTrue, &ev);
   1.163 -        AuDispatchEvent(this->hidden->aud, &ev);
   1.164 +        NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
   1.165 +        NAS_AuDispatchEvent(this->hidden->aud, &ev);
   1.166      }
   1.167      this->hidden->buf_free -= this->hidden->mixlen;
   1.168  
   1.169      /* Write the audio data */
   1.170 -    AuWriteElement(this->hidden->aud, this->hidden->flow, 0,
   1.171 +    NAS_AuWriteElement(this->hidden->aud, this->hidden->flow, 0,
   1.172                     this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
   1.173  
   1.174      this->hidden->written += this->hidden->mixlen;
   1.175 @@ -106,11 +218,12 @@
   1.176              this->hidden->mixbuf = NULL;
   1.177          }
   1.178          if (this->hidden->aud) {
   1.179 -            AuCloseServer(this->hidden->aud);
   1.180 +            NAS_AuCloseServer(this->hidden->aud);
   1.181              this->hidden->aud = 0;
   1.182          }
   1.183          SDL_free(this->hidden);
   1.184          this2 = this->hidden = NULL;
   1.185 +        UnloadNASLibrary();
   1.186      }
   1.187  }
   1.188  
   1.189 @@ -175,6 +288,7 @@
   1.190  static AuDeviceID
   1.191  find_device(_THIS, int nch)
   1.192  {
   1.193 +    /* These "Au" things are all macros, not functions... */
   1.194      int i;
   1.195      for (i = 0; i < AuServerNumDevices(this->hidden->aud); i++) {
   1.196          if ((AuDeviceKind(AuServerDevice(this->hidden->aud, i)) ==
   1.197 @@ -202,6 +316,11 @@
   1.198      }
   1.199      SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   1.200  
   1.201 +    if (LoadNASLibrary() < 0) {
   1.202 +        NAS_CloseDevice(this);
   1.203 +        return 0;
   1.204 +    }
   1.205 +
   1.206      /* Try for a closest match on audio format */
   1.207      format = 0;
   1.208      for (test_format = SDL_FirstAudioFormat(this->spec.format);
   1.209 @@ -218,7 +337,7 @@
   1.210      }
   1.211      this->spec.format = test_format;
   1.212  
   1.213 -    this->hidden->aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
   1.214 +    this->hidden->aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
   1.215      if (this->hidden->aud == 0) {
   1.216          NAS_CloseDevice(this);
   1.217          SDL_SetError("NAS: Couldn't open connection to NAS server");
   1.218 @@ -227,7 +346,7 @@
   1.219  
   1.220      this->hidden->dev = find_device(this, this->spec.channels);
   1.221      if ((this->hidden->dev == AuNone)
   1.222 -        || (!(this->hidden->flow = AuCreateFlow(this->hidden->aud, NULL)))) {
   1.223 +        || (!(this->hidden->flow = NAS_AuCreateFlow(this->hidden->aud, 0)))) {
   1.224          NAS_CloseDevice(this);
   1.225          SDL_SetError("NAS: Couldn't find a fitting device on NAS server");
   1.226          return 0;
   1.227 @@ -249,12 +368,13 @@
   1.228                                AuTrue, buffer_size, buffer_size / 4, 0, NULL);
   1.229      AuMakeElementExportDevice(elms + 1, 0, this->hidden->dev, this->spec.freq,
   1.230                                AuUnlimitedSamples, 0, NULL);
   1.231 -    AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL);
   1.232 -    AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0,
   1.233 -                           this->hidden->flow, event_handler,
   1.234 -                           (AuPointer) NULL);
   1.235 +    NAS_AuSetElements(this->hidden->aud, this->hidden->flow,
   1.236 +                      AuTrue, 2, elms, NULL);
   1.237 +    NAS_AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0,
   1.238 +                               this->hidden->flow, event_handler,
   1.239 +                               (AuPointer) NULL);
   1.240  
   1.241 -    AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
   1.242 +    NAS_AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
   1.243  
   1.244      /* Allocate mixing buffer */
   1.245      this->hidden->mixlen = this->spec.size;
   1.246 @@ -262,7 +382,7 @@
   1.247      if (this->hidden->mixbuf == NULL) {
   1.248          NAS_CloseDevice(this);
   1.249          SDL_OutOfMemory();
   1.250 -        return (-1);
   1.251 +        return 0;
   1.252      }
   1.253      SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
   1.254