diff --git a/pkg/sfu/forwarder.go b/pkg/sfu/forwarder.go index 02f711d0..b8343e75 100644 --- a/pkg/sfu/forwarder.go +++ b/pkg/sfu/forwarder.go @@ -1013,7 +1013,7 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti bandwidthDelta := int64(math.Max(float64(0), float64(existingBandwidthNeeded-f.provisional.Bitrates[s][t]))) transitionCost := int32(0) - // LK-TODO: SVC will need a different cost transition + // SVC-TODO: SVC will need a different cost transition if targetLayer.Spatial != s { transitionCost = TransitionCostSpatial } @@ -1036,7 +1036,7 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti return VideoTransition{ From: targetLayer, To: bestLayer, - BandwidthDelta: bestBandwidthDelta, + BandwidthDelta: -bestBandwidthDelta, } } diff --git a/pkg/sfu/forwarder_test.go b/pkg/sfu/forwarder_test.go index 2da1c524..a9909583 100644 --- a/pkg/sfu/forwarder_test.go +++ b/pkg/sfu/forwarder_test.go @@ -847,7 +847,7 @@ func TestForwarderProvisionalAllocateGetBestWeightedTransition(t *testing.T) { expectedTransition := VideoTransition{ From: f.TargetLayer(), To: buffer.VideoLayer{Spatial: 2, Temporal: 0}, - BandwidthDelta: 2, + BandwidthDelta: -2, } transition := f.ProvisionalAllocateGetBestWeightedTransition() require.Equal(t, expectedTransition, transition) diff --git a/pkg/sfu/streamallocator/streamallocator.go b/pkg/sfu/streamallocator/streamallocator.go index f0c17db9..4e12bcee 100644 --- a/pkg/sfu/streamallocator/streamallocator.go +++ b/pkg/sfu/streamallocator/streamallocator.go @@ -79,7 +79,7 @@ func (s streamAllocatorState) String() string { case streamAllocatorStateDeficient: return "DEFICIENT" default: - return fmt.Sprintf("%d", int(s)) + return fmt.Sprintf("UNKNOWN: %d", int(s)) } } @@ -696,8 +696,8 @@ func (s *StreamAllocator) handleSignalResume(event *Event) { if track != nil { update := NewStreamStateUpdate() - if track.SetPaused(false) { - update.HandleStreamingChange(false, track) + if track.SetStreamState(StreamStateActive) { + update.HandleStreamingChange(track, StreamStateActive) } s.maybeSendUpdate(update) } @@ -840,9 +840,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) { if !s.params.Config.Enabled || s.state == streamAllocatorStateStable || !track.IsManaged() { update := NewStreamStateUpdate() allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal) - if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { - update.HandleStreamingChange(true, track) - } + updateStreamStateChange(track, allocation, update) s.maybeSendUpdate(update) return } @@ -867,9 +865,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) { allocation := track.ProvisionalAllocateCommit() update := NewStreamStateUpdate() - if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { - update.HandleStreamingChange(true, track) - } + updateStreamStateChange(track, allocation, update) s.maybeSendUpdate(update) s.adjustState() @@ -910,9 +906,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) { // commit the tracks that contributed for _, t := range contributingTracks { allocation := t.ProvisionalAllocateCommit() - if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { - update.HandleStreamingChange(true, t) - } + updateStreamStateChange(t, allocation, update) } // STREAM-ALLOCATOR-TODO if got too much extra, can potentially give it to some deficient track @@ -921,9 +915,11 @@ func (s *StreamAllocator) allocateTrack(track *Track) { // commit the track that needs change if enough could be acquired or pause not allowed if !s.allowPause || bandwidthAcquired >= transition.BandwidthDelta { allocation := track.ProvisionalAllocateCommit() - if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { - update.HandleStreamingChange(true, track) - } + updateStreamStateChange(track, allocation, update) + } else { + // explicitly pause to ensure stream state update happens if a track coming out of mute cannot be allocated + allocation := track.Pause() + updateStreamStateChange(track, allocation, update) } s.maybeSendUpdate(update) @@ -989,9 +985,7 @@ func (s *StreamAllocator) maybeBoostDeficientTracks() { continue } - if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { - update.HandleStreamingChange(true, track) - } + updateStreamStateChange(track, allocation, update) availableChannelCapacity -= allocation.BandwidthDelta if availableChannelCapacity <= 0 { @@ -1053,9 +1047,7 @@ func (s *StreamAllocator) allocateAllTracks() { } allocation := track.AllocateOptimal(FlagAllowOvershootExemptTrackWhileDeficient) - if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { - update.HandleStreamingChange(true, track) - } + updateStreamStateChange(track, allocation, update) // STREAM-ALLOCATOR-TODO: optimistic allocation before bitrate is available will return 0. How to account for that? availableChannelCapacity -= allocation.BandwidthRequested @@ -1072,9 +1064,7 @@ func (s *StreamAllocator) allocateAllTracks() { } allocation := track.Pause() - if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { - update.HandleStreamingChange(true, track) - } + updateStreamStateChange(track, allocation, update) } } else { sorted := s.getSorted() @@ -1101,9 +1091,7 @@ func (s *StreamAllocator) allocateAllTracks() { for _, track := range sorted { allocation := track.ProvisionalAllocateCommit() - if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { - update.HandleStreamingChange(true, track) - } + updateStreamStateChange(track, allocation, update) } } @@ -1225,9 +1213,7 @@ func (s *StreamAllocator) maybeProbeWithMedia() { } update := NewStreamStateUpdate() - if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { - update.HandleStreamingChange(true, track) - } + updateStreamStateChange(track, allocation, update) s.maybeSendUpdate(update) s.probeController.Reset() @@ -1354,3 +1340,26 @@ func (s *StreamAllocator) getTracksHistory() map[livekit.TrackID]string { } // ------------------------------------------------ + +func updateStreamStateChange(track *Track, allocation sfu.VideoAllocation, update *StreamStateUpdate) { + updated := false + streamState := StreamStateInactive + switch allocation.PauseReason { + case sfu.VideoPauseReasonMuted: + fallthrough + + case sfu.VideoPauseReasonPubMuted: + streamState = StreamStateInactive + updated = track.SetStreamState(streamState) + + case sfu.VideoPauseReasonBandwidth: + streamState = StreamStatePaused + updated = track.SetStreamState(streamState) + } + + if updated { + update.HandleStreamingChange(track, streamState) + } +} + +// ------------------------------------------------ diff --git a/pkg/sfu/streamallocator/streamstateupdate.go b/pkg/sfu/streamallocator/streamstateupdate.go index e5b3156f..b7bca6a2 100644 --- a/pkg/sfu/streamallocator/streamstateupdate.go +++ b/pkg/sfu/streamallocator/streamstateupdate.go @@ -1,6 +1,8 @@ package streamallocator import ( + "fmt" + "github.com/livekit/protocol/livekit" ) @@ -9,18 +11,21 @@ import ( type StreamState int const ( - StreamStateActive StreamState = iota + StreamStateInactive StreamState = iota + StreamStateActive StreamStatePaused ) func (s StreamState) String() string { switch s { + case StreamStateInactive: + return "INACTIVE" case StreamStateActive: - return "active" + return "ACTIVE" case StreamStatePaused: - return "paused" + return "PAUSED" default: - return "unknown" + return fmt.Sprintf("UNKNOWN: %d", int(s)) } } @@ -40,19 +45,22 @@ func NewStreamStateUpdate() *StreamStateUpdate { return &StreamStateUpdate{} } -func (s *StreamStateUpdate) HandleStreamingChange(isPaused bool, track *Track) { - if isPaused { - s.StreamStates = append(s.StreamStates, &StreamStateInfo{ - ParticipantID: track.PublisherID(), - TrackID: track.ID(), - State: StreamStatePaused, - }) - } else { +func (s *StreamStateUpdate) HandleStreamingChange(track *Track, streamState StreamState) { + switch streamState { + case StreamStateInactive: + // inactive is not a notification, could get into this state because of mute + case StreamStateActive: s.StreamStates = append(s.StreamStates, &StreamStateInfo{ ParticipantID: track.PublisherID(), TrackID: track.ID(), State: StreamStateActive, }) + case StreamStatePaused: + s.StreamStates = append(s.StreamStates, &StreamStateInfo{ + ParticipantID: track.PublisherID(), + TrackID: track.ID(), + State: StreamStatePaused, + }) } } diff --git a/pkg/sfu/streamallocator/track.go b/pkg/sfu/streamallocator/track.go index 4c1a125e..7a74d605 100644 --- a/pkg/sfu/streamallocator/track.go +++ b/pkg/sfu/streamallocator/track.go @@ -42,7 +42,7 @@ type Track struct { isDirty bool - isPaused bool + streamState StreamState } func NewTrack( @@ -61,7 +61,7 @@ func NewTrack( nackInfos: make(map[uint16]sfu.NackInfo), nackHistory: make([]string, 0, 10), receiverReportHistory: make([]string, 0, 10), - isPaused: true, + streamState: StreamStateInactive, } t.SetPriority(0) t.SetMaxLayer(downTrack.MaxLayer()) @@ -78,12 +78,12 @@ func (t *Track) SetDirty(isDirty bool) bool { return true } -func (t *Track) SetPaused(isPaused bool) bool { - if t.isPaused == isPaused { +func (t *Track) SetStreamState(streamState StreamState) bool { + if t.streamState == streamState { return false } - t.isPaused = isPaused + t.streamState = streamState return true }