SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Classes | Public Member Functions | Private Slots | Private Member Functions | Private Attributes | List of all members
FileInputThread Class Reference

#include <fileinputthread.h>

Inherits QThread.

+ Collaboration diagram for FileInputThread:

Classes

class  MsgReportEOF
 

Public Member Functions

 FileInputThread (std::ifstream *samplesStream, SampleSinkFifo *sampleFifo, const QTimer &timer, MessageQueue *fileInputMessageQueue, QObject *parent=NULL)
 
 ~FileInputThread ()
 
void startWork ()
 
void stopWork ()
 
void setSampleRateAndSize (int samplerate, quint32 samplesize)
 
void setBuffers (std::size_t chunksize)
 
bool isRunning () const
 
quint64 getSamplesCount () const
 
void setSamplesCount (quint64 samplesCount)
 

Private Slots

void tick ()
 

Private Member Functions

void run ()
 
void writeToSampleFifo (const quint8 *buf, qint32 nbBytes)
 

Private Attributes

QMutex m_startWaitMutex
 
QWaitCondition m_startWaiter
 
volatile bool m_running
 
std::ifstream * m_ifstream
 
quint8 * m_fileBuf
 
quint8 * m_convertBuf
 
std::size_t m_bufsize
 
qint64 m_chunksize
 
SampleSinkFifom_sampleFifo
 
quint64 m_samplesCount
 
const QTimer & m_timer
 
MessageQueuem_fileInputMessageQueue
 
int m_samplerate
 File I/Q stream original sample rate. More...
 
quint64 m_samplesize
 File effective sample size in bits (I or Q). Ex: 16, 24. More...
 
quint64 m_samplebytes
 Number of bytes used to store a I or Q sample. Ex: 2. 4. More...
 
qint64 m_throttlems
 
QElapsedTimer m_elapsedTimer
 
bool m_throttleToggle
 

Detailed Description

Definition at line 38 of file fileinputthread.h.

Constructor & Destructor Documentation

◆ FileInputThread()

FileInputThread::FileInputThread ( std::ifstream *  samplesStream,
SampleSinkFifo sampleFifo,
const QTimer &  timer,
MessageQueue fileInputMessageQueue,
QObject *  parent = NULL 
)

Definition at line 30 of file fileinputthread.cpp.

Referenced by FileInputThread::MsgReportEOF::MsgReportEOF().

34  :
35  QThread(parent),
36  m_running(false),
37  m_ifstream(samplesStream),
38  m_fileBuf(0),
39  m_convertBuf(0),
40  m_bufsize(0),
41  m_chunksize(0),
42  m_sampleFifo(sampleFifo),
43  m_samplesCount(0),
44  m_timer(timer),
45  m_fileInputMessageQueue(fileInputMessageQueue),
46  m_samplerate(0),
47  m_samplesize(0),
48  m_samplebytes(0),
50  m_throttleToggle(false)
51 {
52  assert(m_ifstream != 0);
53 }
std::size_t m_bufsize
volatile bool m_running
quint64 m_samplesize
File effective sample size in bits (I or Q). Ex: 16, 24.
#define FILESOURCE_THROTTLE_MS
quint8 * m_convertBuf
const QTimer & m_timer
quint64 m_samplebytes
Number of bytes used to store a I or Q sample. Ex: 2. 4.
std::ifstream * m_ifstream
MessageQueue * m_fileInputMessageQueue
quint64 m_samplesCount
SampleSinkFifo * m_sampleFifo
int m_samplerate
File I/Q stream original sample rate.
+ Here is the caller graph for this function:

◆ ~FileInputThread()

FileInputThread::~FileInputThread ( )

Definition at line 55 of file fileinputthread.cpp.

References m_convertBuf, m_fileBuf, m_running, and stopWork().

Referenced by FileInputThread::MsgReportEOF::MsgReportEOF().

56 {
57  if (m_running) {
58  stopWork();
59  }
60 
61  if (m_fileBuf != 0) {
62  free(m_fileBuf);
63  }
64 
65  if (m_convertBuf != 0) {
66  free(m_convertBuf);
67  }
68 }
volatile bool m_running
quint8 * m_convertBuf
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Function Documentation

◆ getSamplesCount()

quint64 FileInputThread::getSamplesCount ( ) const
inline

Definition at line 68 of file fileinputthread.h.

References m_samplesCount.

Referenced by FileInput::handleMessage(), and FileInput::webapiFormatDeviceReport().

68 { return m_samplesCount; }
quint64 m_samplesCount
+ Here is the caller graph for this function:

◆ isRunning()

bool FileInputThread::isRunning ( ) const
inline

Definition at line 67 of file fileinputthread.h.

