-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathpacket.go
More file actions
636 lines (530 loc) · 17.7 KB
/
packet.go
File metadata and controls
636 lines (530 loc) · 17.7 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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package rtp
import (
"encoding/binary"
"fmt"
"io"
)
// Extension RTP Header extension.
type Extension struct {
id uint8
payload []byte
}
// Header represents an RTP packet header.
type Header struct {
Version uint8
Padding bool
Extension bool
Marker bool
PayloadType uint8
SequenceNumber uint16
Timestamp uint32
SSRC uint32
CSRC []uint32
ExtensionProfile uint16
Extensions []Extension
// PaddingLength is the length of the padding in bytes. It is not part of the RTP header
// (it is sent in the last byte of RTP packet padding), but logically it belongs here.
PaddingSize byte
// Deprecated: will be removed in a future version.
PayloadOffset int
}
// Packet represents an RTP Packet.
type Packet struct {
Header
Payload []byte
PaddingSize byte // Deprecated: will be removed in a future version. Use Header.PaddingSize instead.
// Deprecated: will be removed in a future version.
Raw []byte
// Please do not add any new field directly to Packet struct unless you know that it is safe.
// pion internally passes Header and Payload separately, what causes bugs like
// https://github.com/pion/webrtc/issues/2403 .
}
const (
// ExtensionProfileOneByte is the RTP One Byte Header Extension Profile, defined in RFC 8285.
ExtensionProfileOneByte = 0xBEDE
// ExtensionProfileTwoByte is the RTP Two Byte Header Extension Profile, defined in RFC 8285.
ExtensionProfileTwoByte = 0x1000
// CryptexProfileOneByte is the Cryptex One Byte Header Extension Profile, defined in RFC 9335.
CryptexProfileOneByte = 0xC0DE
// CryptexProfileTwoByte is the Cryptex Two Byte Header Extension Profile, defined in RFC 9335.
CryptexProfileTwoByte = 0xC2DE
)
const (
headerLength = 4
versionShift = 6
versionMask = 0x3
paddingShift = 5
paddingMask = 0x1
extensionShift = 4
extensionMask = 0x1
extensionIDReserved = 0xF
extensionIDPadding = 0x0
ccMask = 0xF
markerShift = 7
markerMask = 0x1
ptMask = 0x7F
seqNumOffset = 2
seqNumLength = 2
timestampOffset = 4
timestampLength = 4
ssrcOffset = 8
ssrcLength = 4
csrcOffset = 12
csrcLength = 4
)
// String helps with debugging by printing packet information in a readable way.
func (p Packet) String() string {
out := "RTP PACKET:\n"
out += fmt.Sprintf("\tVersion: %v\n", p.Version)
out += fmt.Sprintf("\tMarker: %v\n", p.Marker)
out += fmt.Sprintf("\tPayload Type: %d\n", p.PayloadType)
out += fmt.Sprintf("\tSequence Number: %d\n", p.SequenceNumber)
out += fmt.Sprintf("\tTimestamp: %d\n", p.Timestamp)
out += fmt.Sprintf("\tSSRC: %d (%x)\n", p.SSRC, p.SSRC)
out += fmt.Sprintf("\tPayload Length: %d\n", len(p.Payload))
return out
}
// Unmarshal parses the passed byte slice and stores the result in the Header.
// It returns the number of bytes read n and any error.
func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit,cyclop
if len(buf) < headerLength {
return 0, fmt.Errorf("%w: %d < %d", errHeaderSizeInsufficient, len(buf), headerLength)
}
/*
* 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 |
* | .... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
h.Version = buf[0] >> versionShift & versionMask
h.Padding = (buf[0] >> paddingShift & paddingMask) > 0
h.Extension = (buf[0] >> extensionShift & extensionMask) > 0
nCSRC := int(buf[0] & ccMask)
if cap(h.CSRC) < nCSRC {
h.CSRC = make([]uint32, nCSRC)
} else {
h.CSRC = h.CSRC[:nCSRC]
}
n = csrcOffset + (nCSRC * csrcLength)
if len(buf) < n {
return n, fmt.Errorf("size %d < %d: %w", len(buf), n,
errHeaderSizeInsufficient)
}
headerLength := n
h.Marker = (buf[1] >> markerShift & markerMask) > 0
h.PayloadType = buf[1] & ptMask
h.SequenceNumber = binary.BigEndian.Uint16(buf[seqNumOffset : seqNumOffset+seqNumLength])
h.Timestamp = binary.BigEndian.Uint32(buf[timestampOffset : timestampOffset+timestampLength])
h.SSRC = binary.BigEndian.Uint32(buf[ssrcOffset : ssrcOffset+ssrcLength])
for i := range h.CSRC {
offset := csrcOffset + (i * csrcLength)
h.CSRC[i] = binary.BigEndian.Uint32(buf[offset:])
}
h.Extensions = h.Extensions[:0]
if h.Extension { // nolint: nestif
if expected := n + 4; len(buf) < expected {
return n, fmt.Errorf("size %d < %d: %w",
len(buf), expected,
errHeaderSizeInsufficientForExtension,
)
}
h.ExtensionProfile = binary.BigEndian.Uint16(buf[n:])
n += 2
extensionLength := int(binary.BigEndian.Uint16(buf[n:])) * 4
n += 2
extensionEnd := n + extensionLength
headerLength = extensionEnd
if len(buf) < extensionEnd {
return n, fmt.Errorf("size %d < %d: %w", len(buf), extensionEnd, errHeaderSizeInsufficientForExtension)
}
if h.ExtensionProfile == ExtensionProfileOneByte || h.ExtensionProfile == ExtensionProfileTwoByte {
var (
extid uint8
payloadLen int
)
for n < extensionEnd {
if buf[n] == extensionIDPadding { // padding
n++
continue
}
if h.ExtensionProfile == ExtensionProfileOneByte {
extid = buf[n] >> 4 // nolint:gosec // n is defined to be in bounds
payloadLen = int(buf[n]&^0xF0 + 1) //nolint:gosec // n is defined to be in bounds
n++
// Stop parsing extensions if we reach the reserved ID or padding with non-zero length
if extid == extensionIDReserved || extid == extensionIDPadding {
break
}
} else {
extid = buf[n] // nolint:gosec // n is defined to be in bounds
n++
if extensionEnd <= n {
return n, fmt.Errorf("size %d < %d: %w", extensionEnd, n, errHeaderSizeInsufficientForExtension)
}
payloadLen = int(buf[n])
n++
}
if extensionPayloadEnd := n + payloadLen; extensionEnd < extensionPayloadEnd {
return n, fmt.Errorf("size %d < %d: %w", extensionEnd, extensionPayloadEnd, errHeaderSizeInsufficientForExtension)
}
extension := Extension{id: extid, payload: buf[n : n+payloadLen]}
h.Extensions = append(h.Extensions, extension)
n += payloadLen
}
} else {
// RFC3550 Extension
extension := Extension{id: 0, payload: buf[n:extensionEnd]}
h.Extensions = append(h.Extensions, extension)
}
}
return headerLength, nil
}
// Unmarshal parses the passed byte slice and stores the result in the Packet.
func (p *Packet) Unmarshal(buf []byte) error {
n, err := p.Header.Unmarshal(buf)
if err != nil {
return err
}
end := len(buf)
if p.Header.Padding {
if end <= n {
return errTooSmall
}
p.Header.PaddingSize = buf[end-1]
if p.Header.PaddingSize == 0 {
return errInvalidRTPPadding
}
end -= int(p.Header.PaddingSize)
} else {
p.Header.PaddingSize = 0
}
p.PaddingSize = p.Header.PaddingSize
if end < n {
return errTooSmall
}
p.Payload = buf[n:end]
return nil
}
// Marshal serializes the header into bytes.
func (h Header) Marshal() (buf []byte, err error) {
buf = make([]byte, h.MarshalSize())
n, err := h.MarshalTo(buf)
if err != nil {
return nil, err
}
return buf[:n], nil
}
// MarshalTo serializes the header and writes to the buffer.
func (h Header) MarshalTo(buf []byte) (n int, err error) { //nolint:cyclop
/*
* 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 |
* | .... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
size := h.MarshalSize()
if size > len(buf) {
return 0, io.ErrShortBuffer
}
// The first byte contains the version, padding bit, extension bit,
// and csrc size.
buf[0] = (h.Version << versionShift) | uint8(len(h.CSRC)) // nolint: gosec // G115
if h.Padding {
buf[0] |= 1 << paddingShift
}
if h.Extension {
buf[0] |= 1 << extensionShift
}
// The second byte contains the marker bit and payload type.
buf[1] = h.PayloadType
if h.Marker {
buf[1] |= 1 << markerShift
}
binary.BigEndian.PutUint16(buf[2:4], h.SequenceNumber)
binary.BigEndian.PutUint32(buf[4:8], h.Timestamp)
binary.BigEndian.PutUint32(buf[8:12], h.SSRC)
n = 12
for _, csrc := range h.CSRC {
binary.BigEndian.PutUint32(buf[n:n+4], csrc)
n += 4
}
if h.Extension {
extHeaderPos := n
binary.BigEndian.PutUint16(buf[n+0:n+2], h.ExtensionProfile)
n += 4
startExtensionsPos := n
switch h.ExtensionProfile {
// RFC 8285 RTP One Byte Header Extension
case ExtensionProfileOneByte:
for _, extension := range h.Extensions {
buf[n] = extension.id<<4 | (uint8(len(extension.payload)) - 1) // nolint: gosec // G115
n++
n += copy(buf[n:], extension.payload)
}
// RFC 8285 RTP Two Byte Header Extension
case ExtensionProfileTwoByte:
for _, extension := range h.Extensions {
buf[n] = extension.id
n++
buf[n] = uint8(len(extension.payload)) // nolint: gosec // G115
n++
n += copy(buf[n:], extension.payload)
}
default: // RFC3550 Extension
// Zero length extension is valid per the RFC3550 spec
// https://www.rfc-editor.org/rfc/rfc3550#section-5.3.1
if len(h.Extensions) > 0 {
extlen := len(h.Extensions[0].payload)
if extlen%4 != 0 {
// the payload must be in 32-bit words.
return 0, io.ErrShortBuffer
}
n += copy(buf[n:], h.Extensions[0].payload)
}
}
// calculate extensions size and round to 4 bytes boundaries
extSize := n - startExtensionsPos
roundedExtSize := ((extSize + 3) / 4) * 4
// nolint: gosec // G115 false positive
binary.BigEndian.PutUint16(buf[extHeaderPos+2:extHeaderPos+4], uint16(roundedExtSize/4))
// add padding to reach 4 bytes boundaries
for i := 0; i < roundedExtSize-extSize; i++ {
buf[n] = 0
n++
}
}
return n, nil
}
// MarshalSize returns the size of the header once marshaled.
func (h Header) MarshalSize() int {
// NOTE: Be careful to match the MarshalTo() method.
size := 12 + (len(h.CSRC) * csrcLength)
if h.Extension {
extSize := 4
switch h.ExtensionProfile {
// RFC 8285 RTP One Byte Header Extension
case ExtensionProfileOneByte:
for _, extension := range h.Extensions {
extSize += 1 + len(extension.payload)
}
// RFC 8285 RTP Two Byte Header Extension
case ExtensionProfileTwoByte:
for _, extension := range h.Extensions {
extSize += 2 + len(extension.payload)
}
default:
if len(h.Extensions) > 0 {
extSize += len(h.Extensions[0].payload)
}
}
// extensions size must have 4 bytes boundaries
size += ((extSize + 3) / 4) * 4
}
return size
}
// SetExtension sets an RTP header extension.
func (h *Header) SetExtension(id uint8, payload []byte) error { //nolint:gocognit, cyclop
if h.Extension { // nolint: nestif
if err := headerExtensionCheck(h.ExtensionProfile, id, payload); err != nil {
return err
}
// Update existing if it exists else add new extension
for i, extension := range h.Extensions {
if extension.id == id {
h.Extensions[i].payload = payload
return nil
}
}
h.Extensions = append(h.Extensions, Extension{id: id, payload: payload})
return nil
}
// No existing header extensions
h.Extension = true
switch payloadLen := len(payload); {
case payloadLen <= 16:
h.ExtensionProfile = ExtensionProfileOneByte
case payloadLen > 16 && payloadLen < 256:
h.ExtensionProfile = ExtensionProfileTwoByte
}
h.Extensions = append(h.Extensions, Extension{id: id, payload: payload})
return nil
}
// SetExtensionWithProfile sets an RTP header extension and converts Header Extension Profile if needed.
func (h *Header) SetExtensionWithProfile(id uint8, payload []byte, intendedProfile uint16) error {
if !h.Extension || h.ExtensionProfile == intendedProfile {
return h.SetExtension(id, payload)
}
// Don't mutate the packet if Set is going to fail anyway
if err := headerExtensionCheck(intendedProfile, id, payload); err != nil {
return err
}
// If downgrading assert that existing Extensions will work
if intendedProfile == ExtensionProfileOneByte {
for i := range h.Extensions {
if err := headerExtensionCheck(intendedProfile, h.Extensions[i].id, h.Extensions[i].payload); err != nil {
return err
}
}
}
h.ExtensionProfile = intendedProfile
return h.SetExtension(id, payload)
}
// GetExtensionIDs returns an extension id array.
func (h *Header) GetExtensionIDs() []uint8 {
if !h.Extension {
return nil
}
if len(h.Extensions) == 0 {
return nil
}
ids := make([]uint8, 0, len(h.Extensions))
for _, extension := range h.Extensions {
ids = append(ids, extension.id)
}
return ids
}
// GetExtension returns an RTP header extension.
func (h *Header) GetExtension(id uint8) []byte {
if !h.Extension {
return nil
}
for _, extension := range h.Extensions {
if extension.id == id {
return extension.payload
}
}
return nil
}
// DelExtension Removes an RTP Header extension.
func (h *Header) DelExtension(id uint8) error {
if !h.Extension {
return errHeaderExtensionsNotEnabled
}
for i, extension := range h.Extensions {
if extension.id == id {
h.Extensions = append(h.Extensions[:i], h.Extensions[i+1:]...)
return nil
}
}
return errHeaderExtensionNotFound
}
// Marshal serializes the packet into bytes.
func (p Packet) Marshal() (buf []byte, err error) {
buf = make([]byte, p.MarshalSize())
n, err := p.MarshalTo(buf)
if err != nil {
return nil, err
}
return buf[:n], nil
}
// MarshalTo serializes the packet and writes to the buffer.
func (p *Packet) MarshalTo(buf []byte) (n int, err error) {
if p.Header.Padding && p.paddingSize() == 0 {
return 0, errInvalidRTPPadding
}
n, err = p.Header.MarshalTo(buf)
if err != nil {
return 0, err
}
return marshalPayloadAndPaddingTo(buf, n, &p.Header, p.Payload, p.paddingSize())
}
func marshalPayloadAndPaddingTo(buf []byte, offset int, header *Header, payload []byte, paddingSize byte,
) (n int, err error) {
// Make sure the buffer is large enough to hold the packet.
if offset+len(payload)+int(paddingSize) > len(buf) {
return 0, io.ErrShortBuffer
}
m := copy(buf[offset:], payload)
if header.Padding {
buf[offset+m+int(paddingSize-1)] = paddingSize
}
return offset + m + int(paddingSize), nil
}
// MarshalSize returns the size of the packet once marshaled.
func (p Packet) MarshalSize() int {
return p.Header.MarshalSize() + len(p.Payload) + int(p.paddingSize())
}
// Clone returns a deep copy of p.
func (p Packet) Clone() *Packet {
clone := &Packet{}
clone.Header = p.Header.Clone()
if p.Payload != nil {
clone.Payload = make([]byte, len(p.Payload))
copy(clone.Payload, p.Payload)
}
clone.PaddingSize = p.PaddingSize
return clone
}
// Clone returns a deep copy h.
func (h Header) Clone() Header {
clone := h
if h.CSRC != nil {
clone.CSRC = make([]uint32, len(h.CSRC))
copy(clone.CSRC, h.CSRC)
}
if h.Extensions != nil {
ext := make([]Extension, len(h.Extensions))
for i, e := range h.Extensions {
ext[i] = e
if e.payload != nil {
ext[i].payload = make([]byte, len(e.payload))
copy(ext[i].payload, e.payload)
}
}
clone.Extensions = ext
}
return clone
}
func (p *Packet) paddingSize() byte {
if p.Header.PaddingSize > 0 {
return p.Header.PaddingSize
}
return p.PaddingSize
}
// MarshalPacketTo serializes the header and payload into bytes.
// Parts of pion code passes RTP header and payload separately, so this function
// is provided to help with that.
//
// Deprecated: this function is a temporary workaround and will be removed in pion/webrtc v5.
func MarshalPacketTo(buf []byte, header *Header, payload []byte) (int, error) {
n, err := header.MarshalTo(buf)
if err != nil {
return 0, err
}
return marshalPayloadAndPaddingTo(buf, n, header, payload, header.PaddingSize)
}
// PacketMarshalSize returns the size of the header and payload once marshaled.
// Parts of pion code passes RTP header and payload separately, so this function
// is provided to help with that.
//
// Deprecated: this function is a temporary workaround and will be removed in pion/webrtc v5.
func PacketMarshalSize(header *Header, payload []byte) int {
return header.MarshalSize() + len(payload) + int(header.PaddingSize)
}
// HeaderAndPacketMarshalSize returns the size of the header and full packet once marshaled.
// Parts of pion code passes RTP header and payload separately, so this function
// is provided to help with that.
//
// Deprecated: this function is a temporary workaround and will be removed in pion/webrtc v5.
func HeaderAndPacketMarshalSize(header *Header, payload []byte) (headerSize int, packetSize int) {
headerSize = header.MarshalSize()
return headerSize, headerSize + len(payload) + int(header.PaddingSize)
}