浏览代码

win-wasapi: Persist objects beyond Start/Stop

Initialize IMMNotificationClient and IMMDeviceEnumerator in constructor
to simplify cleanup and initializaiton. Both can survive a reset.
jpark37 4 年之前
父节点
当前提交
15f9d37aef
共有 1 个文件被更改,包括 16 次插入15 次删除
  1. 16 15
      plugins/win-wasapi/win-wasapi.cpp

+ 16 - 15
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<IMMNotificationClient> notify;
+	ComPtr<IMMDeviceEnumerator> enumerator;
 	ComPtr<IMMDevice> device;
 	ComPtr<IAudioClient> client;
 	ComPtr<IAudioCaptureClient> capture;
 	ComPtr<IAudioRenderClient> render;
-	ComPtr<IMMDeviceEnumerator> enumerator;
-	ComPtr<IMMNotificationClient> 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;