This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Stream.cpp
executable file
·395 lines (327 loc) · 12.9 KB
/
Stream.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <fcntl.h>
#include "capture/Stream.h"
namespace kerberos
{
// ----------------------------------
// Configure stream thread settings
void Stream::configureStream(StringMap & settings)
{
//read port from settings
int enabled = (settings.at("streams.Mjpg.enabled") == "true");
int port = std::atoi(settings.at("streams.Mjpg.streamPort").c_str());
int quality = std::atoi(settings.at("streams.Mjpg.quality").c_str());
int fps = std::atoi(settings.at("streams.Mjpg.fps").c_str());
std::string username = settings.at("streams.Mjpg.username");
std::string password = settings.at("streams.Mjpg.password");
//use port up to well known ports range
if(port >= 1024)
{
//TODO: here it would be nice to check if port is valid and free
m_enabled = enabled;
m_username = username;
m_password = password;
m_streamPort = port;
m_quality = quality;
wait = 1. / fps;
}
else
{
LERROR << "Settings: can't use invalid port";
//TODO: manage invalid port error
}
}
bool Stream::hasClients()
{
return (clients.size() > 0);
}
bool Stream::release()
{
for(int i = 0; i < clients.size(); i++)
{
shutdown(clients[i], 2);
FD_CLR(clients[i],&master);
}
clients.clear();
if (sock != INVALID_SOCKET)
{
shutdown(sock, 2);
close(sock);
}
sock = (INVALID_SOCKET);
LINFO << "Stream: Succesfully closed streaming";
return false;
}
bool Stream::open()
{
if(m_enabled)
{
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int reuse = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse));
struct timeval timeout;
timeout.tv_sec = 2;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (struct timeval *)&timeout,sizeof(timeout));
SOCKADDR_IN address;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_family = AF_INET;
address.sin_port = htons(m_streamPort);
while(bind(sock, (SOCKADDR*) &address, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
LERROR << "Stream: couldn't bind sock";
release();
usleep(1000*10000);
sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
while(listen(sock, 2) == SOCKET_ERROR)
{
LERROR << "Stream: couldn't listen on sock";
usleep(1000*10000);
}
FD_SET(sock, &master);
LINFO << "Stream: Configured stream on port " << helper::to_string(m_streamPort) << " with quality: " << helper::to_string(m_quality);
return true;
}
return false;
}
bool Stream::isOpened()
{
return sock != INVALID_SOCKET;
}
std::map<std::string, std::string> Stream::getRequestInfo(SOCKET client)
{
std::map<std::string, std::string> info;
// We need some information from the client
// Get data from the request.
char method[10]={'\0'};
char url[512]={'\0'};
char protocol[10]={'\0'};
unsigned short int length = 1023;
char buffer[1024] = {'\0'};
int nread = read(client, buffer, length);
sscanf (buffer, "%9s %511s %9s", method, url, protocol);
info["buffer"] = (std::string) buffer;
info["method"] = (std::string) method;
info["url"] = (std::string) url;
info["protocol"] = (std::string) protocol;
return info;
}
bool Stream::authenticate(std::map<std::string, std::string> & requestInfo)
{
// Check if we have authentication enabled.
// verify if both username and password is set.
if(m_username != "" && m_password != "")
{
std::string credentials = m_username + ":" + m_password;
std::string credentialsBase64 = base64_encode((unsigned char const*) credentials.c_str(), credentials.length());
std::string payload = requestInfo.find("buffer")->second;
// We'll check if there is an authentication header
// in the request send by the user.
int findAuthenticationInHeader = payload.find("Basic", 0);
if(findAuthenticationInHeader != std::string::npos)
{
// We need to find the token in the payload, therefore
// we'll split the payload by a spaces, and add them to
// a vector sequentially. After this we'll search for Basic again,
// and if found we take the next entry in the vector (as this will be the token).
bool tokenFound = false;
std::string token = "";
std::istringstream payloadBySpaces(payload);
std::string line;
while (std::getline(payloadBySpaces, line, ' ' ) && !tokenFound)
{
if(line == "Basic")
{
std::getline(payloadBySpaces, token, ' ');
token = token.substr(0 ,token.find('\r'));
tokenFound = true;
}
}
if(!tokenFound)
{
//LERROR << "Stream: no token found in client request.";
LERROR << "Stream: no token found in client request.";
return false;
}
else if(token != credentialsBase64)
{
//LERROR << "Stream: token found, but it's not correct.";
LERROR << "Stream: token found, but it's not correct.";
return false;
}
return true;
}
else
{
//LERROR << "Stream: no token found in client request.";
LERROR << "Stream: no token found in client request.";
return false;
}
return false;
}
return true;
}
bool Stream::connect()
{
fd_set rread = master;
struct timeval to = {0,m_timeout};
SOCKET maxfd = sock+1;
if(select( maxfd, &rread, NULL, NULL, &to ) <= 0)
return true;
int addrlen = sizeof(SOCKADDR);
SOCKADDR_IN address = {0};
SOCKET client = accept(sock, (SOCKADDR*)&address, (socklen_t*) &addrlen);
if (client == SOCKET_ERROR)
{
//LERROR << "Stream: couldn't accept connection on sock";
//LERROR << "Stream: reopening master sock";
LERROR << "Stream: couldn't accept connection on sock";
LERROR << "Stream: reopening master sock";
release();
open();
return false;
}
struct timeval timeout;
timeout.tv_sec = 2;
timeout.tv_usec = 0;
setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&timeout, sizeof(timeout));
maxfd=(maxfd>client?maxfd:client);
FD_SET( client, &master );
std::map<std::string, std::string> requestInfo = getRequestInfo(client);
// If requesting the favicon, don't do anything..
if(requestInfo["url"] == "/favicon.ico")
{
return false;
}
bool isAuthenticated = authenticate(requestInfo);
if(isAuthenticated)
{
_write( client,"HTTP/1.0 200 OK\r\n"
"Server: Mozarella/2.2\r\n"
"Accept-Range: bytes\r\n"
"Max-Age: 0\r\n"
"Expires: 0\r\n"
"Cache-Control: no-cache, private\r\n"
"Pragma: no-cache\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-Type: multipart/x-mixed-replace; boundary=mjpegstream\r\n"
"\r\n",0);
LINFO << "Stream: authentication success";
LINFO << "Stream: opening socket for new client.";
clients.push_back(client);
packetsSend[client] = 0;
return true;
}
else
{
const char * unauthorized =
"HTTP/1.0 401 Authorization Required\r\n"
"Access-Control-Allow-Origin: *\r\n"
"WWW-Authenticate: Basic realm=\"Kerberos.io Security Access\"\r\n\r\n\r\n";
// Request for authorization
char response[1024]={'\0'};
snprintf (response, sizeof (response), unauthorized, requestInfo["method"].c_str());
_write (client, response, strlen(response));
//LINFO << "Stream: authentication failed.";
LOG(INFO) << "Stream: authentication failed.";
FD_CLR(client, &master);
shutdown(client, 2);
close(client);
return false;
}
}
void Stream::writeRAW(uint8_t* data, int32_t length)
{
try
{
// Check if some clients connected
// if not drop this shit..
if(clients.size()==0) return;
if(length > 0)
{
for(int i = 0; i < clients.size(); i++)
{
packetsSend[clients[i]]++;
int error = 0;
socklen_t len = sizeof (error);
int retval = getsockopt(clients[i], SOL_SOCKET, SO_ERROR, &error, &len);
int socketState = 0;
if (retval == 0 && error == 0)
{
char head[400];
sprintf(head,"--mjpegstream\r\nContent-Type: image/jpeg\r\nContent-Length: %lu\r\n\r\n",length);
socketState = _write(clients[i],head,0);
retval = getsockopt(clients[i], SOL_SOCKET, SO_ERROR, &error, &len);
if (retval == 0 && error == 0)
{
socketState = _write(clients[i], (char*) data, length);
}
}
if (retval != 0 || error != 0 || socketState == -1)
{
FD_CLR(clients[i],&master);
shutdown(clients[i], 2);
close(clients[i]);
std::vector<int>::iterator position = std::find(clients.begin(), clients.end(), clients[i]);
if (position != clients.end())
{
clients.erase(position);
}
}
}
}
}
catch(cv::Exception & ex){}
}
void Stream::write(Image image)
{
try
{
// Check if some clients connected
// if not drop this shit..
if(clients.size()==0) return;
// Encode the image
cv::Mat frame = image.getImage();
if(frame.cols > 0 && frame.rows > 0)
{
std::vector<uchar>outbuf;
std::vector<int> params;
params.push_back(cv::IMWRITE_JPEG_QUALITY);
params.push_back(m_quality);
cv::imencode(".jpg", frame, outbuf, params);
int outlen = outbuf.size();
for(int i = 0; i < clients.size(); i++)
{
packetsSend[clients[i]]++;
int error = 0;
socklen_t len = sizeof (error);
int retval = getsockopt(clients[i], SOL_SOCKET, SO_ERROR, &error, &len);
int socketState = 0;
char buffer[1024];
if (retval == 0 && error == 0)
{
char head[400];
sprintf(head,"--mjpegstream\r\nContent-Type: image/jpeg\r\nContent-Length: %lu\r\n\r\n",outlen);
socketState = _write(clients[i],head,0);
retval = getsockopt(clients[i], SOL_SOCKET, SO_ERROR, &error, &len);
if (retval == 0 && error == 0)
{
socketState = _write(clients[i],(char*)(&outbuf[0]),outlen);
}
}
if (retval != 0 || error != 0 || socketState == -1)
{
shutdown(clients[i], 2);
FD_CLR(clients[i],&master);
std::vector<int>::iterator position = std::find(clients.begin(), clients.end(), clients[i]);
if (position != clients.end())
{
clients.erase(position);
}
}
}
}
}
catch(cv::Exception & ex){}
}
}