Monitor and log RTP time stmap drifts (#1710)

The PID controller seems to be working well. But, it is unclear where
it can be applied as some of the data shows significant jumps
(either caused by BT devices or possibly noise cancellation/cpu
constraint) and although PID controller is slowly pulling things
to expected sample rate, it could be a bit slow.
Unfortunately, cannot munge too much in a middle box.
However leaving the controller in there as it is doing its job
for cases where things slip slowly.

Changing things to log significant jumps (more than 200 ms away
from expected) at Infow level.

Also, recording drift and sample rate in RTP stats proto and string
representation.
This commit is contained in:
Raja Subramanian
2023-05-13 18:41:09 +05:30
committed by GitHub
parent a085afc6ee
commit 61102533ae
3 changed files with 103 additions and 75 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ require (
github.com/jxskiss/base62 v1.1.0 github.com/jxskiss/base62 v1.1.0
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1
github.com/livekit/mediatransportutil v0.0.0-20230511025422-058ebf6b48c9 github.com/livekit/mediatransportutil v0.0.0-20230511025422-058ebf6b48c9
github.com/livekit/protocol v1.5.7-0.20230510002113-cadccd54108e github.com/livekit/protocol v1.5.7-0.20230513090813-c5dc103838fd
github.com/livekit/psrpc v0.3.1-0.20230502152150-df9dd21fba11 github.com/livekit/psrpc v0.3.1-0.20230502152150-df9dd21fba11
github.com/mackerelio/go-osstat v0.2.4 github.com/mackerelio/go-osstat v0.2.4
github.com/magefile/mage v1.14.0 github.com/magefile/mage v1.14.0
+2 -2
View File
@@ -121,8 +121,8 @@ github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 h1:jm09419p0lqTkD
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
github.com/livekit/mediatransportutil v0.0.0-20230511025422-058ebf6b48c9 h1:aqivx5Tal2Fa6z1ZQrrBk/vYShosQx3ecl1aMwcQRV0= github.com/livekit/mediatransportutil v0.0.0-20230511025422-058ebf6b48c9 h1:aqivx5Tal2Fa6z1ZQrrBk/vYShosQx3ecl1aMwcQRV0=
github.com/livekit/mediatransportutil v0.0.0-20230511025422-058ebf6b48c9/go.mod h1:MRc0zSOSzXuFt0X218SgabzlaKevkvCckPgBEoHYc34= github.com/livekit/mediatransportutil v0.0.0-20230511025422-058ebf6b48c9/go.mod h1:MRc0zSOSzXuFt0X218SgabzlaKevkvCckPgBEoHYc34=
github.com/livekit/protocol v1.5.7-0.20230510002113-cadccd54108e h1:0F3RjOkUS71P2ODHI5ZW09Cbrr6A0Pg8vCFFy6gcqkk= github.com/livekit/protocol v1.5.7-0.20230513090813-c5dc103838fd h1:wK+Vp0Oa0oggHYKBymHRJFeDWFzrcMjmyuOw4TLzT7c=
github.com/livekit/protocol v1.5.7-0.20230510002113-cadccd54108e/go.mod h1:vjGsR1YxXnN5BLS0yr/YjGnJOPrS0ymddCF3JwxSHGM= github.com/livekit/protocol v1.5.7-0.20230513090813-c5dc103838fd/go.mod h1:vjGsR1YxXnN5BLS0yr/YjGnJOPrS0ymddCF3JwxSHGM=
github.com/livekit/psrpc v0.3.1-0.20230502152150-df9dd21fba11 h1:VS23iVQu/TNiLEM5XjbBSY28+B6nSewjKWPDbieg0Ho= github.com/livekit/psrpc v0.3.1-0.20230502152150-df9dd21fba11 h1:VS23iVQu/TNiLEM5XjbBSY28+B6nSewjKWPDbieg0Ho=
github.com/livekit/psrpc v0.3.1-0.20230502152150-df9dd21fba11/go.mod h1:n6JntEg+zT6Ji8InoyTpV7wusPNwGqqtxmHlkNhDN0U= github.com/livekit/psrpc v0.3.1-0.20230502152150-df9dd21fba11/go.mod h1:n6JntEg+zT6Ji8InoyTpV7wusPNwGqqtxmHlkNhDN0U=
github.com/mackerelio/go-osstat v0.2.4 h1:qxGbdPkFo65PXOb/F/nhDKpF2nGmGaCFDLXoZjJTtUs= github.com/mackerelio/go-osstat v0.2.4 h1:qxGbdPkFo65PXOb/F/nhDKpF2nGmGaCFDLXoZjJTtUs=
+90 -62
View File
@@ -3,6 +3,7 @@ package buffer
import ( import (
"errors" "errors"
"fmt" "fmt"
"math"
"sync" "sync"
"time" "time"
@@ -177,6 +178,7 @@ type RTPStats struct {
srData *RTCPSenderReportData srData *RTCPSenderReportData
lastSRTime time.Time lastSRTime time.Time
lastSRNTP mediatransportutil.NtpTime lastSRNTP mediatransportutil.NtpTime
lastSRRTP uint32
pidController *PIDController pidController *PIDController
nextSnapshotId uint32 nextSnapshotId uint32
@@ -286,6 +288,7 @@ func (r *RTPStats) Seed(from *RTPStats) {
} }
r.lastSRTime = from.lastSRTime r.lastSRTime = from.lastSRTime
r.lastSRNTP = from.lastSRNTP r.lastSRNTP = from.lastSRNTP
r.lastSRRTP = from.lastSRRTP
r.nextSnapshotId = from.nextSnapshotId r.nextSnapshotId = from.nextSnapshotId
for id, ss := range from.snapshots { for id, ss := range from.snapshots {
@@ -732,7 +735,7 @@ func (r *RTPStats) SetRtcpSenderReportData(srData *RTCPSenderReportData) {
// prevent against extreme case of anachronous sender reports // prevent against extreme case of anachronous sender reports
if r.srData != nil && r.srData.NTPTimestamp > srData.NTPTimestamp { if r.srData != nil && r.srData.NTPTimestamp > srData.NTPTimestamp {
r.logger.Debugw( r.logger.Infow(
"received anachronous sender report", "received anachronous sender report",
"current", srData.NTPTimestamp.Time(), "current", srData.NTPTimestamp.Time(),
"last", r.srData.NTPTimestamp.Time(), "last", r.srData.NTPTimestamp.Time(),
@@ -740,38 +743,38 @@ func (r *RTPStats) SetRtcpSenderReportData(srData *RTCPSenderReportData) {
return return
} }
// TODO-REMOVE-AFTER-DEBUG-START // monitor and log RTP timestamp anomalies
ntpTime := srData.NTPTimestamp.Time() isWarped := false
var ntpDiffSinceLast, arrivalDiffSinceLast time.Duration
var rtpDiffSinceLast uint32
if r.srData != nil { if r.srData != nil {
ntpDiffSinceLast = ntpTime.Sub(r.srData.NTPTimestamp.Time()) ntpDiffSinceLast := srData.NTPTimestamp.Time().Sub(r.srData.NTPTimestamp.Time())
rtpDiffSinceLast = srData.RTPTimestamp - r.srData.RTPTimestamp rtpDiffSinceLast := srData.RTPTimestamp - r.srData.RTPTimestamp
arrivalDiffSinceLast = srData.ArrivalTime.Sub(r.srData.ArrivalTime) arrivalDiffSinceLast := srData.ArrivalTime.Sub(r.srData.ArrivalTime)
expectedTimeDiffSinceLast := float64(rtpDiffSinceLast) / float64(r.params.ClockRate)
if math.Abs(expectedTimeDiffSinceLast-ntpDiffSinceLast.Seconds()) > 0.2 {
// more than 200 ms away from expected delta
isWarped = true
} }
timeSinceFirst := time.Since(r.firstTime) // ideally should use NTP time from SR, but that is a different time base, now is a resonable approximation if isWarped {
rtpDiffSinceFirst := getExtTS(srData.RTPTimestamp, r.tsCycles) - r.extStartTS timeSinceFirst, rtpDiffSinceFirst, drift, driftMs, sampleRate := r.getDrift()
drift := int64(rtpDiffSinceFirst - uint64(timeSinceFirst.Nanoseconds()*int64(r.params.ClockRate)/1e9)) r.logger.Infow(
driftMs := (float64(drift) * 1000) / float64(r.params.ClockRate) "received sender report, time warp",
"ntp", srData.NTPTimestamp.Time().String(),
r.logger.Debugw(
"received sender report",
"ntp", ntpTime,
"rtp", srData.RTPTimestamp, "rtp", srData.RTPTimestamp,
"arrival", srData.ArrivalTime, "arrival", srData.ArrivalTime.String(),
"ntpDiff", ntpDiffSinceLast, "ntpDiffSincelast", ntpDiffSinceLast.Seconds(),
"rtpDiff", rtpDiffSinceLast, "rtpDiffSincelast", rtpDiffSinceLast,
"arrivalDiff", arrivalDiffSinceLast, "arrivalDiffSincelast", arrivalDiffSinceLast.Seconds(),
"expectedTimeDiff", float64(rtpDiffSinceLast)/float64(r.params.ClockRate), "expectedTimeDiffSincelast", expectedTimeDiffSinceLast,
"timeSinceFirst", timeSinceFirst, "timeSinceFirst", timeSinceFirst.Seconds(),
"rtpDiffSinceFirst", rtpDiffSinceFirst, "rtpDiffSinceFirst", rtpDiffSinceFirst,
"drift", drift, "drift", drift,
"driftMs", driftMs, "driftMs", driftMs,
"rate", float64(rtpDiffSinceFirst)/timeSinceFirst.Seconds(), "sampleRate", sampleRate,
) )
// TODO-REMOVE-AFTER-DEBUG-END }
}
srDataCopy := *srData srDataCopy := *srData
r.srData = &srDataCopy r.srData = &srDataCopy
@@ -828,24 +831,9 @@ func (r *RTPStats) GetRtcpSenderReport(ssrc uint32) (*rtcp.SenderReport, float64
now := r.firstTime.Add(timeSinceFirst) now := r.firstTime.Add(timeSinceFirst)
nowNTP := mediatransportutil.ToNtpTime(now) nowNTP := mediatransportutil.ToNtpTime(now)
expectedExtRTP := r.extStartTS + uint64(timeSinceFirst.Nanoseconds()*int64(r.params.ClockRate)/1e9) timeSinceHighest := now.Sub(r.highestTime)
if getExtTS(r.highestTS, r.tsCycles) > expectedExtRTP || now.Before(r.highestTime) {
r.logger.Debugw(
"sending anachronous sender report",
"firstTime", r.firstTime.String(),
"currentTime", now.String(),
"highestTime", r.highestTime.String(),
"timeSinceFirst", timeSinceFirst,
"extStartTS", r.extStartTS,
"highestExtRTP", getExtTS(r.highestTS, r.tsCycles),
"expectedExtRTP", expectedExtRTP,
)
}
timeSinceHighest := time.Since(r.highestTime)
nowRTP := r.highestTS + uint32(timeSinceHighest.Nanoseconds()*int64(r.params.ClockRate)/1e9) nowRTP := r.highestTS + uint32(timeSinceHighest.Nanoseconds()*int64(r.params.ClockRate)/1e9)
// TODO-REMOVE-AFTER-DEBUG-START
rtpDiffSinceFirst := getExtTS(nowRTP, r.tsCycles) - r.extStartTS rtpDiffSinceFirst := getExtTS(nowRTP, r.tsCycles) - r.extStartTS
rate := float64(rtpDiffSinceFirst) / timeSinceFirst.Seconds() rate := float64(rtpDiffSinceFirst) / timeSinceFirst.Seconds()
pidOutput := r.pidController.Update( pidOutput := r.pidController.Update(
@@ -853,37 +841,51 @@ func (r *RTPStats) GetRtcpSenderReport(ssrc uint32) (*rtcp.SenderReport, float64
rate, rate,
now, now,
) )
// TODO-REMOVE-AFTER-DEBUG-STOP
// TODO-REMOVE-AFTER-DEBUG-START // monitor and log RTP timestamp anomalies
ntpTime := nowNTP.Time() isWarped := false
if r.lastSRNTP != 0 {
ntpDiffSinceLast := nowNTP.Time().Sub(r.lastSRNTP.Time())
rtpDiffSinceLast := nowRTP - r.lastSRRTP
departureDiffSinceLast := now.Sub(r.lastSRTime)
ntpDiffLocal := ntpTime.Sub(r.highestTime) expectedTimeDiffSinceLast := float64(rtpDiffSinceLast) / float64(r.params.ClockRate)
if math.Abs(expectedTimeDiffSinceLast-ntpDiffSinceLast.Seconds()) > 0.2 {
// more than 200 ms away from expected delta
isWarped = true
}
if isWarped {
expectedExtRTP := r.extStartTS + uint64(timeSinceFirst.Nanoseconds()*int64(r.params.ClockRate)/1e9)
ntpDiffLocal := nowNTP.Time().Sub(r.highestTime)
rtpDiffLocal := int32(nowRTP - r.highestTS) rtpDiffLocal := int32(nowRTP - r.highestTS)
rtpOffsetLocal := int32(nowRTP - r.highestTS - uint32(ntpDiffLocal.Nanoseconds()*int64(r.params.ClockRate)/1e9)) timeSinceFirst, rtpDiffSinceFirst, drift, driftMs, sampleRate := r.getDrift()
r.logger.Infow(
drift := int64(rtpDiffSinceFirst - uint64(timeSinceFirst.Nanoseconds()*int64(r.params.ClockRate)/1e9)) "sending sender report, time warp",
driftMs := (float64(drift) * 1000) / float64(r.params.ClockRate) "ntp", nowNTP.Time().String(),
r.logger.Debugw( "rtp", nowRTP,
"sending sender report", "expectedRTP", uint32(expectedExtRTP),
"highestTS", r.highestTS, "departure", now.String(),
"highestTime", r.highestTime.String(), "ntpDiffSincelast", ntpDiffSinceLast.Seconds(),
"reportTS", nowRTP, "rtpDiffSincelast", rtpDiffSinceLast,
"expectedTS", uint32(expectedExtRTP), "departureDiffSincelast", departureDiffSinceLast.Seconds(),
"reportTime", ntpTime.String(), "expectedTimeDiffSincelast", expectedTimeDiffSinceLast,
"rtpDiffLocal", rtpDiffLocal, "timeSinceFirst", timeSinceFirst.Seconds(),
"ntpDiffLocal", ntpDiffLocal,
"rtpOffsetLocal", rtpOffsetLocal,
"timeSinceFirst", timeSinceFirst,
"rtpDiffSinceFirst", rtpDiffSinceFirst, "rtpDiffSinceFirst", rtpDiffSinceFirst,
"drift", drift, "drift", drift,
"driftMs", driftMs, "driftMs", driftMs,
"rate", rate, "sampleRate", sampleRate,
"highestTS", r.highestTS,
"highestTime", r.highestTime.String(),
"rtpDiffLocal", rtpDiffLocal,
"ntpDiffLocal", ntpDiffLocal,
) )
// TODO-REMOVE-AFTER-DEBUG-END }
}
r.lastSRTime = now r.lastSRTime = now
r.lastSRNTP = nowNTP r.lastSRNTP = nowNTP
r.lastSRRTP = nowRTP
return &rtcp.SenderReport{ return &rtcp.SenderReport{
SSRC: ssrc, SSRC: ssrc,
@@ -1152,6 +1154,12 @@ func (r *RTPStats) ToString() string {
str += ", rtt(ms):" str += ", rtt(ms):"
str += fmt.Sprintf("%d|%d", p.RttCurrent, p.RttMax) str += fmt.Sprintf("%d|%d", p.RttCurrent, p.RttMax)
str += ", drift(ms):"
str += fmt.Sprintf("%.2f", p.DriftMs)
str += ", sr(Hz):"
str += fmt.Sprintf("%.2f", p.SampleRate)
return str return str
} }
@@ -1199,6 +1207,8 @@ func (r *RTPStats) ToProto() *livekit.RTPStats {
jitterTime := jitter / float64(r.params.ClockRate) * 1e6 jitterTime := jitter / float64(r.params.ClockRate) * 1e6
maxJitterTime := maxJitter / float64(r.params.ClockRate) * 1e6 maxJitterTime := maxJitter / float64(r.params.ClockRate) * 1e6
_, _, _, driftMs, sampleRate := r.getDrift()
p := &livekit.RTPStats{ p := &livekit.RTPStats{
StartTime: timestamppb.New(r.startTime), StartTime: timestamppb.New(r.startTime),
EndTime: timestamppb.New(endTime), EndTime: timestamppb.New(endTime),
@@ -1240,6 +1250,8 @@ func (r *RTPStats) ToProto() *livekit.RTPStats {
LastFir: timestamppb.New(r.lastFir), LastFir: timestamppb.New(r.lastFir),
RttCurrent: r.rtt, RttCurrent: r.rtt,
RttMax: r.maxRtt, RttMax: r.maxRtt,
DriftMs: driftMs,
SampleRate: sampleRate,
} }
gapsPresent := false gapsPresent := false
@@ -1433,6 +1445,15 @@ func (r *RTPStats) updateJitter(rtph *rtp.Header, packetTime time.Time) {
r.lastJitterRTP = rtph.Timestamp r.lastJitterRTP = rtph.Timestamp
} }
func (r *RTPStats) getDrift() (timeSinceFirst time.Duration, rtpDiffSinceFirst uint64, drift int64, driftMs float64, sampleRate float64) {
timeSinceFirst = r.highestTime.Sub(r.firstTime)
rtpDiffSinceFirst = getExtTS(r.highestTS, r.tsCycles) - r.extStartTS
drift = int64(rtpDiffSinceFirst - uint64(timeSinceFirst.Nanoseconds()*int64(r.params.ClockRate)/1e9))
driftMs = (float64(drift) * 1000) / float64(r.params.ClockRate)
sampleRate = float64(rtpDiffSinceFirst) / timeSinceFirst.Seconds()
return
}
func (r *RTPStats) updateGapHistogram(gap int) { func (r *RTPStats) updateGapHistogram(gap int) {
if gap < 2 { if gap < 2 {
return return
@@ -1533,6 +1554,8 @@ func AggregateRTPStats(statsList []*livekit.RTPStats) *livekit.RTPStats {
lastFir := time.Time{} lastFir := time.Time{}
rtt := uint32(0) rtt := uint32(0)
maxRtt := uint32(0) maxRtt := uint32(0)
driftMs := float64(0.0)
sampleRate := float64(0.0)
for _, stats := range statsList { for _, stats := range statsList {
if startTime.IsZero() || startTime.After(stats.StartTime.AsTime()) { if startTime.IsZero() || startTime.After(stats.StartTime.AsTime()) {
@@ -1599,6 +1622,9 @@ func AggregateRTPStats(statsList []*livekit.RTPStats) *livekit.RTPStats {
if stats.RttMax > maxRtt { if stats.RttMax > maxRtt {
maxRtt = stats.RttMax maxRtt = stats.RttMax
} }
driftMs += stats.DriftMs
sampleRate += stats.SampleRate
} }
if endTime.IsZero() { if endTime.IsZero() {
@@ -1661,6 +1687,8 @@ func AggregateRTPStats(statsList []*livekit.RTPStats) *livekit.RTPStats {
LastFir: timestamppb.New(lastFir), LastFir: timestamppb.New(lastFir),
RttCurrent: rtt / uint32(len(statsList)), RttCurrent: rtt / uint32(len(statsList)),
RttMax: maxRtt, RttMax: maxRtt,
DriftMs: driftMs / float64(len(statsList)),
SampleRate: sampleRate / float64(len(statsList)),
} }
} }