1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-22 16:08:39 -05:00
sdrangel/plugins/channelrx/demoddatv/datvideostream.cpp

238 lines
6.0 KiB
C++
Raw Normal View History

2018-02-22 16:52:49 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018-2019, 2021 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2022 Jiří Pinkava <jiri.pinkava@rossum.ai> //
2018-02-22 16:52:49 -05:00
// Copyright (C) 2018 F4HKW //
// for F4EXB / SDRAngel //
// //
// 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 00:39:30 -04:00
// (at your option) any later version. //
2018-02-22 16:52:49 -05: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/>. //
///////////////////////////////////////////////////////////////////////////////////
2018-02-22 16:52:49 -05:00
#include "datvideostream.h"
#include <stdio.h>
DATVideostream::DATVideostream()
2018-02-22 16:52:49 -05:00
{
cleanUp();
2021-04-05 07:13:57 -04:00
m_totalReceived = 0;
m_packetReceived = 0;
m_memoryLimit = m_defaultMemoryLimit;
m_multiThreaded = false;
m_threadTimeout = -1;
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
m_eventLoop.connect(this, SIGNAL(dataAvailable()), &m_eventLoop, SLOT(quit()), Qt::QueuedConnection);
2018-02-22 16:52:49 -05:00
}
DATVideostream::~DATVideostream()
{
2021-04-05 07:13:57 -04:00
m_eventLoop.disconnect(this, SIGNAL(dataAvailable()), &m_eventLoop, SLOT(quit()));
2018-02-22 16:52:49 -05:00
cleanUp();
}
void DATVideostream::cleanUp()
{
2021-04-05 07:13:57 -04:00
if (m_fifo.size() > 0) {
m_fifo.clear();
2018-02-22 16:52:49 -05:00
}
2021-04-05 07:13:57 -04:00
if (m_eventLoop.isRunning()) {
m_eventLoop.exit();
2018-02-22 16:52:49 -05:00
}
2021-04-05 07:13:57 -04:00
m_bytesAvailable = 0;
m_bytesWaiting = 0;
m_percentBuffer = 0;
2018-02-22 16:52:49 -05:00
}
void DATVideostream::resetTotalReceived()
{
2021-04-05 07:13:57 -04:00
m_totalReceived = 0;
emit fifoData(m_bytesWaiting, m_percentBuffer, m_totalReceived);
}
void DATVideostream::setMultiThreaded(bool multiThreaded)
{
if (multiThreaded)
{
2021-04-05 07:13:57 -04:00
if (m_eventLoop.isRunning()) {
m_eventLoop.exit();
}
}
m_multiThreaded = multiThreaded;
}
2018-02-22 16:52:49 -05:00
int DATVideostream::pushData(const char * chrData, int intSize)
{
2019-03-16 20:36:44 -04:00
if (intSize <= 0) {
2018-02-22 16:52:49 -05:00
return 0;
}
2021-04-05 07:13:57 -04:00
m_mutex.lock();
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
m_packetReceived++;
m_bytesWaiting += intSize;
2019-03-16 20:36:44 -04:00
2021-04-05 07:13:57 -04:00
if (m_bytesWaiting > m_memoryLimit) {
m_bytesWaiting -= m_fifo.dequeue().size();
2018-02-22 16:52:49 -05:00
}
2021-04-05 07:13:57 -04:00
m_fifo.enqueue(QByteArray(chrData,intSize));
m_bytesAvailable = m_fifo.head().size();
m_totalReceived += intSize;
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
m_mutex.unlock();
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
if (m_eventLoop.isRunning()) {
emit dataAvailable();
2018-02-22 16:52:49 -05:00
}
2021-04-05 07:13:57 -04:00
m_percentBuffer = (100*m_bytesWaiting) / m_memoryLimit;
m_percentBuffer = m_percentBuffer > 100 ? 100 : m_percentBuffer;
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
if (m_packetReceived % 10 == 1) {
emit fifoData(m_bytesWaiting, m_percentBuffer, m_totalReceived);
2018-02-22 16:52:49 -05:00
}
return intSize;
}
bool DATVideostream::isSequential() const
2018-02-25 19:04:45 -05:00
{
2018-02-22 16:52:49 -05:00
return true;
}
qint64 DATVideostream::bytesAvailable() const
{
2021-04-05 07:13:57 -04:00
return m_bytesAvailable;
2018-02-22 16:52:49 -05:00
}
void DATVideostream::close()
{
QIODevice::close();
cleanUp();
}
bool DATVideostream::open(OpenMode mode)
{
//cleanUp();
return QIODevice::open(mode);
}
//PROTECTED
qint64 DATVideostream::readData(char *data, qint64 len)
2018-02-25 19:04:45 -05:00
{
2021-04-05 07:13:57 -04:00
QByteArray currentArray;
int effectiveLen = 0;
int expectedLen = (int) len;
int threadLoop = 0;
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
if (expectedLen <= 0) {
2018-02-22 16:52:49 -05:00
return 0;
}
2021-04-05 07:13:57 -04:00
if (m_eventLoop.isRunning()) {
2018-02-22 16:52:49 -05:00
return 0;
}
2021-04-05 07:13:57 -04:00
m_mutex.lock();
2018-02-22 16:52:49 -05:00
//DATA in FIFO ? -> Waiting for DATA
2021-04-05 07:13:57 -04:00
if ((m_fifo.isEmpty()) || (m_fifo.count() < m_minStackSize))
2018-02-22 16:52:49 -05:00
{
2021-04-05 07:13:57 -04:00
m_mutex.unlock();
2018-02-22 16:52:49 -05:00
if (m_multiThreaded == true)
2018-02-22 16:52:49 -05:00
{
2021-04-05 07:13:57 -04:00
threadLoop = 0;
2019-03-16 20:36:44 -04:00
2021-04-05 07:13:57 -04:00
while ((m_fifo.isEmpty()) || (m_fifo.count() < m_minStackSize))
2018-02-22 16:52:49 -05:00
{
QThread::msleep(5);
2021-04-05 07:13:57 -04:00
threadLoop++;
2018-02-25 19:04:45 -05:00
if (m_threadTimeout >= 0)
2018-02-25 19:04:45 -05:00
{
2021-04-05 07:13:57 -04:00
if (threadLoop*5 > m_threadTimeout) {
2018-02-25 19:04:45 -05:00
return -1;
}
}
2018-02-22 16:52:49 -05:00
}
}
else
{
2021-04-05 07:13:57 -04:00
m_eventLoop.exec();
2018-02-22 16:52:49 -05:00
}
2021-04-05 07:13:57 -04:00
m_mutex.lock();
2018-02-22 16:52:49 -05:00
}
//Read DATA
2021-04-05 07:13:57 -04:00
effectiveLen = m_fifo.head().size();
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
if (expectedLen < effectiveLen)
2018-02-22 16:52:49 -05:00
{
//Partial Read
2021-04-05 07:13:57 -04:00
currentArray = m_fifo.head();
std::copy(
currentArray.constData(),
currentArray.constData() + expectedLen,
data
);
m_fifo.head().remove(0, expectedLen);
effectiveLen = expectedLen;
m_bytesWaiting -= expectedLen;
2018-02-22 16:52:49 -05:00
}
else
{
//Complete Read
2021-04-05 07:13:57 -04:00
currentArray = m_fifo.dequeue();
std::copy(
currentArray.constData(),
currentArray.constData() + effectiveLen,
data
);
m_bytesWaiting -= effectiveLen;
2018-02-22 16:52:49 -05:00
}
2021-04-05 07:13:57 -04:00
m_percentBuffer = (100*m_bytesWaiting) / m_memoryLimit;
m_percentBuffer = m_percentBuffer > 100 ? 100 : m_percentBuffer;
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
if (m_packetReceived % 10 == 0) {
emit fifoData(m_bytesWaiting, m_percentBuffer, m_totalReceived);
2021-04-05 07:13:57 -04:00
}
2018-02-22 16:52:49 -05:00
//Next available DATA
2021-04-05 07:13:57 -04:00
m_bytesAvailable = m_fifo.head().size();
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
m_mutex.unlock();
2018-02-22 16:52:49 -05:00
2021-04-05 07:13:57 -04:00
return (qint64) effectiveLen;
2018-02-22 16:52:49 -05:00
}
qint64 DATVideostream::writeData(const char *data, qint64 len)
2018-02-22 16:52:49 -05:00
{
(void) data;
(void) len;
2018-02-22 16:52:49 -05:00
return 0;
}
qint64 DATVideostream::readLineData(char *data, qint64 maxSize)
2018-02-25 19:04:45 -05:00
{
(void) data;
(void) maxSize;
2018-02-22 16:52:49 -05:00
return 0;
}