From fe520f5ae5fdd2d81a2c669058c08262ee85f01d Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 13 Jul 2020 10:33:05 +0200 Subject: [PATCH] Channel Analyzer: improved baseband thread management --- plugins/channelrx/chanalyzer/chanalyzer.cpp | 30 ++++++++++------ plugins/channelrx/chanalyzer/chanalyzer.h | 5 +-- .../chanalyzer/chanalyzerbaseband.cpp | 36 ++++++++++++++----- .../channelrx/chanalyzer/chanalyzerbaseband.h | 4 +++ 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/plugins/channelrx/chanalyzer/chanalyzer.cpp b/plugins/channelrx/chanalyzer/chanalyzer.cpp index 46a770bc4..ac3752165 100644 --- a/plugins/channelrx/chanalyzer/chanalyzer.cpp +++ b/plugins/channelrx/chanalyzer/chanalyzer.cpp @@ -40,9 +40,8 @@ ChannelAnalyzer::ChannelAnalyzer(DeviceAPI *deviceAPI) : qDebug("ChannelAnalyzer::ChannelAnalyzer"); setObjectName(m_channelId); - m_thread = new QThread(this); m_basebandSink = new ChannelAnalyzerBaseband(); - m_basebandSink->moveToThread(m_thread); + m_basebandSink->moveToThread(&m_thread); applySettings(m_settings, true); @@ -54,8 +53,12 @@ ChannelAnalyzer::~ChannelAnalyzer() { m_deviceAPI->removeChannelSinkAPI(this); m_deviceAPI->removeChannelSink(this); + + if (m_basebandSink->isRunning()) { + stop(); + } + delete m_basebandSink; - delete m_thread; } void ChannelAnalyzer::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly) @@ -64,24 +67,28 @@ void ChannelAnalyzer::feed(const SampleVector::const_iterator& begin, const Samp m_basebandSink->feed(begin, end); } - void ChannelAnalyzer::start() { qDebug() << "ChannelAnalyzer::start"; - if (m_basebandSampleRate != 0) { - m_basebandSink->setBasebandSampleRate(m_basebandSampleRate); - } - m_basebandSink->reset(); - m_thread->start(); + m_basebandSink->startWork(); + m_thread.start(); + + DSPSignalNotification *dspMsg = new DSPSignalNotification(m_basebandSampleRate, m_centerFrequency); + m_basebandSink->getInputMessageQueue()->push(dspMsg); + + ChannelAnalyzerBaseband::MsgConfigureChannelAnalyzerBaseband *msg = + ChannelAnalyzerBaseband::MsgConfigureChannelAnalyzerBaseband::create(m_settings, true); + m_basebandSink->getInputMessageQueue()->push(msg); } void ChannelAnalyzer::stop() { qDebug() << "ChannelAnalyzer::stop"; - m_thread->exit(); - m_thread->wait(); + m_basebandSink->stopWork(); + m_thread.quit(); + m_thread.wait(); } bool ChannelAnalyzer::handleMessage(const Message& cmd) @@ -99,6 +106,7 @@ bool ChannelAnalyzer::handleMessage(const Message& cmd) { DSPSignalNotification& cfg = (DSPSignalNotification&) cmd; m_basebandSampleRate = cfg.getSampleRate(); + m_centerFrequency = cfg.getCenterFrequency(); DSPSignalNotification *notif = new DSPSignalNotification(cfg); m_basebandSink->getInputMessageQueue()->push(notif); diff --git a/plugins/channelrx/chanalyzer/chanalyzer.h b/plugins/channelrx/chanalyzer/chanalyzer.h index e7f210b9f..cc3edfffd 100644 --- a/plugins/channelrx/chanalyzer/chanalyzer.h +++ b/plugins/channelrx/chanalyzer/chanalyzer.h @@ -19,6 +19,7 @@ #define INCLUDE_CHANALYZER_H #include +#include #include #include "dsp/basebandsamplesink.h" @@ -29,7 +30,6 @@ #include "chanalyzerbaseband.h" -class QThread; class DownChannelizer; class ChannelAnalyzer : public BasebandSampleSink, public ChannelAPI { @@ -98,11 +98,12 @@ public: private: DeviceAPI *m_deviceAPI; - QThread *m_thread; + QThread m_thread; ChannelAnalyzerBaseband *m_basebandSink; ChannelAnalyzerSettings m_settings; SpectrumVis m_spectrumVis; int m_basebandSampleRate; //!< stored from device message used when starting baseband sink + qint64 m_centerFrequency; //!< stored from device message used when starting baseband sink void applySettings(const ChannelAnalyzerSettings& settings, bool force = false); }; diff --git a/plugins/channelrx/chanalyzer/chanalyzerbaseband.cpp b/plugins/channelrx/chanalyzer/chanalyzerbaseband.cpp index a93218fbb..ca3b34470 100644 --- a/plugins/channelrx/chanalyzer/chanalyzerbaseband.cpp +++ b/plugins/channelrx/chanalyzer/chanalyzerbaseband.cpp @@ -26,12 +26,30 @@ MESSAGE_CLASS_DEFINITION(ChannelAnalyzerBaseband::MsgConfigureChannelAnalyzerBaseband, Message) ChannelAnalyzerBaseband::ChannelAnalyzerBaseband() : + m_running(false), m_mutex(QMutex::Recursive) { qDebug("ChannelAnalyzerBaseband::ChannelAnalyzerBaseband"); m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(48000)); m_channelizer = new DownChannelizer(&m_sink); +} +ChannelAnalyzerBaseband::~ChannelAnalyzerBaseband() +{ + m_inputMessageQueue.clear(); + delete m_channelizer; +} + +void ChannelAnalyzerBaseband::reset() +{ + QMutexLocker mutexLocker(&m_mutex); + m_inputMessageQueue.clear(); + m_sampleFifo.reset(); +} + +void ChannelAnalyzerBaseband::startWork() +{ + QMutexLocker mutexLocker(&m_mutex); QObject::connect( &m_sampleFifo, &SampleSinkFifo::dataReady, @@ -39,19 +57,21 @@ ChannelAnalyzerBaseband::ChannelAnalyzerBaseband() : &ChannelAnalyzerBaseband::handleData, Qt::QueuedConnection ); - connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); + m_running = true; } -ChannelAnalyzerBaseband::~ChannelAnalyzerBaseband() -{ - delete m_channelizer; -} - -void ChannelAnalyzerBaseband::reset() +void ChannelAnalyzerBaseband::stopWork() { QMutexLocker mutexLocker(&m_mutex); - m_sampleFifo.reset(); + disconnect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); + QObject::disconnect( + &m_sampleFifo, + &SampleSinkFifo::dataReady, + this, + &ChannelAnalyzerBaseband::handleData + ); + m_running = false; } void ChannelAnalyzerBaseband::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end) diff --git a/plugins/channelrx/chanalyzer/chanalyzerbaseband.h b/plugins/channelrx/chanalyzer/chanalyzerbaseband.h index bcb958551..709c440f6 100644 --- a/plugins/channelrx/chanalyzer/chanalyzerbaseband.h +++ b/plugins/channelrx/chanalyzer/chanalyzerbaseband.h @@ -59,6 +59,9 @@ public: ChannelAnalyzerBaseband(); ~ChannelAnalyzerBaseband(); void reset(); + void startWork(); + void stopWork(); + bool isRunning() const { return m_running; } void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end); MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; } //!< Get the queue for asynchronous inbound communication int getChannelSampleRate() const; @@ -77,6 +80,7 @@ private: ChannelAnalyzerSink m_sink; MessageQueue m_inputMessageQueue; //!< Queue for asynchronous inbound communication ChannelAnalyzerSettings m_settings; + bool m_running; QMutex m_mutex; bool handleMessage(const Message& cmd);