1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 00:25:58 +01:00

Make duration functions work with negative values.

This commit is contained in:
Juliusz Chroboczek 2022-04-15 03:35:34 +02:00
parent d36111d9f9
commit 99055e5cae
5 changed files with 32 additions and 13 deletions

View file

@ -23,7 +23,9 @@ 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),
interval: uint64(
rtptime.FromDuration(interval, rtptime.JiffiesPerSec),
),
time: rtptime.Now(rtptime.JiffiesPerSec),
}
}

View file

@ -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 /

View file

@ -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 {

View file

@ -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.

View file

@ -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)
}
}