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.forwarder = NewForwarder(
d.kind, d.kind,
d.params.Logger, d.params.Logger,
false, false, // skipReferenceTS
false, // disableOpportunisticAllocation
d.rtpStats, d.rtpStats,
) )
+29 -23
View File
@@ -214,13 +214,14 @@ func (r refInfo) MarshalLogObject(e zapcore.ObjectEncoder) error {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
type Forwarder struct { type Forwarder struct {
lock sync.RWMutex lock sync.RWMutex
mime mime.MimeType mime mime.MimeType
clockRate uint32 clockRate uint32
kind webrtc.RTPCodecType kind webrtc.RTPCodecType
logger logger.Logger logger logger.Logger
skipReferenceTS bool skipReferenceTS bool
rtpStats *rtpstats.RTPStatsSender disableOpportunisticAllocation bool
rtpStats *rtpstats.RTPStatsSender
muted bool muted bool
pubMuted bool pubMuted bool
@@ -253,20 +254,22 @@ func NewForwarder(
kind webrtc.RTPCodecType, kind webrtc.RTPCodecType,
logger logger.Logger, logger logger.Logger,
skipReferenceTS bool, skipReferenceTS bool,
disableOpportunisticAllocation bool,
rtpStats *rtpstats.RTPStatsSender, rtpStats *rtpstats.RTPStatsSender,
) *Forwarder { ) *Forwarder {
f := &Forwarder{ f := &Forwarder{
mime: mime.MimeTypeUnknown, mime: mime.MimeTypeUnknown,
kind: kind, kind: kind,
logger: logger, logger: logger,
skipReferenceTS: skipReferenceTS, skipReferenceTS: skipReferenceTS,
rtpStats: rtpStats, disableOpportunisticAllocation: disableOpportunisticAllocation,
referenceLayerSpatial: buffer.InvalidLayerSpatial, rtpStats: rtpStats,
lastAllocation: VideoAllocationDefault, referenceLayerSpatial: buffer.InvalidLayerSpatial,
lastReferencePayloadType: -1, lastAllocation: VideoAllocationDefault,
rtpMunger: NewRTPMunger(logger), lastReferencePayloadType: -1,
vls: videolayerselector.NewNull(logger), rtpMunger: NewRTPMunger(logger),
codecMunger: codecmunger.NewNull(logger), vls: videolayerselector.NewNull(logger),
codecMunger: codecmunger.NewNull(logger),
} }
if f.kind == webrtc.RTPCodecTypeVideo { 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.logger.Debugw("setting max spatial layer", "layer", spatialLayer)
f.vls.SetMaxSpatial(spatialLayer) f.vls.SetMaxSpatial(spatialLayer)
newMax := f.vls.GetMax() if !f.disableOpportunisticAllocation && f.vls.GetTarget().Spatial == buffer.InvalidLayerSpatial && !f.isDeficientLocked() {
if f.vls.GetTarget().Spatial == buffer.InvalidLayerSpatial && !f.isDeficientLocked() { f.logger.Debugw("opportunistically setting target spatial layer", "layer", spatialLayer)
f.logger.Debugw("opportunistically setting target spatial layer", "layer", newMax.Spatial) f.allocateOptimalLocked(nil, Bitrates{}, true, false)
f.vls.SetTarget(newMax)
} }
return true, newMax return true, f.vls.GetMax()
} }
func (f *Forwarder) SetMaxTemporalLayer(temporalLayer int32) (bool, buffer.VideoLayer) { 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() f.lock.Lock()
defer f.lock.Unlock() 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 { if f.kind == webrtc.RTPCodecTypeAudio {
return f.lastAllocation return f.lastAllocation
} }
+7 -1
View File
@@ -33,7 +33,13 @@ func disable(f *Forwarder) {
} }
func newForwarder(codec webrtc.RTPCodecCapability, kind webrtc.RTPCodecType) *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) f.DetermineCodec(codec, nil, livekit.VideoLayer_MODE_UNUSED)
return f return f
} }