src/audio/wasapi/SDL_wasapi_win32.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 21 Oct 2018 22:40:17 -0400
changeset 12345 50e1cca28b39
parent 12222 09e314140a28
child 12503 806492103856
permissions -rw-r--r--
wasapi/win32: Sort initial device lists by device GUID.

This makes an unchanged set of hardware always report devices in the same
order on each run.
icculus@11726
     1
/*
icculus@11726
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
icculus@11726
     4
icculus@11726
     5
  This software is provided 'as-is', without any express or implied
icculus@11726
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@11726
     7
  arising from the use of this software.
icculus@11726
     8
icculus@11726
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@11726
    10
  including commercial applications, and to alter it and redistribute it
icculus@11726
    11
  freely, subject to the following restrictions:
icculus@11726
    12
icculus@11726
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@11726
    14
     claim that you wrote the original software. If you use this software
icculus@11726
    15
     in a product, an acknowledgment in the product documentation would be
icculus@11726
    16
     appreciated but is not required.
icculus@11726
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@11726
    18
     misrepresented as being the original software.
icculus@11726
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@11726
    20
*/
icculus@11726
    21
icculus@11726
    22
#include "../../SDL_internal.h"
icculus@11726
    23
icculus@11726
    24
/* This is code that Windows uses to talk to WASAPI-related system APIs.
icculus@11726
    25
   This is for non-WinRT desktop apps. The C++/CX implementation of these
icculus@11726
    26
   functions, exclusive to WinRT, are in SDL_wasapi_winrt.cpp.
icculus@11726
    27
   The code in SDL_wasapi.c is used by both standard Windows and WinRT builds
icculus@11726
    28
   to deal with audio and calls into these functions. */
icculus@11726
    29
icculus@11726
    30
#if SDL_AUDIO_DRIVER_WASAPI && !defined(__WINRT__)
icculus@11726
    31
icculus@11726
    32
#include "../../core/windows/SDL_windows.h"
icculus@11726
    33
#include "SDL_audio.h"
icculus@11726
    34
#include "SDL_timer.h"
icculus@11726
    35
#include "../SDL_audio_c.h"
icculus@11726
    36
#include "../SDL_sysaudio.h"
icculus@11726
    37
#include "SDL_assert.h"
icculus@11726
    38
#include "SDL_log.h"
icculus@11726
    39
icculus@11726
    40
#define COBJMACROS
icculus@11726
    41
#include <mmdeviceapi.h>
icculus@11726
    42
#include <audioclient.h>
icculus@11726
    43
icculus@11726
    44
#include "SDL_wasapi.h"
icculus@11726
    45
icculus@11726
    46
static const ERole SDL_WASAPI_role = eConsole;  /* !!! FIXME: should this be eMultimedia? Should be a hint? */
icculus@11726
    47
icculus@11726
    48
/* This is global to the WASAPI target, to handle hotplug and default device lookup. */
icculus@11726
    49
static IMMDeviceEnumerator *enumerator = NULL;
icculus@11726
    50
icculus@11726
    51
/* PropVariantInit() is an inline function/macro in PropIdl.h that calls the C runtime's memset() directly. Use ours instead, to avoid dependency. */
icculus@11726
    52
#ifdef PropVariantInit
icculus@11726
    53
#undef PropVariantInit
icculus@11726
    54
#endif
icculus@11726
    55
#define PropVariantInit(p) SDL_zerop(p)
icculus@11726
    56
icculus@11726
    57
/* handle to Avrt.dll--Vista and later!--for flagging the callback thread as "Pro Audio" (low latency). */
icculus@11726
    58
static HMODULE libavrt = NULL;
icculus@11726
    59
typedef HANDLE(WINAPI *pfnAvSetMmThreadCharacteristicsW)(LPWSTR, LPDWORD);
icculus@11726
    60
typedef BOOL(WINAPI *pfnAvRevertMmThreadCharacteristics)(HANDLE);
icculus@11726
    61
static pfnAvSetMmThreadCharacteristicsW pAvSetMmThreadCharacteristicsW = NULL;
icculus@11726
    62
