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 package mts
import ( import (
"errors"
"fmt"
"io" "io"
"sync" "sync"
"time" "time"
@ -88,43 +90,53 @@ const (
psiSndCnt = 7 psiSndCnt = 7
) )
// timeLocation holds time and location data // global Meta
type timeLocation struct { var meta Meta
mu sync.RWMutex
time uint64 type Meta struct {
location string mu sync.RWMutex
data map[string]string
} }
// SetTimeStamp sets the time field of a TimeLocation. // Add adds metadata with key and val, if already exists return error
func (tl *timeLocation) SetTimeStamp(t uint64) { func (m *Meta) Add(key, val string) error {
tl.mu.Lock() m.mu.Lock()
tl.time = t if _, exists := m.data[key]; !exists {
tl.mu.Unlock() 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. // All returns the a copy of the map containing the meta data
func (tl *timeLocation) TimeStamp() uint64 { func (m *Meta) All() map[string]string {
tl.mu.RLock() var cpy map[string]string
t := tl.time for k, v := range m.data {
tl.mu.RUnlock() cpy[k] = v
return t }
return cpy
} }
// SetLocation sets the location of a TimeLocation. // Get returns the meta data for the passed key
func (tl *timeLocation) SetLocation(l string) { func (m *Meta) Get(key string) (string, error) {
tl.mu.Lock() val, ok := m.data[key]
tl.location = l if !ok {
tl.mu.Unlock() return "", errors.New("Key does not exist in metadata map")
}
return val, nil
} }
// GetLocation returns the location of a TimeLocation. // Remove deletes a meta entry in the map and returns error if it doesnt exist
func (tl *timeLocation) Location() string { func (m *Meta) Delete(key string) error {
tl.mu.RLock() if _, ok := m.data[key]; ok {
l := tl.location delete(m.data, key)
tl.mu.RUnlock() return nil
return l }
return errors.New("Trying to delete map entry that doesn't exist")
} }
// updateMeta ...
func updateMeta(b []byte) error { func updateMeta(b []byte) error {
var p psi.PSIBytes var p psi.PSIBytes
p = b p = b
@ -136,10 +148,6 @@ func updateMeta(b []byte) error {
return nil 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 ( var (
patTable = standardPat.Bytes() patTable = standardPat.Bytes()
pmtTable = standardPmt.Bytes() pmtTable = standardPmt.Bytes()