From 181efe4b1c1cae7539c6e9a2e4ec14bf29d984f2 Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 13 Jul 2020 10:15:27 +0200 Subject: [PATCH] AM demod: improved baseband thread management --- plugins/channelrx/demodam/amdemod.cpp | 28 ++++++++----- plugins/channelrx/demodam/amdemod.h | 4 +- plugins/channelrx/demodam/amdemodbaseband.cpp | 40 ++++++++++++++----- plugins/channelrx/demodam/amdemodbaseband.h | 4 ++ 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/plugins/channelrx/demodam/amdemod.cpp b/plugins/channelrx/demodam/amdemod.cpp index 7670e8e88..e499fe821 100644 --- a/plugins/channelrx/demodam/amdemod.cpp +++ b/plugins/channelrx/demodam/amdemod.cpp @@ -50,9 +50,8 @@ AMDemod::AMDemod(DeviceAPI *deviceAPI) : { setObjectName(m_channelId); - m_thread = new QThread(this); m_basebandSink = new AMDemodBaseband(); - m_basebandSink->moveToThread(m_thread); + m_basebandSink->moveToThread(&m_thread); applySettings(m_settings, true); @@ -69,8 +68,12 @@ AMDemod::~AMDemod() delete m_networkManager; m_deviceAPI->removeChannelSinkAPI(this); m_deviceAPI->removeChannelSink(this); + + if (m_basebandSink->isRunning()) { + stop(); + } + delete m_basebandSink; - delete m_thread; } uint32_t AMDemod::getNumberOfDeviceStreams() const @@ -88,19 +91,23 @@ void AMDemod::start() { qDebug("AMDemod::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); + + AMDemodBaseband::MsgConfigureAMDemodBaseband *msg = AMDemodBaseband::MsgConfigureAMDemodBaseband::create(m_settings, true); + m_basebandSink->getInputMessageQueue()->push(msg); } void AMDemod::stop() { qDebug("AMDemod::stop"); - m_thread->exit(); - m_thread->wait(); + m_basebandSink->stopWork(); + m_thread.quit(); + m_thread.wait(); } bool AMDemod::handleMessage(const Message& cmd) @@ -117,6 +124,7 @@ bool AMDemod::handleMessage(const Message& cmd) { DSPSignalNotification& notif = (DSPSignalNotification&) cmd; m_basebandSampleRate = notif.getSampleRate(); + m_centerFrequency = notif.getCenterFrequency(); // Forward to the sink DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy qDebug() << "AMDemod::handleMessage: DSPSignalNotification"; diff --git a/plugins/channelrx/demodam/amdemod.h b/plugins/channelrx/demodam/amdemod.h index dbf029bc5..4063f7c6c 100644 --- a/plugins/channelrx/demodam/amdemod.h +++ b/plugins/channelrx/demodam/amdemod.h @@ -21,6 +21,7 @@ #include #include +#include #include "dsp/basebandsamplesink.h" #include "channel/channelapi.h" @@ -126,10 +127,11 @@ public: private: DeviceAPI *m_deviceAPI; - QThread *m_thread; + QThread m_thread; AMDemodBaseband* m_basebandSink; AMDemodSettings m_settings; int m_basebandSampleRate; //!< stored from device message used when starting baseband sink + qint64 m_centerFrequency; static const int m_udpBlockSize; diff --git a/plugins/channelrx/demodam/amdemodbaseband.cpp b/plugins/channelrx/demodam/amdemodbaseband.cpp index 1204483be..6bae1dff3 100644 --- a/plugins/channelrx/demodam/amdemodbaseband.cpp +++ b/plugins/channelrx/demodam/amdemodbaseband.cpp @@ -26,6 +26,7 @@ MESSAGE_CLASS_DEFINITION(AMDemodBaseband::MsgConfigureAMDemodBaseband, Message) AMDemodBaseband::AMDemodBaseband() : + m_running(false), m_mutex(QMutex::Recursive) { qDebug("AMDemodBaseband::AMDemodBaseband"); @@ -33,22 +34,13 @@ AMDemodBaseband::AMDemodBaseband() : m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(48000)); m_channelizer = new DownChannelizer(&m_sink); - QObject::connect( - &m_sampleFifo, - &SampleSinkFifo::dataReady, - this, - &AMDemodBaseband::handleData, - Qt::QueuedConnection - ); - DSPEngine::instance()->getAudioDeviceManager()->addAudioSink(m_sink.getAudioFifo(), getInputMessageQueue()); m_sink.applyAudioSampleRate(DSPEngine::instance()->getAudioDeviceManager()->getOutputSampleRate()); - - connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); } AMDemodBaseband::~AMDemodBaseband() { + m_inputMessageQueue.clear(); DSPEngine::instance()->getAudioDeviceManager()->removeAudioSink(m_sink.getAudioFifo()); delete m_channelizer; } @@ -56,9 +48,37 @@ AMDemodBaseband::~AMDemodBaseband() void AMDemodBaseband::reset() { QMutexLocker mutexLocker(&m_mutex); + m_inputMessageQueue.clear(); m_sampleFifo.reset(); } +void AMDemodBaseband::startWork() +{ + QMutexLocker mutexLocker(&m_mutex); + QObject::connect( + &m_sampleFifo, + &SampleSinkFifo::dataReady, + this, + &AMDemodBaseband::handleData, + Qt::QueuedConnection + ); + connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); + m_running = true; +} + +void AMDemodBaseband::stopWork() +{ + QMutexLocker mutexLocker(&m_mutex); + disconnect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); + QObject::disconnect( + &m_sampleFifo, + &SampleSinkFifo::dataReady, + this, + &AMDemodBaseband::handleData + ); + m_running = false; +} + void AMDemodBaseband::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end) { m_sampleFifo.write(begin, end); diff --git a/plugins/channelrx/demodam/amdemodbaseband.h b/plugins/channelrx/demodam/amdemodbaseband.h index f56f1a4e6..91f8ab6b4 100644 --- a/plugins/channelrx/demodam/amdemodbaseband.h +++ b/plugins/channelrx/demodam/amdemodbaseband.h @@ -59,6 +59,8 @@ public: AMDemodBaseband(); ~AMDemodBaseband(); void reset(); + void startWork(); + void stopWork(); 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; @@ -69,6 +71,7 @@ public: double getMagSq() const { return m_sink.getMagSq(); } bool getPllLocked() const { return m_sink.getPllLocked(); } Real getPllFrequency() const { return m_sink.getPllFrequency(); } + bool isRunning() const { return m_running; } private: SampleSinkFifo m_sampleFifo; @@ -76,6 +79,7 @@ private: AMDemodSink m_sink; MessageQueue m_inputMessageQueue; //!< Queue for asynchronous inbound communication AMDemodSettings m_settings; + bool m_running; QMutex m_mutex; bool handleMessage(const Message& cmd);