Repository: pion/webrtc
Pull request #1831 (2021-06-13 12:06:04)

Implemented RTPReceiver SetRTPParameters for ORTC
See discussion with @OrlandoCo https://gophers.slack.com/archives/CAK2124AG/p1623504625397700

- RID is now set on remote tracks, if provided.
- Adds SetRTPParameters which can be used in ORTC to set codec information about the remote track.

I'm not sure if this is the best way to implement this, as the track will still miss the `id` and `streamID` + I'm not a big fan of adding public method for edge cases. 
Maybe it'd be better to  refacto `RTPReceiver.Receive()` to look more like `RTPSender.Send()`?

codecov[bot]: # [Codecov](https://codecov.io/gh/pion/webrtc/pull/1831?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) Report
> Merging [#1831](https://codecov.io/gh/pion/webrtc/pull/1831?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) (4d583fc) into [master](https://codecov.io/gh/pion/webrtc/commit/f7f760f8c6e2f1aba1bf7c544fdd84d70f6e05b6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) (f7f760f) will **decrease** coverage by `4.33%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pion/webrtc/pull/1831/graphs/tree.svg?width=650&height=150&src=pr&token=XOOuQo07I2&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion)](https://codecov.io/gh/pion/webrtc/pull/1831?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion)

```diff
@@            Coverage Diff             @@
##           master    #1831      +/-   ##
==========================================
- Coverage   75.39%   71.05%   -4.34%     
==========================================
  Files          83       61      -22     
  Lines        5979     2277    -3702     
==========================================
- Hits         4508     1618    -2890     
+ Misses       1083      551     -532     
+ Partials      388      108     -280     
```

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

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/pion/webrtc/pull/1831?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | Coverage Δ | |
|---|---|---|
| [constants.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-Y29uc3RhbnRzLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [icecandidatepair.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-aWNlY2FuZGlkYXRlcGFpci5nbw==) | `0.00% <0.00%> (-85.72%)` | :arrow_down: |
| [track\_local.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-dHJhY2tfbG9jYWwuZ28=) | `0.00% <0.00%> (-80.00%)` | :arrow_down: |
| [rtpcodec.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-cnRwY29kZWMuZ28=) | `15.00% <0.00%> (-75.00%)` | :arrow_down: |
| [stats.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-c3RhdHMuZ28=) | `0.00% <0.00%> (-67.86%)` | :arrow_down: |
| [internal/mux/muxfunc.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-aW50ZXJuYWwvbXV4L211eGZ1bmMuZ28=) | `8.33% <0.00%> (-58.34%)` | :arrow_down: |
| [icemux.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-aWNlbXV4Lmdv) | `0.00% <0.00%> (-55.56%)` | :arrow_down: |
| [gathering\_complete\_promise.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-Z2F0aGVyaW5nX2NvbXBsZXRlX3Byb21pc2UuZ28=) | `60.00% <0.00%> (-40.00%)` | :arrow_down: |
| [internal/mux/endpoint.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-aW50ZXJuYWwvbXV4L2VuZHBvaW50Lmdv) | `22.22% <0.00%> (-38.89%)` | :arrow_down: |
| [sdpsemantics.go](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-c2Rwc2VtYW50aWNzLmdv) | `0.00% <0.00%> (-37.50%)` | :arrow_down: |
| ... and [31 more](https://codecov.io/gh/pion/webrtc/pull/1831/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pion/webrtc/pull/1831?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pion/webrtc/pull/1831?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion). Last update [f7f760f...4d583fc](https://codecov.io/gh/pion/webrtc/pull/1831?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).

OrlandoCo: > I'm not sure if this is the best way to implement this, as the track will still miss the `id` and `streamID` + I'm not a big fan of adding public method for edge cases.

Looks good to me, but since this is not a standard W3C method then I guess we can add the track and stream ID, we can extend `RTPParameters` to include them. Any thoughts @Sean-Der ??
OrlandoCo: @Sean-Der Any comments before merge this?

Sean-Der: Sorry for the delay @Antonito And @OrlandoCo 

Code looks good! Could you add a test before merging? Thanks

Patch:
diff --git a/rtpreceiver.go b/rtpreceiver.go
index 6c44ed6c..446e8ae9 100644
--- a/rtpreceiver.go
+++ b/rtpreceiver.go
@@ -114,7 +114,7 @@ func (r *RTPReceiver) Receive(parameters RTPReceiveParameters) error {
 			track: newTrackRemote(
 				r.kind,
 				parameters.Encodings[0].SSRC,
-				"",
+				parameters.Encodings[0].RID,
 				r,
 			),
 		}
diff --git a/rtpreceiver_go.go b/rtpreceiver_go.go
new file mode 100644
index 00000000..7f347e4b
--- /dev/null
+++ b/rtpreceiver_go.go
@@ -0,0 +1,32 @@
+// +build !js
+
+package webrtc
+
+import "github.com/pion/interceptor"
+
+// SetRTPParameters applies provided RTPParameters the RTPReceiver's tracks.
+//
+// This method is part of the ORTC API. It is not
+// meant to be used together with the basic WebRTC API.
+//
+// The amount of provided codecs must match the number of tracks on the receiver.
+func (r *RTPReceiver) SetRTPParameters(params RTPParameters) {
+	headerExtensions := make([]interceptor.RTPHeaderExtension, 0, len(params.HeaderExtensions))
+	for _, h := range params.HeaderExtensions {
+		headerExtensions = append(headerExtensions, interceptor.RTPHeaderExtension{ID: h.ID, URI: h.URI})
+	}
+
+	r.mu.Lock()
+	defer r.mu.Unlock()
+
+	for ndx, codec := range params.Codecs {
+		currentTrack := r.tracks[ndx].track
+
+		r.tracks[ndx].streamInfo.RTPHeaderExtensions = headerExtensions
+
+		currentTrack.mu.Lock()
+		currentTrack.codec = codec
+		currentTrack.params = params
+		currentTrack.mu.Unlock()
+	}
+}


Test patch:
diff --git a/rtpreceiver_go_test.go b/rtpreceiver_go_test.go
new file mode 100644
index 00000000..106cca2f
--- /dev/null
+++ b/rtpreceiver_go_test.go
@@ -0,0 +1,67 @@
+// +build !js
+
+package webrtc
+
+import (
+	"context"
+	"testing"
+	"time"
+
+	"github.com/pion/webrtc/v3/pkg/media"
+	"github.com/stretchr/testify/assert"
+)
+
+func TestSetRTPParameters(t *testing.T) {
+	sender, receiver, wan := createVNetPair(t)
+
+	outgoingTrack, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion")
+	assert.NoError(t, err)
+
+	_, err = sender.AddTrack(outgoingTrack)
+	assert.NoError(t, err)
+
+	// Those parameters wouldn't make sense in a real application,
+	// but for the sake of the test we just need different values.
+	p := RTPParameters{
+		Codecs: []RTPCodecParameters{
+			{
+				RTPCodecCapability: RTPCodecCapability{MimeTypeOpus, 48000, 2, "minptime=10;useinbandfec=1", []RTCPFeedback{{"nack", ""}}},
+				PayloadType:        111,
+			},
+		},
+		HeaderExtensions: []RTPHeaderExtensionParameter{
+			{URI: "urn:ietf:params:rtp-hdrext:sdes:mid"},
+			{URI: "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"},
+			{URI: "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"},
+		},
+	}
+
+	seenPacket, seenPacketCancel := context.WithCancel(context.Background())
+	receiver.OnTrack(func(trackRemote *TrackRemote, r *RTPReceiver) {
+		r.SetRTPParameters(p)
+
+		incomingTrackCodecs := r.Track().Codec()
+
+		assert.EqualValues(t, p.HeaderExtensions, r.Track().params.HeaderExtensions)
+
+		assert.EqualValues(t, p.Codecs[0].MimeType, incomingTrackCodecs.MimeType)
+		assert.EqualValues(t, p.Codecs[0].ClockRate, incomingTrackCodecs.ClockRate)
+		assert.EqualValues(t, p.Codecs[0].Channels, incomingTrackCodecs.Channels)
+		assert.EqualValues(t, p.Codecs[0].SDPFmtpLine, incomingTrackCodecs.SDPFmtpLine)
+		assert.EqualValues(t, p.Codecs[0].RTCPFeedback, incomingTrackCodecs.RTCPFeedback)
+		assert.EqualValues(t, p.Codecs[0].PayloadType, incomingTrackCodecs.PayloadType)
+
+		seenPacketCancel()
+	})
+
+	peerConnectionsConnected := untilConnectionState(PeerConnectionStateConnected, sender, receiver)
+
+	assert.NoError(t, signalPair(sender, receiver))
+
+	peerConnectionsConnected.Wait()
+	assert.NoError(t, outgoingTrack.WriteSample(media.Sample{Data: []byte{0xAA}, Duration: time.Second}))
+
+	<-seenPacket.Done()
+	assert.NoError(t, wan.Stop())
+	closePairNow(t, sender, receiver)
+}