Deficient state handling when a track needs a change (#261)

* WIP commit

* deficient handling

* Add missing ProvisionalAllocatePrepare

* adjust state on track removal

* Increase test timeout

* - Add comments about cooperative routines
- Take down transition if available in cooperative scheme
- Use layer comparison when taking down transition. Because of when the
  bitrate is measured, it is not always guaranteed bandwidthDelta is -ve
  when moving down.
- Do not add track to stream allocator till bind.

* make comment better

* a bit more clear comments

* Use OnBind on subscribed track
This commit is contained in:
Raja Subramanian
2021-12-16 10:58:34 +05:30
committed by GitHub
parent c08d1e9d72
commit e504b6678c
10 changed files with 509 additions and 26 deletions
+122 -10
View File
@@ -134,6 +134,10 @@ type Event struct {
Data interface{}
}
func (e Event) String() string {
return fmt.Sprintf("StreamAllocator:Event{signal: %s, data: %s}", e.Signal, e.Data)
}
func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
s := &StreamAllocator{
logger: params.Logger,
@@ -390,6 +394,7 @@ func (s *StreamAllocator) handleSignalRemoveTrack(event *Event) {
}
// LK-TODO: use any saved bandwidth to re-distribute
s.adjustState()
}
}
@@ -632,11 +637,90 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
return
}
// LK-TODO-START
// Two possible scenarios
// - Down allocate as necessary, take saved bits and re-distribute
// - If up shifting, try to steal from tracks which are closest to the desired
// LK-TODO-END
//
// In DEFICIENT state,
// 1. Find cooperative transition from track that needs allocation.
// 2. If track is currently streaming at minimum, do not do anything.
// 3. If that track is giving back bits, apply the transition.
// 4. If this track needs more, ask for best offer from others and try to use it.
//
track.ProvisionalAllocatePrepare()
transition := track.ProvisionalAllocateGetCooperativeTransition()
// track is currently streaming at minimum
if transition.bandwidthDelta == 0 {
return
}
// downgrade, giving back bits
if transition.from.GreaterThan(transition.to) {
allocation := track.ProvisionalAllocateCommit()
update := NewStreamStateUpdate()
update.HandleStreamingChange(allocation.change, track)
s.maybeSendUpdate(update)
s.adjustState()
return
// LK-TODO-START
// Should use the bits given back to start any paused track.
// Note layer downgrade may actually have positive delta (i. e. consume more bits)
// because of when the measurement is done. Watch for that.
// LK-TODO-END
}
//
// This track is currently not streaming and needs bits to start.
// Try to redistribute starting with tracks that are closest to their desired.
//
var minDistanceSorted MinDistanceSorter
for _, t := range s.managedVideoTracksSorted {
if t != track {
minDistanceSorted = append(minDistanceSorted, t)
}
}
sort.Sort(minDistanceSorted)
bandwidthAcquired := int64(0)
var contributingTracks []*Track
for _, t := range minDistanceSorted {
t.ProvisionalAllocatePrepare()
}
for _, t := range minDistanceSorted {
tx := t.ProvisionalAllocateGetBestWeightedTransition()
if tx.bandwidthDelta < 0 {
contributingTracks = append(contributingTracks, t)
bandwidthAcquired += -tx.bandwidthDelta
if bandwidthAcquired >= transition.bandwidthDelta {
break
}
}
}
if bandwidthAcquired < transition.bandwidthDelta {
// could not get enough from other tracks, let probing deal with starting the track
return
}
// commit the tracks that contributed
update := NewStreamStateUpdate()
for _, t := range contributingTracks {
allocation := t.ProvisionalAllocateCommit()
update.HandleStreamingChange(allocation.change, t)
}
// commit the track that needs change
allocation := track.ProvisionalAllocateCommit()
update.HandleStreamingChange(allocation.change, track)
// LK-TODO if got too much extra, can potentially give it to some deficient track
s.maybeSendUpdate(update)
s.adjustState()
}
func (s *StreamAllocator) allocateAllTracks() {
@@ -727,6 +811,10 @@ func (s *StreamAllocator) maybeSendUpdate(update *StreamStateUpdate) {
}
func (s *StreamAllocator) finalizeTracks() {
for _, t := range s.exemptVideoTracksSorted {
t.FinalizeAllocate()
}
for _, t := range s.managedVideoTracksSorted {
t.FinalizeAllocate()
}
@@ -786,14 +874,14 @@ func (s *StreamAllocator) maybeProbe() {
}
func (s *StreamAllocator) maybeBoostLayer() {
var distanceSorted MaxDistanceSorter
var maxDistanceSorted MaxDistanceSorter
for _, track := range s.managedVideoTracksSorted {
distanceSorted = append(distanceSorted, track)
maxDistanceSorted = append(maxDistanceSorted, track)
}
sort.Sort(distanceSorted)
sort.Sort(maxDistanceSorted)
// boost first deficient track in priority order
for _, track := range distanceSorted {
for _, track := range maxDistanceSorted {
if !track.IsDeficient() {
continue
}
@@ -824,7 +912,7 @@ func (s *StreamAllocator) isTimeToBoost() bool {
}
func (s *StreamAllocator) resetBoost() {
s.lastBoostTime = time.Now()
s.lastBoostTime = time.Time{}
}
func (s *StreamAllocator) maybeGratuitousProbe() bool {
@@ -992,6 +1080,14 @@ func (t *Track) ProvisionalAllocate(availableChannelCapacity int64, layers Video
return t.downTrack.ProvisionalAllocate(availableChannelCapacity, layers)
}
func (t *Track) ProvisionalAllocateGetCooperativeTransition() VideoTransition {
return t.downTrack.ProvisionalAllocateGetCooperativeTransition()
}
func (t *Track) ProvisionalAllocateGetBestWeightedTransition() VideoTransition {
return t.downTrack.ProvisionalAllocateGetBestWeightedTransition()
}
func (t *Track) ProvisionalAllocateCommit() VideoAllocation {
return t.downTrack.ProvisionalAllocateCommit()
}
@@ -1057,3 +1153,19 @@ func (m MaxDistanceSorter) Less(i, j int) bool {
}
//------------------------------------------------
type MinDistanceSorter []*Track
func (m MinDistanceSorter) Len() int {
return len(m)
}
func (m MinDistanceSorter) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m MinDistanceSorter) Less(i, j int) bool {
return m[i].DistanceToDesired() < m[j].DistanceToDesired()
}
//------------------------------------------------