2016-10-03 09:55:16 -04:00
# include "threadedbasebandsamplesink.h"
2014-05-18 11:52:39 -04:00
# include <QThread>
2015-08-12 03:03:02 -04:00
# include <QDebug>
2015-08-17 20:47:14 -04:00
# include "dsp/dspcommands.h"
2014-05-18 11:52:39 -04:00
# include "util/message.h"
2016-10-14 12:47:19 -04:00
ThreadedBasebandSampleSinkFifo : : ThreadedBasebandSampleSinkFifo ( BasebandSampleSink * sampleSink , std : : size_t size ) :
2015-08-23 18:51:27 -04:00
m_sampleSink ( sampleSink )
{
connect ( & m_sampleFifo , SIGNAL ( dataReady ( ) ) , this , SLOT ( handleFifoData ( ) ) ) ;
m_sampleFifo . setSize ( size ) ;
}
2016-10-14 12:47:19 -04:00
ThreadedBasebandSampleSinkFifo : : ~ ThreadedBasebandSampleSinkFifo ( )
2015-08-23 18:51:27 -04:00
{
m_sampleFifo . readCommit ( m_sampleFifo . fill ( ) ) ;
}
2016-10-14 12:47:19 -04:00
void ThreadedBasebandSampleSinkFifo : : writeToFifo ( SampleVector : : const_iterator & begin , SampleVector : : const_iterator & end )
2015-08-23 18:51:27 -04:00
{
m_sampleFifo . write ( begin , end ) ;
}
2016-10-14 12:47:19 -04:00
void ThreadedBasebandSampleSinkFifo : : handleFifoData ( ) // FIXME: Fixed? Move it to the new threadable sink class
2015-10-14 22:05:19 -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 )
{
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 09:55:16 -04:00
ThreadedBasebandSampleSink : : ThreadedBasebandSampleSink ( BasebandSampleSink * sampleSink , QObject * parent ) :
2016-10-03 19:49:28 -04:00
m_basebandSampleSink ( sampleSink )
2014-05-18 11:52:39 -04:00
{
2016-10-08 04:35:07 -04:00
QString name = " ThreadedBasebandSampleSink( " + m_basebandSampleSink - > objectName ( ) + " ) " ;
2015-08-21 02:54:28 -04:00
setObjectName ( name ) ;
2016-10-08 04:35:07 -04:00
qDebug ( ) < < " ThreadedBasebandSampleSink::ThreadedBasebandSampleSink: " < < name ;
2015-08-21 02:54:28 -04:00
m_thread = new QThread ( parent ) ;
2016-10-14 12:47:19 -04:00
m_threadedBasebandSampleSinkFifo = new ThreadedBasebandSampleSinkFifo ( m_basebandSampleSink ) ;
2015-08-23 18:51:27 -04: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 19:49:28 -04:00
m_basebandSampleSink - > moveToThread ( m_thread ) ;
2016-10-14 12:47:19 -04:00
m_threadedBasebandSampleSinkFifo - > moveToThread ( m_thread ) ;
2018-03-05 20:23:47 -05:00
BasebandSampleSink : : MsgThreadedSink * msg = BasebandSampleSink : : MsgThreadedSink : : create ( m_thread ) ; // inform of the new thread
2018-03-06 02:40:46 -05:00
m_basebandSampleSink - > handleMessage ( * msg ) ;
delete msg ;
2015-08-23 18:51:27 -04:00
//m_sampleFifo.moveToThread(m_thread);
//connect(&m_sampleFifo, SIGNAL(dataReady()), this, SLOT(handleData()));
//m_sampleFifo.setSize(262144);
2015-08-21 02:54:28 -04:00
2016-10-08 04:35:07 -04:00
qDebug ( ) < < " ThreadedBasebandSampleSink::ThreadedBasebandSampleSink: thread: " < < thread ( ) < < " m_thread: " < < m_thread ;
2015-08-17 02:29:34 -04:00
}
2014-05-18 11:52:39 -04:00
2016-10-03 09:55:16 -04:00
ThreadedBasebandSampleSink : : ~ ThreadedBasebandSampleSink ( )
2015-08-17 02:29:34 -04:00
{
2016-10-14 12:47:19 -04:00
delete m_threadedBasebandSampleSinkFifo ; // Valgrind memcheck
2015-08-21 02:54:28 -04:00
delete m_thread ;
2015-08-17 02:29:34 -04:00
}
2014-05-18 11:52:39 -04:00
2016-10-03 09:55:16 -04:00
void ThreadedBasebandSampleSink : : start ( )
2015-08-17 02:29:34 -04:00
{
2016-10-08 04:35:07 -04:00
qDebug ( ) < < " ThreadedBasebandSampleSink::start " ;
2015-08-21 02:54:28 -04:00
m_thread - > start ( ) ;
2016-10-03 19:49:28 -04:00
m_basebandSampleSink - > start ( ) ;
2015-08-17 02:29:34 -04:00
}
2014-05-18 11:52:39 -04:00
2016-10-03 09:55:16 -04:00
void ThreadedBasebandSampleSink : : stop ( )
2015-08-17 02:29:34 -04:00
{
2016-10-08 04:35:07 -04:00
qDebug ( ) < < " ThreadedBasebandSampleSink::stop " ;
2016-10-03 19:49:28 -04:00
m_basebandSampleSink - > stop ( ) ;
2015-08-21 02:54:28 -04:00
m_thread - > exit ( ) ;
m_thread - > wait ( ) ;
2014-05-18 11:52:39 -04:00
}
2017-05-25 14:13:34 -04:00
void ThreadedBasebandSampleSink : : feed ( SampleVector : : const_iterator begin , SampleVector : : const_iterator end , bool positiveOnly __attribute__ ( ( unused ) ) )
2014-05-18 11:52:39 -04:00
{
2015-08-23 18:51:27 -04:00
//m_sampleSink->feed(begin, end, positiveOnly);
//m_sampleFifo.write(begin, end);
2016-10-14 12:47:19 -04:00
m_threadedBasebandSampleSinkFifo - > writeToFifo ( begin , end ) ;
2014-05-18 11:52:39 -04:00
}
2016-10-03 09:55:16 -04:00
bool ThreadedBasebandSampleSink : : handleSinkMessage ( const Message & cmd )
2014-05-18 11:52:39 -04:00
{
2016-10-03 19:49:28 -04:00
return m_basebandSampleSink - > handleMessage ( cmd ) ;
2014-05-18 11:52:39 -04:00
}
2016-10-03 09:55:16 -04:00
QString ThreadedBasebandSampleSink : : getSampleSinkObjectName ( ) const
2014-05-18 11:52:39 -04:00
{
2016-10-03 19:49:28 -04:00
return m_basebandSampleSink - > objectName ( ) ;
2014-05-18 11:52:39 -04:00
}