2014-05-18 15:52:39 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2018-01-24 07:49:18 +00:00
|
|
|
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
|
2014-05-18 15:52:39 +00:00
|
|
|
// written by Christian Daniel //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation as version 3 of the License, or //
|
2019-04-11 12:32:15 +00:00
|
|
|
// (at your option) any later version. //
|
2014-05-18 15:52:39 +00:00
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2022-03-15 21:21:06 +00:00
|
|
|
#include "maincore.h"
|
2016-10-06 17:18:02 +00:00
|
|
|
#include "samplesinkfifo.h"
|
2014-05-18 15:52:39 +00:00
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
//#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
2014-05-18 15:52:39 +00:00
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
void SampleSinkFifo::create(unsigned int s)
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
|
|
|
m_fill = 0;
|
|
|
|
m_head = 0;
|
|
|
|
m_tail = 0;
|
|
|
|
|
|
|
|
m_data.resize(s);
|
|
|
|
m_size = m_data.size();
|
|
|
|
}
|
|
|
|
|
2019-11-23 06:39:57 +00:00
|
|
|
void SampleSinkFifo::reset()
|
|
|
|
{
|
2021-04-26 17:33:02 +00:00
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2019-11-23 06:39:57 +00:00
|
|
|
m_suppressed = -1;
|
|
|
|
m_fill = 0;
|
|
|
|
m_head = 0;
|
|
|
|
m_tail = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 17:18:02 +00:00
|
|
|
SampleSinkFifo::SampleSinkFifo(QObject* parent) :
|
2014-05-18 15:52:39 +00:00
|
|
|
QObject(parent),
|
2021-04-26 17:33:02 +00:00
|
|
|
m_data(),
|
2022-03-15 21:21:06 +00:00
|
|
|
m_total(0),
|
|
|
|
m_writtenSignalCount(0),
|
|
|
|
m_writtenSignalRateDivider(1),
|
2021-04-26 17:33:02 +00:00
|
|
|
m_mutex(QMutex::Recursive)
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
|
|
|
m_suppressed = -1;
|
|
|
|
m_size = 0;
|
|
|
|
m_fill = 0;
|
|
|
|
m_head = 0;
|
|
|
|
m_tail = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 17:18:02 +00:00
|
|
|
SampleSinkFifo::SampleSinkFifo(int size, QObject* parent) :
|
2014-05-18 15:52:39 +00:00
|
|
|
QObject(parent),
|
2021-04-26 17:33:02 +00:00
|
|
|
m_data(),
|
2022-03-15 21:21:06 +00:00
|
|
|
m_total(0),
|
|
|
|
m_writtenSignalCount(0),
|
|
|
|
m_writtenSignalRateDivider(1),
|
2021-04-26 17:33:02 +00:00
|
|
|
m_mutex(QMutex::Recursive)
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
|
|
|
m_suppressed = -1;
|
|
|
|
create(size);
|
|
|
|
}
|
|
|
|
|
2019-05-18 04:30:37 +00:00
|
|
|
SampleSinkFifo::SampleSinkFifo(const SampleSinkFifo& other) :
|
|
|
|
QObject(other.parent()),
|
2021-04-26 17:33:02 +00:00
|
|
|
m_data(other.m_data),
|
2022-03-15 21:21:06 +00:00
|
|
|
m_total(0),
|
|
|
|
m_writtenSignalCount(0),
|
|
|
|
m_writtenSignalRateDivider(1),
|
2021-04-26 17:33:02 +00:00
|
|
|
m_mutex(QMutex::Recursive)
|
2019-05-18 04:30:37 +00:00
|
|
|
{
|
|
|
|
m_suppressed = -1;
|
|
|
|
m_size = m_data.size();
|
|
|
|
m_fill = 0;
|
|
|
|
m_head = 0;
|
|
|
|
m_tail = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 17:18:02 +00:00
|
|
|
SampleSinkFifo::~SampleSinkFifo()
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 17:18:02 +00:00
|
|
|
bool SampleSinkFifo::setSize(int size)
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
2021-04-26 17:33:02 +00:00
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2014-05-18 15:52:39 +00:00
|
|
|
create(size);
|
2019-10-03 06:36:38 +00:00
|
|
|
return m_data.size() == (unsigned int)size;
|
2014-05-18 15:52:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 21:21:06 +00:00
|
|
|
void SampleSinkFifo::setWrittenSignalRateDivider(unsigned int divider)
|
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
m_writtenSignalRateDivider = divider;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
unsigned int SampleSinkFifo::write(const quint8* data, unsigned int count)
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2021-04-25 09:06:21 +00:00
|
|
|
|
|
|
|
if (m_size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
unsigned int total;
|
|
|
|
unsigned int remaining;
|
|
|
|
unsigned int len;
|
2014-05-18 15:52:39 +00:00
|
|
|
const Sample* begin = (const Sample*)data;
|
2018-01-24 07:49:18 +00:00
|
|
|
count /= sizeof(Sample);
|
2014-05-18 15:52:39 +00:00
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
total = std::min(count, m_size - m_fill);
|
|
|
|
|
|
|
|
if (total < count)
|
|
|
|
{
|
|
|
|
if (m_suppressed < 0)
|
|
|
|
{
|
2014-05-18 15:52:39 +00:00
|
|
|
m_suppressed = 0;
|
|
|
|
m_msgRateTimer.start();
|
2022-03-17 20:10:30 +00:00
|
|
|
qCritical("SampleSinkFifo::write: (%s) overflow - dropping %u samples",
|
|
|
|
qPrintable(m_label), count - total);
|
2022-03-15 21:21:06 +00:00
|
|
|
emit overflow(count - total);
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_msgRateTimer.elapsed() > 2500)
|
|
|
|
{
|
2022-03-17 20:10:30 +00:00
|
|
|
qCritical("SampleSinkFifo::write: (%s) %u messages dropped", qPrintable(m_label), m_suppressed);
|
|
|
|
qCritical("SampleSinkFifo::write: (%s) overflow - dropping %u samples",
|
|
|
|
qPrintable(m_label), count - total);
|
2022-03-15 21:21:06 +00:00
|
|
|
emit overflow(count - total);
|
2014-05-18 15:52:39 +00:00
|
|
|
m_suppressed = -1;
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-18 15:52:39 +00:00
|
|
|
m_suppressed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining = total;
|
2019-10-03 06:36:38 +00:00
|
|
|
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
|
|
|
len = std::min(remaining, m_size - m_tail);
|
2014-05-18 15:52:39 +00:00
|
|
|
std::copy(begin, begin + len, m_data.begin() + m_tail);
|
|
|
|
m_tail += len;
|
|
|
|
m_tail %= m_size;
|
|
|
|
m_fill += len;
|
|
|
|
begin += len;
|
|
|
|
remaining -= len;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
if (m_fill > 0) {
|
2014-05-18 15:52:39 +00:00
|
|
|
emit dataReady();
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
2014-05-18 15:52:39 +00:00
|
|
|
|
2022-03-15 21:21:06 +00:00
|
|
|
m_total += total;
|
|
|
|
|
|
|
|
if (++m_writtenSignalCount >= m_writtenSignalRateDivider)
|
|
|
|
{
|
|
|
|
emit written(m_total, MainCore::instance()->getElapsedNsecs());
|
|
|
|
m_total = 0;
|
|
|
|
m_writtenSignalCount = 0;
|
|
|
|
}
|
|
|
|
|
2014-05-18 15:52:39 +00:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
unsigned int SampleSinkFifo::write(SampleVector::const_iterator begin, SampleVector::const_iterator end)
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2021-04-25 09:06:21 +00:00
|
|
|
|
|
|
|
if (m_size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
unsigned int count = end - begin;
|
|
|
|
unsigned int total;
|
|
|
|
unsigned int remaining;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
total = std::min(count, m_size - m_fill);
|
|
|
|
|
|
|
|
if (total < count)
|
|
|
|
{
|
|
|
|
if (m_suppressed < 0)
|
|
|
|
{
|
2014-05-18 15:52:39 +00:00
|
|
|
m_suppressed = 0;
|
|
|
|
m_msgRateTimer.start();
|
2022-03-17 20:10:30 +00:00
|
|
|
qCritical("SampleSinkFifo::write: (%s) overflow - dropping %u samples",
|
|
|
|
qPrintable(m_label), count - total);
|
2022-03-15 21:21:06 +00:00
|
|
|
emit overflow(count - total);
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_msgRateTimer.elapsed() > 2500)
|
|
|
|
{
|
2022-03-17 20:10:30 +00:00
|
|
|
qCritical("SampleSinkFifo::write: (%s) %u messages dropped", qPrintable(m_label), m_suppressed);
|
|
|
|
qCritical("SampleSinkFifo::write: (%s) overflow - dropping %u samples",
|
|
|
|
qPrintable(m_label), count - total);
|
2022-03-15 21:21:06 +00:00
|
|
|
emit overflow(count - total);
|
2014-05-18 15:52:39 +00:00
|
|
|
m_suppressed = -1;
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-18 15:52:39 +00:00
|
|
|
m_suppressed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining = total;
|
2019-10-03 06:36:38 +00:00
|
|
|
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
|
|
|
len = std::min(remaining, m_size - m_tail);
|
2014-05-18 15:52:39 +00:00
|
|
|
std::copy(begin, begin + len, m_data.begin() + m_tail);
|
|
|
|
m_tail += len;
|
|
|
|
m_tail %= m_size;
|
|
|
|
m_fill += len;
|
|
|
|
begin += len;
|
|
|
|
remaining -= len;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
if (m_fill > 0) {
|
2014-05-18 15:52:39 +00:00
|
|
|
emit dataReady();
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
2014-05-18 15:52:39 +00:00
|
|
|
|
2022-03-15 21:21:06 +00:00
|
|
|
m_total += total;
|
|
|
|
|
|
|
|
if (++m_writtenSignalCount >= m_writtenSignalRateDivider)
|
|
|
|
{
|
|
|
|
emit written(m_total, MainCore::instance()->getElapsedNsecs());
|
|
|
|
m_total = 0;
|
|
|
|
m_writtenSignalCount = 0;
|
|
|
|
}
|
|
|
|
|
2014-05-18 15:52:39 +00:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
unsigned int SampleSinkFifo::read(SampleVector::iterator begin, SampleVector::iterator end)
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2021-04-25 09:06:21 +00:00
|
|
|
|
|
|
|
if (m_size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
unsigned int count = end - begin;
|
|
|
|
unsigned int total;
|
|
|
|
unsigned int remaining;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
total = std::min(count, m_fill);
|
2014-05-18 15:52:39 +00:00
|
|
|
|
2022-03-15 21:21:06 +00:00
|
|
|
if (total < count)
|
|
|
|
{
|
2022-03-17 20:10:30 +00:00
|
|
|
qCritical("SampleSinkFifo::read: (%s) underflow - missing %u samples",
|
|
|
|
qPrintable(m_label), count - total);
|
2022-03-15 21:21:06 +00:00
|
|
|
emit underflow(count - total);
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
2014-05-18 15:52:39 +00:00
|
|
|
|
|
|
|
remaining = total;
|
2019-10-03 06:36:38 +00:00
|
|
|
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
|
|
|
len = std::min(remaining, m_size - m_head);
|
2014-05-18 15:52:39 +00:00
|
|
|
std::copy(m_data.begin() + m_head, m_data.begin() + m_head + len, begin);
|
|
|
|
m_head += len;
|
|
|
|
m_head %= m_size;
|
|
|
|
m_fill -= len;
|
|
|
|
begin += len;
|
|
|
|
remaining -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
unsigned int SampleSinkFifo::readBegin(unsigned int count,
|
2014-05-18 15:52:39 +00:00
|
|
|
SampleVector::iterator* part1Begin, SampleVector::iterator* part1End,
|
|
|
|
SampleVector::iterator* part2Begin, SampleVector::iterator* part2End)
|
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2021-04-25 09:06:21 +00:00
|
|
|
|
|
|
|
if (m_size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
unsigned int total;
|
|
|
|
unsigned int remaining;
|
|
|
|
unsigned int len;
|
|
|
|
unsigned int head = m_head;
|
|
|
|
|
|
|
|
total = std::min(count, m_fill);
|
2014-05-18 15:52:39 +00:00
|
|
|
|
2022-03-15 21:21:06 +00:00
|
|
|
if (total < count)
|
|
|
|
{
|
2022-03-17 20:10:30 +00:00
|
|
|
qCritical("SampleSinkFifo::readBegin: (%s) underflow - missing %u samples",
|
|
|
|
qPrintable(m_label), count - total);
|
2022-03-15 21:21:06 +00:00
|
|
|
emit underflow(count - total);
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
2014-05-18 15:52:39 +00:00
|
|
|
|
|
|
|
remaining = total;
|
2019-10-03 06:36:38 +00:00
|
|
|
|
|
|
|
if (remaining > 0)
|
|
|
|
{
|
|
|
|
len = std::min(remaining, m_size - head);
|
2014-05-18 15:52:39 +00:00
|
|
|
*part1Begin = m_data.begin() + head;
|
|
|
|
*part1End = m_data.begin() + head + len;
|
|
|
|
head += len;
|
|
|
|
head %= m_size;
|
|
|
|
remaining -= len;
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-18 15:52:39 +00:00
|
|
|
*part1Begin = m_data.end();
|
|
|
|
*part1End = m_data.end();
|
|
|
|
}
|
2019-10-03 06:36:38 +00:00
|
|
|
|
|
|
|
if (remaining > 0)
|
|
|
|
{
|
|
|
|
len = std::min(remaining, m_size - head);
|
2014-05-18 15:52:39 +00:00
|
|
|
*part2Begin = m_data.begin() + head;
|
|
|
|
*part2End = m_data.begin() + head + len;
|
2019-10-03 06:36:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-18 15:52:39 +00:00
|
|
|
*part2Begin = m_data.end();
|
|
|
|
*part2End = m_data.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
unsigned int SampleSinkFifo::readCommit(unsigned int count)
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
|
2021-04-25 09:06:21 +00:00
|
|
|
if (m_size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:36:38 +00:00
|
|
|
if (count > m_fill)
|
|
|
|
{
|
2022-03-17 20:10:30 +00:00
|
|
|
qCritical("SampleSinkFifo::readCommit: (%s) cannot commit more than available samples", qPrintable(m_label));
|
2014-05-18 15:52:39 +00:00
|
|
|
count = m_fill;
|
|
|
|
}
|
2019-10-03 06:36:38 +00:00
|
|
|
|
|
|
|
m_head = (m_head + count) % m_size;
|
2014-05-18 15:52:39 +00:00
|
|
|
m_fill -= count;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2019-11-23 06:39:57 +00:00
|
|
|
|
|
|
|
unsigned int SampleSinkFifo::getSizePolicy(unsigned int sampleRate)
|
|
|
|
{
|
|
|
|
return (sampleRate/100)*64; // .64s
|
2021-04-25 09:06:21 +00:00
|
|
|
}
|