stream/mts: wrote test in metaEncode_test.go and found bug which was fixed

This commit is contained in:
saxon 2019-01-30 15:37:15 +10:30
parent 7dd1ce99e1
commit d373f85b85
8 changed files with 53 additions and 40 deletions

View File

@ -204,14 +204,16 @@ func (e *Encoder) writePSI() error {
PID: PatPid, PID: PatPid,
CC: e.ccFor(PatPid), CC: e.ccFor(PatPid),
AFC: HasPayload, AFC: HasPayload,
Payload: psi.AddPadding(patTable),
} }
patPkt.FillPayload(patTable)
_, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize])) _, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil { if err != nil {
return err return err
} }
updateMeta(pmtTable) if err = updateMeta(&pmtTable); err != nil {
return err
}
// Create mts packet from pmt table. // Create mts packet from pmt table.
pmtPkt := Packet{ pmtPkt := Packet{
@ -219,8 +221,8 @@ func (e *Encoder) writePSI() error {
PID: PmtPid, PID: PmtPid,
CC: e.ccFor(PmtPid), CC: e.ccFor(PmtPid),
AFC: HasPayload, AFC: HasPayload,
Payload: psi.AddPadding(pmtTable),
} }
pmtPkt.FillPayload(pmtTable)
_, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize])) _, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil { if err != nil {
return err return err
@ -253,10 +255,10 @@ func (e *Encoder) ccFor(pid int) byte {
} }
// updateMeta ... // updateMeta ...
func updateMeta(b []byte) error { func updateMeta(b *[]byte) error {
var p psi.PSIBytes
p = b
m := Meta.Encode() m := Meta.Encode()
p.AddDescriptor(psi.MetadataTag, m) p := psi.PSIBytes(*b)
return nil err := p.AddDescriptor(psi.MetadataTag, m)
*b = []byte(p)
return err
} }

View File

@ -34,6 +34,11 @@ import (
"bitbucket.org/ausocean/av/stream/mts/psi" "bitbucket.org/ausocean/av/stream/mts/psi"
) )
const (
errNotExpectedOut = "Unexpected output. \n Got : %v\n, Want: %v\n"
errUnexpectedErr = "Unexpected error: %v\n"
)
const fps = 25 const fps = 25
func TestMetaEncode(t *testing.T) { func TestMetaEncode(t *testing.T) {
@ -41,14 +46,22 @@ func TestMetaEncode(t *testing.T) {
buf := bytes.NewBuffer(b) buf := bytes.NewBuffer(b)
e := NewEncoder(buf, fps) e := NewEncoder(buf, fps)
Meta.Add("ts", "12345678") Meta.Add("ts", "12345678")
e.writePSI() if err := e.writePSI(); err != nil {
t.Errorf(errUnexpectedErr, err.Error())
}
out := buf.Bytes() out := buf.Bytes()
got := out[PacketSize:] got := out[PacketSize+4:]
want := []byte{ want := []byte{
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a, 0x00, 0x02, 0xb0, 0x23, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x11,
psi.MetadataTag, // Descriptor tag psi.MetadataTag, // Descriptor tag
0x08, // Length of bytes to follow 0x0f, // Length of bytes to follow
0x00, 0x00, 0x00, 0x00, 0x49, 0xa2, 0x36, 0x0b, // timestamp 0x00, 0x10, 0x00, 0x0b, 't', 's', '=', '1', '2', '3', '4', '5', '6', '7', '8', // timestamp
0x1b, 0xe1, 0x00, 0xf0, 0x00, 0x1b, 0xe1, 0x00, 0xf0, 0x00,
} }
want = psi.AddCrc(want)
want = psi.AddPadding(want)
if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want)
}
} }

View File

@ -144,7 +144,6 @@ func TestEncode(t *testing.T) {
tstKey2+"="+tstData2)...) tstKey2+"="+tstData2)...)
got := meta.Encode() got := meta.Encode()
if !bytes.Equal(expectedOut, got) { if !bytes.Equal(expectedOut, got) {
t.Errorf("Did not get expected out. \nGot : %v \nwant: %v", got, expectedOut) t.Errorf("Did not get expected out. \nGot : %v \nwant: %v", got, expectedOut)
} }

View File

