src/audio/SDL_audio.c
author Ryan C. Gordon
Tue, 02 Aug 2016 13:50:21 -0400
changeset 10239 04cda108b406
parent 10238 6fa358b97f4b
child 10241 59c16a96a5a1
permissions -rw-r--r--
audio: implemented higher level infrastructure for running capture devices.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@0
    23
/* Allow access to a raw mixing buffer */
slouken@0
    24
slouken@0
    25
#include "SDL.h"
slouken@2984
    26
#include "SDL_audio.h"
slouken@0
    27
#include "SDL_audio_c.h"
slouken@0
    28
#include "SDL_audiomem.h"
slouken@0
    29
#include "SDL_sysaudio.h"
icculus@10146
    30
#include "../thread/SDL_systhread.h"
slouken@0
    31
aschiffler@4865
    32
#define _THIS SDL_AudioDevice *_this
icculus@2049
    33
icculus@2049
    34
static SDL_AudioDriver current_audio;
icculus@2049
    35
static SDL_AudioDevice *open_devices[16];
icculus@2049
    36
icculus@2049
    37
/* !!! FIXME: These are wordy and unlocalized... */
icculus@2049
    38
#define DEFAULT_OUTPUT_DEVNAME "System audio output device"
icculus@2049
    39
#define DEFAULT_INPUT_DEVNAME "System audio capture device"
icculus@2049
    40
icculus@2049
    41
icculus@2049
    42
/*
icculus@2049
    43
 * Not all of these will be compiled and linked in, but it's convenient
icculus@2049
    44
 *  to have a complete list here and saves yet-another block of #ifdefs...
icculus@2049
    45
 *  Please see bootstrap[], below, for the actual #ifdef mess.
icculus@2049
    46
 */
icculus@2049
    47
extern AudioBootStrap BSD_AUDIO_bootstrap;
icculus@2049
    48
extern AudioBootStrap DSP_bootstrap;
icculus@2049
    49
extern AudioBootStrap ALSA_bootstrap;
icculus@2271
    50
extern AudioBootStrap PULSEAUDIO_bootstrap;
slouken@3099
    51
extern AudioBootStrap QSAAUDIO_bootstrap;
icculus@2049
    52
extern AudioBootStrap SUNAUDIO_bootstrap;
icculus@2049
    53
extern AudioBootStrap ARTS_bootstrap;
icculus@2049
    54
extern AudioBootStrap ESD_bootstrap;
gabomdq@8833
    55
extern AudioBootStrap NACLAUD_bootstrap;
icculus@2049
    56
extern AudioBootStrap NAS_bootstrap;
icculus@5592
    57
extern AudioBootStrap XAUDIO2_bootstrap;
icculus@2049
    58
extern AudioBootStrap DSOUND_bootstrap;
icculus@5588
    59
extern AudioBootStrap WINMM_bootstrap;
icculus@2049
    60
extern AudioBootStrap PAUDIO_bootstrap;
icculus@7981
    61
extern AudioBootStrap HAIKUAUDIO_bootstrap;
icculus@2049
    62
extern AudioBootStrap COREAUDIO_bootstrap;
icculus@2049
    63
extern AudioBootStrap SNDMGR_bootstrap;
icculus@2049
    64
extern AudioBootStrap DISKAUD_bootstrap;
icculus@2049
    65
extern AudioBootStrap DUMMYAUD_bootstrap;
icculus@2049
    66
extern AudioBootStrap DCAUD_bootstrap;
icculus@2049
    67
extern AudioBootStrap DART_bootstrap;
slouken@2735
    68
extern AudioBootStrap NDSAUD_bootstrap;
slouken@2947
    69
extern AudioBootStrap FUSIONSOUND_bootstrap;
paul@4718
    70
extern AudioBootStrap ANDROIDAUD_bootstrap;
kimonline@7009
    71
extern AudioBootStrap PSPAUD_bootstrap;
icculus@7367
    72
extern AudioBootStrap SNDIO_bootstrap;
icculus@9278
    73
extern AudioBootStrap EmscriptenAudio_bootstrap;
icculus@1190
    74
gabomdq@8833
    75
slouken@0
    76
/* Available audio drivers */
slouken@3162
    77
static const AudioBootStrap *const bootstrap[] = {
icculus@2939
    78
#if SDL_AUDIO_DRIVER_PULSEAUDIO
icculus@2939
    79
    &PULSEAUDIO_bootstrap,
slouken@0
    80
#endif
slouken@1361
    81
#if SDL_AUDIO_DRIVER_ALSA
slouken@1895
    82
    &ALSA_bootstrap,
slouken@0
    83
#endif
icculus@7367
    84
#if SDL_AUDIO_DRIVER_SNDIO
icculus@7367
    85
    &SNDIO_bootstrap,
icculus@7367
    86
#endif
slouken@4548
    87
#if SDL_AUDIO_DRIVER_BSD
slouken@4548
    88
    &BSD_AUDIO_bootstrap,
slouken@4548
    89
#endif
icculus@2939
    90
#if SDL_AUDIO_DRIVER_OSS
icculus@2939
    91
    &DSP_bootstrap,
icculus@2271
    92
#endif
slouken@3099
    93
#if SDL_AUDIO_DRIVER_QSA
slouken@3099
    94
    &QSAAUDIO_bootstrap,
slouken@663
    95
#endif
slouken@1361
    96
#if SDL_AUDIO_DRIVER_SUNAUDIO
slouken@1895
    97
    &SUNAUDIO_bootstrap,
slouken@148
    98
#endif
slouken@1361
    99
#if SDL_AUDIO_DRIVER_ARTS
slouken@1895
   100
    &ARTS_bootstrap,
slouken@0
   101
#endif
slouken@1361
   102
#if SDL_AUDIO_DRIVER_ESD
slouken@1895
   103
    &ESD_bootstrap,
slouken@0
   104
#endif
gabomdq@8833
   105
#if SDL_AUDIO_DRIVER_NACL
icculus@10233
   106
    &NACLAUD_bootstrap,
gabomdq@8833
   107
#endif
slouken@1361
   108
#if SDL_AUDIO_DRIVER_NAS
slouken@1895
   109
    &NAS_bootstrap,
slouken@0
   110
#endif
icculus@5592
   111
#if SDL_AUDIO_DRIVER_XAUDIO2
icculus@5592
   112
    &XAUDIO2_bootstrap,
icculus@5592
   113
#endif
slouken@1361
   114
#if SDL_AUDIO_DRIVER_DSOUND
slouken@1895
   115
    &DSOUND_bootstrap,
slouken@0
   116
#endif
icculus@5588
   117
#if SDL_AUDIO_DRIVER_WINMM
icculus@5588
   118
    &WINMM_bootstrap,
slouken@0
   119
#endif
icculus@2049
   120
#if SDL_AUDIO_DRIVER_PAUDIO
icculus@2049
   121
    &PAUDIO_bootstrap,
slouken@1361
   122
#endif
icculus@7981
   123
#if SDL_AUDIO_DRIVER_HAIKU
icculus@7981
   124
    &HAIKUAUDIO_bootstrap,
slouken@0
   125
#endif
slouken@1361
   126
#if SDL_AUDIO_DRIVER_COREAUDIO
slouken@1895
   127
    &COREAUDIO_bootstrap,
slouken@935
   128
#endif
slouken@1361
   129
#if SDL_AUDIO_DRIVER_DISK
slouken@1895
   130
    &DISKAUD_bootstrap,
slouken@68
   131
#endif
icculus@1532
   132
#if SDL_AUDIO_DRIVER_DUMMY
slouken@1895
   133
    &DUMMYAUD_bootstrap,
icculus@1532
   134
#endif
slouken@2947
   135
#if SDL_AUDIO_DRIVER_FUSIONSOUND
slouken@2947
   136
    &FUSIONSOUND_bootstrap,
slouken@2947
   137
#endif
paul@4718
   138
#if SDL_AUDIO_DRIVER_ANDROID
paul@4718
   139
    &ANDROIDAUD_bootstrap,
paul@4718
   140
#endif
kimonline@7009
   141
#if SDL_AUDIO_DRIVER_PSP
kimonline@7009
   142
    &PSPAUD_bootstrap,
kimonline@7009
   143
#endif
icculus@9278
   144
#if SDL_AUDIO_DRIVER_EMSCRIPTEN
icculus@9278
   145
    &EmscriptenAudio_bootstrap,
icculus@9278
   146
#endif
slouken@1895
   147
    NULL
slouken@0
   148
};
icculus@2049
   149
slouken@2060
   150
static SDL_AudioDevice *
slouken@2060
   151
get_audio_device(SDL_AudioDeviceID id)
icculus@2049
   152
{
icculus@2049
   153
    id--;
slouken@2060
   154
    if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) {
icculus@2049
   155
        SDL_SetError("Invalid audio device ID");
icculus@2049
   156
        return NULL;
icculus@2049
   157
    }
icculus@2049
   158
icculus@2049
   159
    return open_devices[id];
icculus@2049
   160
}
icculus@2049
   161
icculus@2049
   162
icculus@2049
   163
/* stubs for audio drivers that don't need a specific entry point... */
icculus@5593
   164
static void
icculus@9394
   165
SDL_AudioDetectDevices_Default(void)
icculus@9394
   166
{
icculus@9394
   167
    /* you have to write your own implementation if these assertions fail. */
icculus@9394
   168
    SDL_assert(current_audio.impl.OnlyHasDefaultOutputDevice);
icculus@9394
   169
    SDL_assert(current_audio.impl.OnlyHasDefaultInputDevice || !current_audio.impl.HasCaptureSupport);
icculus@9394
   170
icculus@9394
   171
    SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, (void *) ((size_t) 0x1));
icculus@9394
   172
    if (current_audio.impl.HasCaptureSupport) {
icculus@9394
   173
        SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, (void *) ((size_t) 0x2));
icculus@9394
   174
    }
slouken@2060
   175
}
slouken@2735
   176
slouken@2060
   177
static void
slouken@2060
   178
SDL_AudioThreadInit_Default(_THIS)
slouken@2060
   179
{                               /* no-op. */
slouken@2060
   180
}
slouken@2735
   181
