2018-02-13 11:39:26 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2018-08-19 14:09:57 +03:00
|
|
|
encoder.go
|
2018-02-13 11:39:26 +03:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
2018-08-18 04:57:36 +03:00
|
|
|
Dan Kortschak <dan@ausocean.org>
|
2018-02-28 16:46:59 +03:00
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
2018-02-13 11:39:26 +03:00
|
|
|
|
|
|
|
LICENSE
|
2018-08-19 14:09:57 +03:00
|
|
|
encoder.go is Copyright (C) 2017-2018 the Australian Ocean Lab (AusOcean)
|
2018-02-13 11:39:26 +03:00
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-08-18 04:57:36 +03:00
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
2018-02-13 11:39:26 +03:00
|
|
|
*/
|
2018-08-18 04:57:36 +03:00
|
|
|
|
2018-08-19 13:59:22 +03:00
|
|
|
package mts
|
2017-12-30 11:31:50 +03:00
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
import (
|
2018-10-19 03:50:08 +03:00
|
|
|
"io"
|
2019-01-08 12:31:30 +03:00
|
|
|
"sync"
|
2018-08-18 06:51:50 +03:00
|
|
|
"time"
|
|
|
|
|
2018-08-20 01:19:02 +03:00
|
|
|
"bitbucket.org/ausocean/av/stream/mts/pes"
|
2018-12-10 10:14:56 +03:00
|
|
|
"bitbucket.org/ausocean/av/stream/mts/psi"
|
|
|
|
)
|
|
|
|
|
2019-01-07 09:43:50 +03:00
|
|
|
// Some common manifestations of PSI
|
|
|
|
var (
|
2019-01-10 03:42:33 +03:00
|
|
|
// standardPat is a minimal PAT.
|
2019-01-08 12:16:24 +03:00
|
|
|
standardPat = psi.PSI{
|
2019-01-07 09:43:50 +03:00
|
|
|
Pf: 0x00,
|
|
|
|
Tid: 0x00,
|
|
|
|
Ssi: true,
|
|
|
|
Pb: false,
|
|
|
|
Sl: 0x0d,
|
|
|
|
Tss: &psi.TSS{
|
|
|
|
Tide: 0x01,
|
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
Sd: &psi.PAT{
|
|
|
|
Pn: 0x01,
|
|
|
|
Pmpid: 0x1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-10 03:42:33 +03:00
|
|
|
// standardPmt is a minimal PMT without descriptors for time and location.
|
2019-01-08 12:16:24 +03:00
|
|
|
standardPmt = psi.PSI{
|
2019-01-07 09:43:50 +03:00
|
|
|
Pf: 0x00,
|
|
|
|
Tid: 0x02,
|
|
|
|
Ssi: true,
|
|
|
|
Sl: 0x12,
|
|
|
|
Tss: &psi.TSS{
|
|
|
|
Tide: 0x01,
|
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
Sd: &psi.PMT{
|
2019-01-08 12:14:33 +03:00
|
|
|
Pcrpid: 0x0100,
|
2019-01-07 09:43:50 +03:00
|
|
|
Pil: 0,
|
|
|
|
Essd: &psi.ESSD{
|
|
|
|
St: 0x1b,
|
|
|
|
Epid: 0x0100,
|
|
|
|
Esil: 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-10 03:42:33 +03:00
|
|
|
// standardPmtTimeLocation is a minimal pmt with descriptors for time and location.
|
2019-01-08 12:16:24 +03:00
|
|
|
standardPmtTimeLocation = psi.PSI{
|
2019-01-07 09:43:50 +03:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-11-20 17:21:57 +03:00
|
|
|
const (
|
|
|
|
psiPacketSize = 184
|
2018-12-14 12:35:10 +03:00
|
|
|
psiSendCount = 7
|
2018-11-20 17:21:57 +03:00
|
|
|
)
|
2018-04-17 08:20:23 +03:00
|
|
|
|
2019-01-09 01:42:46 +03:00
|
|
|
// timeLocation holds time and location data
|
2019-01-08 12:31:30 +03:00
|
|
|
type timeLocation struct {
|
2019-01-10 09:56:48 +03:00
|
|
|
mu sync.RWMutex
|
2019-01-08 12:31:30 +03:00
|
|
|
time uint64
|
|
|
|
location string
|
2018-08-19 03:29:33 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 12:31:30 +03:00
|
|
|
// SetTimeStamp sets the time field of a TimeLocation.
|
|
|
|
func (tl *timeLocation) SetTimeStamp(t uint64) {
|
2019-01-10 09:56:48 +03:00
|
|
|
tl.mu.Lock()
|
2019-01-08 12:31:30 +03:00
|
|
|
tl.time = t
|
2019-01-10 09:56:48 +03:00
|
|
|
tl.mu.Unlock()
|
2019-01-08 12:31:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTimeStamp returns the location of a TimeLocation.
|
2019-01-09 01:45:08 +03:00
|
|
|
func (tl *timeLocation) TimeStamp() uint64 {
|
2019-01-10 09:56:48 +03:00
|
|
|
tl.mu.RLock()
|
|
|
|
t := tl.time
|
|
|
|
tl.mu.RUnlock()
|
|
|
|
return t
|
2019-01-08 12:31:30 +03:00
|
|
|
}
|
2018-12-13 04:52:06 +03:00
|
|
|
|
2019-01-08 12:31:30 +03:00
|
|
|
// SetLocation sets the location of a TimeLocation.
|
|
|
|
func (tl *timeLocation) SetLocation(l string) {
|
2019-01-10 09:56:48 +03:00
|
|
|
tl.mu.Lock()
|
2019-01-08 12:31:30 +03:00
|
|
|
tl.location = l
|
2019-01-10 09:56:48 +03:00
|
|
|
tl.mu.Unlock()
|
2018-08-19 03:29:33 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 12:31:30 +03:00
|
|
|
// GetLocation returns the location of a TimeLocation.
|
2019-01-09 01:45:08 +03:00
|
|
|
func (tl *timeLocation) Location() string {
|
2019-01-10 09:56:48 +03:00
|
|
|
tl.mu.RLock()
|
|
|
|
l := tl.location
|
|
|
|
tl.mu.RUnlock()
|
|
|
|
return l
|
2018-08-19 03:29:33 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 12:31:30 +03:00
|
|
|
// 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
|
|
|
|
|
2018-12-14 08:39:53 +03:00
|
|
|
var (
|
2019-01-08 12:16:24 +03:00
|
|
|
patTable = standardPat.Bytes()
|
|
|
|
pmtTable = standardPmtTimeLocation.Bytes()
|
2018-12-14 08:39:53 +03:00
|
|
|
)
|
2018-02-28 17:42:00 +03:00
|
|
|
|
2018-01-16 08:49:18 +03:00
|
|
|
const (
|
2018-08-18 10:49:26 +03:00
|
|
|
sdtPid = 17
|
|
|
|
patPid = 0
|
|
|
|
pmtPid = 4096
|
|
|
|
videoPid = 256
|
|
|
|
streamID = 0xe0 // First video stream ID.
|
|
|
|
)
|
|
|
|
|
|
|
|
// Time related constants.
|
|
|
|
const (
|
|
|
|
// ptsOffset is the offset added to the clock to determine
|
2019-01-08 12:12:46 +03:00
|
|
|
// the current presentation timestamp.
|
2018-08-18 06:51:50 +03:00
|
|
|
ptsOffset = 700 * time.Millisecond
|
2018-08-18 10:49:26 +03:00
|
|
|
|
|
|
|
// pcrFreq is the base Program Clock Reference frequency.
|
|
|
|
pcrFreq = 90000 // Hz
|
2018-01-16 08:49:18 +03:00
|
|
|
)
|
|
|
|
|
2018-08-19 14:09:57 +03:00
|
|
|
// Encoder encapsulates properties of an mpegts generator.
|
|
|
|
type Encoder struct {
|
2018-10-19 03:50:08 +03:00
|
|
|
dst io.Writer
|
2018-08-18 07:06:14 +03:00
|
|
|
|
|
|
|
clock time.Duration
|
|
|
|
frameInterval time.Duration
|
|
|
|
ptsOffset time.Duration
|
|
|
|
|
2018-12-14 06:05:56 +03:00
|
|
|
psiCount int
|
2018-11-20 16:39:42 +03:00
|
|
|
|
2018-08-18 07:06:14 +03:00
|
|
|
continuity map[int]byte
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|
|
|
|
|
2018-08-19 14:09:57 +03:00
|
|
|
// NewEncoder returns an Encoder with the specified frame rate.
|
2018-10-19 03:50:08 +03:00
|
|
|
func NewEncoder(dst io.Writer, fps float64) *Encoder {
|
2018-08-19 14:09:57 +03:00
|
|
|
return &Encoder{
|
2018-10-19 03:50:08 +03:00
|
|
|
dst: dst,
|
2018-08-18 07:06:14 +03:00
|
|
|
|
|
|
|
frameInterval: time.Duration(float64(time.Second) / fps),
|
|
|
|
ptsOffset: ptsOffset,
|
|
|
|
|
2018-08-18 04:57:36 +03:00
|
|
|
continuity: map[int]byte{
|
2018-08-18 05:36:34 +03:00
|
|
|
patPid: 0,
|
2018-08-18 04:57:36 +03:00
|
|
|
pmtPid: 0,
|
|
|
|
videoPid: 0,
|
|
|
|
},
|
|
|
|
}
|
2018-01-08 04:12:26 +03:00
|
|
|
}
|
|
|
|
|
2018-08-18 10:49:26 +03:00
|
|
|
const (
|
|
|
|
hasPayload = 0x1
|
|
|
|
hasAdaptationField = 0x2
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hasDTS = 0x1
|
|
|
|
hasPTS = 0x2
|
|
|
|
)
|
|
|
|
|
2018-02-28 17:42:00 +03:00
|
|
|
// generate handles the incoming data and generates equivalent mpegts packets -
|
2019-01-08 12:12:46 +03:00
|
|
|
// sending them to the output channel.
|
2018-08-19 14:58:20 +03:00
|
|
|
func (e *Encoder) Encode(nalu []byte) error {
|
|
|
|
// Prepare PES data.
|
|
|
|
pesPkt := pes.Packet{
|
|
|
|
StreamID: streamID,
|
|
|
|
PDI: hasPTS,
|
|
|
|
PTS: e.pts(),
|
|
|
|
Data: nalu,
|
|
|
|
HeaderLength: 5,
|
|
|
|
}
|
|
|
|
buf := pesPkt.Bytes()
|
|
|
|
|
|
|
|
pusi := true
|
|
|
|
for len(buf) != 0 {
|
|
|
|
pkt := Packet{
|
|
|
|
PUSI: pusi,
|
|
|
|
PID: videoPid,
|
|
|
|
RAI: pusi,
|
|
|
|
CC: e.ccFor(videoPid),
|
|
|
|
AFC: hasAdaptationField | hasPayload,
|
|
|
|
PCRF: pusi,
|
2018-08-18 04:57:36 +03:00
|
|
|
}
|
2018-08-19 14:58:20 +03:00
|
|
|
n := pkt.FillPayload(buf)
|
|
|
|
buf = buf[n:]
|
|
|
|
|
|
|
|
if pusi {
|
|
|
|
// If the packet has a Payload Unit Start Indicator
|
|
|
|
// flag set then we need to write a PCR.
|
|
|
|
pkt.PCR = e.pcr()
|
|
|
|
pusi = false
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
2018-12-14 06:05:56 +03:00
|
|
|
if e.psiCount <= 0 {
|
|
|
|
err := e.writePSI()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.psiCount--
|
2018-10-19 03:50:08 +03:00
|
|
|
_, err := e.dst.Write(pkt.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
2018-08-19 14:58:20 +03:00
|
|
|
|
|
|
|
e.tick()
|
|
|
|
|
|
|
|
return nil
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|
2018-08-18 04:57:36 +03:00
|
|
|
|
2018-12-14 09:16:36 +03:00
|
|
|
// writePSI creates mpegts with pat and pmt tables - with pmt table having updated
|
|
|
|
// location and time data.
|
2018-11-21 05:04:00 +03:00
|
|
|
func (e *Encoder) writePSI() error {
|
2019-01-08 12:12:46 +03:00
|
|
|
// Write PAT.
|
2018-11-21 05:04:00 +03:00
|
|
|
patPkt := Packet{
|
|
|
|
PUSI: true,
|
|
|
|
PID: patPid,
|
|
|
|
CC: e.ccFor(patPid),
|
|
|
|
AFC: hasPayload,
|
2018-12-13 07:39:23 +03:00
|
|
|
Payload: addPadding(patTable),
|
2018-11-21 05:04:00 +03:00
|
|
|
}
|
|
|
|
_, err := e.dst.Write(patPkt.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-08 12:12:46 +03:00
|
|
|
// Update pmt table time and location.
|
2019-01-09 01:45:08 +03:00
|
|
|
err = psi.UpdateTime(pmtTable, MetaData.TimeStamp())
|
2018-12-13 09:43:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-09 01:45:08 +03:00
|
|
|
err = psi.UpdateLocation(pmtTable, MetaData.Location())
|
2018-12-13 09:43:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-13 04:52:06 +03:00
|
|
|
|
2019-01-08 12:12:46 +03:00
|
|
|
// Create mts packet from pmt table.
|
2018-11-21 05:04:00 +03:00
|
|
|
pmtPkt := Packet{
|
|
|
|
PUSI: true,
|
|
|
|
PID: pmtPid,
|
|
|
|
CC: e.ccFor(pmtPid),
|
|
|
|
AFC: hasPayload,
|
2018-12-13 09:43:34 +03:00
|
|
|
Payload: addPadding(pmtTable),
|
2018-11-21 05:04:00 +03:00
|
|
|
}
|
|
|
|
_, err = e.dst.Write(pmtPkt.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.psiCount = psiSendCount
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-14 09:16:36 +03:00
|
|
|
// addPadding adds an appropriate amount of padding to a pat or pmt table for
|
2019-01-08 12:12:46 +03:00
|
|
|
// addition to an mpegts packet.
|
2018-12-13 07:39:23 +03:00
|
|
|
func addPadding(d []byte) []byte {
|
|
|
|
for len(d) < psiPacketSize {
|
|
|
|
d = append(d, 0xff)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-08-18 07:06:14 +03:00
|
|
|
// tick advances the clock one frame interval.
|
2018-08-19 14:09:57 +03:00
|
|
|
func (e *Encoder) tick() {
|
|
|
|
e.clock += e.frameInterval
|
2018-08-18 07:06:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pts retuns the current presentation timestamp.
|
2018-08-19 14:09:57 +03:00
|
|
|
func (e *Encoder) pts() uint64 {
|
|
|
|
return uint64((e.clock + e.ptsOffset).Seconds() * pcrFreq)
|
2018-08-18 04:57:36 +03:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:06:14 +03:00
|
|
|
// pcr returns the current program clock reference.
|
2018-08-19 14:09:57 +03:00
|
|
|
func (e *Encoder) pcr() uint64 {
|
|
|
|
return uint64(e.clock.Seconds() * pcrFreq)
|
2018-08-18 04:57:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ccFor returns the next continuity counter for pid.
|
2018-08-19 14:09:57 +03:00
|
|
|
func (e *Encoder) ccFor(pid int) byte {
|
|
|
|
cc := e.continuity[pid]
|
2018-08-18 10:49:26 +03:00
|
|
|
const continuityCounterMask = 0xf
|
2018-08-19 14:09:57 +03:00
|
|
|
e.continuity[pid] = (cc + 1) & continuityCounterMask
|
2018-08-18 04:57:36 +03:00
|
|
|
return cc
|
|
|
|
}
|