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