source: trunk/src/multimedia/audio/qaudiodevicefactory.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 10.1 KB
Line 
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 <QtCore/qdebug.h>
43#include <QtMultimedia/qaudioengine.h>
44#include <QtMultimedia/qaudioengineplugin.h>
45#include <private/qfactoryloader_p.h>
46#include "qaudiodevicefactory_p.h"
47
48#ifndef QT_NO_AUDIO_BACKEND
49#if defined(Q_OS_WIN)
50#include "qaudiodeviceinfo_win32_p.h"
51#include "qaudiooutput_win32_p.h"
52#include "qaudioinput_win32_p.h"
53#elif defined(Q_OS_MAC)
54#include "qaudiodeviceinfo_mac_p.h"
55#include "qaudiooutput_mac_p.h"
56#include "qaudioinput_mac_p.h"
57#elif defined(HAS_ALSA)
58#include "qaudiodeviceinfo_alsa_p.h"
59#include "qaudiooutput_alsa_p.h"
60#include "qaudioinput_alsa_p.h"
61#elif defined(Q_OS_SYMBIAN)
62#include "qaudiodeviceinfo_symbian_p.h"
63#include "qaudiooutput_symbian_p.h"
64#include "qaudioinput_symbian_p.h"
65#endif
66#endif
67
68QT_BEGIN_NAMESPACE
69
70
71#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
72Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
73 (QAudioEngineFactoryInterface_iid, QLatin1String("/audio"), Qt::CaseInsensitive))
74#endif
75
76class QNullDeviceInfo : public QAbstractAudioDeviceInfo
77{
78public:
79 QAudioFormat preferredFormat() const { qWarning()<<"using null deviceinfo, none available"; return QAudioFormat(); }
80 bool isFormatSupported(const QAudioFormat& ) const { return false; }
81 QAudioFormat nearestFormat(const QAudioFormat& ) const { return QAudioFormat(); }
82 QString deviceName() const { return QString(); }
83 QStringList codecList() { return QStringList(); }
84 QList<int> frequencyList() { return QList<int>(); }
85 QList<int> channelsList() { return QList<int>(); }
86 QList<int> sampleSizeList() { return QList<int>(); }
87 QList<QAudioFormat::Endian> byteOrderList() { return QList<QAudioFormat::Endian>(); }
88 QList<QAudioFormat::SampleType> sampleTypeList() { return QList<QAudioFormat::SampleType>(); }
89};
90
91class QNullInputDevice : public QAbstractAudioInput
92{
93public:
94 QIODevice* start(QIODevice* ) { qWarning()<<"using null input device, none available"; return 0; }
95 void stop() {}
96 void reset() {}
97 void suspend() {}
98 void resume() {}
99 int bytesReady() const { return 0; }
100 int periodSize() const { return 0; }
101 void setBufferSize(int ) {}
102 int bufferSize() const { return 0; }
103 void setNotifyInterval(int ) {}
104 int notifyInterval() const { return 0; }
105 qint64 processedUSecs() const { return 0; }
106 qint64 elapsedUSecs() const { return 0; }
107 QAudio::Error error() const { return QAudio::OpenError; }
108 QAudio::State state() const { return QAudio::StoppedState; }
109 QAudioFormat format() const { return QAudioFormat(); }
110};
111
112class QNullOutputDevice : public QAbstractAudioOutput
113{
114public:
115 QIODevice* start(QIODevice* ) { qWarning()<<"using null output device, none available"; return 0; }
116 void stop() {}
117 void reset() {}
118 void suspend() {}
119 void resume() {}
120 int bytesFree() const { return 0; }
121 int periodSize() const { return 0; }
122 void setBufferSize(int ) {}
123 int bufferSize() const { return 0; }
124 void setNotifyInterval(int ) {}
125 int notifyInterval() const { return 0; }
126 qint64 processedUSecs() const { return 0; }
127 qint64 elapsedUSecs() const { return 0; }
128 QAudio::Error error() const { return QAudio::OpenError; }
129 QAudio::State state() const { return QAudio::StoppedState; }
130 QAudioFormat format() const { return QAudioFormat(); }
131};
132
133QList<QAudioDeviceInfo> QAudioDeviceFactory::availableDevices(QAudio::Mode mode)
134{
135 QList<QAudioDeviceInfo> devices;
136#ifndef QT_NO_AUDIO_BACKEND
137#if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
138 foreach (const QByteArray &handle, QAudioDeviceInfoInternal::availableDevices(mode))
139 devices << QAudioDeviceInfo(QLatin1String("builtin"), handle, mode);
140#endif
141#endif
142#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
143 QFactoryLoader* l = loader();
144
145 foreach (QString const& key, l->keys()) {
146 QAudioEngineFactoryInterface* plugin = qobject_cast<QAudioEngineFactoryInterface*>(l->instance(key));
147 if (plugin) {
148 foreach (QByteArray const& handle, plugin->availableDevices(mode))
149 devices << QAudioDeviceInfo(key, handle, mode);
150 }
151
152 delete plugin;
153 }
154#endif
155 return devices;
156}
157
158QAudioDeviceInfo QAudioDeviceFactory::defaultInputDevice()
159{
160#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
161 QAudioEngineFactoryInterface* plugin = qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(QLatin1String("default")));
162
163 if (plugin) {
164 QList<QByteArray> list = plugin->availableDevices(QAudio::AudioInput);
165 if (list.size() > 0)
166 return QAudioDeviceInfo(QLatin1String("default"), list.at(0), QAudio::AudioInput);
167 }
168#endif
169
170#ifndef QT_NO_AUDIO_BACKEND
171#if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
172 return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultInputDevice(), QAudio::AudioInput);
173#endif
174#endif
175 return QAudioDeviceInfo();
176}
177
178QAudioDeviceInfo QAudioDeviceFactory::defaultOutputDevice()
179{
180#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
181 QAudioEngineFactoryInterface* plugin = qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(QLatin1String("default")));
182
183 if (plugin) {
184 QList<QByteArray> list = plugin->availableDevices(QAudio::AudioOutput);
185 if (list.size() > 0)
186 return QAudioDeviceInfo(QLatin1String("default"), list.at(0), QAudio::AudioOutput);
187 }
188#endif
189
190#ifndef QT_NO_AUDIO_BACKEND
191#if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
192 return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultOutputDevice(), QAudio::AudioOutput);
193#endif
194#endif
195 return QAudioDeviceInfo();
196}
197
198QAbstractAudioDeviceInfo* QAudioDeviceFactory::audioDeviceInfo(const QString &realm, const QByteArray &handle, QAudio::Mode mode)
199{
200 QAbstractAudioDeviceInfo *rc = 0;
201
202#ifndef QT_NO_AUDIO_BACKEND
203#if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
204 if (realm == QLatin1String("builtin"))
205 return new QAudioDeviceInfoInternal(handle, mode);
206#endif
207#endif
208#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
209 QAudioEngineFactoryInterface* plugin =
210 qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(realm));
211
212 if (plugin)
213 rc = plugin->createDeviceInfo(handle, mode);
214#endif
215 return rc == 0 ? new QNullDeviceInfo() : rc;
216}
217
218QAbstractAudioInput* QAudioDeviceFactory::createDefaultInputDevice(QAudioFormat const &format)
219{
220 return createInputDevice(defaultInputDevice(), format);
221}
222
223QAbstractAudioOutput* QAudioDeviceFactory::createDefaultOutputDevice(QAudioFormat const &format)
224{
225 return createOutputDevice(defaultOutputDevice(), format);
226}
227
228QAbstractAudioInput* QAudioDeviceFactory::createInputDevice(QAudioDeviceInfo const& deviceInfo, QAudioFormat const &format)
229{
230 if (deviceInfo.isNull())
231 return new QNullInputDevice();
232#ifndef QT_NO_AUDIO_BACKEND
233#if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
234 if (deviceInfo.realm() == QLatin1String("builtin"))
235 return new QAudioInputPrivate(deviceInfo.handle(), format);
236#endif
237#endif
238#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
239 QAudioEngineFactoryInterface* plugin =
240 qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(deviceInfo.realm()));
241
242 if (plugin)
243 return plugin->createInput(deviceInfo.handle(), format);
244#endif
245 return new QNullInputDevice();
246}
247
248QAbstractAudioOutput* QAudioDeviceFactory::createOutputDevice(QAudioDeviceInfo const& deviceInfo, QAudioFormat const &format)
249{
250 if (deviceInfo.isNull())
251 return new QNullOutputDevice();
252#ifndef QT_NO_AUDIO_BACKEND
253#if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
254 if (deviceInfo.realm() == QLatin1String("builtin"))
255 return new QAudioOutputPrivate(deviceInfo.handle(), format);
256#endif
257#endif
258#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
259 QAudioEngineFactoryInterface* plugin =
260 qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(deviceInfo.realm()));
261
262 if (plugin)
263 return plugin->createOutput(deviceInfo.handle(), format);
264#endif
265 return new QNullOutputDevice();
266}
267
268QT_END_NAMESPACE
269
Note: See TracBrowser for help on using the repository browser.