mirror of
https://github.com/f4exb/sdrangel.git
synced 2025-04-05 19:18:38 -04:00
Reformat sample sink FIFO code
This commit is contained in:
parent
7bc413f8a9
commit
aeaba8b378
@ -18,9 +18,9 @@
|
||||
|
||||
#include "samplesinkfifo.h"
|
||||
|
||||
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
//#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
|
||||
void SampleSinkFifo::create(uint s)
|
||||
void SampleSinkFifo::create(unsigned int s)
|
||||
{
|
||||
m_size = 0;
|
||||
m_fill = 0;
|
||||
@ -29,9 +29,6 @@ void SampleSinkFifo::create(uint s)
|
||||
|
||||
m_data.resize(s);
|
||||
m_size = m_data.size();
|
||||
|
||||
if(m_size != s)
|
||||
qCritical("SampleSinkFifo: out of memory");
|
||||
}
|
||||
|
||||
SampleSinkFifo::SampleSinkFifo(QObject* parent) :
|
||||
@ -50,7 +47,6 @@ SampleSinkFifo::SampleSinkFifo(int size, QObject* parent) :
|
||||
m_data()
|
||||
{
|
||||
m_suppressed = -1;
|
||||
|
||||
create(size);
|
||||
}
|
||||
|
||||
@ -68,7 +64,6 @@ SampleSinkFifo::SampleSinkFifo(const SampleSinkFifo& other) :
|
||||
SampleSinkFifo::~SampleSinkFifo()
|
||||
{
|
||||
QMutexLocker mutexLocker(&m_mutex);
|
||||
|
||||
m_size = 0;
|
||||
}
|
||||
|
||||
@ -76,38 +71,48 @@ bool SampleSinkFifo::setSize(int size)
|
||||
{
|
||||
create(size);
|
||||
|
||||
return m_data.size() == (uint)size;
|
||||
return m_data.size() == (unsigned int)size;
|
||||
}
|
||||
|
||||
uint SampleSinkFifo::write(const quint8* data, uint count)
|
||||
unsigned int SampleSinkFifo::write(const quint8* data, unsigned int count)
|
||||
{
|
||||
QMutexLocker mutexLocker(&m_mutex);
|
||||
uint total;
|
||||
uint remaining;
|
||||
uint len;
|
||||
unsigned int total;
|
||||
unsigned int remaining;
|
||||
unsigned int len;
|
||||
const Sample* begin = (const Sample*)data;
|
||||
count /= sizeof(Sample);
|
||||
|
||||
total = MIN(count, m_size - m_fill);
|
||||
if(total < count) {
|
||||
if(m_suppressed < 0) {
|
||||
total = std::min(count, m_size - m_fill);
|
||||
|
||||
if (total < count)
|
||||
{
|
||||
if (m_suppressed < 0)
|
||||
{
|
||||
m_suppressed = 0;
|
||||
m_msgRateTimer.start();
|
||||
qCritical("SampleSinkFifo: overflow - dropping %u samples", count - total);
|
||||
} else {
|
||||
if(m_msgRateTimer.elapsed() > 2500) {
|
||||
qCritical("SampleSinkFifo: %u messages dropped", m_suppressed);
|
||||
qCritical("SampleSinkFifo: overflow - dropping %u samples", count - total);
|
||||
qCritical("SampleSinkFifo::write: overflow - dropping %u samples", count - total);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_msgRateTimer.elapsed() > 2500)
|
||||
{
|
||||
qCritical("SampleSinkFifo::write: %u messages dropped", m_suppressed);
|
||||
qCritical("SampleSinkFifo::write: overflow - dropping %u samples", count - total);
|
||||
m_suppressed = -1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
m_suppressed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
remaining = total;
|
||||
while(remaining > 0) {
|
||||
len = MIN(remaining, m_size - m_tail);
|
||||
|
||||
while (remaining > 0)
|
||||
{
|
||||
len = std::min(remaining, m_size - m_tail);
|
||||
std::copy(begin, begin + len, m_data.begin() + m_tail);
|
||||
m_tail += len;
|
||||
m_tail %= m_size;
|
||||
@ -116,40 +121,51 @@ uint SampleSinkFifo::write(const quint8* data, uint count)
|
||||
remaining -= len;
|
||||
}
|
||||
|
||||
if(m_fill > 0)
|
||||
if (m_fill > 0) {
|
||||
emit dataReady();
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
uint SampleSinkFifo::write(SampleVector::const_iterator begin, SampleVector::const_iterator end)
|
||||
unsigned int SampleSinkFifo::write(SampleVector::const_iterator begin, SampleVector::const_iterator end)
|
||||
{
|
||||
QMutexLocker mutexLocker(&m_mutex);
|
||||
uint count = end - begin;
|
||||
uint total;
|
||||
uint remaining;
|
||||
uint len;
|
||||
unsigned int count = end - begin;
|
||||
unsigned int total;
|
||||
unsigned int remaining;
|
||||
unsigned int len;
|
||||
|
||||
total = MIN(count, m_size - m_fill);
|
||||
if(total < count) {
|
||||
if(m_suppressed < 0) {
|
||||
total = std::min(count, m_size - m_fill);
|
||||
|
||||
if (total < count)
|
||||
{
|
||||
if (m_suppressed < 0)
|
||||
{
|
||||
m_suppressed = 0;
|
||||
m_msgRateTimer.start();
|
||||
qCritical("SampleSinkFifo: overflow - dropping %u samples", count - total);
|
||||
} else {
|
||||
if(m_msgRateTimer.elapsed() > 2500) {
|
||||
qCritical("SampleSinkFifo: %u messages dropped", m_suppressed);
|
||||
qCritical("SampleSinkFifo: overflow - dropping %u samples", count - total);
|
||||
qCritical("SampleSinkFifo::write: overflow - dropping %u samples", count - total);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_msgRateTimer.elapsed() > 2500)
|
||||
{
|
||||
qCritical("SampleSinkFifo::write: %u messages dropped", m_suppressed);
|
||||
qCritical("SampleSinkFifo::write: overflow - dropping %u samples", count - total);
|
||||
m_suppressed = -1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
m_suppressed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
remaining = total;
|
||||
while(remaining > 0) {
|
||||
len = MIN(remaining, m_size - m_tail);
|
||||
|
||||
while (remaining > 0)
|
||||
{
|
||||
len = std::min(remaining, m_size - m_tail);
|
||||
std::copy(begin, begin + len, m_data.begin() + m_tail);
|
||||
m_tail += len;
|
||||
m_tail %= m_size;
|
||||
@ -158,27 +174,32 @@ uint SampleSinkFifo::write(SampleVector::const_iterator begin, SampleVector::con
|
||||
remaining -= len;
|
||||
}
|
||||
|
||||
if(m_fill > 0)
|
||||
if (m_fill > 0) {
|
||||
emit dataReady();
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
uint SampleSinkFifo::read(SampleVector::iterator begin, SampleVector::iterator end)
|
||||
unsigned int SampleSinkFifo::read(SampleVector::iterator begin, SampleVector::iterator end)
|
||||
{
|
||||
QMutexLocker mutexLocker(&m_mutex);
|
||||
uint count = end - begin;
|
||||
uint total;
|
||||
uint remaining;
|
||||
uint len;
|
||||
unsigned int count = end - begin;
|
||||
unsigned int total;
|
||||
unsigned int remaining;
|
||||
unsigned int len;
|
||||
|
||||
total = MIN(count, m_fill);
|
||||
if(total < count)
|
||||
qCritical("SampleSinkFifo: underflow - missing %u samples", count - total);
|
||||
total = std::min(count, m_fill);
|
||||
|
||||
if (total < count) {
|
||||
qCritical("SampleSinkFifo::read: underflow - missing %u samples", count - total);
|
||||
}
|
||||
|
||||
remaining = total;
|
||||
while(remaining > 0) {
|
||||
len = MIN(remaining, m_size - m_head);
|
||||
|
||||
while (remaining > 0)
|
||||
{
|
||||
len = std::min(remaining, m_size - m_head);
|
||||
std::copy(m_data.begin() + m_head, m_data.begin() + m_head + len, begin);
|
||||
m_head += len;
|
||||
m_head %= m_size;
|
||||
@ -190,37 +211,47 @@ uint SampleSinkFifo::read(SampleVector::iterator begin, SampleVector::iterator e
|
||||
return total;
|
||||
}
|
||||
|
||||
uint SampleSinkFifo::readBegin(uint count,
|
||||
unsigned int SampleSinkFifo::readBegin(unsigned int count,
|
||||
SampleVector::iterator* part1Begin, SampleVector::iterator* part1End,
|
||||
SampleVector::iterator* part2Begin, SampleVector::iterator* part2End)
|
||||
{
|
||||
QMutexLocker mutexLocker(&m_mutex);
|
||||
uint total;
|
||||
uint remaining;
|
||||
uint len;
|
||||
uint head = m_head;
|
||||
unsigned int total;
|
||||
unsigned int remaining;
|
||||
unsigned int len;
|
||||
unsigned int head = m_head;
|
||||
|
||||
total = MIN(count, m_fill);
|
||||
if(total < count)
|
||||
qCritical("SampleSinkFifo: underflow - missing %u samples", count - total);
|
||||
total = std::min(count, m_fill);
|
||||
|
||||
if (total < count) {
|
||||
qCritical("SampleSinkFifo::readBegin: underflow - missing %u samples", count - total);
|
||||
}
|
||||
|
||||
remaining = total;
|
||||
if(remaining > 0) {
|
||||
len = MIN(remaining, m_size - head);
|
||||
|
||||
if (remaining > 0)
|
||||
{
|
||||
len = std::min(remaining, m_size - head);
|
||||
*part1Begin = m_data.begin() + head;
|
||||
*part1End = m_data.begin() + head + len;
|
||||
head += len;
|
||||
head %= m_size;
|
||||
remaining -= len;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
*part1Begin = m_data.end();
|
||||
*part1End = m_data.end();
|
||||
}
|
||||
if(remaining > 0) {
|
||||
len = MIN(remaining, m_size - head);
|
||||
|
||||
if (remaining > 0)
|
||||
{
|
||||
len = std::min(remaining, m_size - head);
|
||||
*part2Begin = m_data.begin() + head;
|
||||
*part2End = m_data.begin() + head + len;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
*part2Begin = m_data.end();
|
||||
*part2End = m_data.end();
|
||||
}
|
||||
@ -228,15 +259,17 @@ uint SampleSinkFifo::readBegin(uint count,
|
||||
return total;
|
||||
}
|
||||
|
||||
uint SampleSinkFifo::readCommit(uint count)
|
||||
unsigned int SampleSinkFifo::readCommit(unsigned int count)
|
||||
{
|
||||
QMutexLocker mutexLocker(&m_mutex);
|
||||
|
||||
if(count > m_fill) {
|
||||
qCritical("SampleSinkFifo: cannot commit more than available samples");
|
||||
if (count > m_fill)
|
||||
{
|
||||
qCritical("SampleSinkFifo::readCommit: cannot commit more than available samples");
|
||||
count = m_fill;
|
||||
}
|
||||
m_head = (m_head + count) % m_size;
|
||||
|
||||
m_head = (m_head + count) % m_size;
|
||||
m_fill -= count;
|
||||
|
||||
return count;
|
||||
|
@ -35,12 +35,12 @@ private:
|
||||
|
||||
SampleVector m_data;
|
||||
|
||||
uint m_size;
|
||||
uint m_fill;
|
||||
uint m_head;
|
||||
uint m_tail;
|
||||
unsigned int m_size;
|
||||
unsigned int m_fill;
|
||||
unsigned int m_head;
|
||||
unsigned int m_tail;
|
||||
|
||||
void create(uint s);
|
||||
void create(unsigned int s);
|
||||
|
||||
public:
|
||||
SampleSinkFifo(QObject* parent = nullptr);
|
||||
@ -49,18 +49,18 @@ public:
|
||||
~SampleSinkFifo();
|
||||
|
||||
bool setSize(int size);
|
||||
inline uint size() const { return m_size; }
|
||||
inline uint fill() { QMutexLocker mutexLocker(&m_mutex); uint fill = m_fill; return fill; }
|
||||
inline unsigned int size() const { return m_size; }
|
||||
inline unsigned int fill() { QMutexLocker mutexLocker(&m_mutex); unsigned int fill = m_fill; return fill; }
|
||||
|
||||
uint write(const quint8* data, uint count);
|
||||
uint write(SampleVector::const_iterator begin, SampleVector::const_iterator end);
|
||||
unsigned int write(const quint8* data, unsigned int count);
|
||||
unsigned int write(SampleVector::const_iterator begin, SampleVector::const_iterator end);
|
||||
|
||||
uint read(SampleVector::iterator begin, SampleVector::iterator end);
|
||||
unsigned int read(SampleVector::iterator begin, SampleVector::iterator end);
|
||||
|
||||
uint readBegin(uint count,
|
||||
unsigned int readBegin(unsigned int count,
|
||||
SampleVector::iterator* part1Begin, SampleVector::iterator* part1End,
|
||||
SampleVector::iterator* part2Begin, SampleVector::iterator* part2End);
|
||||
uint readCommit(uint count);
|
||||
unsigned int readCommit(unsigned int count);
|
||||
|
||||
signals:
|
||||
void dataReady();
|
||||
|
Loading…
Reference in New Issue
Block a user