Refactor to use Microsoft::WRL::ComPtr (#1599)
Some checks failed
Generate translation template / generate-pot (push) Failing after 2m59s
Build check / build (push) Has been cancelled

This commit is contained in:
oltolm 2025-06-16 23:25:06 +02:00 committed by GitHub
parent da98aa4176
commit 2f02fda9ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 56 additions and 119 deletions

View file

@ -1,9 +1,8 @@
#include "DirectSoundAPI.h"
#include "gui/wxgui.h"
#include "util/helpers/helpers.h"
#include "gui/guiWrapper.h"
#include <wrl/client.h>
#pragma comment(lib, "Dsound.lib")
@ -15,12 +14,9 @@ std::wstring DirectSoundAPI::DirectSoundDeviceDescription::GetIdentifier() const
DirectSoundAPI::DirectSoundAPI(GUID* guid, sint32 samplerate, sint32 channels, sint32 samples_per_block, sint32 bits_per_sample)
: IAudioAPI(samplerate, channels, samples_per_block, bits_per_sample)
{
LPDIRECTSOUND8 direct_sound;
if (DirectSoundCreate8(guid, &direct_sound, nullptr) != DS_OK)
if (DirectSoundCreate8(guid, &m_direct_sound, nullptr) != DS_OK)
throw std::runtime_error("can't create directsound device");
m_direct_sound = decltype(m_direct_sound)(direct_sound);
if (FAILED(m_direct_sound->SetCooperativeLevel(gui_getWindowInfo().window_main.hwnd, DSSCL_PRIORITY)))
throw std::runtime_error("can't set directsound priority");
@ -30,7 +26,7 @@ DirectSoundAPI::DirectSoundAPI(GUID* guid, sint32 samplerate, sint32 channels, s
bd.dwBufferBytes = kBufferCount * m_bytesPerBlock; // kBlockCount * (samples_per_block * channels * (bits_per_sample / 8));
bd.lpwfxFormat = (LPWAVEFORMATEX)&m_wfx;
LPDIRECTSOUNDBUFFER sound_buffer;
Microsoft::WRL::ComPtr<IDirectSoundBuffer> sound_buffer;
if (FAILED(m_direct_sound->CreateSoundBuffer(&bd, &sound_buffer, nullptr)))
throw std::runtime_error("can't create directsound soundbuffer");
@ -41,27 +37,17 @@ DirectSoundAPI::DirectSoundAPI(GUID* guid, sint32 samplerate, sint32 channels, s
m_sound_buffer_size = caps.dwBufferBytes;
LPDIRECTSOUNDBUFFER8 sound_buffer8;
LPDIRECTSOUNDNOTIFY8 notify8;
sound_buffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&sound_buffer8);
Microsoft::WRL::ComPtr<IDirectSoundNotify8> notify8;
if (!sound_buffer8)
if (FAILED(sound_buffer->QueryInterface(IID_IDirectSoundBuffer8, &m_sound_buffer)))
{
sound_buffer->Release();
throw std::runtime_error("can't get directsound buffer interface");
}
m_sound_buffer = decltype(m_sound_buffer)(sound_buffer8);
sound_buffer->QueryInterface(IID_IDirectSoundNotify8, (void**)&notify8);
if (!notify8)
if (FAILED(sound_buffer->QueryInterface(IID_IDirectSoundNotify8, &m_notify)))
{
sound_buffer->Release();
throw std::runtime_error("can't get directsound notify interface");
}
m_notify = decltype(m_notify)(notify8);
sound_buffer->Release();
{ // initialize sound buffer
void *ptr1, *ptr2;
@ -155,10 +141,6 @@ DirectSoundAPI::~DirectSoundAPI()
if(m_thread.joinable())
m_thread.join();
m_notify.reset();
m_sound_buffer.reset();
m_direct_sound.reset();
for(auto entry : m_notify_event)
{
if (entry)
@ -186,7 +168,7 @@ bool DirectSoundAPI::Stop()
bool DirectSoundAPI::FeedBlock(sint16* data)
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
if (m_buffer.size() > kBlockCount)
{
cemuLog_logDebug(LogType::Force, "dropped direct sound block since too many buffers are queued");

View file

@ -2,8 +2,8 @@
#define DIRECTSOUND_VERSION 0x0800
#include <mmsystem.h>
//#include <mmreg.h>
#include <dsound.h>
#include <wrl/client.h>
#include "IAudioAPI.h"
@ -41,15 +41,10 @@ public:
static std::vector<DeviceDescriptionPtr> GetInputDevices();
private:
struct DirectSoundDeleter
{
void operator()(IUnknown* ptr) const { if (ptr) ptr->Release(); }
};
std::unique_ptr<IDirectSound8, DirectSoundDeleter> m_direct_sound;
//std::unique_ptr<IDirectSoundCapture8, DirectSoundDeleter> m_direct_sound_capture;
std::unique_ptr<IDirectSoundBuffer8, DirectSoundDeleter> m_sound_buffer;
std::unique_ptr<IDirectSoundNotify8, DirectSoundDeleter> m_notify;
Microsoft::WRL::ComPtr<IDirectSound8> m_direct_sound;
//Microsoft::WRL::ComPtr<IDirectSoundCapture8> m_direct_sound_capture;
Microsoft::WRL::ComPtr<IDirectSoundBuffer8> m_sound_buffer;
Microsoft::WRL::ComPtr<IDirectSoundNotify8> m_notify;
DWORD m_sound_buffer_size = 0;
uint32_t m_offset = 0;

View file

@ -2,6 +2,7 @@
//#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
#include <wrl/client.h>
#include <xaudio2.h>
#ifndef XAUDIO2_DLL
@ -33,17 +34,15 @@ XAudio2API::XAudio2API(std::wstring device_id, uint32 samplerate, uint32 channel
throw std::runtime_error("can't find XAudio2Create import");
HRESULT hres;
IXAudio2* xaudio;
if (FAILED((hres = _XAudio2Create(&xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR))))
if (FAILED((hres = _XAudio2Create(&m_xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR))))
throw std::runtime_error(fmt::format("can't create xaudio device (hres: {:#x})", hres));
m_xaudio = decltype(m_xaudio)(xaudio);
IXAudio2MasteringVoice* mastering_voice;
if (FAILED((hres = m_xaudio->CreateMasteringVoice(&mastering_voice, channels, samplerate, 0, m_device_id.empty() ? nullptr : m_device_id.c_str()))))
throw std::runtime_error(fmt::format("can't create xaudio mastering voice (hres: {:#x})", hres));
m_mastering_voice = decltype(m_mastering_voice)(mastering_voice);
m_mastering_voice.reset(mastering_voice);
m_wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
m_wfx.Format.nChannels = channels;
@ -88,12 +87,6 @@ XAudio2API::XAudio2API(std::wstring device_id, uint32 samplerate, uint32 channel
m_xaudio->StartEngine();
}
void XAudio2API::XAudioDeleter::operator()(IXAudio2* ptr) const
{
if (ptr)
ptr->Release();
}
void XAudio2API::VoiceDeleter::operator()(IXAudio2Voice* ptr) const
{
if (ptr)
@ -106,10 +99,6 @@ XAudio2API::~XAudio2API()
m_xaudio->StopEngine();
XAudio2API::Stop();
m_source_voice.reset();
m_mastering_voice.reset();
m_xaudio.reset();
}
void XAudio2API::SetVolume(sint32 volume)
@ -179,10 +168,10 @@ const std::vector<XAudio2API::DeviceDescriptionPtr>& XAudio2API::RefreshDevices(
try
{
struct IWbemLocator *wbem_locator = nullptr;
Microsoft::WRL::ComPtr<IWbemLocator> wbem_locator;
HRESULT hres = CoCreateInstance(__uuidof(WbemLocator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWbemLocator), (LPVOID*)&wbem_locator);
if (FAILED(hres) || !wbem_locator)
HRESULT hres = CoCreateInstance(__uuidof(WbemLocator), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&wbem_locator));
if (FAILED(hres))
throw std::system_error(hres, std::system_category());
std::shared_ptr<OLECHAR> path(SysAllocString(LR"(\\.\root\cimv2)"), SysFreeString);
@ -191,20 +180,19 @@ const std::vector<XAudio2API::DeviceDescriptionPtr>& XAudio2API::RefreshDevices(
std::shared_ptr<OLECHAR> name_row(SysAllocString(L"Name"), SysFreeString);
std::shared_ptr<OLECHAR> device_id_row(SysAllocString(L"DeviceID"), SysFreeString);
IWbemServices *wbem_services = nullptr;
Microsoft::WRL::ComPtr<IWbemServices> wbem_services;
hres = wbem_locator->ConnectServer(path.get(), nullptr, nullptr, nullptr, 0, nullptr, nullptr, &wbem_services);
wbem_locator->Release(); // Free memory resources.
if (FAILED(hres) || !wbem_services)
throw std::system_error(hres, std::system_category());
hres = CoSetProxyBlanket(wbem_services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE);
if (FAILED(hres))
throw std::system_error(hres, std::system_category());
IEnumWbemClassObject* wbem_enum = nullptr;
hres = CoSetProxyBlanket(wbem_services.Get(), RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE);
if (FAILED(hres))
throw std::system_error(hres, std::system_category());
Microsoft::WRL::ComPtr<IEnumWbemClassObject> wbem_enum;
hres = wbem_services->ExecQuery(language.get(), query.get(), WBEM_FLAG_RETURN_WBEM_COMPLETE | WBEM_FLAG_FORWARD_ONLY, nullptr, &wbem_enum);
if (FAILED(hres) || !wbem_enum)
if (FAILED(hres))
throw std::system_error(hres, std::system_category());
ULONG returned;
@ -250,11 +238,6 @@ const std::vector<XAudio2API::DeviceDescriptionPtr>& XAudio2API::RefreshDevices(
auto default_device = std::make_shared<XAudio2DeviceDescription>(L"Primary Sound Driver", L"");
s_devices.insert(s_devices.begin(), default_device);
}
wbem_enum->Release();
// Clean up
wbem_services->Release();
}
catch (const std::system_error& ex)
{

View file

@ -4,6 +4,7 @@
#include <mmsystem.h>
#include <mmreg.h>
#include <dsound.h>
#include <wrl/client.h>
#include "IAudioAPI.h"
@ -50,11 +51,6 @@ private:
static const std::vector<DeviceDescriptionPtr>& RefreshDevices();
struct XAudioDeleter
{
void operator()(IXAudio2* ptr) const;
};
struct VoiceDeleter
{
void operator()(IXAudio2Voice* ptr) const;
@ -63,7 +59,7 @@ private:
static HMODULE s_xaudio_dll;
static std::vector<DeviceDescriptionPtr> s_devices;
std::unique_ptr<IXAudio2, XAudioDeleter> m_xaudio;
Microsoft::WRL::ComPtr<IXAudio2> m_xaudio;
std::wstring m_device_id;
std::unique_ptr<IXAudio2MasteringVoice, VoiceDeleter> m_mastering_voice;
std::unique_ptr<IXAudio2SourceVoice, VoiceDeleter> m_source_voice;