Skip to content

Commit ae98311

Browse files
committed
win-wasapi: Store in source settings 'default_device_id'
This enables the new setting 'default_device_id' which is useful for audio monitoring deduplication. Currently only 'device_id' is stored; but no information about the real device is known if 'device_id' is 'default'. The 'default_device_id' is stored during creation of the source and is updated whenever the user changes the default audio output device in windows audio settings. Signed-off-by: pkv <[email protected]>
1 parent a2f7228 commit ae98311

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

plugins/win-wasapi/enum-wasapi.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,40 @@ void GetWASAPIAudioDevices(vector<AudioDeviceInfo> &devices, bool input)
9090
blog(LOG_WARNING, "[GetWASAPIAudioDevices] %s: %lX", error.str, error.hr);
9191
}
9292
}
93+
94+
bool GetDefaultOuputDeviceId(char **out_id)
95+
{
96+
HRESULT hr;
97+
ComPtr<IMMDeviceEnumerator> enumerator;
98+
IMMDevice *device = NULL;
99+
LPWSTR w_id = NULL;
100+
bool success = false;
101+
102+
*out_id = NULL;
103+
104+
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
105+
(void **)enumerator.Assign());
106+
if (FAILED(hr))
107+
return false;
108+
109+
hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
110+
if (FAILED(hr))
111+
goto fail;
112+
113+
hr = device->GetId(&w_id);
114+
if (FAILED(hr))
115+
goto fail;
116+
117+
os_wcs_to_utf8_ptr((const wchar_t *)w_id, 0, out_id);
118+
119+
success = (*out_id != NULL);
120+
121+
fail:
122+
if (w_id)
123+
CoTaskMemFree(w_id);
124+
if (device)
125+
device->Release();
126+
if (enumerator)
127+
enumerator->Release();
128+
return success;
129+
}

plugins/win-wasapi/enum-wasapi.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ struct AudioDeviceInfo {
3939

4040
std::string GetDeviceName(IMMDevice *device);
4141
void GetWASAPIAudioDevices(std::vector<AudioDeviceInfo> &devices, bool input);
42+
bool GetDefaultOuputDeviceId(char **out_id);

plugins/win-wasapi/win-wasapi.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ using namespace std;
2828
#define OPT_USE_DEVICE_TIMING "use_device_timing"
2929
#define OPT_WINDOW "window"
3030
#define OPT_PRIORITY "priority"
31+
#define OPT_DEFAULT_DEVICE_ID "default_device_id"
3132

3233
WASAPINotify *GetNotify();
3334
static void GetWASAPIDefaults(obs_data_t *settings);
@@ -304,6 +305,15 @@ WASAPISource::WASAPISource(obs_data_t *settings, obs_source_t *source_, SourceTy
304305
(PFN_ActivateAudioInterfaceAsync)GetProcAddress(mmdevapi_module, "ActivateAudioInterfaceAsync");
305306
}
306307

308+
char *default_id = NULL;
309+
if (GetDefaultOuputDeviceId(&default_id)) {
310+
blog(LOG_DEBUG, "[WASAPI] Default output device ID resolved to: %s", default_id);
311+
obs_data_set_string(settings, OPT_DEFAULT_DEVICE_ID, default_id);
312+
bfree(default_id);
313+
} else {
314+
blog(LOG_WARNING, "[WASAPI] Failed to get default output device ID");
315+
}
316+
307317
UpdateSettings(BuildUpdateParams(settings));
308318
LogSettings();
309319

@@ -1197,10 +1207,23 @@ void WASAPISource::SetDefaultDevice(EDataFlow flow, ERole role, LPCWSTR id)
11971207
if (default_id.compare(id) == 0)
11981208
return;
11991209
default_id = id;
1210+
char *default_device_id = nullptr;
1211+
os_wcs_to_utf8_ptr((const wchar_t *)id, 0, &default_device_id);
1212+
obs_data_t *s = obs_source_get_settings(source);
1213+
if (s) {
1214+
obs_data_set_string(s, OPT_DEFAULT_DEVICE_ID, default_device_id);
1215+
obs_data_release(s);
1216+
}
1217+
bfree(default_device_id);
12001218
} else {
12011219
if (default_id.empty())
12021220
return;
12031221
default_id.clear();
1222+
obs_data_t *s = obs_source_get_settings(source);
1223+
if (s) {
1224+
obs_data_set_string(s, OPT_DEFAULT_DEVICE_ID, NULL);
1225+
obs_data_release(s);
1226+
}
12041227
}
12051228

12061229
blog(LOG_INFO, "WASAPI: Default %s device changed", input ? "input" : "output");

0 commit comments

Comments
 (0)