-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDS.cpp
More file actions
232 lines (193 loc) · 6.42 KB
/
Copy pathDDS.cpp
File metadata and controls
232 lines (193 loc) · 6.42 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
#include <iostream>
#include <exception>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#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/client_pool.hpp>
#include <DDS/core/media/manager.hpp>
#include <DDS/core/flight_data/manager.hpp>
#ifdef COMPILE_WEBSOCKET
#include <DDS/websocket/server.hpp>
std::shared_ptr<WebsocketServer> websocket_server_p;
#endif
#ifdef COMPILE_RECORDER
#include <DDS/recorder/controller.hpp>
extern "C"
{
#include <libavformat/avformat.h>
}
#endif
#ifdef COMPILE_FLIGHT_DATABASE
#include <DDS/flight_database/controller.hpp>
#endif
#ifdef COMPILE_VEHICLE_DETECTION
#include <DDS/vehicle_detection/controller.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.dbool["record_force"])
{
auto curr_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::ostringstream oss;
oss << std::put_time(std::localtime(&curr_time), "%Y%m%d-%H%M%S");
oss << "-";
oss << cid_to_hex(cid);
oss << ".mp4";
media_manager::get().pipe(cid)->add_writer(std::make_shared<media_recorder>(oss.str()));
}
#endif
}
};
std::shared_ptr<tcp_server<my_rtmp_session>> rtmp_server_p;
#endif
int run()
{
auto& sett = settings::get();
auto& fdm = flight_data_manager::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_RECORDER
fdm.add(std::make_shared<RecorderController>());
#endif
#ifdef COMPILE_FLIGHT_DATABASE
fdm.add(std::make_shared<FlightDatabaseController>());
#endif
#ifdef COMPILE_VEHICLE_DETECTION
fdm.add(std::make_shared<VehicleDetectionController>());
#endif
#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;
}
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<bool>(&sett.dbool["record_force"])->default_value(false), "force stream recording")
#endif
#ifdef COMPILE_FLIGHT_DATABASE
("fdb_h", po::value<std::string>(&sett.dstring["mariadb_hostname"])->default_value("localhost"), "mariadb host name")
("fdb_u", po::value<std::string>(&sett.dstring["mariadb_username"])->default_value("root"), "mariadb user name")
("fdb_p", po::value<std::string>(&sett.dstring["mariadb_password"])->default_value("root"), "mariadb password")
("fdb_s", po::value<std::string>(&sett.dstring["mariadb_database"])->default_value("DDS"), "mariadb scheme/database")
#endif
#ifdef COMPILE_VEHICLE_DETECTION
("vdet_fp", po::value<std::string>(&sett.dstring["vehicle_detection_filepath"])->default_value("cars.xml"), "vehicle detection cascade file path")
#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();
}