static pfnAvRevertMmThreadCharacteristics pAvRevertMmThreadCharacteristics = NULL;
icculus@11726
    63
icculus@11726
    64
/* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */
icculus@11726
    65
static const CLSID SDL_CLSID_MMDeviceEnumerator = { 0xbcde0395, 0xe52f, 0x467c,{ 0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e } };
icculus@11726
    66
static const IID SDL_IID_IMMDeviceEnumerator = { 0xa95664d2, 0x9614, 0x4f35,{ 0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6 } };
icculus@11726
    67
static const IID SDL_IID_IMMNotificationClient = { 0x7991eec9, 0x7e89, 0x4d85,{ 0x83, 0x90, 0x6c, 0x70, 0x3c, 0xec, 0x60, 0xc0 } };
icculus@11726
    68
static const IID SDL_IID_IMMEndpoint = { 0x1be09788, 0x6894, 0x4089,{ 0x85, 0x86, 0x9a, 0x2a, 0x6c, 0x26, 0x5a, 0xc5 } };
icculus@11726
    69
static const IID SDL_IID_IAudioClient = { 0x1cb9ad4c, 0xdbfa, 0x4c32,{ 0xb1, 0x78, 0xc2, 0xf5, 0x68, 0xa7, 0x03, 0xb2 } };
icculus@11726
    70
static const PROPERTYKEY SDL_PKEY_Device_FriendlyName = { { 0xa45c254e, 0xdf1c, 0x4efd,{ 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, } }, 14 };
icculus@11726
    71
icculus@11726
    72
icculus@11726
    73
static char *
icculus@11726
    74
GetWasapiDeviceName(IMMDevice *device)
icculus@11726
    75
{
icculus@11726
    76
    /* PKEY_Device_FriendlyName gives you "Speakers (SoundBlaster Pro)" which drives me nuts. I'd rather it be
icculus@11726
    77
       "SoundBlaster Pro (Speakers)" but I guess that's developers vs users. Windows uses the FriendlyName in
icculus@11726
    78
       its own UIs, like Volume Control, etc. */
icculus@11726
    79
    char *utf8dev = NULL;
icculus@11726
    80
    IPropertyStore *props = NULL;
icculus@11726
    81
    if (SUCCEEDED(IMMDevice_OpenPropertyStore(device, STGM_READ, &props))) {
icculus@11726
    82
        PROPVARIANT var;
icculus@11726
    83
        PropVariantInit(&var);
icculus@11726
    84
        if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_Device_FriendlyName, &var))) {
icculus@11726
    85
            utf8dev = WIN_StringToUTF8(var.pwszVal);
icculus@11726
    86
        }
icculus@11726
    87
        PropVariantClear(&var);
icculus@11726
    88
        IPropertyStore_Release(props);
icculus@11726
    89
    }
icculus@11726
    90
    return utf8dev;
icculus@11726
    91
}
icculus@11726
    92
icculus@11726
    93
icculus@11726
    94
/* We need a COM subclass of IMMNotificationClient for hotplug support, which is
icculus@11726
    95
   easy in C++, but we have to tapdance more to make work in C.
icculus@11726
    96
   Thanks to this page for coaching on how to make this work:
icculus@11726
    97
     https://www.codeproject.com/Articles/13601/COM-in-plain-C */
icculus@11726
    98
icculus@11726
    99
typedef struct SDLMMNotificationClient
icculus@11726
   100
{
icculus@11726
   101
    const IMMNotificationClientVtbl *lpVtbl;
icculus@11726
   102
    SDL_atomic_t refcount;
icculus@11726
   103
} SDLMMNotificationClient;
icculus@11726
   104
icculus@11726
   105
static HRESULT STDMETHODCALLTYPE
icculus@11726
   106
SDLMMNotificationClient_QueryInterface(IMMNotificationClient *this, REFIID iid, void **ppv)
icculus@11726
   107
{
icculus@11726
   108
    if ((WIN_IsEqualIID(iid, &IID_IUnknown)) || (WIN_IsEqualIID(iid, &SDL_IID_IMMNotificationClient)))
icculus@11726
   109
    {
icculus@11726
   110
        *ppv = this;
icculus@11726
   111
        this->lpVtbl->AddRef(this);
icculus@11726
   112
        return S_OK;
icculus@11726
   113
    }
icculus@11726
   114
icculus@11726
   115
    *ppv = NULL;
icculus@11726
   116
    return E_NOINTERFACE;
icculus@11726
   117
}
icculus@11726
   118
