src/multimedia/audio/qaudiodevicefactory.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     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 "qaudiopluginloader_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(QAudioPluginLoader, audioLoader,
       
    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     QAudioPluginLoader* l = audioLoader();
       
   150     foreach (const QString& key, l->keys()) {
       
   151         QAudioSystemFactoryInterface* plugin = qobject_cast<QAudioSystemFactoryInterface*>(l->instance(key));
       
   152         if (plugin) {
       
   153             foreach (QByteArray const& handle, plugin->availableDevices(mode))
       
   154                 devices << QAudioDeviceInfo(key, handle, mode);
       
   155         }
       
   156 
       
   157         delete plugin;
       
   158     }
       
   159 #endif
       
   160 
       
   161     return devices;
       
   162 }
       
   163 
       
   164 QAudioDeviceInfo QAudioDeviceFactory::defaultInputDevice()
       
   165 {
       
   166 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   167     QAudioSystemFactoryInterface* plugin = qobject_cast<QAudioSystemFactoryInterface*>(audioLoader()->instance(QLatin1String("default")));
       
   168 
       
   169     if (plugin) {
       
   170         QList<QByteArray> list = plugin->availableDevices(QAudio::AudioInput);
       
   171         if (list.size() > 0)
       
   172             return QAudioDeviceInfo(QLatin1String("default"), list.at(0), QAudio::AudioInput);
       
   173     }
       
   174 #endif
       
   175 
       
   176 #ifndef QT_NO_AUDIO_BACKEND
       
   177 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   178     return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultInputDevice(), QAudio::AudioInput);
       
   179 #endif
       
   180 #endif
       
   181     return QAudioDeviceInfo();
       
   182 }
       
   183 
       
   184 QAudioDeviceInfo QAudioDeviceFactory::defaultOutputDevice()
       
   185 {
       
   186 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   187     QAudioSystemFactoryInterface* plugin = qobject_cast<QAudioSystemFactoryInterface*>(audioLoader()->instance(QLatin1String("default")));
       
   188 
       
   189     if (plugin) {
       
   190         QList<QByteArray> list = plugin->availableDevices(QAudio::AudioOutput);
       
   191         if (list.size() > 0)
       
   192             return QAudioDeviceInfo(QLatin1String("default"), list.at(0), QAudio::AudioOutput);
       
   193     }
       
   194 #endif
       
   195 
       
   196 #ifndef QT_NO_AUDIO_BACKEND
       
   197 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   198     return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultOutputDevice(), QAudio::AudioOutput);
       
   199 #endif
       
   200 #endif
       
   201     return QAudioDeviceInfo();
       
   202 }
       
   203 
       
   204 QAbstractAudioDeviceInfo* QAudioDeviceFactory::audioDeviceInfo(const QString &realm, const QByteArray &handle, QAudio::Mode mode)
       
   205 {
       
   206     QAbstractAudioDeviceInfo *rc = 0;
       
   207 
       
   208 #ifndef QT_NO_AUDIO_BACKEND
       
   209 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   210     if (realm == QLatin1String("builtin"))
       
   211         return new QAudioDeviceInfoInternal(handle, mode);
       
   212 #endif
       
   213 #endif
       
   214 
       
   215 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   216     QAudioSystemFactoryInterface* plugin =
       
   217         qobject_cast<QAudioSystemFactoryInterface*>(audioLoader()->instance(realm));
       
   218 
       
   219     if (plugin)
       
   220         rc = plugin->createDeviceInfo(handle, mode);
       
   221 #endif
       
   222 
       
   223     return rc == 0 ? new QNullDeviceInfo() : rc;
       
   224 }
       
   225 
       
   226 QAbstractAudioInput* QAudioDeviceFactory::createDefaultInputDevice(QAudioFormat const &format)
       
   227 {
       
   228     return createInputDevice(defaultInputDevice(), format);
       
   229 }
       
   230 
       
   231 QAbstractAudioOutput* QAudioDeviceFactory::createDefaultOutputDevice(QAudioFormat const &format)
       
   232 {
       
   233     return createOutputDevice(defaultOutputDevice(), format);
       
   234 }
       
   235 
       
   236 QAbstractAudioInput* QAudioDeviceFactory::createInputDevice(QAudioDeviceInfo const& deviceInfo, QAudioFormat const &format)
       
   237 {
       
   238     if (deviceInfo.isNull())
       
   239         return new QNullInputDevice();
       
   240 #ifndef QT_NO_AUDIO_BACKEND
       
   241 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   242     if (deviceInfo.realm() == QLatin1String("builtin")) {
       
   243         QAbstractAudioInput* p = new QAudioInputPrivate(deviceInfo.handle());
       
   244         if (p) p->setFormat(format);
       
   245         return p;
       
   246     }
       
   247 #endif
       
   248 #endif
       
   249 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   250     QAudioSystemFactoryInterface* plugin =
       
   251         qobject_cast<QAudioSystemFactoryInterface*>(audioLoader()->instance(deviceInfo.realm()));
       
   252 
       
   253     if (plugin) {
       
   254         QAbstractAudioInput* p = plugin->createInput(deviceInfo.handle());
       
   255         if (p) p->setFormat(format);
       
   256         return p;
       
   257     }
       
   258 #endif
       
   259 
       
   260     return new QNullInputDevice();
       
   261 }
       
   262 
       
   263 QAbstractAudioOutput* QAudioDeviceFactory::createOutputDevice(QAudioDeviceInfo const& deviceInfo, QAudioFormat const &format)
       
   264 {
       
   265     if (deviceInfo.isNull())
       
   266         return new QNullOutputDevice();
       
   267 #ifndef QT_NO_AUDIO_BACKEND
       
   268 #if (defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(HAS_ALSA) || defined(Q_OS_SYMBIAN))
       
   269     if (deviceInfo.realm() == QLatin1String("builtin")) {
       
   270         QAbstractAudioOutput* p = new QAudioOutputPrivate(deviceInfo.handle());
       
   271         if (p) p->setFormat(format);
       
   272         return p;
       
   273     }
       
   274 #endif
       
   275 #endif
       
   276 
       
   277 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
       
   278     QAudioSystemFactoryInterface* plugin =
       
   279         qobject_cast<QAudioSystemFactoryInterface*>(audioLoader()->instance(deviceInfo.realm()));
       
   280 
       
   281     if (plugin) {
       
   282         QAbstractAudioOutput* p = plugin->createOutput(deviceInfo.handle());
       
   283         if (p) p->setFormat(format);
       
   284         return p;
       
   285     }
       
   286 #endif
       
   287 
       
   288     return new QNullOutputDevice();
       
   289 }
       
   290 
       
   291 QT_END_NAMESPACE
       
   292