CoreAudio: Create an audio plugin supporting iOS and OS X

This removes the Mac audio backend that was hardcoded into QtMultimedia
and adds a new audio plugin using the CoreAudio API.

Change-Id: Ib15291825f9452a3763e0eeb281d952deb0bad3d
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@digia.com>
Reviewed-by: Christian Stromme <christian.stromme@digia.com>
Reviewed-by: Yoann Lopes <yoann.lopes@digia.com>
This commit is contained in:
Andy Nichols
2013-03-08 15:18:36 +01:00
committed by The Qt Project
parent 044e48d5a4
commit b357c55f2d
22 changed files with 3461 additions and 2572 deletions

View File

@@ -39,20 +39,6 @@ SOURCES += \
audio/qaudiodecoder.cpp \
audio/qaudiohelpers.cpp
mac:!ios {
PRIVATE_HEADERS += audio/qaudioinput_mac_p.h \
audio/qaudiooutput_mac_p.h \
audio/qaudiodeviceinfo_mac_p.h \
audio/qaudio_mac_p.h
SOURCES += audio/qaudiodeviceinfo_mac_p.cpp \
audio/qaudiooutput_mac_p.cpp \
audio/qaudioinput_mac_p.cpp \
audio/qaudio_mac.cpp
LIBS += -framework ApplicationServices -framework CoreAudio -framework AudioUnit -framework AudioToolbox
}
win32 {
PRIVATE_HEADERS += audio/qaudioinput_win32_p.h audio/qaudiooutput_win32_p.h audio/qaudiodeviceinfo_win32_p.h
SOURCES += audio/qaudiodeviceinfo_win32_p.cpp \

View File

@@ -1,130 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qaudio_mac_p.h"
QT_BEGIN_NAMESPACE
// Conversion
QAudioFormat toQAudioFormat(AudioStreamBasicDescription const& sf)
{
QAudioFormat audioFormat;
audioFormat.setSampleRate(sf.mSampleRate);
audioFormat.setChannelCount(sf.mChannelsPerFrame);
audioFormat.setSampleSize(sf.mBitsPerChannel);
audioFormat.setCodec(QString::fromLatin1("audio/pcm"));
audioFormat.setByteOrder((sf.mFormatFlags & kAudioFormatFlagIsBigEndian) != 0 ? QAudioFormat::BigEndian : QAudioFormat::LittleEndian);
QAudioFormat::SampleType type = QAudioFormat::UnSignedInt;
if ((sf.mFormatFlags & kAudioFormatFlagIsSignedInteger) != 0)
type = QAudioFormat::SignedInt;
else if ((sf.mFormatFlags & kAudioFormatFlagIsFloat) != 0)
type = QAudioFormat::Float;
audioFormat.setSampleType(type);
return audioFormat;
}
AudioStreamBasicDescription toAudioStreamBasicDescription(QAudioFormat const& audioFormat)
{
AudioStreamBasicDescription sf;
sf.mFormatFlags = kAudioFormatFlagIsPacked;
sf.mSampleRate = audioFormat.sampleRate();
sf.mFramesPerPacket = 1;
sf.mChannelsPerFrame = audioFormat.channelCount();
sf.mBitsPerChannel = audioFormat.sampleSize();
sf.mBytesPerFrame = sf.mChannelsPerFrame * (sf.mBitsPerChannel / 8);
sf.mBytesPerPacket = sf.mFramesPerPacket * sf.mBytesPerFrame;
sf.mFormatID = kAudioFormatLinearPCM;
switch (audioFormat.sampleType()) {
case QAudioFormat::SignedInt: sf.mFormatFlags |= kAudioFormatFlagIsSignedInteger; break;
case QAudioFormat::UnSignedInt: /* default */ break;
case QAudioFormat::Float: sf.mFormatFlags |= kAudioFormatFlagIsFloat; break;
case QAudioFormat::Unknown: default: break;
}
if (audioFormat.byteOrder() == QAudioFormat::BigEndian)
sf.mFormatFlags |= kAudioFormatFlagIsBigEndian;
return sf;
}
// QAudioRingBuffer
QAudioRingBuffer::QAudioRingBuffer(int bufferSize):
m_bufferSize(bufferSize)
{
m_buffer = new char[m_bufferSize];
reset();
}
QAudioRingBuffer::~QAudioRingBuffer()
{
delete m_buffer;
}
int QAudioRingBuffer::used() const
{
return m_bufferUsed.load();
}
int QAudioRingBuffer::free() const
{
return m_bufferSize - m_bufferUsed.load();
}
int QAudioRingBuffer::size() const
{
return m_bufferSize;
}
void QAudioRingBuffer::reset()
{
m_readPos = 0;
m_writePos = 0;
m_bufferUsed.store(0);
}
QT_END_NAMESPACE

View File

@@ -1,135 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of other Qt classes. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#ifndef QAUDIO_MAC_P_H
#define QAUDIO_MAC_P_H
#include <CoreAudio/CoreAudio.h>
#include <QtCore/qdebug.h>
#include <QtCore/qatomic.h>
#include <qaudioformat.h>
QT_BEGIN_NAMESPACE
extern QAudioFormat toQAudioFormat(const AudioStreamBasicDescription& streamFormat);
extern AudioStreamBasicDescription toAudioStreamBasicDescription(QAudioFormat const& audioFormat);
class QAudioRingBuffer
{
public:
typedef QPair<char*, int> Region;
QAudioRingBuffer(int bufferSize);
~QAudioRingBuffer();
Region acquireReadRegion(int size)
{
const int used = m_bufferUsed.fetchAndAddAcquire(0);
if (used > 0) {
const int readSize = qMin(size, qMin(m_bufferSize - m_readPos, used));
return readSize > 0 ? Region(m_buffer + m_readPos, readSize) : Region(0, 0);
}
return Region(0, 0);
}
void releaseReadRegion(Region const& region)
{
m_readPos = (m_readPos + region.second) % m_bufferSize;
m_bufferUsed.fetchAndAddRelease(-region.second);
}
Region acquireWriteRegion(int size)
{
const int free = m_bufferSize - m_bufferUsed.fetchAndAddAcquire(0);
if (free > 0) {
const int writeSize = qMin(size, qMin(m_bufferSize - m_writePos, free));
return writeSize > 0 ? Region(m_buffer + m_writePos, writeSize) : Region(0, 0);
}
return Region(0, 0);
}
void releaseWriteRegion(Region const& region)
{
m_writePos = (m_writePos + region.second) % m_bufferSize;
m_bufferUsed.fetchAndAddRelease(region.second);
}
int used() const;
int free() const;
int size() const;
void reset();
private:
int m_bufferSize;
int m_readPos;
int m_writePos;
char* m_buffer;
QAtomicInt m_bufferUsed;
};
QT_END_NAMESPACE
#endif // QAUDIO_MAC_P_H

View File

@@ -52,10 +52,6 @@
#include "qaudiodeviceinfo_win32_p.h"
#include "qaudiooutput_win32_p.h"
#include "qaudioinput_win32_p.h"
#elif defined(Q_OS_MAC) && !defined(Q_OS_IOS)
#include "qaudiodeviceinfo_mac_p.h"
#include "qaudiooutput_mac_p.h"
#include "qaudioinput_mac_p.h"
#elif defined(HAS_ALSA)
#include "qaudiodeviceinfo_alsa_p.h"
#include "qaudiooutput_alsa_p.h"
@@ -137,7 +133,7 @@ QList<QAudioDeviceInfo> QAudioDeviceFactory::availableDevices(QAudio::Mode mode)
{
QList<QAudioDeviceInfo> devices;
#ifndef QT_NO_AUDIO_BACKEND
#if (defined(Q_OS_WIN) || (defined(Q_OS_MAC) && !defined(Q_OS_IOS)) || defined(HAS_ALSA))
#if (defined(Q_OS_WIN) || defined(HAS_ALSA))
foreach (const QByteArray &handle, QAudioDeviceInfoInternal::availableDevices(mode))
devices << QAudioDeviceInfo(QLatin1String("builtin"), handle, mode);
#endif
@@ -170,7 +166,7 @@ QAudioDeviceInfo QAudioDeviceFactory::defaultInputDevice()
#endif
#ifndef QT_NO_AUDIO_BACKEND
#if (defined(Q_OS_WIN) || (defined(Q_OS_MAC) && !defined(Q_OS_IOS)) || defined(HAS_ALSA))
#if (defined(Q_OS_WIN) || defined(HAS_ALSA))
return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultInputDevice(), QAudio::AudioInput);
#endif
#endif
@@ -190,7 +186,7 @@ QAudioDeviceInfo QAudioDeviceFactory::defaultOutputDevice()
#endif
#ifndef QT_NO_AUDIO_BACKEND
#if (defined(Q_OS_WIN) || (defined(Q_OS_MAC) && !defined(Q_OS_IOS)) || defined(HAS_ALSA))
#if (defined(Q_OS_WIN) || defined(HAS_ALSA))
return QAudioDeviceInfo(QLatin1String("builtin"), QAudioDeviceInfoInternal::defaultOutputDevice(), QAudio::AudioOutput);
#endif
#endif
@@ -202,7 +198,7 @@ QAbstractAudioDeviceInfo* QAudioDeviceFactory::audioDeviceInfo(const QString &re
QAbstractAudioDeviceInfo *rc = 0;
#ifndef QT_NO_AUDIO_BACKEND
#if (defined(Q_OS_WIN) || (defined(Q_OS_MAC) && !defined(Q_OS_IOS)) || defined(HAS_ALSA))
#if (defined(Q_OS_WIN) || defined(HAS_ALSA))
if (realm == QLatin1String("builtin"))
return new QAudioDeviceInfoInternal(handle, mode);
#endif
@@ -234,7 +230,7 @@ QAbstractAudioInput* QAudioDeviceFactory::createInputDevice(QAudioDeviceInfo con
if (deviceInfo.isNull())
return new QNullInputDevice();
#ifndef QT_NO_AUDIO_BACKEND
#if (defined(Q_OS_WIN) || (defined(Q_OS_MAC) && !defined(Q_OS_IOS)) || defined(HAS_ALSA))
#if (defined(Q_OS_WIN) || defined(HAS_ALSA))
if (deviceInfo.realm() == QLatin1String("builtin")) {
QAbstractAudioInput* p = new QAudioInputPrivate(deviceInfo.handle());
if (p) p->setFormat(format);
@@ -261,7 +257,7 @@ QAbstractAudioOutput* QAudioDeviceFactory::createOutputDevice(QAudioDeviceInfo c
if (deviceInfo.isNull())
return new QNullOutputDevice();
#ifndef QT_NO_AUDIO_BACKEND
#if (defined(Q_OS_WIN) || (defined(Q_OS_MAC) && !defined(Q_OS_IOS)) || defined(HAS_ALSA))
#if (defined(Q_OS_WIN) || defined(HAS_ALSA))
if (deviceInfo.realm() == QLatin1String("builtin")) {
QAbstractAudioOutput* p = new QAudioOutputPrivate(deviceInfo.handle());
if (p) p->setFormat(format);

View File

@@ -1,351 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of other Qt classes. This header file may change from version to
// version without notice, or even be removed.
//
// INTERNAL USE ONLY: Do NOT use for any other purpose.
//
#include <QtCore/qstringlist.h>
#include <QtCore/qlist.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qdatastream.h>
#include <QtCore/qdebug.h>
#include <qaudiodeviceinfo.h>
#include "qaudio_mac_p.h"
#include "qaudiodeviceinfo_mac_p.h"
QT_BEGIN_NAMESPACE
// XXX: remove at some future date
static inline QString cfStringToQString(CFStringRef str)
{
CFIndex length = CFStringGetLength(str);
const UniChar *chars = CFStringGetCharactersPtr(str);
if (chars)
return QString(reinterpret_cast<const QChar *>(chars), length);
UniChar buffer[length];
CFStringGetCharacters(str, CFRangeMake(0, length), buffer);
return QString(reinterpret_cast<const QChar *>(buffer), length);
}
QAudioDeviceInfoInternal::QAudioDeviceInfoInternal(QByteArray const& handle, QAudio::Mode)
{
QDataStream ds(handle);
quint32 did, tm;
ds >> did >> tm >> name;
deviceId = AudioDeviceID(did);
mode = QAudio::Mode(tm);
}
bool QAudioDeviceInfoInternal::isFormatSupported(const QAudioFormat& format) const
{
QAudioDeviceInfoInternal *self = const_cast<QAudioDeviceInfoInternal*>(this);
return format.isValid()
&& format.codec() == QString::fromLatin1("audio/pcm")
&& self->supportedSampleRates().contains(format.sampleRate())
&& self->supportedChannelCounts().contains(format.channelCount())
&& self->supportedSampleSizes().contains(format.sampleSize());
}
QAudioFormat QAudioDeviceInfoInternal::preferredFormat() const
{
QAudioFormat rc;
UInt32 propSize = 0;
if (AudioDeviceGetPropertyInfo(deviceId,
0,
mode == QAudio::AudioInput,
kAudioDevicePropertyStreams,
&propSize,
0) == noErr) {
const int sc = propSize / sizeof(AudioStreamID);
if (sc > 0) {
AudioStreamID* streams = new AudioStreamID[sc];
if (AudioDeviceGetProperty(deviceId,
0,
mode == QAudio::AudioInput,
kAudioDevicePropertyStreams,
&propSize,
streams) == noErr) {
for (int i = 0; i < sc; ++i) {
if (AudioStreamGetPropertyInfo(streams[i],
0,
kAudioStreamPropertyPhysicalFormat,
&propSize,
0) == noErr) {
AudioStreamBasicDescription sf;
if (AudioStreamGetProperty(streams[i],
0,
kAudioStreamPropertyPhysicalFormat,
&propSize,
&sf) == noErr) {
rc = toQAudioFormat(sf);
break;
}
}
}
}
delete streams;
}
}
return rc;
}
QString QAudioDeviceInfoInternal::deviceName() const
{
return name;
}
QStringList QAudioDeviceInfoInternal::supportedCodecs()
{
return QStringList() << QString::fromLatin1("audio/pcm");
}
QList<int> QAudioDeviceInfoInternal::supportedSampleRates()
{
QSet<int> rc;
// Add some common frequencies
rc << 8000 << 11025 << 22050 << 44100;
//
UInt32 propSize = 0;
if (AudioDeviceGetPropertyInfo(deviceId,
0,
mode == QAudio::AudioInput,
kAudioDevicePropertyAvailableNominalSampleRates,
&propSize,
0) == noErr) {
const int pc = propSize / sizeof(AudioValueRange);
if (pc > 0) {
AudioValueRange* vr = new AudioValueRange[pc];
if (AudioDeviceGetProperty(deviceId,
0,
mode == QAudio::AudioInput,
kAudioDevicePropertyAvailableNominalSampleRates,
&propSize,
vr) == noErr) {
for (int i = 0; i < pc; ++i)
rc << vr[i].mMaximum;
}
delete vr;
}
}
return rc.toList();
}
QList<int> QAudioDeviceInfoInternal::supportedChannelCounts()
{
QList<int> rc;
// Can mix down to 1 channel
rc << 1;
UInt32 propSize = 0;
int channels = 0;
if (AudioDeviceGetPropertyInfo(deviceId,
0,
mode == QAudio::AudioInput,
kAudioDevicePropertyStreamConfiguration,
&propSize,
0) == noErr) {
AudioBufferList* audioBufferList = static_cast<AudioBufferList*>(malloc(propSize));
if (audioBufferList != 0) {
if (AudioDeviceGetProperty(deviceId,
0,
mode == QAudio::AudioInput,
kAudioDevicePropertyStreamConfiguration,
&propSize,
audioBufferList) == noErr) {
for (int i = 0; i < int(audioBufferList->mNumberBuffers); ++i) {
channels += audioBufferList->mBuffers[i].mNumberChannels;
rc << channels;
}
}
free(audioBufferList);
}
}
return rc;
}
QList<int> QAudioDeviceInfoInternal::supportedSampleSizes()
{
return QList<int>() << 8 << 16 << 24 << 32 << 64;
}
QList<QAudioFormat::Endian> QAudioDeviceInfoInternal::supportedByteOrders()
{
return QList<QAudioFormat::Endian>() << QAudioFormat::LittleEndian << QAudioFormat::BigEndian;
}
QList<QAudioFormat::SampleType> QAudioDeviceInfoInternal::supportedSampleTypes()
{
return QList<QAudioFormat::SampleType>() << QAudioFormat::SignedInt << QAudioFormat::UnSignedInt << QAudioFormat::Float;
}
static QByteArray get_device_info(AudioDeviceID audioDevice, QAudio::Mode mode)
{
UInt32 size;
QByteArray device;
QDataStream ds(&device, QIODevice::WriteOnly);
AudioStreamBasicDescription sf;
CFStringRef name;
Boolean isInput = mode == QAudio::AudioInput;
// Id
ds << quint32(audioDevice);
// Mode
size = sizeof(AudioStreamBasicDescription);
if (AudioDeviceGetProperty(audioDevice, 0, isInput, kAudioDevicePropertyStreamFormat,
&size, &sf) != noErr) {
return QByteArray();
}
ds << quint32(mode);
// Name
size = sizeof(CFStringRef);
if (AudioDeviceGetProperty(audioDevice, 0, isInput, kAudioObjectPropertyName,
&size, &name) != noErr) {
qWarning() << "QAudioDeviceInfo: Unable to find device name";
return QByteArray();
}
ds << cfStringToQString(name);
CFRelease(name);
return device;
}
QByteArray QAudioDeviceInfoInternal::defaultInputDevice()
{
AudioDeviceID audioDevice;
UInt32 size = sizeof(audioDevice);
if (AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &size,
&audioDevice) != noErr) {
qWarning() << "QAudioDeviceInfo: Unable to find default input device";
return QByteArray();
}
return get_device_info(audioDevice, QAudio::AudioInput);
}
QByteArray QAudioDeviceInfoInternal::defaultOutputDevice()
{
AudioDeviceID audioDevice;
UInt32 size = sizeof(audioDevice);
if (AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size,
&audioDevice) != noErr) {
qWarning() << "QAudioDeviceInfo: Unable to find default output device";
return QByteArray();
}
return get_device_info(audioDevice, QAudio::AudioOutput);
}
QList<QByteArray> QAudioDeviceInfoInternal::availableDevices(QAudio::Mode mode)
{
QList<QByteArray> devices;
UInt32 propSize = 0;
if (AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &propSize, 0) == noErr) {
const int dc = propSize / sizeof(AudioDeviceID);
if (dc > 0) {
AudioDeviceID* audioDevices = new AudioDeviceID[dc];
if (AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &propSize, audioDevices) == noErr) {
for (int i = 0; i < dc; ++i) {
QByteArray info = get_device_info(audioDevices[i], mode);
if (!info.isNull())
devices << info;
}
}
delete audioDevices;
}
}
return devices;
}
QT_END_NAMESPACE

View File

@@ -1,93 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of other Qt classes. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#ifndef QDEVICEINFO_MAC_P_H
#define QDEVICEINFO_MAC_P_H
#include <CoreAudio/CoreAudio.h>
#include <qaudiosystem.h>
QT_BEGIN_NAMESPACE
class QAudioDeviceInfoInternal : public QAbstractAudioDeviceInfo
{
public:
AudioDeviceID deviceId;
QString name;
QAudio::Mode mode;
QAudioDeviceInfoInternal(QByteArray const& handle, QAudio::Mode mode);
bool isFormatSupported(const QAudioFormat& format) const;
QAudioFormat preferredFormat() const;
QString deviceName() const;
QStringList supportedCodecs();
QList<int> supportedSampleRates();
QList<int> supportedChannelCounts();
QList<int> supportedSampleSizes();
QList<QAudioFormat::Endian> supportedByteOrders();
QList<QAudioFormat::SampleType> supportedSampleTypes();
static QByteArray defaultInputDevice();
static QByteArray defaultOutputDevice();
static QList<QByteArray> availableDevices(QAudio::Mode mode);
};
QT_END_NAMESPACE
#endif // QDEVICEINFO_MAC_P_H

File diff suppressed because it is too large Load Diff

View File

@@ -1,173 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of other Qt classes. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#ifndef QAUDIOINPUT_MAC_P_H
#define QAUDIOINPUT_MAC_P_H
#include <CoreServices/CoreServices.h>
#include <CoreAudio/CoreAudio.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/AudioToolbox.h>
#include <QtCore/qobject.h>
#include <QtCore/qmutex.h>
#include <QtCore/qwaitcondition.h>
#include <QtCore/qatomic.h>
#include <qaudio.h>
#include <qaudioformat.h>
#include <qaudiosystem.h>
QT_BEGIN_NAMESPACE
class QTimer;
class QIODevice;
class QAbstractAudioDeviceInfo;
namespace QtMultimediaInternal
{
class QAudioInputBuffer;
}
class QAudioInputPrivate : public QAbstractAudioInput
{
Q_OBJECT
public:
bool isOpen;
int periodSizeBytes;
int internalBufferSize;
qint64 totalFrames;
QAudioFormat audioFormat;
QIODevice* audioIO;
AudioUnit audioUnit;
AudioDeviceID audioDeviceId;
Float64 clockFrequency;
UInt64 startTime;
QAudio::Error errorCode;
QAudio::State stateCode;
QtMultimediaInternal::QAudioInputBuffer* audioBuffer;
QMutex mutex;
QWaitCondition threadFinished;
QAtomicInt audioThreadState;
QTimer* intervalTimer;
AudioStreamBasicDescription streamFormat;
AudioStreamBasicDescription deviceFormat;
QAbstractAudioDeviceInfo *audioDeviceInfo;
qreal m_volume;
QAudioInputPrivate(const QByteArray& device);
~QAudioInputPrivate();
bool open();
void close();
QAudioFormat format() const;
void setFormat(const QAudioFormat& fmt);
QIODevice* start();
void start(QIODevice* device);
void stop();
void reset();
void suspend();
void resume();
void idle();
int bytesReady() const;
int periodSize() const;
void setBufferSize(int value);
int bufferSize() const;
void setNotifyInterval(int milliSeconds);
int notifyInterval() const;
qint64 processedUSecs() const;
qint64 elapsedUSecs() const;
QAudio::Error error() const;
QAudio::State state() const;
qreal volume() const;
void setVolume(qreal volume);
void audioThreadStart();
void audioThreadStop();
void audioDeviceStop();
void audioDeviceActive();
void audioDeviceFull();
void audioDeviceError();
void startTimers();
void stopTimers();
private slots:
void deviceStopped();
private:
enum { Running, Stopped };
// Input callback
static OSStatus inputCallback(void* inRefCon,
AudioUnitRenderActionFlags* ioActionFlags,
const AudioTimeStamp* inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList* ioData);
};
QT_END_NAMESPACE
#endif // QAUDIOINPUT_MAC_P_H

View File

@@ -1,759 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of other Qt classes. This header file may change from version to
// version without notice, or even be removed.
//
// INTERNAL USE ONLY: Do NOT use for any other purpose.
//
#include <CoreServices/CoreServices.h>
#include <CoreAudio/CoreAudio.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/AudioToolbox.h>
#include <QtCore/qendian.h>
#include <QtCore/qbuffer.h>
#include <QtCore/qtimer.h>
#include <QtCore/qdebug.h>
#include <qaudiooutput.h>
#include "qaudio_mac_p.h"
#include "qaudiooutput_mac_p.h"
#include "qaudiodeviceinfo_mac_p.h"
QT_BEGIN_NAMESPACE
namespace QtMultimediaInternal
{
static const int default_buffer_size = 8 * 1024;
class QAudioOutputBuffer : public QObject
{
Q_OBJECT
public:
QAudioOutputBuffer(int bufferSize, int maxPeriodSize, QAudioFormat const& audioFormat):
m_deviceError(false),
m_maxPeriodSize(maxPeriodSize),
m_device(0)
{
m_buffer = new QAudioRingBuffer(bufferSize + (bufferSize % maxPeriodSize == 0 ? 0 : maxPeriodSize - (bufferSize % maxPeriodSize)));
m_bytesPerFrame = (audioFormat.sampleSize() / 8) * audioFormat.channelCount();
m_periodTime = m_maxPeriodSize / m_bytesPerFrame * 1000 / audioFormat.sampleRate();
m_fillTimer = new QTimer(this);
connect(m_fillTimer, SIGNAL(timeout()), SLOT(fillBuffer()));
}
~QAudioOutputBuffer()
{
delete m_buffer;
}
qint64 readFrames(char* data, qint64 maxFrames)
{
bool wecan = true;
qint64 framesRead = 0;
while (wecan && framesRead < maxFrames) {
QAudioRingBuffer::Region region = m_buffer->acquireReadRegion((maxFrames - framesRead) * m_bytesPerFrame);
if (region.second > 0) {
// Ensure that we only read whole frames.
region.second -= region.second % m_bytesPerFrame;
if (region.second > 0) {
memcpy(data + (framesRead * m_bytesPerFrame), region.first, region.second);
framesRead += region.second / m_bytesPerFrame;
} else
wecan = false; // If there is only a partial frame left we should exit.
}
else
wecan = false;
m_buffer->releaseReadRegion(region);
}
if (framesRead == 0 && m_deviceError)
framesRead = -1;
return framesRead;
}
qint64 writeBytes(const char* data, qint64 maxSize)
{
bool wecan = true;
qint64 bytesWritten = 0;
maxSize -= maxSize % m_bytesPerFrame;
while (wecan && bytesWritten < maxSize) {
QAudioRingBuffer::Region region = m_buffer->acquireWriteRegion(maxSize - bytesWritten);
if (region.second > 0) {
memcpy(region.first, data + bytesWritten, region.second);
bytesWritten += region.second;
}
else
wecan = false;
m_buffer->releaseWriteRegion(region);
}
if (bytesWritten > 0)
emit readyRead();
return bytesWritten;
}
int available() const
{
return m_buffer->free();
}
void reset()
{
m_buffer->reset();
m_device = 0;
m_deviceError = false;
}
void setPrefetchDevice(QIODevice* device)
{
if (m_device != device) {
m_device = device;
if (m_device != 0)
fillBuffer();
}
}
void startFillTimer()
{
if (m_device != 0)
m_fillTimer->start(m_buffer->size() / 2 / m_maxPeriodSize * m_periodTime);
}
void stopFillTimer()
{
m_fillTimer->stop();
}
signals:
void readyRead();
private slots:
void fillBuffer()
{
const int free = m_buffer->free();
const int writeSize = free - (free % m_maxPeriodSize);
if (writeSize > 0) {
bool wecan = true;
int filled = 0;
while (!m_deviceError && wecan && filled < writeSize) {
QAudioRingBuffer::Region region = m_buffer->acquireWriteRegion(writeSize - filled);
if (region.second > 0) {
region.second = m_device->read(region.first, region.second);
if (region.second > 0)
filled += region.second;
else if (region.second == 0)
wecan = false;
else if (region.second < 0) {
m_fillTimer->stop();
region.second = 0;
m_deviceError = true;
}
}
else
wecan = false;
m_buffer->releaseWriteRegion(region);
}
if (filled > 0)
emit readyRead();
}
}
private:
bool m_deviceError;
int m_maxPeriodSize;
int m_bytesPerFrame;
int m_periodTime;
QIODevice* m_device;
QTimer* m_fillTimer;
QAudioRingBuffer* m_buffer;
};
}
class MacOutputDevice : public QIODevice
{
Q_OBJECT
public:
MacOutputDevice(QtMultimediaInternal::QAudioOutputBuffer* audioBuffer, QObject* parent):
QIODevice(parent),
m_audioBuffer(audioBuffer)
{
open(QIODevice::WriteOnly | QIODevice::Unbuffered);
}
qint64 readData(char* data, qint64 len)
{
Q_UNUSED(data);
Q_UNUSED(len);
return 0;
}
qint64 writeData(const char* data, qint64 len)
{
return m_audioBuffer->writeBytes(data, len);
}
bool isSequential() const
{
return true;
}
private:
QtMultimediaInternal::QAudioOutputBuffer* m_audioBuffer;
};
QAudioOutputPrivate::QAudioOutputPrivate(const QByteArray& device)
{
QDataStream ds(device);
quint32 did, mode;
ds >> did >> mode;
if (QAudio::Mode(mode) == QAudio::AudioInput)
errorCode = QAudio::OpenError;
else {
audioDeviceInfo = new QAudioDeviceInfoInternal(device, QAudio::AudioOutput);
isOpen = false;
audioDeviceId = AudioDeviceID(did);
audioUnit = 0;
audioIO = 0;
startTime = 0;
totalFrames = 0;
audioBuffer = 0;
internalBufferSize = QtMultimediaInternal::default_buffer_size;
clockFrequency = AudioGetHostClockFrequency() / 1000;
errorCode = QAudio::NoError;
stateCode = QAudio::StoppedState;
audioThreadState.store(Stopped);
cachedVolume = (qreal)1.;
intervalTimer = new QTimer(this);
intervalTimer->setInterval(1000);
connect(intervalTimer, SIGNAL(timeout()), SIGNAL(notify()));
}
}
QAudioOutputPrivate::~QAudioOutputPrivate()
{
delete audioDeviceInfo;
close();
}
bool QAudioOutputPrivate::open()
{
if (errorCode != QAudio::NoError)
return false;
if (isOpen)
return true;
ComponentDescription cd;
cd.componentType = kAudioUnitType_Output;
cd.componentSubType = kAudioUnitSubType_HALOutput;
cd.componentManufacturer = kAudioUnitManufacturer_Apple;
cd.componentFlags = 0;
cd.componentFlagsMask = 0;
// Open
Component cp = FindNextComponent(NULL, &cd);
if (cp == 0) {
qWarning() << "QAudioOutput: Failed to find HAL Output component";
return false;
}
if (OpenAComponent(cp, &audioUnit) != noErr) {
qWarning() << "QAudioOutput: Unable to Open Output Component";
return false;
}
// register callback
AURenderCallbackStruct cb;
cb.inputProc = renderCallback;
cb.inputProcRefCon = this;
if (AudioUnitSetProperty(audioUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Global,
0,
&cb,
sizeof(cb)) != noErr) {
qWarning() << "QAudioOutput: Failed to set AudioUnit callback";
return false;
}
// Set Audio Device
if (AudioUnitSetProperty(audioUnit,
kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global,
0,
&audioDeviceId,
sizeof(audioDeviceId)) != noErr) {
qWarning() << "QAudioOutput: Unable to use configured device";
return false;
}
// Set stream format
streamFormat = toAudioStreamBasicDescription(audioFormat);
UInt32 size = sizeof(streamFormat);
if (AudioUnitSetProperty(audioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&streamFormat,
sizeof(streamFormat)) != noErr) {
qWarning() << "QAudioOutput: Unable to Set Stream information";
return false;
}
// Allocate buffer
UInt32 numberOfFrames = 0;
size = sizeof(UInt32);
if (AudioUnitGetProperty(audioUnit,
kAudioDevicePropertyBufferFrameSize,
kAudioUnitScope_Global,
0,
&numberOfFrames,
&size) != noErr) {
qWarning() << "QAudioInput: Failed to get audio period size";
return false;
}
periodSizeBytes = numberOfFrames * streamFormat.mBytesPerFrame;
if (internalBufferSize < periodSizeBytes * 2)
internalBufferSize = periodSizeBytes * 2;
else
internalBufferSize -= internalBufferSize % streamFormat.mBytesPerFrame;
audioBuffer = new QtMultimediaInternal::QAudioOutputBuffer(internalBufferSize, periodSizeBytes, audioFormat);
connect(audioBuffer, SIGNAL(readyRead()), SLOT(inputReady())); // Pull
audioIO = new MacOutputDevice(audioBuffer, this);
// Init
if (AudioUnitInitialize(audioUnit)) {
qWarning() << "QAudioOutput: Failed to initialize AudioUnit";
return false;
}
isOpen = true;
setVolume(cachedVolume);
return true;
}
void QAudioOutputPrivate::close()
{
if (audioUnit != 0) {
AudioOutputUnitStop(audioUnit);
AudioUnitUninitialize(audioUnit);
CloseComponent(audioUnit);
}
delete audioBuffer;
}
QAudioFormat QAudioOutputPrivate::format() const
{
return audioFormat;
}
void QAudioOutputPrivate::setFormat(const QAudioFormat& fmt)
{
if (stateCode == QAudio::StoppedState)
audioFormat = fmt;
}
void QAudioOutputPrivate::start(QIODevice* device)
{
QIODevice* op = device;
if (!audioDeviceInfo->isFormatSupported(audioFormat) || !open()) {
stateCode = QAudio::StoppedState;
errorCode = QAudio::OpenError;
}
reset();
audioBuffer->reset();
audioBuffer->setPrefetchDevice(op);
if (op == 0) {
op = audioIO;
stateCode = QAudio::IdleState;
}
else
stateCode = QAudio::ActiveState;
// Start
errorCode = QAudio::NoError;
totalFrames = 0;
startTime = AudioGetCurrentHostTime();
if (stateCode == QAudio::ActiveState)
audioThreadStart();
emit stateChanged(stateCode);
}
QIODevice* QAudioOutputPrivate::start()
{
if (!audioDeviceInfo->isFormatSupported(audioFormat) || !open()) {
stateCode = QAudio::StoppedState;
errorCode = QAudio::OpenError;
return audioIO;
}
reset();
audioBuffer->reset();
audioBuffer->setPrefetchDevice(0);
stateCode = QAudio::IdleState;
// Start
errorCode = QAudio::NoError;
totalFrames = 0;
startTime = AudioGetCurrentHostTime();
emit stateChanged(stateCode);
return audioIO;
}
void QAudioOutputPrivate::stop()
{
QMutexLocker lock(&mutex);
if (stateCode != QAudio::StoppedState) {
audioThreadDrain();
stateCode = QAudio::StoppedState;
errorCode = QAudio::NoError;
QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode));
}
}
void QAudioOutputPrivate::reset()
{
QMutexLocker lock(&mutex);
if (stateCode != QAudio::StoppedState) {
audioThreadStop();
stateCode = QAudio::StoppedState;
errorCode = QAudio::NoError;
QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode));
}
}
void QAudioOutputPrivate::suspend()
{
QMutexLocker lock(&mutex);
if (stateCode == QAudio::ActiveState || stateCode == QAudio::IdleState) {
audioThreadStop();
stateCode = QAudio::SuspendedState;
errorCode = QAudio::NoError;
QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode));
}
}
void QAudioOutputPrivate::resume()
{
QMutexLocker lock(&mutex);
if (stateCode == QAudio::SuspendedState) {
audioThreadStart();
stateCode = QAudio::ActiveState;
errorCode = QAudio::NoError;
QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode));
}
}
int QAudioOutputPrivate::bytesFree() const
{
return audioBuffer->available();
}
int QAudioOutputPrivate::periodSize() const
{
return periodSizeBytes;
}
void QAudioOutputPrivate::setBufferSize(int bs)
{
if (stateCode == QAudio::StoppedState)
internalBufferSize = bs;
}
int QAudioOutputPrivate::bufferSize() const
{
return internalBufferSize;
}
void QAudioOutputPrivate::setNotifyInterval(int milliSeconds)
{
if (intervalTimer->interval() == milliSeconds)
return;
if (milliSeconds <= 0)
milliSeconds = 0;
intervalTimer->setInterval(milliSeconds);
}
int QAudioOutputPrivate::notifyInterval() const
{
return intervalTimer->interval();
}
qint64 QAudioOutputPrivate::processedUSecs() const
{
return totalFrames * 1000000 / audioFormat.sampleRate();
}
qint64 QAudioOutputPrivate::elapsedUSecs() const
{
if (stateCode == QAudio::StoppedState)
return 0;
return (AudioGetCurrentHostTime() - startTime) / (clockFrequency / 1000);
}
QAudio::Error QAudioOutputPrivate::error() const
{
return errorCode;
}
QAudio::State QAudioOutputPrivate::state() const
{
return stateCode;
}
void QAudioOutputPrivate::audioThreadStart()
{
startTimers();
audioThreadState.store(Running);
AudioOutputUnitStart(audioUnit);
}
void QAudioOutputPrivate::audioThreadStop()
{
stopTimers();
if (audioThreadState.testAndSetAcquire(Running, Stopped))
threadFinished.wait(&mutex);
}
void QAudioOutputPrivate::audioThreadDrain()
{
stopTimers();
if (audioThreadState.testAndSetAcquire(Running, Draining))
threadFinished.wait(&mutex);
}
void QAudioOutputPrivate::audioDeviceStop()
{
AudioOutputUnitStop(audioUnit);
audioThreadState.store(Stopped);
threadFinished.wakeOne();
}
void QAudioOutputPrivate::audioDeviceIdle()
{
QMutexLocker lock(&mutex);
if (stateCode == QAudio::ActiveState) {
audioDeviceStop();
errorCode = QAudio::UnderrunError;
stateCode = QAudio::IdleState;
QMetaObject::invokeMethod(this, "deviceStopped", Qt::QueuedConnection);
}
}
void QAudioOutputPrivate::audioDeviceError()
{
QMutexLocker lock(&mutex);
if (stateCode == QAudio::ActiveState) {
audioDeviceStop();
errorCode = QAudio::IOError;
stateCode = QAudio::StoppedState;
QMetaObject::invokeMethod(this, "deviceStopped", Qt::QueuedConnection);
}
}
void QAudioOutputPrivate::startTimers()
{
audioBuffer->startFillTimer();
if (intervalTimer->interval() > 0)
intervalTimer->start();
}
void QAudioOutputPrivate::stopTimers()
{
audioBuffer->stopFillTimer();
intervalTimer->stop();
}
void QAudioOutputPrivate::setVolume(qreal v)
{
const qreal normalizedVolume = qBound(qreal(0.0), v, qreal(1.0));
if (!isOpen) {
cachedVolume = normalizedVolume;
return;
}
if (AudioUnitSetParameter(audioUnit,
kHALOutputParam_Volume,
kAudioUnitScope_Global,
0 /* bus */,
(float)normalizedVolume,
0) == noErr)
cachedVolume = normalizedVolume;
}
qreal QAudioOutputPrivate::volume() const
{
return cachedVolume;
}
void QAudioOutputPrivate::deviceStopped()
{
intervalTimer->stop();
emit stateChanged(stateCode);
}
void QAudioOutputPrivate::inputReady()
{
QMutexLocker lock(&mutex);
if (stateCode == QAudio::IdleState) {
audioThreadStart();
stateCode = QAudio::ActiveState;
errorCode = QAudio::NoError;
QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode));
}
}
OSStatus QAudioOutputPrivate::renderCallback(void* inRefCon,
AudioUnitRenderActionFlags* ioActionFlags,
const AudioTimeStamp* inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList* ioData)
{
Q_UNUSED(ioActionFlags)
Q_UNUSED(inTimeStamp)
Q_UNUSED(inBusNumber)
Q_UNUSED(inNumberFrames)
QAudioOutputPrivate* d = static_cast<QAudioOutputPrivate*>(inRefCon);
const int threadState = d->audioThreadState.fetchAndAddAcquire(0);
if (threadState == Stopped) {
ioData->mBuffers[0].mDataByteSize = 0;
d->audioDeviceStop();
}
else {
const UInt32 bytesPerFrame = d->streamFormat.mBytesPerFrame;
qint64 framesRead;
framesRead = d->audioBuffer->readFrames((char*)ioData->mBuffers[0].mData,
ioData->mBuffers[0].mDataByteSize / bytesPerFrame);
if (framesRead > 0) {
ioData->mBuffers[0].mDataByteSize = framesRead * bytesPerFrame;
d->totalFrames += framesRead;
}
else {
ioData->mBuffers[0].mDataByteSize = 0;
if (framesRead == 0) {
if (threadState == Draining)
d->audioDeviceStop();
else
d->audioDeviceIdle();
}
else
d->audioDeviceError();
}
}
return noErr;
}
QT_END_NAMESPACE
#include "qaudiooutput_mac_p.moc"

