revid & psi: fixed playback issues... added padding to pat/pmt tables

This commit is contained in:
saxon 2018-12-13 15:09:23 +10:30
parent ce036abf8b
commit 35d86b559d
4 changed files with 44 additions and 19 deletions

View File

@ -63,7 +63,7 @@ func GpsMeta(g string) {
func init() { func init() {
patTable = psi.StdPat.Bytes() patTable = psi.StdPat.Bytes()
pmtTable = psi.StdPmtTimeGps.Bytes() pmtTable = psi.StdPmt.Bytes()
} }
const ( const (
@ -180,22 +180,24 @@ func (e *Encoder) writePSI() error {
PID: patPid, PID: patPid,
CC: e.ccFor(patPid), CC: e.ccFor(patPid),
AFC: hasPayload, AFC: hasPayload,
Payload: patTable, Payload: addPadding(patTable),
} }
_, err := e.dst.Write(patPkt.Bytes()) _, err := e.dst.Write(patPkt.Bytes())
if err != nil { if err != nil {
return err return err
} }
// Update pmt table time and gps /*
err = psi.UpdateTime(pmtTable, metaData.time) // Update pmt table time and gps
if err != nil { err = psi.UpdateTime(pmtTable, metaData.time)
return err if err != nil {
} return err
err = psi.UpdateGps(pmtTable, metaData.gps) }
if err != nil { err = psi.UpdateGps(pmtTable, metaData.gps)
return nil if err != nil {
} return nil
}
*/
// Create mts packet from pmt table // Create mts packet from pmt table
pmtPkt := Packet{ pmtPkt := Packet{
@ -203,7 +205,7 @@ func (e *Encoder) writePSI() error {
PID: pmtPid, PID: pmtPid,
CC: e.ccFor(pmtPid), CC: e.ccFor(pmtPid),
AFC: hasPayload, AFC: hasPayload,
Payload: pmtTable, Payload: addPadding(pmtTable),
} }
_, err = e.dst.Write(pmtPkt.Bytes()) _, err = e.dst.Write(pmtPkt.Bytes())
if err != nil { if err != nil {
@ -213,6 +215,13 @@ func (e *Encoder) writePSI() error {
return nil return nil
} }
func addPadding(d []byte) []byte {
for len(d) < psiPacketSize {
d = append(d, 0xff)
}
return d
}
// tick advances the clock one frame interval. // tick advances the clock one frame interval.
func (e *Encoder) tick() { func (e *Encoder) tick() {
e.clock += e.frameInterval e.clock += e.frameInterval

View File

@ -30,6 +30,8 @@ package psi
import ( import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"hash/crc32"
"math/bits"
) )
// timeBytes takes a timestamp as an uint64 and converts to an 8 byte slice - // timeBytes takes a timestamp as an uint64 and converts to an 8 byte slice -
@ -70,6 +72,7 @@ func UpdateTime(d []byte, t uint64) error {
for i := range d[timeDataIndx : timeDataIndx+timeDataSize] { for i := range d[timeDataIndx : timeDataIndx+timeDataSize] {
d[i+timeDataIndx] = ts[i] d[i+timeDataIndx] = ts[i]
} }
d = updateCrc(d)
return nil return nil
} }
@ -109,5 +112,20 @@ func UpdateGps(d []byte, s string) error {
for i := range d[gpsDataIndx : gpsDataIndx+gpsDataSize] { for i := range d[gpsDataIndx : gpsDataIndx+gpsDataSize] {
d[i+gpsDataIndx] = gb[i] d[i+gpsDataIndx] = gb[i]
} }
d = updateCrc(d)
return nil return nil
} }
func addCrc(out []byte) []byte {
out = append(out, make([]byte, 4)...)
out = updateCrc(out)
return out
}
func updateCrc(out []byte) []byte {
crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[1:len(out)-4])
out[len(out)-4] = byte(crc32 >> 24)
out[len(out)-3] = byte(crc32 >> 16)
out[len(out)-2] = byte(crc32 >> 8)
out[len(out)-1] = byte(crc32)
return out
}

View File

@ -28,7 +28,6 @@ package psi
import ( import (
"hash/crc32" "hash/crc32"
"math/bits"
) )
// Lengths of section definitions // Lengths of section definitions
@ -230,12 +229,7 @@ func (p *PSI) Bytes() []byte {
out[2] = 0x80 | 0x30 | (0x03 & byte(p.Sl>>8)) out[2] = 0x80 | 0x30 | (0x03 & byte(p.Sl>>8))
out[3] = byte(p.Sl) out[3] = byte(p.Sl)
out = append(out, p.Tss.Bytes()...) out = append(out, p.Tss.Bytes()...)
crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[3:]) out = addCrc(out)
out = append(out, make([]byte, 0, 4)...)
out = append(out, byte(crc32>>24))
out = append(out, byte(crc32>>16))
out = append(out, byte(crc32>>8))
out = append(out, byte(crc32))
return out return out
} }

View File

@ -218,7 +218,9 @@ func TestTimestampToBytes(t *testing.T) {
func TestTimeUpdate(t *testing.T) { func TestTimeUpdate(t *testing.T) {
cpy := make([]byte, len(pmtTimeBytes1)) cpy := make([]byte, len(pmtTimeBytes1))
copy(cpy, pmtTimeBytes1) copy(cpy, pmtTimeBytes1)
cpy = addCrc(cpy)
err := UpdateTime(cpy, tstTime2) err := UpdateTime(cpy, tstTime2)
cpy = cpy[:len(cpy)-4]
if err != nil { if err != nil {
t.Errorf("Update time returned err: %v", err) t.Errorf("Update time returned err: %v", err)
} }
@ -242,7 +244,9 @@ func TestTimeGet(t *testing.T) {
func TestGpsUpdate(t *testing.T) { func TestGpsUpdate(t *testing.T) {
cpy := make([]byte, len(pmtTimeGpsBytes1)) cpy := make([]byte, len(pmtTimeGpsBytes1))
copy(cpy, pmtTimeGpsBytes1) copy(cpy, pmtTimeGpsBytes1)
cpy = addCrc(cpy)
err := UpdateGps(cpy, gpsTstStr2) err := UpdateGps(cpy, gpsTstStr2)
cpy = cpy[:len(cpy)-4]
if err != nil { if err != nil {
t.Errorf("Update time returned err: %v", err) t.Errorf("Update time returned err: %v", err)
} }