From 6f326be4f781ac10e2a10eaea6b30f0b7d3f7821 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Mon, 20 Feb 2023 16:41:58 +0530 Subject: [PATCH] Do not overshoot when layer is locked. (#1449) * Do not overshoot when layer is locked. One more challenging case. When current layer is already locked, should not set up for overshoot. * set target to current --- pkg/sfu/forwarder.go | 50 ++++++++++++++++++++++++----- pkg/sfu/forwarder_test.go | 66 ++++++++++++++++++--------------------- 2 files changed, 74 insertions(+), 42 deletions(-) diff --git a/pkg/sfu/forwarder.go b/pkg/sfu/forwarder.go index 77103663..c155a20b 100644 --- a/pkg/sfu/forwarder.go +++ b/pkg/sfu/forwarder.go @@ -585,9 +585,15 @@ func (f *Forwarder) AllocateOptimal(availableLayers []int32, brs Bitrates, allow alloc.requestLayerSpatial = alloc.targetLayers.Spatial } else { - // opportunistically latch on to anything - opportunisticAlloc() - alloc.requestLayerSpatial = int32(math.Min(float64(f.maxLayers.Spatial), float64(f.maxPublishedLayer))) + if f.currentLayers.IsValid() && f.currentLayers.Spatial == f.requestLayerSpatial { + // current is locked to desired, stay there + alloc.targetLayers = f.currentLayers + alloc.requestLayerSpatial = f.requestLayerSpatial + } else { + // opportunistically latch on to anything + opportunisticAlloc() + alloc.requestLayerSpatial = int32(math.Min(float64(f.maxLayers.Spatial), float64(f.maxPublishedLayer))) + } } } @@ -1198,7 +1204,8 @@ func (f *Forwarder) Pause(brs Bitrates) VideoAllocation { func (f *Forwarder) updateAllocation(alloc VideoAllocation, reason string) VideoAllocation { if alloc.isDeficient != f.lastAllocation.isDeficient || alloc.pauseReason != f.lastAllocation.pauseReason || - alloc.targetLayers != f.lastAllocation.targetLayers { + alloc.targetLayers != f.lastAllocation.targetLayers || + alloc.requestLayerSpatial != f.lastAllocation.requestLayerSpatial { f.logger.Infow(fmt.Sprintf("stream allocation: %s", reason), "allocation", alloc) } f.lastAllocation = alloc @@ -1437,12 +1444,30 @@ func (f *Forwarder) getTranslationParamsVideo(extPkt *buffer.ExtPacket, layer in } else { if extPkt.KeyFrame { if layer > f.currentLayers.Spatial && layer <= f.targetLayers.Spatial { - f.logger.Infow("upgrading layer", "current", f.currentLayers, "target", f.targetLayers, "layer", layer, "feed", extPkt.Packet.SSRC) + f.logger.Infow( + "upgrading layer", + "current", f.currentLayers, + "target", f.targetLayers, + "max", f.maxLayers, + "layer", layer, + "req", f.requestLayerSpatial, + "maxPublished", f.maxPublishedLayer, + "feed", extPkt.Packet.SSRC, + ) found = true } if layer < f.currentLayers.Spatial && layer >= f.targetLayers.Spatial { - f.logger.Infow("downgrading layer", "current", f.currentLayers, "target", f.targetLayers, "layer", layer, "feed", extPkt.Packet.SSRC) + f.logger.Infow( + "downgrading layer", + "current", f.currentLayers, + "target", f.targetLayers, + "max", f.maxLayers, + "layer", layer, + "req", f.requestLayerSpatial, + "maxPublished", f.maxPublishedLayer, + "feed", extPkt.Packet.SSRC, + ) found = true } @@ -1467,6 +1492,8 @@ func (f *Forwarder) getTranslationParamsVideo(extPkt *buffer.ExtPacket, layer in "current", f.currentLayers, "target", f.targetLayers, "max", f.maxLayers, + "layer", layer, + "req", f.requestLayerSpatial, "maxPublished", f.maxPublishedLayer, "feed", extPkt.Packet.SSRC, ) @@ -1477,7 +1504,16 @@ func (f *Forwarder) getTranslationParamsVideo(extPkt *buffer.ExtPacket, layer in // if locked to higher than max layer due to overshoot, check if it can be dialed back if f.targetLayers.Spatial > f.maxLayers.Spatial { if layer <= f.maxLayers.Spatial && extPkt.KeyFrame { - f.logger.Infow("adjusting overshoot", "current", f.currentLayers, "target", f.targetLayers, "layer", layer, "feed", extPkt.Packet.SSRC) + f.logger.Infow( + "adjusting overshoot", + "current", f.currentLayers, + "target", f.targetLayers, + "max", f.maxLayers, + "layer", layer, + "req", f.requestLayerSpatial, + "maxPublished", f.maxPublishedLayer, + "feed", extPkt.Packet.SSRC, + ) f.currentLayers.Spatial = layer f.targetLayers.Spatial = layer diff --git a/pkg/sfu/forwarder_test.go b/pkg/sfu/forwarder_test.go index 31ee32bd..a7e54c68 100644 --- a/pkg/sfu/forwarder_test.go +++ b/pkg/sfu/forwarder_test.go @@ -215,7 +215,7 @@ func TestForwarderAllocateOptimal(t *testing.T) { require.Equal(t, f.parkedLayers, f.TargetLayers()) f.parkedLayers = InvalidLayers - // when max layers changes, should switch to that + // when max layers changes, target is opportunistic, but requested spatial layer should be at max f.maxLayers = VideoLayers{Spatial: 1, Temporal: 3} expectedResult = VideoAllocation{ pauseReason: VideoPauseReasonFeedDry, @@ -280,32 +280,7 @@ func TestForwarderAllocateOptimal(t *testing.T) { require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedTargetLayers, f.TargetLayers()) - // max layers changing, feed dry, current invalid, no overshoot, should set target to max layers - f.SetMaxSpatialLayer(0) - f.SetMaxTemporalLayer(3) f.currentLayers = InvalidLayers - expectedTargetLayers = VideoLayers{ - Spatial: 0, - Temporal: DefaultMaxLayerTemporal, - } - expectedMaxLayers := VideoLayers{ - Spatial: 0, - Temporal: DefaultMaxLayerTemporal, - } - expectedResult = VideoAllocation{ - pauseReason: VideoPauseReasonFeedDry, - bandwidthRequested: 0, - bandwidthDelta: 0, - bitrates: emptyBitrates, - targetLayers: expectedTargetLayers, - requestLayerSpatial: expectedTargetLayers.Spatial, - maxLayers: expectedMaxLayers, - distanceToDesired: 0, - } - result = f.AllocateOptimal(nil, emptyBitrates, false) - require.Equal(t, expectedResult, result) - require.Equal(t, expectedResult, f.lastAllocation) - require.Equal(t, expectedTargetLayers, f.TargetLayers()) // opportunistic target if feed is not dry and current is not valid, i. e. not forwarding expectedResult = VideoAllocation{ @@ -314,8 +289,8 @@ func TestForwarderAllocateOptimal(t *testing.T) { bandwidthDelta: 0, bitrates: emptyBitrates, targetLayers: DefaultMaxLayers, - requestLayerSpatial: 0, - maxLayers: expectedMaxLayers, + requestLayerSpatial: 2, + maxLayers: DefaultMaxLayers, distanceToDesired: 0, } result = f.AllocateOptimal([]int32{0, 1}, emptyBitrates, true) @@ -323,16 +298,16 @@ func TestForwarderAllocateOptimal(t *testing.T) { require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, DefaultMaxLayers, f.TargetLayers()) - // if feed is not dry and target is not valid, should be opportunistic (with and without overshoot) + // if feed is not dry and current is not locked, should be opportunistic (with and without overshoot) f.targetLayers = InvalidLayers expectedResult = VideoAllocation{ pauseReason: VideoPauseReasonFeedDry, bandwidthRequested: 0, bandwidthDelta: 0, bitrates: emptyBitrates, - targetLayers: expectedTargetLayers, - requestLayerSpatial: 0, - maxLayers: expectedMaxLayers, + targetLayers: DefaultMaxLayers, + requestLayerSpatial: 2, + maxLayers: DefaultMaxLayers, distanceToDesired: 0, } result = f.AllocateOptimal([]int32{0, 1}, emptyBitrates, false) @@ -350,8 +325,8 @@ func TestForwarderAllocateOptimal(t *testing.T) { bandwidthDelta: 0, bitrates: emptyBitrates, targetLayers: expectedTargetLayers, - requestLayerSpatial: 0, - maxLayers: expectedMaxLayers, + requestLayerSpatial: 2, + maxLayers: DefaultMaxLayers, distanceToDesired: 0, } result = f.AllocateOptimal([]int32{0, 1}, emptyBitrates, true) @@ -371,12 +346,33 @@ func TestForwarderAllocateOptimal(t *testing.T) { bitrates: emptyBitrates, targetLayers: expectedTargetLayers, requestLayerSpatial: 1, - maxLayers: expectedMaxLayers, + maxLayers: DefaultMaxLayers, distanceToDesired: 0, } result = f.AllocateOptimal([]int32{1}, emptyBitrates, true) require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, f.lastAllocation) + + // stays the same if feed is not dry and current is valid, available and locked + f.currentLayers = VideoLayers{Spatial: 0, Temporal: 1} + f.requestLayerSpatial = 0 + expectedTargetLayers = VideoLayers{ + Spatial: 0, + Temporal: 1, + } + expectedResult = VideoAllocation{ + pauseReason: VideoPauseReasonFeedDry, + bandwidthRequested: 0, + bandwidthDelta: 0, + bitrates: emptyBitrates, + targetLayers: expectedTargetLayers, + requestLayerSpatial: 0, + maxLayers: DefaultMaxLayers, + distanceToDesired: 0, + } + result = f.AllocateOptimal([]int32{0, 1}, emptyBitrates, true) + require.Equal(t, expectedResult, result) + require.Equal(t, expectedResult, f.lastAllocation) } func TestForwarderProvisionalAllocate(t *testing.T) {