YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
signalhandler.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "signalhandler.h"
7#include <QUrl>
8#include <QCoreApplication>
9#include <QFileInfo>
10#include <QDir>
11#include <QDateTime>
13 QObject(parent),timer(this)
14{
15
16 b_saveSetFrameMode = false;
17 b_saveCurrentFrameMode = false;
18 b_freezeMode = false;
19 b_synchRateMode = false;
20 b_autosizeMode = false;
21 defaultNameCounter = 0;
22 customNameCounter = 0;
23 framesetCounter = 0;
24
25 checkDefaultNameCounterCount();
26
27 fpsTimer.setSingleShot(false);
28 fpsTimer.setInterval(3000);
29
30 timer.setTimerType(Qt::PreciseTimer);
31
32 connect(this,SIGNAL(internalSendFrame(QVideoFrame)),
33 this,SLOT(internalReceiveFrame(QVideoFrame)),Qt::QueuedConnection);
34
35 connect(&timer,SIGNAL(timeout()),this,SLOT(onTimerElapsed()),Qt::QueuedConnection);
36
37 connect(this,SIGNAL(selfStartTimer()),
38 &timer,SLOT(start()),Qt::QueuedConnection);
39
40 connect(&fpsTimer,SIGNAL(timeout()),
41 this,SLOT(onFpsTimer()),Qt::DirectConnection);
42 fpsTimer.start();
43
44
45}
46
48{
49 disconnect(this,SIGNAL(internalSendFrame(QVideoFrame)),
50 this,SLOT(internalReceiveFrame(QVideoFrame)));
51
52 disconnect(&timer,SIGNAL(timeout()),this,SLOT(onTimerElapsed()));
53
54 disconnect(this,SIGNAL(selfStartTimer()),&timer,SLOT(start()));
55 if(timer.isActive()){
56 timer.stop();
57 }
58}
59
65void SignalHandler::onSelfStartTimer()
66{
67 timer.start();
68}
69
77{
78 portFps.update();
79
80 if(b_synchRateMode){
81 displayFps.update();
82 emit internalSendFrame(f);
83 }else{
84 mutex.lock();
85 frame = f;
86 if(!timer.isActive()){
87 emit selfStartTimer();
88 }
89 mutex.unlock();
90
91 }
92
93 if(b_saveCurrentFrameMode){
94 b_saveCurrentFrameMode = false;
95 f.map(QAbstractVideoBuffer::ReadOnly);
96 QImage img = QImage(f.bits(),f.width(),f.height(),QImage::Format_RGB32);
97 f.unmap();
98 saveFrame(img);
99 }
100
101 if(b_saveSetFrameMode){
102 f.map(QAbstractVideoBuffer::ReadOnly);
103 QImage img = QImage(f.bits(),f.width(),f.height(),QImage::Format_RGB32);
104 f.unmap();
105 saveFrameSet(img);
106 }
107
108}
109
117void SignalHandler::internalReceiveFrame(QVideoFrame f)
118{
119 if(!b_freezeMode){
120 emit sendFrame(&f);
121 }
122}
123
129{
130 b_synchRateMode = check;
131 if(b_synchRateMode){
132 if(timer.isActive()){
133 timer.stop();
134 }
135 }else{
136 if(frame.isValid() && !timer.isActive()){
137 timer.start();
138 }
139 }
140}
141
147{
148 b_autosizeMode = check;
149}
150
152{
153 return b_autosizeMode;
154}
155
160void SignalHandler::freeze(bool check)
161{
162 b_freezeMode = check;
163}
164
170{
171 timer.setInterval(interval);
172}
173
174
175
176void SignalHandler::onTimerElapsed()
177{
178 displayFps.update();
179 mutex.lock();
180 internalReceiveFrame(frame);
181 mutex.unlock();
182}
183
187{
188 b_saveCurrentFrameMode = true;
189}
190
196{
197
198 this->fileName = url.toLocalFile();
199 customNameCounter = 0;
200 checkCustomNameCounterCount(this->fileName);
201}
202
207void SignalHandler::saveFrame(QImage img)
208{
209 if(fileName.isEmpty()){
210 QString dir = QCoreApplication::applicationDirPath();
211 QString name = QString("%1/frame%2.ppm").arg(dir).arg(defaultNameCounter,3,10,QChar('0'));
212 img.save(name,"PPM");
213 defaultNameCounter++;
214 }else{
215 QString name = QString("%1%2.ppm").arg(fileName).arg(customNameCounter,3,10,QChar('0'));
216 img.save(name,"PPM");
217 customNameCounter++;
218 }
219}
220
221
226void SignalHandler::saveFrameSet(QImage img)
227{
228 if(fileNames.isEmpty()){
229 QString dir = QCoreApplication::applicationDirPath();
230 QString name = QString("%1/frameset%2.ppm").arg(dir).arg(framesetCounter);
231 img.save(name,"PPM");
232 framesetCounter++;
233 }else{
234 QString name = QString("%1%2.ppm").arg(fileNames).arg(framesetCounter);
235 img.save(name,"PPM");
236 framesetCounter++;
237 }
238}
239
240
245void SignalHandler::checkDefaultNameCounterCount()
246{
247 QDir dir = QDir(QCoreApplication::applicationDirPath());
248 QStringList filter;
249 filter.append("*.ppm");
250 QFileInfoList fil = dir.entryInfoList(filter,QDir::Files|QDir::NoDotAndDotDot);
251 QListIterator<QFileInfo> li(fil);
252
253 while(li.hasNext()){
254 QFileInfo fi = li.next();
255 if (fi.fileName().contains("frame")) {
256 defaultNameCounter++;
257 }
258 }
259
260}
261
266void SignalHandler::checkCustomNameCounterCount(QString file)
267{
268 int index = file.lastIndexOf("/");
269 QString sdir = file.left(index);
270 QString sfile = file.right(file.length() - index -1);
271
272 QDir dir = QDir(sdir);
273 QStringList filter;
274 filter.append("*.ppm");
275 QFileInfoList fil = dir.entryInfoList(filter,QDir::Files|QDir::NoDotAndDotDot);
276 QListIterator<QFileInfo> li(fil);
277
278 while(li.hasNext()){
279 QFileInfo fi = li.next();
280 if (fi.fileName().contains(sfile)) {
281 customNameCounter++;
282 }
283 }
284
285}
286
292{
293 if (b_saveSetFrameMode == false) {
294 this->fileNames = url.toLocalFile();
295 }
296}
297
300{
301 framesetCounter = 0;
302 b_saveSetFrameMode = true;
303}
304
307{
308 b_saveSetFrameMode = false;
309}
310
311
312void SignalHandler::onFpsTimer()
313{
314 double pAvg,pMin,pMax,dAvg,dMin,dMax;
315 portFps.getStats(pAvg,pMin,pMax);
316 portFps.reset();
317 displayFps.getStats(dAvg,dMin,dMax);
318 displayFps.reset();
319
320 emit sendFps(pAvg,pMin,pMax,dAvg,dMin,dMax);
321}
int SIGNAL(int pid, int signum)
void update()
This function must be called each time you want to track a frame.
Definition FpsStats.h:45
void getStats(double &av, double &m, double &M)
This function returns the Minimum, Maximum and Average values.
Definition FpsStats.h:67
void reset()
This function must be called each time you want to reset the acquiring data period Typically if you w...
Definition FpsStats.h:33
void sendVideoFrame(QVideoFrame)
Gets a videoframe.
void setFileNames(QUrl url)
Sets the filename used for saving a video frame set.
void sendFrame(QVideoFrame *)
void stopDumpFrames()
Stops the Dump frame modality (Save frame set).
void saveCurrentFrame()
Enable the save curretn frame mode.
void selfStartTimer()
void internalSendFrame(QVideoFrame)
void changeRefreshInterval(int ineterval)
Sets the refresh interval.
void freeze(bool check)
Enable/Disable the freeze mode.
void setFileName(QUrl url)
Sets the filename used for saving a video frame.
void sendFps(double portAvg, double portMin, double portMax, double dispAvg, double dispMin, double dispMax)
void startDumpFrames()
Enables the Dump frame modality (Save frame set).
void synchDisplayPeriod(bool check)
Enable/Disable the synch mode.
void synchDisplaySize(bool check)
Enable/Disable the synch size mode.
SignalHandler(QObject *parent=0)