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