-
Notifications
You must be signed in to change notification settings - Fork 13
/
descriptor.js
72 lines (66 loc) · 1.69 KB
/
descriptor.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
var tagToName = {
0x03: 'ESDescriptor',
0x04: 'DecoderConfigDescriptor',
0x05: 'DecoderSpecificInfo',
0x06: 'SLConfigDescriptor'
}
exports.Descriptor = {}
exports.Descriptor.decode = function (buf, start, end) {
var tag = buf.readUInt8(start)
var ptr = start + 1
var lenByte
var len = 0
do {
lenByte = buf.readUInt8(ptr++)
len = (len << 7) | (lenByte & 0x7f)
} while (lenByte & 0x80)
var obj
var tagName = tagToName[tag] // May be undefined; that's ok
if (exports[tagName]) {
obj = exports[tagName].decode(buf, ptr, end)
} else {
obj = {
buffer: Buffer.from(buf.slice(ptr, ptr + len))
}
}
obj.tag = tag
obj.tagName = tagName
obj.length = (ptr - start) + len
obj.contentsLen = len
return obj
}
exports.DescriptorArray = {}
exports.DescriptorArray.decode = function (buf, start, end) {
var ptr = start
var obj = {}
while (ptr + 2 <= end) {
var descriptor = exports.Descriptor.decode(buf, ptr, end)
ptr += descriptor.length
var tagName = tagToName[descriptor.tag] || ('Descriptor' + descriptor.tag)
obj[tagName] = descriptor
}
return obj
}
exports.ESDescriptor = {}
exports.ESDescriptor.decode = function (buf, start, end) {
var flags = buf.readUInt8(start + 2)
var ptr = start + 3
if (flags & 0x80) {
ptr += 2
}
if (flags & 0x40) {
var len = buf.readUInt8(ptr)
ptr += len + 1
}
if (flags & 0x20) {
ptr += 2
}
return exports.DescriptorArray.decode(buf, ptr, end)
}
exports.DecoderConfigDescriptor = {}
exports.DecoderConfigDescriptor.decode = function (buf, start, end) {
var oti = buf.readUInt8(start)
var obj = exports.DescriptorArray.decode(buf, start + 13, end)
obj.oti = oti
return obj
}