Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
4
#
include
"qrtaudioengine_p.h"
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
19
QT_WARNING_PUSH
20
QT_WARNING_DISABLE_GCC(
"-Wmaybe-uninitialized"
)
21
#
endif
22
23
QT_BEGIN_NAMESPACE
24
25
namespace
QtMultimediaPrivate
{
26
27
using
namespace
QtPrivate
;
28
using
namespace
std
::chrono_literals;
29
30
static
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
46
QRtAudioEngine
::
QRtAudioEngine
(
const
QAudioDevice
&
device
,
const
QAudioFormat
&
format
,
47
std
::
optional
<
AudioEndpointRole
>
role
,
48
std
::
optional
<
NativePeriodFrames
>
nativePeriodFrames
)
49
:
QRtAudioEngine
{
MoveToApplicationThread
::
No
,
device
,
format
,
role
,
nativePeriodFrames
}
50
{
51
}
52
53
QRtAudioEngine
::
QRtAudioEngine
(
MoveToApplicationThread
moveToApplicationThread
,
54
const
QAudioDevice
&
device
,
const
QAudioFormat
&
format
,
55
std
::
optional
<
AudioEndpointRole
>
role
,
56
std
::
optional
<
NativePeriodFrames
>
nativePeriodFrames
)
57
:
m_sink
{
58
device
,
59
format
,
60
},
61
m_rtMemoryPool
{
62
std
::
make_unique
<
QTlsfMemoryResource
>(
poolSize
)
63
}
64
{
65
m_notificationEvent
.
callOnActivated
([
this
] {
66
runNonRtNotifications
();
67
});
68
69
if
(
moveToApplicationThread
==
MoveToApplicationThread
::
Yes
&& !
QThread
::
isMainThread
()) {
70
QThread
*
appThread
=
qApp
->
thread
();
71
moveToThread
(
appThread
);
72
m_sink
.
moveToThread
(
appThread
);
73
m_notificationEvent
.
moveToThread
(
appThread
);
74
m_pendingCommandsTimer
.
moveToThread
(
appThread
);
75
}
76
77
m_pendingCommandsTimer
.
setInterval
(10ms);
78
m_pendingCommandsTimer
.
setTimerType
(
Qt
::
CoarseTimer
);
79
m_pendingCommandsTimer
.
callOnTimeout
(&
m_pendingCommandsTimer
, [
this
] {
80
auto
lock
=
std
::
lock_guard
{
m_mutex
};
81
sendPendingRtCommands
();
82
if
(
m_appToRtOverflowBuffer
.
empty
())
83
m_pendingCommandsTimer
.
stop
();
84
});
85
86
QMetaObject
::
invokeMethod
(
this
, [
this
,
role
,
nativePeriodFrames
] {
87
QPlatformAudioSink
*
platformSink
=
QPlatformAudioSink
::
get
(
m_sink
);
88
89
if
(
role
)
90
platformSink
->
setRole
(*
role
);
91
if
(
nativePeriodFrames
)
92
platformSink
->
setNativePeriodFrames
(*
nativePeriodFrames
);
93
94
m_sink
.
start
([
this
](
QSpan
<
float
>
outputBuffer
) {
95
audioCallback
(
outputBuffer
);
96
});
97
98
// we start suspended
99
m_sink
.
suspend
();
100
101
QObject
::
connect
(&
m_sink
, &
QAudioSink
::
stateChanged
,
this
, [&](
QtAudio
::
State
state
) {
102
Q_EMIT
stateChanged
(
sinkStateToEngineState
(
state
));
103
});
104
});
105
}
106
107
QRtAudioEngine
::~
QRtAudioEngine
()
108
{
109
m_sink
.
reset
();
110
111
// consume the ringbuffers
112
m_appToRt
.
consumeAll
([](
auto
) {
113
});
114
m_rtToApp
.
consumeAll
([](
auto
) {
115
});
116
}
117
118
void
QRtAudioEngine
::
play
(
SharedVoice
voice
)
119
{
120
auto
lock
=
std
::
lock_guard
{
m_mutex
};
121
122
// TODO: where do we expect reampling to happen?
123
Q_ASSERT
(
voice
->
format
() ==
m_sink
.
format
());
124
125
if
(
m_voices
.
empty
()) {
126
QMetaObject
::
invokeMethod
(
this
, [
this
] {
127
m_sink
.
resume
();
128
});
129
}
130
131
m_voices
.
insert
(
voice
);
132
133
sendAppToRtCommand
(
PlayCommand
{
134
std
::
move
(
voice
),
135
});
136
}
137
138
void
QRtAudioEngine
::
stop
(
const
SharedVoice
&
voice
)
139
{
140
stop
(
voice
->
voiceId
());
141
}
142
143
void
QRtAudioEngine
::
release
(
SharedVoice
voice
)
144
{
145
if
(
thread
()->
isCurrentThread
())
146
return
;
147
148
QMetaObject
::
invokeMethod
(
this
, [
voice
=
std
::
move
(
voice
)] {
149
// release the voice on the owning thread
150
});
151
}
152
153
void
QRtAudioEngine
::
stop
(
VoiceId
voiceId
)
154
{
155
auto
lock
=
std
::
lock_guard
{
m_mutex
};
156
sendAppToRtCommand
(
StopCommand
{
voiceId
});
157
}
158
159
void
QRtAudioEngine
::
visitVoiceRt
(
VoiceId
voiceId
,
RtVoiceVisitor
fn
,
bool
visitorIsTrivial
)
160
{
161
auto
lock
=
std
::
lock_guard
{
m_mutex
};
162
163
if
(
visitorIsTrivial
) {
164
sendAppToRtCommand
(
VisitCommandTrivial
{
165
voiceId
,
166
std
::
move
(
fn
),
167
});
168
169
}
else
{
170
sendAppToRtCommand
(
VisitCommand
{
171
voiceId
,
172
std
::
move
(
fn
),
173
});
174
}
175
}
176
177
VoiceId
QRtAudioEngine
::
allocateVoiceId
()
178
{
179
static
std
::
atomic_uint64_t
allocator
{ 0 };
180
return
VoiceId
{
allocator
.
fetch_add
(1,
std
::
memory_order_relaxed
) };
181
}
182
183
QtAudio
::
State
QRtAudioEngine
::
audioState
()
const
184
{
185
return
sinkStateToEngineState
(
m_sink
.
state
());
186
}
187
188
void
QRtAudioEngine
::
audioCallback
(
QSpan
<
float
>
outputBuffer
)
noexcept
Q_DECL_NONBLOCKING_FUNCTION
189
{
190
runRtCommands
();
191
bool
sendNotification
=
sendPendingAppNotifications
();
192
193
std
::
fill
(
outputBuffer
.
begin
(),
outputBuffer
.
end
(), 0.f);
194
195
std
::
vector
<
SharedVoice
,
pmr
::
polymorphic_allocator
<
SharedVoice
>>
finishedVoices
{
196
m_rtMemoryPool
.
get
(),
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
202
VoicePlayResult
playResult
=
voice
->
play
(
outputBuffer
);
203
if
(
playResult
==
VoicePlayResult
::
Finished
)
204
finishedVoices
.
push_back
(
voice
);
205
}
206
207
if
(!
finishedVoices
.
empty
()) {
208
withRTSanDisabled
([&] {
209
for
(
SharedVoice
&
voice
:
finishedVoices
) {
210
m_rtVoiceRegistry
.
erase
(
voice
);
211
bool
stopSent
=
sendRtToAppNotification
(
StopNotification
{
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
222
bool
voicesRetired
=
cleanupRetiredVoices
();
223
if
(
sendNotification
||
voicesRetired
)
224
m_notificationEvent
.
set
();
225
}
226
227
bool
QRtAudioEngine
::
cleanupRetiredVoices
()
noexcept
Q_DECL_NONBLOCKING_FUNCTION
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
245
withRTSanDisabled
([&] {
246
pmr
::
vector
<
SharedVoice
>
finishedVoices
{
247
m_rtMemoryPool
.
get
(),
248
};
249
250
erase_if
(
m_rtVoiceRegistry
, [&](
const
SharedVoice
&
voice
) {
251
if
(
voice
->
isActive
())
252
return
false
;
253
254
finishedVoices
.
push_back
(
voice
);
255
return
true
;
256
});
257
258
for
(
SharedVoice
&
voice
:
finishedVoices
) {
259
bool
stopSent
=
sendRtToAppNotification
(
StopNotification
{
260
std
::
move
(
voice
),
261
});
262
if
(
stopSent
)
263
notifyApp
=
true
;
264
}
265
});
266
267
return
notifyApp
;
268
}
269
270
void
QRtAudioEngine
::
runRtCommands
()
noexcept
Q_DECL_NONBLOCKING_FUNCTION
271
{
272
m_appToRt
.
consumeAll
([&](
QSpan
<
RtCommand
>
commands
) {
273
for
(
RtCommand
&
cmd
:
commands
) {
274
std
::
visit
([&](
auto
cmd
) {
275
runRtCommand
(
std
::
move
(
cmd
));
276
},
std
::
move
(
cmd
));
277
}
278
});
279
}
280
281
void
QRtAudioEngine
::
runRtCommand
(
PlayCommand
cmd
)
noexcept
Q_DECL_NONBLOCKING_FUNCTION
282
{
283
withRTSanDisabled
([&] {
284
m_rtVoiceRegistry
.
insert
(
cmd
.
voice
);
285
});
286
}
287
288
void
QRtAudioEngine
::
runRtCommand
(
StopCommand
cmd
)
noexcept
Q_DECL_NONBLOCKING_FUNCTION
289
{
290
auto
it
=
m_rtVoiceRegistry
.
find
(
cmd
.
voiceId
);
291
if
(
it
==
m_rtVoiceRegistry
.
end
())
292
return
;
293
294
SharedVoice
voice
= *
it
;
295
m_rtVoiceRegistry
.
erase
(
it
);
296
297
bool
emitNotify
=
sendRtToAppNotification
(
StopNotification
{
298
std
::
move
(
voice
),
299
});
300
if
(
emitNotify
)
301
m_notificationEvent
.
set
();
302
}
303
304
void
QRtAudioEngine
::
runRtCommand
(
VisitCommand
cmd
)
noexcept
Q_DECL_NONBLOCKING_FUNCTION
305
{
306
auto
it
=
m_rtVoiceRegistry
.
find
(
cmd
.
voiceId
);
307
if
(
it
==
m_rtVoiceRegistry
.
end
())
308
return
;
309
310
cmd
.
callback
(**
it
);
311
312
// send callback back to application for destruction
313
bool
emitNotify
=
sendRtToAppNotification
(
VisitReply
{
314
std
::
move
(
cmd
.
callback
),
315
});
316
if
(
emitNotify
)
317
m_notificationEvent
.
set
();
318
}
319
320
void
QRtAudioEngine
::
runRtCommand
(
VisitCommandTrivial
cmd
)
noexcept
Q_DECL_NONBLOCKING_FUNCTION
321
{
322
auto
it
=
m_rtVoiceRegistry
.
find
(
cmd
.
voiceId
);
323
if
(
it
==
m_rtVoiceRegistry
.
end
())
324
return
;
325
326
cmd
.
callback
(**
it
);
327
}
328
329
void
QRtAudioEngine
::
runNonRtNotifications
()
330
{
331
std
::
vector
<
VoiceId
>
finishedVoices
;
332
{
333
auto
lock
=
std
::
lock_guard
{
m_mutex
};
334
m_rtToApp
.
consumeAll
([&](
QSpan
<
Notification
>
notifications
) {
335
for
(
Notification
&
notification
:
notifications
) {
336
std
::
visit
([&](
auto
notification
) {
337
runNonRtNotification
(
std
::
move
(
notification
));
338
},
std
::
move
(
notification
));
339
}
340
});
341
342
finishedVoices
=
std
::
move
(
m_finishedVoices
);
343
m_finishedVoices
.
clear
();
344
}
345
346
// emit voiceFinished outside of the lock
347
for
(
VoiceId
voiceId
:
finishedVoices
)
348
Q_EMIT
voiceFinished
(
voiceId
);
349
}
350
351
void
QRtAudioEngine
::
runNonRtNotification
(
StopNotification
notification
)
352
{
353
m_voices
.
erase
(
notification
.
voice
);
354
if
(
m_voices
.
empty
())
355
m_sink
.
suspend
();
356
m_finishedVoices
.
push_back
(
notification
.
voice
->
voiceId
());
357
}
358
359
void
QRtAudioEngine
::
runNonRtNotification
(
VisitReply
)
360
{
361
// nop (just making sure to delete on the application thread);
362
}
363
364
void
QRtAudioEngine
::
sendAppToRtCommand
(
RtCommand
cmd
)
365
{
366
// first write all pending commands from overflow buffer
367
sendPendingRtCommands
();
368
369
bool
written
=
m_appToRt
.
produceOne
([&] {
370
return
std
::
move
(
cmd
);
371
});
372
373
if
(
written
)
374
return
;
375
376
m_appToRtOverflowBuffer
.
emplace_back
(
std
::
move
(
cmd
));
377
378
QMetaObject
::
invokeMethod
(&
m_pendingCommandsTimer
, [
this
] {
379
if
(!
m_pendingCommandsTimer
.
isActive
())
380
m_pendingCommandsTimer
.
start
();
381
});
382
}
383
384
bool
QRtAudioEngine
::
sendRtToAppNotification
(
Notification
cmd
)
385
{
386
// first write all pending commands from overflow buffer
387
bool
emitNotification
=
sendPendingAppNotifications
();
388
389
bool
written
=
m_rtToApp
.
produceOne
([&] {
390
return
std
::
move
(
cmd
);
391
});
392
393
if
(
written
)
394
return
true
;
395
396
m_rtToAppOverflowBuffer
.
emplace_back
(
std
::
move
(
cmd
));
397
398
return
emitNotification
;
399
}
400
401
void
QRtAudioEngine
::
sendPendingRtCommands
()
402
{
403
while
(!
m_appToRtOverflowBuffer
.
empty
()) {
404
Q_UNLIKELY_BRANCH
;
405
bool
written
=
m_appToRt
.
produceOne
([&] {
406
return
std
::
move
(
m_appToRtOverflowBuffer
.
front
());
407
});
408
if
(!
written
)
409
return
;
410
411
m_appToRtOverflowBuffer
.
pop_front
();
412
}
413
}
414
415
bool
QRtAudioEngine
::
sendPendingAppNotifications
()
416
{
417
bool
emitNotification
=
false
;
418
while
(!
m_rtToAppOverflowBuffer
.
empty
()) {
419
Q_UNLIKELY_BRANCH
;
420
421
// first write all pending commands from overflow buffer
422
bool
written
=
m_rtToApp
.
produceOne
([&] {
423
return
std
::
move
(
m_rtToAppOverflowBuffer
.
front
());
424
});
425
if
(!
written
)
426
break
;
427
428
m_rtToAppOverflowBuffer
.
pop_front
();
429
emitNotification
=
true
;
430
}
431
432
return
emitNotification
;
433
}
434
435
}
// namespace QtMultimediaPrivate
436
437
QT_END_NAMESPACE
438
439
#
ifdef
Q_CC_MINGW
440
QT_WARNING_POP
441
#
endif
442
443
#
include
"moc_qrtaudioengine_p.cpp"
QtMultimediaPrivate::impl
Definition
qaudiosystem_p.h:257
QtMultimediaPrivate::sinkStateToEngineState
static QtAudio::State sinkStateToEngineState(QtAudio::State state)
Definition
qrtaudioengine.cpp:30
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtmultimedia
src
multimedia
audio
qrtaudioengine.cpp
Generated on
for Qt by
1.18.0