/
rtp.ts
296 lines (265 loc) · 9.07 KB
/
rtp.ts
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
import { BitWriter, getBit } from "../utils";
export type Extension = { id: number; payload: Buffer };
export const ExtensionProfiles = {
OneByte: 0xbede, // 48862
TwoByte: 0x1000, // 4096
} as const;
type ExtensionProfile =
typeof ExtensionProfiles[keyof typeof ExtensionProfiles];
const seqNumOffset = 2;
const timestampOffset = 4;
const ssrcOffset = 8;
const csrcOffset = 12;
const csrcLength = 4;
/*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |V=2|P|X| CC |M| PT | sequence number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | timestamp |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | synchronization source (SSRC) identifier |
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* | contributing source (CSRC) identifiers |
* | .... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
export class RtpHeader {
version: number = 2;
padding: boolean = false;
paddingSize: number = 0;
extension: boolean = false;
marker: boolean = false;
payloadOffset: number = 0;
payloadType: number = 0;
sequenceNumber: number = 0;
timestamp: number = 0;
ssrc: number = 0;
csrc: number[] = [];
extensionProfile: ExtensionProfile = ExtensionProfiles.OneByte;
/**deserialize only */
extensionLength?: number;
extensions: Extension[] = [];
constructor(props: Partial<RtpHeader> = {}) {
Object.assign(this, props);
}
static deSerialize(rawPacket: Buffer) {
const h = new RtpHeader();
let currOffset = 0;
const v_p_x_cc = rawPacket[currOffset++];
h.version = getBit(v_p_x_cc, 0, 2);
h.padding = getBit(v_p_x_cc, 2) > 0;
h.extension = getBit(v_p_x_cc, 3) > 0;
const cc = getBit(v_p_x_cc, 4, 4);
h.csrc = [...Array(cc)].map(() => {
const csrc = rawPacket.readUInt32BE(currOffset);
currOffset += 4;
return csrc;
});
currOffset += csrcOffset - 1;
const m_pt = rawPacket[1];
h.marker = getBit(m_pt, 0) > 0;
h.payloadType = getBit(m_pt, 1, 7);
h.sequenceNumber = rawPacket.readUInt16BE(seqNumOffset);
h.timestamp = rawPacket.readUInt32BE(timestampOffset);
h.ssrc = rawPacket.readUInt32BE(ssrcOffset);
for (let i = 0; i < h.csrc.length; i++) {
const offset = csrcOffset + i * csrcLength;
h.csrc[i] = rawPacket.slice(offset).readUInt32BE();
}
if (h.extension) {
h.extensionProfile = rawPacket
.slice(currOffset)
.readUInt16BE() as ExtensionProfile;
currOffset += 2;
const extensionLength = rawPacket.slice(currOffset).readUInt16BE() * 4;
h.extensionLength = extensionLength;
currOffset += 2;
switch (h.extensionProfile) {
// RFC 8285 RTP One Byte Header Extension
case ExtensionProfiles.OneByte:
{
const end = currOffset + extensionLength;
while (currOffset < end) {
if (rawPacket[currOffset] === 0x00) {
currOffset++;
continue;
}
const extId = rawPacket[currOffset] >> 4;
const len = // and not &^
(rawPacket[currOffset] & (rawPacket[currOffset] ^ 0xf0)) + 1;
currOffset++;
if (extId === 0xf) {
break;
}
const extension: Extension = {
id: extId,
payload: rawPacket.slice(currOffset, currOffset + len),
};
h.extensions = [...h.extensions, extension];
currOffset += len;
}
}
break;
// RFC 8285 RTP Two Byte Header Extension
case ExtensionProfiles.TwoByte:
{
const end = currOffset + extensionLength;
while (currOffset < end) {
if (rawPacket[currOffset] === 0x00) {
currOffset++;
continue;
}
const extId = rawPacket[currOffset];
currOffset++;
const len = rawPacket[currOffset];
currOffset++;
const extension: Extension = {
id: extId,
payload: rawPacket.slice(currOffset, currOffset + len),
};
h.extensions = [...h.extensions, extension];
currOffset += len;
}
}
break;
default:
{
const extension: Extension = {
id: 0,
payload: rawPacket.slice(
currOffset,
currOffset + extensionLength
),
};
h.extensions = [...h.extensions, extension];
currOffset += h.extensions[0].payload.length;
}
break;
}
}
h.payloadOffset = currOffset;
if (h.padding) {
h.paddingSize = rawPacket[rawPacket.length - 1];
}
return h;
}
get serializeSize() {
const { csrc, extensionProfile, extensions } = this;
let size = 12 + csrc.length * csrcLength;
if (extensions.length > 0 || this.extension === true) {
let extSize = 4;
switch (extensionProfile) {
case ExtensionProfiles.OneByte:
extensions.forEach((extension) => {
extSize += 1 + extension.payload.length;
});
break;
case ExtensionProfiles.TwoByte:
extensions.forEach((extension) => {
extSize += 2 + extension.payload.length;
});
break;
default:
extSize += extensions[0].payload.length;
}
size += Math.floor((extSize + 3) / 4) * 4;
}
return size;
}
serialize(size: number) {
const buf = Buffer.alloc(size);
let offset = 0;
const v_p_x_cc = new BitWriter(8);
v_p_x_cc.set(2, 0, this.version);
if (this.padding) v_p_x_cc.set(1, 2, 1);
if (this.extensions.length > 0) this.extension = true;
if (this.extension) v_p_x_cc.set(1, 3, 1);
v_p_x_cc.set(4, 4, this.csrc.length);
buf.writeUInt8(v_p_x_cc.value, offset++);
const m_pt = new BitWriter(8);
if (this.marker) m_pt.set(1, 0, 1);
m_pt.set(7, 1, this.payloadType);
buf.writeUInt8(m_pt.value, offset++);
buf.writeUInt16BE(this.sequenceNumber, seqNumOffset);
offset += 2;
buf.writeUInt32BE(this.timestamp, timestampOffset);
offset += 4;
buf.writeUInt32BE(this.ssrc, ssrcOffset);
offset += 4;
this.csrc.forEach((csrc) => {
buf.writeUInt32BE(csrc, offset);
offset += 4;
});
if (this.extension) {
const extHeaderPos = offset;
buf.writeUInt16BE(this.extensionProfile, offset);
offset += 4;
const startExtensionsPos = offset;
switch (this.extensionProfile) {
case ExtensionProfiles.OneByte:
this.extensions.forEach((extension) => {
buf.writeUInt8(
(extension.id << 4) | (extension.payload.length - 1),
offset++
);
extension.payload.copy(buf, offset);
offset += extension.payload.length;
});
break;
case ExtensionProfiles.TwoByte: // 1バイトで収まらなくなった歴史的経緯
this.extensions.forEach((extension) => {
buf.writeUInt8(extension.id, offset++);
buf.writeUInt8(extension.payload.length, offset++);
extension.payload.copy(buf, offset);
offset += extension.payload.length;
});
break;
default:
const extLen = this.extensions[0].payload.length;
if (extLen % 4 != 0) {
throw new Error();
}
this.extensions[0].payload.copy(buf, offset);
offset += extLen;
}
const extSize = offset - startExtensionsPos;
const roundedExtSize = Math.floor((extSize + 3) / 4) * 4;
buf.writeUInt16BE(Math.floor(roundedExtSize / 4), extHeaderPos + 2);
for (let i = 0; i < roundedExtSize - extSize; i++) {
buf.writeUInt8(0, offset);
offset++;
}
}
this.payloadOffset = offset;
return buf;
}
}
export class RtpPacket {
constructor(public header: RtpHeader, public payload: Buffer) {}
get serializeSize() {
return this.header.serializeSize + this.payload.length;
}
serialize() {
let buf = this.header.serialize(
this.header.serializeSize + this.payload.length
);
const { payloadOffset } = this.header;
this.payload.copy(buf, payloadOffset);
if (this.header.padding) {
const padding = Buffer.alloc(this.header.paddingSize);
padding.writeUInt8(this.header.paddingSize, this.header.paddingSize - 1);
buf = Buffer.concat([buf, padding]);
}
return buf;
}
static deSerialize(buf: Buffer) {
const header = RtpHeader.deSerialize(buf);
const p = new RtpPacket(
header,
buf.slice(header.payloadOffset, buf.length - header.paddingSize)
);
return p;
}
}