-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_monitor.cpp
More file actions
283 lines (234 loc) · 8.52 KB
/
audio_monitor.cpp
File metadata and controls
283 lines (234 loc) · 8.52 KB
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
#include "audio_monitor.h"
#include "logger.h"
#include <Propvarutil.h>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "propsys.lib")
AudioMonitor::AudioMonitor() = default;
AudioMonitor::~AudioMonitor() {
stop();
}
bool AudioMonitor::start() {
HRESULT hr = CoCreateInstance(
__uuidof(MMDeviceEnumerator),
nullptr,
CLSCTX_ALL,
__uuidof(IMMDeviceEnumerator),
reinterpret_cast<void**>(&m_enumerator));
if (FAILED(hr)) {
LOG_ERROR(L"Failed to create MMDeviceEnumerator");
return false;
}
hr = m_enumerator->RegisterEndpointNotificationCallback(this);
if (FAILED(hr)) {
LOG_ERROR(L"Failed to register audio notification callback");
m_enumerator->Release();
m_enumerator = nullptr;
return false;
}
m_started = true;
return true;
}
void AudioMonitor::stop() {
if (m_enumerator) {
if (m_started) {
m_enumerator->UnregisterEndpointNotificationCallback(this);
}
m_enumerator->Release();
m_enumerator = nullptr;
}
m_started = false;
}
std::wstring AudioMonitor::getDeviceFriendlyName(IMMDevice* device) {
IPropertyStore* props = nullptr;
HRESULT hr = device->OpenPropertyStore(STGM_READ, &props);
if (FAILED(hr)) return L"Unknown Device";
PROPVARIANT varName;
PropVariantInit(&varName);
hr = props->GetValue(PKEY_Device_FriendlyName, &varName);
std::wstring name = L"Unknown Device";
if (SUCCEEDED(hr) && varName.vt == VT_LPWSTR) {
name = varName.pwszVal;
}
PropVariantClear(&varName);
props->Release();
return name;
}
std::wstring AudioMonitor::deviceStateToString(DWORD state) {
switch (state) {
case DEVICE_STATE_ACTIVE: return L"Active";
case DEVICE_STATE_DISABLED: return L"Disabled";
case DEVICE_STATE_NOTPRESENT: return L"Not Present";
case DEVICE_STATE_UNPLUGGED: return L"Unplugged";
default: return L"Unknown";
}
}
std::wstring AudioMonitor::dataFlowToString(EDataFlow flow) {
switch (flow) {
case eRender: return L"[OUT]";
case eCapture: return L"[IN] ";
default: return L"[?] ";
}
}
AudioDeviceInfo AudioMonitor::getDeviceInfo(LPCWSTR deviceId, EDataFlow flow) {
AudioDeviceInfo info;
info.id = deviceId;
info.isOutput = (flow == eRender);
if (!m_enumerator) return info;
IMMDevice* device = nullptr;
HRESULT hr = m_enumerator->GetDevice(deviceId, &device);
if (SUCCEEDED(hr) && device) {
info.name = getDeviceFriendlyName(device);
DWORD state = 0;
device->GetState(&state);
info.isActive = (state == DEVICE_STATE_ACTIVE);
device->Release();
}
return info;
}
void AudioMonitor::listCurrentDevices() {
if (!m_enumerator) return;
LOG_SECTION(L"AUDIO DEVICES");
// List output devices (speakers, headphones)
IMMDeviceCollection* renderDevices = nullptr;
HRESULT hr = m_enumerator->EnumAudioEndpoints(eRender, DEVICE_STATEMASK_ALL, &renderDevices);
if (SUCCEEDED(hr) && renderDevices) {
UINT count = 0;
renderDevices->GetCount(&count);
// Get default device
IMMDevice* defaultDevice = nullptr;
std::wstring defaultId;
if (SUCCEEDED(m_enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice)) && defaultDevice) {
LPWSTR id = nullptr;
if (SUCCEEDED(defaultDevice->GetId(&id))) {
defaultId = id;
CoTaskMemFree(id);
}
defaultDevice->Release();
}
for (UINT i = 0; i < count; i++) {
IMMDevice* device = nullptr;
if (SUCCEEDED(renderDevices->Item(i, &device)) && device) {
AudioDeviceInfo info;
info.isOutput = true;
info.name = getDeviceFriendlyName(device);
DWORD state = 0;
device->GetState(&state);
info.isActive = (state == DEVICE_STATE_ACTIVE);
LPWSTR id = nullptr;
if (SUCCEEDED(device->GetId(&id))) {
info.id = id;
info.isDefault = (info.id == defaultId);
CoTaskMemFree(id);
}
LOG_AUDIO(info.getDisplayString());
device->Release();
}
}
renderDevices->Release();
}
// List input devices (microphones)
IMMDeviceCollection* captureDevices = nullptr;
hr = m_enumerator->EnumAudioEndpoints(eCapture, DEVICE_STATEMASK_ALL, &captureDevices);
if (SUCCEEDED(hr) && captureDevices) {
UINT count = 0;
captureDevices->GetCount(&count);
// Get default capture device
IMMDevice* defaultDevice = nullptr;
std::wstring defaultId;
if (SUCCEEDED(m_enumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &defaultDevice)) && defaultDevice) {
LPWSTR id = nullptr;
if (SUCCEEDED(defaultDevice->GetId(&id))) {
defaultId = id;
CoTaskMemFree(id);
}
defaultDevice->Release();
}
for (UINT i = 0; i < count; i++) {
IMMDevice* device = nullptr;
if (SUCCEEDED(captureDevices->Item(i, &device)) && device) {
AudioDeviceInfo info;
info.isOutput = false;
info.name = getDeviceFriendlyName(device);
DWORD state = 0;
device->GetState(&state);
info.isActive = (state == DEVICE_STATE_ACTIVE);
LPWSTR id = nullptr;
if (SUCCEEDED(device->GetId(&id))) {
info.id = id;
info.isDefault = (info.id == defaultId);
CoTaskMemFree(id);
}
LOG_AUDIO(info.getDisplayString());
device->Release();
}
}
captureDevices->Release();
}
}
// IUnknown implementation
ULONG STDMETHODCALLTYPE AudioMonitor::AddRef() {
return ++m_refCount;
}
ULONG STDMETHODCALLTYPE AudioMonitor::Release() {
LONG ref = --m_refCount;
if (ref == 0) {
delete this;
}
return ref;
}
HRESULT STDMETHODCALLTYPE AudioMonitor::QueryInterface(REFIID riid, void** ppvObject) {
if (riid == IID_IUnknown || riid == __uuidof(IMMNotificationClient)) {
*ppvObject = static_cast<IMMNotificationClient*>(this);
AddRef();
return S_OK;
}
*ppvObject = nullptr;
return E_NOINTERFACE;
}
// IMMNotificationClient implementation
HRESULT STDMETHODCALLTYPE AudioMonitor::OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) {
if (!m_enumerator) return S_OK;
IMMDevice* device = nullptr;
HRESULT hr = m_enumerator->GetDevice(pwstrDeviceId, &device);
if (SUCCEEDED(hr) && device) {
std::wstring name = getDeviceFriendlyName(device);
std::wstring state = deviceStateToString(dwNewState);
std::wstring message = name + L" -> " + state;
LOG_AUDIO_CHANGE(message);
device->Release();
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE AudioMonitor::OnDeviceAdded(LPCWSTR pwstrDeviceId) {
if (!m_enumerator) return S_OK;
IMMDevice* device = nullptr;
HRESULT hr = m_enumerator->GetDevice(pwstrDeviceId, &device);
if (SUCCEEDED(hr) && device) {
std::wstring name = getDeviceFriendlyName(device);
LOG_AUDIO_CHANGE(L"Device Added: " + name);
device->Release();
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE AudioMonitor::OnDeviceRemoved(LPCWSTR pwstrDeviceId) {
// Note: Device is already removed, so we can't get its name
LOG_AUDIO_CHANGE(L"Device Removed: " + std::wstring(pwstrDeviceId));
return S_OK;
}
HRESULT STDMETHODCALLTYPE AudioMonitor::OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDefaultDeviceId) {
if (role != eConsole) return S_OK; // Only care about console role
if (!m_enumerator || !pwstrDefaultDeviceId) return S_OK;
IMMDevice* device = nullptr;
HRESULT hr = m_enumerator->GetDevice(pwstrDefaultDeviceId, &device);
if (SUCCEEDED(hr) && device) {
std::wstring name = getDeviceFriendlyName(device);
std::wstring flowStr = dataFlowToString(flow);
LOG_AUDIO_CHANGE(L"Default " + flowStr + L" changed to: " + name);
device->Release();
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE AudioMonitor::OnPropertyValueChanged(LPCWSTR /*pwstrDeviceId*/, const PROPERTYKEY /*key*/) {
// We don't log property changes to reduce noise
return S_OK;
}