View File

@@ -1,172 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of other Qt classes. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#ifndef QAUDIOOUTPUT_MAC_P_H
#define QAUDIOOUTPUT_MAC_P_H
#include <CoreServices/CoreServices.h>
#include <CoreAudio/CoreAudio.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/AudioToolbox.h>
#include <QtCore/qobject.h>
#include <QtCore/qmutex.h>
#include <QtCore/qwaitcondition.h>
#include <QtCore/qtimer.h>
#include <QtCore/qatomic.h>
#include <qaudio.h>
#include <qaudioformat.h>
#include <qaudiosystem.h>
QT_BEGIN_NAMESPACE
class QIODevice;
class QAbstractAudioDeviceInfo;
namespace QtMultimediaInternal
{
class QAudioOutputBuffer;
}
class QAudioOutputPrivate : public QAbstractAudioOutput
{
Q_OBJECT
public:
bool isOpen;
int internalBufferSize;
int periodSizeBytes;
qint64 totalFrames;
QAudioFormat audioFormat;
QIODevice* audioIO;
AudioDeviceID audioDeviceId;
AudioUnit audioUnit;
Float64 clockFrequency;
UInt64 startTime;
AudioStreamBasicDescription deviceFormat;
AudioStreamBasicDescription streamFormat;
QtMultimediaInternal::QAudioOutputBuffer* audioBuffer;
QAtomicInt audioThreadState;
QWaitCondition threadFinished;
QMutex mutex;
QTimer* intervalTimer;
QAbstractAudioDeviceInfo *audioDeviceInfo;
qreal cachedVolume;
QAudio::Error errorCode;
QAudio::State stateCode;
QAudioOutputPrivate(const QByteArray& device);
~QAudioOutputPrivate();
bool open();
void close();
QAudioFormat format() const;
void setFormat(const QAudioFormat& fmt);
QIODevice* start();
void start(QIODevice* device);
void stop();
void reset();
void suspend();
void resume();
int bytesFree() const;
int periodSize() const;
void setBufferSize(int value);
int bufferSize() const;
void setNotifyInterval(int milliSeconds);
int notifyInterval() const;
qint64 processedUSecs() const;
qint64 elapsedUSecs() const;
QAudio::Error error() const;
QAudio::State state() const;
void audioThreadStart();
void audioThreadStop();
void audioThreadDrain();
void audioDeviceStop();
void audioDeviceIdle();
void audioDeviceError();
void startTimers();
void stopTimers();
void setVolume(qreal);
qreal volume() const;
private slots:
void deviceStopped();
void inputReady();
private:
enum { Running, Draining, Stopped };
static OSStatus renderCallback(void* inRefCon,
AudioUnitRenderActionFlags* ioActionFlags,
const AudioTimeStamp* inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList* ioData);
};
QT_END_NAMESPACE
#endif