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 "qplatformmediadevices_p.h"
5#include "qplatformmediaintegration_p.h"
6#include "qmediadevices.h"
7#include "qaudiodevice.h"
8#include "qcameradevice.h"
9#include "qaudiosystem_p.h"
10#include "qaudiodevice.h"
11#include "qplatformvideodevices_p.h"
12
13#include <qmutex.h>
14#include <qloggingcategory.h>
15
16#if defined(Q_OS_ANDROID)
17#include <qandroidmediadevices_p.h>
18#elif defined(Q_OS_DARWIN)
19#include <qdarwinmediadevices_p.h>
20#elif defined(Q_OS_WINDOWS)
21#include <qwindowsmediadevices_p.h>
22#elif QT_CONFIG(alsa)
23#include <qalsamediadevices_p.h>
24#elif QT_CONFIG(pulseaudio)
25#include <qpulseaudiomediadevices_p.h>
26#elif defined(Q_OS_QNX)
27#include <qqnxmediadevices_p.h>
28#elif defined(Q_OS_WASM)
29#include <private/qwasmmediadevices_p.h>
30#endif
31
32
33QT_BEGIN_NAMESPACE
34
35namespace {
36struct DevicesHolder {
37 ~DevicesHolder()
38 {
39 QMutexLocker locker(&mutex);
40 delete nativeInstance;
41 nativeInstance = nullptr;
42 instance = nullptr;
43 }
44 QBasicMutex mutex;
45 QPlatformMediaDevices *instance = nullptr;
46 QPlatformMediaDevices *nativeInstance = nullptr;
47} devicesHolder;
48
49}
50
51QPlatformMediaDevices *QPlatformMediaDevices::instance()
52{
53 QMutexLocker locker(&devicesHolder.mutex);
54 if (devicesHolder.instance)
55 return devicesHolder.instance;
56
57#ifdef Q_OS_DARWIN
58 devicesHolder.nativeInstance = new QDarwinMediaDevices;
59#elif defined(Q_OS_WINDOWS)
60 devicesHolder.nativeInstance = new QWindowsMediaDevices;
61#elif defined(Q_OS_ANDROID)
62 devicesHolder.nativeInstance = new QAndroidMediaDevices;
63#elif QT_CONFIG(alsa)
64 devicesHolder.nativeInstance = new QAlsaMediaDevices;
65#elif QT_CONFIG(pulseaudio)
66 devicesHolder.nativeInstance = new QPulseAudioMediaDevices;
67#elif defined(Q_OS_QNX)
68 devicesHolder.nativeInstance = new QQnxMediaDevices;
69#elif defined(Q_OS_WASM)
70 devicesHolder.nativeInstance = new QWasmMediaDevices;
71#else
72 devicesHolder.nativeInstance = new QPlatformMediaDevices;
73#endif
74
75 devicesHolder.instance = devicesHolder.nativeInstance;
76 return devicesHolder.instance;
77}
78
79
80QPlatformMediaDevices::QPlatformMediaDevices() = default;
81
82void QPlatformMediaDevices::initVideoDevicesConnection() {
83 std::call_once(once&: m_videoDevicesConnectionFlag, f: [this]() {
84 QMetaObject::invokeMethod(object: this, function: [this]() {
85 auto videoDevices = QPlatformMediaIntegration::instance()->videoDevices();
86 if (videoDevices)
87 connect(sender: videoDevices, signal: &QPlatformVideoDevices::videoInputsChanged, context: this,
88 slot: &QPlatformMediaDevices::videoInputsChanged);
89 }, type: Qt::QueuedConnection);
90 });
91}
92
93void QPlatformMediaDevices::setDevices(QPlatformMediaDevices *devices)
94{
95 devicesHolder.instance = devices;
96}
97
98QPlatformMediaDevices::~QPlatformMediaDevices() = default;
99
100QList<QAudioDevice> QPlatformMediaDevices::audioInputs() const
101{
102 return {};
103}
104
105QList<QAudioDevice> QPlatformMediaDevices::audioOutputs() const
106{
107 return {};
108}
109
110QPlatformAudioSource *QPlatformMediaDevices::createAudioSource(const QAudioDevice &, QObject *)
111{
112 return nullptr;
113}
114QPlatformAudioSink *QPlatformMediaDevices::createAudioSink(const QAudioDevice &, QObject *)
115{
116 return nullptr;
117}
118
119QPlatformAudioSource *QPlatformMediaDevices::audioInputDevice(const QAudioFormat &format,
120 const QAudioDevice &deviceInfo,
121 QObject *parent)
122{
123 QAudioDevice info = deviceInfo;
124 if (info.isNull())
125 info = audioInputs().value(i: 0);
126
127 QPlatformAudioSource* p = !info.isNull() ? createAudioSource(info, parent) : nullptr;
128 if (p)
129 p->setFormat(format);
130 return p;
131}
132
133QPlatformAudioSink *QPlatformMediaDevices::audioOutputDevice(const QAudioFormat &format,
134 const QAudioDevice &deviceInfo,
135 QObject *parent)
136{
137 QAudioDevice info = deviceInfo;
138 if (info.isNull())
139 info = audioOutputs().value(i: 0);
140
141 QPlatformAudioSink* p = !info.isNull() ? createAudioSink(info, parent) : nullptr;
142 if (p)
143 p->setFormat(format);
144 return p;
145}
146
147void QPlatformMediaDevices::prepareAudio() { }
148
149QT_END_NAMESPACE
150
151#include "moc_qplatformmediadevices_p.cpp"
152

source code of qtmultimedia/src/multimedia/platform/qplatformmediadevices.cpp