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.
datvideostream.cpp
Go to the documentation of this file.
1 // Copyright (C) 2018 F4HKW //
3 // for F4EXB / SDRAngel //
4 // //
5 // This program is free software; you can redistribute it and/or modify //
6 // it under the terms of the GNU General Public License as published by //
7 // the Free Software Foundation as version 3 of the License, or //
8 // (at your option) any later version. //
9 // //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License V3 for more details. //
14 // //
15 // You should have received a copy of the GNU General Public License //
16 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
18 
19 #include "datvideostream.h"
20 #include <stdio.h>
21 
23  m_objMutex(QMutex::NonRecursive)
24 {
25  cleanUp();
29  MultiThreaded = false;
30  ThreadTimeOut = -1;
31 
32  m_objeventLoop.connect(this,SIGNAL(onDataAvailable()), &m_objeventLoop, SLOT(quit()),Qt::QueuedConnection);
33 }
34 
36 {
37  m_objeventLoop.disconnect(this,SIGNAL(onDataAvailable()), &m_objeventLoop, SLOT(quit()));
38  cleanUp();
39 }
40 
42 {
43  if (m_objFIFO.size() > 0) {
44  m_objFIFO.clear();
45  }
46 
47  if (m_objeventLoop.isRunning()) {
48  m_objeventLoop.exit();
49  }
50 
55 }
56 
57 bool DATVideostream::setMemoryLimit(int intMemoryLimit)
58 {
59  if (intMemoryLimit <= 0) {
60  return false;
61  }
62 
63  m_intMemoryLimit = intMemoryLimit;
64 
65  return true;
66 }
67 
68 int DATVideostream::pushData(const char * chrData, int intSize)
69 {
70  if (intSize <= 0) {
71  return 0;
72  }
73 
74  m_objMutex.lock();
75 
77  m_intBytesWaiting += intSize;
78 
80  m_intBytesWaiting -= m_objFIFO.dequeue().size();
81  }
82 
83  m_objFIFO.enqueue(QByteArray(chrData,intSize));
84  m_intBytesAvailable = m_objFIFO.head().size();
85  m_intTotalReceived += intSize;
87 
88  m_objMutex.unlock();
89 
90  if ((m_objeventLoop.isRunning())
92  {
93  emit onDataAvailable();
94  }
95 
97  {
99 
100  if (m_intPercentBuffer > 100) {
101  m_intPercentBuffer = 100;
102  }
103 
105  }
106 
107  return intSize;
108 }
109 
111 {
112  return true;
113 }
114 
116 {
117  return m_intBytesAvailable;
118 }
119 
121 {
122  QIODevice::close();
123  cleanUp();
124 }
125 
126 bool DATVideostream::open(OpenMode mode)
127 {
128  //cleanUp();
129  return QIODevice::open(mode);
130 }
131 
132 //PROTECTED
133 
134 qint64 DATVideostream::readData(char *data, qint64 len)
135 {
136  QByteArray objCurrentArray;
137  int intEffectiveLen = 0;
138  int intExpectedLen = 0;
139  int intThreadLoop = 0;
140 
141  intExpectedLen = (int) len;
142 
143  if (intExpectedLen <= 0) {
144  return 0;
145  }
146 
147  if (m_objeventLoop.isRunning()) {
148  return 0;
149  }
150 
151  m_objMutex.lock();
152 
153  //DATA in FIFO ? -> Waiting for DATA
154  if ((m_objFIFO.isEmpty()) || (m_objFIFO.count()<MinStackSize))
155  {
156  m_objMutex.unlock();
157 
158  if (MultiThreaded == true)
159  {
160  intThreadLoop=0;
161 
162  while ((m_objFIFO.isEmpty()) || (m_objFIFO.count() < MinStackSize))
163  {
164  QThread::msleep(5);
165  intThreadLoop++;
166 
167  if (ThreadTimeOut >= 0)
168  {
169  if (intThreadLoop*5 > ThreadTimeOut) {
170  return -1;
171  }
172  }
173  }
174  }
175  else
176  {
177  m_objeventLoop.exec();
178  }
179 
180  m_objMutex.lock();
181  }
182 
183  //Read DATA
184  intEffectiveLen=m_objFIFO.head().size();
185 
186  if (intExpectedLen < intEffectiveLen)
187  {
188  //Partial Read
189  objCurrentArray = m_objFIFO.head();
190  memcpy((void *)data,objCurrentArray.constData(),intExpectedLen);
191  m_objFIFO.head().remove(0,intExpectedLen);
192  intEffectiveLen = intExpectedLen;
193  m_intBytesWaiting -= intExpectedLen;
194  }
195  else
196  {
197  //Complete Read
198  objCurrentArray = m_objFIFO.dequeue();
199  memcpy((void *)data,objCurrentArray.constData(),intEffectiveLen);
200  m_intBytesWaiting -= intEffectiveLen;
201  }
202 
203  m_intQueueWaiting = m_objFIFO.count();
205 
207 
208  //Next available DATA
209  m_intBytesAvailable = m_objFIFO.head().size();
210 
211  m_objMutex.unlock();
212 
213  return (qint64)intEffectiveLen;
214 }
215 
216 qint64 DATVideostream::writeData(const char *data, qint64 len)
217 {
218  (void) data;
219  (void) len;
220  return 0;
221 }
222 
223 qint64 DATVideostream::readLineData(char *data, qint64 maxSize)
224 {
225  (void) data;
226  (void) maxSize;
227  return 0;
228 }
QEventLoop m_objeventLoop
virtual void close()
virtual bool isSequential() const
bool setMemoryLimit(int intMemoryLimit)
#define MinStackSize
virtual qint64 readData(char *data, qint64 len)
QQueue< QByteArray > m_objFIFO
qint64 m_intTotalReceived
int pushData(const char *chrData, int intSize)
void onDataAvailable()
virtual qint64 writeData(const char *data, qint64 len)
virtual qint64 bytesAvailable() const
qint64 m_intPacketReceived
virtual qint64 readLineData(char *data, qint64 maxSize)
virtual bool open(OpenMode mode)
#define DefaultMemoryLimit
void onDataPackets(int *intDataPackets, int *intDataBytes, int *intPercentBuffer, qint64 *intTotalReceived)