Repository: pion/webrtc
Pull request #2192 (2022-04-23 20:36:43)

Max receive buffer size via SettingEngine
Relates to pion/sctp#218

#### Description
Current pion/sctp's receive buffer size is hardcoded as 1 MiB. This change makes the value configurable from the SettingEngine.

#### Reference issue
See pion/sctp#218 for more details about the context of this pull-request.

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

```diff
@@            Coverage Diff             @@
##           master    #2192      +/-   ##
==========================================
- Coverage   76.88%   69.94%   -6.95%     
==========================================
  Files          87       64      -23     
  Lines        8990     3307    -5683     
==========================================
- Hits         6912     2313    -4599     
+ Misses       1654      867     -787     
+ Partials      424      127     -297     
```

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

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/2192?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/2192/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/2192/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%> (-81.82%)` | :arrow_down: |
| [rtpcodec.go](https://codecov.io/gh/pion/webrtc/pull/2192/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-cnRwY29kZWMuZ28=) | `13.33% <0.00%> (-80.00%)` | :arrow_down: |
| [stats.go](https://codecov.io/gh/pion/webrtc/pull/2192/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%> (-75.00%)` | :arrow_down: |
| [track\_local.go](https://codecov.io/gh/pion/webrtc/pull/2192/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%> (-66.67%)` | :arrow_down: |
| [icemux.go](https://codecov.io/gh/pion/webrtc/pull/2192/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%> (-54.55%)` | :arrow_down: |
| [internal/mux/endpoint.go](https://codecov.io/gh/pion/webrtc/pull/2192/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-aW50ZXJuYWwvbXV4L2VuZHBvaW50Lmdv) | `13.79% <0.00%> (-44.83%)` | :arrow_down: |
| [atomicbool.go](https://codecov.io/gh/pion/webrtc/pull/2192/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-YXRvbWljYm9vbC5nbw==) | `57.14% <0.00%> (-42.86%)` | :arrow_down: |
| [sdpsemantics.go](https://codecov.io/gh/pion/webrtc/pull/2192/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%> (-34.49%)` | :arrow_down: |
| [dtlsrole.go](https://codecov.io/gh/pion/webrtc/pull/2192/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-ZHRsc3JvbGUuZ28=) | `71.42% <0.00%> (-28.58%)` | :arrow_down: |
| ... and [37 more](https://codecov.io/gh/pion/webrtc/pull/2192/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/2192?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/2192?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion). Last update [2cc6ea0...2f898a0](https://codecov.io/gh/pion/webrtc/pull/2192?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).


Patch:
diff --git a/sctptransport.go b/sctptransport.go
index 27e02ea6..a379a96b 100644
--- a/sctptransport.go
+++ b/sctptransport.go
@@ -103,8 +103,9 @@ func (r *SCTPTransport) Start(remoteCaps SCTPCapabilities) error {
 	}
 
 	sctpAssociation, err := sctp.Client(sctp.Config{
-		NetConn:       dtlsTransport.conn,
-		LoggerFactory: r.api.settingEngine.LoggerFactory,
+		NetConn:              dtlsTransport.conn,
+		MaxReceiveBufferSize: r.api.settingEngine.sctp.maxReceiveBufferSize,
+		LoggerFactory:        r.api.settingEngine.LoggerFactory,
 	})
 	if err != nil {
 		return err
diff --git a/settingengine.go b/settingengine.go
index a2f5ef41..7cd0bb30 100644
--- a/settingengine.go
+++ b/settingengine.go
@@ -54,6 +54,9 @@ type SettingEngine struct {
 	dtls struct {
 		retransmissionInterval time.Duration
 	}
+	sctp struct {
+		maxReceiveBufferSize uint32
+	}
 	sdpMediaLevelFingerprints                 bool
 	answeringDTLSRole                         DTLSRole
 	disableCertificateFingerprintVerification bool
@@ -304,3 +307,9 @@ func (e *SettingEngine) SetReceiveMTU(receiveMTU uint) {
 func (e *SettingEngine) SetDTLSRetransmissionInterval(interval time.Duration) {
 	e.dtls.retransmissionInterval = interval
 }
+
+// SetSCTPMaxReceiveBufferSize sets the maximum receive buffer size.
+// Leave this 0 for the default maxReceiveBufferSize.
+func (e *SettingEngine) SetSCTPMaxReceiveBufferSize(maxReceiveBufferSize uint32) {
+	e.sctp.maxReceiveBufferSize = maxReceiveBufferSize
+}


Test patch:
diff --git a/settingengine_test.go b/settingengine_test.go
index 80817ca3..dd661d5e 100644
--- a/settingengine_test.go
+++ b/settingengine_test.go
@@ -233,3 +233,12 @@ func TestSetDTLSRetransmissionInterval(t *testing.T) {
 		t.Errorf("Failed to set DTLS retransmission interval")
 	}
 }
+
+func TestSetSCTPMaxReceiverBufferSize(t *testing.T) {
+	s := SettingEngine{}
+	assert.Equal(t, uint32(0), s.sctp.maxReceiveBufferSize)
+
+	expSize := uint32(4 * 1024 * 1024)
+	s.SetSCTPMaxReceiveBufferSize(expSize)
+	assert.Equal(t, expSize, s.sctp.maxReceiveBufferSize)
+}