References m_running.

Referenced by FileInput::seekFileStream().

67 { return m_running; }
volatile bool m_running
+ Here is the caller graph for this function:

◆ run()

void FileInputThread::run ( )
private

Definition at line 162 of file fileinputthread.cpp.

References m_running, and m_startWaiter.

163 {
164  m_running = true;
165  m_startWaiter.wakeAll();
166 
167  while(m_running) // actual work is in the tick() function
168  {
169  sleep(1);
170  }
171 
172  m_running = false;
173 }
volatile bool m_running
QWaitCondition m_startWaiter

◆ setBuffers()

void FileInputThread::setBuffers ( std::size_t  chunksize)

Definition at line 124 of file fileinputthread.cpp.

References m_bufsize, m_convertBuf, m_fileBuf, and m_samplebytes.

Referenced by FileInputThread::MsgReportEOF::MsgReportEOF(), setSampleRateAndSize(), and tick().

125 {
126  if (chunksize > m_bufsize)
127  {
128  m_bufsize = chunksize;
129  int nbSamples = m_bufsize/(2 * m_samplebytes);
130 
131  if (m_fileBuf == 0)
132  {
133  qDebug() << "FileInputThread::setBuffers: Allocate file buffer";
134  m_fileBuf = (quint8*) malloc(m_bufsize);
135  }
136  else
137  {
138  qDebug() << "FileInputThread::setBuffers: Re-allocate file buffer";
139  quint8 *buf = m_fileBuf;
140  m_fileBuf = (quint8*) realloc((void*) m_fileBuf, m_bufsize);
141  if (!m_fileBuf) free(buf);
142  }
143 
144  if (m_convertBuf == 0)
145  {
146  qDebug() << "FileInputThread::setBuffers: Allocate conversion buffer";
147  m_convertBuf = (quint8*) malloc(nbSamples*sizeof(Sample));
148  }
149  else
150  {
151  qDebug() << "FileInputThread::setBuffers: Re-allocate conversion buffer";
152  quint8 *buf = m_convertBuf;
153  m_convertBuf = (quint8*) realloc((void*) m_convertBuf, nbSamples*sizeof(Sample));
154  if (!m_convertBuf) free(buf);
155  }
156 
157  qDebug() << "FileInputThread::setBuffers: size: " << m_bufsize
158  << " #samples: " << nbSamples;
159  }
160 }
std::size_t m_bufsize
quint8 * m_convertBuf
quint64 m_samplebytes
Number of bytes used to store a I or Q sample. Ex: 2. 4.
+ Here is the caller graph for this function:

◆ setSampleRateAndSize()

void FileInputThread::setSampleRateAndSize ( int  samplerate,
quint32  samplesize 
)

Definition at line 99 of file fileinputthread.cpp.

References m_chunksize, m_running, m_samplebytes, m_samplerate, m_samplesize, m_throttlems, setBuffers(), and stopWork().

Referenced by FileInput::applySettings(), FileInputThread::MsgReportEOF::MsgReportEOF(), and FileInput::start().

100 {
101  qDebug() << "FileInputThread::setSampleRateAndSize:"
102  << " new rate:" << samplerate
103  << " new size:" << samplesize
104  << " old rate:" << m_samplerate
105  << " old size:" << m_samplesize;
106 
107  if ((samplerate != m_samplerate) || (samplesize != m_samplesize))
108  {
109  if (m_running) {
110  stopWork();
111  }
112 
113  m_samplerate = samplerate;
114  m_samplesize = samplesize;
115  m_samplebytes = m_samplesize > 16 ? sizeof(int32_t) : sizeof(int16_t);
117 
119  }
120 
121  //m_samplerate = samplerate;
122 }
short int16_t
Definition: rtptypes_win.h:43
volatile bool m_running
quint64 m_samplesize
File effective sample size in bits (I or Q). Ex: 16, 24.
quint64 m_samplebytes
Number of bytes used to store a I or Q sample. Ex: 2. 4.
int int32_t
Definition: rtptypes_win.h:45
void setBuffers(std::size_t chunksize)
int m_samplerate
File I/Q stream original sample rate.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setSamplesCount()

void FileInputThread::setSamplesCount ( quint64  samplesCount)
inline

Definition at line 69 of file fileinputthread.h.

References m_samplesCount.

Referenced by FileInput::seekFileStream().

69 { m_samplesCount = samplesCount; }
quint64 m_samplesCount
+ Here is the caller graph for this function:

◆ startWork()

void FileInputThread::startWork ( )

Definition at line 70 of file fileinputthread.cpp.

References m_elapsedTimer, m_ifstream, m_running, m_startWaiter, m_startWaitMutex, m_timer, and tick().

