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
qgstreameraudiodevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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 <common/qgst_p.h>
7#include <common/qgstutils_p.h>
8#include <private/qplatformmediaintegration_p.h>
9
10#if QT_CONFIG(pulseaudio)
11# include <QtMultimedia/private/qpulsehelpers_p.h>
12#endif
13
14#include <optional>
15
16QT_BEGIN_NAMESPACE
17
18using namespace Qt::Literals;
19
20QGStreamerCustomAudioDeviceInfo::QGStreamerCustomAudioDeviceInfo(const QByteArray &gstreamerPipeline,
21 QAudioDevice::Mode mode)
22 : QAudioDevicePrivate{
23 gstreamerPipeline,
24 mode,
25 QString::fromUtf8(gstreamerPipeline),
26 false,
27 std::future<QAudioDevicePrivate::AudioDeviceFormat>{} // invalid future - format not predetermined
28 }
29{
30}
31
32QAudioDevice qMakeCustomGStreamerAudioInput(const QByteArray &gstreamerPipeline)
33{
34 auto deviceInfo = std::make_unique<QGStreamerCustomAudioDeviceInfo>(gstreamerPipeline,
35 QAudioDevice::Mode::Input);
36
37 return QAudioDevicePrivate::createQAudioDevice(std::move(deviceInfo));
38}
39
40QAudioDevice qMakeCustomGStreamerAudioOutput(const QByteArray &gstreamerPipeline)
41{
42 auto deviceInfo = std::make_unique<QGStreamerCustomAudioDeviceInfo>(gstreamerPipeline,
43 QAudioDevice::Mode::Output);
44
45 return QAudioDevicePrivate::createQAudioDevice(std::move(deviceInfo));
46}
47
48bool isCustomAudioDevice(const QAudioDevicePrivate *device)
49{
50 return dynamic_cast<const QGStreamerCustomAudioDeviceInfo *>(device);
51}
52
53bool isCustomAudioDevice(const QAudioDevice &device)
54{
55 return isCustomAudioDevice(QAudioDevicePrivate::handle(device));
56}
57
58namespace {
59
60constexpr QGstAudioElements pipeWireElements{ "pipewiresrc", "pipewiresink" };
61constexpr QGstAudioElements pulseElements{ "pulsesrc", "pulsesink" };
62constexpr QGstAudioElements alsaElements{ "alsasrc", "alsasink" };
63constexpr QGstAudioElements autoElements{ "autoaudiosrc", "autoaudiosink" };
64constexpr QGstAudioElements noElements{ nullptr, "fakesink" };
65
66bool isPulseAudioServerReachable()
67{
68#if QT_CONFIG(pulseaudio)
69 using namespace QPulseAudioInternal;
70 return pulseaudioDetectServerType() != PulseaudioServerType::NotConnected;
71#else
72 return false; // Cannot check without libpulse
73#endif
74}
75
76std::optional<QGstAudioElements> requestedAudioElements(const QByteArray &requested)
77{
78 if (requested == "pipewire"_ba)
79 return pipeWireElements;
80 if (requested == "pulseaudio"_ba)
81 return pulseElements;
82 if (requested == "alsa"_ba)
83 return alsaElements;
84
85 qWarning() << "Unknown QT_GSTREAMER_AUDIO_ELEMENTS value" << requested;
86 return std::nullopt;
87}
88
89std::optional<QGstAudioElements> pipeWireAudioElements()
90{
91 // pipewiresrc and pipewiresink are only used if requested with
92 // QT_GSTREAMER_AUDIO_ELEMENTS=pipewire, because they cannot switch devices: a running stream
93 // cannot be moved to another device, and replacing a running pipewiresink can deadlock
94 // (https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/5171). Consider making them the
95 // default once that is fixed.
96
97 if (!isPulseAudioServerReachable()) {
98 qWarning() << "No PulseAudio server is reachable for the GStreamer audio elements. Set "
99 "QT_GSTREAMER_AUDIO_ELEMENTS=pipewire to use PipeWire directly.";
100 return std::nullopt;
101 }
102 return pulseElements;
103}
104
105std::optional<QGstAudioElements> backendAudioElements()
106{
107 const QLatin1StringView backend = QPlatformMediaIntegration::audioBackendName();
108 if (backend == "PipeWire"_L1)
109 return pipeWireAudioElements();
110 if (backend == "PulseAudio"_L1)
111 return pulseElements;
112 if (backend == "ALSA"_L1)
113 return alsaElements;
114 return autoElements;
115}
116
117QGstAudioElements selectAudioElements()
118{
119 const QByteArray requested = qgetenv("QT_GSTREAMER_AUDIO_ELEMENTS").toLower();
120 std::optional<QGstAudioElements> elements =
121 requested.isEmpty() ? backendAudioElements() : requestedAudioElements(requested);
122
123 if (elements) {
124 if (auto error = qGstErrorMessageIfElementsNotAvailable(elements->source, elements->sink)) {
125 qWarning().noquote() << *error;
126 elements = std::nullopt;
127 }
128 }
129 if (!elements) {
130 qWarning() << "Audio output is discarded and audio input is not available";
131 return noElements;
132 }
133 return *elements;
134}
135
136} // namespace
137
139{
140 static const QGstAudioElements elements = selectAudioElements();
141 return elements;
142}
143
144void qGstSetAudioDevice(QGstElement &element, const QAudioDevice &device)
145{
146 if (device.isNull())
147 return;
148
149 const QByteArray id = device.id();
150
151 const QLatin1StringView type = element.typeName();
152 if (type == "GstPipeWireSrc"_L1 || type == "GstPipeWireSink"_L1) {
153 // target-object is only read when the stream connects, so it selects the device of a
154 // newly created element, but cannot move a running stream to another device.
155 element.set("target-object", id.constData());
156 } else if (type == "GstPulseSrc"_L1 || type == "GstPulseSink"_L1 || type == "GstAlsaSrc"_L1
157 || type == "GstAlsaSink"_L1) {
158 element.set("device", id.constData());
159 }
160}
161
162QT_END_NAMESPACE
bool isCustomAudioDevice(const QAudioDevicePrivate *device)
bool isCustomAudioDevice(const QAudioDevice &device)
void qGstSetAudioDevice(QGstElement &element, const QAudioDevice &device)
const QGstAudioElements & qGstAudioElements()
QAudioDevice qMakeCustomGStreamerAudioInput(const QByteArray &gstreamerPipeline)
QAudioDevice qMakeCustomGStreamerAudioOutput(const QByteArray &gstreamerPipeline)