icculus@11726
   119
static ULONG STDMETHODCALLTYPE
icculus@11726
   120
SDLMMNotificationClient_AddRef(IMMNotificationClient *ithis)
icculus@11726
   121
{
icculus@11726
   122
    SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis;
icculus@11726
   123
    return (ULONG) (SDL_AtomicIncRef(&this->refcount) + 1);
icculus@11726
   124
}
icculus@11726
   125
icculus@11726
   126
static ULONG STDMETHODCALLTYPE
icculus@11726
   127
SDLMMNotificationClient_Release(IMMNotificationClient *ithis)
icculus@11726
   128
{
icculus@11726
   129
    /* this is a static object; we don't ever free it. */
icculus@11726
   130
    SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis;
icculus@11726
   131
    const ULONG retval = SDL_AtomicDecRef(&this->refcount);
icculus@11726
   132
    if (retval == 0) {
icculus@11726
   133
        SDL_AtomicSet(&this->refcount, 0);  /* uhh... */
icculus@11726
   134
        return 0;
icculus@11726
   135
    }
icculus@11726
   136
    return retval - 1;
icculus@11726
   137
}
icculus@11726
   138
icculus@11726
   139
/* These are the entry points called when WASAPI device endpoints change. */
icculus@11726
   140
static HRESULT STDMETHODCALLTYPE
icculus@11726
   141
SDLMMNotificationClient_OnDefaultDeviceChanged(IMMNotificationClient *ithis, EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId)
icculus@11726
   142
{
icculus@11726
   143
    if (role != SDL_WASAPI_role) {
icculus@11726
   144
        return S_OK;  /* ignore it. */
icculus@11726
   145
    }
icculus@11726
   146
icculus@11726
   147
    /* Increment the "generation," so opened devices will pick this up in their threads. */
icculus@11726
   148
    switch (flow) {
icculus@11726
   149
        case eRender:
icculus@11726
   150
            SDL_AtomicAdd(&WASAPI_DefaultPlaybackGeneration, 1);
icculus@11726
   151
            break;
icculus@11726
   152
icculus@11726
   153
        case eCapture:
icculus@11726
   154
            SDL_AtomicAdd(&WASAPI_DefaultCaptureGeneration, 1);
icculus@11726
   155
            break;
icculus@11726
   156
icculus@11726
   157
        case eAll:
icculus@11726
   158
            SDL_AtomicAdd(&WASAPI_DefaultPlaybackGeneration, 1);
icculus@11726
   159
            SDL_AtomicAdd(&WASAPI_DefaultCaptureGeneration, 1);
icculus@11726
   160
            break;
icculus@11726
   161
icculus@11726
   162
        default:
icculus@11726
   163
            SDL_assert(!"uhoh, unexpected OnDefaultDeviceChange flow!");
icculus@11726
   164
            break;
icculus@11726
   165
    }
icculus@11726
   166
icculus@11726
   167
    return S_OK;
icculus@11726
   168
}
icculus@11726
   169
icculus@11726
   170
static HRESULT STDMETHODCALLTYPE
icculus@11726
   171
