Deficient state handling when a track needs a change (#261)
* WIP commit * deficient handling * Add missing ProvisionalAllocatePrepare * adjust state on track removal * Increase test timeout * - Add comments about cooperative routines - Take down transition if available in cooperative scheme - Use layer comparison when taking down transition. Because of when the bitrate is measured, it is not always guaranteed bandwidthDelta is -ve when moving down. - Do not add track to stream allocator till bind. * make comment better * a bit more clear comments * Use OnBind on subscribed track
This commit is contained in:
+206
-13
@@ -112,18 +112,16 @@ type VideoAllocationProvisional struct {
|
||||
bitrates Bitrates
|
||||
}
|
||||
|
||||
const (
|
||||
SpatialTransitionCost = 10
|
||||
TemporalTransitionCost = 0
|
||||
)
|
||||
|
||||
type VideoAllocationMove struct {
|
||||
layers VideoLayers
|
||||
deltaBitrate int64
|
||||
transitionCost int32
|
||||
qualityCost int32
|
||||
type VideoTransition struct {
|
||||
from VideoLayers
|
||||
to VideoLayers
|
||||
bandwidthDelta int64
|
||||
}
|
||||
|
||||
const (
|
||||
TransitionCostSpatial = 10
|
||||
)
|
||||
|
||||
type TranslationParams struct {
|
||||
shouldDrop bool
|
||||
isDroppingRelevant bool
|
||||
@@ -520,6 +518,203 @@ func (f *Forwarder) ProvisionalAllocate(availableChannelCapacity int64, layers V
|
||||
return 0
|
||||
}
|
||||
|
||||
func (f *Forwarder) ProvisionalAllocateGetCooperativeTransition() VideoTransition {
|
||||
//
|
||||
// This is called when a track needs a change (could be mute/unmute, subscribed layers changed, published layers changed)
|
||||
// when channel is congested.
|
||||
//
|
||||
// The goal is to provide a co-operative transition. Co-operative stream allocation aims to keep all the streams active
|
||||
// as much as possible.
|
||||
//
|
||||
// When channel is congested, effecting a transition which will consume more bits will lead to more congestion.
|
||||
// So, this routine does the following
|
||||
// 1. When muting, it is not going to increase consumption.
|
||||
// 2. If the stream is currently active and the transition needs more bits (higher layers = more bits), do not make the up move.
|
||||
// The higher layer requirement could be due to a new published layer becoming available or subscribed layers changing.
|
||||
// 3. If the new target layers are lower than current target, take the move down and save bits.
|
||||
// 4. If not currently streaming, find the minimum layers that can unpause the stream.
|
||||
//
|
||||
// To summarize, co-operative streaming means
|
||||
// - Try to keep tracks streaming, i. e. no pauses even if not at optimal layers
|
||||
// - Do not make an upgrade as it could affect other tracks
|
||||
//
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.provisional.muted {
|
||||
f.provisional.layers = InvalidLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: InvalidLayers,
|
||||
bandwidthDelta: 0 - f.lastAllocation.bandwidthRequested,
|
||||
// LK-TODO should this take current bitrate of current target layers?
|
||||
}
|
||||
}
|
||||
|
||||
// check if we should preserve current target
|
||||
if f.targetLayers != InvalidLayers {
|
||||
// what is the highest that is available
|
||||
maximalLayers := InvalidLayers
|
||||
maximalBandwidthRequired := int64(0)
|
||||
for s := f.maxLayers.spatial; s >= 0; s-- {
|
||||
for t := f.maxLayers.temporal; t >= 0; t-- {
|
||||
if f.provisional.bitrates[s][t] != 0 {
|
||||
maximalLayers = VideoLayers{spatial: s, temporal: t}
|
||||
maximalBandwidthRequired = f.provisional.bitrates[s][t]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if maximalBandwidthRequired != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if maximalLayers != InvalidLayers {
|
||||
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
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: f.targetLayers,
|
||||
bandwidthDelta: 0,
|
||||
}
|
||||
}
|
||||
|
||||
if f.targetLayers.GreaterThan(maximalLayers) {
|
||||
// maximalLayers <= f.targetLayers, make the down move
|
||||
f.provisional.layers = maximalLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: maximalLayers,
|
||||
bandwidthDelta: maximalBandwidthRequired - f.lastAllocation.bandwidthRequested,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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.maxLayers.spatial; s++ {
|
||||
for t := int32(0); s <= f.maxLayers.temporal; t++ {
|
||||
if f.provisional.bitrates[s][t] != 0 {
|
||||
minimalLayers = VideoLayers{spatial: s, temporal: t}
|
||||
bandwidthRequired = f.provisional.bitrates[s][t]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if bandwidthRequired != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
targetLayers := f.targetLayers
|
||||
if targetLayers == InvalidLayers || targetLayers.GreaterThan(minimalLayers) || (f.provisional.bitrates[targetLayers.spatial][targetLayers.temporal] == 0) {
|
||||
targetLayers = minimalLayers
|
||||
}
|
||||
|
||||
f.provisional.layers = targetLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: targetLayers,
|
||||
bandwidthDelta: bandwidthRequired - f.lastAllocation.bandwidthRequested,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Forwarder) ProvisionalAllocateGetBestWeightedTransition() VideoTransition {
|
||||
//
|
||||
// This is called when a track needs a change (could be mute/unmute, subscribed layers changed, published layers changed)
|
||||
// when channel is congested.
|
||||
//
|
||||
// The goal is to keep all tracks streaming as much as possible. So, the track that needs a change needs bits to be unpaused.
|
||||
//
|
||||
// This tries to figure out how much this track can contribute back to the pool to enable the track that needs to be unpaused.
|
||||
// 1. Track muted OR feed dry - can contribute everything back in case it was using bits.
|
||||
// 2. Look at all possible down transitions from current target and find the best offer.
|
||||
// Best offer is calculated as bits saved moving to a down layer divided by cost.
|
||||
// Cost has two components
|
||||
// a. Transition cost: Spatial layer switch is expensive due to key frame requiremnt, but temporal layer switch is free.
|
||||
// b. Quality cost: The farther away from desired layers, the higher the quality cost.
|
||||
//
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.provisional.muted {
|
||||
f.provisional.layers = InvalidLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: InvalidLayers,
|
||||
bandwidthDelta: 0 - f.lastAllocation.bandwidthRequested,
|
||||
// LK-TODO should this take current bitrate of current target layers?
|
||||
}
|
||||
}
|
||||
|
||||
maxReachableLayerTemporal := int32(-1)
|
||||
for t := f.maxLayers.temporal; t >= 0; t-- {
|
||||
for s := f.maxLayers.spatial; s >= 0; s-- {
|
||||
if f.provisional.bitrates[s][t] != 0 {
|
||||
maxReachableLayerTemporal = t
|
||||
break
|
||||
}
|
||||
}
|
||||
if maxReachableLayerTemporal != -1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if maxReachableLayerTemporal == -1 {
|
||||
// feed has gone dry,
|
||||
f.provisional.layers = InvalidLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: InvalidLayers,
|
||||
bandwidthDelta: 0 - f.lastAllocation.bandwidthRequested,
|
||||
}
|
||||
}
|
||||
|
||||
// starting from mimimum to target, find transition which gives the best
|
||||
// transition taking into account bits saved vs cost of such a transition
|
||||
bestLayers := InvalidLayers
|
||||
bestBandwidthDelta := int64(0)
|
||||
bestValue := float32(0)
|
||||
for s := int32(0); s <= f.targetLayers.spatial; s++ {
|
||||
for t := int32(0); t <= f.targetLayers.temporal; t++ {
|
||||
if s == f.targetLayers.spatial && t == f.targetLayers.temporal {
|
||||
break
|
||||
}
|
||||
|
||||
bandwidthDelta := int64(math.Max(float64(0), float64(f.lastAllocation.bandwidthRequested-f.provisional.bitrates[s][t])))
|
||||
|
||||
transitionCost := int32(0)
|
||||
if f.targetLayers.spatial != s {
|
||||
transitionCost = TransitionCostSpatial
|
||||
}
|
||||
|
||||
qualityCost := (maxReachableLayerTemporal+1)*(f.targetLayers.spatial-s) + (f.targetLayers.temporal - t)
|
||||
|
||||
value := float32(0)
|
||||
if (transitionCost + qualityCost) != 0 {
|
||||
value = float32(bandwidthDelta) / float32(transitionCost+qualityCost)
|
||||
}
|
||||
if value > bestValue || (value == bestValue && bandwidthDelta > bestBandwidthDelta) {
|
||||
bestValue = value
|
||||
bestBandwidthDelta = bandwidthDelta
|
||||
bestLayers = VideoLayers{spatial: s, temporal: t}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
f.provisional.layers = bestLayers
|
||||
return VideoTransition{
|
||||
from: f.targetLayers,
|
||||
to: bestLayers,
|
||||
bandwidthDelta: bestBandwidthDelta,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
@@ -531,7 +726,7 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
|
||||
bandwidthRequested := int64(0)
|
||||
|
||||
switch {
|
||||
case f.muted:
|
||||
case f.provisional.muted:
|
||||
state = VideoAllocationStateMuted
|
||||
case optimalBandwidthNeeded == 0:
|
||||
if len(f.availableLayers) == 0 {
|
||||
@@ -582,8 +777,6 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
|
||||
f.currentLayers = InvalidLayers
|
||||
}
|
||||
|
||||
f.provisional = nil
|
||||
|
||||
return f.lastAllocation
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user