audio_driver_wasapi.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /**************************************************************************/
  2. /* audio_driver_wasapi.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifdef WASAPI_ENABLED
  31. #include "audio_driver_wasapi.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/os/os.h"
  34. #include <stdint.h> // INT32_MAX
  35. #include <functiondiscoverykeys.h>
  36. #include <wrl/client.h>
  37. using Microsoft::WRL::ComPtr;
  38. // Define IAudioClient3 if not already defined by MinGW headers
  39. #if defined __MINGW32__ || defined __MINGW64__
  40. #ifndef __IAudioClient3_FWD_DEFINED__
  41. #define __IAudioClient3_FWD_DEFINED__
  42. typedef interface IAudioClient3 IAudioClient3;
  43. #endif // __IAudioClient3_FWD_DEFINED__
  44. #ifndef __IAudioClient3_INTERFACE_DEFINED__
  45. #define __IAudioClient3_INTERFACE_DEFINED__
  46. MIDL_INTERFACE("7ED4EE07-8E67-4CD4-8C1A-2B7A5987AD42")
  47. IAudioClient3 : public IAudioClient2 {
  48. public:
  49. virtual HRESULT STDMETHODCALLTYPE GetSharedModeEnginePeriod(
  50. /* [annotation][in] */
  51. _In_ const WAVEFORMATEX *pFormat,
  52. /* [annotation][out] */
  53. _Out_ UINT32 *pDefaultPeriodInFrames,
  54. /* [annotation][out] */
  55. _Out_ UINT32 *pFundamentalPeriodInFrames,
  56. /* [annotation][out] */
  57. _Out_ UINT32 *pMinPeriodInFrames,
  58. /* [annotation][out] */
  59. _Out_ UINT32 *pMaxPeriodInFrames) = 0;
  60. virtual HRESULT STDMETHODCALLTYPE GetCurrentSharedModeEnginePeriod(
  61. /* [unique][annotation][out] */
  62. _Out_ WAVEFORMATEX * *ppFormat,
  63. /* [annotation][out] */
  64. _Out_ UINT32 * pCurrentPeriodInFrames) = 0;
  65. virtual HRESULT STDMETHODCALLTYPE InitializeSharedAudioStream(
  66. /* [annotation][in] */
  67. _In_ DWORD StreamFlags,
  68. /* [annotation][in] */
  69. _In_ UINT32 PeriodInFrames,
  70. /* [annotation][in] */
  71. _In_ const WAVEFORMATEX *pFormat,
  72. /* [annotation][in] */
  73. _In_opt_ LPCGUID AudioSessionGuid) = 0;
  74. }
  75. __CRT_UUID_DECL(IAudioClient3, 0x7ED4EE07, 0x8E67, 0x4CD4, 0x8C, 0x1A, 0x2B, 0x7A, 0x59, 0x87, 0xAD, 0x42)
  76. #endif // __IAudioClient3_INTERFACE_DEFINED__
  77. #endif // __MINGW32__ || __MINGW64__
  78. #ifndef PKEY_Device_FriendlyName
  79. #undef DEFINE_PROPERTYKEY
  80. /* clang-format off */
  81. #define DEFINE_PROPERTYKEY(id, a, b, c, d, e, f, g, h, i, j, k, l) \
  82. const PROPERTYKEY id = { { a, b, c, { d, e, f, g, h, i, j, k, } }, l };
  83. /* clang-format on */
  84. DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);
  85. #endif
  86. const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
  87. const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
  88. const IID IID_IAudioClient = __uuidof(IAudioClient);
  89. const IID IID_IAudioClient3 = __uuidof(IAudioClient3);
  90. const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
  91. const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient);
  92. #define SAFE_RELEASE(memory) \
  93. if ((memory) != nullptr) { \
  94. (memory)->Release(); \
  95. (memory) = nullptr; \
  96. }
  97. #define REFTIMES_PER_SEC 10000000
  98. #define REFTIMES_PER_MILLISEC 10000
  99. #define CAPTURE_BUFFER_CHANNELS 2
  100. static bool default_output_device_changed = false;
  101. static bool default_input_device_changed = false;
  102. // Silence warning due to a COM API weirdness (GH-35194).
  103. #if defined(__GNUC__) && !defined(__clang__)
  104. #pragma GCC diagnostic push
  105. #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  106. #endif
  107. class CMMNotificationClient : public IMMNotificationClient {
  108. LONG _cRef = 1;
  109. public:
  110. CMMNotificationClient() {}
  111. virtual ~CMMNotificationClient() {}
  112. ULONG STDMETHODCALLTYPE AddRef() {
  113. return InterlockedIncrement(&_cRef);
  114. }
  115. ULONG STDMETHODCALLTYPE Release() {
  116. ULONG ulRef = InterlockedDecrement(&_cRef);
  117. if (0 == ulRef) {
  118. delete this;
  119. }
  120. return ulRef;
  121. }
  122. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) {
  123. if (IID_IUnknown == riid) {
  124. AddRef();
  125. *ppvInterface = (IUnknown *)this;
  126. } else if (__uuidof(IMMNotificationClient) == riid) {
  127. AddRef();
  128. *ppvInterface = (IMMNotificationClient *)this;
  129. } else {
  130. *ppvInterface = nullptr;
  131. return E_NOINTERFACE;
  132. }
  133. return S_OK;
  134. }
  135. HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) {
  136. return S_OK;
  137. }
  138. HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) {
  139. return S_OK;
  140. }
  141. HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) {
  142. return S_OK;
  143. }
  144. HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) {
  145. if (role == eConsole) {
  146. if (flow == eRender) {
  147. default_output_device_changed = true;
  148. } else if (flow == eCapture) {
  149. default_input_device_changed = true;
  150. }
  151. }
  152. return S_OK;
  153. }
  154. HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) {
  155. return S_OK;
  156. }
  157. };
  158. #if defined(__GNUC__) && !defined(__clang__)
  159. #pragma GCC diagnostic pop
  160. #endif
  161. static CMMNotificationClient notif_client;
  162. Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_input, bool p_reinit, bool p_no_audio_client_3) {
  163. WAVEFORMATEX *pwfex;
  164. ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
  165. ComPtr<IMMDevice> output_device = nullptr;
  166. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  167. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  168. if (p_device->device_name == "Default") {
  169. hr = enumerator->GetDefaultAudioEndpoint(p_input ? eCapture : eRender, eConsole, &output_device);
  170. } else {
  171. ComPtr<IMMDeviceCollection> devices = nullptr;
  172. hr = enumerator->EnumAudioEndpoints(p_input ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
  173. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  174. LPWSTR strId = nullptr;
  175. bool found = false;
  176. UINT count = 0;
  177. hr = devices->GetCount(&count);
  178. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  179. for (ULONG i = 0; i < count && !found; i++) {
  180. ComPtr<IMMDevice> tmp_device = nullptr;
  181. hr = devices->Item(i, &tmp_device);
  182. ERR_BREAK(hr != S_OK);
  183. ComPtr<IPropertyStore> props = nullptr;
  184. hr = tmp_device->OpenPropertyStore(STGM_READ, &props);
  185. ERR_BREAK(hr != S_OK);
  186. PROPVARIANT propvar;
  187. PropVariantInit(&propvar);
  188. hr = props->GetValue(PKEY_Device_FriendlyName, &propvar);
  189. ERR_BREAK(hr != S_OK);
  190. if (p_device->device_name == String(propvar.pwszVal)) {
  191. hr = tmp_device->GetId(&strId);
  192. ERR_BREAK(hr != S_OK);
  193. found = true;
  194. }
  195. PropVariantClear(&propvar);
  196. }
  197. if (found) {
  198. hr = enumerator->GetDevice(strId, &output_device);
  199. }
  200. if (strId) {
  201. CoTaskMemFree(strId);
  202. }
  203. if (output_device == nullptr) {
  204. hr = enumerator->GetDefaultAudioEndpoint(p_input ? eCapture : eRender, eConsole, &output_device);
  205. }
  206. }
  207. if (p_reinit) {
  208. // In case we're trying to re-initialize the device, prevent throwing this error on the console,
  209. // otherwise if there is currently no device available this will spam the console.
  210. if (hr != S_OK) {
  211. return ERR_CANT_OPEN;
  212. }
  213. } else {
  214. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  215. }
  216. hr = enumerator->RegisterEndpointNotificationCallback(&notif_client);
  217. if (hr != S_OK) {
  218. ERR_PRINT("WASAPI: RegisterEndpointNotificationCallback error");
  219. }
  220. using_audio_client_3 = !p_input; // IID_IAudioClient3 is only used for adjustable output latency (not input)
  221. if (p_no_audio_client_3) {
  222. using_audio_client_3 = false;
  223. }
  224. if (using_audio_client_3) {
  225. hr = output_device->Activate(IID_IAudioClient3, CLSCTX_ALL, nullptr, (void **)&p_device->audio_client);
  226. if (hr != S_OK) {
  227. // IID_IAudioClient3 will never activate on OS versions before Windows 10.
  228. // Older Windows versions should fall back gracefully.
  229. using_audio_client_3 = false;
  230. print_verbose("WASAPI: Couldn't activate output_device with IAudioClient3 interface, falling back to IAudioClient interface");
  231. } else {
  232. print_verbose("WASAPI: Activated output_device using IAudioClient3 interface");
  233. }
  234. }
  235. if (!using_audio_client_3) {
  236. hr = output_device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void **)&p_device->audio_client);
  237. }
  238. if (p_reinit) {
  239. if (hr != S_OK) {
  240. return ERR_CANT_OPEN;
  241. }
  242. } else {
  243. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  244. }
  245. if (using_audio_client_3) {
  246. AudioClientProperties audioProps{};
  247. audioProps.cbSize = sizeof(AudioClientProperties);
  248. audioProps.bIsOffload = FALSE;
  249. audioProps.eCategory = AudioCategory_GameEffects;
  250. hr = ((IAudioClient3 *)p_device->audio_client)->SetClientProperties(&audioProps);
  251. ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_CANT_OPEN, "WASAPI: SetClientProperties failed with error 0x" + String::num_uint64(hr, 16) + ".");
  252. }
  253. hr = p_device->audio_client->GetMixFormat(&pwfex);
  254. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  255. print_verbose("WASAPI: wFormatTag = " + itos(pwfex->wFormatTag));
  256. print_verbose("WASAPI: nChannels = " + itos(pwfex->nChannels));
  257. print_verbose("WASAPI: nSamplesPerSec = " + itos(pwfex->nSamplesPerSec));
  258. print_verbose("WASAPI: nAvgBytesPerSec = " + itos(pwfex->nAvgBytesPerSec));
  259. print_verbose("WASAPI: nBlockAlign = " + itos(pwfex->nBlockAlign));
  260. print_verbose("WASAPI: wBitsPerSample = " + itos(pwfex->wBitsPerSample));
  261. print_verbose("WASAPI: cbSize = " + itos(pwfex->cbSize));
  262. WAVEFORMATEX *closest = nullptr;
  263. hr = p_device->audio_client->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, pwfex, &closest);
  264. if (hr == S_FALSE) {
  265. WARN_PRINT("WASAPI: Mix format is not supported by the output_device");
  266. if (closest) {
  267. print_verbose("WASAPI: closest->wFormatTag = " + itos(closest->wFormatTag));
  268. print_verbose("WASAPI: closest->nChannels = " + itos(closest->nChannels));
  269. print_verbose("WASAPI: closest->nSamplesPerSec = " + itos(closest->nSamplesPerSec));
  270. print_verbose("WASAPI: closest->nAvgBytesPerSec = " + itos(closest->nAvgBytesPerSec));
  271. print_verbose("WASAPI: closest->nBlockAlign = " + itos(closest->nBlockAlign));
  272. print_verbose("WASAPI: closest->wBitsPerSample = " + itos(closest->wBitsPerSample));
  273. print_verbose("WASAPI: closest->cbSize = " + itos(closest->cbSize));
  274. WARN_PRINT("WASAPI: Using closest match instead");
  275. pwfex = closest;
  276. }
  277. }
  278. // Since we're using WASAPI Shared Mode we can't control any of these, we just tag along
  279. p_device->channels = pwfex->nChannels;
  280. p_device->format_tag = pwfex->wFormatTag;
  281. p_device->bits_per_sample = pwfex->wBitsPerSample;
  282. p_device->frame_size = (p_device->bits_per_sample / 8) * p_device->channels;
  283. if (p_device->format_tag == WAVE_FORMAT_EXTENSIBLE) {
  284. WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex;
  285. if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
  286. p_device->format_tag = WAVE_FORMAT_PCM;
  287. } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
  288. p_device->format_tag = WAVE_FORMAT_IEEE_FLOAT;
  289. } else {
  290. ERR_PRINT("WASAPI: Format not supported");
  291. ERR_FAIL_V(ERR_CANT_OPEN);
  292. }
  293. } else {
  294. if (p_device->format_tag != WAVE_FORMAT_PCM && p_device->format_tag != WAVE_FORMAT_IEEE_FLOAT) {
  295. ERR_PRINT("WASAPI: Format not supported");
  296. ERR_FAIL_V(ERR_CANT_OPEN);
  297. }
  298. }
  299. if (!using_audio_client_3) {
  300. DWORD streamflags = 0;
  301. if ((DWORD)mix_rate != pwfex->nSamplesPerSec) {
  302. streamflags |= AUDCLNT_STREAMFLAGS_RATEADJUST;
  303. pwfex->nSamplesPerSec = mix_rate;
  304. pwfex->nAvgBytesPerSec = pwfex->nSamplesPerSec * pwfex->nChannels * (pwfex->wBitsPerSample / 8);
  305. }
  306. hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, p_input ? REFTIMES_PER_SEC : 0, 0, pwfex, nullptr);
  307. ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_CANT_OPEN, "WASAPI: Initialize failed with error 0x" + String::num_uint64(hr, 16) + ".");
  308. UINT32 max_frames;
  309. hr = p_device->audio_client->GetBufferSize(&max_frames);
  310. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  311. // Due to WASAPI Shared Mode we have no control of the buffer size
  312. if (!p_input) {
  313. buffer_frames = max_frames;
  314. int64_t latency = 0;
  315. audio_output.audio_client->GetStreamLatency(&latency);
  316. // WASAPI REFERENCE_TIME units are 100 nanoseconds per unit
  317. // https://docs.microsoft.com/en-us/windows/win32/directshow/reference-time
  318. // Convert REFTIME to seconds as godot uses for latency
  319. real_latency = (float)latency / (float)REFTIMES_PER_SEC;
  320. }
  321. } else {
  322. IAudioClient3 *device_audio_client_3 = (IAudioClient3 *)p_device->audio_client;
  323. // AUDCLNT_STREAMFLAGS_RATEADJUST is an invalid flag with IAudioClient3, therefore we have to use
  324. // the closest supported mix rate supported by the audio driver.
  325. mix_rate = pwfex->nSamplesPerSec;
  326. print_verbose("WASAPI: mix_rate = " + itos(mix_rate));
  327. UINT32 default_period_frames, fundamental_period_frames, min_period_frames, max_period_frames;
  328. hr = device_audio_client_3->GetSharedModeEnginePeriod(
  329. pwfex,
  330. &default_period_frames,
  331. &fundamental_period_frames,
  332. &min_period_frames,
  333. &max_period_frames);
  334. if (hr != S_OK) {
  335. print_verbose("WASAPI: GetSharedModeEnginePeriod failed with error 0x" + String::num_uint64(hr, 16) + ", falling back to IAudioClient.");
  336. CoTaskMemFree(pwfex);
  337. return audio_device_init(p_device, p_input, p_reinit, true);
  338. }
  339. // Period frames must be an integral multiple of fundamental_period_frames or IAudioClient3 initialization will fail,
  340. // so we need to select the closest multiple to the user-specified latency.
  341. UINT32 desired_period_frames = target_latency_ms * mix_rate / 1000;
  342. UINT32 period_frames = (desired_period_frames / fundamental_period_frames) * fundamental_period_frames;
  343. if (ABS((int64_t)period_frames - (int64_t)desired_period_frames) > ABS((int64_t)(period_frames + fundamental_period_frames) - (int64_t)desired_period_frames)) {
  344. period_frames = period_frames + fundamental_period_frames;
  345. }
  346. period_frames = CLAMP(period_frames, min_period_frames, max_period_frames);
  347. print_verbose("WASAPI: fundamental_period_frames = " + itos(fundamental_period_frames));
  348. print_verbose("WASAPI: min_period_frames = " + itos(min_period_frames));
  349. print_verbose("WASAPI: max_period_frames = " + itos(max_period_frames));
  350. print_verbose("WASAPI: selected a period frame size of " + itos(period_frames));
  351. buffer_frames = period_frames;
  352. hr = device_audio_client_3->InitializeSharedAudioStream(0, period_frames, pwfex, nullptr);
  353. if (hr != S_OK) {
  354. print_verbose("WASAPI: InitializeSharedAudioStream failed with error 0x" + String::num_uint64(hr, 16) + ", falling back to IAudioClient.");
  355. CoTaskMemFree(pwfex);
  356. return audio_device_init(p_device, p_input, p_reinit, true);
  357. } else {
  358. uint32_t output_latency_in_frames;
  359. WAVEFORMATEX *current_pwfex;
  360. hr = device_audio_client_3->GetCurrentSharedModeEnginePeriod(&current_pwfex, &output_latency_in_frames);
  361. if (hr == OK) {
  362. real_latency = (float)output_latency_in_frames / (float)current_pwfex->nSamplesPerSec;
  363. CoTaskMemFree(current_pwfex);
  364. } else {
  365. print_verbose("WASAPI: GetCurrentSharedModeEnginePeriod failed with error 0x" + String::num_uint64(hr, 16) + ", falling back to IAudioClient.");
  366. CoTaskMemFree(pwfex);
  367. return audio_device_init(p_device, p_input, p_reinit, true);
  368. }
  369. }
  370. }
  371. if (p_input) {
  372. hr = p_device->audio_client->GetService(IID_IAudioCaptureClient, (void **)&p_device->capture_client);
  373. } else {
  374. hr = p_device->audio_client->GetService(IID_IAudioRenderClient, (void **)&p_device->render_client);
  375. }
  376. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  377. // Free memory
  378. CoTaskMemFree(pwfex);
  379. return OK;
  380. }
  381. Error AudioDriverWASAPI::init_output_device(bool p_reinit) {
  382. Error err = audio_device_init(&audio_output, false, p_reinit);
  383. if (err != OK) {
  384. return err;
  385. }
  386. switch (audio_output.channels) {
  387. case 1: // Mono
  388. case 3: // Surround 2.1
  389. case 5: // Surround 5.0
  390. case 7: // Surround 7.0
  391. // We will downmix as required.
  392. channels = audio_output.channels + 1;
  393. break;
  394. case 2: // Stereo
  395. case 4: // Surround 3.1
  396. case 6: // Surround 5.1
  397. case 8: // Surround 7.1
  398. channels = audio_output.channels;
  399. break;
  400. default:
  401. WARN_PRINT("WASAPI: Unsupported number of channels: " + itos(audio_output.channels));
  402. channels = 2;
  403. break;
  404. }
  405. // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
  406. samples_in.resize(buffer_frames * channels);
  407. input_position = 0;
  408. input_size = 0;
  409. print_verbose("WASAPI: detected " + itos(audio_output.channels) + " channels");
  410. print_verbose("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
  411. return OK;
  412. }
  413. Error AudioDriverWASAPI::init_input_device(bool p_reinit) {
  414. Error err = audio_device_init(&audio_input, true, p_reinit);
  415. if (err != OK) {
  416. return err;
  417. }
  418. // Get the max frames
  419. UINT32 max_frames;
  420. HRESULT hr = audio_input.audio_client->GetBufferSize(&max_frames);
  421. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  422. input_buffer_init(max_frames);
  423. return OK;
  424. }
  425. Error AudioDriverWASAPI::audio_device_finish(AudioDeviceWASAPI *p_device) {
  426. if (p_device->active.is_set()) {
  427. if (p_device->audio_client) {
  428. p_device->audio_client->Stop();
  429. }
  430. p_device->active.clear();
  431. }
  432. SAFE_RELEASE(p_device->audio_client)
  433. SAFE_RELEASE(p_device->render_client)
  434. SAFE_RELEASE(p_device->capture_client)
  435. return OK;
  436. }
  437. Error AudioDriverWASAPI::finish_output_device() {
  438. return audio_device_finish(&audio_output);
  439. }
  440. Error AudioDriverWASAPI::finish_input_device() {
  441. return audio_device_finish(&audio_input);
  442. }
  443. Error AudioDriverWASAPI::init() {
  444. mix_rate = _get_configured_mix_rate();
  445. target_latency_ms = Engine::get_singleton()->get_audio_output_latency();
  446. exit_thread.clear();
  447. Error err = init_output_device();
  448. ERR_FAIL_COND_V_MSG(err != OK, err, "WASAPI: init_output_device error.");
  449. thread.start(thread_func, this);
  450. return OK;
  451. }
  452. int AudioDriverWASAPI::get_mix_rate() const {
  453. return mix_rate;
  454. }
  455. float AudioDriverWASAPI::get_latency() {
  456. return real_latency;
  457. }
  458. AudioDriver::SpeakerMode AudioDriverWASAPI::get_speaker_mode() const {
  459. return get_speaker_mode_by_total_channels(channels);
  460. }
  461. PackedStringArray AudioDriverWASAPI::audio_device_get_list(bool p_input) {
  462. PackedStringArray list;
  463. ComPtr<IMMDeviceCollection> devices = nullptr;
  464. ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
  465. list.push_back(String("Default"));
  466. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  467. ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
  468. hr = enumerator->EnumAudioEndpoints(p_input ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
  469. ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
  470. UINT count = 0;
  471. hr = devices->GetCount(&count);
  472. ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
  473. for (ULONG i = 0; i < count; i++) {
  474. ComPtr<IMMDevice> output_device = nullptr;
  475. hr = devices->Item(i, &output_device);
  476. ERR_BREAK(hr != S_OK);
  477. ComPtr<IPropertyStore> props = nullptr;
  478. hr = output_device->OpenPropertyStore(STGM_READ, &props);
  479. ERR_BREAK(hr != S_OK);
  480. PROPVARIANT propvar;
  481. PropVariantInit(&propvar);
  482. hr = props->GetValue(PKEY_Device_FriendlyName, &propvar);
  483. ERR_BREAK(hr != S_OK);
  484. list.push_back(String(propvar.pwszVal));
  485. PropVariantClear(&propvar);
  486. }
  487. return list;
  488. }
  489. PackedStringArray AudioDriverWASAPI::get_output_device_list() {
  490. return audio_device_get_list(false);
  491. }
  492. String AudioDriverWASAPI::get_output_device() {
  493. lock();
  494. String name = audio_output.device_name;
  495. unlock();
  496. return name;
  497. }
  498. void AudioDriverWASAPI::set_output_device(const String &p_name) {
  499. lock();
  500. audio_output.new_device = p_name;
  501. unlock();
  502. }
  503. int32_t AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i) {
  504. if (format_tag == WAVE_FORMAT_PCM) {
  505. int32_t sample = 0;
  506. switch (bits_per_sample) {
  507. case 8:
  508. sample = int32_t(((int8_t *)buffer)[i]) << 24;
  509. break;
  510. case 16:
  511. sample = int32_t(((int16_t *)buffer)[i]) << 16;
  512. break;
  513. case 24:
  514. sample |= int32_t(((int8_t *)buffer)[i * 3 + 2]) << 24;
  515. sample |= int32_t(((int8_t *)buffer)[i * 3 + 1]) << 16;
  516. sample |= int32_t(((int8_t *)buffer)[i * 3 + 0]) << 8;
  517. break;
  518. case 32:
  519. sample = ((int32_t *)buffer)[i];
  520. break;
  521. }
  522. return sample;
  523. } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  524. return int32_t(((float *)buffer)[i] * 32768.0) << 16;
  525. } else {
  526. ERR_PRINT("WASAPI: Unknown format tag");
  527. }
  528. return 0;
  529. }
  530. void AudioDriverWASAPI::write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample) {
  531. if (format_tag == WAVE_FORMAT_PCM) {
  532. switch (bits_per_sample) {
  533. case 8:
  534. ((int8_t *)buffer)[i] = sample >> 24;
  535. break;
  536. case 16:
  537. ((int16_t *)buffer)[i] = sample >> 16;
  538. break;
  539. case 24:
  540. ((int8_t *)buffer)[i * 3 + 2] = sample >> 24;
  541. ((int8_t *)buffer)[i * 3 + 1] = sample >> 16;
  542. ((int8_t *)buffer)[i * 3 + 0] = sample >> 8;
  543. break;
  544. case 32:
  545. ((int32_t *)buffer)[i] = sample;
  546. break;
  547. }
  548. } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  549. ((float *)buffer)[i] = (sample >> 16) / 32768.f;
  550. } else {
  551. ERR_PRINT("WASAPI: Unknown format tag");
  552. }
  553. }
  554. void AudioDriverWASAPI::thread_func(void *p_udata) {
  555. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  556. AudioDriverWASAPI *ad = static_cast<AudioDriverWASAPI *>(p_udata);
  557. uint32_t avail_frames = 0;
  558. uint32_t write_ofs = 0;
  559. while (!ad->exit_thread.is_set()) {
  560. uint32_t read_frames = 0;
  561. uint32_t written_frames = 0;
  562. if (avail_frames == 0) {
  563. ad->lock();
  564. ad->start_counting_ticks();
  565. if (ad->audio_output.active.is_set()) {
  566. ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
  567. } else {
  568. for (int i = 0; i < ad->samples_in.size(); i++) {
  569. ad->samples_in.write[i] = 0;
  570. }
  571. }
  572. avail_frames = ad->buffer_frames;
  573. write_ofs = 0;
  574. ad->stop_counting_ticks();
  575. ad->unlock();
  576. }
  577. ad->lock();
  578. ad->start_counting_ticks();
  579. if (avail_frames > 0 && ad->audio_output.audio_client) {
  580. UINT32 buffer_size;
  581. UINT32 cur_frames;
  582. bool invalidated = false;
  583. HRESULT hr = ad->audio_output.audio_client->GetBufferSize(&buffer_size);
  584. if (hr != S_OK) {
  585. ERR_PRINT("WASAPI: GetBufferSize error");
  586. }
  587. hr = ad->audio_output.audio_client->GetCurrentPadding(&cur_frames);
  588. if (hr == S_OK) {
  589. // Check how much frames are available on the WASAPI buffer
  590. UINT32 write_frames = MIN(buffer_size - cur_frames, avail_frames);
  591. if (write_frames > 0) {
  592. BYTE *buffer = nullptr;
  593. hr = ad->audio_output.render_client->GetBuffer(write_frames, &buffer);
  594. if (hr == S_OK) {
  595. // We're using WASAPI Shared Mode so we must convert the buffer
  596. if (ad->channels == ad->audio_output.channels) {
  597. for (unsigned int i = 0; i < write_frames * ad->channels; i++) {
  598. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i, ad->samples_in.write[write_ofs++]);
  599. }
  600. } else if (ad->channels == ad->audio_output.channels + 1) {
  601. // Pass all channels except the last two as-is, and then mix the last two
  602. // together as one channel. E.g. stereo -> mono, or 3.1 -> 2.1.
  603. unsigned int last_chan = ad->audio_output.channels - 1;
  604. for (unsigned int i = 0; i < write_frames; i++) {
  605. for (unsigned int j = 0; j < last_chan; j++) {
  606. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, ad->samples_in.write[write_ofs++]);
  607. }
  608. int32_t l = ad->samples_in.write[write_ofs++];
  609. int32_t r = ad->samples_in.write[write_ofs++];
  610. int32_t c = (int32_t)(((int64_t)l + (int64_t)r) / 2);
  611. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + last_chan, c);
  612. }
  613. } else {
  614. for (unsigned int i = 0; i < write_frames; i++) {
  615. for (unsigned int j = 0; j < MIN(ad->channels, ad->audio_output.channels); j++) {
  616. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, ad->samples_in.write[write_ofs++]);
  617. }
  618. if (ad->audio_output.channels > ad->channels) {
  619. for (unsigned int j = ad->channels; j < ad->audio_output.channels; j++) {
  620. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, 0);
  621. }
  622. }
  623. }
  624. }
  625. hr = ad->audio_output.render_client->ReleaseBuffer(write_frames, 0);
  626. if (hr != S_OK) {
  627. ERR_PRINT("WASAPI: Release buffer error");
  628. }
  629. avail_frames -= write_frames;
  630. written_frames += write_frames;
  631. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  632. // output_device is not valid anymore, reopen it
  633. Error err = ad->finish_output_device();
  634. if (err != OK) {
  635. ERR_PRINT("WASAPI: finish_output_device error");
  636. } else {
  637. // We reopened the output device and samples_in may have resized, so invalidate the current avail_frames
  638. avail_frames = 0;
  639. }
  640. } else {
  641. ERR_PRINT("WASAPI: Get buffer error");
  642. ad->exit_thread.set();
  643. }
  644. }
  645. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  646. invalidated = true;
  647. } else {
  648. ERR_PRINT("WASAPI: GetCurrentPadding error");
  649. }
  650. if (invalidated) {
  651. // output_device is not valid anymore
  652. WARN_PRINT("WASAPI: Current output_device invalidated, closing output_device");
  653. Error err = ad->finish_output_device();
  654. if (err != OK) {
  655. ERR_PRINT("WASAPI: finish_output_device error");
  656. }
  657. }
  658. }
  659. // If we're using the Default output device and it changed finish it so we'll re-init the output device
  660. if (ad->audio_output.device_name == "Default" && default_output_device_changed) {
  661. Error err = ad->finish_output_device();
  662. if (err != OK) {
  663. ERR_PRINT("WASAPI: finish_output_device error");
  664. }
  665. default_output_device_changed = false;
  666. }
  667. // User selected a new output device, finish the current one so we'll init the new output device
  668. if (ad->audio_output.device_name != ad->audio_output.new_device) {
  669. ad->audio_output.device_name = ad->audio_output.new_device;
  670. Error err = ad->finish_output_device();
  671. if (err != OK) {
  672. ERR_PRINT("WASAPI: finish_output_device error");
  673. }
  674. }
  675. if (!ad->audio_output.audio_client) {
  676. Error err = ad->init_output_device(true);
  677. if (err == OK) {
  678. ad->start();
  679. }
  680. avail_frames = 0;
  681. write_ofs = 0;
  682. }
  683. if (ad->audio_input.active.is_set()) {
  684. UINT32 packet_length = 0;
  685. BYTE *data;
  686. UINT32 num_frames_available;
  687. DWORD flags;
  688. HRESULT hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
  689. if (hr == S_OK) {
  690. while (packet_length != 0) {
  691. hr = ad->audio_input.capture_client->GetBuffer(&data, &num_frames_available, &flags, nullptr, nullptr);
  692. ERR_BREAK(hr != S_OK);
  693. // fixme: Only works for floating point atm
  694. for (UINT32 j = 0; j < num_frames_available; j++) {
  695. int32_t l, r;
  696. if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
  697. l = r = 0;
  698. } else {
  699. if (ad->audio_input.channels == 2) {
  700. l = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2);
  701. r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1);
  702. } else if (ad->audio_input.channels == 1) {
  703. l = r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j);
  704. } else {
  705. l = r = 0;
  706. ERR_PRINT("WASAPI: unsupported channel count in microphone!");
  707. }
  708. }
  709. ad->input_buffer_write(l);
  710. ad->input_buffer_write(r);
  711. }
  712. read_frames += num_frames_available;
  713. hr = ad->audio_input.capture_client->ReleaseBuffer(num_frames_available);
  714. ERR_BREAK(hr != S_OK);
  715. hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
  716. ERR_BREAK(hr != S_OK);
  717. }
  718. }
  719. // If we're using the Default output device and it changed finish it so we'll re-init the output device
  720. if (ad->audio_input.device_name == "Default" && default_input_device_changed) {
  721. Error err = ad->finish_input_device();
  722. if (err != OK) {
  723. ERR_PRINT("WASAPI: finish_input_device error");
  724. }
  725. default_input_device_changed = false;
  726. }
  727. // User selected a new input device, finish the current one so we'll init the new input device
  728. if (ad->audio_input.device_name != ad->audio_input.new_device) {
  729. ad->audio_input.device_name = ad->audio_input.new_device;
  730. Error err = ad->finish_input_device();
  731. if (err != OK) {
  732. ERR_PRINT("WASAPI: finish_input_device error");
  733. }
  734. }
  735. if (!ad->audio_input.audio_client) {
  736. Error err = ad->init_input_device(true);
  737. if (err == OK) {
  738. ad->input_start();
  739. }
  740. }
  741. }
  742. ad->stop_counting_ticks();
  743. ad->unlock();
  744. // Let the thread rest a while if we haven't read or write anything
  745. if (written_frames == 0 && read_frames == 0) {
  746. OS::get_singleton()->delay_usec(1000);
  747. }
  748. }
  749. CoUninitialize();
  750. }
  751. void AudioDriverWASAPI::start() {
  752. if (audio_output.audio_client) {
  753. HRESULT hr = audio_output.audio_client->Start();
  754. if (hr != S_OK) {
  755. ERR_PRINT("WASAPI: Start failed");
  756. } else {
  757. audio_output.active.set();
  758. }
  759. }
  760. }
  761. void AudioDriverWASAPI::lock() {
  762. mutex.lock();
  763. }
  764. void AudioDriverWASAPI::unlock() {
  765. mutex.unlock();
  766. }
  767. void AudioDriverWASAPI::finish() {
  768. exit_thread.set();
  769. if (thread.is_started()) {
  770. thread.wait_to_finish();
  771. }
  772. finish_input_device();
  773. finish_output_device();
  774. }
  775. Error AudioDriverWASAPI::input_start() {
  776. Error err = init_input_device();
  777. if (err != OK) {
  778. ERR_PRINT("WASAPI: init_input_device error");
  779. return err;
  780. }
  781. if (audio_input.active.is_set()) {
  782. return FAILED;
  783. }
  784. audio_input.audio_client->Start();
  785. audio_input.active.set();
  786. return OK;
  787. }
  788. Error AudioDriverWASAPI::input_stop() {
  789. if (audio_input.active.is_set()) {
  790. audio_input.audio_client->Stop();
  791. audio_input.active.clear();
  792. return OK;
  793. }
  794. return FAILED;
  795. }
  796. PackedStringArray AudioDriverWASAPI::get_input_device_list() {
  797. return audio_device_get_list(true);
  798. }
  799. String AudioDriverWASAPI::get_input_device() {
  800. lock();
  801. String name = audio_input.device_name;
  802. unlock();
  803. return name;
  804. }
  805. void AudioDriverWASAPI::set_input_device(const String &p_name) {
  806. lock();
  807. audio_input.new_device = p_name;
  808. unlock();
  809. }
  810. AudioDriverWASAPI::AudioDriverWASAPI() {
  811. samples_in.clear();
  812. }
  813. #endif // WASAPI_ENABLED