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
+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)
}
// ------------------------------------------