@ -34,15 +34,15 @@ import (
) )
// addCrc appends a crc table to a given psi table in bytes // addCrc appends a crc table to a given psi table in bytes
func addCrc(out []byte) []byte { func AddCrc(out []byte) []byte {
t := make([]byte, len(out)+4) t := make([]byte, len(out)+4)
copy(t, out) copy(t, out)
updateCrc(t[1:]) UpdateCrc(t[1:])
return t return t
} }
// updateCrc updates the crc of bytes slice, writing the checksum into the last four bytes. // updateCrc updates the crc of bytes slice, writing the checksum into the last four bytes.
func updateCrc(b []byte) { func UpdateCrc(b []byte) {
crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), b[:len(b)-4]) crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), b[:len(b)-4])
binary.BigEndian.PutUint32(b[len(b)-4:], crc32) binary.BigEndian.PutUint32(b[len(b)-4:], crc32)
} }

View File

@ -231,7 +231,7 @@ func TestDescriptorsNone(t *testing.T) {
func TestCreateDescriptorEmpty(t *testing.T) { func TestCreateDescriptorEmpty(t *testing.T) {
got := PSIBytes(tstPsi2.Bytes()) got := PSIBytes(tstPsi2.Bytes())
got.createDescriptor(TimeDescTag, make([]byte, TimeDataSize)) got.createDescriptor(TimeDescTag, make([]byte, TimeDataSize))
updateCrc(got[1:]) UpdateCrc(got[1:])
want := PSIBytes(tstPsi1.Bytes()) want := PSIBytes(tstPsi1.Bytes())
if !bytes.Equal(want, got) { if !bytes.Equal(want, got) {
t.Errorf(errNotExpectedOut, got, want) t.Errorf(errNotExpectedOut, got, want)
@ -244,7 +244,7 @@ func TestCreateDescriptorEmpty(t *testing.T) {
func TestCreateDescriptorNotEmpty(t *testing.T) { func TestCreateDescriptorNotEmpty(t *testing.T) {
got := PSIBytes(tstPsi1.Bytes()) got := PSIBytes(tstPsi1.Bytes())
got.createDescriptor(LocationDescTag, make([]byte, LocationDataSize)) got.createDescriptor(LocationDescTag, make([]byte, LocationDataSize))
updateCrc(got[1:]) UpdateCrc(got[1:])
want := PSIBytes(tstPsi3.Bytes()) want := PSIBytes(tstPsi3.Bytes())
if !bytes.Equal(want, got) { if !bytes.Equal(want, got) {
t.Errorf(errNotExpectedOut, got, want) t.Errorf(errNotExpectedOut, got, want)
@ -301,7 +301,7 @@ func TestAddDescriptorUpdateBigger(t *testing.T) {
if err := got.AddDescriptor(TimeDescTag, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a}); err != nil { if err := got.AddDescriptor(TimeDescTag, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a}); err != nil {
t.Errorf(errUnexpectedErr, err.Error()) t.Errorf(errUnexpectedErr, err.Error())
} }
want := addCrc(pmtTimeBytesResizedBigger) want := AddCrc(pmtTimeBytesResizedBigger)
if !bytes.Equal(got, want) { if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want) t.Errorf(errNotExpectedOut, got, want)
} }
@ -315,7 +315,7 @@ func TestAddDescriptorUpdateSmaller(t *testing.T) {
if err := got.AddDescriptor(TimeDescTag, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}); err != nil { if err := got.AddDescriptor(TimeDescTag, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}); err != nil {
t.Errorf(errUnexpectedErr, err.Error()) t.Errorf(errUnexpectedErr, err.Error())
} }
want := addCrc(pmtTimeBytesResizedSmaller) want := AddCrc(pmtTimeBytesResizedSmaller)
if !bytes.Equal(got, want) { if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want) t.Errorf(errNotExpectedOut, got, want)
} }

View File

@ -64,7 +64,7 @@ func UpdateTime(dst []byte, t uint64) error {
for i := range dst[TimeDataIndx : TimeDataIndx+TimeDataSize] { for i := range dst[TimeDataIndx : TimeDataIndx+TimeDataSize] {
dst[i+TimeDataIndx] = ts[i] dst[i+TimeDataIndx] = ts[i]
} }
updateCrc(dst[1:]) UpdateCrc(dst[1:])
return nil return nil
} }
@ -111,7 +111,7 @@ func UpdateLocation(d []byte, s string) error {
for i := range loc { for i := range loc {
loc[i] = 0 loc[i] = 0
} }
updateCrc(d[1:]) UpdateCrc(d[1:])
return nil return nil
} }
@ -126,7 +126,7 @@ func trimTo(d []byte, t byte) []byte {
// addPadding adds an appropriate amount of padding to a pat or pmt table for // addPadding adds an appropriate amount of padding to a pat or pmt table for
// addition to an mpegts packet // addition to an mpegts packet
func addPadding(d []byte) []byte { func AddPadding(d []byte) []byte {
t := make([]byte, PacketSize) t := make([]byte, PacketSize)
copy(t, d) copy(t, d)
padding := t[len(d):] padding := t[len(d):]

View File

@ -169,7 +169,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()...)
out = addCrc(out) out = AddCrc(out)
return out return out
} }
@ -283,7 +283,7 @@ func (p *PSIBytes) AddDescriptor(tag int, data []byte) error {
p.setProgInfoLen(newProgInfoLen) p.setProgInfoLen(newProgInfoLen)
newSectionLen := int(psi.SectionLength(*p)) + delta newSectionLen := int(psi.SectionLength(*p)) + delta
p.setSectionLen(newSectionLen) p.setSectionLen(newSectionLen)
updateCrc((*p)[1:]) UpdateCrc((*p)[1:])
return nil return nil
} }
@ -320,7 +320,6 @@ func (p *PSIBytes) createDescriptor(tag int, data []byte) {
copy(tmp, *p) copy(tmp, *p)
*p = tmp *p = tmp
copy((*p)[newDescIdx+newDescLen:], (*p)[newDescIdx:newDescIdx+newDescLen]) copy((*p)[newDescIdx+newDescLen:], (*p)[newDescIdx:newDescIdx+newDescLen])
// Set the tag, data len and data of the new desriptor. // Set the tag, data len and data of the new desriptor.
(*p)[newDescIdx] = byte(tag) (*p)[newDescIdx] = byte(tag)
(*p)[newDescIdx+1] = byte(dataLen) (*p)[newDescIdx+1] = byte(dataLen)
@ -332,7 +331,7 @@ func (p *PSIBytes) createDescriptor(tag int, data []byte) {
p.setProgInfoLen(newProgInfoLen) p.setProgInfoLen(newProgInfoLen)
newSyntaxSectionLen := int(oldSyntaxSectionLen) + addedLen newSyntaxSectionLen := int(oldSyntaxSectionLen) + addedLen
p.setSectionLen(newSyntaxSectionLen) p.setSectionLen(newSyntaxSectionLen)
updateCrc((*p)[1:]) UpdateCrc((*p)[1:])
} }
// setProgInfoLen sets the program information length in a psi with a pmt. // setProgInfoLen sets the program information length in a psi with a pmt.

View File

@ -282,7 +282,7 @@ var bytesTests = []struct {
func TestBytes(t *testing.T) { func TestBytes(t *testing.T) {
for _, test := range bytesTests { for _, test := range bytesTests {
got := test.input.Bytes() got := test.input.Bytes()
if !bytes.Equal(got, addCrc(test.want)) { if !bytes.Equal(got, AddCrc(test.want)) {
t.Errorf("unexpected error for test %v: got:%v want:%v", test.name, got, t.Errorf("unexpected error for test %v: got:%v want:%v", test.name, got,
test.want) test.want)
} }
@ -301,7 +301,7 @@ 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) cpy = AddCrc(cpy)
err := UpdateTime(cpy, tstTime2) err := UpdateTime(cpy, tstTime2)
cpy = cpy[:len(cpy)-4] cpy = cpy[:len(cpy)-4]
if err != nil { if err != nil {
@ -343,7 +343,7 @@ func TestLocationGet(t *testing.T) {
func TestLocationUpdate(t *testing.T) { func TestLocationUpdate(t *testing.T) {
cpy := make([]byte, len(pmtWithMetaTst1)) cpy := make([]byte, len(pmtWithMetaTst1))
copy(cpy, pmtWithMetaTst1) copy(cpy, pmtWithMetaTst1)
cpy = addCrc(cpy) cpy = AddCrc(cpy)
err := UpdateLocation(cpy, locationTstStr2) err := UpdateLocation(cpy, locationTstStr2)
cpy = cpy[:len(cpy)-4] cpy = cpy[:len(cpy)-4]
if err != nil { if err != nil {