av/container/mts/audio_test.go

145 lines
3.8 KiB
Go
Raw Normal View History

2019-04-05 08:58:25 +03:00
/*
NAME
audio_test.go
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
audio_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
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-04-05 08:58:25 +03:00
*/
package mts
import (
"bytes"
2019-04-18 12:01:49 +03:00
"io"
2019-04-05 08:58:25 +03:00
"io/ioutil"
"testing"
"github.com/Comcast/gots/packet"
"github.com/Comcast/gots/pes"
"bitbucket.org/ausocean/av/container/mts/meta"
2019-04-05 08:58:25 +03:00
)
2019-04-18 12:01:49 +03:00
type nopCloser struct{ io.Writer }
2019-04-18 12:01:49 +03:00
func (nopCloser) Close() error { return nil }
2019-04-05 08:58:25 +03:00
// 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.
2019-04-05 08:58:25 +03:00
func TestEncodePcm(t *testing.T) {
Meta = meta.New()
2019-04-18 12:01:49 +03:00
var buf bytes.Buffer
2019-04-05 08:58:25 +03:00
sampleRate := 48000
sampleSize := 2
blockSize := 16000
writeFreq := float64(sampleRate*sampleSize) / float64(blockSize)
e := NewEncoder(nopCloser{&buf}, writeFreq, Audio)
2019-04-05 08:58:25 +03:00
inPath := "../../../test/test-data/av/input/sweep_400Hz_20000Hz_-3dBFS_5s_48khz.pcm"
inPcm, err := ioutil.ReadFile(inPath)
if err != nil {
t.Errorf("unable to read file: %v", err)
2019-04-05 08:58:25 +03:00
}
// Break pcm into blocks and encode to mts and get the resulting bytes.
for i := 0; i < len(inPcm); i += blockSize {
if len(inPcm)-i < blockSize {
block := inPcm[i:]
_, err = e.Write(block)
if err != nil {
t.Errorf("unable to write block: %v", err)
}
} else {
block := inPcm[i : i+blockSize]
_, err = e.Write(block)
if err != nil {
t.Errorf("unable to write block: %v", err)
}
}
2019-04-05 08:58:25 +03:00
}
2019-04-23 06:48:41 +03:00
clip := buf.Bytes()
2019-04-05 08:58:25 +03:00
// Get the first MTS packet to check
2019-04-05 08:58:25 +03:00
var pkt packet.Packet
pesPacket := make([]byte, 0, blockSize)
2019-04-05 08:58:25 +03:00
got := make([]byte, 0, len(inPcm))
i := 0
if i+PacketSize <= len(clip) {
2019-04-05 08:58:25 +03:00
copy(pkt[:], clip[i:i+PacketSize])
}
// Loop through MTS packets until all the audio data from PES packets has been retrieved
for i+PacketSize <= len(clip) {
// Check MTS packet
if !(pkt.PID() == audioPid) {
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
continue
}
if !pkt.PayloadUnitStartIndicator() {
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
} else {
// Copy the first MTS payload
payload, err := pkt.Payload()
if err != nil {
t.Errorf("unable to get MTS payload: %v", err)
}
pesPacket = append(pesPacket, payload...)
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
// Copy the rest of the MTS payloads that are part of the same PES packet
for (!pkt.PayloadUnitStartIndicator()) && i+PacketSize <= len(clip) {
payload, err = pkt.Payload()
2019-04-05 08:58:25 +03:00
if err != nil {
t.Errorf("unable to get MTS payload: %v", err)
2019-04-05 08:58:25 +03:00
}
pesPacket = append(pesPacket, payload...)
2019-04-05 08:58:25 +03:00
i += PacketSize
if i+PacketSize <= len(clip) {
2019-04-05 08:58:25 +03:00
copy(pkt[:], clip[i:i+PacketSize])
}
}
2019-04-05 08:58:25 +03:00
}
// Get the audio data from the current PES packet
pesHeader, err := pes.NewPESHeader(pesPacket)
if err != nil {
t.Errorf("unable to read PES packet: %v", err)
}
got = append(got, pesHeader.Data()...)
pesPacket = pesPacket[:0]
2019-04-05 08:58:25 +03:00
}
// Compare data from MTS with original data.
2019-04-05 08:58:25 +03:00
if !bytes.Equal(got, inPcm) {
t.Error("data decoded from mts did not match input data")
2019-04-05 08:58:25 +03:00
}
}