Skip to content

Commit

Permalink
feat: support publishing average loss rate
Browse files Browse the repository at this point in the history
This change exposes an API for reading the loss rate.
  • Loading branch information
kevmo314 committed May 24, 2022
1 parent 0748586 commit 7c5d0ee
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ boks1971 <[email protected]>
David Zhao <[email protected]>
Jonathan Müller <[email protected]>
Kevin Caffrey <[email protected]>
Kevin Wang <[email protected]>
Mathis Engelbart <[email protected]>
Sean DuBois <[email protected]>
2 changes: 2 additions & 0 deletions pkg/cc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ type BandwidthEstimator interface {
AddStream(*interceptor.StreamInfo, interceptor.RTPWriter) interceptor.RTPWriter
WriteRTCP([]rtcp.Packet, interceptor.Attributes) error
GetTargetBitrate() int
GetLossRate() float64
OnTargetBitrateChange(f func(bitrate int))
OnLossRateChange(f func(loss float64))
GetStats() map[string]interface{}
Close() error
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/gcc/send_side_bwe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type SendSideBWE struct {
feedbackAdapter *cc.FeedbackAdapter

onTargetBitrateChange func(bitrate int)
onLossRateChange func(loss float64)

lock sync.Mutex
latestStats Stats
Expand Down Expand Up @@ -164,6 +165,14 @@ func (e *SendSideBWE) GetTargetBitrate() int {
return e.latestBitrate
}

// GetLossRate returns the current packet loss rate in the range [0, 1].
func (e *SendSideBWE) GetLossRate() float64 {
e.lossController.lock.Lock()
defer e.lossController.lock.Unlock()

return e.lossController.averageLoss
}

// GetStats returns some internal statistics of the bandwidth estimator
func (e *SendSideBWE) GetStats() map[string]interface{} {
e.lock.Lock()
Expand All @@ -188,6 +197,12 @@ func (e *SendSideBWE) OnTargetBitrateChange(f func(bitrate int)) {
e.onTargetBitrateChange = f
}

// OnLossRateChange sets the callback that is called when the packet loss
// rate changes
func (e *SendSideBWE) OnLossRateChange(f func(loss float64)) {
e.onLossRateChange = f
}

// isClosed returns true if SendSideBWE is closed
func (e *SendSideBWE) isClosed() bool {
select {
Expand Down Expand Up @@ -227,6 +242,11 @@ func (e *SendSideBWE) onDelayUpdate(delayStats DelayStats) {
go e.onTargetBitrateChange(bitrate)
}

// in principle this could update more frequently but this is a convenient place to put this hook.
if e.latestStats.LossStats.AverageLoss != lossStats.AverageLoss && e.onLossRateChange != nil {
go e.onLossRateChange(lossStats.AverageLoss)
}

e.latestStats = Stats{
LossStats: lossStats,
DelayStats: delayStats,
Expand Down
2 changes: 2 additions & 0 deletions pkg/gcc/send_side_bwe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestSendSideBWE(t *testing.T) {
twccSender.BindRTCPWriter(m.twccResponder)

require.Equal(t, latestBitrate, bwe.GetTargetBitrate())
require.Equal(t, 0.0, bwe.GetLossRate())
require.NotEqual(t, 0, len(bwe.GetStats()))

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

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

func TestSendSideBWE_ErrorOnWriteRTCPAtClosedState(t *testing.T) {
Expand Down

0 comments on commit 7c5d0ee

Please sign in to comment.