-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
448 lines (395 loc) · 16.1 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
'use strict'
const dgram = require('dgram')
const EventEmitter = require('events')
const camClient = require('./lib/camClient')
const cloudClient = require('./lib/cloudClient')
const client = function client (opts = {}, udpSocket) {
let socket
let servers = opts.servers ? opts.servers : []
let uid = opts.uid ? opts.uid.replace(/-/g, '') : ''
let camAddresses = opts.camDirectAddresses ? opts.camDirectAddresses : []
let camCredentials = opts.credentials ? opts.credentials : {user: 'admin', pass: ''}
let emitter = new EventEmitter()
let currentBuffer = Buffer.from([])
let pingNum = 0
let currentCamSession = {
init: false,
active: false,
address: {host: null, port: null},
mySeq: 0,
lastACK: null,
lastRemoteACKed: null,
lastTimeReceivedPacket: null,
remoteHttpSeqs: [],
remoteAudioSeqs: [],
remoteVideoSeqs: [],
// pingerId: null,
ackerId: null,
receivedIds: 0,
receivedFirstPacket: false,
bytesToReceive: 0,
receivedBytes: 0
}
if (udpSocket) {
socket = udpSocket
} else {
socket = dgram.createSocket('udp4')
socket.bind()
}
// TODO better buffer handling (deal with ordered sequence numbers, packet repetitions and packet losses)
// create a 'buffer' object and put packets as properties using seq as key.
// when a message is complete remove all parts and return complete
// TODO deal with retransmission of our packets lost during transmission
// -> must check remote acks and pings
// -> must keep a cache of our packets
socket.on('message',
function (msg, rinfo) { // check if message is from a server or the camera
if (addressExists(servers, {host: rinfo.address, port: rinfo.port})) {
cloudClient.parseMessage(msg, (type, info) => { emitter.emit(type, info) })
} else if (addressExists(camAddresses, {host: rinfo.address, port: rinfo.port})) {
currentCamSession.lastTimeReceivedPacket = Date.now()
camClient.parseMessage(msg, (type, info) => {
emitter.emit(type, info)
if ((type === 'pingpong') && (info.subtype === 'ping')) {
// keep the session alive
camClient.sendPong(socket, {host: rinfo.address, port: rinfo.port})
} else if ((type === 'confirmed') && (!currentCamSession.active)) {
currentCamSession.active = true
camClient.sendPing(socket, {host: rinfo.address, port: rinfo.port})
// currentCamSession.receivedIds++
// camClient.openSession(socket, {host: rinfo.address, port: rinfo.port}, uid)
// camClient.sendPing(socket, {host: rinfo.address, port: rinfo.port})
// currentCamSession.pingerId = setInterval(() => {
// camClient.sendPing(socket, {host: rinfo.address, port: rinfo.port})
// let now = Date.now()
// let past = now - currentCamSession.lastTimeReceivedPacket
// if (past > 10000) {
// emitter.emit('lostConnection', {lastReceived: currentCamSession.lastTimeReceivedPacket, timePast: past, message: `not receiving packets since ${past / 1000} seconds`})
// }
// }, 1000)
currentCamSession.ackerId = setInterval(() => { // either send acks or pings
if (currentCamSession.remoteHttpSeqs.length > 0 || currentCamSession.remoteAudioSeqs.length > 0 || currentCamSession.remoteVideoSeqs.length > 0) {
if (currentCamSession.remoteHttpSeqs.length > 0) {
camClient.sendHttpAck(socket, {host: rinfo.address, port: rinfo.port}, currentCamSession.remoteHttpSeqs)
currentCamSession.remoteHttpSeqs = []
}
if (currentCamSession.remoteAudioSeqs.length > 0) {
camClient.sendAudioAck(socket, {host: rinfo.address, port: rinfo.port}, currentCamSession.remoteAudioSeqs)
currentCamSession.remoteAudioSeqs = []
}
if (currentCamSession.remoteVideoSeqs.length > 0) {
camClient.sendVideoAck(socket, {host: rinfo.address, port: rinfo.port}, currentCamSession.remoteVideoSeqs)
currentCamSession.remoteVideoSeqs = []
}
} else { // NOTE: don't send ping BEFORE sending acks or the camera will think the packets not yet acket got lost
if (pingNum === 0) {
camClient.sendPing(socket, {host: rinfo.address, port: rinfo.port})
let now = Date.now()
let past = now - currentCamSession.lastTimeReceivedPacket
if (past > 10000) { // TODO should send close message and reset the connection at this point
emitter.emit('lostConnection', {lastReceived: currentCamSession.lastTimeReceivedPacket, timePast: past, message: `not receiving packets since ${past / 1000} seconds`})
}
}
}
// limiting the rate of pings sent
pingNum++
if (pingNum === 20) {
pingNum = 0
}
}, 50)
// } else if ((type === 'confirmed') && (currentCamSession.active)) {
// currentCamSession.receivedIds++
// if (currentCamSession.receivedIds < 4) {
// camClient.openSession(socket, {host: rinfo.address, port: rinfo.port}, uid)
// } else {
// camClient.sendPing(socket, {host: rinfo.address, port: rinfo.port})
// }
} else if ((type === 'close') && (currentCamSession.active)) {
cleanUpSession()
} else if (type === 'http') {
currentCamSession.remoteHttpSeqs.push(info.seq)
if (!currentCamSession.receivedFirstPacket) {
currentCamSession.receivedFirstPacket = true
currentCamSession.receivedBytes = msg.length - 16
camClient.parseHttp(msg, (info) => {
// console.log('bytes to expect: ' + info.size)
currentCamSession.bytesToReceive = info.size
currentBuffer = info.payload
if (info.size === currentCamSession.receivedBytes) {
emitter.emit('complete', {data: currentBuffer})
currentCamSession.receivedFirstPacket = false
currentCamSession.receivedBytes = 0
currentCamSession.bytesToReceive = 0
}
})
} else {
currentCamSession.receivedBytes += (msg.length - 8)
currentBuffer = Buffer.concat([currentBuffer, info.payload])
if (currentCamSession.bytesToReceive === currentCamSession.receivedBytes) {
emitter.emit('complete', {data: currentBuffer})
currentCamSession.receivedFirstPacket = false
currentCamSession.receivedBytes = 0
currentCamSession.bytesToReceive = 0
}
}
} else if (type === 'audio') {
currentCamSession.remoteAudioSeqs.push(info.seq)
} else if (type === 'video') {
currentCamSession.remoteVideoSeqs.push(info.seq)
}
})
} else { // unknown sender
emitter.emit('unknownSender', JSON.stringify(rinfo) + ' - ' + msg.toString('hex'))
}
})
const addServer = function addServer (address) {
if (!address || !address.host || !address.port) {
} else {
if (!addressExists(servers, address)) {
servers.push(address)
}
}
}
const addCamAddress = function addCamAddress (address) {
if (!address || !address.host || !address.port) {
} else {
if (!addressExists(camAddresses, address)) {
camAddresses.push(address)
}
}
}
const setCamCredentials = function setCamCredentials (credentials) {
camCredentials = credentials
}
const setUid = function setUid (newUid) {
uid = newUid.replace(/-/g, '')
}
const sendSTUNRequest = function sendSTUNRequest () {
servers.forEach((address) => { cloudClient.sendSTUN(socket, address) })
}
const lookupUid = function lookupUid (uidToCheck) {
if (!uidToCheck) {
uidToCheck = uid
} else {
uidToCheck = uidToCheck.replace(/-/g, '')
}
servers.forEach((address) => {
cloudClient.lookupUid(socket, address, uidToCheck, (error) => {
if (error) {
emitter.emit('error', error)
}
})
})
}
const openDirectCamSession = function openDirectCamSession (address) {
currentCamSession.init = true
addCamAddress(address)
currentCamSession.address = address
camClient.openSession(socket, address, uid)
}
const closeCamSession = function closeCamSession () {
camClient.closeSession(socket, currentCamSession.address)
cleanUpSession()
}
// TODO: assign to each request an id and a buffer, return the buffer when the response is complete
const checkCredentials = function checkCredentials () {
camClient.checkCredentials(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getSnapshot = function getSnapshot () {
camClient.getSnapshot(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const sendMultipleGet = function sendMultipleGet (urls) {
// TOTO check request length and eventually split in multiple requests
camClient.sendMultipleGet(socket, currentCamSession.address, currentCamSession.mySeq, urls)
currentCamSession.mySeq++
}
// TODO: add authentication parameters
const sendGet = function sendGet (url) {
camClient.sendGet(socket, currentCamSession.address, currentCamSession.mySeq, url)
currentCamSession.mySeq++
}
const getAudioStream = function getAudioStream (stream = 1) {
camClient.getAudioStream(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials, stream)
currentCamSession.mySeq++
}
const getVideoStream = function getVideoStream (stream = 10) {
camClient.getVideoStream(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials, stream)
currentCamSession.mySeq++
}
const stopAudioStream = function stopAudioStream () {
camClient.stopAudioStream(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const stopVideoStream = function stopVideoStream () {
camClient.stopVideoStream(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getParams = function getParams () {
camClient.getParams(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getStatus = function getStatus () {
camClient.getStatus(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const cameraControl = function cameraControl () {
camClient.cameraControl(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getMisc = function getMisc () {
camClient.getMisc(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getRtsp = function getRtsp () {
camClient.getRtsp(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getOnvif = function getOnvif () {
camClient.getOnvif(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getRecord = function getRecord () {
camClient.getRecord(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const wifiScan = function wifiScan () {
camClient.wifiScan(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getWifiScanResult = function getWifiScanResult () {
camClient.getWifiScanResult(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const login = function login () {
camClient.login(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getCameraParams = function getCameraParams () {
camClient.getCameraParams(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const getFactoryParam = function getFactoryParam () {
camClient.getFactoryParam(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
// const sendPing = function sendPing () {
// camClient.sendPing(socket, currentCamSession.address)
// }
const moveDown = function moveDown () {
camClient.moveDown(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const moveLeft = function moveLeft () {
camClient.moveLeft(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const moveRight = function moveRight () {
camClient.moveRight(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const moveUp = function moveUp () {
camClient.moveUp(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const stepDown = function stepDown () {
camClient.stepDown(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const stepLeft = function stepLeft () {
camClient.stepLeft(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const stepRight = function stepRight () {
camClient.stepRight(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const stepUp = function stepUp () {
camClient.stepUp(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const stopMove = function stopMove () {
camClient.stopMove(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const moveDownLeft = function moveDownLeft () {
camClient.moveDownLeft(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const moveDownRight = function moveDownRight () {
camClient.moveDownRight(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const moveUpLeft = function moveUpLeft () {
camClient.moveUpLeft(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const moveUpRight = function moveUpRight () {
camClient.moveUpRight(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials)
currentCamSession.mySeq++
}
const on = function on (ev, cb) {
emitter.addListener(ev, cb)
}
// TODO
function cleanUpSession () {
currentCamSession.active = false
// if (currentCamSession.pingerId) {
// clearInterval(currentCamSession.pingerId)
// }
if (currentCamSession.ackerId) {
clearInterval(currentCamSession.ackerId)
}
}
return {
addServer,
setUid,
sendSTUNRequest,
lookupUid,
setCamCredentials,
addCamAddress,
openDirectCamSession,
closeCamSession,
checkCredentials,
sendMultipleGet,
sendGet,
getSnapshot,
getAudioStream,
getVideoStream,
stopAudioStream,
stopVideoStream,
getParams,
getCameraParams,
getFactoryParam,
getStatus,
cameraControl,
getMisc,
login,
getRtsp,
getOnvif,
getRecord,
wifiScan,
getWifiScanResult,
moveDown,
moveLeft,
moveRight,
moveUp,
stepDown,
stepLeft,
stepRight,
stepUp,
stopMove,
moveDownRight,
moveUpRight,
moveUpLeft,
moveDownLeft,
on
}
}
const addressExists = function addressExists (arr, address) {
return arr.some(function (el) {
return (el.host === address.host && el.port === address.port)
})
}
module.exports.client = client