diff --git a/stream/mts/encoder.go b/stream/mts/encoder.go index 33194ab8..4567aea2 100644 --- a/stream/mts/encoder.go +++ b/stream/mts/encoder.go @@ -88,7 +88,11 @@ const ( ) // global Meta -var meta Meta +var Meta *Metadata + +func init() { + Meta = NewMeta() +} var ( patTable = standardPat.Bytes() @@ -252,7 +256,7 @@ func (e *Encoder) ccFor(pid int) byte { func updateMeta(b []byte) error { var p psi.PSIBytes p = b - m := meta.Encode() + m := Meta.Encode() p.AddDescriptor(psi.MetadataTag, m) return nil } diff --git a/stream/mts/meta.go b/stream/mts/meta.go index 465a537e..62097d30 100644 --- a/stream/mts/meta.go +++ b/stream/mts/meta.go @@ -48,14 +48,14 @@ var ( errKeyAbsent = errors.New("Key does not exist in map") ) -type Meta struct { +type Metadata struct { mu sync.RWMutex data map[string]string enc []byte } -func NewMeta() *Meta { - return &Meta{ +func NewMeta() *Metadata { + return &Metadata{ data: make(map[string]string), enc: []byte{ 0x00, // Reserved byte @@ -67,14 +67,14 @@ func NewMeta() *Meta { } // Add adds metadata with key and val, if already exists return error -func (m *Meta) Add(key, val string) { +func (m *Metadata) Add(key, val string) { m.mu.Lock() m.data[key] = val m.mu.Unlock() } // All returns the a copy of the map containing the meta data -func (m *Meta) All() map[string]string { +func (m *Metadata) All() map[string]string { m.mu.Lock() cpy := make(map[string]string) for k, v := range m.data { @@ -85,7 +85,7 @@ func (m *Meta) All() map[string]string { } // Get returns the meta data for the passed key -func (m *Meta) Get(key string) (string, error) { +func (m *Metadata) Get(key string) (string, error) { m.mu.Lock() val, ok := m.data[key] m.mu.Unlock() @@ -97,7 +97,7 @@ func (m *Meta) Get(key string) (string, error) { } // Remove deletes a meta entry in the map and returns error if it doesn’t exist -func (m *Meta) Delete(key string) error { +func (m *Metadata) Delete(key string) error { m.mu.Lock() defer m.mu.Unlock() if _, ok := m.data[key]; ok { @@ -109,7 +109,7 @@ func (m *Meta) Delete(key string) error { // Encode takes the meta data map and encods into a byte slice with header // describing the version, length of data and data in TSV format. -func (m *Meta) Encode() []byte { +func (m *Metadata) Encode() []byte { m.enc = m.enc[:headSize] // Iterate over map and append entries, only adding tab if we're not on the last entry diff --git a/stream/mts/metaEncode_test.go b/stream/mts/metaEncode_test.go index d422c700..849f5c0d 100644 --- a/stream/mts/metaEncode_test.go +++ b/stream/mts/metaEncode_test.go @@ -27,8 +27,28 @@ LICENSE package mts -import "testing" +import ( + "bytes" + "testing" + + "bitbucket.org/ausocean/av/stream/mts/psi" +) + +const fps = 25 func TestMetaEncode(t *testing.T) { - + var b []byte + buf := bytes.NewBuffer(b) + e := NewEncoder(buf, fps) + Meta.Add("ts", "12345678") + e.writePSI() + out := buf.Bytes() + got := out[PacketSize:] + want := []byte{ + 0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a, + psi.MetadataTag, // Descriptor tag + 0x08, // Length of bytes to follow + 0x00, 0x00, 0x00, 0x00, 0x49, 0xa2, 0x36, 0x0b, // timestamp + 0x1b, 0xe1, 0x00, 0xf0, 0x00, + } }