stream/mts/encoder.go: implemented metadata receiver functions: Add, Get, All and Delete

This commit is contained in:
saxon 2019-01-26 21:53:19 +10:30
parent d107231224
commit 87ded6bf2e
1 changed files with 39 additions and 31 deletions

View File

@ -29,6 +29,8 @@ LICENSE
package mts
import (
"errors"
"fmt"
"io"
"sync"
"time"
@ -88,43 +90,53 @@ const (
psiSndCnt = 7
)
// timeLocation holds time and location data
type timeLocation struct {
// global Meta
var meta Meta
type Meta struct {
mu sync.RWMutex
time uint64
location string
data map[string]string
}
// SetTimeStamp sets the time field of a TimeLocation.
func (tl *timeLocation) SetTimeStamp(t uint64) {
tl.mu.Lock()
tl.time = t
tl.mu.Unlock()
// Add adds metadata with key and val, if already exists return error
func (m *Meta) Add(key, val string) error {
m.mu.Lock()
if _, exists := m.data[key]; !exists {
return errors.New(fmt.Sprintf("Metadata for: %v already exists", key))
}
m.data[key] = val
m.mu.Unlock()
return nil
}
// GetTimeStamp returns the location of a TimeLocation.
func (tl *timeLocation) TimeStamp() uint64 {
tl.mu.RLock()
t := tl.time
tl.mu.RUnlock()
return t
// All returns the a copy of the map containing the meta data
func (m *Meta) All() map[string]string {
var cpy map[string]string
for k, v := range m.data {
cpy[k] = v
}
return cpy
}
// SetLocation sets the location of a TimeLocation.
func (tl *timeLocation) SetLocation(l string) {
tl.mu.Lock()
tl.location = l
tl.mu.Unlock()
// Get returns the meta data for the passed key
func (m *Meta) Get(key string) (string, error) {
val, ok := m.data[key]
if !ok {
return "", errors.New("Key does not exist in metadata map")
}
return val, nil
}
// GetLocation returns the location of a TimeLocation.
func (tl *timeLocation) Location() string {
tl.mu.RLock()
l := tl.location
tl.mu.RUnlock()
return l
// Remove deletes a meta entry in the map and returns error if it doesnt exist
func (m *Meta) Delete(key string) error {
if _, ok := m.data[key]; ok {
delete(m.data, key)
return nil
}
return errors.New("Trying to delete map entry that doesn't exist")
}
// updateMeta ...
func updateMeta(b []byte) error {
var p psi.PSIBytes
p = b
@ -136,10 +148,6 @@ func updateMeta(b []byte) error {
return nil
}
// MetData will hold time and location data which may be set externally if
// this data is available. It is then inserted into mpegts packets outputted.
var MetaData timeLocation
var (
patTable = standardPat.Bytes()
pmtTable = standardPmt.Bytes()