Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support publishing average loss rate #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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