2016-10-03 13:55:16 +00:00
# include "threadedbasebandsamplesink.h"
2014-05-18 15:52:39 +00:00
# include <QThread>
2015-08-12 07:03:02 +00:00
# include <QDebug>
2015-08-18 00:47:14 +00:00
# include "dsp/dspcommands.h"
2014-05-18 15:52:39 +00:00
# include "util/message.h"
2016-10-14 16:47:19 +00:00
ThreadedBasebandSampleSinkFifo : : ThreadedBasebandSampleSinkFifo ( BasebandSampleSink * sampleSink , std : : size_t size ) :
2015-08-23 22:51:27 +00:00
m_sampleSink ( sampleSink )
{
connect ( & m_sampleFifo , SIGNAL ( dataReady ( ) ) , this , SLOT ( handleFifoData ( ) ) ) ;
m_sampleFifo . setSize ( size ) ;
}
2016-10-14 16:47:19 +00:00
ThreadedBasebandSampleSinkFifo : : ~ ThreadedBasebandSampleSinkFifo ( )
2015-08-23 22:51:27 +00:00
{
m_sampleFifo . readCommit ( m_sampleFifo . fill ( ) ) ;
}
2016-10-14 16:47:19 +00:00
void ThreadedBasebandSampleSinkFifo : : writeToFifo ( SampleVector : : const_iterator & begin , SampleVector : : const_iterator & end )
2015-08-23 22:51:27 +00:00
{
m_sampleFifo . write ( begin , end ) ;
}
2016-10-14 16:47:19 +00:00
void ThreadedBasebandSampleSinkFifo : : handleFifoData ( ) // FIXME: Fixed? Move it to the new threadable sink class
2015-10-15 02:05:19 +00: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 )
{
m_sampleSink - > feed ( part1begin , part1end , positiveOnly ) ;
}
m_sampleFifo . readCommit ( part1end - part1begin ) ;
}
// second part of FIFO data (used when block wraps around)
if ( part2begin ! = part2end )
{
// handle data
if ( m_sampleSink ! = NULL )
{
m_sampleSink - > feed ( part2begin , part2end , positiveOnly ) ;
}
m_sampleFifo . readCommit ( part2end - part2begin ) ;
}
}
}
2016-10-03 13:55:16 +00:00
ThreadedBasebandSampleSink : : ThreadedBasebandSampleSink ( BasebandSampleSink * sampleSink , QObject * parent ) :
2016-10-03 23:49:28 +00:00
m_basebandSampleSink ( sampleSink )
2014-05-18 15:52:39 +00:00
{
2016-10-08 08:35:07 +00:00
QString name = " ThreadedBasebandSampleSink( " + m_basebandSampleSink - > objectName ( ) + " ) " ;
2015-08-21 06:54:28 +00:00
setObjectName ( name ) ;
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " ThreadedBasebandSampleSink::ThreadedBasebandSampleSink: " < < name ;
2015-08-21 06:54:28 +00:00
m_thread = new QThread ( parent ) ;
2016-10-14 16:47:19 +00:00
m_threadedBasebandSampleSinkFifo = new ThreadedBasebandSampleSinkFifo ( m_basebandSampleSink ) ;
2015-08-23 22:51:27 +00:00
//moveToThread(m_thread); // FIXME: Fixed? the intermediate FIFO should be handled within the sink. Define a new type of sink that is compatible with threading
2016-10-03 23:49:28 +00:00
m_basebandSampleSink - > moveToThread ( m_thread ) ;
2016-10-14 16:47:19 +00:00
m_threadedBasebandSampleSinkFifo - > moveToThread ( m_thread ) ;
2018-03-06 01:23:47 +00:00
BasebandSampleSink : : MsgThreadedSink * msg = BasebandSampleSink : : MsgThreadedSink : : create ( m_thread ) ; // inform of the new thread
2018-03-06 07:40:46 +00:00
m_basebandSampleSink - > handleMessage ( * msg ) ;
delete msg ;
2015-08-23 22:51:27 +00:00
//m_sampleFifo.moveToThread(m_thread);
//connect(&m_sampleFifo, SIGNAL(dataReady()), this, SLOT(handleData()));
//m_sampleFifo.setSize(262144);
2015-08-21 06:54:28 +00:00
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " ThreadedBasebandSampleSink::ThreadedBasebandSampleSink: thread: " < < thread ( ) < < " m_thread: " < < m_thread ;
2015-08-17 06:29:34 +00:00
}
2014-05-18 15:52:39 +00:00
2016-10-03 13:55:16 +00:00
ThreadedBasebandSampleSink : : ~ ThreadedBasebandSampleSink ( )
2015-08-17 06:29:34 +00:00
{
2018-03-24 21:37:36 +00:00
if ( m_thread - > isRunning ( ) ) {
stop ( ) ;
}
delete m_threadedBasebandSampleSinkFifo ; // Valgrind memcheck
2015-08-21 06:54:28 +00:00
delete m_thread ;
2015-08-17 06:29:34 +00:00
}
2014-05-18 15:52:39 +00:00
2016-10-03 13:55:16 +00:00
void ThreadedBasebandSampleSink : : start ( )
2015-08-17 06:29:34 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " ThreadedBasebandSampleSink::start " ;
2015-08-21 06:54:28 +00:00
m_thread - > start ( ) ;
2016-10-03 23:49:28 +00:00
m_basebandSampleSink - > start ( ) ;
2015-08-17 06:29:34 +00:00
}
2014-05-18 15:52:39 +00:00
2016-10-03 13:55:16 +00:00
void ThreadedBasebandSampleSink : : stop ( )
2015-08-17 06:29:34 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " ThreadedBasebandSampleSink::stop " ;
2016-10-03 23:49:28 +00:00
m_basebandSampleSink - > stop ( ) ;
2015-08-21 06:54:28 +00:00
m_thread - > exit ( ) ;
m_thread - > wait ( ) ;
2014-05-18 15:52:39 +00:00
}
2018-11-12 15:58:20 +00:00
void ThreadedBasebandSampleSink : : feed ( SampleVector : : const_iterator begin , SampleVector : : const_iterator end , bool positiveOnly )
2014-05-18 15:52:39 +00:00
{
2018-11-12 15:58:20 +00:00
( void ) positiveOnly ;
2015-08-23 22:51:27 +00:00
//m_sampleSink->feed(begin, end, positiveOnly);
//m_sampleFifo.write(begin, end);
2016-10-14 16:47:19 +00:00
m_threadedBasebandSampleSinkFifo - > writeToFifo ( begin , end ) ;
2014-05-18 15:52:39 +00:00
}
2016-10-03 13:55:16 +00:00
bool ThreadedBasebandSampleSink : : handleSinkMessage ( const Message & cmd )
2014-05-18 15:52:39 +00:00
{
2016-10-03 23:49:28 +00:00
return m_basebandSampleSink - > handleMessage ( cmd ) ;
2014-05-18 15:52:39 +00:00
}
2016-10-03 13:55:16 +00:00
QString ThreadedBasebandSampleSink : : getSampleSinkObjectName ( ) const
2014-05-18 15:52:39 +00:00
{
2016-10-03 23:49:28 +00:00
return m_basebandSampleSink - > objectName ( ) ;
2014-05-18 15:52:39 +00:00
}