SDLMMNotificationClient_OnDeviceAdded(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
icculus@11726
   172
{
icculus@11726
   173
    /* we ignore this; devices added here then progress to ACTIVE, if appropriate, in 
icculus@11726
   174
       OnDeviceStateChange, making that a better place to deal with device adds. More 
icculus@11726
   175
       importantly: the first time you plug in a USB audio device, this callback will 
icculus@11726
   176
       fire, but when you unplug it, it isn't removed (it's state changes to NOTPRESENT).
icculus@11726
   177
       Plugging it back in won't fire this callback again. */
icculus@11726
   178
    return S_OK;
icculus@11726
   179
}
icculus@11726
   180
icculus@11726
   181
static HRESULT STDMETHODCALLTYPE
icculus@11726
   182
SDLMMNotificationClient_OnDeviceRemoved(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
icculus@11726
   183
{
icculus@11726
   184
    /* See notes in OnDeviceAdded handler about why we ignore this. */
icculus@11726
   185
    return S_OK;
icculus@11726
   186
}
icculus@11726
   187
icculus@11726
   188
static HRESULT STDMETHODCALLTYPE
icculus@11726
   189
SDLMMNotificationClient_OnDeviceStateChanged(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId, DWORD dwNewState)
icculus@11726
   190
{
icculus@11726
   191
    IMMDevice *device = NULL;
icculus@11726
   192
icculus@11726
   193
    if (SUCCEEDED(IMMDeviceEnumerator_GetDevice(enumerator, pwstrDeviceId, &device))) {
icculus@11726
   194
        IMMEndpoint *endpoint = NULL;
icculus@11726
   195
        if (SUCCEEDED(IMMDevice_QueryInterface(device, &SDL_IID_IMMEndpoint, (void **) &endpoint))) {
icculus@11726
   196
            EDataFlow flow;
icculus@11726
   197
            if (SUCCEEDED(IMMEndpoint_GetDataFlow(endpoint, &flow))) {
icculus@11726
   198
                const SDL_bool iscapture = (flow == eCapture);
icculus@11726
   199
                if (dwNewState == DEVICE_STATE_ACTIVE) {
icculus@11726
   200
                    char *utf8dev = GetWasapiDeviceName(device);
icculus@11726
   201
                    if (utf8dev) {
icculus@11726
   202
                        WASAPI_AddDevice(iscapture, utf8dev, pwstrDeviceId);
icculus@11726
   203
                        SDL_free(utf8dev);
icculus@11726
   204
                    }
icculus@11726
   205
                } else {
icculus@11726
   206
                    WASAPI_RemoveDevice(iscapture, pwstrDeviceId);
icculus@11726
   207
                }
icculus@11726
   208
            }
icculus@11726
   209
            IMMEndpoint_Release(endpoint);
icculus@11726
   210
        }
icculus@11726
   211
        IMMDevice_Release(device);
icculus@11726
   212
    }
icculus@11726
   213
icculus@11726
   214
    return S_OK;
icculus@11726
   215
}
icculus@11726
   216
icculus@11726
   217
static HRESULT STDMETHODCALLTYPE
icculus@11726
   218
SDLMMNotificationClient_OnPropertyValueChanged(IMMNotificationClient *this, LPCWSTR pwstrDeviceId, const PROPERTYKEY key)
icculus@11726
   219
{
icculus@11726
   220
    return S_OK;  /* we don't care about these. */
icculus@11726
   221
}
icculus@11726
   222
icculus@11726
   223
static const IMMNotificationClientVtbl notification_client_vtbl = {
icculus@11726
   224
    SDLMMNotificationClient_QueryInterface,
icculus@11726
   225
    SDLMMNotificationClient_AddRef,
icculus@11726
   226
    SDLMMNotificationClient_Release,
icculus@11726
   227
    SDLMMNotificationClient_OnDeviceStateChanged,
icculus@11726
   228
    SDLMMNotificationClient_OnDeviceAdded,
icculus@11726
   229
    SDLMMNotificationClient_OnDeviceRemoved,
icculus@11726
   230
    SDLMMNotificationClient_OnDefaultDeviceChanged,
icculus@11726
   231
    SDLMMNotificationClient_OnPropertyValueChanged
icculus@11726
   232
};
icculus@11726
   233
icculus@11832
   234
static SDLMMNotificationClient notification_client = { &notification_client_vtbl, { 1 } };
icculus@11726
   235
icculus@11726
   236
icculus@11726
   237
int
icculus@11726
   238
WASAPI_PlatformInit(void)
icculus@11726
   239
{
icculus@11726
   240
    HRESULT ret;
icculus@11726
   241
icculus@11726
   242
    /* just skip the discussion with COM here. */
icculus@11726
   243
    if (!WIN_IsWindowsVistaOrGreater()) {
icculus@11726
   244
        return SDL_SetError("WASAPI support requires Windows Vista or later");
icculus@11726
   245
    }
icculus@11726
   246
icculus@11726
   247
    if (FAILED(WIN_CoInitialize())) {
icculus@11726
   248
        return SDL_SetError("WASAPI: CoInitialize() failed");
icculus@11726
   249
    }
icculus@11726
   250
icculus@11726
   251
    ret = CoCreateInstance(&SDL_CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &SDL_IID_IMMDeviceEnumerator, (LPVOID) &enumerator);
icculus@11726
   252
    if (FAILED(ret)) {
icculus@11726
   253
        WIN_CoUninitialize();
icculus@11726
   254
        return WIN_SetErrorFromHRESULT("WASAPI CoCreateInstance(MMDeviceEnumerator)", ret);
icculus@11726
   255
    }
icculus@11726
   256
icculus@11726
   257
    libavrt = LoadLibraryW(L"avrt.dll");  /* this library is available in Vista and later. No WinXP, so have to LoadLibrary to use it for now! */
icculus@11726
   258
    if (libavrt) {
sezero@11872
   259
        pAvSetMmThreadCharacteristicsW = (pfnAvSetMmThreadCharacteristicsW) GetProcAddress(libavrt, "AvSetMmThreadCharacteristicsW");
sezero@11872
   260
        pAvRevertMmThreadCharacteristics = (pfnAvRevertMmThreadCharacteristics) GetProcAddress(libavrt, "AvRevertMmThreadCharacteristics");
icculus@11726
   261
    }
icculus@11726
   262
icculus@11726
   263
    return 0;
icculus@11726
   264
}
icculus@11726
   265
icculus@11726
   266
void
icculus@11726
   267
WASAPI_PlatformDeinit(void)
icculus@11726
   268
{
icculus@11726
   269
    if (enumerator) {
icculus@11726
   270
        IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) &notification_client);
icculus@11726
   271
        IMMDeviceEnumerator_Release(enumerator);
icculus@11726
   272
        enumerator = NULL;
icculus@11726
   273
    }
icculus@11726
   274
icculus@11726
   275
    if (libavrt) {
icculus@11726
   276
        FreeLibrary(libavrt);
icculus@11726
   277
        libavrt = NULL;
icculus@11726
   278
    }
icculus@11726
   279
icculus@11726
   280
    pAvSetMmThreadCharacteristicsW = NULL;
icculus@11726
   281
    pAvRevertMmThreadCharacteristics = NULL;
icculus@11726
   282
icculus@11726
   283
    WIN_CoUninitialize();
icculus@11726
   284
}
icculus@11726
   285
