-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.jsonnet
More file actions
355 lines (323 loc) · 13.7 KB
/
common.jsonnet
File metadata and controls
355 lines (323 loc) · 13.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
local common = {
local constants = import 'constants.jsonnet',
mk_webrtc: function (name, appWebrtc) {
type: "webrtc",
name: "webrtc_" + name,
stunsrv: 3478,
ice_servers: [
{
"urls": "stun:demo.viinex.com:3478"
}
],
meta: {
"ice_servers": [
{
"urls": "stun:demo.viinex.com:3478"
}
],
"stunsrv": 3478
},
key: constants.tls.key,
certificate: constants.tls.certificate,
events: true,
} + common.webrtcAppOverrides(appWebrtc),
// construct a mixin {keyName: value} from something stored inside of "obj"
// this something may be a boolean, in which case value is taken from env var
// or it can be immediate value. The "immediate" value can itself be
// a construct of "$(env...)" -- does not matter here, as it's interpreted by viinex
// at cluster startup.
valueFromEnvOrInlineF(keyName, obj, name, envName, transform):
if name in obj
then (if std.isBoolean(obj[name])
then (if obj[name]
then {[keyName]: transform("$(env@json." + envName + ")")}
else {})
else {[keyName]: transform(obj[name])}) // isBoolean
else {},
valueFromEnvOrInline(keyName, obj, name, envName):
common.valueFromEnvOrInlineF(keyName, obj, name, envName, function(x) x),
webrtcAppOverrides(appWebrtc):
common.valueFromEnvOrInline("ice_servers",
appWebrtc, "ice_servers", "ICE_SERVERS") +
common.valueFromEnvOrInline("ice_candidates_filter",
appWebrtc, "ice_candidates_filter", "ICE_CANDIDATES_FILTER") +
common.valueFromEnvOrInline("ice_candidates_static",
appWebrtc, "ice_candidates_static", "ICE_CANDIDATES_STATIC") +
common.valueFromEnvOrInline("port_range",
appWebrtc, "port_range", "PORT_RANGE") +
common.valueFromEnvOrInlineF("meta",
appWebrtc, "meta_ice_servers", "META_ICE_SERVERS", function (x) {ice_servers: x}),
mk_mediafile: function (name, path) {
type: "mediasourceplugin",
name: name,
dynamic: false,
library: if constants.isWindows then "vnxvideo.dll" else "libvnxvideo.so",
factory : "create_file_media_source",
init : {
file: path
}
},
local parseRtspUrl (u) =
local p = {
at: std.findSubstr("@", u),
semicolon: std.findSubstr(":", u),
rtsp: std.startsWith(u, "rtsp://"),
}; if !p.rtsp
then error "RTSP URL should start with rtsp://, was given: "+u
else {
url: if std.length(p.at) > 0 then "rtsp://"+std.substr(u, p.at[0]+1, std.length(u)) else u,
auth: if std.length(p.at) > 0
then [std.substr(u, 7, p.semicolon[1]-7),
std.substr(u, p.semicolon[1]+1, p.at[0]-p.semicolon[1]-1)]
else null
},
local cleanupOrig(o) = o, //std.prune (o + { __orig: null }), //std.objectRemoveKey(o, "__orig"), // <- absent in 0.20, crashes in 0.21
mk_cam_name: function (clusterId, app, camId)
if ("preserveSourceIds" in app) && app["preserveSourceIds"]
then camId
else "cam_" + clusterId + "_" + camId,
mk_cam_name_sub: function (clusterId, app, camId, suffix) common.mk_cam_name(clusterId, app, camId) + "_" + suffix,
mk_rtsp: function (name, url, auth) {
type: "rtsp",
name: name,
local urlparsed = parseRtspUrl(url),
url: urlparsed.url,
auth: if auth != null then auth else urlparsed.auth,
rtpstats: true,
transport: ["tcp"]
},
mk_onvif: function (name, addr, auth, profile) {
type: "onvif",
name: name,
auth: auth,
profile: profile,
enable: ["video", "audio", "events", "ptz"],
rtpstats: true,
transport: ["udp"],
} + if std.startsWith(addr, "http://") || std.startsWith(addr, "https://")
then { url: addr }
else { host: addr },
mk_recctl: function(cid, name, prerecord, postrecord) {
type: "recctl",
name: "recctl_" + cid + "_" + name,
prerecord: prerecord,
postrecord: postrecord
},
mk_rule_motion: function(cid, name) {
type: "rule",
name: "rule_" + cid + "_" + name,
filter: ["MotionAlarm"]
},
// renderer for video analytics
mk_renderer_name: function (cid, name) "rend_" + cid + "_" + name,
mk_renderer: function (cid, name, refreshRate) {
type: "renderer",
name: common.mk_renderer_name(cid, name),
share: true,
refresh_rate: refreshRate,
layout: {
size: [0, 0],
viewports: [
{
input: 0,
dst: [0,0,1,1]
}
]
},
encoder: {
type: "cpu",
quality: "small_size",
profile: "baseline",
preset: "ultrafast",
dynamic: true
}
},
mk_webserver: function (name) {
type: "webserver",
name: "web_" + name,
port: constants.webserverPort,
staticpath: if constants.isWindows then "c:/Program Files/Viinex/share/web" else "/usr/share/viinex/web/browser/en",
"rem tls":{
key: constants.tls.key,
certificate: constants.tls.certificate
},
cors: "*",
clusters: true,
},
mk_storage: function (name, app) {
type: "storage",
name: "stor_" + name,
folder: constants.storageRoot + "/" + self.name,
filesize: 4,
limits: {
keep_free_percents: 20
} + if "recordRetainDaysMax" in app && app.recordRetainDaysMax != null
then { max_depth_abs_hours: app.recordRetainDaysMax * 24 }
else {}
},
mk_rtspsrv: function (name, port) {
type: "rtspsrv",
name: "rtspsrv_" + name,
port: port
},
mk_zmq_exporter: function (name, scheme, behavior, endpoint) {
type: "zmq.export",
name: "zmq_export_" + name,
scheme: scheme,
socket_behavior: if behavior == null then "bind" else behavior,
zmq_endpoint: if endpoint == null then constants.refDeployParam("ZMQ_ENDPOINT") else endpoint,
},
mk_metrics: function (cid) {
type: "metrics",
name: "metrics",
labels: [['cluster', cid]],
},
mk_db_sqlite: function (name) {
type: "sqlite",
name: "db_" + name,
connect: { database: constants.storageRoot + "/" + "db_" + name + ".sqlite3" },
events: {
store: true,
writers: 1,
limits: {
storage_aware: true
},
},
},
mk_replsrc: function (name, sinkEndpoint, auth) {
type: "replsrc",
name: "replsrc_" + name,
sink: sinkEndpoint
} + if auth != null then { key: auth[0], secret: auth[1] } else {},
mk_wamp_client: function (clusterId, name) {
type: "wamp",
name: "wamp_" + name,
realm: constants.refDeployParam("REALM"),
auth: {
method: "cryptosign",
role: constants.refDeployParam("AUTHID"),
secret: constants.refDeployParam("PRIVATE_KEY"),
},
url: constants.refDeployParam("URL"),
app: "com.viinex.api",
prefix: clusterId,
clusters: false
},
mk_authnz_static: function (name, cfg) {
type: "authnz",
name: "authnz_" + name,
realm: cfg.realm,
secret: cfg.secret,
accounts: cfg.accounts,
roles: cfg.roles,
acl: cfg.acl,
},
namesOf: function (objects) std.map(function(x) x.name, objects),
default_app: function () {
sources: [],
record: null,
webrtc: true,
rtspsrv: false,
websrv: true,
wamp: false,
metrics: true
},
build_viinex_config: function (cid, app) {
local mediaSources = app.sources,
local mediaSourcesMain = std.filter(function (s) !("substreamOf" in s.__orig) || s.__orig.substreamOf == null, mediaSources),
local storages = if app.record != null then [common.mk_storage(cid+"_1", app)] else [],
local replsrcs = if app.repl != null && app.record != null then [common.mk_replsrc(cid+"_1", app.repl.sink, app.repl.auth)] else [],
local recctls = if app.record != null then [common.mk_recctl(cid, s.__orig.id, 5, 5) for s in app.record.motion] else [],
local rules = if app.record != null then [common.mk_rule_motion(cid, s.__orig.id) for s in app.record.motion] else [],
local linksRecctlRule = if app.record != null
then [[recctls[i].name, rules[i].name, common.mk_cam_name(cid, app, app.record.motion[i].__orig.id)]
for i in std.range(0, std.length(app.record.motion)-1) if !("recEventSource" in app.record.motion[i].__orig)]
+[[recctls[i].name, rules[i].name]
for i in std.range(0, std.length(app.record.motion)-1) if "recEventSource" in app.record.motion[i].__orig]
+[[recctls[i].name, app.record.motion[i].__orig.camName]
for i in std.range(0, std.length(app.record.motion)-1) if "recEventSource" in app.record.motion[i].__orig]
+[[rules[i].name, app.record.motion[i].__orig.recEventSource]
for i in std.range(0, std.length(app.record.motion)-1) if "recEventSource" in app.record.motion[i].__orig]
else [],
local linksRecPermanent = if app.record != null
then [[common.namesOf(storages), common.namesOf(app.record.permanent)]]
else [],
local linksStoragesReplsrcs = [[common.namesOf(storages), common.namesOf(replsrcs)]],
local srcProc = std.filter(function (s) "proc" in s.__orig && s.__orig.proc != null, mediaSources),
local procRefreshRate = if ("proc" in app) && ("fps" in app.proc) then app.proc.fps else 5,
local procRenderers = [common.mk_renderer (cid, s.__orig.id, procRefreshRate) for s in srcProc],
local linksProcRenderers = [[common.mk_renderer_name (cid, s.__orig.id), s.__orig.camName] for s in srcProc],
local procWorkers = if "mk_proc_worker" in app
then [app.mk_proc_worker(s.__orig.id,
common.mk_renderer_name(cid, s.__orig.id),
s.__orig.proc,
s.__orig)
for s in srcProc]
else [],
local procHandlers = if "mk_proc_handler" in app
then [app.mk_proc_handler(s.__orig.id,
app.mk_proc_worker_name(cid, s.__orig.id),
s.__orig.proc,
s.__orig)
for s in srcProc]
else [],
local linksProc = [ [procWorkers[i].name, procHandlers[i].name]
for i in std.range(0, std.length(srcProc)-1) ],
local linksProcHandlers = [[common.namesOf(storages), common.namesOf(procHandlers)]],
local webrtcs = if app.webrtc
then [common.mk_webrtc(cid+"_0", if std.isObject(app.webrtcOverrides)
then app.webrtcOverrides
else {})]
else [],
local rtspsrvs = if app.rtspsrv then [common.mk_rtspsrv(cid+"_0", constants.rtspsrvPort)] else [],
local websrvs = if app.websrv then [common.mk_webserver(cid+"_0")] else [],
local metrics = if app.metrics then [common.mk_metrics(cid)] else [],
local databases = if app.events == "sqlite"
then [common.mk_db_sqlite(cid+"_1")]
else [],
local wamps = if app.wamp then [common.mk_wamp_client(cid, cid + "_0")] else [],
local authnz = if app.auth != null && !("acl_id" in app.auth)
then [common.mk_authnz_static(cid + "_0", app.auth)]
else [],
local zmq = if app.zmq != null
then [common.mk_zmq_exporter(cid+"_0",
if "scheme" in app.zmq
then app.zmq.scheme
else "viinex",
if "behavior" in app.zmq
then app.zmq.behavior
else null,
if "endpoint" in app.zmq
then app.zmq.endpoint
else null)]
else [],
local publishers = webrtcs + rtspsrvs + websrvs + wamps,
local mediaPublishers = webrtcs + rtspsrvs + websrvs,
local mediaPublishersLive = zmq,
local apiPublishers = websrvs + wamps,
local apiProviders = mediaSources + storages + webrtcs + metrics + databases,
local metricsProviders = mediaSources + storages + procRenderers + databases + webrtcs + rtspsrvs + websrvs + zmq,
local eventProducers = mediaSourcesMain + storages + procWorkers + procHandlers,
objects: mediaSources + storages + publishers + metrics +
recctls + replsrcs + rules + databases + zmq +
procRenderers + procWorkers + procHandlers + authnz +
app.otherObjects,
links: [
[common.namesOf(mediaSources), common.namesOf(mediaPublishers) + common.namesOf(mediaPublishersLive)],
[common.namesOf(mediaPublishers), common.namesOf(storages)],
[common.namesOf(apiPublishers), common.namesOf(apiProviders)],
[common.namesOf(metrics), common.namesOf(metricsProviders)],
[common.namesOf(recctls), common.namesOf(storages)],
[common.namesOf(databases), common.namesOf(eventProducers)],
[common.namesOf(publishers), common.namesOf(authnz)],
]
+ linksRecctlRule
+ linksRecPermanent
+ linksStoragesReplsrcs
+ linksProcRenderers
+ linksProc
+ linksProcHandlers
+ app.otherLinks
},
};
common