-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.js
575 lines (486 loc) · 18.5 KB
/
index.js
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import { logger } from '@nrchkb/logger'
import * as util from 'util'
const HapNodeJS = require('hap-nodejs')
const uuid = HapNodeJS.uuid
const StreamController = HapNodeJS.StreamController
const crypto = require('crypto')
const fs = require('fs')
const ip = require('ip')
const spawn = require('child_process').spawn
const log = logger('CameraSource')
module.exports = {
Camera: Camera,
}
function Camera(cameraControlService, config, cameraNode) {
this.configure(config, cameraControlService)
this.cameraNode = cameraNode
}
Camera.prototype.configure = function (config, cameraControlService) {
this.name = config.name
this.vcodec = config.cameraConfigVideoCodec
this.videoProcessor = config.cameraConfigVideoProcessor
this.audio = config.cameraConfigAudio
this.acodec = config.cameraConfigAudioCodec
this.packetsize = config.cameraConfigPacketSize
this.fps = config.cameraConfigMaxFPS
this.maxBitrate = config.cameraConfigMaxBitrate
this.debug = config.cameraConfigDebug
this.additionalCommandline = config.cameraConfigAdditionalCommandLine
this.vflip = config.cameraConfigVerticalFlip
this.hflip = config.cameraConfigHorizontalFlip
this.mapvideo = config.cameraConfigMapVideo
this.mapaudio = config.cameraConfigMapAudio
this.videoFilter = config.cameraConfigVideoFilter
this.uploader = config.cameraConfigSnapshotOutput
this.interfaceName = config.cameraConfigInterfaceName
if (!config.cameraConfigSource) {
throw new NRCHKBError('Missing source for camera.')
}
if (this.interfaceName !== '') {
if (
!Object.keys(require('os').networkInterfaces()).includes(
this.interfaceName
)
) {
throw new NRCHKBError('Incorrect interface name for camera.')
}
}
this.ffmpegSource = config.cameraConfigSource
this.ffmpegImageSource = config.cameraConfigStillImageSource
this.services = []
this.streamControllers = []
this.pendingSessions = {}
this.ongoingSessions = {}
const numberOfStreams = config.cameraConfigMaxStreams
const videoResolutions = []
this.maxWidth = config.cameraConfigMaxWidth
this.maxHeight = config.cameraConfigMaxHeight
const maxFPS = this.fps > 30 ? 30 : this.fps
if (this.maxWidth >= 320) {
if (this.maxHeight >= 240) {
videoResolutions.push([320, 240, maxFPS])
if (maxFPS > 15) {
videoResolutions.push([320, 240, 15])
}
}
if (this.maxHeight >= 180) {
videoResolutions.push([320, 180, maxFPS])
if (maxFPS > 15) {
videoResolutions.push([320, 180, 15])
}
}
}
if (this.maxWidth >= 480) {
if (this.maxHeight >= 360) {
videoResolutions.push([480, 360, maxFPS])
}
if (this.maxHeight >= 270) {
videoResolutions.push([480, 270, maxFPS])
}
}
if (this.maxWidth >= 640) {
if (this.maxHeight >= 480) {
videoResolutions.push([640, 480, maxFPS])
}
if (this.maxHeight >= 360) {
videoResolutions.push([640, 360, maxFPS])
}
}
if (this.maxWidth >= 1280) {
if (this.maxHeight >= 960) {
videoResolutions.push([1280, 960, maxFPS])
}
if (this.maxHeight >= 720) {
videoResolutions.push([1280, 720, maxFPS])
}
}
if (this.maxWidth >= 1920) {
if (this.maxHeight >= 1080) {
videoResolutions.push([1920, 1080, maxFPS])
}
}
let options = {
proxy: false, // Requires RTP/RTCP MUX Proxy TODO: Should I make it configurable?
srtp: true, // Supports SRTP AES_CM_128_HMAC_SHA1_80 encryption TODO: Should I make it configurable?
video: {
resolutions: videoResolutions,
codec: {
profiles: [0, 1, 2], // Enum, please refer StreamController.VideoCodecParamProfileIDTypes TODO: Should I make it configurable?
levels: [0, 1, 2], // Enum, please refer StreamController.VideoCodecParamLevelTypes TODO: Should I make it configurable?
},
},
audio: {
codecs: [
{
type: 'OPUS', // Audio Codec TODO: Should I make it configurable?
samplerate: 24, // 8, 16, 24 KHz TODO: Should I make it configurable?
},
{
type: 'AAC-eld', // TODO: Should I make it configurable?
samplerate: 16, // TODO: Should I make it configurable?
},
],
},
}
if (cameraControlService) {
this._createStreamControllers(
cameraControlService,
numberOfStreams,
options
)
} else {
log.error(
'Camera reconfigure on the fly is not yet supported. ' +
'Please restart node-red to apply all changes to the Camera.'
)
}
}
Camera.prototype.handleCloseConnection = function (connectionID) {
this.streamControllers.forEach(function (controller) {
controller.handleCloseConnection(connectionID)
})
}
Camera.prototype.handleSnapshotRequest = function (request, callback) {
let resolution = request.width + 'x' + request.height
let imageSource = this.ffmpegImageSource
if (imageSource === undefined || !imageSource.trim()) {
imageSource = this.ffmpegSource
}
let ffmpeg = spawn(
this.videoProcessor,
(imageSource + ' -t 1 -s ' + resolution + ' -f image2 -').split(' '),
{ env: process.env }
)
let imageBuffer = Buffer.alloc(0)
log.debug('Snapshot from ' + this.name + ' at ' + resolution)
log.debug(
'ffmpeg ' + imageSource + ' -t 1 -s ' + resolution + ' -f image2 -'
)
ffmpeg.stdout.on('data', function (data) {
imageBuffer = Buffer.concat([imageBuffer, data])
})
ffmpeg.on('error', function (error) {
log.error('An error occurs while making snapshot request')
log.error(util.inspect(error))
})
ffmpeg.on(
'close',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function (code) {
let msg
if (this.cameraNode) {
const topic = this.cameraNode.topic
? this.cameraNode.topic
: this.cameraNode.topic_in
msg = { payload: {}, name: this.cameraNode.name, topic: topic }
}
if (this.uploader !== 'disabled' && !this.cameraNode) {
log.error('Node required for camera to send snapshot!')
} else if (this.uploader === 'path') {
const filePath =
__dirname + '/../../cameraSnapshots/' + new Date().getTime()
log.debug('Saving camera snapshot to: ' + filePath)
const self = this
fs.writeFile(filePath, imageBuffer, function (err) {
if (err) {
log.error(
'Camera snapshot file failed to save due to: ' + err
)
return
}
msg.payload['cameraSnapshot'] = filePath
log.debug(
'Camera snapshot file has been saved! Sending to output... ' +
JSON.stringify(msg)
)
self.cameraNode.send([null, msg])
})
} else if (this.uploader === 'content') {
msg.payload['cameraSnapshot'] = imageBuffer
log.debug(
'Sending camera snapshot buffer to output...' +
JSON.stringify(msg)
)
this.cameraNode.send([null, null, msg])
}
callback(undefined, imageBuffer)
}.bind(this)
)
}
Camera.prototype.prepareStream = function (request, callback) {
const sessionInfo = {}
let sessionID = request['sessionID']
sessionInfo['address'] = request['targetAddress']
const response = {}
let videoInfo = request['video']
if (videoInfo) {
let targetPort = videoInfo['port']
let srtp_key = videoInfo['srtp_key']
let srtp_salt = videoInfo['srtp_salt']
// SSRC is a 32 bit integer that is unique per stream
let ssrcSource = crypto.randomBytes(4)
ssrcSource[0] = 0
let ssrc = ssrcSource.readInt32BE(0, true)
response['video'] = {
port: targetPort,
ssrc: ssrc,
srtp_key: srtp_key,
srtp_salt: srtp_salt,
}
sessionInfo['video_port'] = targetPort
sessionInfo['video_srtp'] = Buffer.concat([srtp_key, srtp_salt])
sessionInfo['video_ssrc'] = ssrc
}
let audioInfo = request['audio']
if (audioInfo) {
let targetPort = audioInfo['port']
let srtp_key = audioInfo['srtp_key']
let srtp_salt = audioInfo['srtp_salt']
// SSRC is a 32 bit integer that is unique per stream
let ssrcSource = crypto.randomBytes(4)
ssrcSource[0] = 0
let ssrc = ssrcSource.readInt32BE(0, true)
response['audio'] = {
port: targetPort,
ssrc: ssrc,
srtp_key: srtp_key,
srtp_salt: srtp_salt,
}
sessionInfo['audio_port'] = targetPort
sessionInfo['audio_srtp'] = Buffer.concat([srtp_key, srtp_salt])
sessionInfo['audio_ssrc'] = ssrc
}
let currentAddress = ip.address(this.interfaceName)
const addressResp = {
address: currentAddress,
}
if (ip.isV4Format(currentAddress)) {
addressResp['type'] = 'v4'
} else {
addressResp['type'] = 'v6'
}
response['address'] = addressResp
this.pendingSessions[uuid.unparse(sessionID)] = sessionInfo
callback(response)
}
Camera.prototype.handleStreamRequest = function (request) {
const sessionID = request['sessionID']
const requestType = request['type']
if (sessionID) {
let sessionIdentifier = uuid.unparse(sessionID)
log.debug('Request type: ' + requestType)
if (requestType === 'start') {
const sessionInfo = this.pendingSessions[sessionIdentifier]
if (sessionInfo) {
let width = 1280
let height = 720
let fps = this.fps || 30
let vbitrate = this.maxBitrate
let abitrate = 32
let asamplerate = 16
const vcodec = this.vcodec
const acodec = this.acodec
const packetsize = this.packetsize || 1316
const additionalCommandline = this.additionalCommandline
const mapvideo = this.mapvideo
const mapaudio = this.mapaudio
let videoInfo = request['video']
if (videoInfo) {
width = videoInfo['width']
height = videoInfo['height']
let expectedFPS = videoInfo['fps']
if (expectedFPS < fps) {
fps = expectedFPS
}
if (videoInfo['max_bit_rate'] < vbitrate) {
vbitrate = videoInfo['max_bit_rate']
}
}
let audioInfo = request['audio']
if (audioInfo) {
abitrate = audioInfo['max_bit_rate']
asamplerate = audioInfo['sample_rate']
}
let targetAddress = sessionInfo['address']
let targetVideoPort = sessionInfo['video_port']
let videoKey = sessionInfo['video_srtp']
let videoSsrc = sessionInfo['video_ssrc']
let targetAudioPort = sessionInfo['audio_port']
let audioKey = sessionInfo['audio_srtp']
let audioSsrc = sessionInfo['audio_ssrc']
let vf = []
// Skip vf entirely if videoFilter is empty
if (this.videoFilter !== null && this.videoFilter !== '') {
vf.push(this.videoFilter)
if (this.hflip) vf.push('hflip')
if (this.vflip) vf.push('vflip')
}
let fcmd = this.ffmpegSource
let ffmpegVideoArgs =
' -map ' +
mapvideo +
' -vcodec ' +
vcodec +
' -pix_fmt yuv420p' +
' -r ' +
fps +
' -f rawvideo' +
' ' +
additionalCommandline +
(vf.length > 0 ? ' -vf ' + vf.join(',') : '') +
' -b:v ' +
vbitrate +
'k' +
' -bufsize ' +
vbitrate +
'k' +
' -maxrate ' +
vbitrate +
'k' +
' -payload_type 99'
let ffmpegVideoStream =
' -ssrc ' +
videoSsrc +
' -f rtp' +
' -srtp_out_suite AES_CM_128_HMAC_SHA1_80' +
' -srtp_out_params ' +
videoKey.toString('base64') +
' srtp://' +
targetAddress +
':' +
targetVideoPort +
'?rtcpport=' +
targetVideoPort +
'&localrtcpport=' +
targetVideoPort +
'&pkt_size=' +
packetsize
// build required video arguments
fcmd += ffmpegVideoArgs
fcmd += ffmpegVideoStream
// build optional audio arguments
if (this.audio) {
let ffmpegAudioArgs =
' -map ' +
mapaudio +
' -acodec ' +
acodec +
' -profile:a aac_eld' +
' -flags +global_header' +
' -f null' +
' -ar ' +
asamplerate +
'k' +
' -b:a ' +
abitrate +
'k' +
' -bufsize ' +
abitrate +
'k' +
' -ac 1' +
' -payload_type 110'
let ffmpegAudioStream =
' -ssrc ' +
audioSsrc +
' -f rtp' +
' -srtp_out_suite AES_CM_128_HMAC_SHA1_80' +
' -srtp_out_params ' +
audioKey.toString('base64') +
' srtp://' +
targetAddress +
':' +
targetAudioPort +
'?rtcpport=' +
targetAudioPort +
'&localrtcpport=' +
targetAudioPort +
'&pkt_size=' +
packetsize
fcmd += ffmpegAudioArgs
fcmd += ffmpegAudioStream
}
if (this.debug) {
fcmd += ' -loglevel debug'
}
let ffmpeg = spawn(this.videoProcessor, fcmd.split(' '), {
env: process.env,
})
log.debug(
'Start streaming video from ' +
this.name +
' with ' +
width +
'x' +
height +
'@' +
vbitrate +
'kBit'
)
log.debug('ffmpeg ' + fcmd)
ffmpeg.stderr.on(
'data',
function (data) {
log.debug('ffmpeg data: ' + data.toString())
}.bind(this)
)
let self = this
ffmpeg.on('error', function (error) {
log.error('An error occurred while making stream request')
log.error(util.inspect(error))
})
ffmpeg.on('close', (code) => {
if (code == null || code === 0 || code === 255) {
log.debug('Stopped streaming')
} else {
log.error('ERROR: FFmpeg exited with code ' + code)
for (
let i = 0;
i < self.streamControllers.length;
i++
) {
const controller = self.streamControllers[i]
if (controller.sessionIdentifier === sessionID) {
controller.forceStop()
}
}
}
})
this.ongoingSessions[sessionIdentifier] = ffmpeg
}
delete this.pendingSessions[sessionIdentifier]
} else if (requestType === 'stop') {
const ffmpegProcess = this.ongoingSessions[sessionIdentifier]
if (ffmpegProcess) {
ffmpegProcess.kill('SIGTERM')
}
delete this.ongoingSessions[sessionIdentifier]
} else if (requestType === 'reconfigure') {
//TODO: What to do here?
}
}
}
Camera.prototype._createStreamControllers = function (
cameraControlService,
maxStreams,
options
) {
let self = this
log.debug('Configuring services for Camera...')
self.services.push(cameraControlService)
log.debug('...added CameraControl Service')
if (self.audio) {
log.debug('...audio available')
} else {
log.debug('...audio not available')
}
log.debug(
'Creating Camera Stream Controllers: ' + maxStreams + ' - Started'
)
log.debug('Camera options: ' + JSON.stringify(options))
for (let i = 0; i < maxStreams; i++) {
const streamController = new StreamController(i, options, self)
self.services.push(streamController.service)
self.streamControllers.push(streamController)
}
log.debug('Creating Camera Stream Controllers - Finished')
}