diff --git a/interface/resources/qml/hifi/Audio.qml b/interface/resources/qml/hifi/Audio.qml index 66760ff290..46cec99e69 100644 --- a/interface/resources/qml/hifi/Audio.qml +++ b/interface/resources/qml/hifi/Audio.qml @@ -161,7 +161,12 @@ Rectangle { text.text: devicename onCheckBoxClicked: { if (checked) { - AudioDevice.setInputDeviceAsync(devicename) + if (devicename.length > 0) { + console.log("Audio.qml about to call AudioDevice.setInputDeviceAsync().devicename:" + devicename); + AudioDevice.setInputDeviceAsync(devicename); + } else { + console.log("Audio.qml attempted to set input device to empty device name."); + } } } } @@ -217,7 +222,13 @@ Rectangle { text.text: devicename onCheckBoxClicked: { if (checked) { - AudioDevice.setOutputDeviceAsync(devicename) + if (devicename.length > 0) { + console.log("Audio.qml about to call AudioDevice.setOutputDeviceAsync().devicename:" + devicename); + AudioDevice.setOutputDeviceAsync(devicename); + } else { + console.log("Audio.qml attempted to set output device to empty device name."); + } + } } } diff --git a/interface/src/scripting/AudioDeviceScriptingInterface.cpp b/interface/src/scripting/AudioDeviceScriptingInterface.cpp index 05168b0d4c..d22f948344 100644 --- a/interface/src/scripting/AudioDeviceScriptingInterface.cpp +++ b/interface/src/scripting/AudioDeviceScriptingInterface.cpp @@ -9,7 +9,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include "AudioClient.h" +#include +#include + #include "AudioDeviceScriptingInterface.h" #include "SettingsScriptingInterface.h" @@ -44,17 +46,23 @@ AudioDeviceScriptingInterface::AudioDeviceScriptingInterface(): QAbstractListMod onDeviceChanged(); //set up previously saved device SettingsScriptingInterface* settings = SettingsScriptingInterface::getInstance(); - const QString inDevice = settings->getValue("audio_input_device").toString(); + 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); } - const QString outDevice = settings->getValue("audio_output_device").toString(); + + // 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); } } bool AudioDeviceScriptingInterface::setInputDevice(const QString& deviceName) { + qCDebug(audioclient) << __FUNCTION__ << "deviceName:" << deviceName; + bool result; QMetaObject::invokeMethod(DependencyManager::get().data(), "switchInputToAudioDevice", Qt::BlockingQueuedConnection, @@ -64,6 +72,9 @@ bool AudioDeviceScriptingInterface::setInputDevice(const QString& deviceName) { } bool AudioDeviceScriptingInterface::setOutputDevice(const QString& deviceName) { + + qCDebug(audioclient) << __FUNCTION__ << "deviceName:" << deviceName; + bool result; QMetaObject::invokeMethod(DependencyManager::get().data(), "switchOutputToAudioDevice", Qt::BlockingQueuedConnection, @@ -86,8 +97,10 @@ bool AudioDeviceScriptingInterface::setDeviceFromMenu(const QString& deviceMenuN for (ScriptingAudioDeviceInfo di: _devices) { if (mode == di.mode && deviceMenuName.contains(di.name)) { if (mode == QAudio::AudioOutput) { + qCDebug(audioclient) << __FUNCTION__ << "about to call setOutputDeviceAsync() device: [" << di.name << "]"; setOutputDeviceAsync(di.name); } else { + qCDebug(audioclient) << __FUNCTION__ << "about to call setInputDeviceAsync() device: [" << di.name << "]"; setInputDeviceAsync(di.name); } return true; @@ -98,12 +111,26 @@ bool AudioDeviceScriptingInterface::setDeviceFromMenu(const QString& deviceMenuN } void AudioDeviceScriptingInterface::setInputDeviceAsync(const QString& deviceName) { + qCDebug(audioclient) << __FUNCTION__ << "deviceName:" << deviceName; + + if (deviceName.isEmpty()) { + qCDebug(audioclient) << __FUNCTION__ << "attempt to set empty deviceName:" << deviceName << "... ignoring!"; + return; + } + QMetaObject::invokeMethod(DependencyManager::get().data(), "switchInputToAudioDevice", Qt::QueuedConnection, Q_ARG(const QString&, deviceName)); } void AudioDeviceScriptingInterface::setOutputDeviceAsync(const QString& deviceName) { + qCDebug(audioclient) << __FUNCTION__ << "deviceName:" << deviceName; + + if (deviceName.isEmpty()) { + qCDebug(audioclient) << __FUNCTION__ << "attempt to set empty deviceName:" << deviceName << "... ignoring!"; + return; + } + QMetaObject::invokeMethod(DependencyManager::get().data(), "switchOutputToAudioDevice", Qt::QueuedConnection, Q_ARG(const QString&, deviceName)); @@ -241,8 +268,11 @@ void AudioDeviceScriptingInterface::onCurrentInputDeviceChanged(const QString& n void AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged(const QString& name) { currentDeviceUpdate(name, QAudio::AudioOutput); + + // FIXME - this is kinda janky to set the setting on the result of a signal //we got a signal that device changed. Save it now SettingsScriptingInterface* settings = SettingsScriptingInterface::getInstance(); + qCDebug(audioclient) << __FUNCTION__ << "about to call settings->setValue('audio_output_device', name); name:" << name; settings->setValue("audio_output_device", name); emit currentOutputDeviceChanged(name); } diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp index e03ca83131..f0363cd49c 100644 --- a/libraries/audio-client/src/AudioClient.cpp +++ b/libraries/audio-client/src/AudioClient.cpp @@ -216,7 +216,10 @@ AudioClient::AudioClient() : connect(&_receivedAudioStream, &MixedProcessedAudioStream::processSamples, this, &AudioClient::processReceivedSamples, Qt::DirectConnection); - connect(this, &AudioClient::changeDevice, this, [=](const QAudioDeviceInfo& outputDeviceInfo) { switchOutputToAudioDevice(outputDeviceInfo); }); + connect(this, &AudioClient::changeDevice, this, [=](const QAudioDeviceInfo& outputDeviceInfo) { + qCDebug(audioclient) << "got AudioClient::changeDevice signal, about to call switchOutputToAudioDevice() outputDeviceInfo: [" << outputDeviceInfo.deviceName() << "]"; + switchOutputToAudioDevice(outputDeviceInfo); + }); connect(&_receivedAudioStream, &InboundAudioStream::mismatchedAudioCodec, this, &AudioClient::handleMismatchAudioFormat); @@ -438,7 +441,8 @@ QAudioDeviceInfo defaultAudioDeviceForMode(QAudio::Mode mode) { CoUninitialize(); } - qCDebug(audioclient) << "[" << deviceName << "] [" << getNamedAudioDeviceForMode(mode, deviceName).deviceName() << "]"; + qCDebug(audioclient) << "defaultAudioDeviceForMode mode: " << (mode == QAudio::AudioOutput ? "Output" : "Input") + << " [" << deviceName << "] [" << getNamedAudioDeviceForMode(mode, deviceName).deviceName() << "]"; return getNamedAudioDeviceForMode(mode, deviceName); #endif @@ -614,8 +618,12 @@ void AudioClient::start() { } void AudioClient::stop() { + // "switch" to invalid devices in order to shut down the state + qCDebug(audioclient) << "AudioClient::stop(), about to call switchInputToAudioDevice(null)"; switchInputToAudioDevice(QAudioDeviceInfo()); + + qCDebug(audioclient) << "AudioClient::stop(), about to call switchOutputToAudioDevice(null)"; switchOutputToAudioDevice(QAudioDeviceInfo()); } @@ -807,12 +815,12 @@ QVector AudioClient::getDeviceNames(QAudio::Mode mode) { } bool AudioClient::switchInputToAudioDevice(const QString& inputDeviceName) { - qCDebug(audioclient) << "[" << inputDeviceName << "] [" << getNamedAudioDeviceForMode(QAudio::AudioInput, inputDeviceName).deviceName() << "]"; + qCDebug(audioclient) << "switchInputToAudioDevice [" << inputDeviceName << "] [" << getNamedAudioDeviceForMode(QAudio::AudioInput, inputDeviceName).deviceName() << "]"; return switchInputToAudioDevice(getNamedAudioDeviceForMode(QAudio::AudioInput, inputDeviceName)); } bool AudioClient::switchOutputToAudioDevice(const QString& outputDeviceName) { - qCDebug(audioclient) << "[" << outputDeviceName << "] [" << getNamedAudioDeviceForMode(QAudio::AudioOutput, outputDeviceName).deviceName() << "]"; + qCDebug(audioclient) << "switchOutputToAudioDevice [" << outputDeviceName << "] [" << getNamedAudioDeviceForMode(QAudio::AudioOutput, outputDeviceName).deviceName() << "]"; return switchOutputToAudioDevice(getNamedAudioDeviceForMode(QAudio::AudioOutput, outputDeviceName)); } @@ -1357,6 +1365,7 @@ void AudioClient::setIsStereoInput(bool isStereoInput) { } // change in channel count for desired input format, restart the input device + qCDebug(audioclient) << __FUNCTION__ << "about to call switchInputToAudioDevice:" << _inputAudioDeviceName; switchInputToAudioDevice(_inputAudioDeviceName); } } @@ -1395,6 +1404,7 @@ void AudioClient::outputFormatChanged() { } bool AudioClient::switchInputToAudioDevice(const QAudioDeviceInfo& inputDeviceInfo) { + qCDebug(audioclient) << __FUNCTION__ << "inputDeviceInfo: [" << inputDeviceInfo.deviceName() << "]"; bool supportedFormat = false; // cleanup any previously initialized device @@ -1509,6 +1519,8 @@ void AudioClient::outputNotify() { } bool AudioClient::switchOutputToAudioDevice(const QAudioDeviceInfo& outputDeviceInfo) { + qCDebug(audioclient) << "AudioClient::switchOutputToAudioDevice() outputDeviceInfo: [" << outputDeviceInfo.deviceName() << "]"; + bool supportedFormat = false; Lock localAudioLock(_localAudioMutex); @@ -1643,7 +1655,11 @@ bool AudioClient::switchOutputToAudioDevice(const QAudioDeviceInfo& outputDevice } int AudioClient::setOutputBufferSize(int numFrames, bool persist) { + qCDebug(audioclient) << __FUNCTION__ << "numFrames:" << numFrames << "persist:" << persist; + numFrames = std::min(std::max(numFrames, MIN_BUFFER_FRAMES), MAX_BUFFER_FRAMES); + qCDebug(audioclient) << __FUNCTION__ << "clamped numFrames:" << numFrames << "_sessionOutputBufferSizeFrames:" << _sessionOutputBufferSizeFrames; + if (numFrames != _sessionOutputBufferSizeFrames) { qCInfo(audioclient, "Audio output buffer set to %d frames", numFrames); _sessionOutputBufferSizeFrames = numFrames; @@ -1655,6 +1671,7 @@ int AudioClient::setOutputBufferSize(int numFrames, bool persist) { // The buffer size can't be adjusted after QAudioOutput::start() has been called, so // recreate the device by switching to the default. QAudioDeviceInfo outputDeviceInfo = defaultAudioDeviceForMode(QAudio::AudioOutput); + qCDebug(audioclient) << __FUNCTION__ << "about to send changeDevice signal outputDeviceInfo: [" << outputDeviceInfo.deviceName() << "]"; emit changeDevice(outputDeviceInfo); // On correct thread, please, as setOutputBufferSize can be called from main thread. } }