From 7d1bfcefe954c0548a7506a77c34367ebb88feb5 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sat, 18 Aug 2018 13:36:14 +0930 Subject: [PATCH] generator: merge PTS and PCR clocks --- generator/mpegts_generator.go | 50 +++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/generator/mpegts_generator.go b/generator/mpegts_generator.go index 58ebd077..1f31983a 100644 --- a/generator/mpegts_generator.go +++ b/generator/mpegts_generator.go @@ -65,22 +65,25 @@ const ( // tsGenerator encapsulates properties of an mpegts generator. type tsGenerator struct { - outputChan chan []byte - nalInputChan chan []byte - currentPtsTime time.Duration - currentPcrTime time.Duration - frameInterval time.Duration - continuity map[int]byte - isGenerating bool + outputChan chan []byte + nalInputChan chan []byte + + clock time.Duration + frameInterval time.Duration + ptsOffset time.Duration + + continuity map[int]byte } // NewTsGenerator returns an instance of the tsGenerator struct func NewTsGenerator(fps float64) (g *tsGenerator) { return &tsGenerator{ - outputChan: make(chan []byte, 1), - nalInputChan: make(chan []byte, 1), - frameInterval: time.Duration(float64(time.Second) / fps), - currentPtsTime: ptsOffset, + outputChan: make(chan []byte, 1), + nalInputChan: make(chan []byte, 1), + + frameInterval: time.Duration(float64(time.Second) / fps), + ptsOffset: ptsOffset, + continuity: map[int]byte{ patPid: 0, pmtPid: 0, @@ -147,6 +150,7 @@ func (g *tsGenerator) generate() { Payload: patTable, } g.outputChan <- patPkt.Bytes() + // Create pmt table pmtPkt := mpegts.Packet{ PUSI: pusi, @@ -156,27 +160,33 @@ func (g *tsGenerator) generate() { Payload: pmtTable, } g.outputChan <- pmtPkt.Bytes() + // If pusi then we need to gen a pcr pkt.PCR = g.pcr() + + g.tick() + pusi = false } + g.outputChan <- pkt.Bytes() } } } -// pts retuns the next presentation timestamp. -func (g *tsGenerator) pts() uint64 { - pts := uint64(g.currentPtsTime.Seconds() * ptsFreq) - g.currentPtsTime += g.frameInterval - return pts +// tick advances the clock one frame interval. +func (g *tsGenerator) tick() { + g.clock += g.frameInterval } -// pcr returns the next program clock reference. +// pts retuns the current presentation timestamp. +func (g *tsGenerator) pts() uint64 { + return uint64((g.clock + g.ptsOffset).Seconds() * ptsFreq) +} + +// pcr returns the current program clock reference. func (g *tsGenerator) pcr() uint64 { - pcr := uint64(g.currentPcrTime.Seconds() * ptsFreq) - g.currentPcrTime += g.frameInterval - return pcr + return uint64(g.clock.Seconds() * ptsFreq) } // ccFor returns the next continuity counter for pid.