Time stamp adjustments v2 (I think) (#1875)
* WIP commit * WIP commit * WIP commit * Some clean up - Removed a chatty debug log - some spelling, punctuation correction in comments - missed an `Abs` in check, add it.
This commit is contained in:
+47
-46
@@ -750,13 +750,13 @@ func (r *RTPStats) GetRtt() uint32 {
|
||||
}
|
||||
|
||||
func (r *RTPStats) SetRtcpSenderReportData(srData *RTCPSenderReportData) {
|
||||
if srData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
if srData == nil || !r.initialized {
|
||||
return
|
||||
}
|
||||
|
||||
// prevent against extreme case of anachronous sender reports
|
||||
if r.srNewest != nil && r.srNewest.NTPTimestamp > srData.NTPTimestamp {
|
||||
r.logger.Infow(
|
||||
@@ -767,29 +767,6 @@ func (r *RTPStats) SetRtcpSenderReportData(srData *RTCPSenderReportData) {
|
||||
return
|
||||
}
|
||||
|
||||
// monitor and log RTP timestamp anomalies
|
||||
var ntpDiffSinceLast time.Duration
|
||||
var rtpDiffSinceLast uint32
|
||||
var arrivalDiffSinceLast time.Duration
|
||||
var expectedTimeDiffSinceLast float64
|
||||
var reason string
|
||||
if r.srNewest != nil {
|
||||
ntpDiffSinceLast = srData.NTPTimestamp.Time().Sub(r.srNewest.NTPTimestamp.Time())
|
||||
rtpDiffSinceLast = srData.RTPTimestamp - r.srNewest.RTPTimestamp
|
||||
arrivalDiffSinceLast = srData.At.Sub(r.srNewest.At)
|
||||
|
||||
expectedTimeDiffSinceLast = float64(rtpDiffSinceLast) / float64(r.params.ClockRate)
|
||||
|
||||
if (srData.RTPTimestamp - r.srNewest.RTPTimestamp) > (1 << 31) {
|
||||
reason = "received sender report, out-of-order" // should not happen, just a sanity check
|
||||
} else {
|
||||
if math.Abs(expectedTimeDiffSinceLast-ntpDiffSinceLast.Seconds()) > 0.2 {
|
||||
// more than 200 ms away from expected delta
|
||||
reason = "received sender report, time warp"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cycles := uint64(0)
|
||||
if r.srNewest != nil {
|
||||
cycles = r.srNewest.RTPTimestampExt & 0xFF_FF_FF_FF_00_00_00_00
|
||||
@@ -800,15 +777,47 @@ func (r *RTPStats) SetRtcpSenderReportData(srData *RTCPSenderReportData) {
|
||||
|
||||
srDataCopy := *srData
|
||||
srDataCopy.RTPTimestampExt = uint64(srDataCopy.RTPTimestamp) + cycles
|
||||
|
||||
// monitor and log RTP timestamp anomalies
|
||||
var ntpDiffSinceLast time.Duration
|
||||
var rtpDiffSinceLast uint32
|
||||
var arrivalDiffSinceLast time.Duration
|
||||
var expectedTimeDiffSinceLast float64
|
||||
var isWarped bool
|
||||
if r.srNewest != nil {
|
||||
if srDataCopy.RTPTimestampExt < r.srNewest.RTPTimestampExt {
|
||||
// This can happen when a track is replaced with a null and then restored -
|
||||
// i. e. muting replacing with null and unmute restoring the original track.
|
||||
// Under such a condition reset the sender reports to start from this point.
|
||||
// Resetting will ensure sample rate calculations do not go haywire due to negative time.
|
||||
r.logger.Infow(
|
||||
"received sender report, out-of-order, resetting",
|
||||
"prevTSExt", r.srNewest.RTPTimestampExt,
|
||||
"currTSExt", srDataCopy.RTPTimestampExt,
|
||||
)
|
||||
r.srFirst = &srDataCopy
|
||||
r.srNewest = &srDataCopy
|
||||
}
|
||||
|
||||
ntpDiffSinceLast = srDataCopy.NTPTimestamp.Time().Sub(r.srNewest.NTPTimestamp.Time())
|
||||
rtpDiffSinceLast = srDataCopy.RTPTimestamp - r.srNewest.RTPTimestamp
|
||||
arrivalDiffSinceLast = srDataCopy.At.Sub(r.srNewest.At)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
r.srNewest = &srDataCopy
|
||||
if r.srFirst == nil {
|
||||
r.srFirst = &srDataCopy
|
||||
}
|
||||
|
||||
if reason != "" {
|
||||
if isWarped {
|
||||
packetDriftResult, reportDriftResult := r.getDrift()
|
||||
r.logger.Infow(
|
||||
reason,
|
||||
"received sender report, time warp",
|
||||
"ntp", srData.NTPTimestamp.Time().String(),
|
||||
"rtp", srData.RTPTimestamp,
|
||||
"arrival", srData.At.String(),
|
||||
@@ -840,26 +849,22 @@ func (r *RTPStats) GetRtcpSenderReportData() (srFirst *RTCPSenderReportData, srN
|
||||
return
|
||||
}
|
||||
|
||||
func (r *RTPStats) GetExpectedRTPTimestamp(at time.Time) (uint32, uint64, error) {
|
||||
func (r *RTPStats) GetExpectedRTPTimestamp(at time.Time) (expectedTSExt uint64, err error) {
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
|
||||
if !r.initialized {
|
||||
return 0, 0, errors.New("uninitilaized")
|
||||
err = errors.New("uninitilaized")
|
||||
return
|
||||
}
|
||||
|
||||
timeDiff := at.Sub(r.firstTime)
|
||||
expectedRTPDiff := timeDiff.Nanoseconds() * int64(r.params.ClockRate) / 1e9
|
||||
expectedExtRTP := r.extStartTS + uint64(expectedRTPDiff)
|
||||
|
||||
minTS := ^uint64(0)
|
||||
if r.srNewest != nil {
|
||||
minTS = r.srNewest.RTPTimestampExt
|
||||
}
|
||||
return uint32(expectedExtRTP), minTS, nil
|
||||
expectedTSExt = r.extStartTS + uint64(expectedRTPDiff)
|
||||
return
|
||||
}
|
||||
|
||||
func (r *RTPStats) GetRtcpSenderReport(ssrc uint32, srFirst *RTCPSenderReportData, srNewest *RTCPSenderReportData) *rtcp.SenderReport {
|
||||
func (r *RTPStats) GetRtcpSenderReport(ssrc uint32, calculatedClockRate uint32) *rtcp.SenderReport {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
@@ -877,18 +882,14 @@ func (r *RTPStats) GetRtcpSenderReport(ssrc uint32, srFirst *RTCPSenderReportDat
|
||||
|
||||
// It is possible that publisher is pacing at a slower rate.
|
||||
// That would make `highestTS` to be lagging the RTP time stamp in the RTCP Sender Report from publisher.
|
||||
// Check for that and use the later time stamp if applicable.
|
||||
// Check for that using calculated clock rate and use the later time stamp if applicable.
|
||||
tsCycles := r.tsCycles
|
||||
if nowRTP < r.highestTS {
|
||||
tsCycles++
|
||||
}
|
||||
nowRTPExt := getExtTS(nowRTP, tsCycles)
|
||||
if srFirst != nil && srNewest != nil && srFirst.RTPTimestamp != srNewest.RTPTimestamp {
|
||||
// use incoming rate as a guide
|
||||
tsf := srNewest.NTPTimestamp.Time().Sub(srFirst.NTPTimestamp.Time())
|
||||
rdsf := srNewest.RTPTimestampExt - srFirst.RTPTimestampExt
|
||||
sr := float64(rdsf) / tsf.Seconds()
|
||||
nowRTPExtUsingRate := r.extStartTS + uint64(sr*timeSinceFirst.Seconds())
|
||||
if calculatedClockRate != 0 {
|
||||
nowRTPExtUsingRate := r.extStartTS + uint64(float64(calculatedClockRate)*timeSinceFirst.Seconds())
|
||||
if nowRTPExtUsingRate > nowRTPExt {
|
||||
nowRTPExt = nowRTPExtUsingRate
|
||||
nowRTP = uint32(nowRTPExtUsingRate)
|
||||
|
||||
Reference in New Issue
Block a user