1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-23 10:05:46 -05:00
sdrangel/sdrbase/dsp/threadedsamplesink.cpp

109 lines
2.6 KiB
C++
Raw Normal View History

#include <QThread>
#include "dsp/threadedsamplesink.h"
#include "util/message.h"
ThreadedSampleSink::ThreadedSampleSink(SampleSink* sampleSink) :
m_thread(new QThread),
m_sampleSink(sampleSink)
{
moveToThread(m_thread);
connect(m_thread, SIGNAL(started()), this, SLOT(threadStarted()));
connect(m_thread, SIGNAL(finished()), this, SLOT(threadFinished()));
m_messageQueue.moveToThread(m_thread);
connect(&m_messageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleMessages()));
m_sampleFifo.moveToThread(m_thread);
connect(&m_sampleFifo, SIGNAL(dataReady()), this, SLOT(handleData()));
m_sampleFifo.setSize(262144);
sampleSink->moveToThread(m_thread);
}
ThreadedSampleSink::~ThreadedSampleSink()
{
m_thread->exit();
m_thread->wait();
delete m_thread;
}
2014-06-15 04:32:25 -04:00
void ThreadedSampleSink::feed(SampleVector::const_iterator begin, SampleVector::const_iterator end, bool positiveOnly)
{
2014-06-15 04:32:25 -04:00
Q_UNUSED(positiveOnly);
m_sampleFifo.write(begin, end);
}
void ThreadedSampleSink::start()
{
m_thread->start();
}
void ThreadedSampleSink::stop()
{
m_thread->exit();
m_thread->wait();
m_sampleFifo.readCommit(m_sampleFifo.fill());
}
bool ThreadedSampleSink::handleMessage(Message* cmd)
{
// called from other thread
m_messageQueue.submit(cmd);
return true;
}
void ThreadedSampleSink::handleData()
{
2014-06-15 04:32:25 -04:00
bool positiveOnly = false;
while((m_sampleFifo.fill() > 0) && (m_messageQueue.countPending() == 0)) {
SampleVector::iterator part1begin;
SampleVector::iterator part1end;
SampleVector::iterator part2begin;
SampleVector::iterator part2end;
size_t count = m_sampleFifo.readBegin(m_sampleFifo.fill(), &part1begin, &part1end, &part2begin, &part2end);
// first part of FIFO data
2014-11-21 10:41:58 -05:00
if(count > 0) {
// handle data
if(m_sampleSink != NULL)
2014-06-15 04:32:25 -04:00
m_sampleSink->feed(part1begin, part1end, positiveOnly);
2014-07-03 04:45:14 -04:00
m_sampleFifo.readCommit(part1end - part1begin);
}
// second part of FIFO data (used when block wraps around)
if(part2begin != part2end) {
// handle data
if(m_sampleSink != NULL)
2014-06-15 04:32:25 -04:00
m_sampleSink->feed(part2begin, part2end, positiveOnly);
2014-07-03 04:45:14 -04:00
m_sampleFifo.readCommit(part2end - part2begin);
}
}
}
void ThreadedSampleSink::handleMessages()
{
Message* message;
while((message = m_messageQueue.accept()) != NULL) {
qDebug("CMD: %s", message->getIdentifier());
if(m_sampleSink != NULL) {
if(!m_sampleSink->handleMessage(message))
message->completed();
} else {
message->completed();
}
}
}
void ThreadedSampleSink::threadStarted()
{
if(m_sampleSink != NULL)
m_sampleSink->start();
}
void ThreadedSampleSink::threadFinished()
{
if(m_sampleSink != NULL)
m_sampleSink->stop();
}