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.
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 void
icculus@3825
   101
UnloadNASLibrary(void)
icculus@3825
   102
{
icculus@3846
   103
    if (nas_handle != NULL) {
icculus@3825
   104
        SDL_UnloadObject(nas_handle);
icculus@3825
   105
        nas_handle = NULL;
icculus@3825
   106
    }
icculus@3825
   107
}
icculus@3825
   108
icculus@3825
   109
static int
icculus@3825
   110
LoadNASLibrary(void)
icculus@3825
   111
{
icculus@3825
   112
    int retval = 0;
icculus@3846
   113
    if (nas_handle == NULL) {
icculus@3825
   114
        nas_handle = SDL_LoadObject(nas_library);
icculus@3825
   115
        if (nas_handle == NULL) {
icculus@3825
   116
            /* Copy error string so we can use it in a new SDL_SetError(). */
icculus@3825
   117
            char *origerr = SDL_GetError();
icculus@3825
   118
            size_t len = SDL_strlen(origerr) + 1;
icculus@3825
   119
            char *err = (char *) alloca(len);
icculus@3825
   120
            SDL_strlcpy(err, origerr, len);
icculus@3825
   121
            retval = -1;
icculus@3825
   122
            SDL_SetError("NAS: SDL_LoadObject('%s') failed: %s\n",
icculus@3825
   123
                          nas_library, err);
icculus@3825
   124
        } else {
icculus@3825
   125
            retval = load_nas_syms();
icculus@3825
   126
            if (retval < 0) {
icculus@3825
   127
                UnloadNASLibrary();
icculus@3825
   128
            }
icculus@3825
   129
        }
icculus@3825
   130
    }
icculus@3825
   131
    return retval;
icculus@3825
   132
}
icculus@3825
   133
icculus@3825
   134
#else
icculus@3825
   135
icculus@3825
   136
static void
icculus@3825
   137
UnloadNASLibrary(void)
icculus@3825
   138
{
icculus@3825
   139
}
icculus@3825
   140
icculus@3825
   141
static int
icculus@3825
   142
LoadNASLibrary(void)
icculus@3825
   143
{
icculus@3825
   144
    load_nas_syms();
icculus@3825
   145
    return 0;
icculus@3825
   146
}
icculus@3825
   147
icculus@3825
   148
#endif /* SDL_AUDIO_DRIVER_NAS_DYNAMIC */
slouken@0
   149
slouken@0
   150
/* This function waits until it is possible to write a full sound buffer */
slouken@1895
   151
static void
icculus@3821
   152
NAS_WaitDevice(_THIS)
slouken@0
   153
{
slouken@1895
   154
    while (this->hidden->buf_free < this->hidden->mixlen) {
slouken@1895
   155
        AuEvent ev;
icculus@3825
   156
        NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
icculus@3825
   157
        NAS_AuDispatchEvent(this->hidden->aud, &ev);
slouken@1895
   158
    }
slouken@0
   159
}
slouken@0
   160
slouken@1895
   161
static void
icculus@3821
   162
