Refactoring connection stats (#384)
This commit is contained in:
+29
-152
@@ -21,7 +21,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
upLostUpdateDelta = time.Second
|
||||
connectionQualityUpdateInterval = 5 * time.Second
|
||||
)
|
||||
|
||||
@@ -37,18 +36,12 @@ type MediaTrack struct {
|
||||
audioLevelMu sync.RWMutex
|
||||
audioLevel *AudioLevel
|
||||
|
||||
statsLock sync.Mutex
|
||||
currentUpFracLost uint32
|
||||
maxUpFracLost uint8
|
||||
maxUpFracLostTs time.Time
|
||||
connectionStats *connectionquality.ConnectionStats
|
||||
connectionStats *connectionquality.ConnectionStats
|
||||
|
||||
done chan struct{}
|
||||
|
||||
*MediaTrackReceiver
|
||||
|
||||
onMediaLossUpdate func(trackID livekit.TrackID, fractionalLoss uint32)
|
||||
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
@@ -70,9 +63,7 @@ type MediaTrackParams struct {
|
||||
|
||||
func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTrack {
|
||||
t := &MediaTrack{
|
||||
params: params,
|
||||
connectionStats: connectionquality.NewConnectionStats(),
|
||||
done: make(chan struct{}),
|
||||
params: params,
|
||||
}
|
||||
|
||||
t.MediaTrackReceiver = NewMediaTrackReceiver(MediaTrackReceiverParams{
|
||||
@@ -106,13 +97,30 @@ func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTra
|
||||
}
|
||||
})
|
||||
|
||||
// on close signal via closing channel to workers
|
||||
t.AddOnClose(t.closeChan)
|
||||
return t
|
||||
}
|
||||
t.connectionStats = connectionquality.NewConnectionStats(connectionquality.ConnectionStatsParams{
|
||||
UpdateInterval: connectionQualityUpdateInterval,
|
||||
CodecType: track.Kind(),
|
||||
GetTotalBytes: func() uint64 {
|
||||
receiver := t.Receiver()
|
||||
if receiver != nil {
|
||||
return receiver.(*sfu.WebRTCReceiver).GetTotalBytes()
|
||||
}
|
||||
|
||||
func (t *MediaTrack) OnMediaLossUpdate(f func(trackID livekit.TrackID, fractionalLoss uint32)) {
|
||||
t.onMediaLossUpdate = f
|
||||
return 0
|
||||
},
|
||||
GetIsReducedQuality: func() bool {
|
||||
publishing, expected := t.getNumUpTracks()
|
||||
return publishing < expected
|
||||
},
|
||||
})
|
||||
t.connectionStats.OnStatsUpdate(func(_cs *connectionquality.ConnectionStats, stat *livekit.AnalyticsStat) {
|
||||
t.params.Telemetry.TrackStats(livekit.StreamType_UPSTREAM, t.PublisherID(), t.ID(), stat)
|
||||
})
|
||||
|
||||
t.AddOnClose(func() {
|
||||
t.connectionStats.Close()
|
||||
})
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *MediaTrack) SignalCid() string {
|
||||
@@ -138,10 +146,6 @@ func (t *MediaTrack) ToProto() *livekit.TrackInfo {
|
||||
return info
|
||||
}
|
||||
|
||||
func (t *MediaTrack) publishLossPercentage() uint32 {
|
||||
return FixedPointToPercent(uint8(atomic.LoadUint32(&t.currentUpFracLost)))
|
||||
}
|
||||
|
||||
func (t *MediaTrack) getNumUpTracks() (uint32, uint32) {
|
||||
numExpected := atomic.LoadUint32(&t.numUpTracks)
|
||||
|
||||
@@ -224,7 +228,8 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
|
||||
t.buffer = buff
|
||||
|
||||
t.MediaTrackReceiver.SetupReceiver(wr)
|
||||
go t.updateStats()
|
||||
|
||||
t.connectionStats.Start()
|
||||
}
|
||||
t.lock.Unlock()
|
||||
|
||||
@@ -266,80 +271,7 @@ func (t *MediaTrack) GetAudioLevel() (level uint8, active bool) {
|
||||
}
|
||||
|
||||
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
|
||||
var nackCount int32
|
||||
var pliCount int32
|
||||
var firCount int32
|
||||
|
||||
for _, p := range packets {
|
||||
switch pkt := p.(type) {
|
||||
// sfu.Buffer generates ReceiverReports for the publisher
|
||||
case *rtcp.ReceiverReport:
|
||||
for _, rr := range pkt.Reports {
|
||||
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
|
||||
case *rtcp.TransportLayerNack:
|
||||
nackCount++
|
||||
hasReport = true
|
||||
case *rtcp.PictureLossIndication:
|
||||
pliCount++
|
||||
hasReport = true
|
||||
case *rtcp.FullIntraRequest:
|
||||
firCount++
|
||||
hasReport = true
|
||||
}
|
||||
}
|
||||
|
||||
if hasReport {
|
||||
t.statsLock.Lock()
|
||||
if maxLost > t.maxUpFracLost {
|
||||
t.maxUpFracLost = maxLost
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if now.Sub(t.maxUpFracLostTs) > upLostUpdateDelta {
|
||||
atomic.StoreUint32(&t.currentUpFracLost, uint32(t.maxUpFracLost))
|
||||
t.maxUpFracLost = 0
|
||||
t.maxUpFracLostTs = now
|
||||
}
|
||||
t.statsLock.Unlock()
|
||||
|
||||
// update feedback stats
|
||||
t.connectionStats.Lock.Lock()
|
||||
current := t.connectionStats
|
||||
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
|
||||
t.connectionStats.Lock.Unlock()
|
||||
}
|
||||
t.connectionStats.RTCPFeedback(packets, 0)
|
||||
|
||||
// also look for sender reports
|
||||
// feedback for the source RTCP
|
||||
@@ -347,60 +279,5 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
|
||||
}
|
||||
|
||||
func (t *MediaTrack) GetConnectionScore() float64 {
|
||||
t.connectionStats.Lock.Lock()
|
||||
defer t.connectionStats.Lock.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.connectionStats.Lock.Lock()
|
||||
|
||||
//we are accessing stats after receiver has buffer assigned
|
||||
stats := t.buffer.GetStats()
|
||||
|
||||
delta := t.connectionStats.UpdateStats(stats.TotalByte)
|
||||
if t.Kind() == livekit.TrackType_AUDIO {
|
||||
t.connectionStats.Score = connectionquality.AudioConnectionScore(delta, t.connectionStats.Jitter)
|
||||
} else {
|
||||
t.connectionStats.Score = t.calculateVideoScore()
|
||||
}
|
||||
stat := &livekit.AnalyticsStat{
|
||||
Jitter: float64(t.connectionStats.Jitter),
|
||||
TotalPackets: uint64(t.connectionStats.TotalPackets),
|
||||
PacketLost: uint64(t.connectionStats.PacketsLost),
|
||||
Delay: uint64(t.connectionStats.Delay),
|
||||
TotalBytes: t.connectionStats.TotalBytes,
|
||||
NackCount: t.connectionStats.NackCount,
|
||||
PliCount: t.connectionStats.PliCount,
|
||||
FirCount: t.connectionStats.FirCount,
|
||||
ConnectionScore: float32(t.connectionStats.Score),
|
||||
}
|
||||
t.params.Telemetry.TrackStats(livekit.StreamType_UPSTREAM, t.PublisherID(), t.ID(), stat)
|
||||
t.connectionStats.Lock.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *MediaTrack) calculateVideoScore() float64 {
|
||||
var reducedQuality bool
|
||||
publishing, expected := t.getNumUpTracks()
|
||||
if publishing < expected {
|
||||
reducedQuality = true
|
||||
}
|
||||
|
||||
loss := t.publishLossPercentage()
|
||||
if expected == 0 {
|
||||
loss = 0
|
||||
}
|
||||
return connectionquality.VideoConnectionScore(loss, reducedQuality)
|
||||
return t.connectionStats.GetScore()
|
||||
}
|
||||
|
||||
@@ -734,21 +734,21 @@ func (p *ParticipantImpl) GetAudioLevel() (level uint8, active bool) {
|
||||
|
||||
func (p *ParticipantImpl) GetConnectionQuality() *livekit.ConnectionQualityInfo {
|
||||
// avg loss across all tracks, weigh published the same as subscribed
|
||||
scores, numTracks := p.getPublisherConnectionQuality()
|
||||
totalScore, numTracks := p.getPublisherConnectionQuality()
|
||||
|
||||
p.lock.RLock()
|
||||
for _, subTrack := range p.subscribedTracks {
|
||||
if subTrack.IsMuted() || subTrack.MediaTrack().IsMuted() {
|
||||
continue
|
||||
}
|
||||
scores += subTrack.DownTrack().GetConnectionScore()
|
||||
totalScore += subTrack.DownTrack().GetConnectionScore()
|
||||
numTracks++
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
|
||||
avgScore := 5.0
|
||||
if numTracks > 0 {
|
||||
avgScore = scores / float64(numTracks)
|
||||
avgScore = totalScore / float64(numTracks)
|
||||
}
|
||||
|
||||
rating := connectionquality.Score2Rating(avgScore)
|
||||
@@ -1332,12 +1332,12 @@ func (p *ParticipantImpl) setTrackMuted(trackID livekit.TrackID, muted bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) getPublisherConnectionQuality() (scores float64, numTracks int) {
|
||||
func (p *ParticipantImpl) getPublisherConnectionQuality() (totalScore float64, numTracks int) {
|
||||
for _, pt := range p.GetPublishedTracks() {
|
||||
if pt.IsMuted() {
|
||||
continue
|
||||
}
|
||||
scores += pt.(types.LocalMediaTrack).GetConnectionScore()
|
||||
totalScore += pt.(types.LocalMediaTrack).GetConnectionScore()
|
||||
numTracks++
|
||||
}
|
||||
|
||||
|
||||
@@ -245,7 +245,7 @@ func TestMuteSetting(t *testing.T) {
|
||||
func TestConnectionQuality(t *testing.T) {
|
||||
|
||||
// loss based score is currently a publisher method.
|
||||
videoScore := func(loss, numPublishing, numRegistered uint32) float64 {
|
||||
videoScore := func(loss float64, numPublishing, numRegistered uint32) float64 {
|
||||
var reducedQuality bool
|
||||
if numRegistered > 0 && numPublishing != numRegistered {
|
||||
reducedQuality = true
|
||||
@@ -253,7 +253,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
return connectionquality.VideoConnectionScore(loss, reducedQuality)
|
||||
}
|
||||
|
||||
testPublishedVideoTrack := func(loss, numPublishing, numRegistered uint32) *typesfakes.FakeLocalMediaTrack {
|
||||
testPublishedVideoTrack := func(loss float64, numPublishing, numRegistered uint32) *typesfakes.FakeLocalMediaTrack {
|
||||
tr := &typesfakes.FakeLocalMediaTrack{}
|
||||
score := videoScore(loss, numPublishing, numRegistered)
|
||||
t.Log("video score: ", score)
|
||||
@@ -264,22 +264,13 @@ func TestConnectionQuality(t *testing.T) {
|
||||
testPublishedAudioTrack := func(totalPackets, packetsLost uint32) *typesfakes.FakeLocalMediaTrack {
|
||||
tr := &typesfakes.FakeLocalMediaTrack{}
|
||||
|
||||
stat := connectionquality.ConnectionStat{
|
||||
PacketsLost: packetsLost,
|
||||
TotalPackets: totalPackets,
|
||||
LastSeqNum: 0,
|
||||
pctLoss := float64(0.0)
|
||||
if totalPackets > 0 {
|
||||
pctLoss = (float64(packetsLost) / float64(totalPackets)) * 100.0
|
||||
}
|
||||
stats := &connectionquality.ConnectionStats{
|
||||
ConnectionStat: stat,
|
||||
Prev: &connectionquality.ConnectionStat{},
|
||||
}
|
||||
stats.Score = connectionquality.AudioConnectionScore(connectionquality.ConnectionStat{
|
||||
TotalPackets: stat.TotalPackets,
|
||||
PacketsLost: stat.PacketsLost,
|
||||
TotalBytes: stat.TotalBytes,
|
||||
}, 0)
|
||||
t.Log("audio score: ", stats.Score)
|
||||
tr.GetConnectionScoreReturns(stats.Score)
|
||||
score := connectionquality.AudioConnectionScore(pctLoss, 0)
|
||||
t.Log("audio score: ", score)
|
||||
tr.GetConnectionScoreReturns(score)
|
||||
return tr
|
||||
}
|
||||
|
||||
|
||||
@@ -45,11 +45,6 @@ func UnpackDataTrackLabel(packed string) (peerID livekit.ParticipantID, trackID
|
||||
return
|
||||
}
|
||||
|
||||
// converts a fixed point number to the number part of %
|
||||
func FixedPointToPercent(frac uint8) uint32 {
|
||||
return (uint32(frac) * 100) >> 8
|
||||
}
|
||||
|
||||
func ToProtoParticipants(participants []types.LocalParticipant) []*livekit.ParticipantInfo {
|
||||
infos := make([]*livekit.ParticipantInfo, 0, len(participants))
|
||||
for _, op := range participants {
|
||||
|
||||
Reference in New Issue
Block a user