Expected vs actual Layer based connection quality. (#1509)
* Expected vs actual Layer based connection quality. With VBR streams (like screen share), bit rate is not a good indicator of whether desired layer (spatial/temporal) is achieved due to high variance. Using expected vs actual layer (i. e. distance to desired) can capture any short fall and include it in quality scoring. This PR uses distance to desired, i. e. how many steps it would take to go from actual spatial/temporal -> desired spatial/temporal and that distance is propotionally used (currently it is just linear) to decrease score. * wire up layer transitions for screen share tracks
This commit is contained in:
@@ -74,8 +74,12 @@ func (cs *ConnectionStats) UpdateMute(isMuted bool, at time.Time) {
|
||||
cs.scorer.UpdateMute(isMuted, at)
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) AddTransition(bitrate int64, at time.Time) {
|
||||
cs.scorer.AddTransition(bitrate, at)
|
||||
func (cs *ConnectionStats) AddBitrateTransition(bitrate int64, at time.Time) {
|
||||
cs.scorer.AddBitrateTransition(bitrate, at)
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) AddLayerTransition(distance float64, at time.Time) {
|
||||
cs.scorer.AddLayerTransition(distance, at)
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) GetScoreAndQuality() (float32, livekit.ConnectionQuality) {
|
||||
|
||||
@@ -273,8 +273,8 @@ func TestConnectionQuality(t *testing.T) {
|
||||
cs.UpdateMute(false, now.Add(2*time.Second))
|
||||
|
||||
// bitrate based calculation can drop quality even if there is no loss
|
||||
cs.AddTransition(1_000_000, now)
|
||||
cs.AddTransition(2_000_000, now.Add(2*time.Second))
|
||||
cs.AddBitrateTransition(1_000_000, now)
|
||||
cs.AddBitrateTransition(2_000_000, now.Add(2*time.Second))
|
||||
|
||||
streams = map[uint32]*buffer.StreamStatsWithLayers{
|
||||
1: &buffer.StreamStatsWithLayers{
|
||||
@@ -293,13 +293,13 @@ func TestConnectionQuality(t *testing.T) {
|
||||
|
||||
// a transition to 0 (all layers stopped) should flip quality to EXCELLENT
|
||||
now = now.Add(duration)
|
||||
cs.AddTransition(0, now)
|
||||
cs.AddBitrateTransition(0, now)
|
||||
mos, quality = cs.GetScoreAndQuality()
|
||||
require.Greater(t, float32(4.6), mos)
|
||||
require.Equal(t, livekit.ConnectionQuality_EXCELLENT, quality)
|
||||
})
|
||||
|
||||
t.Run("codecs - packets", func(t *testing.T) {
|
||||
t.Run("codecs - packet", func(t *testing.T) {
|
||||
type expectedQuality struct {
|
||||
packetLossPercentage float64
|
||||
expectedMOS float32
|
||||
@@ -465,7 +465,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("bytes", func(t *testing.T) {
|
||||
t.Run("bitrate", func(t *testing.T) {
|
||||
type transition struct {
|
||||
bitrate int64
|
||||
offset time.Duration
|
||||
@@ -537,7 +537,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
cs.Start(&livekit.TrackInfo{Type: livekit.TrackType_VIDEO}, now)
|
||||
|
||||
for _, tr := range tc.transitions {
|
||||
cs.AddTransition(tr.bitrate, now.Add(tr.offset))
|
||||
cs.AddBitrateTransition(tr.bitrate, now.Add(tr.offset))
|
||||
}
|
||||
|
||||
streams := map[uint32]*buffer.StreamStatsWithLayers{
|
||||
@@ -557,4 +557,90 @@ func TestConnectionQuality(t *testing.T) {
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("layer", func(t *testing.T) {
|
||||
type transition struct {
|
||||
distance float64
|
||||
offset time.Duration
|
||||
}
|
||||
testCases := []struct {
|
||||
name string
|
||||
transitions []transition
|
||||
expectedMOS float32
|
||||
expectedQuality livekit.ConnectionQuality
|
||||
}{
|
||||
// NOTE: Because of EWMA (Exponentially Weighted Moving Average), these cut off points are not exact
|
||||
// each spatial layer missed drops o quality level
|
||||
{
|
||||
name: "excellent",
|
||||
transitions: []transition{
|
||||
{
|
||||
distance: 0.5,
|
||||
},
|
||||
{
|
||||
distance: 0.0,
|
||||
offset: 3 * time.Second,
|
||||
},
|
||||
},
|
||||
expectedMOS: 4.6,
|
||||
expectedQuality: livekit.ConnectionQuality_EXCELLENT,
|
||||
},
|
||||
{
|
||||
name: "good",
|
||||
transitions: []transition{
|
||||
{
|
||||
distance: 1.0,
|
||||
},
|
||||
{
|
||||
distance: 1.5,
|
||||
offset: 2 * time.Second,
|
||||
},
|
||||
},
|
||||
expectedMOS: 4.1,
|
||||
expectedQuality: livekit.ConnectionQuality_GOOD,
|
||||
},
|
||||
{
|
||||
name: "poor",
|
||||
transitions: []transition{
|
||||
{
|
||||
distance: 2.0,
|
||||
},
|
||||
{
|
||||
distance: 2.7,
|
||||
offset: 1 * time.Second,
|
||||
},
|
||||
},
|
||||
expectedMOS: 3.2,
|
||||
expectedQuality: livekit.ConnectionQuality_POOR,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cs := newConnectionStats("video/vp8", false)
|
||||
|
||||
duration := 5 * time.Second
|
||||
now := time.Now()
|
||||
cs.Start(&livekit.TrackInfo{Type: livekit.TrackType_VIDEO}, now)
|
||||
|
||||
for _, tr := range tc.transitions {
|
||||
cs.AddLayerTransition(tr.distance, now.Add(tr.offset))
|
||||
}
|
||||
|
||||
streams := map[uint32]*buffer.StreamStatsWithLayers{
|
||||
123: &buffer.StreamStatsWithLayers{
|
||||
RTPStats: &buffer.RTPDeltaInfo{
|
||||
StartTime: now,
|
||||
Duration: duration,
|
||||
Packets: 200,
|
||||
},
|
||||
},
|
||||
}
|
||||
cs.updateScore(streams, now.Add(duration))
|
||||
mos, quality := cs.GetScoreAndQuality()
|
||||
require.Greater(t, tc.expectedMOS, mos)
|
||||
require.Equal(t, tc.expectedQuality, quality)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ const (
|
||||
increaseFactor = float64(0.4) // slow increase
|
||||
decreaseFactor = float64(0.8) // fast decrease
|
||||
|
||||
distanceWeight = float64(20.0) // each spatial layer missed drops a quality level
|
||||
|
||||
unmuteTimeThreshold = float64(0.5)
|
||||
)
|
||||
|
||||
@@ -63,7 +65,7 @@ func (w *windowStat) calculatePacketScore(plw float64) float64 {
|
||||
return score
|
||||
}
|
||||
|
||||
func (w *windowStat) calculateByteScore(expectedBitrate int64) float64 {
|
||||
func (w *windowStat) calculateBitrateScore(expectedBitrate int64) float64 {
|
||||
if expectedBitrate == 0 {
|
||||
// unsupported mode OR all layers stopped
|
||||
return maxScore
|
||||
@@ -100,11 +102,16 @@ func (w *windowStat) String() string {
|
||||
|
||||
// ------------------------------------------
|
||||
|
||||
type layerTransition struct {
|
||||
type bitrateTransition struct {
|
||||
startedAt time.Time
|
||||
bitrate int64
|
||||
}
|
||||
|
||||
type layerTransition struct {
|
||||
startedAt time.Time
|
||||
distance float64
|
||||
}
|
||||
|
||||
type qualityScorerParams struct {
|
||||
PacketLossWeight float64
|
||||
Logger logger.Logger
|
||||
@@ -121,12 +128,13 @@ type qualityScorer struct {
|
||||
mutedAt time.Time
|
||||
unmutedAt time.Time
|
||||
|
||||
layersMutedAt time.Time
|
||||
layersUnmutedAt time.Time
|
||||
bitrateMutedAt time.Time
|
||||
bitrateUnmutedAt time.Time
|
||||
|
||||
maxPPS float64
|
||||
|
||||
transitions []layerTransition
|
||||
bitrateTransitions []bitrateTransition
|
||||
layerTransitions []layerTransition
|
||||
}
|
||||
|
||||
func newQualityScorer(params qualityScorerParams) *qualityScorer {
|
||||
@@ -155,31 +163,42 @@ func (q *qualityScorer) UpdateMute(isMuted bool, at time.Time) {
|
||||
}
|
||||
}
|
||||
|
||||
func (q *qualityScorer) AddTransition(bitrate int64, at time.Time) {
|
||||
func (q *qualityScorer) AddBitrateTransition(bitrate int64, at time.Time) {
|
||||
q.lock.Lock()
|
||||
defer q.lock.Unlock()
|
||||
|
||||
q.transitions = append(q.transitions, layerTransition{
|
||||
q.bitrateTransitions = append(q.bitrateTransitions, bitrateTransition{
|
||||
startedAt: at,
|
||||
bitrate: bitrate,
|
||||
})
|
||||
|
||||
if bitrate == 0 {
|
||||
q.layersMutedAt = at
|
||||
q.bitrateMutedAt = at
|
||||
q.score = maxScore
|
||||
} else {
|
||||
if q.layersUnmutedAt.IsZero() || q.layersMutedAt.After(q.layersUnmutedAt) {
|
||||
q.layersUnmutedAt = at
|
||||
if q.bitrateUnmutedAt.IsZero() || q.bitrateMutedAt.After(q.bitrateUnmutedAt) {
|
||||
q.bitrateUnmutedAt = at
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (q *qualityScorer) AddLayerTransition(distance float64, at time.Time) {
|
||||
q.lock.Lock()
|
||||
defer q.lock.Unlock()
|
||||
|
||||
q.layerTransitions = append(q.layerTransitions, layerTransition{
|
||||
startedAt: at,
|
||||
distance: distance,
|
||||
})
|
||||
}
|
||||
|
||||
func (q *qualityScorer) Update(stat *windowStat, at time.Time) {
|
||||
q.lock.Lock()
|
||||
defer q.lock.Unlock()
|
||||
|
||||
// always update transitions
|
||||
expectedBitrate := q.getExpectedBitsAndUpdateTransitions(at)
|
||||
expectedDistance := q.getExpectedDistanceAndUpdateTransitions(at)
|
||||
|
||||
// nothing to do when muted or not unmuted for long enough
|
||||
// NOTE: it is possible that unmute -> mute -> unmute transition happens in the
|
||||
@@ -187,7 +206,7 @@ func (q *qualityScorer) Update(stat *windowStat, at time.Time) {
|
||||
// to stable and quality EXCELLENT for responsiveness. On an unmute, the
|
||||
// entire window data is considered (as long as enough time has passed since
|
||||
// unmute) including the data before mute.
|
||||
if q.isMuted() || !q.isUnmutedEnough(at) || q.areLayersMuted() {
|
||||
if q.isMuted() || !q.isUnmutedEnough(at) || q.isBitrateMuted() {
|
||||
q.lastUpdateAt = at
|
||||
return
|
||||
}
|
||||
@@ -199,13 +218,24 @@ func (q *qualityScorer) Update(stat *windowStat, at time.Time) {
|
||||
score = poorScore
|
||||
} else {
|
||||
packetScore := stat.calculatePacketScore(q.getPacketLossWeight(stat))
|
||||
byteScore := stat.calculateByteScore(expectedBitrate)
|
||||
if packetScore < byteScore {
|
||||
bitrateScore := stat.calculateBitrateScore(expectedBitrate)
|
||||
layerScore := math.Max(math.Min(maxScore, maxScore-(expectedDistance*distanceWeight)), 0.0)
|
||||
|
||||
minScore := math.Min(packetScore, bitrateScore)
|
||||
minScore = math.Min(minScore, layerScore)
|
||||
|
||||
switch {
|
||||
case packetScore == minScore:
|
||||
reason = "packet"
|
||||
score = packetScore
|
||||
} else {
|
||||
|
||||
case bitrateScore == minScore:
|
||||
reason = "bitrate"
|
||||
score = byteScore
|
||||
score = bitrateScore
|
||||
|
||||
case layerScore == minScore:
|
||||
reason = "layer"
|
||||
score = layerScore
|
||||
}
|
||||
}
|
||||
factor := increaseFactor
|
||||
@@ -224,6 +254,7 @@ func (q *qualityScorer) Update(stat *windowStat, at time.Time) {
|
||||
"quality", scoreToConnectionQuality(score),
|
||||
"stat", stat,
|
||||
"expectedBitrate", expectedBitrate,
|
||||
"expectedDistance", expectedDistance,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -244,10 +275,10 @@ func (q *qualityScorer) isUnmutedEnough(at time.Time) bool {
|
||||
}
|
||||
|
||||
var sinceLayersUnmute time.Duration
|
||||
if q.layersUnmutedAt.IsZero() {
|
||||
if q.bitrateUnmutedAt.IsZero() {
|
||||
sinceLayersUnmute = at.Sub(q.lastUpdateAt)
|
||||
} else {
|
||||
sinceLayersUnmute = at.Sub(q.layersUnmutedAt)
|
||||
sinceLayersUnmute = at.Sub(q.bitrateUnmutedAt)
|
||||
}
|
||||
|
||||
validDuration := sinceUnmute
|
||||
@@ -260,8 +291,8 @@ func (q *qualityScorer) isUnmutedEnough(at time.Time) bool {
|
||||
return validDuration.Seconds()/sinceLastUpdate.Seconds() > unmuteTimeThreshold
|
||||
}
|
||||
|
||||
func (q *qualityScorer) areLayersMuted() bool {
|
||||
return !q.layersMutedAt.IsZero() && (q.layersUnmutedAt.IsZero() || q.layersMutedAt.After(q.layersUnmutedAt))
|
||||
func (q *qualityScorer) isBitrateMuted() bool {
|
||||
return !q.bitrateMutedAt.IsZero() && (q.bitrateUnmutedAt.IsZero() || q.bitrateMutedAt.After(q.bitrateUnmutedAt))
|
||||
}
|
||||
|
||||
func (q *qualityScorer) getPacketLossWeight(stat *windowStat) float64 {
|
||||
@@ -283,39 +314,97 @@ func (q *qualityScorer) getPacketLossWeight(stat *windowStat) float64 {
|
||||
}
|
||||
|
||||
func (q *qualityScorer) getExpectedBitsAndUpdateTransitions(at time.Time) int64 {
|
||||
if len(q.transitions) == 0 {
|
||||
if len(q.bitrateTransitions) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
var startedAt time.Time
|
||||
totalBits := float64(0.0)
|
||||
for idx := 0; idx < len(q.transitions)-1; idx++ {
|
||||
lt := &q.transitions[idx]
|
||||
ltNext := &q.transitions[idx+1]
|
||||
for idx := 0; idx < len(q.bitrateTransitions)-1; idx++ {
|
||||
bt := &q.bitrateTransitions[idx]
|
||||
btNext := &q.bitrateTransitions[idx+1]
|
||||
|
||||
if bt.startedAt.After(q.lastUpdateAt) {
|
||||
startedAt = bt.startedAt
|
||||
} else {
|
||||
startedAt = q.lastUpdateAt
|
||||
}
|
||||
totalBits += btNext.startedAt.Sub(startedAt).Seconds() * float64(bt.bitrate)
|
||||
}
|
||||
|
||||
// last transition
|
||||
bt := &q.bitrateTransitions[len(q.bitrateTransitions)-1]
|
||||
if bt.startedAt.After(q.lastUpdateAt) {
|
||||
startedAt = bt.startedAt
|
||||
} else {
|
||||
startedAt = q.lastUpdateAt
|
||||
}
|
||||
totalBits += at.Sub(startedAt).Seconds() * float64(bt.bitrate)
|
||||
|
||||
// set up last bit rate as the startig bit rate for next analysis window
|
||||
q.bitrateTransitions = []bitrateTransition{bitrateTransition{
|
||||
startedAt: at,
|
||||
bitrate: bt.bitrate,
|
||||
}}
|
||||
|
||||
return int64(totalBits)
|
||||
}
|
||||
|
||||
func (q *qualityScorer) getExpectedDistanceAndUpdateTransitions(at time.Time) float64 {
|
||||
if len(q.layerTransitions) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
var startedAt time.Time
|
||||
totalDistance := float64(0.0)
|
||||
totalDuration := time.Duration(0)
|
||||
for idx := 0; idx < len(q.layerTransitions)-1; idx++ {
|
||||
lt := &q.layerTransitions[idx]
|
||||
ltNext := &q.layerTransitions[idx+1]
|
||||
|
||||
if lt.startedAt.After(q.lastUpdateAt) {
|
||||
startedAt = lt.startedAt
|
||||
} else {
|
||||
startedAt = q.lastUpdateAt
|
||||
}
|
||||
totalBits += ltNext.startedAt.Sub(startedAt).Seconds() * float64(lt.bitrate)
|
||||
dur := ltNext.startedAt.Sub(startedAt)
|
||||
totalDuration += dur
|
||||
|
||||
dist := lt.distance
|
||||
if dist < 0.0 {
|
||||
// negative distances are overshoot, that does not compensate for shortfalls, so use optimal, i. e. 0 distance when overshooting
|
||||
dist = 0.0
|
||||
}
|
||||
totalDistance += dur.Seconds() * float64(dist)
|
||||
}
|
||||
|
||||
// last transition
|
||||
lt := &q.transitions[len(q.transitions)-1]
|
||||
lt := &q.layerTransitions[len(q.layerTransitions)-1]
|
||||
if lt.startedAt.After(q.lastUpdateAt) {
|
||||
startedAt = lt.startedAt
|
||||
} else {
|
||||
startedAt = q.lastUpdateAt
|
||||
}
|
||||
totalBits += at.Sub(startedAt).Seconds() * float64(lt.bitrate)
|
||||
dur := at.Sub(startedAt)
|
||||
totalDuration += dur
|
||||
|
||||
// set up last bit rate as the startig bit rate for next analysis window
|
||||
q.transitions = []layerTransition{layerTransition{
|
||||
dist := lt.distance
|
||||
if dist < 0.0 {
|
||||
dist = 0.0
|
||||
}
|
||||
totalDistance += dur.Seconds() * float64(dist)
|
||||
|
||||
// set up last distance as the startig distance for next analysis window
|
||||
q.layerTransitions = []layerTransition{layerTransition{
|
||||
startedAt: at,
|
||||
bitrate: lt.bitrate,
|
||||
distance: lt.distance,
|
||||
}}
|
||||
|
||||
return int64(totalBits)
|
||||
if totalDuration == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return totalDistance / totalDuration.Seconds()
|
||||
}
|
||||
|
||||
func (q *qualityScorer) GetScoreAndQuality() (float32, livekit.ConnectionQuality) {
|
||||
|
||||
Reference in New Issue
Block a user