NAS_PlayDevice(_THIS)
slouken@0
   163
{
icculus@3821
   164
    while (this->hidden->mixlen > this->hidden->buf_free) {
icculus@3821
   165
        /*
icculus@3821
   166
         * We think the buffer is full? Yikes! Ask the server for events,
icculus@3821
   167
         *  in the hope that some of them is LowWater events telling us more
icculus@3821
   168
         *  of the buffer is free now than what we think.
icculus@3821
   169
         */
slouken@1895
   170
        AuEvent ev;
icculus@3825
   171
        NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
icculus@3825
   172
        NAS_AuDispatchEvent(this->hidden->aud, &ev);
slouken@1895
   173
    }
slouken@1895
   174
    this->hidden->buf_free -= this->hidden->mixlen;
slouken@0
   175
slouken@1895
   176
    /* Write the audio data */
icculus@3825
   177
    NAS_AuWriteElement(this->hidden->aud, this->hidden->flow, 0,
slouken@1895
   178
                   this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
slouken@0
   179
slouken@1895
   180
    this->hidden->written += this->hidden->mixlen;
slouken@1895
   181
slouken@0
   182
#ifdef DEBUG_AUDIO
slouken@1895
   183
    fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
slouken@0
   184
#endif
slouken@0
   185
}
slouken@0
   186
slouken@1895
   187
static Uint8 *
icculus@3821
   188
NAS_GetDeviceBuf(_THIS)
slouken@0
   189
{
slouken@1895
   190
    return (this->hidden->mixbuf);
slouken@0
   191
}
slouken@0
   192
slouken@1895
   193
static void
icculus@3821
   194
NAS_CloseDevice(_THIS)
slouken@0
   195
{
icculus@3821
   196
    if (this->hidden != NULL) {
icculus@3821
   197
        if (this->hidden->mixbuf != NULL) {
icculus@3821
   198
            SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@3821
   199
            this->hidden->mixbuf = NULL;
icculus@3821
   200
        }
icculus@3821
   201
        if (this->hidden->aud) {
icculus@3825
   202
            NAS_AuCloseServer(this->hidden->aud);
icculus@3821
   203
            this->hidden->aud = 0;
icculus@3821
   204
        }
icculus@3821
   205
        SDL_free(this->hidden);
icculus@3822
   206
        this2 = this->hidden = NULL;
slouken@1895
   207
    }
slouken@0
   208
}
slouken@0
   209
slouken@1895
   210
static unsigned char
slouken@1895
   211
sdlformat_to_auformat(unsigned int fmt)
slouken@0
   212
{
slouken@1895
   213
    switch (fmt) {
slouken@0
   214
    case AUDIO_U8:
slouken@1895
   215
        return AuFormatLinearUnsigned8;
slouken@0
   216
    case AUDIO_S8:
slouken@1895
   217
        return AuFormatLinearSigned8;
slouken@0
   218
    case AUDIO_U16LSB:
slouken@1895
   219
        return AuFormatLinearUnsigned16LSB;
slouken@0
   220
    case AUDIO_U16MSB:
slouken@1895
   221
        return AuFormatLinearUnsigned16MSB;
slouken@0
   222
    case AUDIO_S16LSB:
slouken@1895
   223
        return AuFormatLinearSigned16LSB;
slouken@0
   224
    case AUDIO_S16MSB:
slouken@1895
   225
        return AuFormatLinearSigned16MSB;
slouken@0
   226
    }
slouken@1895
   227
    return AuNone;
slouken@0
   228
}
slouken@0
   229
slouken@0
   230
static AuBool
slouken@1895
   231
event_handler(AuServer * aud, AuEvent * ev, AuEventHandlerRec * hnd)
slouken@0
   232
{
slouken@1895
   233
    switch (ev->type) {
slouken@1895
   234
    case AuEventTypeElementNotify:
slouken@1895
   235
        {
slouken@1895
   236
            AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
slouken@0
   237
slouken@1895
   238
            switch (event->kind) {
slouken@1895
   239
            case AuElementNotifyKindLowWater:
slouken@1895
   240
                if (this2->buf_free >= 0) {
slouken@1895
   241
                    this2->really += event->num_bytes;
slouken@1895
   242
                    gettimeofday(&this2->last_tv, 0);
slouken@1895
   243
                    this2->buf_free += event->num_bytes;
slouken@1895
   244
                } else {
slouken@1895
   245
                    this2->buf_free = event->num_bytes;
slouken@1895
   246
                }
slouken@1895
   247
                break;
slouken@1895
   248
            case AuElementNotifyKindState:
slouken@1895
   249
                switch (event->cur_state) {
slouken@1895
   250
                case AuStatePause:
slouken@1895
   251
                    if (event->reason != AuReasonUser) {
slouken@1895
   252
                        if (this2->buf_free >= 0) {
slouken@1895
   253
                            this2->really += event->num_bytes;
slouken@1895
   254
                            gettimeofday(&this2->last_tv, 0);
slouken@1895
   255
                            this2->buf_free += event->num_bytes;
slouken@1895
   256
                        } else {
slouken@1895
   257
                            this2->buf_free = event->num_bytes;
slouken@1895
   258
                        }
slouken@1895
   259
                    }
slouken@1895
   260
                    break;
slouken@1895
   261
                }
slouken@1895
   262
            }
slouken@1895
   263
        }
slouken@1895
   264
    }
slouken@1895
   265
    return AuTrue;
slouken@0
   266
}
slouken@0
   267
slouken@0
   268
static AuDeviceID
slouken@0
   269
find_device(_THIS, int nch)
slouken@0
   270
{
icculus@3825
   271
    /* These "Au" things are all macros, not functions... */
slouken@1895
   272
    int i;
slouken@1895
   273
    for (i = 0; i < AuServerNumDevices(this->hidden->aud); i++) {
slouken@1895
   274
        if ((AuDeviceKind(AuServerDevice(this->hidden->aud, i)) ==
slouken@1895
   275
             AuComponentKindPhysicalOutput) &&
slouken@1895
   276
            AuDeviceNumTracks(AuServerDevice(this->hidden->aud, i)) == nch) {
slouken@1895
   277
            return AuDeviceIdentifier(AuServerDevice(this->hidden->aud, i));
slouken@1895
   278
        }
slouken@1895
   279
    }
slouken@1895
   280
    return AuNone;
slouken@0
   281
}
slouken@0
   282
slouken@1895
   283
static int
icculus@3821
   284
NAS_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@0
   285
{
slouken@1895
   286
    AuElement elms[3];
slouken@1895
   287
    int buffer_size;
icculus@1982
   288
    SDL_AudioFormat test_format, format;
slouken@1895
   289
icculus@3821
   290
    /* Initialize all variables that we clean on shutdown */
icculus@3821
   291
    this->hidden = (struct SDL_PrivateAudioData *)
icculus@3821
   292
                        SDL_malloc((sizeof *this->hidden));
icculus@3821
   293
    if (this->hidden == NULL) {
icculus@3821
   294
        SDL_OutOfMemory();
icculus@3821
   295
        return 0;
icculus@3821
   296
    }
icculus@3821
   297
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   298
slouken@1895
   299
    /* Try for a closest match on audio format */
slouken@1895
   300
    format = 0;
icculus@3821
   301
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@1895
   302
         !format && test_format;) {
slouken@1895
   303
        format = sdlformat_to_auformat(test_format);
slouken@1895
   304
        if (format == AuNone) {
slouken@1895
   305
            test_format = SDL_NextAudioFormat();
slouken@1895
   306
        }
slouken@1895
   307
    }
slouken@1895
   308
    if (format == 0) {
icculus@3821
   309
        NAS_CloseDevice(this);
icculus@3821
   310
        SDL_SetError("NAS: Couldn't find any hardware audio formats");
icculus@3821
   311
        return 0;
slouken@1895
   312
    }
icculus@3821
   313
    this->spec.format = test_format;
slouken@0
   314
icculus@3825
   315
    this->hidden->aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
slouken@1895
   316
    if (this->hidden->aud == 0) {
icculus@3821
   317
        NAS_CloseDevice(this);
icculus@3821
   318
        SDL_SetError("NAS: Couldn't open connection to NAS server");
icculus@3821
   319
        return 0;
slouken@1895
   320
    }
slouken@1895
   321
icculus@3821
   322
    this->hidden->dev = find_device(this, this->spec.channels);
slouken@1895
   323
    if ((this->hidden->dev == AuNone)
icculus@3825
   324
        || (!(this->hidden->flow = NAS_AuCreateFlow(this->hidden->aud, 0)))) {
icculus@3821
   325
        NAS_CloseDevice(this);
icculus@3821
   326
        SDL_SetError("NAS: Couldn't find a fitting device on NAS server");
icculus@3821
   327
        return 0;
slouken@1895
   328
    }
slouken@0
   329
icculus@3821
   330
    buffer_size = this->spec.freq;
slouken@1895
   331
    if (buffer_size < 4096)
slouken@1895
   332
        buffer_size = 4096;
slouken@0
   333
slouken@1895
   334
    if (buffer_size > 32768)
slouken@1895
   335
        buffer_size = 32768;    /* So that the buffer won't get unmanageably big. */
slouken@0
   336
slouken@1895
   337
    /* Calculate the final parameters for this audio specification */
icculus@3821
   338
    SDL_CalculateAudioSpec(&this->spec);
slouken@1895
   339
slouken@1895
   340
    this2 = this->hidden;
slouken@0
   341
icculus@3821
   342
    AuMakeElementImportClient(elms,this->spec.freq,format,this->spec.channels,
slouken@1895
   343
                              AuTrue, buffer_size, buffer_size / 4, 0, NULL);
icculus@3821
   344
    AuMakeElementExportDevice(elms + 1, 0, this->hidden->dev, this->spec.freq,
slouken@1895
   345
                              AuUnlimitedSamples, 0, NULL);
icculus@3825
   346
    NAS_AuSetElements(this->hidden->aud, this->hidden->flow,
icculus@3825
   347
                      AuTrue, 2, elms, NULL);
icculus@3825
   348
    NAS_AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0,
icculus@3825
   349
                               this->hidden->flow, event_handler,
icculus@3825
   350
                               (AuPointer) NULL);
