Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2025 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "audiodrivercontroller.h"
24 :
25 : #include "common/audiotaskscheduler.h"
26 : #include "common/iaudiotaskscheduler.h"
27 : #include "global/async/async.h"
28 :
29 : #include "audio/driver/platform/dummy/dummyaudiodriver.h"
30 :
31 : #include "log.h"
32 :
33 : using namespace muse;
34 : using namespace muse::audio;
35 : using namespace muse::audio::rpc;
36 :
37 24 : AudioDriverController::AudioDriverController()
38 48 : : m_audioTaskScheduler(std::make_shared<AudioTaskScheduler>())
39 : {
40 48 : }
41 :
42 1 : std::vector<std::string> AudioDriverController::availableAudioDrivers() const
43 : {
44 1 : return audioDriverFactory()->availableAudioDrivers();
45 : }
46 :
47 27 : void AudioDriverController::setNewDriver(IAudioDriverPtr newDriver)
48 : {
49 27 : if (m_audioDriver) {
50 : // unsubscribe
51 4 : m_audioDriver->availableOutputDevicesChanged().disconnect(this);
52 4 : m_audioDriver->activeSpecChanged().disconnect(this);
53 : }
54 :
55 27 : m_audioDriver = newDriver;
56 :
57 27 : if (m_audioDriver) {
58 : // subscribe
59 27 : m_audioDriver->availableOutputDevicesChanged().onNotify(this, [this]() {
60 6 : async::Async::call(this, [this]() {
61 6 : LOGI() << "Available output devices changed, checking connection...";
62 6 : handleOutputDeviceChange();
63 6 : m_availableOutputDevicesChanged.notify();
64 6 : });
65 6 : });
66 :
67 27 : m_audioDriver->activeSpecChanged().onReceive(this, [this](const IAudioDriver::Spec& spec) {
68 34 : if (spec.isValid()) {
69 34 : m_lastValidSpec = spec;
70 : } else {
71 0 : LOGW() << "Driver reported an invalid spec";
72 : }
73 :
74 34 : m_activeSpecChanged.send(spec);
75 :
76 34 : async::Async::call(this, [this, spec]() {
77 34 : configuration()->setAudioOutputDeviceId(spec.deviceId);
78 :
79 34 : m_outputDeviceChanged.notify();
80 34 : m_outputDeviceBufferSizeChanged.notify();
81 34 : m_outputDeviceSampleRateChanged.notify();
82 :
83 34 : updateOutputSpec();
84 34 : });
85 34 : });
86 : }
87 :
88 27 : auto audioWorkgroupSource = std::dynamic_pointer_cast<AudioTaskScheduler>(m_audioTaskScheduler);
89 27 : if (audioWorkgroupSource) {
90 27 : audioWorkgroupSource->setAudioDriver(m_audioDriver);
91 : }
92 27 : }
93 :
94 0 : std::string AudioDriverController::currentAudioDriverName() const
95 : {
96 0 : return configuration()->currentAudioDriverName();
97 : }
98 :
99 27 : IAudioDriverPtr AudioDriverController::createDriver(const std::string& name) const
100 : {
101 27 : if (configuration()->isOutputDisabled()) {
102 0 : return audioDriverFactory()->createDriver(DummyAudioDriver::NAME);
103 : }
104 :
105 27 : return audioDriverFactory()->createDriver(name);
106 : }
107 :
108 2 : void AudioDriverController::changeCurrentAudioDriver(const std::string& name)
109 : {
110 2 : IF_ASSERT_FAILED(m_audioDriver) {
111 0 : return;
112 : }
113 :
114 2 : if (m_audioDriver->isOpened()) {
115 2 : m_audioDriver->close();
116 : }
117 :
118 2 : IAudioDriverPtr driver = createDriver(name);
119 2 : setNewDriver(driver);
120 2 : m_audioDriver->init();
121 2 : LOGI() << "Used audio driver: " << m_audioDriver->name();
122 :
123 : // reset to default
124 2 : IAudioDriver::Spec spec;
125 2 : spec.output = configuration()->defaultOutputSpec();
126 2 : spec.callback = m_callback;
127 :
128 2 : if (m_audioDriver && !m_audioDriver->defaultDevice().empty()) {
129 1 : spec.deviceId = DEFAULT_DEVICE_ID;
130 1 : bool ok = m_audioDriver->open(spec, nullptr);
131 1 : if (!ok) {
132 0 : LOGE() << "Failed to open audio driver: " << name;
133 : }
134 : } else {
135 1 : LOGW() << "No devices for " << name;
136 : }
137 :
138 2 : configuration()->setCurrentAudioDriverName(name);
139 2 : m_currentAudioDriverChanged.notify();
140 2 : m_availableOutputDevicesChanged.notify();
141 2 : }
142 :
143 5 : async::Notification AudioDriverController::currentAudioDriverChanged() const
144 : {
145 5 : return m_currentAudioDriverChanged;
146 : }
147 :
148 0 : AudioDeviceList AudioDriverController::availableOutputDevices() const
149 : {
150 0 : IF_ASSERT_FAILED(m_audioDriver) {
151 0 : return {};
152 : }
153 0 : return m_audioDriver->availableOutputDevices();
154 : }
155 :
156 3 : async::Notification AudioDriverController::availableOutputDevicesChanged() const
157 : {
158 3 : return m_availableOutputDevicesChanged;
159 : }
160 :
161 23 : bool AudioDriverController::open(const IAudioDriver::Spec& spec, IAudioDriver::Spec* activeSpec)
162 : {
163 23 : m_callback = spec.callback;
164 :
165 23 : const std::string currentAudioDriverName = configuration()->currentAudioDriverName();
166 23 : IAudioDriverPtr driver = createDriver(currentAudioDriverName);
167 23 : driver->init();
168 23 : setNewDriver(driver);
169 :
170 23 : LOGI() << "Trying to open audio driver: " << m_audioDriver->name() << ", " << spec;
171 23 : bool ok = m_audioDriver->open(spec, activeSpec);
172 23 : if (!ok) {
173 : // reset to default device
174 4 : IAudioDriver::Spec defaultDeviceSpec = spec;
175 4 : defaultDeviceSpec.deviceId = m_audioDriver->defaultDevice();
176 4 : LOGW() << "Failed to open device: " << spec.deviceId << ", falling back to default: " << defaultDeviceSpec.deviceId;
177 4 : ok = m_audioDriver->open(defaultDeviceSpec, activeSpec);
178 4 : }
179 :
180 23 : if (!ok) {
181 3 : ok = switchToDefaultAudioDriver(activeSpec);
182 : }
183 :
184 23 : if (ok) {
185 21 : LOGI() << "Opened audio driver: " << m_audioDriver->name() << ", " << m_audioDriver->activeSpec();
186 : } else {
187 2 : LOGE() << "Failed to open any audio driver, last tried: " << m_audioDriver->name();
188 : }
189 :
190 23 : return ok;
191 23 : }
192 :
193 4 : void AudioDriverController::close()
194 : {
195 4 : if (m_audioDriver) {
196 4 : m_audioDriver->close();
197 : }
198 4 : }
199 :
200 24 : bool AudioDriverController::isOpened() const
201 : {
202 24 : IF_ASSERT_FAILED(m_audioDriver) {
203 0 : return false;
204 : }
205 24 : return m_audioDriver->isOpened();
206 : }
207 :
208 15 : const IAudioDriver::Spec& AudioDriverController::activeSpec() const
209 : {
210 15 : IF_ASSERT_FAILED(m_audioDriver) {
211 0 : static IAudioDriver::Spec dummy;
212 0 : return dummy;
213 : }
214 15 : return m_audioDriver->activeSpec();
215 : }
216 :
217 0 : async::Channel<IAudioDriver::Spec> AudioDriverController::activeSpecChanged() const
218 : {
219 0 : return m_activeSpecChanged;
220 : }
221 :
222 10 : AudioDeviceID AudioDriverController::outputDevice() const
223 : {
224 10 : IF_ASSERT_FAILED(m_audioDriver) {
225 0 : return AudioDeviceID();
226 : }
227 10 : return m_audioDriver->activeSpec().deviceId;
228 : }
229 :
230 5 : bool AudioDriverController::selectOutputDevice(const AudioDeviceID& deviceId)
231 : {
232 5 : IF_ASSERT_FAILED(m_audioDriver) {
233 0 : return false;
234 : }
235 :
236 : // Audio has never been started yet
237 5 : if (!m_audioDriver->isOpened() && !m_lastValidSpec.isValid()) {
238 1 : configuration()->setAudioOutputDeviceId(deviceId);
239 1 : return true;
240 : }
241 :
242 4 : IAudioDriver::Spec spec;
243 4 : spec.deviceId = deviceId;
244 4 : spec.callback = m_callback;
245 4 : spec.output = configuration()->desiredOutputSpec();
246 :
247 4 : LOGI() << "Trying to change output device to " << spec;
248 :
249 4 : return reopenWithSpec(spec);
250 4 : }
251 :
252 3 : async::Notification AudioDriverController::outputDeviceChanged() const
253 : {
254 3 : return m_outputDeviceChanged;
255 : }
256 :
257 6 : void AudioDriverController::handleOutputDeviceChange()
258 : {
259 6 : if (!m_audioDriver->isOpened() && !m_retryOpenDevice) {
260 1 : return;
261 : }
262 :
263 : // Some drivers reset their internal spec to a blank state after a failed open attempt, so fall
264 : // back to the last spec that was actually confirmed to work
265 5 : IAudioDriver::Spec spec = m_audioDriver->activeSpec();
266 5 : if (!spec.isValid()) {
267 1 : if (m_lastValidSpec.isValid()) {
268 1 : LOGW() << "Active spec is invalid, using last valid spec";
269 1 : spec = m_lastValidSpec;
270 : } else {
271 0 : LOGW() << "Active spec is invalid, using default spec";
272 0 : spec.deviceId = DEFAULT_DEVICE_ID;
273 0 : spec.output = configuration()->defaultOutputSpec();
274 0 : spec.callback = m_callback;
275 : }
276 : }
277 :
278 5 : LOGI() << "Reopening output device, " << spec;
279 :
280 5 : m_audioDriver->close();
281 5 : bool ok = m_audioDriver->open(spec, nullptr);
282 5 : if (!ok) {
283 : // reset to default device
284 3 : LOGW() << "Failed to reopen device: " << spec.deviceId << ", falling back to default";
285 3 : spec.deviceId = DEFAULT_DEVICE_ID;
286 3 : ok = m_audioDriver->open(spec, nullptr);
287 3 : if (!ok) {
288 2 : LOGE() << "Failed to reopen default device on " << m_audioDriver->name() << ", switching to default audio driver";
289 2 : ok = switchToDefaultAudioDriver();
290 : }
291 : }
292 :
293 5 : if (ok) {
294 4 : LOGI() << "Reopened output device: " << m_audioDriver->activeSpec();
295 : } else {
296 1 : LOGE() << "Failed to reopen output device, will retry on next device list change";
297 : }
298 :
299 5 : m_retryOpenDevice = !ok;
300 5 : }
301 :
302 5 : bool AudioDriverController::switchToDefaultAudioDriver(IAudioDriver::Spec* activeSpec)
303 : {
304 5 : const std::string defaultAudioDriverName = configuration()->defaultAudioDriverName();
305 5 : const std::string currentAudioDriverName = configuration()->currentAudioDriverName();
306 :
307 5 : if (defaultAudioDriverName == currentAudioDriverName) {
308 3 : LOGE() << "Already on the default audio driver: " << defaultAudioDriverName << ", cannot fall back further";
309 3 : return false;
310 : }
311 :
312 2 : LOGW() << "Switching from " << currentAudioDriverName << " to default audio driver: " << defaultAudioDriverName;
313 :
314 2 : IAudioDriverPtr defaultDriver = createDriver(defaultAudioDriverName);
315 2 : defaultDriver->init();
316 2 : setNewDriver(defaultDriver);
317 :
318 2 : IAudioDriver::Spec defSpec;
319 2 : defSpec.deviceId = DEFAULT_DEVICE_ID;
320 2 : defSpec.output = configuration()->defaultOutputSpec();
321 2 : defSpec.callback = m_callback;
322 :
323 2 : bool ok = m_audioDriver->open(defSpec, activeSpec);
324 2 : if (ok) {
325 2 : configuration()->setCurrentAudioDriverName(defaultAudioDriverName);
326 2 : m_currentAudioDriverChanged.notify();
327 2 : LOGI() << "Successfully switched to default audio driver: " << defaultAudioDriverName;
328 : } else {
329 0 : LOGE() << "Failed to open default audio driver: " << defaultAudioDriverName;
330 : }
331 :
332 2 : return ok;
333 5 : }
334 :
335 8 : bool AudioDriverController::reopenWithSpec(const IAudioDriver::Spec& spec)
336 : {
337 : // A failed reopen can blank the driver's spec
338 8 : IAudioDriver::Spec oldSpec = m_audioDriver->activeSpec();
339 8 : if (!oldSpec.isValid()) {
340 2 : oldSpec = m_lastValidSpec;
341 : }
342 :
343 8 : m_audioDriver->close();
344 8 : if (m_audioDriver->open(spec, nullptr)) {
345 4 : LOGI() << "Reopened with: " << spec;
346 4 : m_retryOpenDevice = false;
347 4 : return true;
348 : }
349 :
350 4 : LOGE() << "Failed to reopen with: " << spec << ", returning to: " << oldSpec;
351 4 : if (m_audioDriver->open(oldSpec, nullptr)) {
352 4 : LOGI() << "Restored previous spec: " << oldSpec;
353 4 : m_retryOpenDevice = false;
354 : } else {
355 0 : LOGE() << "Failed to restore previous spec: " << oldSpec << ", will retry on next device list change";
356 0 : m_retryOpenDevice = true;
357 : }
358 :
359 4 : return false;
360 8 : }
361 :
362 38 : void AudioDriverController::updateOutputSpec()
363 : {
364 38 : if (!m_audioDriver->isOpened()) {
365 2 : return;
366 : }
367 :
368 36 : IAudioDriver::Spec activeSpec = m_audioDriver->activeSpec();
369 36 : rpcChannel()->send(rpc::make_request(rpc::GLOBAL_CTX_ID, MsgCode::SetOutputSpec, RpcPacker::pack(activeSpec.output)));
370 36 : }
371 :
372 0 : std::vector<samples_t> AudioDriverController::availableOutputDeviceBufferSizes() const
373 : {
374 0 : IF_ASSERT_FAILED(m_audioDriver) {
375 0 : return {};
376 : }
377 0 : return m_audioDriver->availableOutputDeviceBufferSizes();
378 : }
379 :
380 3 : void AudioDriverController::changeBufferSize(samples_t samples)
381 : {
382 3 : IF_ASSERT_FAILED(m_audioDriver) {
383 0 : return;
384 : }
385 :
386 3 : LOGI() << "Trying to change buffer size: " << samples;
387 3 : bool ok = true;
388 :
389 3 : if (m_audioDriver->isOpened()) {
390 2 : IAudioDriver::Spec spec = m_audioDriver->activeSpec();
391 2 : spec.output.samplesPerChannel = samples;
392 2 : ok = reopenWithSpec(spec);
393 2 : }
394 :
395 3 : if (ok) {
396 2 : updateOutputSpec();
397 2 : configuration()->setDriverBufferSize(samples);
398 2 : m_outputDeviceBufferSizeChanged.notify();
399 : } else {
400 1 : LOGE() << "Failed to change buffer size to: " << samples;
401 : }
402 : }
403 :
404 0 : async::Notification AudioDriverController::outputDeviceBufferSizeChanged() const
405 : {
406 0 : return m_outputDeviceBufferSizeChanged;
407 : }
408 :
409 0 : std::vector<sample_rate_t> AudioDriverController::availableOutputDeviceSampleRates() const
410 : {
411 0 : IF_ASSERT_FAILED(m_audioDriver) {
412 0 : return {};
413 : }
414 0 : return m_audioDriver->availableOutputDeviceSampleRates();
415 : }
416 :
417 3 : void AudioDriverController::changeSampleRate(sample_rate_t sampleRate)
418 : {
419 3 : IF_ASSERT_FAILED(m_audioDriver) {
420 0 : return;
421 : }
422 :
423 3 : LOGI() << "Trying to change sampleRate: " << sampleRate;
424 3 : bool ok = true;
425 :
426 3 : if (m_audioDriver->isOpened()) {
427 2 : IAudioDriver::Spec spec = m_audioDriver->activeSpec();
428 2 : spec.output.sampleRate = sampleRate;
429 2 : ok = reopenWithSpec(spec);
430 2 : }
431 :
432 3 : if (ok) {
433 2 : updateOutputSpec();
434 2 : configuration()->setSampleRate(sampleRate);
435 2 : m_outputDeviceSampleRateChanged.notify();
436 : } else {
437 1 : LOGE() << "Failed to change sample rate to: " << sampleRate;
438 : }
439 : }
440 :
441 0 : async::Notification AudioDriverController::outputDeviceSampleRateChanged() const
442 : {
443 0 : return m_outputDeviceSampleRateChanged;
444 : }
445 :
446 0 : IAudioTaskSchedulerPtr muse::audio::AudioDriverController::getAudioTaskScheduler() const
447 : {
448 0 : return m_audioTaskScheduler;
449 : }
|