CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/WASAPIStream.cpp
Views: 1401
1
#include "stdafx.h"
2
3
#include "WindowsAudio.h"
4
#include "WASAPIStream.h"
5
#include "Common/Log.h"
6
#include "Common/LogReporting.h"
7
#include "Core/Config.h"
8
#include "Core/Util/AudioFormat.h"
9
#include "Common/Data/Encoding/Utf8.h"
10
11
#include "Common/Thread/ThreadUtil.h"
12
13
#include <mutex>
14
#include <Objbase.h>
15
#include <Mmreg.h>
16
#include <MMDeviceAPI.h>
17
#include <AudioClient.h>
18
#include <AudioPolicy.h>
19
#include "Functiondiscoverykeys_devpkey.h"
20
21
// Includes some code from https://msdn.microsoft.com/en-us/library/dd370810%28VS.85%29.aspx?f=255&MSPPError=-2147217396
22
23
#pragma comment(lib, "ole32.lib")
24
25
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
26
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
27
const IID IID_IAudioClient = __uuidof(IAudioClient);
28
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
29
30
// Adapted from a MSDN sample.
31
32
#define SAFE_RELEASE(punk) \
33
if ((punk) != NULL) \
34
{ (punk)->Release(); (punk) = NULL; }
35
36
class CMMNotificationClient final : public IMMNotificationClient {
37
public:
38
CMMNotificationClient() {
39
}
40
41
virtual ~CMMNotificationClient() {
42
CoTaskMemFree(currentDevice_);
43
currentDevice_ = nullptr;
44
SAFE_RELEASE(_pEnumerator)
45
}
46
47
void SetCurrentDevice(IMMDevice *device) {
48
std::lock_guard<std::mutex> guard(lock_);
49
50
CoTaskMemFree(currentDevice_);
51
currentDevice_ = nullptr;
52
if (!device || FAILED(device->GetId(&currentDevice_))) {
53
currentDevice_ = nullptr;
54
}
55
56
if (currentDevice_) {
57
INFO_LOG(Log::sceAudio, "Switching to WASAPI audio device: '%s'", GetDeviceName(currentDevice_).c_str());
58
}
59
60
deviceChanged_ = false;
61
}
62
63
bool HasDefaultDeviceChanged() const {
64
return deviceChanged_;
65
}
66
67
// IUnknown methods -- AddRef, Release, and QueryInterface
68
ULONG STDMETHODCALLTYPE AddRef() override {
69
return InterlockedIncrement(&_cRef);
70
}
71
72
ULONG STDMETHODCALLTYPE Release() override {
73
ULONG ulRef = InterlockedDecrement(&_cRef);
74
if (0 == ulRef) {
75
delete this;
76
}
77
return ulRef;
78
}
79
80
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) override {
81
if (IID_IUnknown == riid) {
82
AddRef();
83
*ppvInterface = (IUnknown*)this;
84
} else if (__uuidof(IMMNotificationClient) == riid) {
85
AddRef();
86
*ppvInterface = (IMMNotificationClient*)this;
87
} else {
88
*ppvInterface = nullptr;
89
return E_NOINTERFACE;
90
}
91
return S_OK;
92
}
93
94
// Callback methods for device-event notifications.
95
96
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) override {
97
std::lock_guard<std::mutex> guard(lock_);
98
99
if (flow != eRender || role != eConsole) {
100
// Not relevant to us.
101
return S_OK;
102
}
103
104
// pwstrDeviceId can be null. We consider that a new device, I think?
105
bool same = currentDevice_ == pwstrDeviceId;
106
if (!same && currentDevice_ && pwstrDeviceId) {
107
same = !wcscmp(currentDevice_, pwstrDeviceId);
108
}
109
if (same) {
110
// Already the current device, nothing to do.
111
return S_OK;
112
}
113
114
deviceChanged_ = true;
115
INFO_LOG(Log::sceAudio, "New default eRender/eConsole WASAPI audio device detected: '%s'", GetDeviceName(pwstrDeviceId).c_str());
116
return S_OK;
117
}
118
119
HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) override {
120
// Ignore.
121
return S_OK;
122
};
123
124
HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) override {
125
// Ignore.
126
return S_OK;
127
}
128
129
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) override {
130
// Ignore.
131
return S_OK;
132
}
133
134
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) override {
135
INFO_LOG(Log::sceAudio, "Changed audio device property "
136
"{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}#%d",
137
(uint32_t)key.fmtid.Data1, key.fmtid.Data2, key.fmtid.Data3,
138
key.fmtid.Data4[0], key.fmtid.Data4[1],
139
key.fmtid.Data4[2], key.fmtid.Data4[3],
140
key.fmtid.Data4[4], key.fmtid.Data4[5],
141
key.fmtid.Data4[6], key.fmtid.Data4[7],
142
(int)key.pid);
143
return S_OK;
144
}
145
146
std::string GetDeviceName(LPCWSTR pwstrId)
147
{
148
HRESULT hr = S_OK;
149
IMMDevice *pDevice = NULL;
150
IPropertyStore *pProps = NULL;
151
PROPVARIANT varString;
152
PropVariantInit(&varString);
153
154
if (_pEnumerator == NULL)
155
{
156
// Get enumerator for audio endpoint devices.
157
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
158
NULL, CLSCTX_INPROC_SERVER,
159
__uuidof(IMMDeviceEnumerator),
160
(void**)&_pEnumerator);
161
}
162
if (hr == S_OK && _pEnumerator) {
163
hr = _pEnumerator->GetDevice(pwstrId, &pDevice);
164
}
165
if (hr == S_OK && pDevice) {
166
hr = pDevice->OpenPropertyStore(STGM_READ, &pProps);
167
}
168
if (hr == S_OK && pProps) {
169
// Get the endpoint device's friendly-name property.
170
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varString);
171
}
172
173
std::string name = ConvertWStringToUTF8((hr == S_OK) ? varString.pwszVal : L"null device");
174
175
PropVariantClear(&varString);
176
177
SAFE_RELEASE(pProps)
178
SAFE_RELEASE(pDevice)
179
return name;
180
}
181
182
private:
183
std::mutex lock_;
184
LONG _cRef = 1;
185
IMMDeviceEnumerator *_pEnumerator = nullptr;
186
wchar_t *currentDevice_ = nullptr;
187
bool deviceChanged_ = false;
188
};
189
190
// TODO: Make these adjustable. This is from the example in MSDN.
191
// 200 times/sec = 5ms, pretty good :) Wonder if all computers can handle it though.
192
#define REFTIMES_PER_SEC (10000000/200)
193
#define REFTIMES_PER_MILLISEC (REFTIMES_PER_SEC / 1000)
194
195
WASAPIAudioBackend::WASAPIAudioBackend() : threadData_(0) {
196
}
197
198
WASAPIAudioBackend::~WASAPIAudioBackend() {
199
if (threadData_ == 0) {
200
threadData_ = 1;
201
}
202
203
if (hThread_) {
204
WaitForSingleObject(hThread_, 1000);
205
CloseHandle(hThread_);
206
hThread_ = nullptr;
207
}
208
209
if (threadData_ == 2) {
210
// blah.
211
}
212
}
213
214
unsigned int WINAPI WASAPIAudioBackend::soundThread(void *param) {
215
WASAPIAudioBackend *backend = (WASAPIAudioBackend *)param;
216
return backend->RunThread();
217
}
218
219
bool WASAPIAudioBackend::Init(HWND window, StreamCallback callback, int sampleRate) {
220
threadData_ = 0;
221
callback_ = callback;
222
sampleRate_ = sampleRate;
223
hThread_ = (HANDLE)_beginthreadex(0, 0, soundThread, (void *)this, 0, 0);
224
if (!hThread_)
225
return false;
226
SetThreadPriority(hThread_, THREAD_PRIORITY_ABOVE_NORMAL);
227
return true;
228
}
229
230
// This to be run only on the thread.
231
class WASAPIAudioThread {
232
public:
233
WASAPIAudioThread(std::atomic<int> &threadData, int &sampleRate, StreamCallback &callback)
234
: threadData_(threadData), sampleRate_(sampleRate), callback_(callback) {
235
}
236
~WASAPIAudioThread();
237
238
void Run();
239
240
private:
241
bool ActivateDefaultDevice();
242
bool InitAudioDevice();
243
void ShutdownAudioDevice();
244
bool DetectFormat();
245
bool ValidateFormat(const WAVEFORMATEXTENSIBLE *fmt);
246
bool PrepareFormat();
247
248
std::atomic<int> &threadData_;
249
int &sampleRate_;
250
StreamCallback &callback_;
251
252
IMMDeviceEnumerator *deviceEnumerator_ = nullptr;
253
IMMDevice *device_ = nullptr;
254
IAudioClient *audioInterface_ = nullptr;
255
CMMNotificationClient *notificationClient_ = nullptr;
256
WAVEFORMATEXTENSIBLE *deviceFormat_ = nullptr;
257
IAudioRenderClient *renderClient_ = nullptr;
258
int16_t *shortBuf_ = nullptr;
259
260
enum class Format {
261
UNKNOWN = 0,
262
IEEE_FLOAT = 1,
263
PCM16 = 2,
264
};
265
266
uint32_t numBufferFrames = 0;
267
Format format_ = Format::UNKNOWN;
268
REFERENCE_TIME actualDuration_{};
269
};
270
271
WASAPIAudioThread::~WASAPIAudioThread() {
272
delete [] shortBuf_;
273
shortBuf_ = nullptr;
274
ShutdownAudioDevice();
275
if (notificationClient_ && deviceEnumerator_)
276
deviceEnumerator_->UnregisterEndpointNotificationCallback(notificationClient_);
277
delete notificationClient_;
278
notificationClient_ = nullptr;
279
SAFE_RELEASE(deviceEnumerator_);
280
}
281
282
bool WASAPIAudioThread::ActivateDefaultDevice() {
283
_assert_(device_ == nullptr);
284
HRESULT hresult = deviceEnumerator_->GetDefaultAudioEndpoint(eRender, eConsole, &device_);
285
if (FAILED(hresult) || device_ == nullptr)
286
return false;
287
288
_assert_(audioInterface_ == nullptr);
289
hresult = device_->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void **)&audioInterface_);
290
if (FAILED(hresult) || audioInterface_ == nullptr)
291
return false;
292
293
return true;
294
}
295
296
bool WASAPIAudioThread::InitAudioDevice() {
297
REFERENCE_TIME hnsBufferDuration = REFTIMES_PER_SEC;
298
_assert_(deviceFormat_ == nullptr);
299
HRESULT hresult = audioInterface_->GetMixFormat((WAVEFORMATEX **)&deviceFormat_);
300
if (FAILED(hresult) || !deviceFormat_)
301
return false;
302
303
if (!DetectFormat()) {
304
// Format unsupported - let's not even try to initialize.
305
return false;
306
}
307
308
hresult = audioInterface_->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, hnsBufferDuration, 0, &deviceFormat_->Format, nullptr);
309
if (FAILED(hresult))
310
return false;
311
_assert_(renderClient_ == nullptr);
312
hresult = audioInterface_->GetService(IID_IAudioRenderClient, (void **)&renderClient_);
313
if (FAILED(hresult) || !renderClient_)
314
return false;
315
316
numBufferFrames = 0;
317
hresult = audioInterface_->GetBufferSize(&numBufferFrames);
318
if (FAILED(hresult) || numBufferFrames == 0)
319
return false;
320
321
sampleRate_ = deviceFormat_->Format.nSamplesPerSec;
322
323
return true;
324
}
325
326
void WASAPIAudioThread::ShutdownAudioDevice() {
327
SAFE_RELEASE(renderClient_);
328
CoTaskMemFree(deviceFormat_);
329
deviceFormat_ = nullptr;
330
SAFE_RELEASE(audioInterface_);
331
SAFE_RELEASE(device_);
332
}
333
334
bool WASAPIAudioThread::DetectFormat() {
335
if (deviceFormat_ && !ValidateFormat(deviceFormat_)) {
336
// Last chance, let's try to ask for one we support instead.
337
WAVEFORMATEXTENSIBLE fmt{};
338
fmt.Format.cbSize = sizeof(fmt);
339
fmt.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
340
fmt.Format.nChannels = 2;
341
fmt.Format.nSamplesPerSec = 44100;
342
if (deviceFormat_->Format.nSamplesPerSec >= 22050 && deviceFormat_->Format.nSamplesPerSec <= 192000)
343
fmt.Format.nSamplesPerSec = deviceFormat_->Format.nSamplesPerSec;
344
fmt.Format.nBlockAlign = 2 * sizeof(float);
345
fmt.Format.nAvgBytesPerSec = fmt.Format.nSamplesPerSec * fmt.Format.nBlockAlign;
346
fmt.Format.wBitsPerSample = sizeof(float) * 8;
347
fmt.Samples.wReserved = 0;
348
fmt.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
349
fmt.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
350
351
WAVEFORMATEXTENSIBLE *closest = nullptr;
352
HRESULT hr = audioInterface_->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, &fmt.Format, (WAVEFORMATEX **)&closest);
353
if (hr == S_OK) {
354
// Okay, great. Let's just use ours.
355
CoTaskMemFree(closest);
356
CoTaskMemFree(deviceFormat_);
357
deviceFormat_ = (WAVEFORMATEXTENSIBLE *)CoTaskMemAlloc(sizeof(fmt));
358
if (deviceFormat_)
359
memcpy(deviceFormat_, &fmt, sizeof(fmt));
360
361
// In case something above gets out of date.
362
return ValidateFormat(deviceFormat_);
363
} else if (hr == S_FALSE && closest != nullptr) {
364
// This means check closest. We'll allow it only if it's less specific on channels.
365
if (ValidateFormat(closest)) {
366
CoTaskMemFree(deviceFormat_);
367
deviceFormat_ = closest;
368
} else {
369
wchar_t guid[256]{};
370
StringFromGUID2(closest->SubFormat, guid, 256);
371
ERROR_LOG_REPORT_ONCE(badfallbackclosest, Log::sceAudio, "WASAPI fallback and closest unsupported (fmt=%04x/%s)", closest->Format.wFormatTag, guid);
372
CoTaskMemFree(closest);
373
return false;
374
}
375
} else {
376
CoTaskMemFree(closest);
377
if (hr != AUDCLNT_E_DEVICE_INVALIDATED && hr != AUDCLNT_E_SERVICE_NOT_RUNNING)
378
ERROR_LOG_REPORT_ONCE(badfallback, Log::sceAudio, "WASAPI fallback format was unsupported (%08x)", hr);
379
return false;
380
}
381
}
382
383
return true;
384
}
385
386
bool WASAPIAudioThread::ValidateFormat(const WAVEFORMATEXTENSIBLE *fmt) {
387
// Don't know if PCM16 ever shows up here, the documentation only talks about float... but let's blindly
388
// try to support it :P
389
format_ = Format::UNKNOWN;
390
if (!fmt)
391
return false;
392
393
if (fmt->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
394
if (!memcmp(&fmt->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(fmt->SubFormat))) {
395
if (fmt->Format.nChannels >= 1)
396
format_ = Format::IEEE_FLOAT;
397
} else {
398
wchar_t guid[256]{};
399
StringFromGUID2(fmt->SubFormat, guid, 256);
400
ERROR_LOG_REPORT_ONCE(unexpectedformat, Log::sceAudio, "Got unexpected WASAPI 0xFFFE stream format (%S), expected float!", guid);
401
if (fmt->Format.wBitsPerSample == 16 && fmt->Format.nChannels == 2) {
402
format_ = Format::PCM16;
403
}
404
}
405
} else if (fmt->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
406
if (fmt->Format.nChannels >= 1)
407
format_ = Format::IEEE_FLOAT;
408
} else {
409
ERROR_LOG_REPORT_ONCE(unexpectedformat2, Log::sceAudio, "Got unexpected non-extensible WASAPI stream format, expected extensible float!");
410
if (fmt->Format.wBitsPerSample == 16 && fmt->Format.nChannels == 2) {
411
format_ = Format::PCM16;
412
}
413
}
414
415
return format_ != Format::UNKNOWN;
416
}
417
418
bool WASAPIAudioThread::PrepareFormat() {
419
delete [] shortBuf_;
420
shortBuf_ = nullptr;
421
422
BYTE *pData = nullptr;
423
HRESULT hresult = renderClient_->GetBuffer(numBufferFrames, &pData);
424
if (FAILED(hresult) || !pData)
425
return false;
426
427
const int numSamples = numBufferFrames * deviceFormat_->Format.nChannels;
428
if (format_ == Format::IEEE_FLOAT) {
429
memset(pData, 0, sizeof(float) * numSamples);
430
// This buffer is always stereo - PPSSPP writes to it.
431
shortBuf_ = new short[numBufferFrames * 2];
432
} else if (format_ == Format::PCM16) {
433
memset(pData, 0, sizeof(short) * numSamples);
434
}
435
436
hresult = renderClient_->ReleaseBuffer(numBufferFrames, 0);
437
if (FAILED(hresult))
438
return false;
439
440
actualDuration_ = (REFERENCE_TIME)((double)REFTIMES_PER_SEC * numBufferFrames / deviceFormat_->Format.nSamplesPerSec);
441
return true;
442
}
443
444
void WASAPIAudioThread::Run() {
445
// Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/dd316756(v=vs.85).aspx
446
447
_assert_(deviceEnumerator_ == nullptr);
448
HRESULT hresult = CoCreateInstance(CLSID_MMDeviceEnumerator,
449
nullptr, /* Object is not created as the part of the aggregate */
450
CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&deviceEnumerator_);
451
452
if (FAILED(hresult) || deviceEnumerator_ == nullptr)
453
return;
454
455
if (!ActivateDefaultDevice()) {
456
ERROR_LOG(Log::sceAudio, "WASAPI: Could not activate default device");
457
return;
458
}
459
460
notificationClient_ = new CMMNotificationClient();
461
notificationClient_->SetCurrentDevice(device_);
462
hresult = deviceEnumerator_->RegisterEndpointNotificationCallback(notificationClient_);
463
if (FAILED(hresult)) {
464
// Let's just keep going, but release the client since it doesn't work.
465
delete notificationClient_;
466
notificationClient_ = nullptr;
467
}
468
469
if (!InitAudioDevice()) {
470
ERROR_LOG(Log::sceAudio, "WASAPI: Could not init audio device");
471
return;
472
}
473
if (!PrepareFormat()) {
474
ERROR_LOG(Log::sceAudio, "WASAPI: Could not find a suitable audio output format");
475
return;
476
}
477
478
hresult = audioInterface_->Start();
479
if (FAILED(hresult)) {
480
ERROR_LOG(Log::sceAudio, "WASAPI: Failed to start audio stream");
481
return;
482
}
483
484
DWORD flags = 0;
485
while (flags != AUDCLNT_BUFFERFLAGS_SILENT) {
486
Sleep((DWORD)(actualDuration_ / REFTIMES_PER_MILLISEC / 2));
487
488
uint32_t pNumPaddingFrames = 0;
489
hresult = audioInterface_->GetCurrentPadding(&pNumPaddingFrames);
490
if (FAILED(hresult)) {
491
// What to do?
492
pNumPaddingFrames = 0;
493
}
494
uint32_t pNumAvFrames = numBufferFrames - pNumPaddingFrames;
495
496
BYTE *pData = nullptr;
497
hresult = renderClient_->GetBuffer(pNumAvFrames, &pData);
498
if (FAILED(hresult) || pData == nullptr) {
499
// What to do?
500
} else if (pNumAvFrames) {
501
int chans = deviceFormat_->Format.nChannels;
502
switch (format_) {
503
case Format::IEEE_FLOAT:
504
callback_(shortBuf_, pNumAvFrames, sampleRate_);
505
if (chans == 1) {
506
float *ptr = (float *)pData;
507
memset(ptr, 0, pNumAvFrames * chans * sizeof(float));
508
for (uint32_t i = 0; i < pNumAvFrames; i++) {
509
ptr[i * chans + 0] = 0.5f * ((float)shortBuf_[i * 2] + (float)shortBuf_[i * 2 + 1]) * (1.0f / 32768.0f);
510
}
511
} else if (chans == 2) {
512
ConvertS16ToF32((float *)pData, shortBuf_, pNumAvFrames * chans);
513
} else if (chans > 2) {
514
float *ptr = (float *)pData;
515
memset(ptr, 0, pNumAvFrames * chans * sizeof(float));
516
for (uint32_t i = 0; i < pNumAvFrames; i++) {
517
ptr[i * chans + 0] = (float)shortBuf_[i * 2] * (1.0f / 32768.0f);
518
ptr[i * chans + 1] = (float)shortBuf_[i * 2 + 1] * (1.0f / 32768.0f);
519
}
520
}
521
break;
522
case Format::PCM16:
523
callback_((short *)pData, pNumAvFrames, sampleRate_);
524
break;
525
}
526
}
527
528
if (threadData_ != 0) {
529
flags = AUDCLNT_BUFFERFLAGS_SILENT;
530
}
531
532
if (!FAILED(hresult) && pData) {
533
hresult = renderClient_->ReleaseBuffer(pNumAvFrames, flags);
534
if (FAILED(hresult)) {
535
// Not much to do here either...
536
}
537
}
538
539
// Check if we should use a new device.
540
if (notificationClient_ && notificationClient_->HasDefaultDeviceChanged() && g_Config.bAutoAudioDevice) {
541
hresult = audioInterface_->Stop();
542
ShutdownAudioDevice();
543
544
if (!ActivateDefaultDevice()) {
545
ERROR_LOG(Log::sceAudio, "WASAPI: Could not activate default device");
546
// TODO: Return to the old device here?
547
return;
548
}
549
notificationClient_->SetCurrentDevice(device_);
550
if (!InitAudioDevice()) {
551
ERROR_LOG(Log::sceAudio, "WASAPI: Could not init audio device");
552
return;
553
}
554
if (!PrepareFormat()) {
555
ERROR_LOG(Log::sceAudio, "WASAPI: Could not find a suitable audio output format");
556
return;
557
}
558
559
hresult = audioInterface_->Start();
560
if (FAILED(hresult)) {
561
ERROR_LOG(Log::sceAudio, "WASAPI: Failed to start audio stream");
562
return;
563
}
564
}
565
}
566
567
// Wait for last data in buffer to play before stopping.
568
Sleep((DWORD)(actualDuration_ / REFTIMES_PER_MILLISEC / 2));
569
570
hresult = audioInterface_->Stop();
571
if (FAILED(hresult)) {
572
ERROR_LOG(Log::sceAudio, "WASAPI: Failed to stop audio stream");
573
}
574
}
575
576
int WASAPIAudioBackend::RunThread() {
577
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
578
_dbg_assert_(SUCCEEDED(hr));
579
SetCurrentThreadName("WASAPI_audio");
580
581
if (threadData_ == 0) {
582
// This will free everything once it's done.
583
WASAPIAudioThread renderer(threadData_, sampleRate_, callback_);
584
renderer.Run();
585
}
586
587
threadData_ = 2;
588
CoUninitialize();
589
return 0;
590
}
591
592