From 15f9d37aefe3f5a99218b787f6f089a841ff6d2b Mon Sep 17 00:00:00 2001 From: jpark37 Date: Sat, 25 Sep 2021 11:51:14 -0700 Subject: [PATCH] win-wasapi: Persist objects beyond Start/Stop Initialize IMMNotificationClient and IMMDeviceEnumerator in constructor to simplify cleanup and initializaiton. Both can survive a reset. --- plugins/win-wasapi/win-wasapi.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/plugins/win-wasapi/win-wasapi.cpp b/plugins/win-wasapi/win-wasapi.cpp index c7303dc64..7cf703d4e 100644 --- a/plugins/win-wasapi/win-wasapi.cpp +++ b/plugins/win-wasapi/win-wasapi.cpp @@ -23,12 +23,12 @@ static void GetWASAPIDefaults(obs_data_t *settings); (KSAUDIO_SPEAKER_SURROUND | SPEAKER_LOW_FREQUENCY) class WASAPISource { + ComPtr notify; + ComPtr enumerator; ComPtr device; ComPtr client; ComPtr capture; ComPtr render; - ComPtr enumerator; - ComPtr notify; obs_source_t *source; wstring default_id; @@ -149,6 +149,20 @@ WASAPISource::WASAPISource(obs_data_t *settings, obs_source_t *source_, if (!receiveSignal.Valid()) throw "Could not create receive signal"; + notify = new WASAPINotify(this); + if (!notify) + throw "Could not create WASAPINotify"; + + HRESULT res = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, + CLSCTX_ALL, + IID_PPV_ARGS(enumerator.Assign())); + if (FAILED(res)) + throw HRError("Failed to create enumerator", res); + + res = enumerator->RegisterEndpointNotificationCallback(notify); + if (FAILED(res)) + throw HRError("Failed to register endpoint callback", res); + Start(); } @@ -365,23 +379,10 @@ void WASAPISource::InitCapture() void WASAPISource::Initialize() { - HRESULT res; - - res = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, - CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), - (void **)enumerator.Assign()); - if (FAILED(res)) - throw HRError("Failed to create enumerator", res); - InitDevice(); device_name = GetDeviceName(device); - if (!notify) { - notify = new WASAPINotify(this); - enumerator->RegisterEndpointNotificationCallback(notify); - } - HRESULT resSample; IPropertyStore *store = nullptr; PWAVEFORMATEX deviceFormatProperties;