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 @@ ...@@ -50,7 +50,7 @@
}, },
"devDependencies": { "devDependencies": {
"eslint": "^6.0.1", "eslint": "^6.0.1",
"eslint-plugin-jest": "^22.9.0", "eslint-plugin-jest": "^22.10.0",
"gulp": "^4.0.2", "gulp": "^4.0.2",
"gulp-clang-format": "^1.0.27", "gulp-clang-format": "^1.0.27",
"jest": "^24.8.0", "jest": "^24.8.0",
......
...@@ -33,20 +33,24 @@ namespace RTC ...@@ -33,20 +33,24 @@ namespace RTC
}; };
private: 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; std::unique_ptr<BufferItem[]> buffer;
// Time (in milliseconds) for oldest item in the time window. // Time (in milliseconds) for oldest item in the time window.
uint64_t oldestTime{ 0 }; uint64_t oldestTime{ 0 };
// Index for the oldest item in the time window. // Index for the oldest item in the time window.
uint32_t oldestIndex{ 0 }; uint32_t oldestIndex{ 0 };
// Total count in the time window. // Total count in the time window.
size_t totalCount{ 0 }; 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. // Total bytes transmitted.
size_t bytes{ 0 }; 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. */ /* Inline instance methods. */
...@@ -72,9 +76,11 @@ namespace RTC ...@@ -72,9 +76,11 @@ namespace RTC
inline void RateCalculator::Reset(uint64_t now) inline void RateCalculator::Reset(uint64_t now)
{ {
this->buffer.reset(new BufferItem[this->windowSize]); this->buffer.reset(new BufferItem[this->windowSize]);
this->totalCount = 0;
this->oldestIndex = 0;
this->oldestTime = now - this->windowSize; this->oldestTime = now - this->windowSize;
this->oldestIndex = 0;
this->totalCount = 0;
this->lastRate = 0;
this->lastTime = 0;
} }
class RtpDataCounter class RtpDataCounter
......
...@@ -29,17 +29,28 @@ namespace RTC ...@@ -29,17 +29,28 @@ namespace RTC
this->buffer[index].count += size; this->buffer[index].count += size;
this->totalCount += 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) uint32_t RateCalculator::GetRate(uint64_t now)
{ {
MS_TRACE(); MS_TRACE();
if (now == this->lastTime)
return this->lastRate;
RemoveOldData(now); RemoveOldData(now);
float scale = this->scale / this->windowSize; 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) inline void RateCalculator::RemoveOldData(uint64_t now)
......
...@@ -268,6 +268,9 @@ namespace RTC ...@@ -268,6 +268,9 @@ namespace RTC
this->incomingBitrate.Update(payloadSize, arrivalTimeMs); this->incomingBitrate.Update(payloadSize, arrivalTimeMs);
// Update incoming bitrate.
incomingBitrate = this->incomingBitrate.GetRate(arrivalTimeMs);
if (this->firstPacketTimeMs == -1) if (this->firstPacketTimeMs == -1)
this->firstPacketTimeMs = nowMs; this->firstPacketTimeMs = nowMs;
...@@ -349,9 +352,7 @@ namespace RTC ...@@ -349,9 +352,7 @@ namespace RTC
} }
else if (this->detector.State() == BW_OVERUSING) else if (this->detector.State() == BW_OVERUSING)
{ {
uint32_t incomingRate = this->incomingBitrate.GetRate(arrivalTimeMs); if ((incomingBitrate != 0u) && this->remoteRate.TimeToReduceFurther(nowMs, incomingBitrate))
if ((incomingRate != 0u) && this->remoteRate.TimeToReduceFurther(nowMs, incomingRate))
updateEstimate = true; updateEstimate = true;
} }
} }
...@@ -362,9 +363,7 @@ namespace RTC ...@@ -362,9 +363,7 @@ namespace RTC
// We also have to update the estimate immediately if we are overusing // 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. // and the target bitrate is too high compared to what we are receiving.
const RateControlInput input( const RateControlInput input(
this->detector.State(), this->detector.State(), incomingBitrate, this->estimator->GetVarNoise());
this->incomingBitrate.GetRate(arrivalTimeMs),
this->estimator->GetVarNoise());
this->remoteRate.Update(&input, nowMs); this->remoteRate.Update(&input, nowMs);
targetBitrateBps = this->remoteRate.UpdateBandwidthEstimate(nowMs); targetBitrateBps = this->remoteRate.UpdateBandwidthEstimate(nowMs);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论