From 6cb56421d3944019d7714349d062ff6ea77068e4 Mon Sep 17 00:00:00 2001 From: saxon Date: Tue, 8 Jan 2019 20:01:30 +1030 Subject: [PATCH] mts: add getters and setters with mutex to the TimeLocation struct. --- stream/mts/encoder.go | 48 +++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/stream/mts/encoder.go b/stream/mts/encoder.go index 172a56e6..9e0bcc6f 100644 --- a/stream/mts/encoder.go +++ b/stream/mts/encoder.go @@ -30,6 +30,7 @@ package mts import ( "io" + "sync" "time" "bitbucket.org/ausocean/av/stream/mts/pes" @@ -124,21 +125,46 @@ const ( psiSendCount = 7 ) -type TimeLocation struct { - Time uint64 - Location string +// TimeLocation can hold time and location data for the insertion into mpegts +// packets. +type timeLocation struct { + sync.RWMutex + time uint64 + location string } -var metaData = TimeLocation{} - -func SetTimeStamp(t uint64) { - metaData.Time = t +// SetTimeStamp sets the time field of a TimeLocation. +func (tl *timeLocation) SetTimeStamp(t uint64) { + tl.Lock() + tl.time = t + tl.Unlock() } -func SetLocation(g string) { - metaData.Location = g +// GetTimeStamp returns the location of a TimeLocation. +func (tl *timeLocation) GetTimeStamp() uint64 { + tl.RLock() + defer tl.RUnlock() + return tl.time } +// SetLocation sets the location of a TimeLocation. +func (tl *timeLocation) SetLocation(l string) { + tl.Lock() + tl.location = l + tl.Unlock() +} + +// GetLocation returns the location of a TimeLocation. +func (tl *timeLocation) GetLocation() string { + tl.RLock() + defer tl.RUnlock() + return tl.location +} + +// 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 = standardPmtTimeLocation.Bytes() @@ -269,11 +295,11 @@ func (e *Encoder) writePSI() error { } // Update pmt table time and location. - err = psi.UpdateTime(pmtTable, metaData.Time) + err = psi.UpdateTime(pmtTable, MetaData.GetTimeStamp()) if err != nil { return err } - err = psi.UpdateLocation(pmtTable, metaData.Location) + err = psi.UpdateLocation(pmtTable, MetaData.GetLocation()) if err != nil { return nil }