summaryrefslogtreecommitdiffstats
path: root/src/audio_core/sink
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_core/sink')
-rw-r--r--src/audio_core/sink/cubeb_sink.cpp47
-rw-r--r--src/audio_core/sink/cubeb_sink.h7
-rw-r--r--src/audio_core/sink/sdl2_sink.cpp40
-rw-r--r--src/audio_core/sink/sdl2_sink.h7
-rw-r--r--src/audio_core/sink/sink_details.cpp73
-rw-r--r--src/audio_core/sink/sink_details.h8
6 files changed, 130 insertions, 52 deletions
diff --git a/src/audio_core/sink/cubeb_sink.cpp b/src/audio_core/sink/cubeb_sink.cpp
index 9a0801888..bbb598bc5 100644
--- a/src/audio_core/sink/cubeb_sink.cpp
+++ b/src/audio_core/sink/cubeb_sink.cpp
@@ -8,6 +8,7 @@
#include "audio_core/sink/cubeb_sink.h"
#include "audio_core/sink/sink_stream.h"
#include "common/logging/log.h"
+#include "common/scope_exit.h"
#include "core/core.h"
#ifdef _WIN32
@@ -332,25 +333,38 @@ std::vector<std::string> ListCubebSinkDevices(bool capture) {
return device_list;
}
-u32 GetCubebLatency() {
- cubeb* ctx;
+namespace {
+static long TmpDataCallback(cubeb_stream*, void*, const void*, void*, long) {
+ return TargetSampleCount;
+}
+static void TmpStateCallback(cubeb_stream*, void*, cubeb_state) {}
+} // namespace
+
+bool IsCubebSuitable() {
+#if !defined(HAVE_CUBEB)
+ return false;
+#else
+ cubeb* ctx{nullptr};
#ifdef _WIN32
auto com_init_result = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
#endif
+ // Init cubeb
if (cubeb_init(&ctx, "yuzu Latency Getter", nullptr) != CUBEB_OK) {
- LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
- // Return a large latency so we choose SDL instead.
- return 10000u;
+ LOG_ERROR(Audio_Sink, "Cubeb failed to init, it is not suitable.");
+ return false;
}
+ SCOPE_EXIT({ cubeb_destroy(ctx); });
+
#ifdef _WIN32
if (SUCCEEDED(com_init_result)) {
CoUninitialize();
}
#endif
+ // Get min latency
cubeb_stream_params params{};
params.rate = TargetSampleRate;
params.channels = 2;
@@ -361,12 +375,27 @@ u32 GetCubebLatency() {
u32 latency{0};
const auto latency_error = cubeb_get_min_latency(ctx, &params, &latency);
if (latency_error != CUBEB_OK) {
- LOG_CRITICAL(Audio_Sink, "Error getting minimum latency, error: {}", latency_error);
- latency = TargetSampleCount * 2;
+ LOG_ERROR(Audio_Sink, "Cubeb could not get min latency, it is not suitable.");
+ return false;
}
latency = std::max(latency, TargetSampleCount * 2);
- cubeb_destroy(ctx);
- return latency;
+
+ // Test opening a device with standard parameters
+ cubeb_devid output_device{0};
+ cubeb_devid input_device{0};
+ std::string name{"Yuzu test"};
+ cubeb_stream* stream{nullptr};
+
+ if (cubeb_stream_init(ctx, &stream, name.c_str(), input_device, nullptr, output_device, &params,
+ latency, &TmpDataCallback, &TmpStateCallback, nullptr) != CUBEB_OK) {
+ LOG_CRITICAL(Audio_Sink, "Cubeb could not open a device, it is not suitable.");
+ return false;
+ }
+
+ cubeb_stream_stop(stream);
+ cubeb_stream_destroy(stream);
+ return true;
+#endif
}
} // namespace AudioCore::Sink
diff --git a/src/audio_core/sink/cubeb_sink.h b/src/audio_core/sink/cubeb_sink.h
index 3302cb98d..f49a6fdaa 100644
--- a/src/audio_core/sink/cubeb_sink.h
+++ b/src/audio_core/sink/cubeb_sink.h
@@ -97,10 +97,11 @@ private:
std::vector<std::string> ListCubebSinkDevices(bool capture);
/**
- * Get the reported latency for this sink.
+ * Check if this backend is suitable for use.
+ * Checks if enabled, its latency, whether it opens successfully, etc.
*
- * @return Minimum latency for this sink.
+ * @return True is this backend is suitable, false otherwise.
*/
-u32 GetCubebLatency();
+bool IsCubebSuitable();
} // namespace AudioCore::Sink
diff --git a/src/audio_core/sink/sdl2_sink.cpp b/src/audio_core/sink/sdl2_sink.cpp
index c1529d1f9..7b89151de 100644
--- a/src/audio_core/sink/sdl2_sink.cpp
+++ b/src/audio_core/sink/sdl2_sink.cpp
@@ -9,6 +9,7 @@
#include "audio_core/sink/sdl2_sink.h"
#include "audio_core/sink/sink_stream.h"
#include "common/logging/log.h"
+#include "common/scope_exit.h"
#include "core/core.h"
namespace AudioCore::Sink {
@@ -84,6 +85,7 @@ public:
}
Stop();
+ SDL_ClearQueuedAudio(device);
SDL_CloseAudioDevice(device);
}
@@ -227,8 +229,42 @@ std::vector<std::string> ListSDLSinkDevices(bool capture) {
return device_list;
}
-u32 GetSDLLatency() {
- return TargetSampleCount * 2;
+bool IsSDLSuitable() {
+#if !defined(HAVE_SDL2)
+ return false;
+#else
+ // Check SDL can init
+ if (!SDL_WasInit(SDL_INIT_AUDIO)) {
+ if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
+ LOG_ERROR(Audio_Sink, "SDL failed to init, it is not suitable. Error: {}",
+ SDL_GetError());
+ return false;
+ }
+ }
+
+ // We can set any latency frequency we want with SDL, so no need to check that.
+
+ // Check we can open a device with standard parameters
+ SDL_AudioSpec spec;
+ spec.freq = TargetSampleRate;
+ spec.channels = 2u;
+ spec.format = AUDIO_S16SYS;
+ spec.samples = TargetSampleCount * 2;
+ spec.callback = nullptr;
+ spec.userdata = nullptr;
+
+ SDL_AudioSpec obtained;
+ auto device = SDL_OpenAudioDevice(nullptr, false, &spec, &obtained, false);
+
+ if (device == 0) {
+ LOG_ERROR(Audio_Sink, "SDL failed to open a device, it is not suitable. Error: {}",
+ SDL_GetError());
+ return false;
+ }
+
+ SDL_CloseAudioDevice(device);
+ return true;
+#endif
}
} // namespace AudioCore::Sink
diff --git a/src/audio_core/sink/sdl2_sink.h b/src/audio_core/sink/sdl2_sink.h
index 27ed1ab94..9211d2e97 100644
--- a/src/audio_core/sink/sdl2_sink.h
+++ b/src/audio_core/sink/sdl2_sink.h
@@ -88,10 +88,11 @@ private:
std::vector<std::string> ListSDLSinkDevices(bool capture);
/**
- * Get the reported latency for this sink.
+ * Check if this backend is suitable for use.
+ * Checks if enabled, its latency, whether it opens successfully, etc.
*
- * @return Minimum latency for this sink.
+ * @return True is this backend is suitable, false otherwise.
*/
-u32 GetSDLLatency();
+bool IsSDLSuitable();
} // namespace AudioCore::Sink
diff --git a/src/audio_core/sink/sink_details.cpp b/src/audio_core/sink/sink_details.cpp
index 39ea6d91b..7c9a4e3ac 100644
--- a/src/audio_core/sink/sink_details.cpp
+++ b/src/audio_core/sink/sink_details.cpp
@@ -15,86 +15,95 @@
#endif
#include "audio_core/sink/null_sink.h"
#include "common/logging/log.h"
+#include "common/settings_enums.h"
namespace AudioCore::Sink {
namespace {
struct SinkDetails {
using FactoryFn = std::unique_ptr<Sink> (*)(std::string_view);
using ListDevicesFn = std::vector<std::string> (*)(bool);
- using LatencyFn = u32 (*)();
+ using SuitableFn = bool (*)();
/// Name for this sink.
- std::string_view id;
+ Settings::AudioEngine id;
/// A method to call to construct an instance of this type of sink.
FactoryFn factory;
/// A method to call to list available devices.
ListDevicesFn list_devices;
- /// Method to get the latency of this backend.
- LatencyFn latency;
+ /// Check whether this backend is suitable to be used.
+ SuitableFn is_suitable;
};
// sink_details is ordered in terms of desirability, with the best choice at the top.
constexpr SinkDetails sink_details[] = {
#ifdef HAVE_CUBEB
SinkDetails{
- "cubeb",
+ Settings::AudioEngine::Cubeb,
[](std::string_view device_id) -> std::unique_ptr<Sink> {
return std::make_unique<CubebSink>(device_id);
},
&ListCubebSinkDevices,
- &GetCubebLatency,
+ &IsCubebSuitable,
},
#endif
#ifdef HAVE_SDL2
SinkDetails{
- "sdl2",
+ Settings::AudioEngine::Sdl2,
[](std::string_view device_id) -> std::unique_ptr<Sink> {
return std::make_unique<SDLSink>(device_id);
},
&ListSDLSinkDevices,
- &GetSDLLatency,
+ &IsSDLSuitable,
},
#endif
- SinkDetails{"null",
- [](std::string_view device_id) -> std::unique_ptr<Sink> {
- return std::make_unique<NullSink>(device_id);
- },
- [](bool capture) { return std::vector<std::string>{"null"}; }, []() { return 0u; }},
+ SinkDetails{
+ Settings::AudioEngine::Null,
+ [](std::string_view device_id) -> std::unique_ptr<Sink> {
+ return std::make_unique<NullSink>(device_id);
+ },
+ [](bool capture) { return std::vector<std::string>{"null"}; },
+ []() { return true; },
+ },
};
-const SinkDetails& GetOutputSinkDetails(std::string_view sink_id) {
- const auto find_backend{[](std::string_view id) {
+const SinkDetails& GetOutputSinkDetails(Settings::AudioEngine sink_id) {
+ const auto find_backend{[](Settings::AudioEngine id) {
return std::find_if(std::begin(sink_details), std::end(sink_details),
[&id](const auto& sink_detail) { return sink_detail.id == id; });
}};
auto iter = find_backend(sink_id);
- if (sink_id == "auto") {
- // Auto-select a backend. Prefer CubeB, but it may report a large minimum latency which
- // causes audio issues, in that case go with SDL.
-#if defined(HAVE_CUBEB) && defined(HAVE_SDL2)
- iter = find_backend("cubeb");
- if (iter->latency() > TargetSampleCount * 3) {
- iter = find_backend("sdl2");
+ if (sink_id == Settings::AudioEngine::Auto) {
+ // Auto-select a backend. Use the sink details ordering, preferring cubeb first, checking
+ // that the backend is available and suitable to use.
+ for (auto& details : sink_details) {
+ if (details.is_suitable()) {
+ iter = &details;
+ break;
+ }
+ }
+ LOG_INFO(Service_Audio, "Auto-selecting the {} backend",
+ Settings::CanonicalizeEnum(iter->id));
+ } else {
+ if (iter != std::end(sink_details) && !iter->is_suitable()) {
+ LOG_ERROR(Service_Audio, "Selected backend {} is not suitable, falling back to null",
+ Settings::CanonicalizeEnum(iter->id));
+ iter = find_backend(Settings::AudioEngine::Null);
}
-#else
- iter = std::begin(sink_details);
-#endif
- LOG_INFO(Service_Audio, "Auto-selecting the {} backend", iter->id);
}
if (iter == std::end(sink_details)) {
- LOG_ERROR(Audio, "Invalid sink_id {}", sink_id);
- iter = find_backend("null");
+ LOG_ERROR(Audio, "Invalid sink_id {}", Settings::CanonicalizeEnum(sink_id));
+ iter = find_backend(Settings::AudioEngine::Null);
}
return *iter;
}
} // Anonymous namespace
-std::vector<std::string_view> GetSinkIDs() {
- std::vector<std::string_view> sink_ids(std::size(sink_details));
+std::vector<Settings::AudioEngine> GetSinkIDs() {
+ std::vector<Settings::AudioEngine> sink_ids(std::size(sink_details));
std::transform(std::begin(sink_details), std::end(sink_details), std::begin(sink_ids),
[](const auto& sink) { return sink.id; });
@@ -102,11 +111,11 @@ std::vector<std::string_view> GetSinkIDs() {
return sink_ids;
}
-std::vector<std::string> GetDeviceListForSink(std::string_view sink_id, bool capture) {
+std::vector<std::string> GetDeviceListForSink(Settings::AudioEngine sink_id, bool capture) {
return GetOutputSinkDetails(sink_id).list_devices(capture);
}
-std::unique_ptr<Sink> CreateSinkFromID(std::string_view sink_id, std::string_view device_id) {
+std::unique_ptr<Sink> CreateSinkFromID(Settings::AudioEngine sink_id, std::string_view device_id) {
return GetOutputSinkDetails(sink_id).factory(device_id);
}
diff --git a/src/audio_core/sink/sink_details.h b/src/audio_core/sink/sink_details.h
index e75932898..c8498842b 100644
--- a/src/audio_core/sink/sink_details.h
+++ b/src/audio_core/sink/sink_details.h
@@ -3,9 +3,11 @@
#pragma once
+#include <memory>
#include <string>
#include <string_view>
#include <vector>
+#include "common/settings_enums.h"
namespace AudioCore {
class AudioManager;
@@ -19,7 +21,7 @@ class Sink;
*
* @return Vector of available sink names.
*/
-std::vector<std::string_view> GetSinkIDs();
+std::vector<Settings::AudioEngine> GetSinkIDs();
/**
* Gets the list of devices for a particular sink identified by the given ID.
@@ -28,7 +30,7 @@ std::vector<std::string_view> GetSinkIDs();
* @param capture - Get capture (input) devices, or output devices?
* @return Vector of device names.
*/
-std::vector<std::string> GetDeviceListForSink(std::string_view sink_id, bool capture);
+std::vector<std::string> GetDeviceListForSink(Settings::AudioEngine sink_id, bool capture);
/**
* Creates an audio sink identified by the given device ID.
@@ -37,7 +39,7 @@ std::vector<std::string> GetDeviceListForSink(std::string_view sink_id, bool cap
* @param device_id - Name of the device to create.
* @return Pointer to the created sink.
*/
-std::unique_ptr<Sink> CreateSinkFromID(std::string_view sink_id, std::string_view device_id);
+std::unique_ptr<Sink> CreateSinkFromID(Settings::AudioEngine sink_id, std::string_view device_id);
} // namespace Sink
} // namespace AudioCore