qtmobility/src/multimedia/audio/qaudiodeviceinfo_mac_p.cpp
changeset 14 6fbed849b4f4
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 //
       
    43 //  W A R N I N G
       
    44 //  -------------
       
    45 //
       
    46 // This file is not part of the Qt API.  It exists for the convenience
       
    47 // of other Qt classes.  This header file may change from version to
       
    48 // version without notice, or even be removed.
       
    49 //
       
    50 // We mean it.
       
    51 //
       
    52 
       
    53 #include <QtCore/qstringlist.h>
       
    54 #include <QtCore/qlist.h>
       
    55 #include <QtCore/qbytearray.h>
       
    56 #include <QtCore/qdatastream.h>
       
    57 #include <QtCore/qdebug.h>
       
    58 #include <private/qcore_mac_p.h>
       
    59 
       
    60 #include <qaudiodeviceinfo.h>
       
    61 #include "qaudio_mac_p.h"
       
    62 #include "qaudiodeviceinfo_mac_p.h"
       
    63 
       
    64 
       
    65 
       
    66 QT_BEGIN_NAMESPACE
       
    67 
       
    68 
       
    69 QAudioDeviceInfoInternal::QAudioDeviceInfoInternal(QByteArray const& handle, QAudio::Mode)
       
    70 {
       
    71     QDataStream ds(handle);
       
    72     quint32 did, tm;
       
    73 
       
    74     ds >> did >> tm >> name;
       
    75     deviceId = AudioDeviceID(did);
       
    76     mode = QAudio::Mode(tm);
       
    77 }
       
    78 
       
    79 bool QAudioDeviceInfoInternal::isFormatSupported(const QAudioFormat& format) const
       
    80 {
       
    81     return format.codec() == QString::fromLatin1("audio/pcm");
       
    82 }
       
    83 
       
    84 QAudioFormat QAudioDeviceInfoInternal::preferredFormat() const
       
    85 {
       
    86     QAudioFormat    rc;
       
    87 
       
    88     UInt32  propSize = 0;
       
    89 
       
    90     if (AudioDeviceGetPropertyInfo(deviceId,
       
    91                                     0,
       
    92                                     mode == QAudio::AudioInput,
       
    93                                     kAudioDevicePropertyStreams,
       
    94                                     &propSize,
       
    95                                     0) == noErr) {
       
    96 
       
    97         const int sc = propSize / sizeof(AudioStreamID);
       
    98 
       
    99         if (sc > 0) {
       
   100             AudioStreamID*  streams = new AudioStreamID[sc];
       
   101 
       
   102             if (AudioDeviceGetProperty(deviceId,
       
   103                                         0,
       
   104                                         mode == QAudio::AudioInput,
       
   105                                         kAudioDevicePropertyStreams,
       
   106                                         &propSize,
       
   107                                         streams) == noErr) {
       
   108 
       
   109                 for (int i = 0; i < sc; ++i) {
       
   110                     if (AudioStreamGetPropertyInfo(streams[i],
       
   111                                                     0,
       
   112                                                     kAudioStreamPropertyPhysicalFormat,
       
   113                                                     &propSize,
       
   114                                                     0) == noErr) {
       
   115 
       
   116                         AudioStreamBasicDescription sf;
       
   117 
       
   118                         if (AudioStreamGetProperty(streams[i],
       
   119                                                     0,
       
   120                                                     kAudioStreamPropertyPhysicalFormat,
       
   121                                                     &propSize,
       
   122                                                     &sf) == noErr) {
       
   123                             rc = toQAudioFormat(sf);
       
   124                             break;
       
   125                         }
       
   126                     }
       
   127                 }
       
   128             }
       
   129 
       
   130             delete streams;
       
   131         }
       
   132     }
       
   133 
       
   134     return rc;
       
   135 }
       
   136 
       
   137 QString QAudioDeviceInfoInternal::deviceName() const
       
   138 {
       
   139     return name;
       
   140 }
       
   141 
       
   142 QStringList QAudioDeviceInfoInternal::supportedCodecs()
       
   143 {
       
   144     return QStringList() << QString::fromLatin1("audio/pcm");
       
   145 }
       
   146 
       
   147 QList<int> QAudioDeviceInfoInternal::supportedSampleRates()
       
   148 {
       
   149     QSet<int>  rc;
       
   150 
       
   151     // Add some common frequencies
       
   152     rc << 8000 << 11025 << 22050 << 44100;
       
   153 
       
   154     //
       
   155     UInt32  propSize = 0;
       
   156 
       
   157     if (AudioDeviceGetPropertyInfo(deviceId,
       
   158                                     0,
       
   159                                     mode == QAudio::AudioInput,
       
   160                                     kAudioDevicePropertyAvailableNominalSampleRates,
       
   161                                     &propSize,
       
   162                                     0) == noErr) {
       
   163 
       
   164         const int pc = propSize / sizeof(AudioValueRange);
       
   165 
       
   166         if (pc > 0) {
       
   167             AudioValueRange*    vr = new AudioValueRange[pc];
       
   168 
       
   169             if (AudioDeviceGetProperty(deviceId,
       
   170                                         0,
       
   171                                         mode == QAudio::AudioInput,
       
   172                                         kAudioDevicePropertyAvailableNominalSampleRates,
       
   173                                         &propSize,
       
   174                                         vr) == noErr) {
       
   175 
       
   176                 for (int i = 0; i < pc; ++i)
       
   177                     rc << vr[i].mMaximum;
       
   178             }
       
   179 
       
   180             delete vr;
       
   181         }
       
   182     }
       
   183 
       
   184     return rc.toList();
       
   185 }
       
   186 
       
   187 QList<int> QAudioDeviceInfoInternal::supportedChannelCounts()
       
   188 {
       
   189     QList<int>  rc;
       
   190 
       
   191     // Can mix down to 1 channel
       
   192     rc << 1;
       
   193 
       
   194     UInt32  propSize = 0;
       
   195     int     channels = 0;
       
   196 
       
   197     if (AudioDeviceGetPropertyInfo(deviceId, 
       
   198                                     0,
       
   199                                     mode == QAudio::AudioInput,
       
   200                                     kAudioDevicePropertyStreamConfiguration,
       
   201                                     &propSize, 
       
   202                                     0) == noErr) {
       
   203 
       
   204         AudioBufferList* audioBufferList = static_cast<AudioBufferList*>(qMalloc(propSize));
       
   205 
       
   206         if (audioBufferList != 0) {
       
   207             if (AudioDeviceGetProperty(deviceId, 
       
   208                                         0,
       
   209                                         mode == QAudio::AudioInput,
       
   210                                         kAudioDevicePropertyStreamConfiguration,
       
   211                                         &propSize,
       
   212                                         audioBufferList) == noErr) {
       
   213 
       
   214                 for (int i = 0; i < int(audioBufferList->mNumberBuffers); ++i) {
       
   215                     channels += audioBufferList->mBuffers[i].mNumberChannels;
       
   216                     rc << channels;
       
   217                 }
       
   218             }
       
   219 
       
   220             qFree(audioBufferList);
       
   221         }
       
   222     }
       
   223 
       
   224     return rc;
       
   225 }
       
   226 
       
   227 QList<int> QAudioDeviceInfoInternal::supportedSampleSizes()
       
   228 {
       
   229     return QList<int>() << 8 << 16 << 24 << 32 << 64;
       
   230 }
       
   231 
       
   232 QList<QAudioFormat::Endian> QAudioDeviceInfoInternal::supportedByteOrders()
       
   233 {
       
   234     return QList<QAudioFormat::Endian>() << QAudioFormat::LittleEndian << QAudioFormat::BigEndian;
       
   235 }
       
   236 
       
   237 QList<QAudioFormat::SampleType> QAudioDeviceInfoInternal::supportedSampleTypes()
       
   238 {
       
   239     return QList<QAudioFormat::SampleType>() << QAudioFormat::SignedInt << QAudioFormat::UnSignedInt << QAudioFormat::Float;
       
   240 }
       
   241 
       
   242 static QByteArray get_device_info(AudioDeviceID audioDevice, QAudio::Mode mode)
       
   243 {
       
   244     UInt32      size;
       
   245     QByteArray  device;
       
   246     QDataStream ds(&device, QIODevice::WriteOnly);
       
   247     AudioStreamBasicDescription     sf;
       
   248     CFStringRef name;
       
   249     Boolean     isInput = mode == QAudio::AudioInput;
       
   250 
       
   251     // Id
       
   252     ds << quint32(audioDevice);
       
   253 
       
   254     // Mode
       
   255     size = sizeof(AudioStreamBasicDescription);
       
   256     if (AudioDeviceGetProperty(audioDevice, 0, isInput, kAudioDevicePropertyStreamFormat,
       
   257                                 &size, &sf) != noErr) {
       
   258         return QByteArray();
       
   259     }
       
   260     ds << quint32(mode);
       
   261 
       
   262     // Name
       
   263     size = sizeof(CFStringRef);
       
   264     if (AudioDeviceGetProperty(audioDevice, 0, isInput, kAudioObjectPropertyName,
       
   265                                 &size, &name) != noErr) {
       
   266         qWarning() << "QAudioDeviceInfo: Unable to find device name";
       
   267     }
       
   268     ds << QCFString::toQString(name);
       
   269 
       
   270     CFRelease(name);
       
   271 
       
   272     return device;
       
   273 }
       
   274 
       
   275 QByteArray QAudioDeviceInfoInternal::defaultInputDevice()
       
   276 {
       
   277     AudioDeviceID   audioDevice;
       
   278     UInt32          size = sizeof(audioDevice);
       
   279 
       
   280     if (AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &size,
       
   281                                     &audioDevice) != noErr) {
       
   282         qWarning() << "QAudioDeviceInfo: Unable to find default input device";
       
   283         return QByteArray();
       
   284     }
       
   285 
       
   286     return get_device_info(audioDevice, QAudio::AudioInput);
       
   287 }
       
   288 
       
   289 QByteArray QAudioDeviceInfoInternal::defaultOutputDevice()
       
   290 {
       
   291     AudioDeviceID audioDevice;
       
   292     UInt32        size = sizeof(audioDevice);
       
   293 
       
   294     if (AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size,
       
   295                                     &audioDevice) != noErr) {
       
   296         qWarning() << "QAudioDeviceInfo: Unable to find default output device";
       
   297         return QByteArray();
       
   298     }
       
   299 
       
   300     return get_device_info(audioDevice, QAudio::AudioOutput);
       
   301 }
       
   302 
       
   303 QList<QByteArray> QAudioDeviceInfoInternal::availableDevices(QAudio::Mode mode)
       
   304 {
       
   305     QList<QByteArray>   devices;
       
   306 
       
   307     UInt32  propSize = 0;
       
   308 
       
   309     if (AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &propSize, 0) == noErr) {
       
   310 
       
   311         const int dc = propSize / sizeof(AudioDeviceID);
       
   312 
       
   313         if (dc > 0) {
       
   314             AudioDeviceID*  audioDevices = new AudioDeviceID[dc];
       
   315 
       
   316             if (AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &propSize, audioDevices) == noErr) {
       
   317                 for (int i = 0; i < dc; ++i) {
       
   318                     QByteArray info = get_device_info(audioDevices[i], mode);
       
   319                     if (!info.isNull())
       
   320                         devices << info;
       
   321                 }
       
   322             }
       
   323 
       
   324             delete audioDevices;
       
   325         }
       
   326     }
       
   327 
       
   328     return devices;
       
   329 }
       
   330 
       
   331 
       
   332 QT_END_NAMESPACE
       
   333