Repository: pion/interceptor
Pull request #201 (2023-08-23 18:53:13)

Add PacketsHeld to TWCC recorder
#### Description

When using `twcc.Recorder` outside the context of `twcc.SenderInterceptor`, it is useful to have access to the number of packets currently held. This enables writing logic other than an interval to determine when to build feedback packets.

#### Reference issue
N/A


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pion/interceptor/pull/201?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) Report
Patch coverage: **`100.00%`** and project coverage change: **`+0.01%`** :tada:
> Comparison is base [(`0efb978`)](https://app.codecov.io/gh/pion/interceptor/commit/0efb9784894d3c1441afb3f1e4346d26f4c57de9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) 79.41% compared to head [(`2ea68e7`)](https://app.codecov.io/gh/pion/interceptor/pull/201?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) 79.43%.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #201      +/-   ##
==========================================
+ Coverage   79.41%   79.43%   +0.01%     
==========================================
  Files          67       67              
  Lines        3367     3369       +2     
==========================================
+ Hits         2674     2676       +2     
  Misses        574      574              
  Partials      119      119              
```

| [Flag](https://app.codecov.io/gh/pion/interceptor/pull/201/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | Coverage Δ | |
|---|---|---|
| [go](https://app.codecov.io/gh/pion/interceptor/pull/201/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | `79.43% <100.00%> (+0.01%)` | :arrow_up: |
| [wasm](https://app.codecov.io/gh/pion/interceptor/pull/201/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | `?` | |

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.

| [Files Changed](https://app.codecov.io/gh/pion/interceptor/pull/201?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | Coverage Δ | |
|---|---|---|
| [pkg/twcc/twcc.go](https://app.codecov.io/gh/pion/interceptor/pull/201?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-cGtnL3R3Y2MvdHdjYy5nbw==) | `95.00% <100.00%> (+0.05%)` | :arrow_up: |

... and [2 files with indirect coverage changes](https://app.codecov.io/gh/pion/interceptor/pull/201/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion)


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pion/interceptor/pull/201?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).


Patch:
diff --git a/AUTHORS.txt b/AUTHORS.txt
index 44a421e..7ce429f 100644
--- a/AUTHORS.txt
+++ b/AUTHORS.txt
@@ -3,6 +3,8 @@
 #
 # This file is auto generated, using git to list all individuals contributors.
 # see https://github.com/pion/.goassets/blob/master/scripts/generate-authors.sh for the scripting
+ Sean DuBois <sean@siobud.com>
+ ziminghua <565209960@qq.com>
 Aaron Boushley <boushley@gmail.com>
 Adam Kiss <masterada@gmail.com>
 adamroach <adam@nostrum.com>
@@ -23,8 +25,8 @@ Quentin Renard <contact@asticode.com>
 Rayleigh Li <rayleigh.li@zoom.us>
 Sean DuBois <sean@siobud.com>
 Steffen Vogel <post@steffenvogel.de>
+treyhakanson <treyhakanson@gmail.com>
 XLPolar <guangjin_pan@163.com>
-ziminghua <565209960@qq.com>
 
 # List of contributors not appearing in Git history
 
diff --git a/pkg/twcc/twcc.go b/pkg/twcc/twcc.go
index 235f1f1..5dd8fd3 100644
--- a/pkg/twcc/twcc.go
+++ b/pkg/twcc/twcc.go
@@ -51,6 +51,11 @@ func (r *Recorder) Record(mediaSSRC uint32, sequenceNumber uint16, arrivalTime i
 	r.lastSequenceNumber = sequenceNumber
 }
 
+// PacketsHeld returns the number of received packets currently held by the recorder
+func (r *Recorder) PacketsHeld() int {
+	return len(r.receivedPackets)
+}
+
 func insertSorted(list []pktInfo, element pktInfo) []pktInfo {
 	if len(list) == 0 {
 		return append(list, element)


Test patch:
diff --git a/pkg/twcc/twcc_test.go b/pkg/twcc/twcc_test.go
index 871d990..ac8df59 100644
--- a/pkg/twcc/twcc_test.go
+++ b/pkg/twcc/twcc_test.go
@@ -940,3 +940,25 @@ func TestInsertSorted(t *testing.T) {
 		})
 	}
 }
+
+func TestPacketsHheld(t *testing.T) {
+	r := NewRecorder(5000)
+	assert.Zero(t, r.PacketsHeld())
+
+	arrivalTime := int64(scaleFactorReferenceTime)
+	addRun(t, r, []uint16{0, 1, 2}, []int64{
+		arrivalTime,
+		increaseTime(&arrivalTime, rtcp.TypeTCCDeltaScaleFactor),
+		increaseTime(&arrivalTime, rtcp.TypeTCCDeltaScaleFactor),
+	})
+	assert.Equal(t, r.PacketsHeld(), 3)
+
+	addRun(t, r, []uint16{3, 4}, []int64{
+		increaseTime(&arrivalTime, rtcp.TypeTCCDeltaScaleFactor),
+		increaseTime(&arrivalTime, rtcp.TypeTCCDeltaScaleFactor),
+	})
+	assert.Equal(t, r.PacketsHeld(), 5)
+
+	r.BuildFeedbackPacket()
+	assert.Zero(t, r.PacketsHeld())
+}