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
qplatformaudiodevices.cpp
Go to the documentation of this file.
1
// Copyright (C) 2021 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
"qplatformaudiodevices_p.h"
5
6
#
include
<
QtMultimedia
/
private
/
qaudiosystem_p
.
h
>
7
#
include
<
QtMultimedia
/
qaudiodevice
.
h
>
8
#
include
<
QtMultimedia
/
qmediadevices
.
h
>
9
#
include
<
QtCore
/
private
/
qminimalflatset_p
.
h
>
10
#
include
<
QtCore
/
qdebug
.
h
>
11
#
include
<
QtCore
/
qvarlengtharray
.
h
>
12
13
#
include
<
optional
>
14
15
#
if
defined
(
Q_OS_ANDROID
)
16
#
include
<
QtMultimedia
/
private
/
qandroidaudiodevices_p
.
h
>
17
#
endif
18
#
if
QT_CONFIG
(
ohaudio
)
19
#
include
<
QtMultimedia
/
private
/
qohosaudiodevices_p
.
h
>
20
#
endif
21
#
if
defined
(
Q_OS_DARWIN
)
22
#
include
<
QtMultimedia
/
private
/
qcoreaudiodevices_p
.
h
>
23
#
endif
24
#
if
defined
(
Q_OS_WINDOWS
)
25
#
include
<
QtMultimedia
/
private
/
qwindowsaudiodevices_p
.
h
>
26
#
endif
27
#
if
QT_CONFIG
(
alsa
)
28
#
include
<
QtMultimedia
/
private
/
qalsaaudiodevices_p
.
h
>
29
#
endif
30
#
if
QT_CONFIG
(
pulseaudio
)
31
#
include
<
QtMultimedia
/
private
/
qpulseaudiodevices_p
.
h
>
32
#
include
<
QtMultimedia
/
private
/
qpulsehelpers_p
.
h
>
33
#
endif
34
#
if
QT_CONFIG
(
pipewire
)
35
#
include
<
QtMultimedia
/
private
/
qpipewire_audiodevices_p
.
h
>
36
#
endif
37
#
if
defined
(
Q_OS_QNX
)
&&
defined
(
QT_MM_QNX_IO_SND
)
38
#
include
<
QtMultimedia
/
private
/
qqnxsndaudiodevices_p
.
h
>
39
#
endif
40
#
if
defined
(
Q_OS_QNX
)
&&
defined
(
QT_MM_QNX_IO_AUDIO
)
41
#
include
<
QtMultimedia
/
private
/
qqnxaudiodevices_p
.
h
>
42
#
endif
43
#
if
defined
(
Q_OS_WASM
)
44
#
include
<
QtMultimedia
/
private
/
qwasmmediadevices_p
.
h
>
45
#
endif
46
47
QT_BEGIN_NAMESPACE
48
49
using
namespace
Qt::Literals;
50
51
#
if
(
defined
(
Q_OS_LINUX
)
&&
!
defined
(
Q_OS_ANDROID
)
&&
!
defined
(
Q_OS_HARMONY
)
)
52
53
namespace
{
54
55
enum
class
AudioBackend : uint8_t {
56
PipeWire,
57
PulseAudio,
58
ALSA,
59
Unavailable,
60
};
61
62
std::optional<AudioBackend> requestedAudioBackend()
63
{
64
const
QByteArray requested = qgetenv(
"QT_AUDIO_BACKEND"
).toLower();
65
if
(requested.isEmpty())
66
return
std::nullopt;
67
#
if
QT_CONFIG
(
pipewire
)
68
if
(requested ==
"pipewire"_ba
) {
69
if
(QtPipeWire::QAudioDevices::isSupported())
70
return
AudioBackend::PipeWire;
71
qWarning() <<
"Audio backend pipewire requested, but not supported on this system."
72
<<
"Using default backend"
;
73
return
std::nullopt;
74
}
75
#
endif
76
#
if
QT_CONFIG
(
pulseaudio
)
77
if
(requested ==
"pulseaudio"_ba
)
78
return
AudioBackend::PulseAudio;
79
#
endif
80
#
if
QT_CONFIG
(
alsa
)
81
if
(requested ==
"alsa"_ba
)
82
return
AudioBackend::ALSA;
83
#
endif
84
qWarning() <<
"Audio backend"
<< requested <<
"requested, but not available."
85
<<
"Using default backend"
;
86
return
std::nullopt;
87
}
88
89
// Some distros still ship PipeWire and the vanilla PulseAudio server (not PipeWire's PulseAudio
90
// server) side by side. In that case, PipeWire might not report any audio devices.
91
[[maybe_unused]]
bool
isVanillaPulseAudioServerRunning()
92
{
93
#
if
QT_CONFIG
(
pulseaudio
)
94
using
namespace
QPulseAudioInternal;
95
return
pulseaudioDetectServerType() == PulseaudioServerType::Pulseaudio;
96
#
else
97
return
false
;
98
#
endif
99
}
100
101
AudioBackend selectedAudioBackend()
102
{
103
if
(
const
std::optional<AudioBackend> requested = requestedAudioBackend())
104
return
*requested;
105
106
#
if
QT_CONFIG
(
pipewire
)
107
if
(QtPipeWire::QAudioDevices::isSupported() && !isVanillaPulseAudioServerRunning())
108
return
AudioBackend::PipeWire;
109
#
endif
110
#
if
QT_CONFIG
(
pulseaudio
)
111
return
AudioBackend::PulseAudio;
112
#
endif
113
#
if
QT_CONFIG
(
alsa
)
114
return
AudioBackend::ALSA;
115
#
endif
116
return
AudioBackend::Unavailable;
117
}
118
119
}
// namespace
120
121
#
endif
122
123
std::unique_ptr<QPlatformAudioDevices> QPlatformAudioDevices::create()
124
{
125
#
ifdef
Q_OS_DARWIN
126
return
std::make_unique<QCoreAudioDevices>();
127
#
endif
128
#
if
defined
(
Q_OS_WINDOWS
)
129
return
std::make_unique<QWindowsAudioDevices>();
130
#
endif
131
#
if
defined
(
Q_OS_ANDROID
)
132
return
std::make_unique<QAndroidAudioDevices>();
133
#
endif
134
#
if
QT_CONFIG
(
ohaudio
)
135
return
std::make_unique<QOhosAudioDevices>();
136
#
endif
137
#
if
(
defined
(
Q_OS_LINUX
)
&&
!
defined
(
Q_OS_ANDROID
)
&&
!
defined
(
Q_OS_HARMONY
)
)
138
switch
(selectedAudioBackend()) {
139
#
if
QT_CONFIG
(
pipewire
)
140
case
AudioBackend::PipeWire:
141
return
std::make_unique<QtPipeWire::QAudioDevices>();
142
#
endif
143
#
if
QT_CONFIG
(
pulseaudio
)
144
case
AudioBackend::PulseAudio:
145
return
std::make_unique<QPulseAudioDevices>();
146
#
endif
147
#
if
QT_CONFIG
(
alsa
)
148
case
AudioBackend::ALSA:
149
return
std::make_unique<QAlsaAudioDevices>();
150
#
endif
151
default
:
152
break
;
153
}
154
#
endif
155
#
if
defined
(
Q_OS_QNX
)
&&
defined
(
QT_MM_QNX_IO_SND
)
156
return
std::make_unique<QQnxSndAudioDevices>();
157
#
endif
158
#
if
defined
(
Q_OS_QNX
)
&&
defined
(
QT_MM_QNX_IO_AUDIO
)
159
return
std::make_unique<QQnxAudioDevices>();
160
#
endif
161
#
if
defined
(
Q_OS_WASM
)
162
return
std::make_unique<QWasmAudioDevices>();
163
#
endif
164
qWarning() <<
"No audio backend available"
;
165
return
std::make_unique<QPlatformAudioDevices>();
166
}
167
168
QPlatformAudioDevices::QPlatformAudioDevices()
169
{
170
qRegisterMetaType<PrivateTag>();
// for the case of queued connections
171
}
172
173
QPlatformAudioDevices::~QPlatformAudioDevices() =
default
;
174
175
QList<QAudioDevice> QPlatformAudioDevices::audioInputs()
const
176
{
177
return
m_audioInputs.ensure([
this
]() {
178
return
findAudioInputs();
179
});
180
}
181
182
QList<QAudioDevice> QPlatformAudioDevices::audioOutputs()
const
183
{
184
return
m_audioOutputs.ensure([
this
]() {
185
return
findAudioOutputs();
186
});
187
}
188
189
void
QPlatformAudioDevices::onAudioInputsChanged()
190
{
191
m_audioInputs.reset();
192
Q_EMIT audioInputsChanged(PrivateTag{});
193
}
194
195
void
QPlatformAudioDevices::onAudioOutputsChanged()
196
{
197
m_audioOutputs.reset();
198
Q_EMIT audioOutputsChanged(PrivateTag{});
199
}
200
201
void
QPlatformAudioDevices::updateAudioInputsCache()
202
{
203
if
(m_audioInputs.update(findAudioInputs()))
204
Q_EMIT audioInputsChanged(PrivateTag{});
205
}
206
207
void
QPlatformAudioDevices::updateAudioOutputsCache()
208
{
209
if
(m_audioOutputs.update(findAudioOutputs()))
210
Q_EMIT audioOutputsChanged(PrivateTag{});
211
}
212
213
QPlatformAudioSource *QPlatformAudioDevices::createAudioSource(
const
QAudioDevice &,
214
const
QAudioFormat &, QObject *)
215
{
216
return
nullptr
;
217
}
218
QPlatformAudioSink *QPlatformAudioDevices::createAudioSink(
const
QAudioDevice &,
219
const
QAudioFormat &, QObject *)
220
{
221
return
nullptr
;
222
}
223
224
QPlatformAudioSource *QPlatformAudioDevices::audioInputDevice(QAudioFormat format,
225
const
QAudioDevice &deviceInfo,
226
QObject *parent)
227
{
228
QAudioDevice device = deviceInfo;
229
if
(device.isNull())
230
device = QMediaDevices::defaultAudioInput();
231
232
if
(device.isNull())
233
return
nullptr
;
234
235
if
(format == QAudioFormat{})
236
format = device.preferredFormat();
237
238
return
createAudioSource(device, format, parent);
239
}
240
241
QPlatformAudioSink *QPlatformAudioDevices::audioOutputDevice(QAudioFormat format,
242
const
QAudioDevice &deviceInfo,
243
QObject *parent)
244
{
245
QAudioDevice device = deviceInfo;
246
if
(device.isNull())
247
device = QMediaDevices::defaultAudioOutput();
248
249
if
(device.isNull())
250
return
nullptr
;
251
252
if
(format == QAudioFormat{})
253
format = device.preferredFormat();
254
255
return
createAudioSink(device, format, parent);
256
}
257
258
bool
QPlatformAudioDevices::hasCallbackApi()
const
259
{
260
return
false
;
261
}
262
263
QT_END_NAMESPACE
264
265
#
include
"moc_qplatformaudiodevices_p.cpp"
qtmultimedia
src
multimedia
platform
qplatformaudiodevices.cpp
Generated on
for Qt by
1.18.0