slouken@2060
   182
static void
slouken@2060
   183
SDL_AudioWaitDevice_Default(_THIS)
slouken@2060
   184
{                               /* no-op. */
slouken@2060
   185
}
slouken@2735
   186
slouken@2060
   187
static void
slouken@2060
   188
SDL_AudioPlayDevice_Default(_THIS)
slouken@2060
   189
{                               /* no-op. */
slouken@2060
   190
}
slouken@2735
   191
icculus@9031
   192
static int
icculus@9031
   193
SDL_AudioGetPendingBytes_Default(_THIS)
icculus@9031
   194
{
icculus@9031
   195
    return 0;
icculus@9031
   196
}
icculus@9031
   197
slouken@2060
   198
static Uint8 *
slouken@2060
   199
SDL_AudioGetDeviceBuf_Default(_THIS)
slouken@2060
   200
{
slouken@2060
   201
    return NULL;
slouken@2060
   202
}
slouken@2735
   203
slouken@2060
   204
static void
slouken@2060
   205
SDL_AudioWaitDone_Default(_THIS)
slouken@2060
   206
{                               /* no-op. */
slouken@2060
   207
}
slouken@2735
   208
icculus@10239
   209
static int
icculus@10239
   210
SDL_AudioCaptureFromDevice_Default(_THIS, void *buffer, int buflen)
icculus@10239
   211
{
icculus@10239
   212
    return -1;  /* just fail immediately. */
icculus@10239
   213
}
icculus@10239
   214
icculus@10239
   215
static void
icculus@10239
   216
SDL_AudioFlushCapture_Default(_THIS)
icculus@10239
   217
{                               /* no-op. */
icculus@10239
   218
}
icculus@10239
   219
slouken@2060
   220
static void
slouken@2060
   221
SDL_AudioCloseDevice_Default(_THIS)
slouken@2060
   222
{                               /* no-op. */
slouken@2060
   223
}
slouken@2735
   224
slouken@2060
   225
static void
slouken@2060
   226
SDL_AudioDeinitialize_Default(void)
slouken@2060
   227
{                               /* no-op. */
slouken@2060
   228
}
icculus@2049
   229
icculus@9394
   230
static void
icculus@9394
   231
SDL_AudioFreeDeviceHandle_Default(void *handle)
icculus@9394
   232
{                               /* no-op. */
icculus@9394
   233
}
icculus@9394
   234
icculus@9394
   235
icculus@2049
   236
static int
icculus@9394
   237
SDL_AudioOpenDevice_Default(_THIS, void *handle, const char *devname, int iscapture)
icculus@2049
   238
{
icculus@9394
   239
    return SDL_Unsupported();
icculus@2049
   240
}
icculus@2049
   241
icculus@9010
   242
static SDL_INLINE SDL_bool
icculus@9010
   243
is_in_audio_device_thread(SDL_AudioDevice * device)
icculus@9010
   244
{
icculus@9010
   245
    /* The device thread locks the same mutex, but not through the public API.
icculus@9010
   246
       This check is in case the application, in the audio callback,
icculus@9010
   247
       tries to lock the thread that we've already locked from the
icculus@9010
   248
       device thread...just in case we only have non-recursive mutexes. */
icculus@9010
   249
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@9010
   250
        return SDL_TRUE;
icculus@9010
   251
    }
icculus@9010
   252
icculus@9010
   253
    return SDL_FALSE;
icculus@9010
   254
}
icculus@9010
   255
icculus@2049
   256
static void
icculus@2049
   257
SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   258
{
icculus@9010
   259
    if (!is_in_audio_device_thread(device)) {
icculus@9010
   260
        SDL_LockMutex(device->mixer_lock);
icculus@2049
   261
    }
icculus@2049
   262
}
icculus@2049
   263
icculus@2049
   264
static void
icculus@2049
   265
SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   266
{
icculus@9010
   267
    if (!is_in_audio_device_thread(device)) {
icculus@9010
   268
        SDL_UnlockMutex(device->mixer_lock);
icculus@2049
   269
    }
icculus@2049
   270
}
icculus@2049
   271
slouken@0
   272
slouken@2060
   273
static void
slouken@2060
   274
finalize_audio_entry_points(void)
icculus@2049
   275
{
icculus@2049
   276
    /*
icculus@2049
   277
     * Fill in stub functions for unused driver entry points. This lets us
icculus@2049
   278
     *  blindly call them without having to check for validity first.
icculus@2049
   279
     */
icculus@2049
   280
slouken@2060
   281
#define FILL_STUB(x) \
icculus@2049
   282
        if (current_audio.impl.x == NULL) { \
icculus@2049
   283
            current_audio.impl.x = SDL_Audio##x##_Default; \
icculus@2049
   284
        }
icculus@2049
   285
    FILL_STUB(DetectDevices);
icculus@2049
   286
    FILL_STUB(OpenDevice);
icculus@2049
   287
    FILL_STUB(ThreadInit);
icculus@2049
   288
    FILL_STUB(WaitDevice);
icculus@2049
   289
    FILL_STUB(PlayDevice);
icculus@9031
   290
    FILL_STUB(GetPendingBytes);
icculus@2049
   291
    FILL_STUB(GetDeviceBuf);
icculus@2049
   292
    FILL_STUB(WaitDone);
icculus@10239
   293
    FILL_STUB(CaptureFromDevice);
icculus@10239
   294
    FILL_STUB(FlushCapture);
icculus@2049
   295
    FILL_STUB(CloseDevice);
icculus@2049
   296
    FILL_STUB(LockDevice);
icculus@2049
   297
    FILL_STUB(UnlockDevice);
icculus@9394
   298
    FILL_STUB(FreeDeviceHandle);
icculus@2049
   299
    FILL_STUB(Deinitialize);
slouken@2060
   300
#undef FILL_STUB
icculus@2049
   301
}
icculus@2049
   302
slouken@2716
   303
icculus@9393
   304
/* device hotplug support... */
icculus@9393
   305
icculus@9393
   306
static int
icculus@9394
   307
add_audio_device(const char *name, void *handle, SDL_AudioDeviceItem **devices, int *devCount)
icculus@9393
   308
{
icculus@9393
   309
    int retval = -1;
icculus@9394
   310
    const size_t size = sizeof (SDL_AudioDeviceItem) + SDL_strlen(name) + 1;
icculus@9394
   311
    SDL_AudioDeviceItem *item = (SDL_AudioDeviceItem *) SDL_malloc(size);
icculus@9394
   312
    if (item == NULL) {
icculus@9394
   313
        return -1;
icculus@9394
   314
    }
icculus@9393
   315
icculus@9410
   316
    SDL_assert(handle != NULL);  /* we reserve NULL, audio backends can't use it. */
icculus@9394
   317
icculus@9394
   318
    item->handle = handle;
icculus@9394
   319
    SDL_strlcpy(item->name, name, size - sizeof (SDL_AudioDeviceItem));
icculus@9394
   320
icculus@9394
   321
    SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   322
    item->next = *devices;
icculus@9394
   323
    *devices = item;
icculus@9394
   324
    retval = (*devCount)++;
icculus@9394
   325
    SDL_UnlockMutex(current_audio.detectionLock);
icculus@9393
   326
icculus@9393
   327
    return retval;
icculus@9393
   328
}
icculus@9393
   329
icculus@9394
   330
static SDL_INLINE int
icculus@9394
   331
add_capture_device(const char *name, void *handle)
icculus@9393
   332
{
icculus@10231
   333
    SDL_assert(current_audio.impl.HasCaptureSupport);
icculus@9394
   334
    return add_audio_device(name, handle, &current_audio.inputDevices, &current_audio.inputDeviceCount);
icculus@9393
   335
}
icculus@9393
   336
icculus@9394
   337
static SDL_INLINE int
icculus@9394
   338
add_output_device(const char *name, void *handle)
icculus@9393
   339
{
icculus@9394
   340
    return add_audio_device(name, handle, &current_audio.outputDevices, &current_audio.outputDeviceCount);
icculus@9393
   341
}
icculus@9393
   342
icculus@9393
   343
static void
icculus@9394
   344
free_device_list(SDL_AudioDeviceItem **devices, int *devCount)
icculus@9393
   345
{
icculus@9394
   346
    SDL_AudioDeviceItem *item, *next;
icculus@9394
   347
    for (item = *devices; item != NULL; item = next) {
icculus@9394
   348
        next = item->next;
icculus@9394
   349
        if (item->handle != NULL) {
icculus@9394
   350
            current_audio.impl.FreeDeviceHandle(item->handle);
icculus@9393
   351
        }
icculus@9394
   352
        SDL_free(item);
icculus@9393
   353
    }
icculus@9393
   354
    *devices = NULL;
icculus@9393
   355
    *devCount = 0;
icculus@9393
   356
}
icculus@9393
   357
icculus@9393
   358
icculus@9393
   359
/* The audio backends call this when a new device is plugged in. */
icculus@9393
   360
void
icculus@9394
   361
SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
icculus@9393
   362
{
icculus@9394
   363
    const int device_index = iscapture ? add_capture_device(name, handle) : add_output_device(name, handle);
icculus@9393
   364
    if (device_index != -1) {
icculus@9393
   365
        /* Post the event, if desired */
icculus@9393
   366
        if (SDL_GetEventState(SDL_AUDIODEVICEADDED) == SDL_ENABLE) {
icculus@9393
   367
            SDL_Event event;
icculus@9401
   368
            SDL_zero(event);
icculus@9393
   369
            event.adevice.type = SDL_AUDIODEVICEADDED;
icculus@9393
   370
            event.adevice.which = device_index;
icculus@9393
   371
            event.adevice.iscapture = iscapture;
icculus@9393
   372
            SDL_PushEvent(&event);
icculus@9393
   373
        }
icculus@9393
   374
    }
icculus@9393
   375
}
icculus@9393
   376
icculus@9394
   377
