Skip to content

Commit e8906ad

Browse files
metaclipsSean-Der
authored andcommitted
Make MTU a uint16
A MTU can never be negative so this removes a class of runtime errors.
1 parent 6cf5e9b commit e8906ad

13 files changed

+24
-61
lines changed

AUTHORS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
88
Antoine Baché <[email protected]>
9+
Antoine Baché <[email protected]>
910
Atsushi Watanabe <[email protected]>
1011
Bao Nguyen <[email protected]>
1112
debiandebiandebian <[email protected]>
@@ -20,6 +21,7 @@ Luke Curley <[email protected]>
2021
2122
Michael MacDonald <[email protected]>
2223
Michael MacDonald <[email protected]>
24+
Michael Uti <[email protected]>
2325
Raphael Derosso Pereira <[email protected]>
2426
Rob Lofthouse <[email protected]>
2527
Robin Raymond <[email protected]>

codecs/g711_packet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ package codecs
44
type G711Payloader struct{}
55

66
// Payload fragments an G711 packet across one or more byte arrays
7-
func (p *G711Payloader) Payload(mtu int, payload []byte) [][]byte {
7+
func (p *G711Payloader) Payload(mtu uint16, payload []byte) [][]byte {
88
var out [][]byte
99
if payload == nil || mtu <= 0 {
1010
return out
1111
}
1212

13-
for len(payload) > mtu {
13+
for len(payload) > int(mtu) {
1414
o := make([]byte, mtu)
1515
copy(o, payload[:mtu])
1616
payload = payload[mtu:]

codecs/g711_packet_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,9 @@ func TestG711Payloader(t *testing.T) {
4545
}
4646

4747
payload := []byte{0x90, 0x90, 0x90}
48-
// Nil payload
49-
res := p.Payload(-1, nil)
50-
if len(res) != 0 {
51-
t.Fatal("Generated payload should be empty")
52-
}
53-
54-
// Negative MTU, small payload
55-
res = p.Payload(-1, payload)
56-
if len(res) != 0 {
57-
t.Fatal("Generated payload should be empty")
58-
}
5948

6049
// 0 MTU, small payload
61-
res = p.Payload(0, payload)
50+
res := p.Payload(0, payload)
6251
if len(res) != 0 {
6352
t.Fatal("Generated payload should be empty")
6453
}
@@ -70,7 +59,7 @@ func TestG711Payloader(t *testing.T) {
7059
}
7160

7261
// Positive MTU, small payload
73-
res = p.Payload(len(payload)-1, payload)
62+
res = p.Payload(uint16(len(payload)-1), payload)
7463
if len(res) != len(payload)-1 {
7564
t.Fatal("Generated payload should be the same smaller than original payload size")
7665
}

codecs/g722_packet.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ package codecs
44
type G722Payloader struct{}
55

66
// Payload fragments an G722 packet across one or more byte arrays
7-
func (p *G722Payloader) Payload(mtu int, payload []byte) [][]byte {
7+
func (p *G722Payloader) Payload(mtu uint16, payload []byte) [][]byte {
88
var out [][]byte
9-
if payload == nil || mtu <= 0 {
9+
if payload == nil || mtu == 0 {
1010
return out
1111
}
1212

13-
for len(payload) > mtu {
13+
for len(payload) > int(mtu) {
1414
o := make([]byte, mtu)
1515
copy(o, payload[:mtu])
1616
payload = payload[mtu:]

codecs/g722_packet_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,9 @@ func TestG722Payloader(t *testing.T) {
4545
}
4646

4747
payload := []byte{0x90, 0x90, 0x90}
48-
// Nil payload
49-
res := p.Payload(-1, nil)
50-
if len(res) != 0 {
51-
t.Fatal("Generated payload should be empty")
52-
}
53-
54-
// Negative MTU, small payload
55-
res = p.Payload(-1, payload)
56-
if len(res) != 0 {
57-
t.Fatal("Generated payload should be empty")
58-
}
5948

6049
// 0 MTU, small payload
61-
res = p.Payload(0, payload)
50+
res := p.Payload(0, payload)
6251
if len(res) != 0 {
6352
t.Fatal("Generated payload should be empty")
6453
}
@@ -70,7 +59,7 @@ func TestG722Payloader(t *testing.T) {
7059
}
7160

7261
// Positive MTU, small payload
73-
res = p.Payload(len(payload)-1, payload)
62+
res = p.Payload(uint16(len(payload)-1), payload)
7463
if len(res) != len(payload)-1 {
7564
t.Fatal("Generated payload should be the same smaller than original payload size")
7665
}

codecs/h264_packet.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func emitNalus(nals []byte, emit func([]byte)) {
6161
}
6262

6363
// Payload fragments a H264 packet across one or more byte arrays
64-
func (p *H264Payloader) Payload(mtu int, payload []byte) [][]byte {
64+
func (p *H264Payloader) Payload(mtu uint16, payload []byte) [][]byte {
6565
var payloads [][]byte
6666
if len(payload) == 0 {
6767
return payloads
@@ -80,15 +80,15 @@ func (p *H264Payloader) Payload(mtu int, payload []byte) [][]byte {
8080
}
8181

8282
// Single NALU
83-
if len(nalu) <= mtu {
83+
if len(nalu) <= int(mtu) {
8484
out := make([]byte, len(nalu))
8585
copy(out, nalu)
8686
payloads = append(payloads, out)
8787
return
8888
}
8989

9090
// FU-A
91-
maxFragmentSize := mtu - fuaHeaderSize
91+
maxFragmentSize := int(mtu) - fuaHeaderSize
9292

9393
// The FU payload consists of fragments of the payload of the fragmented
9494
// NAL unit so that if the fragmentation unit payloads of consecutive

codecs/opus_packet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package codecs
44
type OpusPayloader struct{}
55

66
// Payload fragments an Opus packet across one or more byte arrays
7-
func (p *OpusPayloader) Payload(mtu int, payload []byte) [][]byte {
7+
func (p *OpusPayloader) Payload(mtu uint16, payload []byte) [][]byte {
88
if payload == nil {
99
return [][]byte{}
1010
}

codecs/opus_packet_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ func TestOpusPayloader_Payload(t *testing.T) {
5252
t.Fatal("Generated payload should be the 1")
5353
}
5454

55-
// Negative MTU, small payload
56-
res = pck.Payload(-1, payload)
57-
if len(res) != 1 {
58-
t.Fatal("Generated payload should be the 1")
59-
}
60-
6155
// Positive MTU, small payload
6256
res = pck.Payload(2, payload)
6357
if len(res) != 1 {

codecs/vp8_packet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const (
1111
)
1212

1313
// Payload fragments a VP8 packet across one or more byte arrays
14-
func (p *VP8Payloader) Payload(mtu int, payload []byte) [][]byte {
14+
func (p *VP8Payloader) Payload(mtu uint16, payload []byte) [][]byte {
1515
/*
1616
* https://tools.ietf.org/html/rfc7741#section-4.2
1717
*
@@ -44,7 +44,7 @@ func (p *VP8Payloader) Payload(mtu int, payload []byte) [][]byte {
4444
}
4545
}
4646

47-
maxFragmentSize := mtu - usingHeaderSize
47+
maxFragmentSize := int(mtu) - usingHeaderSize
4848

4949
payloadData := payload
5050
payloadDataRemaining := len(payload)

codecs/vp8_packet_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) {
112112
func TestVP8Payloader_Payload(t *testing.T) {
113113
testCases := map[string]struct {
114114
payloader VP8Payloader
115-
mtu int
115+
mtu uint16
116116
payload [][]byte
117117
expected [][][]byte
118118
}{
@@ -199,12 +199,6 @@ func TestVP8Payloader_Payload(t *testing.T) {
199199
if len(res) != 0 {
200200
t.Fatal("Generated payload should be empty")
201201
}
202-
203-
// Negative MTU, small payload
204-
res = pck.Payload(-1, payload)
205-
if len(res) != 0 {
206-
t.Fatal("Generated payload should be empty")
207-
}
208202
})
209203
}
210204

codecs/vp9_packet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
)
2424

2525
// Payload fragments an VP9 packet across one or more byte arrays
26-
func (p *VP9Payloader) Payload(mtu int, payload []byte) [][]byte {
26+
func (p *VP9Payloader) Payload(mtu uint16, payload []byte) [][]byte {
2727
/*
2828
* https://www.ietf.org/id/draft-ietf-payload-vp9-13.txt
2929
*
@@ -75,7 +75,7 @@ func (p *VP9Payloader) Payload(mtu int, payload []byte) [][]byte {
7575
return [][]byte{}
7676
}
7777

78-
maxFragmentSize := mtu - vp9HeaderSize
78+
maxFragmentSize := int(mtu) - vp9HeaderSize
7979
payloadDataRemaining := len(payload)
8080
payloadDataIndex := 0
8181

codecs/vp9_packet_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestVP9Payloader_Payload(t *testing.T) {
205205

206206
cases := map[string]struct {
207207
b [][]byte
208-
mtu int
208+
mtu uint16
209209
res [][]byte
210210
}{
211211
"NilPayload": {
@@ -218,11 +218,6 @@ func TestVP9Payloader_Payload(t *testing.T) {
218218
mtu: 1,
219219
res: [][]byte{},
220220
},
221-
"NegativeMTU": {
222-
b: [][]byte{{0x00, 0x00}},
223-
mtu: -1,
224-
res: [][]byte{},
225-
},
226221
"OnePacket": {
227222
b: [][]byte{{0x01, 0x02}},
228223
mtu: 10,

packetizer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
// Payloader payloads a byte array for use as rtp.Packet payloads
88
type Payloader interface {
9-
Payload(mtu int, payload []byte) [][]byte
9+
Payload(mtu uint16, payload []byte) [][]byte
1010
}
1111

1212
// Packetizer packetizes a payload
@@ -17,7 +17,7 @@ type Packetizer interface {
1717
}
1818

1919
type packetizer struct {
20-
MTU int
20+
MTU uint16
2121
PayloadType uint8
2222
SSRC uint32
2323
Payloader Payloader
@@ -31,7 +31,7 @@ type packetizer struct {
3131
}
3232

3333
// NewPacketizer returns a new instance of a Packetizer for a specific payloader
34-
func NewPacketizer(mtu int, pt uint8, ssrc uint32, payloader Payloader, sequencer Sequencer, clockRate uint32) Packetizer {
34+
func NewPacketizer(mtu uint16, pt uint8, ssrc uint32, payloader Payloader, sequencer Sequencer, clockRate uint32) Packetizer {
3535
return &packetizer{
3636
MTU: mtu,
3737
PayloadType: pt,

0 commit comments

Comments
 (0)