qtmobility/src/multimedia/audio/qaudiodevicefactory.cpp
changeset 14 6fbed849b4f4
child 15 1f895d8a5b2b
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtCore/qdebug.h>
       
    43 
       
    44 #include "qaudiosystem.h"
       
    45 #include "qaudiosystemplugin.h"
       
    46 
       
    47 #include <private/qfactoryloader_p.h>
       
    48 #include "qaudiodevicefactory_p.h"
       
    49 
       
    50 #ifndef QT_NO_AUDIO_BACKEND
       
    51 #if defined(Q_OS_WIN)
       
    52 #include "qaudiodeviceinfo_win32_p.h"
       
    53 #include "qaudiooutput_win32_p.h"
       
    54 #include "qaudioinput_win32_p.h"
       
    55 #elif defined(Q_OS_MAC)
       
    56 #include "qaudiodeviceinfo_mac_p.h"
       
    57 #include "qaudiooutput_mac_p.h"
       
    58 #include "qaudioinput_mac_p.h"
       
    59 #elif defined(HAS_ALSA)
       
    60 #include "qaudiodeviceinfo_alsa_p.h"
       
    61 #include "qaudiooutput_alsa_p.h"
       
    62 #include "qaudioinput_alsa_p.h"
       
    63 #elif defined(Q_OS_SYMBIAN)
       
    64 #include "qaudiodeviceinfo_symbian_p.h"
       
    65 #include "qaudiooutput_symbian_p.h"
       
    66 #include "qaudioinput_symbian_p.h"
       
    67 #endif
       
    68 #endif
       
    69 
       
    70 QT_BEGIN_NAMESPACE
       
    71 
       
    72 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
    73 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
       
    74         (QAudioSystemFactoryInterface_iid, QLatin1String("/audio"), Qt::CaseInsensitive))
       
    75 #endif
       
    76 
       
    77 class QNullDeviceInfo : public QAbstractAudioDeviceInfo
       
    78 {
       
    79 public:
       
    80     QAudioFormat preferredFormat() const { qWarning()<<"using null deviceinfo, none available"; return QAudioFormat(); }
       
    81     bool isFormatSupported(const QAudioFormat& ) const { return false; }
       
    82     QAudioFormat nearestFormat(const QAudioFormat& ) const { return QAudioFormat(); }
       
    83     QString deviceName() const { return QString(); }
       
    84     QStringList supportedCodecs() { return QStringList(); }
       
    85     QList<int> supportedSampleRates()  { return QList<int>(); }
       
    86     QList<int> supportedChannelCounts() { return QList<int>(); }
       
    87     QList<int> supportedSampleSizes() { return QList<int>(); }
       
    88     QList<QAudioFormat::Endian> supportedByteOrders() { return QList<QAudioFormat::Endian>(); }
       
    89     QList<QAudioFormat::SampleType> supportedSampleTypes() { return QList<QAudioFormat::SampleType>(); }
       
    90 };
       
    91 
       
    92 class QNullInputDevice : public QAbstractAudioInput
       
    93 {
       
    94 public:
       
    95     void start(QIODevice*) { qWarning()<<"using null input device, none available";}
       
    96     QIODevice* start() { qWarning()<<"using null input device, none available"; return 0; }
       
    97     void stop() {}
       
    98     void reset() {}
       
    99     void suspend() {}
       
   100     void resume() {}
       
   101     int bytesReady() const { return 0; }
       
   102     int periodSize() const { return 0; }
       
   103     void setBufferSize(int ) {}
       
   104     int bufferSize() const  { return 0; }
       
   105     void setNotifyInterval(int ) {}
       
   106     int notifyInterval() const { return 0; }
       
   107     qint64 processedUSecs() const { return 0; }
       
   108     qint64 elapsedUSecs() const { return 0; }
       
   109     QAudio::Error error() const { return QAudio::OpenError; }
       
   110     QAudio::State state() const { return QAudio::StoppedState; }
       
   111     void setFormat(const QAudioFormat&) {}
       
   112     QAudioFormat format() const { return QAudioFormat(); }
       
   113 };
       
   114 
       
   115 class QNullOutputDevice : public QAbstractAudioOutput
       
   116 {
       
   117 public:
       
   118     void start(QIODevice*) {qWarning()<<"using null output device, none available";}
       
   119     QIODevice* start() { qWarning()<<"using null output device, none available"; return 0; }
       
   120     void stop() {}
       
   121     void reset() {}
       
   122     void suspend() {}
       
   123     void resume() {}
       
   124     int bytesFree() const { return 0; }
       
   125     int periodSize() const { return 0; }
       
   126     void setBufferSize(int ) {}
       
   127     int bufferSize() const  { return 0; }
       
   128     void setNotifyInterval(int ) {}
       
   129     int notifyInterval() const { return 0; }
       
   130     qint64 processedUSecs() const { return 0; }
       
   131     qint64 elapsedUSecs() const { return 0; }
       
   132     QAudio::Error error() const { return QAudio::OpenError; }
       
   133     QAudio::State state() const { return QAudio::StoppedState; }
       
   134     void setFormat(const QAudioFormat&) {}
       
   135     QAudioFormat format() const { return QAudioFormat(); }
       
   136 };
       
   137 
       
   138 QList<QAudioDeviceInfo> QAudioDeviceFactory::availableDevices(QAudio::Mode mode)
       
   139 {
       
   140     QList<QAudioDeviceInfo> devices;
       
   141 #ifndef QT_NO_AUDIO_BACKEND
       
   142 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   143     foreach (const QByteArray &handle, QAudioDeviceInfoInternal::availableDevices(mode))
       
   144         devices << QAudioDeviceInfo(QLatin1String("builtin"), handle, mode);
       
   145 #endif
       
   146 #endif
       
   147 
       
   148 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   149     QFactoryLoader* l = loader();
       
   150 
       
   151     foreach (QString const& key, l->keys()) {
       
   152         QAudioSystemFactoryInterface* plugin = qobject_cast<QAudioSystemFactoryInterface*>(l->instance(key));
       
   153         if (plugin) {
       
   154             foreach (QByteArray const& handle, plugin->availableDevices(mode))
       
   155                 devices << QAudioDeviceInfo(key, handle, mode);
       
   156         }
       
   157 
       
   158         delete plugin;
       
   159     }
       
   160 #endif
       
   161 
       
   162     return devices;
       
   163 }
       
   164 
       
   165 QAudioDeviceInfo QAudioDeviceFactory::defaultInputDevice()
       
   166 {
       
   167 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   168     QAudioSystemFactoryInterface* plugin = qobject_cast<QAudioSystemFactoryInterface*>(loader()->instance(QLatin1String("default")));
       
   169 
       
   170     if (plugin) {
       
   171         QList<QByteArray> list = plugin->availableDevices(QAudio::AudioInput);
       
   172         if (list.size() > 0)
       
   173             return QAudioDeviceInfo(QLatin1String("default"), list.at(0), QAudio::AudioInput);
       
   174     }
       
   175 #endif
       
   176 
       
   177 #ifndef QT_NO_AUDIO_BACKEND
       
   178 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   179     return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultInputDevice(), QAudio::AudioInput);
       
   180 #endif
       
   181 #endif
       
   182     return QAudioDeviceInfo();
       
   183 }
       
   184 
       
   185 QAudioDeviceInfo QAudioDeviceFactory::defaultOutputDevice()
       
   186 {
       
   187 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   188     QAudioSystemFactoryInterface* plugin = qobject_cast<QAudioSystemFactoryInterface*>(loader()->instance(QLatin1String("default")));
       
   189 
       
   190     if (plugin) {
       
   191         QList<QByteArray> list = plugin->availableDevices(QAudio::AudioOutput);
       
   192         if (list.size() > 0)
       
   193             return QAudioDeviceInfo(QLatin1String("default"), list.at(0), QAudio::AudioOutput);
       
   194     }
       
   195 #endif
       
   196 
       
   197 #ifndef QT_NO_AUDIO_BACKEND
       
   198 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   199     return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultOutputDevice(), QAudio::AudioOutput);
       
   200 #endif
       
   201 #endif
       
   202     return QAudioDeviceInfo();
       
   203 }
       
   204 
       
   205 QAbstractAudioDeviceInfo* QAudioDeviceFactory::audioDeviceInfo(const QString &realm, const QByteArray &handle, QAudio::Mode mode)
       
   206 {
       
   207     QAbstractAudioDeviceInfo *rc = 0;
       
   208 
       
   209 #ifndef QT_NO_AUDIO_BACKEND
       
   210 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   211     if (realm == QLatin1String("builtin"))
       
   212         return new QAudioDeviceInfoInternal(handle, mode);
       
   213 #endif
       
   214 #endif
       
   215 
       
   216 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   217     QAudioSystemFactoryInterface* plugin =
       
   218         qobject_cast<QAudioSystemFactoryInterface*>(loader()->instance(realm));
       
   219 
       
   220     if (plugin)
       
   221         rc = plugin->createDeviceInfo(handle, mode);
       
   222 #endif
       
   223 
       
   224     return rc == 0 ? new QNullDeviceInfo() : rc;
       
   225 }
       
   226 
       
   227 QAbstractAudioInput* QAudioDeviceFactory::createDefaultInputDevice(QAudioFormat const &format)
       
   228 {
       
   229     return createInputDevice(defaultInputDevice(), format);
       
   230 }
       
   231 
       
   232 QAbstractAudioOutput* QAudioDeviceFactory::createDefaultOutputDevice(QAudioFormat const &format)
       
   233 {
       
   234     return createOutputDevice(defaultOutputDevice(), format);
       
   235 }
       
   236 
       
   237 QAbstractAudioInput* QAudioDeviceFactory::createInputDevice(QAudioDeviceInfo const& deviceInfo, QAudioFormat const &format)
       
   238 {
       
   239     if (deviceInfo.isNull())
       
   240         return new QNullInputDevice();
       
   241 #ifndef QT_NO_AUDIO_BACKEND
       
   242 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   243     if (deviceInfo.realm() == QLatin1String("builtin")) {
       
   244         QAbstractAudioInput* p = new QAudioInputPrivate(deviceInfo.handle());
       
   245         if (p) p->setFormat(format);
       
   246         return p;
       
   247     }
       
   248 #endif
       
   249 #endif
       
   250 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   251     QAudioSystemFactoryInterface* plugin =
       
   252         qobject_cast<QAudioSystemFactoryInterface*>(loader()->instance(deviceInfo.realm()));
       
   253 
       
   254     if (plugin) {
       
   255         QAbstractAudioInput* p = plugin->createInput(deviceInfo.handle());
       
   256         if (p) p->setFormat(format);
       
   257         return p;
       
   258     }
       
   259 #endif
       
   260 
       
   261     return new QNullInputDevice();
       
   262 }
       
   263 
       
   264 QAbstractAudioOutput* QAudioDeviceFactory::createOutputDevice(QAudioDeviceInfo const& deviceInfo, QAudioFormat const &format)
       
   265 {
       
   266     if (deviceInfo.isNull())
       
   267         return new QNullOutputDevice();
       
   268 #ifndef QT_NO_AUDIO_BACKEND
       
   269 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   270     if (deviceInfo.realm() == QLatin1String("builtin")) {
       
   271         QAbstractAudioOutput* p = new QAudioOutputPrivate(deviceInfo.handle());
       
   272         if (p) p->setFormat(format);
       
   273         return p;
       
   274     }
       
   275 #endif
       
   276 #endif
       
   277 
       
   278 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   279     QAudioSystemFactoryInterface* plugin =
       
   280         qobject_cast<QAudioSystemFactoryInterface*>(loader()->instance(deviceInfo.realm()));
       
   281 
       
   282     if (plugin) {
       
   283         QAbstractAudioOutput* p = plugin->createOutput(deviceInfo.handle());
       
   284         if (p) p->setFormat(format);
       
   285         return p;
       
   286     }
       
   287 #endif
       
   288 
       
   289     return new QNullOutputDevice();
       
   290 }
       
   291 
       
   292 QT_END_NAMESPACE
       
   293