-
Notifications
You must be signed in to change notification settings - Fork 3
/
MicCtrl.cpp
256 lines (238 loc) · 7.88 KB
/
MicCtrl.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
#include "framework.h"
#include "MicCtrl.h"
#include <Audioclient.h>
#include <Audiopolicy.h>
#include <Functiondiscoverykeys_devpkey.h>
#include <string>
#include <vector>
#include <algorithm>
#include "Util.h"
static const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
static const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
static const IID IID_IAudioEndpointVolume = __uuidof(IAudioEndpointVolume);
static const IID IID_ISimpleAudioVolume = __uuidof(ISimpleAudioVolume);
//void test() {
// const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
// const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
// const IID IID_IAudioEndpointVolume = __uuidof(IAudioEndpointVolume);
// const IID IID_ISimpleAudioVolume = __uuidof(ISimpleAudioVolume);
// const IID IID_IAudioSessionManager = __uuidof(IAudioSessionManager);
//
// CComPtr<IMMDeviceEnumerator > m_pIMMEnumerator;
// CComPtr<ISimpleAudioVolume> m_pRenderSimpleVol;
//
// CComPtr<IMMDevice> pIMMDeivce = NULL;
// HRESULT hr = S_OK;
// hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER,
// IID_IMMDeviceEnumerator, (void**)&m_pIMMEnumerator);
// //Audio
// //hr = m_pIMMEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pIMMDeivce);
//
// CComPtr<IMMDeviceCollection> pDevCollection = NULL;
// hr = m_pIMMEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &pDevCollection);
// UINT devCnt = 0;
// hr = pDevCollection->GetCount(&devCnt);
// for (UINT i = 0; i < devCnt; i++) {
// CComPtr<IMMDevice> pDev = NULL;
// pDevCollection->Item(i, &pDev);
// CComPtr<IPropertyStore> pProp;
// hr = pDev->OpenPropertyStore(STGM_READ, &pProp);
// PROPVARIANT name;
// pProp->GetValue(PKEY_Device_FriendlyName, &name);
//
// OutputDebugString(name.pwszVal);
// OutputDebugString(L"\n");
//
// volumes.push_back(NULL);
// hr = pDev->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&volumes.back());
//
//
// callbacks.push_back(new CVolumeCallback(name.pwszVal));
// volumes.back()->RegisterControlChangeNotify(callbacks.back());
//
// }
// //Microphone
// hr = m_pIMMEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pIMMDeivce);
// CComPtr<IAudioEndpointVolume> volume;
// hr = pIMMDeivce->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&volume);
//
// BOOL bMute = FALSE;
// hr = volume->GetMute(&bMute);
// bMute = TRUE;
// hr = volume->SetMute(bMute, NULL);
//}
MicCtrl::MicCtrl() :notifClient(new MMNotificationClient()) {
VERIFY_OK(CoInitialize(NULL));
VERIFY_OK(CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER,
IID_IMMDeviceEnumerator, (void**)&devEnum));
VERIFY_OK(devEnum->RegisterEndpointNotificationCallback(notifClient));
ReloadDevices();
}
MicCtrl::~MicCtrl() {
UnregisterAudioCallbacks();
VERIFY_OK(devEnum->UnregisterEndpointNotificationCallback(notifClient));
notifClient->Release();
devEnum->Release();
}
void MicCtrl::UnregisterAudioCallbacks() {
std::for_each(audioCallbacks.begin(), audioCallbacks.end(),
[](auto& callback) {
VERIFY_OK(callback.first->UnregisterControlChangeNotify(callback.second));
callback.first->Release();
callback.second->Release();
});
}
// Reload microphone devices. Should be called when device changed.
void MicCtrl::ReloadDevices() {
UnregisterAudioCallbacks();
audioCallbacks.clear();
IMMDeviceCollection* devCollection = NULL;
VERIFY_OK(devEnum->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &devCollection));
UINT devCount = 0;
VERIFY_OK(devCollection->GetCount(&devCount));
for (UINT i = 0; i < devCount; i++) {
IMMDevice* dev = NULL;
VERIFY_OK(devCollection->Item(i, &dev));
IPropertyStore* propStore = NULL;
VERIFY_OK(dev->OpenPropertyStore(STGM_READ, &propStore));
PROPVARIANT name;
VERIFY_OK(propStore->GetValue(PKEY_Device_FriendlyName, &name));
propStore->Release();
IAudioEndpointVolume* volume = NULL;
VERIFY_OK(dev->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&volume));
AudioEndpointVolumeCallback* callback = new AudioEndpointVolumeCallback(name.pwszVal);
VERIFY_OK(volume->RegisterControlChangeNotify(callback));
audioCallbacks.push_back(AudioVolumeCallback(volume, callback));
dev->Release();
}
devCollection->Release();
}
bool MicCtrl::GetMuted() {
IMMDeviceCollection* devCollection = NULL;
VERIFY_OK(devEnum->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &devCollection));
UINT devCount = 0;
VERIFY_OK(devCollection->GetCount(&devCount));
BOOL bMute = TRUE;
for (UINT i = 0; i < devCount; i++) {
IMMDevice* dev = NULL;
VERIFY_OK(devCollection->Item(i, &dev));
LPWSTR id = NULL;
VERIFY_OK(dev->GetId(&id));
if (devFilter && !devFilter(id)) {
CoTaskMemFree(id);
dev->Release();
continue;
}
CoTaskMemFree(id);
IAudioEndpointVolume* volume = NULL;
VERIFY_OK(dev->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&volume));
dev->Release();
VERIFY_OK(volume->GetMute(&bMute));
volume->Release();
if (!bMute) {
break;
}
}
devCollection->Release();
return bMute;
}
void MicCtrl::SetMuted(bool mute) {
IMMDeviceCollection* devCollection = NULL;
VERIFY_OK(devEnum->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &devCollection));
UINT devCount = 0;
VERIFY_OK(devCollection->GetCount(&devCount));
for (UINT i = 0; i < devCount; i++) {
IMMDevice* dev = NULL;
VERIFY_OK(devCollection->Item(i, &dev));
LPWSTR id = NULL;
VERIFY_OK(dev->GetId(&id));
if (devFilter && !devFilter(id)) {
CoTaskMemFree(id);
dev->Release();;
continue;
}
CoTaskMemFree(id);
IAudioEndpointVolume* volume = NULL;
VERIFY_OK(dev->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&volume));
dev->Release();
VERIFY_SUCCEEDED(volume->SetMute(mute, NULL));
volume->Release();
}
devCollection->Release();
}
std::vector<std::wstring> MicCtrl::GetActiveDevices() {
IMMDeviceCollection* devCollection = NULL;
VERIFY_OK(devEnum->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &devCollection));
UINT devCount = 0;
VERIFY_OK(devCollection->GetCount(&devCount));
std::vector<std::wstring> ids;
for (UINT i = 0; i < devCount; i++) {
IMMDevice* dev = NULL;
VERIFY_OK(devCollection->Item(i, &dev));
LPWSTR id = NULL;
VERIFY_OK(dev->GetId(&id));
ids.push_back(id);
CoTaskMemFree(id);
dev->Release();
}
devCollection->Release();
return ids;
}
std::wstring MicCtrl::GetDevName(const wchar_t* devID) {
IMMDevice* dev = NULL;
if (devEnum->GetDevice(devID, &dev) != S_OK) {
return L"";
}
IPropertyStore* propStore = NULL;
VERIFY_OK(dev->OpenPropertyStore(STGM_READ, &propStore));
dev->Release();
PROPVARIANT name;
VERIFY_OK(propStore->GetValue(PKEY_Device_FriendlyName, &name));
propStore->Release();
return name.pwszVal;
}
std::wstring MicCtrl::GetDevIfaceName(const wchar_t* devID) {
IMMDevice* dev = NULL;
if (devEnum->GetDevice(devID, &dev) != S_OK) {
return L"";
}
IPropertyStore* propStore = NULL;
VERIFY_OK(dev->OpenPropertyStore(STGM_READ, &propStore));
dev->Release();
PROPVARIANT name;
VERIFY_OK(propStore->GetValue(PKEY_Device_FriendlyName, &name));
propStore->Release();
return name.pwszVal;
}
int MicCtrl::GetDevMuted(const wchar_t* devID) {
IMMDevice* dev = NULL;
if (devEnum->GetDevice(devID, &dev) != S_OK) {
return -1;
}
DWORD state = 0;
VERIFY_OK(dev->GetState(&state));
if (state != DEVICE_STATE_ACTIVE) {
return -1;
}
IAudioEndpointVolume* volume = NULL;
VERIFY_OK(dev->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&volume));
dev->Release();
BOOL mute = FALSE;
VERIFY_OK(volume->GetMute(&mute));
volume->Release();
return mute ? 1 : 0;
}
std::wstring MicCtrl::GetDefaultMicphone() {
IMMDevice* dev = NULL;
auto ret = devEnum->GetDefaultAudioEndpoint(eCapture, eConsole, &dev);
if (ret == S_OK) {
LPWSTR devID = NULL;
VERIFY_OK(dev->GetId(&devID));
dev->Release();
std::wstring strDevID(devID);
CoTaskMemFree(devID);
return strDevID;
}
VERIFY(ret == E_NOTFOUND);
return L"";
}