mirror of
https://github.com/aler9/gortsplib
synced 2025-10-07 16:10:59 +08:00

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))
26 lines
602 B
Go
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)
|
|
}
|