diff --git a/plugins/channelrx/demodatv/atvdemod.cpp b/plugins/channelrx/demodatv/atvdemod.cpp index 7ce1a9eb2..82c8d368f 100644 --- a/plugins/channelrx/demodatv/atvdemod.cpp +++ b/plugins/channelrx/demodatv/atvdemod.cpp @@ -18,7 +18,6 @@ #include #include -#include #include #include @@ -42,9 +41,8 @@ ATVDemod::ATVDemod(DeviceAPI *deviceAPI) : qDebug("ATVDemod::ATVDemod"); setObjectName(m_channelId); - m_thread = new QThread(this); m_basebandSink = new ATVDemodBaseband(); - m_basebandSink->moveToThread(m_thread); + m_basebandSink->moveToThread(&m_thread); applySettings(m_settings, true); @@ -57,8 +55,12 @@ ATVDemod::~ATVDemod() qDebug("ATVDemod::~ATVDemod"); m_deviceAPI->removeChannelSinkAPI(this); m_deviceAPI->removeChannelSink(this); + + if (m_basebandSink->isRunning()) { + stop(); + } + delete m_basebandSink; - delete m_thread; } void ATVDemod::start() @@ -66,7 +68,8 @@ void ATVDemod::start() qDebug("ATVDemod::start"); m_basebandSink->reset(); - m_thread->start(); + m_basebandSink->startWork(); + m_thread.start(); // re-apply essential messages @@ -80,8 +83,9 @@ void ATVDemod::start() void ATVDemod::stop() { qDebug("ATVDemod::stop"); - m_thread->exit(); - m_thread->wait(); + m_basebandSink->stopWork(); + m_thread.exit(); + m_thread.wait(); } void ATVDemod::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool firstOfBurst) diff --git a/plugins/channelrx/demodatv/atvdemod.h b/plugins/channelrx/demodatv/atvdemod.h index 95133c5f3..97827c24e 100644 --- a/plugins/channelrx/demodatv/atvdemod.h +++ b/plugins/channelrx/demodatv/atvdemod.h @@ -20,6 +20,7 @@ #define INCLUDE_ATVDEMOD_H #include +#include #include #include "dsp/basebandsamplesink.h" @@ -30,7 +31,6 @@ #include "atvdemodbaseband.h" -class QThread; class DeviceAPI; class ATVDemod : public BasebandSampleSink, public ChannelAPI @@ -97,7 +97,7 @@ public: private: DeviceAPI* m_deviceAPI; - QThread *m_thread; + QThread m_thread; ATVDemodBaseband* m_basebandSink; ATVDemodSettings m_settings; qint64 m_centerFrequency; //!< center frequency stored from device message used when starting baseband sink diff --git a/plugins/channelrx/demodatv/atvdemodbaseband.cpp b/plugins/channelrx/demodatv/atvdemodbaseband.cpp index 2cf5b0e61..b310c557d 100644 --- a/plugins/channelrx/demodatv/atvdemodbaseband.cpp +++ b/plugins/channelrx/demodatv/atvdemodbaseband.cpp @@ -26,21 +26,12 @@ MESSAGE_CLASS_DEFINITION(ATVDemodBaseband::MsgConfigureATVDemodBaseband, Message) ATVDemodBaseband::ATVDemodBaseband() : + m_running(false), m_mutex(QMutex::Recursive) { qDebug("ATVDemodBaseband::ATVDemodBaseband"); m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(48000)); m_channelizer = new DownChannelizer(&m_sink); - - QObject::connect( - &m_sampleFifo, - &SampleSinkFifo::dataReady, - this, - &ATVDemodBaseband::handleData, - Qt::QueuedConnection - ); - - connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); } ATVDemodBaseband::~ATVDemodBaseband() @@ -55,6 +46,34 @@ void ATVDemodBaseband::reset() m_sampleFifo.reset(); } +void ATVDemodBaseband::startWork() +{ + QMutexLocker mutexLocker(&m_mutex); + QObject::connect( + &m_sampleFifo, + &SampleSinkFifo::dataReady, + this, + &ATVDemodBaseband::handleData, + Qt::QueuedConnection + ); + + connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); + m_running = true; +} + +void ATVDemodBaseband::stopWork() +{ + QMutexLocker mutexLocker(&m_mutex); + disconnect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); + QObject::disconnect( + &m_sampleFifo, + &SampleSinkFifo::dataReady, + this, + &ATVDemodBaseband::handleData + ); + m_running = false; +} + void ATVDemodBaseband::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end) { m_sampleFifo.write(begin, end); diff --git a/plugins/channelrx/demodatv/atvdemodbaseband.h b/plugins/channelrx/demodatv/atvdemodbaseband.h index a154a5a83..e9598f1b7 100644 --- a/plugins/channelrx/demodatv/atvdemodbaseband.h +++ b/plugins/channelrx/demodatv/atvdemodbaseband.h @@ -59,6 +59,8 @@ public: ATVDemodBaseband(); ~ATVDemodBaseband(); 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; @@ -68,6 +70,7 @@ public: bool getBFOLocked() { return m_sink.getBFOLocked(); } void setVideoTabIndex(int videoTabIndex) { m_sink.setVideoTabIndex(videoTabIndex); } void setBasebandSampleRate(int sampleRate); //!< To be used when supporting thread is stopped + bool isRunning() const { return m_running; } private: SampleSinkFifo m_sampleFifo; @@ -75,6 +78,7 @@ private: ATVDemodSink m_sink; MessageQueue m_inputMessageQueue; //!< Queue for asynchronous inbound communication ATVDemodSettings m_settings; + bool m_running; QMutex m_mutex; bool handleMessage(const Message& cmd);