Windows: Use COM smart pointers in WASAPI plugin

Use COM smart pointer (`ComPtr`) to simplify memory management in WASAPI driver.

This reduces chances of calling `Release()` when a COM object has not been allocated.

Pull Request: https://projects.blender.org/blender/blender/pulls/121828
This commit is contained in:
Lalit Shankar Chowdhury
2024-05-17 17:00:40 +02:00
committed by Joerg Mueller
parent e30893e3c2
commit 1456dafa27
2 changed files with 8 additions and 25 deletions

View File

@@ -22,15 +22,6 @@
AUD_NAMESPACE_BEGIN
template <class T> void SafeRelease(T **ppT)
{
if(*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
HRESULT WASAPIDevice::setupRenderClient(IAudioRenderClient*& render_client, UINT32& buffer_size)
{
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
@@ -120,7 +111,6 @@ void WASAPIDevice::runMixingThread()
{
stop_thread:
m_audio_client->Stop();
SafeRelease(&render_client);
if(result == AUDCLNT_E_DEVICE_INVALIDATED)
{
@@ -149,15 +139,12 @@ void WASAPIDevice::runMixingThread()
bool WASAPIDevice::setupDevice(DeviceSpecs &specs)
{
SafeRelease(&m_audio_client);
SafeRelease(&m_imm_device);
const IID IID_IAudioClient = __uuidof(IAudioClient);
if(FAILED(m_imm_device_enumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &m_imm_device)))
return false;
if(FAILED(m_imm_device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, reinterpret_cast<void**>(&m_audio_client))))
if(FAILED(m_imm_device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, reinterpret_cast<void**>(m_audio_client.GetAddressOf()))))
return false;
WAVEFORMATEXTENSIBLE wave_format_extensible_closest_match;
@@ -389,7 +376,7 @@ WASAPIDevice::WASAPIDevice(DeviceSpecs specs, int buffersize) :
if(specs.rate == RATE_INVALID)
specs.rate = RATE_48000;
if(FAILED(CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, reinterpret_cast<void**>(&m_imm_device_enumerator))))
if(FAILED(CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, reinterpret_cast<void**>(m_imm_device_enumerator.GetAddressOf()))))
goto error;
if(!setupDevice(specs))
@@ -404,9 +391,6 @@ WASAPIDevice::WASAPIDevice(DeviceSpecs specs, int buffersize) :
return;
error:
SafeRelease(&m_imm_device);
SafeRelease(&m_imm_device_enumerator);
SafeRelease(&m_audio_client);
AUD_THROW(DeviceException, "The audio device couldn't be opened with WASAPI.");
}
@@ -416,10 +400,6 @@ WASAPIDevice::~WASAPIDevice()
m_imm_device_enumerator->UnregisterEndpointNotificationCallback(this);
SafeRelease(&m_audio_client);
SafeRelease(&m_imm_device);
SafeRelease(&m_imm_device_enumerator);
destroy();
}

View File

@@ -34,9 +34,12 @@
#include <audioclient.h>
#include <mmdeviceapi.h>
#include <mmreg.h>
#include <wrl/client.h>
AUD_NAMESPACE_BEGIN
using Microsoft::WRL::ComPtr;
/**
* This device plays back through WASAPI, the Windows audio API.
*/
@@ -44,9 +47,9 @@ class AUD_PLUGIN_API WASAPIDevice : IMMNotificationClient, public ThreadedDevice
{
private:
int m_buffersize;
IMMDeviceEnumerator* m_imm_device_enumerator;
IMMDevice* m_imm_device;
IAudioClient* m_audio_client;
ComPtr<IMMDeviceEnumerator> m_imm_device_enumerator;
ComPtr<IMMDevice> m_imm_device;
ComPtr<IAudioClient> m_audio_client;
WAVEFORMATEXTENSIBLE m_wave_format_extensible;
bool m_default_device_changed;
LONG m_reference_count;