stream/mts/encoder.go: writing psi based on time interval rather than number of packets interval

This commit is contained in:
saxon 2019-01-24 14:33:22 +10:30
parent 286ae4334b
commit 42c9fb1d09
1 changed files with 8 additions and 6 deletions

View File

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