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. // tsGenerator encapsulates properties of an mpegts generator.
type tsGenerator struct { type tsGenerator struct {
outputChan chan []byte outputChan chan []byte
nalInputChan chan []byte nalInputChan chan []byte
currentPtsTime time.Duration
currentPcrTime time.Duration clock time.Duration
frameInterval time.Duration frameInterval time.Duration
continuity map[int]byte ptsOffset time.Duration
isGenerating bool
continuity map[int]byte
} }
// NewTsGenerator returns an instance of the tsGenerator struct // NewTsGenerator returns an instance of the tsGenerator struct
func NewTsGenerator(fps float64) (g *tsGenerator) { func NewTsGenerator(fps float64) (g *tsGenerator) {
return &tsGenerator{ return &tsGenerator{
outputChan: make(chan []byte, 1), outputChan: make(chan []byte, 1),
nalInputChan: make(chan []byte, 1), nalInputChan: make(chan []byte, 1),
frameInterval: time.Duration(float64(time.Second) / fps),
currentPtsTime: ptsOffset, frameInterval: time.Duration(float64(time.Second) / fps),
ptsOffset: ptsOffset,
continuity: map[int]byte{ continuity: map[int]byte{
patPid: 0, patPid: 0,
pmtPid: 0, pmtPid: 0,
@ -147,6 +150,7 @@ func (g *tsGenerator) generate() {
Payload: patTable, Payload: patTable,
} }
g.outputChan <- patPkt.Bytes() g.outputChan <- patPkt.Bytes()
// Create pmt table // Create pmt table
pmtPkt := mpegts.Packet{ pmtPkt := mpegts.Packet{
PUSI: pusi, PUSI: pusi,
@ -156,27 +160,33 @@ func (g *tsGenerator) generate() {
Payload: pmtTable, Payload: pmtTable,
} }
g.outputChan <- pmtPkt.Bytes() g.outputChan <- pmtPkt.Bytes()
// If pusi then we need to gen a pcr // If pusi then we need to gen a pcr
pkt.PCR = g.pcr() pkt.PCR = g.pcr()
g.tick()
pusi = false pusi = false
} }
g.outputChan <- pkt.Bytes() g.outputChan <- pkt.Bytes()
} }
} }
} }
// pts retuns the next presentation timestamp. // tick advances the clock one frame interval.
func (g *tsGenerator) pts() uint64 { func (g *tsGenerator) tick() {
pts := uint64(g.currentPtsTime.Seconds() * ptsFreq) g.clock += g.frameInterval
g.currentPtsTime += g.frameInterval
return pts
} }
// 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 { func (g *tsGenerator) pcr() uint64 {
pcr := uint64(g.currentPcrTime.Seconds() * ptsFreq) return uint64(g.clock.Seconds() * ptsFreq)
g.currentPcrTime += g.frameInterval
return pcr
} }
// ccFor returns the next continuity counter for pid. // ccFor returns the next continuity counter for pid.