/* The audio backends call this when a currently-opened device is lost. */
icculus@9394
   378
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
icculus@9393
   379
{
icculus@9394
   380
    SDL_assert(get_audio_device(device->id) == device);
icculus@9396
   381
icculus@10238
   382
    if (!SDL_AtomicGet(&device->enabled)) {
icculus@9396
   383
        return;
icculus@9396
   384
    }
icculus@9394
   385
icculus@9394
   386
    /* Ends the audio callback and mark the device as STOPPED, but the
icculus@9394
   387
       app still needs to close the device to free resources. */
icculus@9394
   388
    current_audio.impl.LockDevice(device);
icculus@10238
   389
    SDL_AtomicSet(&device->enabled, 0);
icculus@9394
   390
    current_audio.impl.UnlockDevice(device);
icculus@9393
   391
icculus@9394
   392
    /* Post the event, if desired */
icculus@9394
   393
    if (SDL_GetEventState(SDL_AUDIODEVICEREMOVED) == SDL_ENABLE) {
icculus@9394
   394
        SDL_Event event;
icculus@9401
   395
        SDL_zero(event);
icculus@9394
   396
        event.adevice.type = SDL_AUDIODEVICEREMOVED;
icculus@9394
   397
        event.adevice.which = device->id;
icculus@9394
   398
        event.adevice.iscapture = device->iscapture ? 1 : 0;
icculus@9394
   399
        SDL_PushEvent(&event);
icculus@9394
   400
    }
icculus@9394
   401
}
icculus@9393
   402
icculus@9394
   403
static void
icculus@9394
   404
mark_device_removed(void *handle, SDL_AudioDeviceItem *devices, SDL_bool *removedFlag)
icculus@9394
   405
{
icculus@9394
   406
    SDL_AudioDeviceItem *item;
icculus@9394
   407
    SDL_assert(handle != NULL);
icculus@9394
   408
    for (item = devices; item != NULL; item = item->next) {
icculus@9394
   409
        if (item->handle == handle) {
icculus@9394
   410
            item->handle = NULL;
icculus@9394
   411
            *removedFlag = SDL_TRUE;
icculus@9394
   412
            return;
icculus@9393
   413
        }
icculus@9393
   414
    }
icculus@9394
   415
}
icculus@9393
   416
icculus@9394
   417
/* The audio backends call this when a device is removed from the system. */
icculus@9394
   418
void
icculus@9399
   419
SDL_RemoveAudioDevice(const int iscapture, void *handle)
icculus@9394
   420
{
icculus@9394
   421
    SDL_LockMutex(current_audio.detectionLock);
icculus@9399
   422
    if (iscapture) {
icculus@9399
   423
        mark_device_removed(handle, current_audio.inputDevices, &current_audio.captureDevicesRemoved);
icculus@9399
   424
    } else {
icculus@9399
   425
        mark_device_removed(handle, current_audio.outputDevices, &current_audio.outputDevicesRemoved);
icculus@9399
   426
    }
icculus@9394
   427
    SDL_UnlockMutex(current_audio.detectionLock);
icculus@9394
   428
    current_audio.impl.FreeDeviceHandle(handle);
icculus@9393
   429
}
icculus@9393
   430
icculus@9393
   431
icculus@9012
   432
icculus@9012
   433
/* buffer queueing support... */
icculus@9012
   434
icculus@9012
   435
/* this expects that you managed thread safety elsewhere. */
icculus@9012
   436
static void
icculus@9012
   437
free_audio_queue(SDL_AudioBufferQueue *buffer)
icculus@9012
   438
{
icculus@9012
   439
    while (buffer) {
icculus@9012
   440
        SDL_AudioBufferQueue *next = buffer->next;
icculus@9012
   441
        SDL_free(buffer);
icculus@9012
   442
        buffer = next;
icculus@9012
   443
    }
icculus@9012
   444
}
icculus@9012
   445
icculus@9012
   446
static void SDLCALL
icculus@9012
   447
SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int _len)
icculus@9012
   448
{
icculus@9012
   449
    /* this function always holds the mixer lock before being called. */
icculus@9012
   450
    Uint32 len = (Uint32) _len;
icculus@9012
   451
    SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
icculus@9012
   452
    SDL_AudioBufferQueue *buffer;
icculus@9012
   453
icculus@9012
   454
    SDL_assert(device != NULL);  /* this shouldn't ever happen, right?! */
icculus@9012
   455
    SDL_assert(_len >= 0);  /* this shouldn't ever happen, right?! */
icculus@9012
   456
icculus@9012
   457
    while ((len > 0) && ((buffer = device->buffer_queue_head) != NULL)) {
icculus@9012
   458
        const Uint32 avail = buffer->datalen - buffer->startpos;
icculus@9012
   459
        const Uint32 cpy = SDL_min(len, avail);
icculus@9012
   460
        SDL_assert(device->queued_bytes >= avail);
icculus@9012
   461
icculus@9012
   462
        SDL_memcpy(stream, buffer->data + buffer->startpos, cpy);
icculus@9012
   463
        buffer->startpos += cpy;
icculus@9012
   464
        stream += cpy;
icculus@9012
   465
        device->queued_bytes -= cpy;
icculus@9012
   466
        len -= cpy;
icculus@9012
   467
icculus@9012
   468
        if (buffer->startpos == buffer->datalen) {  /* packet is done, put it in the pool. */
icculus@9012
   469
            device->buffer_queue_head = buffer->next;
icculus@9012
   470
            SDL_assert((buffer->next != NULL) || (buffer == device->buffer_queue_tail));
icculus@9012
   471
            buffer->next = device->buffer_queue_pool;
icculus@9012
   472
            device->buffer_queue_pool = buffer;
icculus@9012
   473
        }
icculus@9012
   474
    }
icculus@9012
   475
icculus@9012
   476
    SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
icculus@9012
   477
icculus@9012
   478
    if (len > 0) {  /* fill any remaining space in the stream with silence. */
icculus@9012
   479
        SDL_assert(device->buffer_queue_head == NULL);
icculus@9012
   480
        SDL_memset(stream, device->spec.silence, len);
icculus@9012
   481
    }
icculus@9012
   482
icculus@9012
   483
    if (device->buffer_queue_head == NULL) {
icculus@9012
   484
        device->buffer_queue_tail = NULL;  /* in case we drained the queue entirely. */
icculus@9012
   485
    }
icculus@9012
   486
}
icculus@9012
   487
icculus@9012
   488
int
icculus@9012
   489
SDL_QueueAudio(SDL_AudioDeviceID devid, const void *_data, Uint32 len)
icculus@9012
   490
{
icculus@9012
   491
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9012
   492
    const Uint8 *data = (const Uint8 *) _data;
icculus@9012
   493
    SDL_AudioBufferQueue *orighead;
icculus@9012
   494
    SDL_AudioBufferQueue *origtail;
icculus@9012
   495
    Uint32 origlen;
icculus@9012
   496
    Uint32 datalen;
icculus@9012
   497
icculus@9012
   498
    if (!device) {
icculus@9012
   499
        return -1;  /* get_audio_device() will have set the error state */
icculus@9012
   500
    }
icculus@9012
   501
icculus@9012
   502
    if (device->spec.callback != SDL_BufferQueueDrainCallback) {
icculus@9012
   503
        return SDL_SetError("Audio device has a callback, queueing not allowed");
icculus@9012
   504
    }
icculus@9012
   505
icculus@9012
   506
    current_audio.impl.LockDevice(device);
icculus@9012
   507
icculus@9012
   508
    orighead = device->buffer_queue_head;
icculus@9012
   509
    origtail = device->buffer_queue_tail;
icculus@9012
   510
    origlen = origtail ? origtail->datalen : 0;
icculus@9012
   511
icculus@9012
   512
    while (len > 0) {
icculus@9012
   513
        SDL_AudioBufferQueue *packet = device->buffer_queue_tail;
icculus@9012
   514
        SDL_assert(!packet || (packet->datalen <= SDL_AUDIOBUFFERQUEUE_PACKETLEN));
icculus@9012
   515
        if (!packet || (packet->datalen >= SDL_AUDIOBUFFERQUEUE_PACKETLEN)) {
icculus@9012
   516
            /* tail packet missing or completely full; we need a new packet. */
icculus@9012
   517
            packet = device->buffer_queue_pool;
icculus@9012
   518
            if (packet != NULL) {
icculus@9012
   519
                /* we have one available in the pool. */
icculus@9012
   520
                device->buffer_queue_pool = packet->next;
icculus@9012
   521
            } else {
icculus@9012
   522
                /* Have to allocate a new one! */
icculus@9012
   523
                packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
icculus@9012
   524
                if (packet == NULL) {
icculus@9012
   525
                    /* uhoh, reset so we've queued nothing new, free what we can. */
icculus@9012
   526
                    if (!origtail) {
icculus@9012
   527
                        packet = device->buffer_queue_head;  /* whole queue. */
icculus@9012
   528
                    } else {
icculus@9012
   529
                        packet = origtail->next;  /* what we added to existing queue. */
icculus@9012
   530
                        origtail->next = NULL;
icculus@9012
   531
                        origtail->datalen = origlen;
icculus@9012
   532
                    }
icculus@9012
   533
                    device->buffer_queue_head = orighead;
icculus@9012
   534
                    device->buffer_queue_tail = origtail;
icculus@9012
   535
                    device->buffer_queue_pool = NULL;
icculus@9012
   536
icculus@9012
   537
                    current_audio.impl.UnlockDevice(device);
icculus@9012
   538
icculus@9012
   539
                    free_audio_queue(packet);  /* give back what we can. */
icculus@9012
   540
icculus@9012
   541
                    return SDL_OutOfMemory();
icculus@9012
   542
                }
icculus@9012
   543
            }
icculus@9012
   544
            packet->datalen = 0;
icculus@9012
   545
            packet->startpos = 0;
icculus@9012
   546
            packet->next = NULL;
icculus@9012
   547
icculus@9012
   548
            SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
icculus@9012
   549
            if (device->buffer_queue_tail == NULL) {
icculus@9012
   550
                device->buffer_queue_head = packet;
icculus@9012
   551
            } else {
icculus@9012
   552
                device->buffer_queue_tail->next = packet;
icculus@9012
   553
            }
icculus@9012
   554
            device->buffer_queue_tail = packet;
icculus@9012
   555
        }
icculus@9012
   556
icculus@9012
   557
        datalen = SDL_min(len, SDL_AUDIOBUFFERQUEUE_PACKETLEN - packet->datalen);
icculus@9012
   558
        SDL_memcpy(packet->data + packet->datalen, data, datalen);
icculus@9012
   559
        data += datalen;
icculus@9012
   560
        len -= datalen;
icculus@9012
   561
        packet->datalen += datalen;
icculus@9012
   562
        device->queued_bytes += datalen;
icculus@9012
   563
    }
icculus@9012
   564
icculus@9012
   565
    current_audio.impl.UnlockDevice(device);
icculus@9012
   566
icculus@9012
   567
    return 0;
icculus@9012
   568
}
icculus@9012
   569
