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:
shishirng
2021-12-20 07:54:14 -05:00
committed by GitHub
parent b0454b00a2
commit 0f728b0b72
13 changed files with 530 additions and 142 deletions
+85 -8
View File
@@ -3,6 +3,7 @@ package rtc
import (
"context"
"errors"
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
"sort"
"sync"
"sync/atomic"
@@ -31,8 +32,9 @@ var (
)
const (
lostUpdateDelta = time.Second
layerSelectionTolerance = 0.8
lostUpdateDelta = time.Second
connectionQualityUpdateInterval = 5 * time.Second
layerSelectionTolerance = 0.8
)
// MediaTrack represents a WebRTC track that needs to be forwarded
@@ -58,13 +60,15 @@ type MediaTrack struct {
layerDimensions sync.Map // quality => *livekit.VideoLayer
// track audio fraction lost
fracLostLock sync.Mutex
statsLock sync.Mutex
maxDownFracLost uint8
maxDownFracLostTs time.Time
currentUpFracLost uint32
maxUpFracLost uint8
maxUpFracLostTs time.Time
connectionStats *connectionquality.ConnectionStats
done chan struct{}
// quality level enable/disable
maxQualityLock sync.Mutex
maxSubscriberQuality map[string]livekit.VideoQuality
@@ -95,6 +99,8 @@ func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTra
ssrc: track.SSRC(),
streamID: track.StreamID(),
codec: track.Codec(),
connectionStats: connectionquality.NewConnectionStats(),
done: make(chan struct{}),
maxSubscriberQuality: make(map[string]livekit.VideoQuality),
}
@@ -106,6 +112,10 @@ func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTra
t.UpdateVideoLayers(params.TrackInfo.Layers)
// LK-TODO: maybe use this or simulcast flag in TrackInfo to set simulcasted here
}
// on close signal via closing channel to workers
t.AddOnClose(t.closeChan)
go t.updateStats()
return t
}
@@ -576,6 +586,11 @@ func (t *MediaTrack) sendDownTrackBindingReports(sub types.Participant) {
func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
var maxLost uint8
var hasReport bool
var delay uint32
var jitter uint32
var totalLost uint32
var maxSeqNum uint32
for _, p := range packets {
switch pkt := p.(type) {
// sfu.Buffer generates ReceiverReports for the publisher
@@ -584,13 +599,26 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
if rr.FractionLost > maxLost {
maxLost = rr.FractionLost
}
if rr.Delay > delay {
delay = rr.Delay
}
if rr.Jitter > jitter {
jitter = rr.Jitter
}
if rr.LastSequenceNumber > maxSeqNum {
maxSeqNum = rr.LastSequenceNumber
}
totalLost = rr.TotalLost
hasReport = true
}
}
}
if hasReport {
t.fracLostLock.Lock()
t.statsLock.Lock()
if maxLost > t.maxUpFracLost {
t.maxUpFracLost = maxLost
}
@@ -601,7 +629,19 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
t.maxUpFracLost = 0
t.maxUpFracLostTs = now
}
t.fracLostLock.Unlock()
// update feedback stats
current := t.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
t.statsLock.Unlock()
}
// also look for sender reports
@@ -610,12 +650,13 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
}
// handles max loss for audio packets
func (t *MediaTrack) handleMaxLossFeedback(_ *sfu.DownTrack, report *rtcp.ReceiverReport) {
func (t *MediaTrack) handleMaxLossFeedback(_ *sfu.DownTrack,
report *rtcp.ReceiverReport) {
var (
shouldUpdate bool
maxLost uint8
)
t.fracLostLock.Lock()
t.statsLock.Lock()
for _, rr := range report.Reports {
if t.maxDownFracLost < rr.FractionLost {
t.maxDownFracLost = rr.FractionLost
@@ -629,7 +670,7 @@ func (t *MediaTrack) handleMaxLossFeedback(_ *sfu.DownTrack, report *rtcp.Receiv
t.maxDownFracLost = 0
t.maxDownFracLostTs = now
}
t.fracLostLock.Unlock()
t.statsLock.Unlock()
if shouldUpdate && t.buffer != nil {
// ok to access buffer since receivers are added before subscribers
@@ -671,6 +712,42 @@ func (t *MediaTrack) Receiver() sfu.TrackReceiver {
return t.receiver
}
func (t *MediaTrack) GetConnectionScore() float64 {
t.statsLock.Lock()
defer t.statsLock.Unlock()
return t.connectionStats.Score
}
func (t *MediaTrack) closeChan() {
close(t.done)
}
func (t *MediaTrack) updateStats() {
for {
select {
case <-t.done:
return
case <-time.After(connectionQualityUpdateInterval):
t.statsLock.Lock()
if t.Kind() == livekit.TrackType_AUDIO {
t.connectionStats.CalculateAudioScore()
} else {
t.calculateVideoScore()
}
t.statsLock.Unlock()
}
}
}
func (t *MediaTrack) calculateVideoScore() {
var reducedQuality bool
publishing, registered := t.NumUpTracks()
if registered > 0 && publishing != registered {
reducedQuality = true
}
t.connectionStats.Score = connectionquality.Loss2Score(t.PublishLossPercentage(), reducedQuality)
}
func (t *MediaTrack) OnSubscribedMaxQualityChange(f func(trackSid string, subscribedQualities []*livekit.SubscribedQuality) error) {
t.onSubscribedMaxQualityChange = f
}