2023-01-05 11:54:00 +00:00
|
|
|
package formatprocessor
|
2022-12-13 20:26:35 +00:00
|
|
|
|
|
|
|
import (
|
2022-12-14 14:41:59 +00:00
|
|
|
"bytes"
|
2022-12-13 20:26:35 +00:00
|
|
|
"time"
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/formats"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/formats/rtph265"
|
|
|
|
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
|
2022-12-13 20:26:35 +00:00
|
|
|
"github.com/pion/rtp"
|
2023-05-04 18:16:41 +00:00
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2022-12-13 20:26:35 +00:00
|
|
|
)
|
|
|
|
|
2022-12-14 14:41:59 +00:00
|
|
|
// extract VPS, SPS and PPS without decoding RTP packets
|
|
|
|
func rtpH265ExtractVPSSPSPPS(pkt *rtp.Packet) ([]byte, []byte, []byte) {
|
|
|
|
if len(pkt.Payload) < 2 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
typ := h265.NALUType((pkt.Payload[0] >> 1) & 0b111111)
|
|
|
|
|
|
|
|
switch typ {
|
2022-12-28 19:32:03 +00:00
|
|
|
case h265.NALUType_VPS_NUT:
|
2022-12-14 14:41:59 +00:00
|
|
|
return pkt.Payload, nil, nil
|
|
|
|
|
2022-12-28 19:32:03 +00:00
|
|
|
case h265.NALUType_SPS_NUT:
|
2022-12-14 14:41:59 +00:00
|
|
|
return nil, pkt.Payload, nil
|
|
|
|
|
2022-12-28 19:32:03 +00:00
|
|
|
case h265.NALUType_PPS_NUT:
|
2022-12-14 14:41:59 +00:00
|
|
|
return nil, nil, pkt.Payload
|
|
|
|
|
2022-12-28 19:32:03 +00:00
|
|
|
case h265.NALUType_AggregationUnit:
|
2022-12-14 14:41:59 +00:00
|
|
|
payload := pkt.Payload[2:]
|
|
|
|
var vps []byte
|
|
|
|
var sps []byte
|
|
|
|
var pps []byte
|
|
|
|
|
|
|
|
for len(payload) > 0 {
|
|
|
|
if len(payload) < 2 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
size := uint16(payload[0])<<8 | uint16(payload[1])
|
|
|
|
payload = payload[2:]
|
|
|
|
|
|
|
|
if size == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(size) > len(payload) {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nalu := payload[:size]
|
|
|
|
payload = payload[size:]
|
|
|
|
|
|
|
|
typ = h265.NALUType((pkt.Payload[0] >> 1) & 0b111111)
|
|
|
|
|
|
|
|
switch typ {
|
2022-12-28 19:32:03 +00:00
|
|
|
case h265.NALUType_VPS_NUT:
|
2022-12-14 14:41:59 +00:00
|
|
|
vps = nalu
|
|
|
|
|
2022-12-28 19:32:03 +00:00
|
|
|
case h265.NALUType_SPS_NUT:
|
2022-12-14 14:41:59 +00:00
|
|
|
sps = nalu
|
|
|
|
|
2022-12-28 19:32:03 +00:00
|
|
|
case h265.NALUType_PPS_NUT:
|
2022-12-14 14:41:59 +00:00
|
|
|
pps = nalu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vps, sps, pps
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 11:44:59 +00:00
|
|
|
// UnitH265 is a H265 data unit.
|
|
|
|
type UnitH265 struct {
|
2023-01-05 11:54:00 +00:00
|
|
|
RTPPackets []*rtp.Packet
|
|
|
|
NTP time.Time
|
|
|
|
PTS time.Duration
|
|
|
|
AU [][]byte
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 11:44:59 +00:00
|
|
|
// GetRTPPackets implements Unit.
|
|
|
|
func (d *UnitH265) GetRTPPackets() []*rtp.Packet {
|
2023-01-05 11:54:00 +00:00
|
|
|
return d.RTPPackets
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 11:44:59 +00:00
|
|
|
// GetNTP implements Unit.
|
|
|
|
func (d *UnitH265) GetNTP() time.Time {
|
2023-01-05 11:54:00 +00:00
|
|
|
return d.NTP
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type formatProcessorH265 struct {
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize int
|
2023-04-01 16:39:12 +00:00
|
|
|
format *formats.H265
|
2023-05-04 18:16:41 +00:00
|
|
|
log logger.Writer
|
2022-12-13 20:26:35 +00:00
|
|
|
|
2023-05-18 16:00:53 +00:00
|
|
|
encoder *rtph265.Encoder
|
|
|
|
decoder *rtph265.Decoder
|
|
|
|
lastKeyFrameTimeReceived bool
|
|
|
|
lastKeyFrameTime time.Time
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
func newH265(
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize int,
|
2023-04-01 16:39:12 +00:00
|
|
|
forma *formats.H265,
|
2023-05-04 18:16:41 +00:00
|
|
|
generateRTPPackets bool,
|
|
|
|
log logger.Writer,
|
2022-12-13 20:26:35 +00:00
|
|
|
) (*formatProcessorH265, error) {
|
|
|
|
t := &formatProcessorH265{
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize: udpMaxPayloadSize,
|
|
|
|
format: forma,
|
2023-05-04 18:16:41 +00:00
|
|
|
log: log,
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
if generateRTPPackets {
|
2023-04-25 16:13:51 +00:00
|
|
|
t.encoder = &rtph265.Encoder{
|
|
|
|
PayloadMaxSize: t.udpMaxPayloadSize - 12,
|
|
|
|
PayloadType: forma.PayloadTyp,
|
|
|
|
}
|
|
|
|
t.encoder.Init()
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *formatProcessorH265) updateTrackParametersFromRTPPacket(pkt *rtp.Packet) {
|
2022-12-14 14:41:59 +00:00
|
|
|
vps, sps, pps := rtpH265ExtractVPSSPSPPS(pkt)
|
2023-04-01 16:39:12 +00:00
|
|
|
update := false
|
2022-12-14 14:41:59 +00:00
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
if vps != nil && !bytes.Equal(vps, t.format.VPS) {
|
|
|
|
update = true
|
2022-12-14 14:41:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
if sps != nil && !bytes.Equal(sps, t.format.SPS) {
|
|
|
|
update = true
|
2022-12-14 14:41:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
if pps != nil && !bytes.Equal(pps, t.format.PPS) {
|
|
|
|
update = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if update {
|
|
|
|
if vps == nil {
|
|
|
|
vps = t.format.VPS
|
|
|
|
}
|
|
|
|
if sps == nil {
|
|
|
|
sps = t.format.SPS
|
|
|
|
}
|
|
|
|
if pps == nil {
|
|
|
|
pps = t.format.PPS
|
|
|
|
}
|
|
|
|
t.format.SafeSetParams(vps, sps, pps)
|
2022-12-14 14:41:59 +00:00
|
|
|
}
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *formatProcessorH265) updateTrackParametersFromNALUs(nalus [][]byte) {
|
2023-04-01 16:39:12 +00:00
|
|
|
vps := t.format.VPS
|
|
|
|
sps := t.format.SPS
|
|
|
|
pps := t.format.PPS
|
|
|
|
update := false
|
|
|
|
|
2022-12-29 19:46:31 +00:00
|
|
|
for _, nalu := range nalus {
|
|
|
|
typ := h265.NALUType((nalu[0] >> 1) & 0b111111)
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case h265.NALUType_VPS_NUT:
|
2023-04-01 16:39:12 +00:00
|
|
|
if !bytes.Equal(nalu, t.format.VPS) {
|
|
|
|
vps = nalu
|
|
|
|
update = true
|
2022-12-29 19:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case h265.NALUType_SPS_NUT:
|
2023-04-01 16:39:12 +00:00
|
|
|
if !bytes.Equal(nalu, t.format.SPS) {
|
|
|
|
sps = nalu
|
|
|
|
update = true
|
2022-12-29 19:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case h265.NALUType_PPS_NUT:
|
2023-04-01 16:39:12 +00:00
|
|
|
if !bytes.Equal(nalu, t.format.PPS) {
|
|
|
|
pps = nalu
|
|
|
|
update = true
|
2022-12-29 19:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 16:39:12 +00:00
|
|
|
|
|
|
|
if update {
|
|
|
|
t.format.SafeSetParams(vps, sps, pps)
|
|
|
|
}
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:00:53 +00:00
|
|
|
func (t *formatProcessorH265) checkKeyFrameInterval(ntp time.Time, isKeyFrame bool) {
|
|
|
|
if !t.lastKeyFrameTimeReceived || isKeyFrame {
|
|
|
|
t.lastKeyFrameTimeReceived = true
|
|
|
|
t.lastKeyFrameTime = ntp
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ntp.Sub(t.lastKeyFrameTime) >= maxKeyFrameInterval {
|
|
|
|
t.lastKeyFrameTime = ntp
|
|
|
|
t.log.Log(logger.Warn, "no H265 key frames received in %v, stream can't be decoded", maxKeyFrameInterval)
|
2023-05-04 18:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:00:53 +00:00
|
|
|
func (t *formatProcessorH265) remuxAccessUnit(ntp time.Time, nalus [][]byte) [][]byte {
|
2023-05-04 18:16:41 +00:00
|
|
|
isKeyFrame := false
|
2022-12-29 19:46:31 +00:00
|
|
|
n := 0
|
|
|
|
|
|
|
|
for _, nalu := range nalus {
|
|
|
|
typ := h265.NALUType((nalu[0] >> 1) & 0b111111)
|
|
|
|
|
|
|
|
switch typ {
|
2023-05-04 18:16:41 +00:00
|
|
|
case h265.NALUType_VPS_NUT, h265.NALUType_SPS_NUT, h265.NALUType_PPS_NUT: // parameters: remove
|
2022-12-29 19:46:31 +00:00
|
|
|
continue
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
case h265.NALUType_AUD_NUT: // AUD: remove
|
2022-12-29 19:46:31 +00:00
|
|
|
continue
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
case h265.NALUType_IDR_W_RADL, h265.NALUType_IDR_N_LP, h265.NALUType_CRA_NUT: // key frame
|
|
|
|
if !isKeyFrame {
|
|
|
|
isKeyFrame = true
|
2023-01-05 12:10:28 +00:00
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
// prepend parameters
|
2023-04-01 16:39:12 +00:00
|
|
|
if t.format.VPS != nil && t.format.SPS != nil && t.format.PPS != nil {
|
2023-01-05 12:10:28 +00:00
|
|
|
n += 3
|
|
|
|
}
|
2022-12-29 19:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:00:53 +00:00
|
|
|
t.checkKeyFrameInterval(ntp, isKeyFrame)
|
2023-05-04 18:16:41 +00:00
|
|
|
|
2022-12-29 19:46:31 +00:00
|
|
|
if n == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredNALUs := make([][]byte, n)
|
|
|
|
i := 0
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
if isKeyFrame && t.format.VPS != nil && t.format.SPS != nil && t.format.PPS != nil {
|
2023-04-01 16:39:12 +00:00
|
|
|
filteredNALUs[0] = t.format.VPS
|
|
|
|
filteredNALUs[1] = t.format.SPS
|
|
|
|
filteredNALUs[2] = t.format.PPS
|
2022-12-29 19:46:31 +00:00
|
|
|
i = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nalu := range nalus {
|
|
|
|
typ := h265.NALUType((nalu[0] >> 1) & 0b111111)
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case h265.NALUType_VPS_NUT, h265.NALUType_SPS_NUT, h265.NALUType_PPS_NUT:
|
|
|
|
continue
|
|
|
|
|
|
|
|
case h265.NALUType_AUD_NUT:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredNALUs[i] = nalu
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredNALUs
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 11:44:59 +00:00
|
|
|
func (t *formatProcessorH265) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
|
|
|
|
tunit := unit.(*UnitH265)
|
2022-12-13 20:26:35 +00:00
|
|
|
|
2023-03-10 11:44:59 +00:00
|
|
|
if tunit.RTPPackets != nil {
|
|
|
|
pkt := tunit.RTPPackets[0]
|
2022-12-13 20:26:35 +00:00
|
|
|
t.updateTrackParametersFromRTPPacket(pkt)
|
|
|
|
|
|
|
|
if t.encoder == nil {
|
|
|
|
// remove padding
|
|
|
|
pkt.Header.Padding = false
|
|
|
|
pkt.PaddingSize = 0
|
|
|
|
|
2022-12-14 17:50:08 +00:00
|
|
|
// RTP packets exceed maximum size: start re-encoding them
|
2023-03-31 09:53:49 +00:00
|
|
|
if pkt.MarshalSize() > t.udpMaxPayloadSize {
|
2022-12-14 17:50:08 +00:00
|
|
|
v1 := pkt.SSRC
|
|
|
|
v2 := pkt.SequenceNumber
|
|
|
|
v3 := pkt.Timestamp
|
|
|
|
t.encoder = &rtph265.Encoder{
|
2023-03-31 09:53:49 +00:00
|
|
|
PayloadMaxSize: t.udpMaxPayloadSize - 12,
|
2022-12-14 17:50:08 +00:00
|
|
|
PayloadType: pkt.PayloadType,
|
|
|
|
SSRC: &v1,
|
|
|
|
InitialSequenceNumber: &v2,
|
|
|
|
InitialTimestamp: &v3,
|
|
|
|
MaxDONDiff: t.format.MaxDONDiff,
|
|
|
|
}
|
|
|
|
t.encoder.Init()
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode from RTP
|
2023-05-14 12:18:03 +00:00
|
|
|
if hasNonRTSPReaders || t.decoder != nil || t.encoder != nil {
|
2022-12-13 20:26:35 +00:00
|
|
|
if t.decoder == nil {
|
|
|
|
t.decoder = t.format.CreateDecoder()
|
|
|
|
}
|
|
|
|
|
2022-12-18 22:16:20 +00:00
|
|
|
if t.encoder != nil {
|
2023-03-10 11:44:59 +00:00
|
|
|
tunit.RTPPackets = nil
|
2022-12-18 22:16:20 +00:00
|
|
|
}
|
2022-12-14 17:50:08 +00:00
|
|
|
|
|
|
|
// DecodeUntilMarker() is necessary, otherwise Encode() generates partial groups
|
2023-03-14 17:24:33 +00:00
|
|
|
au, pts, err := t.decoder.DecodeUntilMarker(pkt)
|
2022-12-13 20:26:35 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == rtph265.ErrNonStartingPacketAndNoPrevious || err == rtph265.ErrMorePacketsNeeded {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:00:53 +00:00
|
|
|
tunit.AU = t.remuxAccessUnit(tunit.NTP, au)
|
2023-03-14 17:24:33 +00:00
|
|
|
tunit.PTS = pts
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// route packet as is
|
|
|
|
if t.encoder == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-10 11:44:59 +00:00
|
|
|
t.updateTrackParametersFromNALUs(tunit.AU)
|
2023-05-18 16:00:53 +00:00
|
|
|
tunit.AU = t.remuxAccessUnit(tunit.NTP, tunit.AU)
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 16:13:51 +00:00
|
|
|
// encode into RTP
|
2023-03-10 11:44:59 +00:00
|
|
|
if len(tunit.AU) != 0 {
|
|
|
|
pkts, err := t.encoder.Encode(tunit.AU, tunit.PTS)
|
2023-02-23 13:46:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-10 11:44:59 +00:00
|
|
|
tunit.RTPPackets = pkts
|
2023-02-23 13:46:26 +00:00
|
|
|
} else {
|
2023-03-10 11:44:59 +00:00
|
|
|
tunit.RTPPackets = nil
|
2022-12-14 17:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
2023-05-14 12:18:03 +00:00
|
|
|
|
|
|
|
func (t *formatProcessorH265) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
|
|
|
|
return &UnitH265{
|
|
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
|
|
NTP: ntp,
|
|
|
|
}
|
|
|
|
}
|