| 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 <QtCore/qdebug.h> |
| 7 | #include <QtMultimedia/qaudiodevice.h> |
| 8 | #include <QtMultimedia/qmediadevices.h> |
| 9 | #include <QtMultimedia/private/qaudiosystem_p.h> |
| 10 | |
| 11 | #if defined(Q_OS_ANDROID) |
| 12 | # include <QtMultimedia/private/qandroidaudiodevices_p.h> |
| 13 | #endif |
| 14 | #if defined(Q_OS_DARWIN) |
| 15 | # include <QtMultimedia/private/qdarwinaudiodevices_p.h> |
| 16 | #endif |
| 17 | #if defined(Q_OS_WINDOWS) |
| 18 | # include <QtMultimedia/private/qwindowsaudiodevices_p.h> |
| 19 | #endif |
| 20 | #if QT_CONFIG(alsa) |
| 21 | # include <QtMultimedia/private/qalsaaudiodevices_p.h> |
| 22 | #endif |
| 23 | #if QT_CONFIG(pulseaudio) |
| 24 | # include <QtMultimedia/private/qpulseaudiodevices_p.h> |
| 25 | #endif |
| 26 | #if QT_CONFIG(pipewire) |
| 27 | # include <QtMultimedia/private/qpipewire_audiodevices_p.h> |
| 28 | #endif |
| 29 | #if defined(Q_OS_QNX) |
| 30 | # include <QtMultimedia/private/qqnxaudiodevices_p.h> |
| 31 | #endif |
| 32 | #if defined(Q_OS_WASM) |
| 33 | # include <QtMultimedia/private/qwasmmediadevices_p.h> |
| 34 | #endif |
| 35 | |
| 36 | QT_BEGIN_NAMESPACE |
| 37 | |
| 38 | std::unique_ptr<QPlatformAudioDevices> QPlatformAudioDevices::create() |
| 39 | { |
| 40 | #ifdef Q_OS_DARWIN |
| 41 | return std::make_unique<QDarwinAudioDevices>(); |
| 42 | #endif |
| 43 | #if defined(Q_OS_WINDOWS) |
| 44 | return std::make_unique<QWindowsAudioDevices>(); |
| 45 | #endif |
| 46 | #if defined(Q_OS_ANDROID) |
| 47 | return std::make_unique<QAndroidAudioDevices>(); |
| 48 | #endif |
| 49 | #if QT_CONFIG(pipewire) |
| 50 | using namespace Qt::Literals; |
| 51 | QByteArray requestedBackend = qgetenv("QT_AUDIO_BACKEND" ); |
| 52 | const bool pipewireRequested = requestedBackend == "pipewire"_ba ; |
| 53 | const bool considerPipewire = requestedBackend.isNull() || pipewireRequested; |
| 54 | |
| 55 | if (considerPipewire && QtPipeWire::QAudioDevices::isSupported()) |
| 56 | return std::make_unique<QtPipeWire::QAudioDevices>(); |
| 57 | |
| 58 | if (pipewireRequested) |
| 59 | qDebug() << "PipeWire audio backend requested. not available. Using default backend" ; |
| 60 | |
| 61 | #endif |
| 62 | #if QT_CONFIG(pulseaudio) |
| 63 | return std::make_unique<QPulseAudioDevices>(); |
| 64 | #endif |
| 65 | #if QT_CONFIG(alsa) |
| 66 | return std::make_unique<QAlsaAudioDevices>(); |
| 67 | #endif |
| 68 | #if defined(Q_OS_QNX) |
| 69 | return std::make_unique<QQnxAudioDevices>(); |
| 70 | #endif |
| 71 | #if defined(Q_OS_WASM) |
| 72 | return std::make_unique<QWasmMediaDevices>(); |
| 73 | #endif |
| 74 | return std::make_unique<QPlatformAudioDevices>(); |
| 75 | } |
| 76 | |
| 77 | QPlatformAudioDevices::QPlatformAudioDevices() |
| 78 | { |
| 79 | qRegisterMetaType<PrivateTag>(); // for the case of queued connections |
| 80 | } |
| 81 | |
| 82 | QPlatformAudioDevices::~QPlatformAudioDevices() = default; |
| 83 | |
| 84 | QList<QAudioDevice> QPlatformAudioDevices::audioInputs() const |
| 85 | { |
| 86 | return m_audioInputs.ensure(creator: [this]() { |
| 87 | return findAudioInputs(); |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | QList<QAudioDevice> QPlatformAudioDevices::audioOutputs() const |
| 92 | { |
| 93 | return m_audioOutputs.ensure(creator: [this]() { |
| 94 | return findAudioOutputs(); |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | void QPlatformAudioDevices::onAudioInputsChanged() |
| 99 | { |
| 100 | m_audioInputs.reset(); |
| 101 | emit audioInputsChanged(PrivateTag{}); |
| 102 | } |
| 103 | |
| 104 | void QPlatformAudioDevices::onAudioOutputsChanged() |
| 105 | { |
| 106 | m_audioOutputs.reset(); |
| 107 | emit audioOutputsChanged(PrivateTag{}); |
| 108 | } |
| 109 | |
| 110 | void QPlatformAudioDevices::updateAudioInputsCache() |
| 111 | { |
| 112 | if (m_audioInputs.update(value: findAudioInputs())) |
| 113 | emit audioInputsChanged(PrivateTag{}); |
| 114 | } |
| 115 | |
| 116 | void QPlatformAudioDevices::updateAudioOutputsCache() |
| 117 | { |
| 118 | if (m_audioOutputs.update(value: findAudioOutputs())) |
| 119 | emit audioOutputsChanged(PrivateTag{}); |
| 120 | } |
| 121 | |
| 122 | QPlatformAudioSource *QPlatformAudioDevices::createAudioSource(const QAudioDevice &, |
| 123 | const QAudioFormat &, QObject *) |
| 124 | { |
| 125 | return nullptr; |
| 126 | } |
| 127 | QPlatformAudioSink *QPlatformAudioDevices::createAudioSink(const QAudioDevice &, |
| 128 | const QAudioFormat &, QObject *) |
| 129 | { |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
| 133 | QPlatformAudioSource *QPlatformAudioDevices::audioInputDevice(QAudioFormat format, |
| 134 | const QAudioDevice &deviceInfo, |
| 135 | QObject *parent) |
| 136 | { |
| 137 | QAudioDevice device = deviceInfo; |
| 138 | if (device.isNull()) |
| 139 | device = QMediaDevices::defaultAudioInput(); |
| 140 | |
| 141 | if (device.isNull()) |
| 142 | return nullptr; |
| 143 | |
| 144 | if (format == QAudioFormat{}) |
| 145 | format = device.preferredFormat(); |
| 146 | |
| 147 | return createAudioSource(device, format, parent); |
| 148 | } |
| 149 | |
| 150 | QPlatformAudioSink *QPlatformAudioDevices::audioOutputDevice(QAudioFormat format, |
| 151 | const QAudioDevice &deviceInfo, |
| 152 | QObject *parent) |
| 153 | { |
| 154 | QAudioDevice device = deviceInfo; |
| 155 | if (device.isNull()) |
| 156 | device = QMediaDevices::defaultAudioOutput(); |
| 157 | |
| 158 | if (device.isNull()) |
| 159 | return nullptr; |
| 160 | |
| 161 | if (format == QAudioFormat{}) |
| 162 | format = device.preferredFormat(); |
| 163 | |
| 164 | return createAudioSink(device, format, parent); |
| 165 | } |
| 166 | |
| 167 | QT_END_NAMESPACE |
| 168 | |
| 169 | #include "moc_qplatformaudiodevices_p.cpp" |
| 170 | |