icculus@11726
   286
void
icculus@11726
   287
WASAPI_PlatformThreadInit(_THIS)
icculus@11726
   288
{
icculus@11726
   289
    /* this thread uses COM. */
icculus@11726
   290
    if (SUCCEEDED(WIN_CoInitialize())) {    /* can't report errors, hope it worked! */
icculus@11726
   291
        this->hidden->coinitialized = SDL_TRUE;
icculus@11726
   292
    }
icculus@11726
   293
icculus@11726
   294
    /* Set this thread to very high "Pro Audio" priority. */
icculus@11726
   295
    if (pAvSetMmThreadCharacteristicsW) {
icculus@11726
   296
        DWORD idx = 0;
icculus@11726
   297
        this->hidden->task = pAvSetMmThreadCharacteristicsW(TEXT("Pro Audio"), &idx);
icculus@11726
   298
    }
icculus@11726
   299
}
icculus@11726
   300
icculus@11726
   301
void
icculus@11726
   302
WASAPI_PlatformThreadDeinit(_THIS)
icculus@11726
   303
{
icculus@11726
   304
    /* Set this thread back to normal priority. */
icculus@11726
   305
    if (this->hidden->task && pAvRevertMmThreadCharacteristics) {
icculus@11726
   306
        pAvRevertMmThreadCharacteristics(this->hidden->task);
icculus@11726
   307
        this->hidden->task = NULL;
icculus@11726
   308
    }
icculus@11726
   309
icculus@11726
   310
    if (this->hidden->coinitialized) {
icculus@11726
   311
        WIN_CoUninitialize();
icculus@11726
   312
        this->hidden->coinitialized = SDL_FALSE;
icculus@11726
   313
    }
icculus@11726
   314
}
icculus@11726
   315
