H265 supoort and codec regression (#3358)

* H265 supoort and codec regression

Support H265 codec.
Add optional codec regression for subscribers don't
support advanced codecs like H265, AV1, VP9.

* restart forwarder on upstream codec change

* tests

* Reneogitate new codec if client doesn't support change

* Add option to disable codec regression

---------

Co-authored-by: boks1971 <raja.gobi@tutanota.com>
This commit is contained in:
cnderrauber
2025-02-06 11:56:49 +08:00
committed by GitHub
co-authored by boks1971
parent abcaa9b8ab
commit aeec75edeb
27 changed files with 1423 additions and 201 deletions
+66 -26
View File
@@ -220,15 +220,16 @@ type Forwarder struct {
pubMuted bool
resumeBehindThreshold float64
started bool
preStartTime time.Time
extFirstTS uint64
lastSSRC uint32
lastSwitchExtIncomingTS uint64
referenceLayerSpatial int32
dummyStartTSOffset uint64
refInfos [buffer.DefaultMaxLayerSpatial + 1]refInfo
refIsSVC bool
started bool
preStartTime time.Time
extFirstTS uint64
lastSSRC uint32
lastReferencePayloadType int8
lastSwitchExtIncomingTS uint64
referenceLayerSpatial int32
dummyStartTSOffset uint64
refInfos [buffer.DefaultMaxLayerSpatial + 1]refInfo
refIsSVC bool
provisional *VideoAllocationProvisional
@@ -248,15 +249,16 @@ func NewForwarder(
rtpStats *rtpstats.RTPStatsSender,
) *Forwarder {
f := &Forwarder{
kind: kind,
logger: logger,
skipReferenceTS: skipReferenceTS,
rtpStats: rtpStats,
referenceLayerSpatial: buffer.InvalidLayerSpatial,
lastAllocation: VideoAllocationDefault,
rtpMunger: NewRTPMunger(logger),
vls: videolayerselector.NewNull(logger),
codecMunger: codecmunger.NewNull(logger),
kind: kind,
logger: logger,
skipReferenceTS: skipReferenceTS,
rtpStats: rtpStats,
referenceLayerSpatial: buffer.InvalidLayerSpatial,
lastAllocation: VideoAllocationDefault,
lastReferencePayloadType: -1,
rtpMunger: NewRTPMunger(logger),
vls: videolayerselector.NewNull(logger),
codecMunger: codecmunger.NewNull(logger),
}
if f.kind == webrtc.RTPCodecTypeVideo {
@@ -297,8 +299,9 @@ func (f *Forwarder) DetermineCodec(codec webrtc.RTPCodecCapability, extensions [
f.lock.Lock()
defer f.lock.Unlock()
if f.codec.MimeType != "" {
return
codecChanged := f.codec.MimeType != "" && f.codec.MimeType != codec.MimeType
if codecChanged {
f.logger.Debugw("forwarder codec changed", "from", f.codec.MimeType, "to", codec.MimeType)
}
f.codec = codec
@@ -315,15 +318,21 @@ func (f *Forwarder) DetermineCodec(codec webrtc.RTPCodecCapability, extensions [
case "video/vp8":
f.codecMunger = codecmunger.NewVP8FromNull(f.codecMunger, f.logger)
if f.vls != nil {
f.vls = videolayerselector.NewSimulcastFromNull(f.vls)
if vls := videolayerselector.NewSimulcastFromOther(f.vls); vls != nil {
f.vls = vls
} else {
f.logger.Errorw("failed to create simulcast on codec change", nil)
}
} else {
f.vls = videolayerselector.NewSimulcast(f.logger)
}
f.vls.SetTemporalLayerSelector(temporallayerselector.NewVP8(f.logger))
case "video/h264":
fallthrough
case "video/h265":
if f.vls != nil {
f.vls = videolayerselector.NewSimulcastFromNull(f.vls)
f.vls = videolayerselector.NewSimulcastFromOther(f.vls)
} else {
f.vls = videolayerselector.NewSimulcast(f.logger)
}
@@ -357,7 +366,7 @@ func (f *Forwarder) DetermineCodec(codec webrtc.RTPCodecCapability, extensions [
}
} else {
if f.vls != nil {
f.vls = videolayerselector.NewSimulcastFromNull(f.vls)
f.vls = videolayerselector.NewSimulcastFromOther(f.vls)
} else {
f.vls = videolayerselector.NewSimulcast(f.logger)
}
@@ -1566,6 +1575,22 @@ func (f *Forwarder) CheckSync() (bool, int32) {
return true, layer
}
func (f *Forwarder) Restart() {
f.lock.Lock()
defer f.lock.Unlock()
f.resyncLocked()
f.setTargetLayer(buffer.InvalidLayer, buffer.InvalidLayerSpatial)
f.referenceLayerSpatial = buffer.InvalidLayerSpatial
f.lastReferencePayloadType = -1
for layer := 0; layer < len(f.refInfos); layer++ {
f.refInfos[layer] = refInfo{}
}
f.lastSwitchExtIncomingTS = 0
f.refIsSVC = false
}
func (f *Forwarder) FilterRTX(nacks []uint16) (filtered []uint16, disallowedLayers [buffer.DefaultMaxLayerSpatial + 1]bool) {
f.lock.RLock()
defer f.lock.RUnlock()
@@ -1738,6 +1763,12 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
// potentially happening very quickly. Erroring out and waiting for a layer for which a sender report has been
// received will calculate a better offset, but may result in initial adaptation to take a bit longer depending
// on how often publisher/remote side sends RTCP sender report.
f.logger.Debugw(
"could not get ref layer timestamp",
"referenceLayerSpatial", f.referenceLayerSpatial,
"layer", layer,
"error", err,
)
return err
}
}
@@ -1757,6 +1788,10 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
tsExt, err := f.rtpStats.GetExpectedRTPTimestamp(switchingAt)
if err == nil {
extExpectedTS = tsExt
if f.lastReferencePayloadType == -1 {
f.dummyStartTSOffset = extExpectedTS - uint64(refTS)
extRefTS = extExpectedTS
}
} else {
if !f.preStartTime.IsZero() {
timeSinceFirst := time.Since(f.preStartTime)
@@ -1834,6 +1869,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
// AVSYNC-TODO: Consider some forcing function to do the switch
// (like "have waited for too long for layer switch, nothing available, switch to whatever is available" kind of condition).
logTransition("layer switch, reference too far behind", extExpectedTS, extRefTS, extLastTS, diffSeconds)
return errors.New("switch point too far behind")
}
@@ -1918,9 +1954,12 @@ func (f *Forwarder) getTranslationParamsCommon(extPkt *buffer.ExtPacket, layer i
f.vls.Rollback()
return nil
}
f.logger.Debugw("switching feed",
"from", f.lastSSRC,
"to", extPkt.Packet.SSRC,
f.logger.Debugw(
"switching feed",
"fromSSRC", f.lastSSRC,
"toSSRC", extPkt.Packet.SSRC,
"fromPayloadType", f.lastReferencePayloadType,
"toPayloadType", extPkt.Packet.PayloadType,
"layer", layer,
"refInfos", logger.ObjectSlice(f.refInfos[:]),
"lastSwitchExtIncomingTS", f.lastSwitchExtIncomingTS,
@@ -1929,6 +1968,7 @@ func (f *Forwarder) getTranslationParamsCommon(extPkt *buffer.ExtPacket, layer i
"maxLayer", f.vls.GetMax(),
)
f.lastSSRC = extPkt.Packet.SSRC
f.lastReferencePayloadType = int8(extPkt.Packet.PayloadType)
f.lastSwitchExtIncomingTS = extPkt.ExtTimestamp
}