Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qalsamediadevices.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
5#include "qmediadevices.h"
6#include "qcameradevice_p.h"
7
8#include "private/qalsaaudiosource_p.h"
9#include "private/qalsaaudiosink_p.h"
10#include "private/qalsaaudiodevice_p.h"
11
12#include <alsa/asoundlib.h>
13
15
16namespace {
17
19{
20 void operator()(char *c) const { ::free(c); }
21};
22
23using unique_str = std::unique_ptr<char, free_char>;
24
25bool operator==(const unique_str &str, std::string_view sv)
26{
27 return std::string_view{ str.get() } == sv;
28}
29bool operator!=(const unique_str &str, std::string_view sv)
30{
31 return !(str == sv);
32}
33
34} // namespace
35
40
41static QList<QAudioDevice> availableDevices(QAudioDevice::Mode mode)
42{
43 QList<QAudioDevice> devices;
44
45 // Create a list of all current audio devices that support mode
46 void **hints;
47 if (snd_device_name_hint(-1, "pcm", &hints) < 0) {
48 qWarning() << "no alsa devices available";
49 return devices;
50 }
51
52 std::string_view filter = (mode == QAudioDevice::Input) ? "Input" : "Output";
53
54 QAlsaAudioDeviceInfo *sysdefault = nullptr;
55
56 auto makeDeviceInfo = [&filter, mode](void *entry) -> QAlsaAudioDeviceInfo * {
57 unique_str name{ snd_device_name_get_hint(entry, "NAME") };
58 if (name && name != "null") {
59 unique_str descr{ snd_device_name_get_hint(entry, "DESC") };
60 unique_str io{ snd_device_name_get_hint(entry, "IOID") };
61
62 if (descr && (!io || (io == filter))) {
63 auto *infop = new QAlsaAudioDeviceInfo{
64 name.get(),
65 QString::fromUtf8(descr.get()),
66 mode,
67 };
68 return infop;
69 }
70 }
71 return nullptr;
72 };
73
74 bool hasDefault = false;
75 void **n = hints;
76 while (*n != NULL) {
77 QAlsaAudioDeviceInfo *infop = makeDeviceInfo(*n++);
78
79 if (infop) {
80 devices.append(infop->create());
81 if (!hasDefault && infop->id.startsWith("default")) {
82 infop->isDefault = true;
83 hasDefault = true;
84 }
85 if (!sysdefault && infop->id.startsWith("sysdefault"))
86 sysdefault = infop;
87 }
88 }
89
90 if (!hasDefault && sysdefault) {
91 // Make "sysdefault" the default device if there is no "default" device exists
92 sysdefault->isDefault = true;
93 hasDefault = true;
94 }
95 if (!hasDefault && devices.size() > 0) {
96 // forcefully declare the first device as "default"
97 QAlsaAudioDeviceInfo *infop = makeDeviceInfo(hints[0]);
98 if (infop) {
99 infop->isDefault = true;
100 devices.prepend(infop->create());
101 }
102 }
103
104 snd_device_name_free_hint(hints);
105 return devices;
106}
107
108QList<QAudioDevice> QAlsaMediaDevices::audioInputs() const
109{
111}
112
113QList<QAudioDevice> QAlsaMediaDevices::audioOutputs() const
114{
116}
117
119 QObject *parent)
120{
121 return new QAlsaAudioSource(deviceInfo.id(), parent);
122}
123
125 QObject *parent)
126{
127 return new QAlsaAudioSink(deviceInfo.id(), parent);
128}
129
QList< QAudioDevice > audioOutputs() const override
QPlatformAudioSource * createAudioSource(const QAudioDevice &deviceInfo, QObject *parent) override
QList< QAudioDevice > audioInputs() const override
QPlatformAudioSink * createAudioSink(const QAudioDevice &deviceInfo, QObject *parent) override
The QAudioDevice class provides an information about audio devices and their functionality.
Mode
Describes the mode of this device.
QByteArray id
\qmlproperty string QtMultimedia::audioDevice::id
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
QString str
[2]
Combined button and popup list for selecting options.
std::unique_ptr< char, free_char > unique_str
static QList< QAudioDevice > availableDevices(QAudioDevice::Mode mode)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
EGLDeviceEXT * devices
#define qWarning
Definition qlogging.h:166
GLenum mode
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLuint name
GLfloat n
const GLubyte * c
GLuint entry
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220