1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtMultimedia module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qaudiodevicefactory_p.h"
|
---|
43 | #include <QtMultimedia/qaudioengine.h>
|
---|
44 | #include <QtMultimedia/qaudiodeviceinfo.h>
|
---|
45 |
|
---|
46 | #include <QtCore/qmap.h>
|
---|
47 |
|
---|
48 | QT_BEGIN_NAMESPACE
|
---|
49 |
|
---|
50 | class QAudioDeviceInfoPrivate : public QSharedData
|
---|
51 | {
|
---|
52 | public:
|
---|
53 | QAudioDeviceInfoPrivate():info(0) {}
|
---|
54 | QAudioDeviceInfoPrivate(const QString &r, const QByteArray &h, QAudio::Mode m):
|
---|
55 | realm(r), handle(h), mode(m)
|
---|
56 | {
|
---|
57 | info = QAudioDeviceFactory::audioDeviceInfo(realm, handle, mode);
|
---|
58 | }
|
---|
59 |
|
---|
60 | QAudioDeviceInfoPrivate(const QAudioDeviceInfoPrivate &other):
|
---|
61 | QSharedData(other),
|
---|
62 | realm(other.realm), handle(other.handle), mode(other.mode)
|
---|
63 | {
|
---|
64 | info = QAudioDeviceFactory::audioDeviceInfo(realm, handle, mode);
|
---|
65 | }
|
---|
66 |
|
---|
67 | QAudioDeviceInfoPrivate& operator=(const QAudioDeviceInfoPrivate &other)
|
---|
68 | {
|
---|
69 | delete info;
|
---|
70 |
|
---|
71 | realm = other.realm;
|
---|
72 | handle = other.handle;
|
---|
73 | mode = other.mode;
|
---|
74 | info = QAudioDeviceFactory::audioDeviceInfo(realm, handle, mode);
|
---|
75 | return *this;
|
---|
76 | }
|
---|
77 |
|
---|
78 | ~QAudioDeviceInfoPrivate()
|
---|
79 | {
|
---|
80 | delete info;
|
---|
81 | }
|
---|
82 |
|
---|
83 | QString realm;
|
---|
84 | QByteArray handle;
|
---|
85 | QAudio::Mode mode;
|
---|
86 | QAbstractAudioDeviceInfo* info;
|
---|
87 | };
|
---|
88 |
|
---|
89 |
|
---|
90 | /*!
|
---|
91 | \class QAudioDeviceInfo
|
---|
92 | \brief The QAudioDeviceInfo class provides an interface to query audio devices and their functionality.
|
---|
93 | \inmodule QtMultimedia
|
---|
94 | \ingroup multimedia
|
---|
95 |
|
---|
96 | \since 4.6
|
---|
97 |
|
---|
98 | QAudioDeviceInfo lets you query for audio devices--such as sound
|
---|
99 | cards and USB headsets--that are currently available on the system.
|
---|
100 | The audio devices available are dependent on the platform or audio plugins installed.
|
---|
101 |
|
---|
102 | You can also query each device for the formats it supports. A
|
---|
103 | format in this context is a set consisting of a specific byte
|
---|
104 | order, channel, codec, frequency, sample rate, and sample type. A
|
---|
105 | format is represented by the QAudioFormat class.
|
---|
106 |
|
---|
107 | The values supported by the the device for each of these
|
---|
108 | parameters can be fetched with
|
---|
109 | supportedByteOrders(), supportedChannelCounts(), supportedCodecs(),
|
---|
110 | supportedSampleRates(), supportedSampleSizes(), and
|
---|
111 | supportedSampleTypes(). The combinations supported are dependent on the platform,
|
---|
112 | audio plugins installed and the audio device capabilities. If you need a specific format, you can check if
|
---|
113 | the device supports it with isFormatSupported(), or fetch a
|
---|
114 | supported format that is as close as possible to the format with
|
---|
115 | nearestFormat(). For instance:
|
---|
116 |
|
---|
117 | \snippet doc/src/snippets/audio/main.cpp 1
|
---|
118 | \dots 8
|
---|
119 | \snippet doc/src/snippets/audio/main.cpp 2
|
---|
120 |
|
---|
121 | A QAudioDeviceInfo is used by Qt to construct
|
---|
122 | classes that communicate with the device--such as
|
---|
123 | QAudioInput, and QAudioOutput. The static
|
---|
124 | functions defaultInputDevice(), defaultOutputDevice(), and
|
---|
125 | availableDevices() let you get a list of all available
|
---|
126 | devices. Devices are fetch according to the value of mode
|
---|
127 | this is specified by the QAudio::Mode enum.
|
---|
128 | The QAudioDeviceInfo returned are only valid for the QAudio::Mode.
|
---|
129 |
|
---|
130 | For instance:
|
---|
131 |
|
---|
132 | \code
|
---|
133 | foreach(const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput))
|
---|
134 | qDebug() << "Device name: " << deviceInfo.deviceName();
|
---|
135 | \endcode
|
---|
136 |
|
---|
137 | In this code sample, we loop through all devices that are able to output
|
---|
138 | sound, i.e., play an audio stream in a supported format. For each device we
|
---|
139 | find, we simply print the deviceName().
|
---|
140 |
|
---|
141 | \sa QAudioOutput, QAudioInput
|
---|
142 | */
|
---|
143 |
|
---|
144 | /*!
|
---|
145 | Constructs an empty QAudioDeviceInfo object.
|
---|
146 | */
|
---|
147 |
|
---|
148 | QAudioDeviceInfo::QAudioDeviceInfo():
|
---|
149 | d(new QAudioDeviceInfoPrivate)
|
---|
150 | {
|
---|
151 | }
|
---|
152 |
|
---|
153 | /*!
|
---|
154 | Constructs a copy of \a other.
|
---|
155 | */
|
---|
156 |
|
---|
157 | QAudioDeviceInfo::QAudioDeviceInfo(const QAudioDeviceInfo& other):
|
---|
158 | d(other.d)
|
---|
159 | {
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*!
|
---|
163 | Destroy this audio device info.
|
---|
164 | */
|
---|
165 |
|
---|
166 | QAudioDeviceInfo::~QAudioDeviceInfo()
|
---|
167 | {
|
---|
168 | }
|
---|
169 |
|
---|
170 | /*!
|
---|
171 | Sets the QAudioDeviceInfo object to be equal to \a other.
|
---|
172 | */
|
---|
173 |
|
---|
174 | QAudioDeviceInfo& QAudioDeviceInfo::operator=(const QAudioDeviceInfo &other)
|
---|
175 | {
|
---|
176 | d = other.d;
|
---|
177 | return *this;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*!
|
---|
181 | Returns whether this QAudioDeviceInfo object holds a device definition.
|
---|
182 | */
|
---|
183 |
|
---|
184 | bool QAudioDeviceInfo::isNull() const
|
---|
185 | {
|
---|
186 | return d->info == 0;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /*!
|
---|
190 | Returns human readable name of audio device.
|
---|
191 |
|
---|
192 | Device names vary depending on platform/audio plugin being used.
|
---|
193 |
|
---|
194 | They are a unique string identifiers for the audio device.
|
---|
195 |
|
---|
196 | eg. default, Intel, U0x46d0x9a4
|
---|
197 | */
|
---|
198 |
|
---|
199 | QString QAudioDeviceInfo::deviceName() const
|
---|
200 | {
|
---|
201 | return isNull() ? QString() : d->info->deviceName();
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*!
|
---|
205 | Returns true if \a settings are supported by the audio device of this QAudioDeviceInfo.
|
---|
206 | */
|
---|
207 |
|
---|
208 | bool QAudioDeviceInfo::isFormatSupported(const QAudioFormat &settings) const
|
---|
209 | {
|
---|
210 | return isNull() ? false : d->info->isFormatSupported(settings);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*!
|
---|
214 | Returns QAudioFormat of default settings.
|
---|
215 |
|
---|
216 | These settings are provided by the platform/audio plugin being used.
|
---|
217 |
|
---|
218 | They also are dependent on the QAudio::Mode being used.
|
---|
219 |
|
---|
220 | A typical audio system would provide something like:
|
---|
221 | \list
|
---|
222 | \o Input settings: 8000Hz mono 8 bit.
|
---|
223 | \o Output settings: 44100Hz stereo 16 bit little endian.
|
---|
224 | \endlist
|
---|
225 | */
|
---|
226 |
|
---|
227 | QAudioFormat QAudioDeviceInfo::preferredFormat() const
|
---|
228 | {
|
---|
229 | return isNull() ? QAudioFormat() : d->info->preferredFormat();
|
---|
230 | }
|
---|
231 |
|
---|
232 | /*!
|
---|
233 | Returns closest QAudioFormat to \a settings that system audio supports.
|
---|
234 |
|
---|
235 | These settings are provided by the platform/audio plugin being used.
|
---|
236 |
|
---|
237 | They also are dependent on the QAudio::Mode being used.
|
---|
238 | */
|
---|
239 |
|
---|
240 | QAudioFormat QAudioDeviceInfo::nearestFormat(const QAudioFormat &settings) const
|
---|
241 | {
|
---|
242 | if (isFormatSupported(settings))
|
---|
243 | return settings;
|
---|
244 |
|
---|
245 | QAudioFormat nearest = settings;
|
---|
246 |
|
---|
247 | nearest.setCodec(QLatin1String("audio/pcm"));
|
---|
248 |
|
---|
249 | if (nearest.sampleType() == QAudioFormat::Unknown) {
|
---|
250 | QAudioFormat preferred = preferredFormat();
|
---|
251 | nearest.setSampleType(preferred.sampleType());
|
---|
252 | }
|
---|
253 |
|
---|
254 | QMap<int,int> testFrequencies;
|
---|
255 | QList<int> frequenciesAvailable = supportedFrequencies();
|
---|
256 | QMap<int,int> testSampleSizes;
|
---|
257 | QList<int> sampleSizesAvailable = supportedSampleSizes();
|
---|
258 |
|
---|
259 | // Get sorted sampleSizes (equal to and ascending values only)
|
---|
260 | if (sampleSizesAvailable.contains(settings.sampleSize()))
|
---|
261 | testSampleSizes.insert(0,settings.sampleSize());
|
---|
262 | sampleSizesAvailable.removeAll(settings.sampleSize());
|
---|
263 | foreach (int size, sampleSizesAvailable) {
|
---|
264 | int larger = (size > settings.sampleSize()) ? size : settings.sampleSize();
|
---|
265 | int smaller = (size > settings.sampleSize()) ? settings.sampleSize() : size;
|
---|
266 | if (size >= settings.sampleSize()) {
|
---|
267 | int diff = larger - smaller;
|
---|
268 | testSampleSizes.insert(diff, size);
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | // Get sorted frequencies (equal to and ascending values only)
|
---|
273 | if (frequenciesAvailable.contains(settings.frequency()))
|
---|
274 | testFrequencies.insert(0,settings.frequency());
|
---|
275 | frequenciesAvailable.removeAll(settings.frequency());
|
---|
276 | foreach (int frequency, frequenciesAvailable) {
|
---|
277 | int larger = (frequency > settings.frequency()) ? frequency : settings.frequency();
|
---|
278 | int smaller = (frequency > settings.frequency()) ? settings.frequency() : frequency;
|
---|
279 | if (frequency >= settings.frequency()) {
|
---|
280 | int diff = larger - smaller;
|
---|
281 | testFrequencies.insert(diff, frequency);
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | // Try to find nearest
|
---|
286 | // Check ascending frequencies, ascending sampleSizes
|
---|
287 | QMapIterator<int, int> sz(testSampleSizes);
|
---|
288 | while (sz.hasNext()) {
|
---|
289 | sz.next();
|
---|
290 | nearest.setSampleSize(sz.value());
|
---|
291 | QMapIterator<int, int> i(testFrequencies);
|
---|
292 | while (i.hasNext()) {
|
---|
293 | i.next();
|
---|
294 | nearest.setFrequency(i.value());
|
---|
295 | if (isFormatSupported(nearest))
|
---|
296 | return nearest;
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | //Fallback
|
---|
301 | return preferredFormat();
|
---|
302 | }
|
---|
303 |
|
---|
304 | /*!
|
---|
305 | Returns a list of supported codecs.
|
---|
306 |
|
---|
307 | All platform and plugin implementations should provide support for:
|
---|
308 |
|
---|
309 | "audio/pcm" - Linear PCM
|
---|
310 |
|
---|
311 | For writing plugins to support additional codecs refer to:
|
---|
312 |
|
---|
313 | http://www.iana.org/assignments/media-types/audio/
|
---|
314 | */
|
---|
315 |
|
---|
316 | QStringList QAudioDeviceInfo::supportedCodecs() const
|
---|
317 | {
|
---|
318 | return isNull() ? QStringList() : d->info->codecList();
|
---|
319 | }
|
---|
320 |
|
---|
321 | /*!
|
---|
322 | Returns a list of supported sample rates.
|
---|
323 |
|
---|
324 | \since 4.7
|
---|
325 | */
|
---|
326 |
|
---|
327 | QList<int> QAudioDeviceInfo::supportedSampleRates() const
|
---|
328 | {
|
---|
329 | return supportedFrequencies();
|
---|
330 | }
|
---|
331 |
|
---|
332 | /*!
|
---|
333 | \obsolete
|
---|
334 |
|
---|
335 | Use supportedSampleRates() instead.
|
---|
336 | */
|
---|
337 |
|
---|
338 | QList<int> QAudioDeviceInfo::supportedFrequencies() const
|
---|
339 | {
|
---|
340 | return isNull() ? QList<int>() : d->info->frequencyList();
|
---|
341 | }
|
---|
342 |
|
---|
343 | /*!
|
---|
344 | Returns a list of supported channel counts.
|
---|
345 |
|
---|
346 | \since 4.7
|
---|
347 | */
|
---|
348 |
|
---|
349 | QList<int> QAudioDeviceInfo::supportedChannelCounts() const
|
---|
350 | {
|
---|
351 | return supportedChannels();
|
---|
352 | }
|
---|
353 |
|
---|
354 | /*!
|
---|
355 | \obsolete
|
---|
356 |
|
---|
357 | Use supportedChannelCount() instead.
|
---|
358 | */
|
---|
359 |
|
---|
360 | QList<int> QAudioDeviceInfo::supportedChannels() const
|
---|
361 | {
|
---|
362 | return isNull() ? QList<int>() : d->info->channelsList();
|
---|
363 | }
|
---|
364 |
|
---|
365 | /*!
|
---|
366 | Returns a list of supported sample sizes.
|
---|
367 | */
|
---|
368 |
|
---|
369 | QList<int> QAudioDeviceInfo::supportedSampleSizes() const
|
---|
370 | {
|
---|
371 | return isNull() ? QList<int>() : d->info->sampleSizeList();
|
---|
372 | }
|
---|
373 |
|
---|
374 | /*!
|
---|
375 | Returns a list of supported byte orders.
|
---|
376 | */
|
---|
377 |
|
---|
378 | QList<QAudioFormat::Endian> QAudioDeviceInfo::supportedByteOrders() const
|
---|
379 | {
|
---|
380 | return isNull() ? QList<QAudioFormat::Endian>() : d->info->byteOrderList();
|
---|
381 | }
|
---|
382 |
|
---|
383 | /*!
|
---|
384 | Returns a list of supported sample types.
|
---|
385 | */
|
---|
386 |
|
---|
387 | QList<QAudioFormat::SampleType> QAudioDeviceInfo::supportedSampleTypes() const
|
---|
388 | {
|
---|
389 | return isNull() ? QList<QAudioFormat::SampleType>() : d->info->sampleTypeList();
|
---|
390 | }
|
---|
391 |
|
---|
392 | /*!
|
---|
393 | Returns the name of the default input audio device.
|
---|
394 | All platform and audio plugin implementations provide a default audio device to use.
|
---|
395 | */
|
---|
396 |
|
---|
397 | QAudioDeviceInfo QAudioDeviceInfo::defaultInputDevice()
|
---|
398 | {
|
---|
399 | return QAudioDeviceFactory::defaultInputDevice();
|
---|
400 | }
|
---|
401 |
|
---|
402 | /*!
|
---|
403 | Returns the name of the default output audio device.
|
---|
404 | All platform and audio plugin implementations provide a default audio device to use.
|
---|
405 | */
|
---|
406 |
|
---|
407 | QAudioDeviceInfo QAudioDeviceInfo::defaultOutputDevice()
|
---|
408 | {
|
---|
409 | return QAudioDeviceFactory::defaultOutputDevice();
|
---|
410 | }
|
---|
411 |
|
---|
412 | /*!
|
---|
413 | Returns a list of audio devices that support \a mode.
|
---|
414 | */
|
---|
415 |
|
---|
416 | QList<QAudioDeviceInfo> QAudioDeviceInfo::availableDevices(QAudio::Mode mode)
|
---|
417 | {
|
---|
418 | return QAudioDeviceFactory::availableDevices(mode);
|
---|
419 | }
|
---|
420 |
|
---|
421 |
|
---|
422 | /*!
|
---|
423 | \internal
|
---|
424 | */
|
---|
425 |
|
---|
426 | QAudioDeviceInfo::QAudioDeviceInfo(const QString &realm, const QByteArray &handle, QAudio::Mode mode):
|
---|
427 | d(new QAudioDeviceInfoPrivate(realm, handle, mode))
|
---|
428 | {
|
---|
429 | }
|
---|
430 |
|
---|
431 | /*!
|
---|
432 | \internal
|
---|
433 | */
|
---|
434 |
|
---|
435 | QString QAudioDeviceInfo::realm() const
|
---|
436 | {
|
---|
437 | return d->realm;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /*!
|
---|
441 | \internal
|
---|
442 | */
|
---|
443 |
|
---|
444 | QByteArray QAudioDeviceInfo::handle() const
|
---|
445 | {
|
---|
446 | return d->handle;
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /*!
|
---|
451 | \internal
|
---|
452 | */
|
---|
453 |
|
---|
454 | QAudio::Mode QAudioDeviceInfo::mode() const
|
---|
455 | {
|
---|
456 | return d->mode;
|
---|
457 | }
|
---|
458 |
|
---|
459 | QT_END_NAMESPACE
|
---|
460 |
|
---|