forked from chenxiaoqino/udp-image-streaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer-r.cpp
148 lines (121 loc) · 5.37 KB
/
Server-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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* C++ UDP socket server for live image upstreaming
* Modified from http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/UDPEchoServer.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 "ThreadPool.h"
#include "PracticalSocket.h" // For UDPSocket and SocketException
#include <iostream> // For cout and cerr
#include <cstdlib> // For atoi()
#include <chrono>
#include <fstream> // std::ofstream
#include <mutex>
#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[]) {
ThreadPool pool(4);
std::vector<std::future<void> > results;
if (argc != 2) { // Test for correct number of parameters
cerr << "Usage: " << argv[0] << " <Server Port>" << endl;
exit(1);
}
unsigned short servPort = atoi(argv[1]); // First arg: local port
string foreignAddress;
unsigned short foreignPort;
try {
UDPSocket sock(servPort);
pool.enqueue([&sock, &foreignAddress, &foreignPort] {
while (1) {
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
// Block until receive message from a client
do {
recvMsgSize = sock.recvFrom(buffer, BUF_LEN, foreignAddress, foreignPort);
} while (recvMsgSize > sizeof(int));
}
});
pool.enqueue([&sock, &foreignAddress, &foreignPort] {
int jpegqual = ENCODE_QUALITY; // Compression Parameter
Mat frame, send;
vector<uchar> encoded;
VideoCapture cap("test.mp4"); // Grab the camera
//cap.set(CV_CAP_PROP_FRAME_HEIGHT, 360);
//cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
int fps = cap.get(CV_CAP_PROP_FPS);
int frames_count = cap.get(CV_CAP_PROP_FRAME_COUNT);
int count = 0;
//namedWindow("send", CV_WINDOW_AUTOSIZE);
if (!cap.isOpened()) {
cerr << "OpenCV Failed to open camera";
exit(1);
}
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::time_point end;
while (1) {
if (foreignAddress.empty()) {
cout << "ForeignAddress empty..." << endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
continue;
}
cap >> frame;
if (count == frames_count-1) {
cap.set(CV_CAP_PROP_POS_FRAMES, 1);
count = 0;
}
count++;
if (frame.size().width == 0)continue;//simple integrity check; skip erroneous data...
resize(frame, send, Size(FRAME_WIDTH, FRAME_HEIGHT), 0, 0, INTER_LINEAR);
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(jpegqual);
imencode(".jpg", send, encoded, compression_params);
//imshow("send", send);
int total_pack = 1 + (encoded.size() - 1) / PACK_SIZE;
Mat rawData = Mat(1, PACK_SIZE * total_pack, CV_8UC1, encoded.data());
// std::cout<<rawData<<std::endl;
// std::ofstream ofs ("test1.jpg", std::ofstream::out);
// ofs.write((char*)rawData.data,PACK_SIZE * total_pack*sizeof(uchar));
// ofs.close();
// break;
int ibuf[1];
ibuf[0] = total_pack;
sock.sendTo(ibuf, sizeof(int), foreignAddress, foreignPort);
for (int i = 0; i < total_pack; i++)
sock.sendTo(&encoded[i * PACK_SIZE], PACK_SIZE, foreignAddress, foreignPort);
//waitKey(1000/fps);
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;
}
});
for (;;) {
}
} catch (SocketException &e) {
cerr << e.what() << endl;
exit(1);
}
return 0;
}