SDL_wasapi_win32.c 15 KB

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