Telemetry connection scores (#377)
* octets - total bytes needs to be uint64 uint32 wraps at 4GB Signed-off-by: shishir gowda <shishir@livekit.io> * Cleanup stats handler to use connectionQuality stats remove per packet rtcp handlers, buffer stats * cleanup connection stats * Update mediatrack to store rtcp stats in connection stats * Update downstream handling of connection stats and telemetry * Update telemetry tests Signed-off-by: shishir gowda <shishir@livekit.io> * Misc fixes Signed-off-by: shishir gowda <shishir@livekit.io> * Minor fix to avoid accessing buffer before its allocated Signed-off-by: shishir gowda <shishir@livekit.io> * start updateStats worker in AddReciever() Signed-off-by: shishir gowda <shishir@livekit.io> * Use previous score to calculate avg scores * Restructure connectionStats Signed-off-by: shishir gowda <shishir@livekit.io>
This commit is contained in:
@@ -1,25 +1,28 @@
|
||||
package connectionquality
|
||||
|
||||
import (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
import "sync"
|
||||
|
||||
type ConnectionStat struct {
|
||||
PacketsLost uint32
|
||||
Delay uint32
|
||||
Jitter uint32
|
||||
TotalPackets uint32
|
||||
LastSeqNum uint32
|
||||
TotalBytes uint64
|
||||
}
|
||||
|
||||
type ConnectionStats struct {
|
||||
Curr *ConnectionStat
|
||||
Prev *ConnectionStat
|
||||
Score float64
|
||||
Lock sync.Mutex
|
||||
ConnectionStat
|
||||
Prev *ConnectionStat
|
||||
Delay uint32
|
||||
Jitter uint32
|
||||
NackCount int32
|
||||
PliCount int32
|
||||
FirCount int32
|
||||
Score float64
|
||||
}
|
||||
|
||||
func NewConnectionStats() *ConnectionStats {
|
||||
return &ConnectionStats{Curr: &ConnectionStat{}, Prev: &ConnectionStat{}, Score: 4.0}
|
||||
return &ConnectionStats{Prev: &ConnectionStat{}, Score: 4.0}
|
||||
}
|
||||
|
||||
func getTotalPackets(curSN, prevSN uint32) uint32 {
|
||||
@@ -33,18 +36,22 @@ func getTotalPackets(curSN, prevSN uint32) uint32 {
|
||||
return increment
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) CalculateAudioScore() float64 {
|
||||
func (cs *ConnectionStats) UpdateStats(totalBytes uint64) ConnectionStat {
|
||||
// update feedback stats
|
||||
current := cs.Curr
|
||||
previous := cs.Prev
|
||||
|
||||
// Update TotalPackets from SeqNum here
|
||||
current.TotalPackets += getTotalPackets(current.LastSeqNum, previous.LastSeqNum)
|
||||
cs.Score = ConnectionScore(current, previous, livekit.TrackType_AUDIO)
|
||||
cs.TotalPackets += getTotalPackets(cs.LastSeqNum, previous.LastSeqNum)
|
||||
cs.TotalBytes = totalBytes
|
||||
|
||||
var delta ConnectionStat
|
||||
|
||||
delta.TotalPackets = cs.TotalPackets - previous.TotalPackets
|
||||
delta.PacketsLost = cs.PacketsLost - previous.PacketsLost
|
||||
delta.TotalBytes = cs.TotalBytes - previous.TotalBytes
|
||||
|
||||
// store previous stats
|
||||
cs.Prev = current
|
||||
cs.Curr = &ConnectionStat{TotalPackets: cs.Prev.TotalPackets, PacketsLost: cs.Prev.PacketsLost}
|
||||
cs.Prev = &ConnectionStat{TotalPackets: cs.TotalPackets, PacketsLost: cs.PacketsLost, TotalBytes: cs.TotalBytes, LastSeqNum: cs.LastSeqNum}
|
||||
|
||||
return cs.Score
|
||||
return delta
|
||||
}
|
||||
|
||||
@@ -22,26 +22,20 @@ func Score2Rating(score float64) livekit.ConnectionQuality {
|
||||
return livekit.ConnectionQuality_POOR
|
||||
}
|
||||
|
||||
func mosAudioEmodel(cur, prev *ConnectionStat) float64 {
|
||||
|
||||
if cur == nil {
|
||||
return 0.0
|
||||
}
|
||||
func mosAudioEmodel(delta ConnectionStat, jitter uint32) float64 {
|
||||
|
||||
// find percentage of lost packets in this window
|
||||
deltaTotalPackets := cur.TotalPackets - prev.TotalPackets
|
||||
if deltaTotalPackets == 0 {
|
||||
if delta.TotalPackets == 0 {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
deltaTotalLostPackets := cur.PacketsLost - prev.PacketsLost
|
||||
percentageLost := (float64(deltaTotalLostPackets) / float64(deltaTotalPackets)) * 100
|
||||
percentageLost := (float64(delta.PacketsLost) / float64(delta.TotalPackets)) * 100
|
||||
|
||||
rx := 93.2 - percentageLost
|
||||
ry := 0.18*rx*rx - 27.9*rx + 1126.62
|
||||
|
||||
// Jitter is in MicroSecs (1/1e6) units. Convert it to MilliSecs
|
||||
d := float64(rtt + (cur.Jitter / 1000))
|
||||
d := float64(rtt + (jitter / 1000))
|
||||
h := d - 177.3
|
||||
if h < 0 {
|
||||
h = 0
|
||||
@@ -61,7 +55,7 @@ func mosAudioEmodel(cur, prev *ConnectionStat) float64 {
|
||||
return score
|
||||
}
|
||||
|
||||
func Loss2Score(loss uint32, reducedQuality bool) float64 {
|
||||
func loss2Score(loss uint32, reducedQuality bool) float64 {
|
||||
// No Loss, excellent
|
||||
if loss == 0 && !reducedQuality {
|
||||
return 5
|
||||
@@ -78,9 +72,10 @@ func Loss2Score(loss uint32, reducedQuality bool) float64 {
|
||||
return score
|
||||
}
|
||||
|
||||
func ConnectionScore(cur, prev *ConnectionStat, kind livekit.TrackType) float64 {
|
||||
if kind == livekit.TrackType_AUDIO {
|
||||
return mosAudioEmodel(cur, prev)
|
||||
}
|
||||
return 0
|
||||
func AudioConnectionScore(delta ConnectionStat, jitter uint32) float64 {
|
||||
return mosAudioEmodel(delta, jitter)
|
||||
}
|
||||
|
||||
func VideoConnectionScore(pctLoss uint32, reducedQuality bool) float64 {
|
||||
return loss2Score(pctLoss, reducedQuality)
|
||||
}
|
||||
|
||||
+69
-38
@@ -66,7 +66,7 @@ var (
|
||||
type ReceiverReportListener func(dt *DownTrack, report *rtcp.ReceiverReport)
|
||||
|
||||
type PacketStats struct {
|
||||
octets uint32
|
||||
octets uint64
|
||||
packets uint32
|
||||
}
|
||||
|
||||
@@ -103,7 +103,6 @@ type DownTrack struct {
|
||||
primaryStats atomic.Value // contains *PacketStats
|
||||
rtxStats atomic.Value // contains *PacketStats
|
||||
paddingStats atomic.Value // contains *PacketStats
|
||||
statsLock sync.Mutex
|
||||
connectionStats *connectionquality.ConnectionStats
|
||||
done chan struct{}
|
||||
|
||||
@@ -133,6 +132,9 @@ type DownTrack struct {
|
||||
|
||||
// padding packet sent callback
|
||||
onPaddingSent []func(dt *DownTrack, size int)
|
||||
|
||||
// update stats
|
||||
onStatsUpdate func(dt *DownTrack, stat *livekit.AnalyticsStat)
|
||||
}
|
||||
|
||||
// NewDownTrack returns a DownTrack.
|
||||
@@ -552,6 +554,10 @@ func (d *DownTrack) OnPaddingSent(fn func(dt *DownTrack, size int)) {
|
||||
d.onPaddingSent = append(d.onPaddingSent, fn)
|
||||
}
|
||||
|
||||
func (d *DownTrack) OnStatsUpdate(fn func(dt *DownTrack, stat *livekit.AnalyticsStat)) {
|
||||
d.onStatsUpdate = fn
|
||||
}
|
||||
|
||||
func (d *DownTrack) IsDeficient() bool {
|
||||
return d.forwarder.IsDeficient()
|
||||
}
|
||||
@@ -641,19 +647,20 @@ func (d *DownTrack) CreateSenderReport() *rtcp.SenderReport {
|
||||
|
||||
diff := (uint64(now.Sub(ntpTime(srNTP).Time())) * uint64(d.codec.ClockRate)) / uint64(time.Second)
|
||||
octets, packets := d.getSRStats()
|
||||
|
||||
return &rtcp.SenderReport{
|
||||
SSRC: d.ssrc,
|
||||
NTPTime: uint64(nowNTP),
|
||||
RTPTime: srRTP + uint32(diff),
|
||||
PacketCount: packets,
|
||||
OctetCount: octets,
|
||||
OctetCount: uint32(octets),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DownTrack) UpdatePrimaryStats(packetLen uint32) {
|
||||
primaryStats, _ := d.primaryStats.Load().(*PacketStats)
|
||||
|
||||
primaryStats.octets += packetLen
|
||||
primaryStats.octets += uint64(packetLen)
|
||||
primaryStats.packets += 1
|
||||
|
||||
d.primaryStats.Store(primaryStats)
|
||||
@@ -662,7 +669,7 @@ func (d *DownTrack) UpdatePrimaryStats(packetLen uint32) {
|
||||
func (d *DownTrack) UpdateRtxStats(packetLen uint32) {
|
||||
rtxStats, _ := d.rtxStats.Load().(*PacketStats)
|
||||
|
||||
rtxStats.octets += packetLen
|
||||
rtxStats.octets += uint64(packetLen)
|
||||
rtxStats.packets += 1
|
||||
|
||||
d.rtxStats.Store(rtxStats)
|
||||
@@ -671,7 +678,7 @@ func (d *DownTrack) UpdateRtxStats(packetLen uint32) {
|
||||
func (d *DownTrack) UpdatePaddingStats(packetLen uint32) {
|
||||
paddingStats, _ := d.paddingStats.Load().(*PacketStats)
|
||||
|
||||
paddingStats.octets += packetLen
|
||||
paddingStats.octets += uint64(packetLen)
|
||||
paddingStats.packets += 1
|
||||
|
||||
d.paddingStats.Store(paddingStats)
|
||||
@@ -807,14 +814,21 @@ func (d *DownTrack) handleRTCP(bytes []byte) {
|
||||
var jitter uint32
|
||||
var totalLost uint32
|
||||
var maxSeqNum uint32
|
||||
var nackCount int32
|
||||
var pliCount int32
|
||||
var firCount int32
|
||||
|
||||
maxRatePacketLoss := uint8(0)
|
||||
for _, pkt := range pkts {
|
||||
switch p := pkt.(type) {
|
||||
case *rtcp.PictureLossIndication:
|
||||
sendPliOnce()
|
||||
pliCount++
|
||||
hasReport = true
|
||||
case *rtcp.FullIntraRequest:
|
||||
sendPliOnce()
|
||||
firCount++
|
||||
hasReport = true
|
||||
case *rtcp.ReceiverEstimatedMaximumBitrate:
|
||||
if d.onREMB != nil {
|
||||
d.onREMB(d, p)
|
||||
@@ -854,34 +868,40 @@ func (d *DownTrack) handleRTCP(bytes []byte) {
|
||||
}
|
||||
d.listenerLock.RUnlock()
|
||||
}
|
||||
if hasReport {
|
||||
d.statsLock.Lock()
|
||||
// update feedback stats
|
||||
current := d.connectionStats.Curr
|
||||
if jitter > current.Jitter {
|
||||
current.Jitter = jitter
|
||||
}
|
||||
if delay > current.Delay {
|
||||
current.Delay = delay
|
||||
}
|
||||
if maxSeqNum > current.LastSeqNum {
|
||||
current.LastSeqNum = maxSeqNum
|
||||
}
|
||||
current.PacketsLost = totalLost
|
||||
d.statsLock.Unlock()
|
||||
}
|
||||
|
||||
case *rtcp.TransportLayerNack:
|
||||
var nackedPackets []packetMeta
|
||||
for _, pair := range p.Nacks {
|
||||
nackedPackets = append(nackedPackets, d.sequencer.getSeqNoPairs(pair.PacketList())...)
|
||||
}
|
||||
go d.retransmitPackets(nackedPackets)
|
||||
nackCount += int32(len(nackedPackets))
|
||||
hasReport = true
|
||||
case *rtcp.TransportLayerCC:
|
||||
if p.MediaSSRC == d.ssrc && d.onTransportCCFeedback != nil {
|
||||
d.onTransportCCFeedback(d, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
if hasReport {
|
||||
d.connectionStats.Lock.Lock()
|
||||
current := d.connectionStats
|
||||
// update feedback stats
|
||||
if jitter > current.Jitter {
|
||||
current.Jitter = jitter
|
||||
}
|
||||
if delay > current.Delay {
|
||||
current.Delay = delay
|
||||
}
|
||||
if maxSeqNum > current.LastSeqNum {
|
||||
current.LastSeqNum = maxSeqNum
|
||||
}
|
||||
current.PacketsLost = totalLost
|
||||
current.NackCount += nackCount
|
||||
current.PliCount += pliCount
|
||||
current.FirCount += firCount
|
||||
d.connectionStats.Lock.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DownTrack) retransmitPackets(nackedPackets []packetMeta) {
|
||||
@@ -959,7 +979,7 @@ func (d *DownTrack) retransmitPackets(nackedPackets []packetMeta) {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DownTrack) getSRStats() (uint32, uint32) {
|
||||
func (d *DownTrack) getSRStats() (uint64, uint32) {
|
||||
primary := d.primaryStats.Load().(*PacketStats)
|
||||
rtx := d.rtxStats.Load().(*PacketStats)
|
||||
padding := d.paddingStats.Load().(*PacketStats)
|
||||
@@ -1061,8 +1081,8 @@ func (d *DownTrack) DebugInfo() map[string]interface{} {
|
||||
}
|
||||
|
||||
func (d *DownTrack) GetConnectionScore() float64 {
|
||||
d.statsLock.Lock()
|
||||
defer d.statsLock.Unlock()
|
||||
d.connectionStats.Lock.Lock()
|
||||
defer d.connectionStats.Lock.Unlock()
|
||||
return d.connectionStats.Score
|
||||
}
|
||||
|
||||
@@ -1072,13 +1092,33 @@ func (d *DownTrack) updateStats() {
|
||||
case <-d.done:
|
||||
return
|
||||
case <-time.After(connectionQualityUpdateInterval):
|
||||
d.statsLock.Lock()
|
||||
d.connectionStats.Lock.Lock()
|
||||
totalBytes, _ := d.getSRStats()
|
||||
delta := d.connectionStats.UpdateStats(totalBytes)
|
||||
if d.Kind() == webrtc.RTPCodecTypeAudio {
|
||||
d.connectionStats.CalculateAudioScore()
|
||||
d.connectionStats.Score = connectionquality.AudioConnectionScore(delta, d.connectionStats.Jitter)
|
||||
} else {
|
||||
d.calculateVideoScore()
|
||||
var reducedQuality bool
|
||||
if d.GetForwardingStatus() != ForwardingStatusOptimal {
|
||||
reducedQuality = true
|
||||
}
|
||||
d.connectionStats.Score = connectionquality.VideoConnectionScore(FixedPointToPercent(d.CurrentMaxLossFraction()), reducedQuality)
|
||||
}
|
||||
d.statsLock.Unlock()
|
||||
stat := &livekit.AnalyticsStat{
|
||||
Jitter: float64(d.connectionStats.Jitter),
|
||||
TotalPackets: uint64(d.connectionStats.TotalPackets),
|
||||
PacketLost: uint64(d.connectionStats.PacketsLost),
|
||||
Delay: uint64(d.connectionStats.Delay),
|
||||
TotalBytes: d.connectionStats.TotalBytes,
|
||||
NackCount: d.connectionStats.NackCount,
|
||||
PliCount: d.connectionStats.PliCount,
|
||||
FirCount: d.connectionStats.FirCount,
|
||||
ConnectionScore: float32(d.connectionStats.Score),
|
||||
}
|
||||
if d.onStatsUpdate != nil {
|
||||
d.onStatsUpdate(d, stat)
|
||||
}
|
||||
d.connectionStats.Lock.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1087,12 +1127,3 @@ func (d *DownTrack) updateStats() {
|
||||
func FixedPointToPercent(frac uint8) uint32 {
|
||||
return (uint32(frac) * 100) >> 8
|
||||
}
|
||||
|
||||
func (d *DownTrack) calculateVideoScore() {
|
||||
var reducedQuality bool
|
||||
if d.GetForwardingStatus() != ForwardingStatusOptimal {
|
||||
reducedQuality = true
|
||||
}
|
||||
d.connectionStats.Score = connectionquality.Loss2Score(FixedPointToPercent(d.CurrentMaxLossFraction()), reducedQuality)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user