Cleaning up availableLayers and exemptedLayers (#1407)

* WIP commit

* Send stream state paused only when it is paused due to bandwidth limitation.

When stream is resumed after a stream is paused, an active update is
sent. Note that this means if there are intervening events like
mute/unmute between pause and resume, resume will be sent.

* WIP commit

* fix compile

* WIP commit

* fixing tests

* clean up exempted layers

* clean up unused stuff

* correct comment

* Don't need ops queue as order is not important now

* static check

* kick off allocation when callbacks are set up, calling from receiver means callbacks may not be set up
This commit is contained in:
Raja Subramanian
2023-02-17 13:53:11 +05:30
committed by GitHub
parent 9f94fc8347
commit 85a23bfffc
7 changed files with 371 additions and 583 deletions
-3
View File
@@ -165,7 +165,6 @@ type StreamTrackerFrameConfig struct {
type StreamTrackerConfig struct {
StreamTrackerType StreamTrackerType `yaml:"stream_tracker_type,omitempty"`
BitrateReportInterval map[int32]time.Duration `yaml:"bitrate_report_interval,omitempty"`
ExemptedLayers []int32 `yaml:"exempted_layers,omitempty"`
PacketTracker map[int32]StreamTrackerPacketConfig `yaml:"packet_tracker,omitempty"`
FrameTracker map[int32]StreamTrackerFrameConfig `yaml:"frame_tracker,omitempty"`
}
@@ -304,7 +303,6 @@ func NewConfig(confString string, strictMode bool, c *cli.Context, baseFlags []c
1: 1 * time.Second,
2: 1 * time.Second,
},
ExemptedLayers: []int32{},
PacketTracker: map[int32]StreamTrackerPacketConfig{
0: StreamTrackerPacketConfig{
SamplesRequired: 1,
@@ -341,7 +339,6 @@ func NewConfig(confString string, strictMode bool, c *cli.Context, baseFlags []c
1: 4 * time.Second,
2: 4 * time.Second,
},
ExemptedLayers: []int32{0},
PacketTracker: map[int32]StreamTrackerPacketConfig{
0: StreamTrackerPacketConfig{
SamplesRequired: 1,
+11 -14
View File
@@ -27,7 +27,7 @@ import (
// TrackSender defines an interface send media to remote peer
type TrackSender interface {
UpTrackLayersChange(availableLayers []int32, exemptedLayers []int32)
UpTrackLayersChange()
UpTrackBitrateAvailabilityChange()
WriteRTP(p *buffer.ExtPacket, layer int32) error
Close()
@@ -555,12 +555,12 @@ func (d *DownTrack) WriteRTP(extPkt *buffer.ExtPacket, layer int32) error {
d.stopKeyFrameRequester()
}
if !tp.isSwitchingToTargetLayer {
if extPkt.KeyFrame {
d.logger.Debugw("forwarding key frame", "layer", layer)
} else {
if d.onTargetLayerFound != nil {
d.onTargetLayerFound(d)
}
}
if tp.isSwitchingToTargetLayer && d.onTargetLayerFound != nil {
d.onTargetLayerFound(d)
}
}
@@ -859,13 +859,7 @@ func (d *DownTrack) SeedState(state DownTrackState) {
d.forwarder.SeedState(state.ForwarderState)
}
func (d *DownTrack) GetForwardingStatus() ForwardingStatus {
return d.forwarder.GetForwardingStatus()
}
func (d *DownTrack) UpTrackLayersChange(availableLayers []int32, exemptedLayers []int32) {
d.forwarder.UpTrackLayersChange(availableLayers, exemptedLayers)
func (d *DownTrack) UpTrackLayersChange() {
if onAvailableLayersChanged, ok := d.onAvailableLayersChanged.Load().(func(dt *DownTrack)); ok {
onAvailableLayersChanged(d)
}
@@ -911,6 +905,9 @@ func (d *DownTrack) OnBitrateAvailabilityChanged(fn func(dt *DownTrack)) {
func (d *DownTrack) OnSubscriptionChanged(fn func(dt *DownTrack)) {
d.onSubscriptionChanged = fn
// kick off an allocation just in case other events happened before callbacks were set up
go fn(d)
}
func (d *DownTrack) OnSubscribedLayersChanged(fn func(dt *DownTrack, layers VideoLayers)) {
@@ -1220,7 +1217,7 @@ func (d *DownTrack) handleRTCP(bytes []byte) {
sendPliOnce := func() {
if pliOnce {
targetLayers := d.forwarder.TargetLayers()
if targetLayers != InvalidLayers {
if targetLayers != InvalidLayers && !d.forwarder.IsAnyMuted() {
d.logger.Debugw("sending PLI RTCP", "layer", targetLayers.Spatial)
d.receiver.SendPLI(targetLayers.Spatial, false)
d.isNACKThrottled.Store(true)
+127 -167
View File
@@ -25,16 +25,6 @@ const (
// -------------------------------------------------------------------
type ForwardingStatus int
const (
ForwardingStatusOff ForwardingStatus = iota
ForwardingStatusPartial
ForwardingStatusOptimal
)
// -------------------------------------------------------------------
type VideoPauseReason int
const (
@@ -69,21 +59,22 @@ type VideoAllocation struct {
isDeficient bool
bandwidthRequested int64
bandwidthDelta int64
availableLayers []int32
exemptedLayers []int32
bitrates Bitrates
targetLayers VideoLayers
maxLayers VideoLayers
distanceToDesired int32
}
func (v VideoAllocation) String() string {
return fmt.Sprintf("VideoAllocation{pause: %s, def: %+v, bw: %d, del: %d, avail: %+v, exempt: %+v, rates: %+v, target: %s, dist: %d}",
v.pauseReason, v.isDeficient, v.bandwidthRequested, v.bandwidthDelta, v.availableLayers, v.exemptedLayers, v.bitrates, v.targetLayers, v.distanceToDesired)
return fmt.Sprintf("VideoAllocation{pause: %s, def: %+v, bw: %d, del: %d, rates: %+v, target: %s, max: %s, dist: %d}",
v.pauseReason, v.isDeficient, v.bandwidthRequested, v.bandwidthDelta, v.bitrates, v.targetLayers, v.maxLayers, v.distanceToDesired)
}
var (
VideoAllocationDefault = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry, // start with no feed till feed is seen
targetLayers: InvalidLayers,
maxLayers: InvalidLayers,
}
)
@@ -93,8 +84,6 @@ type VideoAllocationProvisional struct {
muted bool
pubMuted bool
bitrates Bitrates
availableLayers []int32
exemptedLayers []int32
maxLayers VideoLayers
currentLayers VideoLayers
parkedLayers VideoLayers
@@ -144,6 +133,11 @@ const (
var (
InvalidLayers = buffer.InvalidLayers
DefaultMaxLayers = VideoLayers{
Spatial: DefaultMaxLayerSpatial,
Temporal: DefaultMaxLayerTemporal,
}
)
// -------------------------------------------------------------------
@@ -186,9 +180,6 @@ type Forwarder struct {
lastAllocation VideoAllocation
availableLayers []int32
exemptedLayers []int32
rtpMunger *RTPMunger
vp8Munger *VP8Munger
@@ -361,6 +352,13 @@ func (f *Forwarder) IsPubMuted() bool {
return f.pubMuted
}
func (f *Forwarder) IsAnyMuted() bool {
f.lock.RLock()
defer f.lock.RUnlock()
return f.muted || f.pubMuted
}
func (f *Forwarder) SetMaxSpatialLayer(spatialLayer int32) (bool, VideoLayers, VideoLayers) {
f.lock.Lock()
defer f.lock.Unlock()
@@ -421,30 +419,11 @@ func (f *Forwarder) GetReferenceLayerSpatial() int32 {
return f.referenceLayerSpatial
}
func (f *Forwarder) GetForwardingStatus() ForwardingStatus {
f.lock.RLock()
defer f.lock.RUnlock()
if f.muted || f.pubMuted || len(f.availableLayers) == 0 {
return ForwardingStatusOptimal
}
if f.targetLayers == InvalidLayers {
return ForwardingStatusOff
}
if f.targetLayers.Spatial < f.maxLayers.Spatial && f.targetLayers.Spatial < f.availableLayers[len(f.availableLayers)-1] {
return ForwardingStatusPartial
}
return ForwardingStatusOptimal
}
func (f *Forwarder) IsReducedQuality() (int32, bool) {
f.lock.RLock()
defer f.lock.RUnlock()
if f.muted || f.pubMuted || len(f.availableLayers) == 0 || f.targetLayers.Spatial == InvalidLayerSpatial {
if f.muted || f.pubMuted || f.lastAllocation.pauseReason == VideoPauseReasonFeedDry || f.targetLayers.Spatial == InvalidLayerSpatial {
return 0, false
}
@@ -464,25 +443,6 @@ func (f *Forwarder) IsReducedQuality() (int32, bool) {
return distance, f.isDeficientLocked()
}
func (f *Forwarder) UpTrackLayersChange(availableLayers []int32, exemptedLayers []int32) {
f.lock.Lock()
defer f.lock.Unlock()
if len(availableLayers) > 0 {
f.availableLayers = make([]int32, len(availableLayers))
copy(f.availableLayers, availableLayers)
} 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, maxLayers VideoLayers) int64 {
if f.muted || f.pubMuted {
return 0
@@ -506,42 +466,36 @@ func (f *Forwarder) getOptimalBandwidthNeeded(brs Bitrates, maxLayers VideoLayer
return 0
}
func (f *Forwarder) bitrateAvailable(brs Bitrates) bool {
neededLayers := 0
bitrateAvailableLayers := 0
for _, layer := range f.availableLayers {
if layer > f.maxLayers.Spatial {
continue
}
func (f *Forwarder) getLayerChanges(brs Bitrates) (addedLayers []int32, removedLayers []int32) {
lastAllocationLayers := f.lastAllocation.bitrates.GetLayers()
nowLayers := brs.GetLayers()
//
// 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
for _, ln := range nowLayers {
found := false
for _, lla := range lastAllocationLayers {
if lla == ln {
found = true
break
}
}
if exempt {
continue
}
neededLayers++
for t := f.maxLayers.Temporal; t >= 0; t-- {
if brs[layer][t] != 0 {
bitrateAvailableLayers++
break
}
if !found {
addedLayers = append(addedLayers, ln)
}
}
return bitrateAvailableLayers == neededLayers
for _, lla := range lastAllocationLayers {
found := false
for _, ln := range nowLayers {
if ln == lla {
found = true
break
}
}
if !found {
removedLayers = append(removedLayers, lla)
}
}
return
}
func (f *Forwarder) getDistanceToDesired(brs Bitrates, targetLayers VideoLayers, maxLayers VideoLayers) int32 {
@@ -623,11 +577,13 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates, allowOvershoot bool) VideoAllo
}
alloc := VideoAllocation{
pauseReason: VideoPauseReasonNone,
availableLayers: f.availableLayers,
exemptedLayers: f.exemptedLayers,
bitrates: brs,
targetLayers: InvalidLayers,
pauseReason: VideoPauseReasonNone,
bitrates: brs,
targetLayers: InvalidLayers,
maxLayers: f.maxLayers,
}
if f.getOptimalBandwidthNeeded(brs, f.maxLayers) == 0 {
alloc.pauseReason = VideoPauseReasonFeedDry
}
switch {
@@ -643,53 +599,51 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates, allowOvershoot bool) VideoAllo
// if parked on a layer, let it continue
alloc.targetLayers = f.parkedLayers
case !f.bitrateAvailable(brs):
// Feed bitrate not yet calculated for all available layers
//
// Target the highest layer available <= max subscribed layer.
//
// If target already set, move allocation to the highest available layer <= max subscribed layer only if that is higher than existing target.
// It is possible that in situations like coming out of publisher mute, target is already set higher due to parked layers.
if f.availableLayers[len(f.availableLayers)-1] > f.targetLayers.Spatial {
alloc.targetLayers = VideoLayers{
Spatial: int32(math.Min(float64(f.maxLayers.Spatial), float64(f.availableLayers[len(f.availableLayers)-1]))),
Temporal: int32(math.Max(0, float64(f.maxLayers.Temporal))),
case !f.targetLayers.IsValid():
if f.maxLayers.IsValid() {
if allowOvershoot {
alloc.targetLayers = VideoLayers{
Spatial: int32(math.Max(0, float64(f.numAdvertisedLayers-1))),
Temporal: DefaultMaxLayerTemporal,
}
} else {
alloc.targetLayers = f.maxLayers
}
} else {
alloc.targetLayers = f.targetLayers
}
case f.lastAllocation.maxLayers != f.maxLayers:
alloc.targetLayers = f.maxLayers
default:
// allocate best layer available
for s := f.maxLayers.Spatial; s >= 0; s-- {
for t := f.maxLayers.Temporal; t >= 0; t-- {
if brs[s][t] == 0 {
continue
}
doAlloc := false
added, removed := f.getLayerChanges(brs)
alloc.targetLayers = VideoLayers{
Spatial: s,
Temporal: t,
}
alloc.bandwidthRequested = brs[s][t]
break
}
if alloc.bandwidthRequested != 0 {
// check for an added higher than current target
for _, l := range added {
if l > f.targetLayers.Spatial {
doAlloc = true
break
}
}
if alloc.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 := f.maxLayers.Spatial + 1; s <= DefaultMaxLayerSpatial; s++ {
for t := int32(0); t <= DefaultMaxLayerTemporal; t++ {
// check for current target being removed
if !doAlloc {
for _, l := range removed {
if l == f.targetLayers.Spatial {
doAlloc = true
break
}
}
}
if !doAlloc {
// no layer changes, leave target as is
alloc.targetLayers = f.targetLayers
alloc.bandwidthRequested = brs[alloc.targetLayers.Spatial][alloc.targetLayers.Temporal]
} else {
// allocate best layer available
for s := f.maxLayers.Spatial; s >= 0; s-- {
for t := f.maxLayers.Temporal; t >= 0; t-- {
if brs[s][t] == 0 {
continue
}
@@ -700,7 +654,6 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates, allowOvershoot bool) VideoAllo
}
alloc.bandwidthRequested = brs[s][t]
f.logger.Infow("allowing overshoot", "maxLayer", f.maxLayers, "targetLayers", alloc.targetLayers)
break
}
@@ -708,18 +661,47 @@ func (f *Forwarder) AllocateOptimal(brs Bitrates, allowOvershoot bool) VideoAllo
break
}
}
}
// feed may be dry, leave target at current if already started for opportunistic resume
if alloc.bandwidthRequested == 0 && f.maxLayers.IsValid() {
alloc.pauseReason = VideoPauseReasonFeedDry
if f.started {
alloc.targetLayers = f.currentLayers
} else {
// opportunisitically latch on to anything
alloc.targetLayers = VideoLayers{
Spatial: int32(math.Max(0, float64(f.numAdvertisedLayers-1))),
Temporal: DefaultMaxLayerTemporal,
if alloc.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 := f.maxLayers.Spatial + 1; s <= DefaultMaxLayerSpatial; s++ {
for t := int32(0); t <= DefaultMaxLayerTemporal; t++ {
if brs[s][t] == 0 {
continue
}
alloc.targetLayers = VideoLayers{
Spatial: s,
Temporal: t,
}
alloc.bandwidthRequested = brs[s][t]
alloc.pauseReason = VideoPauseReasonNone
f.logger.Infow("allowing overshoot", "maxLayer", f.maxLayers, "targetLayers", alloc.targetLayers)
break
}
if alloc.bandwidthRequested != 0 {
break
}
}
}
// feed may be dry, leave target at current if already started for opportunistic resume
if alloc.bandwidthRequested == 0 && f.maxLayers.IsValid() {
if f.started && f.currentLayers.IsValid() {
alloc.targetLayers = f.currentLayers
} else {
// opportunisitically latch on to anything
alloc.targetLayers = VideoLayers{
Spatial: int32(math.Max(0, float64(f.numAdvertisedLayers-1))),
Temporal: DefaultMaxLayerTemporal,
}
}
}
}
@@ -747,14 +729,6 @@ func (f *Forwarder) ProvisionalAllocatePrepare(bitrates Bitrates) {
currentLayers: f.currentLayers,
parkedLayers: f.parkedLayers,
}
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, allowOvershoot bool) int64 {
@@ -1043,10 +1017,9 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
alloc := VideoAllocation{
bandwidthRequested: 0,
bandwidthDelta: -f.lastAllocation.bandwidthRequested,
availableLayers: f.provisional.availableLayers,
exemptedLayers: f.provisional.exemptedLayers,
bitrates: f.provisional.bitrates,
targetLayers: f.provisional.allocatedLayers,
maxLayers: f.provisional.maxLayers,
distanceToDesired: f.getDistanceToDesired(f.provisional.bitrates, f.provisional.allocatedLayers, f.provisional.maxLayers),
}
@@ -1063,7 +1036,6 @@ func (f *Forwarder) ProvisionalAllocateCommit() VideoAllocation {
alloc.bandwidthRequested = f.provisional.bitrates[f.provisional.allocatedLayers.Spatial][f.provisional.allocatedLayers.Temporal]
alloc.bandwidthDelta = alloc.bandwidthRequested - f.lastAllocation.bandwidthRequested
} else {
// some exempt layer may be available, but technically the feed is dry
alloc.pauseReason = VideoPauseReasonFeedDry
// leave target at current for opportunistic forwarding
@@ -1141,10 +1113,9 @@ func (f *Forwarder) AllocateNextHigher(availableChannelCapacity int64, brs Bitra
isDeficient: true,
bandwidthRequested: bandwidthRequested,
bandwidthDelta: bandwidthRequested - alreadyAllocated,
availableLayers: f.availableLayers,
exemptedLayers: f.exemptedLayers,
bitrates: brs,
targetLayers: targetLayers,
maxLayers: f.maxLayers,
distanceToDesired: f.getDistanceToDesired(brs, targetLayers, f.maxLayers),
}
if targetLayers.GreaterThan(f.maxLayers) || bandwidthRequested >= optimalBandwidthNeeded {
@@ -1286,10 +1257,9 @@ func (f *Forwarder) Pause(brs Bitrates) VideoAllocation {
alloc := VideoAllocation{
bandwidthRequested: 0,
bandwidthDelta: 0 - f.lastAllocation.bandwidthRequested,
availableLayers: f.availableLayers,
exemptedLayers: f.exemptedLayers,
bitrates: brs,
targetLayers: InvalidLayers,
maxLayers: f.maxLayers,
distanceToDesired: f.getDistanceToDesired(brs, InvalidLayers, f.maxLayers),
}
@@ -1301,7 +1271,6 @@ func (f *Forwarder) Pause(brs Bitrates) VideoAllocation {
alloc.pauseReason = VideoPauseReasonPubMuted
case f.getOptimalBandwidthNeeded(brs, f.maxLayers) == 0:
// some exempt layer may be available, but technically the feed is dry
alloc.pauseReason = VideoPauseReasonFeedDry
default:
@@ -1320,16 +1289,7 @@ func (f *Forwarder) updateAllocation(alloc VideoAllocation, reason string) Video
alloc.targetLayers != f.lastAllocation.targetLayers {
f.logger.Infow(fmt.Sprintf("stream allocation: %s", reason), "allocation", alloc)
}
f.lastAllocation = alloc
if len(alloc.availableLayers) > 0 {
f.lastAllocation.availableLayers = make([]int32, len(alloc.availableLayers))
copy(f.lastAllocation.availableLayers, alloc.availableLayers)
}
if len(alloc.exemptedLayers) > 0 {
f.lastAllocation.exemptedLayers = make([]int32, len(alloc.exemptedLayers))
copy(f.lastAllocation.exemptedLayers, alloc.exemptedLayers)
}
f.setTargetLayers(f.lastAllocation.targetLayers)
if f.targetLayers == InvalidLayers {
+177 -275
View File
@@ -110,61 +110,6 @@ func TestForwarderLayersVideo(t *testing.T) {
require.Equal(t, VideoLayers{Spatial: 0, Temporal: 1}, currentLayers)
}
func TestForwarderGetForwardingStatus(t *testing.T) {
f := newForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo)
f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
// no available layers, should be optimal
require.Equal(t, ForwardingStatusOptimal, f.GetForwardingStatus())
// with available layers, should be off
availableLayers := []int32{0, 1, 2}
f.UpTrackLayersChange(availableLayers, []int32{})
require.Equal(t, ForwardingStatusOff, f.GetForwardingStatus())
// when muted, should be optimal
f.Mute(true)
require.Equal(t, ForwardingStatusOptimal, f.GetForwardingStatus())
// when target is the max, should be optimal
f.Mute(false)
f.targetLayers.Spatial = DefaultMaxLayerSpatial
require.Equal(t, ForwardingStatusOptimal, f.GetForwardingStatus())
// when target is less than max subscribed and max available, should be partial
f.targetLayers.Spatial = DefaultMaxLayerSpatial - 1
require.Equal(t, ForwardingStatusPartial, f.GetForwardingStatus())
// when available layers are lower than max subscribed, optimal as long as target is at max available
availableLayers = []int32{0, 1}
f.UpTrackLayersChange(availableLayers, []int32{})
require.Equal(t, ForwardingStatusOptimal, f.GetForwardingStatus())
}
func TestForwarderUpTrackLayersChange(t *testing.T) {
f := newForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo)
require.Nil(t, f.availableLayers)
availableLayers := []int32{0, 1, 2}
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, []int32{})
require.Equal(t, availableLayers, f.availableLayers)
require.Nil(t, f.exemptedLayers)
availableLayers = []int32{}
exemptedLayers = []int32{}
f.UpTrackLayersChange(availableLayers, exemptedLayers)
require.Nil(t, f.availableLayers)
require.Nil(t, f.exemptedLayers)
}
func TestForwarderAllocateOptimal(t *testing.T) {
f := newForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo)
f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
@@ -184,9 +129,9 @@ func TestForwarderAllocateOptimal(t *testing.T) {
pauseReason: VideoPauseReasonMuted,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: bitrates,
targetLayers: InvalidLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 0,
}
result := f.AllocateOptimal(bitrates, true)
@@ -195,7 +140,27 @@ func TestForwarderAllocateOptimal(t *testing.T) {
f.Mute(false)
// feed dry, target set to maximum for opportunistic start
// when parked layers valid, should stay there
f.parkedLayers = VideoLayers{
Spatial: 0,
Temporal: 1,
}
expectedResult = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry,
bandwidthRequested: 0,
bandwidthDelta: 0,
bitrates: emptyBitrates,
targetLayers: f.parkedLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(emptyBitrates, true)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, f.parkedLayers, f.TargetLayers())
f.parkedLayers = InvalidLayers
// when target is invalid, should set up for opportunistic forwarding
f.SetNumAdvertisedLayers(3)
disable(f)
expectedTargetLayers := VideoLayers{
@@ -206,9 +171,9 @@ func TestForwarderAllocateOptimal(t *testing.T) {
pauseReason: VideoPauseReasonFeedDry,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: emptyBitrates,
targetLayers: expectedTargetLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(emptyBitrates, true)
@@ -216,127 +181,63 @@ func TestForwarderAllocateOptimal(t *testing.T) {
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
f.parkedLayers = VideoLayers{
f.targetLayers = VideoLayers{Spatial: 0, Temporal: 0} // set to valid to trigger paths in tests below
// max layers changing should adopt that
f.SetMaxSpatialLayer(0)
f.SetMaxTemporalLayer(3)
expectedTargetLayers = VideoLayers{
Spatial: 0,
Temporal: DefaultMaxLayerTemporal,
}
expectedMaxLayers := VideoLayers{
Spatial: 0,
Temporal: DefaultMaxLayerTemporal,
}
expectedResult = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry,
bandwidthRequested: 0,
bandwidthDelta: 0,
bitrates: emptyBitrates,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(emptyBitrates, true)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// when no layer changes, should not change target
expectedTargetLayers = VideoLayers{
Spatial: 0,
Temporal: 3,
}
expectedResult = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry,
bandwidthRequested: 0,
bandwidthDelta: 0,
bitrates: emptyBitrates,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(emptyBitrates, true)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
// allocate using bitrates, allocation should choose optimal
f.currentLayers = InvalidLayers
expectedTargetLayers = VideoLayers{
Spatial: 0,
Temporal: 1,
}
expectedResult = VideoAllocation{
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: emptyBitrates,
targetLayers: f.parkedLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(emptyBitrates, true)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, f.parkedLayers, f.TargetLayers())
f.parkedLayers = InvalidLayers
// awaiting measurement, i.e. bitrates are not available, but layers available
disable(f)
f.UpTrackLayersChange([]int32{0}, []int32{})
expectedTargetLayers = VideoLayers{
Spatial: 0,
Temporal: DefaultMaxLayerTemporal,
}
expectedResult = VideoAllocation{
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: []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())
require.Equal(t, InvalidLayers, f.CurrentLayers())
// layers are available, but all layers <= max are exempted.
// forwarder not started yet, so should set target to max.
// pauseReason should be populated to indicate feed is dry
// because exempt layers are actually detected as stopped.
f.SetMaxSpatialLayer(0)
f.currentLayers = VideoLayers{Spatial: 0, Temporal: 0}
f.UpTrackLayersChange([]int32{0, 1, 2}, []int32{0})
expectedTargetLayers = VideoLayers{
Spatial: DefaultMaxLayerSpatial,
Temporal: DefaultMaxLayerTemporal,
}
expectedResult = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry,
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())
// when started, should just leave it at current for opportunistic resume
f.started = true
expectedTargetLayers = VideoLayers{
Spatial: 0,
Temporal: 0,
}
expectedResult = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry,
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 still leave at current for opportunistic resume
f.currentLayers = VideoLayers{Spatial: 1, Temporal: 0}
expectedTargetLayers = VideoLayers{
Spatial: 1,
Temporal: 0,
}
expectedResult = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry,
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())
// allocate using bitrates, allocation should choose optimal
f.currentLayers = InvalidLayers
f.UpTrackLayersChange([]int32{0, 1, 2}, []int32{})
f.maxLayers = VideoLayers{Spatial: 1, Temporal: 3}
expectedTargetLayers = VideoLayers{
Spatial: 1,
Temporal: 3,
}
expectedResult = VideoAllocation{
bandwidthRequested: bitrates[1][3],
bandwidthDelta: bitrates[1][3],
availableLayers: []int32{0, 1, 2},
bandwidthRequested: bitrates[0][1],
bandwidthDelta: bitrates[0][1],
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(bitrates, true)
@@ -346,7 +247,6 @@ func TestForwarderAllocateOptimal(t *testing.T) {
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// allocate using bitrates above maximum layer, allowing overshoot
f.UpTrackLayersChange([]int32{2}, []int32{})
sparseBitrates := Bitrates{
{0, 0, 0, 0},
{0, 0, 0, 0},
@@ -358,10 +258,10 @@ func TestForwarderAllocateOptimal(t *testing.T) {
}
expectedResult = VideoAllocation{
bandwidthRequested: sparseBitrates[2][1],
bandwidthDelta: sparseBitrates[2][1] - bitrates[1][3],
availableLayers: []int32{2},
bandwidthDelta: sparseBitrates[2][1] - bitrates[0][1],
bitrates: sparseBitrates,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: -1,
}
result = f.AllocateOptimal(sparseBitrates, true)
@@ -370,21 +270,73 @@ func TestForwarderAllocateOptimal(t *testing.T) {
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// when not allowing overshoot, should leave it at current (current is InvalidLayers in this test)
// when not allowing overshoot, should leave it at current when started and current is valid
sparseBitrates[1][2] = 10
sparseBitrates[2][1] = 0
f.started = true
f.currentLayers = VideoLayers{Spatial: 0, Temporal: 1}
expectedTargetLayers = VideoLayers{
Spatial: 0,
Temporal: 1,
}
expectedResult = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry,
bandwidthRequested: 0,
bandwidthDelta: -sparseBitrates[2][1],
availableLayers: []int32{2},
bandwidthDelta: -7,
bitrates: sparseBitrates,
targetLayers: InvalidLayers,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: 0,
}
result = f.AllocateOptimal(sparseBitrates, false)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// when not allowing overshoot, should leave it at max when started and current is not valid
sparseBitrates[1][2] = 0
sparseBitrates[2][1] = 7
f.started = true
f.currentLayers = InvalidLayers
expectedTargetLayers = VideoLayers{
Spatial: 2,
Temporal: 3,
}
expectedResult = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry,
bandwidthRequested: 0,
bandwidthDelta: 0,
bitrates: sparseBitrates,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: -1,
}
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())
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// when not allowing overshoot, should leave it at max for opportunistic forwarding when not started
f.started = false
f.targetLayers = VideoLayers{Spatial: 2, Temporal: 3}
sparseBitrates[1][2] = 10
sparseBitrates[2][1] = 0
expectedResult = VideoAllocation{
pauseReason: VideoPauseReasonFeedDry,
bandwidthRequested: 0,
bandwidthDelta: 0,
bitrates: sparseBitrates,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: -1,
}
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, DefaultMaxLayers, f.TargetLayers())
}
func TestForwarderProvisionalAllocate(t *testing.T) {
@@ -392,14 +344,12 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
availableLayers := []int32{0, 1, 2}
bitrates := Bitrates{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
}
f.availableLayers = availableLayers
f.ProvisionalAllocatePrepare(bitrates)
usedBitrate := f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true, false)
@@ -427,9 +377,9 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
isDeficient: true,
bandwidthRequested: bitrates[1][2],
bandwidthDelta: bitrates[1][2],
availableLayers: availableLayers,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 5,
}
result := f.ProvisionalAllocateCommit()
@@ -452,9 +402,9 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
isDeficient: true,
bandwidthRequested: bitrates[0][0],
bandwidthDelta: bitrates[0][0] - bitrates[1][2],
availableLayers: availableLayers,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 11,
}
result = f.ProvisionalAllocateCommit()
@@ -465,19 +415,14 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
//
// 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)
@@ -496,13 +441,16 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
Spatial: 1,
Temporal: 3,
}
expectedMaxLayers := VideoLayers{
Spatial: 0,
Temporal: 3,
}
expectedResult = VideoAllocation{
bandwidthRequested: bitrates[1][3],
bandwidthDelta: bitrates[1][3] - 1, // 1 is the last allocation bandwith requested
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: -4,
}
result = f.ProvisionalAllocateCommit()
@@ -511,25 +459,18 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
require.Equal(t, expectedTargetLayers, f.TargetLayers())
//
// Test exemptedLayers
// Even if overshoot is allowed, but if higher layers do not have bit rates and the current layer is exempted,
// should continue with current layer.
// Even if overshoot is allowed, but if higher layers do not have bit rates, 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
// all the provisional allocations should not succeed because the feed is dry
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, false, true)
require.Equal(t, int64(0), usedBitrate)
@@ -541,7 +482,7 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
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)
// committing should set target to (0, 2), i. e. leave it at current for opportunistic forwarding
expectedTargetLayers = VideoLayers{
Spatial: 0,
Temporal: 2,
@@ -550,10 +491,9 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
pauseReason: VideoPauseReasonFeedDry,
bandwidthRequested: bitrates[0][2],
bandwidthDelta: bitrates[0][2] - 8, // 8 is the last allocation bandwith requested
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: 0,
}
result = f.ProvisionalAllocateCommit()
@@ -562,23 +502,12 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
require.Equal(t, expectedTargetLayers, f.TargetLayers())
//
// Test currentLayers not in exemptedLayers
// Same case as above, but current is above max, so target should go to invalid
//
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 not succeed
// all the provisional allocations below should not succeed because the feed is dry
usedBitrate = f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, false, true)
require.Equal(t, int64(0), usedBitrate)
@@ -594,20 +523,22 @@ func TestForwarderProvisionalAllocate(t *testing.T) {
pauseReason: VideoPauseReasonFeedDry,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: InvalidLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: 0,
}
result = f.ProvisionalAllocateCommit()
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.TargetLayers())
require.Equal(t, InvalidLayers, f.CurrentLayers())
}
func TestForwarderProvisionalAllocateMute(t *testing.T) {
f := newForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo)
f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
bitrates := Bitrates{
{1, 2, 3, 4},
@@ -629,9 +560,9 @@ func TestForwarderProvisionalAllocateMute(t *testing.T) {
pauseReason: VideoPauseReasonMuted,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: bitrates,
targetLayers: InvalidLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 0,
}
result := f.ProvisionalAllocateCommit()
@@ -645,14 +576,12 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
availableLayers := []int32{0, 1, 2}
bitrates := Bitrates{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 0, 0},
}
f.availableLayers = availableLayers
f.ProvisionalAllocatePrepare(bitrates)
// from scratch (InvalidLayers) should give back layer (0, 0)
@@ -670,9 +599,9 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
isDeficient: true,
bandwidthRequested: 1,
bandwidthDelta: 1,
availableLayers: availableLayers,
bitrates: bitrates,
targetLayers: expectedLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 9,
}
result := f.ProvisionalAllocateCommit()
@@ -697,9 +626,9 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
expectedResult = VideoAllocation{
bandwidthRequested: 10,
bandwidthDelta: 0,
availableLayers: availableLayers,
bitrates: bitrates,
targetLayers: expectedLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 0,
}
result = f.ProvisionalAllocateCommit()
@@ -741,8 +670,6 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
f.Mute(false)
f.SetMaxSpatialLayer(0)
availableLayers = []int32{0, 1, 2}
exemptedLayers := []int32{0}
bitrates = Bitrates{
{0, 0, 0, 0},
{5, 6, 7, 8},
@@ -750,8 +677,6 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
}
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
@@ -765,13 +690,13 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
// committing should set target to (1, 0)
expectedLayers = VideoLayers{Spatial: 1, Temporal: 0}
expectedMaxLayers := VideoLayers{Spatial: 0, Temporal: DefaultMaxLayerTemporal}
expectedResult = VideoAllocation{
bandwidthRequested: 5,
bandwidthDelta: 5,
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: expectedLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: -1,
}
result = f.ProvisionalAllocateCommit()
@@ -779,11 +704,9 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
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}
//
// Test continuting at current layers when feed is dry
//
bitrates = Bitrates{
{0, 0, 0, 0},
{0, 0, 0, 0},
@@ -792,11 +715,9 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
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
// from scratch (InvalidLayers) should go to current 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,
@@ -811,10 +732,9 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
expectedResult = VideoAllocation{
bandwidthRequested: 0,
bandwidthDelta: -5,
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: expectedLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: 0,
}
result = f.ProvisionalAllocateCommit()
@@ -822,31 +742,13 @@ func TestForwarderProvisionalAllocateGetCooperativeTransition(t *testing.T) {
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedLayers, f.TargetLayers())
// Test exemptedLayers not matching current layers,
// leave target at current if no transition available to facilitate opportunistic forwarding
f.currentLayers = VideoLayers{Spatial: 2, Temporal: 2}
f.targetLayers = f.currentLayers
f.availableLayers = availableLayers
f.exemptedLayers = exemptedLayers
f.ProvisionalAllocatePrepare(bitrates)
expectedLayers = VideoLayers{Spatial: 2, Temporal: 2}
expectedTransition = VideoTransition{
from: VideoLayers{Spatial: 2, Temporal: 2},
to: expectedLayers,
bandwidthDelta: 0,
}
transition = f.ProvisionalAllocateGetCooperativeTransition(true)
require.Equal(t, expectedTransition, transition)
// committing should set target to current layers to enable opportunistic forwarding
expectedResult = VideoAllocation{
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: availableLayers,
exemptedLayers: exemptedLayers,
bitrates: bitrates,
targetLayers: expectedLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: 0,
}
result = f.ProvisionalAllocateCommit()
@@ -905,6 +807,7 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
require.False(t, boosted)
// if layers have not caught up, should not allocate next layer even if deficient
f.lastAllocation.pauseReason = VideoPauseReasonNone
f.lastAllocation.isDeficient = true
f.targetLayers = VideoLayers{
Spatial: 0,
@@ -914,9 +817,9 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
isDeficient: true,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: emptyBitrates,
targetLayers: InvalidLayers,
maxLayers: InvalidLayers,
distanceToDesired: 0,
}
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
@@ -939,9 +842,9 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
isDeficient: true,
bandwidthRequested: 2,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: emptyBitrates,
targetLayers: InvalidLayers,
maxLayers: InvalidLayers,
distanceToDesired: 0,
}
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, emptyBitrates, false)
@@ -958,9 +861,9 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
isDeficient: true,
bandwidthRequested: 3,
bandwidthDelta: 1,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 3,
}
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
@@ -979,9 +882,9 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
isDeficient: true,
bandwidthRequested: 4,
bandwidthDelta: 1,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 2,
}
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
@@ -1001,9 +904,9 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
isDeficient: true,
bandwidthRequested: 5,
bandwidthDelta: 1,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 1,
}
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
@@ -1021,9 +924,9 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
expectedResult = VideoAllocation{
bandwidthRequested: 7,
bandwidthDelta: 2,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 0,
}
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
@@ -1054,9 +957,9 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
isDeficient: true,
bandwidthRequested: 2,
bandwidthDelta: 2,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 4,
}
result, boosted = f.AllocateNextHigher(ChannelCapacityInfinity, bitrates, false)
@@ -1070,9 +973,9 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
isDeficient: true,
bandwidthRequested: 2,
bandwidthDelta: 2,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 4,
}
result, boosted = f.AllocateNextHigher(0, bitrates, false)
@@ -1096,13 +999,16 @@ func TestForwarderAllocateNextHigher(t *testing.T) {
Spatial: 1,
Temporal: 0,
}
expectedMaxLayers := VideoLayers{
Spatial: 0,
Temporal: DefaultMaxLayerTemporal,
}
expectedResult = VideoAllocation{
bandwidthRequested: bitrates[1][0],
bandwidthDelta: bitrates[1][0],
availableLayers: nil,
exemptedLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
maxLayers: expectedMaxLayers,
distanceToDesired: -1,
}
// overshoot should return (1, 0) even if there is not enough capacity
@@ -1118,14 +1024,12 @@ func TestForwarderPause(t *testing.T) {
f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
availableLayers := []int32{0, 1, 2}
bitrates := Bitrates{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
}
f.availableLayers = availableLayers
f.ProvisionalAllocatePrepare(bitrates)
f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true, false)
// should have set target at (0, 0)
@@ -1136,9 +1040,9 @@ func TestForwarderPause(t *testing.T) {
isDeficient: true,
bandwidthRequested: 0,
bandwidthDelta: 0 - bitrates[0][0],
availableLayers: availableLayers,
bitrates: bitrates,
targetLayers: InvalidLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 12,
}
result := f.Pause(bitrates)
@@ -1152,14 +1056,12 @@ func TestForwarderPauseMute(t *testing.T) {
f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
availableLayers := []int32{0, 1, 2}
bitrates := Bitrates{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
}
f.availableLayers = availableLayers
f.ProvisionalAllocatePrepare(bitrates)
f.ProvisionalAllocate(bitrates[2][3], VideoLayers{Spatial: 0, Temporal: 0}, true, true)
// should have set target at (0, 0)
@@ -1170,9 +1072,9 @@ func TestForwarderPauseMute(t *testing.T) {
pauseReason: VideoPauseReasonMuted,
bandwidthRequested: 0,
bandwidthDelta: 0 - bitrates[0][0],
availableLayers: availableLayers,
bitrates: bitrates,
targetLayers: InvalidLayers,
maxLayers: DefaultMaxLayers,
distanceToDesired: 0,
}
result := f.Pause(bitrates)
+21 -10
View File
@@ -30,8 +30,27 @@ var (
)
type AudioLevelHandle func(level uint8, duration uint32)
// ---------------------------------------------------
type Bitrates [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64
func (b *Bitrates) GetLayers() []int32 {
layers := []int32{}
for i := 0; i < len(b); i++ {
for j := 0; j < len(b[0]); j++ {
if b[i][j] != 0 {
layers = append(layers, int32(i))
break
}
}
}
return layers
}
// ---------------------------------------------------
// TrackReceiver defines an interface receive media from remote peer
type TrackReceiver interface {
TrackID() livekit.TrackID
@@ -197,7 +216,6 @@ func NewWebRTCReceiver(
w.streamTrackerManager = NewStreamTrackerManager(logger, trackInfo, w.isSVC, w.codec.ClockRate, trackersConfig)
w.streamTrackerManager.OnAvailableLayersChanged(w.downTrackLayerChange)
w.streamTrackerManager.OnBitrateAvailabilityChanged(w.downTrackBitrateAvailabilityChange)
w.streamTrackerManager.Start()
for _, opt := range opts {
w = opt(w)
@@ -381,11 +399,6 @@ func (w *WebRTCReceiver) AddDownTrack(track TrackSender) error {
w.logger.Infow("subscriberID already exists, replacing downtrack", "subscriberID", track.SubscriberID())
}
if w.Kind() == webrtc.RTPCodecTypeVideo {
// notify added down track of available layers
availableLayers, exemptedLayers := w.streamTrackerManager.GetAvailableLayers()
track.UpTrackLayersChange(availableLayers, exemptedLayers)
}
track.TrackInfoAvailable()
w.downTrackSpreader.Store(track)
@@ -396,9 +409,9 @@ func (w *WebRTCReceiver) SetMaxExpectedSpatialLayer(layer int32) {
w.streamTrackerManager.SetMaxExpectedSpatialLayer(layer)
}
func (w *WebRTCReceiver) downTrackLayerChange(availableLayers []int32, exemptedLayers []int32) {
func (w *WebRTCReceiver) downTrackLayerChange() {
for _, dt := range w.downTrackSpreader.GetDownTracks() {
dt.UpTrackLayersChange(availableLayers, exemptedLayers)
dt.UpTrackLayersChange()
}
}
@@ -563,8 +576,6 @@ func (w *WebRTCReceiver) forwardRTP(layer int32) {
if pr := w.redReceiver.Load(); pr != nil {
pr.(*RedReceiver).Close()
}
w.streamTrackerManager.Stop()
})
w.streamTrackerManager.RemoveTracker(layer)
+9
View File
@@ -217,6 +217,15 @@ func (s *StreamTracker) BitrateTemporalCumulative() []int64 {
}
}
// clear higher layers
for i := 0; i < len(brs); i++ {
if brs[i] == 0 {
for j := i + 1; j < len(brs); j++ {
brs[j] = 0
}
}
}
return brs
}
+26 -114
View File
@@ -7,7 +7,6 @@ import (
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/livekit-server/pkg/sfu/streamtracker"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
)
@@ -21,18 +20,15 @@ type StreamTrackerManager struct {
trackerConfig config.StreamTrackerConfig
layersNotifyOpQueue *utils.OpsQueue
lock sync.RWMutex
trackers [DefaultMaxLayerSpatial + 1]*streamtracker.StreamTracker
availableLayers []int32
exemptedLayers []int32
maxExpectedLayer int32
paused bool
onAvailableLayersChanged func(availableLayers []int32, exemptedLayers []int32)
onAvailableLayersChanged func()
onBitrateAvailabilityChanged func()
onMaxLayerChanged func(maxLayer int32)
}
@@ -45,12 +41,11 @@ func NewStreamTrackerManager(
trackersConfig config.StreamTrackersConfig,
) *StreamTrackerManager {
s := &StreamTrackerManager{
logger: logger,
trackInfo: trackInfo,
isSVC: isSVC,
maxPublishedLayer: 0,
clockRate: clockRate,
layersNotifyOpQueue: utils.NewOpsQueue(logger, "layer-notify", 10),
logger: logger,
trackInfo: trackInfo,
isSVC: isSVC,
maxPublishedLayer: 0,
clockRate: clockRate,
}
switch s.trackInfo.Source {
@@ -73,15 +68,7 @@ func NewStreamTrackerManager(
return s
}
func (s *StreamTrackerManager) Start() {
s.layersNotifyOpQueue.Start()
}
func (s *StreamTrackerManager) Stop() {
s.layersNotifyOpQueue.Stop()
}
func (s *StreamTrackerManager) OnAvailableLayersChanged(f func(availableLayers []int32, exemptedLayers []int32)) {
func (s *StreamTrackerManager) OnAvailableLayersChanged(f func()) {
s.onAvailableLayersChanged = f
}
@@ -145,14 +132,12 @@ func (s *StreamTrackerManager) AddTracker(layer int32) *streamtracker.StreamTrac
s.logger.Debugw("StreamTrackerManager add track", "layer", layer)
tracker.OnStatusChanged(func(status streamtracker.StreamStatus) {
s.layersNotifyOpQueue.Enqueue(func() {
s.logger.Debugw("StreamTrackerManager OnStatusChanged", "layer", layer, "status", status)
if status == streamtracker.StreamStatusStopped {
s.removeAvailableLayer(layer)
} else {
s.addAvailableLayer(layer)
}
})
s.logger.Debugw("StreamTrackerManager OnStatusChanged", "layer", layer, "status", status)
if status == streamtracker.StreamStatusStopped {
s.removeAvailableLayer(layer)
} else {
s.addAvailableLayer(layer)
}
})
tracker.OnBitrateAvailable(func() {
if s.onBitrateAvailabilityChanged != nil {
@@ -188,7 +173,6 @@ 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()
@@ -353,26 +337,6 @@ func (s *StreamTrackerManager) GetLayeredBitrate() Bitrates {
return br
}
func (s *StreamTrackerManager) GetAvailableLayers() ([]int32, []int32) {
s.lock.RLock()
defer s.lock.RUnlock()
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 {
s.lock.RLock()
defer s.lock.RUnlock()
return s.hasSpatialLayerLocked(layer)
}
func (s *StreamTrackerManager) hasSpatialLayerLocked(layer int32) bool {
for _, l := range s.availableLayers {
if l == layer {
@@ -400,35 +364,21 @@ 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] })
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()
// check if new layer is the max layer
isMaxLayerChange := s.availableLayers[len(s.availableLayers)-1] == layer
s.logger.Infow(
"available layers changed - layer seen",
"added", layer,
"availableLayers", availableLayers,
"exemptedLayers", exemptedLayers,
"availableLayers", s.availableLayers,
)
s.lock.Unlock()
if s.onAvailableLayersChanged != nil {
s.onAvailableLayersChanged(availableLayers, exemptedLayers)
s.onAvailableLayersChanged()
}
// check if new layer was the max layer
if availableLayers[len(availableLayers)-1] == layer && s.onMaxLayerChanged != nil {
if isMaxLayerChange && s.onMaxLayerChanged != nil {
s.onMaxLayerChanged(layer)
}
}
@@ -440,52 +390,21 @@ func (s *StreamTrackerManager) removeAvailableLayer(layer int32) {
prevMaxLayer = s.availableLayers[len(s.availableLayers)-1]
}
//
// remove from available if not exempt
//
exempt := false
for _, l := range s.trackerConfig.ExemptedLayers {
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 {
// do not remove layers for non-simulcast
if exempt || l != layer || len(s.trackInfo.Layers) < 2 {
if l != layer || len(s.trackInfo.Layers) < 2 {
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
//
if exempt {
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...)
s.logger.Infow(
"available layers changed - layer gone",
"removed", layer,
"availableLayers", newLayers,
)
curMaxLayer := InvalidLayerSpatial
if len(s.availableLayers) > 0 {
@@ -493,16 +412,9 @@ func (s *StreamTrackerManager) removeAvailableLayer(layer int32) {
}
s.lock.Unlock()
s.logger.Infow(
"available layers changed - layer gone",
"removed", layer,
"availableLayers", newLayers,
"exeptedLayers", exemptedLayers,
)
// need to immediately switch off unavailable layers
if s.onAvailableLayersChanged != nil {
s.onAvailableLayersChanged(newLayers, exemptedLayers)
s.onAvailableLayersChanged()
}
// if maxLayer was removed, send the new maxLayer