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
|
|
|
|
|
|
|
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 (
|
2019-08-25 10:44:06 +03:00
|
|
|
"fmt"
|
2018-10-19 03:50:08 +03:00
|
|
|
"io"
|
2019-08-25 10:44:06 +03:00
|
|
|
"strconv"
|
2018-08-18 06:51:50 +03:00
|
|
|
"time"
|
|
|
|
|
2019-08-25 10:44:06 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/h264"
|
2019-08-26 02:54:18 +03:00
|
|
|
"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"
|
2019-08-26 06:59:07 +03:00
|
|
|
"bitbucket.org/ausocean/utils/realtime"
|
2018-12-10 10:14:56 +03:00
|
|
|
)
|
|
|
|
|
2019-08-26 06:59:07 +03:00
|
|
|
// Media type values.
|
|
|
|
// TODO: reference relevant specifications.
|
|
|
|
const (
|
|
|
|
H264ID = 27
|
|
|
|
H265ID = 36
|
2019-09-12 12:46:11 +03:00
|
|
|
audioStreamID = 0xc0 // ADPCM audio stream ID.
|
2019-08-26 06:59:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Constants used to communicate which media codec will be packetized.
|
|
|
|
const (
|
|
|
|
EncodeH264 = iota
|
|
|
|
EncodeH265
|
|
|
|
EncodeAudio
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2019-07-10 02:56:54 +03:00
|
|
|
// Some common manifestations of PSI.
|
2019-01-07 09:43:50 +03:00
|
|
|
var (
|
2019-07-10 02:56:54 +03:00
|
|
|
// StandardPAT is a minimal PAT.
|
|
|
|
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-07-10 02:56:54 +03:00
|
|
|
|
2019-07-10 03:47:34 +03:00
|
|
|
// Base PMT is a minimal PMT without specific data.
|
|
|
|
BasePMT = psi.PSI{
|
2019-07-10 02:56:54 +03:00
|
|
|
Pf: 0x00,
|
|
|
|
Tid: 0x02,
|
|
|
|
Ssi: true,
|
|
|
|
Sl: 0x12,
|
|
|
|
Tss: &psi.TSS{
|
|
|
|
Tide: 0x01,
|
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
},
|
|
|
|
}
|
2019-01-07 09:43:50 +03:00
|
|
|
)
|
|
|
|
|
2019-02-07 00:59:51 +03:00
|
|
|
// Meta allows addition of metadata to encoded mts from outside of this pkg.
|
|
|
|
// See meta pkg for usage.
|
2019-02-07 01:01:02 +03:00
|
|
|
//
|
|
|
|
// TODO: make this not global.
|
2019-02-06 02:49:12 +03:00
|
|
|
var Meta *meta.Data
|
2019-02-05 05:44:42 +03:00
|
|
|
|
2019-08-26 06:59:07 +03:00
|
|
|
// This will help us obtain a realtime for timestamp meta encoding.
|
|
|
|
var RealTime = realtime.NewRealTime()
|
|
|
|
|
2018-12-14 08:39:53 +03:00
|
|
|
var (
|
2019-07-10 02:56:54 +03:00
|
|
|
patTable = StandardPAT.Bytes()
|
2019-05-16 07:27:10 +03:00
|
|
|
pmtTable []byte
|
2018-12-14 08:39:53 +03:00
|
|
|
)
|
2018-02-28 17:42:00 +03:00
|
|
|
|
2019-05-11 14:44:28 +03:00
|
|
|
// Encoder encapsulates properties of an MPEG-TS generator.
|
2018-08-19 14:09:57 +03:00
|
|
|
type Encoder struct {
|
2019-04-15 02:12:56 +03:00
|
|
|
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[int]byte
|
2019-01-24 07:03:22 +03:00
|
|
|
|
2019-08-25 10:44:06 +03:00
|
|
|
nalBasedPSI bool
|
2019-02-16 09:03:39 +03:00
|
|
|
pktCount int
|
|
|
|
psiSendCount int
|
2019-04-05 08:58:25 +03:00
|
|
|
mediaPid int
|
|
|
|
streamID byte
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|
|
|
|
|
2019-04-10 10:48:42 +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.
|
2019-04-15 02:12:56 +03:00
|
|
|
func NewEncoder(dst io.WriteCloser, rate float64, mediaType int) *Encoder {
|
2019-04-05 08:58:25 +03:00
|
|
|
var mPid int
|
|
|
|
var sid byte
|
2019-09-12 16:42:55 +03:00
|
|
|
nbp := true
|
2019-04-05 08:58:25 +03:00
|
|
|
switch mediaType {
|
2019-05-21 10:27:17 +03:00
|
|
|
case EncodeAudio:
|
2019-06-05 17:58:26 +03:00
|
|
|
mPid = AudioPid
|
2019-04-05 08:58:25 +03:00
|
|
|
sid = audioStreamID
|
2019-09-12 16:42:55 +03:00
|
|
|
nbp = false
|
2019-05-21 10:27:17 +03:00
|
|
|
case EncodeH265:
|
2019-06-05 17:58:26 +03:00
|
|
|
mPid = VideoPid
|
2019-05-16 10:11:52 +03:00
|
|
|
sid = H265ID
|
2019-05-21 10:27:17 +03:00
|
|
|
case EncodeH264:
|
2019-06-05 17:58:26 +03:00
|
|
|
mPid = VideoPid
|
2019-05-16 10:11:52 +03:00
|
|
|
sid = H264ID
|
2019-04-05 08:58:25 +03:00
|
|
|
}
|
|
|
|
|
2019-07-10 03:47:34 +03:00
|
|
|
pmt := BasePMT
|
|
|
|
pmt.Tss.Sd = &psi.PMT{
|
|
|
|
Pcrpid: 0x0100,
|
|
|
|
Pil: 0,
|
|
|
|
Essd: &psi.ESSD{
|
|
|
|
St: byte(sid),
|
2019-09-12 14:59:40 +03:00
|
|
|
Epid: uint16(mPid),
|
2019-07-10 03:47:34 +03:00
|
|
|
Esil: 0x00,
|
2019-05-16 07:27:10 +03:00
|
|
|
},
|
2019-07-10 02:56:54 +03:00
|
|
|
}
|
|
|
|
pmtTable = pmt.Bytes()
|
2019-05-16 07:27:10 +03:00
|
|
|
|
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
|
|
|
|
2019-04-10 10:48:42 +03:00
|
|
|
writePeriod: time.Duration(float64(time.Second) / rate),
|
2019-04-05 08:58:25 +03:00
|
|
|
ptsOffset: ptsOffset,
|
2018-08-18 07:06:14 +03:00
|
|
|
|
2019-09-12 16:42:55 +03:00
|
|
|
nalBasedPSI: nbp,
|
2019-02-16 09:03:39 +03:00
|
|
|
|
|
|
|
pktCount: 8,
|
|
|
|
|
2019-04-05 08:58:25 +03:00
|
|
|
mediaPid: mPid,
|
|
|
|
streamID: sid,
|
|
|
|
|
2018-08-18 04:57:36 +03:00
|
|
|
continuity: map[int]byte{
|
2019-06-05 17:58:26 +03:00
|
|
|
PatPid: 0,
|
|
|
|
PmtPid: 0,
|
2019-04-05 08:58:25 +03:00
|
|
|
mPid: 0,
|
2018-08-18 04:57:36 +03:00
|
|
|
},
|
|
|
|
}
|
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
|
|
|
|
)
|
|
|
|
|
2019-08-25 10:44:06 +03:00
|
|
|
func (e *Encoder) NALBasedPSI(b bool, sendCount int) {
|
|
|
|
e.nalBasedPSI = b
|
2019-02-16 09:03:39 +03:00
|
|
|
e.psiSendCount = sendCount
|
2019-02-16 20:05:59 +03:00
|
|
|
e.pktCount = e.psiSendCount
|
2019-02-16 09:03:39 +03:00
|
|
|
}
|
|
|
|
|
2019-06-03 11:20:48 +03:00
|
|
|
// Write implements io.Writer. Write takes raw video or audio data and encodes into MPEG-TS,
|
2019-03-10 05:34:15 +03:00
|
|
|
// 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) {
|
2019-08-25 10:44:06 +03:00
|
|
|
if e.nalBasedPSI {
|
|
|
|
nalType, err := h264.NALType(data)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not get type from NAL unit, failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-08-26 02:54:18 +03:00
|
|
|
if nalType == h264dec.NALTypeSPS {
|
2019-08-25 10:44:06 +03:00
|
|
|
err := e.writePSI()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if e.pktCount >= e.psiSendCount {
|
2019-02-16 20:05:59 +03:00
|
|
|
e.pktCount = 0
|
2019-01-23 08:20:25 +03:00
|
|
|
err := e.writePSI()
|
|
|
|
if err != nil {
|
2019-03-10 05:30:58 +03:00
|
|
|
return 0, err
|
2019-01-23 08:20:25 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-24 07:03:22 +03:00
|
|
|
|
2018-08-19 14:58:20 +03:00
|
|
|
// Prepare PES data.
|
|
|
|
pesPkt := pes.Packet{
|
2019-04-05 08:58:25 +03:00
|
|
|
StreamID: e.streamID,
|
2018-08-19 14:58:20 +03:00
|
|
|
PDI: hasPTS,
|
|
|
|
PTS: e.pts(),
|
2019-04-05 08:58:25 +03:00
|
|
|
Data: data,
|
2018-08-19 14:58:20 +03:00
|
|
|
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])
|
2018-08-19 14:58:20 +03:00
|
|
|
|
|
|
|
pusi := true
|
|
|
|
for len(buf) != 0 {
|
|
|
|
pkt := Packet{
|
|
|
|
PUSI: pusi,
|
2019-04-05 08:58:25 +03:00
|
|
|
PID: uint16(e.mediaPid),
|
2018-08-19 14:58:20 +03:00
|
|
|
RAI: pusi,
|
2019-04-05 08:58:25 +03:00
|
|
|
CC: e.ccFor(e.mediaPid),
|
2018-08-19 14:58:20 +03:00
|
|
|
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
|
|
|
}
|
2019-04-26 10:33:30 +03:00
|
|
|
_, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize]))
|
2018-10-19 03:50:08 +03:00
|
|
|
if err != nil {
|
2019-04-05 08:58:25 +03:00
|
|
|
return len(data), err
|
2018-10-19 03:50:08 +03:00
|
|
|
}
|
2019-02-16 09:03:39 +03:00
|
|
|
e.pktCount++
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
2018-08-19 14:58:20 +03:00
|
|
|
|
|
|
|
e.tick()
|
|
|
|
|
2019-04-05 08:58:25 +03:00
|
|
|
return len(data), nil
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|
2018-08-18 04:57:36 +03:00
|
|
|
|
2019-05-11 14:44:28 +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.
|
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{
|
2019-01-30 08:07:15 +03:00
|
|
|
PUSI: true,
|
|
|
|
PID: PatPid,
|
|
|
|
CC: e.ccFor(PatPid),
|
2019-05-07 16:40:03 +03:00
|
|
|
AFC: hasPayload,
|
2019-01-30 08:07:15 +03:00
|
|
|
Payload: psi.AddPadding(patTable),
|
2018-11-21 05:04:00 +03:00
|
|
|
}
|
2019-01-12 10:06:35 +03:00
|
|
|
_, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize]))
|
2018-11-21 05:04:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-16 09:03:39 +03:00
|
|
|
e.pktCount++
|
2019-02-05 15:54:00 +03:00
|
|
|
pmtTable, err = updateMeta(pmtTable)
|
2019-02-05 15:48:05 +03:00
|
|
|
if err != nil {
|
2019-01-30 08:07:15 +03:00
|
|
|
return err
|
|
|
|
}
|
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{
|
2019-01-30 08:07:15 +03:00
|
|
|
PUSI: true,
|
|
|
|
PID: PmtPid,
|
|
|
|
CC: e.ccFor(PmtPid),
|
2019-05-07 16:40:03 +03:00
|
|
|
AFC: hasPayload,
|
2019-01-30 08:07:15 +03:00
|
|
|
Payload: psi.AddPadding(pmtTable),
|
2018-11-21 05:04:00 +03:00
|
|
|
}
|
2019-01-12 10:06:35 +03:00
|
|
|
_, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize]))
|
2018-11-21 05:04:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-16 09:03:39 +03:00
|
|
|
e.pktCount++
|
2018-11-21 05:04:00 +03:00
|
|
|
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 {
|
2019-05-11 07:20:40 +03:00
|
|
|
return uint64((e.clock + e.ptsOffset).Seconds() * PTSFrequency)
|
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 {
|
2019-05-11 07:20:40 +03:00
|
|
|
return uint64(e.clock.Seconds() * PCRFrequency)
|
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]
|
2019-01-12 10:04:43 +03:00
|
|
|
const continuityCounterMask = 0xf
|
|
|
|
e.continuity[pid] = (cc + 1) & continuityCounterMask
|
2018-08-18 04:57:36 +03:00
|
|
|
return cc
|
|
|
|
}
|
2019-01-26 15:12:31 +03:00
|
|
|
|
2019-02-04 14:47:39 +03:00
|
|
|
// updateMeta adds/updates a metaData descriptor in the given psi bytes using data
|
|
|
|
// contained in the global Meta struct.
|
2019-02-05 15:54:00 +03:00
|
|
|
func updateMeta(b []byte) ([]byte, error) {
|
|
|
|
p := psi.PSIBytes(b)
|
2019-08-26 06:59:07 +03:00
|
|
|
if RealTime.IsSet() {
|
|
|
|
Meta.Add("ts", strconv.Itoa(int(RealTime.Get().Unix())))
|
2019-08-25 10:44:06 +03:00
|
|
|
}
|
2019-02-01 04:09:47 +03:00
|
|
|
err := p.AddDescriptor(psi.MetadataTag, Meta.Encode())
|
2019-02-05 15:54:00 +03:00
|
|
|
return []byte(p), err
|
2019-01-26 15:12:31 +03:00
|
|
|
}
|
2019-04-15 02:12:56 +03:00
|
|
|
|
|
|
|
func (e *Encoder) Close() error {
|
|
|
|
return e.dst.Close()
|
|
|
|
}
|