CubebUtils: Add COM helper class

This commit is contained in:
Sepalani 2024-08-25 00:06:50 +04:00
parent c7c96ff1f2
commit 7520f427a9
4 changed files with 107 additions and 91 deletions

View file

@ -16,6 +16,13 @@
#include <cubeb/cubeb.h>
#ifdef _WIN32
#include <Objbase.h>
#include "Common/Event.h"
#include "Common/ScopeGuard.h"
#endif
static ptrdiff_t s_path_cutoff_point = 0;
static void LogCallback(const char* format, ...)
@ -49,7 +56,9 @@ static void DestroyContext(cubeb* ctx)
}
}
std::shared_ptr<cubeb> CubebUtils::GetContext()
namespace CubebUtils
{
std::shared_ptr<cubeb> GetContext()
{
static std::weak_ptr<cubeb> weak;
@ -83,12 +92,12 @@ std::shared_ptr<cubeb> CubebUtils::GetContext()
return shared;
}
std::vector<std::pair<std::string, std::string>> CubebUtils::ListInputDevices()
std::vector<std::pair<std::string, std::string>> ListInputDevices()
{
std::vector<std::pair<std::string, std::string>> devices;
cubeb_device_collection collection;
auto cubeb_ctx = CubebUtils::GetContext();
auto cubeb_ctx = GetContext();
int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection);
if (r != CUBEB_OK)
@ -135,13 +144,13 @@ std::vector<std::pair<std::string, std::string>> CubebUtils::ListInputDevices()
return devices;
}
cubeb_devid CubebUtils::GetInputDeviceById(std::string_view id)
cubeb_devid GetInputDeviceById(std::string_view id)
{
if (id.empty())
return nullptr;
cubeb_device_collection collection;
auto cubeb_ctx = CubebUtils::GetContext();
auto cubeb_ctx = GetContext();
int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection);
if (r != CUBEB_OK)
@ -169,3 +178,59 @@ cubeb_devid CubebUtils::GetInputDeviceById(std::string_view id)
return device_id;
}
CoInitSyncWorker::CoInitSyncWorker([[maybe_unused]] std::string_view worker_name)
#ifdef _WIN32
: m_work_queue
{
worker_name, [](const CoInitSyncWorker::FunctionType& f) { f(); }
}
#endif
{
#ifdef _WIN32
Common::Event sync_event;
m_work_queue.EmplaceItem([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
auto result = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
m_coinit_success = result == S_OK;
m_should_couninit = result == S_OK || result == S_FALSE;
});
sync_event.Wait();
#endif
}
CoInitSyncWorker::~CoInitSyncWorker()
{
#ifdef _WIN32
if (m_should_couninit)
{
Common::Event sync_event;
m_work_queue.EmplaceItem([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
m_should_couninit = false;
CoUninitialize();
});
sync_event.Wait();
}
m_coinit_success = false;
#endif
}
bool CoInitSyncWorker::Execute(FunctionType f)
{
#ifdef _WIN32
if (!m_coinit_success)
return false;
Common::Event sync_event;
m_work_queue.EmplaceItem([&sync_event, f] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif
f();
#ifdef _WIN32
});
sync_event.Wait();
#endif
return true;
}
} // namespace CubebUtils

View file

@ -10,6 +10,10 @@
#include <utility>
#include <vector>
#ifdef _WIN32
#include "Common/WorkQueueThread.h"
#endif
struct cubeb;
namespace CubebUtils
@ -17,4 +21,23 @@ namespace CubebUtils
std::shared_ptr<cubeb> GetContext();
std::vector<std::pair<std::string, std::string>> ListInputDevices();
const void* GetInputDeviceById(std::string_view id);
// Helper used to handle Windows COM library for cubeb WASAPI backend
class CoInitSyncWorker
{
public:
using FunctionType = std::function<void()>;
CoInitSyncWorker(std::string_view worker_name);
~CoInitSyncWorker();
bool Execute(FunctionType f);
#ifdef _WIN32
private:
Common::WorkQueueThread<FunctionType> m_work_queue;
bool m_coinit_success = false;
bool m_should_couninit = false;
#endif
};
} // namespace CubebUtils

View file

