1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtMultimedia/private/qtmultimediaglobal_p.h>
41#include "qgstreameraudioinputselector_p.h"
42
43#include <QtCore/QDir>
44#include <QtCore/QDebug>
45
46#include <gst/gst.h>
47
48#if QT_CONFIG(alsa)
49#include <alsa/asoundlib.h>
50#endif
51
52QGstreamerAudioInputSelector::QGstreamerAudioInputSelector(QObject *parent)
53 :QAudioInputSelectorControl(parent)
54{
55 update();
56}
57
58QGstreamerAudioInputSelector::~QGstreamerAudioInputSelector()
59{
60}
61
62QList<QString> QGstreamerAudioInputSelector::availableInputs() const
63{
64 return m_names;
65}
66
67QString QGstreamerAudioInputSelector::inputDescription(const QString& name) const
68{
69 QString desc;
70
71 for (int i = 0; i < m_names.size(); i++) {
72 if (m_names.at(i).compare(s: name) == 0) {
73 desc = m_descriptions.at(i);
74 break;
75 }
76 }
77 return desc;
78}
79
80QString QGstreamerAudioInputSelector::defaultInput() const
81{
82 if (m_names.size() > 0)
83 return m_names.at(i: 0);
84
85 return QString();
86}
87
88QString QGstreamerAudioInputSelector::activeInput() const
89{
90 return m_audioInput;
91}
92
93void QGstreamerAudioInputSelector::setActiveInput(const QString& name)
94{
95 if (m_audioInput.compare(s: name) != 0) {
96 m_audioInput = name;
97 emit activeInputChanged(name);
98 }
99}
100
101void QGstreamerAudioInputSelector::update()
102{
103 m_names.clear();
104 m_descriptions.clear();
105
106 //use autoaudiosrc as the first default device
107 m_names.append(t: QLatin1String("default:"));
108 m_descriptions.append(t: tr(s: "System default device"));
109
110 updatePulseDevices();
111 updateAlsaDevices();
112 updateOssDevices();
113 if (m_names.size() > 0)
114 m_audioInput = m_names.at(i: 0);
115}
116
117void QGstreamerAudioInputSelector::updateAlsaDevices()
118{
119#if QT_CONFIG(alsa)
120 void **hints, **n;
121 if (snd_device_name_hint(-1, "pcm", &hints) < 0) {
122 qWarning()<<"no alsa devices available";
123 return;
124 }
125 n = hints;
126
127 while (*n != nullptr) {
128 char *name = snd_device_name_get_hint(*n, "NAME");
129 char *descr = snd_device_name_get_hint(*n, "DESC");
130 char *io = snd_device_name_get_hint(*n, "IOID");
131
132 if ((name != nullptr) && (descr != nullptr)) {
133 if (io == nullptr || qstrcmp(io, "Input") == 0) {
134 m_names.append(QLatin1String("alsa:")+QString::fromUtf8(name));
135 m_descriptions.append(QString::fromUtf8(descr));
136 }
137 }
138
139 free(name);
140 free(descr);
141 free(io);
142 n++;
143 }
144 snd_device_name_free_hint(hints);
145#endif
146}
147
148void QGstreamerAudioInputSelector::updateOssDevices()
149{
150 QDir devDir(QStringLiteral("/dev"));
151 devDir.setFilter(QDir::System);
152 const QFileInfoList entries = devDir.entryInfoList(nameFilters: QStringList() << QLatin1String("dsp*"));
153 for (const QFileInfo& entryInfo : entries) {
154 m_names.append(t: QLatin1String("oss:")+entryInfo.filePath());
155 m_descriptions.append(t: QString::fromLatin1(str: "OSS device %1").arg(a: entryInfo.fileName()));
156 }
157}
158
159void QGstreamerAudioInputSelector::updatePulseDevices()
160{
161 GstElementFactory *factory = gst_element_factory_find(name: "pulsesrc");
162 if (factory) {
163 m_names.append(t: QLatin1String("pulseaudio:"));
164 m_descriptions.append(t: QLatin1String("PulseAudio device."));
165 gst_object_unref(GST_OBJECT(factory));
166 }
167}
168

source code of qtmultimedia/src/gsttools/qgstreameraudioinputselector.cpp