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

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

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