container/mts: wrote test TestEncodeVideo

Wrote a test to check mts encoding of video to validate packet creation etc. Test is failing because how I did mts
encoding is interesting, thinking about changing.
This commit is contained in:
Saxon 2019-05-07 21:22:57 +09:30
parent a805dc13a6
commit 0e362e6056
2 changed files with 91 additions and 3 deletions

View File

@ -1,12 +1,13 @@
/*
NAME
audio_test.go
encoder_test.go
AUTHOR
Trek Hopton <trek@ausocean.org>
Saxon A. Nelson-Milton <saxon@ausocean.org>
LICENSE
audio_test.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
encoder_test.go is Copyright (C) 2017-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
@ -26,6 +27,7 @@ package mts
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"testing"
@ -40,6 +42,91 @@ type nopCloser struct{ io.Writer }
func (nopCloser) Close() error { return nil }
type destination struct {
packets [][]byte
}
func (d *destination) Write(p []byte) (int, error) {
tmp := make([]byte, PacketSize)
copy(tmp, p)
d.packets = append(d.packets, tmp)
return len(p), nil
}
func TestEncodeVideo(t *testing.T) {
Meta = meta.New()
const headAndAdaptLen = 12
const adaptNoStuffLen = 7
const dataLength = 440
const numOfPackets = 3
const stuffingLen = 88
// Generate test data.
data := make([]byte, 0, dataLength)
for i := 0; i < dataLength; i++ {
data = append(data, byte(i))
}
// Expect headers for PID 256 (video)
// NB: timing fieldsl ike PCR are neglected.
expectedHeaders := [][]byte{
{
0x47, // Sync byte.
0x41, // TEI=0, PUSI=1, TP=0, PID=00001 (256).
0x00, // PID(Cont)=00000000.
0x30, // TSC=00, AFC=11(adaptation followed by payload), CC=0000(0).
0x07, // AFL= 7.
0x50, // DI=0,RAI=1,ESPI=0,PCRF=1,OPCRF=0,SPF=0,TPDF=0, AFEF=0.
},
{
0x47, // Sync byte.
0x01, // TEI=0, PUSI=0, TP=0, PID=00001 (256).
0x00, // PID(Cont)=00000000.
0x31, // TSC=00, AFC=11(adaptation followed by payload), CC=0000(0).
0x07, // AFL= 7.
0x10, // DI=0,RAI=0,ESPI=0,PCRF=1,OPCRF=0,SPF=0,TPDF=0, AFEF=0.
},
{
0x47, // Sync byte.
0x01, // TEI=0, PUSI=0, TP=0, PID=00001 (256).
0x00, // PID(Cont)=00000000.
0x31, // TSC=00, AFC=11(adaptation followed by payload), CC=0000(0).
byte(adaptNoStuffLen + stuffingLen), // AFL= 7+stuffingLen.
0x10, // DI=0,RAI=0,ESPI=0,PCRF=1,OPCRF=0,SPF=0,TPDF=0, AFEF=0.
},
}
dst := &destination{}
e := NewEncoder(nopCloser{dst}, 25, Video)
// Write data.
_, err := e.Write(data)
if err != nil {
t.Fatalf("could not write data to encoder, failed with err: %v\n", err)
}
// Check headers.
var expectedIdx int
for _, p := range dst.packets {
// Get PID.
var _p packet.Packet
copy(_p[:], p)
pid := packet.Pid(&_p)
if pid == VideoPid {
// Get mts header, excluding PCR.
gotHeader := p[0:6]
fmt.Printf("got: %v\n", p)
wantHeader := expectedHeaders[expectedIdx]
fmt.Printf("want: %v\n", expectedHeaders[expectedIdx])
if !bytes.Equal(gotHeader, wantHeader) {
t.Errorf("did not get expected header.\n Got: %v\n Want: %v\n", gotHeader, wantHeader)
}
expectedIdx++
}
}
}
// TestEncodePcm tests the mpegts encoder's ability to encode pcm audio data.
// It reads and encodes input pcm data into mpegts, then decodes the mpegts and compares the result to the input pcm.
func TestEncodePcm(t *testing.T) {

View File

@ -196,6 +196,7 @@ func FindPid(d []byte, pid uint16) (pkt []byte, i int, err error) {
func (p *Packet) FillPayload(data []byte) int {
currentPktLen := 6 + asInt(p.PCRF)*6 + asInt(p.OPCRF)*6 +
asInt(p.SPF)*1 + asInt(p.TPDF)*1 + len(p.TPD)
fmt.Printf("currentPktLen: %v\n", currentPktLen)
if len(data) > PayloadSize-currentPktLen {
p.Payload = make([]byte, PayloadSize-currentPktLen)
} else {