Repository: pion/rtp
Pull request #66 (2020-05-19 05:28:49)

Expose AbsSendTimeExtension constructor
Provide a way to generate extension payload from given time.Time.


codecov[bot]: # [Codecov](https://codecov.io/gh/pion/rtp/pull/66?src=pr&el=h1) Report
> Merging [#66](https://codecov.io/gh/pion/rtp/pull/66?src=pr&el=desc) into [master](https://codecov.io/gh/pion/rtp/commit/c23812f8c9034d180c05430d71babea3d4e2db26&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff             @@
##           master      #66      +/-   ##
==========================================
+ Coverage   90.86%   90.88%   +0.01%     
==========================================
  Files          13       13              
  Lines         635      636       +1     
==========================================
+ Hits          577      578       +1     
  Misses         41       41              
  Partials       17       17              
```

| Flag | Coverage Δ | |
|---|---|---|
| #go | `90.88% <100.00%> (+0.01%)` | :arrow_up: |
| #wasm | `90.88% <100.00%> (+0.01%)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pion/rtp/pull/66?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [abssendtimeextension.go](https://codecov.io/gh/pion/rtp/pull/66/diff?src=pr&el=tree#diff-YWJzc2VuZHRpbWVleHRlbnNpb24uZ28=) | `94.87% <100.00%> (+0.42%)` | :arrow_up: |
| [packetizer.go](https://codecov.io/gh/pion/rtp/pull/66/diff?src=pr&el=tree#diff-cGFja2V0aXplci5nbw==) | `85.71% <100.00%> (-0.65%)` | :arrow_down: |

------

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


Patch:
diff --git a/abssendtimeextension.go b/abssendtimeextension.go
index 99a2fe9..dc86c0d 100644
--- a/abssendtimeextension.go
+++ b/abssendtimeextension.go
@@ -45,6 +45,13 @@ func (t *AbsSendTimeExtension) Estimate(receive time.Time) time.Time {
 	return toTime(ntp)
 }
 
+// NewAbsSendTimeExtension makes new AbsSendTimeExtension from time.Time.
+func NewAbsSendTimeExtension(sendTime time.Time) *AbsSendTimeExtension {
+	return &AbsSendTimeExtension{
+		Timestamp: toNtpTime(sendTime) >> 14,
+	}
+}
+
 func toNtpTime(t time.Time) uint64 {
 	var s uint64
 	var f uint64
diff --git a/packet.go b/packet.go
index ff6bd2a..b0e5777 100644
--- a/packet.go
+++ b/packet.go
@@ -393,7 +393,7 @@ func (h *Header) SetExtension(id uint8, payload []byte) error {
 	h.Extension = true
 
 	switch len := len(payload); {
-	case len < 16:
+	case len <= 16:
 		h.ExtensionProfile = extensionProfileOneByte
 	case len > 16 && len < 256:
 		h.ExtensionProfile = extensionProfileTwoByte
diff --git a/packetizer.go b/packetizer.go
index 140ad7b..16c54b0 100644
--- a/packetizer.go
+++ b/packetizer.go
@@ -79,11 +79,9 @@ func (p *packetizer) Packetize(payload []byte, samples uint32) []*Packet {
 	p.Timestamp += samples
 
 	if len(packets) != 0 && p.extensionNumbers.AbsSendTime != 0 {
-		t := toNtpTime(p.timegen()) >> 14
+		sendTime := NewAbsSendTimeExtension(p.timegen())
 		//apply http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
-		b, err := (&AbsSendTimeExtension{
-			Timestamp: t,
-		}).Marshal()
+		b, err := sendTime.Marshal()
 		if err != nil {
 			return nil // never happens
 		}


Test patch:
diff --git a/packet_test.go b/packet_test.go
index a5f502f..add250d 100644
--- a/packet_test.go
+++ b/packet_test.go
@@ -613,6 +613,41 @@ func TestRFC8285OneByteSetExtensionShouldEnableExensionsWhenAdding(t *testing.T)
 	}
 }
 
+func TestRFC8285OneByteSetExtensionShouldSetCorrectExtensionProfileFor16ByteExtension(t *testing.T) {
+	payload := []byte{
+		// Payload
+		0x98, 0x36, 0xbe, 0x88, 0x9e,
+	}
+	p := &Packet{Header: Header{
+		Marker:         true,
+		Extension:      false,
+		Version:        2,
+		PayloadOffset:  26,
+		PayloadType:    96,
+		SequenceNumber: 27023,
+		Timestamp:      3653407706,
+		SSRC:           476325762,
+		CSRC:           []uint32{},
+	},
+		Payload: payload,
+	}
+
+	extension := []byte{
+		0xAA, 0xAA, 0xAA, 0xAA,
+		0xAA, 0xAA, 0xAA, 0xAA,
+		0xAA, 0xAA, 0xAA, 0xAA,
+		0xAA, 0xAA, 0xAA, 0xAA,
+	}
+	err := p.SetExtension(1, extension)
+	if err != nil {
+		t.Error("Error setting extension")
+	}
+
+	if p.ExtensionProfile != 0xBEDE {
+		t.Error("Extension profile should be set to 0xBEDE")
+	}
+}
+
 func TestRFC8285OneByteSetExtensionShouldUpdateExistingExension(t *testing.T) {
 	payload := []byte{
 		// Payload