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])))
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,
}
}
+1 -1
View File
@@ -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)
+39 -30
View File
@@ -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)
}
}
// ------------------------------------------------
+20 -12
View File
@@ -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,
})
}
}
+5 -5
View File
@@ -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
}