From c0397696077573f377723d18b221523f1ac1bf50 Mon Sep 17 00:00:00 2001 From: Alexey Sokolov <39658248+am-sokolov@users.noreply.github.com> Date: Tue, 21 Oct 2025 08:24:05 +0100 Subject: [PATCH] Issue #1 only: Fix spatial layer initialization in Forwarder (#4003) When SetMaxSpatialLayer() is called with target/current layers in InvalidLayerSpatial state, opportunistically initialize the target layer to avoid dropped packets during async stream allocator initialization. Guards: - Only sets target if not congestion-throttled (isDeficientLocked) - Does not set current layer (deferred to keyframe-based forwarder start) - Logs at Debug level to avoid log noise This prevents undefined layer state during manual subscription with immediate quality upgrades (WithAutoSubscribe(false) + SetVideoQuality(HIGH)). --- pkg/sfu/forwarder.go | 7 ++++++- pkg/sfu/forwarder_test.go | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/sfu/forwarder.go b/pkg/sfu/forwarder.go index 3059439a..6fe51513 100644 --- a/pkg/sfu/forwarder.go +++ b/pkg/sfu/forwarder.go @@ -556,7 +556,12 @@ func (f *Forwarder) SetMaxSpatialLayer(spatialLayer int32) (bool, buffer.VideoLa f.logger.Debugw("setting max spatial layer", "layer", spatialLayer) f.vls.SetMaxSpatial(spatialLayer) - return true, f.vls.GetMax() + newMax := f.vls.GetMax() + if f.vls.GetTarget().Spatial == buffer.InvalidLayerSpatial && !f.isDeficientLocked() { + f.logger.Debugw("opportunistically setting target spatial layer", "layer", newMax.Spatial) + f.vls.SetTarget(newMax) + } + return true, newMax } func (f *Forwarder) SetMaxTemporalLayer(temporalLayer int32) (bool, buffer.VideoLayer) { diff --git a/pkg/sfu/forwarder_test.go b/pkg/sfu/forwarder_test.go index ee2743b9..ad24469a 100644 --- a/pkg/sfu/forwarder_test.go +++ b/pkg/sfu/forwarder_test.go @@ -464,6 +464,9 @@ func TestForwarderProvisionalAllocate(t *testing.T) { f.SetMaxPublishedLayer(buffer.DefaultMaxLayerSpatial) f.SetMaxTemporalLayerSeen(buffer.DefaultMaxLayerTemporal) + // Reset to invalid layers for testing allocation from scratch + disable(f) + bitrates := Bitrates{ {1, 2, 3, 4}, {5, 6, 7, 8}, @@ -682,6 +685,9 @@ func TestForwarderProvisionalAllocateMute(t *testing.T) { f.SetMaxSpatialLayer(buffer.DefaultMaxLayerSpatial) f.SetMaxTemporalLayer(buffer.DefaultMaxLayerTemporal) + // Reset to invalid layers for testing muted state + disable(f) + bitrates := Bitrates{ {1, 2, 3, 4}, {5, 6, 7, 8}, @@ -723,6 +729,9 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) { f.SetMaxPublishedLayer(buffer.DefaultMaxLayerSpatial) f.SetMaxTemporalLayerSeen(buffer.DefaultMaxLayerTemporal) + // Reset to invalid layers for testing cooperative transition from scratch + disable(f) + availableLayers := []int32{0, 1, 2} bitrates := Bitrates{ {1, 2, 3, 4},