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:
+51
-24
@@ -87,7 +87,7 @@ func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTra
|
||||
Logger: params.Logger,
|
||||
})
|
||||
t.MediaTrackReceiver.OnMediaLossUpdate(func(fractionalLoss uint8) {
|
||||
if t.buffer != nil {
|
||||
if t.buffer != nil && t.Kind() == livekit.TrackType_AUDIO {
|
||||
// ok to access buffer since receivers are added before subscribers
|
||||
t.buffer.SetLastFractionLostReport(fractionalLoss)
|
||||
}
|
||||
@@ -108,8 +108,6 @@ func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTra
|
||||
|
||||
// on close signal via closing channel to workers
|
||||
t.AddOnClose(t.closeChan)
|
||||
go t.updateStats()
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
@@ -222,16 +220,15 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
|
||||
t.params.Telemetry.TrackUnpublished(context.Background(), t.PublisherID(), t.ToProto(), uint32(track.SSRC()))
|
||||
})
|
||||
t.params.Telemetry.TrackPublished(context.Background(), t.PublisherID(), t.ToProto())
|
||||
if t.Kind() == livekit.TrackType_AUDIO {
|
||||
t.buffer = buff
|
||||
}
|
||||
|
||||
t.buffer = buff
|
||||
|
||||
t.MediaTrackReceiver.SetupReceiver(wr)
|
||||
go t.updateStats()
|
||||
}
|
||||
t.lock.Unlock()
|
||||
|
||||
t.Receiver().(*sfu.WebRTCReceiver).AddUpTrack(track, buff)
|
||||
t.params.Telemetry.AddUpTrack(t.PublisherID(), t.ID(), buff)
|
||||
|
||||
atomic.AddUint32(&t.numUpTracks, 1)
|
||||
// LK-TODO: can remove this completely when VideoLayers protocol becomes the default as it has info from client or if we decide to use TrackInfo.Simulcast
|
||||
@@ -275,9 +272,9 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
|
||||
var jitter uint32
|
||||
var totalLost uint32
|
||||
var maxSeqNum uint32
|
||||
|
||||
// forward to telemetry
|
||||
t.params.Telemetry.HandleRTCP(livekit.StreamType_UPSTREAM, t.params.ParticipantID, t.ID(), packets)
|
||||
var nackCount int32
|
||||
var pliCount int32
|
||||
var firCount int32
|
||||
|
||||
for _, p := range packets {
|
||||
switch pkt := p.(type) {
|
||||
@@ -287,7 +284,6 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
|
||||
if rr.FractionLost > maxLost {
|
||||
maxLost = rr.FractionLost
|
||||
}
|
||||
|
||||
if rr.Delay > delay {
|
||||
delay = rr.Delay
|
||||
}
|
||||
@@ -297,11 +293,18 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
|
||||
if rr.LastSequenceNumber > maxSeqNum {
|
||||
maxSeqNum = rr.LastSequenceNumber
|
||||
}
|
||||
|
||||
totalLost = rr.TotalLost
|
||||
|
||||
hasReport = true
|
||||
}
|
||||
hasReport = true
|
||||
case *rtcp.TransportLayerNack:
|
||||
nackCount++
|
||||
hasReport = true
|
||||
case *rtcp.PictureLossIndication:
|
||||
pliCount++
|
||||
hasReport = true
|
||||
case *rtcp.FullIntraRequest:
|
||||
firCount++
|
||||
hasReport = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,8 +320,11 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
|
||||
t.maxUpFracLost = 0
|
||||
t.maxUpFracLostTs = now
|
||||
}
|
||||
t.statsLock.Unlock()
|
||||
|
||||
// update feedback stats
|
||||
current := t.connectionStats.Curr
|
||||
t.connectionStats.Lock.Lock()
|
||||
current := t.connectionStats
|
||||
if jitter > current.Jitter {
|
||||
current.Jitter = jitter
|
||||
}
|
||||
@@ -329,7 +335,10 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
|
||||
current.LastSeqNum = maxSeqNum
|
||||
}
|
||||
current.PacketsLost = totalLost
|
||||
t.statsLock.Unlock()
|
||||
current.NackCount += nackCount
|
||||
current.PliCount += pliCount
|
||||
current.FirCount += firCount
|
||||
t.connectionStats.Lock.Unlock()
|
||||
}
|
||||
|
||||
// also look for sender reports
|
||||
@@ -338,8 +347,8 @@ func (t *MediaTrack) handlePublisherFeedback(packets []rtcp.Packet) {
|
||||
}
|
||||
|
||||
func (t *MediaTrack) GetConnectionScore() float64 {
|
||||
t.statsLock.Lock()
|
||||
defer t.statsLock.Unlock()
|
||||
t.connectionStats.Lock.Lock()
|
||||
defer t.connectionStats.Lock.Unlock()
|
||||
return t.connectionStats.Score
|
||||
}
|
||||
|
||||
@@ -348,23 +357,41 @@ func (t *MediaTrack) closeChan() {
|
||||
}
|
||||
|
||||
func (t *MediaTrack) updateStats() {
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-t.done:
|
||||
return
|
||||
case <-time.After(connectionQualityUpdateInterval):
|
||||
t.statsLock.Lock()
|
||||
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.CalculateAudioScore()
|
||||
t.connectionStats.Score = connectionquality.AudioConnectionScore(delta, t.connectionStats.Jitter)
|
||||
} else {
|
||||
t.calculateVideoScore()
|
||||
t.connectionStats.Score = t.calculateVideoScore()
|
||||
}
|
||||
t.statsLock.Unlock()
|
||||
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() {
|
||||
func (t *MediaTrack) calculateVideoScore() float64 {
|
||||
var reducedQuality bool
|
||||
publishing, expected := t.getNumUpTracks()
|
||||
if publishing < expected {
|
||||
@@ -375,5 +402,5 @@ func (t *MediaTrack) calculateVideoScore() {
|
||||
if expected == 0 {
|
||||
loss = 0
|
||||
}
|
||||
t.connectionStats.Score = connectionquality.Loss2Score(loss, reducedQuality)
|
||||
return connectionquality.VideoConnectionScore(loss, reducedQuality)
|
||||
}
|
||||
|
||||
@@ -185,14 +185,9 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, code
|
||||
go t.sendDownTrackBindingReports(sub)
|
||||
})
|
||||
trackID := t.params.MediaTrack.ID()
|
||||
downTrack.OnPacketSent(func(_ *sfu.DownTrack, size int) {
|
||||
t.params.Telemetry.OnDownstreamPacket(subscriberID, trackID, size)
|
||||
})
|
||||
downTrack.OnPaddingSent(func(_ *sfu.DownTrack, size int) {
|
||||
t.params.Telemetry.OnDownstreamPacket(subscriberID, trackID, size)
|
||||
})
|
||||
downTrack.OnRTCP(func(pkts []rtcp.Packet) {
|
||||
t.params.Telemetry.HandleRTCP(livekit.StreamType_DOWNSTREAM, subscriberID, trackID, pkts)
|
||||
|
||||
downTrack.OnStatsUpdate(func(_ *sfu.DownTrack, stat *livekit.AnalyticsStat) {
|
||||
t.params.Telemetry.TrackStats(livekit.StreamType_DOWNSTREAM, subscriberID, trackID, stat)
|
||||
})
|
||||
|
||||
downTrack.OnCloseHandler(func() {
|
||||
|
||||
@@ -250,7 +250,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
if numRegistered > 0 && numPublishing != numRegistered {
|
||||
reducedQuality = true
|
||||
}
|
||||
return connectionquality.Loss2Score(loss, reducedQuality)
|
||||
return connectionquality.VideoConnectionScore(loss, reducedQuality)
|
||||
}
|
||||
|
||||
testPublishedVideoTrack := func(loss, numPublishing, numRegistered uint32) *typesfakes.FakeLocalMediaTrack {
|
||||
@@ -264,16 +264,20 @@ func TestConnectionQuality(t *testing.T) {
|
||||
testPublishedAudioTrack := func(totalPackets, packetsLost uint32) *typesfakes.FakeLocalMediaTrack {
|
||||
tr := &typesfakes.FakeLocalMediaTrack{}
|
||||
|
||||
stat := &connectionquality.ConnectionStat{
|
||||
stat := connectionquality.ConnectionStat{
|
||||
PacketsLost: packetsLost,
|
||||
TotalPackets: totalPackets,
|
||||
LastSeqNum: 0,
|
||||
}
|
||||
stats := &connectionquality.ConnectionStats{
|
||||
Curr: stat,
|
||||
Prev: &connectionquality.ConnectionStat{},
|
||||
ConnectionStat: stat,
|
||||
Prev: &connectionquality.ConnectionStat{},
|
||||
}
|
||||
stats.CalculateAudioScore()
|
||||
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)
|
||||
return tr
|
||||
|
||||
Reference in New Issue
Block a user