2014-05-18 11:52:39 -04:00
# include <QThread>
2015-08-12 03:03:02 -04:00
# include <QDebug>
2014-05-18 11:52:39 -04:00
# include "dsp/threadedsamplesink.h"
2015-08-17 20:47:14 -04:00
# include "dsp/dspcommands.h"
2014-05-18 11:52:39 -04:00
# include "util/message.h"
2015-08-23 18:51:27 -04:00
ThreadedSampleFifo : : ThreadedSampleFifo ( SampleSink * sampleSink , std : : size_t size ) :
m_sampleSink ( sampleSink )
{
connect ( & m_sampleFifo , SIGNAL ( dataReady ( ) ) , this , SLOT ( handleFifoData ( ) ) ) ;
m_sampleFifo . setSize ( size ) ;
}
ThreadedSampleFifo : : ~ ThreadedSampleFifo ( )
{
m_sampleFifo . readCommit ( m_sampleFifo . fill ( ) ) ;
}
void ThreadedSampleFifo : : writeToFifo ( SampleVector : : const_iterator & begin , SampleVector : : const_iterator & end )
{
m_sampleFifo . write ( begin , end ) ;
}
2015-10-14 22:05:19 -04:00
void ThreadedSampleFifo : : handleFifoData ( ) // FIXME: Fixed? Move it to the new threadable sink class
{
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 ) ;
}
}
}
2015-08-21 02:54:28 -04:00
ThreadedSampleSink : : ThreadedSampleSink ( SampleSink * sampleSink , QObject * parent ) :
2014-05-18 11:52:39 -04:00
m_sampleSink ( sampleSink )
{
2015-08-21 02:54:28 -04:00
QString name = " ThreadedSampleSink( " + m_sampleSink - > objectName ( ) + " ) " ;
setObjectName ( name ) ;
qDebug ( ) < < " ThreadedSampleSink::ThreadedSampleSink: " < < name ;
m_thread = new QThread ( parent ) ;
2015-08-23 18:51:27 -04:00
m_threadedSampleFifo = new ThreadedSampleFifo ( m_sampleSink ) ;
//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
2015-08-21 02:54:28 -04:00
m_sampleSink - > moveToThread ( m_thread ) ;
2015-08-23 18:51:27 -04:00
m_threadedSampleFifo - > moveToThread ( m_thread ) ;
//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
qDebug ( ) < < " ThreadedSampleSink::ThreadedSampleSink: thread: " < < thread ( ) < < " m_thread: " < < m_thread ;
2015-08-17 02:29:34 -04:00
}
2014-05-18 11:52:39 -04:00
2015-08-17 02:29:34 -04:00
ThreadedSampleSink : : ~ ThreadedSampleSink ( )
{
2015-08-28 21:09:44 -04:00
delete m_threadedSampleFifo ; // 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
2015-08-17 02:29:34 -04:00
void ThreadedSampleSink : : start ( )
{
2015-08-17 20:47:14 -04:00
qDebug ( ) < < " ThreadedSampleSink::start " ;
2015-08-21 02:54:28 -04:00
m_thread - > start ( ) ;
m_sampleSink - > start ( ) ;
2015-08-17 02:29:34 -04:00
}
2014-05-18 11:52:39 -04:00
2015-08-17 02:29:34 -04:00
void ThreadedSampleSink : : stop ( )
{
2015-08-17 20:47:14 -04:00
qDebug ( ) < < " ThreadedSampleSink::stop " ;
2015-08-21 02:54:28 -04:00
m_sampleSink - > stop ( ) ;
m_thread - > exit ( ) ;
m_thread - > wait ( ) ;
2014-05-18 11:52:39 -04:00
}
2015-08-19 21:38:31 -04:00
void ThreadedSampleSink : : feed ( SampleVector : : const_iterator begin , SampleVector : : const_iterator end , bool positiveOnly )
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);
m_threadedSampleFifo - > writeToFifo ( begin , end ) ;
2014-05-18 11:52:39 -04:00
}
2015-12-09 19:56:38 -05:00
bool ThreadedSampleSink : : handleSinkMessage ( const Message & cmd )
2014-05-18 11:52:39 -04:00
{
2015-08-21 02:54:28 -04:00
return m_sampleSink - > handleMessage ( cmd ) ;
2014-05-18 11:52:39 -04:00
}
2015-08-17 02:29:34 -04:00
QString ThreadedSampleSink : : getSampleSinkObjectName ( ) const
2014-05-18 11:52:39 -04:00
{
2015-08-17 02:29:34 -04:00
return m_sampleSink - > objectName ( ) ;
2014-05-18 11:52:39 -04:00
}