A couple of stream allocator tweaks (#1628)
* A coupke of stream allocator tweaks - Do not overshoot on catch up. It so happens that during probe the next higher layer is at some bit rate which is much lower than normal bit rate for that layer. But, by the time the probe ends, publisher has climbed up to normal bit rate. So, the probe goal although achieved is not enough. Allowing overshoot latches on the next layer which might be more than the channel capacity. - Use a collapse window to record values in case of a only one or two changes in an evaluation window. Some times it happens that the estimate falls once or twice and stays there. By collapsing repeated values, it could be a long time before that fall in estimate is processed. Introduce a collapse window and record duplicate value if a value was not recorded for collapse window duration. This allows delayed processing of those isolated falls in estimate. * minor clean up * add a probe max rate * fix max * use max of committed, expected for max limiting * have to probe at goal
This commit is contained in:
@@ -59,7 +59,7 @@ type ChannelObserverParams struct {
|
||||
Name string
|
||||
EstimateRequiredSamples int
|
||||
EstimateDownwardTrendThreshold float64
|
||||
EstimateCollapseValues bool
|
||||
EstimateCollapseThreshold time.Duration
|
||||
NackWindowMinDuration time.Duration
|
||||
NackWindowMaxDuration time.Duration
|
||||
NackRatioThreshold float64
|
||||
@@ -86,7 +86,7 @@ func NewChannelObserver(params ChannelObserverParams, logger logger.Logger) *Cha
|
||||
Logger: logger,
|
||||
RequiredSamples: params.EstimateRequiredSamples,
|
||||
DownwardTrendThreshold: params.EstimateDownwardTrendThreshold,
|
||||
CollapseValues: params.EstimateCollapseValues,
|
||||
CollapseThreshold: params.EstimateCollapseThreshold,
|
||||
}),
|
||||
nackTracker: NewNackTracker(NackTrackerParams{
|
||||
Name: params.Name + "-estimate",
|
||||
|
||||
@@ -488,7 +488,7 @@ func (c *Cluster) Process(pl ProberListener) {
|
||||
pl.OnSendProbe(bytesShortFall)
|
||||
}
|
||||
|
||||
// LK-TODO look at adapting sleep time based on how many bytes and how much time is left
|
||||
// STREAM-ALLOCATOR-TODO look at adapting sleep time based on how many bytes and how much time is left
|
||||
}
|
||||
|
||||
func (c *Cluster) String() string {
|
||||
|
||||
@@ -45,7 +45,8 @@ const (
|
||||
FlagAllowOvershootWhileDeficient = false
|
||||
FlagAllowOvershootExemptTrackWhileDeficient = true
|
||||
FlagAllowOvershootInProbe = true
|
||||
FlagAllowOvershootInCatchup = true
|
||||
FlagAllowOvershootInCatchup = false
|
||||
FlagAllowOvershootInBoost = true
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -55,7 +56,7 @@ var (
|
||||
Name: "probe",
|
||||
EstimateRequiredSamples: 3,
|
||||
EstimateDownwardTrendThreshold: 0.0,
|
||||
EstimateCollapseValues: false,
|
||||
EstimateCollapseThreshold: 0,
|
||||
NackWindowMinDuration: 500 * time.Millisecond,
|
||||
NackWindowMaxDuration: 1 * time.Second,
|
||||
NackRatioThreshold: 0.04,
|
||||
@@ -65,7 +66,7 @@ var (
|
||||
Name: "non-probe",
|
||||
EstimateRequiredSamples: 8,
|
||||
EstimateDownwardTrendThreshold: -0.5,
|
||||
EstimateCollapseValues: true,
|
||||
EstimateCollapseThreshold: 500 * time.Millisecond,
|
||||
NackWindowMinDuration: 1 * time.Second,
|
||||
NackWindowMaxDuration: 2 * time.Second,
|
||||
NackRatioThreshold: 0.08,
|
||||
@@ -259,7 +260,7 @@ func (s *StreamAllocator) AddTrack(downTrack *sfu.DownTrack, params AddTrackPara
|
||||
|
||||
downTrack.SetStreamAllocatorListener(s)
|
||||
if s.prober.IsRunning() {
|
||||
// LK-TODO: this can be changed to adapt to probe rate
|
||||
// STREAM-ALLOCATOR-TODO: this can be changed to adapt to probe rate
|
||||
downTrack.SetStreamAllocatorReportInterval(50 * time.Millisecond)
|
||||
}
|
||||
|
||||
@@ -273,7 +274,7 @@ func (s *StreamAllocator) RemoveTrack(downTrack *sfu.DownTrack) {
|
||||
}
|
||||
s.videoTracksMu.Unlock()
|
||||
|
||||
// LK-TODO: use any saved bandwidth to re-distribute
|
||||
// STREAM-ALLOCATOR-TODO: use any saved bandwidth to re-distribute
|
||||
s.postEvent(Event{
|
||||
Signal: streamAllocatorSignalAdjustState,
|
||||
})
|
||||
@@ -333,11 +334,11 @@ func (s *StreamAllocator) OnREMB(downTrack *sfu.DownTrack, remb *rtcp.ReceiverEs
|
||||
// callback from previous REMB comes after another down track's callback
|
||||
// from the new REMB. REMBs could fire very quickly especially when
|
||||
// the network is entering congestion.
|
||||
// LK-TODO-START
|
||||
// STREAM-ALLOCATOR-TODO-START
|
||||
// Need to check if the same SSRC reports can somehow race, i.e. does pion send
|
||||
// RTCP dispatch for same SSRC on different threads? If not, the tracking SSRC
|
||||
// should prevent racing
|
||||
// LK-TODO-END
|
||||
// STREAM-ALLOCATOR-TODO-END
|
||||
//
|
||||
|
||||
// if there are no video tracks, ignore any straggler REMB
|
||||
@@ -487,7 +488,7 @@ func (s *StreamAllocator) OnProbeClusterDone(info ProbeClusterInfo) {
|
||||
func (s *StreamAllocator) OnActiveChanged(isActive bool) {
|
||||
for _, t := range s.getTracks() {
|
||||
if isActive {
|
||||
// LK-TODO: this can be changed to adapt to probe rate
|
||||
// STREAM-ALLOCATOR-TODO: this can be changed to adapt to probe rate
|
||||
t.DownTrack().SetStreamAllocatorReportInterval(50 * time.Millisecond)
|
||||
} else {
|
||||
t.DownTrack().ClearStreamAllocatorReportInterval()
|
||||
@@ -662,7 +663,7 @@ func (s *StreamAllocator) handleSignalProbeClusterDone(event *Event) {
|
||||
}
|
||||
|
||||
// ensure probe queue is flushed
|
||||
// LK-TODO: ProbeSettleWait should actually be a certain number of RTTs.
|
||||
// STREAM-ALLOCATOR-TODO: ProbeSettleWait should actually be a certain number of RTTs.
|
||||
lowestEstimate := int64(math.Min(float64(s.committedChannelCapacity), float64(s.channelObserver.GetLowestEstimate())))
|
||||
expectedDuration := float64(info.BytesSent*8*1000) / float64(lowestEstimate)
|
||||
queueTime := expectedDuration - float64(info.Duration.Milliseconds())
|
||||
@@ -746,7 +747,7 @@ func (s *StreamAllocator) handleNewEstimateInProbe() {
|
||||
//
|
||||
// More of a safety net.
|
||||
// In rare cases, the estimate gets stuck. Prevent from probe running amok
|
||||
// LK-TODO: Need more testing here to ensure that probe does not cause a lot of damage
|
||||
// STREAM-ALLOCATOR-TODO: Need more testing here to ensure that probe does not cause a lot of damage
|
||||
//
|
||||
s.params.Logger.Infow("stream allocator: probe: aborting, no trend", "cluster", s.probeClusterId)
|
||||
s.abortProbe()
|
||||
@@ -853,11 +854,11 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
|
||||
|
||||
s.adjustState()
|
||||
return
|
||||
// LK-TODO-START
|
||||
// STREAM-ALLOCATOR-TODO-START
|
||||
// Should use the bits given back to start any paused track.
|
||||
// Note layer downgrade may actually have positive delta (i.e. consume more bits)
|
||||
// because of when the measurement is done. Watch for that.
|
||||
// LK-TODO-END
|
||||
// STREAM_ALLOCATOR-TODO-END
|
||||
}
|
||||
|
||||
//
|
||||
@@ -894,7 +895,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
|
||||
}
|
||||
}
|
||||
|
||||
// LK-TODO if got too much extra, can potentially give it to some deficient track
|
||||
// STREAM-ALLOCATOR-TODO if got too much extra, can potentially give it to some deficient track
|
||||
}
|
||||
|
||||
// commit the track that needs change if enough could be acquired or pause not allowed
|
||||
@@ -1040,7 +1041,7 @@ func (s *StreamAllocator) allocateAllTracks() {
|
||||
update.HandleStreamingChange(true, track)
|
||||
}
|
||||
|
||||
// LK-TODO: optimistic allocation before bitrate is available will return 0. How to account for that?
|
||||
// STREAM-ALLOCATOR-TODO: optimistic allocation before bitrate is available will return 0. How to account for that?
|
||||
availableChannelCapacity -= allocation.BandwidthRequested
|
||||
}
|
||||
|
||||
@@ -1144,11 +1145,25 @@ func (s *StreamAllocator) newChannelObserverNonProbe() *ChannelObserver {
|
||||
return NewChannelObserver(ChannelObserverParamsNonProbe, s.params.Logger)
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) initProbe(probeRateBps int64) {
|
||||
func (s *StreamAllocator) initProbe(probeGoalDeltaBps int64) {
|
||||
s.lastProbeStartTime = time.Now()
|
||||
|
||||
expectedBandwidthUsage := s.getExpectedBandwidthUsage()
|
||||
s.probeGoalBps = expectedBandwidthUsage + probeRateBps
|
||||
if float64(expectedBandwidthUsage) > 1.5*float64(s.committedChannelCapacity) {
|
||||
// STREAM-ALLOCATOR-TODO-START
|
||||
// Should probably skip probing if the expected usage is much higher than committed channel capacity.
|
||||
// But, give that bandwidth estimate is volatile at times and can drop down to small values,
|
||||
// not probing means streaming stuck in a well for long.
|
||||
// Observe this and figure out if there is a threshold from practical use cases that can be used to
|
||||
// skip probing safely
|
||||
// STREAM-ALLOCATOR-TODO-END
|
||||
s.params.Logger.Warnw(
|
||||
"stream allocator: starting probe alarm",
|
||||
fmt.Errorf("expected too high, expected: %d, committed: %d", expectedBandwidthUsage, s.committedChannelCapacity),
|
||||
)
|
||||
}
|
||||
// overshoot a bit to account for noise (in measurement/estimate etc)
|
||||
s.probeGoalBps = expectedBandwidthUsage + ((probeGoalDeltaBps * ProbePct) / 100)
|
||||
|
||||
s.abortedProbeClusterId = ProbeClusterIdInvalid
|
||||
|
||||
@@ -1163,9 +1178,8 @@ func (s *StreamAllocator) initProbe(probeRateBps int64) {
|
||||
s.channelObserver = s.newChannelObserverProbe()
|
||||
s.channelObserver.SeedEstimate(s.lastReceivedEstimate)
|
||||
|
||||
desiredRateBps := int(probeRateBps) + int(math.Max(float64(s.committedChannelCapacity), float64(expectedBandwidthUsage)))
|
||||
s.probeClusterId = s.prober.AddCluster(
|
||||
desiredRateBps,
|
||||
int(s.probeGoalBps),
|
||||
int(expectedBandwidthUsage),
|
||||
ProbeMinDuration,
|
||||
ProbeMaxDuration,
|
||||
@@ -1177,7 +1191,7 @@ func (s *StreamAllocator) initProbe(probeRateBps int64) {
|
||||
"committed", s.committedChannelCapacity,
|
||||
"lastReceived", s.lastReceivedEstimate,
|
||||
"channel", channelState,
|
||||
"probeRateBps", probeRateBps,
|
||||
"probeGoalDeltaBps", probeGoalDeltaBps,
|
||||
"goalBps", s.probeGoalBps,
|
||||
)
|
||||
}
|
||||
@@ -1237,7 +1251,7 @@ func (s *StreamAllocator) maybeProbe() {
|
||||
func (s *StreamAllocator) maybeProbeWithMedia() {
|
||||
// boost deficient track farthest from desired layer
|
||||
for _, track := range s.getMaxDistanceSortedDeficient() {
|
||||
allocation, boosted := track.AllocateNextHigher(ChannelCapacityInfinity, FlagAllowOvershootInCatchup)
|
||||
allocation, boosted := track.AllocateNextHigher(ChannelCapacityInfinity, FlagAllowOvershootInBoost)
|
||||
if !boosted {
|
||||
continue
|
||||
}
|
||||
@@ -1261,12 +1275,7 @@ func (s *StreamAllocator) maybeProbeWithPadding() {
|
||||
continue
|
||||
}
|
||||
|
||||
probeRateBps := (transition.BandwidthDelta * ProbePct) / 100
|
||||
if probeRateBps < ProbeMinBps {
|
||||
probeRateBps = ProbeMinBps
|
||||
}
|
||||
|
||||
s.initProbe(probeRateBps)
|
||||
s.initProbe(transition.BandwidthDelta)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ type TrendDetectorParams struct {
|
||||
Logger logger.Logger
|
||||
RequiredSamples int
|
||||
DownwardTrendThreshold float64
|
||||
CollapseValues bool
|
||||
CollapseThreshold time.Duration
|
||||
}
|
||||
|
||||
type TrendDetector struct {
|
||||
@@ -49,6 +49,9 @@ type TrendDetector struct {
|
||||
lowestValue int64
|
||||
highestValue int64
|
||||
|
||||
hasFallen bool
|
||||
lastSampleAt time.Time
|
||||
|
||||
direction TrendDirection
|
||||
}
|
||||
|
||||
@@ -66,6 +69,8 @@ func (t *TrendDetector) Seed(value int64) {
|
||||
}
|
||||
|
||||
t.values = append(t.values, value)
|
||||
t.lastSampleAt = time.Now()
|
||||
t.hasFallen = false
|
||||
}
|
||||
|
||||
func (t *TrendDetector) AddValue(value int64) {
|
||||
@@ -77,10 +82,32 @@ func (t *TrendDetector) AddValue(value int64) {
|
||||
t.highestValue = value
|
||||
}
|
||||
|
||||
// ignore duplicate values
|
||||
if t.params.CollapseValues && len(t.values) != 0 && t.values[len(t.values)-1] == value {
|
||||
return
|
||||
// Ignore duplicate values in collapse window.
|
||||
//
|
||||
// Bandwidth estimate is received periodically. If the estimate does not change, it will be repeated.
|
||||
// When there is congestion, there are several estimates received with decreasing values.
|
||||
//
|
||||
// Using a sliding window, collapsing repeated values and waiting for falling trend is to ensure that
|
||||
// the reaction is not too fast, i. e. reacting to falling values too quick could mean a lot of re-allocation
|
||||
// resulting in layer switches, key frames and more congestion.
|
||||
//
|
||||
// But, on the flip side, estimate could fall once or twice withing a sliding window and stay there.
|
||||
// In those cases, using a collapse window to record value even if it is duplicate. By doing that,
|
||||
// a trend could be detected eventually. If will be delayed, but that is fine with slow changing estimates.
|
||||
lastValue := int64(0)
|
||||
if len(t.values) != 0 {
|
||||
lastValue = t.values[len(t.values)-1]
|
||||
}
|
||||
if lastValue == value && t.params.CollapseThreshold > 0 {
|
||||
if !t.hasFallen || (!t.lastSampleAt.IsZero() && time.Since(t.lastSampleAt) < t.params.CollapseThreshold) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if lastValue > value {
|
||||
t.hasFallen = true
|
||||
}
|
||||
t.lastSampleAt = time.Now()
|
||||
|
||||
if len(t.values) == t.params.RequiredSamples {
|
||||
t.values = t.values[1:]
|
||||
|
||||
Reference in New Issue
Block a user