icculus@9012
   570
Uint32
icculus@9012
   571
SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
icculus@9012
   572
{
icculus@9012
   573
    Uint32 retval = 0;
icculus@9012
   574
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9032
   575
icculus@9032
   576
    /* Nothing to do unless we're set up for queueing. */
icculus@9032
   577
    if (device && (device->spec.callback == SDL_BufferQueueDrainCallback)) {
icculus@9012
   578
        current_audio.impl.LockDevice(device);
icculus@9031
   579
        retval = device->queued_bytes + current_audio.impl.GetPendingBytes(device);
icculus@9012
   580
        current_audio.impl.UnlockDevice(device);
icculus@9012
   581
    }
icculus@9012
   582
icculus@9012
   583
    return retval;
icculus@9012
   584
}
icculus@9012
   585
icculus@9012
   586
void
icculus@9012
   587
SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
icculus@9012
   588
{
icculus@9012
   589
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9012
   590
    SDL_AudioBufferQueue *buffer = NULL;
icculus@9012
   591
    if (!device) {
icculus@9012
   592
        return;  /* nothing to do. */
icculus@9012
   593
    }
icculus@9012
   594
icculus@9012
   595
    /* Blank out the device and release the mutex. Free it afterwards. */
icculus@9012
   596
    current_audio.impl.LockDevice(device);
icculus@9012
   597
    buffer = device->buffer_queue_head;
icculus@9012
   598
    device->buffer_queue_tail = NULL;
icculus@9012
   599
    device->buffer_queue_head = NULL;
icculus@9012
   600
    device->queued_bytes = 0;
icculus@9012
   601
    current_audio.impl.UnlockDevice(device);
icculus@9012
   602
icculus@9012
   603
    free_audio_queue(buffer);
icculus@9012
   604
}
icculus@9012
   605
icculus@9012
   606
slouken@0
   607
/* The general mixing thread function */
icculus@10239
   608
static int SDLCALL
icculus@2049
   609
SDL_RunAudio(void *devicep)
slouken@0
   610
{
icculus@2049
   611
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
icculus@9398
   612
    const int silence = (int) device->spec.silence;
icculus@9398
   613
    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
icculus@9398
   614
    const int stream_len = (device->convert.needed) ? device->convert.len : device->spec.size;
slouken@1895
   615
    Uint8 *stream;
icculus@9398
   616
    void *udata = device->spec.userdata;
icculus@10239
   617
    void (SDLCALL *callback) (void *, Uint8 *, int) = device->spec.callback;
icculus@10239
   618
icculus@10239
   619
    SDL_assert(!device->iscapture);
slouken@2716
   620
slouken@5509
   621
    /* The audio mixing is always a high priority thread */
slouken@5509
   622
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
slouken@5509
   623
slouken@1895
   624
    /* Perform any thread setup */
icculus@2049
   625
    device->threadid = SDL_ThreadID();
icculus@2049
   626
    current_audio.impl.ThreadInit(device);
slouken@0
   627
icculus@9398
   628
    /* Loop, filling the audio buffers */
icculus@10233
   629
    while (!SDL_AtomicGet(&device->shutdown)) {
icculus@9398
   630
        /* Fill the current buffer with sound */
icculus@9398
   631
        if (device->convert.needed) {
icculus@9398
   632
            stream = device->convert.buf;
icculus@10238
   633
        } else if (SDL_AtomicGet(&device->enabled)) {
icculus@9398
   634
            stream = current_audio.impl.GetDeviceBuf(device);
icculus@9398
   635
        } else {
icculus@9398
   636
            /* if the device isn't enabled, we still write to the
icculus@9398
   637
               fake_stream, so the app's callback will fire with
icculus@9398
   638
               a regular frequency, in case they depend on that
icculus@9398
   639
               for timing or progress. They can use hotplug
icculus@9398
   640
               now to know if the device failed. */
icculus@9398
   641
            stream = NULL;
slouken@2716
   642
        }
slouken@3336
   643
icculus@9398
   644
        if (stream == NULL) {
icculus@9398
   645
            stream = device->fake_stream;
icculus@9398
   646
        }
slouken@2716
   647
icculus@9398
   648
        /* !!! FIXME: this should be LockDevice. */
icculus@9398
   649
        SDL_LockMutex(device->mixer_lock);
icculus@10238
   650
        if (SDL_AtomicGet(&device->paused)) {
icculus@9398
   651
            SDL_memset(stream, silence, stream_len);
icculus@9398
   652
        } else {
icculus@10239
   653
            (*callback) (udata, stream, stream_len);
icculus@9398
   654
        }
icculus@9398
   655
        SDL_UnlockMutex(device->mixer_lock);
slouken@2716
   656
icculus@9398
   657
        /* Convert the audio if necessary */
icculus@10238
   658
        if (device->convert.needed && SDL_AtomicGet(&device->enabled)) {
icculus@9398
   659
            SDL_ConvertAudio(&device->convert);
icculus@9398
   660
            stream = current_audio.impl.GetDeviceBuf(device);
icculus@9397
   661
            if (stream == NULL) {
icculus@9397
   662
                stream = device->fake_stream;
icculus@9398
   663
            } else {
icculus@9398
   664
                SDL_memcpy(stream, device->convert.buf,
icculus@9398
   665
                           device->convert.len_cvt);
slouken@1895
   666
            }
icculus@9398
   667
        }
slouken@1562
   668
icculus@9398
   669
        /* Ready current buffer for play and change current buffer */
icculus@9398
   670
        if (stream == device->fake_stream) {
icculus@9398
   671
            SDL_Delay(delay);
icculus@9398
   672
        } else {
icculus@9398
   673
            current_audio.impl.PlayDevice(device);
icculus@9398
   674
            current_audio.impl.WaitDevice(device);
slouken@1895
   675
        }
slouken@1895
   676
    }
slouken@1562
   677
icculus@9398
   678
    /* Wait for the audio to drain. */
icculus@10239
   679
    /* !!! FIXME: can we rename this WaitDrain? */
icculus@2049
   680
    current_audio.impl.WaitDone(device);
slouken@21
   681
icculus@9382
   682
    return 0;
slouken@0
   683
}
slouken@0
   684
icculus@10239
   685
/* The general capture thread function */
icculus@10239
   686
static int SDLCALL
icculus@10239
   687
SDL_CaptureAudio(void *devicep)
icculus@10239
   688
{
icculus@10239
   689
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
icculus@10239
   690
    const int silence = (int) device->spec.silence;
icculus@10239
   691
    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
icculus@10239
   692
    const int stream_len = (device->convert.needed) ? device->convert.len : device->spec.size;
icculus@10239
   693
    Uint8 *stream;
icculus@10239
   694
    void *udata = device->spec.userdata;
icculus@10239
   695
    void (SDLCALL *callback) (void *, Uint8 *, int) = device->spec.callback;
icculus@10239
   696
icculus@10239
   697
    SDL_assert(device->iscapture);
icculus@10239
   698
icculus@10239
   699
    /* The audio mixing is always a high priority thread */
icculus@10239
   700
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
icculus@10239
   701
icculus@10239
   702
    /* Perform any thread setup */
icculus@10239
   703
    device->threadid = SDL_ThreadID();
icculus@10239
   704
    current_audio.impl.ThreadInit(device);
icculus@10239
   705
icculus@10239
   706
    /* Loop, filling the audio buffers */
icculus@10239
   707
    while (!SDL_AtomicGet(&device->shutdown)) {
icculus@10239
   708
        int still_need;
icculus@10239
   709
        Uint8 *ptr;
icculus@10239
   710
icculus@10239
   711
        if (!SDL_AtomicGet(&device->enabled) || SDL_AtomicGet(&device->paused)) {
icculus@10239
   712
            SDL_Delay(delay);  /* just so we don't cook the CPU. */
icculus@10239
   713
            current_audio.impl.FlushCapture(device);  /* dump anything pending. */
icculus@10239
   714
            continue;
icculus@10239
   715
        }
icculus@10239
   716
icculus@10239
   717
        /* Fill the current buffer with sound */
icculus@10239
   718
        still_need = stream_len;
icculus@10239
   719
        if (device->convert.needed) {
icculus@10239
   720
            ptr = stream = device->convert.buf;
icculus@10239
   721
        } else {
icculus@10239
   722
            /* just use the "fake" stream to hold data read from the device. */
icculus@10239
   723
            ptr = stream = device->fake_stream;
icculus@10239
   724
        }
icculus@10239
   725
icculus@10239
   726
        /* We still read from the device when "paused" to keep the state sane,
icculus@10239
   727
           and block when there isn't data so this thread isn't eating CPU.
icculus@10239
   728
           But we don't process it further or call the app's callback. */
icculus@10239
   729
icculus@10239
   730
        while (still_need > 0) {
icculus@10239
   731
            const int rc = current_audio.impl.CaptureFromDevice(device, ptr, still_need);
icculus@10239
   732
            SDL_assert(rc != 0);  /* device should have blocked, failed, or returned data. */
icculus@10239
   733
            SDL_assert(rc <= still_need);  /* device should not overflow buffer. :) */
icculus@10239
   734
            if (rc > 0) {
icculus@10239
   735
                still_need -= rc;
icculus@10239
   736
                ptr += rc;
icculus@10239
   737
            } else {  /* uhoh, device failed for some reason! */
icculus@10239
   738
                SDL_OpenedAudioDeviceDisconnected(device);
icculus@10239
   739
                break;
icculus@10239
   740
            }
icculus@10239
   741
        }
icculus@10239
   742
icculus@10239
   743
        if (still_need > 0) {
icculus@10239
   744
            /* Keep any data we already read, silence the rest. */
icculus@10239
   745
            SDL_memset(ptr, silence, still_need);
icculus@10239
   746
        }
icculus@10239
   747
icculus@10239
   748
        if (device->convert.needed) {
icculus@10239
   749
            SDL_ConvertAudio(&device->convert);
icculus@10239
   750
        }
icculus@10239
   751
icculus@10239
   752
        /* !!! FIXME: this should be LockDevice. */
icculus@10239
   753
        SDL_LockMutex(device->mixer_lock);
icculus@10239
   754
        if (!SDL_AtomicGet(&device->paused)) {
icculus@10239
   755
            (*callback)(udata, stream, stream_len);
icculus@10239
   756
        }
icculus@10239
   757
        SDL_UnlockMutex(device->mixer_lock);
icculus@10239
   758
    }
icculus@10239
   759
icculus@10239
   760
    current_audio.impl.FlushCapture(device);
icculus@10239
   761
icculus@10239
   762
    return 0;
icculus@10239
   763
}
icculus@10239
   764
