mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-12 21:30:45 +01:00
Make interval budget use ratio instead of percent
All usages compare the budget usage to ratios, so we can skip a few multiplications. Bug: webrtc:10719 Change-Id: I0205d74762043d972c087c152915e4fdd9510057 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/140289 Commit-Queue: Jonas Olsson <jonasolsson@webrtc.org> Reviewed-by: Sebastian Jansson <srte@webrtc.org> Cr-Commit-Position: refs/heads/master@{#28190}
This commit is contained in:
parent
71061bcca8
commit
944dacec25
4 changed files with 25 additions and 22 deletions
|
@ -92,12 +92,11 @@ void AlrDetector::OnBytesSent(size_t bytes_sent, int64_t send_time_ms) {
|
|||
alr_budget_.UseBudget(bytes_sent);
|
||||
alr_budget_.IncreaseBudget(delta_time_ms);
|
||||
bool state_changed = false;
|
||||
if (alr_budget_.budget_level_percent() > start_budget_level_ratio_ * 100 &&
|
||||
if (alr_budget_.budget_ratio() > start_budget_level_ratio_ &&
|
||||
!alr_started_time_ms_) {
|
||||
alr_started_time_ms_.emplace(rtc::TimeMillis());
|
||||
state_changed = true;
|
||||
} else if (alr_budget_.budget_level_percent() <
|
||||
stop_budget_level_ratio_ * 100 &&
|
||||
} else if (alr_budget_.budget_ratio() < stop_budget_level_ratio_ &&
|
||||
alr_started_time_ms_) {
|
||||
state_changed = true;
|
||||
alr_started_time_ms_.reset();
|
||||
|
|
|
@ -8,9 +8,10 @@
|
|||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "modules/pacing/interval_budget.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "modules/pacing/interval_budget.h"
|
||||
#include "rtc_base/numerics/safe_conversions.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
@ -54,11 +55,10 @@ size_t IntervalBudget::bytes_remaining() const {
|
|||
return static_cast<size_t>(std::max(0, bytes_remaining_));
|
||||
}
|
||||
|
||||
int IntervalBudget::budget_level_percent() const {
|
||||
double IntervalBudget::budget_ratio() const {
|
||||
if (max_bytes_in_budget_ == 0)
|
||||
return 0;
|
||||
return rtc::dchecked_cast<int>(int64_t{bytes_remaining_} * 100 /
|
||||
max_bytes_in_budget_);
|
||||
return 0.0;
|
||||
return static_cast<double>(bytes_remaining_) / max_bytes_in_budget_;
|
||||
}
|
||||
|
||||
int IntervalBudget::target_rate_kbps() const {
|
||||
|
|
|
@ -29,7 +29,7 @@ class IntervalBudget {
|
|||
void UseBudget(size_t bytes);
|
||||
|
||||
size_t bytes_remaining() const;
|
||||
int budget_level_percent() const;
|
||||
double budget_ratio() const;
|
||||
int target_rate_kbps() const;
|
||||
|
||||
private:
|
||||
|
|
|
@ -26,7 +26,7 @@ size_t TimeToBytes(int bitrate_kbps, int time_ms) {
|
|||
|
||||
TEST(IntervalBudgetTest, InitailState) {
|
||||
IntervalBudget interval_budget(kBitrateKbps);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(), 0);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), 0.0);
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(), 0u);
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,8 @@ TEST(IntervalBudgetTest, Underuse) {
|
|||
IntervalBudget interval_budget(kBitrateKbps);
|
||||
int delta_time_ms = 50;
|
||||
interval_budget.IncreaseBudget(delta_time_ms);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(), kWindowMs / delta_time_ms);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(),
|
||||
kWindowMs / static_cast<double>(100 * delta_time_ms));
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(),
|
||||
TimeToBytes(kBitrateKbps, delta_time_ms));
|
||||
}
|
||||
|
@ -43,7 +44,7 @@ TEST(IntervalBudgetTest, DontUnderuseMoreThanMaxWindow) {
|
|||
IntervalBudget interval_budget(kBitrateKbps);
|
||||
int delta_time_ms = 1000;
|
||||
interval_budget.IncreaseBudget(delta_time_ms);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(), 100);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), 1.0);
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(),
|
||||
TimeToBytes(kBitrateKbps, kWindowMs));
|
||||
}
|
||||
|
@ -53,7 +54,7 @@ TEST(IntervalBudgetTest, DontUnderuseMoreThanMaxWindowWhenChangeBitrate) {
|
|||
int delta_time_ms = kWindowMs / 2;
|
||||
interval_budget.IncreaseBudget(delta_time_ms);
|
||||
interval_budget.set_target_rate_kbps(kBitrateKbps / 10);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(), 100);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), 1.0);
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(),
|
||||
TimeToBytes(kBitrateKbps / 10, kWindowMs));
|
||||
}
|
||||
|
@ -63,7 +64,7 @@ TEST(IntervalBudgetTest, BalanceChangeOnBitrateChange) {
|
|||
int delta_time_ms = kWindowMs;
|
||||
interval_budget.IncreaseBudget(delta_time_ms);
|
||||
interval_budget.set_target_rate_kbps(kBitrateKbps * 2);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(), 50);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), 0.5);
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(),
|
||||
TimeToBytes(kBitrateKbps, kWindowMs));
|
||||
}
|
||||
|
@ -73,8 +74,8 @@ TEST(IntervalBudgetTest, Overuse) {
|
|||
int overuse_time_ms = 50;
|
||||
int used_bytes = TimeToBytes(kBitrateKbps, overuse_time_ms);
|
||||
interval_budget.UseBudget(used_bytes);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(),
|
||||
-kWindowMs / overuse_time_ms);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(),
|
||||
-kWindowMs / static_cast<double>(100 * overuse_time_ms));
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(), 0u);
|
||||
}
|
||||
|
||||
|
@ -83,7 +84,7 @@ TEST(IntervalBudgetTest, DontOveruseMoreThanMaxWindow) {
|
|||
int overuse_time_ms = 1000;
|
||||
int used_bytes = TimeToBytes(kBitrateKbps, overuse_time_ms);
|
||||
interval_budget.UseBudget(used_bytes);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(), -100);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), -1.0);
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(), 0u);
|
||||
}
|
||||
|
||||
|
@ -91,13 +92,14 @@ TEST(IntervalBudgetTest, CanBuildUpUnderuseWhenConfigured) {
|
|||
IntervalBudget interval_budget(kBitrateKbps, kCanBuildUpUnderuse);
|
||||
int delta_time_ms = 50;
|
||||
interval_budget.IncreaseBudget(delta_time_ms);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(), kWindowMs / delta_time_ms);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(),
|
||||
kWindowMs / static_cast<double>(100 * delta_time_ms));
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(),
|
||||
TimeToBytes(kBitrateKbps, delta_time_ms));
|
||||
|
||||
interval_budget.IncreaseBudget(delta_time_ms);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(),
|
||||
2 * kWindowMs / delta_time_ms);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(),
|
||||
2 * kWindowMs / static_cast<double>(100 * delta_time_ms));
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(),
|
||||
TimeToBytes(kBitrateKbps, 2 * delta_time_ms));
|
||||
}
|
||||
|
@ -106,12 +108,14 @@ TEST(IntervalBudgetTest, CanNotBuildUpUnderuseWhenConfigured) {
|
|||
IntervalBudget interval_budget(kBitrateKbps, kCanNotBuildUpUnderuse);
|
||||
int delta_time_ms = 50;
|
||||
interval_budget.IncreaseBudget(delta_time_ms);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(), kWindowMs / delta_time_ms);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(),
|
||||
kWindowMs / static_cast<double>(100 * delta_time_ms));
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(),
|
||||
TimeToBytes(kBitrateKbps, delta_time_ms));
|
||||
|
||||
interval_budget.IncreaseBudget(delta_time_ms);
|
||||
EXPECT_EQ(interval_budget.budget_level_percent(), kWindowMs / delta_time_ms);
|
||||
EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(),
|
||||
kWindowMs / static_cast<double>(100 * delta_time_ms));
|
||||
EXPECT_EQ(interval_budget.bytes_remaining(),
|
||||
TimeToBytes(kBitrateKbps, delta_time_ms));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue