mirror of https://bitbucket.org/ausocean/av.git
container/mts: wrote test TestFindPid and corrected bug
Completed test TestFindPid to validate function of FindPid func in mpegts.go. Through this process, it was found that there was a bug in this func, whereby the returned packet was not complete due to indexing issues. The bug is fixed and the test passes.
This commit is contained in:
parent
52c8a43cb0
commit
a8aec484df
|
@ -179,12 +179,12 @@ func FindPat(d []byte) ([]byte, int, error) {
|
||||||
// is found, then it is returned along with its index, otherwise nil, -1 and an error is returned.
|
// is found, then it is returned along with its index, otherwise nil, -1 and an error is returned.
|
||||||
func FindPid(d []byte, pid uint16) (pkt []byte, i int, err error) {
|
func FindPid(d []byte, pid uint16) (pkt []byte, i int, err error) {
|
||||||
if len(d) < PacketSize {
|
if len(d) < PacketSize {
|
||||||
return nil, -1, errors.New("Mmpegts data not of valid length")
|
return nil, -1, errors.New("Mpegts data not of valid length")
|
||||||
}
|
}
|
||||||
for i = 0; i < len(d); i += PacketSize {
|
for i = 0; i < len(d); i += PacketSize {
|
||||||
p := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2])
|
p := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2])
|
||||||
if p == pid {
|
if p == pid {
|
||||||
pkt = d[i+4 : i+PacketSize]
|
pkt = d[i : i+PacketSize]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,8 @@ package mts
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Comcast/gots/packet"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBytes(t *testing.T) {
|
func TestBytes(t *testing.T) {
|
||||||
|
@ -94,3 +96,42 @@ func TestBytes(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindPid(t *testing.T) {
|
||||||
|
const targetPacketNum, numOfPackets, targetPid, stdPid = 6, 15, 1, 0
|
||||||
|
|
||||||
|
var stream []byte
|
||||||
|
for i := 0; i < numOfPackets; i++ {
|
||||||
|
pid := uint16(0)
|
||||||
|
if i == targetPacketNum {
|
||||||
|
pid = 1
|
||||||
|
}
|
||||||
|
p := Packet{
|
||||||
|
PID: pid,
|
||||||
|
AFC: hasPayload | hasAdaptationField,
|
||||||
|
}
|
||||||
|
p.FillPayload([]byte{byte(i)})
|
||||||
|
stream = append(stream, p.Bytes(nil)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
p, i, err := FindPid(stream, targetPid)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error finding PID: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _p packet.Packet
|
||||||
|
copy(_p[:], p)
|
||||||
|
payload, err := packet.Payload(&_p)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error getting packet payload: %v\n", err)
|
||||||
|
}
|
||||||
|
got := payload[0]
|
||||||
|
if got != targetPacketNum {
|
||||||
|
t.Errorf("payload of found packet is not correct.\nGot: %v, Want: %v\n", got, targetPacketNum)
|
||||||
|
}
|
||||||
|
|
||||||
|
_got := i / PacketSize
|
||||||
|
if _got != targetPacketNum {
|
||||||
|
t.Errorf("index of found packet is not correct.\nGot: %v, want: %v\n", _got, targetPacketNum)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue