Misc changes (#1669)

This commit is contained in:
Raja Subramanian
2023-04-30 15:42:39 +05:30
committed by GitHub
parent 35b8319b08
commit dfa3d77945
3 changed files with 63 additions and 12 deletions
+53 -3
View File
@@ -124,6 +124,7 @@ type RTPStats struct {
lastRR rtcp.ReceptionReport
highestTS uint32
tsCycles uint32
highestTime int64
lastTransit uint32
@@ -174,7 +175,9 @@ type RTPStats struct {
rtt uint32
maxRtt uint32
srDataExt *RTCPSenderReportDataExt
srDataExt *RTCPSenderReportDataExt
firstSenderReportNTP mediatransportutil.NtpTime
firstSenderReportRTP uint32
nextSnapshotId uint32
snapshots map[uint32]*Snapshot
@@ -212,6 +215,7 @@ func (r *RTPStats) Seed(from *RTPStats) {
r.lastRR = from.lastRR
r.highestTS = from.highestTS
r.tsCycles = from.tsCycles
r.highestTime = from.highestTime
r.lastTransit = from.lastTransit
@@ -270,6 +274,8 @@ func (r *RTPStats) Seed(from *RTPStats) {
} else {
r.srDataExt = nil
}
r.firstSenderReportNTP = from.firstSenderReportNTP
r.firstSenderReportRTP = from.firstSenderReportRTP
r.nextSnapshotId = from.nextSnapshotId
for id, ss := range from.snapshots {
@@ -334,6 +340,7 @@ func (r *RTPStats) Update(rtph *rtp.Header, payloadSize int, paddingSize int, pa
r.extStartSN = uint32(rtph.SequenceNumber)
r.cycles = 0
r.tsCycles = 0
first = true
@@ -400,6 +407,9 @@ func (r *RTPStats) Update(rtph *rtp.Header, payloadSize int, paddingSize int, pa
r.cycles++
}
r.highestSN = rtph.SequenceNumber
if rtph.Timestamp < r.highestTS && !first {
r.tsCycles++
}
r.highestTS = rtph.Timestamp
r.highestTime = packetTime
}
@@ -706,6 +716,11 @@ func (r *RTPStats) SetRtcpSenderReportData(srData *RTCPSenderReportData) {
// prevent against extreme case of anachronous sender reports
if r.srDataExt != nil && r.srDataExt.SenderReportData.NTPTimestamp > srData.NTPTimestamp {
r.logger.Debugw(
"anachronous RTCP sender report",
"current", srData.NTPTimestamp.Time(),
"last", r.srDataExt.SenderReportData.NTPTimestamp.Time(),
)
return
}
@@ -750,8 +765,8 @@ func (r *RTPStats) GetRtcpSenderReportDataExt() *RTCPSenderReportDataExt {
}
func (r *RTPStats) GetRtcpSenderReport(ssrc uint32, srDataExt *RTCPSenderReportDataExt) *rtcp.SenderReport {
r.lock.RLock()
defer r.lock.RUnlock()
r.lock.Lock()
defer r.lock.Unlock()
if !r.initialized {
return nil
@@ -782,6 +797,37 @@ func (r *RTPStats) GetRtcpSenderReport(ssrc uint32, srDataExt *RTCPSenderReportD
nowRTP = srDataExt.SenderReportData.RTPTimestamp + uint32(now.Sub(smoothedLocalTimeOfLatestSenderReportNTP).Milliseconds()*int64(r.params.ClockRate)/1000)
}
// TODO-REMOVE-AFTER-DEBUG
if r.firstSenderReportNTP == 0 {
r.firstSenderReportNTP = nowNTP
r.firstSenderReportRTP = nowRTP
} else {
highestTime := time.Unix(0, r.highestTime)
ntpTime := nowNTP.Time()
ntpDiff := ntpTime.Sub(highestTime)
rtpDiff := int32(nowRTP - r.highestTS)
rtpOffset := int32(nowRTP - r.highestTS - uint32(ntpDiff.Milliseconds()*int64(r.params.ClockRate)/1000))
timeSinceFirst := nowNTP.Time().Sub(r.firstSenderReportNTP.Time())
rtpDiffSinceFirst := getExtTS(nowRTP, r.tsCycles) - getExtTS(r.firstSenderReportRTP, 0)
drift := int64(uint64(timeSinceFirst.Milliseconds()*int64(r.params.ClockRate)/1000) - rtpDiffSinceFirst)
driftTime := float64(drift) / float64(r.params.ClockRate) / 1000
r.logger.Debugw(
"sender report",
"highestTS", r.highestTS,
"reportTS", nowRTP,
"rtpDiff", rtpDiff,
"highestTime", highestTime,
"reportTime", ntpTime,
"timeDiff", ntpDiff,
"rtpOffset", rtpOffset,
"timeSinceFirst", timeSinceFirst,
"rtpDiffSinceFirst", rtpDiffSinceFirst,
"drift", drift,
"driftTime(ms)", driftTime,
)
}
return &rtcp.SenderReport{
SSRC: ssrc,
NTPTime: uint64(nowNTP),
@@ -1375,6 +1421,10 @@ func (r *RTPStats) getAndResetSnapshot(snapshotId uint32, override bool) (*Snaps
// ----------------------------------
func getExtTS(ts uint32, cycles uint32) uint64 {
return (uint64(cycles) << 32) | uint64(ts)
}
func AggregateRTPStats(statsList []*livekit.RTPStats) *livekit.RTPStats {
if len(statsList) == 0 {
return nil