Channel Analyzer: improved baseband thread management

This commit is contained in:
f4exb 2020-07-13 10:33:05 +02:00
parent 181efe4b1c
commit fe520f5ae5
4 changed files with 54 additions and 21 deletions

View File

@ -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);

View File

@ -19,6 +19,7 @@
#define INCLUDE_CHANALYZER_H
#include <QMutex>
#include <QThread>
#include <vector>
#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);
};

View File

@ -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)

View File

@ -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);