audio_driver_wasapi.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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_FriendlyNameGodot
  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_FriendlyNameGodot, 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. static int output_reinit_countdown = 0;
  103. static int input_reinit_countdown = 0;
  104. // Silence warning due to a COM API weirdness (GH-35194).
  105. #if defined(__GNUC__) && !defined(__clang__)
  106. #pragma GCC diagnostic push
  107. #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  108. #endif
  109. class CMMNotificationClient : public IMMNotificationClient {
  110. LONG _cRef = 1;
  111. public:
  112. ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
  113. CMMNotificationClient() {}
  114. virtual ~CMMNotificationClient() {}
  115. ULONG STDMETHODCALLTYPE AddRef() {
  116. return InterlockedIncrement(&_cRef);
  117. }
  118. ULONG STDMETHODCALLTYPE Release() {
  119. ULONG ulRef = InterlockedDecrement(&_cRef);
  120. if (0 == ulRef) {
  121. delete this;
  122. }
  123. return ulRef;
  124. }
  125. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) {
  126. if (IID_IUnknown == riid) {
  127. AddRef();
  128. *ppvInterface = (IUnknown *)this;
  129. } else if (__uuidof(IMMNotificationClient) == riid) {
  130. AddRef();
  131. *ppvInterface = (IMMNotificationClient *)this;
  132. } else {
  133. *ppvInterface = nullptr;
  134. return E_NOINTERFACE;
  135. }
  136. return S_OK;
  137. }
  138. HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) {
  139. return S_OK;
  140. }
  141. HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) {
  142. return S_OK;
  143. }
  144. HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) {
  145. return S_OK;
  146. }
  147. HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) {
  148. if (role == eConsole) {
  149. if (flow == eRender) {
  150. default_output_device_changed = true;
  151. } else if (flow == eCapture) {
  152. default_input_device_changed = true;
  153. }
  154. }
  155. return S_OK;
  156. }
  157. HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) {
  158. return S_OK;
  159. }
  160. };
  161. #if defined(__GNUC__) && !defined(__clang__)
  162. #pragma GCC diagnostic pop
  163. #endif
  164. static CMMNotificationClient notif_client;
  165. Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_input, bool p_reinit, bool p_no_audio_client_3) {
  166. // This function can be called recursively, so clean up before starting:
  167. audio_device_finish(p_device);
  168. WAVEFORMATEX *pwfex;
  169. ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
  170. ComPtr<IMMDevice> output_device = nullptr;
  171. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  172. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  173. if (p_device->device_name == "Default") {
  174. hr = enumerator->GetDefaultAudioEndpoint(p_input ? eCapture : eRender, eConsole, &output_device);
  175. } else {
  176. ComPtr<IMMDeviceCollection> devices = nullptr;
  177. hr = enumerator->EnumAudioEndpoints(p_input ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
  178. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  179. LPWSTR strId = nullptr;
  180. bool found = false;
  181. UINT count = 0;
  182. hr = devices->GetCount(&count);
  183. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  184. for (ULONG i = 0; i < count && !found; i++) {
  185. ComPtr<IMMDevice> tmp_device = nullptr;
  186. hr = devices->Item(i, &tmp_device);
  187. ERR_BREAK_MSG(hr != S_OK, "Cannot get devices item.");
  188. ComPtr<IPropertyStore> props = nullptr;
  189. hr = tmp_device->OpenPropertyStore(STGM_READ, &props);
  190. ERR_BREAK_MSG(hr != S_OK, "Cannot open property store.");
  191. PROPVARIANT propvar;
  192. PropVariantInit(&propvar);
  193. hr = props->GetValue(PKEY_Device_FriendlyNameGodot, &propvar);
  194. ERR_BREAK_MSG(hr != S_OK, "Cannot get value.");
  195. if (p_device->device_name == String(propvar.pwszVal)) {
  196. hr = tmp_device->GetId(&strId);
  197. if (unlikely(hr != S_OK)) {
  198. PropVariantClear(&propvar);
  199. ERR_PRINT("Cannot get device ID string.");
  200. break;
  201. }
  202. found = true;
  203. }
  204. PropVariantClear(&propvar);
  205. }
  206. if (found) {
  207. hr = enumerator->GetDevice(strId, &output_device);
  208. }
  209. if (strId) {
  210. CoTaskMemFree(strId);
  211. }
  212. if (output_device == nullptr) {
  213. hr = enumerator->GetDefaultAudioEndpoint(p_input ? eCapture : eRender, eConsole, &output_device);
  214. }
  215. }
  216. if (p_reinit) {
  217. // In case we're trying to re-initialize the device, prevent throwing this error on the console,
  218. // otherwise if there is currently no device available this will spam the console.
  219. if (hr != S_OK) {
  220. return ERR_CANT_OPEN;
  221. }
  222. } else {
  223. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  224. }
  225. if (notif_client.enumerator != nullptr) {
  226. notif_client.enumerator->UnregisterEndpointNotificationCallback(&notif_client);
  227. notif_client.enumerator = nullptr;
  228. }
  229. hr = enumerator->RegisterEndpointNotificationCallback(&notif_client);
  230. if (hr == S_OK) {
  231. notif_client.enumerator = enumerator;
  232. } else {
  233. ERR_PRINT("WASAPI: RegisterEndpointNotificationCallback error");
  234. }
  235. using_audio_client_3 = !p_input; // IID_IAudioClient3 is only used for adjustable output latency (not input)
  236. if (p_no_audio_client_3) {
  237. using_audio_client_3 = false;
  238. }
  239. if (using_audio_client_3) {
  240. hr = output_device->Activate(IID_IAudioClient3, CLSCTX_ALL, nullptr, (void **)&p_device->audio_client);
  241. if (hr != S_OK) {
  242. // IID_IAudioClient3 will never activate on OS versions before Windows 10.
  243. // Older Windows versions should fall back gracefully.
  244. using_audio_client_3 = false;
  245. print_verbose("WASAPI: Couldn't activate output_device with IAudioClient3 interface, falling back to IAudioClient interface");
  246. } else {
  247. print_verbose("WASAPI: Activated output_device using IAudioClient3 interface");
  248. }
  249. }
  250. if (!using_audio_client_3) {
  251. hr = output_device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void **)&p_device->audio_client);
  252. }
  253. if (p_reinit) {
  254. if (hr != S_OK) {
  255. return ERR_CANT_OPEN;
  256. }
  257. } else {
  258. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  259. }
  260. if (using_audio_client_3) {
  261. AudioClientProperties audioProps{};
  262. audioProps.cbSize = sizeof(AudioClientProperties);
  263. audioProps.bIsOffload = FALSE;
  264. audioProps.eCategory = AudioCategory_GameEffects;
  265. hr = ((IAudioClient3 *)p_device->audio_client)->SetClientProperties(&audioProps);
  266. ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_CANT_OPEN, "WASAPI: SetClientProperties failed with error 0x" + String::num_uint64(hr, 16) + ".");
  267. }
  268. hr = p_device->audio_client->GetMixFormat(&pwfex);
  269. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  270. // From this point onward, CoTaskMemFree(pwfex) must be called before returning or pwfex will leak!
  271. print_verbose("WASAPI: wFormatTag = " + itos(pwfex->wFormatTag));
  272. print_verbose("WASAPI: nChannels = " + itos(pwfex->nChannels));
  273. print_verbose("WASAPI: nSamplesPerSec = " + itos(pwfex->nSamplesPerSec));
  274. print_verbose("WASAPI: nAvgBytesPerSec = " + itos(pwfex->nAvgBytesPerSec));
  275. print_verbose("WASAPI: nBlockAlign = " + itos(pwfex->nBlockAlign));
  276. print_verbose("WASAPI: wBitsPerSample = " + itos(pwfex->wBitsPerSample));
  277. print_verbose("WASAPI: cbSize = " + itos(pwfex->cbSize));
  278. WAVEFORMATEX *closest = nullptr;
  279. hr = p_device->audio_client->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, pwfex, &closest);
  280. if (hr == S_FALSE) {
  281. WARN_PRINT("WASAPI: Mix format is not supported by the output_device");
  282. if (closest) {
  283. print_verbose("WASAPI: closest->wFormatTag = " + itos(closest->wFormatTag));
  284. print_verbose("WASAPI: closest->nChannels = " + itos(closest->nChannels));
  285. print_verbose("WASAPI: closest->nSamplesPerSec = " + itos(closest->nSamplesPerSec));
  286. print_verbose("WASAPI: closest->nAvgBytesPerSec = " + itos(closest->nAvgBytesPerSec));
  287. print_verbose("WASAPI: closest->nBlockAlign = " + itos(closest->nBlockAlign));
  288. print_verbose("WASAPI: closest->wBitsPerSample = " + itos(closest->wBitsPerSample));
  289. print_verbose("WASAPI: closest->cbSize = " + itos(closest->cbSize));
  290. WARN_PRINT("WASAPI: Using closest match instead");
  291. CoTaskMemFree(pwfex);
  292. pwfex = closest;
  293. }
  294. }
  295. // Since we're using WASAPI Shared Mode we can't control any of these, we just tag along
  296. p_device->channels = pwfex->nChannels;
  297. p_device->format_tag = pwfex->wFormatTag;
  298. p_device->bits_per_sample = pwfex->wBitsPerSample;
  299. p_device->frame_size = (p_device->bits_per_sample / 8) * p_device->channels;
  300. if (p_device->format_tag == WAVE_FORMAT_EXTENSIBLE) {
  301. WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex;
  302. if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
  303. p_device->format_tag = WAVE_FORMAT_PCM;
  304. } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
  305. p_device->format_tag = WAVE_FORMAT_IEEE_FLOAT;
  306. } else {
  307. ERR_PRINT("WASAPI: Format not supported");
  308. CoTaskMemFree(pwfex);
  309. ERR_FAIL_V(ERR_CANT_OPEN);
  310. }
  311. } else {
  312. if (p_device->format_tag != WAVE_FORMAT_PCM && p_device->format_tag != WAVE_FORMAT_IEEE_FLOAT) {
  313. ERR_PRINT("WASAPI: Format not supported");
  314. CoTaskMemFree(pwfex);
  315. ERR_FAIL_V(ERR_CANT_OPEN);
  316. }
  317. }
  318. if (!using_audio_client_3) {
  319. DWORD streamflags = 0;
  320. if ((DWORD)mix_rate != pwfex->nSamplesPerSec) {
  321. streamflags |= AUDCLNT_STREAMFLAGS_RATEADJUST;
  322. pwfex->nSamplesPerSec = mix_rate;
  323. pwfex->nAvgBytesPerSec = pwfex->nSamplesPerSec * pwfex->nChannels * (pwfex->wBitsPerSample / 8);
  324. }
  325. hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, p_input ? REFTIMES_PER_SEC : 0, 0, pwfex, nullptr);
  326. if (p_reinit) {
  327. // In case we're trying to re-initialize the device, prevent throwing this error on the console,
  328. // otherwise if there is currently no device available this will spam the console.
  329. if (hr != S_OK) {
  330. print_verbose("WASAPI: Initialize failed with error 0x" + String::num_uint64(hr, 16) + ".");
  331. CoTaskMemFree(pwfex);
  332. return ERR_CANT_OPEN;
  333. }
  334. } else {
  335. if (unlikely(hr != S_OK)) {
  336. CoTaskMemFree(pwfex);
  337. ERR_FAIL_V_MSG(ERR_CANT_OPEN, "WASAPI: Initialize failed with error 0x" + String::num_uint64(hr, 16) + ".");
  338. }
  339. }
  340. UINT32 max_frames;
  341. hr = p_device->audio_client->GetBufferSize(&max_frames);
  342. if (unlikely(hr != S_OK)) {
  343. CoTaskMemFree(pwfex);
  344. ERR_FAIL_V(ERR_CANT_OPEN);
  345. }
  346. // Due to WASAPI Shared Mode we have no control of the buffer size
  347. if (!p_input) {
  348. buffer_frames = max_frames;
  349. int64_t latency = 0;
  350. audio_output.audio_client->GetStreamLatency(&latency);
  351. // WASAPI REFERENCE_TIME units are 100 nanoseconds per unit
  352. // https://docs.microsoft.com/en-us/windows/win32/directshow/reference-time
  353. // Convert REFTIME to seconds as godot uses for latency
  354. real_latency = (float)latency / (float)REFTIMES_PER_SEC;
  355. }
  356. } else {
  357. IAudioClient3 *device_audio_client_3 = (IAudioClient3 *)p_device->audio_client;
  358. // AUDCLNT_STREAMFLAGS_RATEADJUST is an invalid flag with IAudioClient3, therefore we have to use
  359. // the closest supported mix rate supported by the audio driver.
  360. mix_rate = pwfex->nSamplesPerSec;
  361. print_verbose("WASAPI: mix_rate = " + itos(mix_rate));
  362. UINT32 default_period_frames, fundamental_period_frames, min_period_frames, max_period_frames;
  363. hr = device_audio_client_3->GetSharedModeEnginePeriod(
  364. pwfex,
  365. &default_period_frames,
  366. &fundamental_period_frames,
  367. &min_period_frames,
  368. &max_period_frames);
  369. if (hr != S_OK) {
  370. print_verbose("WASAPI: GetSharedModeEnginePeriod failed with error 0x" + String::num_uint64(hr, 16) + ", falling back to IAudioClient.");
  371. CoTaskMemFree(pwfex);
  372. return audio_device_init(p_device, p_input, p_reinit, true);
  373. }
  374. // Period frames must be an integral multiple of fundamental_period_frames or IAudioClient3 initialization will fail,
  375. // so we need to select the closest multiple to the user-specified latency.
  376. UINT32 desired_period_frames = target_latency_ms * mix_rate / 1000;
  377. UINT32 period_frames = (desired_period_frames / fundamental_period_frames) * fundamental_period_frames;
  378. if (ABS((int64_t)period_frames - (int64_t)desired_period_frames) > ABS((int64_t)(period_frames + fundamental_period_frames) - (int64_t)desired_period_frames)) {
  379. period_frames = period_frames + fundamental_period_frames;
  380. }
  381. period_frames = CLAMP(period_frames, min_period_frames, max_period_frames);
  382. print_verbose("WASAPI: fundamental_period_frames = " + itos(fundamental_period_frames));
  383. print_verbose("WASAPI: min_period_frames = " + itos(min_period_frames));
  384. print_verbose("WASAPI: max_period_frames = " + itos(max_period_frames));
  385. print_verbose("WASAPI: selected a period frame size of " + itos(period_frames));
  386. buffer_frames = period_frames;
  387. hr = device_audio_client_3->InitializeSharedAudioStream(0, period_frames, pwfex, nullptr);
  388. if (hr != S_OK) {
  389. print_verbose("WASAPI: InitializeSharedAudioStream failed with error 0x" + String::num_uint64(hr, 16) + ", falling back to IAudioClient.");
  390. CoTaskMemFree(pwfex);
  391. return audio_device_init(p_device, p_input, p_reinit, true);
  392. } else {
  393. uint32_t output_latency_in_frames;
  394. WAVEFORMATEX *current_pwfex;
  395. hr = device_audio_client_3->GetCurrentSharedModeEnginePeriod(&current_pwfex, &output_latency_in_frames);
  396. if (hr == OK) {
  397. real_latency = (float)output_latency_in_frames / (float)current_pwfex->nSamplesPerSec;
  398. CoTaskMemFree(current_pwfex);
  399. } else {
  400. print_verbose("WASAPI: GetCurrentSharedModeEnginePeriod failed with error 0x" + String::num_uint64(hr, 16) + ", falling back to IAudioClient.");
  401. CoTaskMemFree(pwfex);
  402. return audio_device_init(p_device, p_input, p_reinit, true);
  403. }
  404. }
  405. }
  406. if (p_input) {
  407. hr = p_device->audio_client->GetService(IID_IAudioCaptureClient, (void **)&p_device->capture_client);
  408. } else {
  409. hr = p_device->audio_client->GetService(IID_IAudioRenderClient, (void **)&p_device->render_client);
  410. }
  411. if (unlikely(hr != S_OK)) {
  412. CoTaskMemFree(pwfex);
  413. ERR_FAIL_V(ERR_CANT_OPEN);
  414. }
  415. // Free memory
  416. CoTaskMemFree(pwfex);
  417. return OK;
  418. }
  419. Error AudioDriverWASAPI::init_output_device(bool p_reinit) {
  420. Error err = audio_device_init(&audio_output, false, p_reinit);
  421. if (err != OK) {
  422. // We've tried to init the device, but have failed. Time to clean up.
  423. Error finish_err = finish_output_device();
  424. if (finish_err != OK) {
  425. ERR_PRINT("WASAPI: finish_output_device error after failed output audio_device_init");
  426. }
  427. return err;
  428. }
  429. switch (audio_output.channels) {
  430. case 1: // Mono
  431. case 3: // Surround 2.1
  432. case 5: // Surround 5.0
  433. case 7: // Surround 7.0
  434. // We will downmix as required.
  435. channels = audio_output.channels + 1;
  436. break;
  437. case 2: // Stereo
  438. case 4: // Surround 3.1
  439. case 6: // Surround 5.1
  440. case 8: // Surround 7.1
  441. channels = audio_output.channels;
  442. break;
  443. default:
  444. WARN_PRINT("WASAPI: Unsupported number of channels: " + itos(audio_output.channels));
  445. channels = 2;
  446. break;
  447. }
  448. // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
  449. samples_in.resize(buffer_frames * channels);
  450. input_position = 0;
  451. input_size = 0;
  452. print_verbose("WASAPI: detected " + itos(audio_output.channels) + " channels");
  453. print_verbose("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
  454. return OK;
  455. }
  456. Error AudioDriverWASAPI::init_input_device(bool p_reinit) {
  457. Error err = audio_device_init(&audio_input, true, p_reinit);
  458. if (err != OK) {
  459. // We've tried to init the device, but have failed. Time to clean up.
  460. Error finish_err = finish_input_device();
  461. if (finish_err != OK) {
  462. ERR_PRINT("WASAPI: finish_input_device error after failed input audio_device_init");
  463. }
  464. return err;
  465. }
  466. // Get the max frames
  467. UINT32 max_frames;
  468. HRESULT hr = audio_input.audio_client->GetBufferSize(&max_frames);
  469. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  470. input_buffer_init(max_frames);
  471. return OK;
  472. }
  473. Error AudioDriverWASAPI::audio_device_finish(AudioDeviceWASAPI *p_device) {
  474. if (p_device->active.is_set()) {
  475. if (p_device->audio_client) {
  476. p_device->audio_client->Stop();
  477. }
  478. p_device->active.clear();
  479. }
  480. SAFE_RELEASE(p_device->audio_client)
  481. SAFE_RELEASE(p_device->render_client)
  482. SAFE_RELEASE(p_device->capture_client)
  483. return OK;
  484. }
  485. Error AudioDriverWASAPI::finish_output_device() {
  486. return audio_device_finish(&audio_output);
  487. }
  488. Error AudioDriverWASAPI::finish_input_device() {
  489. return audio_device_finish(&audio_input);
  490. }
  491. Error AudioDriverWASAPI::init() {
  492. mix_rate = _get_configured_mix_rate();
  493. target_latency_ms = Engine::get_singleton()->get_audio_output_latency();
  494. exit_thread.clear();
  495. Error err = init_output_device();
  496. ERR_FAIL_COND_V_MSG(err != OK, err, "WASAPI: init_output_device error.");
  497. thread.start(thread_func, this);
  498. return OK;
  499. }
  500. int AudioDriverWASAPI::get_mix_rate() const {
  501. return mix_rate;
  502. }
  503. float AudioDriverWASAPI::get_latency() {
  504. return real_latency;
  505. }
  506. AudioDriver::SpeakerMode AudioDriverWASAPI::get_speaker_mode() const {
  507. return get_speaker_mode_by_total_channels(channels);
  508. }
  509. PackedStringArray AudioDriverWASAPI::audio_device_get_list(bool p_input) {
  510. PackedStringArray list;
  511. ComPtr<IMMDeviceCollection> devices = nullptr;
  512. ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
  513. list.push_back(String("Default"));
  514. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  515. ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
  516. hr = enumerator->EnumAudioEndpoints(p_input ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
  517. ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
  518. UINT count = 0;
  519. hr = devices->GetCount(&count);
  520. ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
  521. for (ULONG i = 0; i < count; i++) {
  522. ComPtr<IMMDevice> output_device = nullptr;
  523. hr = devices->Item(i, &output_device);
  524. ERR_BREAK(hr != S_OK);
  525. ComPtr<IPropertyStore> props = nullptr;
  526. hr = output_device->OpenPropertyStore(STGM_READ, &props);
  527. ERR_BREAK(hr != S_OK);
  528. PROPVARIANT propvar;
  529. PropVariantInit(&propvar);
  530. hr = props->GetValue(PKEY_Device_FriendlyNameGodot, &propvar);
  531. ERR_BREAK(hr != S_OK);
  532. list.push_back(String(propvar.pwszVal));
  533. PropVariantClear(&propvar);
  534. }
  535. return list;
  536. }
  537. PackedStringArray AudioDriverWASAPI::get_output_device_list() {
  538. return audio_device_get_list(false);
  539. }
  540. String AudioDriverWASAPI::get_output_device() {
  541. lock();
  542. String name = audio_output.device_name;
  543. unlock();
  544. return name;
  545. }
  546. void AudioDriverWASAPI::set_output_device(const String &p_name) {
  547. lock();
  548. audio_output.new_device = p_name;
  549. unlock();
  550. }
  551. int32_t AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i) {
  552. if (format_tag == WAVE_FORMAT_PCM) {
  553. int32_t sample = 0;
  554. switch (bits_per_sample) {
  555. case 8:
  556. sample = int32_t(((int8_t *)buffer)[i]) << 24;
  557. break;
  558. case 16:
  559. sample = int32_t(((int16_t *)buffer)[i]) << 16;
  560. break;
  561. case 24:
  562. sample |= int32_t(((int8_t *)buffer)[i * 3 + 2]) << 24;
  563. sample |= int32_t(((int8_t *)buffer)[i * 3 + 1]) << 16;
  564. sample |= int32_t(((int8_t *)buffer)[i * 3 + 0]) << 8;
  565. break;
  566. case 32:
  567. sample = ((int32_t *)buffer)[i];
  568. break;
  569. }
  570. return sample;
  571. } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  572. return int32_t(((float *)buffer)[i] * 32768.0) << 16;
  573. } else {
  574. ERR_PRINT("WASAPI: Unknown format tag");
  575. }
  576. return 0;
  577. }
  578. void AudioDriverWASAPI::write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample) {
  579. if (format_tag == WAVE_FORMAT_PCM) {
  580. switch (bits_per_sample) {
  581. case 8:
  582. ((int8_t *)buffer)[i] = sample >> 24;
  583. break;
  584. case 16:
  585. ((int16_t *)buffer)[i] = sample >> 16;
  586. break;
  587. case 24:
  588. ((int8_t *)buffer)[i * 3 + 2] = sample >> 24;
  589. ((int8_t *)buffer)[i * 3 + 1] = sample >> 16;
  590. ((int8_t *)buffer)[i * 3 + 0] = sample >> 8;
  591. break;
  592. case 32:
  593. ((int32_t *)buffer)[i] = sample;
  594. break;
  595. }
  596. } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  597. ((float *)buffer)[i] = (sample >> 16) / 32768.f;
  598. } else {
  599. ERR_PRINT("WASAPI: Unknown format tag");
  600. }
  601. }
  602. void AudioDriverWASAPI::thread_func(void *p_udata) {
  603. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  604. AudioDriverWASAPI *ad = static_cast<AudioDriverWASAPI *>(p_udata);
  605. uint32_t avail_frames = 0;
  606. uint32_t write_ofs = 0;
  607. while (!ad->exit_thread.is_set()) {
  608. uint32_t read_frames = 0;
  609. uint32_t written_frames = 0;
  610. if (avail_frames == 0) {
  611. ad->lock();
  612. ad->start_counting_ticks();
  613. if (ad->audio_output.active.is_set()) {
  614. ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
  615. } else {
  616. for (int i = 0; i < ad->samples_in.size(); i++) {
  617. ad->samples_in.write[i] = 0;
  618. }
  619. }
  620. avail_frames = ad->buffer_frames;
  621. write_ofs = 0;
  622. ad->stop_counting_ticks();
  623. ad->unlock();
  624. }
  625. ad->lock();
  626. ad->start_counting_ticks();
  627. if (avail_frames > 0 && ad->audio_output.audio_client) {
  628. UINT32 buffer_size;
  629. UINT32 cur_frames;
  630. bool invalidated = false;
  631. HRESULT hr = ad->audio_output.audio_client->GetBufferSize(&buffer_size);
  632. if (hr != S_OK) {
  633. ERR_PRINT("WASAPI: GetBufferSize error");
  634. }
  635. hr = ad->audio_output.audio_client->GetCurrentPadding(&cur_frames);
  636. if (hr == S_OK) {
  637. // Check how much frames are available on the WASAPI buffer
  638. UINT32 write_frames = MIN(buffer_size - cur_frames, avail_frames);
  639. if (write_frames > 0) {
  640. BYTE *buffer = nullptr;
  641. hr = ad->audio_output.render_client->GetBuffer(write_frames, &buffer);
  642. if (hr == S_OK) {
  643. // We're using WASAPI Shared Mode so we must convert the buffer
  644. if (ad->channels == ad->audio_output.channels) {
  645. for (unsigned int i = 0; i < write_frames * ad->channels; i++) {
  646. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i, ad->samples_in.write[write_ofs++]);
  647. }
  648. } else if (ad->channels == ad->audio_output.channels + 1) {
  649. // Pass all channels except the last two as-is, and then mix the last two
  650. // together as one channel. E.g. stereo -> mono, or 3.1 -> 2.1.
  651. unsigned int last_chan = ad->audio_output.channels - 1;
  652. for (unsigned int i = 0; i < write_frames; i++) {
  653. for (unsigned int j = 0; j < last_chan; j++) {
  654. 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++]);
  655. }
  656. int32_t l = ad->samples_in.write[write_ofs++];
  657. int32_t r = ad->samples_in.write[write_ofs++];
  658. int32_t c = (int32_t)(((int64_t)l + (int64_t)r) / 2);
  659. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + last_chan, c);
  660. }
  661. } else {
  662. for (unsigned int i = 0; i < write_frames; i++) {
  663. for (unsigned int j = 0; j < MIN(ad->channels, ad->audio_output.channels); j++) {
  664. 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++]);
  665. }
  666. if (ad->audio_output.channels > ad->channels) {
  667. for (unsigned int j = ad->channels; j < ad->audio_output.channels; j++) {
  668. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, 0);
  669. }
  670. }
  671. }
  672. }
  673. hr = ad->audio_output.render_client->ReleaseBuffer(write_frames, 0);
  674. if (hr != S_OK) {
  675. ERR_PRINT("WASAPI: Release buffer error");
  676. }
  677. avail_frames -= write_frames;
  678. written_frames += write_frames;
  679. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  680. // output_device is not valid anymore, reopen it
  681. Error err = ad->finish_output_device();
  682. if (err != OK) {
  683. ERR_PRINT("WASAPI: finish_output_device error");
  684. } else {
  685. // We reopened the output device and samples_in may have resized, so invalidate the current avail_frames
  686. avail_frames = 0;
  687. }
  688. } else {
  689. ERR_PRINT("WASAPI: Get buffer error");
  690. ad->exit_thread.set();
  691. }
  692. }
  693. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  694. invalidated = true;
  695. } else {
  696. ERR_PRINT("WASAPI: GetCurrentPadding error");
  697. }
  698. if (invalidated) {
  699. // output_device is not valid anymore
  700. WARN_PRINT("WASAPI: Current output_device invalidated, closing output_device");
  701. Error err = ad->finish_output_device();
  702. if (err != OK) {
  703. ERR_PRINT("WASAPI: finish_output_device error");
  704. }
  705. }
  706. }
  707. // If we're using the Default output device and it changed finish it so we'll re-init the output device
  708. if (ad->audio_output.device_name == "Default" && default_output_device_changed) {
  709. Error err = ad->finish_output_device();
  710. if (err != OK) {
  711. ERR_PRINT("WASAPI: finish_output_device error");
  712. }
  713. default_output_device_changed = false;
  714. }
  715. // User selected a new output device, finish the current one so we'll init the new output device
  716. if (ad->audio_output.device_name != ad->audio_output.new_device) {
  717. ad->audio_output.device_name = ad->audio_output.new_device;
  718. Error err = ad->finish_output_device();
  719. if (err != OK) {
  720. ERR_PRINT("WASAPI: finish_output_device error");
  721. }
  722. }
  723. if (!ad->audio_output.audio_client) {
  724. if (output_reinit_countdown < 1) {
  725. Error err = ad->init_output_device(true);
  726. if (err == OK) {
  727. ad->start();
  728. } else {
  729. output_reinit_countdown = 1000;
  730. }
  731. } else {
  732. output_reinit_countdown--;
  733. }
  734. avail_frames = 0;
  735. write_ofs = 0;
  736. }
  737. if (ad->audio_input.active.is_set()) {
  738. UINT32 packet_length = 0;
  739. BYTE *data;
  740. UINT32 num_frames_available;
  741. DWORD flags;
  742. HRESULT hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
  743. if (hr == S_OK) {
  744. while (packet_length != 0) {
  745. hr = ad->audio_input.capture_client->GetBuffer(&data, &num_frames_available, &flags, nullptr, nullptr);
  746. ERR_BREAK(hr != S_OK);
  747. // fixme: Only works for floating point atm
  748. for (UINT32 j = 0; j < num_frames_available; j++) {
  749. int32_t l, r;
  750. if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
  751. l = r = 0;
  752. } else {
  753. if (ad->audio_input.channels == 2) {
  754. l = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2);
  755. r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1);
  756. } else if (ad->audio_input.channels == 1) {
  757. l = r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j);
  758. } else {
  759. l = r = 0;
  760. ERR_PRINT("WASAPI: unsupported channel count in microphone!");
  761. }
  762. }
  763. ad->input_buffer_write(l);
  764. ad->input_buffer_write(r);
  765. }
  766. read_frames += num_frames_available;
  767. hr = ad->audio_input.capture_client->ReleaseBuffer(num_frames_available);
  768. ERR_BREAK(hr != S_OK);
  769. hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
  770. ERR_BREAK(hr != S_OK);
  771. }
  772. }
  773. // If we're using the Default output device and it changed finish it so we'll re-init the output device
  774. if (ad->audio_input.device_name == "Default" && default_input_device_changed) {
  775. Error err = ad->finish_input_device();
  776. if (err != OK) {
  777. ERR_PRINT("WASAPI: finish_input_device error");
  778. }
  779. default_input_device_changed = false;
  780. }
  781. // User selected a new input device, finish the current one so we'll init the new input device
  782. if (ad->audio_input.device_name != ad->audio_input.new_device) {
  783. ad->audio_input.device_name = ad->audio_input.new_device;
  784. Error err = ad->finish_input_device();
  785. if (err != OK) {
  786. ERR_PRINT("WASAPI: finish_input_device error");
  787. }
  788. }
  789. if (!ad->audio_input.audio_client) {
  790. if (input_reinit_countdown < 1) {
  791. Error err = ad->init_input_device(true);
  792. if (err == OK) {
  793. ad->input_start();
  794. } else {
  795. input_reinit_countdown = 1000;
  796. }
  797. } else {
  798. input_reinit_countdown--;
  799. }
  800. }
  801. }
  802. ad->stop_counting_ticks();
  803. ad->unlock();
  804. // Let the thread rest a while if we haven't read or write anything
  805. if (written_frames == 0 && read_frames == 0) {
  806. OS::get_singleton()->delay_usec(1000);
  807. }
  808. }
  809. CoUninitialize();
  810. }
  811. void AudioDriverWASAPI::start() {
  812. if (audio_output.audio_client) {
  813. HRESULT hr = audio_output.audio_client->Start();
  814. if (hr != S_OK) {
  815. ERR_PRINT("WASAPI: Start failed");
  816. } else {
  817. audio_output.active.set();
  818. }
  819. }
  820. }
  821. void AudioDriverWASAPI::lock() {
  822. mutex.lock();
  823. }
  824. void AudioDriverWASAPI::unlock() {
  825. mutex.unlock();
  826. }
  827. void AudioDriverWASAPI::finish() {
  828. exit_thread.set();
  829. if (thread.is_started()) {
  830. thread.wait_to_finish();
  831. }
  832. finish_input_device();
  833. finish_output_device();
  834. }
  835. Error AudioDriverWASAPI::input_start() {
  836. Error err = init_input_device();
  837. if (err != OK) {
  838. ERR_PRINT("WASAPI: init_input_device error");
  839. return err;
  840. }
  841. if (audio_input.active.is_set()) {
  842. return FAILED;
  843. }
  844. audio_input.audio_client->Start();
  845. audio_input.active.set();
  846. return OK;
  847. }
  848. Error AudioDriverWASAPI::input_stop() {
  849. if (audio_input.active.is_set()) {
  850. audio_input.audio_client->Stop();
  851. audio_input.active.clear();
  852. return OK;
  853. }
  854. return FAILED;
  855. }
  856. PackedStringArray AudioDriverWASAPI::get_input_device_list() {
  857. return audio_device_get_list(true);
  858. }
  859. String AudioDriverWASAPI::get_input_device() {
  860. lock();
  861. String name = audio_input.device_name;
  862. unlock();
  863. return name;
  864. }
  865. void AudioDriverWASAPI::set_input_device(const String &p_name) {
  866. lock();
  867. audio_input.new_device = p_name;
  868. unlock();
  869. }
  870. AudioDriverWASAPI::AudioDriverWASAPI() {
  871. samples_in.clear();
  872. }
  873. #endif // WASAPI_ENABLED