Referenced by FileInput::handleMessage(), FileInputThread::MsgReportEOF::MsgReportEOF(), and FileInput::start().

71 {
72  qDebug() << "FileInputThread::startWork: ";
73 
74  if (m_ifstream->is_open())
75  {
76  qDebug() << "FileInputThread::startWork: file stream open, starting...";
77  m_startWaitMutex.lock();
78  m_elapsedTimer.start();
79  start();
80  while(!m_running)
81  m_startWaiter.wait(&m_startWaitMutex, 100);
82  m_startWaitMutex.unlock();
83  connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
84  }
85  else
86  {
87  qDebug() << "FileInputThread::startWork: file stream closed, not starting.";
88  }
89 }
volatile bool m_running
QWaitCondition m_startWaiter
const QTimer & m_timer
std::ifstream * m_ifstream
QElapsedTimer m_elapsedTimer
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ stopWork()

void FileInputThread::stopWork ( )

Definition at line 91 of file fileinputthread.cpp.

References m_running, m_timer, and tick().

Referenced by FileInput::handleMessage(), FileInputThread::MsgReportEOF::MsgReportEOF(), setSampleRateAndSize(), FileInput::stop(), and ~FileInputThread().

92 {
93  qDebug() << "FileInputThread::stopWork";
94  disconnect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
95  m_running = false;
96  wait();
97 }
volatile bool m_running
const QTimer & m_timer
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tick

void FileInputThread::tick ( )
privateslot

Definition at line 175 of file fileinputthread.cpp.

References FileInputThread::MsgReportEOF::create(), m_chunksize, m_elapsedTimer, m_fileBuf, m_fileInputMessageQueue, m_ifstream, m_running, m_samplebytes, m_samplerate, m_samplesCount, m_throttlems, m_throttleToggle, MessageQueue::push(), setBuffers(), and writeToSampleFifo().

Referenced by startWork(), and stopWork().

176 {
177  if (m_running)
178  {
179  qint64 throttlems = m_elapsedTimer.restart();
180 
181  if (throttlems != m_throttlems)
182  {
183  m_throttlems = throttlems;
184  m_chunksize = 2 * m_samplebytes * ((m_samplerate * (m_throttlems+(m_throttleToggle ? 1 : 0))) / 1000);
187  }
188 
189  // read samples directly feeding the SampleFifo (no callback)
190  m_ifstream->read(reinterpret_cast<char*>(m_fileBuf), m_chunksize);
191 
192  if (m_ifstream->eof())
193  {
194  writeToSampleFifo(m_fileBuf, (qint32) m_ifstream->gcount());
195  MsgReportEOF *message = MsgReportEOF::create();
196  m_fileInputMessageQueue->push(message);
197  }
198  else
199  {
202  }
203  }
204 }
void push(Message *message, bool emitSignal=true)
Push message onto queue.
volatile bool m_running
void writeToSampleFifo(const quint8 *buf, qint32 nbBytes)
quint64 m_samplebytes
Number of bytes used to store a I or Q sample. Ex: 2. 4.
std::ifstream * m_ifstream
MessageQueue * m_fileInputMessageQueue
quint64 m_samplesCount
void setBuffers(std::size_t chunksize)
static MsgReportEOF * create()
QElapsedTimer m_elapsedTimer
int m_samplerate
File I/Q stream original sample rate.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeToSampleFifo()

void FileInputThread::writeToSampleFifo ( const quint8 *  buf,
qint32  nbBytes 
)
private

Definition at line 206 of file fileinputthread.cpp.

References m_convertBuf, m_samplebytes, m_sampleFifo, m_samplesize, SDR_RX_SAMP_SZ, and SampleSinkFifo::write().

Referenced by tick().

