Repository: pion/rtp
Pull request #116 (2021-01-28 19:28:14)

Mtu fix
#### Description
Set MTU type to an uint16 as maximum MTU size is 1500
This is a breaking change

#### Reference issue
Fixes #...

codecov[bot]: # [Codecov](https://codecov.io/gh/pion/rtp/pull/116?src=pr&el=h1) Report
> Merging [#116](https://codecov.io/gh/pion/rtp/pull/116?src=pr&el=desc) (bf2700a) into [master](https://codecov.io/gh/pion/rtp/commit/b58e1cba07f8cf32c0b42167e3c9df8f8635d6c2?el=desc) (b58e1cb) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/pion/rtp/pull/116/graphs/tree.svg?width=650&height=150&src=pr&token=4KrbiBsspj)](https://codecov.io/gh/pion/rtp/pull/116?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #116   +/-   ##
=======================================
  Coverage   91.37%   91.37%           
=======================================
  Files          13       13           
  Lines         626      626           
=======================================
  Hits          572      572           
  Misses         37       37           
  Partials       17       17           
```

| Flag | Coverage Δ | |
|---|---|---|
| go | `91.37% <100.00%> (ø)` | |
| wasm | `?` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/pion/rtp/pull/116?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [codecs/g711\_packet.go](https://codecov.io/gh/pion/rtp/pull/116/diff?src=pr&el=tree#diff-Y29kZWNzL2c3MTFfcGFja2V0Lmdv) | `100.00% <ø> (ø)` | |
| [codecs/opus\_packet.go](https://codecov.io/gh/pion/rtp/pull/116/diff?src=pr&el=tree#diff-Y29kZWNzL29wdXNfcGFja2V0Lmdv) | `100.00% <ø> (ø)` | |
| [packetizer.go](https://codecov.io/gh/pion/rtp/pull/116/diff?src=pr&el=tree#diff-cGFja2V0aXplci5nbw==) | `83.33% <ø> (ø)` | |
| [codecs/g722\_packet.go](https://codecov.io/gh/pion/rtp/pull/116/diff?src=pr&el=tree#diff-Y29kZWNzL2c3MjJfcGFja2V0Lmdv) | `100.00% <100.00%> (ø)` | |
| [codecs/h264\_packet.go](https://codecov.io/gh/pion/rtp/pull/116/diff?src=pr&el=tree#diff-Y29kZWNzL2gyNjRfcGFja2V0Lmdv) | `97.87% <100.00%> (ø)` | |
| [codecs/vp8\_packet.go](https://codecov.io/gh/pion/rtp/pull/116/diff?src=pr&el=tree#diff-Y29kZWNzL3ZwOF9wYWNrZXQuZ28=) | `100.00% <100.00%> (ø)` | |
| [codecs/vp9\_packet.go](https://codecov.io/gh/pion/rtp/pull/116/diff?src=pr&el=tree#diff-Y29kZWNzL3ZwOV9wYWNrZXQuZ28=) | `93.23% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pion/rtp/pull/116?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pion/rtp/pull/116?src=pr&el=footer). Last update [b58e1cb...bf2700a](https://codecov.io/gh/pion/rtp/pull/116?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Sean-Der: Hey @metaclips 

Thanks for the PR
metaclips: Hello @Sean-Der should I rebase, or we close the PR?
Sean-Der: @metaclips sorry about the noise, I am rebasing now :) 

Sorry this got ignored for so long.

Patch:
diff --git a/AUTHORS.txt b/AUTHORS.txt
index 5e5c680..0a0c424 100644
--- a/AUTHORS.txt
+++ b/AUTHORS.txt
@@ -6,6 +6,7 @@
 adwpc <adwpc@hotmail.com>
 aler9 <46489434+aler9@users.noreply.github.com>
 Antoine Baché <antoine.bache@epitech.eu>
+Antoine Baché <antoine@tenten.app>
 Atsushi Watanabe <atsushi.w@ieee.org>
 Bao Nguyen <bao@n4n.dev>
 debiandebiandebian <debiandebiandebiandebian@gmail.com>
