Skip to content

Commit be6ccef

Browse files
committed
Updated Header Extension Profile constants
Make Header Extension Profile constants public and added Cryptex ones.
1 parent 6469fef commit be6ccef

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

header_extension.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
)
1111

1212
const (
13-
headerExtensionProfileOneByte = 0xBEDE
14-
headerExtensionProfileTwoByte = 0x1000
15-
headerExtensionIDReserved = 0xF
13+
headerExtensionIDReserved = 0xF
1614
)
1715

1816
// HeaderExtension represents an RTP extension header.
@@ -140,7 +138,7 @@ func (e *OneByteHeaderExtension) Del(id uint8) error {
140138
// Unmarshal parses the extension payload.
141139
func (e *OneByteHeaderExtension) Unmarshal(buf []byte) (int, error) {
142140
profile := binary.BigEndian.Uint16(buf[0:2])
143-
if profile != headerExtensionProfileOneByte {
141+
if profile != ExtensionProfileOneByte {
144142
return 0, fmt.Errorf("%w actual(%x)", errHeaderExtensionNotFound, buf[0:2])
145143
}
146144
e.payload = buf
@@ -283,7 +281,7 @@ func (e *TwoByteHeaderExtension) Del(id uint8) error {
283281
// Unmarshal parses the extension payload.
284282
func (e *TwoByteHeaderExtension) Unmarshal(buf []byte) (int, error) {
285283
profile := binary.BigEndian.Uint16(buf[0:2])
286-
if profile != headerExtensionProfileTwoByte {
284+
if profile != ExtensionProfileTwoByte {
287285
return 0, fmt.Errorf("%w actual(%x)", errHeaderExtensionNotFound, buf[0:2])
288286
}
289287
e.payload = buf
@@ -354,7 +352,7 @@ func (e *RawExtension) Del(id uint8) error {
354352
// Unmarshal parses the extension from the given buffer.
355353
func (e *RawExtension) Unmarshal(buf []byte) (int, error) {
356354
profile := binary.BigEndian.Uint16(buf[0:2])
357-
if profile == headerExtensionProfileOneByte || profile == headerExtensionProfileTwoByte {
355+
if profile == ExtensionProfileOneByte || profile == ExtensionProfileTwoByte {
358356
return 0, fmt.Errorf("%w actual(%x)", errHeaderExtensionNotFound, buf[0:2])
359357
}
360358
e.payload = buf

packet.go

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,37 @@ type Packet struct {
5353
}
5454

5555
const (
56-
headerLength = 4
57-
versionShift = 6
58-
versionMask = 0x3
59-
paddingShift = 5
60-
paddingMask = 0x1
61-
extensionShift = 4
62-
extensionMask = 0x1
63-
extensionProfileOneByte = 0xBEDE
64-
extensionProfileTwoByte = 0x1000
65-
extensionIDReserved = 0xF
66-
ccMask = 0xF
67-
markerShift = 7
68-
markerMask = 0x1
69-
ptMask = 0x7F
70-
seqNumOffset = 2
71-
seqNumLength = 2
72-
timestampOffset = 4
73-
timestampLength = 4
74-
ssrcOffset = 8
75-
ssrcLength = 4
76-
csrcOffset = 12
77-
csrcLength = 4
56+
// ExtensionProfileOneByte is the RTP One Byte Header Extension Profile, defined in RFC 8285.
57+
ExtensionProfileOneByte = 0xBEDE
58+
// ExtensionProfileTwoByte is the RTP Two Byte Header Extension Profile, defined in RFC 8285.
59+
ExtensionProfileTwoByte = 0x1000
60+
// CryptexProfileOneByte is the Cryptex One Byte Header Extension Profile, defined in RFC 9335.
61+
CryptexProfileOneByte = 0xC0DE
62+
// CryptexProfileTwoByte is the Cryptex Two Byte Header Extension Profile, defined in RFC 9335.
63+
CryptexProfileTwoByte = 0xC2DE
64+
)
65+
66+
const (
67+
headerLength = 4
68+
versionShift = 6
69+
versionMask = 0x3
70+
paddingShift = 5
71+
paddingMask = 0x1
72+
extensionShift = 4
73+
extensionMask = 0x1
74+
extensionIDReserved = 0xF
75+
ccMask = 0xF
76+
markerShift = 7
77+
markerMask = 0x1
78+
ptMask = 0x7F
79+
seqNumOffset = 2
80+
seqNumLength = 2
81+
timestampOffset = 4
82+
timestampLength = 4
83+
ssrcOffset = 8
84+
ssrcLength = 4
85+
csrcOffset = 12
86+
csrcLength = 4
7887
)
7988

8089
// String helps with debugging by printing packet information in a readable way.
@@ -164,7 +173,7 @@ func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit,cy
164173
return n, fmt.Errorf("size %d < %d: %w", len(buf), extensionEnd, errHeaderSizeInsufficientForExtension)
165174
}
166175

167-
if h.ExtensionProfile == extensionProfileOneByte || h.ExtensionProfile == extensionProfileTwoByte {
176+
if h.ExtensionProfile == ExtensionProfileOneByte || h.ExtensionProfile == ExtensionProfileTwoByte {
168177
var (
169178
extid uint8
170179
payloadLen int
@@ -177,7 +186,7 @@ func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit,cy
177186
continue
178187
}
179188

180-
if h.ExtensionProfile == extensionProfileOneByte {
189+
if h.ExtensionProfile == ExtensionProfileOneByte {
181190
extid = buf[n] >> 4
182191
payloadLen = int(buf[n]&^0xF0 + 1)
183192
n++
@@ -312,14 +321,14 @@ func (h Header) MarshalTo(buf []byte) (n int, err error) { //nolint:cyclop
312321

313322
switch h.ExtensionProfile {
314323
// RFC 8285 RTP One Byte Header Extension
315-
case extensionProfileOneByte:
324+
case ExtensionProfileOneByte:
316325
for _, extension := range h.Extensions {
317326
buf[n] = extension.id<<4 | (uint8(len(extension.payload)) - 1) // nolint: gosec // G115
318327
n++
319328
n += copy(buf[n:], extension.payload)
320329
}
321330
// RFC 8285 RTP Two Byte Header Extension
322-
case extensionProfileTwoByte:
331+
case ExtensionProfileTwoByte:
323332
for _, extension := range h.Extensions {
324333
buf[n] = extension.id
325334
n++
@@ -363,12 +372,12 @@ func (h Header) MarshalSize() int {
363372

364373
switch h.ExtensionProfile {
365374
// RFC 8285 RTP One Byte Header Extension
366-
case extensionProfileOneByte:
375+
case ExtensionProfileOneByte:
367376
for _, extension := range h.Extensions {
368377
extSize += 1 + len(extension.payload)
369378
}
370379
// RFC 8285 RTP Two Byte Header Extension
371-
case extensionProfileTwoByte:
380+
case ExtensionProfileTwoByte:
372381
for _, extension := range h.Extensions {
373382
extSize += 2 + len(extension.payload)
374383
}
@@ -388,15 +397,15 @@ func (h *Header) SetExtension(id uint8, payload []byte) error { //nolint:gocogni
388397
if h.Extension { // nolint: nestif
389398
switch h.ExtensionProfile {
390399
// RFC 8285 RTP One Byte Header Extension
391-
case extensionProfileOneByte:
400+
case ExtensionProfileOneByte:
392401
if id < 1 || id > 14 {
393402
return fmt.Errorf("%w actual(%d)", errRFC8285OneByteHeaderIDRange, id)
394403
}
395404
if len(payload) > 16 {
396405
return fmt.Errorf("%w actual(%d)", errRFC8285OneByteHeaderSize, len(payload))
397406
}
398407
// RFC 8285 RTP Two Byte Header Extension
399-
case extensionProfileTwoByte:
408+
case ExtensionProfileTwoByte:
400409
if id < 1 {
401410
return fmt.Errorf("%w actual(%d)", errRFC8285TwoByteHeaderIDRange, id)
402411
}
@@ -428,9 +437,9 @@ func (h *Header) SetExtension(id uint8, payload []byte) error { //nolint:gocogni
428437

429438
switch payloadLen := len(payload); {
430439
case payloadLen <= 16:
431-
h.ExtensionProfile = extensionProfileOneByte
440+
h.ExtensionProfile = ExtensionProfileOneByte
432441
case payloadLen > 16 && payloadLen < 256:
433-
h.ExtensionProfile = extensionProfileTwoByte
442+
h.ExtensionProfile = ExtensionProfileTwoByte
434443
}
435444

436445
h.Extensions = append(h.Extensions, Extension{id: id, payload: payload})

packet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ func BenchmarkUnmarshal(b *testing.B) {
13911391
Header: Header{
13921392
Extension: true,
13931393
CSRC: []uint32{1, 2},
1394-
ExtensionProfile: extensionProfileTwoByte,
1394+
ExtensionProfile: ExtensionProfileTwoByte,
13951395
Extensions: []Extension{
13961396
{id: 1, payload: []byte{3, 4}},
13971397
{id: 2, payload: []byte{5, 6}},

0 commit comments

Comments
 (0)