Use the optimal allocation function for opportunistic allocation. (#4030)

* Use the optimal allocation function for opportunistic allocation.

Allocation functions set the `lastAllocation` state also.
This might have been causing an e2e failure with v1 client on migration.

* annotate args
This commit is contained in:
Raja Subramanian
2025-10-26 00:27:41 +05:30
committed by GitHub
parent a2ce73e0d0
commit 81fbd3551a
3 changed files with 38 additions and 25 deletions
+2 -1
View File
@@ -413,7 +413,8 @@ func NewDownTrack(params DownTrackParams) (*DownTrack, error) {
d.forwarder = NewForwarder(
d.kind,
d.params.Logger,
false,
false, // skipReferenceTS
false, // disableOpportunisticAllocation
d.rtpStats,
)
+29 -23
View File
@@ -214,13 +214,14 @@ func (r refInfo) MarshalLogObject(e zapcore.ObjectEncoder) error {
// -------------------------------------------------------------------
type Forwarder struct {
lock sync.RWMutex
mime mime.MimeType
clockRate uint32
kind webrtc.RTPCodecType
logger logger.Logger
skipReferenceTS bool
rtpStats *rtpstats.RTPStatsSender
lock sync.RWMutex
mime mime.MimeType
clockRate uint32
kind webrtc.RTPCodecType
logger logger.Logger
skipReferenceTS bool
disableOpportunisticAllocation bool
rtpStats *rtpstats.RTPStatsSender
muted bool
pubMuted bool
@@ -253,20 +254,22 @@ func NewForwarder(
kind webrtc.RTPCodecType,
logger logger.Logger,
skipReferenceTS bool,
disableOpportunisticAllocation bool,
rtpStats *rtpstats.RTPStatsSender,
) *Forwarder {
f := &Forwarder{
mime: mime.MimeTypeUnknown,
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),
mime: mime.MimeTypeUnknown,
kind: kind,
logger: logger,
skipReferenceTS: skipReferenceTS,
disableOpportunisticAllocation: disableOpportunisticAllocation,
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 {
@@ -556,12 +559,11 @@ func (f *Forwarder) SetMaxSpatialLayer(spatialLayer int32) (bool, buffer.VideoLa
f.logger.Debugw("setting max spatial layer", "layer", spatialLayer)
f.vls.SetMaxSpatial(spatialLayer)
newMax := f.vls.GetMax()
if f.vls.GetTarget().Spatial == buffer.InvalidLayerSpatial && !f.isDeficientLocked() {
f.logger.Debugw("opportunistically setting target spatial layer", "layer", newMax.Spatial)
f.vls.SetTarget(newMax)
if !f.disableOpportunisticAllocation && f.vls.GetTarget().Spatial == buffer.InvalidLayerSpatial && !f.isDeficientLocked() {
f.logger.Debugw("opportunistically setting target spatial layer", "layer", spatialLayer)
f.allocateOptimalLocked(nil, Bitrates{}, true, false)
}
return true, newMax
return true, f.vls.GetMax()
}
func (f *Forwarder) SetMaxTemporalLayer(temporalLayer int32) (bool, buffer.VideoLayer) {
@@ -757,6 +759,10 @@ func (f *Forwarder) AllocateOptimal(availableLayers []int32, brs Bitrates, allow
f.lock.Lock()
defer f.lock.Unlock()
return f.allocateOptimalLocked(availableLayers, brs, allowOvershoot, hold)
}
func (f *Forwarder) allocateOptimalLocked(availableLayers []int32, brs Bitrates, allowOvershoot bool, hold bool) VideoAllocation {
if f.kind == webrtc.RTPCodecTypeAudio {
return f.lastAllocation
}
+7 -1
View File
@@ -33,7 +33,13 @@ func disable(f *Forwarder) {
}
func newForwarder(codec webrtc.RTPCodecCapability, kind webrtc.RTPCodecType) *Forwarder {
f := NewForwarder(kind, logger.GetLogger(), true, nil)
f := NewForwarder(
kind,
logger.GetLogger(),
true, // skipReferenceTS
true, // disableOpportunisticAllocation
nil,
)
f.DetermineCodec(codec, nil, livekit.VideoLayer_MODE_UNUSED)
return f
}