Skip to content

Commit

Permalink
CubebUtils: Add COM helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
sepalani committed Sep 22, 2024
1 parent d10d596 commit d92e2ef
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 88 deletions.
73 changes: 69 additions & 4 deletions Source/Core/AudioCommon/CubebUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -141,7 +150,7 @@ cubeb_devid GetInputDeviceById(std::string_view id)
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)
Expand Down Expand Up @@ -169,3 +178,59 @@ cubeb_devid 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
23 changes: 23 additions & 0 deletions Source/Core/AudioCommon/CubebUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,34 @@
#include <utility>
#include <vector>

#ifdef _WIN32
#include "Common/WorkQueueThread.h"
#endif

struct cubeb;

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
89 changes: 12 additions & 77 deletions Source/Core/Core/IOS/USB/Emulated/Microphone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,56 +28,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();
Expand All @@ -88,20 +53,7 @@ void Microphone::StreamTerminate()
StopStream();

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)
Expand All @@ -113,14 +65,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(
Expand Down Expand Up @@ -164,30 +109,20 @@ void Microphone::StreamStart()
}

INFO_LOG_FMT(IOS_USB, "started cubeb stream");
#ifdef _WIN32
});
sync_event.Wait();
#endif
}

void Microphone::StopStream()
{
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,
Expand Down
9 changes: 2 additions & 7 deletions Source/Core/Core/IOS/USB/Emulated/Microphone.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,11 +52,6 @@ class Microphone final

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

0 comments on commit d92e2ef

Please sign in to comment.