Skip to content

Commit

Permalink
Fix for #300 : Added null check to GetAudioDeviceFriendlyName
Browse files Browse the repository at this point in the history
  • Loading branch information
sskodje committed May 28, 2024
1 parent 6612916 commit aa23342
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions ScreenRecorderLibNative/CoreAudio.util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ HRESULT GetAudioDeviceFriendlyName(_In_ IMMDevice *pDevice, _Out_ std::wstring *
// Get the endpoint device's friendly-name property.
RETURN_ON_BAD_HR(hr = pProps->GetValue(PKEY_Device_FriendlyName, &varString));

if (varString.pwszVal == NULL) {
return E_FAIL;
}
*deviceName = std::wstring(varString.pwszVal);
return hr;
}
Expand Down
9 changes: 6 additions & 3 deletions ScreenRecorderLibNative/WASAPICapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,26 @@ HRESULT WASAPICapture::Initialize(_In_ std::wstring deviceId, _In_ EDataFlow flo
else {
RETURN_ON_BAD_HR(GetActiveAudioDevice(deviceId.c_str(), flow, &pDevice));
}

HRESULT hr = E_FAIL;
if (pDevice) {
LPWSTR deviceId;
pDevice->GetId(&deviceId);
m_DeviceId = std::wstring(deviceId);
if (m_IsDefaultDevice) {
m_DefaultDeviceId = m_DeviceId;
}
GetAudioDeviceFriendlyName(deviceId, &m_DeviceName);
hr = GetAudioDeviceFriendlyName(deviceId, &m_DeviceName);
if (FAILED(hr)) {
m_DeviceName = L"Unknown Device";
}
CoTaskMemFree(deviceId);
}
else {
LOG_ERROR("IMMDevice cannot be NULL");
return E_FAIL;
}

HRESULT hr = InitializeAudioClient(pDevice, &m_AudioClient);
hr = InitializeAudioClient(pDevice, &m_AudioClient);
if (SUCCEEDED(hr)) {
WWMFResampler *pResampler;
hr = InitializeResampler(m_AudioOptions->GetAudioSamplesPerSecond(), m_AudioOptions->GetAudioChannels(), m_AudioClient, &m_InputFormat, &m_OutputFormat, &pResampler);
Expand Down
5 changes: 4 additions & 1 deletion ScreenRecorderLibNative/WASAPINotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ HRESULT STDMETHODCALLTYPE WASAPINotify::OnDefaultDeviceChanged(EDataFlow flow, E
if (flow == m_CaptureClient->GetFlow()) {
m_CaptureClient->SetDefaultDevice(flow, role, pwstrDeviceId);
std::wstring deviceName;
GetAudioDeviceFriendlyName(pwstrDeviceId, &deviceName);
HRESULT hr = GetAudioDeviceFriendlyName(pwstrDeviceId, &deviceName);
if (FAILED(hr)) {
deviceName = L"Unknown Device";
}

LOG_DEBUG("New default device: name=%s, flow = %s, role = %s\n",
deviceName.c_str(), pszFlow, pszRole);
Expand Down

0 comments on commit aa23342

Please sign in to comment.