@@ -66,46 +66,25 @@ void FSystemMicLiteManager::DestroyInstance()
6666 }
6767}
6868
69- FString FSystemMicLiteManager::GetDefaultDeviceName ()
69+ FString FSystemMicLiteManager::GetDefaultDeviceName () const
7070{
71- return GetDeviceNameFromId (GetDefaultDeviceId ());
71+ #if PLATFORM_WINDOWS
72+ return GetDeviceName (GetDevice ());
73+ #else
74+ return FString (TEXT (" " ));
75+ #endif
7276}
7377
74- FString FSystemMicLiteManager::GetDefaultDeviceId ()
78+ FString FSystemMicLiteManager::GetDefaultDeviceId () const
7579{
76- FString DeviceIdStr;
77-
7880#if PLATFORM_WINDOWS
79- TComPtr<IMMDevice> Device = GetDevice ();
80- if (!Device.IsValid ())
81- {
82- return FString (TEXT (" " ));
83- }
84-
85- // https://msdn.microsoft.com/en-us/library/windows/desktop/dd371405(v=vs.85).aspx, see Return value!
86- TComPtr<IAudioEndpointVolume> AudioEndpointVolume = GetAudioEndpointVolume (Device);
87- if (!AudioEndpointVolume.IsValid ())
88- {
89- return FString (TEXT (" " ));
90- }
91-
92- WCHAR* swDeviceId = TCHAR_TO_WCHAR (TEXT (" " ));
93- HRESULT Result = Device->GetId (&swDeviceId);
94- if (Result != S_OK)
95- {
96- UE_LOG (LogSystemMicLiteManager, Warning, TEXT (" Result != S_OK, Device->GetId, [%s], line: %d" ), ANSI_TO_TCHAR (__FUNCTION__), __LINE__);
97- return FString (TEXT (" " ));
98- }
99- DeviceIdStr = FString (WCHAR_TO_TCHAR (swDeviceId));
100-
101- CoTaskMemFree (swDeviceId);
102- swDeviceId = nullptr ;
81+ return GetDeviceId (GetDevice ());
82+ #else
83+ return FString (TEXT (" " ));
10384#endif
104-
105- return DeviceIdStr;
10685}
10786
108- FString FSystemMicLiteManager::GetDeviceIdFromName (const FString& DeviceName)
87+ FString FSystemMicLiteManager::GetDeviceIdFromName (const FString& DeviceName) const
10988{
11089 FString DeviceId;
11190
@@ -121,7 +100,7 @@ FString FSystemMicLiteManager::GetDeviceIdFromName(const FString& DeviceName)
121100 return DeviceId;
122101}
123102
124- FString FSystemMicLiteManager::GetDeviceNameFromId (const FString& DeviceId)
103+ FString FSystemMicLiteManager::GetDeviceNameFromId (const FString& DeviceId) const
125104{
126105 FString DeviceName;
127106
@@ -137,7 +116,7 @@ FString FSystemMicLiteManager::GetDeviceNameFromId(const FString& DeviceId)
137116 return DeviceName;
138117}
139118
140- TMap<FString, FString> FSystemMicLiteManager::GetActiveDevices ()
119+ TMap<FString, FString> FSystemMicLiteManager::GetActiveDevices () const
141120{
142121 TMap<FString, FString> ActiveDevices;
143122
@@ -162,24 +141,12 @@ TMap<FString, FString> FSystemMicLiteManager::GetActiveDevices()
162141 for (UINT i = 0 ; i < CountActiveDevices; i++)
163142 {
164143 TComPtr<IMMDevice> Device;
165- TComPtr<IPropertyStore> PropertyStore;
166- LPWSTR pwszID = nullptr ;
167-
168144 DevicesCollection->Item (i, &Device);
169- Device->GetId (&pwszID);
170- Device->OpenPropertyStore (STGM_READ, &PropertyStore);
171-
172- PROPVARIANT nameDevice;
173- PropVariantInit (&nameDevice);
174- PropertyStore->GetValue (PKEY_Device_FriendlyName, &nameDevice);
175145
176- ActiveDevices. Add ( FString ( WCHAR_TO_TCHAR (pwszID)) );
177- ActiveDevices[ FString ( WCHAR_TO_TCHAR (pwszID))] = FString ( WCHAR_TO_TCHAR (nameDevice. pwszVal ) );
146+ const FString DeviceId = GetDeviceId (Device );
147+ const FString DeviceName = GetDeviceName (Device );
178148
179- PropVariantClear (&nameDevice);
180-
181- CoTaskMemFree (pwszID);
182- pwszID = nullptr ;
149+ ActiveDevices.Add (DeviceId, DeviceName);
183150 }
184151#endif
185152
@@ -212,7 +179,7 @@ void FSystemMicLiteManager::SetVolume(float Value, const FString& DeviceId)
212179#endif
213180}
214181
215- float FSystemMicLiteManager::GetVolume (const FString& DeviceId)
182+ float FSystemMicLiteManager::GetVolume (const FString& DeviceId) const
216183{
217184 float MicVolume = 0 .0f ;
218185
@@ -234,18 +201,18 @@ float FSystemMicLiteManager::GetVolume(const FString& DeviceId)
234201 return GetValueFromScalar (MicVolume);
235202}
236203
237- float FSystemMicLiteManager::GetScalarFromValue (int32 Value)
204+ float FSystemMicLiteManager::GetScalarFromValue (int32 Value) const
238205{
239206 return FMath::Abs (Value) >= 100 .0f ? 1 .0f : Value / 100 .0f ;
240207}
241208
242- float FSystemMicLiteManager::GetValueFromScalar (float Value)
209+ float FSystemMicLiteManager::GetValueFromScalar (float Value) const
243210{
244211 return FMath::RoundToFloat (FMath::Abs (Value) > 0 .0f ? Value * 100 .0f : 0 .0f );
245212}
246213
247214#if PLATFORM_WINDOWS
248- TComPtr<IMMDevice> FSystemMicLiteManager::GetDevice (const FString& DeviceId)
215+ TComPtr<IMMDevice> FSystemMicLiteManager::GetDevice (const FString& DeviceId) const
249216{
250217 TComPtr<IMMDevice> Device;
251218 HRESULT Result = S_OK;
@@ -272,7 +239,51 @@ TComPtr<IMMDevice> FSystemMicLiteManager::GetDevice(const FString& DeviceId)
272239 return Device;
273240}
274241
275- TComPtr<IAudioEndpointVolume> FSystemMicLiteManager::GetAudioEndpointVolume (const TComPtr<IMMDevice>& Device)
242+ FString FSystemMicLiteManager::GetDeviceName (const TComPtr<IMMDevice>& Device) const
243+ {
244+ if (!Device.IsValid ())
245+ {
246+ return FString (TEXT (" " ));
247+ }
248+
249+ TComPtr<IPropertyStore> PropertyStore;
250+ Device->OpenPropertyStore (STGM_READ, &PropertyStore);
251+
252+ PROPVARIANT nameDevice;
253+ PropVariantInit (&nameDevice);
254+ PropertyStore->GetValue (PKEY_Device_FriendlyName, &nameDevice);
255+
256+ const FString DeviceName = FString (WCHAR_TO_TCHAR (nameDevice.pwszVal ));
257+
258+ PropVariantClear (&nameDevice);
259+
260+ return DeviceName;
261+ }
262+
263+ FString FSystemMicLiteManager::GetDeviceId (const TComPtr<IMMDevice>& Device) const
264+ {
265+ if (!Device.IsValid ())
266+ {
267+ return FString (TEXT (" " ));
268+ }
269+
270+ WCHAR* swDeviceId = TCHAR_TO_WCHAR (TEXT (" " ));
271+ HRESULT Result = Device->GetId (&swDeviceId);
272+ if (Result != S_OK)
273+ {
274+ UE_LOG (LogSystemMicLiteManager, Warning, TEXT (" Result != S_OK, Device->GetId, [%s], line: %d" ), ANSI_TO_TCHAR (__FUNCTION__), __LINE__);
275+ return FString (TEXT (" " ));
276+ }
277+
278+ const FString DeviceId = FString (WCHAR_TO_TCHAR (swDeviceId));
279+
280+ CoTaskMemFree (swDeviceId);
281+ swDeviceId = nullptr ;
282+
283+ return DeviceId;
284+ }
285+
286+ TComPtr<IAudioEndpointVolume> FSystemMicLiteManager::GetAudioEndpointVolume (const TComPtr<IMMDevice>& Device) const
276287{
277288 if (!Device.IsValid ())
278289 {
@@ -290,7 +301,7 @@ TComPtr<IAudioEndpointVolume> FSystemMicLiteManager::GetAudioEndpointVolume(cons
290301 return AudioEndpointVolume;
291302}
292303
293- TComPtr<IAudioEndpointVolume> FSystemMicLiteManager::GetAudioEndpointVolume (const FString& DeviceId)
304+ TComPtr<IAudioEndpointVolume> FSystemMicLiteManager::GetAudioEndpointVolume (const FString& DeviceId) const
294305{
295306 TComPtr<IMMDevice> Device = GetDevice (DeviceId);
296307 if (!Device.IsValid ())
0 commit comments