slouken@322
   765
icculus@1982
   766
static SDL_AudioFormat
slouken@1895
   767
SDL_ParseAudioFormat(const char *string)
slouken@1794
   768
{
icculus@2076
   769
#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
icculus@2049
   770
    CHECK_FMT_STRING(U8);
icculus@2049
   771
    CHECK_FMT_STRING(S8);
icculus@2049
   772
    CHECK_FMT_STRING(U16LSB);
icculus@2049
   773
    CHECK_FMT_STRING(S16LSB);
icculus@2049
   774
    CHECK_FMT_STRING(U16MSB);
icculus@2049
   775
    CHECK_FMT_STRING(S16MSB);
icculus@2049
   776
    CHECK_FMT_STRING(U16SYS);
icculus@2049
   777
    CHECK_FMT_STRING(S16SYS);
icculus@2049
   778
    CHECK_FMT_STRING(U16);
icculus@2049
   779
    CHECK_FMT_STRING(S16);
icculus@2049
   780
    CHECK_FMT_STRING(S32LSB);
icculus@2049
   781
    CHECK_FMT_STRING(S32MSB);
icculus@2049
   782
    CHECK_FMT_STRING(S32SYS);
icculus@2049
   783
    CHECK_FMT_STRING(S32);
icculus@2049
   784
    CHECK_FMT_STRING(F32LSB);
icculus@2049
   785
    CHECK_FMT_STRING(F32MSB);
icculus@2049
   786
    CHECK_FMT_STRING(F32SYS);
icculus@2049
   787
    CHECK_FMT_STRING(F32);
slouken@2060
   788
#undef CHECK_FMT_STRING
icculus@2049
   789
    return 0;
slouken@1895
   790
}
slouken@1895
   791
slouken@1895
   792
int
slouken@1895
   793
SDL_GetNumAudioDrivers(void)
slouken@1895
   794
{
icculus@9382
   795
    return SDL_arraysize(bootstrap) - 1;
slouken@1895
   796
}
slouken@1895
   797
slouken@1895
   798
const char *
slouken@1895
   799
SDL_GetAudioDriver(int index)
slouken@1895
   800
{
slouken@1895
   801
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
icculus@9382
   802
        return bootstrap[index]->name;
slouken@1895
   803
    }
icculus@9382
   804
    return NULL;
slouken@1794
   805
}
slouken@1794
   806
slouken@1895
   807
int
slouken@1895
   808
SDL_AudioInit(const char *driver_name)
slouken@0
   809
{
icculus@2049
   810
    int i = 0;
icculus@2049
   811
    int initialized = 0;
icculus@2049
   812
    int tried_to_init = 0;
slouken@0
   813
icculus@2049
   814
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
slouken@2060
   815
        SDL_AudioQuit();        /* shutdown driver if already running. */
slouken@1895
   816
    }
slouken@0
   817
icculus@9392
   818
    SDL_zero(current_audio);
icculus@9392
   819
    SDL_zero(open_devices);
icculus@2049
   820
slouken@1895
   821
    /* Select the proper audio driver */
slouken@1909
   822
    if (driver_name == NULL) {
slouken@1909
   823
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
slouken@1909
   824
    }
icculus@2049
   825
icculus@2049
   826
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
icculus@2049
   827
        /* make sure we should even try this driver before doing so... */
icculus@2049
   828
        const AudioBootStrap *backend = bootstrap[i];
slouken@6900
   829
        if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
slouken@6900
   830
            (!driver_name && backend->demand_only)) {
icculus@2049
   831
            continue;
icculus@2049
   832
        }
slouken@0
   833
icculus@2049
   834
        tried_to_init = 1;
icculus@9392
   835
        SDL_zero(current_audio);
icculus@2049
   836
        current_audio.name = backend->name;
icculus@2049
   837
        current_audio.desc = backend->desc;
icculus@3699
   838
        initialized = backend->init(&current_audio.impl);
slouken@1895
   839
    }
icculus@2049
   840
icculus@2049
   841
    if (!initialized) {
icculus@2049
   842
        /* specific drivers will set the error message if they fail... */
icculus@2049
   843
        if (!tried_to_init) {
slouken@1895
   844
            if (driver_name) {
icculus@3699
   845
                SDL_SetError("Audio target '%s' not available", driver_name);
slouken@1895
   846
            } else {
slouken@1895
   847
                SDL_SetError("No available audio device");
slouken@1895
   848
            }
slouken@1895
   849
        }
icculus@2049
   850
icculus@9392
   851
        SDL_zero(current_audio);
icculus@9382
   852
        return -1;            /* No driver was available, so fail. */
slouken@1895
   853
    }
icculus@2049
   854
icculus@9394
   855
    current_audio.detectionLock = SDL_CreateMutex();
icculus@9393
   856
icculus@2049
   857
    finalize_audio_entry_points();
icculus@2049
   858
icculus@9393
   859
    /* Make sure we have a list of devices available at startup. */
icculus@9394
   860
    current_audio.impl.DetectDevices();
icculus@9393
   861
icculus@9382
   862
    return 0;
slouken@0
   863
}
slouken@0
   864
slouken@1895
   865
/*
slouken@1895
   866
 * Get the current audio driver name
slouken@1895
   867
 */
slouken@1895
   868
const char *
slouken@1895
   869
SDL_GetCurrentAudioDriver()
slouken@0
   870
{
icculus@2049
   871
    return current_audio.name;
slouken@0
   872
}
slouken@0
   873
icculus@9394
   874
/* Clean out devices that we've removed but had to keep around for stability. */
icculus@9394
   875
static void
icculus@9394
   876
clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *removedFlag)
icculus@9394
   877
{
icculus@9394
   878
    SDL_AudioDeviceItem *item = *devices;
icculus@9394
   879
    SDL_AudioDeviceItem *prev = NULL;
icculus@9394
   880
    int total = 0;
icculus@9394
   881
icculus@9394
   882
    while (item) {
icculus@9394
   883
        SDL_AudioDeviceItem *next = item->next;
icculus@9394
   884
        if (item->handle != NULL) {
icculus@9394
   885
            total++;
icculus@9394
   886
            prev = item;
icculus@9394
   887
        } else {
icculus@9394
   888
            if (prev) {
icculus@9394
   889
                prev->next = next;
icculus@9394
   890
            } else {
icculus@9394
   891
                *devices = next;
icculus@9394
   892
            }
icculus@9394
   893
            SDL_free(item);
icculus@9394
   894
        }
icculus@9394
   895
        item = next;
icculus@9394
   896
    }
icculus@9394
   897
icculus@9394
   898
    *devCount = total;
icculus@9394
   899
    *removedFlag = SDL_FALSE;
icculus@9394
   900
}
icculus@9394
   901
icculus@9394
   902
slouken@1895
   903
int
icculus@2049
   904
SDL_GetNumAudioDevices(int iscapture)
slouken@0
   905
{
icculus@5593
   906
    int retval = 0;
icculus@5593
   907
icculus@2049
   908
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   909
        return -1;
icculus@2049
   910
    }
icculus@5593
   911
icculus@9394
   912
    SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   913
    if (iscapture && current_audio.captureDevicesRemoved) {
icculus@9394
   914
        clean_out_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount, &current_audio.captureDevicesRemoved);
icculus@2049
   915
    }
icculus@2049
   916
icculus@9394
   917
    if (!iscapture && current_audio.outputDevicesRemoved) {
icculus@9394
   918
        clean_out_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount, &current_audio.outputDevicesRemoved);
icculus@9394
   919
        current_audio.outputDevicesRemoved = SDL_FALSE;
icculus@5593
   920
    }
icculus@5593
   921
icculus@9393
   922
    retval = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
icculus@9394
   923
    SDL_UnlockMutex(current_audio.detectionLock);
icculus@9393
   924
icculus@5593
   925
    return retval;
icculus@2049
   926
}
icculus@2049
   927
slouken@0
   928
icculus@2049
   929
const char *
icculus@2049
   930
SDL_GetAudioDeviceName(int index, int iscapture)
icculus@2049
   931
{
icculus@9393
   932
    const char *retval = NULL;
icculus@9393
   933
icculus@2049
   934
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   935
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   936
        return NULL;
icculus@2049
   937
    }
icculus@2049
   938
icculus@2049
   939
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   940
        SDL_SetError("No capture support");
icculus@2049
   941
        return NULL;
slouken@1895
   942
    }
icculus@2049
   943
