SDL_immdevice.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. #if (defined(__WIN32__) || defined(__GDK__)) && HAVE_MMDEVICEAPI_H
  20. #include "SDL_windows.h"
  21. #include "SDL_immdevice.h"
  22. #include "SDL_timer.h"
  23. #include "../../audio/SDL_sysaudio.h"
  24. #include <objbase.h> /* For CLSIDFromString */
  25. static const ERole SDL_IMMDevice_role = eConsole; /* !!! FIXME: should this be eMultimedia? Should be a hint? */
  26. /* This is global to the WASAPI target, to handle hotplug and default device lookup. */
  27. static IMMDeviceEnumerator *enumerator = NULL;
  28. /* PropVariantInit() is an inline function/macro in PropIdl.h that calls the C runtime's memset() directly. Use ours instead, to avoid dependency. */
  29. #ifdef PropVariantInit
  30. #undef PropVariantInit
  31. #endif
  32. #define PropVariantInit(p) SDL_zerop(p)
  33. /* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */
  34. /* *INDENT-OFF* */ /* clang-format off */
  35. static const CLSID SDL_CLSID_MMDeviceEnumerator = { 0xbcde0395, 0xe52f, 0x467c,{ 0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e } };
  36. static const IID SDL_IID_IMMDeviceEnumerator = { 0xa95664d2, 0x9614, 0x4f35,{ 0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6 } };
  37. static const IID SDL_IID_IMMNotificationClient = { 0x7991eec9, 0x7e89, 0x4d85,{ 0x83, 0x90, 0x6c, 0x70, 0x3c, 0xec, 0x60, 0xc0 } };
  38. static const IID SDL_IID_IMMEndpoint = { 0x1be09788, 0x6894, 0x4089,{ 0x85, 0x86, 0x9a, 0x2a, 0x6c, 0x26, 0x5a, 0xc5 } };
  39. static const PROPERTYKEY SDL_PKEY_Device_FriendlyName = { { 0xa45c254e, 0xdf1c, 0x4efd,{ 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, } }, 14 };
  40. static const PROPERTYKEY SDL_PKEY_AudioEngine_DeviceFormat = { { 0xf19f064d, 0x82c, 0x4e27,{ 0xbc, 0x73, 0x68, 0x82, 0xa1, 0xbb, 0x8e, 0x4c, } }, 0 };
  41. static const PROPERTYKEY SDL_PKEY_AudioEndpoint_GUID = { { 0x1da5d803, 0xd492, 0x4edd,{ 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e, } }, 4 };
  42. static const GUID SDL_KSDATAFORMAT_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
  43. static const GUID SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
  44. /* *INDENT-ON* */ /* clang-format on */
  45. /* these increment as default devices change. Opened default devices pick up changes in their threads. */
  46. SDL_atomic_t SDL_IMMDevice_DefaultPlaybackGeneration;
  47. SDL_atomic_t SDL_IMMDevice_DefaultCaptureGeneration;
  48. static void GetMMDeviceInfo(IMMDevice *device, char **utf8dev, WAVEFORMATEXTENSIBLE *fmt, GUID *guid)
  49. {
  50. /* PKEY_Device_FriendlyName gives you "Speakers (SoundBlaster Pro)" which drives me nuts. I'd rather it be
  51. "SoundBlaster Pro (Speakers)" but I guess that's developers vs users. Windows uses the FriendlyName in
  52. its own UIs, like Volume Control, etc. */
  53. IPropertyStore *props = NULL;
  54. *utf8dev = NULL;
  55. SDL_zerop(fmt);
  56. if (SUCCEEDED(IMMDevice_OpenPropertyStore(device, STGM_READ, &props))) {
  57. PROPVARIANT var;
  58. PropVariantInit(&var);
  59. if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_Device_FriendlyName, &var))) {
  60. *utf8dev = WIN_StringToUTF8W(var.pwszVal);
  61. }
  62. PropVariantClear(&var);
  63. if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_AudioEngine_DeviceFormat, &var))) {
  64. SDL_memcpy(fmt, var.blob.pBlobData, SDL_min(var.blob.cbSize, sizeof(WAVEFORMATEXTENSIBLE)));
  65. }
  66. PropVariantClear(&var);
  67. if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_AudioEndpoint_GUID, &var))) {
  68. CLSIDFromString(var.pwszVal, guid);
  69. }
  70. PropVariantClear(&var);
  71. IPropertyStore_Release(props);
  72. }
  73. }
  74. /* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */
  75. typedef struct DevIdList
  76. {
  77. LPWSTR str;
  78. LPGUID guid;
  79. struct DevIdList *next;
  80. } DevIdList;
  81. static DevIdList *deviceid_list = NULL;
  82. static void SDL_IMMDevice_Remove(const SDL_bool iscapture, LPCWSTR devid, SDL_bool useguid)
  83. {
  84. DevIdList *i;
  85. DevIdList *next;
  86. DevIdList *prev = NULL;
  87. for (i = deviceid_list; i; i = next) {
  88. next = i->next;
  89. if (SDL_wcscmp(i->str, devid) == 0) {
  90. if (prev) {
  91. prev->next = next;
  92. } else {
  93. deviceid_list = next;
  94. }
  95. SDL_RemoveAudioDevice(iscapture, useguid ? ((void *)i->guid) : ((void *)i->str));
  96. SDL_free(i->str);
  97. SDL_free(i);
  98. } else {
  99. prev = i;
  100. }
  101. }
  102. }
  103. static void SDL_IMMDevice_Add(const SDL_bool iscapture, const char *devname, WAVEFORMATEXTENSIBLE *fmt, LPCWSTR devid, GUID *dsoundguid, SDL_bool useguid)
  104. {
  105. DevIdList *devidlist;
  106. SDL_AudioSpec spec;
  107. LPWSTR devidcopy;
  108. LPGUID cpyguid;
  109. LPVOID driverdata;
  110. /* You can have multiple endpoints on a device that are mutually exclusive ("Speakers" vs "Line Out" or whatever).
  111. In a perfect world, things that are unplugged won't be in this collection. The only gotcha is probably for
  112. phones and tablets, where you might have an internal speaker and a headphone jack and expect both to be
  113. available and switch automatically. (!!! FIXME...?) */
  114. /* see if we already have this one. */
  115. for (devidlist = deviceid_list; devidlist; devidlist = devidlist->next) {
  116. if (SDL_wcscmp(devidlist->str, devid) == 0) {
  117. return; /* we already have this. */
  118. }
  119. }
  120. devidlist = (DevIdList *)SDL_malloc(sizeof(*devidlist));
  121. if (!devidlist) {
  122. return; /* oh well. */
  123. }
  124. devidcopy = SDL_wcsdup(devid);
  125. if (!devidcopy) {
  126. SDL_free(devidlist);
  127. return; /* oh well. */
  128. }
  129. if (useguid) {
  130. /* This is freed by DSOUND_FreeDeviceData! */
  131. cpyguid = (LPGUID)SDL_malloc(sizeof(GUID));
  132. if (!cpyguid) {
  133. SDL_free(devidlist);
  134. SDL_free(devidcopy);
  135. return; /* oh well. */
  136. }
  137. SDL_memcpy(cpyguid, dsoundguid, sizeof(GUID));
  138. driverdata = cpyguid;
  139. } else {
  140. cpyguid = NULL;
  141. driverdata = devidcopy;
  142. }
  143. devidlist->str = devidcopy;
  144. devidlist->guid = cpyguid;
  145. devidlist->next = deviceid_list;
  146. deviceid_list = devidlist;
  147. SDL_zero(spec);
  148. spec.channels = (Uint8)fmt->Format.nChannels;
  149. spec.freq = fmt->Format.nSamplesPerSec;
  150. spec.format = WaveFormatToSDLFormat((WAVEFORMATEX *)fmt);
  151. SDL_AddAudioDevice(iscapture, devname, &spec, driverdata);
  152. }
  153. /* We need a COM subclass of IMMNotificationClient for hotplug support, which is
  154. easy in C++, but we have to tapdance more to make work in C.
  155. Thanks to this page for coaching on how to make this work:
  156. https://www.codeproject.com/Articles/13601/COM-in-plain-C */
  157. typedef struct SDLMMNotificationClient
  158. {
  159. const IMMNotificationClientVtbl *lpVtbl;
  160. SDL_atomic_t refcount;
  161. SDL_bool useguid;
  162. } SDLMMNotificationClient;
  163. static HRESULT STDMETHODCALLTYPE SDLMMNotificationClient_QueryInterface(IMMNotificationClient *this, REFIID iid, void **ppv)
  164. {
  165. if ((WIN_IsEqualIID(iid, &IID_IUnknown)) || (WIN_IsEqualIID(iid, &SDL_IID_IMMNotificationClient))) {
  166. *ppv = this;
  167. this->lpVtbl->AddRef(this);
  168. return S_OK;
  169. }
  170. *ppv = NULL;
  171. return E_NOINTERFACE;
  172. }
  173. static ULONG STDMETHODCALLTYPE SDLMMNotificationClient_AddRef(IMMNotificationClient *ithis)
  174. {
  175. SDLMMNotificationClient *this = (SDLMMNotificationClient *)ithis;
  176. return (ULONG)(SDL_AtomicIncRef(&this->refcount) + 1);
  177. }
  178. static ULONG STDMETHODCALLTYPE SDLMMNotificationClient_Release(IMMNotificationClient *ithis)
  179. {
  180. /* this is a static object; we don't ever free it. */
  181. SDLMMNotificationClient *this = (SDLMMNotificationClient *)ithis;
  182. const ULONG retval = SDL_AtomicDecRef(&this->refcount);
  183. if (retval == 0) {
  184. SDL_AtomicSet(&this->refcount, 0); /* uhh... */
  185. return 0;
  186. }
  187. return retval - 1;
  188. }
  189. /* These are the entry points called when WASAPI device endpoints change. */
  190. static HRESULT STDMETHODCALLTYPE SDLMMNotificationClient_OnDefaultDeviceChanged(IMMNotificationClient *ithis, EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId)
  191. {
  192. if (role != SDL_IMMDevice_role) {
  193. return S_OK; /* ignore it. */
  194. }
  195. /* Increment the "generation," so opened devices will pick this up in their threads. */
  196. switch (flow) {
  197. case eRender:
  198. SDL_AtomicAdd(&SDL_IMMDevice_DefaultPlaybackGeneration, 1);
  199. break;
  200. case eCapture:
  201. SDL_AtomicAdd(&SDL_IMMDevice_DefaultCaptureGeneration, 1);
  202. break;
  203. case eAll:
  204. SDL_AtomicAdd(&SDL_IMMDevice_DefaultPlaybackGeneration, 1);
  205. SDL_AtomicAdd(&SDL_IMMDevice_DefaultCaptureGeneration, 1);
  206. break;
  207. default:
  208. SDL_assert(!"uhoh, unexpected OnDefaultDeviceChange flow!");
  209. break;
  210. }
  211. return S_OK;
  212. }
  213. static HRESULT STDMETHODCALLTYPE SDLMMNotificationClient_OnDeviceAdded(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
  214. {
  215. /* we ignore this; devices added here then progress to ACTIVE, if appropriate, in
  216. OnDeviceStateChange, making that a better place to deal with device adds. More
  217. importantly: the first time you plug in a USB audio device, this callback will
  218. fire, but when you unplug it, it isn't removed (it's state changes to NOTPRESENT).
  219. Plugging it back in won't fire this callback again. */
  220. return S_OK;
  221. }
  222. static HRESULT STDMETHODCALLTYPE SDLMMNotificationClient_OnDeviceRemoved(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
  223. {
  224. /* See notes in OnDeviceAdded handler about why we ignore this. */
  225. return S_OK;
  226. }
  227. static HRESULT STDMETHODCALLTYPE SDLMMNotificationClient_OnDeviceStateChanged(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId, DWORD dwNewState)
  228. {
  229. IMMDevice *device = NULL;
  230. if (SUCCEEDED(IMMDeviceEnumerator_GetDevice(enumerator, pwstrDeviceId, &device))) {
  231. IMMEndpoint *endpoint = NULL;
  232. if (SUCCEEDED(IMMDevice_QueryInterface(device, &SDL_IID_IMMEndpoint, (void **)&endpoint))) {
  233. EDataFlow flow;
  234. if (SUCCEEDED(IMMEndpoint_GetDataFlow(endpoint, &flow))) {
  235. const SDL_bool iscapture = (flow == eCapture);
  236. const SDLMMNotificationClient *client = (SDLMMNotificationClient *)ithis;
  237. if (dwNewState == DEVICE_STATE_ACTIVE) {
  238. char *utf8dev;
  239. WAVEFORMATEXTENSIBLE fmt;
  240. GUID dsoundguid;
  241. GetMMDeviceInfo(device, &utf8dev, &fmt, &dsoundguid);
  242. if (utf8dev) {
  243. SDL_IMMDevice_Add(iscapture, utf8dev, &fmt, pwstrDeviceId, &dsoundguid, client->useguid);
  244. SDL_free(utf8dev);
  245. }
  246. } else {
  247. SDL_IMMDevice_Remove(iscapture, pwstrDeviceId, client->useguid);
  248. }
  249. }
  250. IMMEndpoint_Release(endpoint);
  251. }
  252. IMMDevice_Release(device);
  253. }
  254. return S_OK;
  255. }
  256. static HRESULT STDMETHODCALLTYPE SDLMMNotificationClient_OnPropertyValueChanged(IMMNotificationClient *this, LPCWSTR pwstrDeviceId, const PROPERTYKEY key)
  257. {
  258. return S_OK; /* we don't care about these. */
  259. }
  260. static const IMMNotificationClientVtbl notification_client_vtbl = {
  261. SDLMMNotificationClient_QueryInterface,
  262. SDLMMNotificationClient_AddRef,
  263. SDLMMNotificationClient_Release,
  264. SDLMMNotificationClient_OnDeviceStateChanged,
  265. SDLMMNotificationClient_OnDeviceAdded,
  266. SDLMMNotificationClient_OnDeviceRemoved,
  267. SDLMMNotificationClient_OnDefaultDeviceChanged,
  268. SDLMMNotificationClient_OnPropertyValueChanged
  269. };
  270. static SDLMMNotificationClient notification_client = { &notification_client_vtbl, { 1 } };
  271. int SDL_IMMDevice_Init(void)
  272. {
  273. HRESULT ret;
  274. SDL_AtomicSet(&SDL_IMMDevice_DefaultPlaybackGeneration, 1);
  275. SDL_AtomicSet(&SDL_IMMDevice_DefaultCaptureGeneration, 1);
  276. /* just skip the discussion with COM here. */
  277. if (!WIN_IsWindowsVistaOrGreater()) {
  278. return SDL_SetError("WASAPI support requires Windows Vista or later");
  279. }
  280. if (FAILED(WIN_CoInitialize())) {
  281. return SDL_SetError("WASAPI: CoInitialize() failed");
  282. }
  283. ret = CoCreateInstance(&SDL_CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &SDL_IID_IMMDeviceEnumerator, (LPVOID *)&enumerator);
  284. if (FAILED(ret)) {
  285. WIN_CoUninitialize();
  286. return WIN_SetErrorFromHRESULT("WASAPI CoCreateInstance(MMDeviceEnumerator)", ret);
  287. }
  288. return 0;
  289. }
  290. void SDL_IMMDevice_Quit(void)
  291. {
  292. DevIdList *devidlist;
  293. DevIdList *next;
  294. if (enumerator) {
  295. IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *)&notification_client);
  296. IMMDeviceEnumerator_Release(enumerator);
  297. enumerator = NULL;
  298. }
  299. WIN_CoUninitialize();
  300. for (devidlist = deviceid_list; devidlist; devidlist = next) {
  301. next = devidlist->next;
  302. SDL_free(devidlist->str);
  303. SDL_free(devidlist);
  304. }
  305. deviceid_list = NULL;
  306. }
  307. int SDL_IMMDevice_Get(LPCWSTR devid, IMMDevice **device, SDL_bool iscapture)
  308. {
  309. const Uint64 timeout = SDL_GetTicks64() + 8000; /* intel's audio drivers can fail for up to EIGHT SECONDS after a device is connected or we wake from sleep. */
  310. HRESULT ret;
  311. SDL_assert(device != NULL);
  312. while (SDL_TRUE) {
  313. if (!devid) {
  314. const EDataFlow dataflow = iscapture ? eCapture : eRender;
  315. ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_IMMDevice_role, device);
  316. } else {
  317. ret = IMMDeviceEnumerator_GetDevice(enumerator, devid, device);
  318. }
  319. if (SUCCEEDED(ret)) {
  320. break;
  321. }
  322. if (ret == E_NOTFOUND) {
  323. const Uint64 now = SDL_GetTicks64();
  324. if (timeout > now) {
  325. const Uint64 ticksleft = timeout - now;
  326. SDL_Delay((Uint32)SDL_min(ticksleft, 300)); /* wait awhile and try again. */
  327. continue;
  328. }
  329. }
  330. return WIN_SetErrorFromHRESULT("WASAPI can't find requested audio endpoint", ret);
  331. }
  332. return 0;
  333. }
  334. typedef struct
  335. {
  336. LPWSTR devid;
  337. char *devname;
  338. WAVEFORMATEXTENSIBLE fmt;
  339. GUID dsoundguid;
  340. } EndpointItem;
  341. static int SDLCALL sort_endpoints(const void *_a, const void *_b)
  342. {
  343. LPWSTR a = ((const EndpointItem *)_a)->devid;
  344. LPWSTR b = ((const EndpointItem *)_b)->devid;
  345. if (!a && !b) {
  346. return 0;
  347. } else if (!a && b) {
  348. return -1;
  349. } else if (a && !b) {
  350. return 1;
  351. }
  352. while (SDL_TRUE) {
  353. if (*a < *b) {
  354. return -1;
  355. } else if (*a > *b) {
  356. return 1;
  357. } else if (*a == 0) {
  358. break;
  359. }
  360. a++;
  361. b++;
  362. }
  363. return 0;
  364. }
  365. static void EnumerateEndpointsForFlow(const SDL_bool iscapture)
  366. {
  367. IMMDeviceCollection *collection = NULL;
  368. EndpointItem *items;
  369. UINT i, total;
  370. /* Note that WASAPI separates "adapter devices" from "audio endpoint devices"
  371. ...one adapter device ("SoundBlaster Pro") might have multiple endpoint devices ("Speakers", "Line-Out"). */
  372. if (FAILED(IMMDeviceEnumerator_EnumAudioEndpoints(enumerator, iscapture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &collection))) {
  373. return;
  374. }
  375. if (FAILED(IMMDeviceCollection_GetCount(collection, &total))) {
  376. IMMDeviceCollection_Release(collection);
  377. return;
  378. }
  379. items = (EndpointItem *)SDL_calloc(total, sizeof(EndpointItem));
  380. if (!items) {
  381. return; /* oh well. */
  382. }
  383. for (i = 0; i < total; i++) {
  384. EndpointItem *item = items + i;
  385. IMMDevice *device = NULL;
  386. if (SUCCEEDED(IMMDeviceCollection_Item(collection, i, &device))) {
  387. if (SUCCEEDED(IMMDevice_GetId(device, &item->devid))) {
  388. GetMMDeviceInfo(device, &item->devname, &item->fmt, &item->dsoundguid);
  389. }
  390. IMMDevice_Release(device);
  391. }
  392. }
  393. /* sort the list of devices by their guid so list is consistent between runs */
  394. SDL_qsort(items, total, sizeof(*items), sort_endpoints);
  395. /* Send the sorted list on to the SDL's higher level. */
  396. for (i = 0; i < total; i++) {
  397. EndpointItem *item = items + i;
  398. if ((item->devid) && (item->devname)) {
  399. SDL_IMMDevice_Add(iscapture, item->devname, &item->fmt, item->devid, &item->dsoundguid, notification_client.useguid);
  400. }
  401. SDL_free(item->devname);
  402. CoTaskMemFree(item->devid);
  403. }
  404. SDL_free(items);
  405. IMMDeviceCollection_Release(collection);
  406. }
  407. void SDL_IMMDevice_EnumerateEndpoints(SDL_bool useguid)
  408. {
  409. notification_client.useguid = useguid;
  410. EnumerateEndpointsForFlow(SDL_FALSE); /* playback */
  411. EnumerateEndpointsForFlow(SDL_TRUE); /* capture */
  412. /* if this fails, we just won't get hotplug events. Carry on anyhow. */
  413. IMMDeviceEnumerator_RegisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *)&notification_client);
  414. }
  415. int SDL_IMMDevice_GetDefaultAudioInfo(char **name, SDL_AudioSpec *spec, int iscapture)
  416. {
  417. WAVEFORMATEXTENSIBLE fmt;
  418. IMMDevice *device = NULL;
  419. char *filler;
  420. GUID morefiller;
  421. const EDataFlow dataflow = iscapture ? eCapture : eRender;
  422. HRESULT ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_IMMDevice_role, &device);
  423. if (FAILED(ret)) {
  424. SDL_assert(device == NULL);
  425. return WIN_SetErrorFromHRESULT("WASAPI can't find default audio endpoint", ret);
  426. }
  427. if (!name) {
  428. name = &filler;
  429. }
  430. SDL_zero(fmt);
  431. GetMMDeviceInfo(device, name, &fmt, &morefiller);
  432. IMMDevice_Release(device);
  433. if (name == &filler) {
  434. SDL_free(filler);
  435. }
  436. SDL_zerop(spec);
  437. spec->channels = (Uint8)fmt.Format.nChannels;
  438. spec->freq = fmt.Format.nSamplesPerSec;
  439. spec->format = WaveFormatToSDLFormat((WAVEFORMATEX *)&fmt);
  440. return 0;
  441. }
  442. SDL_AudioFormat WaveFormatToSDLFormat(WAVEFORMATEX *waveformat)
  443. {
  444. if ((waveformat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) && (waveformat->wBitsPerSample == 32)) {
  445. return AUDIO_F32SYS;
  446. } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 16)) {
  447. return AUDIO_S16SYS;
  448. } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 32)) {
  449. return AUDIO_S32SYS;
  450. } else if (waveformat->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
  451. const WAVEFORMATEXTENSIBLE *ext = (const WAVEFORMATEXTENSIBLE *)waveformat;
  452. if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
  453. return AUDIO_F32SYS;
  454. } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 16)) {
  455. return AUDIO_S16SYS;
  456. } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
  457. return AUDIO_S32SYS;
  458. }
  459. }
  460. return 0;
  461. }
  462. #endif /* (defined(__WIN32__) || defined(__GDK__)) && HAVE_MMDEVICEAPI_H */