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-02-28 16:46:59 +03:00
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
2020-05-06 13:40:55 +03:00
|
|
|
Dan Kortschak <dan@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
|
2020-05-06 13:40:55 +03:00
|
|
|
for more details.
|
2018-02-13 11:39:26 +03:00
|
|
|
|
|
|
|
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"
|
2021-01-27 09:07:33 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2021-02-03 04:47:23 +03:00
|
|
|
"bitbucket.org/ausocean/utils/realtime"
|
2021-01-27 09:07:33 +03:00
|
|
|
)
|
|
|
|
|
2020-05-03 08:38:15 +03:00
|
|
|
// These three constants are used to select between the three different
|
|
|
|
// methods of when the PSI is sent.
|
|
|
|
const (
|
2020-05-06 13:40:55 +03:00
|
|
|
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.
|
2020-05-03 08:38:15 +03:00
|
|
|
)
|
|
|
|
|
2019-08-26 06:59:07 +03:00
|
|
|
// Constants used to communicate which media codec will be packetized.
|
|
|
|
const (
|
|
|
|
EncodeH264 = iota
|
|
|
|
EncodeH265
|
2021-01-18 07:59:21 +03:00
|
|
|
EncodeJPEG
|
2019-09-24 01:12:26 +03:00
|
|
|
EncodeMJPEG
|
2020-12-22 05:21:20 +03:00
|
|
|
EncodePCM
|
|
|
|
EncodeADPCM
|
2019-08-26 06:59:07 +03:00
|
|
|
)
|
|
|
|
|
2019-09-24 01:12:26 +03:00
|
|
|
// The program IDs we assign to different types of media.
|
|
|
|
const (
|
2020-05-06 13:40:55 +03:00
|
|
|
PIDVideo = 256
|
|
|
|
PIDAudio = 210
|
2019-09-24 01:12:26 +03:00
|
|
|
)
|
|
|
|
|
2019-08-26 06:59:07 +03:00
|
|
|
// 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-09-24 01:12:26 +03:00
|
|
|
const (
|
|
|
|
hasPayload = 0x1
|
|
|
|
hasAdaptationField = 0x2
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hasDTS = 0x1
|
|
|
|
hasPTS = 0x2
|
|
|
|
)
|
|
|
|
|
2020-05-06 13:40:55 +03:00
|
|
|
// Default encoder configuration parameters.
|
|
|
|
const (
|
|
|
|
defaultRate = 25 // FPS
|
|
|
|
defaultPSIMethod = psiMethodNAL
|
2020-12-22 05:21:20 +03:00
|
|
|
defaultStreamID = pes.H264SID
|
2020-05-06 13:40:55 +03:00
|
|
|
defaultMediaPID = PIDVideo
|
|
|
|
)
|
|
|
|
|
2021-02-22 06:46:41 +03:00
|
|
|
// Used to consistently read and write MTS metadata entries.
|
|
|
|
const (
|
2021-02-22 08:47:36 +03:00
|
|
|
WriteRateKey = "writeRate"
|
2021-02-22 09:15:11 +03:00
|
|
|
TimestampKey = "ts"
|
|
|
|
LocationKey = "loc"
|
2021-02-22 06:46:41 +03:00
|
|
|
)
|
|
|
|
|
2019-09-24 06:27:38 +03:00
|
|
|
// Meta allows addition of metadata to encoded mts from outside of this pkg.
|
2019-02-07 00:59:51 +03:00
|
|
|
// 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
|
|
|
|
2021-02-22 09:07:13 +03:00
|
|
|
// RealTime will help us obtain a realtime for timestamp meta encoding.
|
2019-08-26 06:59:07 +03:00
|
|
|
var RealTime = realtime.NewRealTime()
|
|
|
|
|
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
|
|
|
|
2019-09-15 02:57:26 +03:00
|
|
|
continuity map[uint16]byte
|
2019-01-24 07:03:22 +03:00
|
|
|
|
2019-12-17 02:51:56 +03:00
|
|
|
psiMethod int
|
2019-02-16 09:03:39 +03:00
|
|
|
pktCount int
|
|
|
|
psiSendCount int
|
2019-12-17 02:51:56 +03:00
|
|
|
psiTime time.Duration
|
2019-12-19 03:45:47 +03:00
|
|
|
psiSetTime time.Duration
|
2019-12-17 02:51:56 +03:00
|
|
|
startTime time.Time
|
2020-05-06 13:40:55 +03:00
|
|
|
mediaPID uint16
|
2019-04-05 08:58:25 +03:00
|
|
|
streamID byte
|
2020-05-02 15:19:39 +03:00
|
|
|
|
2020-05-12 11:45:37 +03:00
|
|
|
pmt *psi.PSI
|
|
|
|
patBytes, pmtBytes []byte
|
|
|
|
|
2020-05-02 15:19:39 +03:00
|
|
|
// log is a function that will be used through the encoder code for logging.
|
2021-01-27 09:07:33 +03:00
|
|
|
log logger.LoggerIF
|
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.
|
2021-01-27 09:07:33 +03:00
|
|
|
func NewEncoder(dst io.WriteCloser, log logger.LoggerIF, options ...func(*Encoder) error) (*Encoder, error) {
|
2019-09-24 01:12:26 +03:00
|
|
|
e := &Encoder{
|
2020-05-03 08:38:15 +03:00
|
|
|
dst: dst,
|
2020-05-06 13:40:55 +03:00
|
|
|
writePeriod: time.Duration(float64(time.Second) / defaultRate),
|
2019-04-05 08:58:25 +03:00
|
|
|
ptsOffset: ptsOffset,
|
2020-05-06 13:40:55 +03:00
|
|
|
psiMethod: defaultPSIMethod,
|
2020-05-03 08:38:15 +03:00
|
|
|
pktCount: 8,
|
2020-05-06 13:40:55 +03:00
|
|
|
mediaPID: defaultMediaPID,
|
|
|
|
streamID: defaultStreamID,
|
|
|
|
continuity: map[uint16]byte{PatPid: 0, PmtPid: 0, defaultMediaPID: 0},
|
2020-05-03 08:38:15 +03:00
|
|
|
log: log,
|
2020-05-12 11:45:37 +03:00
|
|
|
patBytes: psi.NewPATPSI().Bytes(),
|
|
|
|
pmt: psi.NewPMTPSI(),
|
2018-08-18 04:57:36 +03:00
|
|
|
}
|
2018-01-08 04:12:26 +03:00
|
|
|
|
2019-09-24 01:12:26 +03:00
|
|
|
for _, option := range options {
|
|
|
|
err := option(e)
|
|
|
|
if err != nil {
|
2019-12-04 06:42:20 +03:00
|
|
|
return nil, fmt.Errorf("option failed with error: %w", err)
|
2019-09-24 01:12:26 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 08:38:15 +03:00
|
|
|
log.Debug("encoder options applied")
|
|
|
|
|
2021-02-22 08:47:36 +03:00
|
|
|
Meta.Add(WriteRateKey, fmt.Sprintf("%f", 1/float64(e.writePeriod.Seconds())))
|
2021-02-16 07:27:47 +03:00
|
|
|
|
2020-05-19 15:28:28 +03:00
|
|
|
e.pmt.SyntaxSection.SpecificData.(*psi.PMT).StreamSpecificData.StreamType = e.streamID
|
|
|
|
e.pmt.SyntaxSection.SpecificData.(*psi.PMT).StreamSpecificData.PID = e.mediaPID
|
2020-05-12 11:45:37 +03:00
|
|
|
e.pmtBytes = e.pmt.Bytes()
|
2019-02-16 09:03:39 +03:00
|
|
|
|
2020-05-06 13:40:55 +03:00
|
|
|
return e, nil
|
2019-12-17 02:51:56 +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) {
|
2021-01-27 09:07:33 +03:00
|
|
|
e.log.Debug("writing data", "len(data)", len(data))
|
2019-12-17 02:51:56 +03:00
|
|
|
switch e.psiMethod {
|
2020-05-06 13:40:55 +03:00
|
|
|
case psiMethodPacket:
|
2020-05-03 08:38:15 +03:00
|
|
|
e.log.Debug("checking packet no. conditions for PSI write", "count", e.pktCount, "PSI count", e.psiSendCount)
|
2019-12-17 02:51:56 +03:00
|
|
|
if e.pktCount >= e.psiSendCount {
|
|
|
|
e.pktCount = 0
|
|
|
|
err := e.writePSI()
|
|
|
|
if err != nil {
|
2022-05-27 05:28:53 +03:00
|
|
|
return 0, fmt.Errorf("could not write psi (psiMethodPacket): %w",err)
|
2019-12-17 02:51:56 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 13:40:55 +03:00
|
|
|
case psiMethodNAL:
|
2019-08-25 10:44:06 +03:00
|
|
|
nalType, err := h264.NALType(data)
|
|
|
|
if err != nil {
|
2019-12-04 06:42:20 +03:00
|
|
|
return 0, fmt.Errorf("could not get type from NAL unit, failed with error: %w", err)
|
2019-08-25 10:44:06 +03:00
|
|
|
}
|
2020-05-03 08:38:15 +03:00
|
|
|
e.log.Debug("checking conditions for PSI write", "AU type", nalType, "needed type", h264dec.NALTypeSPS)
|
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 {
|
2022-05-27 05:28:53 +03:00
|
|
|
return 0, fmt.Errorf("could not write psi (psiMethodNAL): %w",err)
|
2019-08-25 10:44:06 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 13:40:55 +03:00
|
|
|
case psiMethodTime:
|
2020-05-03 08:38:15 +03:00
|
|
|
dur := time.Now().Sub(e.startTime)
|
|
|
|
e.log.Debug("checking time conditions for PSI write")
|
|
|
|
if dur >= e.psiTime {
|
2019-12-19 03:45:47 +03:00
|
|
|
e.psiTime = e.psiSetTime
|
2019-12-17 02:51:56 +03:00
|
|
|
e.startTime = time.Now()
|
|
|
|
err := e.writePSI()
|
|
|
|
if err != nil {
|
2022-05-27 05:28:53 +03:00
|
|
|
return 0, fmt.Errorf("could not write psi (psiMethodTime): %w",err)
|
2019-12-17 02:51:56 +03:00
|
|
|
}
|
2019-01-23 08:20:25 +03:00
|
|
|
}
|
2019-12-17 02:51:56 +03:00
|
|
|
default:
|
2020-05-03 08:38:15 +03:00
|
|
|
panic("undefined PSI method")
|
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.
|
2020-05-03 08:38:15 +03:00
|
|
|
pts := e.pts()
|
2018-08-19 14:58:20 +03:00
|
|
|
pesPkt := pes.Packet{
|
2019-04-05 08:58:25 +03:00
|
|
|
StreamID: e.streamID,
|
2018-08-19 14:58:20 +03:00
|
|
|
PDI: hasPTS,
|
2020-05-03 08:38:15 +03:00
|
|
|
PTS: 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,
|
2020-05-06 13:40:55 +03:00
|
|
|
PID: uint16(e.mediaPID),
|
2018-08-19 14:58:20 +03:00
|
|
|
RAI: pusi,
|
2020-05-06 13:40:55 +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.
|
2020-05-03 08:38:15 +03:00
|
|
|
pcr := e.pcr()
|
|
|
|
e.log.Debug("new access unit", "PCR", pcr, "PTS", pts)
|
|
|
|
pkt.PCR = pcr
|
2018-08-19 14:58:20 +03:00
|
|
|
pusi = false
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
2021-02-03 04:47:23 +03:00
|
|
|
|
|
|
|
b := pkt.Bytes(e.tsSpace[:PacketSize])
|
|
|
|
e.log.Debug("writing MTS packet to destination", "size", len(b), "pusi", pusi, "PID", pkt.PID, "PTS", pts, "PCR", pkt.PCR)
|
|
|
|
_, err := e.dst.Write(b)
|
2018-10-19 03:50:08 +03:00
|
|
|
if err != nil {
|
2022-05-27 05:28:53 +03:00
|
|
|
return len(data), fmt.Errorf("could not write MTS packet to destination: %w",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,
|
2020-05-12 11:45:37 +03:00
|
|
|
Payload: psi.AddPadding(e.patBytes),
|
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 {
|
2022-05-27 05:28:53 +03:00
|
|
|
return fmt.Errorf("could not write pat packet: %w",err)
|
2018-11-21 05:04:00 +03:00
|
|
|
}
|
2019-02-16 09:03:39 +03:00
|
|
|
e.pktCount++
|
2020-05-03 08:38:15 +03:00
|
|
|
|
2020-05-12 11:45:37 +03:00
|
|
|
e.pmtBytes, err = updateMeta(e.pmtBytes, e.log)
|
2019-02-05 15:48:05 +03:00
|
|
|
if err != nil {
|
2022-05-27 05:28:53 +03:00
|
|
|
return fmt.Errorf("could not update pmt metadata: %w",err)
|
2019-01-30 08:07:15 +03:00
|
|
|
}
|
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,
|
2020-05-12 11:45:37 +03:00
|
|
|
Payload: psi.AddPadding(e.pmtBytes),
|
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 {
|
2022-05-27 05:28:53 +03:00
|
|
|
return fmt.Errorf("could not write pmt packet: %w",err)
|
2018-11-21 05:04:00 +03:00
|
|
|
}
|
2019-02-16 09:03:39 +03:00
|
|
|
e.pktCount++
|
2020-05-03 08:38:15 +03:00
|
|
|
|
|
|
|
e.log.Debug("PSI written", "PAT CC", patPkt.CC, "PMT CC", pmtPkt.CC)
|
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.
|
2019-09-15 02:57:26 +03:00
|
|
|
func (e *Encoder) ccFor(pid uint16) byte {
|
2018-08-19 14:09:57 +03:00
|
|
|
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.
|
2021-01-27 09:07:33 +03:00
|
|
|
func updateMeta(b []byte, log logger.LoggerIF) ([]byte, error) {
|
2019-02-05 15:54:00 +03:00
|
|
|
p := psi.PSIBytes(b)
|
2019-08-26 06:59:07 +03:00
|
|
|
if RealTime.IsSet() {
|
2020-05-03 08:38:15 +03:00
|
|
|
t := strconv.Itoa(int(RealTime.Get().Unix()))
|
2021-02-22 08:47:36 +03:00
|
|
|
Meta.Add(TimestampKey, t)
|
2020-05-03 08:38:15 +03:00
|
|
|
log.Debug("latest time added to meta", "time", t)
|
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 {
|
2020-05-03 08:38:15 +03:00
|
|
|
e.log.Debug("closing encoder")
|
2019-04-15 02:12:56 +03:00
|
|
|
return e.dst.Close()
|
|
|
|
}
|