icculus@9394
   944
    if (index >= 0) {
icculus@9394
   945
        SDL_AudioDeviceItem *item;
icculus@9394
   946
        int i;
slouken@0
   947
icculus@9394
   948
        SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   949
        item = iscapture ? current_audio.inputDevices : current_audio.outputDevices;
icculus@9394
   950
        i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
icculus@9394
   951
        if (index < i) {
icculus@9394
   952
            for (i--; i > index; i--, item = item->next) {
icculus@9394
   953
                SDL_assert(item != NULL);
icculus@9394
   954
            }
icculus@9394
   955
            SDL_assert(item != NULL);
icculus@9394
   956
            retval = item->name;
slouken@7904
   957
        }
icculus@9394
   958
        SDL_UnlockMutex(current_audio.detectionLock);
slouken@1895
   959
    }
slouken@262
   960
icculus@9394
   961
    if (retval == NULL) {
icculus@9394
   962
        SDL_SetError("No such device");
icculus@5593
   963
    }
icculus@5593
   964
icculus@9394
   965
    return retval;
icculus@2049
   966
}
icculus@2049
   967
icculus@2049
   968
icculus@2049
   969
static void
slouken@2060
   970
close_audio_device(SDL_AudioDevice * device)
icculus@2049
   971
{
icculus@10233
   972
    SDL_AtomicSet(&device->shutdown, 1);
icculus@10238
   973
    SDL_AtomicSet(&device->enabled, 0);
icculus@2049
   974
    if (device->thread != NULL) {
icculus@2049
   975
        SDL_WaitThread(device->thread, NULL);
icculus@2049
   976
    }
icculus@2049
   977
    if (device->mixer_lock != NULL) {
icculus@2049
   978
        SDL_DestroyMutex(device->mixer_lock);
icculus@2049
   979
    }
slouken@7719
   980
    SDL_FreeAudioMem(device->fake_stream);
icculus@2049
   981
    if (device->convert.needed) {
icculus@2049
   982
        SDL_FreeAudioMem(device->convert.buf);
icculus@2049
   983
    }
icculus@2049
   984
    if (device->opened) {
icculus@2049
   985
        current_audio.impl.CloseDevice(device);
icculus@10235
   986
        device->opened = SDL_FALSE;
icculus@2049
   987
    }
icculus@9012
   988
icculus@9012
   989
    free_audio_queue(device->buffer_queue_head);
icculus@9012
   990
    free_audio_queue(device->buffer_queue_pool);
icculus@9012
   991
icculus@2049
   992
    SDL_FreeAudioMem(device);
icculus@2049
   993
}
icculus@2049
   994
icculus@2049
   995
icculus@2049
   996
/*
icculus@2049
   997
 * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
icculus@2049
   998
 *  Fills in a sanitized copy in (prepared).
icculus@2049
   999
 *  Returns non-zero if okay, zero on fatal parameters in (orig).
icculus@2049
  1000
 */
icculus@2049
  1001
static int
slouken@2060
  1002
prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
icculus@2049
  1003
{
slouken@2060
  1004
    SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
icculus@2049
  1005
icculus@2049
  1006
    if (orig->freq == 0) {
icculus@2049
  1007
        const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
slouken@2060
  1008
        if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
slouken@2060
  1009
            prepared->freq = 22050;     /* a reasonable default */
slouken@1895
  1010
        }
slouken@1895
  1011
    }
icculus@2049
  1012
icculus@2049
  1013
    if (orig->format == 0) {
icculus@2049
  1014
        const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
icculus@2049
  1015
        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
slouken@2060
  1016
            prepared->format = AUDIO_S16;       /* a reasonable default */
slouken@1895
  1017
        }
slouken@1895
  1018
    }
icculus@2049
  1019
icculus@2049
  1020
    switch (orig->channels) {
slouken@2060
  1021
    case 0:{
slouken@2060
  1022
            const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
slouken@2141
  1023
            if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
slouken@2060
  1024
                prepared->channels = 2; /* a reasonable default */
slouken@2060
  1025
            }
slouken@2060
  1026
            break;
icculus@2049
  1027
        }
slouken@1895
  1028
    case 1:                    /* Mono */
slouken@1895
  1029
    case 2:                    /* Stereo */
slouken@1895
  1030
    case 4:                    /* surround */
slouken@1895
  1031
    case 6:                    /* surround with center and lfe */
slouken@1895
  1032
        break;
slouken@1895
  1033
    default:
icculus@2049
  1034
        SDL_SetError("Unsupported number of audio channels.");
icculus@2049
  1035
        return 0;
slouken@1895
  1036
    }
icculus@2049
  1037
icculus@2049
  1038
    if (orig->samples == 0) {
icculus@2049
  1039
        const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
slouken@2060
  1040
        if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
icculus@2049
  1041
            /* Pick a default of ~46 ms at desired frequency */
icculus@2049
  1042
            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
icculus@2049
  1043
            const int samples = (prepared->freq / 1000) * 46;
icculus@2049
  1044
            int power2 = 1;
icculus@2049
  1045
            while (power2 < samples) {
icculus@2049
  1046
                power2 *= 2;
icculus@2049
  1047
            }
icculus@2049
  1048
            prepared->samples = power2;
slouken@1895
  1049
        }
slouken@1895
  1050
    }
slouken@0
  1051
slouken@1895
  1052
    /* Calculate the silence and size of the audio specification */
icculus@2049
  1053
    SDL_CalculateAudioSpec(prepared);
slouken@21
  1054
icculus@2049
  1055
    return 1;
icculus@2049
  1056
}
slouken@1408
  1057
icculus@2049
  1058
static SDL_AudioDeviceID
icculus@2049
  1059
open_audio_device(const char *devname, int iscapture,
slouken@2866
  1060
                  const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1061
                  int allowed_changes, int min_id)
icculus@2049
  1062
{
icculus@2049
  1063
    SDL_AudioDeviceID id = 0;
slouken@2866
  1064
    SDL_AudioSpec _obtained;
icculus@2049
  1065
    SDL_AudioDevice *device;
slouken@2866
  1066
    SDL_bool build_cvt;
icculus@9394
  1067
    void *handle = NULL;
icculus@9404
  1068
    Uint32 stream_len;
icculus@2049
  1069
    int i = 0;
slouken@21
  1070
icculus@2049
  1071
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1072
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
  1073
        return 0;
icculus@2049
  1074
    }
icculus@2049
  1075
icculus@2049
  1076
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
  1077
        SDL_SetError("No capture support");
icculus@2049
  1078
        return 0;
icculus@2049
  1079
    }
icculus@2049
  1080
icculus@9393
  1081
    /* Find an available device ID... */
icculus@9393
  1082
    for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
icculus@9393
  1083
        if (open_devices[id] == NULL) {
icculus@9393
  1084
            break;
icculus@9393
  1085
        }
icculus@9393
  1086
    }
icculus@9393
  1087
icculus@9393
  1088
    if (id == SDL_arraysize(open_devices)) {
icculus@9393
  1089
        SDL_SetError("Too many open audio devices");
icculus@9393
  1090
        return 0;
icculus@9393
  1091
    }
icculus@9393
  1092
slouken@2866
  1093
    if (!obtained) {
slouken@2866
  1094
        obtained = &_obtained;
slouken@2866
  1095
    }
slouken@2866
  1096
    if (!prepare_audiospec(desired, obtained)) {
icculus@2049
  1097
        return 0;
icculus@2049
  1098
    }
icculus@2049
  1099
icculus@2049
  1100
    /* If app doesn't care about a specific device, let the user override. */
icculus@2049
  1101
    if (devname == NULL) {
icculus@2049
  1102
        devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
slouken@1895
  1103
    }
slouken@21
  1104
icculus@2049
  1105
    /*
icculus@2049
  1106
     * Catch device names at the high level for the simple case...
icculus@2049
  1107
     * This lets us have a basic "device enumeration" for systems that
icculus@2049
  1108
     *  don't have multiple devices, but makes sure the device name is
icculus@2049
  1109
     *  always NULL when it hits the low level.
icculus@2049
  1110
     *
icculus@2049
  1111
     * Also make sure that the simple case prevents multiple simultaneous
icculus@2049
  1112
     *  opens of the default system device.
icculus@2049
  1113
     */
icculus@2049
  1114
icculus@2049
  1115
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
  1116
        if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
icculus@2049
  1117
            SDL_SetError("No such device");
icculus@2049
  1118
            return 0;
icculus@2049
  1119
        }
icculus@2049
  1120
        devname = NULL;
icculus@2049
  1121
icculus@2049
  1122
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1123
            if ((open_devices[i]) && (open_devices[i]->iscapture)) {
icculus@2049
  1124
                SDL_SetError("Audio device already open");
icculus@2049
  1125
                return 0;
icculus@2049
  1126
            }
icculus@2049
  1127
        }
icculus@9394
  1128
    } else if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
  1129
        if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
icculus@2049
  1130
            SDL_SetError("No such device");
icculus@2049
  1131
            return 0;
icculus@2049
  1132
        }
icculus@2049
  1133
        devname = NULL;
icculus@2049
  1134
icculus@2049
  1135
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1136
            if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
icculus@2049
  1137
                SDL_SetError("Audio device already open");
icculus@2049
  1138
                return 0;
icculus@2049
  1139
            }
icculus@2049
  1140
        }
icculus@9394
  1141
    } else if (devname != NULL) {
icculus@9394
  1142
        /* if the app specifies an exact string, we can pass the backend
icculus@9394
  1143
           an actual device handle thingey, which saves them the effort of
icculus@9394
  1144
           figuring out what device this was (such as, reenumerating
icculus@9394
  1145
           everything again to find the matching human-readable name).
icculus@9394
  1146
           It might still need to open a device based on the string for,
icculus@9394
  1147
           say, a network audio server, but this optimizes some cases. */
icculus@9394
  1148
        SDL_AudioDeviceItem *item;
icculus@9394
  1149
        SDL_LockMutex(current_audio.detectionLock);
icculus@9394
  1150
        for (item = iscapture ? current_audio.inputDevices : current_audio.outputDevices; item; item = item->next) {
icculus@9394
  1151
            if ((item->handle != NULL) && (SDL_strcmp(item->name, devname) == 0)) {
icculus@9394
  1152
                handle = item->handle;
icculus@9394
  1153
                break;
icculus@9394
  1154
            }
icculus@9394
  1155
        }
icculus@9394
  1156
        SDL_UnlockMutex(current_audio.detectionLock);
icculus@9394
  1157
    }
