Qt 4.8
qaudiodevicefactory.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtMultimedia module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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)
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)
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 
69 
70 
71 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
74 #endif
75 
76 class QNullDeviceInfo : public QAbstractAudioDeviceInfo
77 {
78 public:
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>(); }
89 };
90 
92 {
93 public:
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; }
109  QAudioFormat format() const { return QAudioFormat(); }
110 };
111 
113 {
114 public:
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; }
130  QAudioFormat format() const { return QAudioFormat(); }
131 };
132 
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()) {
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 
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) {
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))
173 #endif
174 #endif
175  return QAudioDeviceInfo();
176 }
177 
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) {
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))
193 #endif
194 #endif
195  return QAudioDeviceInfo();
196 }
197 
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)
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 
219 {
220  return createInputDevice(defaultInputDevice(), format);
221 }
222 
224 {
225  return createOutputDevice(defaultOutputDevice(), format);
226 }
227 
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)
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 
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)
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 
269 
T qobject_cast(QObject *object)
Definition: qobject.h:375
static QByteArray defaultOutputDevice()
void stop()
Stops the audio input.
Error
Definition: qaudio.h:58
QByteArray handle() const
void setBufferSize(int)
Sets the audio buffer size to value in bytes.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void reset()
Drops all audio data in the buffers, resets buffers to zero.
virtual QAbstractAudioDeviceInfo * createDeviceInfo(const QByteArray &device, QAudio::Mode mode)=0
static QList< QAudioDeviceInfo > availableDevices(QAudio::Mode mode)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
qint64 processedUSecs() const
Returns the amount of audio data processed since start() was called in milliseconds.
int bytesFree() const
Returns the free space available in bytes in the audio buffer.
The QAbstractAudioOutput class provides access for QAudioOutput to access the audio device provided b...
Definition: qaudioengine.h:73
QString realm() const
QAudio::State state() const
Returns the state of audio processing.
void setNotifyInterval(int)
Sets the interval for notify() signal to be emitted.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
virtual QList< int > sampleSizeList()=0
Returns the list of currently available sample sizes.
virtual QList< QByteArray > availableDevices(QAudio::Mode) const =0
void resume()
Resumes processing audio data after a suspend().
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QAbstractAudioInput * createInput(const QByteArray &device, const QAudioFormat &format=QAudioFormat())=0
virtual bool isFormatSupported(const QAudioFormat &format) const =0
Returns true if format is available from audio device.
void suspend()
Stops processing audio data, preserving buffered audio data.
void reset()
Drops all audio data in the buffers, resets buffers to zero.
QAudio::Error error() const
Returns the error state.
static QByteArray defaultInputDevice()
int periodSize() const
Returns the period size in bytes.
QAudio::Error error() const
Returns the error state.
int bufferSize() const
Returns the audio buffer size in bytes.
QStringList keys() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int bufferSize() const
Returns the audio buffer size in milliseconds.
QAudioFormat format() const
Returns the QAudioFormat being used.
qint64 elapsedUSecs() const
Returns the milliseconds since start() was called, including time in Idle and suspend states...
virtual QAudioFormat nearestFormat(const QAudioFormat &format) const =0
Returns the nearest settings format.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT void qWarning(const char *,...)
The QAudioDeviceInfo class provides an interface to query audio devices and their functionality...
__int64 qint64
Definition: qglobal.h:942
The QAbstractAudioInput class provides access for QAudioInput to access the audio device provided by ...
Definition: qaudioengine.h:100
virtual QString deviceName() const =0
Returns the audio device name.
virtual QStringList codecList()=0
Returns the list of currently available codecs.
void setBufferSize(int)
Sets the audio buffer size to value in milliseconds.
Mode
Definition: qaudio.h:60
QIODevice * start(QIODevice *)
Uses the device as the QIODevice to transfer data.
virtual QAudioFormat preferredFormat() const =0
Returns the nearest settings.
QIODevice * start(QIODevice *)
Uses the device as the QIODevice to transfer data.
void setNotifyInterval(int)
Sets the interval for notify() signal to be emitted.
QAudioFormat format() const
Returns the QAudioFormat being used.
int notifyInterval() const
Returns the notify interval in milliseconds.
int bytesReady() const
Returns the amount of audio data available to read in bytes.
qint64 elapsedUSecs() const
Returns the milliseconds since start() was called, including time in Idle and suspend states...
State
Definition: qaudio.h:59
QObject * instance(const QString &key) const
static QAudioDeviceInfo defaultInputDevice()
static QAbstractAudioInput * createDefaultInputDevice(QAudioFormat const &format)
int key
#define QAudioEngineFactoryInterface_iid
void resume()
Resumes processing audio data after a suspend()
void stop()
Stops the audio output.
virtual QList< int > channelsList()=0
Returns the list of currently available channels.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QFactoryLoader * l
The QAbstractAudioDeviceInfo class provides access for QAudioDeviceInfo to access the audio device pr...
Definition: qaudioengine.h:56
static QAbstractAudioInput * createInputDevice(QAudioDeviceInfo const &device, QAudioFormat const &format)
QAudio::State state() const
Returns the state of audio processing.
int notifyInterval() const
Returns the notify interval in milliseconds.
static QAudioDeviceInfo defaultOutputDevice()
int periodSize() const
Returns the period size in bytes.
bool isNull() const
Returns whether this QAudioDeviceInfo object holds a device definition.
The QAudioFormat class stores audio parameter information.
Definition: qaudioformat.h:60
qint64 processedUSecs() const
Returns the amount of audio data processed since start() was called in milliseconds.
static QList< QByteArray > availableDevices(QAudio::Mode)
static QAbstractAudioDeviceInfo * audioDeviceInfo(const QString &realm, const QByteArray &handle, QAudio::Mode mode)
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
void suspend()
Stops processing audio data, preserving buffered audio data.
virtual QAbstractAudioOutput * createOutput(const QByteArray &device, const QAudioFormat &format=QAudioFormat())=0
static QAbstractAudioOutput * createDefaultOutputDevice(QAudioFormat const &format)
virtual QList< QAudioFormat::SampleType > sampleTypeList()=0
Returns the list of currently available sample types.
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
Declares a global static variable with the specified type and name.
Definition: qglobal.h:1982
static QAbstractAudioOutput * createOutputDevice(QAudioDeviceInfo const &device, QAudioFormat const &format)
virtual QList< int > frequencyList()=0
Returns the list of currently available frequencies.
virtual QList< QAudioFormat::Endian > byteOrderList()=0
Returns the list of currently available byte orders.