2017-03-22 23:09:59 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-03-22 23:09:59 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "AudioCommon/CubebStream.h"
|
|
|
|
|
2017-03-22 23:09:59 +00:00
|
|
|
#include <cubeb/cubeb.h>
|
|
|
|
|
2017-04-24 06:55:37 +00:00
|
|
|
#include "AudioCommon/CubebUtils.h"
|
2017-03-22 23:09:59 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2022-06-18 03:31:14 +00:00
|
|
|
#include "Common/Event.h"
|
2017-03-22 23:09:59 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2022-06-18 03:31:14 +00:00
|
|
|
#include "Common/ScopeGuard.h"
|
2017-03-22 23:09:59 +00:00
|
|
|
#include "Common/Thread.h"
|
2021-10-14 00:29:04 +00:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2017-03-22 23:09:59 +00:00
|
|
|
|
2022-06-18 03:31:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Objbase.h>
|
|
|
|
#endif
|
|
|
|
|
2017-03-22 23:09:59 +00:00
|
|
|
// ~10 ms - needs to be at least 240 for surround
|
|
|
|
constexpr u32 BUFFER_SAMPLES = 512;
|
|
|
|
|
|
|
|
long CubebStream::DataCallback(cubeb_stream* stream, void* user_data, const void* /*input_buffer*/,
|
|
|
|
void* output_buffer, long num_frames)
|
|
|
|
{
|
|
|
|
auto* self = static_cast<CubebStream*>(user_data);
|
|
|
|
|
|
|
|
if (self->m_stereo)
|
|
|
|
self->m_mixer->Mix(static_cast<short*>(output_buffer), num_frames);
|
|
|
|
else
|
2017-04-24 01:05:21 +00:00
|
|
|
self->m_mixer->MixSurround(static_cast<float*>(output_buffer), num_frames);
|
2017-03-22 23:09:59 +00:00
|
|
|
|
|
|
|
return num_frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CubebStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-06-18 03:31:14 +00:00
|
|
|
CubebStream::CubebStream()
|
|
|
|
#ifdef _WIN32
|
2023-02-04 02:56:27 +00:00
|
|
|
: m_work_queue("Cubeb Worker", [](const std::function<void()>& func) { func(); })
|
2022-06-18 03:31:14 +00:00
|
|
|
{
|
|
|
|
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;
|
2022-11-27 02:58:16 +00:00
|
|
|
m_should_couninit = result == S_OK || result == S_FALSE;
|
2022-06-18 03:31:14 +00:00
|
|
|
});
|
|
|
|
sync_event.Wait();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
= default;
|
|
|
|
#endif
|
|
|
|
|
2017-10-21 23:23:40 +00:00
|
|
|
bool CubebStream::Init()
|
2017-03-22 23:09:59 +00:00
|
|
|
{
|
2022-06-18 03:31:14 +00:00
|
|
|
bool return_value = false;
|
2017-03-22 23:09:59 +00:00
|
|
|
|
2022-06-18 03:31:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (!m_coinit_success)
|
|
|
|
return false;
|
|
|
|
Common::Event sync_event;
|
|
|
|
m_work_queue.EmplaceItem([this, &return_value, &sync_event] {
|
|
|
|
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_ctx = CubebUtils::GetContext();
|
|
|
|
if (m_ctx)
|
|
|
|
{
|
|
|
|
m_stereo = !Config::ShouldUseDPL2Decoder();
|
|
|
|
|
|
|
|
cubeb_stream_params params{};
|
|
|
|
params.rate = m_mixer->GetSampleRate();
|
|
|
|
if (m_stereo)
|
|
|
|
{
|
|
|
|
params.channels = 2;
|
|
|
|
params.format = CUBEB_SAMPLE_S16NE;
|
|
|
|
params.layout = CUBEB_LAYOUT_STEREO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
params.channels = 6;
|
|
|
|
params.format = CUBEB_SAMPLE_FLOAT32NE;
|
|
|
|
params.layout = CUBEB_LAYOUT_3F2_LFE;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 minimum_latency = 0;
|
|
|
|
if (cubeb_get_min_latency(m_ctx.get(), ¶ms, &minimum_latency) != CUBEB_OK)
|
|
|
|
ERROR_LOG_FMT(AUDIO, "Error getting minimum latency");
|
|
|
|
INFO_LOG_FMT(AUDIO, "Minimum latency: {} frames", minimum_latency);
|
|
|
|
|
|
|
|
return_value =
|
|
|
|
cubeb_stream_init(m_ctx.get(), &m_stream, "Dolphin Audio Output", nullptr, nullptr,
|
|
|
|
nullptr, ¶ms, std::max(BUFFER_SAMPLES, minimum_latency),
|
|
|
|
DataCallback, StateCallback, this) == CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
});
|
|
|
|
sync_event.Wait();
|
|
|
|
#endif
|
2017-03-22 23:09:59 +00:00
|
|
|
|
2022-06-18 03:31:14 +00:00
|
|
|
return return_value;
|
2017-03-22 23:09:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 23:23:40 +00:00
|
|
|
bool CubebStream::SetRunning(bool running)
|
2017-03-22 23:09:59 +00:00
|
|
|
{
|
2022-06-17 22:39:41 +00:00
|
|
|
bool return_value = false;
|
2022-06-18 03:31:14 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (!m_coinit_success)
|
|
|
|
return false;
|
|
|
|
Common::Event sync_event;
|
|
|
|
m_work_queue.EmplaceItem([this, running, &return_value, &sync_event] {
|
|
|
|
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
|
|
|
|
#endif
|
2022-06-17 22:39:41 +00:00
|
|
|
if (running)
|
|
|
|
return_value = cubeb_stream_start(m_stream) == CUBEB_OK;
|
|
|
|
else
|
|
|
|
return_value = cubeb_stream_stop(m_stream) == CUBEB_OK;
|
2022-06-18 03:31:14 +00:00
|
|
|
#ifdef _WIN32
|
2022-06-17 22:39:41 +00:00
|
|
|
});
|
2022-06-18 03:31:14 +00:00
|
|
|
sync_event.Wait();
|
|
|
|
#endif
|
|
|
|
|
2022-06-17 22:39:41 +00:00
|
|
|
return return_value;
|
2017-10-21 19:49:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 23:23:40 +00:00
|
|
|
CubebStream::~CubebStream()
|
2017-10-21 19:49:34 +00:00
|
|
|
{
|
2022-06-18 03:31:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
Common::Event sync_event;
|
|
|
|
m_work_queue.EmplaceItem([this, &sync_event] {
|
|
|
|
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
|
|
|
|
#endif
|
|
|
|
cubeb_stream_stop(m_stream);
|
2022-06-17 22:39:41 +00:00
|
|
|
cubeb_stream_destroy(m_stream);
|
2022-06-18 03:31:14 +00:00
|
|
|
#ifdef _WIN32
|
2022-11-27 02:58:16 +00:00
|
|
|
if (m_should_couninit)
|
2022-06-18 03:31:14 +00:00
|
|
|
{
|
2022-11-27 02:58:16 +00:00
|
|
|
m_should_couninit = false;
|
2022-06-18 03:31:14 +00:00
|
|
|
CoUninitialize();
|
|
|
|
}
|
2022-11-27 02:58:16 +00:00
|
|
|
m_coinit_success = false;
|
2022-06-17 22:39:41 +00:00
|
|
|
});
|
2022-06-18 03:31:14 +00:00
|
|
|
sync_event.Wait();
|
|
|
|
#endif
|
2017-04-24 06:55:37 +00:00
|
|
|
m_ctx.reset();
|
2017-03-22 23:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CubebStream::SetVolume(int volume)
|
|
|
|
{
|
2022-06-18 03:31:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (!m_coinit_success)
|
|
|
|
return;
|
|
|
|
Common::Event sync_event;
|
|
|
|
m_work_queue.EmplaceItem([this, volume, &sync_event] {
|
|
|
|
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
|
|
|
|
#endif
|
|
|
|
cubeb_stream_set_volume(m_stream, volume / 100.0f);
|
|
|
|
#ifdef _WIN32
|
|
|
|
});
|
|
|
|
sync_event.Wait();
|
|
|
|
#endif
|
2017-03-22 23:09:59 +00:00
|
|
|
}
|