From 795157577139d0fc37d43ed0eae78bf933a4257b Mon Sep 17 00:00:00 2001 From: saxon Date: Fri, 8 Feb 2019 18:00:23 +1030 Subject: [PATCH] stream/mts: undo changes to encoder.go stream/rtp/encoder.go: undoing changes --- stream/mts/encoder.go | 118 +++++++++--------------------------------- stream/rtp/encoder.go | 4 +- 2 files changed, 26 insertions(+), 96 deletions(-) diff --git a/stream/mts/encoder.go b/stream/mts/encoder.go index 02761b91..a309eaf9 100644 --- a/stream/mts/encoder.go +++ b/stream/mts/encoder.go @@ -30,9 +30,9 @@ package mts import ( "io" - "sync" "time" + "bitbucket.org/ausocean/av/stream/mts/meta" "bitbucket.org/ausocean/av/stream/mts/pes" "bitbucket.org/ausocean/av/stream/mts/psi" ) @@ -82,93 +82,21 @@ var ( }, }, } - - // standardPmtTimeLocation is a standard PMT with time and location - // descriptors, but time and location fields zeroed out. - standardPmtTimeLocation = psi.PSI{ - Pf: 0x00, - Tid: 0x02, - Ssi: true, - Sl: 0x3e, - Tss: &psi.TSS{ - Tide: 0x01, - V: 0, - Cni: true, - Sn: 0, - Lsn: 0, - Sd: &psi.PMT{ - Pcrpid: 0x0100, - Pil: psi.PmtTimeLocationPil, - Pd: []psi.Desc{ - { - Dt: psi.TimeDescTag, - Dl: psi.TimeDataSize, - Dd: make([]byte, psi.TimeDataSize), - }, - { - Dt: psi.LocationDescTag, - Dl: psi.LocationDataSize, - Dd: make([]byte, psi.LocationDataSize), - }, - }, - Essd: &psi.ESSD{ - St: 0x1b, - Epid: 0x0100, - Esil: 0x00, - }, - }, - }, - } ) const ( psiInterval = 1 * time.Second ) -// timeLocation holds time and location data -type timeLocation struct { - mu sync.RWMutex - time uint64 - location string -} - -// SetTimeStamp sets the time field of a TimeLocation. -func (tl *timeLocation) SetTimeStamp(t uint64) { - tl.mu.Lock() - tl.time = t - tl.mu.Unlock() -} - -// GetTimeStamp returns the location of a TimeLocation. -func (tl *timeLocation) TimeStamp() uint64 { - tl.mu.RLock() - t := tl.time - tl.mu.RUnlock() - return t -} - -// SetLocation sets the location of a TimeLocation. -func (tl *timeLocation) SetLocation(l string) { - tl.mu.Lock() - tl.location = l - tl.mu.Unlock() -} - -// GetLocation returns the location of a TimeLocation. -func (tl *timeLocation) Location() string { - tl.mu.RLock() - l := tl.location - tl.mu.RUnlock() - return l -} - -// 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 +// 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 var ( patTable = standardPat.Bytes() - pmtTable = standardPmtTimeLocation.Bytes() + pmtTable = standardPmt.Bytes() ) const ( @@ -288,33 +216,27 @@ func (e *Encoder) writePSI() error { // Write PAT. patPkt := Packet{ PUSI: true, - PID: patPid, - CC: e.ccFor(patPid), - AFC: hasPayload, - Payload: patTable, + PID: PatPid, + CC: e.ccFor(PatPid), + AFC: HasPayload, + Payload: psi.AddPadding(patTable), } _, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize])) if err != nil { return err } - - // Update pmt table time and location. - err = psi.UpdateTime(pmtTable, MetaData.TimeStamp()) + pmtTable, err = updateMeta(pmtTable) if err != nil { return err } - err = psi.UpdateLocation(pmtTable, MetaData.Location()) - if err != nil { - return nil - } // Create mts packet from pmt table. pmtPkt := Packet{ PUSI: true, - PID: pmtPid, - CC: e.ccFor(pmtPid), - AFC: hasPayload, - Payload: pmtTable, + PID: PmtPid, + CC: e.ccFor(PmtPid), + AFC: HasPayload, + Payload: psi.AddPadding(pmtTable), } _, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize])) if err != nil { @@ -345,3 +267,11 @@ func (e *Encoder) ccFor(pid int) byte { e.continuity[pid] = (cc + 1) & continuityCounterMask return cc } + +// updateMeta adds/updates a metaData descriptor in the given psi bytes using data +// contained in the global Meta struct. +func updateMeta(b []byte) ([]byte, error) { + p := psi.PSIBytes(b) + err := p.AddDescriptor(psi.MetadataTag, Meta.Encode()) + return []byte(p), err +} diff --git a/stream/rtp/encoder.go b/stream/rtp/encoder.go index 329a24c0..20df9434 100644 --- a/stream/rtp/encoder.go +++ b/stream/rtp/encoder.go @@ -73,8 +73,8 @@ func NewEncoder(dst io.Writer, fps int) *Encoder { func (e *Encoder) Write(data []byte) (int, error) { e.buffer = append(e.buffer, data...) for len(e.buffer) >= sendLen { - e.Encode(e.buffer) - e.buffer = e.buffer[:0] + e.Encode(e.buffer[:sendLen]) + e.buffer = e.buffer[sendLen:] } return len(data), nil }