forked from robofit/drone_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDS.cpp
More file actions
195 lines (161 loc) · 4.82 KB
/
Copy pathDDS.cpp
File metadata and controls
195 lines (161 loc) · 4.82 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
#include <iostream>
#include <exception>
#include <boost/asio.hpp>
#include <boost/exception/all.hpp>
#include <boost/program_options.hpp>
#include <DDS/config.hpp>
#include <DDS/core/logger.hpp>
#include <DDS/core/settings.hpp>
#include <DDS/core/media/manager.hpp>
#ifdef COMPILE_WEBSOCKET
#include <DDS/websocket/server.hpp>
std::shared_ptr<WebsocketServer> websocket_server_p;
#endif
#ifdef COMPILE_RECORDER
#include <DDS/recorder/recorder.hpp>
extern "C"
{
#include <libavformat/avformat.h>
}
#endif
#ifdef COMPILE_VEHICLE_DETECTION
#include <DDS/vehicle_detection/vehicle_detection.hpp>
#endif
#ifdef COMPILE_RTMP
#include <DDS/core/tcp_server.hpp>
#include <DDS/rtmp/rtmp_session.hpp>
class my_rtmp_session : public rtmp_session
{
public:
my_rtmp_session(tcp::socket socket)
: rtmp_session(std::move(socket)) {}
private:
void on_stream_create(ClientID_t cid)
{
auto& sett = settings::get();
#ifdef COMPILE_RECORDER
if(sett.dint["record_force"])
media_manager::get().pipe(cid)->add_writer(std::make_shared<media_recorder>(cid_to_hex(cid) + ".mp4"));
#endif
#ifdef COMPILE_VEHICLE_DETECTION
// media_manager::get().pipe(cid)->add_writer(std::make_shared<VehicleDetector>(websocket_server_p, "cars.xml"));
#endif
}
};
std::shared_ptr<tcp_server<my_rtmp_session>> rtmp_server_p;
#endif
int run()
{
auto& sett = settings::get();
boost::asio::io_context io_context;
boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
signals.async_wait([&io_context](const boost::system::error_code& error, int signum)
{
if(signum == SIGINT)
{
LOG(INFO) << "<DDS> " << "SIGINT received. Stopping server.";
}
else if(signum == SIGTERM)
{
LOG(INFO) << "<DDS> " << "SIGTERM received. Stopping server.";
}
io_context.stop();
});
#ifdef COMPILE_WEBSOCKET
try
{
websocket_server_p = std::make_shared<WebsocketServer>(&io_context);
websocket_server_p->run(sett.dint["websocket_port"]);
}
catch (websocketpp::exception const& e)
{
LOG(ERROR) << "<DDS> " << e.what();
return -1;
}
catch (const std::exception& e)
{
LOG(ERROR) << "<DDS> " << e.what();
return -1;
}
#endif
#ifdef COMPILE_RTMP
try
{
rtmp_server_p = std::make_shared<tcp_server<my_rtmp_session>>(io_context);
rtmp_server_p->run(sett.dint["rtmp_port"]);
}
catch (boost::exception &e)
{
LOG(ERROR) << "<DDS> " << boost::diagnostic_information(e);
return -1;
}
catch (const std::exception& e)
{
LOG(ERROR) << "<DDS> " << e.what();
return -1;
}
#endif
try
{
io_context.run();
}
catch (boost::exception &e)
{
LOG(ERROR) << "<DDS> " << boost::diagnostic_information(e);
return -1;
}
catch (const std::exception& e)
{
LOG(ERROR) << "<DDS> " << e.what();
return -1;
}
media_manager::get().clear();
return 0;
}
namespace po = boost::program_options;
int main(int argc, char* argv[])
{
auto& sett = settings::get();
int log_level;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("log,l", po::value<int>(&log_level)->default_value(2), "log level: 3-ERROR,2-WARNING,1-INFO,0-DEBUG")
#ifdef COMPILE_WEBSOCKET
("websocket_port,wp", po::value<int>(&sett.dint["websocket_port"])->default_value(5555), "Websocket Server port")
#endif
#ifdef COMPILE_RTMP
("rtmp_port,rp", po::value<int>(&sett.dint["rtmp_port"])->default_value(1935), "RTMP Server port")
#endif
#ifdef COMPILE_RECORDER
("rec_width,rcw", po::value<int>(&sett.dint["record_width"])->default_value(1920), "record width")
("rec_height,rch", po::value<int>(&sett.dint["record_height"])->default_value(1080), "record height")
("rec_fps,rcf", po::value<int>(&sett.dint["record_fps"])->default_value(20), "record fps")
("rec_bitrate,rcbr", po::value<int>(&sett.dint["record_bitrate"])->default_value(3500), "record bitrate in kb/s")
("rec_f,r", po::value<int>(&sett.dint["record_force"])->default_value(0), "force stream recording")
#endif
;
po::variables_map vm;
try
{
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
}
catch (po::error_with_option_name &e)
{
LOG(ERROR) << "<DDS> " << e.what();
return -1;
}
#ifdef COMPILE_RECORDER
sett.dint["record_av_codec_id"] = AVCodecID::AV_CODEC_ID_H264;
sett.dint["record_av_pix_fmt_id"] = AVPixelFormat::AV_PIX_FMT_YUV420P;
sett.dint["record_gop_size"] = 12;
#endif
sett.set_loglevel(log_level);
if(vm.count("help"))
{
std::cout << desc << std::endl;
return 0;
}
return run();
}