Debian Codegraph @

Source/Core/AudioCommon/CubebUtils.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "AudioCommon/CubebUtils.h"
5
6 #include <cstdarg>
7 #include <cstring>
8 #include <string_view>
9
10 #include "Common/Logging/Log.h"
11 #include "Common/Logging/LogManager.h"
12 #include "Common/StringUtil.h"
13
14 #include <cubeb/cubeb.h>
15
16 #ifdef _WIN32
17 #include <Objbase.h>
18 #endif
19
20 static void LogCallback(const char* format, ...)
21 {
22 auto* instance = Common::Log::LogManager::GetInstance();
23 if (instance == nullptr)
24 return;
25
26 constexpr auto log_type = Common::Log::LogType::AUDIO;
27 constexpr auto log_level = Common::Log::LogLevel::LINFO;
28 if (!instance->IsEnabled(log_type, log_level))
29 return;
30
31 va_list args;
32 va_start(args, format);
33 const char* filename = va_arg(args, const char*);
34 const auto last_slash = std::string_view(filename).find_last_of("/\\");
35 if (last_slash != std::string_view::npos)
36 filename = filename + last_slash + 1;
37 const int lineno = va_arg(args, int);
38 const std::string adapted_format(StripWhitespace(format + strlen("%s:%d:")));
39 const std::string message = StringFromFormatV(adapted_format.c_str(), args);
40 va_end(args);
41
42 instance->LogWithFullPath(log_level, log_type, filename, lineno, message.c_str());
43 }
44
45 static void DestroyContext(cubeb* ctx)
46 {
47 cubeb_destroy(ctx);
48 if (cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr) != CUBEB_OK)
49 {
50 ERROR_LOG_FMT(AUDIO, "Error removing cubeb log callback");
51 }
52 }
53
54 namespace CubebUtils
55 {
56 std::shared_ptr<cubeb> GetContext()
57 {
58 static std::weak_ptr<cubeb> weak;
59
60 std::shared_ptr<cubeb> shared = weak.lock();
61 // Already initialized
62 if (shared)
63 return shared;
64
65 if (cubeb_set_log_callback(CUBEB_LOG_NORMAL, LogCallback) != CUBEB_OK)
66 {
67 ERROR_LOG_FMT(AUDIO, "Error setting cubeb log callback");
68 }
69
70 cubeb* ctx;
71 if (cubeb_init(&ctx, "Dolphin Emulator", nullptr) != CUBEB_OK)
72 {
73 ERROR_LOG_FMT(AUDIO, "Error initializing cubeb library");
74 return nullptr;
75 }
76 INFO_LOG_FMT(AUDIO, "Cubeb initialized using {} backend", cubeb_get_backend_id(ctx));
77
78 weak = shared = {ctx, DestroyContext};
79 return shared;
80 }
81
82 std::vector<std::pair<std::string, std::string>> ListInputDevices()
83 {
84 std::vector<std::pair<std::string, std::string>> devices;
85
86 cubeb_device_collection collection;
87 auto cubeb_ctx = GetContext();
88 const int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection);
89
90 if (r != CUBEB_OK)
91 {
92 ERROR_LOG_FMT(AUDIO, "Error listing cubeb input devices");
93 return devices;
94 }
95
96 INFO_LOG_FMT(AUDIO, "Listing cubeb input devices:");
97 for (uint32_t i = 0; i < collection.count; i++)
98 {
99 const auto& info = collection.device[i];
100 const auto device_state = info.state;
101 const char* state_name = [device_state] {
102 switch (device_state)
103 {
104 case CUBEB_DEVICE_STATE_DISABLED:
105 return "disabled";
106 case CUBEB_DEVICE_STATE_UNPLUGGED:
107 return "unplugged";
108 case CUBEB_DEVICE_STATE_ENABLED:
109 return "enabled";
110 default:
111 return "unknown?";
112 }
113 }();
114
115 // According to cubeb_device_info definition in cubeb.h:
116 // > "Optional vendor name, may be NULL."
117 // In practice, it seems some other fields might be NULL as well.
118 static constexpr auto fmt_str = [](const char* ptr) constexpr -> const char* {
119 return (ptr == nullptr) ? "(null)" : ptr;
120 };
121
122 INFO_LOG_FMT(AUDIO,
123 "[{}] Device ID: {}\n"
124 "\tName: {}\n"
125 "\tGroup ID: {}\n"
126 "\tVendor: {}\n"
127 "\tState: {}",
128 i, fmt_str(info.device_id), fmt_str(info.friendly_name), fmt_str(info.group_id),
129 fmt_str(info.vendor_name), state_name);
130
131 if (info.device_id == nullptr)
132 continue; // Shouldn't happen
133
134 if (info.state == CUBEB_DEVICE_STATE_ENABLED)
135 {
136 devices.emplace_back(info.device_id, fmt_str(info.friendly_name));
137 }
138 }
139
140 cubeb_device_collection_destroy(cubeb_ctx.get(), &collection);
141
142 return devices;
143 }
144
145 cubeb_devid GetInputDeviceById(std::string_view id)
146 {
147 if (id.empty())
148 return nullptr;
149
150 cubeb_device_collection collection;
151 auto cubeb_ctx = CubebUtils::GetContext();
152 const int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection);
153
154 if (r != CUBEB_OK)
155 {
156 ERROR_LOG_FMT(AUDIO, "Error enumerating cubeb input devices");
157 return nullptr;
158 }
159
160 cubeb_devid device_id = nullptr;
161 for (uint32_t i = 0; i < collection.count; i++)
162 {
163 const auto& info = collection.device[i];
164 if (id.compare(info.device_id) == 0)
165 {
166 device_id = info.devid;
167 break;
168 }
169 }
170 if (device_id == nullptr)
171 {
172 WARN_LOG_FMT(AUDIO, "Failed to find selected input device, defaulting to system preferences");
173 }
174
175 cubeb_device_collection_destroy(cubeb_ctx.get(), &collection);
176
177 return device_id;
178 }
179
180 std::vector<std::pair<std::string, std::string>> ListOutputDevices()
181 {
182 std::vector<std::pair<std::string, std::string>> devices;
183
184 cubeb_device_collection collection;
185 auto cubeb_ctx = GetContext();
186 if (!cubeb_ctx)
187 return devices;
188
189 const int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_OUTPUT, &collection);
190 if (r != CUBEB_OK)
191 {
192 ERROR_LOG_FMT(AUDIO, "Error listing cubeb output devices");
193 return devices;
194 }
195
196 for (uint32_t i = 0; i < collection.count; i++)
197 {
198 const auto& info = collection.device[i];
199 if (info.device_id == nullptr)
200 continue;
201
202 if (info.state == CUBEB_DEVICE_STATE_ENABLED)
203 {
204 const char* name = (info.friendly_name != nullptr) ? info.friendly_name : info.device_id;
205 devices.emplace_back(info.device_id, name);
206 }
207 }
208
209 cubeb_device_collection_destroy(cubeb_ctx.get(), &collection);
210 return devices;
211 }
212
213 const void* GetOutputDeviceById(std::string_view id)
214 {
215 if (id.empty())
216 return nullptr;
217
218 cubeb_device_collection collection;
219 auto cubeb_ctx = GetContext();
220 if (!cubeb_ctx)
221 return nullptr;
222
223 const int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_OUTPUT, &collection);
224 if (r != CUBEB_OK)
225 {
226 ERROR_LOG_FMT(AUDIO, "Error enumerating cubeb output devices");
227 return nullptr;
228 }
229
230 cubeb_devid device_id = nullptr;
231 for (uint32_t i = 0; i < collection.count; i++)
232 {
233 const auto& info = collection.device[i];
234 if (info.device_id && id.compare(info.device_id) == 0)
235 {
236 device_id = info.devid;
237 break;
238 }
239 }
240
241 if (device_id == nullptr)
242 {
243 WARN_LOG_FMT(AUDIO, "Failed to find selected output device, defaulting to system preferences");
244 }
245
246 cubeb_device_collection_destroy(cubeb_ctx.get(), &collection);
247 return device_id;
248 }
249
250 CoInitSyncWorker::CoInitSyncWorker([[maybe_unused]] std::string worker_name)
251 #ifdef _WIN32
252 : m_work_queue{std::move(worker_name)}
253 #endif
254 {
255 #ifdef _WIN32
256 m_work_queue.PushBlocking([this] {
257 const auto result = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
258 m_coinit_success = result == S_OK;
259 m_should_couninit = m_coinit_success || result == S_FALSE;
260 });
261 #endif
262 }
263
264 CoInitSyncWorker::~CoInitSyncWorker()
265 {
266 #ifdef _WIN32
267 if (m_should_couninit)
268 {
269 m_work_queue.PushBlocking([this] {
270 m_should_couninit = false;
271 CoUninitialize();
272 });
273 }
274 m_coinit_success = false;
275 #endif
276 }
277
278 bool CoInitSyncWorker::Execute(FunctionType f)
279 {
280 #ifdef _WIN32
281 if (!m_coinit_success)
282 return false;
283
284 m_work_queue.PushBlocking(std::move(f));
285 #else
286 f();
287 #endif
288 return true;
289 }
290 } // namespace CubebUtils