| 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 <QtMultimedia/qaudiodevice.h> | 
| 7 | #include <QtMultimedia/qmediadevices.h> | 
| 8 | #include <QtMultimedia/private/qaudiosystem_p.h> | 
| 9 |  | 
| 10 | #if defined(Q_OS_ANDROID) | 
| 11 | #include <qandroidaudiodevices_p.h> | 
| 12 | #elif defined(Q_OS_DARWIN) | 
| 13 | #include <qdarwinaudiodevices_p.h> | 
| 14 | #elif defined(Q_OS_WINDOWS) && QT_CONFIG(wmf) | 
| 15 | #include <qwindowsaudiodevices_p.h> | 
| 16 | #elif QT_CONFIG(alsa) | 
| 17 | #include <qalsaaudiodevices_p.h> | 
| 18 | #elif QT_CONFIG(pulseaudio) | 
| 19 | #include <qpulseaudiodevices_p.h> | 
| 20 | #elif defined(Q_OS_QNX) | 
| 21 | #include <qqnxaudiodevices_p.h> | 
| 22 | #elif defined(Q_OS_WASM) | 
| 23 | #include <private/qwasmmediadevices_p.h> | 
| 24 | #endif | 
| 25 |  | 
| 26 | QT_BEGIN_NAMESPACE | 
| 27 |  | 
| 28 | std::unique_ptr<QPlatformAudioDevices> QPlatformAudioDevices::create() | 
| 29 | { | 
| 30 | #ifdef Q_OS_DARWIN | 
| 31 |     return std::make_unique<QDarwinAudioDevices>(); | 
| 32 | #elif defined(Q_OS_WINDOWS) && QT_CONFIG(wmf) | 
| 33 |     return std::make_unique<QWindowsAudioDevices>(); | 
| 34 | #elif defined(Q_OS_ANDROID) | 
| 35 |     return std::make_unique<QAndroidAudioDevices>(); | 
| 36 | #elif QT_CONFIG(alsa) | 
| 37 |     return std::make_unique<QAlsaAudioDevices>(); | 
| 38 | #elif QT_CONFIG(pulseaudio) | 
| 39 |     return std::make_unique<QPulseAudioDevices>(); | 
| 40 | #elif defined(Q_OS_QNX) | 
| 41 |     return std::make_unique<QQnxAudioDevices>(); | 
| 42 | #elif defined(Q_OS_WASM) | 
| 43 |     return std::make_unique<QWasmMediaDevices>(); | 
| 44 | #else | 
| 45 |     return std::make_unique<QPlatformAudioDevices>(); | 
| 46 | #endif | 
| 47 | } | 
| 48 |  | 
| 49 | QPlatformAudioDevices::QPlatformAudioDevices() | 
| 50 | { | 
| 51 |     qRegisterMetaType<PrivateTag>(); // for the case of queued connections | 
| 52 | } | 
| 53 |  | 
| 54 | QPlatformAudioDevices::~QPlatformAudioDevices() = default; | 
| 55 |  | 
| 56 | QList<QAudioDevice> QPlatformAudioDevices::audioInputs() const | 
| 57 | { | 
| 58 |     return m_audioInputs.ensure(creator: [this]() { | 
| 59 |         return findAudioInputs(); | 
| 60 |     }); | 
| 61 | } | 
| 62 |  | 
| 63 | QList<QAudioDevice> QPlatformAudioDevices::audioOutputs() const | 
| 64 | { | 
| 65 |     return m_audioOutputs.ensure(creator: [this]() { | 
| 66 |         return findAudioOutputs(); | 
| 67 |     }); | 
| 68 | } | 
| 69 |  | 
| 70 | void QPlatformAudioDevices::onAudioInputsChanged() | 
| 71 | { | 
| 72 |     m_audioInputs.reset(); | 
| 73 |     emit audioInputsChanged(PrivateTag{}); | 
| 74 | } | 
| 75 |  | 
| 76 | void QPlatformAudioDevices::onAudioOutputsChanged() | 
| 77 | { | 
| 78 |     m_audioOutputs.reset(); | 
| 79 |     emit audioOutputsChanged(PrivateTag{}); | 
| 80 | } | 
| 81 |  | 
| 82 | void QPlatformAudioDevices::updateAudioInputsCache() | 
| 83 | { | 
| 84 |     if (m_audioInputs.update(value: findAudioInputs())) | 
| 85 |         emit audioInputsChanged(PrivateTag{}); | 
| 86 | } | 
| 87 |  | 
| 88 | void QPlatformAudioDevices::updateAudioOutputsCache() | 
| 89 | { | 
| 90 |     if (m_audioOutputs.update(value: findAudioOutputs())) | 
| 91 |         emit audioOutputsChanged(PrivateTag{}); | 
| 92 | } | 
| 93 |  | 
| 94 | QPlatformAudioSource *QPlatformAudioDevices::createAudioSource(const QAudioDevice &, | 
| 95 |                                                                const QAudioFormat &, QObject *) | 
| 96 | { | 
| 97 |     return nullptr; | 
| 98 | } | 
| 99 | QPlatformAudioSink *QPlatformAudioDevices::createAudioSink(const QAudioDevice &, | 
| 100 |                                                            const QAudioFormat &, QObject *) | 
| 101 | { | 
| 102 |     return nullptr; | 
| 103 | } | 
| 104 |  | 
| 105 | QPlatformAudioSource *QPlatformAudioDevices::audioInputDevice(const QAudioFormat &format, | 
| 106 |                                                               const QAudioDevice &deviceInfo, | 
| 107 |                                                               QObject *parent) | 
| 108 | { | 
| 109 |     QAudioDevice device = deviceInfo; | 
| 110 |     if (device.isNull()) | 
| 111 |         device = QMediaDevices::defaultAudioInput(); | 
| 112 |  | 
| 113 |     if (device.isNull()) | 
| 114 |         return nullptr; | 
| 115 |  | 
| 116 |     return createAudioSource(device, format, parent); | 
| 117 | } | 
| 118 |  | 
| 119 | QPlatformAudioSink *QPlatformAudioDevices::audioOutputDevice(const QAudioFormat &format, | 
| 120 |                                                              const QAudioDevice &deviceInfo, | 
| 121 |                                                              QObject *parent) | 
| 122 | { | 
| 123 |     QAudioDevice device = deviceInfo; | 
| 124 |     if (device.isNull()) | 
| 125 |         device = QMediaDevices::defaultAudioOutput(); | 
| 126 |  | 
| 127 |     if (device.isNull()) | 
| 128 |         return nullptr; | 
| 129 |  | 
| 130 |     return createAudioSink(device, format, parent); | 
| 131 | } | 
| 132 |  | 
| 133 | void QPlatformAudioDevices::prepareAudio() { } | 
| 134 |  | 
| 135 | QT_END_NAMESPACE | 
| 136 |  | 
| 137 | #include "moc_qplatformaudiodevices_p.cpp" | 
| 138 |  |