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
qgstreameraudioinput.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 <common/qgstreameraudioinput_p.h>
5
6#include <QtCore/qloggingcategory.h>
7#include <QtMultimedia/qaudiodevice.h>
8#include <QtMultimedia/qaudioinput.h>
9
10#include <audio/qgstreameraudiodevice_p.h>
11#include <common/qgstpipeline_p.h>
12
13
15
16namespace {
17
18Q_STATIC_LOGGING_CATEGORY(qLcMediaAudioInput, "qt.multimedia.audioinput")
19
21 using namespace Qt::Literals;
22
23 if constexpr (QT_CONFIG(pulseaudio))
24 return "pulsesrc"_L1;
25 else if constexpr (QT_CONFIG(alsa))
26 return "alsasrc"_L1;
27 else
28 return "autoaudiosrc"_L1;
29}();
30
32{
33 using namespace Qt::Literals;
34 QLatin1String elementType = element.typeName();
35
36 if constexpr (QT_CONFIG(pulseaudio))
37 return elementType == "GstPulseSrc"_L1;
38
39 if constexpr (0 && QT_CONFIG(alsa)) // alsasrc has a "device" property, but it cannot be changed
40 // during playback
41 return elementType == "GstAlsaSrc"_L1;
42
43 return false;
44}
45
46} // namespace
47
48QMaybe<QPlatformAudioInput *> QGstreamerAudioInput::create(QAudioInput *parent)
49{
50 static const auto error = qGstErrorMessageIfElementsNotAvailable("autoaudiosrc", "volume");
51 if (error)
52 return *error;
53
54 return new QGstreamerAudioInput(parent);
55}
56
57QGstreamerAudioInput::QGstreamerAudioInput(QAudioInput *parent)
58 : QObject(parent),
59 QPlatformAudioInput(parent),
60 m_audioInputBin(QGstBin::create("audioInput")),
61 m_audioSrc{
62 QGstElement::createFromFactory(defaultSrcName.constData(), "autoaudiosrc"),
63 },
64 m_audioVolume{
65 QGstElement::createFromFactory("volume", "volume"),
66 }
67{
68 m_audioInputBin.add(m_audioSrc, m_audioVolume);
69 qLinkGstElements(m_audioSrc, m_audioVolume);
70
71 m_audioInputBin.addGhostPad(m_audioVolume, "src");
72}
73
74QGstElement QGstreamerAudioInput::createGstElement()
75{
76 const auto *customDeviceInfo =
77 dynamic_cast<const QGStreamerCustomAudioDeviceInfo *>(m_audioDevice.handle());
78
79 if (customDeviceInfo) {
80 qCDebug(qLcMediaAudioInput)
81 << "requesting custom audio src element: " << customDeviceInfo->id;
82
83 QGstElement element = QGstBin::createFromPipelineDescription(customDeviceInfo->id,
84 /*name=*/nullptr,
85 /*ghostUnlinkedPads=*/true);
86 if (element)
87 return element;
88
89 qCWarning(qLcMediaAudioInput)
90 << "Cannot create audio source element:" << customDeviceInfo->id;
91 }
92
93 const QByteArray &id = m_audioDevice.id();
94 if constexpr (QT_CONFIG(pulseaudio) || QT_CONFIG(alsa)) {
95 QGstElement newSrc = QGstElement::createFromFactory(defaultSrcName.constData(), "audiosrc");
96 if (newSrc) {
97 newSrc.set("device", id.constData());
98 return newSrc;
99 }
100
101 qWarning() << "Cannot create" << defaultSrcName;
102
103 } else {
104 auto *deviceInfo = dynamic_cast<const QGStreamerAudioDeviceInfo *>(m_audioDevice.handle());
105 if (deviceInfo && deviceInfo->gstDevice) {
106 QGstElement element = QGstElement::createFromDevice(deviceInfo->gstDevice, "audiosrc");
107 if (element)
108 return element;
109 }
110 }
111 qCWarning(qLcMediaAudioInput) << "Invalid audio device";
112 qCWarning(qLcMediaAudioInput)
113 << "Failed to create a gst element for the audio device, using a default audio source";
114 return QGstElement::createFromFactory("autoaudiosrc", "audiosrc");
115}
116
118{
119 m_audioInputBin.setStateSync(GST_STATE_NULL);
120}
121
123{
124 m_audioVolume.set("volume", volume);
125}
126
128{
129 m_audioVolume.set("mute", muted);
130}
131
132void QGstreamerAudioInput::setAudioDevice(const QAudioDevice &device)
133{
134 if (device == m_audioDevice)
135 return;
136 qCDebug(qLcMediaAudioInput) << "setAudioDevice" << device.description() << device.isNull();
137 m_audioDevice = device;
138
139 if (srcHasDeviceProperty(m_audioSrc) && !isCustomAudioDevice(m_audioDevice)) {
140 m_audioSrc.set("device", m_audioDevice.id().constData());
141 return;
142 }
143
144 QGstElement newSrc = createGstElement();
145
146 m_audioVolume.sink().modifyPipelineInIdleProbe([&] {
147 qUnlinkGstElements(m_audioSrc, m_audioVolume);
148 m_audioInputBin.stopAndRemoveElements(m_audioSrc);
149 m_audioSrc = std::move(newSrc);
150 m_audioInputBin.add(m_audioSrc);
151 qLinkGstElements(m_audioSrc, m_audioVolume);
152 m_audioSrc.syncStateWithParent();
153 });
154}
155
156QT_END_NAMESPACE
static QGstBin createFromPipelineDescription(const char *pipelineDescription, const char *name=nullptr, bool ghostUnlinkedPads=false)
Definition qgst.cpp:1292
static QGstElement createFromFactory(const char *factory, const char *name=nullptr)
Definition qgst.cpp:956
void setMuted(bool) override
void setAudioDevice(const QAudioDevice &) override
void setVolume(float) override
Combined button and popup list for selecting options.
constexpr QLatin1String defaultSrcName
bool srcHasDeviceProperty(const QGstElement &element)