From da5fdccaf321753dece70be6c8212684b9874b88 Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 16 Feb 2019 09:09:54 +1030 Subject: [PATCH] stream/mts & stream/rtp: fixed timing calculations Currently time calculations in the mts encoder are based on the premise that each mpegts packet is a 'frame', and in the rtp encoder that each encoding of multiple packets into one rtp packet is a 'frame', these are both false. Mpegts encoding and rtp encoding should do timestamp calculations using a reasonable approximation of time at encoding/send. --- stream/mts/encoder.go | 5 ++++- stream/rtp/encoder.go | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/stream/mts/encoder.go b/stream/mts/encoder.go index a309eaf9..e63c15bc 100644 --- a/stream/mts/encoder.go +++ b/stream/mts/encoder.go @@ -122,6 +122,7 @@ type Encoder struct { dst io.Writer clock time.Duration + lastTime time.Time frameInterval time.Duration ptsOffset time.Duration tsSpace [PacketSize]byte @@ -247,7 +248,9 @@ func (e *Encoder) writePSI() error { // tick advances the clock one frame interval. func (e *Encoder) tick() { - e.clock += e.frameInterval + now := time.Now() + e.clock += now.Sub(e.lastTime) + e.lastTime = now } // pts retuns the current presentation timestamp. diff --git a/stream/rtp/encoder.go b/stream/rtp/encoder.go index 9cda9c00..28119b43 100644 --- a/stream/rtp/encoder.go +++ b/stream/rtp/encoder.go @@ -51,6 +51,7 @@ type Encoder struct { seqNo uint16 clock time.Duration frameInterval time.Duration + lastTime time.Time fps int buffer []byte pktSpace [defPktSize]byte @@ -120,7 +121,9 @@ func (e *Encoder) Encode(payload []byte) error { // tick advances the clock one frame interval. func (e *Encoder) tick() { - e.clock += e.frameInterval + now := time.Now() + e.clock += now.Sub(e.lastTime) + e.lastTime = now } // nxtTimestamp gets the next timestamp