From 0e362e605630b9303267aecb9e6eacf41c3d888a Mon Sep 17 00:00:00 2001 From: Saxon Date: Tue, 7 May 2019 21:22:57 +0930 Subject: [PATCH] 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. --- container/mts/encoder_test.go | 93 +++++++++++++++++++++++++++++++++-- container/mts/mpegts.go | 1 + 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/container/mts/encoder_test.go b/container/mts/encoder_test.go index f785930d..9000794b 100644 --- a/container/mts/encoder_test.go +++ b/container/mts/encoder_test.go @@ -1,12 +1,13 @@ /* NAME - audio_test.go + encoder_test.go AUTHOR Trek Hopton + Saxon A. Nelson-Milton 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 @@ -16,7 +17,7 @@ LICENSE 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. + 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. @@ -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) { diff --git a/container/mts/mpegts.go b/container/mts/mpegts.go index af8cab8d..c2e57731 100644 --- a/container/mts/mpegts.go +++ b/container/mts/mpegts.go @@ -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 {