From d352163d77515ccc1bcde9c92a35147172d06c1d Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Jun 2017 09:24:14 -0700 Subject: [PATCH] fix no audio device from previously removed audio device --- .../AudioDeviceScriptingInterface.cpp | 24 +++++++++++++++---- libraries/audio-client/src/AudioClient.cpp | 5 ++++ libraries/audio-client/src/AudioClient.h | 2 ++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/interface/src/scripting/AudioDeviceScriptingInterface.cpp b/interface/src/scripting/AudioDeviceScriptingInterface.cpp index d22f948344..1fd30af0e3 100644 --- a/interface/src/scripting/AudioDeviceScriptingInterface.cpp +++ b/interface/src/scripting/AudioDeviceScriptingInterface.cpp @@ -48,15 +48,31 @@ AudioDeviceScriptingInterface::AudioDeviceScriptingInterface(): QAbstractListMod SettingsScriptingInterface* settings = SettingsScriptingInterface::getInstance(); const QString inDevice = settings->getValue("audio_input_device", _currentInputDevice).toString(); if (inDevice != _currentInputDevice) { - qCDebug(audioclient) << __FUNCTION__ << "about to call setInputDeviceAsync() device: [" << inDevice << "] _currentInputDevice:" << _currentInputDevice; - setInputDeviceAsync(inDevice); + // before using the old setting, check to make sure the device still exists.... + bool inDeviceExists = DependencyManager::get()->getNamedAudioDeviceForModeExists(QAudio::AudioInput, inDevice); + + if (inDeviceExists) { + qCDebug(audioclient) << __FUNCTION__ << "about to call setInputDeviceAsync() device: [" << inDevice << "] _currentInputDevice:" << _currentInputDevice; + setInputDeviceAsync(inDevice); + } else { + qCDebug(audioclient) << __FUNCTION__ << "previously selected device no longer exists inDevice: [" << inDevice << "] keeping device _currentInputDevice:" << _currentInputDevice; + } } // If the audio_output_device setting is not available, use the _currentOutputDevice auto outDevice = settings->getValue("audio_output_device", _currentOutputDevice).toString(); + if (outDevice != _currentOutputDevice) { - qCDebug(audioclient) << __FUNCTION__ << "about to call setOutputDeviceAsync() outDevice: [" << outDevice << "] _currentOutputDevice:" << _currentOutputDevice; - setOutputDeviceAsync(outDevice); + // before using the old setting, check to make sure the device still exists.... + bool outDeviceExists = DependencyManager::get()->getNamedAudioDeviceForModeExists(QAudio::AudioOutput, outDevice); + + if (outDeviceExists) { + qCDebug(audioclient) << __FUNCTION__ << "about to call setOutputDeviceAsync() outDevice: [" << outDevice << "] _currentOutputDevice:" << _currentOutputDevice; + setOutputDeviceAsync(outDevice); + } else { + qCDebug(audioclient) << __FUNCTION__ << "previously selected device no longer exists outDevice: [" << outDevice << "] keeping device _currentOutputDevice:" << _currentOutputDevice; + } + } } diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp index 0bc72ae689..5297426840 100644 --- a/libraries/audio-client/src/AudioClient.cpp +++ b/libraries/audio-client/src/AudioClient.cpp @@ -395,6 +395,11 @@ QAudioDeviceInfo defaultAudioDeviceForMode(QAudio::Mode mode) { return (mode == QAudio::AudioInput) ? QAudioDeviceInfo::defaultInputDevice() : QAudioDeviceInfo::defaultOutputDevice(); } +bool AudioClient::getNamedAudioDeviceForModeExists(QAudio::Mode mode, const QString& deviceName) { + return (getNamedAudioDeviceForMode(mode, deviceName).deviceName() == deviceName); +} + + // attempt to use the native sample rate and channel count bool nativeFormatForAudioDevice(const QAudioDeviceInfo& audioDevice, QAudioFormat& audioFormat) { diff --git a/libraries/audio-client/src/AudioClient.h b/libraries/audio-client/src/AudioClient.h index bec2fd2cc9..f3e7e418b2 100644 --- a/libraries/audio-client/src/AudioClient.h +++ b/libraries/audio-client/src/AudioClient.h @@ -149,6 +149,8 @@ public: static const float CALLBACK_ACCELERATOR_RATIO; + bool getNamedAudioDeviceForModeExists(QAudio::Mode mode, const QString& deviceName); + #ifdef Q_OS_WIN static QString friendlyNameForAudioDevice(wchar_t* guid); #endif