1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-26 01:39:05 -05:00

WFM demod: updated threading model. Part of #1346

This commit is contained in:
f4exb 2022-10-13 23:52:46 +02:00
parent 0b99a6f309
commit aac1c9cd90
2 changed files with 62 additions and 20 deletions

View File

@ -55,15 +55,12 @@ const int WFMDemod::m_udpBlockSize = 512;
WFMDemod::WFMDemod(DeviceAPI* deviceAPI) : WFMDemod::WFMDemod(DeviceAPI* deviceAPI) :
ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSink), ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSink),
m_deviceAPI(deviceAPI), m_deviceAPI(deviceAPI),
m_thread(nullptr),
m_basebandSink(nullptr),
m_running(false),
m_basebandSampleRate(0) m_basebandSampleRate(0)
{ {
setObjectName(m_channelId); setObjectName(m_channelId);
m_thread = new QThread(this);
m_basebandSink = new WFMDemodBaseband();
m_basebandSink->setChannel(this);
m_basebandSink->moveToThread(m_thread);
applySettings(m_settings, true); applySettings(m_settings, true);
m_deviceAPI->addChannelSink(this); m_deviceAPI->addChannelSink(this);
@ -82,6 +79,7 @@ WFMDemod::WFMDemod(DeviceAPI* deviceAPI) :
this, this,
&WFMDemod::handleIndexInDeviceSetChanged &WFMDemod::handleIndexInDeviceSetChanged
); );
start();
} }
WFMDemod::~WFMDemod() WFMDemod::~WFMDemod()
@ -96,8 +94,7 @@ WFMDemod::~WFMDemod()
m_deviceAPI->removeChannelSinkAPI(this); m_deviceAPI->removeChannelSinkAPI(this);
m_deviceAPI->removeChannelSink(this); m_deviceAPI->removeChannelSink(this);
delete m_basebandSink; stop();
delete m_thread;
} }
void WFMDemod::setDeviceAPI(DeviceAPI *deviceAPI) void WFMDemod::setDeviceAPI(DeviceAPI *deviceAPI)
@ -120,24 +117,49 @@ uint32_t WFMDemod::getNumberOfDeviceStreams() const
void WFMDemod::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool firstOfBurst) void WFMDemod::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool firstOfBurst)
{ {
(void) firstOfBurst; (void) firstOfBurst;
m_basebandSink->feed(begin, end);
if (m_running) {
m_basebandSink->feed(begin, end);
}
} }
void WFMDemod::start() void WFMDemod::start()
{ {
if (m_running) {
return;
}
qDebug() << "WFMDemod::start"; qDebug() << "WFMDemod::start";
m_thread = new QThread(this);
m_basebandSink = new WFMDemodBaseband();
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) { if (m_basebandSampleRate != 0) {
m_basebandSink->setBasebandSampleRate(m_basebandSampleRate); m_basebandSink->setBasebandSampleRate(m_basebandSampleRate);
} }
m_basebandSink->reset(); m_basebandSink->reset();
m_thread->start(); m_thread->start();
WFMDemodBaseband::MsgConfigureWFMDemodBaseband *msg = WFMDemodBaseband::MsgConfigureWFMDemodBaseband::create(m_settings, true);
m_basebandSink->getInputMessageQueue()->push(msg);
m_running = true;
} }
void WFMDemod::stop() void WFMDemod::stop()
{ {
if (!m_running) {
return;
}
qDebug() << "WFMDemod::stop"; qDebug() << "WFMDemod::stop";
m_running = false;
m_thread->exit(); m_thread->exit();
m_thread->wait(); m_thread->wait();
} }
@ -157,10 +179,15 @@ bool WFMDemod::handleMessage(const Message& cmd)
{ {
DSPSignalNotification& notif = (DSPSignalNotification&) cmd; DSPSignalNotification& notif = (DSPSignalNotification&) cmd;
m_basebandSampleRate = notif.getSampleRate(); m_basebandSampleRate = notif.getSampleRate();
// Forward to the sink
DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy
qDebug() << "WFMDemod::handleMessage: DSPSignalNotification"; qDebug() << "WFMDemod::handleMessage: DSPSignalNotification";
m_basebandSink->getInputMessageQueue()->push(rep);
// Forward to the sink
if (m_running)
{
DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy
m_basebandSink->getInputMessageQueue()->push(rep);
}
// Forwatd to GUI if any // Forwatd to GUI if any
if (getMessageQueueToGUI()) { if (getMessageQueueToGUI()) {
getMessageQueueToGUI()->push(new DSPSignalNotification(notif)); getMessageQueueToGUI()->push(new DSPSignalNotification(notif));
@ -255,8 +282,11 @@ void WFMDemod::applySettings(const WFMDemodSettings& settings, bool force)
reverseAPIKeys.append("streamIndex"); reverseAPIKeys.append("streamIndex");
} }
WFMDemodBaseband::MsgConfigureWFMDemodBaseband *msg = WFMDemodBaseband::MsgConfigureWFMDemodBaseband::create(settings, force); if (m_running)
m_basebandSink->getInputMessageQueue()->push(msg); {
WFMDemodBaseband::MsgConfigureWFMDemodBaseband *msg = WFMDemodBaseband::MsgConfigureWFMDemodBaseband::create(settings, force);
m_basebandSink->getInputMessageQueue()->push(msg);
}
if (settings.m_useReverseAPI) if (settings.m_useReverseAPI)
{ {
@ -500,6 +530,10 @@ void WFMDemod::webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& resp
void WFMDemod::webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response) void WFMDemod::webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response)
{ {
if (!m_running) {
return;
}
double magsqAvg, magsqPeak; double magsqAvg, magsqPeak;
int nbMagsqSamples; int nbMagsqSamples;
getMagSqLevels(magsqAvg, magsqPeak, nbMagsqSamples); getMagSqLevels(magsqAvg, magsqPeak, nbMagsqSamples);
@ -645,7 +679,7 @@ void WFMDemod::networkManagerFinished(QNetworkReply *reply)
void WFMDemod::handleIndexInDeviceSetChanged(int index) void WFMDemod::handleIndexInDeviceSetChanged(int index)
{ {
if (index < 0) { if (!m_running || (index < 0)) {
return; return;
} }

View File

@ -92,11 +92,18 @@ public:
return m_settings.m_inputFrequencyOffset; return m_settings.m_inputFrequencyOffset;
} }
double getMagSq() const { return m_basebandSink->getMagSq(); } double getMagSq() const { return m_running ? m_basebandSink->getMagSq() : 0.0; }
bool getSquelchOpen() const { return m_basebandSink->getSquelchOpen(); } bool getSquelchOpen() const { return m_running && m_basebandSink->getSquelchOpen(); }
int getAudioSampleRate() const { return m_basebandSink->getAudioSampleRate(); } int getAudioSampleRate() const { return m_running ? m_basebandSink->getAudioSampleRate() : 0; }
void getMagSqLevels(double& avg, double& peak, int& nbSamples) { m_basebandSink->getMagSqLevels(avg, peak, nbSamples); } void getMagSqLevels(double& avg, double& peak, int& nbSamples)
{
if (m_running) {
m_basebandSink->getMagSqLevels(avg, peak, nbSamples);
} else {
avg = 0.0; peak = 0.0; nbSamples = 1;
}
}
virtual int webapiSettingsGet( virtual int webapiSettingsGet(
SWGSDRangel::SWGChannelSettings& response, SWGSDRangel::SWGChannelSettings& response,
@ -133,7 +140,8 @@ public:
private: private:
DeviceAPI* m_deviceAPI; DeviceAPI* m_deviceAPI;
QThread *m_thread; QThread *m_thread;
WFMDemodBaseband* m_basebandSink; WFMDemodBaseband *m_basebandSink;
bool m_running;
WFMDemodSettings m_settings; WFMDemodSettings 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