-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVideoToWebBridge.cpp
More file actions
363 lines (307 loc) · 11.7 KB
/
VideoToWebBridge.cpp
File metadata and controls
363 lines (307 loc) · 11.7 KB
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
/*
* VideoToWebBridge.cpp
* PyRIDE
*
* Created by Xun Wang on 13/06/2017
*
*/
#include <sys/time.h>
#include <boost/lexical_cast.hpp>
#include "async_web_server_cpp/http_reply.hpp"
#include "VideoToWebBridge.h"
namespace pyride {
using namespace pyride_remote;
VideoToWebBridge * VideoToWebBridge::s_pVideoToWebBridge = NULL;
static bool __verbose = false;
static const int kVideoStreamingGapTolerance = 15;
static bool web_logger( async_web_server_cpp::HttpServerRequestHandler forward,
const async_web_server_cpp::HttpRequest &request,
async_web_server_cpp::HttpConnectionPtr connection, const char* begin,
const char* end )
{
if (__verbose) {
INFO_MSG( "Handling Request: %s.\n", request.uri.c_str() );
}
try {
forward( request, connection, begin, end );
return true;
}
catch (std::exception &e) {
WARNING_MSG( "Error Handling Request: %s.\n", e.what() );
return false;
}
return false;
}
VideoToWebBridge::VideoToWebBridge() :
port_( 8281 ),
address_( "0.0.0.0" ),
server_threads_( 1 ),
isRunning_( false ),
dataTS_( -1 ),
dataStream_( NULL ),
handler_group_(
async_web_server_cpp::HttpReply::stock_reply( async_web_server_cpp::HttpReply::not_found ) )
{
handler_group_.addHandlerForPath("/", boost::bind(&VideoToWebBridge::handle_list_streams, this, _1, _2, _3, _4));
handler_group_.addHandlerForPath("/stream", boost::bind(&VideoToWebBridge::handle_stream, this, _1, _2, _3, _4));
//handler_group_.addHandlerForPath("/stream_viewer",
// boost::bind(&VideoToWebBridge::handle_stream_viewer, this, _1, _2, _3, _4));
//handler_group_.addHandlerForPath("/snapshot", boost::bind(&VideoToWebBridge::handle_snapshot, this, _1, _2, _3, _4));
pyExtension_ = NULL;
streaming_data_thread_ = NULL;
}
VideoToWebBridge::~VideoToWebBridge()
{
this->stop();
}
VideoToWebBridge * VideoToWebBridge::instance()
{
if (!s_pVideoToWebBridge) {
s_pVideoToWebBridge = new VideoToWebBridge();
}
return s_pVideoToWebBridge;
}
bool VideoToWebBridge::start()
{
if (isRunning_) {
WARNING_MSG( "Video to web service is already running.\n" );
return false;
}
dataStream_ = new RTPDataReceiver();
dataStream_->init( PYRIDE_VIDEO_STREAM_BASE_PORT + 4, true );
isRunning_ = true;
struct timeval now;
gettimeofday( &now, NULL );
dataTS_ = now.tv_sec;
streaming_data_thread_ = new boost::thread( &VideoToWebBridge::grabAndDispatchVideoStreamData, this );
try {
server_.reset(
new async_web_server_cpp::HttpServer(address_, boost::lexical_cast<std::string>(port_),
boost::bind( web_logger, handler_group_, _1, _2, _3, _4),
server_threads_ ));
}
catch (boost::exception& e) {
ERROR_MSG( "Exception when creating the web server! %s:%d.\n", address_.c_str(), port_);
return false;
}
server_->run();
if (pyExtension_) {
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyObject * arg = Py_BuildValue( "(O)", Py_True );
pyExtension_->invokeCallback( "onVideoFeedback", arg );
Py_DECREF( arg );
PyGILState_Release( gstate );
}
return true;
}
void VideoToWebBridge::stop( bool selfterminate )
{
if (!isRunning_)
return;
isRunning_ = false;
server_->stop();
if (streaming_data_thread_) {
if (!selfterminate)
streaming_data_thread_->join();
delete streaming_data_thread_;
streaming_data_thread_ = NULL;
}
if (dataStream_) {
delete dataStream_;
dataStream_ = NULL;
}
image_subscribers_.clear();
server_.reset();
if (pyExtension_) {
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyObject * arg = Py_BuildValue( "(O)", Py_False );
pyExtension_->invokeCallback( "onVideoFeedback", arg );
Py_DECREF( arg );
PyGILState_Release( gstate );
}
}
bool VideoToWebBridge::handle_stream( const async_web_server_cpp::HttpRequest &request,
async_web_server_cpp::HttpConnectionPtr connection, const char* begin,
const char* end )
{
std::string type = request.get_query_param_value_or_default( "type", "mjpeg" );
if (type.compare( "mjpeg" ) == 0) {
boost::mutex::scoped_lock lock( subscriber_mutex_ );
image_subscribers_.push_back( boost::shared_ptr<JpegImageStreamer>(new JpegImageStreamer( request, connection )) );
}
else {
async_web_server_cpp::HttpReply::stock_reply(async_web_server_cpp::HttpReply::not_found)( request, connection, begin,
end );
}
return true;
}
bool VideoToWebBridge::handle_snapshot( const async_web_server_cpp::HttpRequest &request,
async_web_server_cpp::HttpConnectionPtr connection, const char* begin,
const char* end )
{
/*
boost::shared_ptr<ImageStreamer> streamer(new JpegSnapshotStreamer(request, connection, nh_));
streamer->start();
*/
return true;
}
bool VideoToWebBridge::handle_stream_viewer( const async_web_server_cpp::HttpRequest &request,
async_web_server_cpp::HttpConnectionPtr connection, const char* begin,
const char* end )
{
/*
std::string type = request.get_query_param_value_or_default("type", "mjpeg");
if (type.compare( "mjpeg") == 0) {
async_web_server_cpp::HttpReply::builder(async_web_server_cpp::HttpReply::ok).header("Connection", "close").header(
"Server", "pyride_server").header("Content-type", "text/html;").write(connection);
std::stringstream ss;
ss << "<html><head><title>" << topic << "</title></head><body>";
ss << "<h1>" << topic << "</h1>";
ss << stream_types_[type]->create_viewer(request);
ss << "</body></html>";
connection->write(ss.str());
}
else {
async_web_server_cpp::HttpReply::stock_reply(async_web_server_cpp::HttpReply::not_found)(request, connection, begin,
end);
}
*/
return true;
}
bool VideoToWebBridge::handle_list_streams(const async_web_server_cpp::HttpRequest &request,
async_web_server_cpp::HttpConnectionPtr connection, const char* begin,
const char* end)
{
connection->write("<html>"
"<head><title>PyRIDE Video Feedback</title></head>"
"<body><h1>Remote user is currently streaming video images</h1>");
connection->write("<a href=\"/stream?topic=mjpeg\">Click here to view image</a>");
connection->write("</body></html>");
return true;
}
void VideoToWebBridge::grabAndDispatchVideoStreamData()
{
unsigned char * rawData = NULL;
unsigned char * data = NULL;
int dataSize = 0, rawDataSize = 0;
bool dataSizeChanged = false;
bool lostConnection = false;
if (!dataStream_)
return;
while (isRunning_) {
int gcount = 0;
do {
rawDataSize = dataStream_->grabData( &rawData, dataSizeChanged );
data = rawData;
dataSize = rawDataSize;
gcount++;
usleep( 1000 ); // 1ms
} while (dataSize == 0 && gcount < 100);
struct timeval now;
gettimeofday( &now, NULL );
if (dataSize == 0) {
if ((now.tv_sec - dataTS_) > kVideoStreamingGapTolerance) {
ERROR_MSG( "Missing video stream for %d second. Assuming the connection is dead. Stop!\n",
kVideoStreamingGapTolerance );
lostConnection = true;
break;
}
else {
continue;
}
}
dataTS_ = now.tv_sec;
double ts = double(now.tv_sec) + (double(now.tv_usec) / 1000000.0);
//DEBUG_MSG( "Got video data %d %lu %lu.\n", dataSize, now.tv_sec, now.tv_usec );
std::vector<unsigned char> dispatchdata( data, data+dataSize );
{
boost::mutex::scoped_lock lock( subscriber_mutex_, boost::try_to_lock );
if (lock) {
typedef std::vector<boost::shared_ptr<JpegImageStreamer> >::iterator itr_type;
//itr_type new_end = std::remove_if(image_subscribers_.begin(), image_subscribers_.end(),
// boost::bind(&ImageStreamer::isInactive, _1));
for (itr_type iter = image_subscribers_.begin(); iter != image_subscribers_.end();) {
bool inerror = false;
try {
if (dispatchdata.size() > 0) { // don't know why sometime it send zero size.
//DEBUG_MSG( "Sending data to chip monitor %d %.6lf.\n", (int)dispatchdata.size(), ts );
(*iter)->sendImage( ts, dispatchdata );
}
}
catch (boost::system::system_error &e) {
// happens when client disconnects
INFO_MSG( "client disconnect: %s.\n", e.what() );
inerror = true;
}
catch (std::exception &e) {
ERROR_MSG( "send image exception: %s.\n", e.what() );
inerror = true;
}
catch (...) {
ERROR_MSG( "send image general exception.\n" );
inerror = true;
}
if (inerror) {
iter = image_subscribers_.erase( iter );
}
else {
++iter;
}
}
}
}
}
if (lostConnection) {
this->stop( true );
}
}
JpegImageStreamer::JpegImageStreamer( const async_web_server_cpp::HttpRequest &request,
async_web_server_cpp::HttpConnectionPtr connection ) :
stream_( connection )
{
stream_.sendInitialHeader();
}
void JpegImageStreamer::sendImage( const double time, std::vector<unsigned char> & data )
{
stream_.sendPartAndClear( time, "image/jpeg", data );
}
MultipartStream::MultipartStream( async_web_server_cpp::HttpConnectionPtr& connection, const std::string & boundry )
: connection_(connection), boundry_(boundry) {}
void MultipartStream::sendInitialHeader() {
async_web_server_cpp::HttpReply::builder(async_web_server_cpp::HttpReply::ok).header("Connection", "close").header(
"Server", "pyride_server").header("Cache-Control",
"no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0").header(
"Pragma", "no-cache").header("Content-type", "multipart/x-mixed-replace;boundary="+boundry_).header(
"Access-Control-Allow-Origin", "*").write(connection_);
connection_->write("--"+boundry_+"\r\n");
}
void MultipartStream::sendPartHeader( const double time, const std::string & type, size_t payload_size) {
char stamp[20];
sprintf( stamp, "%.06lf", time );
boost::shared_ptr<std::vector<async_web_server_cpp::HttpHeader> > headers(
new std::vector<async_web_server_cpp::HttpHeader>());
headers->push_back( async_web_server_cpp::HttpHeader("Content-type", type ) );
headers->push_back( async_web_server_cpp::HttpHeader("X-Timestamp", stamp ) );
headers->push_back(
async_web_server_cpp::HttpHeader("Content-Length", boost::lexical_cast<std::string>(payload_size)));
connection_->write(async_web_server_cpp::HttpReply::to_buffers(*headers), headers);
}
void MultipartStream::sendPartFooter() {
connection_->write("\r\n--"+boundry_+"\r\n");
}
void MultipartStream::sendPartAndClear(const double time, const std::string& type,
std::vector<unsigned char> &data) {
sendPartHeader(time, type, data.size());
connection_->write_and_clear(data);
sendPartFooter();
}
void MultipartStream::sendPart(const double time, const std::string& type,
const boost::asio::const_buffer &buffer,
async_web_server_cpp::HttpConnection::ResourcePtr resource) {
sendPartHeader(time, type, boost::asio::buffer_size(buffer));
connection_->write(buffer, resource);
sendPartFooter();
}
} // namespace pyride