diff --git a/stream/mts/encoder.go b/stream/mts/encoder.go index 69e027f7..e6f35728 100644 --- a/stream/mts/encoder.go +++ b/stream/mts/encoder.go @@ -63,7 +63,7 @@ func GpsMeta(g string) { func init() { patTable = psi.StdPat.Bytes() - pmtTable = psi.StdPmtTimeGps.Bytes() + pmtTable = psi.StdPmt.Bytes() } const ( @@ -180,22 +180,24 @@ func (e *Encoder) writePSI() error { PID: patPid, CC: e.ccFor(patPid), AFC: hasPayload, - Payload: patTable, + Payload: addPadding(patTable), } _, err := e.dst.Write(patPkt.Bytes()) if err != nil { return err } - // Update pmt table time and gps - err = psi.UpdateTime(pmtTable, metaData.time) - if err != nil { - return err - } - err = psi.UpdateGps(pmtTable, metaData.gps) - if err != nil { - return nil - } + /* + // Update pmt table time and gps + err = psi.UpdateTime(pmtTable, metaData.time) + if err != nil { + return err + } + err = psi.UpdateGps(pmtTable, metaData.gps) + if err != nil { + return nil + } + */ // Create mts packet from pmt table pmtPkt := Packet{ @@ -203,7 +205,7 @@ func (e *Encoder) writePSI() error { PID: pmtPid, CC: e.ccFor(pmtPid), AFC: hasPayload, - Payload: pmtTable, + Payload: addPadding(pmtTable), } _, err = e.dst.Write(pmtPkt.Bytes()) if err != nil { @@ -213,6 +215,13 @@ func (e *Encoder) writePSI() error { return nil } +func addPadding(d []byte) []byte { + for len(d) < psiPacketSize { + d = append(d, 0xff) + } + return d +} + // tick advances the clock one frame interval. func (e *Encoder) tick() { e.clock += e.frameInterval diff --git a/stream/mts/psi/op.go b/stream/mts/psi/op.go index 79db4764..7c38af2a 100644 --- a/stream/mts/psi/op.go +++ b/stream/mts/psi/op.go @@ -30,6 +30,8 @@ package psi import ( "encoding/binary" "errors" + "hash/crc32" + "math/bits" ) // 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] { d[i+timeDataIndx] = ts[i] } + d = updateCrc(d) return nil } @@ -109,5 +112,20 @@ func UpdateGps(d []byte, s string) error { for i := range d[gpsDataIndx : gpsDataIndx+gpsDataSize] { d[i+gpsDataIndx] = gb[i] } + d = updateCrc(d) 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 +} diff --git a/stream/mts/psi/psi.go b/stream/mts/psi/psi.go index 6abc3a51..a2c34046 100644 --- a/stream/mts/psi/psi.go +++ b/stream/mts/psi/psi.go @@ -28,7 +28,6 @@ package psi import ( "hash/crc32" - "math/bits" ) // Lengths of section definitions @@ -230,12 +229,7 @@ func (p *PSI) Bytes() []byte { out[2] = 0x80 | 0x30 | (0x03 & byte(p.Sl>>8)) out[3] = byte(p.Sl) out = append(out, p.Tss.Bytes()...) - crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[3:]) - 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)) + out = addCrc(out) return out } diff --git a/stream/mts/psi/psi_test.go b/stream/mts/psi/psi_test.go index 977a29e4..cba8247f 100644 --- a/stream/mts/psi/psi_test.go +++ b/stream/mts/psi/psi_test.go @@ -218,7 +218,9 @@ func TestTimestampToBytes(t *testing.T) { func TestTimeUpdate(t *testing.T) { cpy := make([]byte, len(pmtTimeBytes1)) copy(cpy, pmtTimeBytes1) + cpy = addCrc(cpy) err := UpdateTime(cpy, tstTime2) + cpy = cpy[:len(cpy)-4] if err != nil { t.Errorf("Update time returned err: %v", err) } @@ -242,7 +244,9 @@ func TestTimeGet(t *testing.T) { func TestGpsUpdate(t *testing.T) { cpy := make([]byte, len(pmtTimeGpsBytes1)) copy(cpy, pmtTimeGpsBytes1) + cpy = addCrc(cpy) err := UpdateGps(cpy, gpsTstStr2) + cpy = cpy[:len(cpy)-4] if err != nil { t.Errorf("Update time returned err: %v", err) }