-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathAudioCaptureImpl_WASAPI.cpp
650 lines (529 loc) · 18.2 KB
/
AudioCaptureImpl_WASAPI.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#include "AudioCaptureImpl_WASAPI.h"
#include <projectM-4/projectM.h>
#include <Poco/UnicodeConverter.h>
#include <functiondiscoverykeys_devpkey.h>
#include <mmdeviceapi.h>
#include <objbase.h>
AudioCaptureImpl::AudioCaptureImpl()
: _captureThread(this, &AudioCaptureImpl::CaptureThread)
{
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
}
AudioCaptureImpl::~AudioCaptureImpl()
{
CoUninitialize();
}
std::map<int, std::string> AudioCaptureImpl::AudioDeviceList()
{
std::map<int, std::string> deviceList{
{-1, _defaultDeviceName}};
IMMDeviceEnumerator* enumerator{GetDeviceEnumerator()};
auto captureDevices{GetAudioDeviceList(enumerator)};
enumerator->Release();
uint32_t index{0};
for (const auto& device : captureDevices)
{
if (device.DeviceId() != nullptr)
{
deviceList.insert(std::make_pair(index, device.FriendlyName()));
}
index++;
}
return deviceList;
}
void AudioCaptureImpl::StartRecording(projectm* projectMHandle, int audioDeviceIndex)
{
_projectMHandle = projectMHandle;
_currentAudioDeviceIndex = audioDeviceIndex;
_isCapturing = true;
_captureThreadResult = _captureThread();
}
void AudioCaptureImpl::StopRecording()
{
if (_isCapturing)
{
poco_trace(_logger, "Stopping audio capturing thread.");
_isCapturing = false;
_fillBufferEvent.set();
_captureThreadResult.wait();
poco_trace(_logger, "Audio capturing thread joined.");
}
}
void AudioCaptureImpl::NextAudioDevice()
{
StopRecording();
IMMDeviceEnumerator* enumerator{GetDeviceEnumerator()};
auto captureDevices{GetAudioDeviceList(enumerator)};
enumerator->Release();
// Will wrap around to loopback capture device (-1).
int nextAudioDeviceId = ((_currentAudioDeviceIndex + 2) % (static_cast<int>(captureDevices.size()) + 1)) - 1;
StartRecording(_projectMHandle, nextAudioDeviceId);
}
void AudioCaptureImpl::AudioDeviceIndex(int index)
{
IMMDeviceEnumerator* enumerator{GetDeviceEnumerator()};
auto captureDevices{GetAudioDeviceList(enumerator)};
enumerator->Release();
if (index >= -1 && index < static_cast<int>(captureDevices.size()))
{
_currentAudioDeviceIndex = index;
StopRecording();
StartRecording(_projectMHandle, index);
}
}
int AudioCaptureImpl::AudioDeviceIndex() const
{
return _currentAudioDeviceIndex;
}
std::string AudioCaptureImpl::AudioDeviceName() const
{
if (_currentAudioDeviceIndex < 0)
{
return "System Default Audio Device";
}
IMMDeviceEnumerator* enumerator{GetDeviceEnumerator()};
auto captureDevices{GetAudioDeviceList(enumerator)};
enumerator->Release();
if (captureDevices.empty() || _currentAudioDeviceIndex >= static_cast<int>(captureDevices.size()))
{
return {};
}
return captureDevices.at(_currentAudioDeviceIndex).FriendlyName();
}
void AudioCaptureImpl::FillBuffer()
{
if (_isCapturing)
{
_bufferFilledEvent.reset();
_fillBufferEvent.set();
try
{
_bufferFilledEvent.wait(20);
}
catch (Poco::TimeoutException& ex)
{
poco_debug(_logger, "Timeout waiting for audio buffer fill");
}
}
}
HRESULT AudioCaptureImpl::QueryInterface(const IID& riid, void** ppvObject)
{
if (ppvObject == nullptr)
{
return E_POINTER;
}
if (riid == IID_IUnknown)
{
*ppvObject = static_cast<IUnknown*>(this);
AddRef();
}
else if (riid == __uuidof(IMMNotificationClient))
{
*ppvObject = static_cast<IMMNotificationClient*>(this);
AddRef();
}
return E_NOINTERFACE;
}
ULONG AudioCaptureImpl::AddRef()
{
return InterlockedIncrement(&_referenceCount);
}
ULONG AudioCaptureImpl::Release()
{
return InterlockedDecrement(&_referenceCount);
}
std::string AudioCaptureImpl::UnicodeToString(LPCWSTR unicodeString)
{
std::string utf8String;
Poco::UnicodeConverter::convert(std::wstring(unicodeString), utf8String);
return utf8String;
}
std::vector<AudioCaptureImpl::AudioDevice> AudioCaptureImpl::GetAudioDeviceList(IMMDeviceEnumerator* enumerator) const
{
auto addEndpoints = [this, enumerator](std::vector<AudioDevice>& deviceList, EDataFlow dataFlow) {
HRESULT result{S_OK};
IMMDeviceCollection* audioEndpoints{nullptr};
result = enumerator->EnumAudioEndpoints(dataFlow, DEVICE_STATE_ACTIVE, &audioEndpoints);
if (FAILED(result))
{
poco_error_f1(_logger, "IMMDeviceEnumerator::EnumAudioEndpoints failed: result = 0x%08?x", result);
return;
}
UINT deviceCount{0};
audioEndpoints->GetCount(&deviceCount);
for (UINT item = 0; item < deviceCount; item++)
{
IMMDevice* device{nullptr};
result = audioEndpoints->Item(item, &device);
if (FAILED(result) || device == nullptr)
{
poco_error_f2(_logger, "IMMDeviceEnumerator::Item failed for device %?u: result = 0x%08?x", item, result);
continue;
}
deviceList.emplace_back(device, (dataFlow == eRender));
device->Release();
}
audioEndpoints->Release();
};
std::vector<AudioDevice> deviceList;
addEndpoints(deviceList, eRender);
addEndpoints(deviceList, eCapture);
return deviceList;
}
bool AudioCaptureImpl::OpenAudioDevice(IMMDevice* device, bool useLoopback)
{
// activate an IAudioClient
HRESULT result = device->Activate(__uuidof(IAudioClient),
CLSCTX_ALL,
nullptr,
reinterpret_cast<void**>(&_audioClient));
if (FAILED(result))
{
poco_error_f1(_logger, "IMMDevice::Activate(IAudioClient) failed: result = 0x%08?x", result);
return false;
}
// get the default device periodicity
REFERENCE_TIME hnsDefaultDevicePeriod;
result = _audioClient->GetDevicePeriod(&hnsDefaultDevicePeriod,
nullptr);
if (FAILED(result))
{
poco_error_f1(_logger, "IAudioClient::GetDevicePeriod failed: result = 0x%08?x", result);
return false;
}
// get the default device format
WAVEFORMATEX* pwfx;
result = _audioClient->GetMixFormat(&pwfx);
if (FAILED(result))
{
poco_error_f1(_logger, "IAudioClient::GetMixFormat failed: result = 0x%08?x", result);
return false;
}
// Should default to float32 data, but some devices might deliver other formats.
if (pwfx->wFormatTag != WAVE_FORMAT_IEEE_FLOAT)
{
auto extensibleFormat = reinterpret_cast<PWAVEFORMATEXTENSIBLE>(pwfx);
if (pwfx->wFormatTag != WAVE_FORMAT_EXTENSIBLE)
{
poco_error_f1(_logger, "IAudioClient::GetMixFormat returned non-float sample format: 0x%04?x", pwfx->wFormatTag);
CoTaskMemFree(pwfx);
return false;
}
else if (!IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, extensibleFormat->SubFormat))
{
poco_error_f1(_logger, "IAudioClient::GetMixFormat returned non-float extensible sub format: 0x%04?x", extensibleFormat->SubFormat);
CoTaskMemFree(pwfx);
return false;
}
}
_channels = pwfx->nChannels;
// Can't use event-driven processing in loopback mode, but as we
// get a "fill buffer" request before rendering each frame, this isn't
// really necessary anyway.
result = _audioClient->Initialize(
AUDCLNT_SHAREMODE_SHARED,
useLoopback ? AUDCLNT_STREAMFLAGS_LOOPBACK : 0,
hnsDefaultDevicePeriod,
0,
pwfx,
nullptr);
CoTaskMemFree(pwfx);
if (FAILED(result))
{
poco_error_f1(_logger, "IAudioClient->Initialize failed: result = 0x%08?x", result);
return false;
}
// activate an IAudioCaptureClient
result = _audioClient->GetService(
__uuidof(IAudioCaptureClient),
(void**) &_audioCaptureClient);
if (FAILED(result))
{
poco_error_f1(_logger, "IAudioClient->GetService failed: result = 0x%08?x", result);
return false;
}
result = _audioClient->Start();
if (FAILED(result))
{
poco_error_f1(_logger, "IAudioClient->Start failed: result = 0x%08?x", result);
return false;
}
return true;
}
void AudioCaptureImpl::CloseAudioDevice(IMMDevice* device)
{
poco_trace(_logger, "Stopping audio client.");
_audioClient->Stop();
if (_audioCaptureClient)
{
poco_trace(_logger, "Releasing audio capture client.");
_audioCaptureClient->Release();
_audioCaptureClient = nullptr;
}
if (_audioClient)
{
poco_trace(_logger, "Releasing audio client.");
_audioClient->Release();
poco_trace(_logger, "Audio client released.");
_audioClient = nullptr;
}
if (device)
{
poco_trace(_logger, "Releasing audio device.");
device->Release();
}
}
void AudioCaptureImpl::CaptureThread()
{
poco_debug(_logger, "Audio capture thread starting.");
HRESULT result = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(result))
{
poco_error_f1(_logger, "CoInitializeEx() failed: result = 0x%08?x", result);
throw std::bad_alloc();
}
IMMDeviceEnumerator* enumerator{GetDeviceEnumerator()};
if (enumerator == nullptr)
{
CoUninitialize();
throw std::bad_alloc();
}
poco_trace(_logger, "Registering device callbacks.");
enumerator->RegisterEndpointNotificationCallback(this);
do
{
_restartCapturing = false;
auto devices{GetAudioDeviceList(enumerator)};
bool useLoopback{true};
std::string deviceName;
IMMDevice* device{nullptr};
if (_currentAudioDeviceIndex == -1 || _currentAudioDeviceIndex >= devices.size())
{
// Get the default render endpoint for opening it as a loopback device.
result = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
if (FAILED(result))
{
poco_error_f1(_logger, "IMMDeviceEnumerator::GetDefaultAudioEndpoint failed: result = 0x%08?x", result);
break;
}
deviceName = _defaultDeviceName;
}
else
{
// Get a device by its ID according to the currently selected index.
useLoopback = devices.at(_currentAudioDeviceIndex).IsRenderDevice();
deviceName = devices.at(_currentAudioDeviceIndex).FriendlyName();
result = enumerator->GetDevice(devices.at(_currentAudioDeviceIndex).DeviceId(), &device);
if (FAILED(result))
{
poco_error_f1(_logger, "IMMDeviceEnumerator::GetDevice failed: result = 0x%08?x", result);
break;
}
}
LPWSTR deviceID{nullptr};
result = device->GetId(&deviceID);
if (FAILED(result))
{
poco_error_f1(_logger, "IMMDevice::GetId failed: result = 0x%08?x", result);
}
else
{
_currentCaptureDeviceId = UnicodeToString(deviceID);
}
if (!OpenAudioDevice(device, useLoopback))
{
_isCapturing = false;
}
poco_information_f3(_logger, "Audio device opened: %s (channels: %hu, loopback: %b)", deviceName, _channels, useLoopback);
while (_isCapturing && !_restartCapturing)
{
try
{
_fillBufferEvent.tryWait(500);
}
catch (Poco::TimeoutException& ex)
{
poco_debug(_logger, "FillBuffer event timeout, proceeding to flush buffer or abort.");
}
if (!_isCapturing)
{
break;
}
UINT32 packetLength;
_audioCaptureClient->GetNextPacketSize(&packetLength);
while (packetLength != 0)
{
BYTE* data;
UINT32 framesAvailable;
DWORD flags;
result = _audioCaptureClient->GetBuffer(&data, &framesAvailable, &flags, nullptr, nullptr);
if (FAILED(result))
{
poco_error_f1(_logger, "IAudioCaptureClient::GetBuffer failed: result = 0x%08?x", result);
_isCapturing = false;
break;
}
if (flags & AUDCLNT_BUFFERFLAGS_SILENT)
{
data = nullptr;
}
poco_trace_f1(_logger, "Audio frames available for capturing: %u", data ? framesAvailable : 0);
if (framesAvailable > 0 && data != nullptr)
{
projectm_pcm_add_float(_projectMHandle, reinterpret_cast<float*>(data), framesAvailable, static_cast<projectm_channels>(_channels));
}
_audioCaptureClient->ReleaseBuffer(framesAvailable);
_audioCaptureClient->GetNextPacketSize(&packetLength);
}
_bufferFilledEvent.set();
}
CloseAudioDevice(device);
poco_debug(_logger, "Audio device closed.");
} while (_restartCapturing);
poco_trace(_logger, "Unregistering device callbacks.");
enumerator->UnregisterEndpointNotificationCallback(this);
poco_trace(_logger, "Releasing audio device enumerator.");
enumerator->Release();
poco_trace(_logger, "Audio device enumerator released.");
CoUninitialize();
poco_debug(_logger, "Audio capture thread exiting.");
}
IMMDeviceEnumerator* AudioCaptureImpl::GetDeviceEnumerator() const
{
IMMDeviceEnumerator* enumerator{nullptr};
HRESULT result = CoCreateInstance(
__uuidof(MMDeviceEnumerator),
nullptr,
CLSCTX_ALL,
__uuidof(IMMDeviceEnumerator),
(void**) &enumerator);
if (FAILED(result))
{
poco_error_f1(_logger, "CoCreateInstance(IMMDeviceEnumerator) failed: result = 0x%08?x", result);
}
return enumerator;
}
HRESULT AudioCaptureImpl::OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState)
{
auto deviceId{UnicodeToString(pwstrDeviceId)};
poco_trace_f2(_logger, "Audio device state changed for device ID %s: %lu", deviceId, dwNewState);
// Recalculate current device index and restart only if not default.
if (_currentAudioDeviceIndex >= 0)
{
IMMDeviceEnumerator* enumerator{GetDeviceEnumerator()};
auto captureDevices{GetAudioDeviceList(enumerator)};
enumerator->Release();
for (int index = 0; index < captureDevices.size(); ++index)
{
if (UnicodeToString(captureDevices.at(index).DeviceId()) == deviceId)
{
_currentAudioDeviceIndex = index;
break;
}
}
// Restart capturing only in case the current device state changed.
if (_isCapturing && deviceId == _currentCaptureDeviceId)
{
_restartCapturing = true;
}
}
return S_OK;
}
HRESULT AudioCaptureImpl::OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDefaultDeviceId)
{
poco_trace_f1(_logger, "Default audio device changed to ID %s", UnicodeToString(pwstrDefaultDeviceId));
if (flow != eRender || _currentAudioDeviceIndex != -1)
{
return S_OK;
}
if (_isCapturing)
{
_restartCapturing = true;
}
return S_OK;
}
HRESULT AudioCaptureImpl::OnDeviceAdded(LPCWSTR pwstrDeviceId)
{
poco_trace_f1(_logger, "Audio device added: %s", UnicodeToString(pwstrDeviceId));
return S_OK;
}
HRESULT AudioCaptureImpl::OnDeviceRemoved(LPCWSTR pwstrDeviceId)
{
poco_trace_f1(_logger, "Audio device removed: %s", UnicodeToString(pwstrDeviceId));
return S_OK;
}
HRESULT AudioCaptureImpl::OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key)
{
poco_trace_f1(_logger, "Audio device property changed for device ID %s", UnicodeToString(pwstrDeviceId));
return S_OK;
}
AudioCaptureImpl::AudioDevice::AudioDevice(IMMDevice* device, bool isRenderDevice)
: _friendlyName(GetAudioEndpointFriendlyName(device))
, _isRenderDevice(isRenderDevice)
{
if (device != nullptr)
{
HRESULT result = device->GetId(&_deviceId);
if (FAILED(result))
{
poco_trace_f1(_logger, "IMMDevice::GetId failed: result = 0x%08?x", result);
}
poco_trace_f3(_logger, R"(Added WASAPI audio device "%s" with ID %s (Render device: %b))",
_friendlyName, AudioCaptureImpl::UnicodeToString(_deviceId), _isRenderDevice);
}
}
AudioCaptureImpl::AudioDevice::AudioDevice(AudioCaptureImpl::AudioDevice&& other) noexcept
{
_deviceId = other._deviceId;
other._deviceId = nullptr;
_friendlyName = std::move(other._friendlyName);
_isRenderDevice = other._isRenderDevice;
}
AudioCaptureImpl::AudioDevice::~AudioDevice()
{
if (_deviceId)
{
CoTaskMemFree(_deviceId);
_deviceId = nullptr;
}
}
std::string AudioCaptureImpl::AudioDevice::GetAudioEndpointFriendlyName(IMMDevice* pMMDevice)
{
HRESULT result{S_OK};
if (pMMDevice == nullptr)
{
return AudioCaptureImpl::_defaultDeviceName;
}
IPropertyStore* deviceProps{nullptr};
result = pMMDevice->OpenPropertyStore(STGM_READ, &deviceProps);
if (FAILED(result) || deviceProps == nullptr)
{
poco_error_f1(_logger, "IMMDevice::OpenPropertyStore failed: result = 0x%08?x", result);
return {};
}
PROPVARIANT variantName;
PropVariantInit(&variantName);
result = deviceProps->GetValue(PKEY_Device_FriendlyName, &variantName);
if (FAILED(result))
{
deviceProps->Release();
poco_error_f1(_logger, "IMMDevice::OpenPropertyStore failed: result = 0x%08?x", result);
return {};
}
std::string deviceFriendlyName = AudioCaptureImpl::UnicodeToString(variantName.pwszVal);
PropVariantClear(&variantName);
deviceProps->Release();
return deviceFriendlyName;
}
LPWSTR AudioCaptureImpl::AudioDevice::DeviceId() const
{
return _deviceId;
}
std::string AudioCaptureImpl::AudioDevice::FriendlyName() const
{
return _friendlyName;
}
bool AudioCaptureImpl::AudioDevice::IsRenderDevice() const
{
return _isRenderDevice;
}