mirror of https://bitbucket.org/ausocean/av.git
416 lines
8.7 KiB
Go
416 lines
8.7 KiB
Go
/*
|
|
NAME
|
|
encoder.go
|
|
|
|
AUTHOR
|
|
Dan Kortschak <dan@ausocean.org>
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
LICENSE
|
|
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 (
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
"time"
|
|
|
|
"bitbucket.org/ausocean/av/codec/h264"
|
|
"bitbucket.org/ausocean/av/codec/h264/h264dec"
|
|
"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"
|
|
)
|
|
|
|
// Stream IDs as per ITU-T Rec. H.222.0 / ISO/IEC 13818-1 [1], tables 2-22 and 2-34.
|
|
const (
|
|
H264ID = 27
|
|
H265ID = 36
|
|
MJPEGID = 28
|
|
audioStreamID = 0xc0 // ADPCM audio stream ID.
|
|
)
|
|
|
|
// Constants used to communicate which media codec will be packetized.
|
|
const (
|
|
EncodeH264 = iota
|
|
EncodeH265
|
|
EncodeMJPEG
|
|
EncodeAudio
|
|
)
|
|
|
|
// The program IDs we assign to different types of media.
|
|
const (
|
|
VideoPid = 256
|
|
AudioPid = 210
|
|
)
|
|
|
|
// 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
|
|
|
|
const (
|
|
hasPayload = 0x1
|
|
hasAdaptationField = 0x2
|
|
)
|
|
|
|
const (
|
|
hasDTS = 0x1
|
|
hasPTS = 0x2
|
|
)
|
|
|
|
// Some common manifestations of PSI.
|
|
var (
|
|
// 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,
|
|
},
|
|
},
|
|
}
|
|
|
|
// Base PMT is a minimal PMT without specific data.
|
|
BasePMT = psi.PSI{
|
|
Pf: 0x00,
|
|
Tid: 0x02,
|
|
Ssi: true,
|
|
Sl: 0x12,
|
|
Tss: &psi.TSS{
|
|
Tide: 0x01,
|
|
V: 0,
|
|
Cni: true,
|
|
Sn: 0,
|
|
Lsn: 0,
|
|
},
|
|
}
|
|
)
|
|
|
|
// 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
|
|
)
|
|
|
|
// Encoder encapsulates properties of an MPEG-TS generator.
|
|
type Encoder struct {
|
|
dst io.WriteCloser
|
|
|
|
clock time.Duration
|
|
lastTime time.Time
|
|
writePeriod time.Duration
|
|
ptsOffset time.Duration
|
|
tsSpace [PacketSize]byte
|
|
pesSpace [pes.MaxPesSize]byte
|
|
|
|
continuity map[uint16]byte
|
|
|
|
psiMethod int
|
|
pktCount int
|
|
psiSendCount int
|
|
psiTime time.Duration
|
|
temp time.Duration
|
|
startTime time.Time
|
|
mediaPid uint16
|
|
streamID byte
|
|
}
|
|
|
|
// NewEncoder returns an Encoder with the specified media type and rate eg. if a video stream
|
|
// calls write for every frame, the rate will be the frame rate of the video.
|
|
func NewEncoder(dst io.WriteCloser, rate float64, mediaType int, options ...func(*Encoder) error) (*Encoder, error) {
|
|
var mPID uint16
|
|
var sID byte
|
|
psim := timeBased
|
|
switch mediaType {
|
|
case EncodeAudio:
|
|
mPID = AudioPid
|
|
sID = audioStreamID
|
|
psim = pktBased
|
|
case EncodeH265:
|
|
mPID = VideoPid
|
|
sID = H265ID
|
|
psim = nalBased
|
|
case EncodeH264:
|
|
mPID = VideoPid
|
|
sID = H264ID
|
|
psim = nalBased
|
|
case EncodeMJPEG:
|
|
mPID = VideoPid
|
|
sID = MJPEGID
|
|
psim = timeBased
|
|
}
|
|
|
|
pmt := BasePMT
|
|
pmt.Tss.Sd = &psi.PMT{
|
|
Pcrpid: 0x0100,
|
|
Pil: 0,
|
|
Essd: &psi.ESSD{
|
|
St: byte(sID),
|
|
Epid: mPID,
|
|
Esil: 0x00,
|
|
},
|
|
}
|
|
pmtTable = pmt.Bytes()
|
|
|
|
e := &Encoder{
|
|
dst: dst,
|
|
|
|
writePeriod: time.Duration(float64(time.Second) / rate),
|
|
ptsOffset: ptsOffset,
|
|
|
|
psiMethod: psim,
|
|
|
|
pktCount: 8,
|
|
|
|
mediaPid: mPID,
|
|
streamID: sID,
|
|
|
|
continuity: map[uint16]byte{
|
|
PatPid: 0,
|
|
PmtPid: 0,
|
|
mPID: 0,
|
|
},
|
|
}
|
|
|
|
for _, option := range options {
|
|
err := option(e)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("option failed with error: %w", err)
|
|
}
|
|
}
|
|
return e, nil
|
|
}
|
|
|
|
const (
|
|
pktBased = iota
|
|
timeBased
|
|
nalBased
|
|
)
|
|
|
|
// PacketBasedPSI is an option that can be passed to NewEncoder to select
|
|
// packet based PSI writing, i.e. PSI are written to the destination every
|
|
// sendCount packets.
|
|
func PacketBasedPSI(sendCount int) func(*Encoder) error {
|
|
return func(e *Encoder) error {
|
|
e.psiMethod = pktBased
|
|
e.psiSendCount = sendCount
|
|
e.pktCount = e.psiSendCount
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func TimeBasedPSI(timeBetweenPSI time.Duration) func(*Encoder) error {
|
|
return func(e *Encoder) error {
|
|
e.psiMethod = timeBased
|
|
e.psiTime = 0
|
|
e.temp = timeBetweenPSI
|
|
e.startTime = time.Now()
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Write implements io.Writer. Write takes raw video or audio data and encodes into MPEG-TS,
|
|
// then sending it to the encoder's io.Writer destination.
|
|
func (e *Encoder) Write(data []byte) (int, error) {
|
|
switch e.psiMethod {
|
|
case pktBased:
|
|
if e.pktCount >= e.psiSendCount {
|
|
e.pktCount = 0
|
|
err := e.writePSI()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
case nalBased:
|
|
nalType, err := h264.NALType(data)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("could not get type from NAL unit, failed with error: %w", err)
|
|
}
|
|
|
|
if nalType == h264dec.NALTypeSPS {
|
|
err := e.writePSI()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
case timeBased:
|
|
if time.Now().Sub(e.startTime) >= e.psiTime {
|
|
e.psiTime = e.temp
|
|
e.startTime = time.Now()
|
|
err := e.writePSI()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
}
|
|
default:
|
|
}
|
|
|
|
// Prepare PES data.
|
|
pesPkt := pes.Packet{
|
|
StreamID: e.streamID,
|
|
PDI: hasPTS,
|
|
PTS: e.pts(),
|
|
Data: data,
|
|
HeaderLength: 5,
|
|
}
|
|
|
|
buf := pesPkt.Bytes(e.pesSpace[:pes.MaxPesSize])
|
|
|
|
pusi := true
|
|
for len(buf) != 0 {
|
|
pkt := Packet{
|
|
PUSI: pusi,
|
|
PID: uint16(e.mediaPid),
|
|
RAI: pusi,
|
|
CC: e.ccFor(e.mediaPid),
|
|
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
|
|
}
|
|
_, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize]))
|
|
if err != nil {
|
|
return len(data), err
|
|
}
|
|
e.pktCount++
|
|
}
|
|
|
|
e.tick()
|
|
|
|
return len(data), nil
|
|
}
|
|
|
|
// writePSI creates MPEG-TS with pat and pmt tables - with pmt table having updated
|
|
// location and time data.
|
|
func (e *Encoder) writePSI() error {
|
|
// Write PAT.
|
|
patPkt := Packet{
|
|
PUSI: true,
|
|
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
|
|
}
|
|
e.pktCount++
|
|
pmtTable, err = updateMeta(pmtTable)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create mts packet from pmt table.
|
|
pmtPkt := Packet{
|
|
PUSI: true,
|
|
PID: PmtPid,
|
|
CC: e.ccFor(PmtPid),
|
|
AFC: hasPayload,
|
|
Payload: psi.AddPadding(pmtTable),
|
|
}
|
|
_, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize]))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
e.pktCount++
|
|
return nil
|
|
}
|
|
|
|
// tick advances the clock one frame interval.
|
|
func (e *Encoder) tick() {
|
|
e.clock += e.writePeriod
|
|
}
|
|
|
|
// pts retuns the current presentation timestamp.
|
|
func (e *Encoder) pts() uint64 {
|
|
return uint64((e.clock + e.ptsOffset).Seconds() * PTSFrequency)
|
|
}
|
|
|
|
// pcr returns the current program clock reference.
|
|
func (e *Encoder) pcr() uint64 {
|
|
return uint64(e.clock.Seconds() * PCRFrequency)
|
|
}
|
|
|
|
// ccFor returns the next continuity counter for pid.
|
|
func (e *Encoder) ccFor(pid uint16) 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)
|
|
if RealTime.IsSet() {
|
|
Meta.Add("ts", strconv.Itoa(int(RealTime.Get().Unix())))
|
|
}
|
|
err := p.AddDescriptor(psi.MetadataTag, Meta.Encode())
|
|
return []byte(p), err
|
|
}
|
|
|
|
func (e *Encoder) Close() error {
|
|
return e.dst.Close()
|
|
}
|