av/stream/mts/encoder.go

270 lines
5.4 KiB
Go
Raw Normal View History

/*
NAME
2018-08-19 14:09:57 +03:00
encoder.go
DESCRIPTION
See Readme.md
AUTHOR
Dan Kortschak <dan@ausocean.org>
2018-02-28 16:46:59 +03:00
Saxon Nelson-Milton <saxon@ausocean.org>
LICENSE
2018-08-19 14:09:57 +03:00
encoder.go is Copyright (C) 2017-2018 the Australian Ocean Lab (AusOcean)
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
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package mts
import (
"io"
2018-08-18 06:51:50 +03:00
"time"
"bitbucket.org/ausocean/av/stream/mts/meta"
2018-08-20 01:19:02 +03:00
"bitbucket.org/ausocean/av/stream/mts/pes"
"bitbucket.org/ausocean/av/stream/mts/psi"
)
// Some common manifestations of PSI
var (
2019-01-10 03:42:33 +03:00
// standardPat is a minimal PAT.
standardPat = psi.PSI{
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-11 14:59:48 +03:00
// standardPmt is a minimal PMT, without descriptors for time and location.
standardPmt = psi.PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: 0x12,
Tss: &psi.TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &psi.PMT{
Pcrpid: 0x0100,
Pil: 0,
Essd: &psi.ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
},
},
}
)
const (
psiSndCnt = 7
)
2018-04-17 08:20:23 +03:00
const (
copyright = "copyright"
license = "ausocean.org/license/content2019"
)
// global Meta
var Meta = meta.New()
func init() {
Meta.Add(copyright, license)
}
var (
patTable = standardPat.Bytes()
pmtTable = standardPmt.Bytes()
)
2018-02-28 17:42:00 +03:00
// 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
// 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 {
dst io.Writer
2018-08-18 07:06:14 +03:00
clock time.Duration
frameInterval time.Duration
ptsOffset time.Duration
2019-01-12 10:06:35 +03:00
tsSpace [PacketSize]byte
2019-01-02 03:06:46 +03:00
pesSpace [pes.MaxPesSize]byte
2018-08-18 07:06:14 +03:00
psiCount int
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.
func NewEncoder(dst io.Writer, fps float64) *Encoder {
2018-08-19 14:09:57 +03:00
return &Encoder{
dst: dst,
2018-08-18 07:06:14 +03:00
frameInterval: time.Duration(float64(time.Second) / fps),
ptsOffset: ptsOffset,
continuity: map[int]byte{
2019-01-25 08:55:01 +03:00
PatPid: 0,
PmtPid: 0,
VideoPid: 0,
},
}
2018-01-08 04:12:26 +03:00
}
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.
func (e *Encoder) Encode(nalu []byte) error {
if e.psiCount <= 0 {
err := e.writePSI()
if err != nil {
return err
}
}
// Prepare PES data.
pesPkt := pes.Packet{
2019-01-25 08:55:01 +03:00
StreamID: StreamID,
PDI: hasPTS,
PTS: e.pts(),
Data: nalu,
HeaderLength: 5,
}
2019-01-02 03:06:46 +03:00
buf := pesPkt.Bytes(e.pesSpace[:pes.MaxPesSize])
pusi := true
for len(buf) != 0 {
pkt := Packet{
PUSI: pusi,
2019-01-25 08:55:01 +03:00
PID: VideoPid,
RAI: pusi,
2019-01-25 08:55:01 +03:00
CC: e.ccFor(VideoPid),
AFC: HasAdaptationField | HasPayload,
PCRF: pusi,
}
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
}
2019-01-12 10:06:35 +03:00
_, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize]))
e.psiCount--
if err != nil {
return err
}
}
e.tick()
return nil
2017-12-13 09:52:18 +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.
func (e *Encoder) writePSI() error {
2019-01-08 12:12:46 +03:00
// Write PAT.
patPkt := Packet{
PUSI: true,
PID: PatPid,
CC: e.ccFor(PatPid),
AFC: HasPayload,
Payload: psi.AddPadding(patTable),
}
2019-01-12 10:06:35 +03:00
_, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil {
return err
}
pmtTable, err = updateMeta(pmtTable)
if err != nil {
return err
}
2019-01-08 12:12:46 +03:00
// Create mts packet from pmt table.
pmtPkt := Packet{
PUSI: true,
PID: PmtPid,
CC: e.ccFor(PmtPid),
AFC: HasPayload,
Payload: psi.AddPadding(pmtTable),
}
2019-01-12 10:06:35 +03:00
_, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil {
return err
}
e.psiCount = psiSndCnt
return nil
}
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 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)
}
// 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]
const continuityCounterMask = 0xf
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
}