Unverified 提交 3cde2cb8 authored 作者: Vittorio Palmisano's avatar Vittorio Palmisano 提交者: GitHub

Optimize rate calculator (v2) (#547)

上级 6f8953a3
......@@ -14,15 +14,17 @@ namespace RTC
public:
static constexpr size_t DefaultWindowSize{ 1000u };
static constexpr float DefaultBpsScale{ 8000.0f };
static constexpr uint16_t DefaultWindowItems{ 1000u };
static constexpr uint16_t DefaultWindowItems{ 100u };
public:
RateCalculator(
size_t windowSize = DefaultWindowSize,
size_t windowSizeMs = DefaultWindowSize,
float scale = DefaultBpsScale,
uint16_t windowItems = DefaultWindowItems)
: windowSize(windowSize), scale(scale), windowItems(windowItems)
: windowSizeMs(windowSizeMs), scale(scale), windowItems(windowItems)
{
this->itemSizeMs = std::max(windowSizeMs / windowItems, static_cast<size_t>(1));
Reset();
}
void Update(size_t size, uint64_t nowMs);
......@@ -43,13 +45,13 @@ namespace RTC
void Reset(uint64_t nowMs)
{
this->buffer.reset(new BufferItem[this->windowItems]);
this->newestTime = 0u;
this->newestTimeIndex = -1;
this->oldestTimeIndex = 0u;
this->oldestIndex = -1;
this->totalCount = 0u;
this->lastRate = 0u;
this->lastTime = 0u;
this->newestItemStartTime = 0u;
this->newestItemIndex = -1;
this->oldestItemStartTime = 0u;
this->oldestItemIndex = -1;
this->totalCount = 0u;
this->lastRate = 0u;
this->lastTime = 0u;
}
private:
......@@ -61,21 +63,23 @@ namespace RTC
private:
// Window Size (in milliseconds).
size_t windowSize{ DefaultWindowSize };
size_t windowSizeMs{ DefaultWindowSize };
// Scale in which the rate is represented.
float scale{ DefaultBpsScale };
// Window Size (number of items).
uint16_t windowItems{ DefaultWindowItems };
// Item Size (in milliseconds), calculated as: windowSizeMs / windowItems.
size_t itemSizeMs{ 0u };
// Buffer to keep data.
std::unique_ptr<BufferItem[]> buffer;
// Time (in milliseconds) for last item in the time window.
uint64_t newestTime{ 0u };
uint64_t newestItemStartTime{ 0u };
// Index for the last item in the time window.
int32_t newestTimeIndex{ -1 };
int32_t newestItemIndex{ -1 };
// Time (in milliseconds) for oldest item in the time window.
uint64_t oldestTimeIndex{ 0u };
uint64_t oldestItemStartTime{ 0u };
// Index for the oldest item in the time window.
int32_t oldestIndex{ -1 };
int32_t oldestItemIndex{ -1 };
// Total count in the time window.
size_t totalCount{ 0u };
// Total bytes transmitted.
......@@ -89,7 +93,7 @@ namespace RTC
class RtpDataCounter
{
public:
explicit RtpDataCounter(size_t windowSize = 2500) : rate(windowSize)
explicit RtpDataCounter(size_t windowSizeMs = 2500) : rate(windowSizeMs)
{
}
......
......@@ -12,7 +12,7 @@ namespace RTC
MS_TRACE();
// Ignore too old data. Should never happen.
if (nowMs < this->oldestTimeIndex)
if (nowMs < this->oldestItemStartTime)
return;
// Increase bytes.
......@@ -20,38 +20,49 @@ namespace RTC
RemoveOldData(nowMs);
// Increase the index.
if (++this->newestTimeIndex >= this->windowItems)
this->newestTimeIndex = 0;
// Newest index overlaps with the oldest one, remove it.
if (this->newestTimeIndex == this->oldestIndex && this->oldestIndex != -1)
// If the elapsed time from the newest item start time is greater than the
// item size (in milliseconds), increase the item index.
if (this->newestItemIndex < 0 || nowMs - this->newestItemStartTime >= this->itemSizeMs)
{
MS_WARN_TAG(
info,
"calculation buffer full, windowSize:%zu ms windowItems:%" PRIu16,
this->windowSize,
this->windowItems);
BufferItem& oldestItem = buffer[this->oldestIndex];
this->totalCount -= oldestItem.count;
oldestItem.count = 0u;
oldestItem.time = 0u;
if (++this->oldestIndex >= this->windowItems)
this->oldestIndex = 0;
this->newestItemIndex++;
this->newestItemStartTime = nowMs;
if (this->newestItemIndex >= this->windowItems)
this->newestItemIndex = 0;
// Newest index overlaps with the oldest one, remove it.
if (this->newestItemIndex == this->oldestItemIndex && this->oldestItemIndex != -1)
{
MS_WARN_TAG(
info,
"calculation buffer full, windowSizeMs:%zu ms windowItems:%" PRIu16,
this->windowSizeMs,
this->windowItems);
BufferItem& oldestItem = buffer[this->oldestItemIndex];
this->totalCount -= oldestItem.count;
oldestItem.count = 0u;
oldestItem.time = 0u;
if (++this->oldestItemIndex >= this->windowItems)
this->oldestItemIndex = 0;
}
// Set the newest item.
BufferItem& item = buffer[this->newestItemIndex];
item.count = size;
item.time = nowMs;
}
else
{
// Update the newest item.
BufferItem& item = buffer[this->newestItemIndex];
item.count += size;
}
// Update the newest item.
BufferItem& item = buffer[this->newestTimeIndex];
item.count += size;
item.time = nowMs;
this->newestTime = nowMs;
// Set the oldest item index and time, if not set.
if (this->oldestIndex < 0)
if (this->oldestItemIndex < 0)
{
this->oldestIndex = this->newestTimeIndex;
this->oldestTimeIndex = nowMs;
this->oldestItemIndex = this->newestItemIndex;
this->oldestItemStartTime = nowMs;
}
this->totalCount += size;
......@@ -71,7 +82,7 @@ namespace RTC
RemoveOldData(nowMs);
float scale = this->scale / this->windowSize;
float scale = this->scale / this->windowSizeMs;
this->lastTime = nowMs;
this->lastRate = static_cast<uint32_t>(std::trunc(this->totalCount * scale + 0.5f));
......@@ -83,36 +94,36 @@ namespace RTC
{
MS_TRACE();
// No item set
if (this->newestTimeIndex < 0 || this->oldestIndex < 0)
// No item set.
if (this->newestItemIndex < 0 || this->oldestItemIndex < 0)
return;
uint64_t newoldestTimeIndex = nowMs - this->windowSize;
uint64_t newoldestTime = nowMs - this->windowSizeMs;
// Oldest item already removed.
if (newoldestTimeIndex <= this->oldestTimeIndex)
if (newoldestTime <= this->oldestItemStartTime)
return;
// A whole window size time has elapsed since last entry. Reset the buffer.
if (newoldestTimeIndex > this->newestTime)
if (newoldestTime > this->newestItemStartTime)
{
Reset(nowMs);
return;
}
while (this->oldestTimeIndex < newoldestTimeIndex)
while (this->oldestItemStartTime < newoldestTime)
{
BufferItem& oldestItem = buffer[this->oldestIndex];
BufferItem& oldestItem = buffer[this->oldestItemIndex];
this->totalCount -= oldestItem.count;
oldestItem.count = 0u;
oldestItem.time = 0u;
if (++this->oldestIndex >= this->windowItems)
this->oldestIndex = 0;
if (++this->oldestItemIndex >= this->windowItems)
this->oldestItemIndex = 0;
const BufferItem& newOldestItem = buffer[this->oldestIndex];
this->oldestTimeIndex = newOldestItem.time;
const BufferItem& newOldestItem = buffer[this->oldestItemIndex];
this->oldestItemStartTime = newOldestItem.time;
}
}
......
......@@ -61,7 +61,7 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
SECTION("slide")
{
RateCalculator rate;
RateCalculator rate(1000, 8000, 1000);
// clang-format off
std::vector<data> input =
......@@ -79,23 +79,46 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
REQUIRE(rate.GetRate(nowMs + 3001) == 0);
}
SECTION("slide with 100 items")
{
RateCalculator rate(1000, 8000, 100);
// clang-format off
std::vector<data> input =
{
{ 0, 5, 40 },
{ 999, 2, 56 },
{ 1001, 1, 24 }, // merged inside 999
{ 1001, 1, 32 }, // merged inside 999
{ 2000, 1, 8 } // it will erase the item with timestamp=999,
// removing also the next two samples.
// The end estimation will include only the last sample.
};
// clang-format on
validate(rate, nowMs, input);
REQUIRE(rate.GetRate(nowMs + 3001) == 0);
}
SECTION("wrap")
{
// window: 1000ms, items: 5 (granularity: 200ms)
RateCalculator rate(1000, 8000, 5);
// clang-format off
std::vector<data> input =
{
{ 1000, 1, 1*8 },
{ 1001, 1, 1*8 + 1*8 },
{ 1002, 1, 1*8 + 2*8 },
{ 1003, 1, 1*8 + 3*8 },
{ 1004, 1, 1*8 + 4*8 },
{ 1005, 1, 1*8 + (5-1)*8 }, // starts wrap here
{ 1006, 1, 1*8 + (6-2)*8 },
{ 1007, 1, 1*8 + (7-3)*8 },
{ 1008, 1, 1*8 + (8-4)*8 },
{ 1009, 1, 1*8 + (9-5)*8 },
{ 1200, 1, 1*8 + 1*8 },
{ 1400, 1, 1*8 + 2*8 },
{ 1600, 1, 1*8 + 3*8 },
{ 1800, 1, 1*8 + 4*8 },
{ 2000, 1, 1*8 + (5-1)*8 }, // starts wrap here
{ 2200, 1, 1*8 + (6-2)*8 },
{ 2400, 1, 1*8 + (7-3)*8 },
{ 2600, 1, 1*8 + (8-4)*8 },
{ 2800, 1, 1*8 + (9-5)*8 },
};
// clang-format on
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论