-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
336 lines (287 loc) · 12.5 KB
/
main.cpp
File metadata and controls
336 lines (287 loc) · 12.5 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
#include <deque>
#include <glib.h>
#include <libwebsockets.h>
#include <CxxPtr/CPtr.h>
#include <CxxPtr/GlibPtr.h>
#include "CxxPtr/libconfigDestroy.h"
#include "Helpers/ConfigHelpers.h"
#include "Helpers/LwsLog.h"
#include "RtspParser/Common.h"
#include "RtspSession/Log.h"
#include "RtStreaming/GstRtStreaming/Log.h"
#include "RtStreaming/GstRtStreaming/LibGst.h"
#include "Signalling/Log.h"
#include "Client/Log.h"
#include "Log.h"
#include "Monitor.h"
static const auto Log = MonitorLog;
static bool LoadConfig(Config* config)
{
const std::deque<std::string> configDirs = ::ConfigDirs();
if(configDirs.empty())
return false;
Config loadedConfig = *config;
for(const std::string& configDir: configDirs) {
const std::string configFile = configDir + "/monitor.conf";
if(!g_file_test(configFile.c_str(), G_FILE_TEST_IS_REGULAR)) {
Log()->info("Config file \"{}\" not found", configFile);
continue;
}
config_t config;
config_init(&config);
ConfigDestroy ConfigDestroy(&config);
Log()->info("Loading config from file \"{}\"", configFile);
if(!config_read_file(&config, configFile.c_str())) {
Log()->error("Failed to load config file. {}. {}:{}",
config_error_text(&config),
configFile,
config_error_line(&config));
return false;
}
config_setting_t* webrtcConfig = config_lookup(&config, "webrtc");
if(webrtcConfig && config_setting_is_group(webrtcConfig) != CONFIG_FALSE) {
const char* stunServer = nullptr;
if(config_setting_lookup_string(webrtcConfig, "stun-server", &stunServer) != CONFIG_FALSE) {
if(0 == g_ascii_strncasecmp(stunServer, "stun://", 7)) {
loadedConfig.webRTCConfig->iceServers.emplace_back(stunServer);
} else {
Log()->error("STUN server URL should start with \"stun://\"");
}
}
const char* turnServer = nullptr;
if(config_setting_lookup_string(webrtcConfig, "turn-server", &turnServer) != CONFIG_FALSE) {
if(0 == g_ascii_strncasecmp(turnServer, "turn://", 7)) {
loadedConfig.webRTCConfig->iceServers.emplace_back(turnServer);
} else {
Log()->error("TURN server URL should start with \"turn://\"");
}
}
int minRtpPort;
int rtpPortsCount;
if(config_setting_lookup_int(webrtcConfig, "min-rtp-port", &minRtpPort) != CONFIG_FALSE) {
if(
minRtpPort < std::numeric_limits<uint16_t>::min() ||
minRtpPort > std::numeric_limits<uint16_t>::max())
{
Log()->error(
"min-rtp-port should be in [{}, {}]",
std::numeric_limits<uint16_t>::min(),
std::numeric_limits<uint16_t>::max());
} else {
loadedConfig.webRTCConfig->minRtpPort = minRtpPort;
if(config_setting_lookup_int(webrtcConfig, "rtp-ports-count", &rtpPortsCount) != CONFIG_FALSE) {
if(minRtpPort < 1 || minRtpPort > std::numeric_limits<uint16_t>::max()) {
Log()->error(
"rtp-ports-count should be in [{}, {}]",
1,
std::numeric_limits<uint16_t>::max() - minRtpPort + 1);
} else {
loadedConfig.webRTCConfig->maxRtpPort = minRtpPort + rtpPortsCount - 1;
}
}
}
}
int relayTransportOnly = FALSE;
if(config_setting_lookup_bool(webrtcConfig, "relay-transport-only", &relayTransportOnly) != CONFIG_FALSE) {
loadedConfig.webRTCConfig->useRelayTransport = relayTransportOnly != FALSE;
}
}
config_setting_t* debugConfig = config_lookup(&config, "debug");
if(debugConfig && config_setting_is_group(debugConfig) != CONFIG_FALSE) {
int logLevel = 0;
if(config_setting_lookup_int(debugConfig, "log-level", &logLevel) != CONFIG_FALSE) {
if(logLevel > 0) {
loadedConfig.logLevel =
static_cast<spdlog::level::level_enum>(
spdlog::level::critical - std::min<int>(logLevel, spdlog::level::critical));
}
}
int lwsLogLevel = 0;
if(config_setting_lookup_int(debugConfig, "lws-log-level", &lwsLogLevel) != CONFIG_FALSE) {
if(lwsLogLevel > 0) {
loadedConfig.lwsLogLevel =
static_cast<spdlog::level::level_enum>(
spdlog::level::critical - std::min<int>(lwsLogLevel, spdlog::level::critical));
}
}
}
config_setting_t* recordServerConfig = config_lookup(&config, "record-server");
if(recordServerConfig && config_setting_is_group(recordServerConfig) != CONFIG_FALSE) {
const char* token = "";
config_setting_lookup_string(recordServerConfig, "token", &token);
int wsPort = signalling::DEFAULT_WS_PORT;
if(config_setting_lookup_int(recordServerConfig, "port", &wsPort) != CONFIG_FALSE) {
if(
wsPort < std::numeric_limits<uint16_t>::min() ||
wsPort > std::numeric_limits<uint16_t>::max()
) {
Log()->error(
"\"port\" value is invalid. In should be in [{}, {}]",
std::numeric_limits<uint16_t>::min(),
std::numeric_limits<uint16_t>::max());
wsPort = 0;
}
}
int loopbackOnly = FALSE;
config_setting_lookup_bool(recordServerConfig, "loopback-only", &loopbackOnly);
std::optional<signalling::Config> serverConfig;
if(wsPort) {
serverConfig = signalling::Config {
.bindToLoopbackOnly = loopbackOnly != FALSE,
.port = static_cast<unsigned short>(wsPort),
};
}
loadedConfig.source =
StreamSource {
StreamSource::Type::WebRTSP,
serverConfig,
{},
{},
token,
false,
};
}
config_setting_t* sourceConfig = !recordServerConfig ? config_lookup(&config, "source") : nullptr;
if(sourceConfig && config_setting_is_group(sourceConfig) != CONFIG_FALSE) {
const char* token = "";
config_setting_lookup_string(sourceConfig, "token", &token);
gboolean trackMotion = FALSE;
config_setting_lookup_bool(sourceConfig, "track-motion", &trackMotion);
int previewDuration = 0;
config_setting_lookup_int(sourceConfig, "motion-preview-time", &previewDuration);
StreamSource::Type sourceType = StreamSource::Type::WebRTSP;
const char* url;
bool useTls = false;
GCharPtr userPtr;
GCharPtr passwordPtr;
GCharPtr hostPtr;
unsigned short webrtspPort = 0;
std::string uri;
const bool onvif = config_setting_lookup_string(sourceConfig, "onvif", &url) != CONFIG_FALSE;
if(
onvif ||
config_setting_lookup_string(sourceConfig, "url", &url) != CONFIG_FALSE
) {
gchar* scheme;
gint port;
gchar* user;
gchar* password;
gchar* host;
gchar* path;
if(g_uri_split_with_user(
url,
G_URI_FLAGS_NONE,
&scheme,
&user,
&password,
nullptr, // auth_params
&host,
&port,
&path,
nullptr, // query
nullptr, // fragment
nullptr))
{
GCharPtr schemePtr(scheme);
userPtr.reset(user);
passwordPtr.reset(password);
hostPtr.reset(host);
GCharPtr pathPtr(path);
if(g_strcmp0(scheme, "webrtsp") == 0) {
useTls = false;
} else if(g_strcmp0(scheme, "webrtsps") == 0) {
useTls = true;
} else if(onvif) {
sourceType = StreamSource::Type::Onvif;
} else {
sourceType = StreamSource::Type::Url;
}
if(sourceType == StreamSource::Type::WebRTSP) {
if(port == -1) {
webrtspPort = useTls ?
signalling::DEFAULT_WSS_PORT :
signalling::DEFAULT_WS_PORT;
} else if(
port < std::numeric_limits<uint16_t>::min() ||
port > std::numeric_limits<uint16_t>::max()
) {
Log()->error(
"\"port\" value is invalid. In should be in [{}, {}]",
std::numeric_limits<uint16_t>::min(),
std::numeric_limits<uint16_t>::max());
} else {
webrtspPort = port;
}
if(path[0] == '/')
++path;
if(path[0] == '\0' || g_strcmp0(path, rtsp::WildcardUri) == 0)
uri = rtsp::WildcardUri;
else {
g_autofree gchar* escapedPath = g_uri_escape_string(path, nullptr, false);
uri = escapedPath;
}
} else if(sourceType == StreamSource::Type::Onvif) {
uri = url;
} else {
sourceType == StreamSource::Type::Url;
uri = url;
}
} else {
Log()->error("URL is invalid");
}
}
std::optional<client::Config> clientConfig;
if(sourceType == StreamSource::Type::WebRTSP && hostPtr.get() && webrtspPort) {
clientConfig = client::Config {
.server = hostPtr.get(),
.serverPort = webrtspPort,
.useTls = useTls,
};
}
loadedConfig.source =
StreamSource {
sourceType,
{},
clientConfig,
uri,
token,
trackMotion != FALSE,
std::chrono::seconds(std::max(3, previewDuration)),
};
}
config_setting_t* videoOutputConfig = config_lookup(&config, "video-output");
if(videoOutputConfig && config_setting_is_group(videoOutputConfig) != CONFIG_FALSE) {
gboolean showStats = FALSE;
if(config_setting_lookup_bool(videoOutputConfig, "show-stats", &showStats) != CONFIG_FALSE)
loadedConfig.videoOutput.showStats = showStats != FALSE;
gboolean sync = TRUE;
if(config_setting_lookup_bool(videoOutputConfig, "sync", &sync) != CONFIG_FALSE)
loadedConfig.videoOutput.sync = sync != FALSE;
}
}
bool success = true;
if(!loadedConfig.source) {
Log()->error("\"source\" config is missing");
success = false;
}
if(success) {
*config = loadedConfig;
Log()->info("Config loaded");
}
return success;
}
int main(int argc, char *argv[])
{
Config config {};
if(!LoadConfig(&config))
return -1;
InitLwsLogger(config.lwsLogLevel);
InitWsServerLogger(config.logLevel);
InitServerSessionLogger(config.logLevel);
InitWsClientLogger(config.logLevel);
InitClientSessionLogger(config.logLevel);
InitGstRtStreamingLogger(config.logLevel);
InitMonitorLogger(config.logLevel);
LibGst libGst;
return MonitorMain(config);
}