Unverified 提交 d4396745 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

RateCalculator: improve efficiency (#324)

* Store `this->lastTime` and `this->lastRate` when calling `GetRate(now)` and return `this->lastRate` if `GetRate(now)` is called with `now == this->lastTime` (instead of calculating the rate again).
* Reset both `this->lastTime` and `this->lastRate` to 0 when calling `Reset()` or `Update()` (to allow that immediate new calls to `GetRate()` with same `now` trigger a new rate calculation).
* Avoid unnecessary calls to `GetRate()` in `RemoteBitrateEstimatorAbsSendTime` (even if no longer necessary with the changes above, but looks better).
上级 9dbb28f5
......@@ -50,7 +50,7 @@
},
"devDependencies": {
"eslint": "^6.0.1",
"eslint-plugin-jest": "^22.9.0",
"eslint-plugin-jest": "^22.10.0",
"gulp": "^4.0.2",
"gulp-clang-format": "^1.0.27",
"jest": "^24.8.0",
......
......@@ -33,20 +33,24 @@ namespace RTC
};
private:
// Window Size (in milliseconds).
size_t windowSize{ DefaultWindowSize };
// Scale in which the rate is represented.
float scale{ DefaultBpsScale };
// Buffer to keep data.
std::unique_ptr<BufferItem[]> buffer;
// Time (in milliseconds) for oldest item in the time window.
uint64_t oldestTime{ 0 };
// Index for the oldest item in the time window.
uint32_t oldestIndex{ 0 };
// Total count in the time window.
size_t totalCount{ 0 };
// Window Size (in milliseconds).
size_t windowSize{ DefaultWindowSize };
// Scale in which the rate is represented.
const float scale{ DefaultBpsScale };
// Total bytes transmitted.
size_t bytes{ 0 };
// Last value calculated by GetRate().
uint32_t lastRate{ 0 };
// Last time GetRate() was called.
uint64_t lastTime{ 0 };
};
/* Inline instance methods. */
......@@ -72,9 +76,11 @@ namespace RTC
inline void RateCalculator::Reset(uint64_t now)
{
this->buffer.reset(new BufferItem[this->windowSize]);
this->totalCount = 0;
this->oldestIndex = 0;
this->oldestTime = now - this->windowSize;
this->oldestIndex = 0;
this->totalCount = 0;
this->lastRate = 0;
this->lastTime = 0;
}
class RtpDataCounter
......
......@@ -29,17 +29,28 @@ namespace RTC
this->buffer[index].count += size;
this->totalCount += size;
// Reset lastRate and lastTime so GetRate() will calculate rate again even
// if called with same now in the same loop iteration.
this->lastRate = 0;
this->lastTime = 0;
}
uint32_t RateCalculator::GetRate(uint64_t now)
{
MS_TRACE();
if (now == this->lastTime)
return this->lastRate;
RemoveOldData(now);
float scale = this->scale / this->windowSize;
return static_cast<uint32_t>(std::trunc(this->totalCount * scale + 0.5f));
this->lastTime = now;
this->lastRate = static_cast<uint32_t>(std::trunc(this->totalCount * scale + 0.5f));
return this->lastRate;
}
inline void RateCalculator::RemoveOldData(uint64_t now)
......
......@@ -268,6 +268,9 @@ namespace RTC
this->incomingBitrate.Update(payloadSize, arrivalTimeMs);
// Update incoming bitrate.
incomingBitrate = this->incomingBitrate.GetRate(arrivalTimeMs);
if (this->firstPacketTimeMs == -1)
this->firstPacketTimeMs = nowMs;
......@@ -349,9 +352,7 @@ namespace RTC
}
else if (this->detector.State() == BW_OVERUSING)
{
uint32_t incomingRate = this->incomingBitrate.GetRate(arrivalTimeMs);
if ((incomingRate != 0u) && this->remoteRate.TimeToReduceFurther(nowMs, incomingRate))
if ((incomingBitrate != 0u) && this->remoteRate.TimeToReduceFurther(nowMs, incomingBitrate))
updateEstimate = true;
}
}
......@@ -362,9 +363,7 @@ namespace RTC
// We also have to update the estimate immediately if we are overusing
// and the target bitrate is too high compared to what we are receiving.
const RateControlInput input(
this->detector.State(),
this->incomingBitrate.GetRate(arrivalTimeMs),
this->estimator->GetVarNoise());
this->detector.State(), incomingBitrate, this->estimator->GetVarNoise());
this->remoteRate.Update(&input, nowMs);
targetBitrateBps = this->remoteRate.UpdateBandwidthEstimate(nowMs);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论