From 99055e5cae599c8521b549e56ba6dd23d5104f00 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Fri, 15 Apr 2022 03:35:34 +0200 Subject: [PATCH] Make duration functions work with negative values. --- estimator/estimator.go | 6 ++++-- rtpconn/rtpstats.go | 2 +- rtpconn/rtpwriter.go | 2 +- rtptime/rtptime.go | 19 +++++++++++++------ rtptime/rtptime_test.go | 16 +++++++++++++--- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/estimator/estimator.go b/estimator/estimator.go index 58acb21..44349c0 100644 --- a/estimator/estimator.go +++ b/estimator/estimator.go @@ -23,8 +23,10 @@ type Estimator struct { // New creates a new estimator that estimates rate over the last interval. func New(interval time.Duration) *Estimator { return &Estimator{ - interval: rtptime.FromDuration(interval, rtptime.JiffiesPerSec), - time: rtptime.Now(rtptime.JiffiesPerSec), + interval: uint64( + rtptime.FromDuration(interval, rtptime.JiffiesPerSec), + ), + time: rtptime.Now(rtptime.JiffiesPerSec), } } diff --git a/rtpconn/rtpstats.go b/rtpconn/rtpstats.go index 0fcde0d..882904e 100644 --- a/rtpconn/rtpstats.go +++ b/rtpconn/rtpstats.go @@ -57,7 +57,7 @@ func (c *webClient) GetStats() *stats.Client { maxTid := layer.maxTid rate, _ := t.rate.Estimate() maxRate, _, _ := t.GetMaxBitrate() - rtt := rtptime.ToDuration(t.getRTT(), + rtt := rtptime.ToDuration(int64(t.getRTT()), rtptime.JiffiesPerSec) loss, jitter := t.stats.Get(jiffies) j := time.Duration(jitter) * time.Second / diff --git a/rtpconn/rtpwriter.go b/rtpconn/rtpwriter.go index 6161fb5..f5e6dbc 100644 --- a/rtpconn/rtpwriter.go +++ b/rtpconn/rtpwriter.go @@ -138,7 +138,7 @@ func (wp *rtpWriterPool) write(seqno uint16, index uint16, delay uint32, isvideo // audio, try again with a delay d := delay / uint32(2*len(wp.writers)) timer := time.NewTimer(rtptime.ToDuration( - uint64(d), rtptime.JiffiesPerSec, + int64(d), rtptime.JiffiesPerSec, )) select { diff --git a/rtptime/rtptime.go b/rtptime/rtptime.go index 7feef33..45aacec 100644 --- a/rtptime/rtptime.go +++ b/rtptime/rtptime.go @@ -9,18 +9,25 @@ import ( var epoch = time.Now() // FromDuration converts a time.Duration into units of 1/hz. -func FromDuration(d time.Duration, hz uint32) uint64 { - return uint64(d) * uint64(hz) / uint64(time.Second) +func FromDuration(d time.Duration, hz uint32) int64 { + return int64(d) * int64(hz) / int64(time.Second) } // ToDuration converts units of 1/hz into a time.Duration. -func ToDuration(tm uint64, hz uint32) time.Duration { - return time.Duration(tm * uint64(time.Second) / uint64(hz)) +func ToDuration(tm int64, hz uint32) time.Duration { + return time.Duration(tm * int64(time.Second) / int64(hz)) +} + +func sat(a int64) uint64 { + if a < 0 { + return 0 + } + return uint64(a) } // Now returns the current time in units of 1/hz from an arbitrary origin. func Now(hz uint32) uint64 { - return FromDuration(time.Since(epoch), hz) + return sat(FromDuration(time.Since(epoch), hz)) } // Microseconds is like Now, but uses microseconds. @@ -39,7 +46,7 @@ func Jiffies() uint64 { // TimeToJiffies converts a time.Time into jiffies. func TimeToJiffies(tm time.Time) uint64 { - return FromDuration(tm.Sub(epoch), JiffiesPerSec) + return sat(FromDuration(tm.Sub(epoch), JiffiesPerSec)) } // The origin of NTP time. diff --git a/rtptime/rtptime_test.go b/rtptime/rtptime_test.go index 9f8b01a..0b7e447 100644 --- a/rtptime/rtptime_test.go +++ b/rtptime/rtptime_test.go @@ -11,9 +11,19 @@ func TestDuration(t *testing.T) { t.Errorf("Expected 48000, got %v", a) } - b := ToDuration(48000, 48000) - if b != time.Second { - t.Errorf("Expected %v, got %v", time.Second, b) + b := FromDuration(-time.Second, 48000) + if b != -48000 { + t.Errorf("Expected -48000, got %v", b) + } + + c := ToDuration(48000, 48000) + if c != time.Second { + t.Errorf("Expected %v, got %v", time.Second, c) + } + + d := ToDuration(-48000, 48000) + if d != -time.Second { + t.Errorf("Expected %v, got %v", -time.Second, d) } }