Commit 99055e5c authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Make duration functions work with negative values.

parent d36111d9
...@@ -23,8 +23,10 @@ type Estimator struct { ...@@ -23,8 +23,10 @@ type Estimator struct {
// New creates a new estimator that estimates rate over the last interval. // New creates a new estimator that estimates rate over the last interval.
func New(interval time.Duration) *Estimator { func New(interval time.Duration) *Estimator {
return &Estimator{ return &Estimator{
interval: rtptime.FromDuration(interval, rtptime.JiffiesPerSec), interval: uint64(
time: rtptime.Now(rtptime.JiffiesPerSec), rtptime.FromDuration(interval, rtptime.JiffiesPerSec),
),
time: rtptime.Now(rtptime.JiffiesPerSec),
} }
} }
......
...@@ -57,7 +57,7 @@ func (c *webClient) GetStats() *stats.Client { ...@@ -57,7 +57,7 @@ func (c *webClient) GetStats() *stats.Client {
maxTid := layer.maxTid maxTid := layer.maxTid
rate, _ := t.rate.Estimate() rate, _ := t.rate.Estimate()
maxRate, _, _ := t.GetMaxBitrate() maxRate, _, _ := t.GetMaxBitrate()
rtt := rtptime.ToDuration(t.getRTT(), rtt := rtptime.ToDuration(int64(t.getRTT()),
rtptime.JiffiesPerSec) rtptime.JiffiesPerSec)
loss, jitter := t.stats.Get(jiffies) loss, jitter := t.stats.Get(jiffies)
j := time.Duration(jitter) * time.Second / j := time.Duration(jitter) * time.Second /
......
...@@ -138,7 +138,7 @@ func (wp *rtpWriterPool) write(seqno uint16, index uint16, delay uint32, isvideo ...@@ -138,7 +138,7 @@ func (wp *rtpWriterPool) write(seqno uint16, index uint16, delay uint32, isvideo
// audio, try again with a delay // audio, try again with a delay
d := delay / uint32(2*len(wp.writers)) d := delay / uint32(2*len(wp.writers))
timer := time.NewTimer(rtptime.ToDuration( timer := time.NewTimer(rtptime.ToDuration(
uint64(d), rtptime.JiffiesPerSec, int64(d), rtptime.JiffiesPerSec,
)) ))
select { select {
......
...@@ -9,18 +9,25 @@ import ( ...@@ -9,18 +9,25 @@ import (
var epoch = time.Now() var epoch = time.Now()
// FromDuration converts a time.Duration into units of 1/hz. // FromDuration converts a time.Duration into units of 1/hz.
func FromDuration(d time.Duration, hz uint32) uint64 { func FromDuration(d time.Duration, hz uint32) int64 {
return uint64(d) * uint64(hz) / uint64(time.Second) return int64(d) * int64(hz) / int64(time.Second)
} }
// ToDuration converts units of 1/hz into a time.Duration. // ToDuration converts units of 1/hz into a time.Duration.
func ToDuration(tm uint64, hz uint32) time.Duration { func ToDuration(tm int64, hz uint32) time.Duration {
return time.Duration(tm * uint64(time.Second) / uint64(hz)) 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. // Now returns the current time in units of 1/hz from an arbitrary origin.
func Now(hz uint32) uint64 { 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. // Microseconds is like Now, but uses microseconds.
...@@ -39,7 +46,7 @@ func Jiffies() uint64 { ...@@ -39,7 +46,7 @@ func Jiffies() uint64 {
// TimeToJiffies converts a time.Time into jiffies. // TimeToJiffies converts a time.Time into jiffies.
func TimeToJiffies(tm time.Time) uint64 { func TimeToJiffies(tm time.Time) uint64 {
return FromDuration(tm.Sub(epoch), JiffiesPerSec) return sat(FromDuration(tm.Sub(epoch), JiffiesPerSec))
} }
// The origin of NTP time. // The origin of NTP time.
......
...@@ -11,9 +11,19 @@ func TestDuration(t *testing.T) { ...@@ -11,9 +11,19 @@ func TestDuration(t *testing.T) {
t.Errorf("Expected 48000, got %v", a) t.Errorf("Expected 48000, got %v", a)
} }
b := ToDuration(48000, 48000) b := FromDuration(-time.Second, 48000)
if b != time.Second { if b != -48000 {
t.Errorf("Expected %v, got %v", time.Second, b) 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)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment