diff --git a/container/mts/audio_test.go b/container/mts/audio_test.go index 36df7df8..f4aaf902 100644 --- a/container/mts/audio_test.go +++ b/container/mts/audio_test.go @@ -2,9 +2,6 @@ NAME audio_test.go -DESCRIPTION - See Readme.md - AUTHOR Trek Hopton @@ -21,8 +18,8 @@ LICENSE 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 - along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. + You should have received a copy of the GNU General Public License in gpl.txt. + If not, see http://www.gnu.org/licenses. */ package mts @@ -30,7 +27,6 @@ package mts import ( "bytes" "io/ioutil" - "log" "testing" "bitbucket.org/ausocean/av/container/mts/meta" @@ -39,7 +35,7 @@ import ( ) // 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 compares the result to the input pcm. +// 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) { Meta = meta.New() @@ -54,28 +50,28 @@ func TestEncodePcm(t *testing.T) { inPath := "../../../test/test-data/av/input/sweep_400Hz_20000Hz_-3dBFS_5s_48khz.pcm" inPcm, err := ioutil.ReadFile(inPath) if err != nil { - log.Fatal(err) + t.Errorf("unable to read file: %v", err) } - // Encode pcm to mts and get the resulting bytes. + // 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 { - log.Fatal(err) + t.Errorf("unable to write block: %v", err) } } else { block := inPcm[i : i+blockSize] _, err = e.Write(block) if err != nil { - log.Fatal(err) + t.Errorf("unable to write block: %v", err) } } } clip := buf.Bytes() - // Decode the mts packets to extract the original data + // Get the first MTS packet to check var pkt packet.Packet pesPacket := make([]byte, 0, blockSize) got := make([]byte, 0, len(inPcm)) @@ -84,12 +80,17 @@ func TestEncodePcm(t *testing.T) { 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 { if pkt.PayloadUnitStartIndicator() { + + // Copy the first MTS payload payload, err := pkt.Payload() if err != nil { - t.Fatalf("Unexpected err: %v\n", err) + t.Errorf("unable to get MTS payload: %v", err) } pesPacket = append(pesPacket, payload...) @@ -98,10 +99,11 @@ func TestEncodePcm(t *testing.T) { 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() if err != nil { - t.Fatalf("Unexpected err: %v\n", err) + t.Errorf("unable to get MTS payload: %v", err) } pesPacket = append(pesPacket, payload...) @@ -116,9 +118,10 @@ func TestEncodePcm(t *testing.T) { copy(pkt[:], clip[i:i+PacketSize]) } } + // Get the audio data from the current PES packet pesHeader, err := pes.NewPESHeader(pesPacket) if err != nil { - t.Fatalf("Unexpected err: %v\n", err) + t.Errorf("unable to read PES packet: %v", err) } got = append(got, pesHeader.Data()...) pesPacket = pesPacket[:0] @@ -130,8 +133,8 @@ func TestEncodePcm(t *testing.T) { } } - // Compare encoded data with original data. + // Compare data from MTS with original data. if !bytes.Equal(got, inPcm) { - t.Error("Error, unexpected output") + t.Error("data decoded from mts did not match input data") } } diff --git a/container/mts/encoder.go b/container/mts/encoder.go index 34cfc8de..6ccde63e 100644 --- a/container/mts/encoder.go +++ b/container/mts/encoder.go @@ -2,9 +2,6 @@ NAME encoder.go -DESCRIPTION - See Readme.md - AUTHOR Dan Kortschak Saxon Nelson-Milton