Connection quality clean up (#766)
* WIP commit * WIP commit * Remove debug * Revert to reduce diff * Fix tests * Determine spatial layer from track info quality if non-simulcast * Adjust for invalid layer on no rid, previously that function was returning 0 for no rid case * Fall back to top level width/height if there are no layers * Use duration from RTPDeltaInfo
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package connectionquality
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -24,11 +22,9 @@ type ConnectionStatsParams struct {
|
||||
UpdateInterval time.Duration
|
||||
CodecType webrtc.RTPCodecType
|
||||
CodecName string
|
||||
DtxDisabled bool
|
||||
DtxDisabled bool // RAJA-TODO - fix this
|
||||
MimeType string
|
||||
GetDeltaStats func() map[uint32]*buffer.StreamStatsWithLayers
|
||||
GetQualityParams func() *buffer.ConnectionQualityParams
|
||||
GetIsReducedQuality func() bool
|
||||
GetLayerDimension func(int32) (uint32, uint32)
|
||||
GetMaxExpectedLayer func() *livekit.VideoLayer
|
||||
Logger logger.Logger
|
||||
@@ -39,8 +35,9 @@ type ConnectionStats struct {
|
||||
|
||||
onStatsUpdate func(cs *ConnectionStats, stat *livekit.AnalyticsStat)
|
||||
|
||||
lock sync.RWMutex
|
||||
score float32
|
||||
lock sync.RWMutex
|
||||
score float32
|
||||
lastUpdate time.Time
|
||||
|
||||
done chan struct{}
|
||||
isClosed atomic.Bool
|
||||
@@ -77,64 +74,65 @@ func (cs *ConnectionStats) GetScore() float32 {
|
||||
return cs.score
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) updateScore(streams []*livekit.AnalyticsStream, iteration uint64) float32 {
|
||||
func (cs *ConnectionStats) updateScore(streams map[uint32]*buffer.StreamStatsWithLayers) float32 {
|
||||
cs.lock.Lock()
|
||||
defer cs.lock.Unlock()
|
||||
|
||||
// Initial interval will have partial data
|
||||
if iteration < 2 {
|
||||
if cs.lastUpdate.IsZero() {
|
||||
cs.lastUpdate = time.Now()
|
||||
cs.score = 5
|
||||
return cs.score
|
||||
}
|
||||
|
||||
s := cs.params.GetQualityParams()
|
||||
var qualityParam buffer.ConnectionQualityParams
|
||||
if s == nil {
|
||||
cs.lastUpdate = time.Now()
|
||||
// RAJA-TODO - take mute into account
|
||||
// RAJA-TODO - maybe a probation period at the start, coming out of mute?
|
||||
// RAJA-TODO - maybe a probation period when a layer starts/stops?
|
||||
// RAJA-TODO - maybe layer changes should be notified here directly rather than pulling
|
||||
// RAJA-TODO - maybe track info should be set here and this can know max published without having to pull every time
|
||||
maxAvailableLayer, maxAvailableLayerStats := cs.getMaxAvailableLayerStats(streams)
|
||||
if maxAvailableLayerStats == nil {
|
||||
// retain old score as stats will not be available when muted
|
||||
return cs.score
|
||||
} else {
|
||||
qualityParam = *s
|
||||
if math.IsInf(float64(qualityParam.Jitter), 0) {
|
||||
qualityParam.Jitter = 0
|
||||
}
|
||||
if math.IsInf(float64(qualityParam.LossPercentage), 0) {
|
||||
qualityParam.LossPercentage = 0
|
||||
}
|
||||
}
|
||||
|
||||
interval := cs.params.UpdateInterval
|
||||
if interval == 0 {
|
||||
interval = UpdateInterval
|
||||
params := TrackScoreParams{
|
||||
Duration: maxAvailableLayerStats.Duration,
|
||||
Codec: cs.params.CodecName,
|
||||
PacketsExpected: maxAvailableLayerStats.Packets + maxAvailableLayerStats.PacketsPadding,
|
||||
PacketsLost: maxAvailableLayerStats.PacketsLost,
|
||||
Bytes: maxAvailableLayerStats.Bytes,
|
||||
Frames: maxAvailableLayerStats.Frames,
|
||||
Jitter: maxAvailableLayerStats.JitterMax,
|
||||
Rtt: maxAvailableLayerStats.RttMax,
|
||||
}
|
||||
if cs.params.CodecType == webrtc.RTPCodecTypeAudio {
|
||||
totalBytes, _, _ := cs.getBytesFramesFromStreams(streams)
|
||||
cs.score = AudioConnectionScore(interval, int64(totalBytes), s, cs.params.DtxDisabled)
|
||||
} else {
|
||||
|
||||
switch cs.params.CodecType {
|
||||
case webrtc.RTPCodecTypeAudio:
|
||||
// RAJA-TODO - need to set DTX in params
|
||||
cs.score = AudioTrackScore(params)
|
||||
|
||||
case webrtc.RTPCodecTypeVideo:
|
||||
// get tracks expected max layer and dimensions
|
||||
expectedLayer := cs.params.GetMaxExpectedLayer()
|
||||
if expectedLayer == nil || utils.SpatialLayerForQuality(expectedLayer.Quality) == buffer.InvalidLayerSpatial {
|
||||
maxExpectedLayer := cs.params.GetMaxExpectedLayer()
|
||||
if maxExpectedLayer == nil || utils.SpatialLayerForQuality(maxExpectedLayer.Quality) == buffer.InvalidLayerSpatial {
|
||||
return cs.score
|
||||
}
|
||||
|
||||
// get bytes/frames and max later from actual stream stats
|
||||
totalBytes, totalFrames, maxLayer := cs.getBytesFramesFromStreams(streams)
|
||||
var actualHeight uint32
|
||||
var actualWidth uint32
|
||||
// if data present, but maxLayer == -1 no layer info available, set actual to expected, else fetch
|
||||
if maxLayer == buffer.InvalidLayerSpatial && totalBytes > 0 {
|
||||
actualHeight = expectedLayer.Height
|
||||
actualWidth = expectedLayer.Width
|
||||
} else {
|
||||
actualWidth, actualHeight = cs.params.GetLayerDimension(maxLayer)
|
||||
if maxAvailableLayerStats != nil {
|
||||
params.ActualWidth, params.ActualHeight = cs.params.GetLayerDimension(maxAvailableLayer)
|
||||
}
|
||||
|
||||
cs.score = VideoConnectionScore(interval, int64(totalBytes), int64(totalFrames), &qualityParam, cs.params.CodecName,
|
||||
int32(expectedLayer.Height), int32(expectedLayer.Width), int32(actualHeight), int32(actualWidth))
|
||||
params.ExpectedWidth = maxExpectedLayer.Width
|
||||
params.ExpectedHeight = maxExpectedLayer.Height
|
||||
cs.score = VideoTrackScore(params)
|
||||
}
|
||||
|
||||
return cs.score
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) getStat(iteration uint64) *livekit.AnalyticsStat {
|
||||
func (cs *ConnectionStats) getStat() *livekit.AnalyticsStat {
|
||||
if cs.params.GetDeltaStats == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -155,14 +153,14 @@ func (cs *ConnectionStats) getStat(iteration uint64) *livekit.AnalyticsStat {
|
||||
//
|
||||
if cs.params.CodecType == webrtc.RTPCodecTypeVideo && (len(streams) > 1 || len(stream.Layers) > 1) {
|
||||
for layer, layerStats := range stream.Layers {
|
||||
as.VideoLayers = append(as.VideoLayers, ToAnalyticsVideoLayer(layer, &layerStats))
|
||||
as.VideoLayers = append(as.VideoLayers, ToAnalyticsVideoLayer(layer, layerStats))
|
||||
}
|
||||
}
|
||||
|
||||
analyticsStreams = append(analyticsStreams, as)
|
||||
}
|
||||
|
||||
score := cs.updateScore(analyticsStreams, iteration)
|
||||
score := cs.updateScore(streams)
|
||||
|
||||
return &livekit.AnalyticsStat{
|
||||
Score: score,
|
||||
@@ -180,16 +178,13 @@ func (cs *ConnectionStats) updateStatsWorker() {
|
||||
tk := time.NewTicker(interval)
|
||||
defer tk.Stop()
|
||||
|
||||
// Delay sending scores until 2nd cycle, as 1st will be partial.
|
||||
counter := uint64(0)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-cs.done:
|
||||
return
|
||||
|
||||
case <-tk.C:
|
||||
stat := cs.getStat(counter)
|
||||
stat := cs.getStat()
|
||||
if stat == nil {
|
||||
continue
|
||||
}
|
||||
@@ -197,9 +192,6 @@ func (cs *ConnectionStats) updateStatsWorker() {
|
||||
if cs.onStatsUpdate != nil {
|
||||
cs.onStatsUpdate(cs, stat)
|
||||
}
|
||||
|
||||
counter++
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,49 +215,26 @@ func ToAnalyticsStream(ssrc uint32, deltaStats *buffer.RTPDeltaInfo) *livekit.An
|
||||
}
|
||||
}
|
||||
|
||||
func ToAnalyticsVideoLayer(layer int, layerStats *buffer.LayerStats) *livekit.AnalyticsVideoLayer {
|
||||
func ToAnalyticsVideoLayer(layer int32, layerStats *buffer.RTPDeltaInfo) *livekit.AnalyticsVideoLayer {
|
||||
return &livekit.AnalyticsVideoLayer{
|
||||
Layer: int32(layer),
|
||||
Packets: layerStats.Packets,
|
||||
Bytes: layerStats.Bytes,
|
||||
Layer: layer,
|
||||
Packets: layerStats.Packets + layerStats.PacketsDuplicate + layerStats.PacketsPadding,
|
||||
Bytes: layerStats.Bytes + layerStats.BytesDuplicate + layerStats.BytesPadding,
|
||||
Frames: layerStats.Frames,
|
||||
}
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) getBytesFramesFromStreams(streams []*livekit.AnalyticsStream) (totalBytes uint64, totalFrames uint32, maxLayer int32) {
|
||||
layerStats := make(map[int32]buffer.LayerStats)
|
||||
hasLayers := false
|
||||
maxLayer = buffer.InvalidLayerSpatial
|
||||
func (cs *ConnectionStats) getMaxAvailableLayerStats(streams map[uint32]*buffer.StreamStatsWithLayers) (int32, *buffer.RTPDeltaInfo) {
|
||||
maxAvailableLayer := buffer.InvalidLayerSpatial
|
||||
var maxAvailableLayerStats *buffer.RTPDeltaInfo
|
||||
for _, stream := range streams {
|
||||
// get frames/bytes/packets from video layers if available. Store per layer in layerStats map
|
||||
if len(stream.VideoLayers) > 0 {
|
||||
hasLayers = true
|
||||
|
||||
layers := stream.VideoLayers
|
||||
// find max quality 0(LOW), 1(MED), 2(HIGH) . sort on layer.Layer desc
|
||||
sort.Slice(layers, func(i, j int) bool {
|
||||
return layers[i].Layer > layers[j].Layer
|
||||
})
|
||||
|
||||
layerStats[layers[0].Layer] = buffer.LayerStats{
|
||||
Bytes: layers[0].GetBytes(),
|
||||
Frames: layers[0].GetFrames(),
|
||||
for layer, layerStats := range stream.Layers {
|
||||
if int32(layer) > maxAvailableLayer {
|
||||
maxAvailableLayer = int32(layer)
|
||||
maxAvailableLayerStats = layerStats
|
||||
}
|
||||
if layers[0].Layer > maxLayer {
|
||||
maxLayer = layers[0].Layer
|
||||
}
|
||||
} else {
|
||||
totalFrames += stream.GetFrames()
|
||||
totalBytes += stream.GetPrimaryBytes()
|
||||
}
|
||||
}
|
||||
if hasLayers {
|
||||
if stats, ok := layerStats[maxLayer]; ok {
|
||||
return stats.Bytes, stats.Frames, maxLayer
|
||||
} else {
|
||||
return 0, 0, buffer.InvalidLayerSpatial
|
||||
}
|
||||
}
|
||||
return totalBytes, totalFrames, maxLayer
|
||||
|
||||
return maxAvailableLayer, maxAvailableLayerStats
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package connectionquality
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/rtcscore-go/pkg/rtcmos"
|
||||
)
|
||||
@@ -19,30 +18,55 @@ func Score2Rating(score float32) livekit.ConnectionQuality {
|
||||
return livekit.ConnectionQuality_POOR
|
||||
}
|
||||
|
||||
func getBitRate(interval float64, totalBytes int64) int32 {
|
||||
return int32(float64(totalBytes*8) / interval)
|
||||
func getBitRate(interval float64, bytes uint64) int32 {
|
||||
return int32(float64(bytes*8) / interval)
|
||||
}
|
||||
|
||||
func getFrameRate(interval float64, totalFrames int64) int32 {
|
||||
return int32(float64(totalFrames) / interval)
|
||||
func getFrameRate(interval float64, frames uint32) int32 {
|
||||
return int32(float64(frames) / interval)
|
||||
}
|
||||
|
||||
func getLossPercentage(expected uint32, lost uint32) float32 {
|
||||
if expected == 0 {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
return float32(lost) * 100.0 / float32(expected)
|
||||
}
|
||||
|
||||
func int32Ptr(x int32) *int32 {
|
||||
return &x
|
||||
}
|
||||
|
||||
func AudioConnectionScore(interval time.Duration, totalBytes int64,
|
||||
qualityParam *buffer.ConnectionQualityParams, dtxDisabled bool) float32 {
|
||||
type TrackScoreParams struct {
|
||||
Duration time.Duration
|
||||
Codec string
|
||||
PacketsExpected uint32
|
||||
PacketsLost uint32
|
||||
Bytes uint64
|
||||
Frames uint32
|
||||
Jitter float64
|
||||
Rtt uint32
|
||||
DtxDisabled bool
|
||||
ActualWidth uint32
|
||||
ActualHeight uint32
|
||||
ExpectedWidth uint32
|
||||
ExpectedHeight uint32
|
||||
}
|
||||
|
||||
stat := rtcmos.Stat{
|
||||
Bitrate: getBitRate(interval.Seconds(), totalBytes),
|
||||
PacketLoss: qualityParam.LossPercentage,
|
||||
RoundTripTime: int32Ptr(int32(qualityParam.Rtt)),
|
||||
BufferDelay: int32Ptr(int32(qualityParam.Jitter)),
|
||||
AudioConfig: &rtcmos.AudioConfig{},
|
||||
func getRtcMosStat(params TrackScoreParams) rtcmos.Stat {
|
||||
return rtcmos.Stat{
|
||||
Bitrate: getBitRate(params.Duration.Seconds(), params.Bytes),
|
||||
PacketLoss: getLossPercentage(params.PacketsExpected, params.PacketsLost),
|
||||
RoundTripTime: int32Ptr(int32(params.Rtt)),
|
||||
BufferDelay: int32Ptr(int32(params.Jitter / 1000.0)),
|
||||
}
|
||||
}
|
||||
|
||||
if dtxDisabled {
|
||||
func AudioTrackScore(params TrackScoreParams) float32 {
|
||||
stat := getRtcMosStat(params)
|
||||
stat.AudioConfig = &rtcmos.AudioConfig{}
|
||||
if params.DtxDisabled {
|
||||
flag := false
|
||||
stat.AudioConfig.Dtx = &flag
|
||||
}
|
||||
@@ -54,22 +78,17 @@ func AudioConnectionScore(interval time.Duration, totalBytes int64,
|
||||
return 0
|
||||
}
|
||||
|
||||
func VideoConnectionScore(interval time.Duration, totalBytes int64, totalFrames int64, qualityParam *buffer.ConnectionQualityParams,
|
||||
codec string, expectedHeight int32, expectedWidth int32, actualHeight int32, actualWidth int32) float32 {
|
||||
stat := rtcmos.Stat{
|
||||
Bitrate: getBitRate(interval.Seconds(), totalBytes),
|
||||
PacketLoss: qualityParam.LossPercentage,
|
||||
RoundTripTime: int32Ptr(int32(qualityParam.Rtt)),
|
||||
BufferDelay: int32Ptr(int32(qualityParam.Jitter)),
|
||||
VideoConfig: &rtcmos.VideoConfig{
|
||||
FrameRate: int32Ptr(getFrameRate(interval.Seconds(), totalFrames)),
|
||||
Codec: codec,
|
||||
ExpectedHeight: &expectedHeight,
|
||||
ExpectedWidth: &expectedWidth,
|
||||
Height: &actualHeight,
|
||||
Width: &actualWidth,
|
||||
},
|
||||
func VideoTrackScore(params TrackScoreParams) float32 {
|
||||
stat := getRtcMosStat(params)
|
||||
stat.VideoConfig = &rtcmos.VideoConfig{
|
||||
FrameRate: int32Ptr(getFrameRate(params.Duration.Seconds(), params.Frames)),
|
||||
Codec: params.Codec,
|
||||
ExpectedWidth: int32Ptr(int32(params.ExpectedWidth)),
|
||||
ExpectedHeight: int32Ptr(int32(params.ExpectedHeight)),
|
||||
Width: int32Ptr(int32(params.ActualWidth)),
|
||||
Height: int32Ptr(int32(params.ActualHeight)),
|
||||
}
|
||||
|
||||
scores := rtcmos.Score([]rtcmos.Stat{stat})
|
||||
if len(scores) == 1 {
|
||||
return float32(scores[0].VideoScore)
|
||||
|
||||
Reference in New Issue
Block a user