av/container/mts/encoder.go

353 lines
8.7 KiB
Go
Raw Normal View History

/*
NAME
2018-08-19 14:09:57 +03:00
encoder.go
AUTHOR
2018-02-28 16:46:59 +03:00
Saxon Nelson-Milton <saxon@ausocean.org>
Dan Kortschak <dan@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 (
"fmt"
"io"
"strconv"
2018-08-18 06:51:50 +03:00
"time"
"bitbucket.org/ausocean/av/codec/h264"
"bitbucket.org/ausocean/av/codec/h264/h264dec"
2019-03-25 04:21:03 +03:00
"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 (
streamIDH264 = 27
streamIDH265 = 36
streamIDMJPEG = 28
streamIDAudio = 0xc0 // ADPCM audio stream ID.
)
// These three constants are used to select between the three different
// methods of when the PSI is sent.
const (
psiMethodPacket = iota // PSI is inserted after a certain number of packets.
psiMethodTime // PSI is inserted after a certain amount of time.
psiMethodNAL // PSI is inserted before each "key frame" of media.
)
// 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 (
PIDVideo = 256
PIDAudio = 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
)
// Default encoder configuration parameters.
const (
defaultRate = 25 // FPS
defaultPSIMethod = psiMethodNAL
defaultStreamID = streamIDH264
defaultMediaPID = PIDVideo
)
// 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()
type logger interface {
Debug(string, ...interface{})
Info(string, ...interface{})
Warning(string, ...interface{})
Error(string, ...interface{})
Fatal(string, ...interface{})
}
// Encoder encapsulates properties of an MPEG-TS generator.
2018-08-19 14:09:57 +03:00
type Encoder struct {
dst io.WriteCloser
2018-08-18 07:06:14 +03:00
2019-04-05 08:58:25 +03:00
clock time.Duration
lastTime time.Time
writePeriod time.Duration
ptsOffset time.Duration
tsSpace [PacketSize]byte
pesSpace [pes.MaxPesSize]byte
2018-08-18 07:06:14 +03:00
continuity map[uint16]byte
psiMethod int
pktCount int
psiSendCount int
psiTime time.Duration
psiSetTime time.Duration
startTime time.Time
mediaPID uint16
2019-04-05 08:58:25 +03:00
streamID byte
pmt *psi.PSI
patBytes, pmtBytes []byte
// log is a function that will be used through the encoder code for logging.
log logger
2017-12-13 09:52:18 +03:00
}
// 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, log logger, options ...func(*Encoder) error) (*Encoder, error) {
e := &Encoder{
dst: dst,
writePeriod: time.Duration(float64(time.Second) / defaultRate),
2019-04-05 08:58:25 +03:00
ptsOffset: ptsOffset,
psiMethod: defaultPSIMethod,
pktCount: 8,
mediaPID: defaultMediaPID,
streamID: defaultStreamID,
continuity: map[uint16]byte{PatPid: 0, PmtPid: 0, defaultMediaPID: 0},
log: log,
patBytes: psi.NewPATPSI().Bytes(),
pmt: psi.NewPMTPSI(),
}
2018-01-08 04:12:26 +03:00
for _, option := range options {
err := option(e)
if err != nil {
return nil, fmt.Errorf("option failed with error: %w", err)
}
}
log.Debug("encoder options applied")
e.pmt.Tss.Sd.(*psi.PMT).Essd.St = e.streamID
e.pmt.Tss.Sd.(*psi.PMT).Essd.Epid = e.mediaPID
e.pmtBytes = e.pmt.Bytes()
return e, nil
}
2019-06-03 11:20:48 +03:00
// 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.
2019-04-05 08:58:25 +03:00
func (e *Encoder) Write(data []byte) (int, error) {
switch e.psiMethod {
case psiMethodPacket:
e.log.Debug("checking packet no. conditions for PSI write", "count", e.pktCount, "PSI count", e.psiSendCount)
if e.pktCount >= e.psiSendCount {
e.pktCount = 0
err := e.writePSI()
if err != nil {
return 0, err
}
}
case psiMethodNAL:
nalType, err := h264.NALType(data)
if err != nil {
return 0, fmt.Errorf("could not get type from NAL unit, failed with error: %w", err)
}
e.log.Debug("checking conditions for PSI write", "AU type", nalType, "needed type", h264dec.NALTypeSPS)
if nalType == h264dec.NALTypeSPS {
err := e.writePSI()
if err != nil {
return 0, err
}
}
case psiMethodTime:
dur := time.Now().Sub(e.startTime)
e.log.Debug("checking time conditions for PSI write")
if dur >= e.psiTime {
e.psiTime = e.psiSetTime
e.startTime = time.Now()
err := e.writePSI()
if err != nil {
return 0, err
}
}
default:
panic("undefined PSI method")
}
// Prepare PES data.
pts := e.pts()
pesPkt := pes.Packet{
2019-04-05 08:58:25 +03:00
StreamID: e.streamID,
PDI: hasPTS,
PTS: pts,
2019-04-05 08:58:25 +03:00
Data: data,
HeaderLength: 5,
}
2019-04-05 08:58:25 +03:00
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,
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.
pcr := e.pcr()
e.log.Debug("new access unit", "PCR", pcr, "PTS", pts)
pkt.PCR = pcr
pusi = false
}
_, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil {
2019-04-05 08:58:25 +03:00
return len(data), err
}
e.pktCount++
}
e.tick()
2019-04-05 08:58:25 +03:00
return len(data), nil
2017-12-13 09:52:18 +03:00
}
// writePSI creates MPEG-TS with pat and pmt tables - with pmt table having updated
2018-12-14 09:16:36 +03:00
// 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(e.patBytes),
}
2019-01-12 10:06:35 +03:00
_, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil {
return err
}
e.pktCount++
e.pmtBytes, err = updateMeta(e.pmtBytes, e.log)
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(e.pmtBytes),
}
2019-01-12 10:06:35 +03:00
_, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil {
return err
}
e.pktCount++
e.log.Debug("PSI written", "PAT CC", patPkt.CC, "PMT CC", pmtPkt.CC)
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() {
2019-04-05 08:58:25 +03:00
e.clock += e.writePeriod
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() * PTSFrequency)
}
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() * PCRFrequency)
}
// ccFor returns the next continuity counter for pid.
func (e *Encoder) ccFor(pid uint16) byte {
2018-08-19 14:09:57 +03:00
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, log logger) ([]byte, error) {
p := psi.PSIBytes(b)
if RealTime.IsSet() {
t := strconv.Itoa(int(RealTime.Get().Unix()))
Meta.Add("ts", t)
log.Debug("latest time added to meta", "time", t)
}
err := p.AddDescriptor(psi.MetadataTag, Meta.Encode())
return []byte(p), err
}
func (e *Encoder) Close() error {
e.log.Debug("closing encoder")
return e.dst.Close()
}