stream/mts: started writing metaEncode_test.go file

This commit is contained in:
saxon 2019-01-30 13:23:07 +10:30
parent 83ac98fe84
commit 7dd1ce99e1
3 changed files with 36 additions and 12 deletions

View File

@ -88,7 +88,11 @@ const (
) )
// global Meta // global Meta
var meta Meta var Meta *Metadata
func init() {
Meta = NewMeta()
}
var ( var (
patTable = standardPat.Bytes() patTable = standardPat.Bytes()
@ -252,7 +256,7 @@ func (e *Encoder) ccFor(pid int) byte {
func updateMeta(b []byte) error { func updateMeta(b []byte) error {
var p psi.PSIBytes var p psi.PSIBytes
p = b p = b
m := meta.Encode() m := Meta.Encode()
p.AddDescriptor(psi.MetadataTag, m) p.AddDescriptor(psi.MetadataTag, m)
return nil return nil
} }

View File

@ -48,14 +48,14 @@ var (
errKeyAbsent = errors.New("Key does not exist in map") errKeyAbsent = errors.New("Key does not exist in map")
) )
type Meta struct { type Metadata struct {
mu sync.RWMutex mu sync.RWMutex
data map[string]string data map[string]string
enc []byte enc []byte
} }
func NewMeta() *Meta { func NewMeta() *Metadata {
return &Meta{ return &Metadata{
data: make(map[string]string), data: make(map[string]string),
enc: []byte{ enc: []byte{
0x00, // Reserved byte 0x00, // Reserved byte
@ -67,14 +67,14 @@ func NewMeta() *Meta {
} }
// Add adds metadata with key and val, if already exists return error // 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.mu.Lock()
m.data[key] = val m.data[key] = val
m.mu.Unlock() m.mu.Unlock()
} }
// All returns the a copy of the map containing the meta data // 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() m.mu.Lock()
cpy := make(map[string]string) cpy := make(map[string]string)
for k, v := range m.data { 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 // 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() m.mu.Lock()
val, ok := m.data[key] val, ok := m.data[key]
m.mu.Unlock() 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 doesnt exist // Remove deletes a meta entry in the map and returns error if it doesnt exist
func (m *Meta) Delete(key string) error { func (m *Metadata) Delete(key string) error {
m.mu.Lock() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
if _, ok := m.data[key]; ok { 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 // 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. // 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] m.enc = m.enc[:headSize]
// Iterate over map and append entries, only adding tab if we're not on the last entry // Iterate over map and append entries, only adding tab if we're not on the last entry

View File

@ -27,8 +27,28 @@ LICENSE
package mts package mts
import "testing" import (
"bytes"
"testing"
"bitbucket.org/ausocean/av/stream/mts/psi"
)
const fps = 25
func TestMetaEncode(t *testing.T) { 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,
}
} }