src/audio/nas/SDL_nasaudio.c
author Ryan C. Gordon
Tue, 17 Oct 2006 09:09:21 +0000
branchSDL-ryan-multiple-audio-device
changeset 3846 66fb40445587
parent 3825 76c5a414b996
permissions -rw-r--r--
Removed distinction between "available" and "init" in audio backends, since
both had to be checked for success as a pair at the higher level and several
of the Available methods were just always-succeed placeholders anyhow. Now
the availability check is done in the init code, and the higher level tries
all possible drivers until one manages to initialize successfully.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 
    22     This driver was written by:
    23     Erik Inge Bolsų
    24     knan@mo.himolde.no
    25 */
    26 #include "SDL_config.h"
    27 
    28 /* Allow access to a raw mixing buffer */
    29 
    30 #include <signal.h>
    31 #include <unistd.h>
    32 
    33 #include "SDL_timer.h"
    34 #include "SDL_audio.h"
    35 #include "SDL_loadso.h"
    36 #include "../SDL_audiomem.h"
    37 #include "../SDL_audio_c.h"
    38 #include "SDL_nasaudio.h"
    39 
    40 /* The tag name used by nas audio */
    41 #define NAS_DRIVER_NAME         "nas"
    42 
    43 static struct SDL_PrivateAudioData *this2 = NULL;
    44 
    45 
    46 static void (*NAS_AuCloseServer)(AuServer *);
    47 static void (*NAS_AuNextEvent)(AuServer *, AuBool, AuEvent *);
    48 static AuBool (*NAS_AuDispatchEvent)(AuServer *, AuEvent *);
    49 static AuFlowID (*NAS_AuCreateFlow)(AuServer *, AuStatus *);
    50 static void (*NAS_AuStartFlow)(AuServer *, AuFlowID, AuStatus *);
    51 static void (*NAS_AuSetElements)
    52     (AuServer *, AuFlowID, AuBool, int, AuElement *, AuStatus *);
    53 static void (*NAS_AuWriteElement)
    54     (AuServer *, AuFlowID, int, AuUint32, AuPointer, AuBool, AuStatus *);
    55 static AuServer *(*NAS_AuOpenServer)
    56     (_AuConst char *, int, _AuConst char *, int, _AuConst char *, char **);
    57 static AuEventHandlerRec *(*NAS_AuRegisterEventHandler)
    58     (AuServer *, AuMask, int, AuID, AuEventHandlerCallback, AuPointer);
    59 
    60 
    61 #ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
    62 
    63 static const char *nas_library = SDL_AUDIO_DRIVER_NAS_DYNAMIC;
    64 static void *nas_handle = NULL;
    65 
    66 static int
    67 load_nas_sym(const char *fn, void **addr)
    68 {
    69     *addr = SDL_LoadFunction(nas_handle, fn);
    70     if (*addr == NULL) {
    71         return 0;
    72     }
    73     return 1;
    74 }
    75 
    76 /* cast funcs to char* first, to please GCC's strict aliasing rules. */
    77 #define SDL_NAS_SYM(x) \
    78     if (!load_nas_sym(#x, (void **) (char *) &NAS_##x)) return -1
    79 #else
    80 #define SDL_NAS_SYM(x) NAS_##x = x
    81 #endif
    82 
    83 static int load_nas_syms(void)
    84 {
    85     SDL_NAS_SYM(AuCloseServer);
    86     SDL_NAS_SYM(AuNextEvent);
    87     SDL_NAS_SYM(AuDispatchEvent);
    88     SDL_NAS_SYM(AuCreateFlow);
    89     SDL_NAS_SYM(AuStartFlow);
    90     SDL_NAS_SYM(AuSetElements);
    91     SDL_NAS_SYM(AuWriteElement);
    92     SDL_NAS_SYM(AuOpenServer);
    93     SDL_NAS_SYM(AuRegisterEventHandler);
    94     return 0;
    95 }
    96 #undef SDL_NAS_SYM
    97 
    98 #ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
    99 
   100 static void
   101 UnloadNASLibrary(void)
   102 {
   103     if (nas_handle != NULL) {
   104         SDL_UnloadObject(nas_handle);
   105         nas_handle = NULL;
   106     }
   107 }
   108 
   109 static int
   110 LoadNASLibrary(void)
   111 {
   112     int retval = 0;
   113     if (nas_handle == NULL) {
   114         nas_handle = SDL_LoadObject(nas_library);
   115         if (nas_handle == NULL) {
   116             /* Copy error string so we can use it in a new SDL_SetError(). */
   117             char *origerr = SDL_GetError();
   118             size_t len = SDL_strlen(origerr) + 1;
   119             char *err = (char *) alloca(len);
   120             SDL_strlcpy(err, origerr, len);
   121             retval = -1;
   122             SDL_SetError("NAS: SDL_LoadObject('%s') failed: %s\n",
   123                           nas_library, err);
   124         } else {
   125             retval = load_nas_syms();
   126             if (retval < 0) {
   127                 UnloadNASLibrary();
   128             }
   129         }
   130     }
   131     return retval;
   132 }
   133 
   134 #else
   135 
   136 static void
   137 UnloadNASLibrary(void)
   138 {
   139 }
   140 
   141 static int
   142 LoadNASLibrary(void)
   143 {
   144     load_nas_syms();
   145     return 0;
   146 }
   147 
   148 #endif /* SDL_AUDIO_DRIVER_NAS_DYNAMIC */
   149 
   150 /* This function waits until it is possible to write a full sound buffer */
   151 static void
   152 NAS_WaitDevice(_THIS)
   153 {
   154     while (this->hidden->buf_free < this->hidden->mixlen) {
   155         AuEvent ev;
   156         NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
   157         NAS_AuDispatchEvent(this->hidden->aud, &ev);
   158     }
   159 }
   160 
   161 static void
   162 NAS_PlayDevice(_THIS)
   163 {
   164     while (this->hidden->mixlen > this->hidden->buf_free) {
   165         /*
   166          * We think the buffer is full? Yikes! Ask the server for events,
   167          *  in the hope that some of them is LowWater events telling us more
   168          *  of the buffer is free now than what we think.
   169          */
   170         AuEvent ev;
   171         NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
   172         NAS_AuDispatchEvent(this->hidden->aud, &ev);
   173     }
   174     this->hidden->buf_free -= this->hidden->mixlen;
   175 
   176     /* Write the audio data */
   177     NAS_AuWriteElement(this->hidden->aud, this->hidden->flow, 0,
   178                    this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
   179 
   180     this->hidden->written += this->hidden->mixlen;
   181 
   182 #ifdef DEBUG_AUDIO
   183     fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
   184 #endif
   185 }
   186 
   187 static Uint8 *
   188 NAS_GetDeviceBuf(_THIS)
   189 {
   190     return (this->hidden->mixbuf);
   191 }
   192 
   193 static void
   194 NAS_CloseDevice(_THIS)
   195 {
   196     if (this->hidden != NULL) {
   197         if (this->hidden->mixbuf != NULL) {
   198             SDL_FreeAudioMem(this->hidden->mixbuf);
   199             this->hidden->mixbuf = NULL;
   200         }
   201         if (this->hidden->aud) {
   202             NAS_AuCloseServer(this->hidden->aud);
   203             this->hidden->aud = 0;
   204         }
   205         SDL_free(this->hidden);
   206         this2 = this->hidden = NULL;
   207     }
   208 }
   209 
   210 static unsigned char
   211 sdlformat_to_auformat(unsigned int fmt)
   212 {
   213     switch (fmt) {
   214     case AUDIO_U8:
   215         return AuFormatLinearUnsigned8;
   216     case AUDIO_S8:
   217         return AuFormatLinearSigned8;
   218     case AUDIO_U16LSB:
   219         return AuFormatLinearUnsigned16LSB;
   220     case AUDIO_U16MSB:
   221         return AuFormatLinearUnsigned16MSB;
   222     case AUDIO_S16LSB:
   223         return AuFormatLinearSigned16LSB;
   224     case AUDIO_S16MSB:
   225         return AuFormatLinearSigned16MSB;
   226     }
   227     return AuNone;
   228 }
   229 
   230 static AuBool
   231 event_handler(AuServer * aud, AuEvent * ev, AuEventHandlerRec * hnd)
   232 {
   233     switch (ev->type) {
   234     case AuEventTypeElementNotify:
   235         {
   236             AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
   237 
   238             switch (event->kind) {
   239             case AuElementNotifyKindLowWater:
   240                 if (this2->buf_free >= 0) {
   241                     this2->really += event->num_bytes;
   242                     gettimeofday(&this2->last_tv, 0);
   243                     this2->buf_free += event->num_bytes;
   244                 } else {
   245                     this2->buf_free = event->num_bytes;
   246                 }
   247                 break;
   248             case AuElementNotifyKindState:
   249                 switch (event->cur_state) {
   250                 case AuStatePause:
   251                     if (event->reason != AuReasonUser) {
   252                         if (this2->buf_free >= 0) {
   253                             this2->really += event->num_bytes;
   254                             gettimeofday(&this2->last_tv, 0);
   255                             this2->buf_free += event->num_bytes;
   256                         } else {
   257                             this2->buf_free = event->num_bytes;
   258                         }
   259                     }
   260                     break;
   261                 }
   262             }
   263         }
   264     }
   265     return AuTrue;
   266 }
   267 
   268 static AuDeviceID
   269 find_device(_THIS, int nch)
   270 {
   271     /* These "Au" things are all macros, not functions... */
   272     int i;
   273     for (i = 0; i < AuServerNumDevices(this->hidden->aud); i++) {
   274         if ((AuDeviceKind(AuServerDevice(this->hidden->aud, i)) ==
   275              AuComponentKindPhysicalOutput) &&
   276             AuDeviceNumTracks(AuServerDevice(this->hidden->aud, i)) == nch) {
   277             return AuDeviceIdentifier(AuServerDevice(this->hidden->aud, i));
   278         }
   279     }
   280     return AuNone;
   281 }
   282 
   283 static int
   284 NAS_OpenDevice(_THIS, const char *devname, int iscapture)
   285 {
   286     AuElement elms[3];
   287     int buffer_size;
   288     SDL_AudioFormat test_format, format;
   289 
   290     /* Initialize all variables that we clean on shutdown */
   291     this->hidden = (struct SDL_PrivateAudioData *)
   292                         SDL_malloc((sizeof *this->hidden));
   293     if (this->hidden == NULL) {
   294         SDL_OutOfMemory();
   295         return 0;
   296     }
   297     SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   298 
   299     /* Try for a closest match on audio format */
   300     format = 0;
   301     for (test_format = SDL_FirstAudioFormat(this->spec.format);
   302          !format && test_format;) {
   303         format = sdlformat_to_auformat(test_format);
   304         if (format == AuNone) {
   305             test_format = SDL_NextAudioFormat();
   306         }
   307     }
   308     if (format == 0) {
   309         NAS_CloseDevice(this);
   310         SDL_SetError("NAS: Couldn't find any hardware audio formats");
   311         return 0;
   312     }
   313     this->spec.format = test_format;
   314 
   315     this->hidden->aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
   316     if (this->hidden->aud == 0) {
   317         NAS_CloseDevice(this);
   318         SDL_SetError("NAS: Couldn't open connection to NAS server");
   319         return 0;
   320     }
   321 
   322     this->hidden->dev = find_device(this, this->spec.channels);
   323     if ((this->hidden->dev == AuNone)
   324         || (!(this->hidden->flow = NAS_AuCreateFlow(this->hidden->aud, 0)))) {
   325         NAS_CloseDevice(this);
   326         SDL_SetError("NAS: Couldn't find a fitting device on NAS server");
   327         return 0;
   328     }
   329 
   330     buffer_size = this->spec.freq;
   331     if (buffer_size < 4096)
   332         buffer_size = 4096;
   333 
   334     if (buffer_size > 32768)
   335         buffer_size = 32768;    /* So that the buffer won't get unmanageably big. */
   336 
   337     /* Calculate the final parameters for this audio specification */
   338     SDL_CalculateAudioSpec(&this->spec);
   339 
   340     this2 = this->hidden;
   341 
   342     AuMakeElementImportClient(elms,this->spec.freq,format,this->spec.channels,
   343                               AuTrue, buffer_size, buffer_size / 4, 0, NULL);
   344     AuMakeElementExportDevice(elms + 1, 0, this->hidden->dev, this->spec.freq,
   345                               AuUnlimitedSamples, 0, NULL);
   346     NAS_AuSetElements(this->hidden->aud, this->hidden->flow,
   347                       AuTrue, 2, elms, NULL);
   348     NAS_AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0,
   349                                this->hidden->flow, event_handler,
   350                                (AuPointer) NULL);
   351 
   352     NAS_AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
   353 
   354     /* Allocate mixing buffer */
   355     this->hidden->mixlen = this->spec.size;
   356     this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   357     if (this->hidden->mixbuf == NULL) {
   358         NAS_CloseDevice(this);
   359         SDL_OutOfMemory();
   360         return 0;
   361     }
   362     SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
   363 
   364     /* We're ready to rock and roll. :-) */
   365     return 1;
   366 }
   367 
   368 static void
   369 NAS_Deinitialize(void)
   370 {
   371     UnloadNASLibrary();
   372 }
   373 
   374 static int
   375 NAS_Init(SDL_AudioDriverImpl *impl)
   376 {
   377     if (LoadNASLibrary() < 0) {
   378         return 0;
   379     } else {
   380         AuServer *aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
   381         if (aud == NULL) {
   382             SDL_SetError("NAS: AuOpenServer() failed (no audio server?)");
   383             return 0;
   384         }
   385         NAS_AuCloseServer(aud);
   386     }
   387 
   388     /* Set the function pointers */
   389     impl->OpenDevice = NAS_OpenDevice;
   390     impl->PlayDevice = NAS_PlayDevice;
   391     impl->WaitDevice = NAS_WaitDevice;
   392     impl->GetDeviceBuf = NAS_GetDeviceBuf;
   393     impl->CloseDevice = NAS_CloseDevice;
   394     impl->Deinitialize = NAS_Deinitialize;
   395     impl->OnlyHasDefaultOutputDevice = 1;  /* !!! FIXME: is this true? */
   396 
   397     return 1;
   398 }
   399 
   400 AudioBootStrap NAS_bootstrap = {
   401     NAS_DRIVER_NAME, "Network Audio System", NAS_Init, 0
   402 };
   403 
   404 /* vi: set ts=4 sw=4 expandtab: */