src/audio/SDL_audio.c
author Sam Lantinga
Tue, 04 Oct 2016 06:48:07 -0700
changeset 10467 be888d570cc8
parent 10466 b7267e214a77
child 10468 9f14a7dfaa6a
permissions -rw-r--r--
ensure SDL_AUDIODEVICEREMOVED gets sent when hotplug removes a device

James Zipperer

The problem I was seeing was that the the ALSA hotplug thread would call SDL_RemoveAudioDevice, but my application code was not seeing an SDL_AUDIODEVICEREMOVED event to go along with it. To fix it, I added some code into SDL_RemoveAudioDevice to call SDL_OpenedAudioDeviceDisconnected on the corresponding open audio device. There didn't appear to be a way to cross reference the handle that SDL_RemoveAudioDevice gets and the SDL_AudioDevice pointer that SDL_OpenedAudioDeviceDisconnected needs, so I ended up adding a void *handle field to struct SDL_AudioDevice so that I could do the cross reference.

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