From 42c9fb1d097abb012ba802b5792c16a870a49719 Mon Sep 17 00:00:00 2001 From: saxon Date: Thu, 24 Jan 2019 14:33:22 +1030 Subject: [PATCH 1/2] stream/mts/encoder.go: writing psi based on time interval rather than number of packets interval --- stream/mts/encoder.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/stream/mts/encoder.go b/stream/mts/encoder.go index 60393285..8a4121a4 100644 --- a/stream/mts/encoder.go +++ b/stream/mts/encoder.go @@ -122,7 +122,7 @@ var ( ) const ( - psiSndCnt = 7 + psiInterval = 1 * time.Second ) // timeLocation holds time and location data @@ -199,9 +199,10 @@ type Encoder struct { tsSpace [PacketSize]byte pesSpace [pes.MaxPesSize]byte - psiCount int - continuity map[int]byte + + now time.Time + psiLastTime time.Time } // NewEncoder returns an Encoder with the specified frame rate. @@ -233,12 +234,15 @@ const ( // generate handles the incoming data and generates equivalent mpegts packets - // sending them to the output channel. func (e *Encoder) Encode(nalu []byte) error { - if e.psiCount <= 0 { + e.now = time.Now() + if e.now.Sub(e.psiLastTime) > psiInterval { err := e.writePSI() if err != nil { return err } + e.psiLastTime = e.now } + // Prepare PES data. pesPkt := pes.Packet{ StreamID: streamID, @@ -269,7 +273,6 @@ func (e *Encoder) Encode(nalu []byte) error { pusi = false } _, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize])) - e.psiCount-- if err != nil { return err } @@ -318,7 +321,6 @@ func (e *Encoder) writePSI() error { if err != nil { return err } - e.psiCount = psiSndCnt return nil } From 31b9ec07e9be6b67393f8eda884556233705a685 Mon Sep 17 00:00:00 2001 From: saxon Date: Thu, 24 Jan 2019 14:39:14 +1030 Subject: [PATCH 2/2] stream/mts/encoder.go: no need to have a now field to capture current time - this can be local to encode function --- stream/mts/encoder.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/stream/mts/encoder.go b/stream/mts/encoder.go index 8a4121a4..02761b91 100644 --- a/stream/mts/encoder.go +++ b/stream/mts/encoder.go @@ -201,7 +201,6 @@ type Encoder struct { continuity map[int]byte - now time.Time psiLastTime time.Time } @@ -234,13 +233,13 @@ const ( // generate handles the incoming data and generates equivalent mpegts packets - // sending them to the output channel. func (e *Encoder) Encode(nalu []byte) error { - e.now = time.Now() - if e.now.Sub(e.psiLastTime) > psiInterval { + now := time.Now() + if now.Sub(e.psiLastTime) > psiInterval { err := e.writePSI() if err != nil { return err } - e.psiLastTime = e.now + e.psiLastTime = now } // Prepare PES data.