Files
livekit-3d/pkg/sfu/connectionquality/connectionstats_test.go
T
Raja Subramanian b5c023f986 Connection quality changes (#913)
* WIP commit

* Connection quality changes

- Fix Firefox showing poor quality
  o The issue was that we were using max available layer and
    calculating quality. The rationale being that even if
    server sends dynacast messages, client may not implement
    dynacast and still stream all layers. But, with Firefox
    (maybe a Firefox bug), it sends some small amount of
    data on layer 2 even when that layer is disabled.
    Guessing it is probing (or actually we might be using
    some small value for high layers as Firefox cannot turn off
    layers). That higher layer gets used in quality calculation.
    As the bit rate on that layer is extremely low, it yields low
    score.

    Fixed by considering the max expected layer. That is of most
    interest. Yes, clients may ignore dynacast and stream all layers,
    but, max expected is the one of interest. So, look for
    quality in the max expected layer and not max available layer.
- Lots of clean up around connection quality stuff
  o Use a dynamic scaling thing to ensure that we do not get bitten
    by absolute values. Calculate best possible scenario score and
    map that to maximum MOS score. This will ensure that different
    codecs, different settings do not mess up the scoring. For example,
    a client might use 1 Mbps for 720p, but a different client could
    use 2 Mbps for 720p. As an SFU/infrastructure middlebox, we do
    not have control over quality at those rates. We can only ensure
    that streaming happens smoothly at those rates. So, in that
    example, for client 1, 1 Mbps will map to MOS 5.0 and for client 2,
    2 Mbps will map to MOS 5.0. Any impairments after that will
    reflect in the score.
  o Penalise for missing target layer by one level for one layer missed.
  o Move tests to connection quality directory. The participant test
    was not super useful.

* Add missed file

* Remove debug code

* use more constants and initialise normalisation factor

* rtcscore pointer
2022-08-15 13:21:07 +05:30

479 lines
13 KiB
Go

package connectionquality
import (
"testing"
"time"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/protocol/livekit"
"github.com/stretchr/testify/require"
)
func TestConnectionQuality(t *testing.T) {
t.Run("audio quality changes over time", func(t *testing.T) {
cs := NewConnectionStats(ConnectionStatsParams{
MimeType: "audio/opus",
})
cs.Start(&livekit.TrackInfo{Type: livekit.TrackType_AUDIO})
// first update does nothing
cs.updateScore(nil)
require.Equal(t, float32(5), cs.GetScore())
// perfect conditions should return a score of 5
streams := map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 20000 * 2 / 8, // 20 kbps
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(5), cs.GetScore())
// introduce loss and the score should drop
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
PacketsLost: 10,
Bytes: 18000 * 2 / 8, // 20 kbps - 2 kbps, i. e 10% lost
},
},
},
}
cs.updateScore(streams)
lossQuality := cs.GetScore()
require.Less(t, lossQuality, float32(3.55))
// small packet rate, should avoid calculation as it does not meet DTX threshold
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 44,
PacketsLost: 4,
Bytes: 1000 * 2 / 8, // 1 kbps, DTX
},
},
},
}
cs.updateScore(streams)
require.Equal(t, lossQuality, cs.GetScore())
// setting RTT (i. e. adding delay) should reduce quality
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
PacketsLost: 10,
Bytes: 18000 * 2 / 8, // 20 kbps - 2 kbps, i. e 10% lost
RttMax: 50, // ms
},
},
},
}
cs.updateScore(streams)
rttQuality := cs.GetScore()
require.Less(t, rttQuality, lossQuality)
// adding jitter (i. e. more delay) should reduce quality
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
PacketsLost: 10,
Bytes: 18000 * 2 / 8, // 20 kbps - 2 kbps, i. e 10% lost
RttMax: 50, // ms
JitterMax: 10000, // us
},
},
},
}
cs.updateScore(streams)
require.Less(t, cs.GetScore(), rttQuality)
})
t.Run("video quality changes over time", func(t *testing.T) {
maxExpectedLayer := int32(0)
currentLayer := int32(0)
isReducedQuality := false
cs := NewConnectionStats(ConnectionStatsParams{
MimeType: "video/VP8",
GetMaxExpectedLayer: func() int32 {
return maxExpectedLayer
},
GetIsReducedQuality: func() (int32, bool) {
return maxExpectedLayer - currentLayer, isReducedQuality
},
})
cs.Start(&livekit.TrackInfo{
Type: livekit.TrackType_VIDEO,
Source: livekit.TrackSource_CAMERA,
Layers: []*livekit.VideoLayer{
{
Quality: livekit.VideoQuality_HIGH,
Width: 1280,
Height: 720,
Bitrate: 1_700_000,
},
{
Quality: livekit.VideoQuality_MEDIUM,
Width: 640,
Height: 360,
Bitrate: 500_000,
},
{
Quality: livekit.VideoQuality_LOW,
Width: 320,
Height: 180,
Bitrate: 150_000,
},
},
})
// first update does nothing
cs.updateScore(nil)
require.Equal(t, float32(5), cs.GetScore())
// second update locks to expected layer
cs.updateScore(nil)
require.Equal(t, float32(5), cs.GetScore())
// layer 0 perfect score
streams := map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 150_000 * 2 / 8, // 150 kbps
Frames: 30, // 15 fps
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(5), cs.GetScore())
maxExpectedLayer = 1
currentLayer = 1
// update locks to expected layer and returns previous score
cs.updateScore(nil)
require.Equal(t, float32(5), cs.GetScore())
// layer 1 at layer 0 bitrate should give lower score
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
1: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 150_000 * 2 / 8, // 150 kbps
Frames: 30, // 15 fps
},
},
},
}
cs.updateScore(streams)
require.Less(t, cs.GetScore(), float32(5))
// layer 1 at optimal bit rate should give max score
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
1: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 500_000 * 2 / 8, // 500 kbps
Frames: 40, // 20 fps
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(5), cs.GetScore())
// a layer at higher than optimal bit rate should give max score
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
1: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 600_000 * 2 / 8, // 600 kbps, optimal is 500 kbps
Frames: 40, // 20 fps
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(5), cs.GetScore())
maxExpectedLayer = 2
currentLayer = 2
// update locks to expected layer and returns previous score
cs.updateScore(nil)
require.Equal(t, float32(5), cs.GetScore())
// layer 2 at layer 1 bitrate should give lower score
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
2: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 500_000 * 2 / 8, // 500 kbps
Frames: 40, // 20 fps
},
},
},
}
cs.updateScore(streams)
require.Less(t, cs.GetScore(), float32(5))
// layer 2 at optimal bit rate should give max score
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
2: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 1_700_000 * 2 / 8, // 1.7 Mbps
Frames: 60, // 30 fps
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(5), cs.GetScore())
// layer 2 at optimal bit rate, but with loss should give less score
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
2: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 1_530_000 * 2 / 8, // 1.7 Mbps - 170 kbps, i. e. 10% loss
Frames: 60, // 30 fps
},
},
},
}
cs.updateScore(streams)
score := cs.GetScore()
require.Less(t, score, float32(5))
// adding rtt (i. e. delay) should reduce score further
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
2: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 1_530_000 * 2 / 8, // 1.7 Mbps - 170 kbps, i. e. 10% loss
Frames: 60, // 30 fps
RttMax: 50, // ms
},
},
},
}
cs.updateScore(streams)
require.Less(t, cs.GetScore(), score)
score = cs.GetScore()
// adding jitter (i. e. delay) should reduce score further
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
2: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 1_530_000 * 2 / 8, // 1.7 Mbps - 170 kbps, i. e. 10% loss
Frames: 60, // 30 fps
RttMax: 50, // ms
JitterMax: 10000, // us
},
},
},
}
cs.updateScore(streams)
require.Less(t, cs.GetScore(), score)
// layer 2 max expected, but max available is layer 1 should reduce one level even if layer 1 is perfect
currentLayer = 1
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
1: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 500_000 * 2 / 8, // 500 kbps
Frames: 40, // 20 fps
},
},
},
}
cs.updateScore(streams)
require.Less(t, float32(4), cs.GetScore())
// layer 2 max expected, but max available is layer 0 should reduce two levels even if layer 0 is perfect
currentLayer = 0
streams = map[uint32]*buffer.StreamStatsWithLayers{
0: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 150_000 * 2 / 8, // 150 kbps
Frames: 30, // 15 fps
},
},
},
}
cs.updateScore(streams)
require.Less(t, float32(3), cs.GetScore())
})
t.Run("screen share quality changes over time", func(t *testing.T) {
isReducedQuality := false
cs := NewConnectionStats(ConnectionStatsParams{
MimeType: "video/H264",
GetMaxExpectedLayer: func() int32 {
return 1
},
GetIsReducedQuality: func() (int32, bool) {
return 0, isReducedQuality
},
})
cs.Start(&livekit.TrackInfo{
Type: livekit.TrackType_VIDEO,
Source: livekit.TrackSource_SCREEN_SHARE,
Layers: []*livekit.VideoLayer{
{
Width: 1920,
Height: 1080,
},
},
})
// first update does nothing
cs.updateScore(nil)
require.Equal(t, float32(5), cs.GetScore())
// second update locks to expected layer
cs.updateScore(nil)
require.Equal(t, float32(5), cs.GetScore())
// perfect conditions should return a score of 5
streams := map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
Bytes: 100000 * 2 / 8, // 100 kbps
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(5), cs.GetScore())
// introduce loss and the score should drop
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
PacketsLost: 10,
Bytes: 90000 * 2 / 8, // 100 kbps - 10 kbps, i. e 10% lost
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(2.0), cs.GetScore())
// small loss, but not reduced quality should give a fairly high score
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
PacketsLost: 2,
Bytes: 98000 * 2 / 8, // 100 kbps - 2 kbps, i. e. 2% loss
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(4.5), cs.GetScore())
// medium level loss, but not reduced quality should give a mid level score
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
PacketsLost: 3,
Bytes: 97000 * 2 / 8, // 100 kbps - 3 kbps, i. e 3% lost
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(3.5), cs.GetScore())
// medium level loss, reduced quality should give a mid level score
isReducedQuality = true
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
PacketsLost: 3,
Bytes: 97000 * 2 / 8, // 100 kbps - 3 kbps, i. e 3% lost
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(3.5), cs.GetScore())
// small loss, reduced quality should give a mid level score
streams = map[uint32]*buffer.StreamStatsWithLayers{
1: &buffer.StreamStatsWithLayers{
Layers: map[int32]*buffer.RTPDeltaInfo{
0: &buffer.RTPDeltaInfo{
Duration: 2 * time.Second,
Packets: 100,
PacketsLost: 2,
Bytes: 99000 * 2 / 8, // 100 kbps - 1 kbps, i. e 1% lost
},
},
},
}
cs.updateScore(streams)
require.Equal(t, float32(3.5), cs.GetScore())
})
}