mirror of https://bitbucket.org/ausocean/av.git
container/mts: using RealTime type from utils package instead of global vars with mutator functions
This commit is contained in:
parent
24e9ed69ca
commit
e57e14678a
|
@ -29,7 +29,6 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"bitbucket.org/ausocean/av/codec/h264"
|
||||
|
@ -37,8 +36,43 @@ import (
|
|||
"bitbucket.org/ausocean/av/container/mts/meta"
|
||||
"bitbucket.org/ausocean/av/container/mts/pes"
|
||||
"bitbucket.org/ausocean/av/container/mts/psi"
|
||||
"bitbucket.org/ausocean/utils/realtime"
|
||||
)
|
||||
|
||||
// Media type values.
|
||||
// TODO: reference relevant specifications.
|
||||
const (
|
||||
H264ID = 27
|
||||
H265ID = 36
|
||||
audioStreamID = 0xc0 // First audio stream ID.
|
||||
)
|
||||
|
||||
// Constants used to communicate which media codec will be packetized.
|
||||
const (
|
||||
EncodeH264 = iota
|
||||
EncodeH265
|
||||
EncodeAudio
|
||||
)
|
||||
|
||||
// Time-related constants.
|
||||
const (
|
||||
// ptsOffset is the offset added to the clock to determine
|
||||
// the current presentation timestamp.
|
||||
ptsOffset = 700 * time.Millisecond
|
||||
|
||||
// PCRFrequency is the base Program Clock Reference frequency in Hz.
|
||||
PCRFrequency = 90000
|
||||
|
||||
// PTSFrequency is the presentation timestamp frequency in Hz.
|
||||
PTSFrequency = 90000
|
||||
|
||||
// MaxPTS is the largest PTS value (i.e., for a 33-bit unsigned integer).
|
||||
MaxPTS = (1 << 33) - 1
|
||||
)
|
||||
|
||||
// If we are not using NAL based PSI intervals then we will send PSI every 7 packets.
|
||||
const psiSendCount = 7
|
||||
|
||||
// Some common manifestations of PSI.
|
||||
var (
|
||||
// StandardPAT is a minimal PAT.
|
||||
|
@ -77,87 +111,20 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
const (
|
||||
psiInterval = 1 * time.Second
|
||||
psiSendCount = 7
|
||||
)
|
||||
|
||||
// Meta allows addition of metadata to encoded mts from outside of this pkg.
|
||||
// See meta pkg for usage.
|
||||
//
|
||||
// TODO: make this not global.
|
||||
var Meta *meta.Data
|
||||
|
||||
// This will help us obtain a realtime for timestamp meta encoding.
|
||||
var RealTime = realtime.NewRealTime()
|
||||
|
||||
var (
|
||||
patTable = StandardPAT.Bytes()
|
||||
pmtTable []byte
|
||||
)
|
||||
|
||||
const (
|
||||
H264ID = 27
|
||||
H265ID = 36
|
||||
audioStreamID = 0xc0 // First audio stream ID.
|
||||
)
|
||||
|
||||
// Constants used to communicate which media codec will be packetized.
|
||||
const (
|
||||
EncodeH264 = iota
|
||||
EncodeH265
|
||||
EncodeAudio
|
||||
)
|
||||
|
||||
// Time-related constants.
|
||||
const (
|
||||
// ptsOffset is the offset added to the clock to determine
|
||||
// the current presentation timestamp.
|
||||
ptsOffset = 700 * time.Millisecond
|
||||
|
||||
// PCRFrequency is the base Program Clock Reference frequency in Hz.
|
||||
PCRFrequency = 90000
|
||||
|
||||
// PTSFrequency is the presentation timestamp frequency in Hz.
|
||||
PTSFrequency = 90000
|
||||
|
||||
// MaxPTS is the largest PTS value (i.e., for a 33-bit unsigned integer).
|
||||
MaxPTS = (1 << 33) - 1
|
||||
)
|
||||
|
||||
// Globals for use in keeping real time.
|
||||
var (
|
||||
realRefTime time.Time // Holds a reference real time given to SetTime.
|
||||
sysRefTime time.Time // Holds a system reference time set when realRefTime is obtained.
|
||||
timeIsSet bool // Indicates if the time has been set.
|
||||
mu = sync.Mutex{} // Used when accessing/mutating above time vars.
|
||||
)
|
||||
|
||||
// SetTime allows setting of current time. This is useful if the system running
|
||||
// this encoder does not have time keeping. The user may wish to obtain an
|
||||
// accurate time from an NTP server or local machine and pass to this function.
|
||||
func SetTime(t time.Time) {
|
||||
mu.Lock()
|
||||
realRefTime = t
|
||||
sysRefTime = time.Now()
|
||||
timeIsSet = true
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
// Time provides either a real time that has been calculated from a reference
|
||||
// set by SetTime, or using the current system time.
|
||||
func Time() time.Time {
|
||||
mu.Lock()
|
||||
t := realRefTime.Add(time.Now().Sub(sysRefTime))
|
||||
mu.Unlock()
|
||||
return t
|
||||
}
|
||||
|
||||
// TimeIsSet returns true if SetTime has been used to set a real reference time.
|
||||
func TimeIsSet() bool {
|
||||
mu.Lock()
|
||||
b := timeIsSet
|
||||
mu.Unlock()
|
||||
return b
|
||||
}
|
||||
|
||||
// Encoder encapsulates properties of an MPEG-TS generator.
|
||||
type Encoder struct {
|
||||
dst io.WriteCloser
|
||||
|
@ -373,8 +340,8 @@ func (e *Encoder) ccFor(pid int) byte {
|
|||
// contained in the global Meta struct.
|
||||
func updateMeta(b []byte) ([]byte, error) {
|
||||
p := psi.PSIBytes(b)
|
||||
if TimeIsSet() {
|
||||
Meta.Add("ts", strconv.Itoa(int(Time().Unix())))
|
||||
if RealTime.IsSet() {
|
||||
Meta.Add("ts", strconv.Itoa(int(RealTime.Get().Unix())))
|
||||
}
|
||||
err := p.AddDescriptor(psi.MetadataTag, Meta.Encode())
|
||||
return []byte(p), err
|
||||
|
|
|
@ -118,7 +118,7 @@ func extractMeta(r string, log func(lvl int8, msg string, args ...interface{}))
|
|||
log(logger.Warning, pkg+"No timestamp in reply")
|
||||
} else {
|
||||
log(logger.Debug, fmt.Sprintf("%v got timestamp: %v", pkg, t))
|
||||
mts.SetTime(time.Unix(int64(t), 0))
|
||||
mts.RealTime.Set(time.Unix(int64(t), 0))
|
||||
}
|
||||
|
||||
// Extract location from reply
|
||||
|
|
Loading…
Reference in New Issue