sdrangel/sdrbase/dsp/threadedsamplesink.cpp

106 lines
2.6 KiB
C++
Raw Normal View History

#include <QThread>
#include <QDebug>
#include <QApplication>
#include "dsp/threadedsamplesink.h"
#include "dsp/dspcommands.h"
#include "util/message.h"
ThreadedSampleSink::ThreadedSampleSink(SampleSink* sampleSink, QObject *parent) :
m_sampleSink(sampleSink)
{
QString name = "ThreadedSampleSink(" + m_sampleSink->objectName() + ")";
setObjectName(name);
qDebug() << "ThreadedSampleSink::ThreadedSampleSink: " << name;
m_thread = new QThread(parent);
moveToThread(m_thread); // FIXME: the intermediate FIFO should be handled within the sink. Define a new type of sink that is compatible with threading
m_sampleSink->moveToThread(m_thread);
m_sampleFifo.moveToThread(m_thread);
connect(&m_sampleFifo, SIGNAL(dataReady()), this, SLOT(handleData()));
m_sampleFifo.setSize(262144);
qDebug() << "ThreadedSampleSink::ThreadedSampleSink: thread: " << thread() << " m_thread: " << m_thread;
2015-08-17 02:29:34 -04:00
}
2015-08-17 02:29:34 -04:00
ThreadedSampleSink::~ThreadedSampleSink()
{
delete m_thread;
2015-08-17 02:29:34 -04:00
}
2015-08-17 02:29:34 -04:00
void ThreadedSampleSink::start()
{
qDebug() << "ThreadedSampleSink::start";
m_thread->start();
m_sampleSink->start();
2015-08-17 02:29:34 -04:00
}
2015-08-17 02:29:34 -04:00
void ThreadedSampleSink::stop()
{
qDebug() << "ThreadedSampleSink::stop";
m_sampleSink->stop();
m_thread->exit();
m_thread->wait();
m_sampleFifo.readCommit(m_sampleFifo.fill());
}
void ThreadedSampleSink::feed(SampleVector::const_iterator begin, SampleVector::const_iterator end, bool positiveOnly)
{
// m_sampleSink->feed(begin, end, positiveOnly);
m_sampleFifo.write(begin, end);
}
bool ThreadedSampleSink::handleSinkMessage(Message& cmd)
{
return m_sampleSink->handleMessage(cmd);
}
2015-08-17 02:29:34 -04:00
QString ThreadedSampleSink::getSampleSinkObjectName() const
{
2015-08-17 02:29:34 -04:00
return m_sampleSink->objectName();
}
2015-08-17 02:29:34 -04:00
void ThreadedSampleSink::handleData() // FIXME: Move it to the new threadable sink class
{
2014-06-15 04:32:25 -04:00
bool positiveOnly = false;
while ((m_sampleFifo.fill() > 0) && (m_sampleSink->getInputMessageQueue()->size() == 0))
{
SampleVector::iterator part1begin;
SampleVector::iterator part1end;
SampleVector::iterator part2begin;
SampleVector::iterator part2end;
std::size_t count = m_sampleFifo.readBegin(m_sampleFifo.fill(), &part1begin, &part1end, &part2begin, &part2end);
// first part of FIFO data
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);
}
}
}