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