Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qrtaudioengine.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtMultimedia/private/qaudiosystem_p.h>
7#include <QtMultimedia/private/qmemory_resource_tlsf_p.h>
8#include <QtMultimedia/private/qtmm_rtsan_support_p.h>
9#include <QtCore/q20map.h>
10#include <QtCore/qcoreapplication.h>
11#include <QtCore/qdebug.h>
12#include <QtCore/qmutex.h>
13#include <QtCore/qthread.h>
14
15#include <mutex>
16
17#ifdef Q_CC_MINGW
18// mingw-13.1 seems to have a false positive when using std::function inside a std::variant
19QT_WARNING_PUSH
20QT_WARNING_DISABLE_GCC("-Wmaybe-uninitialized")
21#endif
22
23QT_BEGIN_NAMESPACE
24
25namespace QtMultimediaPrivate {
26
27using namespace QtPrivate;
28using namespace std::chrono_literals;
29
30static QtAudio::State sinkStateToEngineState(QtAudio::State state)
31{
32 switch (state) {
33 case QtAudio::ActiveState:
34 case QtAudio::IdleState:
35 case QtAudio::SuspendedState:
36 return QtAudio::ActiveState;
37 case QtAudio::StoppedState:
38 return QtAudio::StoppedState;
39 default:
40 Q_UNREACHABLE_RETURN(QtAudio::StoppedState);
41 }
42}
43
44///////////////////////////////////////////////////////////////////////////////////////////////////
45
52
57 : m_sink{
58 device,
59 format,
60 },
63 }
64{
67 });
68
75 }
76
80 auto lock = std::lock_guard{ m_mutex };
84 });
85
88
89 if (role)
93
94 m_sink.start([this](QSpan<float> outputBuffer) {
96 });
97
98 // we start suspended
100
103 });
104 });
105}
106
108{
109 m_sink.reset();
110
111 // consume the ringbuffers
112 m_appToRt.consumeAll([](auto) {
113 });
114 m_rtToApp.consumeAll([](auto) {
115 });
116}
117
119{
120 auto lock = std::lock_guard{ m_mutex };
121
122 // TODO: where do we expect reampling to happen?
124
125 if (m_voices.empty()) {
126 QMetaObject::invokeMethod(this, [this] {
127 m_sink.resume();
128 });
129 }
130
132
134 std::move(voice),
135 });
136}
137
139{
140 stop(voice->voiceId());
141}
142
144{
145 if (thread()->isCurrentThread())
146 return;
147
149 // release the voice on the owning thread
150 });
151}
152
158
160{
161 auto lock = std::lock_guard{ m_mutex };
162
163 if (visitorIsTrivial) {
165 voiceId,
166 std::move(fn),
167 });
168
169 } else {
171 voiceId,
172 std::move(fn),
173 });
174 }
175}
176
182
187
189{
192
194
197 };
198
199 for (const SharedVoice &voice : m_rtVoiceRegistry) {
200 Q_ASSERT(voice.use_count() >= 2); // voice in both m_rtVoiceRegistry and m_voices
201
205 }
206
207 if (!finishedVoices.empty()) {
212 std::move(voice),
213 });
214 if (stopSent)
215 sendNotification = true;
216 }
217 });
218 }
219
220 // TODO: we should probably (soft)clip the output buffer
221
225}
226
228{
229 bool notifyApp = false;
230
231#if __cpp_lib_erase_if >= 202002L
232 using std::erase_if;
233#else
234 auto erase_if = [](auto &c, auto &&pred) {
235 auto old_size = c.size();
236 for (auto first = c.begin(), last = c.end(); first != last;) {
237 if (pred(*first))
238 first = c.erase(first);
239 else
240 ++first;
241 }
242 return old_size - c.size();
243 };
244#endif
248 };
249
251 if (voice->isActive())
252 return false;
253
255 return true;
256 });
257
260 std::move(voice),
261 });
262 if (stopSent)
263 notifyApp = true;
264 }
265 });
266
267 return notifyApp;
268}
269
271{
273 for (RtCommand &cmd : commands) {
274 std::visit([&](auto cmd) {
276 }, std::move(cmd));
277 }
278 });
279}
280
282{
285 });
286}
287
289{
291 if (it == m_rtVoiceRegistry.end())
292 return;
293
296
298 std::move(voice),
299 });
300 if (emitNotify)
302}
303
305{
307 if (it == m_rtVoiceRegistry.end())
308 return;
309
310 cmd.callback(**it);
311
312 // send callback back to application for destruction
315 });
316 if (emitNotify)
318}
319
321{
323 if (it == m_rtVoiceRegistry.end())
324 return;
325
326 cmd.callback(**it);
327}
328
330{
332 {
333 auto lock = std::lock_guard{ m_mutex };
336 std::visit([&](auto notification) {
338 }, std::move(notification));
339 }
340 });
341
344 }
345
346 // emit voiceFinished outside of the lock
349}
350
352{
354 if (m_voices.empty())
355 m_sink.suspend();
357}
358
360{
361 // nop (just making sure to delete on the application thread);
362}
363
365{
366 // first write all pending commands from overflow buffer
368
369 bool written = m_appToRt.produceOne([&] {
370 return std::move(cmd);
371 });
372
373 if (written)
374 return;
375
377
381 });
382}
383
385{
386 // first write all pending commands from overflow buffer
388
389 bool written = m_rtToApp.produceOne([&] {
390 return std::move(cmd);
391 });
392
393 if (written)
394 return true;
395
397
398 return emitNotification;
399}
400
402{
403 while (!m_appToRtOverflowBuffer.empty()) {
405 bool written = m_appToRt.produceOne([&] {
407 });
408 if (!written)
409 return;
410
412 }
413}
414
416{
417 bool emitNotification = false;
418 while (!m_rtToAppOverflowBuffer.empty()) {
420
421 // first write all pending commands from overflow buffer
422 bool written = m_rtToApp.produceOne([&] {
424 });
425 if (!written)
426 break;
427
429 emitNotification = true;
430 }
431
432 return emitNotification;
433}
434
435} // namespace QtMultimediaPrivate
436
437QT_END_NAMESPACE
438
439#ifdef Q_CC_MINGW
440QT_WARNING_POP
441#endif
442
443#include "moc_qrtaudioengine_p.cpp"
static QtAudio::State sinkStateToEngineState(QtAudio::State state)