icculus@9394
  1158
icculus@9394
  1159
    if (!current_audio.impl.AllowsArbitraryDeviceNames) {
icculus@9394
  1160
        /* has to be in our device list, or the default device. */
icculus@9394
  1161
        if ((handle == NULL) && (devname != NULL)) {
icculus@9394
  1162
            SDL_SetError("No such device.");
icculus@9394
  1163
            return 0;
icculus@9394
  1164
        }
icculus@2049
  1165
    }
icculus@2049
  1166
slouken@2060
  1167
    device = (SDL_AudioDevice *) SDL_AllocAudioMem(sizeof(SDL_AudioDevice));
icculus@2049
  1168
    if (device == NULL) {
icculus@2049
  1169
        SDL_OutOfMemory();
icculus@2049
  1170
        return 0;
icculus@2049
  1171
    }
icculus@9012
  1172
    SDL_zerop(device);
icculus@9393
  1173
    device->id = id + 1;
slouken@2866
  1174
    device->spec = *obtained;
icculus@10235
  1175
    device->iscapture = iscapture ? SDL_TRUE : SDL_FALSE;
icculus@2049
  1176
icculus@10238
  1177
    SDL_AtomicSet(&device->shutdown, 0);  /* just in case. */
icculus@10238
  1178
    SDL_AtomicSet(&device->paused, 1);
icculus@10238
  1179
    SDL_AtomicSet(&device->enabled, 1);
icculus@10238
  1180
icculus@9391
  1181
    /* Create a mutex for locking the sound buffers */
icculus@2049
  1182
    if (!current_audio.impl.SkipMixerLock) {
icculus@2049
  1183
        device->mixer_lock = SDL_CreateMutex();
icculus@2049
  1184
        if (device->mixer_lock == NULL) {
icculus@2049
  1185
            close_audio_device(device);
icculus@2049
  1186
            SDL_SetError("Couldn't create mixer lock");
icculus@2049
  1187
            return 0;
icculus@2049
  1188
        }
icculus@2049
  1189
    }
icculus@2049
  1190
icculus@9394
  1191
    if (current_audio.impl.OpenDevice(device, handle, devname, iscapture) < 0) {
icculus@2049
  1192
        close_audio_device(device);
icculus@2049
  1193
        return 0;
icculus@2049
  1194
    }
icculus@10235
  1195
    device->opened = SDL_TRUE;
slouken@0
  1196
slouken@1895
  1197
    /* See if we need to do any conversion */
slouken@2866
  1198
    build_cvt = SDL_FALSE;
slouken@2866
  1199
    if (obtained->freq != device->spec.freq) {
slouken@2866
  1200
        if (allowed_changes & SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) {
slouken@2866
  1201
            obtained->freq = device->spec.freq;
slouken@2866
  1202
        } else {
slouken@2866
  1203
            build_cvt = SDL_TRUE;
slouken@2866
  1204
        }
slouken@2866
  1205
    }
slouken@2866
  1206
    if (obtained->format != device->spec.format) {
slouken@2866
  1207
        if (allowed_changes & SDL_AUDIO_ALLOW_FORMAT_CHANGE) {
slouken@2866
  1208
            obtained->format = device->spec.format;
slouken@2866
  1209
        } else {
slouken@2866
  1210
            build_cvt = SDL_TRUE;
slouken@2866
  1211
        }
slouken@2866
  1212
    }
slouken@2866
  1213
    if (obtained->channels != device->spec.channels) {
slouken@2866
  1214
        if (allowed_changes & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) {
slouken@2866
  1215
            obtained->channels = device->spec.channels;
slouken@2866
  1216
        } else {
slouken@2866
  1217
            build_cvt = SDL_TRUE;
slouken@2866
  1218
        }
slouken@2866
  1219
    }
slouken@7518
  1220
slouken@7518
  1221
    /* If the audio driver changes the buffer size, accept it.
slouken@7518
  1222
       This needs to be done after the format is modified above,
slouken@7518
  1223
       otherwise it might not have the correct buffer size.
slouken@7518
  1224
     */
slouken@7518
  1225
    if (device->spec.samples != obtained->samples) {
slouken@7518
  1226
        obtained->samples = device->spec.samples;
slouken@7518
  1227
        SDL_CalculateAudioSpec(obtained);
slouken@7518
  1228
    }
slouken@7518
  1229
slouken@2866
  1230
    if (build_cvt) {
slouken@1895
  1231
        /* Build an audio conversion block */
icculus@2049
  1232
        if (SDL_BuildAudioCVT(&device->convert,
slouken@2866
  1233
                              obtained->format, obtained->channels,
slouken@2866
  1234
                              obtained->freq,
icculus@2049
  1235
                              device->spec.format, device->spec.channels,
icculus@2049
  1236
                              device->spec.freq) < 0) {
icculus@2049
  1237
            close_audio_device(device);
icculus@2049
  1238
            return 0;
slouken@1895
  1239
        }
icculus@2049
  1240
        if (device->convert.needed) {
slouken@7518
  1241
            device->convert.len = (int) (((double) device->spec.size) /
slouken@2060
  1242
                                         device->convert.len_ratio);
icculus@2053
  1243
icculus@2049
  1244
            device->convert.buf =
icculus@2049
  1245
                (Uint8 *) SDL_AllocAudioMem(device->convert.len *
icculus@2049
  1246
                                            device->convert.len_mult);
icculus@2049
  1247
            if (device->convert.buf == NULL) {
icculus@2049
  1248
                close_audio_device(device);
slouken@1895
  1249
                SDL_OutOfMemory();
icculus@2049
  1250
                return 0;
slouken@1895
  1251
            }
slouken@1895
  1252
        }
slouken@1895
  1253
    }
icculus@2049
  1254
icculus@9397
  1255
    /* Allocate a fake audio memory buffer */
icculus@9397
  1256
    stream_len = (device->convert.needed) ? device->convert.len_cvt : 0;
icculus@9397
  1257
    if (device->spec.size > stream_len) {
icculus@9397
  1258
        stream_len = device->spec.size;
icculus@9397
  1259
    }
icculus@9405
  1260
    SDL_assert(stream_len > 0);
icculus@9397
  1261
    device->fake_stream = (Uint8 *)SDL_AllocAudioMem(stream_len);
icculus@9397
  1262
    if (device->fake_stream == NULL) {
icculus@9397
  1263
        close_audio_device(device);
icculus@9397
  1264
        SDL_OutOfMemory();
icculus@9397
  1265
        return 0;
icculus@9397
  1266
    }
icculus@9397
  1267
icculus@9012
  1268
    if (device->spec.callback == NULL) {  /* use buffer queueing? */
icculus@9012
  1269
        /* pool a few packets to start. Enough for two callbacks. */
icculus@9012
  1270
        const int packetlen = SDL_AUDIOBUFFERQUEUE_PACKETLEN;
icculus@9012
  1271
        const int wantbytes = ((device->convert.needed) ? device->convert.len : device->spec.size) * 2;
icculus@9012
  1272
        const int wantpackets = (wantbytes / packetlen) + ((wantbytes % packetlen) ? packetlen : 0);
icculus@9012
  1273
        for (i = 0; i < wantpackets; i++) {
icculus@9012
  1274
            SDL_AudioBufferQueue *packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
icculus@9012
  1275
            if (packet) { /* don't care if this fails, we'll deal later. */
icculus@9012
  1276
                packet->datalen = 0;
icculus@9012
  1277
                packet->startpos = 0;
icculus@9012
  1278
                packet->next = device->buffer_queue_pool;
icculus@9012
  1279
                device->buffer_queue_pool = packet;
icculus@9012
  1280
            }
icculus@9012
  1281
        }
icculus@9012
  1282
icculus@9012
  1283
        device->spec.callback = SDL_BufferQueueDrainCallback;
icculus@9012
  1284
        device->spec.userdata = device;
icculus@9012
  1285
    }
icculus@9012
  1286
icculus@9393
  1287
    /* add it to our list of open devices. */
icculus@9393
  1288
    open_devices[id] = device;
icculus@2049
  1289
slouken@1895
  1290
    /* Start the audio thread if necessary */
icculus@2049
  1291
    if (!current_audio.impl.ProvidesOwnCallbackThread) {
slouken@1895
  1292
        /* Start the audio thread */
icculus@10146
  1293
icculus@10146
  1294
        /* !!! FIXME: we don't force the audio thread stack size here because it calls into user code, but maybe we should? */
icculus@10146
  1295
        /* buffer queueing callback only needs a few bytes, so make the stack tiny. */
icculus@5969
  1296
        char name[64];
icculus@10239
  1297
        const SDL_bool is_internal_thread = (device->spec.callback == SDL_BufferQueueDrainCallback);
icculus@10239
  1298
        const size_t stacksize = is_internal_thread ? 64 * 1024 : 0;
icculus@10146
  1299
icculus@9393
  1300
        SDL_snprintf(name, sizeof (name), "SDLAudioDev%d", (int) device->id);
icculus@10239
  1301
        device->thread = SDL_CreateThreadInternal(iscapture ? SDL_CaptureAudio : SDL_RunAudio, name, stacksize, device);
icculus@10146
  1302
icculus@2049
  1303
        if (device->thread == NULL) {
icculus@9393
  1304
            SDL_CloseAudioDevice(device->id);
slouken@1895
  1305
            SDL_SetError("Couldn't create audio thread");
icculus@2049
  1306
            return 0;
icculus@2049
  1307
        }
icculus@2049
  1308
    }
icculus@2049
  1309
icculus@9393
  1310
    return device->id;
icculus@2049
  1311
}
icculus@2049
  1312
icculus@2049
  1313
icculus@2049
  1314
int
slouken@2866
  1315
SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
icculus@2049
  1316
{
icculus@2049
  1317
    SDL_AudioDeviceID id = 0;
icculus@2049
  1318
icculus@2049
  1319
    /* Start up the audio driver, if necessary. This is legacy behaviour! */
icculus@2049
  1320
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1321
        if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
icculus@9382
  1322
            return -1;
slouken@1895
  1323
        }
