From 9789b23cd63cfbad5823b82f31fe58dc03aadf37 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 23 Jul 2022 11:50:04 +0200 Subject: [PATCH] DSD Demod: applied new threading model. Part of #1346 --- plugins/channelrx/demoddsd/dsddemod.cpp | 98 ++++++++++++++----- plugins/channelrx/demoddsd/dsddemod.h | 3 + .../channelrx/demoddsd/dsddemodbaseband.cpp | 30 +++--- plugins/channelrx/demoddsd/dsddemodbaseband.h | 4 +- 4 files changed, 95 insertions(+), 40 deletions(-) diff --git a/plugins/channelrx/demoddsd/dsddemod.cpp b/plugins/channelrx/demoddsd/dsddemod.cpp index 45c93b3b7..120425a37 100644 --- a/plugins/channelrx/demoddsd/dsddemod.cpp +++ b/plugins/channelrx/demoddsd/dsddemod.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "SWGChannelSettings.h" #include "SWGWorkspaceInfo.h" @@ -57,16 +58,13 @@ const int DSDDemod::m_udpBlockSize = 512; DSDDemod::DSDDemod(DeviceAPI *deviceAPI) : ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSink), m_deviceAPI(deviceAPI), + m_mutex(QMutex::Recursive), + m_running(false), m_basebandSampleRate(0) { qDebug("DSDDemod::DSDDemod"); setObjectName(m_channelId); - m_thread = new QThread(this); - m_basebandSink = new DSDDemodBaseband(); - m_basebandSink->setChannel(this); - m_basebandSink->moveToThread(m_thread); - applySettings(m_settings, true); m_deviceAPI->addChannelSink(this); @@ -99,6 +97,7 @@ DSDDemod::DSDDemod(DeviceAPI *deviceAPI) : ); scanAvailableAMBEFeatures(); + start(); } DSDDemod::~DSDDemod() @@ -112,8 +111,8 @@ DSDDemod::~DSDDemod() delete m_networkManager; m_deviceAPI->removeChannelSinkAPI(this); m_deviceAPI->removeChannelSink(this); - delete m_basebandSink; - delete m_thread; + + stop(); } void DSDDemod::setDeviceAPI(DeviceAPI *deviceAPI) @@ -136,24 +135,66 @@ uint32_t DSDDemod::getNumberOfDeviceStreams() const void DSDDemod::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool firstOfBurst) { (void) firstOfBurst; - m_basebandSink->feed(begin, end); + + if (m_running) { + m_basebandSink->feed(begin, end); + } } void DSDDemod::start() { + QMutexLocker m_lock(&m_mutex); + + if (m_running) { + return; + } + qDebug() << "DSDDemod::start"; + m_thread = new QThread(); + m_basebandSink = new DSDDemodBaseband(); + m_basebandSink->setFifoLabel(QString("%1 [%2:%3]") + .arg(m_channelId) + .arg(m_deviceAPI->getDeviceSetIndex()) + .arg(getIndexInDeviceSet()) + ); + m_basebandSink->setChannel(this); + m_basebandSink->moveToThread(m_thread); + + QObject::connect( + m_thread, + &QThread::finished, + m_basebandSink, + &QObject::deleteLater + ); + QObject::connect( + m_thread, + &QThread::finished, + m_thread, + &QThread::deleteLater + ); if (m_basebandSampleRate != 0) { m_basebandSink->setBasebandSampleRate(m_basebandSampleRate); } - m_basebandSink->reset(); m_thread->start(); + + DSDDemodBaseband::MsgConfigureDSDDemodBaseband *msg = DSDDemodBaseband::MsgConfigureDSDDemodBaseband::create(m_settings, true); + m_basebandSink->getInputMessageQueue()->push(msg); + + m_running = true; } void DSDDemod::stop() { + QMutexLocker m_lock(&m_mutex); + + if (!m_running) { + return; + } + qDebug() << "DSDDemod::stop"; + m_running = false; m_thread->exit(); m_thread->wait(); } @@ -173,12 +214,13 @@ bool DSDDemod::handleMessage(const Message& cmd) } else if (DSPSignalNotification::match(cmd)) { + qDebug() << "DSDDemod::handleMessage: DSPSignalNotification"; DSPSignalNotification& notif = (DSPSignalNotification&) cmd; m_basebandSampleRate = notif.getSampleRate(); // Forward to the sink - DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy - qDebug() << "DSDDemod::handleMessage: DSPSignalNotification"; - m_basebandSink->getInputMessageQueue()->push(rep); + if (m_running) { + m_basebandSink->getInputMessageQueue()->push(new DSPSignalNotification(notif)); + } // Forward to GUI if any if (getMessageQueueToGUI()) { getMessageQueueToGUI()->push(new DSPSignalNotification(notif)); @@ -318,14 +360,16 @@ void DSDDemod::applySettings(const DSDDemodSettings& settings, bool force) { for (const auto& feature : m_availableAMBEFeatures) { - if (feature.m_featureIndex == settings.m_ambeFeatureIndex) { + if (m_running && (feature.m_featureIndex == settings.m_ambeFeatureIndex)) { m_basebandSink->setAMBEFeature(feature.m_feature); } } } else { - m_basebandSink->setAMBEFeature(nullptr); + if (m_running) { + m_basebandSink->setAMBEFeature(nullptr); + } } } @@ -342,8 +386,11 @@ void DSDDemod::applySettings(const DSDDemodSettings& settings, bool force) reverseAPIKeys.append("streamIndex"); } - DSDDemodBaseband::MsgConfigureDSDDemodBaseband *msg = DSDDemodBaseband::MsgConfigureDSDDemodBaseband::create(settings, force); - m_basebandSink->getInputMessageQueue()->push(msg); + if (m_running) + { + DSDDemodBaseband::MsgConfigureDSDDemodBaseband *msg = DSDDemodBaseband::MsgConfigureDSDDemodBaseband::create(settings, force); + m_basebandSink->getInputMessageQueue()->push(msg); + } if (settings.m_useReverseAPI) { @@ -676,10 +723,14 @@ void DSDDemod::webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response int nbMagsqSamples; getMagSqLevels(magsqAvg, magsqPeak, nbMagsqSamples); + if (m_running) + { + response.getDsdDemodReport()->setAudioSampleRate(m_basebandSink->getAudioSampleRate()); + response.getDsdDemodReport()->setChannelSampleRate(m_basebandSink->getChannelSampleRate()); + response.getDsdDemodReport()->setSquelch(m_basebandSink->getSquelchOpen() ? 1 : 0); + } + response.getDsdDemodReport()->setChannelPowerDb(CalcDb::dbPower(magsqAvg)); - response.getDsdDemodReport()->setAudioSampleRate(m_basebandSink->getAudioSampleRate()); - response.getDsdDemodReport()->setChannelSampleRate(m_basebandSink->getChannelSampleRate()); - response.getDsdDemodReport()->setSquelch(m_basebandSink->getSquelchOpen() ? 1 : 0); response.getDsdDemodReport()->setPllLocked(getDecoder().getSymbolPLLLocked() ? 1 : 0); response.getDsdDemodReport()->setSlot1On(getDecoder().getVoice1On() ? 1 : 0); response.getDsdDemodReport()->setSlot2On(getDecoder().getVoice2On() ? 1 : 0); @@ -871,7 +922,7 @@ void DSDDemod::networkManagerFinished(QNetworkReply *reply) void DSDDemod::handleIndexInDeviceSetChanged(int index) { - if (index < 0) { + if (!m_running || (index < 0)) { return; } @@ -893,7 +944,7 @@ void DSDDemod::handleFeatureAdded(int featureSetIndex, Feature *feature) { m_availableAMBEFeatures[feature] = DSDDemodSettings::AvailableAMBEFeature{feature->getIndexInFeatureSet(), feature}; - if (m_settings.m_connectAMBE && (m_settings.m_ambeFeatureIndex == feature->getIndexInFeatureSet())) { + if (m_running && m_settings.m_connectAMBE && (m_settings.m_ambeFeatureIndex == feature->getIndexInFeatureSet())) { m_basebandSink->setAMBEFeature(feature); } @@ -912,7 +963,10 @@ void DSDDemod::handleFeatureRemoved(int featureSetIndex, Feature *feature) if (m_settings.m_ambeFeatureIndex == m_availableAMBEFeatures[feature].m_featureIndex) { m_settings.m_connectAMBE = false; - m_basebandSink->setAMBEFeature(nullptr); + + if (m_running) { + m_basebandSink->setAMBEFeature(nullptr); + } } m_availableAMBEFeatures.remove(feature); diff --git a/plugins/channelrx/demoddsd/dsddemod.h b/plugins/channelrx/demoddsd/dsddemod.h index 147d10143..3d7b94ee6 100644 --- a/plugins/channelrx/demoddsd/dsddemod.h +++ b/plugins/channelrx/demoddsd/dsddemod.h @@ -22,6 +22,7 @@ #include #include +#include #include "dsp/basebandsamplesink.h" #include "channel/channelapi.h" @@ -170,6 +171,8 @@ private: DeviceAPI *m_deviceAPI; QThread *m_thread; DSDDemodBaseband *m_basebandSink; + QMutex m_mutex; + bool m_running; DSDDemodSettings m_settings; int m_basebandSampleRate; //!< stored from device message used when starting baseband sink QHash m_availableAMBEFeatures; diff --git a/plugins/channelrx/demoddsd/dsddemodbaseband.cpp b/plugins/channelrx/demoddsd/dsddemodbaseband.cpp index 97a5500bb..b19119467 100644 --- a/plugins/channelrx/demoddsd/dsddemodbaseband.cpp +++ b/plugins/channelrx/demoddsd/dsddemodbaseband.cpp @@ -19,18 +19,17 @@ #include "dsp/dspengine.h" #include "dsp/dspcommands.h" -#include "dsp/downchannelizer.h" #include "dsddemodbaseband.h" MESSAGE_CLASS_DEFINITION(DSDDemodBaseband::MsgConfigureDSDDemodBaseband, Message) DSDDemodBaseband::DSDDemodBaseband() : + m_channelizer(&m_sink), m_mutex(QMutex::Recursive) { qDebug("DSDDemodBaseband::DSDDemodBaseband"); m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(48000)); - m_channelizer = new DownChannelizer(&m_sink); QObject::connect( &m_sampleFifo, @@ -53,7 +52,6 @@ DSDDemodBaseband::~DSDDemodBaseband() { DSPEngine::instance()->getAudioDeviceManager()->removeAudioSink(m_sink.getAudioFifo1()); DSPEngine::instance()->getAudioDeviceManager()->removeAudioSink(m_sink.getAudioFifo2()); - delete m_channelizer; } void DSDDemodBaseband::reset() @@ -88,12 +86,12 @@ void DSDDemodBaseband::handleData() // first part of FIFO data if (part1begin != part1end) { - m_channelizer->feed(part1begin, part1end); + m_channelizer.feed(part1begin, part1end); } // second part of FIFO data (used when block wraps around) if(part2begin != part2end) { - m_channelizer->feed(part2begin, part2end); + m_channelizer.feed(part2begin, part2end); } m_sampleFifo.readCommit((unsigned int) count); @@ -130,13 +128,13 @@ bool DSDDemodBaseband::handleMessage(const Message& cmd) DSPSignalNotification& notif = (DSPSignalNotification&) cmd; qDebug() << "DSDDemodBaseband::handleMessage: DSPSignalNotification: basebandSampleRate: " << notif.getSampleRate(); m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(notif.getSampleRate())); - m_channelizer->setBasebandSampleRate(notif.getSampleRate()); - m_sink.applyChannelSettings(m_channelizer->getChannelSampleRate(), m_channelizer->getChannelFrequencyOffset()); + m_channelizer.setBasebandSampleRate(notif.getSampleRate()); + m_sink.applyChannelSettings(m_channelizer.getChannelSampleRate(), m_channelizer.getChannelFrequencyOffset()); - if (m_channelSampleRate != m_channelizer->getChannelSampleRate()) + if (m_channelSampleRate != m_channelizer.getChannelSampleRate()) { m_sink.applyAudioSampleRate(m_sink.getAudioSampleRate()); // reapply when channel sample rate changes - m_channelSampleRate = m_channelizer->getChannelSampleRate(); + m_channelSampleRate = m_channelizer.getChannelSampleRate(); } return true; @@ -151,13 +149,13 @@ void DSDDemodBaseband::applySettings(const DSDDemodSettings& settings, bool forc { if ((settings.m_inputFrequencyOffset != m_settings.m_inputFrequencyOffset) || force) { - m_channelizer->setChannelization(48000, settings.m_inputFrequencyOffset); - m_sink.applyChannelSettings(m_channelizer->getChannelSampleRate(), m_channelizer->getChannelFrequencyOffset()); + m_channelizer.setChannelization(48000, settings.m_inputFrequencyOffset); + m_sink.applyChannelSettings(m_channelizer.getChannelSampleRate(), m_channelizer.getChannelFrequencyOffset()); - if (m_channelSampleRate != m_channelizer->getChannelSampleRate()) + if (m_channelSampleRate != m_channelizer.getChannelSampleRate()) { m_sink.applyAudioSampleRate(m_sink.getAudioSampleRate()); // reapply when channel sample rate changes - m_channelSampleRate = m_channelizer->getChannelSampleRate(); + m_channelSampleRate = m_channelizer.getChannelSampleRate(); } } @@ -184,12 +182,12 @@ void DSDDemodBaseband::applySettings(const DSDDemodSettings& settings, bool forc int DSDDemodBaseband::getChannelSampleRate() const { - return m_channelizer->getChannelSampleRate(); + return m_channelizer.getChannelSampleRate(); } void DSDDemodBaseband::setBasebandSampleRate(int sampleRate) { - m_channelizer->setBasebandSampleRate(sampleRate); - m_sink.applyChannelSettings(m_channelizer->getChannelSampleRate(), m_channelizer->getChannelFrequencyOffset()); + m_channelizer.setBasebandSampleRate(sampleRate); + m_sink.applyChannelSettings(m_channelizer.getChannelSampleRate(), m_channelizer.getChannelFrequencyOffset()); } diff --git a/plugins/channelrx/demoddsd/dsddemodbaseband.h b/plugins/channelrx/demoddsd/dsddemodbaseband.h index 7f11382c6..6e2b647ff 100644 --- a/plugins/channelrx/demoddsd/dsddemodbaseband.h +++ b/plugins/channelrx/demoddsd/dsddemodbaseband.h @@ -22,12 +22,12 @@ #include #include "dsp/samplesinkfifo.h" +#include "dsp/downchannelizer.h" #include "util/message.h" #include "util/messagequeue.h" #include "dsddemodsink.h" -class DownChannelizer; class ChannelAPI; class Feature; @@ -80,7 +80,7 @@ public: private: SampleSinkFifo m_sampleFifo; - DownChannelizer *m_channelizer; + DownChannelizer m_channelizer; int m_channelSampleRate; DSDDemodSink m_sink; MessageQueue m_inputMessageQueue; //!< Queue for asynchronous inbound communication