mirror of https://bitbucket.org/ausocean/av.git
stream/mts: wrote test in metaEncode_test.go and found bug which was fixed
This commit is contained in:
parent
7dd1ce99e1
commit
d373f85b85
|
@ -200,27 +200,29 @@ func (e *Encoder) Encode(nalu []byte) error {
|
|||
func (e *Encoder) writePSI() error {
|
||||
// Write PAT.
|
||||
patPkt := Packet{
|
||||
PUSI: true,
|
||||
PID: PatPid,
|
||||
CC: e.ccFor(PatPid),
|
||||
AFC: HasPayload,
|
||||
PUSI: true,
|
||||
PID: PatPid,
|
||||
CC: e.ccFor(PatPid),
|
||||
AFC: HasPayload,
|
||||
Payload: psi.AddPadding(patTable),
|
||||
}
|
||||
patPkt.FillPayload(patTable)
|
||||
_, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updateMeta(pmtTable)
|
||||
if err = updateMeta(&pmtTable); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create mts packet from pmt table.
|
||||
pmtPkt := Packet{
|
||||
PUSI: true,
|
||||
PID: PmtPid,
|
||||
CC: e.ccFor(PmtPid),
|
||||
AFC: HasPayload,
|
||||
PUSI: true,
|
||||
PID: PmtPid,
|
||||
CC: e.ccFor(PmtPid),
|
||||
AFC: HasPayload,
|
||||
Payload: psi.AddPadding(pmtTable),
|
||||
}
|
||||
pmtPkt.FillPayload(pmtTable)
|
||||
_, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize]))
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -253,10 +255,10 @@ func (e *Encoder) ccFor(pid int) byte {
|
|||
}
|
||||
|
||||
// updateMeta ...
|
||||
func updateMeta(b []byte) error {
|
||||
var p psi.PSIBytes
|
||||
p = b
|
||||
func updateMeta(b *[]byte) error {
|
||||
m := Meta.Encode()
|
||||
p.AddDescriptor(psi.MetadataTag, m)
|
||||
return nil
|
||||
p := psi.PSIBytes(*b)
|
||||
err := p.AddDescriptor(psi.MetadataTag, m)
|
||||
*b = []byte(p)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -34,6 +34,11 @@ import (
|
|||
"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
|
||||
|
||||
func TestMetaEncode(t *testing.T) {
|
||||
|
@ -41,14 +46,22 @@ func TestMetaEncode(t *testing.T) {
|
|||
buf := bytes.NewBuffer(b)
|
||||
e := NewEncoder(buf, fps)
|
||||
Meta.Add("ts", "12345678")
|
||||
e.writePSI()
|
||||
if err := e.writePSI(); err != nil {
|
||||
t.Errorf(errUnexpectedErr, err.Error())
|
||||
}
|
||||
out := buf.Bytes()
|
||||
got := out[PacketSize:]
|
||||
got := out[PacketSize+4:]
|
||||
|
||||
want := []byte{
|
||||
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a,
|
||||
psi.MetadataTag, // Descriptor tag
|
||||
0x08, // Length of bytes to follow
|
||||
0x00, 0x00, 0x00, 0x00, 0x49, 0xa2, 0x36, 0x0b, // timestamp
|
||||
0x00, 0x02, 0xb0, 0x23, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x11,
|
||||
psi.MetadataTag, // Descriptor tag
|
||||
0x0f, // Length of bytes to follow
|
||||
0x00, 0x10, 0x00, 0x0b, 't', 's', '=', '1', '2', '3', '4', '5', '6', '7', '8', // timestamp
|
||||
0x1b, 0xe1, 0x00, 0xf0, 0x00,
|
||||
}
|
||||
want = psi.AddCrc(want)
|
||||
want = psi.AddPadding(want)
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Errorf(errNotExpectedOut, got, want)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -144,7 +144,6 @@ func TestEncode(t *testing.T) {
|
|||
tstKey2+"="+tstData2)...)
|
||||
|
||||
got := meta.Encode()
|
||||
|
||||
if !bytes.Equal(expectedOut, got) {
|
||||
t.Errorf("Did not get expected out. \nGot : %v \nwant: %v", got, expectedOut)
|
||||
}
|
||||
|
|
|
@ -34,15 +34,15 @@ import (
|
|||
)
|
||||
|
||||
// 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)
|
||||
copy(t, out)
|
||||
updateCrc(t[1:])
|
||||
UpdateCrc(t[1:])
|
||||
return t
|
||||
}
|
||||
|
||||
// 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])
|
||||
binary.BigEndian.PutUint32(b[len(b)-4:], crc32)
|
||||
}
|
||||
|
|
|
@ -231,7 +231,7 @@ func TestDescriptorsNone(t *testing.T) {
|
|||
func TestCreateDescriptorEmpty(t *testing.T) {
|
||||
got := PSIBytes(tstPsi2.Bytes())
|
||||
got.createDescriptor(TimeDescTag, make([]byte, TimeDataSize))
|
||||
updateCrc(got[1:])
|
||||
UpdateCrc(got[1:])
|
||||
want := PSIBytes(tstPsi1.Bytes())
|
||||
if !bytes.Equal(want, got) {
|
||||
t.Errorf(errNotExpectedOut, got, want)
|
||||
|
@ -244,7 +244,7 @@ func TestCreateDescriptorEmpty(t *testing.T) {
|
|||
func TestCreateDescriptorNotEmpty(t *testing.T) {
|
||||
got := PSIBytes(tstPsi1.Bytes())
|
||||
got.createDescriptor(LocationDescTag, make([]byte, LocationDataSize))
|
||||
updateCrc(got[1:])
|
||||
UpdateCrc(got[1:])
|
||||
want := PSIBytes(tstPsi3.Bytes())
|
||||
if !bytes.Equal(want, got) {
|
||||
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 {
|
||||
t.Errorf(errUnexpectedErr, err.Error())
|
||||
}
|
||||
want := addCrc(pmtTimeBytesResizedBigger)
|
||||
want := AddCrc(pmtTimeBytesResizedBigger)
|
||||
if !bytes.Equal(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 {
|
||||
t.Errorf(errUnexpectedErr, err.Error())
|
||||
}
|
||||
want := addCrc(pmtTimeBytesResizedSmaller)
|
||||
want := AddCrc(pmtTimeBytesResizedSmaller)
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Errorf(errNotExpectedOut, got, want)
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ func UpdateTime(dst []byte, t uint64) error {
|
|||
for i := range dst[TimeDataIndx : TimeDataIndx+TimeDataSize] {
|
||||
dst[i+TimeDataIndx] = ts[i]
|
||||
}
|
||||
updateCrc(dst[1:])
|
||||
UpdateCrc(dst[1:])
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ func UpdateLocation(d []byte, s string) error {
|
|||
for i := range loc {
|
||||
loc[i] = 0
|
||||
}
|
||||
updateCrc(d[1:])
|
||||
UpdateCrc(d[1:])
|
||||
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
|
||||
// addition to an mpegts packet
|
||||
func addPadding(d []byte) []byte {
|
||||
func AddPadding(d []byte) []byte {
|
||||
t := make([]byte, PacketSize)
|
||||
copy(t, d)
|
||||
padding := t[len(d):]
|
||||
|
|
|
@ -169,7 +169,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()...)
|
||||
out = addCrc(out)
|
||||
out = AddCrc(out)
|
||||
return out
|
||||
}
|
||||
|
||||
|
@ -283,7 +283,7 @@ func (p *PSIBytes) AddDescriptor(tag int, data []byte) error {
|
|||
p.setProgInfoLen(newProgInfoLen)
|
||||
newSectionLen := int(psi.SectionLength(*p)) + delta
|
||||
p.setSectionLen(newSectionLen)
|
||||
updateCrc((*p)[1:])
|
||||
UpdateCrc((*p)[1:])
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -320,7 +320,6 @@ func (p *PSIBytes) createDescriptor(tag int, data []byte) {
|
|||
copy(tmp, *p)
|
||||
*p = tmp
|
||||
copy((*p)[newDescIdx+newDescLen:], (*p)[newDescIdx:newDescIdx+newDescLen])
|
||||
|
||||
// Set the tag, data len and data of the new desriptor.
|
||||
(*p)[newDescIdx] = byte(tag)
|
||||
(*p)[newDescIdx+1] = byte(dataLen)
|
||||
|
@ -332,7 +331,7 @@ func (p *PSIBytes) createDescriptor(tag int, data []byte) {
|
|||
p.setProgInfoLen(newProgInfoLen)
|
||||
newSyntaxSectionLen := int(oldSyntaxSectionLen) + addedLen
|
||||
p.setSectionLen(newSyntaxSectionLen)
|
||||
updateCrc((*p)[1:])
|
||||
UpdateCrc((*p)[1:])
|
||||
}
|
||||
|
||||
// setProgInfoLen sets the program information length in a psi with a pmt.
|
||||
|
|
|
@ -282,7 +282,7 @@ var bytesTests = []struct {
|
|||
func TestBytes(t *testing.T) {
|
||||
for _, test := range bytesTests {
|
||||
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,
|
||||
test.want)
|
||||
}
|
||||
|
@ -301,7 +301,7 @@ func TestTimestampToBytes(t *testing.T) {
|
|||
func TestTimeUpdate(t *testing.T) {
|
||||
cpy := make([]byte, len(pmtTimeBytes1))
|
||||
copy(cpy, pmtTimeBytes1)
|
||||
cpy = addCrc(cpy)
|
||||
cpy = AddCrc(cpy)
|
||||
err := UpdateTime(cpy, tstTime2)
|
||||
cpy = cpy[:len(cpy)-4]
|
||||
if err != nil {
|
||||
|
@ -343,7 +343,7 @@ func TestLocationGet(t *testing.T) {
|
|||
func TestLocationUpdate(t *testing.T) {
|
||||
cpy := make([]byte, len(pmtWithMetaTst1))
|
||||
copy(cpy, pmtWithMetaTst1)
|
||||
cpy = addCrc(cpy)
|
||||
cpy = AddCrc(cpy)
|
||||
err := UpdateLocation(cpy, locationTstStr2)
|
||||
cpy = cpy[:len(cpy)-4]
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue