From d18ac6dd498991b0fd16cfa7a6e0664ec705030c Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Fri, 28 Apr 2017 23:02:28 +0200 Subject: [PATCH 01/10] Reimplemented Tablet Audio screen using C++ model --- interface/resources/qml/hifi/Audio.qml | 83 ++++++------- .../qml/hifi/components/AudioCheckbox.qml | 2 + .../AudioDeviceScriptingInterface.cpp | 115 +++++++++++++++++- .../scripting/AudioDeviceScriptingInterface.h | 33 ++++- libraries/audio-client/src/AudioClient.cpp | 5 +- libraries/audio-client/src/AudioClient.h | 3 + scripts/system/selectAudioDevice.js | 15 ++- 7 files changed, 208 insertions(+), 48 deletions(-) diff --git a/interface/resources/qml/hifi/Audio.qml b/interface/resources/qml/hifi/Audio.qml index d0c3122100..adb0cd4cba 100644 --- a/interface/resources/qml/hifi/Audio.qml +++ b/interface/resources/qml/hifi/Audio.qml @@ -35,11 +35,6 @@ Rectangle { property string title: "Audio Options" signal sendToScript(var message); - //set models after Components is shown - Component.onCompleted: { - refreshTimer.start() - refreshTimerOutput.start() - } Component { id: separator @@ -138,30 +133,34 @@ Rectangle { } ListView { - Timer { - id: refreshTimer - interval: 1 - repeat: false - onTriggered: { - //refresh model - inputAudioListView.model = undefined - inputAudioListView.model = AudioDevice.inputAudioDevices - } - } id: inputAudioListView anchors { left: parent.left; right: parent.right; leftMargin: 70 } height: 125 - spacing: 16 + spacing: 0 clip: true snapMode: ListView.SnapToItem - delegate: AudioCheckbox { + model: AudioDevice + delegate: Item { width: parent.width - checkbox.checked: (modelData === AudioDevice.getInputDevice()) - text.text: modelData - onCheckBoxClicked: { - if (checked) { - AudioDevice.setInputDevice(modelData) - refreshTimer.start() + visible: devicemode === 0 + height: visible ? 36 : 0 + + AudioCheckbox { + id: cbin + anchors.verticalCenter: parent.verticalCenter + Binding { + target: cbin.checkbox + property: 'checked' + value: devicechecked + } + + width: parent.width + cbchecked: devicechecked + text.text: devicename + onCheckBoxClicked: { + if (checked) { + AudioDevice.setInputDeviceAsync(devicename) + } } } } @@ -191,31 +190,33 @@ Rectangle { text: qsTr("CHOOSE OUTPUT DEVICE") } } + ListView { id: outputAudioListView - Timer { - id: refreshTimerOutput - interval: 1 - repeat: false - onTriggered: { - //refresh model - outputAudioListView.model = undefined - outputAudioListView.model = AudioDevice.outputAudioDevices - } - } anchors { left: parent.left; right: parent.right; leftMargin: 70 } height: 250 - spacing: 16 + spacing: 0 clip: true snapMode: ListView.SnapToItem - delegate: AudioCheckbox { + model: AudioDevice + delegate: Item { width: parent.width - checkbox.checked: (modelData === AudioDevice.getOutputDevice()) - text.text: modelData - onCheckBoxClicked: { - if (checked) { - AudioDevice.setOutputDevice(modelData) - refreshTimerOutput.start() + visible: devicemode === 1 + height: visible ? 36 : 0 + AudioCheckbox { + id: cbout + width: parent.width + anchors.verticalCenter: parent.verticalCenter + Binding { + target: cbout.checkbox + property: 'checked' + value: devicechecked + } + text.text: devicename + onCheckBoxClicked: { + if (checked) { + AudioDevice.setOutputDeviceAsync(devicename) + } } } } diff --git a/interface/resources/qml/hifi/components/AudioCheckbox.qml b/interface/resources/qml/hifi/components/AudioCheckbox.qml index a8e0441e0a..b23c9b42fe 100644 --- a/interface/resources/qml/hifi/components/AudioCheckbox.qml +++ b/interface/resources/qml/hifi/components/AudioCheckbox.qml @@ -8,6 +8,7 @@ Row { id: row spacing: 16 property alias checkbox: cb + property alias cbchecked: cb.checked property alias text: txt signal checkBoxClicked(bool checked) @@ -17,6 +18,7 @@ Row { colorScheme: hifi.colorSchemes.dark anchors.verticalCenter: parent.verticalCenter onClicked: checkBoxClicked(cb.checked) + onCheckedChanged: console.log("checked", checked, "device ",txt.text) } RalewayBold { id: txt diff --git a/interface/src/scripting/AudioDeviceScriptingInterface.cpp b/interface/src/scripting/AudioDeviceScriptingInterface.cpp index cbb08c0af0..4cec75074f 100644 --- a/interface/src/scripting/AudioDeviceScriptingInterface.cpp +++ b/interface/src/scripting/AudioDeviceScriptingInterface.cpp @@ -33,11 +33,17 @@ bool AudioDeviceScriptingInterface::muted() return getMuted(); } -AudioDeviceScriptingInterface::AudioDeviceScriptingInterface() { +AudioDeviceScriptingInterface::AudioDeviceScriptingInterface(): QAbstractListModel(nullptr) { connect(DependencyManager::get().data(), &AudioClient::muteToggled, this, &AudioDeviceScriptingInterface::muteToggled); connect(DependencyManager::get().data(), &AudioClient::deviceChanged, - this, &AudioDeviceScriptingInterface::deviceChanged); + this, &AudioDeviceScriptingInterface::onDeviceChanged, Qt::QueuedConnection); + connect(DependencyManager::get().data(), &AudioClient::currentInputDeviceChanged, + this, &AudioDeviceScriptingInterface::onCurrentInputDeviceChanged, Qt::QueuedConnection); + connect(DependencyManager::get().data(), &AudioClient::currentOutputDeviceChanged, + this, &AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged, Qt::QueuedConnection); + //fill up model + onDeviceChanged(); } bool AudioDeviceScriptingInterface::setInputDevice(const QString& deviceName) { @@ -51,13 +57,27 @@ bool AudioDeviceScriptingInterface::setInputDevice(const QString& deviceName) { bool AudioDeviceScriptingInterface::setOutputDevice(const QString& deviceName) { bool result; + QTime a; a.start(); QMetaObject::invokeMethod(DependencyManager::get().data(), "switchOutputToAudioDevice", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, result), Q_ARG(const QString&, deviceName)); + qDebug() << "switching to" << deviceName << "elapsed" << a.elapsed(); return result; } +void AudioDeviceScriptingInterface::setInputDeviceAsync(const QString& deviceName) { + QMetaObject::invokeMethod(DependencyManager::get().data(), "switchInputToAudioDevice", + Qt::QueuedConnection, + Q_ARG(const QString&, deviceName)); +} + +void AudioDeviceScriptingInterface::setOutputDeviceAsync(const QString& deviceName) { + QMetaObject::invokeMethod(DependencyManager::get().data(), "switchOutputToAudioDevice", + Qt::QueuedConnection, + Q_ARG(const QString&, deviceName)); +} + QString AudioDeviceScriptingInterface::getInputDevice() { return DependencyManager::get()->getDeviceName(QAudio::AudioInput); } @@ -116,3 +136,94 @@ void AudioDeviceScriptingInterface::setMuted(bool muted) bool AudioDeviceScriptingInterface::getMuted() { return DependencyManager::get()->isMuted(); } + +QVariant AudioDeviceScriptingInterface::data(const QModelIndex& index, int role) const { + //sanity + if (!index.isValid() || index.row() >= _devices.size()) + return QVariant(); + + + if (role == Qt::DisplayRole || role == DisplayNameRole) { + return _devices.at(index.row()).name; + } else if (role == SelectedRole) { + return _devices.at(index.row()).selected; + } else if (role == AudioModeRole) { + return (int)_devices.at(index.row()).mode; + } + return QVariant(); +} + +int AudioDeviceScriptingInterface::rowCount(const QModelIndex& parent) const { + Q_UNUSED(parent) + return _devices.size(); +} + +QHash AudioDeviceScriptingInterface::roleNames() const { + QHash roles; + roles.insert(DisplayNameRole, "devicename"); + roles.insert(SelectedRole, "devicechecked"); + roles.insert(AudioModeRole, "devicemode"); + return roles; +} + +void AudioDeviceScriptingInterface::onDeviceChanged() +{ + beginResetModel(); + _devices.clear(); + const QString &outDevice = getOutputDevice(); + for (QString name: getOutputDevices()) { + AudioDeviceInfo di; + di.name = name; + di.selected = (name == outDevice); + di.mode = QAudio::AudioOutput; + _devices.append(di); + } + const QString &inDevice = getInputDevice(); + for (QString name: getInputDevices()) { + AudioDeviceInfo di; + di.name = name; + di.selected = (name == inDevice); + di.mode = QAudio::AudioInput; + _devices.append(di); + } + + endResetModel(); + emit deviceChanged(); +} + +void AudioDeviceScriptingInterface::onCurrentInputDeviceChanged() +{ + currentDeviceUpdate(getInputDevice(), QAudio::AudioInput); + emit currentInputDeviceChanged(); +} + +void AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged() +{ + currentDeviceUpdate(getOutputDevice(), QAudio::AudioOutput); + emit currentOutputDeviceChanged(); +} + +void AudioDeviceScriptingInterface::currentDeviceUpdate(const QString &name, QAudio::Mode mode) +{ + QVector role; + role.append(SelectedRole); + + qDebug() << "device update" << name << mode; + + for (int i = 0; i < _devices.size(); i++) { + AudioDeviceInfo di = _devices.at(i); + if (di.mode != mode) + continue; + if (di.selected && di.name != name ) { + di.selected = false; + _devices[i] = di; + emit dataChanged(index(i, 0), index(i, 0), role); + } + if (di.name == name) { + di.selected = true; + _devices[i] = di; + emit dataChanged(index(i, 0), index(i, 0), role); + qDebug() << "device updated" << di.name << i; + } + } +} diff --git a/interface/src/scripting/AudioDeviceScriptingInterface.h b/interface/src/scripting/AudioDeviceScriptingInterface.h index 4d1d47dcba..cadbe88d4d 100644 --- a/interface/src/scripting/AudioDeviceScriptingInterface.h +++ b/interface/src/scripting/AudioDeviceScriptingInterface.h @@ -15,10 +15,18 @@ #include #include #include +#include +#include class AudioEffectOptions; -class AudioDeviceScriptingInterface : public QObject { +struct AudioDeviceInfo { + QString name; + bool selected; + QAudio::Mode mode; +}; + +class AudioDeviceScriptingInterface : public QAbstractListModel { Q_OBJECT Q_PROPERTY(QStringList inputAudioDevices READ inputAudioDevices NOTIFY inputAudioDevicesChanged) @@ -32,6 +40,22 @@ public: QStringList outputAudioDevices() const; bool muted(); + QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; + int rowCount(const QModelIndex& parent = QModelIndex()) const override; + QHash roleNames() const override; + + enum Roles { + DisplayNameRole = Qt::UserRole, + SelectedRole, + AudioModeRole + }; + +private slots: + void onDeviceChanged(); + void onCurrentInputDeviceChanged(); + void onCurrentOutputDeviceChanged(); + void currentDeviceUpdate(const QString &name, QAudio::Mode mode); + public slots: bool setInputDevice(const QString& deviceName); bool setOutputDevice(const QString& deviceName); @@ -55,15 +79,22 @@ public slots: void setMuted(bool muted); + void setInputDeviceAsync(const QString &deviceName); + void setOutputDeviceAsync(const QString &deviceName); private: AudioDeviceScriptingInterface(); signals: void muteToggled(); void deviceChanged(); + void currentInputDeviceChanged(); + void currentOutputDeviceChanged(); void mutedChanged(bool muted); void inputAudioDevicesChanged(QStringList inputAudioDevices); void outputAudioDevicesChanged(QStringList outputAudioDevices); + +private: + QVector _devices; }; #endif // hifi_AudioDeviceScriptingInterface_h diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp index b684aac89c..6e0feb3b20 100644 --- a/libraries/audio-client/src/AudioClient.cpp +++ b/libraries/audio-client/src/AudioClient.cpp @@ -769,7 +769,8 @@ QString AudioClient::getDefaultDeviceName(QAudio::Mode mode) { QVector AudioClient::getDeviceNames(QAudio::Mode mode) { QVector deviceNames; - foreach(QAudioDeviceInfo audioDevice, getAvailableDevices(mode)) { + const QList &availableDevice = getAvailableDevices(mode); + foreach(const QAudioDeviceInfo &audioDevice, availableDevice) { deviceNames << audioDevice.deviceName().trimmed(); } return deviceNames; @@ -1371,6 +1372,7 @@ bool AudioClient::switchInputToAudioDevice(const QAudioDeviceInfo& inputDeviceIn if (!inputDeviceInfo.isNull()) { qCDebug(audioclient) << "The audio input device " << inputDeviceInfo.deviceName() << "is available."; _inputAudioDeviceName = inputDeviceInfo.deviceName().trimmed(); + emit currentInputDeviceChanged(); if (adjustedFormatForAudioDevice(inputDeviceInfo, _desiredInputFormat, _inputFormat)) { qCDebug(audioclient) << "The format to be used for audio input is" << _inputFormat; @@ -1488,6 +1490,7 @@ bool AudioClient::switchOutputToAudioDevice(const QAudioDeviceInfo& outputDevice if (!outputDeviceInfo.isNull()) { qCDebug(audioclient) << "The audio output device " << outputDeviceInfo.deviceName() << "is available."; _outputAudioDeviceName = outputDeviceInfo.deviceName().trimmed(); + emit currentOutputDeviceChanged(); if (adjustedFormatForAudioDevice(outputDeviceInfo, _desiredOutputFormat, _outputFormat)) { qCDebug(audioclient) << "The format to be used for audio output is" << _outputFormat; diff --git a/libraries/audio-client/src/AudioClient.h b/libraries/audio-client/src/AudioClient.h index 139749e8e8..acd96c395e 100644 --- a/libraries/audio-client/src/AudioClient.h +++ b/libraries/audio-client/src/AudioClient.h @@ -240,6 +240,9 @@ signals: void muteEnvironmentRequested(glm::vec3 position, float radius); + void currentOutputDeviceChanged(); + void currentInputDeviceChanged(); + protected: AudioClient(); ~AudioClient(); diff --git a/scripts/system/selectAudioDevice.js b/scripts/system/selectAudioDevice.js index 2dd426932f..61862ad67b 100644 --- a/scripts/system/selectAudioDevice.js +++ b/scripts/system/selectAudioDevice.js @@ -64,13 +64,14 @@ function setupAudioMenus() { // Setup audio input devices Menu.addSeparator("Audio", "Input Audio Device"); var inputDevices = AudioDevice.getInputDevices(); + var currentInputDevice = AudioDevice.getInputDevice() for (var i = 0; i < inputDevices.length; i++) { var audioDeviceMenuString = "Use " + inputDevices[i] + " for Input"; Menu.addMenuItem({ menuName: "Audio", menuItemName: audioDeviceMenuString, isCheckable: true, - isChecked: inputDevices[i] == AudioDevice.getInputDevice() + isChecked: inputDevices[i] == currentInputDevice }); audioDevicesList.push(audioDeviceMenuString); } @@ -78,13 +79,14 @@ function setupAudioMenus() { // Setup audio output devices Menu.addSeparator("Audio", "Output Audio Device"); var outputDevices = AudioDevice.getOutputDevices(); + var currentOutputDevice = AudioDevice.getOutputDevice() for (var i = 0; i < outputDevices.length; i++) { var audioDeviceMenuString = "Use " + outputDevices[i] + " for Output"; Menu.addMenuItem({ menuName: "Audio", menuItemName: audioDeviceMenuString, isCheckable: true, - isChecked: outputDevices[i] == AudioDevice.getOutputDevice() + isChecked: outputDevices[i] == currentOutputDevice }); audioDevicesList.push(audioDeviceMenuString); } @@ -133,6 +135,11 @@ function onMenuEvent(audioDeviceMenuString) { } } +function onCurrentDeviceChanged() { + debug("System audio device switched. "); + setupAudioMenus() +} + function switchAudioDevice(audioDeviceMenuString) { // if the device is not plugged in, short-circuit if (!~audioDevicesList.indexOf(audioDeviceMenuString)) { @@ -254,7 +261,9 @@ function checkHMDAudio() { // Wait for the C++ systems to fire up before trying to do anything with audio devices Script.setTimeout(function () { debug("Connecting deviceChanged(), displayModeChanged(), and switchAudioDevice()..."); - AudioDevice.deviceChanged.connect(onDevicechanged); +// AudioDevice.deviceChanged.connect(onDevicechanged); +// AudioDevice.currentInputDeviceChanged.connect(onCurrentDeviceChanged); +// AudioDevice.currentOutputDeviceChanged.connect(onCurrentDeviceChanged); HMD.displayModeChanged.connect(checkHMDAudio); Menu.menuItemEvent.connect(onMenuEvent); debug("Setting up Audio I/O menu for the first time..."); From 3fe8675b0cbc306a5509f392f8769850584ad1c7 Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Sun, 30 Apr 2017 12:39:36 +0200 Subject: [PATCH 02/10] Sync Tablet Audio and Desktop Audio menus --- scripts/system/selectAudioDevice.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/system/selectAudioDevice.js b/scripts/system/selectAudioDevice.js index 61862ad67b..eb20dc0c92 100644 --- a/scripts/system/selectAudioDevice.js +++ b/scripts/system/selectAudioDevice.js @@ -261,9 +261,9 @@ function checkHMDAudio() { // Wait for the C++ systems to fire up before trying to do anything with audio devices Script.setTimeout(function () { debug("Connecting deviceChanged(), displayModeChanged(), and switchAudioDevice()..."); -// AudioDevice.deviceChanged.connect(onDevicechanged); -// AudioDevice.currentInputDeviceChanged.connect(onCurrentDeviceChanged); -// AudioDevice.currentOutputDeviceChanged.connect(onCurrentDeviceChanged); + AudioDevice.deviceChanged.connect(onDevicechanged); + AudioDevice.currentInputDeviceChanged.connect(onCurrentDeviceChanged); + AudioDevice.currentOutputDeviceChanged.connect(onCurrentDeviceChanged); HMD.displayModeChanged.connect(checkHMDAudio); Menu.menuItemEvent.connect(onMenuEvent); debug("Setting up Audio I/O menu for the first time..."); From 28447cde0b4a4295fe7515c089eee021f52a540c Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Mon, 1 May 2017 18:41:38 +0200 Subject: [PATCH 03/10] Get rid of QML errors in HMD mode. Sync Audio menu in Tablet/HMD without recreating menu items --- interface/resources/qml/hifi/Audio.qml | 8 +++++--- .../resources/qml/hifi/components/AudioCheckbox.qml | 1 - interface/src/Application.cpp | 2 ++ scripts/system/selectAudioDevice.js | 13 ++++++++++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/interface/resources/qml/hifi/Audio.qml b/interface/resources/qml/hifi/Audio.qml index adb0cd4cba..09075e6fb5 100644 --- a/interface/resources/qml/hifi/Audio.qml +++ b/interface/resources/qml/hifi/Audio.qml @@ -79,7 +79,7 @@ Rectangle { } Connections { - target: AvatarInputs + target: AvatarInputs !== undefined ? AvatarInputs : null onShowAudioToolsChanged: { audioTools.checkbox.checked = showAudioTools } @@ -100,10 +100,12 @@ Rectangle { id: audioTools width: parent.width anchors { left: parent.left; right: parent.right; leftMargin: 30 } - checkbox.checked: AvatarInputs.showAudioTools + checkbox.checked: AvatarInputs !== undefined ? AvatarInputs.showAudioTools : false text.text: qsTr("Show audio level meter") onCheckBoxClicked: { - AvatarInputs.showAudioTools = checked + if (AvatarInputs !== undefined) { + AvatarInputs.showAudioTools = checked + } } } diff --git a/interface/resources/qml/hifi/components/AudioCheckbox.qml b/interface/resources/qml/hifi/components/AudioCheckbox.qml index b23c9b42fe..b037fe4c7d 100644 --- a/interface/resources/qml/hifi/components/AudioCheckbox.qml +++ b/interface/resources/qml/hifi/components/AudioCheckbox.qml @@ -18,7 +18,6 @@ Row { colorScheme: hifi.colorSchemes.dark anchors.verticalCenter: parent.verticalCenter onClicked: checkBoxClicked(cb.checked) - onCheckedChanged: console.log("checked", checked, "device ",txt.text) } RalewayBold { id: txt diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 171c755101..4cf97ed6b1 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2057,6 +2057,8 @@ void Application::initializeUi() { rootContext->setContextProperty("ApplicationCompositor", &getApplicationCompositor()); + rootContext->setContextProperty("AvatarInputs", AvatarInputs::getInstance()); + if (auto steamClient = PluginManager::getInstance()->getSteamClientPlugin()) { rootContext->setContextProperty("Steam", new SteamScriptingInterface(engine, steamClient.get())); } diff --git a/scripts/system/selectAudioDevice.js b/scripts/system/selectAudioDevice.js index eb20dc0c92..9dcfb86ea5 100644 --- a/scripts/system/selectAudioDevice.js +++ b/scripts/system/selectAudioDevice.js @@ -137,7 +137,16 @@ function onMenuEvent(audioDeviceMenuString) { function onCurrentDeviceChanged() { debug("System audio device switched. "); - setupAudioMenus() + var interfaceInputDevice = "Use " + AudioDevice.getInputDevice() + " for Input"; + var interfaceOutputDevice = "Use " + AudioDevice.getOutputDevice() + " for Output"; + for (var index = 0; index < audioDevicesList.length; index++) { + if (audioDevicesList[index] === interfaceInputDevice || + audioDevicesList[index] === interfaceOutputDevice) { + Menu.setIsOptionChecked(audioDevicesList[index], true); + } else { + Menu.setIsOptionChecked(audioDevicesList[index], false); + } + } } function switchAudioDevice(audioDeviceMenuString) { @@ -279,6 +288,8 @@ Script.scriptEnding.connect(function () { removeAudioMenus(); Menu.menuItemEvent.disconnect(onMenuEvent); HMD.displayModeChanged.disconnect(checkHMDAudio); + AudioDevice.currentInputDeviceChanged.disconnect(onCurrentDeviceChanged); + AudioDevice.currentOutputDeviceChanged.disconnect(onCurrentDeviceChanged); AudioDevice.deviceChanged.disconnect(onDevicechanged); }); From d522864141bf3e53a8dbf6fa15899d5645a5fd43 Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Thu, 4 May 2017 21:16:58 +0200 Subject: [PATCH 04/10] Remove debug prints --- interface/src/scripting/AudioDeviceScriptingInterface.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/interface/src/scripting/AudioDeviceScriptingInterface.cpp b/interface/src/scripting/AudioDeviceScriptingInterface.cpp index 4cec75074f..e5a419b7a2 100644 --- a/interface/src/scripting/AudioDeviceScriptingInterface.cpp +++ b/interface/src/scripting/AudioDeviceScriptingInterface.cpp @@ -57,12 +57,10 @@ bool AudioDeviceScriptingInterface::setInputDevice(const QString& deviceName) { bool AudioDeviceScriptingInterface::setOutputDevice(const QString& deviceName) { bool result; - QTime a; a.start(); QMetaObject::invokeMethod(DependencyManager::get().data(), "switchOutputToAudioDevice", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, result), Q_ARG(const QString&, deviceName)); - qDebug() << "switching to" << deviceName << "elapsed" << a.elapsed(); return result; } @@ -208,8 +206,6 @@ void AudioDeviceScriptingInterface::currentDeviceUpdate(const QString &name, QAu QVector role; role.append(SelectedRole); - qDebug() << "device update" << name << mode; - for (int i = 0; i < _devices.size(); i++) { AudioDeviceInfo di = _devices.at(i); if (di.mode != mode) @@ -223,7 +219,6 @@ void AudioDeviceScriptingInterface::currentDeviceUpdate(const QString &name, QAu di.selected = true; _devices[i] = di; emit dataChanged(index(i, 0), index(i, 0), role); - qDebug() << "device updated" << di.name << i; } } } From fa9a4053cabc9847218625bc952ba4367e39e719 Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Mon, 8 May 2017 21:24:46 +0200 Subject: [PATCH 05/10] Save selected device in settings to make sure it will not be changed back in Menu --- interface/resources/qml/hifi/Audio.qml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interface/resources/qml/hifi/Audio.qml b/interface/resources/qml/hifi/Audio.qml index 09075e6fb5..b497edf58a 100644 --- a/interface/resources/qml/hifi/Audio.qml +++ b/interface/resources/qml/hifi/Audio.qml @@ -162,6 +162,7 @@ Rectangle { onCheckBoxClicked: { if (checked) { AudioDevice.setInputDeviceAsync(devicename) + Settings.setValue("audio_input_device", devicename); } } } @@ -218,6 +219,7 @@ Rectangle { onCheckBoxClicked: { if (checked) { AudioDevice.setOutputDeviceAsync(devicename) + Settings.setValue("audio_output_device", devicename); } } } From 2d46f9d30e640ed995692ce8157f4d1324ed1dc0 Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Wed, 10 May 2017 21:21:39 +0200 Subject: [PATCH 06/10] Move most of the logic to C++ model, speed up initialization. Reduce number of AudioClient blocking calls to minimum --- interface/resources/qml/hifi/Audio.qml | 2 - .../AudioDeviceScriptingInterface.cpp | 86 ++++++++--- .../scripting/AudioDeviceScriptingInterface.h | 19 ++- libraries/audio-client/src/AudioClient.cpp | 4 +- libraries/audio-client/src/AudioClient.h | 4 +- .../src/AudioScriptingInterface.h | 1 + scripts/system/selectAudioDevice.js | 135 ++++-------------- 7 files changed, 109 insertions(+), 142 deletions(-) diff --git a/interface/resources/qml/hifi/Audio.qml b/interface/resources/qml/hifi/Audio.qml index b497edf58a..09075e6fb5 100644 --- a/interface/resources/qml/hifi/Audio.qml +++ b/interface/resources/qml/hifi/Audio.qml @@ -162,7 +162,6 @@ Rectangle { onCheckBoxClicked: { if (checked) { AudioDevice.setInputDeviceAsync(devicename) - Settings.setValue("audio_input_device", devicename); } } } @@ -219,7 +218,6 @@ Rectangle { onCheckBoxClicked: { if (checked) { AudioDevice.setOutputDeviceAsync(devicename) - Settings.setValue("audio_output_device", devicename); } } } diff --git a/interface/src/scripting/AudioDeviceScriptingInterface.cpp b/interface/src/scripting/AudioDeviceScriptingInterface.cpp index e5a419b7a2..d76a37dbe0 100644 --- a/interface/src/scripting/AudioDeviceScriptingInterface.cpp +++ b/interface/src/scripting/AudioDeviceScriptingInterface.cpp @@ -11,21 +11,19 @@ #include "AudioClient.h" #include "AudioDeviceScriptingInterface.h" - +#include "SettingsScriptingInterface.h" AudioDeviceScriptingInterface* AudioDeviceScriptingInterface::getInstance() { static AudioDeviceScriptingInterface sharedInstance; return &sharedInstance; } -QStringList AudioDeviceScriptingInterface::inputAudioDevices() const -{ - return DependencyManager::get()->getDeviceNames(QAudio::AudioInput).toList();; +QStringList AudioDeviceScriptingInterface::inputAudioDevices() const { + return _inputAudioDevices; } -QStringList AudioDeviceScriptingInterface::outputAudioDevices() const -{ - return DependencyManager::get()->getDeviceNames(QAudio::AudioOutput).toList();; +QStringList AudioDeviceScriptingInterface::outputAudioDevices() const { + return _outputAudioDevices; } bool AudioDeviceScriptingInterface::muted() @@ -44,6 +42,16 @@ AudioDeviceScriptingInterface::AudioDeviceScriptingInterface(): QAbstractListMod this, &AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged, Qt::QueuedConnection); //fill up model onDeviceChanged(); + //set up previously saved device + SettingsScriptingInterface *settings = SettingsScriptingInterface::getInstance(); + const QString inDevice = settings->getValue("audio_input_device").toString(); + if (inDevice != _currentInputDevice) { + setInputDeviceAsync(inDevice); + } + const QString outDevice = settings->getValue("audio_output_device").toString(); + if (outDevice != _currentOutputDevice) { + setOutputDeviceAsync(outDevice); + } } bool AudioDeviceScriptingInterface::setInputDevice(const QString& deviceName) { @@ -64,6 +72,31 @@ bool AudioDeviceScriptingInterface::setOutputDevice(const QString& deviceName) { return result; } +bool AudioDeviceScriptingInterface::setDeviceFromMenu(const QString &deviceMenuName) { + QAudio::Mode mode; + + if (deviceMenuName.indexOf("for Output") != -1) { + mode = QAudio::AudioOutput; + } else if (deviceMenuName.indexOf("for Input") != -1) { + mode = QAudio::AudioInput; + } else { + return false; + } + + for (ScriptingAudioDeviceInfo di: _devices) { + if (mode == di.mode && deviceMenuName.contains(di.name)) { + if (mode == QAudio::AudioOutput) { + setOutputDeviceAsync(di.name); + } else { + setInputDeviceAsync(di.name); + } + return true; + } + } + + return false; +} + void AudioDeviceScriptingInterface::setInputDeviceAsync(const QString& deviceName) { QMetaObject::invokeMethod(DependencyManager::get().data(), "switchInputToAudioDevice", Qt::QueuedConnection, @@ -167,38 +200,51 @@ QHash AudioDeviceScriptingInterface::roleNames() const { void AudioDeviceScriptingInterface::onDeviceChanged() { beginResetModel(); + _outputAudioDevices.clear(); _devices.clear(); - const QString &outDevice = getOutputDevice(); + _currentOutputDevice = getOutputDevice(); for (QString name: getOutputDevices()) { - AudioDeviceInfo di; + ScriptingAudioDeviceInfo di; di.name = name; - di.selected = (name == outDevice); + di.selected = (name == _currentOutputDevice); di.mode = QAudio::AudioOutput; _devices.append(di); + _outputAudioDevices.append(name); } - const QString &inDevice = getInputDevice(); + emit outputAudioDevicesChanged(_outputAudioDevices); + + _inputAudioDevices.clear(); + _currentInputDevice = getInputDevice(); for (QString name: getInputDevices()) { - AudioDeviceInfo di; + ScriptingAudioDeviceInfo di; di.name = name; - di.selected = (name == inDevice); + di.selected = (name == _currentInputDevice); di.mode = QAudio::AudioInput; _devices.append(di); + _inputAudioDevices.append(name); } + emit inputAudioDevicesChanged(_inputAudioDevices); endResetModel(); emit deviceChanged(); } -void AudioDeviceScriptingInterface::onCurrentInputDeviceChanged() +void AudioDeviceScriptingInterface::onCurrentInputDeviceChanged(const QString &name) { - currentDeviceUpdate(getInputDevice(), QAudio::AudioInput); - emit currentInputDeviceChanged(); + currentDeviceUpdate(name, QAudio::AudioInput); + //we got a signal that device changed. Save it now + SettingsScriptingInterface *settings = SettingsScriptingInterface::getInstance(); + settings->setValue("audio_input_device", name); + emit currentInputDeviceChanged(name); } -void AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged() +void AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged(const QString &name) { - currentDeviceUpdate(getOutputDevice(), QAudio::AudioOutput); - emit currentOutputDeviceChanged(); + currentDeviceUpdate(name, QAudio::AudioOutput); + //we got a signal that device changed. Save it now + SettingsScriptingInterface *settings = SettingsScriptingInterface::getInstance(); + settings->setValue("audio_output_device", name); + emit currentOutputDeviceChanged(name); } void AudioDeviceScriptingInterface::currentDeviceUpdate(const QString &name, QAudio::Mode mode) @@ -207,7 +253,7 @@ void AudioDeviceScriptingInterface::currentDeviceUpdate(const QString &name, QAu role.append(SelectedRole); for (int i = 0; i < _devices.size(); i++) { - AudioDeviceInfo di = _devices.at(i); + ScriptingAudioDeviceInfo di = _devices.at(i); if (di.mode != mode) continue; if (di.selected && di.name != name ) { diff --git a/interface/src/scripting/AudioDeviceScriptingInterface.h b/interface/src/scripting/AudioDeviceScriptingInterface.h index cadbe88d4d..133069d6f3 100644 --- a/interface/src/scripting/AudioDeviceScriptingInterface.h +++ b/interface/src/scripting/AudioDeviceScriptingInterface.h @@ -20,7 +20,7 @@ class AudioEffectOptions; -struct AudioDeviceInfo { +struct ScriptingAudioDeviceInfo { QString name; bool selected; QAudio::Mode mode; @@ -52,13 +52,14 @@ public: private slots: void onDeviceChanged(); - void onCurrentInputDeviceChanged(); - void onCurrentOutputDeviceChanged(); + void onCurrentInputDeviceChanged(const QString &name); + void onCurrentOutputDeviceChanged(const QString &name); void currentDeviceUpdate(const QString &name, QAudio::Mode mode); public slots: bool setInputDevice(const QString& deviceName); bool setOutputDevice(const QString& deviceName); + bool setDeviceFromMenu(const QString& deviceMenuName); QString getInputDevice(); QString getOutputDevice(); @@ -87,14 +88,20 @@ private: signals: void muteToggled(); void deviceChanged(); - void currentInputDeviceChanged(); - void currentOutputDeviceChanged(); + void currentInputDeviceChanged(const QString &name); + void currentOutputDeviceChanged(const QString &name); void mutedChanged(bool muted); void inputAudioDevicesChanged(QStringList inputAudioDevices); void outputAudioDevicesChanged(QStringList outputAudioDevices); private: - QVector _devices; + QVector _devices; + + QStringList _inputAudioDevices; + QStringList _outputAudioDevices; + + QString _currentInputDevice; + QString _currentOutputDevice; }; #endif // hifi_AudioDeviceScriptingInterface_h diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp index adb100d6b7..e9134b3bd7 100644 --- a/libraries/audio-client/src/AudioClient.cpp +++ b/libraries/audio-client/src/AudioClient.cpp @@ -1375,7 +1375,7 @@ bool AudioClient::switchInputToAudioDevice(const QAudioDeviceInfo& inputDeviceIn if (!inputDeviceInfo.isNull()) { qCDebug(audioclient) << "The audio input device " << inputDeviceInfo.deviceName() << "is available."; _inputAudioDeviceName = inputDeviceInfo.deviceName().trimmed(); - emit currentInputDeviceChanged(); + emit currentInputDeviceChanged(_inputAudioDeviceName); if (adjustedFormatForAudioDevice(inputDeviceInfo, _desiredInputFormat, _inputFormat)) { qCDebug(audioclient) << "The format to be used for audio input is" << _inputFormat; @@ -1493,7 +1493,7 @@ bool AudioClient::switchOutputToAudioDevice(const QAudioDeviceInfo& outputDevice if (!outputDeviceInfo.isNull()) { qCDebug(audioclient) << "The audio output device " << outputDeviceInfo.deviceName() << "is available."; _outputAudioDeviceName = outputDeviceInfo.deviceName().trimmed(); - emit currentOutputDeviceChanged(); + emit currentOutputDeviceChanged(_outputAudioDeviceName); if (adjustedFormatForAudioDevice(outputDeviceInfo, _desiredOutputFormat, _outputFormat)) { qCDebug(audioclient) << "The format to be used for audio output is" << _outputFormat; diff --git a/libraries/audio-client/src/AudioClient.h b/libraries/audio-client/src/AudioClient.h index 8f2e5fcffd..89ba3db14f 100644 --- a/libraries/audio-client/src/AudioClient.h +++ b/libraries/audio-client/src/AudioClient.h @@ -238,8 +238,8 @@ signals: void muteEnvironmentRequested(glm::vec3 position, float radius); - void currentOutputDeviceChanged(); - void currentInputDeviceChanged(); + void currentOutputDeviceChanged(const QString &name); + void currentInputDeviceChanged(const QString &name); protected: AudioClient(); diff --git a/libraries/script-engine/src/AudioScriptingInterface.h b/libraries/script-engine/src/AudioScriptingInterface.h index e97bc329c6..cfc149f59e 100644 --- a/libraries/script-engine/src/AudioScriptingInterface.h +++ b/libraries/script-engine/src/AudioScriptingInterface.h @@ -24,6 +24,7 @@ class AudioScriptingInterface : public QObject, public Dependency { SINGLETON_DEPENDENCY public: + virtual ~AudioScriptingInterface() {} void setLocalAudioInterface(AbstractAudioInterface* audioInterface) { _localAudioInterface = audioInterface; } protected: diff --git a/scripts/system/selectAudioDevice.js b/scripts/system/selectAudioDevice.js index 9dcfb86ea5..1ecd5d23ed 100644 --- a/scripts/system/selectAudioDevice.js +++ b/scripts/system/selectAudioDevice.js @@ -17,18 +17,6 @@ const INPUT = "Input"; const OUTPUT = "Output"; -function parseMenuItem(item) { - const USE = "Use "; - const FOR_INPUT = " for " + INPUT; - const FOR_OUTPUT = " for " + OUTPUT; - if (item.slice(0, USE.length) == USE) { - if (item.slice(-FOR_INPUT.length) == FOR_INPUT) { - return { device: item.slice(USE.length, -FOR_INPUT.length), mode: INPUT }; - } else if (item.slice(-FOR_OUTPUT.length) == FOR_OUTPUT) { - return { device: item.slice(USE.length, -FOR_OUTPUT.length), mode: OUTPUT }; - } - } -} // // VAR DEFINITIONS @@ -36,13 +24,15 @@ function parseMenuItem(item) { var debugPrintStatements = true; const INPUT_DEVICE_SETTING = "audio_input_device"; const OUTPUT_DEVICE_SETTING = "audio_output_device"; -var audioDevicesList = []; +var audioDevicesList = []; // placeholder for menu items var wasHmdActive = false; // assume it's not active to start var switchedAudioInputToHMD = false; var switchedAudioOutputToHMD = false; var previousSelectedInputAudioDevice = ""; var previousSelectedOutputAudioDevice = ""; -var skipMenuEvents = true; + +var interfaceInputDevice = ""; +var interfaceOutputDevice = ""; // // BEGIN FUNCTION DEFINITIONS @@ -56,58 +46,37 @@ function debug() { function setupAudioMenus() { // menu events can be triggered asynchronously; skip them for 200ms to avoid recursion and false switches - skipMenuEvents = true; - Script.setTimeout(function() { skipMenuEvents = false; }, 200); - removeAudioMenus(); // Setup audio input devices Menu.addSeparator("Audio", "Input Audio Device"); - var inputDevices = AudioDevice.getInputDevices(); var currentInputDevice = AudioDevice.getInputDevice() - for (var i = 0; i < inputDevices.length; i++) { - var audioDeviceMenuString = "Use " + inputDevices[i] + " for Input"; + for (var i = 0; i < AudioDevice.inputAudioDevices.length; i++) { + var audioDeviceMenuString = "Use " + AudioDevice.inputAudioDevices[i] + " for Input"; Menu.addMenuItem({ menuName: "Audio", menuItemName: audioDeviceMenuString, isCheckable: true, - isChecked: inputDevices[i] == currentInputDevice + isChecked: AudioDevice.inputAudioDevices[i] == currentInputDevice }); audioDevicesList.push(audioDeviceMenuString); } // Setup audio output devices Menu.addSeparator("Audio", "Output Audio Device"); - var outputDevices = AudioDevice.getOutputDevices(); var currentOutputDevice = AudioDevice.getOutputDevice() - for (var i = 0; i < outputDevices.length; i++) { - var audioDeviceMenuString = "Use " + outputDevices[i] + " for Output"; + for (var i = 0; i < AudioDevice.outputAudioDevices.length; i++) { + var audioDeviceMenuString = "Use " + AudioDevice.outputAudioDevices[i] + " for Output"; Menu.addMenuItem({ menuName: "Audio", menuItemName: audioDeviceMenuString, isCheckable: true, - isChecked: outputDevices[i] == currentOutputDevice + isChecked: AudioDevice.outputAudioDevices[i] == currentOutputDevice }); audioDevicesList.push(audioDeviceMenuString); } } -function checkDeviceMismatch() { - var inputDeviceSetting = Settings.getValue(INPUT_DEVICE_SETTING); - var interfaceInputDevice = AudioDevice.getInputDevice(); - if (interfaceInputDevice != inputDeviceSetting) { - debug("Input Setting & Device mismatch! Input SETTING: " + inputDeviceSetting + "Input DEVICE IN USE: " + interfaceInputDevice); - switchAudioDevice("Use " + inputDeviceSetting + " for Input"); - } - - var outputDeviceSetting = Settings.getValue(OUTPUT_DEVICE_SETTING); - var interfaceOutputDevice = AudioDevice.getOutputDevice(); - if (interfaceOutputDevice != outputDeviceSetting) { - debug("Output Setting & Device mismatch! Output SETTING: " + outputDeviceSetting + "Output DEVICE IN USE: " + interfaceOutputDevice); - switchAudioDevice("Use " + outputDeviceSetting + " for Output"); - } -} - function removeAudioMenus() { Menu.removeSeparator("Audio", "Input Audio Device"); Menu.removeSeparator("Audio", "Output Audio Device"); @@ -126,81 +95,28 @@ function removeAudioMenus() { function onDevicechanged() { debug("System audio devices changed. Removing and replacing Audio Menus..."); setupAudioMenus(); - checkDeviceMismatch(); } function onMenuEvent(audioDeviceMenuString) { - if (!skipMenuEvents) { - switchAudioDevice(audioDeviceMenuString); + if (Menu.isOptionChecked(audioDeviceMenuString) && + (audioDeviceMenuString !== interfaceInputDevice && + audioDeviceMenuString !== interfaceOutputDevice)) { + AudioDevice.setDeviceFromMenu(audioDeviceMenuString) } } function onCurrentDeviceChanged() { debug("System audio device switched. "); - var interfaceInputDevice = "Use " + AudioDevice.getInputDevice() + " for Input"; - var interfaceOutputDevice = "Use " + AudioDevice.getOutputDevice() + " for Output"; + interfaceInputDevice = "Use " + AudioDevice.getInputDevice() + " for Input"; + interfaceOutputDevice = "Use " + AudioDevice.getOutputDevice() + " for Output"; for (var index = 0; index < audioDevicesList.length; index++) { if (audioDevicesList[index] === interfaceInputDevice || audioDevicesList[index] === interfaceOutputDevice) { - Menu.setIsOptionChecked(audioDevicesList[index], true); + if (Menu.isOptionChecked(audioDevicesList[index]) === false) + Menu.setIsOptionChecked(audioDevicesList[index], true); } else { - Menu.setIsOptionChecked(audioDevicesList[index], false); - } - } -} - -function switchAudioDevice(audioDeviceMenuString) { - // if the device is not plugged in, short-circuit - if (!~audioDevicesList.indexOf(audioDeviceMenuString)) { - return; - } - - var selection = parseMenuItem(audioDeviceMenuString); - if (!selection) { - debug("Invalid Audio audioDeviceMenuString! Doesn't end with 'for Input' or 'for Output'"); - return; - } - - // menu events can be triggered asynchronously; skip them for 200ms to avoid recursion and false switches - skipMenuEvents = true; - Script.setTimeout(function() { skipMenuEvents = false; }, 200); - - var selectedDevice = selection.device; - if (selection.mode == INPUT) { - var currentInputDevice = AudioDevice.getInputDevice(); - if (selectedDevice != currentInputDevice) { - debug("Switching audio INPUT device from " + currentInputDevice + " to " + selectedDevice); - Menu.setIsOptionChecked("Use " + currentInputDevice + " for Input", false); - if (AudioDevice.setInputDevice(selectedDevice)) { - Settings.setValue(INPUT_DEVICE_SETTING, selectedDevice); - Menu.setIsOptionChecked(audioDeviceMenuString, true); - } else { - debug("Error setting audio input device!") - Menu.setIsOptionChecked(audioDeviceMenuString, false); - } - } else { - debug("Selected input device is the same as the current input device!") - Settings.setValue(INPUT_DEVICE_SETTING, selectedDevice); - Menu.setIsOptionChecked(audioDeviceMenuString, true); - AudioDevice.setInputDevice(selectedDevice); // Still try to force-set the device (in case the user's trying to forcefully debug an issue) - } - } else if (selection.mode == OUTPUT) { - var currentOutputDevice = AudioDevice.getOutputDevice(); - if (selectedDevice != currentOutputDevice) { - debug("Switching audio OUTPUT device from " + currentOutputDevice + " to " + selectedDevice); - Menu.setIsOptionChecked("Use " + currentOutputDevice + " for Output", false); - if (AudioDevice.setOutputDevice(selectedDevice)) { - Settings.setValue(OUTPUT_DEVICE_SETTING, selectedDevice); - Menu.setIsOptionChecked(audioDeviceMenuString, true); - } else { - debug("Error setting audio output device!") - Menu.setIsOptionChecked(audioDeviceMenuString, false); - } - } else { - debug("Selected output device is the same as the current output device!") - Settings.setValue(OUTPUT_DEVICE_SETTING, selectedDevice); - Menu.setIsOptionChecked(audioDeviceMenuString, true); - AudioDevice.setOutputDevice(selectedDevice); // Still try to force-set the device (in case the user's trying to forcefully debug an issue) + if (Menu.isOptionChecked(audioDevicesList[index]) === true) + Menu.setIsOptionChecked(audioDevicesList[index], false); } } } @@ -208,12 +124,12 @@ function switchAudioDevice(audioDeviceMenuString) { function restoreAudio() { if (switchedAudioInputToHMD) { debug("Switching back from HMD preferred audio input to: " + previousSelectedInputAudioDevice); - switchAudioDevice("Use " + previousSelectedInputAudioDevice + " for Input"); + AudioDevice.setInputDeviceAsync(previousSelectedInputAudioDevice) switchedAudioInputToHMD = false; } if (switchedAudioOutputToHMD) { debug("Switching back from HMD preferred audio output to: " + previousSelectedOutputAudioDevice); - switchAudioDevice("Use " + previousSelectedOutputAudioDevice + " for Output"); + AudioDevice.setOutputDeviceAsync(previousSelectedOutputAudioDevice) switchedAudioOutputToHMD = false; } } @@ -240,7 +156,7 @@ function checkHMDAudio() { debug("previousSelectedInputAudioDevice: " + previousSelectedInputAudioDevice); if (hmdPreferredAudioInput != previousSelectedInputAudioDevice) { switchedAudioInputToHMD = true; - switchAudioDevice("Use " + hmdPreferredAudioInput + " for Input"); + AudioDevice.setInputDeviceAsync(hmdPreferredAudioInput) } } if (hmdPreferredAudioOutput !== "") { @@ -249,7 +165,7 @@ function checkHMDAudio() { debug("previousSelectedOutputAudioDevice: " + previousSelectedOutputAudioDevice); if (hmdPreferredAudioOutput != previousSelectedOutputAudioDevice) { switchedAudioOutputToHMD = true; - switchAudioDevice("Use " + hmdPreferredAudioOutput + " for Output"); + AudioDevice.setOutputDeviceAsync(hmdPreferredAudioOutput) } } } else { @@ -277,10 +193,9 @@ Script.setTimeout(function () { Menu.menuItemEvent.connect(onMenuEvent); debug("Setting up Audio I/O menu for the first time..."); setupAudioMenus(); - checkDeviceMismatch(); debug("Checking HMD audio status...") checkHMDAudio(); -}, 3000); +}, 300); debug("Connecting scriptEnding()"); Script.scriptEnding.connect(function () { From e4d9772bbc50094c22eec423fe35cdc64ed5eeb3 Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Thu, 11 May 2017 15:20:36 +0200 Subject: [PATCH 07/10] Fixed crashes/freezes on audio switch --- libraries/audio-client/src/AudioClient.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp index 225ef69ae3..e8f50e3281 100644 --- a/libraries/audio-client/src/AudioClient.cpp +++ b/libraries/audio-client/src/AudioClient.cpp @@ -1508,11 +1508,13 @@ bool AudioClient::switchOutputToAudioDevice(const QAudioDeviceInfo& outputDevice if (_audioOutput) { _audioOutput->stop(); - delete _audioOutput; + //must be deleted in next eventloop cycle when its called from notify() + _audioOutput->deleteLater(); _audioOutput = NULL; _loopbackOutputDevice = NULL; - delete _loopbackAudioOutput; + //must be deleted in next eventloop cycle when its called from notify() + _loopbackAudioOutput->deleteLater(); _loopbackAudioOutput = NULL; delete[] _outputMixBuffer; From 4290eb21eaf3459f615c6149a7e35cb86e04f371 Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Thu, 11 May 2017 15:41:07 +0200 Subject: [PATCH 08/10] Fixed possible crashes/freezes on input audio switch --- libraries/audio-client/src/AudioClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp index e8f50e3281..1282dbb2dc 100644 --- a/libraries/audio-client/src/AudioClient.cpp +++ b/libraries/audio-client/src/AudioClient.cpp @@ -1403,7 +1403,7 @@ bool AudioClient::switchInputToAudioDevice(const QAudioDeviceInfo& inputDeviceIn _audioInput->stop(); _inputDevice = NULL; - delete _audioInput; + _audioInput->deleteLater(); _audioInput = NULL; _numInputCallbackBytes = 0; From bd6ac7a39e3332cc5878db42fa4aac1b730edf4d Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Thu, 11 May 2017 20:38:09 +0200 Subject: [PATCH 09/10] Code style fixes --- interface/resources/qml/hifi/Audio.qml | 2 +- .../scripting/AudioDeviceScriptingInterface.cpp | 17 +++++++++-------- .../scripting/AudioDeviceScriptingInterface.h | 14 +++++++------- .../script-engine/src/AudioScriptingInterface.h | 2 +- scripts/system/selectAudioDevice.js | 4 ++-- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/interface/resources/qml/hifi/Audio.qml b/interface/resources/qml/hifi/Audio.qml index 09075e6fb5..66760ff290 100644 --- a/interface/resources/qml/hifi/Audio.qml +++ b/interface/resources/qml/hifi/Audio.qml @@ -204,7 +204,7 @@ Rectangle { delegate: Item { width: parent.width visible: devicemode === 1 - height: visible ? 36 : 0 + height: visible ? 36 : 0 AudioCheckbox { id: cbout width: parent.width diff --git a/interface/src/scripting/AudioDeviceScriptingInterface.cpp b/interface/src/scripting/AudioDeviceScriptingInterface.cpp index d76a37dbe0..05168b0d4c 100644 --- a/interface/src/scripting/AudioDeviceScriptingInterface.cpp +++ b/interface/src/scripting/AudioDeviceScriptingInterface.cpp @@ -43,7 +43,7 @@ AudioDeviceScriptingInterface::AudioDeviceScriptingInterface(): QAbstractListMod //fill up model onDeviceChanged(); //set up previously saved device - SettingsScriptingInterface *settings = SettingsScriptingInterface::getInstance(); + SettingsScriptingInterface* settings = SettingsScriptingInterface::getInstance(); const QString inDevice = settings->getValue("audio_input_device").toString(); if (inDevice != _currentInputDevice) { setInputDeviceAsync(inDevice); @@ -72,7 +72,7 @@ bool AudioDeviceScriptingInterface::setOutputDevice(const QString& deviceName) { return result; } -bool AudioDeviceScriptingInterface::setDeviceFromMenu(const QString &deviceMenuName) { +bool AudioDeviceScriptingInterface::setDeviceFromMenu(const QString& deviceMenuName) { QAudio::Mode mode; if (deviceMenuName.indexOf("for Output") != -1) { @@ -229,33 +229,34 @@ void AudioDeviceScriptingInterface::onDeviceChanged() emit deviceChanged(); } -void AudioDeviceScriptingInterface::onCurrentInputDeviceChanged(const QString &name) +void AudioDeviceScriptingInterface::onCurrentInputDeviceChanged(const QString& name) { currentDeviceUpdate(name, QAudio::AudioInput); //we got a signal that device changed. Save it now - SettingsScriptingInterface *settings = SettingsScriptingInterface::getInstance(); + SettingsScriptingInterface* settings = SettingsScriptingInterface::getInstance(); settings->setValue("audio_input_device", name); emit currentInputDeviceChanged(name); } -void AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged(const QString &name) +void AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged(const QString& name) { currentDeviceUpdate(name, QAudio::AudioOutput); //we got a signal that device changed. Save it now - SettingsScriptingInterface *settings = SettingsScriptingInterface::getInstance(); + SettingsScriptingInterface* settings = SettingsScriptingInterface::getInstance(); settings->setValue("audio_output_device", name); emit currentOutputDeviceChanged(name); } -void AudioDeviceScriptingInterface::currentDeviceUpdate(const QString &name, QAudio::Mode mode) +void AudioDeviceScriptingInterface::currentDeviceUpdate(const QString& name, QAudio::Mode mode) { QVector role; role.append(SelectedRole); for (int i = 0; i < _devices.size(); i++) { ScriptingAudioDeviceInfo di = _devices.at(i); - if (di.mode != mode) + if (di.mode != mode) { continue; + } if (di.selected && di.name != name ) { di.selected = false; _devices[i] = di; diff --git a/interface/src/scripting/AudioDeviceScriptingInterface.h b/interface/src/scripting/AudioDeviceScriptingInterface.h index 133069d6f3..f912c35288 100644 --- a/interface/src/scripting/AudioDeviceScriptingInterface.h +++ b/interface/src/scripting/AudioDeviceScriptingInterface.h @@ -52,9 +52,9 @@ public: private slots: void onDeviceChanged(); - void onCurrentInputDeviceChanged(const QString &name); - void onCurrentOutputDeviceChanged(const QString &name); - void currentDeviceUpdate(const QString &name, QAudio::Mode mode); + void onCurrentInputDeviceChanged(const QString& name); + void onCurrentOutputDeviceChanged(const QString& name); + void currentDeviceUpdate(const QString& name, QAudio::Mode mode); public slots: bool setInputDevice(const QString& deviceName); @@ -80,16 +80,16 @@ public slots: void setMuted(bool muted); - void setInputDeviceAsync(const QString &deviceName); - void setOutputDeviceAsync(const QString &deviceName); + void setInputDeviceAsync(const QString& deviceName); + void setOutputDeviceAsync(const QString& deviceName); private: AudioDeviceScriptingInterface(); signals: void muteToggled(); void deviceChanged(); - void currentInputDeviceChanged(const QString &name); - void currentOutputDeviceChanged(const QString &name); + void currentInputDeviceChanged(const QString& name); + void currentOutputDeviceChanged(const QString& name); void mutedChanged(bool muted); void inputAudioDevicesChanged(QStringList inputAudioDevices); void outputAudioDevicesChanged(QStringList outputAudioDevices); diff --git a/libraries/script-engine/src/AudioScriptingInterface.h b/libraries/script-engine/src/AudioScriptingInterface.h index cfc149f59e..5ec8ce4b12 100644 --- a/libraries/script-engine/src/AudioScriptingInterface.h +++ b/libraries/script-engine/src/AudioScriptingInterface.h @@ -24,7 +24,7 @@ class AudioScriptingInterface : public QObject, public Dependency { SINGLETON_DEPENDENCY public: - virtual ~AudioScriptingInterface() {} + virtual ~AudioScriptingInterface() {} void setLocalAudioInterface(AbstractAudioInterface* audioInterface) { _localAudioInterface = audioInterface; } protected: diff --git a/scripts/system/selectAudioDevice.js b/scripts/system/selectAudioDevice.js index 1ecd5d23ed..2d40795692 100644 --- a/scripts/system/selectAudioDevice.js +++ b/scripts/system/selectAudioDevice.js @@ -17,7 +17,7 @@ const INPUT = "Input"; const OUTPUT = "Output"; - +const SELECT_AUDIO_SCRIPT_STARTUP_TIMEOUT = 300; // // VAR DEFINITIONS // @@ -195,7 +195,7 @@ Script.setTimeout(function () { setupAudioMenus(); debug("Checking HMD audio status...") checkHMDAudio(); -}, 300); +}, SELECT_AUDIO_SCRIPT_STARTUP_TIMEOUT); debug("Connecting scriptEnding()"); Script.scriptEnding.connect(function () { From 045e93fb95e8cfdf18d7d6b13f61679bf3041761 Mon Sep 17 00:00:00 2001 From: Vladyslav Stelmakhovskyi Date: Thu, 11 May 2017 21:49:07 +0200 Subject: [PATCH 10/10] Code style fixes #2 --- libraries/audio-client/src/AudioClient.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/audio-client/src/AudioClient.h b/libraries/audio-client/src/AudioClient.h index c65cacb129..47808767b3 100644 --- a/libraries/audio-client/src/AudioClient.h +++ b/libraries/audio-client/src/AudioClient.h @@ -222,8 +222,8 @@ signals: void muteEnvironmentRequested(glm::vec3 position, float radius); - void currentOutputDeviceChanged(const QString &name); - void currentInputDeviceChanged(const QString &name); + void currentOutputDeviceChanged(const QString& name); + void currentInputDeviceChanged(const QString& name); protected: AudioClient();