Repository: pion/rtp
Pull request #25 (2019-10-10 08:46:57)

Add G711 Payloader
Relates to pion/webrtc#851

codecov-io: # [Codecov](https://codecov.io/gh/pion/rtp/pull/25?src=pr&el=h1) Report
> Merging [#25](https://codecov.io/gh/pion/rtp/pull/25?src=pr&el=desc) into [master](https://codecov.io/gh/pion/rtp/commit/23312f4b270b6a469355099f9cfb96d871240098?src=pr&el=desc) will **increase** coverage by `0.77%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master      #25      +/-   ##
==========================================
+ Coverage   78.98%   79.75%   +0.77%     
==========================================
  Files           8        9       +1     
  Lines         314      326      +12     
==========================================
+ Hits          248      260      +12     
  Misses         57       57              
  Partials        9        9
```


| [Impacted Files](https://codecov.io/gh/pion/rtp/pull/25?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [codecs/g711\_packet.go](https://codecov.io/gh/pion/rtp/pull/25/diff?src=pr&el=tree#diff-Y29kZWNzL2c3MTFfcGFja2V0Lmdv) | `100% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pion/rtp/pull/25?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/25?src=pr&el=footer). Last update [23312f4...a253d4b](https://codecov.io/gh/pion/rtp/pull/25?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/.golangci.yml b/.golangci.yml
index a56dd77..e12f4ed 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -9,6 +9,8 @@ linters:
   disable:
     - lll
     - maligned
+    - funlen
+    - dupl
 
 issues:
   exclude-use-default: false
diff --git a/.travis.yml b/.travis.yml
index ba761e1..cd59561 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,21 +1,37 @@
 language: go
 
+branches:
+  only:
+  - master
+
 go:
   - "1.x" # use the latest Go release
 
 env:
   - GO111MODULE=on
 
-notifications:
-  email: false
-
-before_script:
-  - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.13
+cache:
+  directories:
+    - $HOME/.cache/go-build
+    - $GOPATH/pkg/mod
 
-script:
-  - golangci-lint run ./...
-  - go test -coverpkg=$(go list ./... | tr '\n' ',') -coverprofile=cover.out -v -race -covermode=atomic ./...
-  - bash <(curl -s https://codecov.io/bash)
-  - bash .github/assert-contributors.sh
-  - bash .github/lint-disallowed-functions-in-library.sh
-  - bash .github/lint-commit-message.sh
+jobs:
+  include:
+    - stage: lint
+      before_script:
+        - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.18.0
+      install: skip
+      script:
+        - bash .github/assert-contributors.sh
+        - bash .github/lint-disallowed-functions-in-library.sh
+        - bash .github/lint-commit-message.sh
+        - golangci-lint run ./...
+    - stage: test
+      before_install:
+        - go mod download
+      install:
+        - go build ./...
+      script:
+        - go test -coverpkg=$(go list ./... | tr '\n' ',') -coverprofile=cover.out -v -race -covermode=atomic ./...
+      after_success:
+        - travis_retry bash <(curl -s https://codecov.io/bash)
diff --git a/codecs/g711_packet.go b/codecs/g711_packet.go
new file mode 100644
index 0000000..a74876f
--- /dev/null
+++ b/codecs/g711_packet.go
@@ -0,0 +1,22 @@
+package codecs
+
+// G711Payloader payloads G711 packets
+type G711Payloader struct{}
+
+// Payload fragments an G711 packet across one or more byte arrays
+func (p *G711Payloader) Payload(mtu int, payload []byte) [][]byte {
+	var out [][]byte
+	if payload == nil || mtu <= 0 {
+		return out
+	}
+
+	for len(payload) > mtu {
+		o := make([]byte, mtu)
+		copy(o, payload[:mtu])
+		payload = payload[mtu:]
+		out = append(out, o)
+	}
+	o := make([]byte, len(payload))
+	copy(o, payload)
+	return append(out, o)
+}


Test patch:
diff --git a/codecs/g711_packet_test.go b/codecs/g711_packet_test.go
new file mode 100644
index 0000000..21aa514
--- /dev/null
+++ b/codecs/g711_packet_test.go
@@ -0,0 +1,84 @@
+package codecs
+
+import (
+	"bytes"
+	"crypto/rand"
+	"math"
+	"testing"
+)
+
+func TestG711Payloader(t *testing.T) {
+	p := G711Payloader{}
+
+	const (
+		testlen = 10000
+		testmtu = 1500
+	)
+
+	//generate random 8-bit g722 samples
+	samples := make([]byte, testlen)
+	_, err := rand.Read(samples)
+
+	if err != nil {
+		t.Fatal("RNG Error: ", err)
+	}
+
+	//make a copy, for payloader input
+	samplesIn := make([]byte, testlen)
+	copy(samplesIn, samples)
+
+	//split our samples into payloads
+	payloads := p.Payload(testmtu, samplesIn)
+
+	outcnt := int(math.Ceil(float64(testlen) / testmtu))
+	if len(payloads) != outcnt {
+		t.Fatalf("Generated %d payloads instead of %d", len(payloads), outcnt)
+	}
+
+	if !bytes.Equal(samplesIn, samples) {
+		t.Fatal("Modified input samples")
+	}
+
+	samplesOut := bytes.Join(payloads, []byte{})
+
+	if !bytes.Equal(samplesIn, samplesOut) {
+		t.Fatal("Output samples don't match")
+	}
+
+	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)
+	if len(res) != 0 {
+		t.Fatal("Generated payload should be empty")
+	}
+
+	// Positive MTU, small payload
+	res = p.Payload(1, payload)
+	if len(res) != len(payload) {
+		t.Fatal("Generated payload should be the same size as original payload size")
+	}
+
+	// Positive MTU, small payload
+	res = p.Payload(len(payload)-1, payload)
+	if len(res) != len(payload)-1 {
+		t.Fatal("Generated payload should be the same smaller than original payload size")
+	}
+
+	// Positive MTU, small payload
+	res = p.Payload(10, payload)
+	if len(res) != 1 {
+		t.Fatal("Generated payload should be 1")
+	}
+}