Merged in write-psi-by-time (pull request #128)

stream/mts/encoder.go: writing psi based on time interval

Approved-by: Alan Noble <anoble@gmail.com>
This commit is contained in:
Saxon Milton 2019-02-04 11:32:14 +00:00
commit 3f74b76eee
1 changed files with 7 additions and 6 deletions

View File

@ -122,7 +122,7 @@ var (
) )
const ( const (
psiSndCnt = 7 psiInterval = 1 * time.Second
) )
// timeLocation holds time and location data // timeLocation holds time and location data
@ -199,9 +199,9 @@ type Encoder struct {
tsSpace [PacketSize]byte tsSpace [PacketSize]byte
pesSpace [pes.MaxPesSize]byte pesSpace [pes.MaxPesSize]byte
psiCount int
continuity map[int]byte continuity map[int]byte
psiLastTime time.Time
} }
// NewEncoder returns an Encoder with the specified frame rate. // NewEncoder returns an Encoder with the specified frame rate.
@ -233,12 +233,15 @@ const (
// generate handles the incoming data and generates equivalent mpegts packets - // generate handles the incoming data and generates equivalent mpegts packets -
// sending them to the output channel. // sending them to the output channel.
func (e *Encoder) Encode(nalu []byte) error { func (e *Encoder) Encode(nalu []byte) error {
if e.psiCount <= 0 { now := time.Now()
if now.Sub(e.psiLastTime) > psiInterval {
err := e.writePSI() err := e.writePSI()
if err != nil { if err != nil {
return err return err
} }
e.psiLastTime = now
} }
// Prepare PES data. // Prepare PES data.
pesPkt := pes.Packet{ pesPkt := pes.Packet{
StreamID: streamID, StreamID: streamID,
@ -269,7 +272,6 @@ func (e *Encoder) Encode(nalu []byte) error {
pusi = false pusi = false
} }
_, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize])) _, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize]))
e.psiCount--
if err != nil { if err != nil {
return err return err
} }
@ -318,7 +320,6 @@ func (e *Encoder) writePSI() error {
if err != nil { if err != nil {
return err return err
} }
e.psiCount = psiSndCnt
return nil return nil
} }