StreamAllocator tweaks (#240)

* WIP commit

* test padding only with a gap scenario

* Debug

* Fix video corruption, need buffer to include payload and translated header size

* Revert incorrect change

* Fix VP8 translation to return buffer with proper length

* Restore 7-bit mode

* WIP commit

* Clean up

* More clean up and tests compiling again

* Fix tests
This commit is contained in:
Raja Subramanian
2021-12-08 21:56:07 +05:30
committed by GitHub
parent 461f29c097
commit 7948fabce0
4 changed files with 712 additions and 419 deletions
+371 -189
View File
@@ -31,11 +31,11 @@ func TestForwarderLayersAudio(t *testing.T) {
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, InvalidLayers, f.TargetLayers())
changed, layers := f.SetMaxSpatialLayer(1)
changed, layers, _ := f.SetMaxSpatialLayer(1)
require.False(t, changed)
require.Equal(t, InvalidLayers, layers)
changed, layers = f.SetMaxTemporalLayer(1)
changed, layers, _ = f.SetMaxTemporalLayer(1)
require.False(t, changed)
require.Equal(t, InvalidLayers, layers)
@@ -47,36 +47,36 @@ func TestForwarderLayersVideo(t *testing.T) {
maxLayers := f.MaxLayers()
expectedLayers := VideoLayers{
spatial: DefaultMaxSpatialLayer,
temporal: DefaultMaxTemporalLayer,
spatial: DefaultMaxLayerSpatial,
temporal: DefaultMaxLayerTemporal,
}
require.Equal(t, expectedLayers, maxLayers)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, InvalidLayers, f.TargetLayers())
changed, layers := f.SetMaxSpatialLayer(DefaultMaxSpatialLayer)
changed, layers, _ := f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
require.False(t, changed)
require.Equal(t, InvalidLayers, layers)
changed, layers = f.SetMaxSpatialLayer(DefaultMaxSpatialLayer - 1)
changed, layers, _ = f.SetMaxSpatialLayer(DefaultMaxLayerSpatial - 1)
require.True(t, changed)
expectedLayers = VideoLayers{
spatial: DefaultMaxSpatialLayer - 1,
temporal: DefaultMaxTemporalLayer,
spatial: DefaultMaxLayerSpatial - 1,
temporal: DefaultMaxLayerTemporal,
}
require.Equal(t, expectedLayers, layers)
require.Equal(t, expectedLayers, f.MaxLayers())
changed, layers = f.SetMaxTemporalLayer(DefaultMaxTemporalLayer)
changed, layers, _ = f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
require.False(t, changed)
require.Equal(t, InvalidLayers, layers)
changed, layers = f.SetMaxTemporalLayer(DefaultMaxTemporalLayer - 1)
changed, layers, _ = f.SetMaxTemporalLayer(DefaultMaxLayerTemporal - 1)
require.True(t, changed)
expectedLayers = VideoLayers{
spatial: DefaultMaxSpatialLayer - 1,
temporal: DefaultMaxTemporalLayer - 1,
spatial: DefaultMaxLayerSpatial - 1,
temporal: DefaultMaxLayerTemporal - 1,
}
require.Equal(t, expectedLayers, layers)
require.Equal(t, expectedLayers, f.MaxLayers())
@@ -87,10 +87,10 @@ func TestForwarderGetForwardingStatus(t *testing.T) {
require.Equal(t, ForwardingStatusOff, f.GetForwardingStatus())
f.targetLayers.spatial = DefaultMaxSpatialLayer - 1
f.targetLayers.spatial = DefaultMaxLayerSpatial - 1
require.Equal(t, ForwardingStatusPartial, f.GetForwardingStatus())
f.targetLayers.spatial = DefaultMaxSpatialLayer
f.targetLayers.spatial = DefaultMaxLayerSpatial
require.Equal(t, ForwardingStatusOptimal, f.GetForwardingStatus())
}
@@ -115,8 +115,8 @@ func TestForwarderUptrackLayersChange(t *testing.T) {
func TestForwarderAllocate(t *testing.T) {
f := NewForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo)
emptyBitrates := [DefaultMaxSpatialLayer + 1][DefaultMaxTemporalLayer + 1]int64{}
bitrates := [DefaultMaxSpatialLayer + 1][DefaultMaxTemporalLayer + 1]int64{
emptyBitrates := [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{}
bitrates := [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{
{2, 3, 0, 0},
{4, 0, 0, 5},
{0, 7, 0, 0},
@@ -125,117 +125,128 @@ func TestForwarderAllocate(t *testing.T) {
// muted should not consume any bandwidth
f.Mute(true)
f.disable()
expectedResult := VideoAllocationResult{
expectedResult := VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateMuted,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: bitrates,
targetLayers: InvalidLayers,
}
result := f.Allocate(ChannelCapacityInfinity, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateMuted, f.lastAllocationState)
require.Equal(t, int64(0), f.lastAllocationRequestBps)
require.Equal(t, expectedResult, f.lastAllocation)
// feed dry state
f.Mute(false)
f.lastAllocationState = VideoAllocationStateNone
f.lastAllocation.state = VideoAllocationStateNone
f.disable()
expectedResult = VideoAllocationResult{
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateFeedDry,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: emptyBitrates,
targetLayers: InvalidLayers,
}
result = f.Allocate(ChannelCapacityInfinity, emptyBitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateFeedDry, f.lastAllocationState)
require.Equal(t, int64(0), f.lastAllocationRequestBps)
require.Equal(t, expectedResult, f.lastAllocation)
// awaiting measurement, i. e. bitrates are not available, but layers available
f.lastAllocationState = VideoAllocationStateNone
f.lastAllocation.state = VideoAllocationStateNone
f.disable()
f.UptrackLayersChange([]uint16{0})
expectedResult = VideoAllocationResult{
expectedTargetLayers := VideoLayers{
spatial: 0,
temporal: DefaultMaxLayerTemporal,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeResuming,
state: VideoAllocationStateAwaitingMeasurement,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: []uint16{0},
bitrates: emptyBitrates,
targetLayers: expectedTargetLayers,
}
result = f.Allocate(ChannelCapacityInfinity, emptyBitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateAwaitingMeasurement, f.lastAllocationState)
require.Equal(t, int64(0), f.lastAllocationRequestBps)
expectedTargetLayers := VideoLayers{
spatial: 0,
temporal: DefaultMaxTemporalLayer,
}
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.Equal(t, InvalidLayers, f.CurrentLayers())
// while awaiting measurement, less than infinite channel capacity should pause the stream
expectedResult = VideoAllocationResult{
expectedResult = VideoAllocation{
change: VideoStreamingChangePausing,
state: VideoAllocationStateDeficient,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: []uint16{0},
bitrates: emptyBitrates,
targetLayers: InvalidLayers,
}
result = f.Allocate(ChannelCapacityInfinity-1, emptyBitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateDeficient, f.lastAllocationState)
require.Equal(t, int64(0), f.lastAllocationRequestBps)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, InvalidLayers, f.TargetLayers())
// allocate using bitrates and less than infinite channel capacity, but enough for optimal
expectedResult = VideoAllocationResult{
change: VideoStreamingChangeResuming,
state: VideoAllocationStateOptimal,
bandwidthRequested: bitrates[2][1],
bandwidthDelta: bitrates[2][1],
layersChanged: true,
}
result = f.Allocate(ChannelCapacityInfinity-1, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateOptimal, f.lastAllocationState)
require.Equal(t, bitrates[2][1], f.lastAllocationRequestBps)
require.Equal(t, InvalidLayers, f.CurrentLayers())
expectedTargetLayers = VideoLayers{
spatial: 2,
temporal: 1,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeResuming,
state: VideoAllocationStateOptimal,
bandwidthRequested: bitrates[2][1],
bandwidthDelta: bitrates[2][1],
availableLayers: []uint16{0},
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result = f.Allocate(ChannelCapacityInfinity-1, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// give it a bitrate that is less than optimal
expectedResult = VideoAllocationResult{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: bitrates[1][3],
bandwidthDelta: bitrates[1][3] - bitrates[2][1],
layersChanged: true,
}
result = f.Allocate(bitrates[2][1]-1, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateDeficient, f.lastAllocationState)
require.Equal(t, bitrates[1][3], f.lastAllocationRequestBps)
require.Equal(t, InvalidLayers, f.CurrentLayers())
expectedTargetLayers = VideoLayers{
spatial: 1,
temporal: 3,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: bitrates[1][3],
bandwidthDelta: bitrates[1][3] - bitrates[2][1],
availableLayers: []uint16{0},
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result = f.Allocate(bitrates[2][1]-1, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// give it a bitrate that cannot fit any layer
expectedResult = VideoAllocationResult{
expectedResult = VideoAllocation{
change: VideoStreamingChangePausing,
state: VideoAllocationStateDeficient,
bandwidthRequested: 0,
bandwidthDelta: 0 - bitrates[1][3],
layersChanged: true,
availableLayers: []uint16{0},
bitrates: bitrates,
targetLayers: InvalidLayers,
}
result = f.Allocate(bitrates[0][0]-1, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateDeficient, f.lastAllocationState)
require.Equal(t, int64(0), f.lastAllocationRequestBps)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, InvalidLayers, f.TargetLayers())
}
@@ -244,274 +255,364 @@ func TestForwarderTryAllocate(t *testing.T) {
f := NewForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo)
// adjust target layers per given additional channel capacity (which can be negative),
bitrates := [DefaultMaxSpatialLayer + 1][DefaultMaxTemporalLayer + 1]int64{
bitrates := [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{
{2, 3, 0, 0},
{4, 0, 0, 5},
{0, 7, 0, 0},
}
f.lastAllocationState = VideoAllocationStateDeficient
f.lastAllocationRequestBps = bitrates[1][3]
f.lastAllocation.state = VideoAllocationStateDeficient
f.lastAllocation.bandwidthRequested = bitrates[1][3]
f.targetLayers = VideoLayers{
spatial: 1,
temporal: 3,
}
expectedResult := VideoAllocationResult{
expectedTargetLayers := VideoLayers{
spatial: 1,
temporal: 0,
}
expectedResult := VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: bitrates[0][1],
bandwidthDelta: bitrates[0][1] - bitrates[1][3],
layersChanged: true,
bandwidthRequested: bitrates[1][0],
bandwidthDelta: bitrates[1][0] - bitrates[1][3],
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result := f.TryAllocate(-1, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateDeficient, f.lastAllocationState)
require.Equal(t, bitrates[0][1], f.lastAllocationRequestBps)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
expectedTargetLayers := VideoLayers{
spatial: 0,
temporal: 1,
}
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// but should not pause even if no layer fits, i. e. preserve current
expectedResult = VideoAllocationResult{
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: bitrates[0][1],
bandwidthDelta: 0,
bandwidthRequested: bitrates[1][0],
bandwidthDelta: -1,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result = f.TryAllocate(-2, bitrates)
result = f.TryAllocate(-3, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateDeficient, f.lastAllocationState)
require.Equal(t, bitrates[0][1], f.lastAllocationRequestBps)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// can catch up to optimal given enough additional channel capacity
expectedResult = VideoAllocationResult{
change: VideoStreamingChangeNone,
state: VideoAllocationStateOptimal,
bandwidthRequested: bitrates[2][1],
bandwidthDelta: bitrates[2][1] - bitrates[0][1],
layersChanged: true,
}
result = f.TryAllocate(10, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateOptimal, f.lastAllocationState)
require.Equal(t, bitrates[2][1], f.lastAllocationRequestBps)
require.Equal(t, InvalidLayers, f.CurrentLayers())
expectedTargetLayers = VideoLayers{
spatial: 2,
temporal: 1,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateOptimal,
bandwidthRequested: bitrates[2][1],
bandwidthDelta: bitrates[2][1] - bitrates[1][0],
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result = f.TryAllocate(10, bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers())
}
func TestForwarderFinalizeAllocate(t *testing.T) {
f := NewForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo)
bitrates := [DefaultMaxSpatialLayer + 1][DefaultMaxTemporalLayer + 1]int64{
bitrates := [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
}
// FinalizeAllocate should do nothing unless Forwarder allocation state is VideoAllocationStateAwaitingMeasurement
f.FinalizeAllocate(bitrates)
require.Equal(t, VideoAllocationStateNone, f.lastAllocationState)
result := f.FinalizeAllocate(bitrates)
require.Equal(t, VideoAllocationDefault, result)
require.Equal(t, VideoAllocationDefault, f.lastAllocation)
f.lastAllocationState = VideoAllocationStateMuted
f.lastAllocation.state = VideoAllocationStateMuted
f.disable()
f.FinalizeAllocate(bitrates)
require.Equal(t, VideoAllocationStateMuted, f.lastAllocationState)
expectedResult := VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateMuted,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: [3][4]int64{},
targetLayers: InvalidLayers,
}
result = f.FinalizeAllocate(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
f.lastAllocationState = VideoAllocationStateAwaitingMeasurement
f.lastAllocation.state = VideoAllocationStateAwaitingMeasurement
f.disable()
f.FinalizeAllocate(bitrates)
require.Equal(t, VideoAllocationStateOptimal, f.lastAllocationState)
require.Equal(t, InvalidLayers, f.CurrentLayers())
expectedTargetLayers := VideoLayers{
spatial: 2,
temporal: 3,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeResuming,
state: VideoAllocationStateOptimal,
bandwidthRequested: bitrates[2][3],
bandwidthDelta: bitrates[2][3],
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result = f.FinalizeAllocate(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers())
// no layers available => feed dry
f.lastAllocationState = VideoAllocationStateAwaitingMeasurement
f.lastAllocation.state = VideoAllocationStateAwaitingMeasurement
f.disable()
f.FinalizeAllocate([DefaultMaxSpatialLayer + 1][DefaultMaxTemporalLayer + 1]int64{})
require.Equal(t, VideoAllocationStateFeedDry, f.lastAllocationState)
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateFeedDry,
bandwidthRequested: 0,
bandwidthDelta: 0 - bitrates[2][3],
availableLayers: nil,
bitrates: [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{},
targetLayers: InvalidLayers,
}
result = f.FinalizeAllocate([DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{})
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
// layers available, but still awaiting measurement
f.lastAllocationState = VideoAllocationStateAwaitingMeasurement
f.lastAllocation.state = VideoAllocationStateAwaitingMeasurement
f.disable()
f.UptrackLayersChange([]uint16{0, 1})
f.FinalizeAllocate([DefaultMaxSpatialLayer + 1][DefaultMaxTemporalLayer + 1]int64{})
require.Equal(t, VideoAllocationStateAwaitingMeasurement, f.lastAllocationState)
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateAwaitingMeasurement,
bandwidthRequested: 0,
bandwidthDelta: -12,
availableLayers: nil,
bitrates: [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{},
targetLayers: InvalidLayers,
}
result = f.FinalizeAllocate([DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{})
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
// sparse layers
bitrates = [DefaultMaxSpatialLayer + 1][DefaultMaxTemporalLayer + 1]int64{
bitrates = [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{
{1, 2, 0, 0},
{5, 0, 0, 6},
{0, 0, 0, 0},
}
f.lastAllocationState = VideoAllocationStateAwaitingMeasurement
f.disable()
f.FinalizeAllocate(bitrates)
require.Equal(t, VideoAllocationStateOptimal, f.lastAllocationState)
require.Equal(t, InvalidLayers, f.CurrentLayers())
expectedTargetLayers = VideoLayers{
spatial: 1,
temporal: 3,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeResuming,
state: VideoAllocationStateOptimal,
bandwidthRequested: bitrates[1][3],
bandwidthDelta: bitrates[1][3],
availableLayers: []uint16{0, 1},
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result = f.FinalizeAllocate(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, InvalidLayers, f.CurrentLayers())
require.Equal(t, expectedTargetLayers, f.TargetLayers())
}
func TestForwarderAllocateNextHigher(t *testing.T) {
f := NewForwarder(testutils.TestOpusCodec, webrtc.RTPCodecTypeAudio)
emptyBitrates := [DefaultMaxSpatialLayer + 1][DefaultMaxTemporalLayer + 1]int64{}
bitrates := [DefaultMaxSpatialLayer + 1][DefaultMaxTemporalLayer + 1]int64{
emptyBitrates := [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{}
bitrates := [DefaultMaxLayerSpatial + 1][DefaultMaxLayerTemporal + 1]int64{
{2, 3, 0, 0},
{4, 0, 0, 5},
{0, 7, 0, 0},
}
result := f.AllocateNextHigher(bitrates)
require.Equal(t, VideoAllocationResult{}, result) // no layer for audio
result, boosted := f.AllocateNextHigher(bitrates)
require.Equal(t, VideoAllocationDefault, result) // no layer for audio
require.False(t, boosted)
f = NewForwarder(testutils.TestVP8Codec, webrtc.RTPCodecTypeVideo)
// when not in deficient state, does not boost
f.lastAllocationState = VideoAllocationStateNone
result = f.AllocateNextHigher(bitrates)
require.Equal(t, VideoAllocationResult{}, result)
f.lastAllocation.state = VideoAllocationStateNone
result, boosted = f.AllocateNextHigher(bitrates)
require.Equal(t, VideoAllocationDefault, result)
require.False(t, boosted)
// if layers have not caught up, should not allocate next layer
f.lastAllocationState = VideoAllocationStateDeficient
f.lastAllocation.state = VideoAllocationStateDeficient
f.targetLayers.spatial = 0
result = f.AllocateNextHigher(bitrates)
require.Equal(t, VideoAllocationResult{}, result)
expectedResult := VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: 0,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: emptyBitrates,
targetLayers: InvalidLayers,
}
result, boosted = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.False(t, boosted)
f.currentLayers.spatial = 0
f.targetLayers.temporal = 0
result = f.AllocateNextHigher(bitrates)
require.Equal(t, VideoAllocationResult{}, result)
result, boosted = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.False(t, boosted)
f.currentLayers.temporal = 0
f.lastAllocationRequestBps = bitrates[0][0]
f.lastAllocation.bandwidthRequested = bitrates[0][0]
// empty bitrates cannot increase layer
result = f.AllocateNextHigher(emptyBitrates)
require.Equal(t, VideoAllocationResult{}, result)
// move from (0, 0) -> (0, 1), i. e. a higher temporal layer is available in the same spatial layer
expectedResult := VideoAllocationResult{
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: 3,
bandwidthDelta: 1,
layersChanged: true,
bandwidthRequested: 2,
bandwidthDelta: 0,
availableLayers: nil,
bitrates: emptyBitrates,
targetLayers: InvalidLayers,
}
result = f.AllocateNextHigher(bitrates)
result, boosted = f.AllocateNextHigher(emptyBitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateDeficient, f.lastAllocationState)
require.Equal(t, bitrates[0][1], f.lastAllocationRequestBps)
require.Equal(t, expectedResult, f.lastAllocation)
require.False(t, boosted)
// move from (0, 0) -> (0, 1), i. e. a higher temporal layer is available in the same spatial layer
expectedTargetLayers := VideoLayers{
spatial: 0,
temporal: 1,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: 3,
bandwidthDelta: 1,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result, boosted = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.True(t, boosted)
// move from (0, 1) -> (1, 0), i. e. a higher spatial layer is available
f.currentLayers.temporal = 1
expectedResult = VideoAllocationResult{
expectedTargetLayers = VideoLayers{
spatial: 1,
temporal: 0,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: 4,
bandwidthDelta: 1,
layersChanged: true,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result = f.AllocateNextHigher(bitrates)
result, boosted = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateDeficient, f.lastAllocationState)
require.Equal(t, bitrates[1][0], f.lastAllocationRequestBps)
expectedTargetLayers = VideoLayers{
spatial: 1,
temporal: 0,
}
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.True(t, boosted)
// next higher, move from (1, 0) -> (1, 3), still deficient though
f.currentLayers.spatial = 1
f.currentLayers.temporal = 0
expectedResult = VideoAllocationResult{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: 5,
bandwidthDelta: 1,
layersChanged: true,
}
result = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateDeficient, f.lastAllocationState)
require.Equal(t, bitrates[1][3], f.lastAllocationRequestBps)
expectedTargetLayers = VideoLayers{
spatial: 1,
temporal: 3,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateDeficient,
bandwidthRequested: 5,
bandwidthDelta: 1,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result, boosted = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.True(t, boosted)
// next higher, move from (1, 3) -> (2, 1), optimal allocation
f.currentLayers.temporal = 3
expectedResult = VideoAllocationResult{
change: VideoStreamingChangeNone,
state: VideoAllocationStateOptimal,
bandwidthRequested: 7,
bandwidthDelta: 2,
layersChanged: true,
}
result = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateOptimal, f.lastAllocationState)
require.Equal(t, bitrates[2][1], f.lastAllocationRequestBps)
expectedTargetLayers = VideoLayers{
spatial: 2,
temporal: 1,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeNone,
state: VideoAllocationStateOptimal,
bandwidthRequested: 7,
bandwidthDelta: 2,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result, boosted = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.True(t, boosted)
// ask again, should return false as there is no where to go higher
// ask again, should return not boosted as there is no room to go higher
f.currentLayers.spatial = 2
f.currentLayers.temporal = 1
result = f.AllocateNextHigher(bitrates)
require.Equal(t, VideoAllocationResult{}, result)
require.Equal(t, VideoAllocationStateOptimal, f.lastAllocationState)
require.Equal(t, bitrates[2][1], f.lastAllocationRequestBps)
result, boosted = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.False(t, boosted)
// turn off everything, allocating next layer should result
f.disable()
f.lastAllocationState = VideoAllocationStateDeficient
f.lastAllocationRequestBps = 0
f.lastAllocation.state = VideoAllocationStateDeficient
f.lastAllocation.bandwidthRequested = 0
expectedResult = VideoAllocationResult{
change: VideoStreamingChangeResuming,
state: VideoAllocationStateDeficient,
bandwidthRequested: 2,
bandwidthDelta: 2,
layersChanged: true,
}
result = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, VideoAllocationStateDeficient, f.lastAllocationState)
require.Equal(t, bitrates[0][0], f.lastAllocationRequestBps)
expectedTargetLayers = VideoLayers{
spatial: 0,
temporal: 0,
}
expectedResult = VideoAllocation{
change: VideoStreamingChangeResuming,
state: VideoAllocationStateDeficient,
bandwidthRequested: 2,
bandwidthDelta: 2,
availableLayers: nil,
bitrates: bitrates,
targetLayers: expectedTargetLayers,
}
result, boosted = f.AllocateNextHigher(bitrates)
require.Equal(t, expectedResult, result)
require.Equal(t, expectedResult, f.lastAllocation)
require.Equal(t, expectedTargetLayers, f.TargetLayers())
require.True(t, boosted)
}
func TestForwarderGetTranslationParamsMuted(t *testing.T) {
@@ -622,6 +723,47 @@ func TestForwarderGetTranslationParamsAudio(t *testing.T) {
require.NoError(t, err)
require.True(t, reflect.DeepEqual(expectedTP, *actualTP))
// padding only packet after a gap should be forwarded
params = &testutils.TestExtPacketParams{
IsHead: true,
SequenceNumber: 23337,
Timestamp: 0xabcdef,
SSRC: 0x12345678,
}
extPkt, err = testutils.GetTestExtPacket(params)
expectedTP = TranslationParams{
rtp: &TranslationParamsRTP{
snOrdering: SequenceNumberOrderingGap,
sequenceNumber: 23336,
timestamp: 0xabcdef,
},
}
actualTP, err = f.GetTranslationParams(extPkt, 0)
require.NoError(t, err)
require.True(t, reflect.DeepEqual(expectedTP, *actualTP))
// out-of-order should be forwarded using cache
params = &testutils.TestExtPacketParams{
IsHead: false,
SequenceNumber: 23336,
Timestamp: 0xabcdef,
SSRC: 0x12345678,
PayloadSize: 20,
}
extPkt, err = testutils.GetTestExtPacket(params)
expectedTP = TranslationParams{
rtp: &TranslationParamsRTP{
snOrdering: SequenceNumberOrderingOutOfOrder,
sequenceNumber: 23335,
timestamp: 0xabcdef,
},
}
actualTP, err = f.GetTranslationParams(extPkt, 0)
require.NoError(t, err)
require.True(t, reflect.DeepEqual(expectedTP, *actualTP))
// switching source should lock onto the new source, but sequence number should be contiguous
params = &testutils.TestExtPacketParams{
IsHead: true,
@@ -635,7 +777,7 @@ func TestForwarderGetTranslationParamsAudio(t *testing.T) {
expectedTP = TranslationParams{
rtp: &TranslationParamsRTP{
snOrdering: SequenceNumberOrderingContiguous,
sequenceNumber: 23335,
sequenceNumber: 23337,
timestamp: 0xabcdf0,
},
}
@@ -898,6 +1040,46 @@ func TestForwarderGetTranslationParamsVideo(t *testing.T) {
require.NoError(t, err)
require.True(t, reflect.DeepEqual(expectedTP, *actualTP))
// padding only packet after a gap should be forwarded
params = &testutils.TestExtPacketParams{
IsHead: true,
SequenceNumber: 23339,
Timestamp: 0xabcdef,
SSRC: 0x12345678,
}
extPkt, err = testutils.GetTestExtPacket(params)
expectedTP = TranslationParams{
rtp: &TranslationParamsRTP{
snOrdering: SequenceNumberOrderingGap,
sequenceNumber: 23337,
timestamp: 0xabcdef,
},
}
actualTP, err = f.GetTranslationParams(extPkt, 0)
require.NoError(t, err)
require.True(t, reflect.DeepEqual(expectedTP, *actualTP))
// out-of-order should be forwarded using cache, even if it is padding only
params = &testutils.TestExtPacketParams{
IsHead: false,
SequenceNumber: 23338,
Timestamp: 0xabcdef,
SSRC: 0x12345678,
}
extPkt, err = testutils.GetTestExtPacket(params)
expectedTP = TranslationParams{
rtp: &TranslationParamsRTP{
snOrdering: SequenceNumberOrderingOutOfOrder,
sequenceNumber: 23336,
timestamp: 0xabcdef,
},
}
actualTP, err = f.GetTranslationParams(extPkt, 0)
require.NoError(t, err)
require.True(t, reflect.DeepEqual(expectedTP, *actualTP))
// switching SSRC (happens for new layer or new track source)
// should lock onto the new source, but sequence number should be contiguous
f.targetLayers = VideoLayers{
@@ -932,7 +1114,7 @@ func TestForwarderGetTranslationParamsVideo(t *testing.T) {
expectedTP = TranslationParams{
rtp: &TranslationParamsRTP{
snOrdering: SequenceNumberOrderingContiguous,
sequenceNumber: 23336,
sequenceNumber: 23338,
timestamp: 0xabcdf0,
},
vp8: &TranslationParamsVP8{