Push track quality to poor on a bandwidth constrained pause. (#1867)
* Push track quality to poor on a bandwidth constrained pause. * add tests * scale distance by divisor * fix test distance to desired * wait longer for subscription manager to reconcile
This commit is contained in:
@@ -139,6 +139,22 @@ func (cs *ConnectionStats) UpdateLayerMute(isMuted bool) {
|
||||
cs.scorer.UpdateLayerMute(isMuted)
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) UpdatePauseAt(isPaused bool, at time.Time) {
|
||||
if cs.done.IsBroken() {
|
||||
return
|
||||
}
|
||||
|
||||
cs.scorer.UpdatePauseAt(isPaused, at)
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) UpdatePause(isPaused bool) {
|
||||
if cs.done.IsBroken() {
|
||||
return
|
||||
}
|
||||
|
||||
cs.scorer.UpdatePause(isPaused)
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) AddLayerTransitionAt(distance float64, at time.Time) {
|
||||
if cs.done.IsBroken() {
|
||||
return
|
||||
|
||||
@@ -30,7 +30,7 @@ func newConnectionStats(
|
||||
}
|
||||
|
||||
func TestConnectionQuality(t *testing.T) {
|
||||
t.Run("quality scorer state machine", func(t *testing.T) {
|
||||
t.Run("quality scorer operation", func(t *testing.T) {
|
||||
var streams map[uint32]*buffer.StreamStatsWithLayers
|
||||
getDeltaStats := func() map[uint32]*buffer.StreamStatsWithLayers {
|
||||
return streams
|
||||
@@ -314,13 +314,6 @@ func TestConnectionQuality(t *testing.T) {
|
||||
require.Greater(t, float32(4.1), mos)
|
||||
require.Equal(t, livekit.ConnectionQuality_GOOD, quality)
|
||||
|
||||
// a transition to 0 (all layers stopped) should flip quality to EXCELLENT
|
||||
now = now.Add(duration)
|
||||
cs.AddBitrateTransitionAt(0, now)
|
||||
mos, quality = cs.GetScoreAndQuality()
|
||||
require.Greater(t, float32(4.6), mos)
|
||||
require.Equal(t, livekit.ConnectionQuality_EXCELLENT, quality)
|
||||
|
||||
// test layer mute via UpdateLayerMute API
|
||||
cs.AddBitrateTransitionAt(1_000_000, now)
|
||||
cs.AddBitrateTransitionAt(2_000_000, now.Add(2*time.Second))
|
||||
@@ -346,10 +339,36 @@ func TestConnectionQuality(t *testing.T) {
|
||||
require.Greater(t, float32(4.6), mos)
|
||||
require.Equal(t, livekit.ConnectionQuality_EXCELLENT, quality)
|
||||
|
||||
// setting bit rate after layer mute should layer unmute automatically
|
||||
cs.AddBitrateTransitionAt(1_000_000, now)
|
||||
cs.AddBitrateTransitionAt(2_000_000, now.Add(2*time.Second))
|
||||
// unmute layer
|
||||
cs.UpdateLayerMuteAt(false, now.Add(2*time.Second))
|
||||
|
||||
streams = map[uint32]*buffer.StreamStatsWithLayers{
|
||||
1: {
|
||||
RTPStats: &buffer.RTPDeltaInfo{
|
||||
StartTime: now,
|
||||
Duration: duration,
|
||||
Packets: 250,
|
||||
Bytes: 8_000_000 / 8 / 5,
|
||||
},
|
||||
},
|
||||
}
|
||||
cs.updateScoreAt(now.Add(duration))
|
||||
mos, quality = cs.GetScoreAndQuality()
|
||||
require.Greater(t, float32(4.6), mos)
|
||||
require.Equal(t, livekit.ConnectionQuality_EXCELLENT, quality)
|
||||
|
||||
// pause
|
||||
now = now.Add(duration)
|
||||
cs.UpdatePauseAt(true, now)
|
||||
mos, quality = cs.GetScoreAndQuality()
|
||||
require.Greater(t, float32(2.1), mos)
|
||||
require.Equal(t, livekit.ConnectionQuality_POOR, quality)
|
||||
|
||||
// resume
|
||||
cs.UpdatePauseAt(false, now.Add(2*time.Second))
|
||||
|
||||
// although conditions are perfect, climbing back from POOR (because of pause above)
|
||||
// will only climb to GOOD.
|
||||
streams = map[uint32]*buffer.StreamStatsWithLayers{
|
||||
1: {
|
||||
RTPStats: &buffer.RTPDeltaInfo{
|
||||
|
||||
@@ -162,6 +162,9 @@ type qualityScorer struct {
|
||||
layerMutedAt time.Time
|
||||
layerUnmutedAt time.Time
|
||||
|
||||
pausedAt time.Time
|
||||
resumedAt time.Time
|
||||
|
||||
maxPPS float64
|
||||
|
||||
aggregateBitrate *utils.TimedAggregator[int64]
|
||||
@@ -224,17 +227,6 @@ func (q *qualityScorer) UpdateMute(isMuted bool) {
|
||||
|
||||
func (q *qualityScorer) addBitrateTransitionAtLocked(bitrate int64, at time.Time) {
|
||||
q.aggregateBitrate.AddSampleAt(bitrate, at)
|
||||
|
||||
if bitrate == 0 {
|
||||
if !q.isLayerMuted() {
|
||||
q.layerMutedAt = at
|
||||
q.score = maxScore
|
||||
}
|
||||
} else {
|
||||
if q.isLayerMuted() {
|
||||
q.layerUnmutedAt = at
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (q *qualityScorer) AddBitrateTransitionAt(bitrate int64, at time.Time) {
|
||||
@@ -254,8 +246,9 @@ func (q *qualityScorer) AddBitrateTransition(bitrate int64) {
|
||||
func (q *qualityScorer) updateLayerMuteAtLocked(isMuted bool, at time.Time) {
|
||||
if isMuted {
|
||||
if !q.isLayerMuted() {
|
||||
q.aggregateBitrate.AddSampleAt(0, at)
|
||||
q.layerDistance.AddSampleAt(0, at)
|
||||
q.aggregateBitrate.Reset()
|
||||
q.layerDistance.Reset()
|
||||
|
||||
q.layerMutedAt = at
|
||||
q.score = maxScore
|
||||
}
|
||||
@@ -280,6 +273,36 @@ func (q *qualityScorer) UpdateLayerMute(isMuted bool) {
|
||||
q.updateLayerMuteAtLocked(isMuted, time.Now())
|
||||
}
|
||||
|
||||
func (q *qualityScorer) updatePauseAtLocked(isPaused bool, at time.Time) {
|
||||
if isPaused {
|
||||
if !q.isPaused() {
|
||||
q.aggregateBitrate.Reset()
|
||||
q.layerDistance.Reset()
|
||||
|
||||
q.pausedAt = at
|
||||
q.score = poorScore
|
||||
}
|
||||
} else {
|
||||
if q.isPaused() {
|
||||
q.resumedAt = at
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (q *qualityScorer) UpdatePauseAt(isPaused bool, at time.Time) {
|
||||
q.lock.Lock()
|
||||
defer q.lock.Unlock()
|
||||
|
||||
q.updatePauseAtLocked(isPaused, at)
|
||||
}
|
||||
|
||||
func (q *qualityScorer) UpdatePause(isPaused bool) {
|
||||
q.lock.Lock()
|
||||
defer q.lock.Unlock()
|
||||
|
||||
q.updatePauseAtLocked(isPaused, time.Now())
|
||||
}
|
||||
|
||||
func (q *qualityScorer) addLayerTransitionAtLocked(distance float64, at time.Time) {
|
||||
q.layerDistance.AddSampleAt(distance, at)
|
||||
}
|
||||
@@ -311,11 +334,14 @@ func (q *qualityScorer) updateAtLocked(stat *windowStat, at time.Time) {
|
||||
|
||||
// nothing to do when muted or not unmuted for long enough
|
||||
// NOTE: it is possible that unmute -> mute -> unmute transition happens in the
|
||||
// same analysis window. On a transition to mute, state immediately moves
|
||||
// to stable and quality EXCELLENT for responsiveness. On an unmute, the
|
||||
// entire window data is considered (as long as enough time has passed since
|
||||
// unmute) including the data before mute.
|
||||
if q.isMuted() || !q.isUnmutedEnough(at) || q.isLayerMuted() {
|
||||
// same analysis window. On a transition to mute, quality is immediately moved
|
||||
// EXCELLENT for responsiveness. On an unmute, the entire window data is
|
||||
// considered (as long as enough time has passed since unmute).
|
||||
//
|
||||
// Similarly, when paused (possibly due to congestion), score is immediately
|
||||
// set to poorScore for responsiveness. The layer transision is reest.
|
||||
// On a resume, quality climbs back up using normal operation.
|
||||
if q.isMuted() || !q.isUnmutedEnough(at) || q.isLayerMuted() || q.isPaused() {
|
||||
q.lastUpdateAt = at
|
||||
return
|
||||
}
|
||||
@@ -430,6 +456,10 @@ func (q *qualityScorer) isLayerMuted() bool {
|
||||
return !q.layerMutedAt.IsZero() && (q.layerUnmutedAt.IsZero() || q.layerMutedAt.After(q.layerUnmutedAt))
|
||||
}
|
||||
|
||||
func (q *qualityScorer) isPaused() bool {
|
||||
return !q.pausedAt.IsZero() && (q.resumedAt.IsZero() || q.pausedAt.After(q.resumedAt))
|
||||
}
|
||||
|
||||
func (q *qualityScorer) getPacketLossWeight(stat *windowStat) float64 {
|
||||
if stat == nil || stat.duration == 0 {
|
||||
return q.params.PacketLossWeight
|
||||
|
||||
+12
-6
@@ -950,18 +950,24 @@ func (d *DownTrack) UpTrackMaxTemporalLayerSeenChange(maxTemporalLayerSeen int32
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DownTrack) maybeAddTransition(_bitrate int64, distance float64) {
|
||||
func (d *DownTrack) maybeAddTransition(_bitrate int64, distance float64, pauseReason VideoPauseReason) {
|
||||
if d.kind == webrtc.RTPCodecTypeAudio {
|
||||
return
|
||||
}
|
||||
|
||||
d.connectionStats.AddLayerTransition(distance)
|
||||
if pauseReason == VideoPauseReasonBandwidth {
|
||||
d.connectionStats.UpdatePause(true)
|
||||
} else {
|
||||
d.connectionStats.UpdatePause(false)
|
||||
d.connectionStats.AddLayerTransition(distance)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DownTrack) UpTrackBitrateReport(availableLayers []int32, bitrates Bitrates) {
|
||||
d.maybeAddTransition(
|
||||
d.forwarder.GetOptimalBandwidthNeeded(bitrates),
|
||||
d.forwarder.DistanceToDesired(availableLayers, bitrates),
|
||||
d.forwarder.PauseReason(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1011,7 +1017,7 @@ func (d *DownTrack) AllocateOptimal(allowOvershoot bool) VideoAllocation {
|
||||
al, brs := d.receiver.GetLayeredBitrate()
|
||||
allocation := d.forwarder.AllocateOptimal(al, brs, allowOvershoot)
|
||||
d.maybeStartKeyFrameRequester()
|
||||
d.maybeAddTransition(allocation.BandwidthNeeded, allocation.DistanceToDesired)
|
||||
d.maybeAddTransition(allocation.BandwidthNeeded, allocation.DistanceToDesired, allocation.PauseReason)
|
||||
return allocation
|
||||
}
|
||||
|
||||
@@ -1039,7 +1045,7 @@ func (d *DownTrack) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti
|
||||
func (d *DownTrack) ProvisionalAllocateCommit() VideoAllocation {
|
||||
allocation := d.forwarder.ProvisionalAllocateCommit()
|
||||
d.maybeStartKeyFrameRequester()
|
||||
d.maybeAddTransition(allocation.BandwidthNeeded, allocation.DistanceToDesired)
|
||||
d.maybeAddTransition(allocation.BandwidthNeeded, allocation.DistanceToDesired, allocation.PauseReason)
|
||||
return allocation
|
||||
}
|
||||
|
||||
@@ -1047,7 +1053,7 @@ func (d *DownTrack) AllocateNextHigher(availableChannelCapacity int64, allowOver
|
||||
al, brs := d.receiver.GetLayeredBitrate()
|
||||
allocation, available := d.forwarder.AllocateNextHigher(availableChannelCapacity, al, brs, allowOvershoot)
|
||||
d.maybeStartKeyFrameRequester()
|
||||
d.maybeAddTransition(allocation.BandwidthNeeded, allocation.DistanceToDesired)
|
||||
d.maybeAddTransition(allocation.BandwidthNeeded, allocation.DistanceToDesired, allocation.PauseReason)
|
||||
return allocation, available
|
||||
}
|
||||
|
||||
@@ -1062,7 +1068,7 @@ func (d *DownTrack) Pause() VideoAllocation {
|
||||
al, brs := d.receiver.GetLayeredBitrate()
|
||||
allocation := d.forwarder.Pause(al, brs)
|
||||
d.maybeStartKeyFrameRequester()
|
||||
d.maybeAddTransition(allocation.BandwidthNeeded, allocation.DistanceToDesired)
|
||||
d.maybeAddTransition(allocation.BandwidthNeeded, allocation.DistanceToDesired, allocation.PauseReason)
|
||||
return allocation
|
||||
}
|
||||
|
||||
|
||||
@@ -526,6 +526,13 @@ func (f *Forwarder) IsDeficient() bool {
|
||||
return f.isDeficientLocked()
|
||||
}
|
||||
|
||||
func (f *Forwarder) PauseReason() VideoPauseReason {
|
||||
f.lock.RLock()
|
||||
defer f.lock.RUnlock()
|
||||
|
||||
return f.lastAllocation.PauseReason
|
||||
}
|
||||
|
||||
func (f *Forwarder) BandwidthRequested(brs Bitrates) int64 {
|
||||
f.lock.RLock()
|
||||
defer f.lock.RUnlock()
|
||||
@@ -1865,7 +1872,7 @@ done:
|
||||
((adjustedMaxLayer.Spatial - adjustedTargetLayer.Spatial) * (maxSeenLayer.Temporal + 1)) +
|
||||
(adjustedMaxLayer.Temporal - adjustedTargetLayer.Temporal)
|
||||
if !targetLayer.IsValid() {
|
||||
distance++
|
||||
distance += (maxSeenLayer.Temporal + 1)
|
||||
}
|
||||
|
||||
return float64(distance) / float64(maxSeenLayer.Temporal+1)
|
||||
|
||||
@@ -562,7 +562,7 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
|
||||
TargetLayer: expectedTargetLayer,
|
||||
RequestLayerSpatial: expectedTargetLayer.Spatial,
|
||||
MaxLayer: expectedMaxLayer,
|
||||
DistanceToDesired: 0.25,
|
||||
DistanceToDesired: 1.0,
|
||||
}
|
||||
result = f.ProvisionalAllocateCommit()
|
||||
require.Equal(t, expectedResult, result)
|
||||
@@ -595,7 +595,7 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
|
||||
TargetLayer: buffer.InvalidLayer,
|
||||
RequestLayerSpatial: buffer.InvalidLayerSpatial,
|
||||
MaxLayer: expectedMaxLayer,
|
||||
DistanceToDesired: 0.25,
|
||||
DistanceToDesired: 1.0,
|
||||
}
|
||||
result = f.ProvisionalAllocateCommit()
|
||||
require.Equal(t, expectedResult, result)
|
||||
@@ -1114,7 +1114,7 @@ func TestForwarderPause(t *testing.T) {
|
||||
TargetLayer: buffer.InvalidLayer,
|
||||
RequestLayerSpatial: buffer.InvalidLayerSpatial,
|
||||
MaxLayer: buffer.DefaultMaxLayer,
|
||||
DistanceToDesired: 3,
|
||||
DistanceToDesired: 3.75,
|
||||
}
|
||||
result := f.Pause(nil, bitrates)
|
||||
require.Equal(t, expectedResult, result)
|
||||
|
||||
@@ -620,7 +620,7 @@ func TestSubscribeToCodecUnsupported(t *testing.T) {
|
||||
require.Equal(t, h264TrackID, sr.TrackSid)
|
||||
require.Equal(t, livekit.SubscriptionError_SE_CODEC_UNSUPPORTED, sr.Err)
|
||||
return true
|
||||
}, time.Second, 10*time.Millisecond, "did not receive subscription response")
|
||||
}, 5*time.Second, 10*time.Millisecond, "did not receive subscription response")
|
||||
|
||||
// publish another vp8 track again, ensure the transport recovered by sfu and c2 can receive it
|
||||
t4, err := c1.AddStaticTrack("video/vp8", "video2", "webcam2")
|
||||
|
||||
Reference in New Issue
Block a user