-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotClient.cpp
127 lines (97 loc) · 3.16 KB
/
RobotClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "RobotClient.h"
/// @brief Constructor.
RobotClient::RobotClient (void) : mTCPI(TCPCLIENT, STREAMING_PORT_D),
mStreamingImage(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC3)
{
mTCPI.waitForServerConnection();
cvNamedWindow(STREAM_WINDOW_NAME, CV_WINDOW_AUTOSIZE);
}
/// @brief Deconstructor.
RobotClient::~RobotClient (void)
{
cvDestroyAllWindows();
}
void RobotClient::receiveFrame (void)
{
static uint32_t frameSize;
// read frame header and grow our buffer accordingly. The line is initially filled with nulls that must be eaten.
mTCPI.readBytes(&frameSize, 4);
while (frameSize == 0)
mTCPI.readBytes(&frameSize, 4);
mStreamingBuffer.resize(frameSize);
// read the frame body.
mTCPI.readBytes(&(mStreamingBuffer.front()), frameSize);
// Decode it.
const cv::Mat bufferWrapper(mStreamingBuffer, false);
mStreamingImage = cv::imdecode(bufferWrapper, -1);
}
int RobotClient::displayFrame (int number)
{
int retVal;
char filename[128];
sprintf(filename, "../Desktop/output_1/frame%d.png\0", number);
if (mStreamingImage.empty())
printf("ERROR: Image was empty, not displaying :(\n");
else
{
cv::imshow(STREAM_WINDOW_NAME, mStreamingImage);
cv::imwrite(filename, mStreamingImage);
}
retVal = cvWaitKey(5);
return retVal;
}
uint32_t RobotClient::getStreamingBufferSize (void) const
{
return mStreamingBuffer.size();
}
float getDifference_s (timespec* startingTime, timespec* endingTime)
{
time_t diff_sec = endingTime->tv_sec - startingTime->tv_sec;
long diff_nsec = endingTime->tv_nsec - startingTime->tv_nsec;
// account for annoying 'overflow' from nsec into sec.
if (diff_nsec < 0)
{
diff_sec -= 1;
diff_nsec += 1000000000;
}
float diff_in_seconds = diff_sec + (diff_nsec / 1000000000.0f);
return diff_in_seconds;
}
/// @brief Entry point.
int main (void)
{
int buttonPress;
timespec frameStart, frameEnd;
float timePerFrame, fps, kbps;
uint32_t i_fps=0, i_kbps=0;
float CAfps=0, CAkbps=0;
RobotClient rc;
int frame_count = 0;
while (1)
{
clock_gettime(CLOCK_MONOTONIC, &frameStart);
// bulk of the loop
rc.receiveFrame();
buttonPress = rc.displayFrame(frame_count++);
/* if (buttonPress >= 0)
break;*/
// calculate and print fps (and other stats).
clock_gettime(CLOCK_MONOTONIC, &frameEnd);
timePerFrame = getDifference_s(&frameStart, &frameEnd);
fps = 1.0f / timePerFrame;
kbps = (rc.getStreamingBufferSize() / 1024.0f) / timePerFrame;
i_fps += 1;
CAfps += (fps - CAfps) / (i_fps);
i_kbps += 1;
CAkbps += (kbps - CAkbps) / (i_kbps);
printf("FPS: %.1f\tThroughput: %.2f kBps \r", CAfps, CAkbps);
fflush(stdout);
if (fps > 60.0f)
{
printf("FPS exceeded upper bound, the server has probably gone down.\n");
break;
}
}
printf("\nExitting.\n");
return 0;
}