-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioFunctions.cpp
498 lines (435 loc) · 13.2 KB
/
AudioFunctions.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
// Include order matters for these; don't let the autoformatter break things
// clang-format off
#include "windows.h"
#include "endpointvolume.h"
#include "mmdeviceapi.h"
#include "PolicyConfig.h"
#include "Functiondiscoverykeys_devpkey.h"
// clang-format on
#include "AudioFunctions.h"
#include <atlbase.h>
#include <cassert>
#include <codecvt>
#include <locale>
#include <comip.h>
namespace {
[[noreturn]] void assume_unreachable() {
abort();
}
//std::string WCharPtrToString(LPCWSTR in) {
// if (!in) {
// return std::string();
// }
// int utf8_len = WideCharToMultiByte(CP_UTF8, 0, in, -1, 0, 0, 0, 0);
// std::string buf(utf8_len, 0);
// WideCharToMultiByte(CP_UTF8, 0, in, -1, buf.data(), utf8_len, 0, 0);
// buf.resize(utf8_len - 1);
// return buf;
//}
//std::wstring Utf8StrToWString(const std::string& in) {
// int wchar_len
// = MultiByteToWideChar(CP_UTF8, 0, in.c_str(), in.size(), 0, 0);
// std::wstring buf(wchar_len, 0);
// MultiByteToWideChar(CP_UTF8, 0, in.c_str(), in.size(), buf.data(), wchar_len);
// return buf;
//}
// Convert a wide Unicode string to an UTF8 string
std::string WCharPtrToString(const std::wstring &wstr)
{
if( wstr.empty() ) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo( size_needed, 0 );
WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
// Convert an UTF8 string to a wide Unicode String
std::wstring Utf8StrToWString(const std::string &str)
{
if( str.empty() ) return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo( size_needed, 0 );
MultiByteToWideChar (CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
EDataFlow AudioDeviceDirectionToEDataFlow(const AudioDeviceDirection dir) {
switch (dir) {
case AudioDeviceDirection::INPUT:
return eCapture;
case AudioDeviceDirection::OUTPUT:
return eRender;
}
__assume(0);
}
ERole AudioDeviceRoleToERole(const AudioDeviceRole role) {
switch (role) {
case AudioDeviceRole::COMMUNICATION:
return eCommunications;
case AudioDeviceRole::DEFAULT:
return eConsole;
}
__assume(0);
}
CComPtr<IMMDevice> DeviceIDToDevice(const std::string& in) {
CComPtr<IMMDeviceEnumerator> de;
de.CoCreateInstance(__uuidof(MMDeviceEnumerator));
CComPtr<IMMDevice> device;
de->GetDevice(Utf8StrToWString(in).data(), &device);
return device;
}
CComPtr<IAudioEndpointVolume> DeviceIDToAudioEndpointVolume(
const std::string& deviceID) {
auto device = DeviceIDToDevice(deviceID);
if (!device) {
return nullptr;
}
CComPtr<IAudioEndpointVolume> volume;
device->Activate(
__uuidof(IAudioEndpointVolume), CLSCTX_ALL, nullptr, (void**)&volume);
return volume;
}
AudioDeviceState GetAudioDeviceState(CComPtr<IMMDevice> device) {
DWORD nativeState;
device->GetState(&nativeState);
switch (nativeState) {
case DEVICE_STATE_ACTIVE:
return AudioDeviceState::CONNECTED;
case DEVICE_STATE_DISABLED:
return AudioDeviceState::DEVICE_DISABLED;
case DEVICE_STATE_NOTPRESENT:
return AudioDeviceState::DEVICE_NOT_PRESENT;
case DEVICE_STATE_UNPLUGGED:
return AudioDeviceState::DEVICE_PRESENT_NO_CONNECTION;
}
assume_unreachable();
}
}// namespace
AudioDeviceState GetAudioDeviceState(const std::string& id) {
auto device = DeviceIDToDevice(id);
if (device == nullptr) {
return AudioDeviceState::DEVICE_NOT_PRESENT;
}
return GetAudioDeviceState(device);
}
std::map<std::string, AudioDeviceInfo> GetAudioDeviceList(
AudioDeviceDirection direction) {
CComPtr<IMMDeviceEnumerator> de;
de.CoCreateInstance(__uuidof(MMDeviceEnumerator));
CComPtr<IMMDeviceCollection> devices;
de->EnumAudioEndpoints(
AudioDeviceDirectionToEDataFlow(direction), DEVICE_STATEMASK_ALL, &devices);
UINT deviceCount;
devices->GetCount(&deviceCount);
std::map<std::string, AudioDeviceInfo> out;
for (UINT i = 0; i < deviceCount; ++i) {
CComPtr<IMMDevice> device;
devices->Item(i, &device);
LPWSTR nativeID;
device->GetId(&nativeID);
const auto id = WCharPtrToString(nativeID);
CComPtr<IPropertyStore> properties;
device->OpenPropertyStore(STGM_READ, &properties);
PROPVARIANT nativeCombinedName;
properties->GetValue(PKEY_Device_FriendlyName, &nativeCombinedName);
PROPVARIANT nativeInterfaceName;
properties->GetValue(
PKEY_DeviceInterface_FriendlyName, &nativeInterfaceName);
PROPVARIANT nativeEndpointName;
properties->GetValue(PKEY_Device_DeviceDesc, &nativeEndpointName);
if (!nativeCombinedName.pwszVal) {
continue;
}
out[id] = AudioDeviceInfo{
id,
WCharPtrToString(nativeInterfaceName.pwszVal),
WCharPtrToString(nativeEndpointName.pwszVal),
WCharPtrToString(nativeCombinedName.pwszVal),
direction,
GetAudioDeviceState(device)};
}
return out;
}
std::string GetDefaultAudioDeviceID(
AudioDeviceDirection direction,
AudioDeviceRole role) {
CComPtr<IMMDeviceEnumerator> de;
de.CoCreateInstance(__uuidof(MMDeviceEnumerator));
if (!de) {
//ESDDebug("Failed to create MMDeviceEnumerator");
return std::string();
}
CComPtr<IMMDevice> device;
de->GetDefaultAudioEndpoint(
AudioDeviceDirectionToEDataFlow(direction), AudioDeviceRoleToERole(role),
&device);
if (!device) {
//ESDDebug("No default audio device");
return std::string();
}
LPWSTR deviceID;
device->GetId(&deviceID);
if (!deviceID) {
//ESDDebug("No default audio device ID");
return std::string();
}
return WCharPtrToString(deviceID);
}
void SetDefaultAudioDeviceID(
AudioDeviceDirection direction,
AudioDeviceRole role,
const std::string& desiredID) {
if (desiredID == GetDefaultAudioDeviceID(direction, role)) {
return;
}
CComPtr<IPolicyConfigVista> pPolicyConfig;
pPolicyConfig.CoCreateInstance(__uuidof(CPolicyConfigVistaClient));
pPolicyConfig->SetDefaultEndpoint(
Utf8StrToWString(desiredID).c_str(), AudioDeviceRoleToERole(role));
}
bool IsAudioDeviceMuted(const std::string& deviceID) {
auto volume = DeviceIDToAudioEndpointVolume(deviceID);
if (!volume) {
return false;
}
BOOL ret;
volume->GetMute(&ret);
return ret;
}
void SetIsAudioDeviceMuted(const std::string& deviceID, MuteAction action) {
auto volume = DeviceIDToAudioEndpointVolume(deviceID);
if (!volume) {
return;
}
if (action == MuteAction::MUTE) {
volume->SetMute(true, nullptr);
} else if (action == MuteAction::UNMUTE) {
volume->SetMute(false, nullptr);
} else {
assert(action == MuteAction::TOGGLE);
BOOL muted;
volume->GetMute(&muted);
volume->SetMute(!muted, nullptr);
}
}
namespace {
class VolumeCallback : public IAudioEndpointVolumeCallback {
public:
VolumeCallback(std::function<void(bool isMuted)> cb) : mCB(cb), mRefs(1) {
}
virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ret)
override {
if (iid == IID_IUnknown || iid == __uuidof(IAudioEndpointVolumeCallback)) {
*ret = static_cast<IUnknown*>(this);
AddRef();
return S_OK;
}
*ret = nullptr;
return E_NOINTERFACE;
}
virtual ULONG __stdcall AddRef() override {
return InterlockedIncrement(&mRefs);
}
virtual ULONG __stdcall Release() override {
if (InterlockedDecrement(&mRefs) == 0) {
delete this;
}
return mRefs;
}
virtual HRESULT OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA pNotify) override {
mCB(pNotify->bMuted);
return S_OK;
}
private:
std::function<void(bool)> mCB;
long mRefs;
};
struct VolumeCallbackHandle {
std::string deviceID;
CComPtr<VolumeCallback> impl;
CComPtr<IAudioEndpointVolume> dev;
VolumeCallbackHandle(
const std::string& deviceID,
CComPtr<VolumeCallback> impl,
CComPtr<IAudioEndpointVolume> dev) {
this->deviceID = deviceID;
this->impl = impl;
this->dev = dev;
}
VolumeCallbackHandle(const VolumeCallback& copied) = delete;
~VolumeCallbackHandle() {
dev->UnregisterControlChangeNotify(impl);
}
};
}// namespace
AUDIO_DEVICE_MUTE_CALLBACK_HANDLE
AddAudioDeviceMuteUnmuteCallback(
const std::string& deviceID,
std::function<void(bool isMuted)> cb) {
auto dev = DeviceIDToAudioEndpointVolume(deviceID);
if (!dev) {
return nullptr;
}
auto impl = new VolumeCallback(cb);
auto ret = dev->RegisterControlChangeNotify(impl);
if (ret != S_OK) {
delete impl;
return nullptr;
}
return new VolumeCallbackHandle(deviceID, impl, dev);
}
void RemoveAudioDeviceMuteUnmuteCallback(
AUDIO_DEVICE_MUTE_CALLBACK_HANDLE _handle) {
if (!_handle) {
return;
}
const auto handle = reinterpret_cast<VolumeCallbackHandle*>(_handle);
delete handle;
return;
}
namespace {
typedef std::function<
void(AudioDeviceDirection, AudioDeviceRole, const std::string&)>
DefaultChangeCallbackFun;
class DefaultChangeCallback : public IMMNotificationClient {
public:
DefaultChangeCallback(DefaultChangeCallbackFun cb) : mCB(cb), mRefs(1) {
}
virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ret)
override {
if (iid == IID_IUnknown || iid == __uuidof(IMMNotificationClient)) {
*ret = static_cast<IUnknown*>(this);
AddRef();
return S_OK;
}
*ret = nullptr;
return E_NOINTERFACE;
}
virtual ULONG __stdcall AddRef() override {
return InterlockedIncrement(&mRefs);
}
virtual ULONG __stdcall Release() override {
if (InterlockedDecrement(&mRefs) == 0) {
delete this;
}
return mRefs;
}
virtual HRESULT OnDefaultDeviceChanged(
EDataFlow flow,
ERole winAudioDeviceRole,
LPCWSTR defaultDeviceID) override {
AudioDeviceRole role;
switch (winAudioDeviceRole) {
case ERole::eMultimedia:
return S_OK;
case ERole::eCommunications:
role = AudioDeviceRole::COMMUNICATION;
break;
case ERole::eConsole:
role = AudioDeviceRole::DEFAULT;
break;
}
const AudioDeviceDirection direction = (flow == EDataFlow::eCapture)
? AudioDeviceDirection::INPUT
: AudioDeviceDirection::OUTPUT;
mCB(direction, role, WCharPtrToString(defaultDeviceID));
return S_OK;
};
virtual HRESULT OnDeviceAdded(LPCWSTR pwstrDeviceId) override {
return S_OK;
};
virtual HRESULT OnDeviceRemoved(LPCWSTR pwstrDeviceId) override {
return S_OK;
};
virtual HRESULT OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState)
override {
return S_OK;
};
virtual HRESULT OnPropertyValueChanged(
LPCWSTR pwstrDeviceId,
const PROPERTYKEY key) override {
return S_OK;
};
private:
DefaultChangeCallbackFun mCB;
long mRefs;
};
struct DefaultChangeCallbackHandle {
CComPtr<DefaultChangeCallback> impl;
CComPtr<IMMDeviceEnumerator> enumerator;
DefaultChangeCallbackHandle(
CComPtr<DefaultChangeCallback> impl,
CComPtr<IMMDeviceEnumerator> enumerator) {
this->impl = impl;
this->enumerator = enumerator;
}
DefaultChangeCallbackHandle(const DefaultChangeCallbackHandle& copied)
= delete;
~DefaultChangeCallbackHandle() {
//ESDDebug("unregistering native callback");
enumerator->UnregisterEndpointNotificationCallback(this->impl);
}
};
}// namespace
DEFAULT_AUDIO_DEVICE_CHANGE_CALLBACK_HANDLE
AddDefaultAudioDeviceChangeCallback(DefaultChangeCallbackFun cb) {
CComPtr<IMMDeviceEnumerator> de;
de.CoCreateInstance(__uuidof(MMDeviceEnumerator));
if (!de) {
//ESDDebug("failed to get enumerator");
return nullptr;
}
CComPtr<DefaultChangeCallback> impl(new DefaultChangeCallback(cb));
if (de->RegisterEndpointNotificationCallback(impl) != S_OK) {
//ESDDebug("failed to register callback");
return nullptr;
}
//ESDDebug("returning new callback handle");
return new DefaultChangeCallbackHandle(impl, de);
}
void RemoveDefaultAudioDeviceChangeCallback(
DEFAULT_AUDIO_DEVICE_CHANGE_CALLBACK_HANDLE _handle) {
if (!_handle) {
return;
}
//ESDDebug("RemoveDefaultAudioDeviceChangeCallback called");
const auto handle = reinterpret_cast<DefaultChangeCallbackHandle*>(_handle);
delete handle;
return;
}
#ifdef HAVE_FEEDBACK_SOUNDS
namespace {
std::wstring sMuteWav;
std::wstring sUnmuteWav;
void initWavPaths() {
if (!sMuteWav.empty()) {
return;
}
std::wstring path;
path.resize(1024);
const auto len = GetModuleFileName(NULL, path.data(), 1024);
assert(len);
path.resize(len);
auto idx = path.find_last_of(L'\\');
const auto dir = path.substr(0, idx + 1);
auto utf8 = WCharPtrToString(dir.c_str());
ESDDebug("got executable dir: '%s', %d", utf8.c_str(), idx);
sMuteWav = dir + L"mute.wav";
sUnmuteWav = dir + L"unmute.wav";
}
std::wstring muteWavPath() {
initWavPaths();
return sMuteWav;
}
std::wstring unmuteWavPath() {
initWavPaths();
return sUnmuteWav;
}
}// namespace
void PlayFeedbackSound(MuteAction action) {
assert(action != MuteAction::TOGGLE);
const auto feedbackWav
= (action == MuteAction::MUTE) ? muteWavPath() : unmuteWavPath();
const auto utf8 = WCharPtrToString(feedbackWav.c_str());
ESDDebug("Playing feedback sound '%s'", utf8.c_str());
PlaySound(feedbackWav.c_str(), NULL, SND_ASYNC | SND_NODEFAULT);
}
#endif