slouken@0
   351
icculus@3825
   352
    NAS_AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
slouken@0
   353
slouken@1895
   354
    /* Allocate mixing buffer */
icculus@3821
   355
    this->hidden->mixlen = this->spec.size;
slouken@1895
   356
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
slouken@1895
   357
    if (this->hidden->mixbuf == NULL) {
icculus@3821
   358
        NAS_CloseDevice(this);
icculus@3821
   359
        SDL_OutOfMemory();
icculus@3825
   360
        return 0;
slouken@1895
   361
    }
icculus@3821
   362
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@0
   363
slouken@1895
   364
    /* We're ready to rock and roll. :-) */
icculus@3821
   365
    return 1;
slouken@0
   366
}
slouken@1895
   367
icculus@3846
   368
static void
icculus@3846
   369
NAS_Deinitialize(void)
icculus@3846
   370
{
icculus@3846
   371
    UnloadNASLibrary();
icculus@3846
   372
}
icculus@3846
   373
icculus@3821
   374
static int
icculus@3821
   375
NAS_Init(SDL_AudioDriverImpl *impl)
icculus@3821
   376
{
icculus@3846
   377
    if (LoadNASLibrary() < 0) {
icculus@3846
   378
        return 0;
icculus@3846
   379
    } else {
icculus@3846
   380
        AuServer *aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
icculus@3846
   381
        if (aud == NULL) {
icculus@3846
   382
            SDL_SetError("NAS: AuOpenServer() failed (no audio server?)");
icculus@3846
   383
            return 0;
icculus@3846
   384
        }
icculus@3846
   385
        NAS_AuCloseServer(aud);
icculus@3846
   386
    }
icculus@3846
   387
icculus@3821
   388
    /* Set the function pointers */
icculus@3821
   389
    impl->OpenDevice = NAS_OpenDevice;
icculus@3821
   390
    impl->PlayDevice = NAS_PlayDevice;
icculus@3821
   391
    impl->WaitDevice = NAS_WaitDevice;
icculus@3821
   392
    impl->GetDeviceBuf = NAS_GetDeviceBuf;
icculus@3821
   393
    impl->CloseDevice = NAS_CloseDevice;
icculus@3846
   394
    impl->Deinitialize = NAS_Deinitialize;
icculus@3821
   395
    impl->OnlyHasDefaultOutputDevice = 1;  /* !!! FIXME: is this true? */
icculus@3821
   396
icculus@3821
   397
    return 1;
icculus@3821
   398
}
icculus@3821
   399
icculus@3821
   400
AudioBootStrap NAS_bootstrap = {
icculus@3846
   401
    NAS_DRIVER_NAME, "Network Audio System", NAS_Init, 0
icculus@3821
   402
};
icculus@3821
   403
slouken@1895
   404
/* vi: set ts=4 sw=4 expandtab: */