207 {
208  if (m_samplesize == 16)
209  {
210  if (SDR_RX_SAMP_SZ == 16)
211  {
212  m_sampleFifo->write(buf, nbBytes);
213  }
214  else if (SDR_RX_SAMP_SZ == 24)
215  {
216  FixReal *convertBuf = (FixReal *) m_convertBuf;
217  const int16_t *fileBuf = (int16_t *) buf;
218  int nbSamples = nbBytes / (2 * m_samplebytes);
219 
220  for (int is = 0; is < nbSamples; is++)
221  {
222  convertBuf[2*is] = fileBuf[2*is] << 8;
223  convertBuf[2*is+1] = fileBuf[2*is+1] << 8;
224  }
225 
226  m_sampleFifo->write((quint8*) convertBuf, nbSamples*sizeof(Sample));
227  }
228  }
229  else if (m_samplesize == 24)
230  {
231  if (SDR_RX_SAMP_SZ == 24)
232  {
233  m_sampleFifo->write(buf, nbBytes);
234  }
235  else if (SDR_RX_SAMP_SZ == 16)
236  {
237  FixReal *convertBuf = (FixReal *) m_convertBuf;
238  const int32_t *fileBuf = (int32_t *) buf;
239  int nbSamples = nbBytes / (2 * m_samplebytes);
240 
241  for (int is = 0; is < nbSamples; is++)
242  {
243  convertBuf[2*is] = fileBuf[2*is] >> 8;
244  convertBuf[2*is+1] = fileBuf[2*is+1] >> 8;
245  }
246 
247  m_sampleFifo->write((quint8*) convertBuf, nbSamples*sizeof(Sample));
248  }
249  }
250 }
short int16_t
Definition: rtptypes_win.h:43
uint write(const quint8 *data, uint count)
quint64 m_samplesize
File effective sample size in bits (I or Q). Ex: 16, 24.
#define SDR_RX_SAMP_SZ
Definition: dsptypes.h:32
quint8 * m_convertBuf
quint64 m_samplebytes
Number of bytes used to store a I or Q sample. Ex: 2. 4.
int int32_t
Definition: rtptypes_win.h:45
SampleSinkFifo * m_sampleFifo
qint16 FixReal
Definition: dsptypes.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_bufsize

std::size_t FileInputThread::m_bufsize
private

Definition at line 79 of file fileinputthread.h.

Referenced by setBuffers().

◆ m_chunksize

qint64 FileInputThread::m_chunksize
private

Definition at line 80 of file fileinputthread.h.

Referenced by setSampleRateAndSize(), and tick().

◆ m_convertBuf

quint8* FileInputThread::m_convertBuf
private

Definition at line 78 of file fileinputthread.h.

Referenced by setBuffers(), writeToSampleFifo(), and ~FileInputThread().

◆ m_elapsedTimer

QElapsedTimer FileInputThread::m_elapsedTimer
private

Definition at line 90 of file fileinputthread.h.

Referenced by startWork(), and tick().

◆ m_fileBuf

quint8* FileInputThread::m_fileBuf
private

Definition at line 77 of file fileinputthread.h.

Referenced by setBuffers(), tick(), and ~FileInputThread().

◆ m_fileInputMessageQueue

MessageQueue* FileInputThread::m_fileInputMessageQueue
private

Definition at line 84 of file fileinputthread.h.

Referenced by tick().

◆ m_ifstream

std::ifstream* FileInputThread::m_ifstream
private

Definition at line 76 of file fileinputthread.h.

Referenced by startWork(), and tick().

◆ m_running

volatile bool FileInputThread::m_running
private

◆ m_samplebytes

quint64 FileInputThread::m_samplebytes
private

Number of bytes used to store a I or Q sample. Ex: 2. 4.

Definition at line 88 of file fileinputthread.h.

Referenced by setBuffers(), setSampleRateAndSize(), tick(), and writeToSampleFifo().

◆ m_sampleFifo

SampleSinkFifo* FileInputThread::m_sampleFifo
private

Definition at line 81 of file fileinputthread.h.

Referenced by writeToSampleFifo().

◆ m_samplerate

int FileInputThread::m_samplerate
private

File I/Q stream original sample rate.

Definition at line 86 of file fileinputthread.h.

Referenced by setSampleRateAndSize(), and tick().

◆ m_samplesCount

quint64 FileInputThread::m_samplesCount
private

Definition at line 82 of file fileinputthread.h.

Referenced by getSamplesCount(), setSamplesCount(), and tick().

◆ m_samplesize

quint64 FileInputThread::m_samplesize
private

File effective sample size in bits (I or Q). Ex: 16, 24.

Definition at line 87 of file fileinputthread.h.

Referenced by setSampleRateAndSize(), and writeToSampleFifo().

◆ m_startWaiter

QWaitCondition FileInputThread::m_startWaiter
private

Definition at line 73 of file fileinputthread.h.

Referenced by run(), and startWork().

◆ m_startWaitMutex

QMutex FileInputThread::m_startWaitMutex
private

Definition at line 72 of file fileinputthread.h.

Referenced by startWork().

◆ m_throttlems

qint64 FileInputThread::m_throttlems
private

Definition at line 89 of file fileinputthread.h.

Referenced by setSampleRateAndSize(), and tick().

◆ m_throttleToggle

bool FileInputThread::m_throttleToggle
private

Definition at line 91 of file fileinputthread.h.

Referenced by tick().

◆ m_timer

const QTimer& FileInputThread::m_timer
private

Definition at line 83 of file fileinputthread.h.

Referenced by startWork(), and stopWork().


The documentation for this class was generated from the following files: