From 6e3add76e3f78e83c6e59a5327ef248c3bb0f183 Mon Sep 17 00:00:00 2001 From: f4exb Date: Fri, 13 Nov 2020 16:22:29 +0100 Subject: [PATCH] Audio Input: implemented worker + thread --- .../samplesource/audioinput/CMakeLists.txt | 4 +- .../samplesource/audioinput/audioinput.cpp | 41 +++++++++-------- plugins/samplesource/audioinput/audioinput.h | 6 ++- ...ioinputthread.cpp => audioinputworker.cpp} | 45 ++++--------------- ...{audioinputthread.h => audioinputworker.h} | 19 +++----- 5 files changed, 45 insertions(+), 70 deletions(-) rename plugins/samplesource/audioinput/{audioinputthread.cpp => audioinputworker.cpp} (81%) rename plugins/samplesource/audioinput/{audioinputthread.h => audioinputworker.h} (85%) diff --git a/plugins/samplesource/audioinput/CMakeLists.txt b/plugins/samplesource/audioinput/CMakeLists.txt index 13233fdfc..924f4eced 100644 --- a/plugins/samplesource/audioinput/CMakeLists.txt +++ b/plugins/samplesource/audioinput/CMakeLists.txt @@ -5,7 +5,7 @@ set(audioinput_SOURCES audioinputplugin.cpp audioinputsettings.cpp audioinputwebapiadapter.cpp - audioinputthread.cpp + audioinputworker.cpp ) set(audioinput_HEADERS @@ -13,7 +13,7 @@ set(audioinput_HEADERS audioinputplugin.h audioinputsettings.h audioinputwebapiadapter.h - audioinputthread.h + audioinputworker.h ) include_directories( diff --git a/plugins/samplesource/audioinput/audioinput.cpp b/plugins/samplesource/audioinput/audioinput.cpp index 0ee6efee4..4234cdd51 100644 --- a/plugins/samplesource/audioinput/audioinput.cpp +++ b/plugins/samplesource/audioinput/audioinput.cpp @@ -31,7 +31,7 @@ #include "device/deviceapi.h" #include "audioinput.h" -#include "audioinputthread.h" +#include "audioinputworker.h" MESSAGE_CLASS_DEFINITION(AudioInput::MsgConfigureAudioInput, Message) MESSAGE_CLASS_DEFINITION(AudioInput::MsgStartStop, Message) @@ -39,11 +39,10 @@ MESSAGE_CLASS_DEFINITION(AudioInput::MsgStartStop, Message) AudioInput::AudioInput(DeviceAPI *deviceAPI) : m_deviceAPI(deviceAPI), m_settings(), - m_thread(nullptr), m_deviceDescription("AudioInput"), m_running(false) { - m_fifo.setSize(20*AudioInputThread::m_convBufSamples); + m_fifo.setSize(20*AudioInputWorker::m_convBufSamples); openDevice(); m_deviceAPI->setNbSourceStreams(1); m_networkManager = new QNetworkAccessManager(); @@ -106,7 +105,9 @@ bool AudioInput::start() { qDebug() << "AudioInput::start"; - if (m_running) stop(); + if (m_running) { + stop(); + } if(!m_sampleFifo.setSize(96000*4)) { @@ -116,11 +117,12 @@ bool AudioInput::start() applySettings(m_settings, true, true); - m_thread = new AudioInputThread(&m_sampleFifo, &m_fifo); - m_thread->setLog2Decimation(m_settings.m_log2Decim); - m_thread->setIQMapping(m_settings.m_iqMapping); - m_thread->startWork(); - + m_worker = new AudioInputWorker(&m_sampleFifo, &m_fifo); + m_worker->moveToThread(&m_workerThread); + m_worker->setLog2Decimation(m_settings.m_log2Decim); + m_worker->setIQMapping(m_settings.m_iqMapping); + m_worker->startWork(); + m_workerThread.start(); qDebug("AudioInput::started"); m_running = true; @@ -136,12 +138,13 @@ void AudioInput::closeDevice() void AudioInput::stop() { - if (m_thread) + if (m_worker) { - m_thread->stopWork(); - // wait for thread to quit ? - delete m_thread; - m_thread = nullptr; + m_worker->stopWork(); + m_workerThread.quit(); + m_workerThread.wait(); + delete m_worker; + m_worker = nullptr; } m_running = false; @@ -274,9 +277,9 @@ void AudioInput::applySettings(const AudioInputSettings& settings, bool force, b reverseAPIKeys.append("log2Decim"); forwardChange = true; - if (m_thread) + if (m_worker) { - m_thread->setLog2Decimation(settings.m_log2Decim); + m_worker->setLog2Decimation(settings.m_log2Decim); qDebug() << "AudioInput::applySettings: set decimation to " << (1<setIQMapping(settings.m_iqMapping); + + if (m_worker) { + m_worker->setIQMapping(settings.m_iqMapping); + } } if (settings.m_useReverseAPI) diff --git a/plugins/samplesource/audioinput/audioinput.h b/plugins/samplesource/audioinput/audioinput.h index c533503b0..9e5c13349 100644 --- a/plugins/samplesource/audioinput/audioinput.h +++ b/plugins/samplesource/audioinput/audioinput.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "dsp/devicesamplesource.h" #include "audio/audioinputdevice.h" @@ -34,7 +35,7 @@ class QNetworkAccessManager; class QNetworkReply; class DeviceAPI; -class AudioInputThread; +class AudioInputWorker; class AudioInput : public DeviceSampleSource { Q_OBJECT @@ -135,7 +136,8 @@ private: AudioFifo m_fifo; QMutex m_mutex; AudioInputSettings m_settings; - AudioInputThread* m_thread; + AudioInputWorker* m_worker; + QThread m_workerThread; QString m_deviceDescription; bool m_running; QNetworkAccessManager *m_networkManager; diff --git a/plugins/samplesource/audioinput/audioinputthread.cpp b/plugins/samplesource/audioinput/audioinputworker.cpp similarity index 81% rename from plugins/samplesource/audioinput/audioinputthread.cpp rename to plugins/samplesource/audioinput/audioinputworker.cpp index 6575939a6..f72de8f1c 100644 --- a/plugins/samplesource/audioinput/audioinputthread.cpp +++ b/plugins/samplesource/audioinput/audioinputworker.cpp @@ -20,16 +20,14 @@ #include #include #include -#include -#include #include "dsp/samplesinkfifo.h" #include "audio/audiofifo.h" -#include "audioinputthread.h" +#include "audioinputworker.h" -AudioInputThread::AudioInputThread(SampleSinkFifo* sampleFifo, AudioFifo *fifo, QObject* parent) : - QThread(parent), +AudioInputWorker::AudioInputWorker(SampleSinkFifo* sampleFifo, AudioFifo *fifo, QObject* parent) : + QObject(parent), m_fifo(fifo), m_running(false), m_log2Decim(0), @@ -37,50 +35,25 @@ AudioInputThread::AudioInputThread(SampleSinkFifo* sampleFifo, AudioFifo *fifo, m_convertBuffer(m_convBufSamples), m_sampleFifo(sampleFifo) { - start(); } -AudioInputThread::~AudioInputThread() +AudioInputWorker::~AudioInputWorker() { } -void AudioInputThread::startWork() +void AudioInputWorker::startWork() { connect(m_fifo, SIGNAL(dataReady()), this, SLOT(handleAudio())); - m_startWaitMutex.lock(); - - start(); - - while(!m_running) - { - m_startWaiter.wait(&m_startWaitMutex, 100); - } - - m_startWaitMutex.unlock(); + m_running = true; } -void AudioInputThread::stopWork() +void AudioInputWorker::stopWork() { disconnect(m_fifo, SIGNAL(dataReady()), this, SLOT(handleAudio())); m_running = false; - wait(); } -void AudioInputThread::run() -{ - m_running = true; - qDebug("AudioInputThread::run: start running loop"); - - while (m_running) - { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } - - qDebug("AudioInputThread::run: running loop stopped"); - m_running = false; -} - -void AudioInputThread::workIQ(unsigned int nbRead) +void AudioInputWorker::workIQ(unsigned int nbRead) { // Map between left and right audio channels and IQ channels if (m_iqMapping == AudioInputSettings::IQMapping::L) @@ -138,7 +111,7 @@ void AudioInputThread::workIQ(unsigned int nbRead) m_sampleFifo->write(m_convertBuffer.begin(), it); } -void AudioInputThread::handleAudio() +void AudioInputWorker::handleAudio() { uint32_t nbRead; diff --git a/plugins/samplesource/audioinput/audioinputthread.h b/plugins/samplesource/audioinput/audioinputworker.h similarity index 85% rename from plugins/samplesource/audioinput/audioinputthread.h rename to plugins/samplesource/audioinput/audioinputworker.h index 78277ce6c..693e52177 100644 --- a/plugins/samplesource/audioinput/audioinputthread.h +++ b/plugins/samplesource/audioinput/audioinputworker.h @@ -16,12 +16,10 @@ // along with this program. If not, see . // /////////////////////////////////////////////////////////////////////////////////// -#ifndef INCLUDE_AUDIOINPUTTHREAD_H -#define INCLUDE_AUDIOINPUTTHREAD_H +#ifndef INCLUDE_AUDIOINPUTWORKER_H +#define INCLUDE_AUDIOINPUTWORKER_H -#include -#include -#include +#include #include "dsp/samplesinkfifo.h" #include "dsp/decimators.h" @@ -29,12 +27,12 @@ class AudioFifo; -class AudioInputThread : public QThread { +class AudioInputWorker : public QObject { Q_OBJECT public: - AudioInputThread(SampleSinkFifo* sampleFifo, AudioFifo *fifo, QObject* parent = nullptr); - ~AudioInputThread(); + AudioInputWorker(SampleSinkFifo* sampleFifo, AudioFifo *fifo, QObject* parent = nullptr); + ~AudioInputWorker(); void startWork(); void stopWork(); @@ -45,8 +43,6 @@ public: private: AudioFifo* m_fifo; - QMutex m_startWaitMutex; - QWaitCondition m_startWaiter; bool m_running; unsigned int m_log2Decim; AudioInputSettings::IQMapping m_iqMapping; @@ -56,11 +52,10 @@ private: SampleSinkFifo* m_sampleFifo; Decimators m_decimatorsIQ; - void run(); void workIQ(unsigned int nbRead); private slots: void handleAudio(); }; -#endif // INCLUDE_AUDIOINPUTTHREAD_H +#endif // INCLUDE_AUDIOINPUTWORKER_H