/
WASAPIStream.cpp
405 lines (346 loc) · 11.6 KB
/
WASAPIStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "WindowsAudio.h"
#include "WASAPIStream.h"
#include "Common/Log.h"
#include "Core/Reporting.h"
#include "Core/Util/AudioFormat.h"
#include "thread/threadutil.h"
#include <Objbase.h>
#include <Mmreg.h>
#include <MMDeviceAPI.h>
#include <AudioClient.h>
#include <AudioPolicy.h>
#include "Functiondiscoverykeys_devpkey.h"
// Includes some code from https://msdn.microsoft.com/en-us/library/dd370810%28VS.85%29.aspx?f=255&MSPPError=-2147217396
#pragma comment(lib, "ole32.lib")
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
const IID IID_IAudioClient = __uuidof(IAudioClient);
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
// Adapted from a MSDN sample.
#define SAFE_RELEASE(punk) \
if ((punk) != NULL) \
{ (punk)->Release(); (punk) = NULL; }
class CMMNotificationClient : public IMMNotificationClient {
public:
CMMNotificationClient() :
_cRef(1),
_pEnumerator(NULL) {
}
~CMMNotificationClient() {
SAFE_RELEASE(_pEnumerator)
}
// IUnknown methods -- AddRef, Release, and QueryInterface
ULONG STDMETHODCALLTYPE AddRef() override {
return InterlockedIncrement(&_cRef);
}
ULONG STDMETHODCALLTYPE Release() override {
ULONG ulRef = InterlockedDecrement(&_cRef);
if (0 == ulRef) {
delete this;
}
return ulRef;
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) override {
if (IID_IUnknown == riid) {
AddRef();
*ppvInterface = (IUnknown*)this;
} else if (__uuidof(IMMNotificationClient) == riid) {
AddRef();
*ppvInterface = (IMMNotificationClient*)this;
} else {
*ppvInterface = NULL;
return E_NOINTERFACE;
}
return S_OK;
}
// Callback methods for device-event notifications.
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) {
PrintDeviceName(pwstrDeviceId);
const char *pszFlow = "?????";
const char *pszRole = "?????";
switch (flow) {
case eRender:
pszFlow = "eRender";
break;
case eCapture:
pszFlow = "eCapture";
break;
}
switch (role) {
case eConsole:
pszRole = "eConsole";
break;
case eMultimedia:
pszRole = "eMultimedia";
break;
case eCommunications:
pszRole = "eCommunications";
break;
}
INFO_LOG(SCEAUDIO, " -->New default device: flow = %s, role = %s\n", pszFlow, pszRole);
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) {
PrintDeviceName(pwstrDeviceId);
INFO_LOG(SCEAUDIO, " -->Added device\n");
return S_OK;
};
HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) {
PrintDeviceName(pwstrDeviceId);
INFO_LOG(SCEAUDIO, " -->Removed device\n");
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) {
const char *pszState = "?????";
PrintDeviceName(pwstrDeviceId);
switch (dwNewState) {
case DEVICE_STATE_ACTIVE:
pszState = "ACTIVE";
break;
case DEVICE_STATE_DISABLED:
pszState = "DISABLED";
break;
case DEVICE_STATE_NOTPRESENT:
pszState = "NOTPRESENT";
break;
case DEVICE_STATE_UNPLUGGED:
pszState = "UNPLUGGED";
break;
}
INFO_LOG(SCEAUDIO, " -->New device state is DEVICE_STATE_%s (0x%8.8x)\n", pszState, dwNewState);
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) {
PrintDeviceName(pwstrDeviceId);
INFO_LOG(SCEAUDIO, " -->Changed device property "
"{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}#%d\n",
key.fmtid.Data1, key.fmtid.Data2, key.fmtid.Data3,
key.fmtid.Data4[0], key.fmtid.Data4[1],
key.fmtid.Data4[2], key.fmtid.Data4[3],
key.fmtid.Data4[4], key.fmtid.Data4[5],
key.fmtid.Data4[6], key.fmtid.Data4[7],
key.pid);
return S_OK;
}
private:
LONG _cRef;
IMMDeviceEnumerator *_pEnumerator;
// Private function to print device-friendly name
HRESULT PrintDeviceName(LPCWSTR pwstrId);
};
// Given an endpoint ID string, print the friendly device name.
HRESULT CMMNotificationClient::PrintDeviceName(LPCWSTR pwstrId) {
HRESULT hr = S_OK;
IMMDevice *pDevice = NULL;
IPropertyStore *pProps = NULL;
PROPVARIANT varString;
CoInitialize(NULL);
PropVariantInit(&varString);
if (_pEnumerator == NULL) {
// Get enumerator for audio endpoint devices.
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
NULL, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator),
(void**)&_pEnumerator);
}
if (hr == S_OK) {
hr = _pEnumerator->GetDevice(pwstrId, &pDevice);
}
if (hr == S_OK) {
hr = pDevice->OpenPropertyStore(STGM_READ, &pProps);
}
if (hr == S_OK) {
// Get the endpoint device's friendly-name property.
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varString);
}
printf("----------------------\nDevice name: \"%S\"\n"
" Endpoint ID string: \"%S\"\n",
(hr == S_OK) ? varString.pwszVal : L"null device",
(pwstrId != NULL) ? pwstrId : L"null ID");
PropVariantClear(&varString);
SAFE_RELEASE(pProps)
SAFE_RELEASE(pDevice)
CoUninitialize();
return hr;
}
// TODO: Make these adjustable. This is from the example in MSDN.
// 200 times/sec = 5ms, pretty good :) Wonder if all computers can handle it though.
#define REFTIMES_PER_SEC (10000000/200)
#define REFTIMES_PER_MILLISEC (REFTIMES_PER_SEC / 1000)
WASAPIAudioBackend::WASAPIAudioBackend() {
}
WASAPIAudioBackend::~WASAPIAudioBackend() {
if (threadData_ == 0) {
threadData_ = 1;
}
if (hThread_) {
WaitForSingleObject(hThread_, 1000);
CloseHandle(hThread_);
hThread_ = nullptr;
}
if (threadData_ == 2) {
// blah.
}
}
unsigned int WINAPI WASAPIAudioBackend::soundThread(void *param) {
WASAPIAudioBackend *backend = (WASAPIAudioBackend *)param;
return backend->RunThread();
}
bool WASAPIAudioBackend::Init(HWND window, StreamCallback callback, int sampleRate) {
threadData_ = 0;
callback_ = callback;
sampleRate_ = sampleRate;
hThread_ = (HANDLE)_beginthreadex(0, 0, soundThread, (void *)this, 0, 0);
SetThreadPriority(hThread_, THREAD_PRIORITY_ABOVE_NORMAL);
return true;
}
int WASAPIAudioBackend::RunThread() {
// Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/dd316756(v=vs.85).aspx
CoInitializeEx(NULL, COINIT_MULTITHREADED);
setCurrentThreadName("WASAPI_audio");
IMMDeviceEnumerator *pDeviceEnumerator;
IMMDevice *pDevice;
IAudioClient *pAudioInterface;
IAudioRenderClient *pAudioRenderClient;
WAVEFORMATEXTENSIBLE *pDeviceFormat;
DWORD flags = 0;
REFERENCE_TIME hnsBufferDuration, hnsActualDuration;
UINT32 pNumBufferFrames;
UINT32 pNumPaddingFrames, pNumAvFrames;
hnsBufferDuration = REFTIMES_PER_SEC;
HRESULT hresult;
hresult = CoCreateInstance(CLSID_MMDeviceEnumerator,
NULL, /*Object is not created as the part of the aggregate */
CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pDeviceEnumerator);
if (FAILED(hresult)) goto bail;
hresult = pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);
if (FAILED(hresult)) {
pDeviceEnumerator->Release();
goto bail;
}
hresult = pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&pAudioInterface);
if (FAILED(hresult)) {
pDevice->Release();
pDeviceEnumerator->Release();
goto bail;
}
notificationClient_ = new CMMNotificationClient();
hresult = pDeviceEnumerator->RegisterEndpointNotificationCallback(notificationClient_);
if (FAILED(hresult)) {
pDevice->Release();
pDeviceEnumerator->Release();
delete notificationClient_;
notificationClient_ = nullptr;
goto bail;
}
hresult = pAudioInterface->GetMixFormat((WAVEFORMATEX**)&pDeviceFormat);
hresult = pAudioInterface->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, hnsBufferDuration, 0, &pDeviceFormat->Format, NULL);
hresult = pAudioInterface->GetService(IID_IAudioRenderClient, (void**)&pAudioRenderClient);
if (FAILED(hresult)) {
pDevice->Release();
pDeviceEnumerator->Release();
pAudioInterface->Release();
goto bail;
}
hresult = pAudioInterface->GetBufferSize(&pNumBufferFrames);
if (FAILED(hresult)) {
pDevice->Release();
pDeviceEnumerator->Release();
pAudioInterface->Release();
goto bail;
}
sampleRate_ = pDeviceFormat->Format.nSamplesPerSec;
enum {
UNKNOWN_FORMAT = 0,
IEEE_FLOAT = 1,
PCM16 = 2,
} format = UNKNOWN_FORMAT;
// Don't know if PCM16 ever shows up here, the documentation only talks about float... but let's blindly
// try to support it :P
if (pDeviceFormat->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
if (!memcmp(&pDeviceFormat->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(pDeviceFormat->SubFormat))) {
format = IEEE_FLOAT;
// printf("float format\n");
} else {
ERROR_LOG_REPORT_ONCE(unexpectedformat, SCEAUDIO, "Got unexpected WASAPI 0xFFFE stream format, expected float!");
if (pDeviceFormat->Format.wBitsPerSample == 16 && pDeviceFormat->Format.nChannels == 2) {
format = PCM16;
}
}
} else if (pDeviceFormat->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
format = IEEE_FLOAT;
} else {
ERROR_LOG_REPORT_ONCE(unexpectedformat2, SCEAUDIO, "Got unexpected non-extensible WASAPI stream format, expected extensible float!");
if (pDeviceFormat->Format.wBitsPerSample == 16 && pDeviceFormat->Format.nChannels == 2) {
format = PCM16;
}
}
short *shortBuf = nullptr;
BYTE *pData;
hresult = pAudioRenderClient->GetBuffer(pNumBufferFrames, &pData);
int numSamples = pNumBufferFrames * pDeviceFormat->Format.nChannels;
if (format == IEEE_FLOAT) {
memset(pData, 0, sizeof(float) * numSamples);
shortBuf = new short[pNumBufferFrames * pDeviceFormat->Format.nChannels];
} else if (format == PCM16) {
memset(pData, 0, sizeof(short) * numSamples);
}
hresult = pAudioRenderClient->ReleaseBuffer(pNumBufferFrames, flags);
hnsActualDuration = (REFERENCE_TIME)((double)REFTIMES_PER_SEC * pNumBufferFrames / pDeviceFormat->Format.nSamplesPerSec);
hresult = pAudioInterface->Start();
while (flags != AUDCLNT_BUFFERFLAGS_SILENT) {
Sleep((DWORD)(hnsActualDuration / REFTIMES_PER_MILLISEC / 2));
hresult = pAudioInterface->GetCurrentPadding(&pNumPaddingFrames);
if (FAILED(hresult)) {
// What to do?
pNumPaddingFrames = 0;
}
pNumAvFrames = pNumBufferFrames - pNumPaddingFrames;
hresult = pAudioRenderClient->GetBuffer(pNumAvFrames, &pData);
if (FAILED(hresult)) {
// What to do?
} else if (pNumAvFrames) {
switch (format) {
case IEEE_FLOAT:
callback_(shortBuf, pNumAvFrames, 16, sampleRate_, 2);
if (pDeviceFormat->Format.nChannels == 2) {
ConvertS16ToF32((float *)pData, shortBuf, pNumAvFrames * pDeviceFormat->Format.nChannels);
} else {
float *ptr = (float *)pData;
int chans = pDeviceFormat->Format.nChannels;
memset(ptr, 0, pNumAvFrames * chans * sizeof(float));
for (UINT32 i = 0; i < pNumAvFrames; i++) {
ptr[i * chans + 0] = (float)shortBuf[i * 2] * (1.0f / 32768.0f);
ptr[i * chans + 1] = (float)shortBuf[i * 2 + 1] * (1.0f / 32768.0f);
}
}
break;
case PCM16:
callback_((short *)pData, pNumAvFrames, 16, sampleRate_, 2);
break;
}
}
if (threadData_ != 0) {
flags = AUDCLNT_BUFFERFLAGS_SILENT;
}
hresult = pAudioRenderClient->ReleaseBuffer(pNumAvFrames, flags);
if (FAILED(hresult)) {
// Not much to do here either...
}
}
// Wait for last data in buffer to play before stopping.
Sleep((DWORD)(hnsActualDuration / REFTIMES_PER_MILLISEC / 2));
delete[] shortBuf;
hresult = pAudioInterface->Stop();
CoTaskMemFree(pDeviceFormat);
pDeviceEnumerator->UnregisterEndpointNotificationCallback(notificationClient_);
delete notificationClient_;
pDeviceEnumerator->Release();
pDevice->Release();
pAudioInterface->Release();
pAudioRenderClient->Release();
bail:
threadData_ = 2;
CoUninitialize();
return 0;
}