1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-23 08:28:36 -05:00

AM demod: improved baseband thread management

This commit is contained in:
f4exb 2020-07-13 10:15:27 +02:00
parent d1637e0158
commit 181efe4b1c
4 changed files with 55 additions and 21 deletions

View File

@ -50,9 +50,8 @@ AMDemod::AMDemod(DeviceAPI *deviceAPI) :
{ {
setObjectName(m_channelId); setObjectName(m_channelId);
m_thread = new QThread(this);
m_basebandSink = new AMDemodBaseband(); m_basebandSink = new AMDemodBaseband();
m_basebandSink->moveToThread(m_thread); m_basebandSink->moveToThread(&m_thread);
applySettings(m_settings, true); applySettings(m_settings, true);
@ -69,8 +68,12 @@ AMDemod::~AMDemod()
delete m_networkManager; delete m_networkManager;
m_deviceAPI->removeChannelSinkAPI(this); m_deviceAPI->removeChannelSinkAPI(this);
m_deviceAPI->removeChannelSink(this); m_deviceAPI->removeChannelSink(this);
if (m_basebandSink->isRunning()) {
stop();
}
delete m_basebandSink; delete m_basebandSink;
delete m_thread;
} }
uint32_t AMDemod::getNumberOfDeviceStreams() const uint32_t AMDemod::getNumberOfDeviceStreams() const
@ -88,19 +91,23 @@ void AMDemod::start()
{ {
qDebug("AMDemod::start"); qDebug("AMDemod::start");
if (m_basebandSampleRate != 0) {
m_basebandSink->setBasebandSampleRate(m_basebandSampleRate);
}
m_basebandSink->reset(); 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() void AMDemod::stop()
{ {
qDebug("AMDemod::stop"); qDebug("AMDemod::stop");
m_thread->exit(); m_basebandSink->stopWork();
m_thread->wait(); m_thread.quit();
m_thread.wait();
} }
bool AMDemod::handleMessage(const Message& cmd) bool AMDemod::handleMessage(const Message& cmd)
@ -117,6 +124,7 @@ bool AMDemod::handleMessage(const Message& cmd)
{ {
DSPSignalNotification& notif = (DSPSignalNotification&) cmd; DSPSignalNotification& notif = (DSPSignalNotification&) cmd;
m_basebandSampleRate = notif.getSampleRate(); m_basebandSampleRate = notif.getSampleRate();
m_centerFrequency = notif.getCenterFrequency();
// Forward to the sink // Forward to the sink
DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy
qDebug() << "AMDemod::handleMessage: DSPSignalNotification"; qDebug() << "AMDemod::handleMessage: DSPSignalNotification";

View File

@ -21,6 +21,7 @@
#include <vector> #include <vector>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QThread>
#include "dsp/basebandsamplesink.h" #include "dsp/basebandsamplesink.h"
#include "channel/channelapi.h" #include "channel/channelapi.h"
@ -126,10 +127,11 @@ public:
private: private:
DeviceAPI *m_deviceAPI; DeviceAPI *m_deviceAPI;
QThread *m_thread; QThread m_thread;
AMDemodBaseband* m_basebandSink; AMDemodBaseband* m_basebandSink;
AMDemodSettings m_settings; AMDemodSettings m_settings;
int m_basebandSampleRate; //!< stored from device message used when starting baseband sink int m_basebandSampleRate; //!< stored from device message used when starting baseband sink
qint64 m_centerFrequency;
static const int m_udpBlockSize; static const int m_udpBlockSize;

View File

@ -26,6 +26,7 @@
MESSAGE_CLASS_DEFINITION(AMDemodBaseband::MsgConfigureAMDemodBaseband, Message) MESSAGE_CLASS_DEFINITION(AMDemodBaseband::MsgConfigureAMDemodBaseband, Message)
AMDemodBaseband::AMDemodBaseband() : AMDemodBaseband::AMDemodBaseband() :
m_running(false),
m_mutex(QMutex::Recursive) m_mutex(QMutex::Recursive)
{ {
qDebug("AMDemodBaseband::AMDemodBaseband"); qDebug("AMDemodBaseband::AMDemodBaseband");
@ -33,22 +34,13 @@ AMDemodBaseband::AMDemodBaseband() :
m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(48000)); m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(48000));
m_channelizer = new DownChannelizer(&m_sink); 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()); DSPEngine::instance()->getAudioDeviceManager()->addAudioSink(m_sink.getAudioFifo(), getInputMessageQueue());
m_sink.applyAudioSampleRate(DSPEngine::instance()->getAudioDeviceManager()->getOutputSampleRate()); m_sink.applyAudioSampleRate(DSPEngine::instance()->getAudioDeviceManager()->getOutputSampleRate());
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()));
} }
AMDemodBaseband::~AMDemodBaseband() AMDemodBaseband::~AMDemodBaseband()
{ {
m_inputMessageQueue.clear();
DSPEngine::instance()->getAudioDeviceManager()->removeAudioSink(m_sink.getAudioFifo()); DSPEngine::instance()->getAudioDeviceManager()->removeAudioSink(m_sink.getAudioFifo());
delete m_channelizer; delete m_channelizer;
} }
@ -56,9 +48,37 @@ AMDemodBaseband::~AMDemodBaseband()
void AMDemodBaseband::reset() void AMDemodBaseband::reset()
{ {
QMutexLocker mutexLocker(&m_mutex); QMutexLocker mutexLocker(&m_mutex);
m_inputMessageQueue.clear();
m_sampleFifo.reset(); 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) void AMDemodBaseband::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end)
{ {
m_sampleFifo.write(begin, end); m_sampleFifo.write(begin, end);

View File

@ -59,6 +59,8 @@ public:
AMDemodBaseband(); AMDemodBaseband();
~AMDemodBaseband(); ~AMDemodBaseband();
void reset(); void reset();
void startWork();
void stopWork();
void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end); void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end);
MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; } //!< Get the queue for asynchronous inbound communication MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; } //!< Get the queue for asynchronous inbound communication
int getChannelSampleRate() const; int getChannelSampleRate() const;
@ -69,6 +71,7 @@ public:
double getMagSq() const { return m_sink.getMagSq(); } double getMagSq() const { return m_sink.getMagSq(); }
bool getPllLocked() const { return m_sink.getPllLocked(); } bool getPllLocked() const { return m_sink.getPllLocked(); }
Real getPllFrequency() const { return m_sink.getPllFrequency(); } Real getPllFrequency() const { return m_sink.getPllFrequency(); }
bool isRunning() const { return m_running; }
private: private:
SampleSinkFifo m_sampleFifo; SampleSinkFifo m_sampleFifo;
@ -76,6 +79,7 @@ private:
AMDemodSink m_sink; AMDemodSink m_sink;
MessageQueue m_inputMessageQueue; //!< Queue for asynchronous inbound communication MessageQueue m_inputMessageQueue; //!< Queue for asynchronous inbound communication
AMDemodSettings m_settings; AMDemodSettings m_settings;
bool m_running;
QMutex m_mutex; QMutex m_mutex;
bool handleMessage(const Message& cmd); bool handleMessage(const Message& cmd);