mirror of
https://github.com/livekit/livekit.git
synced 2026-03-30 22:05:39 +00:00
Normalize NTP time base when calculating RTT (#1297)
* Normalize NTP time base when calculating RTT * seed last SR
This commit is contained in:
2
go.mod
2
go.mod
@@ -17,7 +17,7 @@ require (
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.1
|
||||
github.com/jxskiss/base62 v1.1.0
|
||||
github.com/livekit/mageutil v0.0.0-20221221221243-f361fbe40290
|
||||
github.com/livekit/mediatransportutil v0.0.0-20221007030528-7440725c362b
|
||||
github.com/livekit/mediatransportutil v0.0.0-20230111064418-e100c2231604
|
||||
github.com/livekit/protocol v1.3.2-0.20230110201647-34cae0997a36
|
||||
github.com/livekit/psrpc v0.2.1
|
||||
github.com/livekit/rtcscore-go v0.0.0-20220815072451-20ee10ae1995
|
||||
|
||||
4
go.sum
4
go.sum
@@ -231,8 +231,8 @@ github.com/lithammer/shortuuid/v4 v4.0.0 h1:QRbbVkfgNippHOS8PXDkti4NaWeyYfcBTHtw
|
||||
github.com/lithammer/shortuuid/v4 v4.0.0/go.mod h1:Zs8puNcrvf2rV9rTH51ZLLcj7ZXqQI3lv67aw4KiB1Y=
|
||||
github.com/livekit/mageutil v0.0.0-20221221221243-f361fbe40290 h1:ZVsQUuUOM9G7O3qfDSSUd1d+KlE5EVzHKylMkMkRhYg=
|
||||
github.com/livekit/mageutil v0.0.0-20221221221243-f361fbe40290/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
|
||||
github.com/livekit/mediatransportutil v0.0.0-20221007030528-7440725c362b h1:RBNV8TckETSkIkKxcD12d8nZKVkB9GSY/sQlMoaruP4=
|
||||
github.com/livekit/mediatransportutil v0.0.0-20221007030528-7440725c362b/go.mod h1:1Dlx20JPoIKGP45eo+yuj0HjeE25zmyeX/EWHiPCjFw=
|
||||
github.com/livekit/mediatransportutil v0.0.0-20230111064418-e100c2231604 h1:Ly+KGeSS7GF13nhCTuYwLQ/FRcINeuezsTvl9OY3OQU=
|
||||
github.com/livekit/mediatransportutil v0.0.0-20230111064418-e100c2231604/go.mod h1:1Dlx20JPoIKGP45eo+yuj0HjeE25zmyeX/EWHiPCjFw=
|
||||
github.com/livekit/protocol v1.3.2-0.20230110201647-34cae0997a36 h1:SlWsB3XEt4fYYkcCeCES2V8CFkRYcX0ThdkNqWP4MPg=
|
||||
github.com/livekit/protocol v1.3.2-0.20230110201647-34cae0997a36/go.mod h1:gwCG03nKlHlC9hTjL4pXQpn783ALhmbyhq65UZxqbb8=
|
||||
github.com/livekit/psrpc v0.2.1 h1:ph/4egUMueUPoh5PZ/Aw4v6SH3wAbA+2t/GyCbpPKTg=
|
||||
|
||||
@@ -164,7 +164,9 @@ type RTPStats struct {
|
||||
rtt uint32
|
||||
maxRtt uint32
|
||||
|
||||
srData *RTCPSenderReportData
|
||||
srData *RTCPSenderReportData
|
||||
lastSRNTP mediatransportutil.NtpTime
|
||||
lastSRAt time.Time
|
||||
|
||||
nextSnapshotId uint32
|
||||
snapshots map[uint32]*Snapshot
|
||||
@@ -261,6 +263,8 @@ func (r *RTPStats) Seed(from *RTPStats) {
|
||||
} else {
|
||||
r.srData = nil
|
||||
}
|
||||
r.lastSRNTP = from.lastSRNTP
|
||||
r.lastSRAt = from.lastSRAt
|
||||
|
||||
r.nextSnapshotId = from.nextSnapshotId
|
||||
for id, ss := range from.snapshots {
|
||||
@@ -460,7 +464,7 @@ func (r *RTPStats) getTotalPacketsPrimary() uint32 {
|
||||
return packetsSeen - r.packetsPadding
|
||||
}
|
||||
|
||||
func (r *RTPStats) UpdateFromReceiverReport(rr rtcp.ReceptionReport, rtt uint32) {
|
||||
func (r *RTPStats) UpdateFromReceiverReport(rr rtcp.ReceptionReport) (rtt uint32, isRttChanged bool) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
@@ -468,13 +472,20 @@ func (r *RTPStats) UpdateFromReceiverReport(rr rtcp.ReceptionReport, rtt uint32)
|
||||
return
|
||||
}
|
||||
|
||||
rtt, err := mediatransportutil.GetRttMs(&rr, r.lastSRNTP, r.lastSRAt)
|
||||
if err == nil {
|
||||
isRttChanged = rtt != r.rtt
|
||||
}
|
||||
|
||||
if r.lastRRTime.IsZero() || r.extHighestSNOverridden <= rr.LastSequenceNumber {
|
||||
r.extHighestSNOverridden = rr.LastSequenceNumber
|
||||
r.packetsLostOverridden = rr.TotalLost
|
||||
|
||||
r.rtt = rtt
|
||||
if rtt > r.maxRtt {
|
||||
r.maxRtt = rtt
|
||||
if isRttChanged {
|
||||
r.rtt = rtt
|
||||
if rtt > r.maxRtt {
|
||||
r.maxRtt = rtt
|
||||
}
|
||||
}
|
||||
|
||||
r.jitterOverridden = float64(rr.Jitter)
|
||||
@@ -484,7 +495,7 @@ func (r *RTPStats) UpdateFromReceiverReport(rr rtcp.ReceptionReport, rtt uint32)
|
||||
|
||||
// update snapshots
|
||||
for _, s := range r.snapshots {
|
||||
if rtt > s.maxRtt {
|
||||
if isRttChanged && rtt > s.maxRtt {
|
||||
s.maxRtt = rtt
|
||||
}
|
||||
|
||||
@@ -504,6 +515,7 @@ func (r *RTPStats) UpdateFromReceiverReport(rr rtcp.ReceptionReport, rtt uint32)
|
||||
"receivedRR", rr,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r *RTPStats) UpdateNack(nackCount uint32) {
|
||||
@@ -726,6 +738,9 @@ func (r *RTPStats) GetRtcpSenderReport(ssrc uint32, srData *RTCPSenderReportData
|
||||
nowRTP = r.highestTS + uint32((now.UnixNano()-r.highestTime)*int64(r.params.ClockRate)/1e9)
|
||||
}
|
||||
|
||||
r.lastSRNTP = nowNTP
|
||||
r.lastSRAt = time.Now()
|
||||
|
||||
return &rtcp.SenderReport{
|
||||
SSRC: ssrc,
|
||||
NTPTime: uint64(nowNTP),
|
||||
|
||||
@@ -16,7 +16,6 @@ import (
|
||||
"github.com/pion/webrtc/v3"
|
||||
"go.uber.org/atomic"
|
||||
|
||||
"github.com/livekit/mediatransportutil"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
|
||||
@@ -1183,12 +1182,10 @@ func (d *DownTrack) handleRTCP(bytes []byte) {
|
||||
}
|
||||
rr.Reports = append(rr.Reports, r)
|
||||
|
||||
rtt := mediatransportutil.GetRttMs(&r)
|
||||
if rtt != d.rtpStats.GetRtt() {
|
||||
rtt, isRttChanged := d.rtpStats.UpdateFromReceiverReport(r)
|
||||
if isRttChanged {
|
||||
rttToReport = rtt
|
||||
}
|
||||
|
||||
d.rtpStats.UpdateFromReceiverReport(r, rtt)
|
||||
}
|
||||
if len(rr.Reports) > 0 {
|
||||
d.listenerLock.RLock()
|
||||
|
||||
Reference in New Issue
Block a user