diff --git a/container/mts/encoder_test.go b/container/mts/encoder_test.go index 24fb823d..fe85a396 100644 --- a/container/mts/encoder_test.go +++ b/container/mts/encoder_test.go @@ -29,12 +29,14 @@ import ( "bytes" "io" "io/ioutil" + "reflect" "testing" "github.com/Comcast/gots/packet" "github.com/Comcast/gots/pes" "bitbucket.org/ausocean/av/container/mts/meta" + "bitbucket.org/ausocean/av/container/mts/psi" ) type nopCloser struct{ io.Writer } @@ -250,3 +252,92 @@ func TestEncodePcm(t *testing.T) { t.Error("data decoded from mts did not match input data") } } + +const ( + errNotExpectedOut = "Unexpected output. \n Got : %v\n, Want: %v\n" + errUnexpectedErr = "Unexpected error: %v\n" +) + +const fps = 25 + +// TestMetaEncode1 checks that we can externally add a single metadata entry to +// the mts global Meta meta.Data struct and then successfully have the mts encoder +// write this to psi. +func TestMetaEncode1(t *testing.T) { + Meta = meta.New() + var buf bytes.Buffer + e := NewEncoder(nopCloser{&buf}, fps, EncodeH264) + Meta.Add("ts", "12345678") + if err := e.writePSI(); err != nil { + t.Errorf(errUnexpectedErr, err.Error()) + } + out := buf.Bytes() + got := out[PacketSize+4:] + + want := []byte{ + 0x00, 0x02, 0xb0, 0x23, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x11, + psi.MetadataTag, // Descriptor tag + 0x0f, // Length of bytes to follow + 0x00, 0x10, 0x00, 0x0b, 't', 's', '=', '1', '2', '3', '4', '5', '6', '7', '8', // timestamp + 0x1b, 0xe1, 0x00, 0xf0, 0x00, + } + want = psi.AddCrc(want) + want = psi.AddPadding(want) + if !bytes.Equal(got, want) { + t.Errorf(errNotExpectedOut, got, want) + } +} + +// TestMetaEncode2 checks that we can externally add two metadata entries to the +// Meta meta.Data global and then have the mts encoder successfully encode this +// into psi. +func TestMetaEncode2(t *testing.T) { + Meta = meta.New() + var buf bytes.Buffer + e := NewEncoder(nopCloser{&buf}, fps, EncodeH264) + Meta.Add("ts", "12345678") + Meta.Add("loc", "1234,4321,1234") + if err := e.writePSI(); err != nil { + t.Errorf(errUnexpectedErr, err.Error()) + } + out := buf.Bytes() + got := out[PacketSize+4:] + want := []byte{ + 0x00, 0x02, 0xb0, 0x36, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x24, + psi.MetadataTag, // Descriptor tag + 0x22, // Length of bytes to follow + 0x00, 0x10, 0x00, 0x1e, 't', 's', '=', '1', '2', '3', '4', '5', '6', '7', '8', '\t', // timestamp + 'l', 'o', 'c', '=', '1', '2', '3', '4', ',', '4', '3', '2', '1', ',', '1', '2', '3', '4', // location + 0x1b, 0xe1, 0x00, 0xf0, 0x00, + } + want = psi.AddCrc(want) + want = psi.AddPadding(want) + if !bytes.Equal(got, want) { + t.Errorf(errNotExpectedOut, got, want) + } +} + +// TestExtractMeta checks that ExtractMeta can correclty get a map of metadata +// from the first PMT in a clip of MPEG-TS. +func TestExtractMeta(t *testing.T) { + Meta = meta.New() + var buf bytes.Buffer + e := NewEncoder(nopCloser{&buf}, fps, EncodeH264) + Meta.Add("ts", "12345678") + Meta.Add("loc", "1234,4321,1234") + if err := e.writePSI(); err != nil { + t.Errorf(errUnexpectedErr, err.Error()) + } + out := buf.Bytes() + got, err := ExtractMeta(out) + if err != nil { + t.Errorf(errUnexpectedErr, err.Error()) + } + want := map[string]string{ + "ts": "12345678", + "loc": "1234,4321,1234", + } + if !reflect.DeepEqual(got, want) { + t.Errorf(errNotExpectedOut, got, want) + } +} diff --git a/container/mts/metaEncode_test.go b/container/mts/metaEncode_test.go deleted file mode 100644 index 3a0dbd62..00000000 --- a/container/mts/metaEncode_test.go +++ /dev/null @@ -1,126 +0,0 @@ -/* -NAME - metaEncode_test.go - -DESCRIPTION - See Readme.md - -AUTHOR - Saxon Nelson-Milton - -LICENSE - metaEncode_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 - along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. -*/ - -package mts - -import ( - "bytes" - "reflect" - "testing" - - "bitbucket.org/ausocean/av/container/mts/meta" - "bitbucket.org/ausocean/av/container/mts/psi" -) - -const ( - errNotExpectedOut = "Unexpected output. \n Got : %v\n, Want: %v\n" - errUnexpectedErr = "Unexpected error: %v\n" -) - -const fps = 25 - -// TestMetaEncode1 checks that we can externally add a single metadata entry to -// the mts global Meta meta.Data struct and then successfully have the mts encoder -// write this to psi. -func TestMetaEncode1(t *testing.T) { - Meta = meta.New() - var buf bytes.Buffer - e := NewEncoder(nopCloser{&buf}, fps, EncodeH264) - Meta.Add("ts", "12345678") - if err := e.writePSI(); err != nil { - t.Errorf(errUnexpectedErr, err.Error()) - } - out := buf.Bytes() - got := out[PacketSize+4:] - - want := []byte{ - 0x00, 0x02, 0xb0, 0x23, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x11, - psi.MetadataTag, // Descriptor tag - 0x0f, // Length of bytes to follow - 0x00, 0x10, 0x00, 0x0b, 't', 's', '=', '1', '2', '3', '4', '5', '6', '7', '8', // timestamp - 0x1b, 0xe1, 0x00, 0xf0, 0x00, - } - want = psi.AddCrc(want) - want = psi.AddPadding(want) - if !bytes.Equal(got, want) { - t.Errorf(errNotExpectedOut, got, want) - } -} - -// TestMetaEncode2 checks that we can externally add two metadata entries to the -// Meta meta.Data global and then have the mts encoder successfully encode this -// into psi. -func TestMetaEncode2(t *testing.T) { - Meta = meta.New() - var buf bytes.Buffer - e := NewEncoder(nopCloser{&buf}, fps, EncodeH264) - Meta.Add("ts", "12345678") - Meta.Add("loc", "1234,4321,1234") - if err := e.writePSI(); err != nil { - t.Errorf(errUnexpectedErr, err.Error()) - } - out := buf.Bytes() - got := out[PacketSize+4:] - want := []byte{ - 0x00, 0x02, 0xb0, 0x36, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x24, - psi.MetadataTag, // Descriptor tag - 0x22, // Length of bytes to follow - 0x00, 0x10, 0x00, 0x1e, 't', 's', '=', '1', '2', '3', '4', '5', '6', '7', '8', '\t', // timestamp - 'l', 'o', 'c', '=', '1', '2', '3', '4', ',', '4', '3', '2', '1', ',', '1', '2', '3', '4', // location - 0x1b, 0xe1, 0x00, 0xf0, 0x00, - } - want = psi.AddCrc(want) - want = psi.AddPadding(want) - if !bytes.Equal(got, want) { - t.Errorf(errNotExpectedOut, got, want) - } -} - -// TestExtractMeta checks that ExtractMeta can correclty get a map of metadata -// from the first PMT in a clip of MPEG-TS. -func TestExtractMeta(t *testing.T) { - Meta = meta.New() - var buf bytes.Buffer - e := NewEncoder(nopCloser{&buf}, fps, EncodeH264) - Meta.Add("ts", "12345678") - Meta.Add("loc", "1234,4321,1234") - if err := e.writePSI(); err != nil { - t.Errorf(errUnexpectedErr, err.Error()) - } - out := buf.Bytes() - got, err := ExtractMeta(out) - if err != nil { - t.Errorf(errUnexpectedErr, err.Error()) - } - want := map[string]string{ - "ts": "12345678", - "loc": "1234,4321,1234", - } - if !reflect.DeepEqual(got, want) { - t.Errorf(errNotExpectedOut, got, want) - } -}