-
-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathsndDevicesDeviceCallbacks.cpp
More file actions
341 lines (274 loc) · 10.7 KB
/
Copy pathsndDevicesDeviceCallbacks.cpp
File metadata and controls
341 lines (274 loc) · 10.7 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
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
/*
FxSound
Copyright (C) 2025 FxSound LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "codedefs.h"
/* Standard includes */
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <mmreg.h>
#include <Mmdeviceapi.h>
#include <Audioclient.h>
#include <Functiondiscoverykeys_devpkey.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <string>
#include "slout.h"
#include "mry.h"
#include "u_sndDevices.h"
#include "sndDevices.h"
// This is for registering the device callbacks. For some reason the callbacks would crash
// when this was an object in the handle, perhaps because it was allocated?
//CsndDevicesMMNotificationClient g_DeviceEvents;
CsndDevicesMMNotificationClient::CsndDevicesMMNotificationClient() :_cRef(1),_pEnumerator(NULL)
{
}
CsndDevicesMMNotificationClient::~CsndDevicesMMNotificationClient()
{
// For some reason even though _pEnumerator comes in here non-null, this crashes when called in windevcon.
//SAFE_RELEASE(_pEnumerator)
}
// IUnknown methods -- AddRef, Release, and QueryInterface
ULONG STDMETHODCALLTYPE CsndDevicesMMNotificationClient::AddRef()
{
return InterlockedIncrement(&_cRef);
}
ULONG STDMETHODCALLTYPE CsndDevicesMMNotificationClient::Release()
{
ULONG ulRef = InterlockedDecrement(&_cRef);
if (0 == ulRef)
{
delete this;
}
return ulRef;
}
HRESULT STDMETHODCALLTYPE CsndDevicesMMNotificationClient::QueryInterface(
REFIID riid, VOID **ppvInterface)
{
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 CsndDevicesMMNotificationClient::OnDefaultDeviceChanged(
EDataFlow flow, ERole role,
LPCWSTR pwstrDeviceId)
{
struct sndDevicesHdlType *cast_handle;
cast_handle = (struct sndDevicesHdlType *)g_sndDevicesCallbacks_hdl;
if (cast_handle == NULL)
return(S_OK);
if (cast_handle->dfxDeviceNum == SND_DEVICES_DEVICE_NOT_PRESENT)
return(S_OK);
SLOUT_FIRST_LINE(L"CsndDevicesMMNotificationClient::OnDefaultDeviceChanged() enters");
if( (flow == eRender) && (role == eMultimedia) && (cast_handle->ignoreDeviceCallbacks == FALSE) )
{
// If one of the DFX capture devices was set as the default and the default device has been changed, force a re-initialization.
// Note- setting the flag only if a DFX device was the default is filtering out callbacks we want made.
//if( (cast_handle->dfxDeviceNum == cast_handle->defaultDeviceNum) || (cast_handle->dfx48DeviceNum == cast_handle->defaultDeviceNum) )
SLOUT_FIRST_LINE(L"CsndDevicesMMNotificationClient::OnDefaultDeviceChanged() setting processing thread kill flag");
// Don't think we need to kill process thread if DFX itself is selected as default from within our code
if (wcscmp(pwstrDeviceId, cast_handle->pwszID[cast_handle->dfxDeviceNum]) == 0)
return(S_OK);
//std::wstring temp_string(pwstrDeviceId);
/* Set the newly targeted playback device as the default. It will then automatically become the targeted device */
//if (sndDevicesSetDeviceType(g_sndDevicesCallbacks_hdl, SND_DEVICES_DEFAULT, &temp_string[0], &i_resultFlag) != OKAY)
// return(NOT_OKAY);
cast_handle->stopAudioCaptureAndPlaybackLoop = 1;
}
cast_handle->defaultDeviceNum = SND_DEVICES_DEVICE_NOT_PRESENT;
for (int i = 0; i < cast_handle->totalNumDevices; i++)
{
if (wcscmp(cast_handle->pwszID[i], pwstrDeviceId) == 0)
{
cast_handle->defaultDeviceNum = i;
break;
}
}
if (cast_handle->deviceChangeCallback != NULL)
cast_handle->deviceChangeCallback();
return S_OK;
}
HRESULT STDMETHODCALLTYPE CsndDevicesMMNotificationClient::OnDeviceAdded(LPCWSTR pwstrDeviceId)
{
struct sndDevicesHdlType *cast_handle;
cast_handle = (struct sndDevicesHdlType *)g_sndDevicesCallbacks_hdl;
if (cast_handle == NULL)
return(S_OK);
SLOUT_FIRST_LINE(L"CsndDevicesMMNotificationClient::OnDeviceAdded() enters");
// These callbacks only appear to get hit when a device is added that didn't already have a driver installed.
if( cast_handle->ignoreDeviceCallbacks == FALSE )
{
SLOUT_FIRST_LINE(L"CsndDevicesMMNotificationClient::OnDeviceAdded() setting processing thread kill flag");
cast_handle->stopAudioCaptureAndPlaybackLoop = 1;
}
if (cast_handle->deviceChangeCallback != NULL)
cast_handle->deviceChangeCallback();
return S_OK;
};
HRESULT STDMETHODCALLTYPE CsndDevicesMMNotificationClient::OnDeviceRemoved(LPCWSTR pwstrDeviceId)
{
struct sndDevicesHdlType *cast_handle;
cast_handle = (struct sndDevicesHdlType *)g_sndDevicesCallbacks_hdl;
if (cast_handle == NULL)
return(S_OK);
SLOUT_FIRST_LINE(L"CsndDevicesMMNotificationClient::OnDeviceRemoved() enters");
// These callbacks only appear to get hit when a device is added that didn't already have a driver installed.
if( cast_handle->ignoreDeviceCallbacks == FALSE )
{
SLOUT_FIRST_LINE(L"CsndDevicesMMNotificationClient::OnDeviceRemoved() setting processing thread kill flag");
cast_handle->stopAudioCaptureAndPlaybackLoop = 1;
}
if (cast_handle->deviceChangeCallback != NULL)
cast_handle->deviceChangeCallback();
return S_OK;
}
HRESULT STDMETHODCALLTYPE CsndDevicesMMNotificationClient::OnDeviceStateChanged(
LPCWSTR pwstrDeviceId,
DWORD dwNewState)
{
struct sndDevicesHdlType *cast_handle;
int type;
IMMDevice *pDevice = NULL;
IMMDeviceEnumerator *pEnumerator = NULL;
IMMEndpoint *pEndpoint = NULL;
EDataFlow flow;
HRESULT hr;
cast_handle = (struct sndDevicesHdlType *)g_sndDevicesCallbacks_hdl;
if (cast_handle == NULL)
return(S_OK);
// Only react to render (playback) device changes, not capture (input) devices
if (pwstrDeviceId != NULL)
{
hr = CoCreateInstance(cast_handle->CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, cast_handle->IID_IMMDeviceEnumerator, (void**)&pEnumerator);
if (SUCCEEDED(hr) && pEnumerator != NULL)
{
hr = pEnumerator->GetDevice(pwstrDeviceId, &pDevice);
if (SUCCEEDED(hr) && pDevice != NULL)
{
hr = pDevice->QueryInterface(__uuidof(IMMEndpoint), (void**)&pEndpoint);
if (SUCCEEDED(hr) && pEndpoint != NULL)
{
hr = pEndpoint->GetDataFlow(&flow);
if (pEndpoint) pEndpoint->Release();
if (SUCCEEDED(hr) && flow != eRender)
{
if (pDevice) pDevice->Release();
if (pEnumerator) pEnumerator->Release();
return S_OK;
}
}
if (pDevice) pDevice->Release();
}
if (pEnumerator) pEnumerator->Release();
}
}
switch (dwNewState)
{
case DEVICE_STATE_ACTIVE:
type = dwNewState; // When connecting device, this gets hit around 10 times.
break;
case DEVICE_STATE_DISABLED:
type = dwNewState;
break;
case DEVICE_STATE_NOTPRESENT:
type = dwNewState; // When disconnecting device, this gets hit around 12 times.
break;
case DEVICE_STATE_UNPLUGGED:
type = dwNewState;
break;
}
if( pwstrDeviceId != NULL )
{
// Track reconnected device: when state changes to ACTIVE from a non-active state,
// this is a device reconnection (e.g., Bluetooth headset connects after being paired/disconnected)
if (dwNewState == DEVICE_STATE_ACTIVE)
{
wcscpy(cast_handle->reconnectedDeviceGuid, pwstrDeviceId);
cast_handle->hasReconnectedDevice = TRUE;
}
// Ignore identical callbacks from the same guid (prevents callback storms)
if( (wcscmp(pwstrDeviceId, cast_handle->lastDeviceAddCallbackGuid) == 0) && ( dwNewState == cast_handle->lastDeviceAddCallbackGuidtype ) )
{
return(S_OK);
}
}
if( pwstrDeviceId == NULL )
wcscpy(cast_handle->lastDeviceAddCallbackGuid, L"");
else
wcscpy(cast_handle->lastDeviceAddCallbackGuid, pwstrDeviceId );
cast_handle->lastDeviceAddCallbackGuidtype = dwNewState;
if( cast_handle->ignoreDeviceCallbacks == FALSE )
{
SLOUT_FIRST_LINE(L"CsndDevicesMMNotificationClient::OnDeviceStateChanged() setting processing thread kill flag");
cast_handle->stopAudioCaptureAndPlaybackLoop = 1;
}
if (cast_handle->deviceChangeCallback != NULL)
cast_handle->deviceChangeCallback();
return S_OK;
}
// NOTE - On Vista/Win7 this callback would only get hit when a legitmate "property" of a device changed,
// such as samp freq, num channels. On Win8 this callback is getting hit when the mute setting is changed.
// The thread stop set has been disabled to avoid Win8 mute problems as any change of these properties should
// cause the capture thread to exit with an error condition.
HRESULT STDMETHODCALLTYPE CsndDevicesMMNotificationClient::OnPropertyValueChanged(
LPCWSTR pwstrDeviceId,
const PROPERTYKEY key)
{
struct sndDevicesHdlType *cast_handle;
cast_handle = (struct sndDevicesHdlType *)g_sndDevicesCallbacks_hdl;
if (cast_handle == NULL)
return(S_OK);
/*
struct sndDevicesHdlType *cast_handle;
wchar_t guidID[PT_MAX_GENERIC_STRLEN];
int deviceNum;
cast_handle = (struct sndDevicesHdlType *)g_sndDevicesCallbacks_hdl;
if (cast_handle == NULL)
return(S_OK);
// Only force a reset if a property value changed on the playback or capture devices.
if( cast_handle->ignoreDeviceCallbacks == FALSE )
{
if( pwstrDeviceId != NULL ) // Sometimes this comes in with a null value.
{
wcscpy(guidID, pwstrDeviceId);
if( sndDevices_UtilsGetIndexFromID(g_sndDevicesCallbacks_hdl, guidID, &deviceNum) == OKAY )
if( (deviceNum == cast_handle->captureDeviceNum) || (deviceNum == cast_handle->playbackDeviceNum) )
cast_handle->stopAudioCaptureAndPlaybackLoop = 1;
}
}
*/
//SLOUT_FIRST_LINE(L"CsndDevicesMMNotificationClient::OnPropertyValueChanged() enters");
return S_OK;
}
// Function to be called from sndDevicesInit to pass in allocated handle for by methods in this class.
void CsndDevicesMMNotificationClient::SetPtHandle(PT_HANDLE *sndDevices_hdl)
{
g_sndDevicesCallbacks_hdl = sndDevices_hdl;
}