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
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
}

View File

@ -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 doesnt 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

View File

@ -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,
}
}