icculus@11726
   316
int
icculus@11726
   317
WASAPI_ActivateDevice(_THIS, const SDL_bool isrecovery)
icculus@11726
   318
{
icculus@11726
   319
    LPCWSTR devid = this->hidden->devid;
icculus@11726
   320
    IMMDevice *device = NULL;
icculus@11726
   321
    HRESULT ret;
icculus@11726
   322
icculus@11726
   323
    if (devid == NULL) {
icculus@11726
   324
        const EDataFlow dataflow = this->iscapture ? eCapture : eRender;
icculus@11726
   325
        ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_WASAPI_role, &device);
icculus@11726
   326
    } else {
icculus@11726
   327
        ret = IMMDeviceEnumerator_GetDevice(enumerator, devid, &device);
icculus@11726
   328
    }
icculus@11726
   329
icculus@11726
   330
    if (FAILED(ret)) {
icculus@11726
   331
        SDL_assert(device == NULL);
icculus@11726
   332
        this->hidden->client = NULL;
icculus@11726
   333
        return WIN_SetErrorFromHRESULT("WASAPI can't find requested audio endpoint", ret);
icculus@11726
   334
    }
icculus@11726
   335
icculus@11726
   336
    /* this is not async in standard win32, yay! */
icculus@11726
   337
    ret = IMMDevice_Activate(device, &SDL_IID_IAudioClient, CLSCTX_ALL, NULL, (void **) &this->hidden->client);
icculus@11726
   338
    IMMDevice_Release(device);
icculus@11726
   339
icculus@11726
   340
    if (FAILED(ret)) {
icculus@11726
   341
        SDL_assert(this->hidden->client == NULL);
icculus@11726
   342
        return WIN_SetErrorFromHRESULT("WASAPI can't activate audio endpoint", ret);
icculus@11726
   343
    }
icculus@11726
   344
icculus@11726
   345
    SDL_assert(this->hidden->client != NULL);
icculus@11726
   346
    if (WASAPI_PrepDevice(this, isrecovery) == -1) {   /* not async, fire it right away. */
icculus@11726
   347
        return -1;
icculus@11726
   348
    }
icculus@11726
   349
icculus@11726
   350
    return 0;  /* good to go. */
icculus@11726
   351
}
icculus@11726
   352
icculus@11726
   353
icculus@12345
   354
typedef struct
icculus@12345
   355
{
icculus@12345
   356
    LPWSTR devid;
icculus@12345
   357
    char *devname;
icculus@12345
   358
} EndpointItem;
icculus@12345
   359
icculus@12345
   360
static int sort_endpoints(const void *_a, const void *_b)
icculus@12345
   361
{
icculus@12345
   362
    LPWSTR a = ((const EndpointItem *) _a)->devid;
icculus@12345
   363
    LPWSTR b = ((const EndpointItem *) _b)->devid;
icculus@12345
   364
    if (!a && b) {
icculus@12345
   365
        return -1;
icculus@12345
   366
    } else if (a && !b) {
icculus@12345
   367
        return 1;
icculus@12345
   368
    }
icculus@12345
   369
icculus@12345
   370
    while (SDL_TRUE) {
icculus@12345
   371
        if (*a < *b) {
icculus@12345
   372
            return -1;
icculus@12345
   373
        } else if (*a > *b) {
icculus@12345
   374
            return 1;
icculus@12345
   375
        } else if (*a == 0) {
icculus@12345
   376
            break;
icculus@12345
   377
        }
icculus@12345
   378
        a++;
icculus@12345
   379
        b++;
icculus@12345
   380
    }
icculus@12345
   381
icculus@12345
   382
    return 0;
icculus@12345
   383
}
icculus@12345
   384
icculus@11726
   385
static void
icculus@11726
   386
