Handle exempted layers (#983)
* WIP commit * WIP commit * Add tests * remove debug
This commit is contained in:
+13
-13
@@ -25,7 +25,7 @@ import (
|
||||
|
||||
// TrackSender defines an interface send media to remote peer
|
||||
type TrackSender interface {
|
||||
UpTrackLayersChange(availableLayers []int32)
|
||||
UpTrackLayersChange(availableLayers []int32, exemptedLayers []int32)
|
||||
UpTrackBitrateAvailabilityChange()
|
||||
WriteRTP(p *buffer.ExtPacket, layer int32) error
|
||||
Close()
|
||||
@@ -755,8 +755,8 @@ func (d *DownTrack) GetForwardingStatus() ForwardingStatus {
|
||||
return d.forwarder.GetForwardingStatus()
|
||||
}
|
||||
|
||||
func (d *DownTrack) UpTrackLayersChange(availableLayers []int32) {
|
||||
d.forwarder.UpTrackLayersChange(availableLayers)
|
||||
func (d *DownTrack) UpTrackLayersChange(availableLayers []int32, exemptedLayers []int32) {
|
||||
d.forwarder.UpTrackLayersChange(availableLayers, exemptedLayers)
|
||||
|
||||
if onAvailableLayersChanged, ok := d.onAvailableLayersChanged.Load().(func(dt *DownTrack)); ok {
|
||||
onAvailableLayersChanged(d)
|
||||
@@ -841,8 +841,8 @@ func (d *DownTrack) DistanceToDesired() int32 {
|
||||
return d.forwarder.DistanceToDesired()
|
||||
}
|
||||
|
||||
func (d *DownTrack) AllocateOptimal() VideoAllocation {
|
||||
allocation := d.forwarder.AllocateOptimal(d.receiver.GetBitrateTemporalCumulative())
|
||||
func (d *DownTrack) AllocateOptimal(allowOvershoot bool) VideoAllocation {
|
||||
allocation := d.forwarder.AllocateOptimal(d.receiver.GetBitrateTemporalCumulative(), allowOvershoot)
|
||||
d.logger.Debugw("stream: allocation optimal available", "allocation", allocation)
|
||||
d.maybeStartKeyFrameRequester()
|
||||
return allocation
|
||||
@@ -852,12 +852,12 @@ func (d *DownTrack) ProvisionalAllocatePrepare() {
|
||||
d.forwarder.ProvisionalAllocatePrepare(d.receiver.GetBitrateTemporalCumulative())
|
||||
}
|
||||
|
||||
func (d *DownTrack) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool) int64 {
|
||||
return d.forwarder.ProvisionalAllocate(availableChannelCapacity, layers, allowPause)
|
||||
func (d *DownTrack) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool, allowOvershoot bool) int64 {
|
||||
return d.forwarder.ProvisionalAllocate(availableChannelCapacity, layers, allowPause, allowOvershoot)
|
||||
}
|
||||
|
||||
func (d *DownTrack) ProvisionalAllocateGetCooperativeTransition() VideoTransition {
|
||||
transition := d.forwarder.ProvisionalAllocateGetCooperativeTransition()
|
||||
func (d *DownTrack) ProvisionalAllocateGetCooperativeTransition(allowOvershoot bool) VideoTransition {
|
||||
transition := d.forwarder.ProvisionalAllocateGetCooperativeTransition(allowOvershoot)
|
||||
d.logger.Debugw("stream: cooperative transition", "transition", transition)
|
||||
return transition
|
||||
}
|
||||
@@ -875,15 +875,15 @@ func (d *DownTrack) ProvisionalAllocateCommit() VideoAllocation {
|
||||
return allocation
|
||||
}
|
||||
|
||||
func (d *DownTrack) AllocateNextHigher(availableChannelCapacity int64) (VideoAllocation, bool) {
|
||||
allocation, available := d.forwarder.AllocateNextHigher(availableChannelCapacity, d.receiver.GetBitrateTemporalCumulative())
|
||||
func (d *DownTrack) AllocateNextHigher(availableChannelCapacity int64, allowOvershoot bool) (VideoAllocation, bool) {
|
||||
allocation, available := d.forwarder.AllocateNextHigher(availableChannelCapacity, d.receiver.GetBitrateTemporalCumulative(), allowOvershoot)
|
||||
d.logger.Debugw("stream: allocation next higher layer", "allocation", allocation, "available", available)
|
||||
d.maybeStartKeyFrameRequester()
|
||||
return allocation, available
|
||||
}
|
||||
|
||||
func (d *DownTrack) GetNextHigherTransition() (VideoTransition, bool) {
|
||||
transition, available := d.forwarder.GetNextHigherTransition(d.receiver.GetBitrateTemporalCumulative())
|
||||
func (d *DownTrack) GetNextHigherTransition(allowOvershoot bool) (VideoTransition, bool) {
|
||||
transition, available := d.forwarder.GetNextHigherTransition(d.receiver.GetBitrateTemporalCumulative(), allowOvershoot)
|
||||
d.logger.Debugw("stream: get next higher layer", "transition", transition, "available", available)
|
||||
return transition, available
|
||||
}
|
||||
|
||||
+363
-176
@@ -95,14 +95,15 @@ type VideoAllocation struct {
|
||||
bandwidthRequested int64
|
||||
bandwidthDelta int64
|
||||
availableLayers []int32
|
||||
exemptedLayers []int32
|
||||
bitrates Bitrates
|
||||
targetLayers VideoLayers
|
||||
distanceToDesired int32
|
||||
}
|
||||
|
||||
func (v VideoAllocation) String() string {
|
||||
return fmt.Sprintf("VideoAllocation{state: %s, change: %s, bw: %d, del: %d, avail: %+v, rates: %+v, target: %s}",
|
||||
v.state, v.change, v.bandwidthRequested, v.bandwidthDelta, v.availableLayers, v.bitrates, v.targetLayers)
|
||||
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.exemptedLayers, v.bitrates, v.targetLayers)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -114,11 +115,13 @@ var (
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
type VideoAllocationProvisional struct {
|
||||
layers VideoLayers
|
||||
muted bool
|
||||
bitrates Bitrates
|
||||
availableLayers []int32
|
||||
exemptedLayers []int32
|
||||
maxLayers VideoLayers
|
||||
|
||||
allocatedLayers VideoLayers
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
@@ -192,6 +195,7 @@ type Forwarder struct {
|
||||
lastAllocation VideoAllocation
|
||||
|
||||
availableLayers []int32
|
||||
exemptedLayers []int32
|
||||
|
||||
rtpMunger *RTPMunger
|
||||
vp8Munger *VP8Munger
|
||||
@@ -390,7 +394,7 @@ func (f *Forwarder) IsReducedQuality() (int32, bool) {
|
||||
return distance, f.lastAllocation.state == VideoAllocationStateDeficient
|
||||
}
|
||||
|
||||
func (f *Forwarder) UpTrackLayersChange(availableLayers []int32) {
|
||||
func (f *Forwarder) UpTrackLayersChange(availableLayers []int32, exemptedLayers []int32) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
@@ -400,11 +404,22 @@ func (f *Forwarder) UpTrackLayersChange(availableLayers []int32) {
|
||||
} else {
|
||||
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 {
|
||||
for i := f.maxLayers.Spatial; i >= 0; i-- {
|
||||
for j := f.maxLayers.Temporal; j >= 0; j-- {
|
||||
func (f *Forwarder) getOptimalBandwidthNeeded(brs Bitrates, maxLayers VideoLayers) int64 {
|
||||
if f.muted {
|
||||
return 0
|
||||
}
|
||||
|
||||
for i := maxLayers.Spatial; i >= 0; i-- {
|
||||
for j := maxLayers.Temporal; j >= 0; j-- {
|
||||
if brs[i][j] == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -418,33 +433,51 @@ func (f *Forwarder) getOptimalBandwidthNeeded(brs Bitrates) int64 {
|
||||
|
||||
func (f *Forwarder) bitrateAvailable(brs Bitrates) bool {
|
||||
neededLayers := 0
|
||||
var bitrateAvailableLayers []int32
|
||||
bitrateAvailableLayers := 0
|
||||
for _, layer := range f.availableLayers {
|
||||
if layer > f.maxLayers.Spatial {
|
||||
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++
|
||||
for t := f.maxLayers.Temporal; t >= 0; t-- {
|
||||
if brs[layer][t] != 0 {
|
||||
bitrateAvailableLayers = append(bitrateAvailableLayers, layer)
|
||||
bitrateAvailableLayers++
|
||||
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 {
|
||||
return 0
|
||||
}
|
||||
|
||||
found := false
|
||||
distance := int32(0)
|
||||
for s := f.maxLayers.Spatial; s >= 0; s-- {
|
||||
found := false
|
||||
for t := f.maxLayers.Temporal; t >= 0; t-- {
|
||||
for s := maxLayers.Spatial; s >= 0; s-- {
|
||||
for t := maxLayers.Temporal; t >= 0; t-- {
|
||||
if brs[s][t] == 0 {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -489,7 +535,7 @@ func (f *Forwarder) DistanceToDesired() int32 {
|
||||
return f.lastAllocation.distanceToDesired
|
||||
}
|
||||
|
||||
func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation {
|
||||
func (f *Forwarder) AllocateOptimal(brs Bitrates, allowOvershoot bool) VideoAllocation {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
@@ -537,10 +583,6 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation {
|
||||
|
||||
bandwidthRequested = brs[s][t]
|
||||
state = VideoAllocationStateOptimal
|
||||
|
||||
if f.targetLayers == InvalidLayers {
|
||||
change = VideoStreamingChangeResuming
|
||||
}
|
||||
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,
|
||||
// look for a layer above. It is okay to overshoot
|
||||
// in optimal allocation (i.e. no bandwidth restrictions).
|
||||
// It is possible that clients send only a higher layer.
|
||||
// To accommodate cases like that, try finding a layer
|
||||
// above the requested maximum to ensure streaming
|
||||
for s := DefaultMaxLayerSpatial; s >= 0; s-- {
|
||||
for t := DefaultMaxLayerTemporal; t >= 0; t-- {
|
||||
for s := f.maxLayers.Spatial + 1; s <= DefaultMaxLayerSpatial; s++ {
|
||||
for t := int32(0); t <= DefaultMaxLayerTemporal; t++ {
|
||||
if brs[s][t] == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -569,10 +611,6 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation {
|
||||
|
||||
bandwidthRequested = brs[s][t]
|
||||
state = VideoAllocationStateOptimal
|
||||
|
||||
if f.targetLayers == InvalidLayers {
|
||||
change = VideoStreamingChangeResuming
|
||||
}
|
||||
f.logger.Infow("allowing overshoot", "maxLayer", f.maxLayers, "targetLayers", targetLayers)
|
||||
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() {
|
||||
@@ -594,12 +661,16 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates) VideoAllocation {
|
||||
bandwidthDelta: bandwidthRequested - f.lastAllocation.bandwidthRequested,
|
||||
bitrates: brs,
|
||||
targetLayers: targetLayers,
|
||||
distanceToDesired: f.getDistanceToDesired(brs, targetLayers),
|
||||
distanceToDesired: f.getDistanceToDesired(brs, targetLayers, f.maxLayers),
|
||||
}
|
||||
if len(f.availableLayers) > 0 {
|
||||
f.lastAllocation.availableLayers = make([]int32, len(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)
|
||||
if f.targetLayers == InvalidLayers {
|
||||
@@ -614,50 +685,75 @@ func (f *Forwarder) ProvisionalAllocatePrepare(bitrates Bitrates) {
|
||||
defer f.lock.Unlock()
|
||||
|
||||
f.provisional = &VideoAllocationProvisional{
|
||||
layers: InvalidLayers,
|
||||
muted: f.muted,
|
||||
bitrates: bitrates,
|
||||
maxLayers: f.maxLayers,
|
||||
allocatedLayers: InvalidLayers,
|
||||
muted: f.muted,
|
||||
bitrates: bitrates,
|
||||
maxLayers: f.maxLayers,
|
||||
}
|
||||
if len(f.availableLayers) > 0 {
|
||||
f.provisional.availableLayers = make([]int32, len(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()
|
||||
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
|
||||
}
|
||||
|
||||
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]
|
||||
if requiredBitrate == 0 {
|
||||
return 0
|
||||
return maybeAdoptExempted()
|
||||
}
|
||||
|
||||
alreadyAllocatedBitrate := int64(0)
|
||||
if f.provisional.layers != InvalidLayers {
|
||||
alreadyAllocatedBitrate = f.provisional.bitrates[f.provisional.layers.Spatial][f.provisional.layers.Temporal]
|
||||
if f.provisional.allocatedLayers != InvalidLayers {
|
||||
alreadyAllocatedBitrate = f.provisional.bitrates[f.provisional.allocatedLayers.Spatial][f.provisional.allocatedLayers.Temporal]
|
||||
}
|
||||
|
||||
if requiredBitrate <= (availableChannelCapacity + alreadyAllocatedBitrate) {
|
||||
f.provisional.layers = layers
|
||||
// a layer under maximum fits, take it
|
||||
if !layers.GreaterThan(f.provisional.maxLayers) && requiredBitrate <= (availableChannelCapacity+alreadyAllocatedBitrate) {
|
||||
f.provisional.allocatedLayers = layers
|
||||
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)) {
|
||||
f.provisional.layers = layers
|
||||
//
|
||||
// Given layer does not fit. But overshoot is allowed.
|
||||
// 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 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)
|
||||
// when channel is congested.
|
||||
@@ -681,7 +777,7 @@ func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransitio
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.provisional.muted {
|
||||
f.provisional.layers = InvalidLayers
|
||||
f.provisional.allocatedLayers = InvalidLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: InvalidLayers,
|
||||
@@ -710,9 +806,9 @@ func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransitio
|
||||
}
|
||||
|
||||
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
|
||||
f.provisional.layers = f.targetLayers
|
||||
f.provisional.allocatedLayers = f.targetLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: f.targetLayers,
|
||||
@@ -722,7 +818,7 @@ func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransitio
|
||||
|
||||
if f.targetLayers.GreaterThan(maximalLayers) {
|
||||
// maximalLayers <= f.targetLayers, make the down move
|
||||
f.provisional.layers = maximalLayers
|
||||
f.provisional.allocatedLayers = maximalLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: maximalLayers,
|
||||
@@ -732,31 +828,66 @@ func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransitio
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
minimalLayers := InvalidLayers
|
||||
bandwidthRequired := int64(0)
|
||||
for s := int32(0); s <= f.provisional.maxLayers.Spatial; s++ {
|
||||
for t := int32(0); t <= f.provisional.maxLayers.Temporal; t++ {
|
||||
if f.provisional.bitrates[s][t] != 0 {
|
||||
minimalLayers = VideoLayers{Spatial: s, Temporal: t}
|
||||
bandwidthRequired = f.provisional.bitrates[s][t]
|
||||
findNextLayer := func(
|
||||
minSpatial, maxSpatial int32,
|
||||
minTemporal, maxTemporal int32,
|
||||
) (VideoLayers, int64) {
|
||||
layers := InvalidLayers
|
||||
bw := int64(0)
|
||||
for s := minSpatial; s <= maxSpatial; s++ {
|
||||
for t := minTemporal; t <= maxTemporal; t++ {
|
||||
if f.provisional.bitrates[s][t] != 0 {
|
||||
layers = VideoLayers{Spatial: s, Temporal: t}
|
||||
bw = f.provisional.bitrates[s][t]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if bw != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if bandwidthRequired != 0 {
|
||||
break
|
||||
}
|
||||
return layers, bw
|
||||
}
|
||||
|
||||
targetLayers := f.targetLayers
|
||||
if targetLayers == InvalidLayers || targetLayers.GreaterThan(minimalLayers) || (f.provisional.bitrates[targetLayers.Spatial][targetLayers.Temporal] == 0) {
|
||||
targetLayers = minimalLayers
|
||||
bandwidthRequired := int64(0)
|
||||
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{
|
||||
from: f.targetLayers,
|
||||
to: targetLayers,
|
||||
@@ -783,7 +914,7 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.provisional.muted {
|
||||
f.provisional.layers = InvalidLayers
|
||||
f.provisional.allocatedLayers = InvalidLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: InvalidLayers,
|
||||
@@ -806,8 +937,23 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti
|
||||
}
|
||||
|
||||
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,
|
||||
f.provisional.layers = InvalidLayers
|
||||
f.provisional.allocatedLayers = InvalidLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: InvalidLayers,
|
||||
@@ -848,7 +994,7 @@ func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransiti
|
||||
}
|
||||
}
|
||||
|
||||
f.provisional.layers = bestLayers
|
||||
f.provisional.allocatedLayers = bestLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: bestLayers,
|
||||
@@ -870,17 +1016,23 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
|
||||
case len(f.provisional.availableLayers) == 0:
|
||||
// feed is dry
|
||||
state = VideoAllocationStateFeedDry
|
||||
case f.provisional.layers == InvalidLayers:
|
||||
case f.provisional.allocatedLayers == InvalidLayers:
|
||||
state = VideoAllocationStateDeficient
|
||||
|
||||
if f.targetLayers != InvalidLayers {
|
||||
change = VideoStreamingChangePausing
|
||||
}
|
||||
default:
|
||||
bandwidthRequested = f.provisional.bitrates[f.provisional.layers.Spatial][f.provisional.layers.Temporal]
|
||||
if bandwidthRequested == f.getOptimalBandwidthNeeded(f.provisional.bitrates) {
|
||||
optimalBandwidthNeeded := f.getOptimalBandwidthNeeded(f.provisional.bitrates, f.provisional.maxLayers)
|
||||
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
|
||||
} else {
|
||||
//
|
||||
// Optimal bandwidth could be 0 if using exempted layer.
|
||||
// Exempted layer is still treated as deficient.
|
||||
//
|
||||
state = VideoAllocationStateDeficient
|
||||
}
|
||||
|
||||
@@ -895,13 +1047,17 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
|
||||
bandwidthRequested: bandwidthRequested,
|
||||
bandwidthDelta: bandwidthRequested - f.lastAllocation.bandwidthRequested,
|
||||
bitrates: f.provisional.bitrates,
|
||||
targetLayers: f.provisional.layers,
|
||||
distanceToDesired: f.getDistanceToDesired(f.provisional.bitrates, f.provisional.layers),
|
||||
targetLayers: f.provisional.allocatedLayers,
|
||||
distanceToDesired: f.getDistanceToDesired(f.provisional.bitrates, f.provisional.allocatedLayers, f.provisional.maxLayers),
|
||||
}
|
||||
if len(f.availableLayers) > 0 {
|
||||
f.lastAllocation.availableLayers = make([]int32, len(f.availableLayers))
|
||||
if len(f.provisional.availableLayers) > 0 {
|
||||
f.lastAllocation.availableLayers = make([]int32, len(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)
|
||||
if f.targetLayers == InvalidLayers {
|
||||
@@ -911,7 +1067,7 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
|
||||
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()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
@@ -932,93 +1088,98 @@ func (f *Forwarder) AllocateNextHigher(availableChannelCapacity int64, brs Bitra
|
||||
return f.lastAllocation, false
|
||||
}
|
||||
|
||||
optimalBandwidthNeeded := f.getOptimalBandwidthNeeded(brs)
|
||||
optimalBandwidthNeeded := f.getOptimalBandwidthNeeded(brs, f.maxLayers)
|
||||
|
||||
alreadyAllocated := int64(0)
|
||||
if f.targetLayers != InvalidLayers {
|
||||
alreadyAllocated = brs[f.targetLayers.Spatial][f.targetLayers.Temporal]
|
||||
}
|
||||
|
||||
doAllocation := func(
|
||||
minSpatial, maxSpatial int32,
|
||||
minTemporal, maxTemporal int32,
|
||||
) (bool, VideoAllocation, bool) {
|
||||
for s := minSpatial; s <= maxSpatial; s++ {
|
||||
for t := minTemporal; t <= maxTemporal; t++ {
|
||||
bandwidthRequested := brs[s][t]
|
||||
if bandwidthRequested == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if !allowOvershoot && bandwidthRequested-alreadyAllocated > availableChannelCapacity {
|
||||
// next higher available layer does not fit, return
|
||||
f.lastAllocation.change = VideoStreamingChangeNone
|
||||
return true, f.lastAllocation, false
|
||||
}
|
||||
|
||||
targetLayers := VideoLayers{Spatial: s, Temporal: t}
|
||||
state := VideoAllocationStateDeficient
|
||||
if targetLayers.GreaterThan(f.maxLayers) || (optimalBandwidthNeeded > 0 && bandwidthRequested >= optimalBandwidthNeeded) {
|
||||
state = VideoAllocationStateOptimal
|
||||
}
|
||||
|
||||
change := VideoStreamingChangeNone
|
||||
if f.targetLayers == InvalidLayers {
|
||||
change = VideoStreamingChangeResuming
|
||||
}
|
||||
|
||||
f.lastAllocation = VideoAllocation{
|
||||
state: state,
|
||||
change: change,
|
||||
bandwidthRequested: bandwidthRequested,
|
||||
bandwidthDelta: bandwidthRequested - alreadyAllocated,
|
||||
bitrates: brs,
|
||||
targetLayers: targetLayers,
|
||||
distanceToDesired: f.getDistanceToDesired(brs, targetLayers, f.maxLayers),
|
||||
}
|
||||
if len(f.availableLayers) > 0 {
|
||||
f.lastAllocation.availableLayers = make([]int32, len(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)
|
||||
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 {
|
||||
for t := f.targetLayers.Temporal + 1; t <= f.maxLayers.Temporal; t++ {
|
||||
bandwidthRequested := brs[f.targetLayers.Spatial][t]
|
||||
if bandwidthRequested == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
for s := f.targetLayers.Spatial + 1; s <= f.maxLayers.Spatial; s++ {
|
||||
for t := int32(0); t <= f.maxLayers.Temporal; t++ {
|
||||
bandwidthRequested := brs[s][t]
|
||||
if bandwidthRequested == 0 {
|
||||
continue
|
||||
}
|
||||
done, allocation, boosted = doAllocation(
|
||||
f.targetLayers.Spatial+1, f.maxLayers.Spatial,
|
||||
0, f.maxLayers.Temporal,
|
||||
)
|
||||
if done {
|
||||
return allocation, boosted
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
change := VideoStreamingChangeNone
|
||||
if f.targetLayers == InvalidLayers {
|
||||
change = VideoStreamingChangeResuming
|
||||
}
|
||||
|
||||
targetLayers := VideoLayers{Spatial: s, Temporal: t}
|
||||
f.lastAllocation = VideoAllocation{
|
||||
state: state,
|
||||
change: change,
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
func (f *Forwarder) GetNextHigherTransition(brs Bitrates) (VideoTransition, bool) {
|
||||
func (f *Forwarder) GetNextHigherTransition(brs Bitrates, allowOvershoot bool) (VideoTransition, bool) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
@@ -1049,39 +1210,61 @@ func (f *Forwarder) GetNextHigherTransition(brs Bitrates) (VideoTransition, bool
|
||||
alreadyAllocated = brs[f.targetLayers.Spatial][f.targetLayers.Temporal]
|
||||
}
|
||||
|
||||
findNextHigher := func(
|
||||
minSpatial, maxSpatial int32,
|
||||
minTemporal, maxTemporal int32,
|
||||
) (bool, VideoTransition, bool) {
|
||||
for s := minSpatial; s <= maxSpatial; s++ {
|
||||
for t := minTemporal; t <= maxTemporal; t++ {
|
||||
bandwidthRequested := brs[s][t]
|
||||
if bandwidthRequested == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
transition := VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: VideoLayers{Spatial: s, Temporal: t},
|
||||
bandwidthDelta: bandwidthRequested - alreadyAllocated,
|
||||
}
|
||||
|
||||
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 {
|
||||
for t := f.targetLayers.Temporal + 1; t <= f.maxLayers.Temporal; t++ {
|
||||
bandwidthRequested := brs[f.targetLayers.Spatial][t]
|
||||
if bandwidthRequested == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
transition := VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: VideoLayers{Spatial: f.targetLayers.Spatial, Temporal: t},
|
||||
bandwidthDelta: bandwidthRequested - alreadyAllocated,
|
||||
}
|
||||
|
||||
return transition, true
|
||||
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
|
||||
for s := f.targetLayers.Spatial + 1; s <= f.maxLayers.Spatial; s++ {
|
||||
for t := int32(0); t <= f.maxLayers.Temporal; t++ {
|
||||
bandwidthRequested := brs[s][t]
|
||||
if bandwidthRequested == 0 {
|
||||
continue
|
||||
}
|
||||
done, transition, isAvailable = findNextHigher(
|
||||
f.targetLayers.Spatial+1, f.maxLayers.Spatial,
|
||||
0, f.maxLayers.Temporal,
|
||||
)
|
||||
if done {
|
||||
return transition, isAvailable
|
||||
}
|
||||
|
||||
transition := VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: VideoLayers{Spatial: s, Temporal: t},
|
||||
bandwidthDelta: bandwidthRequested - alreadyAllocated,
|
||||
}
|
||||
|
||||
return transition, true
|
||||
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,
|
||||
bitrates: brs,
|
||||
targetLayers: InvalidLayers,
|
||||
distanceToDesired: f.getDistanceToDesired(brs, InvalidLayers),
|
||||
distanceToDesired: f.getDistanceToDesired(brs, InvalidLayers, f.maxLayers),
|
||||
}
|
||||
if len(f.availableLayers) > 0 {
|
||||
f.lastAllocation.availableLayers = make([]int32, len(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)
|
||||
if f.targetLayers == InvalidLayers {
|
||||
|
||||
+414
-43
@@ -121,7 +121,7 @@ func TestForwarderGetForwardingStatus(t *testing.T) {
|
||||
|
||||
// with available layers, should be off
|
||||
availableLayers := []int32{0, 1, 2}
|
||||
f.UpTrackLayersChange(availableLayers)
|
||||
f.UpTrackLayersChange(availableLayers, []int32{})
|
||||
require.Equal(t, ForwardingStatusOff, f.GetForwardingStatus())
|
||||
|
||||
// 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
|
||||
availableLayers = []int32{0, 1}
|
||||
f.UpTrackLayersChange(availableLayers)
|
||||
f.UpTrackLayersChange(availableLayers, []int32{})
|
||||
require.Equal(t, ForwardingStatusOptimal, f.GetForwardingStatus())
|
||||
}
|
||||
|
||||
@@ -149,19 +149,24 @@ func TestForwarderUpTrackLayersChange(t *testing.T) {
|
||||
require.Nil(t, f.availableLayers)
|
||||
|
||||
availableLayers := []int32{0, 1, 2}
|
||||
f.UpTrackLayersChange(availableLayers)
|
||||
exemptedLayers := []int32{0}
|
||||
f.UpTrackLayersChange(availableLayers, exemptedLayers)
|
||||
require.Equal(t, availableLayers, f.availableLayers)
|
||||
require.Equal(t, exemptedLayers, f.exemptedLayers)
|
||||
|
||||
availableLayers = []int32{0, 2}
|
||||
f.UpTrackLayersChange(availableLayers)
|
||||
f.UpTrackLayersChange(availableLayers, []int32{})
|
||||
require.Equal(t, availableLayers, f.availableLayers)
|
||||
require.Nil(t, f.exemptedLayers)
|
||||
|
||||
availableLayers = []int32{}
|
||||
f.UpTrackLayersChange(availableLayers)
|
||||
exemptedLayers = []int32{}
|
||||
f.UpTrackLayersChange(availableLayers, exemptedLayers)
|
||||
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.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
|
||||
f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
|
||||
@@ -186,7 +191,7 @@ func TestForwarderAllocate(t *testing.T) {
|
||||
targetLayers: InvalidLayers,
|
||||
distanceToDesired: 0,
|
||||
}
|
||||
result := f.AllocateOptimal(bitrates)
|
||||
result := f.AllocateOptimal(bitrates, true)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
|
||||
@@ -204,14 +209,14 @@ func TestForwarderAllocate(t *testing.T) {
|
||||
targetLayers: InvalidLayers,
|
||||
distanceToDesired: 0,
|
||||
}
|
||||
result = f.AllocateOptimal(emptyBitrates)
|
||||
result = f.AllocateOptimal(emptyBitrates, true)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
|
||||
// awaiting measurement, i.e. bitrates are not available, but layers available
|
||||
f.lastAllocation.state = VideoAllocationStateNone
|
||||
disable(f)
|
||||
f.UpTrackLayersChange([]int32{0})
|
||||
f.UpTrackLayersChange([]int32{0}, []int32{})
|
||||
expectedTargetLayers := VideoLayers{
|
||||
Spatial: 0,
|
||||
Temporal: DefaultMaxLayerTemporal,
|
||||
@@ -226,14 +231,58 @@ func TestForwarderAllocate(t *testing.T) {
|
||||
targetLayers: expectedTargetLayers,
|
||||
distanceToDesired: 0,
|
||||
}
|
||||
result = f.AllocateOptimal(emptyBitrates)
|
||||
result = f.AllocateOptimal(emptyBitrates, true)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.Equal(t, expectedTargetLayers, f.TargetLayers())
|
||||
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
|
||||
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}
|
||||
expectedTargetLayers = VideoLayers{
|
||||
Spatial: 1,
|
||||
@@ -241,7 +290,7 @@ func TestForwarderAllocate(t *testing.T) {
|
||||
}
|
||||
expectedResult = VideoAllocation{
|
||||
state: VideoAllocationStateOptimal,
|
||||
change: VideoStreamingChangeNone,
|
||||
change: VideoStreamingChangeResuming,
|
||||
bandwidthRequested: bitrates[1][3],
|
||||
bandwidthDelta: bitrates[1][3],
|
||||
availableLayers: []int32{0, 1, 2},
|
||||
@@ -249,14 +298,14 @@ func TestForwarderAllocate(t *testing.T) {
|
||||
targetLayers: expectedTargetLayers,
|
||||
distanceToDesired: 0,
|
||||
}
|
||||
result = f.AllocateOptimal(bitrates)
|
||||
result = f.AllocateOptimal(bitrates, true)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.Equal(t, InvalidLayers, f.CurrentLayers())
|
||||
require.Equal(t, expectedTargetLayers, f.TargetLayers())
|
||||
|
||||
// allocate using bitrates above maximum layer
|
||||
f.UpTrackLayersChange([]int32{2})
|
||||
// allocate using bitrates above maximum layer, allowing overshoot
|
||||
f.UpTrackLayersChange([]int32{2}, []int32{})
|
||||
sparseBitrates := Bitrates{
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0},
|
||||
@@ -274,13 +323,30 @@ func TestForwarderAllocate(t *testing.T) {
|
||||
availableLayers: []int32{2},
|
||||
bitrates: sparseBitrates,
|
||||
targetLayers: expectedTargetLayers,
|
||||
distanceToDesired: 0,
|
||||
distanceToDesired: -1,
|
||||
}
|
||||
result = f.AllocateOptimal(sparseBitrates)
|
||||
result = f.AllocateOptimal(sparseBitrates, true)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.Equal(t, InvalidLayers, f.CurrentLayers())
|
||||
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) {
|
||||
@@ -298,20 +364,20 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
|
||||
f.availableLayers = availableLayers
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
// 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)
|
||||
|
||||
// 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)
|
||||
f.targetLayers = InvalidLayers
|
||||
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)
|
||||
|
||||
// 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, f.lastAllocation)
|
||||
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) {
|
||||
@@ -373,10 +586,10 @@ func TestForwarderProvisionalAllocateMute(t *testing.T) {
|
||||
f.Mute(true)
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
// 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},
|
||||
bandwidthDelta: 1,
|
||||
}
|
||||
transition := f.ProvisionalAllocateGetCooperativeTransition()
|
||||
transition := f.ProvisionalAllocateGetCooperativeTransition(false)
|
||||
require.Equal(t, expectedTransition, transition)
|
||||
|
||||
// committing should set target to (0, 0)
|
||||
@@ -446,7 +659,7 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
|
||||
to: targetLayers,
|
||||
bandwidthDelta: 0,
|
||||
}
|
||||
transition = f.ProvisionalAllocateGetCooperativeTransition()
|
||||
transition = f.ProvisionalAllocateGetCooperativeTransition(false)
|
||||
require.Equal(t, expectedTransition, transition)
|
||||
|
||||
// committing should set target to (2, 1)
|
||||
@@ -474,7 +687,7 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
|
||||
to: VideoLayers{Spatial: 2, Temporal: 1},
|
||||
bandwidthDelta: 0,
|
||||
}
|
||||
transition = f.ProvisionalAllocateGetCooperativeTransition()
|
||||
transition = f.ProvisionalAllocateGetCooperativeTransition(false)
|
||||
require.Equal(t, expectedTransition, transition)
|
||||
|
||||
f.ProvisionalAllocateCommit()
|
||||
@@ -489,8 +702,133 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
|
||||
to: InvalidLayers,
|
||||
bandwidthDelta: -10,
|
||||
}
|
||||
transition = f.ProvisionalAllocateGetCooperativeTransition()
|
||||
transition = f.ProvisionalAllocateGetCooperativeTransition(false)
|
||||
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) {
|
||||
@@ -529,7 +867,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
{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.False(t, boosted)
|
||||
|
||||
@@ -539,7 +877,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
|
||||
// when not in deficient state, does not boost
|
||||
f.lastAllocation.state = VideoAllocationStateNone
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates)
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
|
||||
require.Equal(t, VideoAllocationDefault, result)
|
||||
require.False(t, boosted)
|
||||
|
||||
@@ -556,14 +894,14 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
targetLayers: InvalidLayers,
|
||||
distanceToDesired: 0,
|
||||
}
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates)
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.False(t, boosted)
|
||||
f.currentLayers.Spatial = 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, f.lastAllocation)
|
||||
require.False(t, boosted)
|
||||
@@ -582,7 +920,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
targetLayers: InvalidLayers,
|
||||
distanceToDesired: 0,
|
||||
}
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, emptyBitrates)
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, emptyBitrates, false)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.False(t, boosted)
|
||||
@@ -602,7 +940,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
targetLayers: expectedTargetLayers,
|
||||
distanceToDesired: 3,
|
||||
}
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates)
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.Equal(t, expectedTargetLayers, f.TargetLayers())
|
||||
@@ -624,7 +962,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
targetLayers: expectedTargetLayers,
|
||||
distanceToDesired: 2,
|
||||
}
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates)
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.Equal(t, expectedTargetLayers, f.TargetLayers())
|
||||
@@ -647,7 +985,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
targetLayers: expectedTargetLayers,
|
||||
distanceToDesired: 1,
|
||||
}
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates)
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.Equal(t, expectedTargetLayers, f.TargetLayers())
|
||||
@@ -669,7 +1007,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
targetLayers: expectedTargetLayers,
|
||||
distanceToDesired: 0,
|
||||
}
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates)
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
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
|
||||
f.currentLayers.Spatial = 2
|
||||
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, f.lastAllocation)
|
||||
require.Equal(t, expectedTargetLayers, f.TargetLayers())
|
||||
@@ -703,7 +1041,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
targetLayers: expectedTargetLayers,
|
||||
distanceToDesired: 4,
|
||||
}
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates)
|
||||
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.Equal(t, expectedTargetLayers, f.TargetLayers())
|
||||
@@ -720,11 +1058,44 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
|
||||
targetLayers: expectedTargetLayers,
|
||||
distanceToDesired: 4,
|
||||
}
|
||||
result, boosted = f.AllocateNextHigher(0, bitrates)
|
||||
result, boosted = f.AllocateNextHigher(0, bitrates, false)
|
||||
require.Equal(t, expectedResult, result)
|
||||
require.Equal(t, expectedResult, f.lastAllocation)
|
||||
require.Equal(t, expectedTargetLayers, f.TargetLayers())
|
||||
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) {
|
||||
@@ -741,7 +1112,7 @@ func TestForwarderPause(t *testing.T) {
|
||||
|
||||
f.availableLayers = availableLayers
|
||||
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)
|
||||
f.ProvisionalAllocateCommit()
|
||||
|
||||
@@ -775,7 +1146,7 @@ func TestForwarderPauseMute(t *testing.T) {
|
||||
|
||||
f.availableLayers = availableLayers
|
||||
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)
|
||||
f.ProvisionalAllocateCommit()
|
||||
|
||||
|
||||
+4
-6
@@ -381,10 +381,8 @@ func (w *WebRTCReceiver) AddDownTrack(track TrackSender) error {
|
||||
|
||||
if w.Kind() == webrtc.RTPCodecTypeVideo {
|
||||
// notify added down track of available layers
|
||||
layers := w.streamTrackerManager.GetAvailableLayers()
|
||||
if len(layers) != 0 {
|
||||
track.UpTrackLayersChange(layers)
|
||||
}
|
||||
availableLayers, exemptedLayers := w.streamTrackerManager.GetAvailableLayers()
|
||||
track.UpTrackLayersChange(availableLayers, exemptedLayers)
|
||||
}
|
||||
track.TrackInfoAvailable()
|
||||
|
||||
@@ -396,9 +394,9 @@ func (w *WebRTCReceiver) SetMaxExpectedSpatialLayer(layer int32) {
|
||||
w.streamTrackerManager.SetMaxExpectedSpatialLayer(layer)
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) downTrackLayerChange(layers []int32) {
|
||||
func (w *WebRTCReceiver) downTrackLayerChange(availableLayers []int32, exemptedLayers []int32) {
|
||||
for _, dt := range w.downTrackSpreader.GetDownTracks() {
|
||||
dt.UpTrackLayersChange(layers)
|
||||
dt.UpTrackLayersChange(availableLayers, exemptedLayers)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+24
-18
@@ -38,6 +38,12 @@ const (
|
||||
PriorityMax = uint8(255)
|
||||
PriorityDefaultScreenshare = PriorityMax
|
||||
PriorityDefaultVideo = PriorityMin
|
||||
|
||||
FlagAllowOvershootWhileOptimal = true
|
||||
FlagAllowOvershootWhileDeficient = false
|
||||
FlagAllowOvershootExemptTrackWhileDeficient = true
|
||||
FlagAllowOvershootInProbe = true
|
||||
FlagAllowOvershootInCatchup = true
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -707,7 +713,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
|
||||
// if not deficient, free pass allocate track
|
||||
if !s.params.Config.Enabled || s.state == StateStable || !track.IsManaged() {
|
||||
update := NewStreamStateUpdate()
|
||||
allocation := track.AllocateOptimal()
|
||||
allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal)
|
||||
update.HandleStreamingChange(allocation.change, track)
|
||||
s.maybeSendUpdate(update)
|
||||
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.
|
||||
//
|
||||
track.ProvisionalAllocatePrepare()
|
||||
transition := track.ProvisionalAllocateGetCooperativeTransition()
|
||||
transition := track.ProvisionalAllocateGetCooperativeTransition(FlagAllowOvershootWhileDeficient)
|
||||
|
||||
// track is currently streaming at minimum
|
||||
if transition.bandwidthDelta == 0 {
|
||||
@@ -848,7 +854,7 @@ func (s *StreamAllocator) maybeBoostDeficientTracks() {
|
||||
update := NewStreamStateUpdate()
|
||||
|
||||
for _, track := range s.getMaxDistanceSortedDeficient() {
|
||||
allocation, boosted := track.AllocateNextHigher(availableChannelCapacity)
|
||||
allocation, boosted := track.AllocateNextHigher(availableChannelCapacity, FlagAllowOvershootInCatchup)
|
||||
if !boosted {
|
||||
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.
|
||||
// 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()
|
||||
for _, track := range videoTracks {
|
||||
@@ -906,7 +912,7 @@ func (s *StreamAllocator) allocateAllTracks() {
|
||||
continue
|
||||
}
|
||||
|
||||
allocation := track.AllocateOptimal()
|
||||
allocation := track.AllocateOptimal(FlagAllowOvershootExemptTrackWhileDeficient)
|
||||
update.HandleStreamingChange(allocation.change, track)
|
||||
|
||||
// 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 {
|
||||
usedChannelCapacity := track.ProvisionalAllocate(availableChannelCapacity, layers, s.params.Config.AllowPause)
|
||||
usedChannelCapacity := track.ProvisionalAllocate(availableChannelCapacity, layers, s.params.Config.AllowPause, FlagAllowOvershootWhileDeficient)
|
||||
availableChannelCapacity -= usedChannelCapacity
|
||||
if availableChannelCapacity < 0 {
|
||||
availableChannelCapacity = 0
|
||||
@@ -1090,7 +1096,7 @@ func (s *StreamAllocator) maybeProbe() {
|
||||
func (s *StreamAllocator) maybeProbeWithMedia() {
|
||||
// boost deficient track farthest from desired layers
|
||||
for _, track := range s.getMaxDistanceSortedDeficient() {
|
||||
allocation, boosted := track.AllocateNextHigher(ChannelCapacityInfinity)
|
||||
allocation, boosted := track.AllocateNextHigher(ChannelCapacityInfinity, FlagAllowOvershootInCatchup)
|
||||
if !boosted {
|
||||
continue
|
||||
}
|
||||
@@ -1107,7 +1113,7 @@ func (s *StreamAllocator) maybeProbeWithMedia() {
|
||||
func (s *StreamAllocator) maybeProbeWithPadding() {
|
||||
// use deficient track farthest from desired layers to find how much to probe
|
||||
for _, track := range s.getMaxDistanceSortedDeficient() {
|
||||
transition, available := track.GetNextHigherTransition()
|
||||
transition, available := track.GetNextHigherTransition(FlagAllowOvershootInProbe)
|
||||
if !available || transition.bandwidthDelta < 0 {
|
||||
continue
|
||||
}
|
||||
@@ -1326,20 +1332,20 @@ func (t *Track) WritePaddingRTP(bytesToSend int) int {
|
||||
return t.downTrack.WritePaddingRTP(bytesToSend)
|
||||
}
|
||||
|
||||
func (t *Track) AllocateOptimal() VideoAllocation {
|
||||
return t.downTrack.AllocateOptimal()
|
||||
func (t *Track) AllocateOptimal(allowOvershoot bool) VideoAllocation {
|
||||
return t.downTrack.AllocateOptimal(allowOvershoot)
|
||||
}
|
||||
|
||||
func (t *Track) ProvisionalAllocatePrepare() {
|
||||
t.downTrack.ProvisionalAllocatePrepare()
|
||||
}
|
||||
|
||||
func (t *Track) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool) int64 {
|
||||
return t.downTrack.ProvisionalAllocate(availableChannelCapacity, layers, allowPause)
|
||||
func (t *Track) ProvisionalAllocate(availableChannelCapacity int64, layers VideoLayers, allowPause bool, allowOvershoot bool) int64 {
|
||||
return t.downTrack.ProvisionalAllocate(availableChannelCapacity, layers, allowPause, allowOvershoot)
|
||||
}
|
||||
|
||||
func (t *Track) ProvisionalAllocateGetCooperativeTransition() VideoTransition {
|
||||
return t.downTrack.ProvisionalAllocateGetCooperativeTransition()
|
||||
func (t *Track) ProvisionalAllocateGetCooperativeTransition(allowOvershoot bool) VideoTransition {
|
||||
return t.downTrack.ProvisionalAllocateGetCooperativeTransition(allowOvershoot)
|
||||
}
|
||||
|
||||
func (t *Track) ProvisionalAllocateGetBestWeightedTransition() VideoTransition {
|
||||
@@ -1350,12 +1356,12 @@ func (t *Track) ProvisionalAllocateCommit() VideoAllocation {
|
||||
return t.downTrack.ProvisionalAllocateCommit()
|
||||
}
|
||||
|
||||
func (t *Track) AllocateNextHigher(availableChannelCapacity int64) (VideoAllocation, bool) {
|
||||
return t.downTrack.AllocateNextHigher(availableChannelCapacity)
|
||||
func (t *Track) AllocateNextHigher(availableChannelCapacity int64, allowOvershoot bool) (VideoAllocation, bool) {
|
||||
return t.downTrack.AllocateNextHigher(availableChannelCapacity, allowOvershoot)
|
||||
}
|
||||
|
||||
func (t *Track) GetNextHigherTransition() (VideoTransition, bool) {
|
||||
return t.downTrack.GetNextHigherTransition()
|
||||
func (t *Track) GetNextHigherTransition(allowOvershoot bool) (VideoTransition, bool) {
|
||||
return t.downTrack.GetNextHigherTransition(allowOvershoot)
|
||||
}
|
||||
|
||||
func (t *Track) Pause() VideoAllocation {
|
||||
|
||||
@@ -13,7 +13,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
exemptLayers = []int32{0}
|
||||
ExemptedLayersScreenshare = []int32{0}
|
||||
ExemptedLayersVideo = []int32{}
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -71,10 +72,11 @@ type StreamTrackerManager struct {
|
||||
trackers [DefaultMaxLayerSpatial + 1]*StreamTracker
|
||||
|
||||
availableLayers []int32
|
||||
exemptedLayers []int32
|
||||
maxExpectedLayer int32
|
||||
paused bool
|
||||
|
||||
onAvailableLayersChanged func(availableLayers []int32)
|
||||
onAvailableLayersChanged func(availableLayers []int32, exemptedLayers []int32)
|
||||
onBitrateAvailabilityChanged func()
|
||||
onMaxLayerChanged func(maxLayer int32)
|
||||
}
|
||||
@@ -97,7 +99,7 @@ func NewStreamTrackerManager(logger logger.Logger, trackInfo *livekit.TrackInfo)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *StreamTrackerManager) OnAvailableLayersChanged(f func(availableLayers []int32)) {
|
||||
func (s *StreamTrackerManager) OnAvailableLayersChanged(f func(availableLayers []int32, exemptedLayers []int32)) {
|
||||
s.onAvailableLayersChanged = f
|
||||
}
|
||||
|
||||
@@ -130,27 +132,7 @@ func (s *StreamTrackerManager) AddTracker(layer int32) *StreamTracker {
|
||||
tracker.OnStatusChanged(func(status StreamStatus) {
|
||||
s.logger.Debugw("StreamTrackerManager OnStatusChanged", "layer", layer, "status", status)
|
||||
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)
|
||||
} else {
|
||||
s.logger.Debugw("not removing exempt layer", "layer", layer)
|
||||
}
|
||||
s.removeAvailableLayer(layer)
|
||||
} else {
|
||||
s.addAvailableLayer(layer)
|
||||
}
|
||||
@@ -187,6 +169,7 @@ func (s *StreamTrackerManager) RemoveAllTrackers() {
|
||||
s.trackers[layer] = nil
|
||||
}
|
||||
s.availableLayers = make([]int32, 0)
|
||||
s.exemptedLayers = make([]int32, 0)
|
||||
s.maxExpectedLayer = DefaultMaxLayerSpatial
|
||||
s.paused = false
|
||||
s.lock.Unlock()
|
||||
@@ -330,11 +313,17 @@ func (s *StreamTrackerManager) GetBitrateTemporalCumulative() Bitrates {
|
||||
return br
|
||||
}
|
||||
|
||||
func (s *StreamTrackerManager) GetAvailableLayers() []int32 {
|
||||
func (s *StreamTrackerManager) GetAvailableLayers() ([]int32, []int32) {
|
||||
s.lock.RLock()
|
||||
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 {
|
||||
@@ -371,18 +360,30 @@ func (s *StreamTrackerManager) addAvailableLayer(layer int32) {
|
||||
s.availableLayers = append(s.availableLayers, layer)
|
||||
sort.Slice(s.availableLayers, func(i, j int) bool { return s.availableLayers[i] < s.availableLayers[j] })
|
||||
|
||||
var layers []int32
|
||||
layers = append(layers, s.availableLayers...)
|
||||
for idx, el := range s.exemptedLayers {
|
||||
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.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 {
|
||||
s.onAvailableLayersChanged(layers)
|
||||
s.onAvailableLayersChanged(availableLayers, exemptedLayers)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
@@ -394,15 +395,56 @@ func (s *StreamTrackerManager) removeAvailableLayer(layer int32) {
|
||||
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)
|
||||
for _, l := range s.availableLayers {
|
||||
if l != layer {
|
||||
if exempt || l != layer {
|
||||
newLayers = append(newLayers, l)
|
||||
}
|
||||
}
|
||||
sort.Slice(newLayers, func(i, j int) bool { return newLayers[i] < newLayers[j] })
|
||||
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
|
||||
if len(s.availableLayers) > 0 {
|
||||
curMaxLayer = s.availableLayers[len(s.availableLayers)-1]
|
||||
@@ -413,7 +455,7 @@ func (s *StreamTrackerManager) removeAvailableLayer(layer int32) {
|
||||
|
||||
// need to immediately switch off unavailable layers
|
||||
if s.onAvailableLayersChanged != nil {
|
||||
s.onAvailableLayersChanged(newLayers)
|
||||
s.onAvailableLayersChanged(newLayers, exemptedLayers)
|
||||
}
|
||||
|
||||
// if maxLayer was removed, send the new maxLayer
|
||||
|
||||
Reference in New Issue
Block a user