wasapi: Initial WASAPI support, for Windows Vista and later.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 14 Feb 2017 03:03:27 -0500
changeset 108880396fb28bbb9
parent 10887 ccac10a548f9
child 10889 18fca4cd3331
wasapi: Initial WASAPI support, for Windows Vista and later.

This should remain binary compatible with Windows XP, as we dynamically
load anything we need and fall back to DirectSound/WinMM/XAudio2 if not
available.
VisualC/SDL/SDL.vcxproj
VisualC/SDL/SDL_VS2008.vcproj
include/SDL_config_windows.h
src/audio/SDL_audio.c
src/audio/wasapi/SDL_wasapi.c
src/audio/wasapi/SDL_wasapi.h
src/core/windows/SDL_windows.c
     1.1 --- a/VisualC/SDL/SDL.vcxproj	Tue Feb 14 02:49:08 2017 -0500
     1.2 +++ b/VisualC/SDL/SDL.vcxproj	Tue Feb 14 03:03:27 2017 -0500
     1.3 @@ -361,6 +361,7 @@
     1.4      <ClInclude Include="..\..\src\video\windows\SDL_windowsshape.h" />
     1.5      <ClInclude Include="..\..\src\video\windows\SDL_windowsvideo.h" />
     1.6      <ClInclude Include="..\..\src\video\windows\SDL_windowswindow.h" />
     1.7 +    <ClInclude Include="..\..\src\audio\wasapi\SDL_wasapi.h" />
     1.8      <ClInclude Include="..\..\src\audio\winmm\SDL_winmm.h" />
     1.9      <ClInclude Include="..\..\src\core\windows\SDL_xinput.h" />
    1.10      <ClInclude Include="..\..\src\haptic\windows\SDL_xinputhaptic_c.h" />
    1.11 @@ -491,6 +492,7 @@
    1.12      <ClCompile Include="..\..\src\video\windows\SDL_windowsshape.c" />
    1.13      <ClCompile Include="..\..\src\video\windows\SDL_windowsvideo.c" />
    1.14      <ClCompile Include="..\..\src\video\windows\SDL_windowswindow.c" />
    1.15 +    <ClCompile Include="..\..\src\audio\wasapi\SDL_wasapi.c" />
    1.16      <ClCompile Include="..\..\src\audio\winmm\SDL_winmm.c" />
    1.17      <ClCompile Include="..\..\src\audio\xaudio2\SDL_xaudio2.c" />
    1.18      <ClCompile Include="..\..\src\core\windows\SDL_xinput.c" />
     2.1 --- a/VisualC/SDL/SDL_VS2008.vcproj	Tue Feb 14 02:49:08 2017 -0500
     2.2 +++ b/VisualC/SDL/SDL_VS2008.vcproj	Tue Feb 14 03:03:27 2017 -0500
     2.3 @@ -1438,6 +1438,14 @@
     2.4  			>
     2.5  		</File>
     2.6  		<File
     2.7 +			RelativePath="..\..\src\audio\wasapi\SDL_wasapi.c"
     2.8 +			>
     2.9 +		</File>
    2.10 +		<File
    2.11 +			RelativePath="..\..\src\audio\wasapi\SDL_wasapi.h"
    2.12 +			>
    2.13 +		</File>
    2.14 +		<File
    2.15  			RelativePath="..\..\src\audio\winmm\SDL_winmm.c"
    2.16  			>
    2.17  		</File>
     3.1 --- a/include/SDL_config_windows.h	Tue Feb 14 02:49:08 2017 -0500
     3.2 +++ b/include/SDL_config_windows.h	Tue Feb 14 03:03:27 2017 -0500
     3.3 @@ -154,6 +154,7 @@
     3.4  #endif
     3.5  
     3.6  /* Enable various audio drivers */
     3.7 +#define SDL_AUDIO_DRIVER_WASAPI 1
     3.8  #define SDL_AUDIO_DRIVER_DSOUND 1
     3.9  #define SDL_AUDIO_DRIVER_XAUDIO2    1
    3.10  #define SDL_AUDIO_DRIVER_WINMM  1
     4.1 --- a/src/audio/SDL_audio.c	Tue Feb 14 02:49:08 2017 -0500
     4.2 +++ b/src/audio/SDL_audio.c	Tue Feb 14 03:03:27 2017 -0500
     4.3 @@ -68,6 +68,9 @@
     4.4  #if SDL_AUDIO_DRIVER_NAS
     4.5      &NAS_bootstrap,
     4.6  #endif
     4.7 +#if SDL_AUDIO_DRIVER_WASAPI
     4.8 +    &WASAPI_bootstrap,
     4.9 +#endif
    4.10  #if SDL_AUDIO_DRIVER_XAUDIO2
    4.11      &XAUDIO2_bootstrap,
    4.12  #endif
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/audio/wasapi/SDL_wasapi.c	Tue Feb 14 03:03:27 2017 -0500
     5.3 @@ -0,0 +1,798 @@
     5.4 +/*
     5.5 +  Simple DirectMedia Layer
     5.6 +  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     5.7 +
     5.8 +  This software is provided 'as-is', without any express or implied
     5.9 +  warranty.  In no event will the authors be held liable for any damages
    5.10 +  arising from the use of this software.
    5.11 +
    5.12 +  Permission is granted to anyone to use this software for any purpose,
    5.13 +  including commercial applications, and to alter it and redistribute it
    5.14 +  freely, subject to the following restrictions:
    5.15 +
    5.16 +  1. The origin of this software must not be misrepresented; you must not
    5.17 +     claim that you wrote the original software. If you use this software
    5.18 +     in a product, an acknowledgment in the product documentation would be
    5.19 +     appreciated but is not required.
    5.20 +  2. Altered source versions must be plainly marked as such, and must not be
    5.21 +     misrepresented as being the original software.
    5.22 +  3. This notice may not be removed or altered from any source distribution.
    5.23 +*/
    5.24 +
    5.25 +#include "../../SDL_internal.h"
    5.26 +
    5.27 +#if SDL_AUDIO_DRIVER_WASAPI
    5.28 +
    5.29 +#include "../../core/windows/SDL_windows.h"
    5.30 +#include "SDL_audio.h"
    5.31 +#include "SDL_timer.h"
    5.32 +#include "../SDL_audio_c.h"
    5.33 +#include "../SDL_sysaudio.h"
    5.34 +#include "SDL_assert.h"
    5.35 +#include "SDL_log.h"
    5.36 +
    5.37 +#define COBJMACROS
    5.38 +#include <mmdeviceapi.h>
    5.39 +#include <audioclient.h>
    5.40 +
    5.41 +#include "SDL_wasapi.h"
    5.42 +
    5.43 +static const ERole SDL_WASAPI_role = eConsole;  /* !!! FIXME: should this be eMultimedia? Should be a hint? */
    5.44 +
    5.45 +/* This is global to the WASAPI target, to handle hotplug and default device lookup. */
    5.46 +static IMMDeviceEnumerator *enumerator = NULL;
    5.47 +
    5.48 +/* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */
    5.49 +typedef struct DevIdList
    5.50 +{
    5.51 +	WCHAR *str;
    5.52 +	struct DevIdList *next;
    5.53 +} DevIdList;
    5.54 +
    5.55 +static DevIdList *deviceid_list = NULL;
    5.56 +
    5.57 +/* handle to Avrt.dll--Vista and later!--for flagging the callback thread as "Pro Audio" (low latency). */
    5.58 +#ifndef __WINRT__
    5.59 +static HMODULE libavrt = NULL;
    5.60 +#endif
    5.61 +typedef HANDLE (WINAPI *pfnAvSetMmThreadCharacteristicsW)(LPWSTR,LPDWORD);
    5.62 +typedef BOOL (WINAPI *pfnAvRevertMmThreadCharacteristics)(HANDLE);
    5.63 +static pfnAvSetMmThreadCharacteristicsW pAvSetMmThreadCharacteristicsW = NULL;
    5.64 +static pfnAvRevertMmThreadCharacteristics pAvRevertMmThreadCharacteristics = NULL;
    5.65 +
    5.66 +/* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */
    5.67 +static const CLSID SDL_CLSID_MMDeviceEnumerator = { 0xbcde0395, 0xe52f, 0x467c, { 0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e } };
    5.68 +static const IID SDL_IID_IMMDeviceEnumerator = { 0xa95664d2, 0x9614, 0x4f35, { 0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6 } };
    5.69 +static const IID SDL_IID_IMMNotificationClient = { 0x7991eec9, 0x7e89, 0x4d85, { 0x83, 0x90, 0x6c, 0x70, 0x3c, 0xec, 0x60, 0xc0 } };
    5.70 +static const IID SDL_IID_IMMEndpoint = { 0x1be09788, 0x6894, 0x4089, { 0x85, 0x86, 0x9a, 0x2a, 0x6c, 0x26, 0x5a, 0xc5 } };
    5.71 +static const IID SDL_IID_IAudioClient = { 0x1cb9ad4c, 0xdbfa, 0x4c32, { 0xb1, 0x78, 0xc2, 0xf5, 0x68, 0xa7, 0x03, 0xb2 } };
    5.72 +static const IID SDL_IID_IAudioRenderClient = { 0xf294acfc, 0x3146, 0x4483, { 0xa7, 0xbf, 0xad, 0xdc, 0xa7, 0xc2, 0x60, 0xe2 } };
    5.73 +static const IID SDL_IID_IAudioCaptureClient = { 0xc8adbd64, 0xe71e, 0x48a0, { 0xa4, 0xde, 0x18, 0x5c, 0x39, 0x5c, 0xd3, 0x17 } };
    5.74 +static const GUID SDL_KSDATAFORMAT_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
    5.75 +static const GUID SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
    5.76 +static const PROPERTYKEY SDL_PKEY_Device_FriendlyName = { { 0xa45c254e, 0xdf1c, 0x4efd, { 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, } }, 14 };
    5.77 +
    5.78 +/* PropVariantInit() is an inline function/macro in PropIdl.h that calls the C runtime's memset() directly. Use ours instead, to avoid dependency. */ 
    5.79 +#ifdef PropVariantInit
    5.80 +#undef PropVariantInit
    5.81 +#endif
    5.82 +#define PropVariantInit(p) SDL_zerop(p)
    5.83 +
    5.84 +static void AddWASAPIDevice(const SDL_bool iscapture, IMMDevice *device, LPCWSTR devid);
    5.85 +static void RemoveWASAPIDevice(const SDL_bool iscapture, LPCWSTR devid);
    5.86 +
    5.87 +/* We need a COM subclass of IMMNotificationClient for hotplug support, which is
    5.88 +   easy in C++, but we have to tapdance more to make work in C.
    5.89 +   Thanks to this page for coaching on how to make this work:
    5.90 +     https://www.codeproject.com/Articles/13601/COM-in-plain-C */
    5.91 +
    5.92 +typedef struct SDLMMNotificationClient
    5.93 +{
    5.94 +    const IMMNotificationClientVtbl *lpVtbl;
    5.95 +    SDL_atomic_t refcount;
    5.96 +} SDLMMNotificationClient;
    5.97 +
    5.98 +static HRESULT STDMETHODCALLTYPE
    5.99 +SDLMMNotificationClient_QueryInterface(IMMNotificationClient *this, REFIID iid, void **ppv)
   5.100 +{
   5.101 +    if ((WIN_IsEqualIID(iid, &IID_IUnknown)) || (WIN_IsEqualIID(iid, &SDL_IID_IMMNotificationClient)))
   5.102 +    {
   5.103 +        *ppv = this;
   5.104 +        this->lpVtbl->AddRef(this);
   5.105 +        return S_OK;
   5.106 +    }
   5.107 +
   5.108 +    *ppv = NULL;
   5.109 +    return E_NOINTERFACE;
   5.110 +}
   5.111 +
   5.112 +static ULONG STDMETHODCALLTYPE
   5.113 +SDLMMNotificationClient_AddRef(IMMNotificationClient *ithis)
   5.114 +{
   5.115 +	SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis;
   5.116 +    return (ULONG) (SDL_AtomicIncRef(&this->refcount) + 1);
   5.117 +}
   5.118 +
   5.119 +static ULONG STDMETHODCALLTYPE
   5.120 +SDLMMNotificationClient_Release(IMMNotificationClient *ithis)
   5.121 +{
   5.122 +    /* this is a static object; we don't ever free it. */
   5.123 +	SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis;
   5.124 +    const ULONG retval = SDL_AtomicDecRef(&this->refcount);
   5.125 +    if (retval == 0) {
   5.126 +        SDL_AtomicSet(&this->refcount, 0);  /* uhh... */
   5.127 +        return 0;
   5.128 +    }
   5.129 +    return retval - 1;
   5.130 +}
   5.131 +
   5.132 +/* These are the entry points called when WASAPI device endpoints change. */
   5.133 +static HRESULT STDMETHODCALLTYPE
   5.134 +SDLMMNotificationClient_OnDefaultDeviceChanged(IMMNotificationClient *ithis, EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId)
   5.135 +{
   5.136 +#if 0
   5.137 +	const char *flowstr = "?";
   5.138 +	char *utf8;
   5.139 +	SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis;
   5.140 +	if (role != SDL_WASAPI_role) {
   5.141 +		return S_OK;  /* ignore it. */
   5.142 +	}
   5.143 +
   5.144 +	// !!! FIXME: should probably switch endpoints if we have a default device opened; it's not clear how trivial this is, though.
   5.145 +	// !!! FIXME:  also not clear yet how painful it is to switch when someone opens a tablet's speaker and then plugs in headphones.
   5.146 +	utf8 = pwstrDeviceId ? WIN_StringToUTF8(pwstrDeviceId) : NULL;
   5.147 +	switch (flow) {
   5.148 +	case eRender: flowstr = "RENDER"; break;
   5.149 +	case eCapture: flowstr = "CAPTURE"; break;
   5.150 +	case eAll: flowstr = "ALL"; break;
   5.151 +	}
   5.152 +	SDL_Log("WASAPI: OnDefaultDeviceChanged! '%s' [%s]", utf8, flowstr);
   5.153 +	SDL_free(utf8);
   5.154 +#endif
   5.155 +
   5.156 +	return S_OK;
   5.157 +}
   5.158 +
   5.159 +static HRESULT STDMETHODCALLTYPE
   5.160 +SDLMMNotificationClient_OnDeviceAdded(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
   5.161 +{
   5.162 +	/* we ignore this; devices added here then progress to ACTIVE, if appropriate, in 
   5.163 +	   OnDeviceStateChange, making that a better place to deal with device adds. More 
   5.164 +	   importantly: the first time you plug in a USB audio device, this callback will 
   5.165 +	   fire, but when you unplug it, it isn't removed (it's state changes to NOTPRESENT).
   5.166 +	   Plugging it back in won't fire this callback again. */
   5.167 +	return S_OK;
   5.168 +}
   5.169 +
   5.170 +static HRESULT STDMETHODCALLTYPE
   5.171 +SDLMMNotificationClient_OnDeviceRemoved(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
   5.172 +{
   5.173 +	/* See notes in OnDeviceAdded handler about why we ignore this. */
   5.174 +	return S_OK;
   5.175 +}
   5.176 +
   5.177 +static HRESULT STDMETHODCALLTYPE
   5.178 +SDLMMNotificationClient_OnDeviceStateChanged(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId, DWORD dwNewState)
   5.179 +{
   5.180 +	SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis;
   5.181 +	IMMDevice *device = NULL;
   5.182 +
   5.183 +	if (SUCCEEDED(IMMDeviceEnumerator_GetDevice(enumerator, pwstrDeviceId, &device))) {
   5.184 +		IMMEndpoint *endpoint = NULL;
   5.185 +		if (SUCCEEDED(IMMDevice_QueryInterface(device, &SDL_IID_IMMEndpoint, (void **) &endpoint))) {
   5.186 +			EDataFlow flow;
   5.187 +			if (SUCCEEDED(IMMEndpoint_GetDataFlow(endpoint, &flow))) {
   5.188 +				const SDL_bool iscapture = (flow == eCapture);
   5.189 +				if (dwNewState == DEVICE_STATE_ACTIVE) {
   5.190 +					AddWASAPIDevice(iscapture, device, pwstrDeviceId);
   5.191 +				} else {
   5.192 +					RemoveWASAPIDevice(iscapture, pwstrDeviceId);
   5.193 +				}
   5.194 +			}
   5.195 +			IMMEndpoint_Release(endpoint);
   5.196 +		}
   5.197 +		IMMDevice_Release(device);
   5.198 +	}
   5.199 +
   5.200 +	return S_OK;
   5.201 +}
   5.202 +
   5.203 +static HRESULT STDMETHODCALLTYPE
   5.204 +SDLMMNotificationClient_OnPropertyValueChanged(IMMNotificationClient *this, LPCWSTR pwstrDeviceId, const PROPERTYKEY key)
   5.205 +{
   5.206 +    return S_OK;  /* we don't care about these. */
   5.207 +}
   5.208 +
   5.209 +static const IMMNotificationClientVtbl notification_client_vtbl = {
   5.210 +    SDLMMNotificationClient_QueryInterface,
   5.211 +    SDLMMNotificationClient_AddRef,
   5.212 +    SDLMMNotificationClient_Release,
   5.213 +    SDLMMNotificationClient_OnDeviceStateChanged,
   5.214 +    SDLMMNotificationClient_OnDeviceAdded,
   5.215 +    SDLMMNotificationClient_OnDeviceRemoved,
   5.216 +    SDLMMNotificationClient_OnDefaultDeviceChanged,
   5.217 +    SDLMMNotificationClient_OnPropertyValueChanged
   5.218 +};
   5.219 +
   5.220 +static SDLMMNotificationClient notification_client = { &notification_client_vtbl, 1 };
   5.221 +
   5.222 +static SDL_bool
   5.223 +WStrEqual(const WCHAR *a, const WCHAR *b)
   5.224 +{
   5.225 +	while (*a) {
   5.226 +		if (*a != *b) {
   5.227 +			return SDL_FALSE;
   5.228 +		}
   5.229 +		a++;
   5.230 +		b++;
   5.231 +	}
   5.232 +	return *b == 0;
   5.233 +}
   5.234 +
   5.235 +static WCHAR *
   5.236 +WStrDupe(const WCHAR *wstr)
   5.237 +{
   5.238 +	const int len = (lstrlenW(wstr) + 1) * sizeof (WCHAR);
   5.239 +	WCHAR *retval = (WCHAR *) SDL_malloc(len);
   5.240 +	if (retval) {
   5.241 +		SDL_memcpy(retval, wstr, len);
   5.242 +	}
   5.243 +	return retval;
   5.244 +}
   5.245 +
   5.246 +static void 
   5.247 +RemoveWASAPIDevice(const SDL_bool iscapture, LPCWSTR devid)
   5.248 +{
   5.249 +	DevIdList *i;
   5.250 +	DevIdList *next;
   5.251 +	DevIdList *prev = NULL;
   5.252 +	for (i = deviceid_list; i; i = next) {
   5.253 +		next = i->next;
   5.254 +		if (WStrEqual(i->str, devid)) {
   5.255 +			if (prev) {
   5.256 +				prev->next = next;
   5.257 +			} else {
   5.258 +				deviceid_list = next;
   5.259 +			}
   5.260 +			SDL_RemoveAudioDevice(iscapture, i->str);
   5.261 +			SDL_free(i->str);
   5.262 +			SDL_free(i);
   5.263 +		}
   5.264 +		prev = i;
   5.265 +	}
   5.266 +}
   5.267 +
   5.268 +static void
   5.269 +AddWASAPIDevice(const SDL_bool iscapture, IMMDevice *device, LPCWSTR devid)
   5.270 +{
   5.271 +	IPropertyStore *props = NULL;
   5.272 +    char *utf8dev = NULL;
   5.273 +	DevIdList *devidlist;
   5.274 +    PROPVARIANT var;
   5.275 +
   5.276 +	/* You can have multiple endpoints on a device that are mutually exclusive ("Speakers" vs "Line Out" or whatever).
   5.277 +       In a perfect world, things that are unplugged won't be in this collection. The only gotcha is probably for
   5.278 +       phones and tablets, where you might have an internal speaker and a headphone jack and expect both to be
   5.279 +       available and switch automatically. (!!! FIXME...?) */
   5.280 +
   5.281 +    /* PKEY_Device_FriendlyName gives you "Speakers (SoundBlaster Pro)" which drives me nuts. I'd rather it be
   5.282 +       "SoundBlaster Pro (Speakers)" but I guess that's developers vs users. Windows uses the FriendlyName in
   5.283 +       its own UIs, like Volume Control, etc. */
   5.284 +
   5.285 +	/* see if we already have this one. */
   5.286 +	for (devidlist = deviceid_list; devidlist; devidlist = devidlist->next) {
   5.287 +		if (WStrEqual(devidlist->str, devid)) {
   5.288 +			return;  /* we already have this. */
   5.289 +		}
   5.290 +	}
   5.291 +
   5.292 +	devidlist = (DevIdList *) SDL_malloc(sizeof (*devidlist));
   5.293 +	if (!devidlist) {
   5.294 +		return;  /* oh well. */
   5.295 +	}
   5.296 +
   5.297 +	devid = WStrDupe(devid);
   5.298 +	if (!devid) {
   5.299 +		SDL_free(devidlist);
   5.300 +		return;  /* oh well. */
   5.301 +	}
   5.302 +
   5.303 +	devidlist->str = (WCHAR *) devid;
   5.304 +	devidlist->next = deviceid_list;
   5.305 +	deviceid_list = devidlist;
   5.306 +
   5.307 +    if (SUCCEEDED(IMMDevice_OpenPropertyStore(device, STGM_READ, &props))) {
   5.308 +	    PropVariantInit(&var);
   5.309 +	    if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_Device_FriendlyName, &var))) {
   5.310 +			utf8dev = WIN_StringToUTF8(var.pwszVal);
   5.311 +			if (utf8dev) {
   5.312 +				SDL_AddAudioDevice(iscapture, utf8dev, (void *) devid);
   5.313 +		        SDL_free(utf8dev);
   5.314 +			}
   5.315 +		}
   5.316 +	    PropVariantClear(&var);
   5.317 +	    IPropertyStore_Release(props);
   5.318 +	}
   5.319 +}
   5.320 +
   5.321 +static void
   5.322 +EnumerateEndpoints(const SDL_bool iscapture)
   5.323 +{
   5.324 +    IMMDeviceCollection *collection = NULL;
   5.325 +    UINT i, total;
   5.326 +
   5.327 +    /* Note that WASAPI separates "adapter devices" from "audio endpoint devices"
   5.328 +       ...one adapter device ("SoundBlaster Pro") might have multiple endpoint devices ("Speakers", "Line-Out"). */
   5.329 +
   5.330 +    if (FAILED(IMMDeviceEnumerator_EnumAudioEndpoints(enumerator, iscapture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &collection))) {
   5.331 +        return;
   5.332 +    }
   5.333 +
   5.334 +    if (FAILED(IMMDeviceCollection_GetCount(collection, &total))) {
   5.335 +        IMMDeviceCollection_Release(collection);
   5.336 +        return;
   5.337 +    }
   5.338 +
   5.339 +    for (i = 0; i < total; i++) {
   5.340 +        IMMDevice *device = NULL;
   5.341 +        if (SUCCEEDED(IMMDeviceCollection_Item(collection, i, &device))) {
   5.342 +			LPWSTR devid = NULL;
   5.343 +			if (SUCCEEDED(IMMDevice_GetId(device, &devid))) {
   5.344 +				AddWASAPIDevice(iscapture, device, devid);
   5.345 +				CoTaskMemFree(devid);
   5.346 +			}
   5.347 +	        IMMDevice_Release(device);
   5.348 +		}
   5.349 +	}
   5.350 +
   5.351 +    IMMDeviceCollection_Release(collection);
   5.352 +}
   5.353 +
   5.354 +static void
   5.355 +WASAPI_DetectDevices(void)
   5.356 +{
   5.357 +    EnumerateEndpoints(SDL_FALSE);  /* playback */
   5.358 +    EnumerateEndpoints(SDL_TRUE);   /* capture */
   5.359 +
   5.360 +    /* if this fails, we just won't get hotplug events. Carry on anyhow. */
   5.361 +    IMMDeviceEnumerator_RegisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) &notification_client);
   5.362 +}
   5.363 +
   5.364 +static int
   5.365 +WASAPI_GetPendingBytes(_THIS)
   5.366 +{
   5.367 +    UINT32 frames = 0;
   5.368 +    if (FAILED(IAudioClient_GetCurrentPadding(this->hidden->client, &frames))) {
   5.369 +        return 0;  /* oh well. */
   5.370 +    }
   5.371 +
   5.372 +    return ((int) frames) * this->hidden->framesize;
   5.373 +}
   5.374 +
   5.375 +static Uint8 *
   5.376 +WASAPI_GetDeviceBuf(_THIS)
   5.377 +{
   5.378 +    /* get an endpoint buffer from WASAPI. */
   5.379 +    BYTE *buffer = NULL;
   5.380 +	if (FAILED(IAudioRenderClient_GetBuffer(this->hidden->render, this->spec.samples, &buffer))) {
   5.381 +		IAudioClient_Stop(this->hidden->client);
   5.382 +        SDL_OpenedAudioDeviceDisconnected(this);  /* uhoh. */
   5.383 +        SDL_assert(buffer == NULL);
   5.384 +    }
   5.385 +    return (Uint8 *) buffer;
   5.386 +}
   5.387 +
   5.388 +static void
   5.389 +WASAPI_PlayDevice(_THIS)
   5.390 +{
   5.391 +    if (SDL_AtomicGet(&this->enabled)) {  /* not shutting down? */
   5.392 +        if (FAILED(IAudioRenderClient_ReleaseBuffer(this->hidden->render, this->spec.samples, 0))) {
   5.393 +            IAudioClient_Stop(this->hidden->client);
   5.394 +            SDL_OpenedAudioDeviceDisconnected(this);  /* uhoh. */
   5.395 +        }
   5.396 +    }
   5.397 +}
   5.398 +
   5.399 +static void
   5.400 +WASAPI_WaitDevice(_THIS)
   5.401 +{
   5.402 +	const UINT32 maxpadding = this->spec.samples;
   5.403 +    while (SDL_AtomicGet(&this->enabled)) {
   5.404 +		UINT32 padding = 0;
   5.405 +		if (FAILED(IAudioClient_GetCurrentPadding(this->hidden->client, &padding))) {
   5.406 +		    IAudioClient_Stop(this->hidden->client);
   5.407 +			SDL_OpenedAudioDeviceDisconnected(this);
   5.408 +        }
   5.409 +
   5.410 +        if (padding <= maxpadding) {
   5.411 +			break;
   5.412 +		}
   5.413 +
   5.414 +		/* Sleep long enough for half the buffer to be free. */
   5.415 +		SDL_Delay(((padding - maxpadding) * 1000) / this->spec.freq);
   5.416 +	}
   5.417 +}
   5.418 +
   5.419 +static int
   5.420 +WASAPI_CaptureFromDevice(_THIS, void *buffer, int buflen)
   5.421 +{
   5.422 +    SDL_AudioStream *stream = this->hidden->capturestream;
   5.423 +    const int avail = SDL_AudioStreamAvailable(stream);
   5.424 +    if (avail > 0) {
   5.425 +        const int cpy = SDL_min(buflen, avail);
   5.426 +        SDL_AudioStreamGet(stream, buffer, cpy);
   5.427 +        return cpy;
   5.428 +    }
   5.429 +
   5.430 +    while (SDL_AtomicGet(&this->enabled)) {
   5.431 +        HRESULT ret;
   5.432 +        BYTE *ptr = NULL;
   5.433 +        UINT32 frames = 0;
   5.434 +        DWORD flags = 0;
   5.435 +
   5.436 +        ret = IAudioCaptureClient_GetBuffer(this->hidden->capture, &ptr, &frames, &flags, NULL, NULL);
   5.437 +        if ((ret == AUDCLNT_S_BUFFER_EMPTY) || !frames) {
   5.438 +            WASAPI_WaitDevice(this);
   5.439 +        } else if (ret == S_OK) {
   5.440 +            const int total = ((int) frames) * this->hidden->framesize;
   5.441 +            const int cpy = SDL_min(buflen, total);
   5.442 +            const int leftover = total - cpy;
   5.443 +            const SDL_bool silent = (flags & AUDCLNT_BUFFERFLAGS_SILENT) ? SDL_TRUE : SDL_FALSE;
   5.444 +
   5.445 +            if (silent) {
   5.446 +                SDL_memset(buffer, this->spec.silence, cpy);
   5.447 +            } else {
   5.448 +                SDL_memcpy(buffer, ptr, cpy);
   5.449 +            }
   5.450 +            
   5.451 +            if (leftover > 0) {
   5.452 +                ptr += cpy;
   5.453 +                if (silent) {
   5.454 +                    SDL_memset(ptr, this->spec.silence, leftover);  /* I guess this is safe? */
   5.455 +                }
   5.456 +
   5.457 +                if (SDL_AudioStreamPut(stream, ptr, leftover) == -1) {
   5.458 +                    return -1;  /* uhoh, out of memory, etc. Kill device.  :( */
   5.459 +                }
   5.460 +            }
   5.461 +
   5.462 +            IAudioCaptureClient_ReleaseBuffer(this->hidden->capture, frames);
   5.463 +            return cpy;
   5.464 +        } else {
   5.465 +            break;  /* something totally failed. */
   5.466 +        }
   5.467 +    }
   5.468 +
   5.469 +    return -1;  /* unrecoverable error. */
   5.470 +}
   5.471 +
   5.472 +static void
   5.473 +WASAPI_FlushCapture(_THIS)
   5.474 +{
   5.475 +    if (SDL_AtomicGet(&this->enabled)) {
   5.476 +        BYTE *ptr = NULL;
   5.477 +        UINT32 frames = 0;
   5.478 +        DWORD flags = 0;
   5.479 +        HRESULT ret;
   5.480 +        /* just read until we stop getting packets, throwing them away. */
   5.481 +        while ((ret = IAudioCaptureClient_GetBuffer(this->hidden->capture, &ptr, &frames, &flags, NULL, NULL)) == S_OK) {
   5.482 +            IAudioCaptureClient_ReleaseBuffer(this->hidden->capture, frames);
   5.483 +        }
   5.484 +        SDL_AudioStreamClear(this->hidden->capturestream);
   5.485 +    }
   5.486 +}
   5.487 +
   5.488 +static void
   5.489 +WASAPI_CloseDevice(_THIS)
   5.490 +{
   5.491 +    /* don't touch this->hidden->task in here; it has to be reverted from
   5.492 +      our callback thread. We do that in WASAPI_ThreadDeinit().
   5.493 +      (likewise for this->hidden->coinitialized). */
   5.494 +
   5.495 +	if (this->hidden->client) {
   5.496 +        IAudioClient_Stop(this->hidden->client);
   5.497 +	}
   5.498 +
   5.499 +    if (this->hidden->render) {
   5.500 +        IAudioRenderClient_Release(this->hidden->render);
   5.501 +    }
   5.502 +
   5.503 +    if (this->hidden->client) {
   5.504 +        IAudioClient_Release(this->hidden->client);
   5.505 +    }
   5.506 +
   5.507 +    if (this->hidden->waveformat) {
   5.508 +        CoTaskMemFree(this->hidden->waveformat);
   5.509 +    }
   5.510 +
   5.511 +    if (this->hidden->device) {
   5.512 +        IMMDevice_Release(this->hidden->device);
   5.513 +    }
   5.514 +
   5.515 +    if (this->hidden->capturestream) {
   5.516 +        SDL_FreeAudioStream(this->hidden->capturestream);
   5.517 +    }
   5.518 +
   5.519 +    SDL_free(this->hidden);
   5.520 +}
   5.521 +
   5.522 +static int
   5.523 +WASAPI_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
   5.524 +{
   5.525 +    /* !!! FIXME: we could request an exclusive mode stream, which is lower latency;
   5.526 +       !!!  it will write into the kernel's audio buffer directly instead of
   5.527 +       !!!  shared memory that a user-mode mixer then writes to the kernel with
   5.528 +       !!!  everything else. Doing this means any other sound using this device will
   5.529 +       !!!  stop playing, including the user's MP3 player and system notification
   5.530 +       !!!  sounds. You'd probably need to release the device when the app isn't in
   5.531 +       !!!  the foreground, to be a good citizen of the system. It's doable, but it's
   5.532 +       !!!  more work and causes some annoyances, and I don't know what the latency
   5.533 +       !!!  wins actually look like. Maybe add a hint to force exclusive mode at
   5.534 +       !!!  some point. To be sure, defaulting to shared mode is the right thing to
   5.535 +       !!!  do in any case. */
   5.536 +    const AUDCLNT_SHAREMODE sharemode = AUDCLNT_SHAREMODE_SHARED;
   5.537 +    const EDataFlow dataflow = iscapture ? eCapture : eRender;
   5.538 +    UINT32 bufsize = 0;  /* this is in sample frames, not samples, not bytes. */
   5.539 +    REFERENCE_TIME duration = 0;
   5.540 +    IMMDevice *device = NULL;
   5.541 +    IAudioClient *client = NULL;
   5.542 +    IAudioRenderClient *render = NULL;
   5.543 +    IAudioCaptureClient *capture = NULL;
   5.544 +    WAVEFORMATEX *waveformat = NULL;
   5.545 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   5.546 +    SDL_AudioFormat wasapi_format = 0;
   5.547 +    SDL_bool valid_format = SDL_FALSE;
   5.548 +    HRESULT ret = S_OK;
   5.549 +
   5.550 +    /* Initialize all variables that we clean on shutdown */
   5.551 +    this->hidden = (struct SDL_PrivateAudioData *)
   5.552 +        SDL_malloc((sizeof *this->hidden));
   5.553 +    if (this->hidden == NULL) {
   5.554 +        return SDL_OutOfMemory();
   5.555 +    }
   5.556 +    SDL_zerop(this->hidden);
   5.557 +
   5.558 +    if (handle == NULL) {
   5.559 +        ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_WASAPI_role, &device);
   5.560 +	} else {
   5.561 +        ret = IMMDeviceEnumerator_GetDevice(enumerator, (LPCWSTR) handle, &device);
   5.562 +    }
   5.563 +
   5.564 +    if (FAILED(ret)) {
   5.565 +        return WIN_SetErrorFromHRESULT("WASAPI can't find requested audio endpoint", ret);
   5.566 +    }
   5.567 +
   5.568 +    SDL_assert(device != NULL);
   5.569 +    this->hidden->device = device;
   5.570 +
   5.571 +    ret = IMMDevice_Activate(device, &SDL_IID_IAudioClient, CLSCTX_ALL, NULL, (void **) &client);
   5.572 +    if (FAILED(ret)) {
   5.573 +        return WIN_SetErrorFromHRESULT("WASAPI can't activate audio endpoint", ret);
   5.574 +    }
   5.575 +
   5.576 +    SDL_assert(client != NULL);
   5.577 +    this->hidden->client = client;
   5.578 +
   5.579 +    ret = IAudioClient_GetMixFormat(client, &waveformat);
   5.580 +    if (FAILED(ret)) {
   5.581 +        return WIN_SetErrorFromHRESULT("WASAPI can't determine mix format", ret);
   5.582 +    }
   5.583 +
   5.584 +    SDL_assert(waveformat != NULL);
   5.585 +    this->hidden->waveformat = waveformat;
   5.586 +
   5.587 +    /* WASAPI will not do any conversion on our behalf. Force channels and sample rate. */
   5.588 +    this->spec.channels = (Uint8) waveformat->nChannels;
   5.589 +    this->spec.freq = waveformat->nSamplesPerSec;
   5.590 +
   5.591 +    /* Make sure we have a valid format that we can convert to whatever WASAPI wants. */
   5.592 +    if ((waveformat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) && (waveformat->wBitsPerSample == 32)) {
   5.593 +        wasapi_format = AUDIO_F32SYS;
   5.594 +    } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 16)) {
   5.595 +        wasapi_format = AUDIO_S16SYS;
   5.596 +    } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 32)) {
   5.597 +        wasapi_format = AUDIO_S32SYS;
   5.598 +    } else if (waveformat->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
   5.599 +        const WAVEFORMATEXTENSIBLE *ext = (const WAVEFORMATEXTENSIBLE *) waveformat;
   5.600 +        if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof (GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
   5.601 +            wasapi_format = AUDIO_F32SYS;
   5.602 +        } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof (GUID)) == 0) && (waveformat->wBitsPerSample == 16)) {
   5.603 +            wasapi_format = AUDIO_S16SYS;
   5.604 +        } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof (GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
   5.605 +            wasapi_format = AUDIO_S32SYS;
   5.606 +        }
   5.607 +    }
   5.608 +
   5.609 +    while ((!valid_format) && (test_format)) {
   5.610 +        if (test_format == wasapi_format) {
   5.611 +            this->spec.format = test_format;
   5.612 +            valid_format = SDL_TRUE;
   5.613 +            break;
   5.614 +        }
   5.615 +        test_format = SDL_NextAudioFormat();
   5.616 +    }
   5.617 +
   5.618 +    if (!valid_format) {
   5.619 +        return SDL_SetError("WASAPI: Unsupported audio format");
   5.620 +    }
   5.621 +
   5.622 +    ret = IAudioClient_GetDevicePeriod(client, NULL, &duration);
   5.623 +    if (FAILED(ret)) {
   5.624 +        return WIN_SetErrorFromHRESULT("WASAPI can't determine minimum device period", ret);
   5.625 +    }
   5.626 +
   5.627 +    ret = IAudioClient_Initialize(client, sharemode, 0, duration, sharemode == AUDCLNT_SHAREMODE_SHARED ? 0 : duration, waveformat, NULL);
   5.628 +    if (FAILED(ret)) {
   5.629 +        return WIN_SetErrorFromHRESULT("WASAPI can't initialize audio client", ret);
   5.630 +    }
   5.631 +
   5.632 +	ret = IAudioClient_GetBufferSize(client, &bufsize);
   5.633 +    if (FAILED(ret)) {
   5.634 +        return WIN_SetErrorFromHRESULT("WASAPI can't determine buffer size", ret);
   5.635 +    }
   5.636 +
   5.637 +    this->spec.samples = (Uint16) bufsize;
   5.638 +	if (!iscapture) {
   5.639 +		this->spec.samples /= 2;  /* fill half of the DMA buffer on each run. */
   5.640 +	}
   5.641 +
   5.642 +    /* Update the fragment size as size in bytes */
   5.643 +    SDL_CalculateAudioSpec(&this->spec);
   5.644 +
   5.645 +    this->hidden->framesize = (SDL_AUDIO_BITSIZE(this->spec.format) / 8) * this->spec.channels;
   5.646 +
   5.647 +    if (iscapture) {
   5.648 +        this->hidden->capturestream = SDL_NewAudioStream(this->spec.format, this->spec.channels, this->spec.freq, this->spec.format, this->spec.channels, this->spec.freq);
   5.649 +        if (!this->hidden->capturestream) {
   5.650 +            return -1;  /* already set SDL_Error */
   5.651 +        }
   5.652 +
   5.653 +        ret = IAudioClient_GetService(client, &SDL_IID_IAudioCaptureClient, (void**) &capture);
   5.654 +        if (FAILED(ret)) {
   5.655 +            return WIN_SetErrorFromHRESULT("WASAPI can't get capture client service", ret);
   5.656 +        }
   5.657 +
   5.658 +        SDL_assert(capture != NULL);
   5.659 +        this->hidden->capture = capture;
   5.660 +        ret = IAudioClient_Start(client);
   5.661 +        if (FAILED(ret)) {
   5.662 +            return WIN_SetErrorFromHRESULT("WASAPI can't start capture", ret);
   5.663 +        }
   5.664 +
   5.665 +        WASAPI_FlushCapture(this);  /* MSDN says you should flush capture endpoint right after startup. */
   5.666 +    } else {
   5.667 +        ret = IAudioClient_GetService(client, &SDL_IID_IAudioRenderClient, (void**) &render);
   5.668 +        if (FAILED(ret)) {
   5.669 +            return WIN_SetErrorFromHRESULT("WASAPI can't get render client service", ret);
   5.670 +        }
   5.671 +
   5.672 +        SDL_assert(render != NULL);
   5.673 +        this->hidden->render = render;
   5.674 +        ret = IAudioClient_Start(client);
   5.675 +        if (FAILED(ret)) {
   5.676 +            return WIN_SetErrorFromHRESULT("WASAPI can't start playback", ret);
   5.677 +        }
   5.678 +    }
   5.679 +
   5.680 +    return 0;  /* good to go. */
   5.681 +}
   5.682 +
   5.683 +static void
   5.684 +WASAPI_ThreadInit(_THIS)
   5.685 +{
   5.686 +    /* this thread uses COM. */
   5.687 +    if (SUCCEEDED(WIN_CoInitialize())) {    /* can't report errors, hope it worked! */
   5.688 +        this->hidden->coinitialized = SDL_TRUE;
   5.689 +    }
   5.690 +
   5.691 +    /* Set this thread to very high "Pro Audio" priority. */
   5.692 +    if (pAvSetMmThreadCharacteristicsW) {
   5.693 +        DWORD idx = 0;
   5.694 +        this->hidden->task = pAvSetMmThreadCharacteristicsW(TEXT("Pro Audio"), &idx);
   5.695 +    }
   5.696 +}
   5.697 +
   5.698 +static void
   5.699 +WASAPI_ThreadDeinit(_THIS)
   5.700 +{
   5.701 +    /* Set this thread to very high "Pro Audio" priority. */
   5.702 +    if (this->hidden->task && pAvRevertMmThreadCharacteristics) {
   5.703 +        pAvRevertMmThreadCharacteristics(this->hidden->task);
   5.704 +        this->hidden->task = NULL;
   5.705 +    }
   5.706 +
   5.707 +    if (this->hidden->coinitialized) {
   5.708 +        WIN_CoUninitialize();
   5.709 +    }
   5.710 +}
   5.711 +
   5.712 +static void
   5.713 +WASAPI_Deinitialize(void)
   5.714 +{
   5.715 +	DevIdList *devidlist;
   5.716 +	DevIdList *next;
   5.717 +
   5.718 +    if (enumerator) {
   5.719 +        IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) &notification_client);
   5.720 +        IMMDeviceEnumerator_Release(enumerator);
   5.721 +        enumerator = NULL;
   5.722 +    }
   5.723 +
   5.724 +    #ifndef __WINRT__
   5.725 +    if (libavrt) {
   5.726 +        FreeLibrary(libavrt);
   5.727 +        libavrt = NULL;
   5.728 +    }
   5.729 +    #endif
   5.730 +
   5.731 +    pAvSetMmThreadCharacteristicsW = NULL;
   5.732 +    pAvRevertMmThreadCharacteristics = NULL;
   5.733 +
   5.734 +	for (devidlist = deviceid_list; devidlist; devidlist = next) {
   5.735 +		next = devidlist->next;
   5.736 +		SDL_free(devidlist->str);
   5.737 +		SDL_free(devidlist);
   5.738 +	}
   5.739 +	deviceid_list = NULL;
   5.740 +
   5.741 +    WIN_CoUninitialize();
   5.742 +}
   5.743 +
   5.744 +static int
   5.745 +WASAPI_Init(SDL_AudioDriverImpl * impl)
   5.746 +{
   5.747 +	HRESULT ret;
   5.748 +
   5.749 +	/* just skip the discussion with COM here. */
   5.750 +	if (!WIN_IsWindowsVistaOrGreater()) {
   5.751 +		return SDL_SetError("WASAPI support requires Windows Vista or later");
   5.752 +	}
   5.753 +
   5.754 +	if (FAILED(WIN_CoInitialize())) {
   5.755 +        SDL_SetError("WASAPI: CoInitialize() failed");
   5.756 +        return 0;
   5.757 +    }
   5.758 +
   5.759 +	ret = CoCreateInstance(&SDL_CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &SDL_IID_IMMDeviceEnumerator, (LPVOID) &enumerator);
   5.760 +    if (FAILED(ret)) {
   5.761 +        WIN_CoUninitialize();
   5.762 +        WIN_SetErrorFromHRESULT("WASAPI CoCreateInstance(MMDeviceEnumerator)", ret);
   5.763 +        return 0;  /* oh well. */
   5.764 +    }
   5.765 +
   5.766 +    #ifdef __WINRT__
   5.767 +    pAvSetMmThreadCharacteristicsW = AvSetMmThreadCharacteristicsW;
   5.768 +    pAvRevertMmThreadCharacteristics = AvRevertMmThreadCharacteristics;
   5.769 +    #else
   5.770 +    libavrt = LoadLibraryW(L"avrt.dll");  /* this library is available in Vista and later. No WinXP, so have to LoadLibrary to use it for now! */
   5.771 +    if (libavrt) {
   5.772 +        pAvSetMmThreadCharacteristicsW = (pfnAvSetMmThreadCharacteristicsW) GetProcAddress(libavrt, "AvSetMmThreadCharacteristicsW");
   5.773 +        pAvRevertMmThreadCharacteristics = (pfnAvRevertMmThreadCharacteristics) GetProcAddress(libavrt, "AvRevertMmThreadCharacteristics");
   5.774 +    }
   5.775 +    #endif
   5.776 +
   5.777 +    /* Set the function pointers */
   5.778 +    impl->DetectDevices = WASAPI_DetectDevices;
   5.779 +    impl->ThreadInit = WASAPI_ThreadInit;
   5.780 +    impl->ThreadDeinit = WASAPI_ThreadDeinit;
   5.781 +    impl->OpenDevice = WASAPI_OpenDevice;
   5.782 +    impl->PlayDevice = WASAPI_PlayDevice;
   5.783 +    impl->WaitDevice = WASAPI_WaitDevice;
   5.784 +    impl->GetPendingBytes = WASAPI_GetPendingBytes;
   5.785 +    impl->GetDeviceBuf = WASAPI_GetDeviceBuf;
   5.786 +    impl->CaptureFromDevice = WASAPI_CaptureFromDevice;
   5.787 +    impl->FlushCapture = WASAPI_FlushCapture;
   5.788 +    impl->CloseDevice = WASAPI_CloseDevice;
   5.789 +    impl->Deinitialize = WASAPI_Deinitialize;
   5.790 +    impl->HasCaptureSupport = 1;
   5.791 +
   5.792 +    return 1;   /* this audio target is available. */
   5.793 +}
   5.794 +
   5.795 +AudioBootStrap WASAPI_bootstrap = {
   5.796 +    "wasapi", "WASAPI", WASAPI_Init, 0
   5.797 +};
   5.798 +
   5.799 +#endif  /* SDL_AUDIO_DRIVER_WASAPI */
   5.800 +
   5.801 +/* vi: set ts=4 sw=4 expandtab: */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/audio/wasapi/SDL_wasapi.h	Tue Feb 14 03:03:27 2017 -0500
     6.3 @@ -0,0 +1,46 @@
     6.4 +/*
     6.5 +  Simple DirectMedia Layer
     6.6 +  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     6.7 +
     6.8 +  This software is provided 'as-is', without any express or implied
     6.9 +  warranty.  In no event will the authors be held liable for any damages
    6.10 +  arising from the use of this software.
    6.11 +
    6.12 +  Permission is granted to anyone to use this software for any purpose,
    6.13 +  including commercial applications, and to alter it and redistribute it
    6.14 +  freely, subject to the following restrictions:
    6.15 +
    6.16 +  1. The origin of this software must not be misrepresented; you must not
    6.17 +     claim that you wrote the original software. If you use this software
    6.18 +     in a product, an acknowledgment in the product documentation would be
    6.19 +     appreciated but is not required.
    6.20 +  2. Altered source versions must be plainly marked as such, and must not be
    6.21 +     misrepresented as being the original software.
    6.22 +  3. This notice may not be removed or altered from any source distribution.
    6.23 +*/
    6.24 +#include "../../SDL_internal.h"
    6.25 +
    6.26 +#ifndef SDL_wasapi_h_
    6.27 +#define SDL_wasapi_h_
    6.28 +
    6.29 +#include "../SDL_sysaudio.h"
    6.30 +
    6.31 +/* Hidden "this" pointer for the audio functions */
    6.32 +#define _THIS SDL_AudioDevice *this
    6.33 +
    6.34 +struct SDL_PrivateAudioData
    6.35 +{
    6.36 +    IMMDevice *device;
    6.37 +    WAVEFORMATEX *waveformat;
    6.38 +    IAudioClient *client;
    6.39 +    IAudioRenderClient *render;
    6.40 +    IAudioCaptureClient *capture;
    6.41 +    SDL_AudioStream *capturestream;
    6.42 +    HANDLE task;
    6.43 +    SDL_bool coinitialized;
    6.44 +    int framesize;
    6.45 +};
    6.46 +
    6.47 +#endif /* SDL_wasapi_h_ */
    6.48 +
    6.49 +/* vi: set ts=4 sw=4 expandtab: */
     7.1 --- a/src/core/windows/SDL_windows.c	Tue Feb 14 02:49:08 2017 -0500
     7.2 +++ b/src/core/windows/SDL_windows.c	Tue Feb 14 03:03:27 2017 -0500
     7.3 @@ -142,6 +142,8 @@
     7.4  
     7.5  (Also, DirectSound shouldn't be limited to 32 chars, but its device enum
     7.6  has the same problem.)
     7.7 +
     7.8 +WASAPI doesn't need this. This is just for DirectSound/WinMM.
     7.9  */
    7.10  char *
    7.11  WIN_LookupAudioDeviceName(const WCHAR *name, const GUID *guid)