Files
livekit-3d/pkg/sfu/connectionquality/mos.go
T
Raja Subramanian 9032db857c 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
2022-06-18 21:58:47 +05:30

98 lines
2.3 KiB
Go

package connectionquality
import (
"time"
"github.com/livekit/protocol/livekit"
"github.com/livekit/rtcscore-go/pkg/rtcmos"
)
func Score2Rating(score float32) livekit.ConnectionQuality {
if score > 4.0 {
return livekit.ConnectionQuality_EXCELLENT
}
if score > 3.0 {
return livekit.ConnectionQuality_GOOD
}
return livekit.ConnectionQuality_POOR
}
func getBitRate(interval float64, bytes uint64) int32 {
return int32(float64(bytes*8) / 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
}
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
}
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)),
}
}
func AudioTrackScore(params TrackScoreParams) float32 {
stat := getRtcMosStat(params)
stat.AudioConfig = &rtcmos.AudioConfig{}
if params.DtxDisabled {
flag := false
stat.AudioConfig.Dtx = &flag
}
scores := rtcmos.Score([]rtcmos.Stat{stat})
if len(scores) == 1 {
return float32(scores[0].AudioScore)
}
return 0
}
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)
}
return 0
}