Use monotonic time util. (#3112)

Thank you @paulwe for doing this. I was promising to do this for a
while, but just like other times, empty promises :-(
This commit is contained in:
Raja Subramanian
2024-10-17 10:49:24 +05:30
committed by GitHub
parent 19c5ed6343
commit 40b10af960
14 changed files with 23 additions and 117 deletions
+5 -17
View File
@@ -41,6 +41,7 @@ import (
"github.com/livekit/mediatransportutil/pkg/twcc"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils/mono"
)
const (
@@ -91,7 +92,6 @@ type Buffer struct {
closed atomic.Bool
mime string
baseTime time.Time
snRangeMap *utils.RangeMap[uint64, uint64]
latestTSForAudioLevelInitialized bool
@@ -149,7 +149,6 @@ func NewBuffer(ssrc uint32, maxVideoPkts, maxAudioPkts int) *Buffer {
mediaSSRC: ssrc,
maxVideoPkts: maxVideoPkts,
maxAudioPkts: maxAudioPkts,
baseTime: time.Now(),
snRangeMap: utils.NewRangeMap[uint64, uint64](100),
pliThrottle: int64(500 * time.Millisecond),
logger: l.WithComponent(sutils.ComponentPub).WithComponent(sutils.ComponentSFU),
@@ -169,13 +168,6 @@ func (b *Buffer) SetLogger(logger logger.Logger) {
}
}
func (b *Buffer) SetBaseTime(baseTime time.Time) {
b.Lock()
defer b.Unlock()
b.baseTime = baseTime
}
func (b *Buffer) SetPaused(paused bool) {
b.Lock()
defer b.Unlock()
@@ -221,7 +213,7 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili
b.ppsSnapshotId = b.rtpStats.NewSnapshotId()
b.clockRate = codec.ClockRate
b.lastReport = b.getMonotonicNowUnixNano()
b.lastReport = mono.UnixNano()
b.mime = strings.ToLower(codec.MimeType)
for _, codecParameter := range params.Codecs {
if strings.EqualFold(codecParameter.MimeType, codec.MimeType) {
@@ -330,7 +322,7 @@ func (b *Buffer) Write(pkt []byte) (n int, err error) {
return
}
now := b.getMonotonicNowUnixNano()
now := mono.UnixNano()
if b.twcc != nil && b.twccExtID != 0 && !b.closed.Load() {
if ext := rtpPacket.GetExtension(b.twccExtID); ext != nil {
b.twcc.Push(rtpPacket.SSRC, binary.BigEndian.Uint16(ext[0:2]), now, rtpPacket.Marker)
@@ -936,7 +928,7 @@ func (b *Buffer) SetSenderReportData(rtpTime uint32, ntpTime uint64, packets uin
srData := &livekit.RTCPSenderReportState{
RtpTimestamp: rtpTime,
NtpTimestamp: ntpTime,
At: b.getMonotonicNowUnixNano(),
At: mono.UnixNano(),
Packets: packets,
Octets: uint64(octets),
}
@@ -1100,7 +1092,7 @@ func (b *Buffer) GetAudioLevel() (float64, bool) {
return 0, false
}
return b.audioLevel.GetLevel(b.getMonotonicNowUnixNano())
return b.audioLevel.GetLevel(mono.UnixNano())
}
func (b *Buffer) OnFpsChanged(f func()) {
@@ -1120,10 +1112,6 @@ func (b *Buffer) GetTemporalLayerFpsForSpatial(layer int32) []float32 {
return nil
}
func (b *Buffer) getMonotonicNowUnixNano() int64 {
return b.baseTime.Add(time.Since(b.baseTime)).UnixNano()
}
// ---------------------------------------------------------------
// SVC-TODO: Have to use more conditions to differentiate between