Handle exempted layers (#983)

* WIP commit

* WIP commit

* Add tests

* remove debug
This commit is contained in:
Raja Subramanian
2022-09-05 18:42:59 +05:30
committed by GitHub
parent 021ec596b5
commit d8ae453fb9
6 changed files with 893 additions and 289 deletions
+13 -13
View File
@@ -25,7 +25,7 @@ import (
// TrackSender defines an interface send media to remote peer // TrackSender defines an interface send media to remote peer
type TrackSender interface { type TrackSender interface {
UpTrackLayersChange(availableLayers []int32) UpTrackLayersChange(availableLayers []int32, exemptedLayers []int32)
UpTrackBitrateAvailabilityChange() UpTrackBitrateAvailabilityChange()
WriteRTP(p *buffer.ExtPacket, layer int32) error WriteRTP(p *buffer.ExtPacket, layer int32) error
Close() Close()
@@ -755,8 +755,8 @@ func (d *DownTrack) GetForwardingStatus() ForwardingStatus {
return d.forwarder.GetForwardingStatus() return d.forwarder.GetForwardingStatus()
} }
func (d *DownTrack) UpTrackLayersChange(availableLayers []int32) { func (d *DownTrack) UpTrackLayersChange(availableLayers []int32, exemptedLayers []int32) {
d.forwarder.UpTrackLayersChange(availableLayers) d.forwarder.UpTrackLayersChange(availableLayers, exemptedLayers)
if onAvailableLayersChanged, ok := d.onAvailableLayersChanged.Load().(func(dt *DownTrack)); ok { if onAvailableLayersChanged, ok := d.onAvailableLayersChanged.Load().(func(dt *DownTrack)); ok {
onAvailableLayersChanged(d) onAvailableLayersChanged(d)
@@ -841,8 +841,8 @@ func (d *DownTrack) DistanceToDesired() int32 {
return d.forwarder.DistanceToDesired() return d.forwarder.DistanceToDesired()
} }
func (d *DownTrack) AllocateOptimal() VideoAllocation { func (d *DownTrack) AllocateOptimal(allowOvershoot bool) VideoAllocation {
allocation := d.forwarder.AllocateOptimal(d.receiver.GetBitrateTemporalCumulative()) allocation := d.forwarder.AllocateOptimal(d.receiver.GetBitrateTemporalCumulative(), allowOvershoot)
d.logger.Debugw("stream: allocation optimal available", "allocation", allocation) d.logger.Debugw("stream: allocation optimal available", "allocation", allocation)
d.maybeStartKeyFrameRequester() d.maybeStartKeyFrameRequester()
return allocation return allocation
@@ -852,12 +852,12 @@ func (d *DownTrack) ProvisionalAllocatePrepare() {
d.forwarder.ProvisionalAllocatePrepare(d.receiver.GetBitrateTemporalCumulative()) d.forwarder.ProvisionalAllocatePrepare(d.receiver.GetBitrateTemporalCumulative())
} }
func (d *DownTrack) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool) int64 { func (d *DownTrack) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool, allowOvershoot bool) int64 {
return d.forwarder.ProvisionalAllocate(availableChannelCapacity, layers, allowPause) return d.forwarder.ProvisionalAllocate(availableChannelCapacity, layers, allowPause, allowOvershoot)
} }
func (d *DownTrack) ProvisionalAllocateGetCooperativeTransition() VideoTransition { func (d *DownTrack) ProvisionalAllocateGetCooperativeTransition(allowOvershoot bool) VideoTransition {
transition := d.forwarder.ProvisionalAllocateGetCooperativeTransition() transition := d.forwarder.ProvisionalAllocateGetCooperativeTransition(allowOvershoot)
d.logger.Debugw("stream: cooperative transition", "transition", transition) d.logger.Debugw("stream: cooperative transition", "transition", transition)
return transition return transition
} }
@@ -875,15 +875,15 @@ func (d *DownTrack) ProvisionalAllocateCommit() VideoAllocation {
return allocation return allocation
} }
func (d *DownTrack) AllocateNextHigher(availableChannelCapacity int64) (VideoAllocation, bool) { func (d *DownTrack) AllocateNextHigher(availableChannelCapacity int64, allowOvershoot bool) (VideoAllocation, bool) {
allocation, available := d.forwarder.AllocateNextHigher(availableChannelCapacity, d.receiver.GetBitrateTemporalCumulative()) allocation, available := d.forwarder.AllocateNextHigher(availableChannelCapacity, d.receiver.GetBitrateTemporalCumulative(), allowOvershoot)
d.logger.Debugw("stream: allocation next higher layer", "allocation", allocation, "available", available) d.logger.Debugw("stream: allocation next higher layer", "allocation", allocation, "available", available)
d.maybeStartKeyFrameRequester() d.maybeStartKeyFrameRequester()
return allocation, available return allocation, available
} }
func (d *DownTrack) GetNextHigherTransition() (VideoTransition, bool) { func (d *DownTrack) GetNextHigherTransition(allowOvershoot bool) (VideoTransition, bool) {
transition, available := d.forwarder.GetNextHigherTransition(d.receiver.GetBitrateTemporalCumulative()) transition, available := d.forwarder.GetNextHigherTransition(d.receiver.GetBitrateTemporalCumulative(), allowOvershoot)
d.logger.Debugw("stream: get next higher layer", "transition", transition, "available", available) d.logger.Debugw("stream: get next higher layer", "transition", transition, "available", available)
return transition, available return transition, available
} }
+330 -143
View File
@@ -95,14 +95,15 @@ type VideoAllocation struct {
bandwidthRequested int64 bandwidthRequested int64
bandwidthDelta int64 bandwidthDelta int64
availableLayers []int32 availableLayers []int32
exemptedLayers []int32
bitrates Bitrates bitrates Bitrates
targetLayers VideoLayers targetLayers VideoLayers
distanceToDesired int32 distanceToDesired int32
} }
func (v VideoAllocation) String() string { func (v VideoAllocation) String() string {
return fmt.Sprintf("VideoAllocation{state: %s, change: %s, bw: %d, del: %d, avail: %+v, rates: %+v, target: %s}", return fmt.Sprintf("VideoAllocation{state: %s, change: %s, bw: %d, del: %d, avail: %+v, exmpt: %+v, rates: %+v, target: %s}",
v.state, v.change, v.bandwidthRequested, v.bandwidthDelta, v.availableLayers, v.bitrates, v.targetLayers) v.state, v.change, v.bandwidthRequested, v.bandwidthDelta, v.availableLayers, v.exemptedLayers, v.bitrates, v.targetLayers)
} }
var ( var (
@@ -114,11 +115,13 @@ var (
// ------------------------------------------------------------------- // -------------------------------------------------------------------
type VideoAllocationProvisional struct { type VideoAllocationProvisional struct {
layers VideoLayers
muted bool muted bool
bitrates Bitrates bitrates Bitrates
availableLayers []int32 availableLayers []int32
exemptedLayers []int32
maxLayers VideoLayers maxLayers VideoLayers
allocatedLayers VideoLayers
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
@@ -192,6 +195,7 @@ type Forwarder struct {
lastAllocation VideoAllocation lastAllocation VideoAllocation
availableLayers []int32 availableLayers []int32
exemptedLayers []int32
rtpMunger *RTPMunger rtpMunger *RTPMunger
vp8Munger *VP8Munger vp8Munger *VP8Munger
@@ -390,7 +394,7 @@ func (f *Forwarder) IsReducedQuality() (int32, bool) {
return distance, f.lastAllocation.state == VideoAllocationStateDeficient return distance, f.lastAllocation.state == VideoAllocationStateDeficient
} }
func (f *Forwarder) UpTrackLayersChange(availableLayers []int32) { func (f *Forwarder) UpTrackLayersChange(availableLayers []int32, exemptedLayers []int32) {
f.lock.Lock() f.lock.Lock()
defer f.lock.Unlock() defer f.lock.Unlock()
@@ -400,11 +404,22 @@ func (f *Forwarder) UpTrackLayersChange(availableLayers []int32) {
} else { } else {
f.availableLayers = nil f.availableLayers = nil
} }
if len(exemptedLayers) > 0 {
f.exemptedLayers = make([]int32, len(exemptedLayers))
copy(f.exemptedLayers, exemptedLayers)
} else {
f.exemptedLayers = nil
}
} }
func (f *Forwarder) getOptimalBandwidthNeeded(brs Bitrates) int64 { func (f *Forwarder) getOptimalBandwidthNeeded(brs Bitrates, maxLayers VideoLayers) int64 {
for i := f.maxLayers.Spatial; i >= 0; i-- { if f.muted {
for j := f.maxLayers.Temporal; j >= 0; j-- { return 0
}
for i := maxLayers.Spatial; i >= 0; i-- {
for j := maxLayers.Temporal; j >= 0; j-- {
if brs[i][j] == 0 { if brs[i][j] == 0 {
continue continue
} }
@@ -418,33 +433,51 @@ func (f *Forwarder) getOptimalBandwidthNeeded(brs Bitrates) int64 {
func (f *Forwarder) bitrateAvailable(brs Bitrates) bool { func (f *Forwarder) bitrateAvailable(brs Bitrates) bool {
neededLayers := 0 neededLayers := 0
var bitrateAvailableLayers []int32 bitrateAvailableLayers := 0
for _, layer := range f.availableLayers { for _, layer := range f.availableLayers {
if layer > f.maxLayers.Spatial { if layer > f.maxLayers.Spatial {
continue continue
} }
//
// Layers could be exempted from stream tracker.
// If such a layer actually stops, it will not
// be removed from available layers as it is exempt.
// But, it could have zero bit rate as it actually stopped.
// So, do not take exempt layers into bitrate availability condition.
//
exempt := false
for _, el := range f.exemptedLayers {
if layer == el {
exempt = true
break
}
}
if exempt {
continue
}
neededLayers++ neededLayers++
for t := f.maxLayers.Temporal; t >= 0; t-- { for t := f.maxLayers.Temporal; t >= 0; t-- {
if brs[layer][t] != 0 { if brs[layer][t] != 0 {
bitrateAvailableLayers = append(bitrateAvailableLayers, layer) bitrateAvailableLayers++
break break
} }
} }
} }
return len(bitrateAvailableLayers) == neededLayers return bitrateAvailableLayers == neededLayers
} }
func (f *Forwarder) getDistanceToDesired(brs Bitrates, targetLayers VideoLayers) int32 { func (f *Forwarder) getDistanceToDesired(brs Bitrates, targetLayers VideoLayers, maxLayers VideoLayers) int32 {
if f.muted { if f.muted {
return 0 return 0
} }
distance := int32(0)
for s := f.maxLayers.Spatial; s >= 0; s-- {
found := false found := false
for t := f.maxLayers.Temporal; t >= 0; t-- { distance := int32(0)
for s := maxLayers.Spatial; s >= 0; s-- {
for t := maxLayers.Temporal; t >= 0; t-- {
if brs[s][t] == 0 { if brs[s][t] == 0 {
continue continue
} }
@@ -461,6 +494,19 @@ func (f *Forwarder) getDistanceToDesired(brs Bitrates, targetLayers VideoLayers)
} }
} }
// maybe overshooting
if !found && targetLayers.IsValid() {
distance = 0
for s := targetLayers.Spatial; s > f.maxLayers.Spatial; s-- {
for t := f.maxLayers.Temporal; t >= 0; t-- {
if targetLayers.Temporal < t || brs[s][t] == 0 {
continue
}
distance--
}
}
}
return distance return distance
} }
@@ -489,7 +535,7 @@ func (f *Forwarder) DistanceToDesired() int32 {
return f.lastAllocation.distanceToDesired return f.lastAllocation.distanceToDesired
} }
func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation { func (f *Forwarder) AllocateOptimal(brs Bitrates, allowOvershoot bool) VideoAllocation {
f.lock.Lock() f.lock.Lock()
defer f.lock.Unlock() defer f.lock.Unlock()
@@ -537,10 +583,6 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation {
bandwidthRequested = brs[s][t] bandwidthRequested = brs[s][t]
state = VideoAllocationStateOptimal state = VideoAllocationStateOptimal
if f.targetLayers == InvalidLayers {
change = VideoStreamingChangeResuming
}
break break
} }
@@ -549,15 +591,15 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation {
} }
} }
if bandwidthRequested == 0 && f.maxLayers.IsValid() { if bandwidthRequested == 0 && f.maxLayers.IsValid() && allowOvershoot {
// if we cannot allocate anything below max layer, // if we cannot allocate anything below max layer,
// look for a layer above. It is okay to overshoot // look for a layer above. It is okay to overshoot
// in optimal allocation (i.e. no bandwidth restrictions). // in optimal allocation (i.e. no bandwidth restrictions).
// It is possible that clients send only a higher layer. // It is possible that clients send only a higher layer.
// To accommodate cases like that, try finding a layer // To accommodate cases like that, try finding a layer
// above the requested maximum to ensure streaming // above the requested maximum to ensure streaming
for s := DefaultMaxLayerSpatial; s >= 0; s-- { for s := f.maxLayers.Spatial + 1; s <= DefaultMaxLayerSpatial; s++ {
for t := DefaultMaxLayerTemporal; t >= 0; t-- { for t := int32(0); t <= DefaultMaxLayerTemporal; t++ {
if brs[s][t] == 0 { if brs[s][t] == 0 {
continue continue
} }
@@ -569,10 +611,6 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation {
bandwidthRequested = brs[s][t] bandwidthRequested = brs[s][t]
state = VideoAllocationStateOptimal state = VideoAllocationStateOptimal
if f.targetLayers == InvalidLayers {
change = VideoStreamingChangeResuming
}
f.logger.Infow("allowing overshoot", "maxLayer", f.maxLayers, "targetLayers", targetLayers) f.logger.Infow("allowing overshoot", "maxLayer", f.maxLayers, "targetLayers", targetLayers)
break break
} }
@@ -582,6 +620,35 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation {
} }
} }
} }
if bandwidthRequested == 0 && f.maxLayers.IsValid() {
// if overshoot was allowed and it did not also find a layer,
// keep target at exempted layer (if available) and the current layer is at that level.
// i. e. exempted layer may really have stopped, so a layer switch to an exempted layer should
// not happen as layer switch will send PLI requests. Just letting it continue at the current
// layer if the current is exempted will protect against any stream tracker misdetects
// OR latch on to the layer quicker when it restarts
if f.currentLayers.IsValid() {
for _, s := range f.exemptedLayers {
if s <= f.maxLayers.Spatial && f.currentLayers.Spatial == s {
targetLayers = f.currentLayers
bandwidthRequested = brs[targetLayers.Spatial][targetLayers.Temporal]
state = VideoAllocationStateDeficient
break
}
}
}
}
if f.targetLayers == InvalidLayers && targetLayers.IsValid() {
change = VideoStreamingChangeResuming
} else if f.targetLayers != InvalidLayers && !targetLayers.IsValid() {
change = VideoStreamingChangePausing
}
if !targetLayers.IsValid() && f.maxLayers.IsValid() {
state = VideoAllocationStateDeficient
}
} }
if !targetLayers.IsValid() { if !targetLayers.IsValid() {
@@ -594,12 +661,16 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation {
bandwidthDelta: bandwidthRequested - f.lastAllocation.bandwidthRequested, bandwidthDelta: bandwidthRequested - f.lastAllocation.bandwidthRequested,
bitrates: brs, bitrates: brs,
targetLayers: targetLayers, targetLayers: targetLayers,
distanceToDesired: f.getDistanceToDesired(brs, targetLayers), distanceToDesired: f.getDistanceToDesired(brs, targetLayers, f.maxLayers),
} }
if len(f.availableLayers) > 0 { if len(f.availableLayers) > 0 {
f.lastAllocation.availableLayers = make([]int32, len(f.availableLayers)) f.lastAllocation.availableLayers = make([]int32, len(f.availableLayers))
copy(f.lastAllocation.availableLayers, f.availableLayers) copy(f.lastAllocation.availableLayers, f.availableLayers)
} }
if len(f.exemptedLayers) > 0 {
f.lastAllocation.exemptedLayers = make([]int32, len(f.exemptedLayers))
copy(f.lastAllocation.exemptedLayers, f.exemptedLayers)
}
f.setTargetLayers(f.lastAllocation.targetLayers) f.setTargetLayers(f.lastAllocation.targetLayers)
if f.targetLayers == InvalidLayers { if f.targetLayers == InvalidLayers {
@@ -614,7 +685,7 @@ func (f *Forwarder) ProvisionalAllocatePrepare(bitrates Bitrates) {
defer f.lock.Unlock() defer f.lock.Unlock()
f.provisional = &VideoAllocationProvisional{ f.provisional = &VideoAllocationProvisional{
layers: InvalidLayers, allocatedLayers: InvalidLayers,
muted: f.muted, muted: f.muted,
bitrates: bitrates, bitrates: bitrates,
maxLayers: f.maxLayers, maxLayers: f.maxLayers,
@@ -623,41 +694,66 @@ func (f *Forwarder) ProvisionalAllocatePrepare(bitrates Bitrates) {
f.provisional.availableLayers = make([]int32, len(f.availableLayers)) f.provisional.availableLayers = make([]int32, len(f.availableLayers))
copy(f.provisional.availableLayers, f.availableLayers) copy(f.provisional.availableLayers, f.availableLayers)
} }
if len(f.exemptedLayers) > 0 {
f.provisional.exemptedLayers = make([]int32, len(f.exemptedLayers))
copy(f.provisional.exemptedLayers, f.exemptedLayers)
}
} }
func (f *Forwarder) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool) int64 { func (f *Forwarder) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool, allowOvershoot bool) int64 {
f.lock.Lock() f.lock.Lock()
defer f.lock.Unlock() defer f.lock.Unlock()
if f.provisional.muted || !f.provisional.maxLayers.IsValid() || layers.GreaterThan(f.provisional.maxLayers) { if f.provisional.muted || !f.provisional.maxLayers.IsValid() || (!allowOvershoot && layers.GreaterThan(f.provisional.maxLayers)) {
return 0 return 0
} }
maybeAdoptExempted := func() int64 {
br := int64(0)
if f.currentLayers.IsValid() {
for _, s := range f.provisional.exemptedLayers {
if s <= f.provisional.maxLayers.Spatial && f.currentLayers.Spatial == s {
f.provisional.allocatedLayers = f.currentLayers
br = f.provisional.bitrates[f.provisional.allocatedLayers.Spatial][f.provisional.allocatedLayers.Temporal]
break
}
}
}
return br
}
requiredBitrate := f.provisional.bitrates[layers.Spatial][layers.Temporal] requiredBitrate := f.provisional.bitrates[layers.Spatial][layers.Temporal]
if requiredBitrate == 0 { if requiredBitrate == 0 {
return 0 return maybeAdoptExempted()
} }
alreadyAllocatedBitrate := int64(0) alreadyAllocatedBitrate := int64(0)
if f.provisional.layers != InvalidLayers { if f.provisional.allocatedLayers != InvalidLayers {
alreadyAllocatedBitrate = f.provisional.bitrates[f.provisional.layers.Spatial][f.provisional.layers.Temporal] alreadyAllocatedBitrate = f.provisional.bitrates[f.provisional.allocatedLayers.Spatial][f.provisional.allocatedLayers.Temporal]
} }
if requiredBitrate <= (availableChannelCapacity + alreadyAllocatedBitrate) { // a layer under maximum fits, take it
f.provisional.layers = layers if !layers.GreaterThan(f.provisional.maxLayers) && requiredBitrate <= (availableChannelCapacity+alreadyAllocatedBitrate) {
f.provisional.allocatedLayers = layers
return requiredBitrate - alreadyAllocatedBitrate return requiredBitrate - alreadyAllocatedBitrate
} }
// when pause is disallowed, pick the layer if none allocated already or something lower is available //
if !allowPause && (f.provisional.layers == InvalidLayers || !layers.GreaterThan(f.provisional.layers)) { // Given layer does not fit. But overshoot is allowed.
f.provisional.layers = layers // Could be one of
// 1. a layer below maximum that does not fit
// 2. a layer above maximum which may or may not fit.
// In any of those cases, take the lowest possible layer if pause is not allowed
//
if !allowPause && (f.provisional.allocatedLayers == InvalidLayers || !layers.GreaterThan(f.provisional.allocatedLayers)) {
f.provisional.allocatedLayers = layers
return requiredBitrate - alreadyAllocatedBitrate return requiredBitrate - alreadyAllocatedBitrate
} }
return 0 return maybeAdoptExempted()
} }
func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransition { func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition(allowOvershoot bool) VideoTransition {
// //
// This is called when a track needs a change (could be mute/unmute, subscribed layers changed, published layers changed) // This is called when a track needs a change (could be mute/unmute, subscribed layers changed, published layers changed)
// when channel is congested. // when channel is congested.
@@ -681,7 +777,7 @@ func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransitio
defer f.lock.Unlock() defer f.lock.Unlock()
if f.provisional.muted { if f.provisional.muted {
f.provisional.layers = InvalidLayers f.provisional.allocatedLayers = InvalidLayers
return VideoTransition{ return VideoTransition{
from: f.targetLayers, from: f.targetLayers,
to: InvalidLayers, to: InvalidLayers,
@@ -710,9 +806,9 @@ func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransitio
} }
if maximalLayers != InvalidLayers { if maximalLayers != InvalidLayers {
if !f.targetLayers.GreaterThan(maximalLayers) && (f.provisional.bitrates[f.targetLayers.Spatial][f.targetLayers.Temporal] != 0) { if !f.targetLayers.GreaterThan(maximalLayers) && f.provisional.bitrates[f.targetLayers.Spatial][f.targetLayers.Temporal] != 0 {
// currently streaming and wanting an upgrade, just preserve current target in the cooperative scheme of things // currently streaming and wanting an upgrade, just preserve current target in the cooperative scheme of things
f.provisional.layers = f.targetLayers f.provisional.allocatedLayers = f.targetLayers
return VideoTransition{ return VideoTransition{
from: f.targetLayers, from: f.targetLayers,
to: f.targetLayers, to: f.targetLayers,
@@ -722,7 +818,7 @@ func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransitio
if f.targetLayers.GreaterThan(maximalLayers) { if f.targetLayers.GreaterThan(maximalLayers) {
// maximalLayers <= f.targetLayers, make the down move // maximalLayers <= f.targetLayers, make the down move
f.provisional.layers = maximalLayers f.provisional.allocatedLayers = maximalLayers
return VideoTransition{ return VideoTransition{
from: f.targetLayers, from: f.targetLayers,
to: maximalLayers, to: maximalLayers,
@@ -732,31 +828,66 @@ func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransitio
} }
} }
// currently not streaming, find minimal findNextLayer := func(
// NOTE: a layer in feed could have paused and there could be other options than going back to minimal, minSpatial, maxSpatial int32,
// but the cooperative scheme knocks things back to minimal minTemporal, maxTemporal int32,
minimalLayers := InvalidLayers ) (VideoLayers, int64) {
bandwidthRequired := int64(0) layers := InvalidLayers
for s := int32(0); s <= f.provisional.maxLayers.Spatial; s++ { bw := int64(0)
for t := int32(0); t <= f.provisional.maxLayers.Temporal; t++ { for s := minSpatial; s <= maxSpatial; s++ {
for t := minTemporal; t <= maxTemporal; t++ {
if f.provisional.bitrates[s][t] != 0 { if f.provisional.bitrates[s][t] != 0 {
minimalLayers = VideoLayers{Spatial: s, Temporal: t} layers = VideoLayers{Spatial: s, Temporal: t}
bandwidthRequired = f.provisional.bitrates[s][t] bw = f.provisional.bitrates[s][t]
break break
} }
} }
if bandwidthRequired != 0 { if bw != 0 {
break break
} }
} }
return layers, bw
}
targetLayers := f.targetLayers targetLayers := f.targetLayers
if targetLayers == InvalidLayers || targetLayers.GreaterThan(minimalLayers) || (f.provisional.bitrates[targetLayers.Spatial][targetLayers.Temporal] == 0) { bandwidthRequired := int64(0)
targetLayers = minimalLayers if targetLayers == InvalidLayers {
// currently not streaming, find minimal
// NOTE: a layer in feed could have paused and there could be other options than going back to minimal,
// but the cooperative scheme knocks things back to minimal
targetLayers, bandwidthRequired = findNextLayer(
0, f.provisional.maxLayers.Spatial,
0, f.provisional.maxLayers.Temporal,
)
} }
f.provisional.layers = targetLayers // could not find a minimal layer, overshoot if allowed
if bandwidthRequired == 0 && f.provisional.maxLayers.IsValid() && allowOvershoot {
targetLayers, bandwidthRequired = findNextLayer(
f.provisional.maxLayers.Spatial+1, DefaultMaxLayerSpatial,
0, DefaultMaxLayerTemporal,
)
}
// adopt exempted layer if current is at one of the exempted layers below maximum
if bandwidthRequired == 0 && f.provisional.maxLayers.IsValid() && f.currentLayers.IsValid() {
for _, s := range f.provisional.exemptedLayers {
if s <= f.provisional.maxLayers.Spatial && f.currentLayers.Spatial == s {
targetLayers = f.currentLayers
bandwidthRequired = f.provisional.bitrates[targetLayers.Spatial][targetLayers.Temporal]
break
}
}
}
// turn off if nothing found, not even an exempted layer to continue with
if bandwidthRequired == 0 && (!f.currentLayers.IsValid() || f.currentLayers != targetLayers) {
targetLayers = InvalidLayers
}
f.provisional.allocatedLayers = targetLayers
return VideoTransition{ return VideoTransition{
from: f.targetLayers, from: f.targetLayers,
to: targetLayers, to: targetLayers,
@@ -783,7 +914,7 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti
defer f.lock.Unlock() defer f.lock.Unlock()
if f.provisional.muted { if f.provisional.muted {
f.provisional.layers = InvalidLayers f.provisional.allocatedLayers = InvalidLayers
return VideoTransition{ return VideoTransition{
from: f.targetLayers, from: f.targetLayers,
to: InvalidLayers, to: InvalidLayers,
@@ -806,8 +937,23 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti
} }
if maxReachableLayerTemporal == InvalidLayerTemporal { if maxReachableLayerTemporal == InvalidLayerTemporal {
// stick to an exempted layer if available
if f.currentLayers.IsValid() {
for _, s := range f.provisional.exemptedLayers {
if s <= f.provisional.maxLayers.Spatial && f.currentLayers.Spatial == s {
f.provisional.allocatedLayers = f.currentLayers
return VideoTransition{
from: f.targetLayers,
to: f.provisional.allocatedLayers,
bandwidthDelta: 0 - f.lastAllocation.bandwidthRequested,
// LK-TODO should this take current bitrate of current target layers?
}
}
}
}
// feed has gone dry, // feed has gone dry,
f.provisional.layers = InvalidLayers f.provisional.allocatedLayers = InvalidLayers
return VideoTransition{ return VideoTransition{
from: f.targetLayers, from: f.targetLayers,
to: InvalidLayers, to: InvalidLayers,
@@ -848,7 +994,7 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti
} }
} }
f.provisional.layers = bestLayers f.provisional.allocatedLayers = bestLayers
return VideoTransition{ return VideoTransition{
from: f.targetLayers, from: f.targetLayers,
to: bestLayers, to: bestLayers,
@@ -870,17 +1016,23 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
case len(f.provisional.availableLayers) == 0: case len(f.provisional.availableLayers) == 0:
// feed is dry // feed is dry
state = VideoAllocationStateFeedDry state = VideoAllocationStateFeedDry
case f.provisional.layers == InvalidLayers: case f.provisional.allocatedLayers == InvalidLayers:
state = VideoAllocationStateDeficient state = VideoAllocationStateDeficient
if f.targetLayers != InvalidLayers { if f.targetLayers != InvalidLayers {
change = VideoStreamingChangePausing change = VideoStreamingChangePausing
} }
default: default:
bandwidthRequested = f.provisional.bitrates[f.provisional.layers.Spatial][f.provisional.layers.Temporal] optimalBandwidthNeeded := f.getOptimalBandwidthNeeded(f.provisional.bitrates, f.provisional.maxLayers)
if bandwidthRequested == f.getOptimalBandwidthNeeded(f.provisional.bitrates) { bandwidthRequested = f.provisional.bitrates[f.provisional.allocatedLayers.Spatial][f.provisional.allocatedLayers.Temporal]
if f.provisional.allocatedLayers.GreaterThan(f.provisional.maxLayers) || (optimalBandwidthNeeded > 0 && bandwidthRequested >= optimalBandwidthNeeded) {
// could be greater than optimal if overshooting
state = VideoAllocationStateOptimal state = VideoAllocationStateOptimal
} else { } else {
//
// Optimal bandwidth could be 0 if using exempted layer.
// Exempted layer is still treated as deficient.
//
state = VideoAllocationStateDeficient state = VideoAllocationStateDeficient
} }
@@ -895,13 +1047,17 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
bandwidthRequested: bandwidthRequested, bandwidthRequested: bandwidthRequested,
bandwidthDelta: bandwidthRequested - f.lastAllocation.bandwidthRequested, bandwidthDelta: bandwidthRequested - f.lastAllocation.bandwidthRequested,
bitrates: f.provisional.bitrates, bitrates: f.provisional.bitrates,
targetLayers: f.provisional.layers, targetLayers: f.provisional.allocatedLayers,
distanceToDesired: f.getDistanceToDesired(f.provisional.bitrates, f.provisional.layers), distanceToDesired: f.getDistanceToDesired(f.provisional.bitrates, f.provisional.allocatedLayers, f.provisional.maxLayers),
} }
if len(f.availableLayers) > 0 { if len(f.provisional.availableLayers) > 0 {
f.lastAllocation.availableLayers = make([]int32, len(f.availableLayers)) f.lastAllocation.availableLayers = make([]int32, len(f.provisional.availableLayers))
copy(f.lastAllocation.availableLayers, f.provisional.availableLayers) copy(f.lastAllocation.availableLayers, f.provisional.availableLayers)
} }
if len(f.provisional.exemptedLayers) > 0 {
f.lastAllocation.exemptedLayers = make([]int32, len(f.provisional.exemptedLayers))
copy(f.lastAllocation.exemptedLayers, f.provisional.exemptedLayers)
}
f.setTargetLayers(f.lastAllocation.targetLayers) f.setTargetLayers(f.lastAllocation.targetLayers)
if f.targetLayers == InvalidLayers { if f.targetLayers == InvalidLayers {
@@ -911,7 +1067,7 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
return f.lastAllocation return f.lastAllocation
} }
func (f *Forwarder) AllocateNextHigher(availableChannelCapacity int64, brs Bitrates) (VideoAllocation, bool) { func (f *Forwarder) AllocateNextHigher(availableChannelCapacity int64, brs Bitrates, allowOvershoot bool) (VideoAllocation, bool) {
f.lock.Lock() f.lock.Lock()
defer f.lock.Unlock() defer f.lock.Unlock()
@@ -932,69 +1088,34 @@ func (f *Forwarder) AllocateNextHigher(availableChannelCapacity int64, brs Bitra
return f.lastAllocation, false return f.lastAllocation, false
} }
optimalBandwidthNeeded := f.getOptimalBandwidthNeeded(brs) optimalBandwidthNeeded := f.getOptimalBandwidthNeeded(brs, f.maxLayers)
alreadyAllocated := int64(0) alreadyAllocated := int64(0)
if f.targetLayers != InvalidLayers { if f.targetLayers != InvalidLayers {
alreadyAllocated = brs[f.targetLayers.Spatial][f.targetLayers.Temporal] alreadyAllocated = brs[f.targetLayers.Spatial][f.targetLayers.Temporal]
} }
// try moving temporal layer up in currently streaming spatial layer doAllocation := func(
if f.targetLayers != InvalidLayers { minSpatial, maxSpatial int32,
for t := f.targetLayers.Temporal + 1; t <= f.maxLayers.Temporal; t++ { minTemporal, maxTemporal int32,
bandwidthRequested := brs[f.targetLayers.Spatial][t] ) (bool, VideoAllocation, bool) {
if bandwidthRequested == 0 { for s := minSpatial; s <= maxSpatial; s++ {
continue for t := minTemporal; t <= maxTemporal; t++ {
}
if bandwidthRequested-alreadyAllocated > availableChannelCapacity {
// next higher available layer does not fit, return
f.lastAllocation.change = VideoStreamingChangeNone
return f.lastAllocation, false
}
state := VideoAllocationStateOptimal
if bandwidthRequested != optimalBandwidthNeeded {
state = VideoAllocationStateDeficient
}
targetLayers := VideoLayers{Spatial: f.targetLayers.Spatial, Temporal: t}
f.lastAllocation = VideoAllocation{
state: state,
change: VideoStreamingChangeNone,
bandwidthRequested: bandwidthRequested,
bandwidthDelta: bandwidthRequested - alreadyAllocated,
bitrates: brs,
targetLayers: targetLayers,
distanceToDesired: f.getDistanceToDesired(brs, targetLayers),
}
if len(f.availableLayers) > 0 {
f.lastAllocation.availableLayers = make([]int32, len(f.availableLayers))
copy(f.lastAllocation.availableLayers, f.availableLayers)
}
f.setTargetLayers(f.lastAllocation.targetLayers)
return f.lastAllocation, true
}
}
// try moving spatial layer up if temporal layer move up is not available
for s := f.targetLayers.Spatial + 1; s <= f.maxLayers.Spatial; s++ {
for t := int32(0); t <= f.maxLayers.Temporal; t++ {
bandwidthRequested := brs[s][t] bandwidthRequested := brs[s][t]
if bandwidthRequested == 0 { if bandwidthRequested == 0 {
continue continue
} }
if bandwidthRequested-alreadyAllocated > availableChannelCapacity { if !allowOvershoot && bandwidthRequested-alreadyAllocated > availableChannelCapacity {
// next higher available layer does not fit, return // next higher available layer does not fit, return
f.lastAllocation.change = VideoStreamingChangeNone f.lastAllocation.change = VideoStreamingChangeNone
return f.lastAllocation, false return true, f.lastAllocation, false
} }
state := VideoAllocationStateOptimal targetLayers := VideoLayers{Spatial: s, Temporal: t}
if bandwidthRequested != optimalBandwidthNeeded { state := VideoAllocationStateDeficient
state = VideoAllocationStateDeficient if targetLayers.GreaterThan(f.maxLayers) || (optimalBandwidthNeeded > 0 && bandwidthRequested >= optimalBandwidthNeeded) {
state = VideoAllocationStateOptimal
} }
change := VideoStreamingChangeNone change := VideoStreamingChangeNone
@@ -1002,7 +1123,6 @@ func (f *Forwarder) AllocateNextHigher(availableChannelCapacity int64, brs Bitra
change = VideoStreamingChangeResuming change = VideoStreamingChangeResuming
} }
targetLayers := VideoLayers{Spatial: s, Temporal: t}
f.lastAllocation = VideoAllocation{ f.lastAllocation = VideoAllocation{
state: state, state: state,
change: change, change: change,
@@ -1010,15 +1130,56 @@ func (f *Forwarder) AllocateNextHigher(availableChannelCapacity int64, brs Bitra
bandwidthDelta: bandwidthRequested - alreadyAllocated, bandwidthDelta: bandwidthRequested - alreadyAllocated,
bitrates: brs, bitrates: brs,
targetLayers: targetLayers, targetLayers: targetLayers,
distanceToDesired: f.getDistanceToDesired(brs, targetLayers), distanceToDesired: f.getDistanceToDesired(brs, targetLayers, f.maxLayers),
} }
if len(f.availableLayers) > 0 { if len(f.availableLayers) > 0 {
f.lastAllocation.availableLayers = make([]int32, len(f.availableLayers)) f.lastAllocation.availableLayers = make([]int32, len(f.availableLayers))
copy(f.lastAllocation.availableLayers, f.availableLayers) copy(f.lastAllocation.availableLayers, f.availableLayers)
} }
if len(f.exemptedLayers) > 0 {
f.lastAllocation.exemptedLayers = make([]int32, len(f.exemptedLayers))
copy(f.lastAllocation.exemptedLayers, f.exemptedLayers)
}
f.setTargetLayers(f.lastAllocation.targetLayers) f.setTargetLayers(f.lastAllocation.targetLayers)
return f.lastAllocation, true return true, f.lastAllocation, true
}
}
return false, VideoAllocation{}, false
}
done := false
var allocation VideoAllocation
boosted := false
// try moving temporal layer up in currently streaming spatial layer
if f.targetLayers != InvalidLayers {
done, allocation, boosted = doAllocation(
f.targetLayers.Spatial, f.targetLayers.Spatial,
f.targetLayers.Temporal+1, f.maxLayers.Temporal,
)
if done {
return allocation, boosted
}
}
// try moving spatial layer up if temporal layer move up is not available
done, allocation, boosted = doAllocation(
f.targetLayers.Spatial+1, f.maxLayers.Spatial,
0, f.maxLayers.Temporal,
)
if done {
return allocation, boosted
}
if allowOvershoot && f.maxLayers.IsValid() {
done, allocation, boosted = doAllocation(
f.maxLayers.Spatial+1, DefaultMaxLayerSpatial,
0, DefaultMaxLayerTemporal,
)
if done {
return allocation, boosted
} }
} }
@@ -1026,7 +1187,7 @@ func (f *Forwarder) AllocateNextHigher(availableChannelCapacity int64, brs Bitra
return f.lastAllocation, false return f.lastAllocation, false
} }
func (f *Forwarder) GetNextHigherTransition(brs Bitrates) (VideoTransition, bool) { func (f *Forwarder) GetNextHigherTransition(brs Bitrates, allowOvershoot bool) (VideoTransition, bool) {
f.lock.Lock() f.lock.Lock()
defer f.lock.Unlock() defer f.lock.Unlock()
@@ -1049,27 +1210,12 @@ func (f *Forwarder) GetNextHigherTransition(brs Bitrates) (VideoTransition, bool
alreadyAllocated = brs[f.targetLayers.Spatial][f.targetLayers.Temporal] alreadyAllocated = brs[f.targetLayers.Spatial][f.targetLayers.Temporal]
} }
// try moving temporal layer up in currently streaming spatial layer findNextHigher := func(
if f.targetLayers != InvalidLayers { minSpatial, maxSpatial int32,
for t := f.targetLayers.Temporal + 1; t <= f.maxLayers.Temporal; t++ { minTemporal, maxTemporal int32,
bandwidthRequested := brs[f.targetLayers.Spatial][t] ) (bool, VideoTransition, bool) {
if bandwidthRequested == 0 { for s := minSpatial; s <= maxSpatial; s++ {
continue for t := minTemporal; t <= maxTemporal; t++ {
}
transition := VideoTransition{
from: f.targetLayers,
to: VideoLayers{Spatial: f.targetLayers.Spatial, Temporal: t},
bandwidthDelta: bandwidthRequested - alreadyAllocated,
}
return transition, true
}
}
// try moving spatial layer up if temporal layer move up is not available
for s := f.targetLayers.Spatial + 1; s <= f.maxLayers.Spatial; s++ {
for t := int32(0); t <= f.maxLayers.Temporal; t++ {
bandwidthRequested := brs[s][t] bandwidthRequested := brs[s][t]
if bandwidthRequested == 0 { if bandwidthRequested == 0 {
continue continue
@@ -1081,7 +1227,44 @@ func (f *Forwarder) GetNextHigherTransition(brs Bitrates) (VideoTransition, bool
bandwidthDelta: bandwidthRequested - alreadyAllocated, bandwidthDelta: bandwidthRequested - alreadyAllocated,
} }
return transition, true return true, transition, true
}
}
return false, VideoTransition{}, false
}
done := false
var transition VideoTransition
isAvailable := false
// try moving temporal layer up in currently streaming spatial layer
if f.targetLayers != InvalidLayers {
done, transition, isAvailable = findNextHigher(
f.targetLayers.Spatial, f.targetLayers.Spatial,
f.targetLayers.Temporal+1, f.maxLayers.Temporal,
)
if done {
return transition, isAvailable
}
}
// try moving spatial layer up if temporal layer move up is not available
done, transition, isAvailable = findNextHigher(
f.targetLayers.Spatial+1, f.maxLayers.Spatial,
0, f.maxLayers.Temporal,
)
if done {
return transition, isAvailable
}
if allowOvershoot && f.maxLayers.IsValid() {
done, transition, isAvailable = findNextHigher(
f.maxLayers.Spatial+1, DefaultMaxLayerSpatial,
0, DefaultMaxLayerTemporal,
)
if done {
return transition, isAvailable
} }
} }
@@ -1117,12 +1300,16 @@ func (f *Forwarder) Pause(brs Bitrates) VideoAllocation {
bandwidthDelta: 0 - f.lastAllocation.bandwidthRequested, bandwidthDelta: 0 - f.lastAllocation.bandwidthRequested,
bitrates: brs, bitrates: brs,
targetLayers: InvalidLayers, targetLayers: InvalidLayers,
distanceToDesired: f.getDistanceToDesired(brs, InvalidLayers), distanceToDesired: f.getDistanceToDesired(brs, InvalidLayers, f.maxLayers),
} }
if len(f.availableLayers) > 0 { if len(f.availableLayers) > 0 {
f.lastAllocation.availableLayers = make([]int32, len(f.availableLayers)) f.lastAllocation.availableLayers = make([]int32, len(f.availableLayers))
copy(f.lastAllocation.availableLayers, f.availableLayers) copy(f.lastAllocation.availableLayers, f.availableLayers)
} }
if len(f.exemptedLayers) > 0 {
f.lastAllocation.exemptedLayers = make([]int32, len(f.exemptedLayers))
copy(f.lastAllocation.exemptedLayers, f.exemptedLayers)
}
f.setTargetLayers(f.lastAllocation.targetLayers) f.setTargetLayers(f.lastAllocation.targetLayers)
if f.targetLayers == InvalidLayers { if f.targetLayers == InvalidLayers {
+414 -43
View File
@@ -121,7 +121,7 @@ func TestForwarderGetForwardingStatus(t *testing.T) {
// with available layers, should be off // with available layers, should be off
availableLayers := []int32{0, 1, 2} availableLayers := []int32{0, 1, 2}
f.UpTrackLayersChange(availableLayers) f.UpTrackLayersChange(availableLayers, []int32{})
require.Equal(t, ForwardingStatusOff, f.GetForwardingStatus()) require.Equal(t, ForwardingStatusOff, f.GetForwardingStatus())
// when muted, should be optimal // when muted, should be optimal
@@ -139,7 +139,7 @@ func TestForwarderGetForwardingStatus(t *testing.T) {
// when available layers are lower than max subscribed, optimal as long as target is at max available // when available layers are lower than max subscribed, optimal as long as target is at max available
availableLayers = []int32{0, 1} availableLayers = []int32{0, 1}
f.UpTrackLayersChange(availableLayers) f.UpTrackLayersChange(availableLayers, []int32{})
require.Equal(t, ForwardingStatusOptimal, f.GetForwardingStatus()) require.Equal(t, ForwardingStatusOptimal, f.GetForwardingStatus())
} }
@@ -149,19 +149,24 @@ func TestForwarderUpTrackLayersChange(t *testing.T) {
require.Nil(t, f.availableLayers) require.Nil(t, f.availableLayers)
availableLayers := []int32{0, 1, 2} availableLayers := []int32{0, 1, 2}
f.UpTrackLayersChange(availableLayers) exemptedLayers := []int32{0}
f.UpTrackLayersChange(availableLayers, exemptedLayers)
require.Equal(t, availableLayers, f.availableLayers) require.Equal(t, availableLayers, f.availableLayers)
require.Equal(t, exemptedLayers, f.exemptedLayers)
availableLayers = []int32{0, 2} availableLayers = []int32{0, 2}
f.UpTrackLayersChange(availableLayers) f.UpTrackLayersChange(availableLayers, []int32{})
require.Equal(t, availableLayers, f.availableLayers) require.Equal(t, availableLayers, f.availableLayers)
require.Nil(t, f.exemptedLayers)
availableLayers = []int32{} availableLayers = []int32{}
f.UpTrackLayersChange(availableLayers) exemptedLayers = []int32{}
f.UpTrackLayersChange(availableLayers, exemptedLayers)
require.Nil(t, f.availableLayers) require.Nil(t, f.availableLayers)
require.Nil(t, f.exemptedLayers)
} }
func TestForwarderAllocate(t *testing.T) { func TestForwarderAllocateOptimal(t *testing.T) {
f := newForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo) f := newForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo)
f.SetMaxSpatialLayer(DefaultMaxLayerSpatial) f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
f.SetMaxTemporalLayer(DefaultMaxLayerTemporal) f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
@@ -186,7 +191,7 @@ func TestForwarderAllocate(t *testing.T) {
targetLayers: InvalidLayers, targetLayers: InvalidLayers,
distanceToDesired: 0, distanceToDesired: 0,
} }
result := f.AllocateOptimal(bitrates) result := f.AllocateOptimal(bitrates, true)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
@@ -204,14 +209,14 @@ func TestForwarderAllocate(t *testing.T) {
targetLayers: InvalidLayers, targetLayers: InvalidLayers,
distanceToDesired: 0, distanceToDesired: 0,
} }
result = f.AllocateOptimal(emptyBitrates) result = f.AllocateOptimal(emptyBitrates, true)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
// awaiting measurement, i.e. bitrates are not available, but layers available // awaiting measurement, i.e. bitrates are not available, but layers available
f.lastAllocation.state = VideoAllocationStateNone f.lastAllocation.state = VideoAllocationStateNone
disable(f) disable(f)
f.UpTrackLayersChange([]int32{0}) f.UpTrackLayersChange([]int32{0}, []int32{})
expectedTargetLayers := VideoLayers{ expectedTargetLayers := VideoLayers{
Spatial: 0, Spatial: 0,
Temporal: DefaultMaxLayerTemporal, Temporal: DefaultMaxLayerTemporal,
@@ -226,14 +231,58 @@ func TestForwarderAllocate(t *testing.T) {
targetLayers: expectedTargetLayers, targetLayers: expectedTargetLayers,
distanceToDesired: 0, distanceToDesired: 0,
} }
result = f.AllocateOptimal(emptyBitrates) result = f.AllocateOptimal(emptyBitrates, true)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.Equal(t, InvalidLayers, f.CurrentLayers()) require.Equal(t, InvalidLayers, f.CurrentLayers())
// layers are available, but all layers under max are exempted
f.SetMaxSpatialLayer(0)
f.currentLayers = VideoLayers{Spatial: 0, Temporal: 0}
f.UpTrackLayersChange([]int32{0, 1, 2}, []int32{0})
expectedTargetLayers = VideoLayers{
Spatial: 0,
Temporal: 0,
}
expectedResult = VideoAllocation{
state: VideoAllocationStateDeficient,
change: VideoStreamingChangeNone,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: []int32{0, 1, 2},
exemptedLayers: []int32{0},
bitrates: emptyBitrates,
targetLayers: expectedTargetLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(emptyBitrates, true)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// if current is not exempt, should not choose that
f.currentLayers = VideoLayers{Spatial: 1, Temporal: 0}
expectedResult = VideoAllocation{
state: VideoAllocationStateDeficient,
change: VideoStreamingChangePausing,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: []int32{0, 1, 2},
exemptedLayers: []int32{0},
bitrates: emptyBitrates,
targetLayers: InvalidLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(emptyBitrates, true)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.TargetLayers())
// allocate using bitrates, allocation should choose optimal // allocate using bitrates, allocation should choose optimal
f.UpTrackLayersChange([]int32{0, 1, 2}) f.currentLayers = InvalidLayers
f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
f.UpTrackLayersChange([]int32{0, 1, 2}, []int32{})
f.maxLayers = VideoLayers{Spatial: 1, Temporal: 3} f.maxLayers = VideoLayers{Spatial: 1, Temporal: 3}
expectedTargetLayers = VideoLayers{ expectedTargetLayers = VideoLayers{
Spatial: 1, Spatial: 1,
@@ -241,7 +290,7 @@ func TestForwarderAllocate(t *testing.T) {
} }
expectedResult = VideoAllocation{ expectedResult = VideoAllocation{
state: VideoAllocationStateOptimal, state: VideoAllocationStateOptimal,
change: VideoStreamingChangeNone, change: VideoStreamingChangeResuming,
bandwidthRequested: bitrates[1][3], bandwidthRequested: bitrates[1][3],
bandwidthDelta: bitrates[1][3], bandwidthDelta: bitrates[1][3],
availableLayers: []int32{0, 1, 2}, availableLayers: []int32{0, 1, 2},
@@ -249,14 +298,14 @@ func TestForwarderAllocate(t *testing.T) {
targetLayers: expectedTargetLayers, targetLayers: expectedTargetLayers,
distanceToDesired: 0, distanceToDesired: 0,
} }
result = f.AllocateOptimal(bitrates) result = f.AllocateOptimal(bitrates, true)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers()) require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
// allocate using bitrates above maximum layer // allocate using bitrates above maximum layer, allowing overshoot
f.UpTrackLayersChange([]int32{2}) f.UpTrackLayersChange([]int32{2}, []int32{})
sparseBitrates := Bitrates{ sparseBitrates := Bitrates{
{0, 0, 0, 0}, {0, 0, 0, 0},
{0, 0, 0, 0}, {0, 0, 0, 0},
@@ -274,13 +323,30 @@ func TestForwarderAllocate(t *testing.T) {
availableLayers: []int32{2}, availableLayers: []int32{2},
bitrates: sparseBitrates, bitrates: sparseBitrates,
targetLayers: expectedTargetLayers, targetLayers: expectedTargetLayers,
distanceToDesired: 0, distanceToDesired: -1,
} }
result = f.AllocateOptimal(sparseBitrates) result = f.AllocateOptimal(sparseBitrates, true)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers()) require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
// when not allowing overshoot, should not be able to find a layer
expectedResult = VideoAllocation{
state: VideoAllocationStateDeficient,
change: VideoStreamingChangePausing,
bandwidthRequested: 0,
bandwidthDelta: -sparseBitrates[2][1],
availableLayers: []int32{2},
bitrates: sparseBitrates,
targetLayers: InvalidLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(sparseBitrates, false)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, InvalidLayers, f.TargetLayers())
} }
func TestForwarderProvisionalAllocate(t *testing.T) { func TestForwarderProvisionalAllocate(t *testing.T) {
@@ -298,20 +364,20 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
f.availableLayers = availableLayers f.availableLayers = availableLayers
f.ProvisionalAllocatePrepare(bitrates) f.ProvisionalAllocatePrepare(bitrates)
usedBitrate := f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true) usedBitrate := f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true, false)
require.Equal(t, bitrates[0][0], usedBitrate) require.Equal(t, bitrates[0][0], usedBitrate)
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 2, Temporal: 3}, true) usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 2, Temporal: 3}, true, false)
require.Equal(t, bitrates[2][3]-bitrates[0][0], usedBitrate) require.Equal(t, bitrates[2][3]-bitrates[0][0], usedBitrate)
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 3}, true) usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 3}, true, false)
require.Equal(t, bitrates[0][3]-bitrates[2][3], usedBitrate) require.Equal(t, bitrates[0][3]-bitrates[2][3], usedBitrate)
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 1, Temporal: 2}, true) usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 1, Temporal: 2}, true, false)
require.Equal(t, bitrates[1][2]-bitrates[0][3], usedBitrate) require.Equal(t, bitrates[1][2]-bitrates[0][3], usedBitrate)
// available not enough to reach (2, 2), allocating at (2, 2) should not succeed // available not enough to reach (2, 2), allocating at (2, 2) should not succeed
usedBitrate = f.ProvisionalAllocate(bitrates[2][2]-bitrates[1][2]-1, VideoLayers{Spatial: 2, Temporal: 2}, true) usedBitrate = f.ProvisionalAllocate(bitrates[2][2]-bitrates[1][2]-1, VideoLayers{Spatial: 2, Temporal: 2}, true, false)
require.Equal(t, int64(0), usedBitrate) require.Equal(t, int64(0), usedBitrate)
// committing should set target to (1, 2) // committing should set target to (1, 2)
@@ -337,7 +403,7 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
// when nothing fits and pausing disallowed, should allocate (0, 0) // when nothing fits and pausing disallowed, should allocate (0, 0)
f.targetLayers = InvalidLayers f.targetLayers = InvalidLayers
f.ProvisionalAllocatePrepare(bitrates) f.ProvisionalAllocatePrepare(bitrates)
usedBitrate = f.ProvisionalAllocate(0, VideoLayers{Spatial: 0, Temporal: 0}, false) usedBitrate = f.ProvisionalAllocate(0, VideoLayers{Spatial: 0, Temporal: 0}, false, false)
require.Equal(t, int64(1), usedBitrate) require.Equal(t, int64(1), usedBitrate)
// committing should set target to (0, 0) // committing should set target to (0, 0)
@@ -359,6 +425,153 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
//
// Test allowOvershoot.
// Max spatial set to 0 and layer 0 bit rates are not available.
// Layer 0 is exempted layer.
//
f.SetMaxSpatialLayer(0)
availableLayers = []int32{0, 1, 2}
exemptedLayers := []int32{0}
bitrates = Bitrates{
{0, 0, 0, 0},
{5, 6, 7, 8},
{9, 10, 11, 12},
}
f.availableLayers = availableLayers
f.exemptedLayers = exemptedLayers
f.ProvisionalAllocatePrepare(bitrates)
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, false, true)
require.Equal(t, int64(0), usedBitrate)
// overshoot should succeed
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 2, Temporal: 3}, false, true)
require.Equal(t, bitrates[2][3], usedBitrate)
// overshoot should succeed - this should win as this is lesser overshoot
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 1, Temporal: 3}, false, true)
require.Equal(t, bitrates[1][3]-bitrates[2][3], usedBitrate)
// committing should set target to (1, 3)
expectedTargetLayers = VideoLayers{
Spatial: 1,
Temporal: 3,
}
expectedResult = VideoAllocation{
state: VideoAllocationStateOptimal,
change: VideoStreamingChangeNone,
bandwidthRequested: bitrates[1][3],
bandwidthDelta: bitrates[1][3] - 1, // 1 is the last allocation bandwith requested
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
distanceToDesired: -4,
}
result = f.ProvisionalAllocateCommit()
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
//
// Test exemptedLayers
// Even if overshoot is allowed, but higher layers do not have bit rates and the current layer is empted,,
// should continue with current layer.
//
f.SetMaxSpatialLayer(0)
availableLayers = []int32{0, 1, 2}
exemptedLayers = []int32{0}
bitrates = Bitrates{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
}
f.availableLayers = availableLayers
f.exemptedLayers = exemptedLayers
f.currentLayers = VideoLayers{Spatial: 0, Temporal: 2}
f.ProvisionalAllocatePrepare(bitrates)
// all the provisional allocations below should fall back to exempted layer
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, false, true)
require.Equal(t, int64(0), usedBitrate)
// overshoot should not succeed
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 2, Temporal: 3}, false, true)
require.Equal(t, int64(0), usedBitrate)
// overshoot should not succeed
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 1, Temporal: 3}, false, true)
require.Equal(t, int64(0), usedBitrate)
// committing should set target to (0, 2)
expectedTargetLayers = VideoLayers{
Spatial: 0,
Temporal: 2,
}
expectedResult = VideoAllocation{
state: VideoAllocationStateDeficient,
change: VideoStreamingChangeNone,
bandwidthRequested: bitrates[0][2],
bandwidthDelta: bitrates[0][2] - 8, // 8 is the last allocation bandwith requested
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
distanceToDesired: 0,
}
result = f.ProvisionalAllocateCommit()
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
//
// Test currentLayers not in exemptedLayers
//
f.SetMaxSpatialLayer(0)
availableLayers = []int32{0, 1, 2}
exemptedLayers = []int32{0}
bitrates = Bitrates{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
}
f.availableLayers = availableLayers
f.exemptedLayers = exemptedLayers
f.currentLayers = VideoLayers{Spatial: 1, Temporal: 2}
f.ProvisionalAllocatePrepare(bitrates)
// all the provisional allocations below should fall back to exempted layer
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, false, true)
require.Equal(t, int64(0), usedBitrate)
// overshoot should not succeed
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 2, Temporal: 3}, false, true)
require.Equal(t, int64(0), usedBitrate)
// overshoot should not succeed
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 1, Temporal: 3}, false, true)
require.Equal(t, int64(0), usedBitrate)
expectedResult = VideoAllocation{
state: VideoAllocationStateDeficient,
change: VideoStreamingChangePausing,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: InvalidLayers,
distanceToDesired: 0,
}
result = f.ProvisionalAllocateCommit()
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.TargetLayers())
} }
func TestForwarderProvisionalAllocateMute(t *testing.T) { func TestForwarderProvisionalAllocateMute(t *testing.T) {
@@ -373,10 +586,10 @@ func TestForwarderProvisionalAllocateMute(t *testing.T) {
f.Mute(true) f.Mute(true)
f.ProvisionalAllocatePrepare(bitrates) f.ProvisionalAllocatePrepare(bitrates)
usedBitrate := f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true) usedBitrate := f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true, false)
require.Equal(t, int64(0), usedBitrate) require.Equal(t, int64(0), usedBitrate)
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 1, Temporal: 2}, true) usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 1, Temporal: 2}, true, true)
require.Equal(t, int64(0), usedBitrate) require.Equal(t, int64(0), usedBitrate)
// committing should set target to InvalidLayers as track is muted // committing should set target to InvalidLayers as track is muted
@@ -417,7 +630,7 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
to: VideoLayers{Spatial: 0, Temporal: 0}, to: VideoLayers{Spatial: 0, Temporal: 0},
bandwidthDelta: 1, bandwidthDelta: 1,
} }
transition := f.ProvisionalAllocateGetCooperativeTransition() transition := f.ProvisionalAllocateGetCooperativeTransition(false)
require.Equal(t, expectedTransition, transition) require.Equal(t, expectedTransition, transition)
// committing should set target to (0, 0) // committing should set target to (0, 0)
@@ -446,7 +659,7 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
to: targetLayers, to: targetLayers,
bandwidthDelta: 0, bandwidthDelta: 0,
} }
transition = f.ProvisionalAllocateGetCooperativeTransition() transition = f.ProvisionalAllocateGetCooperativeTransition(false)
require.Equal(t, expectedTransition, transition) require.Equal(t, expectedTransition, transition)
// committing should set target to (2, 1) // committing should set target to (2, 1)
@@ -474,7 +687,7 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
to: VideoLayers{Spatial: 2, Temporal: 1}, to: VideoLayers{Spatial: 2, Temporal: 1},
bandwidthDelta: 0, bandwidthDelta: 0,
} }
transition = f.ProvisionalAllocateGetCooperativeTransition() transition = f.ProvisionalAllocateGetCooperativeTransition(false)
require.Equal(t, expectedTransition, transition) require.Equal(t, expectedTransition, transition)
f.ProvisionalAllocateCommit() f.ProvisionalAllocateCommit()
@@ -489,8 +702,133 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
to: InvalidLayers, to: InvalidLayers,
bandwidthDelta: -10, bandwidthDelta: -10,
} }
transition = f.ProvisionalAllocateGetCooperativeTransition() transition = f.ProvisionalAllocateGetCooperativeTransition(false)
require.Equal(t, expectedTransition, transition) require.Equal(t, expectedTransition, transition)
f.ProvisionalAllocateCommit()
//
// Test allowOvershoot
//
f.Mute(false)
f.SetMaxSpatialLayer(0)
availableLayers = []int32{0, 1, 2}
exemptedLayers := []int32{0}
bitrates = Bitrates{
{0, 0, 0, 0},
{5, 6, 7, 8},
{9, 10, 0, 0},
}
f.targetLayers = InvalidLayers
f.availableLayers = availableLayers
f.exemptedLayers = exemptedLayers
f.ProvisionalAllocatePrepare(bitrates)
// from scratch (InvalidLayers) should go to a layer past maximum as overshoot is allowed
expectedTransition = VideoTransition{
from: InvalidLayers,
to: VideoLayers{Spatial: 1, Temporal: 0},
bandwidthDelta: 5,
}
transition = f.ProvisionalAllocateGetCooperativeTransition(true)
require.Equal(t, expectedTransition, transition)
// committing should set target to (1, 0)
expectedLayers = VideoLayers{Spatial: 1, Temporal: 0}
expectedResult = VideoAllocation{
state: VideoAllocationStateOptimal,
change: VideoStreamingChangeResuming,
bandwidthRequested: 5,
bandwidthDelta: 5,
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: expectedLayers,
distanceToDesired: -1,
}
result = f.ProvisionalAllocateCommit()
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedLayers, f.TargetLayers())
// Test exemptedLayers matching current layers
f.SetMaxSpatialLayer(0)
availableLayers = []int32{0, 1, 2}
exemptedLayers = []int32{0}
bitrates = Bitrates{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
}
f.currentLayers = VideoLayers{Spatial: 0, Temporal: 2}
f.targetLayers = InvalidLayers
f.availableLayers = availableLayers
f.exemptedLayers = exemptedLayers
f.ProvisionalAllocatePrepare(bitrates)
// from scratch (InvalidLayers) should go to an exempted layer
// NOTE: targetLayer is set to InvalidLayers for testing, but in practice current layers valid and target layers invalid should not happen
expectedTransition = VideoTransition{
from: InvalidLayers,
to: VideoLayers{Spatial: 0, Temporal: 2},
bandwidthDelta: -5, // 5 was the bandwidth needed for the last allocation
}
transition = f.ProvisionalAllocateGetCooperativeTransition(true)
require.Equal(t, expectedTransition, transition)
// committing should set target to (0, 2)
expectedLayers = VideoLayers{Spatial: 0, Temporal: 2}
expectedResult = VideoAllocation{
state: VideoAllocationStateDeficient,
change: VideoStreamingChangeResuming,
bandwidthRequested: 0,
bandwidthDelta: -5,
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: expectedLayers,
distanceToDesired: 0,
}
result = f.ProvisionalAllocateCommit()
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedLayers, f.TargetLayers())
// Test exemptedLayers not matching current layers
f.currentLayers = VideoLayers{Spatial: 2, Temporal: 2}
f.targetLayers = f.currentLayers
f.availableLayers = availableLayers
f.exemptedLayers = exemptedLayers
f.ProvisionalAllocatePrepare(bitrates)
expectedTransition = VideoTransition{
from: VideoLayers{Spatial: 2, Temporal: 2},
to: InvalidLayers,
bandwidthDelta: 0,
}
transition = f.ProvisionalAllocateGetCooperativeTransition(true)
require.Equal(t, expectedTransition, transition)
// committing should set target to InvalidLayers
expectedResult = VideoAllocation{
state: VideoAllocationStateDeficient,
change: VideoStreamingChangePausing,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: InvalidLayers,
distanceToDesired: 0,
}
result = f.ProvisionalAllocateCommit()
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.TargetLayers())
} }
func TestForwarderProvisionalAllocateGetBestWeightedTransition(t *testing.T) { func TestForwarderProvisionalAllocateGetBestWeightedTransition(t *testing.T) {
@@ -529,7 +867,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
{0, 7, 0, 0}, {0, 7, 0, 0},
} }
result, boosted := f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted := f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, VideoAllocationDefault, result) // no layer for audio require.Equal(t, VideoAllocationDefault, result) // no layer for audio
require.False(t, boosted) require.False(t, boosted)
@@ -539,7 +877,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
// when not in deficient state, does not boost // when not in deficient state, does not boost
f.lastAllocation.state = VideoAllocationStateNone f.lastAllocation.state = VideoAllocationStateNone
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, VideoAllocationDefault, result) require.Equal(t, VideoAllocationDefault, result)
require.False(t, boosted) require.False(t, boosted)
@@ -556,14 +894,14 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
targetLayers: InvalidLayers, targetLayers: InvalidLayers,
distanceToDesired: 0, distanceToDesired: 0,
} }
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.False(t, boosted) require.False(t, boosted)
f.currentLayers.Spatial = 0 f.currentLayers.Spatial = 0
f.targetLayers.Temporal = 0 f.targetLayers.Temporal = 0
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.False(t, boosted) require.False(t, boosted)
@@ -582,7 +920,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
targetLayers: InvalidLayers, targetLayers: InvalidLayers,
distanceToDesired: 0, distanceToDesired: 0,
} }
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, emptyBitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, emptyBitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.False(t, boosted) require.False(t, boosted)
@@ -602,7 +940,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
targetLayers: expectedTargetLayers, targetLayers: expectedTargetLayers,
distanceToDesired: 3, distanceToDesired: 3,
} }
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
@@ -624,7 +962,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
targetLayers: expectedTargetLayers, targetLayers: expectedTargetLayers,
distanceToDesired: 2, distanceToDesired: 2,
} }
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
@@ -647,7 +985,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
targetLayers: expectedTargetLayers, targetLayers: expectedTargetLayers,
distanceToDesired: 1, distanceToDesired: 1,
} }
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
@@ -669,7 +1007,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
targetLayers: expectedTargetLayers, targetLayers: expectedTargetLayers,
distanceToDesired: 0, distanceToDesired: 0,
} }
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
@@ -678,7 +1016,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
// ask again, should return not boosted as there is no room to go higher // ask again, should return not boosted as there is no room to go higher
f.currentLayers.Spatial = 2 f.currentLayers.Spatial = 2
f.currentLayers.Temporal = 1 f.currentLayers.Temporal = 1
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
@@ -703,7 +1041,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
targetLayers: expectedTargetLayers, targetLayers: expectedTargetLayers,
distanceToDesired: 4, distanceToDesired: 4,
} }
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates) result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
@@ -720,11 +1058,44 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
targetLayers: expectedTargetLayers, targetLayers: expectedTargetLayers,
distanceToDesired: 4, distanceToDesired: 4,
} }
result, boosted = f.AllocateNextHigher(0, bitrates) result, boosted = f.AllocateNextHigher(0, bitrates, false)
require.Equal(t, expectedResult, result) require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation) require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers()) require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.False(t, boosted) require.False(t, boosted)
// test allowOvershoot
f.SetMaxSpatialLayer(0)
bitrates = Bitrates{
{0, 0, 0, 0},
{5, 6, 7, 8},
{9, 10, 11, 12},
}
f.currentLayers = f.targetLayers
expectedTargetLayers = VideoLayers{
Spatial: 1,
Temporal: 0,
}
expectedResult = VideoAllocation{
state: VideoAllocationStateOptimal,
change: VideoStreamingChangeNone,
bandwidthRequested: bitrates[1][0],
bandwidthDelta: bitrates[1][0],
availableLayers: nil,
exemptedLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
distanceToDesired: -1,
}
// overshoot should return (1, 0) even if there is not enough capacity
result, boosted = f.AllocateNextHigher(bitrates[1][0]-1, bitrates, true)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.True(t, boosted)
} }
func TestForwarderPause(t *testing.T) { func TestForwarderPause(t *testing.T) {
@@ -741,7 +1112,7 @@ func TestForwarderPause(t *testing.T) {
f.availableLayers = availableLayers f.availableLayers = availableLayers
f.ProvisionalAllocatePrepare(bitrates) f.ProvisionalAllocatePrepare(bitrates)
f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true) f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true, false)
// should have set target at (0, 0) // should have set target at (0, 0)
f.ProvisionalAllocateCommit() f.ProvisionalAllocateCommit()
@@ -775,7 +1146,7 @@ func TestForwarderPauseMute(t *testing.T) {
f.availableLayers = availableLayers f.availableLayers = availableLayers
f.ProvisionalAllocatePrepare(bitrates) f.ProvisionalAllocatePrepare(bitrates)
f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true) f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true, true)
// should have set target at (0, 0) // should have set target at (0, 0)
f.ProvisionalAllocateCommit() f.ProvisionalAllocateCommit()
+4 -6
View File
@@ -381,10 +381,8 @@ func (w *WebRTCReceiver) AddDownTrack(track TrackSender) error {
if w.Kind() == webrtc.RTPCodecTypeVideo { if w.Kind() == webrtc.RTPCodecTypeVideo {
// notify added down track of available layers // notify added down track of available layers
layers := w.streamTrackerManager.GetAvailableLayers() availableLayers, exemptedLayers := w.streamTrackerManager.GetAvailableLayers()
if len(layers) != 0 { track.UpTrackLayersChange(availableLayers, exemptedLayers)
track.UpTrackLayersChange(layers)
}
} }
track.TrackInfoAvailable() track.TrackInfoAvailable()
@@ -396,9 +394,9 @@ func (w *WebRTCReceiver) SetMaxExpectedSpatialLayer(layer int32) {
w.streamTrackerManager.SetMaxExpectedSpatialLayer(layer) w.streamTrackerManager.SetMaxExpectedSpatialLayer(layer)
} }
func (w *WebRTCReceiver) downTrackLayerChange(layers []int32) { func (w *WebRTCReceiver) downTrackLayerChange(availableLayers []int32, exemptedLayers []int32) {
for _, dt := range w.downTrackSpreader.GetDownTracks() { for _, dt := range w.downTrackSpreader.GetDownTracks() {
dt.UpTrackLayersChange(layers) dt.UpTrackLayersChange(availableLayers, exemptedLayers)
} }
} }
+24 -18
View File
@@ -38,6 +38,12 @@ const (
PriorityMax = uint8(255) PriorityMax = uint8(255)
PriorityDefaultScreenshare = PriorityMax PriorityDefaultScreenshare = PriorityMax
PriorityDefaultVideo = PriorityMin PriorityDefaultVideo = PriorityMin
FlagAllowOvershootWhileOptimal = true
FlagAllowOvershootWhileDeficient = false
FlagAllowOvershootExemptTrackWhileDeficient = true
FlagAllowOvershootInProbe = true
FlagAllowOvershootInCatchup = true
) )
var ( var (
@@ -707,7 +713,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
// if not deficient, free pass allocate track // if not deficient, free pass allocate track
if !s.params.Config.Enabled || s.state == StateStable || !track.IsManaged() { if !s.params.Config.Enabled || s.state == StateStable || !track.IsManaged() {
update := NewStreamStateUpdate() update := NewStreamStateUpdate()
allocation := track.AllocateOptimal() allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal)
update.HandleStreamingChange(allocation.change, track) update.HandleStreamingChange(allocation.change, track)
s.maybeSendUpdate(update) s.maybeSendUpdate(update)
return return
@@ -721,7 +727,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
// 4. If this track needs more, ask for best offer from others and try to use it. // 4. If this track needs more, ask for best offer from others and try to use it.
// //
track.ProvisionalAllocatePrepare() track.ProvisionalAllocatePrepare()
transition := track.ProvisionalAllocateGetCooperativeTransition() transition := track.ProvisionalAllocateGetCooperativeTransition(FlagAllowOvershootWhileDeficient)
// track is currently streaming at minimum // track is currently streaming at minimum
if transition.bandwidthDelta == 0 { if transition.bandwidthDelta == 0 {
@@ -848,7 +854,7 @@ func (s *StreamAllocator) maybeBoostDeficientTracks() {
update := NewStreamStateUpdate() update := NewStreamStateUpdate()
for _, track := range s.getMaxDistanceSortedDeficient() { for _, track := range s.getMaxDistanceSortedDeficient() {
allocation, boosted := track.AllocateNextHigher(availableChannelCapacity) allocation, boosted := track.AllocateNextHigher(availableChannelCapacity, FlagAllowOvershootInCatchup)
if !boosted { if !boosted {
continue continue
} }
@@ -898,7 +904,7 @@ func (s *StreamAllocator) allocateAllTracks() {
// //
// This pass is to find out if there is any leftover channel capacity after allocating exempt tracks. // This pass is to find out if there is any leftover channel capacity after allocating exempt tracks.
// Infinite channel capacity is given so that exempt tracks do not stall // Exempt tracks are given optimal allocation (i. e. no bandwidth constraint) so that they do not fail allocation.
// //
videoTracks := s.getTracks() videoTracks := s.getTracks()
for _, track := range videoTracks { for _, track := range videoTracks {
@@ -906,7 +912,7 @@ func (s *StreamAllocator) allocateAllTracks() {
continue continue
} }
allocation := track.AllocateOptimal() allocation := track.AllocateOptimal(FlagAllowOvershootExemptTrackWhileDeficient)
update.HandleStreamingChange(allocation.change, track) update.HandleStreamingChange(allocation.change, track)
// LK-TODO: optimistic allocation before bitrate is available will return 0. How to account for that? // LK-TODO: optimistic allocation before bitrate is available will return 0. How to account for that?
@@ -940,7 +946,7 @@ func (s *StreamAllocator) allocateAllTracks() {
} }
for _, track := range sorted { for _, track := range sorted {
usedChannelCapacity := track.ProvisionalAllocate(availableChannelCapacity, layers, s.params.Config.AllowPause) usedChannelCapacity := track.ProvisionalAllocate(availableChannelCapacity, layers, s.params.Config.AllowPause, FlagAllowOvershootWhileDeficient)
availableChannelCapacity -= usedChannelCapacity availableChannelCapacity -= usedChannelCapacity
if availableChannelCapacity < 0 { if availableChannelCapacity < 0 {
availableChannelCapacity = 0 availableChannelCapacity = 0
@@ -1090,7 +1096,7 @@ func (s *StreamAllocator) maybeProbe() {
func (s *StreamAllocator) maybeProbeWithMedia() { func (s *StreamAllocator) maybeProbeWithMedia() {
// boost deficient track farthest from desired layers // boost deficient track farthest from desired layers
for _, track := range s.getMaxDistanceSortedDeficient() { for _, track := range s.getMaxDistanceSortedDeficient() {
allocation, boosted := track.AllocateNextHigher(ChannelCapacityInfinity) allocation, boosted := track.AllocateNextHigher(ChannelCapacityInfinity, FlagAllowOvershootInCatchup)
if !boosted { if !boosted {
continue continue
} }
@@ -1107,7 +1113,7 @@ func (s *StreamAllocator) maybeProbeWithMedia() {
func (s *StreamAllocator) maybeProbeWithPadding() { func (s *StreamAllocator) maybeProbeWithPadding() {
// use deficient track farthest from desired layers to find how much to probe // use deficient track farthest from desired layers to find how much to probe
for _, track := range s.getMaxDistanceSortedDeficient() { for _, track := range s.getMaxDistanceSortedDeficient() {
transition, available := track.GetNextHigherTransition() transition, available := track.GetNextHigherTransition(FlagAllowOvershootInProbe)
if !available || transition.bandwidthDelta < 0 { if !available || transition.bandwidthDelta < 0 {
continue continue
} }
@@ -1326,20 +1332,20 @@ func (t *Track) WritePaddingRTP(bytesToSend int) int {
return t.downTrack.WritePaddingRTP(bytesToSend) return t.downTrack.WritePaddingRTP(bytesToSend)
} }
func (t *Track) AllocateOptimal() VideoAllocation { func (t *Track) AllocateOptimal(allowOvershoot bool) VideoAllocation {
return t.downTrack.AllocateOptimal() return t.downTrack.AllocateOptimal(allowOvershoot)
} }
func (t *Track) ProvisionalAllocatePrepare() { func (t *Track) ProvisionalAllocatePrepare() {
t.downTrack.ProvisionalAllocatePrepare() t.downTrack.ProvisionalAllocatePrepare()
} }
func (t *Track) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool) int64 { func (t *Track) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool, allowOvershoot bool) int64 {
return t.downTrack.ProvisionalAllocate(availableChannelCapacity, layers, allowPause) return t.downTrack.ProvisionalAllocate(availableChannelCapacity, layers, allowPause, allowOvershoot)
} }
func (t *Track) ProvisionalAllocateGetCooperativeTransition() VideoTransition { func (t *Track) ProvisionalAllocateGetCooperativeTransition(allowOvershoot bool) VideoTransition {
return t.downTrack.ProvisionalAllocateGetCooperativeTransition() return t.downTrack.ProvisionalAllocateGetCooperativeTransition(allowOvershoot)
} }
func (t *Track) ProvisionalAllocateGetBestWeightedTransition() VideoTransition { func (t *Track) ProvisionalAllocateGetBestWeightedTransition() VideoTransition {
@@ -1350,12 +1356,12 @@ func (t *Track) ProvisionalAllocateCommit() VideoAllocation {
return t.downTrack.ProvisionalAllocateCommit() return t.downTrack.ProvisionalAllocateCommit()
} }
func (t *Track) AllocateNextHigher(availableChannelCapacity int64) (VideoAllocation, bool) { func (t *Track) AllocateNextHigher(availableChannelCapacity int64, allowOvershoot bool) (VideoAllocation, bool) {
return t.downTrack.AllocateNextHigher(availableChannelCapacity) return t.downTrack.AllocateNextHigher(availableChannelCapacity, allowOvershoot)
} }
func (t *Track) GetNextHigherTransition() (VideoTransition, bool) { func (t *Track) GetNextHigherTransition(allowOvershoot bool) (VideoTransition, bool) {
return t.downTrack.GetNextHigherTransition() return t.downTrack.GetNextHigherTransition(allowOvershoot)
} }
func (t *Track) Pause() VideoAllocation { func (t *Track) Pause() VideoAllocation {
+74 -32
View File
@@ -13,7 +13,8 @@ import (
) )
var ( var (
exemptLayers = []int32{0} ExemptedLayersScreenshare = []int32{0}
ExemptedLayersVideo = []int32{}
) )
var ( var (
@@ -71,10 +72,11 @@ type StreamTrackerManager struct {
trackers [DefaultMaxLayerSpatial + 1]*StreamTracker trackers [DefaultMaxLayerSpatial + 1]*StreamTracker
availableLayers []int32 availableLayers []int32
exemptedLayers []int32
maxExpectedLayer int32 maxExpectedLayer int32
paused bool paused bool
onAvailableLayersChanged func(availableLayers []int32) onAvailableLayersChanged func(availableLayers []int32, exemptedLayers []int32)
onBitrateAvailabilityChanged func() onBitrateAvailabilityChanged func()
onMaxLayerChanged func(maxLayer int32) onMaxLayerChanged func(maxLayer int32)
} }
@@ -97,7 +99,7 @@ func NewStreamTrackerManager(logger logger.Logger, trackInfo *livekit.TrackInfo)
return s return s
} }
func (s *StreamTrackerManager) OnAvailableLayersChanged(f func(availableLayers []int32)) { func (s *StreamTrackerManager) OnAvailableLayersChanged(f func(availableLayers []int32, exemptedLayers []int32)) {
s.onAvailableLayersChanged = f s.onAvailableLayersChanged = f
} }
@@ -130,27 +132,7 @@ func (s *StreamTrackerManager) AddTracker(layer int32) *StreamTracker {
tracker.OnStatusChanged(func(status StreamStatus) { tracker.OnStatusChanged(func(status StreamStatus) {
s.logger.Debugw("StreamTrackerManager OnStatusChanged", "layer", layer, "status", status) s.logger.Debugw("StreamTrackerManager OnStatusChanged", "layer", layer, "status", status)
if status == StreamStatusStopped { if status == StreamStatusStopped {
exempt := false
for _, l := range exemptLayers {
if layer == l {
exempt = true
break
}
}
if exempt {
s.lock.RLock()
if layer > s.maxExpectedLayer || s.paused {
exempt = false
}
s.lock.RUnlock()
}
if !exempt {
s.removeAvailableLayer(layer) s.removeAvailableLayer(layer)
} else {
s.logger.Debugw("not removing exempt layer", "layer", layer)
}
} else { } else {
s.addAvailableLayer(layer) s.addAvailableLayer(layer)
} }
@@ -187,6 +169,7 @@ func (s *StreamTrackerManager) RemoveAllTrackers() {
s.trackers[layer] = nil s.trackers[layer] = nil
} }
s.availableLayers = make([]int32, 0) s.availableLayers = make([]int32, 0)
s.exemptedLayers = make([]int32, 0)
s.maxExpectedLayer = DefaultMaxLayerSpatial s.maxExpectedLayer = DefaultMaxLayerSpatial
s.paused = false s.paused = false
s.lock.Unlock() s.lock.Unlock()
@@ -330,11 +313,17 @@ func (s *StreamTrackerManager) GetBitrateTemporalCumulative() Bitrates {
return br return br
} }
func (s *StreamTrackerManager) GetAvailableLayers() []int32 { func (s *StreamTrackerManager) GetAvailableLayers() ([]int32, []int32) {
s.lock.RLock() s.lock.RLock()
defer s.lock.RUnlock() defer s.lock.RUnlock()
return s.availableLayers var availableLayers []int32
availableLayers = append(availableLayers, s.availableLayers...)
var exemptedLayers []int32
exemptedLayers = append(exemptedLayers, s.exemptedLayers...)
return availableLayers, exemptedLayers
} }
func (s *StreamTrackerManager) HasSpatialLayer(layer int32) bool { func (s *StreamTrackerManager) HasSpatialLayer(layer int32) bool {
@@ -371,18 +360,30 @@ func (s *StreamTrackerManager) addAvailableLayer(layer int32) {
s.availableLayers = append(s.availableLayers, layer) s.availableLayers = append(s.availableLayers, layer)
sort.Slice(s.availableLayers, func(i, j int) bool { return s.availableLayers[i] < s.availableLayers[j] }) sort.Slice(s.availableLayers, func(i, j int) bool { return s.availableLayers[i] < s.availableLayers[j] })
var layers []int32 for idx, el := range s.exemptedLayers {
layers = append(layers, s.availableLayers...) if el == layer {
s.exemptedLayers[idx] = s.exemptedLayers[len(s.exemptedLayers)-1]
s.exemptedLayers = s.exemptedLayers[:len(s.exemptedLayers)-1]
break
}
}
sort.Slice(s.exemptedLayers, func(i, j int) bool { return s.exemptedLayers[i] < s.exemptedLayers[j] })
var availableLayers []int32
availableLayers = append(availableLayers, s.availableLayers...)
var exemptedLayers []int32
exemptedLayers = append(exemptedLayers, s.exemptedLayers...)
s.lock.Unlock() s.lock.Unlock()
s.logger.Debugw("available layers changed - layer seen", "added", layer, "layers", layers) s.logger.Debugw("available layers changed - layer seen", "added", layer, "availableLayers", availableLayers, "exemptedLayers", exemptedLayers)
if s.onAvailableLayersChanged != nil { if s.onAvailableLayersChanged != nil {
s.onAvailableLayersChanged(layers) s.onAvailableLayersChanged(availableLayers, exemptedLayers)
} }
// check if new layer was the max layer // check if new layer was the max layer
if layers[len(layers)-1] == layer && s.onMaxLayerChanged != nil { if availableLayers[len(availableLayers)-1] == layer && s.onMaxLayerChanged != nil {
s.onMaxLayerChanged(layer) s.onMaxLayerChanged(layer)
} }
} }
@@ -394,15 +395,56 @@ func (s *StreamTrackerManager) removeAvailableLayer(layer int32) {
prevMaxLayer = s.availableLayers[len(s.availableLayers)-1] prevMaxLayer = s.availableLayers[len(s.availableLayers)-1]
} }
//
// remove from available if not exempt
//
exempt := false
var sourceExemptedLayers []int32
if s.trackInfo.Source == livekit.TrackSource_SCREEN_SHARE {
sourceExemptedLayers = ExemptedLayersScreenshare
} else {
sourceExemptedLayers = ExemptedLayersVideo
}
for _, l := range sourceExemptedLayers {
if layer == l {
exempt = true
break
}
}
if exempt {
if layer > s.maxExpectedLayer || s.paused {
exempt = false
}
}
newLayers := make([]int32, 0, DefaultMaxLayerSpatial+1) newLayers := make([]int32, 0, DefaultMaxLayerSpatial+1)
for _, l := range s.availableLayers { for _, l := range s.availableLayers {
if l != layer { if exempt || l != layer {
newLayers = append(newLayers, l) newLayers = append(newLayers, l)
} }
} }
sort.Slice(newLayers, func(i, j int) bool { return newLayers[i] < newLayers[j] }) sort.Slice(newLayers, func(i, j int) bool { return newLayers[i] < newLayers[j] })
s.availableLayers = newLayers s.availableLayers = newLayers
//
// add to exempt if not already present
//
found := false
for _, el := range s.exemptedLayers {
if el == layer {
found = true
break
}
}
if !found {
s.exemptedLayers = append(s.exemptedLayers, layer)
sort.Slice(s.exemptedLayers, func(i, j int) bool { return s.exemptedLayers[i] < s.exemptedLayers[j] })
}
var exemptedLayers []int32
exemptedLayers = append(exemptedLayers, s.exemptedLayers...)
curMaxLayer := InvalidLayerSpatial curMaxLayer := InvalidLayerSpatial
if len(s.availableLayers) > 0 { if len(s.availableLayers) > 0 {
curMaxLayer = s.availableLayers[len(s.availableLayers)-1] curMaxLayer = s.availableLayers[len(s.availableLayers)-1]
@@ -413,7 +455,7 @@ func (s *StreamTrackerManager) removeAvailableLayer(layer int32) {
// need to immediately switch off unavailable layers // need to immediately switch off unavailable layers
if s.onAvailableLayersChanged != nil { if s.onAvailableLayersChanged != nil {
s.onAvailableLayersChanged(newLayers) s.onAvailableLayersChanged(newLayers, exemptedLayers)
} }
// if maxLayer was removed, send the new maxLayer // if maxLayer was removed, send the new maxLayer