Skip to content

Commit 99055e5

Browse files
committed
Make duration functions work with negative values.
1 parent d36111d commit 99055e5

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

estimator/estimator.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ type Estimator struct {
2323
// New creates a new estimator that estimates rate over the last interval.
2424
func New(interval time.Duration) *Estimator {
2525
return &Estimator{
26-
interval: rtptime.FromDuration(interval, rtptime.JiffiesPerSec),
27-
time: rtptime.Now(rtptime.JiffiesPerSec),
26+
interval: uint64(
27+
rtptime.FromDuration(interval, rtptime.JiffiesPerSec),
28+
),
29+
time: rtptime.Now(rtptime.JiffiesPerSec),
2830
}
2931
}
3032

rtpconn/rtpstats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (c *webClient) GetStats() *stats.Client {
5757
maxTid := layer.maxTid
5858
rate, _ := t.rate.Estimate()
5959
maxRate, _, _ := t.GetMaxBitrate()
60-
rtt := rtptime.ToDuration(t.getRTT(),
60+
rtt := rtptime.ToDuration(int64(t.getRTT()),
6161
rtptime.JiffiesPerSec)
6262
loss, jitter := t.stats.Get(jiffies)
6363
j := time.Duration(jitter) * time.Second /

rtpconn/rtpwriter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (wp *rtpWriterPool) write(seqno uint16, index uint16, delay uint32, isvideo
138138
// audio, try again with a delay
139139
d := delay / uint32(2*len(wp.writers))
140140
timer := time.NewTimer(rtptime.ToDuration(
141-
uint64(d), rtptime.JiffiesPerSec,
141+
int64(d), rtptime.JiffiesPerSec,
142142
))
143143

144144
select {

rtptime/rtptime.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@ import (
99
var epoch = time.Now()
1010

1111
// FromDuration converts a time.Duration into units of 1/hz.
12-
func FromDuration(d time.Duration, hz uint32) uint64 {
13-
return uint64(d) * uint64(hz) / uint64(time.Second)
12+
func FromDuration(d time.Duration, hz uint32) int64 {
13+
return int64(d) * int64(hz) / int64(time.Second)
1414
}
1515

1616
// ToDuration converts units of 1/hz into a time.Duration.
17-
func ToDuration(tm uint64, hz uint32) time.Duration {
18-
return time.Duration(tm * uint64(time.Second) / uint64(hz))
17+
func ToDuration(tm int64, hz uint32) time.Duration {
18+
return time.Duration(tm * int64(time.Second) / int64(hz))
19+
}
20+
21+
func sat(a int64) uint64 {
22+
if a < 0 {
23+
return 0
24+
}
25+
return uint64(a)
1926
}
2027

2128
// Now returns the current time in units of 1/hz from an arbitrary origin.
2229
func Now(hz uint32) uint64 {
23-
return FromDuration(time.Since(epoch), hz)
30+
return sat(FromDuration(time.Since(epoch), hz))
2431
}
2532

2633
// Microseconds is like Now, but uses microseconds.
@@ -39,7 +46,7 @@ func Jiffies() uint64 {
3946

4047
// TimeToJiffies converts a time.Time into jiffies.
4148
func TimeToJiffies(tm time.Time) uint64 {
42-
return FromDuration(tm.Sub(epoch), JiffiesPerSec)
49+
return sat(FromDuration(tm.Sub(epoch), JiffiesPerSec))
4350
}
4451

4552
// The origin of NTP time.

rtptime/rtptime_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ func TestDuration(t *testing.T) {
1111
t.Errorf("Expected 48000, got %v", a)
1212
}
1313

14-
b := ToDuration(48000, 48000)
15-
if b != time.Second {
16-
t.Errorf("Expected %v, got %v", time.Second, b)
14+
b := FromDuration(-time.Second, 48000)
15+
if b != -48000 {
16+
t.Errorf("Expected -48000, got %v", b)
17+
}
18+
19+
c := ToDuration(48000, 48000)
20+
if c != time.Second {
21+
t.Errorf("Expected %v, got %v", time.Second, c)
22+
}
23+
24+
d := ToDuration(-48000, 48000)
25+
if d != -time.Second {
26+
t.Errorf("Expected %v, got %v", -time.Second, d)
1727
}
1828
}
1929

0 commit comments

Comments
 (0)