WASAPI_EnumerateEndpointsForFlow(const SDL_bool iscapture)
icculus@11726
   387
{
icculus@11726
   388
    IMMDeviceCollection *collection = NULL;
icculus@12345
   389
    EndpointItem *items;
icculus@11726
   390
    UINT i, total;
icculus@11726
   391
icculus@11726
   392
    /* Note that WASAPI separates "adapter devices" from "audio endpoint devices"
icculus@11726
   393
       ...one adapter device ("SoundBlaster Pro") might have multiple endpoint devices ("Speakers", "Line-Out"). */
icculus@11726
   394
icculus@11726
   395
    if (FAILED(IMMDeviceEnumerator_EnumAudioEndpoints(enumerator, iscapture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &collection))) {
icculus@11726
   396
        return;
icculus@11726
   397
    }
icculus@11726
   398
icculus@11726
   399
    if (FAILED(IMMDeviceCollection_GetCount(collection, &total))) {
icculus@11726
   400
        IMMDeviceCollection_Release(collection);
icculus@11726
   401
        return;
icculus@11726
   402
    }
icculus@11726
   403
icculus@12345
   404
    items = (EndpointItem *) SDL_calloc(total, sizeof (EndpointItem));
icculus@12345
   405
    if (!items) {
icculus@12345
   406
        return;  /* oh well. */
icculus@12345
   407
    }
icculus@12345
   408
icculus@11726
   409
    for (i = 0; i < total; i++) {
icculus@12345
   410
        EndpointItem *item = items + i;
icculus@11726
   411
        IMMDevice *device = NULL;
icculus@11726
   412
        if (SUCCEEDED(IMMDeviceCollection_Item(collection, i, &device))) {
icculus@12345
   413
            if (SUCCEEDED(IMMDevice_GetId(device, &item->devid))) {
icculus@12345
   414
                item->devname = GetWasapiDeviceName(device);
icculus@11726
   415
            }
icculus@11726
   416
            IMMDevice_Release(device);
icculus@11726
   417
        }
icculus@11726
   418
    }
icculus@11726
   419
icculus@12345
   420
    /* sort the list of devices by their guid so list is consistent between runs */
icculus@12345
   421
    SDL_qsort(items, total, sizeof (*items), sort_endpoints);
icculus@12345
   422
icculus@12345
   423
    /* Send the sorted list on to the SDL's higher level. */
icculus@12345
   424
    for (i = 0; i < total; i++) {
icculus@12345
   425
        EndpointItem *item = items + i;
icculus@12345
   426
        if ((item->devid) && (item->devname)) {
icculus@12345
   427
            WASAPI_AddDevice(iscapture, item->devname, item->devid);
icculus@12345
   428
        }
icculus@12345
   429
        SDL_free(item->devname);
icculus@12345
   430
        CoTaskMemFree(item->devid);
icculus@12345
   431
    }
icculus@12345
   432
icculus@12345
   433
    SDL_free(items);
icculus@11726
   434
    IMMDeviceCollection_Release(collection);
icculus@11726
   435
}
icculus@11726
   436
icculus@11726
   437
void
icculus@11726
   438
WASAPI_EnumerateEndpoints(void)
icculus@11726
   439
{
icculus@11726
   440
    WASAPI_EnumerateEndpointsForFlow(SDL_FALSE);  /* playback */
icculus@11726
   441
    WASAPI_EnumerateEndpointsForFlow(SDL_TRUE);  /* capture */
icculus@11726
   442
icculus@11726
   443
    /* if this fails, we just won't get hotplug events. Carry on anyhow. */
icculus@11726
   444
    IMMDeviceEnumerator_RegisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) &notification_client);
icculus@11726
   445
}
icculus@11726
   446
icculus@11726
   447
void
icculus@11726
   448
WASAPI_PlatformDeleteActivationHandler(void *handler)
icculus@11726
   449
{
icculus@11726
   450
    /* not asynchronous. */
icculus@11889
   451
    SDL_assert(!"This function should have only been called on WinRT.");
icculus@11726
   452
}
icculus@11726
   453
icculus@11726
   454
#endif  /* SDL_AUDIO_DRIVER_WASAPI && !defined(__WINRT__) */
icculus@11726
   455
icculus@11726
   456
/* vi: set ts=4 sw=4 expandtab: */
icculus@11726
   457