Some clean up (#505)

* WIP commit

* Refactor NTP time

* Clean up

* Update lk protocol
This commit is contained in:
Raja Subramanian
2022-03-11 22:40:49 +05:30
committed by GitHub
parent a5fa54853f
commit 80bd45f061
10 changed files with 82 additions and 77 deletions
+3 -31
View File
@@ -5,16 +5,11 @@ import (
"strings"
"time"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
)
var (
ntpEpoch = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
)
type ntpTime uint64
const (
QuarterResolution = "q"
HalfResolution = "h"
@@ -42,30 +37,7 @@ func codecParametersFuzzySearch(needle webrtc.RTPCodecParameters, haystack []web
return webrtc.RTPCodecParameters{}, webrtc.ErrCodecNotFound
}
func (t ntpTime) Duration() time.Duration {
sec := (t >> 32) * 1e9
frac := (t & 0xffffffff) * 1e9
nsec := frac >> 32
if uint32(frac) >= 0x80000000 {
nsec++
}
return time.Duration(sec + nsec)
}
func (t ntpTime) Time() time.Time {
return ntpEpoch.Add(t.Duration())
}
func toNtpTime(t time.Time) ntpTime {
nsec := uint64(t.Sub(ntpEpoch))
sec := nsec / 1e9
nsec = (nsec - sec*1e9) << 32
frac := nsec / 1e9
if nsec%1e9 >= 1e9/2 {
frac++
}
return ntpTime(sec<<32 | frac)
}
// -----------------------------------------------
func getRttMs(report *rtcp.ReceptionReport) uint32 {
if report.LastSenderReport == 0 {
@@ -75,7 +47,7 @@ func getRttMs(report *rtcp.ReceptionReport) uint32 {
// RTT calculation reference: https://datatracker.ietf.org/doc/html/rfc3550#section-6.4.1
// middle 32-bits of current NTP time
now := uint32(toNtpTime(time.Now()) >> 16)
now := uint32(buffer.ToNtpTime(time.Now()) >> 16)
ntpDiff := now - report.LastSenderReport - report.Delay
return uint32(math.Ceil(float64(ntpDiff) * 1000.0 / 65536.0))
}