Plug a couple of holes in stream transitions. (#1905)

* Plug a couple of holes in stream transitions.

1. Missed negative sign meant stealing bits from other tracks was not
   working.
2. When a track change (mute, unmute, subscription change) cannot be
   allocated, explicitly pause so that stream state update happens.

Refactor stream state update a bit to make it a bit cleaner.

* correct comment
This commit is contained in:
Raja Subramanian
2023-07-26 13:36:58 +05:30
committed by GitHub
parent 34a7b60991
commit 0484a68342
5 changed files with 67 additions and 50 deletions
+2 -2
View File
@@ -1013,7 +1013,7 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti
bandwidthDelta := int64(math.Max(float64(0), float64(existingBandwidthNeeded-f.provisional.Bitrates[s][t]))) bandwidthDelta := int64(math.Max(float64(0), float64(existingBandwidthNeeded-f.provisional.Bitrates[s][t])))
transitionCost := int32(0) 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 { if targetLayer.Spatial != s {
transitionCost = TransitionCostSpatial transitionCost = TransitionCostSpatial
} }
@@ -1036,7 +1036,7 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti
return VideoTransition{ return VideoTransition{
From: targetLayer, From: targetLayer,
To: bestLayer, To: bestLayer,
BandwidthDelta: bestBandwidthDelta, BandwidthDelta: -bestBandwidthDelta,
} }
} }
+1 -1
View File
@@ -847,7 +847,7 @@ func TestForwarderProvisionalAllocateGetBestWeightedTransition(t *testing.T) {
expectedTransition := VideoTransition{ expectedTransition := VideoTransition{
From: f.TargetLayer(), From: f.TargetLayer(),
To: buffer.VideoLayer{Spatial: 2, Temporal: 0}, To: buffer.VideoLayer{Spatial: 2, Temporal: 0},
BandwidthDelta: 2, BandwidthDelta: -2,
} }
transition := f.ProvisionalAllocateGetBestWeightedTransition() transition := f.ProvisionalAllocateGetBestWeightedTransition()
require.Equal(t, expectedTransition, transition) require.Equal(t, expectedTransition, transition)
+39 -30
View File
@@ -79,7 +79,7 @@ func (s streamAllocatorState) String() string {
case streamAllocatorStateDeficient: case streamAllocatorStateDeficient:
return "DEFICIENT" return "DEFICIENT"
default: 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 { if track != nil {
update := NewStreamStateUpdate() update := NewStreamStateUpdate()
if track.SetPaused(false) { if track.SetStreamState(StreamStateActive) {
update.HandleStreamingChange(false, track) update.HandleStreamingChange(track, StreamStateActive)
} }
s.maybeSendUpdate(update) s.maybeSendUpdate(update)
} }
@@ -840,9 +840,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
if !s.params.Config.Enabled || s.state == streamAllocatorStateStable || !track.IsManaged() { if !s.params.Config.Enabled || s.state == streamAllocatorStateStable || !track.IsManaged() {
update := NewStreamStateUpdate() update := NewStreamStateUpdate()
allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal) allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal)
if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { updateStreamStateChange(track, allocation, update)
update.HandleStreamingChange(true, track)
}
s.maybeSendUpdate(update) s.maybeSendUpdate(update)
return return
} }
@@ -867,9 +865,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
allocation := track.ProvisionalAllocateCommit() allocation := track.ProvisionalAllocateCommit()
update := NewStreamStateUpdate() update := NewStreamStateUpdate()
if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { updateStreamStateChange(track, allocation, update)
update.HandleStreamingChange(true, track)
}
s.maybeSendUpdate(update) s.maybeSendUpdate(update)
s.adjustState() s.adjustState()
@@ -910,9 +906,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
// commit the tracks that contributed // commit the tracks that contributed
for _, t := range contributingTracks { for _, t := range contributingTracks {
allocation := t.ProvisionalAllocateCommit() allocation := t.ProvisionalAllocateCommit()
if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { updateStreamStateChange(t, allocation, update)
update.HandleStreamingChange(true, t)
}
} }
// STREAM-ALLOCATOR-TODO if got too much extra, can potentially give it to some deficient track // 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 // commit the track that needs change if enough could be acquired or pause not allowed
if !s.allowPause || bandwidthAcquired >= transition.BandwidthDelta { if !s.allowPause || bandwidthAcquired >= transition.BandwidthDelta {
allocation := track.ProvisionalAllocateCommit() allocation := track.ProvisionalAllocateCommit()
if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { updateStreamStateChange(track, allocation, update)
update.HandleStreamingChange(true, track) } 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) s.maybeSendUpdate(update)
@@ -989,9 +985,7 @@ func (s *StreamAllocator) maybeBoostDeficientTracks() {
continue continue
} }
if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { updateStreamStateChange(track, allocation, update)
update.HandleStreamingChange(true, track)
}
availableChannelCapacity -= allocation.BandwidthDelta availableChannelCapacity -= allocation.BandwidthDelta
if availableChannelCapacity <= 0 { if availableChannelCapacity <= 0 {
@@ -1053,9 +1047,7 @@ func (s *StreamAllocator) allocateAllTracks() {
} }
allocation := track.AllocateOptimal(FlagAllowOvershootExemptTrackWhileDeficient) allocation := track.AllocateOptimal(FlagAllowOvershootExemptTrackWhileDeficient)
if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { updateStreamStateChange(track, allocation, update)
update.HandleStreamingChange(true, track)
}
// STREAM-ALLOCATOR-TODO: optimistic allocation before bitrate is available will return 0. How to account for that? // STREAM-ALLOCATOR-TODO: optimistic allocation before bitrate is available will return 0. How to account for that?
availableChannelCapacity -= allocation.BandwidthRequested availableChannelCapacity -= allocation.BandwidthRequested
@@ -1072,9 +1064,7 @@ func (s *StreamAllocator) allocateAllTracks() {
} }
allocation := track.Pause() allocation := track.Pause()
if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { updateStreamStateChange(track, allocation, update)
update.HandleStreamingChange(true, track)
}
} }
} else { } else {
sorted := s.getSorted() sorted := s.getSorted()
@@ -1101,9 +1091,7 @@ func (s *StreamAllocator) allocateAllTracks() {
for _, track := range sorted { for _, track := range sorted {
allocation := track.ProvisionalAllocateCommit() allocation := track.ProvisionalAllocateCommit()
if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { updateStreamStateChange(track, allocation, update)
update.HandleStreamingChange(true, track)
}
} }
} }
@@ -1225,9 +1213,7 @@ func (s *StreamAllocator) maybeProbeWithMedia() {
} }
update := NewStreamStateUpdate() update := NewStreamStateUpdate()
if allocation.PauseReason == sfu.VideoPauseReasonBandwidth && track.SetPaused(true) { updateStreamStateChange(track, allocation, update)
update.HandleStreamingChange(true, track)
}
s.maybeSendUpdate(update) s.maybeSendUpdate(update)
s.probeController.Reset() 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)
}
}
// ------------------------------------------------
+20 -12
View File
@@ -1,6 +1,8 @@
package streamallocator package streamallocator
import ( import (
"fmt"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
) )
@@ -9,18 +11,21 @@ import (
type StreamState int type StreamState int
const ( const (
StreamStateActive StreamState = iota StreamStateInactive StreamState = iota
StreamStateActive
StreamStatePaused StreamStatePaused
) )
func (s StreamState) String() string { func (s StreamState) String() string {
switch s { switch s {
case StreamStateInactive:
return "INACTIVE"
case StreamStateActive: case StreamStateActive:
return "active" return "ACTIVE"
case StreamStatePaused: case StreamStatePaused:
return "paused" return "PAUSED"
default: default:
return "unknown" return fmt.Sprintf("UNKNOWN: %d", int(s))
} }
} }
@@ -40,19 +45,22 @@ func NewStreamStateUpdate() *StreamStateUpdate {
return &StreamStateUpdate{} return &StreamStateUpdate{}
} }
func (s *StreamStateUpdate) HandleStreamingChange(isPaused bool, track *Track) { func (s *StreamStateUpdate) HandleStreamingChange(track *Track, streamState StreamState) {
if isPaused { switch streamState {
s.StreamStates = append(s.StreamStates, &StreamStateInfo{ case StreamStateInactive:
ParticipantID: track.PublisherID(), // inactive is not a notification, could get into this state because of mute
TrackID: track.ID(), case StreamStateActive:
State: StreamStatePaused,
})
} else {
s.StreamStates = append(s.StreamStates, &StreamStateInfo{ s.StreamStates = append(s.StreamStates, &StreamStateInfo{
ParticipantID: track.PublisherID(), ParticipantID: track.PublisherID(),
TrackID: track.ID(), TrackID: track.ID(),
State: StreamStateActive, State: StreamStateActive,
}) })
case StreamStatePaused:
s.StreamStates = append(s.StreamStates, &StreamStateInfo{
ParticipantID: track.PublisherID(),
TrackID: track.ID(),
State: StreamStatePaused,
})
} }
} }
+5 -5
View File
@@ -42,7 +42,7 @@ type Track struct {
isDirty bool isDirty bool
isPaused bool streamState StreamState
} }
func NewTrack( func NewTrack(
@@ -61,7 +61,7 @@ func NewTrack(
nackInfos: make(map[uint16]sfu.NackInfo), nackInfos: make(map[uint16]sfu.NackInfo),
nackHistory: make([]string, 0, 10), nackHistory: make([]string, 0, 10),
receiverReportHistory: make([]string, 0, 10), receiverReportHistory: make([]string, 0, 10),
isPaused: true, streamState: StreamStateInactive,
} }
t.SetPriority(0) t.SetPriority(0)
t.SetMaxLayer(downTrack.MaxLayer()) t.SetMaxLayer(downTrack.MaxLayer())
@@ -78,12 +78,12 @@ func (t *Track) SetDirty(isDirty bool) bool {
return true return true
} }
func (t *Track) SetPaused(isPaused bool) bool { func (t *Track) SetStreamState(streamState StreamState) bool {
if t.isPaused == isPaused { if t.streamState == streamState {
return false return false
} }
t.isPaused = isPaused t.streamState = streamState
return true return true
} }