| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-18 09:58:55 +01:00
										 |  |  | // Copyright (C) 2015-2020, 2022-2023 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
 | 
					
						
							|  |  |  | // Copyright (C) 2018 beta-tester <alpha-beta-release@gmx.net>                   //
 | 
					
						
							|  |  |  | // Copyright (C) 2023 Jon Beniston, M7RCE <jon@beniston.com>                     //
 | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify          //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by          //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                  //
 | 
					
						
							|  |  |  | // (at your option) any later version.                                           //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,               //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | #include <QNetworkReply>
 | 
					
						
							|  |  |  | #include <QBuffer>
 | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  | #include <QThread>
 | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "SWGDeviceSettings.h"
 | 
					
						
							|  |  |  | #include "SWGDeviceState.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "device/deviceapi.h"
 | 
					
						
							|  |  |  | #include "audio/audiodevicemanager.h"
 | 
					
						
							|  |  |  | #include "dsp/dspcommands.h"
 | 
					
						
							|  |  |  | #include "dsp/dspengine.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "audiooutputworker.h"
 | 
					
						
							|  |  |  | #include "audiooutput.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(AudioOutput::MsgConfigureAudioOutput, Message) | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(AudioOutput::MsgStartStop, Message) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AudioOutput::AudioOutput(DeviceAPI *deviceAPI) : | 
					
						
							|  |  |  |     m_deviceAPI(deviceAPI), | 
					
						
							|  |  |  |     m_audioFifo(48000), | 
					
						
							|  |  |  | 	m_settings(), | 
					
						
							|  |  |  |     m_audioDeviceIndex(-1), | 
					
						
							|  |  |  |     m_centerFrequency(0), | 
					
						
							|  |  |  | 	m_worker(nullptr), | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |     m_workerThread(nullptr), | 
					
						
							| 
									
										
										
										
											2023-01-27 08:40:12 +00:00
										 |  |  |     m_deviceDescription("AudioOutput"), | 
					
						
							|  |  |  |     m_running(false) | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_deviceAPI->setNbSinkStreams(1); | 
					
						
							|  |  |  |     AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager(); | 
					
						
							|  |  |  |     m_sampleRate = audioDeviceManager->getOutputSampleRate(m_audioDeviceIndex); | 
					
						
							|  |  |  |     m_settings.m_deviceName = AudioDeviceManager::m_defaultDeviceName; | 
					
						
							|  |  |  |     m_sampleSourceFifo.resize(SampleSourceFifo::getSizePolicy(48000)); | 
					
						
							| 
									
										
										
										
											2023-05-18 10:20:27 +02:00
										 |  |  |     m_networkManager = new QNetworkAccessManager(); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AudioOutput::~AudioOutput() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-05-26 22:02:18 +02:00
										 |  |  | 	delete m_networkManager; | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | 	stop(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioOutput::destroy() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     delete this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioOutput::init() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |     applySettings(m_settings, QList<QString>(), true); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool AudioOutput::start() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_running) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | 	qDebug("AudioOutput::start"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager(); | 
					
						
							|  |  |  |     audioDeviceManager->addAudioSink(&m_audioFifo, getInputMessageQueue(), m_audioDeviceIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |     m_workerThread = new QThread(); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | 	m_worker = new AudioOutputWorker(&m_sampleSourceFifo, &m_audioFifo); | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |     m_worker->moveToThread(m_workerThread); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QObject::connect(m_workerThread, &QThread::started, m_worker, &AudioOutputWorker::startWork); | 
					
						
							|  |  |  |     QObject::connect(m_workerThread, &QThread::finished, m_worker, &QObject::deleteLater); | 
					
						
							|  |  |  |     QObject::connect(m_workerThread, &QThread::finished, m_workerThread, &QThread::deleteLater); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     m_worker->setSamplerate(m_sampleRate); | 
					
						
							|  |  |  |     m_worker->setIQMapping(m_settings.m_iqMapping); | 
					
						
							|  |  |  |     m_worker->connectTimer(m_deviceAPI->getMasterTimer()); | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |     m_workerThread->start(); | 
					
						
							|  |  |  |     m_running = true; | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	qDebug("AudioOutput::start: started"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioOutput::stop() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |     if (!m_running) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     qDebug("AudioOutput::stop"); | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |     m_running = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_workerThread) | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         m_worker->stopWork(); | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |         m_workerThread->quit(); | 
					
						
							|  |  |  |         m_workerThread->wait(); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |         m_worker = nullptr; | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |         m_workerThread = nullptr; | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager(); | 
					
						
							|  |  |  |     audioDeviceManager->removeAudioSink(&m_audioFifo); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     qDebug("AudioOutput::stop: stopped"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QByteArray AudioOutput::serialize() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_settings.serialize(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool AudioOutput::deserialize(const QByteArray& data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     bool success = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!m_settings.deserialize(data)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_settings.resetToDefaults(); | 
					
						
							|  |  |  |         success = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |     MsgConfigureAudioOutput* message = MsgConfigureAudioOutput::create(m_settings, QList<QString>(), true); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     m_inputMessageQueue.push(message); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_guiMessageQueue) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |         MsgConfigureAudioOutput* messageToGUI = MsgConfigureAudioOutput::create(m_settings, QList<QString>(), true); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |         m_guiMessageQueue->push(messageToGUI); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return success; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const QString& AudioOutput::getDeviceDescription() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_deviceDescription; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int AudioOutput::getSampleRate() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_sampleRate; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | quint64 AudioOutput::getCenterFrequency() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_centerFrequency; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool AudioOutput::handleMessage(const Message& message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if(MsgConfigureAudioOutput::match(message)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         qDebug() << "AudioOutput::handleMessage: MsgConfigureAudioOutput"; | 
					
						
							|  |  |  |         MsgConfigureAudioOutput& conf = (MsgConfigureAudioOutput&) message; | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |         applySettings(conf.getSettings(), conf.getSettingsKeys(), conf.getForce()); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (MsgStartStop::match(message)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         MsgStartStop& cmd = (MsgStartStop&) message; | 
					
						
							|  |  |  |         qDebug() << "AudioOutput::handleMessage: MsgStartStop: " << (cmd.getStartStop() ? "start" : "stop"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (cmd.getStartStop()) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (m_deviceAPI->initDeviceEngine()) { | 
					
						
							|  |  |  |                 m_deviceAPI->startDeviceEngine(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             m_deviceAPI->stopDeviceEngine(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (m_settings.m_useReverseAPI) { | 
					
						
							|  |  |  |             webapiReverseSendStartStop(cmd.getStartStop()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  | void AudioOutput::applySettings(const AudioOutputSettings& settings, const QList<QString>& settingsKeys, bool force) | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |     qDebug() << "AudioOutput::applySettings: force:" << force << settings.getDebugString(settingsKeys, force); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     bool forwardChange = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |     if (settingsKeys.contains("deviceName") || force) | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager(); | 
					
						
							|  |  |  |         m_audioDeviceIndex = audioDeviceManager->getOutputDeviceIndex(settings.m_deviceName); | 
					
						
							|  |  |  |         //qDebug("AMDemod::applySettings: audioDeviceName: %s audioDeviceIndex: %d", qPrintable(settings.m_audioDeviceName), audioDeviceIndex);
 | 
					
						
							|  |  |  |         audioDeviceManager->removeAudioSink(&m_audioFifo); | 
					
						
							|  |  |  |         audioDeviceManager->addAudioSink(&m_audioFifo, getInputMessageQueue(), m_audioDeviceIndex); | 
					
						
							|  |  |  |         m_sampleRate = audioDeviceManager->getOutputSampleRate(m_audioDeviceIndex); | 
					
						
							|  |  |  |         forwardChange = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |     if (settingsKeys.contains("volume") || force) | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         m_audioOutputDevice.setVolume(settings.m_volume); | 
					
						
							|  |  |  |         qDebug() << "AudioOutput::applySettings: set volume to " << settings.m_volume; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |     if (settingsKeys.contains("iqMapping") || force) | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         forwardChange = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |         if (m_running) { | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |             m_worker->setIQMapping(settings.m_iqMapping); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-14 18:58:12 +02:00
										 |  |  |     if (settings.m_useReverseAPI) | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |         bool fullUpdate = (settingsKeys.contains("useReverseAPI") && settings.m_useReverseAPI) || | 
					
						
							|  |  |  |             settingsKeys.contains("reverseAPIAddress") || | 
					
						
							|  |  |  |             settingsKeys.contains("reverseAPIPort") || | 
					
						
							|  |  |  |             settingsKeys.contains("reverseAPIDeviceIndex"); | 
					
						
							|  |  |  |         webapiReverseSendSettings(settingsKeys, settings, fullUpdate || force); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |     if (force) { | 
					
						
							|  |  |  |         m_settings = settings; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         m_settings.applySettings(settingsKeys, settings); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (forwardChange) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-09 01:50:27 +02:00
										 |  |  |         if (m_running) { | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |             m_worker->setSamplerate(m_sampleRate); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         DSPSignalNotification *notif = new DSPSignalNotification(m_sampleRate, 0); | 
					
						
							|  |  |  |         m_centerFrequency = 0; | 
					
						
							|  |  |  |         m_deviceAPI->getDeviceEngineInputMessageQueue()->push(notif); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int AudioOutput::webapiRunGet( | 
					
						
							|  |  |  |         SWGSDRangel::SWGDeviceState& response, | 
					
						
							|  |  |  |         QString& errorMessage) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     (void) errorMessage; | 
					
						
							|  |  |  |     m_deviceAPI->getDeviceEngineStateStr(*response.getState()); | 
					
						
							|  |  |  |     return 200; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int AudioOutput::webapiRun( | 
					
						
							|  |  |  |         bool run, | 
					
						
							|  |  |  |         SWGSDRangel::SWGDeviceState& response, | 
					
						
							|  |  |  |         QString& errorMessage) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     (void) errorMessage; | 
					
						
							|  |  |  |     m_deviceAPI->getDeviceEngineStateStr(*response.getState()); | 
					
						
							|  |  |  |     MsgStartStop *message = MsgStartStop::create(run); | 
					
						
							|  |  |  |     m_inputMessageQueue.push(message); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_guiMessageQueue) // forward to GUI if any
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         MsgStartStop *msgToGUI = MsgStartStop::create(run); | 
					
						
							|  |  |  |         m_guiMessageQueue->push(msgToGUI); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 200; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int AudioOutput::webapiSettingsGet( | 
					
						
							|  |  |  |         SWGSDRangel::SWGDeviceSettings& response, | 
					
						
							|  |  |  |         QString& errorMessage) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     (void) errorMessage; | 
					
						
							|  |  |  |     response.setAudioOutputSettings(new SWGSDRangel::SWGAudioOutputSettings()); | 
					
						
							|  |  |  |     response.getAudioOutputSettings()->init(); | 
					
						
							|  |  |  |     webapiFormatDeviceSettings(response, m_settings); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 200; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int AudioOutput::webapiSettingsPutPatch( | 
					
						
							|  |  |  |                 bool force, | 
					
						
							|  |  |  |                 const QStringList& deviceSettingsKeys, | 
					
						
							|  |  |  |                 SWGSDRangel::SWGDeviceSettings& response, // query + response
 | 
					
						
							|  |  |  |                 QString& errorMessage) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     (void) errorMessage; | 
					
						
							|  |  |  |     AudioOutputSettings settings = m_settings; | 
					
						
							|  |  |  |     webapiUpdateDeviceSettings(settings, deviceSettingsKeys, response); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |     MsgConfigureAudioOutput *msg = MsgConfigureAudioOutput::create(settings, deviceSettingsKeys, force); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |     m_inputMessageQueue.push(msg); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_guiMessageQueue) // forward to GUI if any
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  |         MsgConfigureAudioOutput *msgToGUI = MsgConfigureAudioOutput::create(settings, deviceSettingsKeys, force); | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  |         m_guiMessageQueue->push(msgToGUI); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     webapiFormatDeviceSettings(response, settings); | 
					
						
							|  |  |  |     return 200; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioOutput::webapiUpdateDeviceSettings( | 
					
						
							|  |  |  |         AudioOutputSettings& settings, | 
					
						
							|  |  |  |         const QStringList& deviceSettingsKeys, | 
					
						
							|  |  |  |         SWGSDRangel::SWGDeviceSettings& response) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("deviceName")) { | 
					
						
							|  |  |  |         settings.m_deviceName = *response.getAudioOutputSettings()->getDeviceName(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("volume")) { | 
					
						
							|  |  |  |         settings.m_volume = response.getAudioOutputSettings()->getVolume(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("iqMapping")) { | 
					
						
							|  |  |  |         settings.m_iqMapping = (AudioOutputSettings::IQMapping) response.getAudioOutputSettings()->getIqMapping(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("useReverseAPI")) { | 
					
						
							|  |  |  |         settings.m_useReverseAPI = response.getAudioOutputSettings()->getUseReverseApi() != 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("reverseAPIAddress")) { | 
					
						
							|  |  |  |         settings.m_reverseAPIAddress = *response.getAudioOutputSettings()->getReverseApiAddress(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("reverseAPIPort")) { | 
					
						
							|  |  |  |         settings.m_reverseAPIPort = response.getAudioOutputSettings()->getReverseApiPort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("reverseAPIDeviceIndex")) { | 
					
						
							|  |  |  |         settings.m_reverseAPIDeviceIndex = response.getAudioOutputSettings()->getReverseApiDeviceIndex(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioOutput::webapiFormatDeviceSettings(SWGSDRangel::SWGDeviceSettings& response, const AudioOutputSettings& settings) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     response.getAudioOutputSettings()->setDeviceName(new QString(settings.m_deviceName)); | 
					
						
							|  |  |  |     response.getAudioOutputSettings()->setVolume(settings.m_volume); | 
					
						
							|  |  |  |     response.getAudioOutputSettings()->setIqMapping((int) settings.m_iqMapping); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response.getAudioOutputSettings()->setUseReverseApi(settings.m_useReverseAPI ? 1 : 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (response.getAudioOutputSettings()->getReverseApiAddress()) { | 
					
						
							|  |  |  |         *response.getAudioOutputSettings()->getReverseApiAddress() = settings.m_reverseAPIAddress; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         response.getAudioOutputSettings()->setReverseApiAddress(new QString(settings.m_reverseAPIAddress)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response.getAudioOutputSettings()->setReverseApiPort(settings.m_reverseAPIPort); | 
					
						
							|  |  |  |     response.getAudioOutputSettings()->setReverseApiDeviceIndex(settings.m_reverseAPIDeviceIndex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 23:25:44 +02:00
										 |  |  | void AudioOutput::webapiReverseSendSettings(const QList<QString>& deviceSettingsKeys, const AudioOutputSettings& settings, bool force) | 
					
						
							| 
									
										
										
										
											2020-11-21 11:15:06 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings(); | 
					
						
							|  |  |  |     swgDeviceSettings->setDirection(1); // single Tx
 | 
					
						
							|  |  |  |     swgDeviceSettings->setOriginatorIndex(m_deviceAPI->getDeviceSetIndex()); | 
					
						
							|  |  |  |     swgDeviceSettings->setDeviceHwType(new QString("AudioOutput")); | 
					
						
							|  |  |  |     swgDeviceSettings->setAudioOutputSettings(new SWGSDRangel::SWGAudioOutputSettings()); | 
					
						
							|  |  |  |     SWGSDRangel::SWGAudioOutputSettings *swgAudioOutputSettings = swgDeviceSettings->getAudioOutputSettings(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // transfer data that has been modified. When force is on transfer all data except reverse API data
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("deviceName") || force) { | 
					
						
							|  |  |  |         swgAudioOutputSettings->setDeviceName(new QString(settings.m_deviceName)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("volume") || force) { | 
					
						
							|  |  |  |         swgAudioOutputSettings->setVolume(settings.m_volume); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (deviceSettingsKeys.contains("iqMapping") || force) { | 
					
						
							|  |  |  |         swgAudioOutputSettings->setIqMapping(settings.m_iqMapping); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString deviceSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/device/settings") | 
					
						
							|  |  |  |             .arg(settings.m_reverseAPIAddress) | 
					
						
							|  |  |  |             .arg(settings.m_reverseAPIPort) | 
					
						
							|  |  |  |             .arg(settings.m_reverseAPIDeviceIndex); | 
					
						
							|  |  |  |     m_networkRequest.setUrl(QUrl(deviceSettingsURL)); | 
					
						
							|  |  |  |     m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QBuffer *buffer = new QBuffer(); | 
					
						
							|  |  |  |     buffer->open((QBuffer::ReadWrite)); | 
					
						
							|  |  |  |     buffer->write(swgDeviceSettings->asJson().toUtf8()); | 
					
						
							|  |  |  |     buffer->seek(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Always use PATCH to avoid passing reverse API settings
 | 
					
						
							|  |  |  |     QNetworkReply *reply = m_networkManager->sendCustomRequest(m_networkRequest, "PATCH", buffer); | 
					
						
							|  |  |  |     buffer->setParent(reply); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     delete swgDeviceSettings; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioOutput::webapiReverseSendStartStop(bool start) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings(); | 
					
						
							|  |  |  |     swgDeviceSettings->setDirection(1); // single Tx
 | 
					
						
							|  |  |  |     swgDeviceSettings->setOriginatorIndex(m_deviceAPI->getDeviceSetIndex()); | 
					
						
							|  |  |  |     swgDeviceSettings->setDeviceHwType(new QString("AudioOutput")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString deviceSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/device/run") | 
					
						
							|  |  |  |             .arg(m_settings.m_reverseAPIAddress) | 
					
						
							|  |  |  |             .arg(m_settings.m_reverseAPIPort) | 
					
						
							|  |  |  |             .arg(m_settings.m_reverseAPIDeviceIndex); | 
					
						
							|  |  |  |     m_networkRequest.setUrl(QUrl(deviceSettingsURL)); | 
					
						
							|  |  |  |     m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QBuffer *buffer = new QBuffer(); | 
					
						
							|  |  |  |     buffer->open((QBuffer::ReadWrite)); | 
					
						
							|  |  |  |     buffer->write(swgDeviceSettings->asJson().toUtf8()); | 
					
						
							|  |  |  |     buffer->seek(0); | 
					
						
							|  |  |  |     QNetworkReply *reply; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (start) { | 
					
						
							|  |  |  |         reply = m_networkManager->sendCustomRequest(m_networkRequest, "POST", buffer); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         reply = m_networkManager->sendCustomRequest(m_networkRequest, "DELETE", buffer); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buffer->setParent(reply); | 
					
						
							|  |  |  |     delete swgDeviceSettings; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioOutput::networkManagerFinished(QNetworkReply *reply) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QNetworkReply::NetworkError replyError = reply->error(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (replyError) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         qWarning() << "AudioOutput::networkManagerFinished:" | 
					
						
							|  |  |  |                 << " error(" << (int) replyError | 
					
						
							|  |  |  |                 << "): " << replyError | 
					
						
							|  |  |  |                 << ": " << reply->errorString(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QString answer = reply->readAll(); | 
					
						
							|  |  |  |         answer.chop(1); // remove last \n
 | 
					
						
							|  |  |  |         qDebug("AudioOutput::networkManagerFinished: reply:\n%s", answer.toStdString().c_str()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     reply->deleteLater(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |