-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameParser.js
More file actions
95 lines (83 loc) · 3.08 KB
/
Copy pathFrameParser.js
File metadata and controls
95 lines (83 loc) · 3.08 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
const ResponseCodes = require('./ResponseCodes')
const MessageIds = require('./MessageIds')
const VideopacketPayloads = require('./VideopacketPayloads')
const HEADER_LENGTH_BYTES = 20
const ReceiverTemplate = {
onResponse: undefined,
onMediaFrame: undefined
}
class FrameParser {
constructor() {
this.onResponse = (msg=>{})
this.onVideoFrame = (msg=>{})
this.onAudioFrame = (msg=>{})
this.onDisconnect = (msg=>{})
}
parseHeader(buffer) {
return {
_Header: buffer.readUInt32LE(0),
SessionID: buffer.readUInt32LE(4),
SequenceID: buffer.readUInt32LE(8),
Channel: buffer.readUInt8(12),
Endflag: buffer.readUInt8(13),
MessageId: buffer.readUInt16LE(14),
Size: buffer.readUInt32LE(16)
}
}
logFrame(str) {
//console.log(str)
}
read(buffer) {
const msg = {}
msg.header = this.parseHeader(buffer)
const length = HEADER_LENGTH_BYTES + msg.header.Size
msg.body = (length > HEADER_LENGTH_BYTES) && buffer.slice(HEADER_LENGTH_BYTES, length)
return msg
}
// DVRIPClient.cmdResponseParser
parse(buffer) {
const msg = this.read(buffer)
const length = HEADER_LENGTH_BYTES + msg.header.Size
if (!msg.body) {
// ff 01 00 00 4a 00 00 00 00 00 00 00 00 01 8e 05 00 00 00 00
this.logFrame('-- Client receive empty payload, length:' + length)
this.onDisconnect()
}
else if ((msg.header.MessageId === MessageIds.MONITOR_DATA)
|| (msg.header.MessageId === MessageIds.PLAY_DATA)) {
if((msg.body[0] !== 0) || (msg.body[1] !== 0) || (msg.body[2] !== 1)) {
this.logFrame('-- parse onVideoFrame (1), length:' + length)
return this.onVideoFrame(msg.body)
}
let PayloadType = msg.body[3]
if(PayloadType === VideopacketPayloads.Audio) {
this.logFrame('-- parse onAudioFrame, length:' + length)
return this.onAudioFrame(msg.body.slice(8))
}
//Failing to remove these 8-16 bytes for these messages for some reason makes FFMPEG unhappy.
//The output is apparently still a valid video stream.
if((PayloadType === VideopacketPayloads.IFrame) || (PayloadType === VideopacketPayloads.PlusEnc)) {
this.logFrame('-- parse onVideoFrame (2), length:' + length)
return this.onVideoFrame(msg.body.slice(16))
}
else {
this.logFrame('-- parse onVideoFrame (3), length:' + length)
return this.onVideoFrame(msg.body.slice(8))
}
}
else if (msg.body[0] !== 123) { // '{'
msg.payload = msg.body.toString('utf8')
}
else {
const json = msg.body.toString('utf8', 0, msg.body[msg.body.length - 1] ? msg.body.length - 1 : msg.body.length - 2)
msg.data = JSON.parse(json)
msg.Ret = msg.data.Ret
msg.SessionID = msg.data.SessionID
if (msg.data.Ret && !ResponseCodes.SuccessCodes[msg.data.Ret]) {
msg.ErrorMessage = ResponseCodes.ErrorCodes[msg.data.Ret] || 'Unknown error'
}
}
this.onResponse(msg)
}
}
module.exports = FrameParser