From 1fb325314d39e65d5385c5ce768f335769f8c60f Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sat, 18 Aug 2018 13:21:50 +0930 Subject: [PATCH] generator: clean up time --- generator/mpegts_generator.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/generator/mpegts_generator.go b/generator/mpegts_generator.go index 0147a7f2..58ebd077 100644 --- a/generator/mpegts_generator.go +++ b/generator/mpegts_generator.go @@ -29,6 +29,8 @@ LICENSE package generator import ( + "time" + "bitbucket.org/ausocean/av/mpegts" "bitbucket.org/ausocean/av/pes" ) @@ -56,7 +58,8 @@ const ( pmtPid = 4096 videoPid = 256 streamID = 0xe0 - ptsOffset = 0.7 + ptsOffset = 700 * time.Millisecond + ptsFreq = 90000 // Hz maxCC = 0xf ) @@ -64,9 +67,9 @@ const ( type tsGenerator struct { outputChan chan []byte nalInputChan chan []byte - currentPtsTime float64 - currentPcrTime float64 - fps float64 + currentPtsTime time.Duration + currentPcrTime time.Duration + frameInterval time.Duration continuity map[int]byte isGenerating bool } @@ -76,7 +79,7 @@ func NewTsGenerator(fps float64) (g *tsGenerator) { return &tsGenerator{ outputChan: make(chan []byte, 1), nalInputChan: make(chan []byte, 1), - fps: fps, + frameInterval: time.Duration(float64(time.Second) / fps), currentPtsTime: ptsOffset, continuity: map[int]byte{ patPid: 0, @@ -164,15 +167,15 @@ func (g *tsGenerator) generate() { // pts retuns the next presentation timestamp. func (g *tsGenerator) pts() uint64 { - pts := uint64(g.currentPtsTime * 90000) // FIXME(kortschak): Name this magic number. - g.currentPtsTime += 1 / g.fps + pts := uint64(g.currentPtsTime.Seconds() * ptsFreq) + g.currentPtsTime += g.frameInterval return pts } // pcr returns the next program clock reference. func (g *tsGenerator) pcr() uint64 { - pcr := uint64(g.currentPcrTime * 90000) // FIXME(kortschak): Name this magic number. - g.currentPcrTime += 1 / g.fps + pcr := uint64(g.currentPcrTime.Seconds() * ptsFreq) + g.currentPcrTime += g.frameInterval return pcr }