src/multimedia/audio/qaudiodevicefactory.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 QtMultimedia module of the Qt Toolkit.
       
     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 #include <QtMultimedia/qaudioengine.h>
       
    44 #include <QtMultimedia/qaudioengineplugin.h>
       
    45 #include <private/qfactoryloader_p.h>
       
    46 #include "qaudiodevicefactory_p.h"
       
    47 
       
    48 #if defined(Q_OS_WIN)
       
    49 #include "qaudiodeviceinfo_win32_p.h"
       
    50 #include "qaudiooutput_win32_p.h"
       
    51 #include "qaudioinput_win32_p.h"
       
    52 #elif defined(Q_OS_MAC)
       
    53 #include "qaudiodeviceinfo_mac_p.h"
       
    54 #include "qaudiooutput_mac_p.h"
       
    55 #include "qaudioinput_mac_p.h"
       
    56 #elif defined(HAS_ALSA)
       
    57 #include "qaudiodeviceinfo_alsa_p.h"
       
    58 #include "qaudiooutput_alsa_p.h"
       
    59 #include "qaudioinput_alsa_p.h"
       
    60 #endif
       
    61 
       
    62 QT_BEGIN_NAMESPACE
       
    63 
       
    64 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
       
    65         (QAudioEngineFactoryInterface_iid, QLatin1String("/audio"), Qt::CaseInsensitive))
       
    66 
       
    67 
       
    68 class QNullDeviceInfo : public QAbstractAudioDeviceInfo
       
    69 {
       
    70 public:
       
    71     QAudioFormat preferredFormat() const { qWarning()<<"using null deviceinfo, none available"; return QAudioFormat(); }
       
    72     bool isFormatSupported(const QAudioFormat& ) const { return false; }
       
    73     QAudioFormat nearestFormat(const QAudioFormat& ) const { return QAudioFormat(); }
       
    74     QString deviceName() const { return QString(); }
       
    75     QStringList codecList() { return QStringList(); }
       
    76     QList<int> frequencyList()  { return QList<int>(); }
       
    77     QList<int> channelsList() { return QList<int>(); }
       
    78     QList<int> sampleSizeList() { return QList<int>(); }
       
    79     QList<QAudioFormat::Endian> byteOrderList() { return QList<QAudioFormat::Endian>(); }
       
    80     QList<QAudioFormat::SampleType> sampleTypeList() { return QList<QAudioFormat::SampleType>(); }
       
    81 };
       
    82 
       
    83 class QNullInputDevice : public QAbstractAudioInput
       
    84 {
       
    85 public:
       
    86     QIODevice* start(QIODevice* ) { qWarning()<<"using null input device, none available"; return 0; }
       
    87     void stop() {}
       
    88     void reset() {}
       
    89     void suspend() {}
       
    90     void resume() {}
       
    91     int bytesReady() const { return 0; }
       
    92     int periodSize() const { return 0; }
       
    93     void setBufferSize(int ) {}
       
    94     int bufferSize() const  { return 0; }
       
    95     void setNotifyInterval(int ) {}
       
    96     int notifyInterval() const { return 0; }
       
    97     qint64 totalTime() const { return 0; }
       
    98     qint64 clock() const { return 0; }
       
    99     QAudio::Error error() const { return QAudio::OpenError; }
       
   100     QAudio::State state() const { return QAudio::StopState; }
       
   101     QAudioFormat format() const { return QAudioFormat(); }
       
   102 };
       
   103 
       
   104 class QNullOutputDevice : public QAbstractAudioOutput
       
   105 {
       
   106 public:
       
   107     QIODevice* start(QIODevice* ) { qWarning()<<"using null output device, none available"; return 0; }
       
   108     void stop() {}
       
   109     void reset() {}
       
   110     void suspend() {}
       
   111     void resume() {}
       
   112     int bytesFree() const { return 0; }
       
   113     int periodSize() const { return 0; }
       
   114     void setBufferSize(int ) {}
       
   115     int bufferSize() const  { return 0; }
       
   116     void setNotifyInterval(int ) {}
       
   117     int notifyInterval() const { return 0; }
       
   118     qint64 totalTime() const { return 0; }
       
   119     qint64 clock() const { return 0; }
       
   120     QAudio::Error error() const { return QAudio::OpenError; }
       
   121     QAudio::State state() const { return QAudio::StopState; }
       
   122     QAudioFormat format() const { return QAudioFormat(); }
       
   123 };
       
   124 
       
   125 QList<QAudioDeviceInfo> QAudioDeviceFactory::deviceList(QAudio::Mode mode)
       
   126 {
       
   127     QList<QAudioDeviceInfo> devices;
       
   128 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA))
       
   129     foreach (const QByteArray &handle, QAudioDeviceInfoInternal::deviceList(mode))
       
   130         devices << QAudioDeviceInfo(QLatin1String("builtin"), handle, mode);
       
   131 #endif
       
   132     QFactoryLoader* l = loader();
       
   133 
       
   134     foreach (QString const& key, l->keys()) {
       
   135         QAudioEngineFactoryInterface* plugin = qobject_cast<QAudioEngineFactoryInterface*>(l->instance(key));
       
   136         if (plugin) {
       
   137             foreach (QByteArray const& handle, plugin->deviceList(mode))
       
   138                 devices << QAudioDeviceInfo(key, handle, mode);
       
   139         }
       
   140 
       
   141         delete plugin;
       
   142     }
       
   143 
       
   144     return devices;
       
   145 }
       
   146 
       
   147 QAudioDeviceInfo QAudioDeviceFactory::defaultInputDevice()
       
   148 {
       
   149     QAudioEngineFactoryInterface* plugin = qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(QLatin1String("default")));
       
   150 
       
   151     if (plugin) {
       
   152         QList<QByteArray> list = plugin->deviceList(QAudio::AudioInput);
       
   153         if (list.size() > 0)
       
   154             return QAudioDeviceInfo(QLatin1String("default"), list.at(0), QAudio::AudioInput);
       
   155     }
       
   156 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA))
       
   157     return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultInputDevice(), QAudio::AudioInput);
       
   158 #endif
       
   159     return QAudioDeviceInfo();
       
   160 }
       
   161 
       
   162 QAudioDeviceInfo QAudioDeviceFactory::defaultOutputDevice()
       
   163 {
       
   164     QAudioEngineFactoryInterface* plugin = qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(QLatin1String("default")));
       
   165 
       
   166     if (plugin) {
       
   167         QList<QByteArray> list = plugin->deviceList(QAudio::AudioOutput);
       
   168         if (list.size() > 0)
       
   169             return QAudioDeviceInfo(QLatin1String("default"), list.at(0), QAudio::AudioOutput);
       
   170     }
       
   171 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA))
       
   172     return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultOutputDevice(), QAudio::AudioOutput);
       
   173 #endif
       
   174     return QAudioDeviceInfo();
       
   175 }
       
   176 
       
   177 QAbstractAudioDeviceInfo* QAudioDeviceFactory::audioDeviceInfo(const QString &realm, const QByteArray &handle, QAudio::Mode mode)
       
   178 {
       
   179     QAbstractAudioDeviceInfo *rc = 0;
       
   180 
       
   181 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA))
       
   182     if (realm == QLatin1String("builtin"))
       
   183         return new QAudioDeviceInfoInternal(handle, mode);
       
   184 #endif
       
   185     QAudioEngineFactoryInterface* plugin =
       
   186         qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(realm));
       
   187 
       
   188     if (plugin)
       
   189         rc = plugin->createDeviceInfo(handle, mode);
       
   190 
       
   191     return rc == 0 ? new QNullDeviceInfo() : rc;
       
   192 }
       
   193 
       
   194 QAbstractAudioInput* QAudioDeviceFactory::createDefaultInputDevice(QAudioFormat const &format)
       
   195 {
       
   196     return createInputDevice(defaultInputDevice(), format);
       
   197 }
       
   198 
       
   199 QAbstractAudioOutput* QAudioDeviceFactory::createDefaultOutputDevice(QAudioFormat const &format)
       
   200 {
       
   201     return createOutputDevice(defaultOutputDevice(), format);
       
   202 }
       
   203 
       
   204 QAbstractAudioInput* QAudioDeviceFactory::createInputDevice(QAudioDeviceInfo const& deviceInfo, QAudioFormat const &format)
       
   205 {
       
   206     if (deviceInfo.isNull())
       
   207         return new QNullInputDevice();
       
   208 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA))
       
   209     if (deviceInfo.realm() == QLatin1String("builtin"))
       
   210         return new QAudioInputPrivate(deviceInfo.handle(), format);
       
   211 #endif
       
   212     QAudioEngineFactoryInterface* plugin =
       
   213         qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(deviceInfo.realm()));
       
   214 
       
   215     if (plugin)
       
   216         return plugin->createInput(deviceInfo.handle(), format);
       
   217 
       
   218     return new QNullInputDevice();
       
   219 }
       
   220 
       
   221 QAbstractAudioOutput* QAudioDeviceFactory::createOutputDevice(QAudioDeviceInfo const& deviceInfo, QAudioFormat const &format)
       
   222 {
       
   223     if (deviceInfo.isNull())
       
   224         return new QNullOutputDevice();
       
   225 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA))
       
   226     if (deviceInfo.realm() == QLatin1String("builtin"))
       
   227         return new QAudioOutputPrivate(deviceInfo.handle(), format);
       
   228 #endif
       
   229     QAudioEngineFactoryInterface* plugin =
       
   230         qobject_cast<QAudioEngineFactoryInterface*>(loader()->instance(deviceInfo.realm()));
       
   231 
       
   232     if (plugin)
       
   233         return plugin->createOutput(deviceInfo.handle(), format);
       
   234 
       
   235     return new QNullOutputDevice();
       
   236 }
       
   237 
       
   238 QT_END_NAMESPACE
       
   239