1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-14 20:31:53 -05:00
sdrangel/plugins/channelrx/demoddatv/datvideostream.cpp

229 lines
5.9 KiB
C++
Raw Normal View History

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/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "datvideostream.h"
#include <stdio.h>
DATVideostream::DATVideostream():
m_objMutex(QMutex::NonRecursive)
{
cleanUp();
2019-03-16 20:36:44 -04:00
m_intTotalReceived = 0;
m_intPacketReceived = 0;
2018-02-22 16:52:49 -05:00
m_intMemoryLimit = DefaultMemoryLimit;
2019-03-16 20:36:44 -04:00
MultiThreaded = false;
ThreadTimeOut = -1;
2018-02-22 16:52:49 -05:00
m_objeventLoop.connect(this,SIGNAL(onDataAvailable()), &m_objeventLoop, SLOT(quit()),Qt::QueuedConnection);
}
DATVideostream::~DATVideostream()
{
m_objeventLoop.disconnect(this,SIGNAL(onDataAvailable()), &m_objeventLoop, SLOT(quit()));
cleanUp();
}
void DATVideostream::cleanUp()
{
2019-03-16 20:36:44 -04:00
if (m_objFIFO.size() > 0) {
2018-02-22 16:52:49 -05:00
m_objFIFO.clear();
}
2019-03-16 20:36:44 -04:00
if (m_objeventLoop.isRunning()) {
2018-02-22 16:52:49 -05:00
m_objeventLoop.exit();
}
2019-03-16 20:36:44 -04:00
m_intBytesAvailable = 0;
m_intBytesWaiting = 0;
m_intQueueWaiting = 0;
m_intPercentBuffer = 0;
2018-02-22 16:52:49 -05:00
}
bool DATVideostream::setMemoryLimit(int intMemoryLimit)
{
2019-03-16 20:36:44 -04:00
if (intMemoryLimit <= 0) {
2018-02-22 16:52:49 -05:00
return false;
}
2019-03-16 20:36:44 -04:00
m_intMemoryLimit = intMemoryLimit;
2018-02-22 16:52:49 -05:00
return true;
}
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;
}
m_objMutex.lock();
2019-03-16 20:36:44 -04:00
m_intPacketReceived++;
2018-02-22 16:52:49 -05:00
m_intBytesWaiting += intSize;
2019-03-16 20:36:44 -04:00
if (m_intBytesWaiting > m_intMemoryLimit) {
2018-02-22 16:52:49 -05:00
m_intBytesWaiting -= m_objFIFO.dequeue().size();
}
m_objFIFO.enqueue(QByteArray(chrData,intSize));
m_intBytesAvailable = m_objFIFO.head().size();
m_intTotalReceived += intSize;
m_intQueueWaiting=m_objFIFO.count();
m_objMutex.unlock();
2019-03-16 20:36:44 -04:00
if ((m_objeventLoop.isRunning())
&& (m_intQueueWaiting >= MinStackSize))
2018-02-22 16:52:49 -05:00
{
emit onDataAvailable();
}
2019-03-16 20:36:44 -04:00
if (m_intPacketReceived % MinStackSize == 1)
2018-02-22 16:52:49 -05:00
{
m_intPercentBuffer = (100*m_intBytesWaiting)/m_intMemoryLimit;
2019-03-16 20:36:44 -04:00
if (m_intPercentBuffer > 100) {
m_intPercentBuffer = 100;
2018-02-22 16:52:49 -05:00
}
emit onDataPackets(&m_intQueueWaiting, &m_intBytesWaiting, &m_intPercentBuffer, &m_intTotalReceived);
}
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
{
return m_intBytesAvailable;
}
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
{
2018-02-22 16:52:49 -05:00
QByteArray objCurrentArray;
2019-03-16 20:36:44 -04:00
int intEffectiveLen = 0;
int intExpectedLen = 0;
int intThreadLoop = 0;
2018-02-22 16:52:49 -05:00
intExpectedLen = (int) len;
2019-03-16 20:36:44 -04:00
if (intExpectedLen <= 0) {
2018-02-22 16:52:49 -05:00
return 0;
}
2019-03-16 20:36:44 -04:00
if (m_objeventLoop.isRunning()) {
2018-02-22 16:52:49 -05:00
return 0;
}
m_objMutex.lock();
//DATA in FIFO ? -> Waiting for DATA
2019-03-16 20:36:44 -04:00
if ((m_objFIFO.isEmpty()) || (m_objFIFO.count()<MinStackSize))
2018-02-22 16:52:49 -05:00
{
m_objMutex.unlock();
2019-03-16 20:36:44 -04:00
if (MultiThreaded == true)
2018-02-22 16:52:49 -05:00
{
2018-02-25 19:04:45 -05:00
intThreadLoop=0;
2019-03-16 20:36:44 -04:00
while ((m_objFIFO.isEmpty()) || (m_objFIFO.count() < MinStackSize))
2018-02-22 16:52:49 -05:00
{
QThread::msleep(5);
2019-03-16 20:36:44 -04:00
intThreadLoop++;
2018-02-25 19:04:45 -05:00
2019-03-16 20:36:44 -04:00
if (ThreadTimeOut >= 0)
2018-02-25 19:04:45 -05:00
{
2019-03-16 20:36:44 -04:00
if (intThreadLoop*5 > ThreadTimeOut) {
2018-02-25 19:04:45 -05:00
return -1;
}
}
2018-02-22 16:52:49 -05:00
}
}
else
{
m_objeventLoop.exec();
}
m_objMutex.lock();
}
//Read DATA
intEffectiveLen=m_objFIFO.head().size();
2019-03-16 20:36:44 -04:00
if (intExpectedLen < intEffectiveLen)
2018-02-22 16:52:49 -05:00
{
//Partial Read
objCurrentArray = m_objFIFO.head();
memcpy((void *)data,objCurrentArray.constData(),intExpectedLen);
m_objFIFO.head().remove(0,intExpectedLen);
2019-03-16 20:36:44 -04:00
intEffectiveLen = intExpectedLen;
2018-02-22 16:52:49 -05:00
m_intBytesWaiting -= intExpectedLen;
}
else
{
//Complete Read
objCurrentArray = m_objFIFO.dequeue();
memcpy((void *)data,objCurrentArray.constData(),intEffectiveLen);
m_intBytesWaiting -= intEffectiveLen;
}
m_intQueueWaiting = m_objFIFO.count();
2019-03-16 20:36:44 -04:00
m_intPercentBuffer = (100*m_intBytesWaiting) / m_intMemoryLimit;
2018-02-22 16:52:49 -05:00
emit onDataPackets(&m_intQueueWaiting, &m_intBytesWaiting, &m_intPercentBuffer, &m_intTotalReceived);
//Next available DATA
m_intBytesAvailable = m_objFIFO.head().size();
m_objMutex.unlock();
return (qint64)intEffectiveLen;
}
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;
}