icculus@2049
  1324
    }
slouken@0
  1325
icculus@2049
  1326
    /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
icculus@2049
  1327
    if (open_devices[0] != NULL) {
icculus@2049
  1328
        SDL_SetError("Audio device is already opened");
icculus@9382
  1329
        return -1;
slouken@1895
  1330
    }
icculus@2049
  1331
slouken@2866
  1332
    if (obtained) {
slouken@2866
  1333
        id = open_audio_device(NULL, 0, desired, obtained,
slouken@2866
  1334
                               SDL_AUDIO_ALLOW_ANY_CHANGE, 1);
slouken@2866
  1335
    } else {
slouken@8919
  1336
        id = open_audio_device(NULL, 0, desired, NULL, 0, 1);
slouken@2866
  1337
    }
slouken@21
  1338
icculus@5964
  1339
    SDL_assert((id == 0) || (id == 1));
icculus@9382
  1340
    return (id == 0) ? -1 : 0;
icculus@2049
  1341
}
slouken@21
  1342
icculus@2049
  1343
SDL_AudioDeviceID
icculus@2049
  1344
SDL_OpenAudioDevice(const char *device, int iscapture,
slouken@2866
  1345
                    const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1346
                    int allowed_changes)
icculus@2049
  1347
{
slouken@2866
  1348
    return open_audio_device(device, iscapture, desired, obtained,
slouken@2866
  1349
                             allowed_changes, 2);
slouken@1895
  1350
}
slouken@1895
  1351
slouken@3537
  1352
SDL_AudioStatus
icculus@2049
  1353
SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
slouken@1895
  1354
{
icculus@2049
  1355
    SDL_AudioDevice *device = get_audio_device(devid);
slouken@3537
  1356
    SDL_AudioStatus status = SDL_AUDIO_STOPPED;
icculus@10238
  1357
    if (device && SDL_AtomicGet(&device->enabled)) {
icculus@10238
  1358
        if (SDL_AtomicGet(&device->paused)) {
slouken@1895
  1359
            status = SDL_AUDIO_PAUSED;
slouken@1895
  1360
        } else {
slouken@1895
  1361
            status = SDL_AUDIO_PLAYING;
slouken@1895
  1362
        }
slouken@1895
  1363
    }
icculus@9382
  1364
    return status;
slouken@0
  1365
}
slouken@0
  1366
icculus@2049
  1367
slouken@3537
  1368
SDL_AudioStatus
icculus@2049
  1369
SDL_GetAudioStatus(void)
icculus@2049
  1370
{
icculus@2049
  1371
    return SDL_GetAudioDeviceStatus(1);
icculus@2049
  1372
}
icculus@2049
  1373
icculus@2049
  1374
void
icculus@2049
  1375
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
icculus@2049
  1376
{
icculus@2049
  1377
    SDL_AudioDevice *device = get_audio_device(devid);
gabomdq@9148
  1378
    if (device) {
gabomdq@9148
  1379
        current_audio.impl.LockDevice(device);
icculus@10238
  1380
        SDL_AtomicSet(&device->paused, pause_on ? 1 : 0);
gabomdq@9148
  1381
        current_audio.impl.UnlockDevice(device);
icculus@2049
  1382
    }
icculus@2049
  1383
}
icculus@2049
  1384
slouken@1895
  1385
void
slouken@1895
  1386
SDL_PauseAudio(int pause_on)
slouken@0
  1387
{
gabomdq@9148
  1388
    SDL_PauseAudioDevice(1, pause_on);
icculus@2049
  1389
}
icculus@2049
  1390
slouken@0
  1391
icculus@2049
  1392
void
icculus@2049
  1393
SDL_LockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1394
{
icculus@2049
  1395
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1396
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1397
    if (device) {
icculus@2049
  1398
        current_audio.impl.LockDevice(device);
slouken@1895
  1399
    }
slouken@0
  1400
}
slouken@0
  1401
slouken@1895
  1402
void
slouken@1895
  1403
SDL_LockAudio(void)
slouken@0
  1404
{
icculus@2049
  1405
    SDL_LockAudioDevice(1);
icculus@2049
  1406
}
slouken@0
  1407
icculus@2049
  1408
void
icculus@2049
  1409
SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1410
{
slouken@1895
  1411
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1412
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1413
    if (device) {
icculus@2049
  1414
        current_audio.impl.UnlockDevice(device);
slouken@1895
  1415
    }
slouken@0
  1416
}
slouken@0
  1417
slouken@1895
  1418
void
slouken@1895
  1419
SDL_UnlockAudio(void)
slouken@0
  1420
{
icculus@2049
  1421
    SDL_UnlockAudioDevice(1);
icculus@2049
  1422
}
slouken@0
  1423
icculus@2049
  1424
void
icculus@2049
  1425
SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1426
{
icculus@2049
  1427
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1428
    if (device) {
icculus@2049
  1429
        close_audio_device(device);
slouken@2060
  1430
        open_devices[devid - 1] = NULL;
slouken@1895
  1431
    }
slouken@0
  1432
}
slouken@0
  1433
slouken@1895
  1434
void
slouken@1895
  1435
SDL_CloseAudio(void)
slouken@0
  1436
{
icculus@2049
  1437
    SDL_CloseAudioDevice(1);
slouken@0
  1438
}
slouken@0
  1439
slouken@1895
  1440
void
slouken@1895
  1441
SDL_AudioQuit(void)
slouken@0
  1442
{
icculus@7348
  1443
    SDL_AudioDeviceID i;
icculus@7348
  1444
icculus@7345
  1445
    if (!current_audio.name) {  /* not initialized?! */
icculus@7345
  1446
        return;
icculus@7345
  1447
    }
icculus@7345
  1448
icculus@2049
  1449
    for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@7342
  1450
        if (open_devices[i] != NULL) {
icculus@7446
  1451
            SDL_CloseAudioDevice(i+1);
icculus@7342
  1452
        }
icculus@2049
  1453
    }
slouken@0
  1454
icculus@9394
  1455
    free_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount);
icculus@9394
  1456
    free_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount);
icculus@9394
  1457
icculus@2049
  1458
    /* Free the driver data */
icculus@2049
  1459
    current_audio.impl.Deinitialize();
icculus@9393
  1460
icculus@9394
  1461
    SDL_DestroyMutex(current_audio.detectionLock);
icculus@9393
  1462
icculus@9393
  1463
    SDL_zero(current_audio);
icculus@9393
  1464
    SDL_zero(open_devices);
slouken@0
  1465
}
slouken@0
  1466
icculus@1982
  1467
#define NUM_FORMATS 10
slouken@0
  1468
static int format_idx;
slouken@0
  1469
static int format_idx_sub;
icculus@1982
  1470
static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
slouken@1895
  1471
    {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1472
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
slouken@1895
  1473
    {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1474
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
icculus@1982
  1475
    {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
icculus@1982
  1476
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1477
    {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
icculus@1982
  1478
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1479
    {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
icculus@1982
  1480
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1481
    {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
icculus@1982
  1482
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1483
    {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
icculus@1993
  1484
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1485
    {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
icculus@1993
  1486
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1487
    {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
icculus@1993
  1488
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1489
    {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
icculus@1993
  1490
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
slouken@0
  1491
};
slouken@0
  1492
icculus@1982
  1493
SDL_AudioFormat
icculus@1982
  1494
SDL_FirstAudioFormat(SDL_AudioFormat format)
slouken@0
  1495
{
slouken@1895
  1496
    for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
slouken@1895
  1497
        if (format_list[format_idx][0] == format) {
slouken@1895
  1498
            break;
slouken@1895
  1499
        }
slouken@1895
  1500
    }
slouken@1895
  1501
    format_idx_sub = 0;
icculus@9382
  1502
    return SDL_NextAudioFormat();
slouken@0
  1503
}
slouken@0
  1504
icculus@1982
  1505
SDL_AudioFormat
slouken@1895
  1506
SDL_NextAudioFormat(void)
slouken@0
  1507
{
slouken@1895
  1508
    if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
icculus@9382
  1509
        return 0;
slouken@1895
  1510
    }
icculus@9382
  1511
    return format_list[format_idx][format_idx_sub++];
slouken@0
  1512
}
slouken@0
  1513
slouken@1895
  1514
void
slouken@1895
  1515
SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
slouken@0
  1516
{
slouken@1895
  1517
    switch (spec->format) {
slouken@1895
  1518
    case AUDIO_U8:
slouken@1895
  1519
        spec->silence = 0x80;
slouken@1895
  1520
        break;
slouken@1895
  1521
    default:
slouken@1895
  1522
        spec->silence = 0x00;
slouken@1895
  1523
        break;
slouken@1895
  1524
    }
icculus@2049
  1525
    spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
slouken@1895
  1526
    spec->size *= spec->channels;
slouken@1895
  1527
    spec->size *= spec->samples;
slouken@0
  1528
}
slouken@1895
  1529
icculus@2049
  1530
icculus@2049
  1531
/*
icculus@2049
  1532
 * Moved here from SDL_mixer.c, since it relies on internals of an opened
icculus@2049
  1533
 *  audio device (and is deprecated, by the way!).
icculus@2049
  1534
 */
icculus@2049
  1535
void
icculus@2049
  1536
SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
icculus@2049
  1537
{
icculus@2049
  1538
    /* Mix the user-level audio format */
icculus@2049
  1539
    SDL_AudioDevice *device = get_audio_device(1);
icculus@2049
  1540
    if (device != NULL) {
icculus@2049
  1541
        SDL_AudioFormat format;
icculus@2049
  1542
        if (device->convert.needed) {
icculus@2049
  1543
            format = device->convert.src_format;
icculus@2049
  1544
        } else {
icculus@2049
  1545
            format = device->spec.format;
icculus@2049
  1546
        }
icculus@2049
  1547
        SDL_MixAudioFormat(dst, src, format, len, volume);
icculus@2049
  1548
    }
icculus@2049
  1549
}
icculus@2049
  1550
slouken@1895
  1551
/* vi: set ts=4 sw=4 expandtab: */