2019-05-06 16:37:26 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
mpegts_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
mpegts_test.go contains testing for functionality found in mpegts.go.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-05-10 10:52:14 +03:00
|
|
|
package mts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math/rand"
|
2019-06-16 22:00:52 +03:00
|
|
|
"reflect"
|
2019-06-15 21:38:41 +03:00
|
|
|
"strconv"
|
2019-05-10 10:52:14 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-06-15 21:38:41 +03:00
|
|
|
"bitbucket.org/ausocean/av/container/mts/meta"
|
2019-05-10 10:52:14 +03:00
|
|
|
"bitbucket.org/ausocean/av/container/mts/pes"
|
|
|
|
"bitbucket.org/ausocean/av/container/mts/psi"
|
2019-05-06 19:50:07 +03:00
|
|
|
"github.com/Comcast/gots/packet"
|
2019-05-10 10:52:14 +03:00
|
|
|
)
|
|
|
|
|
2019-05-10 11:08:22 +03:00
|
|
|
// TestGetPTSRange checks that GetPTSRange can correctly get the first and last
|
2019-07-01 12:38:20 +03:00
|
|
|
// PTS in an MPEGTS clip for a general case.
|
|
|
|
func TestGetPTSRange1(t *testing.T) {
|
2019-05-10 10:52:14 +03:00
|
|
|
const (
|
2019-05-10 11:08:22 +03:00
|
|
|
numOfFrames = 20
|
|
|
|
maxFrameSize = 1000
|
|
|
|
minFrameSize = 100
|
|
|
|
rate = 25 // fps
|
|
|
|
interval = float64(1) / rate // s
|
|
|
|
ptsFreq = 90000 // Hz
|
2019-05-10 10:52:14 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Generate randomly sized data for each frame.
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
frames := make([][]byte, numOfFrames)
|
|
|
|
for i := range frames {
|
2019-05-10 11:08:22 +03:00
|
|
|
size := rand.Intn(maxFrameSize-minFrameSize) + minFrameSize
|
2019-05-10 10:52:14 +03:00
|
|
|
frames[i] = make([]byte, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
var clip bytes.Buffer
|
|
|
|
|
2019-05-10 11:08:22 +03:00
|
|
|
// Write the PSI first.
|
2019-05-10 11:17:48 +03:00
|
|
|
err := writePSI(&clip)
|
2019-05-10 11:08:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("did not expect error writing psi: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now write frames.
|
|
|
|
var curTime float64
|
|
|
|
for _, frame := range frames {
|
|
|
|
nextPTS := curTime * ptsFreq
|
|
|
|
|
2019-05-10 11:17:48 +03:00
|
|
|
err = writeFrame(&clip, frame, uint64(nextPTS))
|
2019-05-10 11:08:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("did not expect error writing frame: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
curTime += interval
|
|
|
|
}
|
|
|
|
|
2019-06-05 20:24:00 +03:00
|
|
|
got, err := GetPTSRange(clip.Bytes(), VideoPid)
|
2019-05-10 11:08:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("did not expect error getting PTS range: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := [2]uint64{0, uint64((numOfFrames - 1) * interval * ptsFreq)}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("did not get expected result.\n Got: %v\n Want: %v\n", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// writePSI is a helper function write the PSI found at the start of a clip.
|
2019-05-10 11:17:48 +03:00
|
|
|
func writePSI(b *bytes.Buffer) error {
|
2019-05-10 11:08:22 +03:00
|
|
|
// Write PAT.
|
2019-05-10 10:52:14 +03:00
|
|
|
pat := Packet{
|
|
|
|
PUSI: true,
|
|
|
|
PID: PatPid,
|
|
|
|
CC: 0,
|
|
|
|
AFC: HasPayload,
|
|
|
|
Payload: psi.AddPadding(patTable),
|
|
|
|
}
|
2019-05-10 11:08:22 +03:00
|
|
|
_, err := b.Write(pat.Bytes(nil))
|
2019-05-10 10:52:14 +03:00
|
|
|
if err != nil {
|
2019-05-10 11:08:22 +03:00
|
|
|
return err
|
2019-05-10 10:52:14 +03:00
|
|
|
}
|
|
|
|
|
2019-05-10 11:08:22 +03:00
|
|
|
// Write PMT.
|
2019-05-10 10:52:14 +03:00
|
|
|
pmt := Packet{
|
|
|
|
PUSI: true,
|
|
|
|
PID: PmtPid,
|
|
|
|
CC: 0,
|
|
|
|
AFC: HasPayload,
|
|
|
|
Payload: psi.AddPadding(pmtTable),
|
|
|
|
}
|
2019-05-10 11:08:22 +03:00
|
|
|
_, err = b.Write(pmt.Bytes(nil))
|
2019-05-10 10:52:14 +03:00
|
|
|
if err != nil {
|
2019-05-10 11:08:22 +03:00
|
|
|
return err
|
2019-05-10 10:52:14 +03:00
|
|
|
}
|
2019-05-10 11:08:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-10 10:52:14 +03:00
|
|
|
|
2019-05-10 11:08:22 +03:00
|
|
|
// writeFrame is a helper function used to form a PES packet from a frame, and
|
|
|
|
// then fragment this across MPEGTS packets where they are then written to the
|
|
|
|
// given buffer.
|
2019-05-10 11:17:48 +03:00
|
|
|
func writeFrame(b *bytes.Buffer, frame []byte, pts uint64) error {
|
2019-05-10 11:08:22 +03:00
|
|
|
// Prepare PES data.
|
|
|
|
pesPkt := pes.Packet{
|
2019-05-16 07:27:10 +03:00
|
|
|
StreamID: H264ID,
|
2019-05-10 11:08:22 +03:00
|
|
|
PDI: hasPTS,
|
|
|
|
PTS: pts,
|
|
|
|
Data: frame,
|
|
|
|
HeaderLength: 5,
|
2019-05-10 10:52:14 +03:00
|
|
|
}
|
2019-05-10 11:08:22 +03:00
|
|
|
buf := pesPkt.Bytes(nil)
|
2019-05-10 10:52:14 +03:00
|
|
|
|
2019-05-10 11:08:22 +03:00
|
|
|
// Write PES data acroos MPEGTS packets.
|
|
|
|
pusi := true
|
|
|
|
for len(buf) != 0 {
|
|
|
|
pkt := Packet{
|
|
|
|
PUSI: pusi,
|
2019-06-05 20:24:00 +03:00
|
|
|
PID: VideoPid,
|
2019-05-10 11:08:22 +03:00
|
|
|
RAI: pusi,
|
|
|
|
CC: 0,
|
|
|
|
AFC: hasAdaptationField | hasPayload,
|
|
|
|
PCRF: pusi,
|
|
|
|
}
|
|
|
|
n := pkt.FillPayload(buf)
|
|
|
|
buf = buf[n:]
|
2019-05-10 10:52:14 +03:00
|
|
|
|
2019-05-10 11:08:22 +03:00
|
|
|
pusi = false
|
|
|
|
_, err := b.Write(pkt.Bytes(nil))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-10 10:52:14 +03:00
|
|
|
}
|
2019-05-10 11:08:22 +03:00
|
|
|
return nil
|
2019-05-10 10:52:14 +03:00
|
|
|
}
|
2019-05-13 10:37:53 +03:00
|
|
|
|
2019-07-01 12:38:20 +03:00
|
|
|
// TestGetPTSRange2 checks that GetPTSRange behaves correctly with cases where
|
|
|
|
// the first instance of a PID is not a payload start, and also where there
|
|
|
|
// are no payload starts.
|
|
|
|
func TestGetPTSRange2(t *testing.T) {
|
|
|
|
const (
|
|
|
|
nPackets = 8 // The number of MTS packets we will generate.
|
|
|
|
wantPID = 1 // The PID we want.
|
|
|
|
)
|
|
|
|
tests := []struct {
|
|
|
|
pusi []bool // The value of PUSI for each packet.
|
|
|
|
pid []uint16 // The PIDs for each packet.
|
|
|
|
pts []uint64 // The PTS for each packet.
|
|
|
|
want [2]uint64 // The wanted PTS from GetPTSRange.
|
|
|
|
err error // The error we expect from GetPTSRange.
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]bool{false, false, false, true, false, false, true, false},
|
|
|
|
[]uint16{0, 0, 1, 1, 1, 1, 1, 1},
|
|
|
|
[]uint64{0, 0, 0, 1, 0, 0, 2, 0},
|
|
|
|
[2]uint64{1, 2},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]bool{false, false, false, true, false, false, false, false},
|
|
|
|
[]uint16{0, 0, 1, 1, 1, 1, 1, 1},
|
|
|
|
[]uint64{0, 0, 0, 1, 0, 0, 0, 0},
|
|
|
|
[2]uint64{1, 1},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]bool{false, false, false, false, false, false, false, false},
|
|
|
|
[]uint16{0, 0, 1, 1, 1, 1, 1, 1},
|
|
|
|
[]uint64{0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
[2]uint64{0, 0},
|
|
|
|
errNoPTS,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var clip bytes.Buffer
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
// Generate MTS packets for this test.
|
|
|
|
clip.Reset()
|
|
|
|
for j := 0; j < nPackets; j++ {
|
|
|
|
pesPkt := pes.Packet{
|
|
|
|
StreamID: H264ID,
|
|
|
|
PDI: hasPTS,
|
|
|
|
PTS: test.pts[j],
|
|
|
|
Data: []byte{},
|
|
|
|
HeaderLength: 5,
|
|
|
|
}
|
|
|
|
buf := pesPkt.Bytes(nil)
|
|
|
|
|
|
|
|
pkt := Packet{
|
|
|
|
PUSI: test.pusi[j],
|
|
|
|
PID: test.pid[j],
|
|
|
|
RAI: true,
|
|
|
|
CC: 0,
|
|
|
|
AFC: hasAdaptationField | hasPayload,
|
|
|
|
PCRF: true,
|
|
|
|
}
|
|
|
|
pkt.FillPayload(buf)
|
|
|
|
|
|
|
|
_, err := clip.Write(pkt.Bytes(nil))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("did not expect clip write error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pts, err := GetPTSRange(clip.Bytes(), wantPID)
|
|
|
|
if err != test.err {
|
|
|
|
t.Errorf("did not get expected error for test: %v\nGot: %v\nWant: %v\n", i, err, test.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pts != test.want {
|
|
|
|
t.Errorf("did not get expected result for test: %v\nGot: %v\nWant: %v\n", i, pts, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:47:33 +03:00
|
|
|
// TestBytes checks that Packet.Bytes() correctly produces a []byte
|
|
|
|
// representation of a Packet.
|
2019-05-06 16:37:26 +03:00
|
|
|
func TestBytes(t *testing.T) {
|
2019-05-07 06:47:33 +03:00
|
|
|
const payloadLen, payloadChar, stuffingChar = 120, 0x11, 0xff
|
|
|
|
const stuffingLen = PacketSize - payloadLen - 12
|
|
|
|
|
2019-05-06 16:37:26 +03:00
|
|
|
tests := []struct {
|
|
|
|
packet Packet
|
|
|
|
expectedHeader []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
packet: Packet{
|
|
|
|
PUSI: true,
|
|
|
|
PID: 1,
|
|
|
|
RAI: true,
|
|
|
|
CC: 4,
|
|
|
|
AFC: HasPayload | HasAdaptationField,
|
|
|
|
PCRF: true,
|
|
|
|
PCR: 1,
|
|
|
|
},
|
|
|
|
expectedHeader: []byte{
|
|
|
|
0x47, // Sync byte.
|
|
|
|
0x40, // TEI=0, PUSI=1, TP=0, PID=00000.
|
|
|
|
0x01, // PID(Cont)=00000001.
|
|
|
|
0x34, // TSC=00, AFC=11(adaptation followed by payload), CC=0100(4).
|
2019-05-07 06:47:33 +03:00
|
|
|
byte(7 + stuffingLen), // AFL=.
|
2019-05-06 16:37:26 +03:00
|
|
|
0x50, // DI=0,RAI=1,ESPI=0,PCRF=1,OPCRF=0,SPF=0,TPDF=0, AFEF=0.
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, // PCR.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for testNum, test := range tests {
|
|
|
|
// Construct payload.
|
2019-05-07 06:47:33 +03:00
|
|
|
payload := make([]byte, 0, payloadLen)
|
|
|
|
for i := 0; i < payloadLen; i++ {
|
2019-05-06 16:37:26 +03:00
|
|
|
payload = append(payload, payloadChar)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill the packet payload.
|
|
|
|
test.packet.FillPayload(payload)
|
|
|
|
|
2019-05-07 06:47:33 +03:00
|
|
|
// Create expected packet data and copy in expected header.
|
2019-05-06 16:37:26 +03:00
|
|
|
expected := make([]byte, len(test.expectedHeader), PacketSize)
|
|
|
|
copy(expected, test.expectedHeader)
|
|
|
|
|
|
|
|
// Append stuffing.
|
2019-05-07 06:47:33 +03:00
|
|
|
for i := 0; i < stuffingLen; i++ {
|
2019-05-06 16:37:26 +03:00
|
|
|
expected = append(expected, stuffingChar)
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:47:33 +03:00
|
|
|
// Append payload to expected bytes.
|
2019-05-06 16:37:26 +03:00
|
|
|
expected = append(expected, payload...)
|
|
|
|
|
|
|
|
// Compare got with expected.
|
|
|
|
got := test.packet.Bytes(nil)
|
|
|
|
if !bytes.Equal(got, expected) {
|
|
|
|
t.Errorf("did not get expected result for test: %v.\n Got: %v\n Want: %v\n", testNum, got, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 19:50:07 +03:00
|
|
|
|
2019-05-07 06:47:33 +03:00
|
|
|
// TestFindPid checks that FindPid can correctly extract the first instance
|
2019-05-11 14:44:28 +03:00
|
|
|
// of a PID from an MPEG-TS stream.
|
2019-05-06 19:50:07 +03:00
|
|
|
func TestFindPid(t *testing.T) {
|
|
|
|
const targetPacketNum, numOfPackets, targetPid, stdPid = 6, 15, 1, 0
|
|
|
|
|
2019-05-07 06:47:33 +03:00
|
|
|
// Prepare the stream of packets.
|
2019-05-06 19:50:07 +03:00
|
|
|
var stream []byte
|
|
|
|
for i := 0; i < numOfPackets; i++ {
|
2019-05-07 17:39:42 +03:00
|
|
|
pid := uint16(stdPid)
|
2019-05-06 19:50:07 +03:00
|
|
|
if i == targetPacketNum {
|
2019-05-07 17:39:42 +03:00
|
|
|
pid = targetPid
|
2019-05-06 19:50:07 +03:00
|
|
|
}
|
2019-05-07 06:47:33 +03:00
|
|
|
|
2019-05-06 19:50:07 +03:00
|
|
|
p := Packet{
|
|
|
|
PID: pid,
|
|
|
|
AFC: hasPayload | hasAdaptationField,
|
|
|
|
}
|
|
|
|
p.FillPayload([]byte{byte(i)})
|
|
|
|
stream = append(stream, p.Bytes(nil)...)
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:47:33 +03:00
|
|
|
// Try to find the targetPid in the stream.
|
2019-05-06 19:50:07 +03:00
|
|
|
p, i, err := FindPid(stream, targetPid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error finding PID: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:47:33 +03:00
|
|
|
// Check the payload.
|
2019-05-06 19:50:07 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:47:33 +03:00
|
|
|
// Check the index.
|
2019-05-06 19:50:07 +03:00
|
|
|
_got := i / PacketSize
|
|
|
|
if _got != targetPacketNum {
|
|
|
|
t.Errorf("index of found packet is not correct.\nGot: %v, want: %v\n", _got, targetPacketNum)
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 21:38:41 +03:00
|
|
|
|
|
|
|
// TestTrimToMetaRange checks that TrimToMetaRange can correctly return a segment
|
|
|
|
// of MPEG-TS corresponding to a meta interval in a slice of MPEG-TS.
|
|
|
|
func TestTrimToMetaRange(t *testing.T) {
|
|
|
|
Meta = meta.New()
|
|
|
|
|
|
|
|
const (
|
|
|
|
nPSI = 10
|
|
|
|
key = "n"
|
|
|
|
)
|
|
|
|
|
2019-07-04 08:06:26 +03:00
|
|
|
var clip bytes.Buffer
|
2019-06-15 21:38:41 +03:00
|
|
|
|
|
|
|
for i := 0; i < nPSI; i++ {
|
|
|
|
Meta.Add(key, strconv.Itoa((i*2)+1))
|
|
|
|
err := writePSIWithMeta(&clip)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("did not expect to get error writing PSI, error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
from string
|
|
|
|
to string
|
|
|
|
expect []byte
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
from: "3",
|
|
|
|
to: "9",
|
|
|
|
expect: clip.Bytes()[3*PacketSize : 10*PacketSize],
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: "30",
|
|
|
|
to: "8",
|
|
|
|
expect: nil,
|
|
|
|
err: errMetaLowerBound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: "3",
|
|
|
|
to: "30",
|
|
|
|
expect: nil,
|
|
|
|
err: errMetaUpperBound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run tests.
|
|
|
|
for i, test := range tests {
|
|
|
|
got, err := TrimToMetaRange(clip.Bytes(), key, test.from, test.to)
|
|
|
|
|
|
|
|
// First check the error.
|
|
|
|
if err != nil && err != test.err {
|
|
|
|
t.Errorf("unexpected error: %v for test: %v", err, i)
|
|
|
|
continue
|
|
|
|
} else if err != test.err {
|
|
|
|
t.Errorf("expected to get error: %v for test: %v", test.err, i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check data.
|
|
|
|
if test.err == nil && !bytes.Equal(test.expect, got) {
|
|
|
|
t.Errorf("did not get expected data for test: %v\n Got: %v\n, Want: %v\n", i, got, test.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 22:00:52 +03:00
|
|
|
|
2019-06-17 10:04:22 +03:00
|
|
|
// TestSegmentForMeta checks that SegmentForMeta can correctly segment some MTS
|
|
|
|
// data based on a given meta key and value.
|
2019-06-16 22:00:52 +03:00
|
|
|
func TestSegmentForMeta(t *testing.T) {
|
|
|
|
Meta = meta.New()
|
|
|
|
|
|
|
|
const (
|
2019-06-17 10:04:22 +03:00
|
|
|
nPSI = 10 // The number of PSI pairs to write.
|
|
|
|
key = "n" // The meta key we will work with.
|
|
|
|
val = "*" // This is the meta value we will look for.
|
2019-06-16 22:00:52 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-06-17 10:04:22 +03:00
|
|
|
metaVals [nPSI]string // This represents the meta value for meta pairs (PAT and PMT)
|
|
|
|
expectIdxs []rng // This gives the expect index ranges for the segments.
|
2019-06-16 22:00:52 +03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
metaVals: [nPSI]string{"1", "2", val, val, val, "3", val, val, "4", "4"},
|
|
|
|
expectIdxs: []rng{
|
|
|
|
scale(2, 5),
|
|
|
|
scale(6, 8),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metaVals: [nPSI]string{"1", "2", val, val, val, "", "3", val, val, "4"},
|
|
|
|
expectIdxs: []rng{
|
|
|
|
scale(2, 5),
|
2019-06-17 13:35:08 +03:00
|
|
|
scale(7, 9),
|
2019-06-16 22:00:52 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metaVals: [nPSI]string{"1", "2", val, val, val, "", "3", val, val, val},
|
|
|
|
expectIdxs: []rng{
|
|
|
|
scale(2, 5),
|
|
|
|
{((7 * 2) + 1) * PacketSize, (nPSI * 2) * PacketSize},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metaVals: [nPSI]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"},
|
|
|
|
expectIdxs: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var clip bytes.Buffer
|
|
|
|
|
|
|
|
for testn, test := range tests {
|
2019-06-17 10:04:22 +03:00
|
|
|
// We want a clean buffer for each new test, so reset.
|
2019-06-16 22:00:52 +03:00
|
|
|
clip.Reset()
|
2019-06-17 10:04:22 +03:00
|
|
|
|
|
|
|
// Add meta and write PSI to clip.
|
2019-06-16 22:00:52 +03:00
|
|
|
for i := 0; i < nPSI; i++ {
|
|
|
|
if test.metaVals[i] != "" {
|
|
|
|
Meta.Add(key, test.metaVals[i])
|
|
|
|
} else {
|
|
|
|
Meta.Delete(key)
|
|
|
|
}
|
|
|
|
err := writePSIWithMeta(&clip)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("did not expect to get error writing PSI, error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 10:04:22 +03:00
|
|
|
|
|
|
|
// Now we get the expected segments using the index ranges from the test.
|
2019-06-16 22:00:52 +03:00
|
|
|
var want [][]byte
|
|
|
|
for _, idxs := range test.expectIdxs {
|
|
|
|
want = append(want, clip.Bytes()[idxs.start:idxs.end])
|
|
|
|
}
|
2019-06-17 10:04:22 +03:00
|
|
|
|
|
|
|
// Now use the function we're testing to get the segments.
|
2019-06-16 22:00:52 +03:00
|
|
|
got, err := SegmentForMeta(clip.Bytes(), key, val)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-17 10:04:22 +03:00
|
|
|
// Check that segments are OK.
|
2019-06-16 22:00:52 +03:00
|
|
|
if !reflect.DeepEqual(want, got) {
|
|
|
|
t.Errorf("did not get expected result for test %v\nGot: %v\nWant: %v\n", testn, got, want)
|
|
|
|
}
|
2019-07-11 12:59:46 +03:00
|
|
|
|
|
|
|
// Now test IndexPid.
|
|
|
|
i, m, err := IndexPid(clip.Bytes(), PatPid, PmtPid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("IndexPid failed with error: %v", err)
|
|
|
|
}
|
|
|
|
if i != 0 {
|
|
|
|
t.Fatalf("IndexPid unexpected index; got %d, expected 0", i)
|
|
|
|
}
|
|
|
|
if m["n"] != "1" {
|
|
|
|
t.Fatalf("IndexPid unexpected metadata; got %s, expected 1", m["n"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, test IndexPid error handling.
|
|
|
|
for _, d := range [][]byte{[]byte{}, make([]byte, PacketSize/2), make([]byte, PacketSize)} {
|
|
|
|
_, _, err := IndexPid(d, PatPid, PmtPid)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("IndexPid expected error")
|
|
|
|
}
|
2019-06-16 22:00:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 10:04:22 +03:00
|
|
|
// rng describes an index range and is used by TestSegmentForMeta.
|
2019-06-16 22:00:52 +03:00
|
|
|
type rng struct {
|
|
|
|
start int
|
|
|
|
end int
|
|
|
|
}
|
|
|
|
|
2019-06-17 10:04:22 +03:00
|
|
|
// scale takes a PSI index (i.e. first PSI is 0, next is 1) and modifies to be
|
|
|
|
// the index of the first byte of the PSI pair (PAT and PMT) in the byte stream.
|
|
|
|
// This assumes there are only PSI written consequitively, and is used by
|
|
|
|
// TestSegmentForMeta.
|
2019-06-16 22:00:52 +03:00
|
|
|
func scale(x, y int) rng {
|
|
|
|
return rng{
|
|
|
|
((x * 2) + 1) * PacketSize,
|
|
|
|
((y * 2) + 1) * PacketSize,
|
|
|
|
}
|
|
|
|
}
|