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 -3
View File
@@ -68,7 +68,7 @@ type Buffer struct {
lastPacketRead int
bitrate atomic.Value
bitrateHelper [4]int64
lastSRNTPTime uint64
lastSRNTPTime NtpTime
lastSRRTPTime uint32
lastSRRecv int64 // Represents wall clock of the most recent sender report arrival
lastTransit uint32
@@ -663,7 +663,7 @@ func (b *Buffer) buildReceptionReport() *rtcp.ReceptionReport {
func (b *Buffer) SetSenderReportData(rtpTime uint32, ntpTime uint64) {
b.Lock()
b.lastSRRTPTime = rtpTime
b.lastSRNTPTime = ntpTime
b.lastSRNTPTime = NtpTime(ntpTime)
b.lastSRRecv = time.Now().UnixNano()
b.Unlock()
}
@@ -755,7 +755,7 @@ func (b *Buffer) GetClockRate() uint32 {
}
// GetSenderReportData returns the rtp, ntp and nanos of the last sender report
func (b *Buffer) GetSenderReportData() (rtpTime uint32, ntpTime uint64, lastReceivedTimeInNanosSinceEpoch int64) {
func (b *Buffer) GetSenderReportData() (rtpTime uint32, ntpTime NtpTime, lastReceivedTimeInNanosSinceEpoch int64) {
b.RLock()
defer b.RUnlock()
+36
View File
@@ -3,6 +3,7 @@ package buffer
import (
"encoding/binary"
"errors"
"time"
"github.com/livekit/protocol/logger"
)
@@ -275,3 +276,38 @@ func IsH264Keyframe(payload []byte) bool {
}
return false
}
// -------------------------------------
var (
ntpEpoch = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
)
type NtpTime uint64
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)
}
// ------------------------------------------
+31
View File
@@ -2,6 +2,7 @@ package buffer
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
@@ -92,3 +93,33 @@ func TestVP8Helper_Unmarshal(t *testing.T) {
})
}
}
// ------------------------------------------
func Test_timeToNtp(t *testing.T) {
type args struct {
ns time.Time
}
tests := []struct {
name string
args args
wantNTP uint64
}{
{
name: "Must return correct NTP time",
args: args{
ns: time.Unix(1602391458, 1234),
},
wantNTP: 16369753560730047668,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
gotNTP := uint64(ToNtpTime(tt.args.ns))
if gotNTP != tt.wantNTP {
t.Errorf("timeToNtp() gotFraction = %v, want %v", gotNTP, tt.wantNTP)
}
})
}
}