@@ -20,6 +21,7 @@ Luke Curley <kixelated@gmail.com>
 lxb <xiangbing.li@uama.com.cn>
 Michael MacDonald <github@macdonald.cx>
 Michael MacDonald <mike.macdonald@savantsystems.com>
+Michael Uti <utimichael9@gmail.com>
 Raphael Derosso Pereira <raphaelpereira@gmail.com>
 Rob Lofthouse <ri.lofthouse@gmail.com>
 Robin Raymond <robin@goheadroom.com>
diff --git a/codecs/g711_packet.go b/codecs/g711_packet.go
index a74876f..2348a79 100644
--- a/codecs/g711_packet.go
+++ b/codecs/g711_packet.go
@@ -4,13 +4,13 @@ package codecs
 type G711Payloader struct{}
 
 // Payload fragments an G711 packet across one or more byte arrays
-func (p *G711Payloader) Payload(mtu int, payload []byte) [][]byte {
+func (p *G711Payloader) Payload(mtu uint16, payload []byte) [][]byte {
 	var out [][]byte
 	if payload == nil || mtu <= 0 {
 		return out
 	}
 
-	for len(payload) > mtu {
+	for len(payload) > int(mtu) {
 		o := make([]byte, mtu)
 		copy(o, payload[:mtu])
 		payload = payload[mtu:]
diff --git a/codecs/g722_packet.go b/codecs/g722_packet.go
index 70c9883..13e17b6 100644
--- a/codecs/g722_packet.go
+++ b/codecs/g722_packet.go
@@ -4,13 +4,13 @@ package codecs
 type G722Payloader struct{}
 
 // Payload fragments an G722 packet across one or more byte arrays
-func (p *G722Payloader) Payload(mtu int, payload []byte) [][]byte {
+func (p *G722Payloader) Payload(mtu uint16, payload []byte) [][]byte {
 	var out [][]byte
-	if payload == nil || mtu <= 0 {
+	if payload == nil || mtu == 0 {
 		return out
 	}
 
-	for len(payload) > mtu {
+	for len(payload) > int(mtu) {
 		o := make([]byte, mtu)
 		copy(o, payload[:mtu])
 		payload = payload[mtu:]
diff --git a/codecs/h264_packet.go b/codecs/h264_packet.go
index 7a22642..a1e0381 100644
--- a/codecs/h264_packet.go
+++ b/codecs/h264_packet.go
@@ -61,7 +61,7 @@ func emitNalus(nals []byte, emit func([]byte)) {
 }
 
 // Payload fragments a H264 packet across one or more byte arrays
-func (p *H264Payloader) Payload(mtu int, payload []byte) [][]byte {
+func (p *H264Payloader) Payload(mtu uint16, payload []byte) [][]byte {
 	var payloads [][]byte
 	if len(payload) == 0 {
 		return payloads
@@ -80,7 +80,7 @@ func (p *H264Payloader) Payload(mtu int, payload []byte) [][]byte {
 		}
 
 		// Single NALU
-		if len(nalu) <= mtu {
+		if len(nalu) <= int(mtu) {
 			out := make([]byte, len(nalu))
 			copy(out, nalu)
 			payloads = append(payloads, out)
@@ -88,7 +88,7 @@ func (p *H264Payloader) Payload(mtu int, payload []byte) [][]byte {
 		}
 
 		// FU-A
-		maxFragmentSize := mtu - fuaHeaderSize
+		maxFragmentSize := int(mtu) - fuaHeaderSize
 
 		// The FU payload consists of fragments of the payload of the fragmented
 		// NAL unit so that if the fragmentation unit payloads of consecutive
diff --git a/codecs/opus_packet.go b/codecs/opus_packet.go
index 2ab4de7..dfae73c 100644
--- a/codecs/opus_packet.go
+++ b/codecs/opus_packet.go
@@ -4,7 +4,7 @@ package codecs
 type OpusPayloader struct{}
 
 // Payload fragments an Opus packet across one or more byte arrays
-func (p *OpusPayloader) Payload(mtu int, payload []byte) [][]byte {
+func (p *OpusPayloader) Payload(mtu uint16, payload []byte) [][]byte {
 	if payload == nil {
 		return [][]byte{}
 	}
diff --git a/codecs/vp8_packet.go b/codecs/vp8_packet.go
index 24e7200..d9d986d 100644
--- a/codecs/vp8_packet.go
+++ b/codecs/vp8_packet.go
@@ -11,7 +11,7 @@ const (
 )
 
 // Payload fragments a VP8 packet across one or more byte arrays
-func (p *VP8Payloader) Payload(mtu int, payload []byte) [][]byte {
+func (p *VP8Payloader) Payload(mtu uint16, payload []byte) [][]byte {
 	/*
 	 * https://tools.ietf.org/html/rfc7741#section-4.2
 	 *
@@ -44,7 +44,7 @@ func (p *VP8Payloader) Payload(mtu int, payload []byte) [][]byte {
 		}
 	}
 
-	maxFragmentSize := mtu - usingHeaderSize
+	maxFragmentSize := int(mtu) - usingHeaderSize
 
 	payloadData := payload
 	payloadDataRemaining := len(payload)
diff --git a/codecs/vp9_packet.go b/codecs/vp9_packet.go
index 9d05340..75cf4eb 100644
--- a/codecs/vp9_packet.go
+++ b/codecs/vp9_packet.go
@@ -23,7 +23,7 @@ const (
 )
 
 // Payload fragments an VP9 packet across one or more byte arrays
-func (p *VP9Payloader) Payload(mtu int, payload []byte) [][]byte {
+func (p *VP9Payloader) Payload(mtu uint16, payload []byte) [][]byte {
 	/*
 	 * https://www.ietf.org/id/draft-ietf-payload-vp9-13.txt
 	 *
@@ -75,7 +75,7 @@ func (p *VP9Payloader) Payload(mtu int, payload []byte) [][]byte {
 		return [][]byte{}
 	}
 
-	maxFragmentSize := mtu - vp9HeaderSize
+	maxFragmentSize := int(mtu) - vp9HeaderSize
 	payloadDataRemaining := len(payload)
 	payloadDataIndex := 0
 
diff --git a/packetizer.go b/packetizer.go
index edde1e1..19f6c75 100644
--- a/packetizer.go
+++ b/packetizer.go
@@ -6,7 +6,7 @@ import (
 
 // Payloader payloads a byte array for use as rtp.Packet payloads
 type Payloader interface {
-	Payload(mtu int, payload []byte) [][]byte
+	Payload(mtu uint16, payload []byte) [][]byte
 }
 
 // Packetizer packetizes a payload
@@ -17,7 +17,7 @@ type Packetizer interface {
 }
 
 type packetizer struct {
-	MTU              int
+	MTU              uint16
 	PayloadType      uint8
 	SSRC             uint32
 	Payloader        Payloader
@@ -31,7 +31,7 @@ type packetizer struct {
 }
 
 // NewPacketizer returns a new instance of a Packetizer for a specific payloader
-func NewPacketizer(mtu int, pt uint8, ssrc uint32, payloader Payloader, sequencer Sequencer, clockRate uint32) Packetizer {
+func NewPacketizer(mtu uint16, pt uint8, ssrc uint32, payloader Payloader, sequencer Sequencer, clockRate uint32) Packetizer {
 	return &packetizer{
 		MTU:         mtu,
 		PayloadType: pt,


Test patch:
diff --git a/codecs/g711_packet_test.go b/codecs/g711_packet_test.go
index d260a06..36ee729 100644
--- a/codecs/g711_packet_test.go
+++ b/codecs/g711_packet_test.go
@@ -45,20 +45,9 @@ func TestG711Payloader(t *testing.T) {
 	}
 
 	payload := []byte{0x90, 0x90, 0x90}
-	// Nil payload
-	res := p.Payload(-1, nil)
-	if len(res) != 0 {
-		t.Fatal("Generated payload should be empty")
-	}
-
-	// Negative MTU, small payload
-	res = p.Payload(-1, payload)
-	if len(res) != 0 {
-		t.Fatal("Generated payload should be empty")
-	}
 
 	// 0 MTU, small payload
-	res = p.Payload(0, payload)
+	res := p.Payload(0, payload)
 	if len(res) != 0 {
 		t.Fatal("Generated payload should be empty")
 	}
@@ -70,7 +59,7 @@ func TestG711Payloader(t *testing.T) {
 	}
 
 	// Positive MTU, small payload
-	res = p.Payload(len(payload)-1, payload)
+	res = p.Payload(uint16(len(payload)-1), payload)
 	if len(res) != len(payload)-1 {
 		t.Fatal("Generated payload should be the same smaller than original payload size")
 	}
diff --git a/codecs/g722_packet_test.go b/codecs/g722_packet_test.go
index 8d1198b..cc75e6f 100644
--- a/codecs/g722_packet_test.go
+++ b/codecs/g722_packet_test.go
@@ -45,20 +45,9 @@ func TestG722Payloader(t *testing.T) {
 	}
 
 	payload := []byte{0x90, 0x90, 0x90}
-	// Nil payload
-	res := p.Payload(-1, nil)
-	if len(res) != 0 {
-		t.Fatal("Generated payload should be empty")
-	}
-
-	// Negative MTU, small payload
-	res = p.Payload(-1, payload)
-	if len(res) != 0 {
-		t.Fatal("Generated payload should be empty")
-	}
 
 	// 0 MTU, small payload
-	res = p.Payload(0, payload)
+	res := p.Payload(0, payload)
 	if len(res) != 0 {
 		t.Fatal("Generated payload should be empty")
 	}
@@ -70,7 +59,7 @@ func TestG722Payloader(t *testing.T) {
 	}
 
 	// Positive MTU, small payload
-	res = p.Payload(len(payload)-1, payload)
+	res = p.Payload(uint16(len(payload)-1), payload)
 	if len(res) != len(payload)-1 {
 		t.Fatal("Generated payload should be the same smaller than original payload size")
 	}
diff --git a/codecs/opus_packet_test.go b/codecs/opus_packet_test.go
index 5366a6b..1c09a5a 100644
--- a/codecs/opus_packet_test.go
+++ b/codecs/opus_packet_test.go
@@ -52,12 +52,6 @@ func TestOpusPayloader_Payload(t *testing.T) {
 		t.Fatal("Generated payload should be the 1")
 	}
 
-	// Negative MTU, small payload
-	res = pck.Payload(-1, payload)
-	if len(res) != 1 {
-		t.Fatal("Generated payload should be the 1")
-	}
-
 	// Positive MTU, small payload
 	res = pck.Payload(2, payload)
 	if len(res) != 1 {
diff --git a/codecs/vp8_packet_test.go b/codecs/vp8_packet_test.go
index 37d160d..7bfe0b6 100644
--- a/codecs/vp8_packet_test.go
+++ b/codecs/vp8_packet_test.go
@@ -112,7 +112,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) {
 func TestVP8Payloader_Payload(t *testing.T) {
 	testCases := map[string]struct {
 		payloader VP8Payloader
-		mtu       int
+		mtu       uint16
 		payload   [][]byte
 		expected  [][][]byte
 	}{
@@ -199,12 +199,6 @@ func TestVP8Payloader_Payload(t *testing.T) {
 		if len(res) != 0 {
 			t.Fatal("Generated payload should be empty")
 		}
-
-		// Negative MTU, small payload
-		res = pck.Payload(-1, payload)
-		if len(res) != 0 {
-			t.Fatal("Generated payload should be empty")
-		}
 	})
 }
 
diff --git a/codecs/vp9_packet_test.go b/codecs/vp9_packet_test.go
index d226324..a1f44af 100644
--- a/codecs/vp9_packet_test.go
+++ b/codecs/vp9_packet_test.go
@@ -205,7 +205,7 @@ func TestVP9Payloader_Payload(t *testing.T) {
 
 	cases := map[string]struct {
 		b   [][]byte
-		mtu int
+		mtu uint16
 		res [][]byte
 	}{
 		"NilPayload": {
@@ -218,11 +218,6 @@ func TestVP9Payloader_Payload(t *testing.T) {
 			mtu: 1,
 			res: [][]byte{},
 		},
-		"NegativeMTU": {
-			b:   [][]byte{{0x00, 0x00}},
-			mtu: -1,
-			res: [][]byte{},
-		},
 		"OnePacket": {
 			b:   [][]byte{{0x01, 0x02}},
 			mtu: 10,