Connection quality v1 (#260)
* audio connection quality mos for publisher stats Signed-off-by: shishir gowda <shishir@livekit.io> * Update tests Signed-off-by: shishir gowda <shishir@livekit.io> * Change ratings range, increase default rtt to 80 Signed-off-by: shishir gowda <shishir@livekit.io> * Use stats worker to get total packets to find %lost in window Signed-off-by: shishir gowda <shishir@livekit.io> * Update go dep Signed-off-by: shishir gowda <shishir@livekit.io> * Increase interval of score cal to 5 seconds Signed-off-by: shishir gowda <shishir@livekit.io> * use lastSequenceNumber in reports to find total packets Signed-off-by: shishir gowda <shishir@livekit.io> * Account for delay while calculating scores Signed-off-by: shishir gowda <shishir@livekit.io> * Fix minor typo Signed-off-by: shishir gowda <shishir@livekit.io> * Add connection stats/score to subscribed audio tracks Signed-off-by: shishir gowda <shishir@livekit.io> * Cleanup Signed-off-by: shishir gowda <shishir@livekit.io> * Ignore duplicate LastSequenceNumbers in rtcp reports Ignore if sequence number is less than what was recieved Signed-off-by: shishir gowda <shishir@livekit.io> * Move video track score calc to media/downtracks Signed-off-by: shishir gowda <shishir@livekit.io> * Deprecate SubscribeLossPercentage() as score calc is now handled downstream Signed-off-by: shishir gowda <shishir@livekit.io> * Initialize connection score to excellent score is calc at 5sec interval. Client fetches score before first score is computed * Update test cases for connection quality Signed-off-by: shishir gowda <shishir@livekit.io>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package connectionquality
|
||||
|
||||
import (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
type ConnectionStat struct {
|
||||
PacketsLost uint32
|
||||
Delay uint32
|
||||
Jitter uint32
|
||||
TotalPackets uint32
|
||||
LastSeqNum uint32
|
||||
}
|
||||
|
||||
type ConnectionStats struct {
|
||||
Curr *ConnectionStat
|
||||
Prev *ConnectionStat
|
||||
Score float64
|
||||
}
|
||||
|
||||
func NewConnectionStats() *ConnectionStats {
|
||||
return &ConnectionStats{Curr: &ConnectionStat{}, Prev: &ConnectionStat{}, Score: 4.0}
|
||||
}
|
||||
|
||||
func getTotalPackets(curSN, prevSN uint32) uint32 {
|
||||
delta := curSN - prevSN
|
||||
// lower 16 bits is the counter
|
||||
counter := uint16(delta)
|
||||
// upper 16 bits contains the cycles/wrap
|
||||
cycles := uint16(delta >> 16)
|
||||
increment := (uint32(cycles) * (1 << 16)) + uint32(counter)
|
||||
|
||||
return increment
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) CalculateAudioScore() float64 {
|
||||
// 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)
|
||||
|
||||
// store previous stats
|
||||
cs.Prev = current
|
||||
cs.Curr = &ConnectionStat{TotalPackets: cs.Prev.TotalPackets, PacketsLost: cs.Prev.PacketsLost}
|
||||
|
||||
return cs.Score
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package connectionquality
|
||||
|
||||
import (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
// MOS score calculation is based on webrtc-stats
|
||||
// available @ https://github.com/oanguenot/webrtc-stats
|
||||
|
||||
const (
|
||||
rtt = 70
|
||||
)
|
||||
|
||||
func Score2Rating(score float64) livekit.ConnectionQuality {
|
||||
if score > 3.9 {
|
||||
return livekit.ConnectionQuality_EXCELLENT
|
||||
}
|
||||
|
||||
if score > 2.5 {
|
||||
return livekit.ConnectionQuality_GOOD
|
||||
}
|
||||
return livekit.ConnectionQuality_POOR
|
||||
}
|
||||
|
||||
func mosAudioEmodel(cur, prev *ConnectionStat) float64 {
|
||||
|
||||
if cur == nil {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
// find percentage of lost packets in this window
|
||||
deltaTotalPackets := cur.TotalPackets - prev.TotalPackets
|
||||
if deltaTotalPackets == 0 {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
deltaTotalLostPackets := cur.PacketsLost - prev.PacketsLost
|
||||
percentageLost := (float64(deltaTotalLostPackets) / float64(deltaTotalPackets)) * 100
|
||||
|
||||
rx := 93.2 - percentageLost
|
||||
ry := 0.18*rx*rx - 27.9*rx + 1126.62
|
||||
|
||||
d := float64(rtt + cur.Jitter)
|
||||
h := d - 177.3
|
||||
if h < 0 {
|
||||
h = 0
|
||||
} else {
|
||||
h = 1
|
||||
}
|
||||
id := 0.024*d + 0.11*(d-177.3)*h
|
||||
r := ry - (id)
|
||||
if r < 0 {
|
||||
return 1
|
||||
}
|
||||
if r > 100 {
|
||||
return 4.5
|
||||
}
|
||||
score := 1 + (0.035 * r) + (7.0/1000000)*r*(r-60)*(100-r)
|
||||
|
||||
return score
|
||||
}
|
||||
|
||||
func Loss2Score(loss uint32, reducedQuality bool) float64 {
|
||||
// No Loss, excellent
|
||||
if loss == 0 && !reducedQuality {
|
||||
return 5
|
||||
}
|
||||
score := 3.0
|
||||
// loss is bad
|
||||
if loss >= 4 {
|
||||
score = 2.0
|
||||
} else if loss <= 2 && !reducedQuality {
|
||||
// loss is acceptable and at reduced quality
|
||||
score = 4.5
|
||||
}
|
||||
return score
|
||||
}
|
||||
|
||||
func ConnectionScore(cur, prev *ConnectionStat, kind livekit.TrackType) float64 {
|
||||
if kind == livekit.TrackType_AUDIO {
|
||||
return mosAudioEmodel(cur, prev)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
+96
-12
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -19,6 +20,10 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
)
|
||||
|
||||
const (
|
||||
connectionQualityUpdateInterval = 5 * time.Second
|
||||
)
|
||||
|
||||
// TrackSender defines an interface send media to remote peer
|
||||
type TrackSender interface {
|
||||
UptrackLayersChange(availableLayers []uint16)
|
||||
@@ -94,9 +99,12 @@ type DownTrack struct {
|
||||
closeOnce sync.Once
|
||||
|
||||
// Report helpers
|
||||
primaryStats atomic.Value // contains *PacketStats
|
||||
rtxStats atomic.Value // contains *PacketStats
|
||||
paddingStats atomic.Value // contains *PacketStats
|
||||
primaryStats atomic.Value // contains *PacketStats
|
||||
rtxStats atomic.Value // contains *PacketStats
|
||||
paddingStats atomic.Value // contains *PacketStats
|
||||
statsLock sync.Mutex
|
||||
connectionStats *connectionquality.ConnectionStats
|
||||
done chan struct{}
|
||||
|
||||
lossFraction atomicUint8
|
||||
|
||||
@@ -138,21 +146,25 @@ func NewDownTrack(c webrtc.RTPCodecCapability, r TrackReceiver, bf *buffer.Facto
|
||||
}
|
||||
|
||||
d := &DownTrack{
|
||||
id: r.TrackID(),
|
||||
peerID: peerID,
|
||||
maxTrack: mt,
|
||||
streamID: r.StreamID(),
|
||||
bufferFactory: bf,
|
||||
receiver: r,
|
||||
codec: c,
|
||||
kind: kind,
|
||||
forwarder: NewForwarder(c, kind),
|
||||
id: r.TrackID(),
|
||||
peerID: peerID,
|
||||
maxTrack: mt,
|
||||
streamID: r.StreamID(),
|
||||
bufferFactory: bf,
|
||||
receiver: r,
|
||||
codec: c,
|
||||
kind: kind,
|
||||
forwarder: NewForwarder(c, kind),
|
||||
connectionStats: connectionquality.NewConnectionStats(),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
|
||||
d.primaryStats.Store(new(PacketStats))
|
||||
d.rtxStats.Store(new(PacketStats))
|
||||
d.paddingStats.Store(new(PacketStats))
|
||||
|
||||
go d.updateStats()
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
@@ -426,6 +438,7 @@ func (d *DownTrack) Close() {
|
||||
if d.onCloseHandler != nil {
|
||||
d.onCloseHandler()
|
||||
}
|
||||
close(d.done)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -764,6 +777,12 @@ func (d *DownTrack) handleRTCP(bytes []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
var hasReport bool
|
||||
var delay uint32
|
||||
var jitter uint32
|
||||
var totalLost uint32
|
||||
var maxSeqNum uint32
|
||||
|
||||
maxRatePacketLoss := uint8(0)
|
||||
for _, pkt := range pkts {
|
||||
switch p := pkt.(type) {
|
||||
@@ -789,6 +808,18 @@ func (d *DownTrack) handleRTCP(bytes []byte) {
|
||||
if maxRatePacketLoss == 0 || maxRatePacketLoss < r.FractionLost {
|
||||
maxRatePacketLoss = r.FractionLost
|
||||
}
|
||||
if r.Delay > delay {
|
||||
delay = r.Delay
|
||||
}
|
||||
if r.Jitter > jitter {
|
||||
jitter = r.Jitter
|
||||
}
|
||||
if r.LastSequenceNumber > maxSeqNum {
|
||||
maxSeqNum = r.LastSequenceNumber
|
||||
}
|
||||
totalLost = r.TotalLost
|
||||
|
||||
hasReport = true
|
||||
}
|
||||
d.lossFraction.set(maxRatePacketLoss)
|
||||
if len(rr.Reports) > 0 {
|
||||
@@ -798,6 +829,22 @@ 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 {
|
||||
@@ -983,3 +1030,40 @@ func (d *DownTrack) DebugInfo() map[string]interface{} {
|
||||
"Stats": stats,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DownTrack) GetConnectionScore() float64 {
|
||||
d.statsLock.Lock()
|
||||
defer d.statsLock.Unlock()
|
||||
return d.connectionStats.Score
|
||||
}
|
||||
|
||||
func (d *DownTrack) updateStats() {
|
||||
for {
|
||||
select {
|
||||
case <-d.done:
|
||||
return
|
||||
case <-time.After(connectionQualityUpdateInterval):
|
||||
d.statsLock.Lock()
|
||||
if d.Kind() == webrtc.RTPCodecTypeAudio {
|
||||
d.connectionStats.CalculateAudioScore()
|
||||
} else {
|
||||
d.calculateVideoScore()
|
||||
}
|
||||
d.statsLock.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// converts a fixed point number to the number part of %
|
||||
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