Skip to content

Commit 7c5d0ee

Browse files
committed
feat: support publishing average loss rate
This change exposes an API for reading the loss rate.
1 parent 0748586 commit 7c5d0ee

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

AUTHORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ boks1971 <[email protected]>
1414
David Zhao <[email protected]>
1515
Jonathan Müller <[email protected]>
1616
Kevin Caffrey <[email protected]>
17+
Kevin Wang <[email protected]>
1718
Mathis Engelbart <[email protected]>
1819
Sean DuBois <[email protected]>

pkg/cc/interceptor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ type BandwidthEstimator interface {
2424
AddStream(*interceptor.StreamInfo, interceptor.RTPWriter) interceptor.RTPWriter
2525
WriteRTCP([]rtcp.Packet, interceptor.Attributes) error
2626
GetTargetBitrate() int
27+
GetLossRate() float64
2728
OnTargetBitrateChange(f func(bitrate int))
29+
OnLossRateChange(f func(loss float64))
2830
GetStats() map[string]interface{}
2931
Close() error
3032
}

pkg/gcc/send_side_bwe.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type SendSideBWE struct {
4343
feedbackAdapter *cc.FeedbackAdapter
4444

4545
onTargetBitrateChange func(bitrate int)
46+
onLossRateChange func(loss float64)
4647

4748
lock sync.Mutex
4849
latestStats Stats
@@ -164,6 +165,14 @@ func (e *SendSideBWE) GetTargetBitrate() int {
164165
return e.latestBitrate
165166
}
166167

168+
// GetLossRate returns the current packet loss rate in the range [0, 1].
169+
func (e *SendSideBWE) GetLossRate() float64 {
170+
e.lossController.lock.Lock()
171+
defer e.lossController.lock.Unlock()
172+
173+
return e.lossController.averageLoss
174+
}
175+
167176
// GetStats returns some internal statistics of the bandwidth estimator
168177
func (e *SendSideBWE) GetStats() map[string]interface{} {
169178
e.lock.Lock()
@@ -188,6 +197,12 @@ func (e *SendSideBWE) OnTargetBitrateChange(f func(bitrate int)) {
188197
e.onTargetBitrateChange = f
189198
}
190199

200+
// OnLossRateChange sets the callback that is called when the packet loss
201+
// rate changes
202+
func (e *SendSideBWE) OnLossRateChange(f func(loss float64)) {
203+
e.onLossRateChange = f
204+
}
205+
191206
// isClosed returns true if SendSideBWE is closed
192207
func (e *SendSideBWE) isClosed() bool {
193208
select {
@@ -227,6 +242,11 @@ func (e *SendSideBWE) onDelayUpdate(delayStats DelayStats) {
227242
go e.onTargetBitrateChange(bitrate)
228243
}
229244

245+
// in principle this could update more frequently but this is a convenient place to put this hook.
246+
if e.latestStats.LossStats.AverageLoss != lossStats.AverageLoss && e.onLossRateChange != nil {
247+
go e.onLossRateChange(lossStats.AverageLoss)
248+
}
249+
230250
e.latestStats = Stats{
231251
LossStats: lossStats,
232252
DelayStats: delayStats,

pkg/gcc/send_side_bwe_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func TestSendSideBWE(t *testing.T) {
7070
twccSender.BindRTCPWriter(m.twccResponder)
7171

7272
require.Equal(t, latestBitrate, bwe.GetTargetBitrate())
73+
require.Equal(t, 0.0, bwe.GetLossRate())
7374
require.NotEqual(t, 0, len(bwe.GetStats()))
7475

7576
rtpWriter := bwe.AddStream(streamInfo, m)
@@ -89,6 +90,7 @@ func TestSendSideBWE(t *testing.T) {
8990

9091
// Sending a stream with zero loss and no RTT should increase estimate
9192
require.Less(t, latestBitrate, bwe.GetTargetBitrate())
93+
require.Equal(t, 0.0, bwe.GetLossRate())
9294
}
9395

9496
func TestSendSideBWE_ErrorOnWriteRTCPAtClosedState(t *testing.T) {

0 commit comments

Comments
 (0)