forked from chenxiaoqino/udp-image-streaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient-r.cpp
106 lines (87 loc) · 3.92 KB
/
Client-r.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
/*
* C++ UDP socket client for live image upstreaming
* Modified from http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/UDPEchoClient.cpp
* Copyright (C) 2015
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "PracticalSocket.h" // For UDPSocket and SocketException
#include <iostream> // For cout and cerr
#include <cstdlib> // For atoi()
#include <chrono>
#define BUF_LEN 65540 // Larger than maximum UDP packet size
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
#include "config.h"
int main(int argc, char * argv[]) {
if ((argc < 4) || (argc > 4)) { // Test for correct number of arguments
cerr << "Usage: " << argv[0] << " <Server> <Server Port>\n";
exit(1);
}
string servAddress = argv[1]; // First arg: server address
unsigned short servPort = Socket::resolveService(argv[2], "udp");
namedWindow("recv", CV_WINDOW_AUTOSIZE);
try {
UDPSocket sock;
char ibuf[1];
ibuf[0] = *argv[3];
sock.sendTo(ibuf, sizeof(uchar), servAddress, servPort);
char buffer[BUF_LEN]; // Buffer for echo string
int recvMsgSize; // Size of received message
string sourceAddress; // Address of datagram source
unsigned short sourcePort; // Port of datagram source
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::time_point end;
while (1) {
// Block until receive message from a client
do {
recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
} while (recvMsgSize > sizeof(int));
int total_pack = ((int *) buffer)[0];
cout << "expecting length of packs:" << total_pack << endl;
char *longbuf = new char[PACK_SIZE * total_pack];
for (int i = 0; i < total_pack; i++) {
recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
if (recvMsgSize != PACK_SIZE) {
cerr << "Received unexpected size pack:" << recvMsgSize << endl;
continue;
}
memcpy(&longbuf[i * PACK_SIZE], buffer, PACK_SIZE);
}
cout << "Received packet from " << sourceAddress << ":" << sourcePort << endl;
Mat rawData = Mat(1, PACK_SIZE * total_pack, CV_8UC1, longbuf);
//std::cout<<rawData<<std::endl;
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR);
if (frame.size().width == 0) {
cerr << "decode failure!" << endl;
continue;
}
imshow("recv", frame);
free(longbuf);
waitKey(1);
end = std::chrono::high_resolution_clock::now();
auto cost = (int) std::chrono::duration_cast<std::chrono::milliseconds>(
end - start).count();
cout << "effective FPS:" << 1.0/(cost/1000.0) << " \tkB/s:" << (PACK_SIZE * total_pack / (cost/1000.0) / 1024)
<< endl;
start = end;
}
} catch (SocketException & e) {
cerr << e.what() << endl;
exit(1);
}
return 0;
}