diff options
author | Christian Stromme <christian.stromme@theqtcompany.com> | 2016-04-19 14:53:24 +0200 |
---|---|---|
committer | Christian Stromme <christian.stromme@qt.io> | 2016-05-20 10:54:32 +0000 |
commit | 3c5bbb0dac7bed3199ddddc88c0175d5a2ac1036 (patch) | |
tree | 51b9987225bf224637b87c3dd20558a3cca95811 /src/plugins/coreaudio | |
parent | 49461f9cffa5439e536a844ec3a0134c252141e6 (diff) |
Improve QAudioDeviceInfo::defaultDevice().
The previous implementation of QAudioDeviceInfo::defaultXDevice() would
always report the first available device as the "default" one, making
the order, in which devices were listed, a hidden requirement when
implementing audio plugins. To make it easier and more reliable to
retrieve the default device, all new plugins should implement the new
QAudioSystemPluginExtension interface as well as the QAudioSystemPlugin.
Devices will be chosen in the following order (first match wins):
default plugin -> default device
-> first available device
plugins -> default device
-> first available device
Task-number: QTBUG-51292
Change-Id: I8ace78858976fe7c60a2c4a117ef15c4e1bb177f
Reviewed-by: Yoann Lopes <yoann.lopes@qt.io>
Diffstat (limited to 'src/plugins/coreaudio')
-rw-r--r-- | src/plugins/coreaudio/coreaudiodeviceinfo.h | 4 | ||||
-rw-r--r-- | src/plugins/coreaudio/coreaudiodeviceinfo.mm | 53 | ||||
-rw-r--r-- | src/plugins/coreaudio/coreaudioplugin.h | 7 | ||||
-rw-r--r-- | src/plugins/coreaudio/coreaudioplugin.mm | 4 |
4 files changed, 25 insertions, 43 deletions
diff --git a/src/plugins/coreaudio/coreaudiodeviceinfo.h b/src/plugins/coreaudio/coreaudiodeviceinfo.h index fc8999850..08c3961e6 100644 --- a/src/plugins/coreaudio/coreaudiodeviceinfo.h +++ b/src/plugins/coreaudio/coreaudiodeviceinfo.h @@ -65,9 +65,7 @@ public: QList<QAudioFormat::Endian> supportedByteOrders(); QList<QAudioFormat::SampleType> supportedSampleTypes(); - static QByteArray defaultInputDevice(); - static QByteArray defaultOutputDevice(); - + static QByteArray defaultDevice(QAudio::Mode mode); static QList<QByteArray> availableDevices(QAudio::Mode mode); private: diff --git a/src/plugins/coreaudio/coreaudiodeviceinfo.mm b/src/plugins/coreaudio/coreaudiodeviceinfo.mm index 66e8ed4d7..1a79438cb 100644 --- a/src/plugins/coreaudio/coreaudiodeviceinfo.mm +++ b/src/plugins/coreaudio/coreaudiodeviceinfo.mm @@ -280,47 +280,29 @@ static QByteArray get_device_info(AudioDeviceID audioDevice, QAudio::Mode mode) } #endif -QByteArray CoreAudioDeviceInfo::defaultInputDevice() +QByteArray CoreAudioDeviceInfo::defaultDevice(QAudio::Mode mode) { #if defined(Q_OS_OSX) AudioDeviceID audioDevice; UInt32 size = sizeof(audioDevice); - AudioObjectPropertyAddress defaultInputDevicePropertyAddress = { kAudioHardwarePropertyDefaultInputDevice, - kAudioObjectPropertyScopeGlobal, - kAudioObjectPropertyElementMaster }; - - if (AudioObjectGetPropertyData(kAudioObjectSystemObject, - &defaultInputDevicePropertyAddress, - 0, NULL, &size, &audioDevice) != noErr) { - qWarning() << "QAudioDeviceInfo: Unable to find default input device"; - return QByteArray(); - } - - return get_device_info(audioDevice, QAudio::AudioInput); -#else //iOS - return CoreAudioSessionManager::instance().inputDevices().first(); -#endif -} - -QByteArray CoreAudioDeviceInfo::defaultOutputDevice() -{ -#if defined(Q_OS_OSX) - AudioDeviceID audioDevice; - UInt32 size = sizeof(audioDevice); - AudioObjectPropertyAddress defaultOutputDevicePropertyAddress = { kAudioHardwarePropertyDefaultOutputDevice, - kAudioObjectPropertyScopeGlobal, - kAudioObjectPropertyElementMaster }; + const AudioObjectPropertySelector selector = (mode == QAudio::AudioOutput) ? kAudioHardwarePropertyDefaultOutputDevice + : kAudioHardwarePropertyDefaultInputDevice; + AudioObjectPropertyAddress defaultDevicePropertyAddress = { selector, + kAudioObjectPropertyScopeGlobal, + kAudioObjectPropertyElementMaster }; if (AudioObjectGetPropertyData(kAudioObjectSystemObject, - &defaultOutputDevicePropertyAddress, + &defaultDevicePropertyAddress, 0, NULL, &size, &audioDevice) != noErr) { - qWarning() << "QAudioDeviceInfo: Unable to find default output device"; + qWarning("QAudioDeviceInfo: Unable to find default %s device", (mode == QAudio::AudioOutput) ? "output" : "input"); return QByteArray(); } - return get_device_info(audioDevice, QAudio::AudioOutput); + return get_device_info(audioDevice, mode); #else //iOS - return CoreAudioSessionManager::instance().outputDevices().first(); + const auto &devices = (mode == QAudio::AudioOutput) ? CoreAudioSessionManager::instance().outputDevices() + : CoreAudioSessionManager::instance().inputDevices(); + return !devices.isEmpty() ? devices.first() : QByteArray(); #endif } @@ -343,15 +325,10 @@ QList<QByteArray> CoreAudioDeviceInfo::availableDevices(QAudio::Mode mode) AudioDeviceID* audioDevices = new AudioDeviceID[dc]; if (AudioObjectGetPropertyData(kAudioObjectSystemObject, &audioDevicesPropertyAddress, 0, NULL, &propSize, audioDevices) == noErr) { - QByteArray defaultDevice = (mode == QAudio::AudioOutput) ? defaultOutputDevice() : defaultInputDevice(); for (int i = 0; i < dc; ++i) { - QByteArray info = get_device_info(audioDevices[i], mode); - if (!info.isNull()) { - if (info == defaultDevice) - devices.prepend(info); - else - devices << info; - } + const QByteArray &info = get_device_info(audioDevices[i], mode); + if (!info.isNull()) + devices << info; } } diff --git a/src/plugins/coreaudio/coreaudioplugin.h b/src/plugins/coreaudio/coreaudioplugin.h index 5868508d2..da18d8cfe 100644 --- a/src/plugins/coreaudio/coreaudioplugin.h +++ b/src/plugins/coreaudio/coreaudioplugin.h @@ -39,19 +39,22 @@ #ifndef IOSAUDIOPLUGIN_H #define IOSAUDIOPLUGIN_H -#include <qaudiosystemplugin.h> +#include <QtMultimedia/qaudiosystemplugin.h> +#include <QtMultimedia/private/qaudiosystempluginext_p.h> QT_BEGIN_NAMESPACE -class CoreAudioPlugin : public QAudioSystemPlugin +class CoreAudioPlugin : public QAudioSystemPlugin, public QAudioSystemPluginExtension { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.qt.audiosystemfactory/5.0" FILE "coreaudio.json") + Q_INTERFACES(QAudioSystemPluginExtension) public: explicit CoreAudioPlugin(QObject *parent = 0); ~CoreAudioPlugin() {} + QByteArray defaultDevice(QAudio::Mode mode) const Q_DECL_OVERRIDE; QList<QByteArray> availableDevices(QAudio::Mode mode) const Q_DECL_OVERRIDE; QAbstractAudioInput *createInput(const QByteArray &device) Q_DECL_OVERRIDE; QAbstractAudioOutput *createOutput(const QByteArray &device) Q_DECL_OVERRIDE; diff --git a/src/plugins/coreaudio/coreaudioplugin.mm b/src/plugins/coreaudio/coreaudioplugin.mm index 6d899fb67..ac51b9cd0 100644 --- a/src/plugins/coreaudio/coreaudioplugin.mm +++ b/src/plugins/coreaudio/coreaudioplugin.mm @@ -49,6 +49,10 @@ CoreAudioPlugin::CoreAudioPlugin(QObject *parent) { } +QByteArray CoreAudioPlugin::defaultDevice(QAudio::Mode mode) const +{ + return CoreAudioDeviceInfo::defaultDevice(mode); +} QList<QByteArray> CoreAudioPlugin::availableDevices(QAudio::Mode mode) const { |