@ -8,9 +8,7 @@
#include <cubeb/cubeb.h>
#include "AudioCommon/CubebUtils.h"
#include "Common/Event.h"
#include "Common/Logging/Log.h"
#include "Common/ScopeGuard.h"
#include "Common/Swap.h"
#include "Core/Config/MainSettings.h"
#include "Core/Core.h"
@ -29,56 +27,21 @@ namespace IOS::HLE::USB
{
Microphone::Microphone(const WiiSpeakState& sampler) : m_sampler(sampler)
{
#ifdef _WIN32
Common::Event sync_event;
m_work_queue.EmplaceItem([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
auto result = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
m_coinit_success = result == S_OK;
m_should_couninit = result == S_OK || result == S_FALSE;
});
sync_event.Wait();
#endif
StreamInit();
}
Microphone::~Microphone()
{
StreamTerminate();
#ifdef _WIN32
if (m_should_couninit)
{
Common::Event sync_event;
m_work_queue.EmplaceItem([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
m_should_couninit = false;
CoUninitialize();
});
sync_event.Wait();
}
m_coinit_success = false;
#endif
}
void Microphone::StreamInit()
{
#ifdef _WIN32
if (!m_coinit_success)
if (!m_worker.Execute([this] { m_cubeb_ctx = CubebUtils::GetContext(); }))
{
ERROR_LOG_FMT(IOS_USB, "Failed to init Wii Speak stream");
return;
}
Common::Event sync_event;
m_work_queue.EmplaceItem([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif
m_cubeb_ctx = CubebUtils::GetContext();
#ifdef _WIN32
});
sync_event.Wait();
#endif
// TODO: Not here but rather inside the WiiSpeak device if possible?
StreamStart();
@ -89,20 +52,7 @@ void Microphone::StreamTerminate()
StreamStop();
if (m_cubeb_ctx)
{
#ifdef _WIN32
if (!m_coinit_success)
return;
Common::Event sync_event;
m_work_queue.EmplaceItem([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif
m_cubeb_ctx.reset();
#ifdef _WIN32
});
sync_event.Wait();
#endif
}
m_worker.Execute([this] { m_cubeb_ctx.reset(); });
}
static void state_callback(cubeb_stream* stream, void* user_data, cubeb_state state)
@ -114,14 +64,7 @@ void Microphone::StreamStart()
if (!m_cubeb_ctx)
return;
#ifdef _WIN32
if (!m_coinit_success)
return;
Common::Event sync_event;
m_work_queue.EmplaceItem([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif
m_worker.Execute([this] {
#ifdef ANDROID
JNIEnv* env = IDCache::GetEnvForThread();
if (jboolean result = env->CallStaticBooleanMethod(
@ -165,30 +108,20 @@ void Microphone::StreamStart()
}
INFO_LOG_FMT(IOS_USB, "started cubeb stream");
#ifdef _WIN32
});
sync_event.Wait();
#endif
}
void Microphone::StreamStop()
{
if (m_cubeb_stream)
{
#ifdef _WIN32
Common::Event sync_event;
m_work_queue.EmplaceItem([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif
if (cubeb_stream_stop(m_cubeb_stream) != CUBEB_OK)
ERROR_LOG_FMT(IOS_USB, "Error stopping cubeb stream");
cubeb_stream_destroy(m_cubeb_stream);
m_cubeb_stream = nullptr;
#ifdef _WIN32
});
sync_event.Wait();
#endif
}
if (!m_cubeb_stream)
return;
m_worker.Execute([this] {
if (cubeb_stream_stop(m_cubeb_stream) != CUBEB_OK)
ERROR_LOG_FMT(IOS_USB, "Error stopping cubeb stream");
cubeb_stream_destroy(m_cubeb_stream);
m_cubeb_stream = nullptr;
});
}
long Microphone::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,

View file

@ -7,8 +7,8 @@
#include <memory>
#include <mutex>
#include "AudioCommon/CubebUtils.h"
#include "Common/CommonTypes.h"
#include "Common/WorkQueueThread.h"
struct cubeb;
struct cubeb_stream;
@ -52,11 +52,6 @@ private:
const WiiSpeakState& m_sampler;
#ifdef _WIN32
Common::WorkQueueThread<std::function<void()>> m_work_queue{
"Wii Speak Worker", [](const std::function<void()>& func) { func(); }};
bool m_coinit_success = false;
bool m_should_couninit = false;
#endif
CubebUtils::CoInitSyncWorker m_worker{"Wii Speak Worker"};
};
} // namespace IOS::HLE::USB