From 3c5bbb0dac7bed3199ddddc88c0175d5a2ac1036 Mon Sep 17 00:00:00 2001 From: Christian Stromme Date: Tue, 19 Apr 2016 14:53:24 +0200 Subject: 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 --- src/multimedia/audio/audio.pri | 3 +- src/multimedia/audio/qaudiodevicefactory.cpp | 73 +++++++++++++++----------- src/multimedia/audio/qaudiodevicefactory_p.h | 3 +- src/multimedia/audio/qaudiodeviceinfo.cpp | 4 +- src/multimedia/audio/qaudiosystemplugin.cpp | 5 ++ src/multimedia/audio/qaudiosystempluginext_p.h | 71 +++++++++++++++++++++++++ 6 files changed, 124 insertions(+), 35 deletions(-) create mode 100644 src/multimedia/audio/qaudiosystempluginext_p.h (limited to 'src/multimedia/audio') diff --git a/src/multimedia/audio/audio.pri b/src/multimedia/audio/audio.pri index 96cfb1ce4..4706fd23e 100644 --- a/src/multimedia/audio/audio.pri +++ b/src/multimedia/audio/audio.pri @@ -19,7 +19,8 @@ PRIVATE_HEADERS += \ audio/qaudiodevicefactory_p.h \ audio/qwavedecoder_p.h \ audio/qsamplecache_p.h \ - audio/qaudiohelpers_p.h + audio/qaudiohelpers_p.h \ + audio/qaudiosystempluginext_p.h SOURCES += \ audio/qaudio.cpp \ diff --git a/src/multimedia/audio/qaudiodevicefactory.cpp b/src/multimedia/audio/qaudiodevicefactory.cpp index 83e1a6c0b..c3e4929b3 100644 --- a/src/multimedia/audio/qaudiodevicefactory.cpp +++ b/src/multimedia/audio/qaudiodevicefactory.cpp @@ -41,6 +41,7 @@ #include "qaudiosystem.h" #include "qaudiosystemplugin.h" +#include "qaudiosystempluginext_p.h" #include "qmediapluginloader_p.h" #include "qaudiodevicefactory_p.h" @@ -139,41 +140,53 @@ QList QAudioDeviceFactory::availableDevices(QAudio::Mode mode) return devices; } -QAudioDeviceInfo QAudioDeviceFactory::defaultInputDevice() +QAudioDeviceInfo QAudioDeviceFactory::defaultDevice(QAudio::Mode mode) { #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS) - QAudioSystemFactoryInterface* plugin = qobject_cast(audioLoader()->instance(defaultKey())); - if (plugin) { - QList list = plugin->availableDevices(QAudio::AudioInput); - if (list.size() > 0) - return QAudioDeviceInfo(defaultKey(), list.at(0), QAudio::AudioInput); - } + QMediaPluginLoader* l = audioLoader(); - // if no plugin is marked as default or if the default plugin doesn't have any input device, - // return the first input available from other plugins. - QList inputDevices = availableDevices(QAudio::AudioInput); - if (!inputDevices.isEmpty()) - return inputDevices.first(); -#endif + // Check if there is a default plugin. + QAudioSystemFactoryInterface *plugin = qobject_cast(l->instance(defaultKey())); + if (plugin) { + // Check if the plugin has the extension interface. + QAudioSystemPluginExtension *pluginExt = qobject_cast(l->instance(defaultKey())); + // Ask for the default device. + if (pluginExt) { + const QByteArray &device = pluginExt->defaultDevice(mode); + if (!device.isEmpty()) + return QAudioDeviceInfo(defaultKey(), device, mode); + } - return QAudioDeviceInfo(); -} + // If there were no default devices, e.g., if the plugin did not implement the extent-ion interface, + // then just pick the first device that's available. + const auto &devices = plugin->availableDevices(mode); + if (!devices.isEmpty()) + return QAudioDeviceInfo(defaultKey(), devices.first(), mode); + } -QAudioDeviceInfo QAudioDeviceFactory::defaultOutputDevice() -{ -#if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS) - QAudioSystemFactoryInterface* plugin = qobject_cast(audioLoader()->instance(defaultKey())); - if (plugin) { - QList list = plugin->availableDevices(QAudio::AudioOutput); - if (list.size() > 0) - return QAudioDeviceInfo(defaultKey(), list.at(0), QAudio::AudioOutput); + // If no plugin is marked as default, check the other plugins. + // Note: We're going to prioritize plugins that report a default device. + const auto &keys = l->keys(); + QAudioDeviceInfo fallbackDevice; + for (const auto &key : keys) { + if (key == defaultKey()) + continue; + QAudioSystemFactoryInterface* plugin = qobject_cast(l->instance(key)); + if (plugin) { + // Check if the plugin has the extent-ion interface. + QAudioSystemPluginExtension *pluginExt = qobject_cast(l->instance(key)); + if (pluginExt) { + const QByteArray &device = pluginExt->defaultDevice(mode); + if (!device.isEmpty()) + return QAudioDeviceInfo(key, device, mode); + } else if (fallbackDevice.isNull()) { + const auto &devices = plugin->availableDevices(mode); + if (!devices.isEmpty()) + fallbackDevice = QAudioDeviceInfo(key, devices.first(), mode); + } + } } - // if no plugin is marked as default or if the default plugin doesn't have any output device, - // return the first output available from other plugins. - QList outputDevices = availableDevices(QAudio::AudioOutput); - if (!outputDevices.isEmpty()) - return outputDevices.first(); #endif return QAudioDeviceInfo(); @@ -196,12 +209,12 @@ QAbstractAudioDeviceInfo* QAudioDeviceFactory::audioDeviceInfo(const QString &re QAbstractAudioInput* QAudioDeviceFactory::createDefaultInputDevice(QAudioFormat const &format) { - return createInputDevice(defaultInputDevice(), format); + return createInputDevice(defaultDevice(QAudio::AudioInput), format); } QAbstractAudioOutput* QAudioDeviceFactory::createDefaultOutputDevice(QAudioFormat const &format) { - return createOutputDevice(defaultOutputDevice(), format); + return createOutputDevice(defaultDevice(QAudio::AudioOutput), format); } QAbstractAudioInput* QAudioDeviceFactory::createInputDevice(QAudioDeviceInfo const& deviceInfo, QAudioFormat const &format) diff --git a/src/multimedia/audio/qaudiodevicefactory_p.h b/src/multimedia/audio/qaudiodevicefactory_p.h index 833184075..7ad5e4e78 100644 --- a/src/multimedia/audio/qaudiodevicefactory_p.h +++ b/src/multimedia/audio/qaudiodevicefactory_p.h @@ -71,8 +71,7 @@ class QAudioDeviceFactory public: static QList availableDevices(QAudio::Mode mode); - static QAudioDeviceInfo defaultInputDevice(); - static QAudioDeviceInfo defaultOutputDevice(); + static QAudioDeviceInfo defaultDevice(QAudio::Mode mode); static QAbstractAudioDeviceInfo* audioDeviceInfo(const QString &realm, const QByteArray &handle, QAudio::Mode mode); diff --git a/src/multimedia/audio/qaudiodeviceinfo.cpp b/src/multimedia/audio/qaudiodeviceinfo.cpp index 945b415e8..f4f548017 100644 --- a/src/multimedia/audio/qaudiodeviceinfo.cpp +++ b/src/multimedia/audio/qaudiodeviceinfo.cpp @@ -419,7 +419,7 @@ QList QAudioDeviceInfo::supportedSampleTypes() const */ QAudioDeviceInfo QAudioDeviceInfo::defaultInputDevice() { - return QAudioDeviceFactory::defaultInputDevice(); + return QAudioDeviceFactory::defaultDevice(QAudio::AudioInput); } /*! @@ -428,7 +428,7 @@ QAudioDeviceInfo QAudioDeviceInfo::defaultInputDevice() */ QAudioDeviceInfo QAudioDeviceInfo::defaultOutputDevice() { - return QAudioDeviceFactory::defaultOutputDevice(); + return QAudioDeviceFactory::defaultDevice(QAudio::AudioOutput); } /*! diff --git a/src/multimedia/audio/qaudiosystemplugin.cpp b/src/multimedia/audio/qaudiosystemplugin.cpp index b4fc0dbca..018392939 100644 --- a/src/multimedia/audio/qaudiosystemplugin.cpp +++ b/src/multimedia/audio/qaudiosystemplugin.cpp @@ -39,6 +39,7 @@ #include "qaudiosystemplugin.h" +#include "qaudiosystempluginext_p.h" QT_BEGIN_NAMESPACE @@ -46,6 +47,10 @@ QAudioSystemFactoryInterface::~QAudioSystemFactoryInterface() { } +QAudioSystemPluginExtension::~QAudioSystemPluginExtension() +{ +} + /*! \class QAudioSystemPlugin \brief The QAudioSystemPlugin class provides an abstract base for audio plugins. diff --git a/src/multimedia/audio/qaudiosystempluginext_p.h b/src/multimedia/audio/qaudiosystempluginext_p.h new file mode 100644 index 000000000..380bc5afa --- /dev/null +++ b/src/multimedia/audio/qaudiosystempluginext_p.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** 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 The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QAUDIOSYSTEMPLUGINEXT_P_H +#define QAUDIOSYSTEMPLUGINEXT_P_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +struct Q_MULTIMEDIA_EXPORT QAudioSystemPluginExtension +{ + virtual QByteArray defaultDevice(QAudio::Mode) const = 0; + virtual ~QAudioSystemPluginExtension(); +}; + +#define QAudioSystemPluginExtension_iid "org.qt-project.qt.audiosystempluginextension" +Q_DECLARE_INTERFACE(QAudioSystemPluginExtension, QAudioSystemPluginExtension_iid) + +QT_END_NAMESPACE + +#endif // QAUDIOSYSTEMPLUGINEXT_P_H -- cgit v1.2.1