Files
gortsplib/pkg/rtptime/encoder.go
Alessandro Ros f0540b4eee fix timestamp encode error after some hours (#206)
previous formula was: uint32(a + uint32(b))
this formula overflows two times, while it should overflow once.
new formula is: uint32(uint64(a) + uint64(b))
2023-03-14 17:55:06 +01:00

26 lines
602 B
Go

package rtptime
import (
"math"
"time"
)
// Encoder is a RTP timestamp encoder.
type Encoder struct {
clockRate float64
initialTimestamp time.Duration
}
// NewEncoder allocates an Encoder.
func NewEncoder(clockRate int, initialTimestamp uint32) *Encoder {
return &Encoder{
clockRate: float64(clockRate),
initialTimestamp: time.Duration(math.Ceil(float64(initialTimestamp) * float64(time.Second) / float64(clockRate))),
}
}
// Encode encodes a timestamp.
func (e *Encoder) Encode(ts time.Duration) uint32 {
return uint32((e.initialTimestamp + ts).Seconds() * e.clockRate)
}