generator: merge PTS and PCR clocks

This commit is contained in:
Dan Kortschak 2018-08-18 13:36:14 +09:30
parent 1fb325314d
commit 7d1bfcefe9
1 changed files with 30 additions and 20 deletions

View File

@ -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.