1// Copyright (C) 2016 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 "qmediadevices.h"
5#include "private/qplatformmediaintegration_p.h"
6#include "private/qplatformmediadevices_p.h"
7
8#include <qaudiodevice.h>
9#include <qcameradevice.h>
10
11QT_BEGIN_NAMESPACE
12
13/*!
14 \class QMediaDevices
15 \brief The QMediaDevices class provides information about available
16 multimedia input and output devices.
17 \ingroup multimedia
18 \inmodule QtMultimedia
19
20 The QMediaDevices class provides information about the available multimedia
21 devices and the system defaults. It monitors the following three groups:
22 \list
23 \li Audio input devices (Microphones)
24 \li Audio output devices (Speakers, Headsets)
25 \li Video input devices (Cameras)
26 \endlist
27
28 QMediaDevices provides a separate list for each device group. If it detects that a
29 new device has been connected to the system or an attached device has been disconnected
30 from the system, it will update the corresponding device list and emit a signal
31 notifying about the change.
32
33 QMediaDevices monitors the system defaults for each device group. It will notify about
34 any changes done through the system settings. For example, if the user selects a new
35 default audio output in the system settings, QMediaDevices will update the default audio
36 output accordingly and emit a signal. If the system does not provide a default for a
37 camera or an audio input, QMediaDevices will select the first device from the list as
38 the default device.
39
40 While using the default input and output devices is often sufficient for
41 playing back or recording multimedia, there is often a need to explicitly
42 select the device to be used.
43
44 QMediaDevices is a singleton object and all getters are thread-safe.
45*/
46
47/*!
48 \qmltype MediaDevices
49 \since 6.2
50 \instantiates QMediaDevices
51 \brief MediaDevices provides information about available
52 multimedia input and output devices.
53 \inqmlmodule QtMultimedia
54 \ingroup multimedia_qml
55
56 The MediaDevices type provides information about the available multimedia
57 devices and the system defaults. It monitors the following three groups:
58 \list
59 \li Audio input devices (Microphones)
60 \li Audio output devices (Speakers, Headsets)
61 \li Video input devices (Cameras)
62 \endlist
63
64 MediaDevices provides a separate list for each device group. If it detects that a
65 new device has been connected to the system or an attached device has been disconnected
66 from the system, it will update the corresponding device list and emit a signal
67 notifying about the change.
68
69 MediaDevices monitors the system defaults for each device group. It will notify about
70 any changes done through the system settings. For example, if the user selects a new
71 default audio output in the system settings, MediaDevices will update the default audio
72 output accordingly and emit a signal. If the system does not provide a default for a
73 camera or an audio input, MediaDevices will select the first device from the list as
74 the default device.
75
76 While using the default input and output devices is often sufficient for
77 playing back or recording multimedia, there is often a need to explicitly
78 select the device to be used.
79
80 For example, the snippet below will ensure that the media player always uses
81 the systems default audio output device for playback:
82
83 \qml
84 MediaDevices {
85 id: devices
86 }
87 MediaPlayer {
88 ...
89 audioOutput: AudioOutput {
90 device: devices.defaultAudioOutput
91 }
92 }
93 \endqml
94
95 \sa Camera, AudioInput, VideoOutput
96*/
97
98/*!
99 \qmlproperty list<audioDevice> QtMultimedia::MediaDevices::audioInputs
100 Contains a list of available audio input devices on the system.
101
102 Those devices are usually microphones. Devices can be either built-in, or
103 connected through for example USB or Bluetooth.
104*/
105
106/*!
107 \property QMediaDevices::audioInputs
108
109 Returns a list of available audio input devices on the system.
110
111 Those devices are usually microphones. Devices can be either built-in, or
112 connected through for example USB or Bluetooth.
113*/
114QList<QAudioDevice> QMediaDevices::audioInputs()
115{
116 return QPlatformMediaDevices::instance()->audioInputs();
117}
118
119/*!
120 \qmlproperty list<audioDevice> QtMultimedia::MediaDevices::audioOutputs
121 Contains a list of available audio output devices on the system.
122
123 Those devices are usually loudspeakers or head sets. Devices can be either
124 built-in, or connected through for example USB or Bluetooth.
125*/
126
127/*!
128 \property QMediaDevices::audioOutputs
129
130 Returns a list of available audio output devices on the system.
131
132 Those devices are usually loudspeakers or head sets. Devices can be either
133 built-in, or connected through for example USB or Bluetooth.
134*/
135QList<QAudioDevice> QMediaDevices::audioOutputs()
136{
137 return QPlatformMediaDevices::instance()->audioOutputs();
138}
139
140/*!
141 \qmlproperty list<cameraDevice> QtMultimedia::MediaDevices::videoInputs
142 Contains a list of cameras on the system.
143*/
144
145/*!
146 \property QMediaDevices::videoInputs
147
148 Returns a list of available cameras on the system.
149*/
150QList<QCameraDevice> QMediaDevices::videoInputs()
151{
152 QPlatformMediaDevices::instance()->initVideoDevicesConnection();
153 return QPlatformMediaIntegration::instance()->videoInputs();
154}
155
156/*!
157 \qmlproperty audioDevice QtMultimedia::MediaDevices::defaultAudioInput
158 Returns the default audio input device.
159
160 The default device can change during the runtime of the application. The value
161 of this property will automatically adjust itself to such changes.
162*/
163
164/*!
165 \property QMediaDevices::defaultAudioInput
166
167 Returns the default audio input device.
168
169 The default device can change during the runtime of the application.
170 The audioInputsChanged() signal is emitted in this case.
171*/
172QAudioDevice QMediaDevices::defaultAudioInput()
173{
174 const auto inputs = audioInputs();
175 if (inputs.isEmpty())
176 return {};
177 for (const auto &info : inputs)
178 if (info.isDefault())
179 return info;
180 return inputs.value(i: 0);
181}
182
183/*!
184 \qmlproperty audioDevice QtMultimedia::MediaDevices::defaultAudioOutput
185 Returns the default audio output device.
186
187 The default device can change during the runtime of the application. The value
188 of this property will automatically adjust itself to such changes.
189*/
190
191/*!
192 \property QMediaDevices::defaultAudioOutput
193
194 Returns the default audio output device.
195
196 The default device can change during the runtime of the application. The
197 audioOutputsChanged() signal is emitted in this case.
198*/
199QAudioDevice QMediaDevices::defaultAudioOutput()
200{
201 const auto outputs = audioOutputs();
202 if (outputs.isEmpty())
203 return {};
204 for (const auto &info : outputs)
205 if (info.isDefault())
206 return info;
207 return outputs.value(i: 0);
208}
209
210/*!
211 \qmlproperty cameraDevice QtMultimedia::MediaDevices::defaultVideoInput
212 Returns the default camera on the system.
213
214 \note The returned object should be checked using isNull() before being used,
215 in case there is no camera available.
216
217 The default device can change during the runtime of the application. The value
218 of this property will automatically adjust itself to such changes.
219*/
220
221/*!
222 \property QMediaDevices::defaultVideoInput
223
224 Returns the default camera on the system.
225
226 \note The returned object should be checked using isNull() before being used,
227 in case there is no default camera or no cameras at all.
228
229 The default device can change during the runtime of the application. The
230 videoInputsChanged() signal is emitted in that case.
231
232 \sa videoInputs()
233*/
234QCameraDevice QMediaDevices::defaultVideoInput()
235{
236 const auto inputs = videoInputs();
237 if (inputs.isEmpty())
238 return {};
239 for (const auto &info : inputs)
240 if (info.isDefault())
241 return info;
242 return inputs.value(i: 0);
243}
244
245/*!
246 \internal
247*/
248QMediaDevices::QMediaDevices(QObject *parent)
249 : QObject(parent)
250{
251 auto platformDevices = QPlatformMediaDevices::instance();
252 connect(sender: platformDevices, signal: &QPlatformMediaDevices::videoInputsChanged, context: this,
253 slot: &QMediaDevices::videoInputsChanged);
254 connect(sender: platformDevices, signal: &QPlatformMediaDevices::audioInputsChanged, context: this,
255 slot: &QMediaDevices::audioInputsChanged);
256 connect(sender: platformDevices, signal: &QPlatformMediaDevices::audioOutputsChanged, context: this,
257 slot: &QMediaDevices::audioOutputsChanged);
258}
259
260/*!
261 \internal
262*/
263QMediaDevices::~QMediaDevices() = default;
264
265void QMediaDevices::connectNotify(const QMetaMethod &signal)
266{
267 if (signal == QMetaMethod::fromSignal(signal: &QMediaDevices::videoInputsChanged))
268 QPlatformMediaDevices::instance()->initVideoDevicesConnection();
269
270 QObject::connectNotify(signal);
271}
272
273QT_END_NAMESPACE
274
275#include "moc_qmediadevices.cpp"
276

source code of qtmultimedia/src/multimedia/qmediadevices.cpp