summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/pacing/interval_budget_unittest.cc
blob: e182d35510831d71aedab3768a5c314fbaa71a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/pacing/interval_budget.h"

#include "test/gtest.h"

namespace webrtc {

namespace {
constexpr int kWindowMs = 500;
constexpr int kBitrateKbps = 100;
constexpr bool kCanBuildUpUnderuse = true;
constexpr bool kCanNotBuildUpUnderuse = false;
size_t TimeToBytes(int bitrate_kbps, int time_ms) {
  return static_cast<size_t>(bitrate_kbps * time_ms / 8);
}
}  // namespace

TEST(IntervalBudgetTest, InitailState) {
  IntervalBudget interval_budget(kBitrateKbps);
  EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), 0.0);
  EXPECT_EQ(interval_budget.bytes_remaining(), 0u);
}

TEST(IntervalBudgetTest, Underuse) {
  IntervalBudget interval_budget(kBitrateKbps);
  int delta_time_ms = 50;
  interval_budget.IncreaseBudget(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));
}

TEST(IntervalBudgetTest, DontUnderuseMoreThanMaxWindow) {
  IntervalBudget interval_budget(kBitrateKbps);
  int delta_time_ms = 1000;
  interval_budget.IncreaseBudget(delta_time_ms);
  EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), 1.0);
  EXPECT_EQ(interval_budget.bytes_remaining(),
            TimeToBytes(kBitrateKbps, kWindowMs));
}

TEST(IntervalBudgetTest, DontUnderuseMoreThanMaxWindowWhenChangeBitrate) {
  IntervalBudget interval_budget(kBitrateKbps);
  int delta_time_ms = kWindowMs / 2;
  interval_budget.IncreaseBudget(delta_time_ms);
  interval_budget.set_target_rate_kbps(kBitrateKbps / 10);
  EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), 1.0);
  EXPECT_EQ(interval_budget.bytes_remaining(),
            TimeToBytes(kBitrateKbps / 10, kWindowMs));
}

TEST(IntervalBudgetTest, BalanceChangeOnBitrateChange) {
  IntervalBudget interval_budget(kBitrateKbps);
  int delta_time_ms = kWindowMs;
  interval_budget.IncreaseBudget(delta_time_ms);
  interval_budget.set_target_rate_kbps(kBitrateKbps * 2);
  EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), 0.5);
  EXPECT_EQ(interval_budget.bytes_remaining(),
            TimeToBytes(kBitrateKbps, kWindowMs));
}

TEST(IntervalBudgetTest, Overuse) {
  IntervalBudget interval_budget(kBitrateKbps);
  int overuse_time_ms = 50;
  int used_bytes = TimeToBytes(kBitrateKbps, overuse_time_ms);
  interval_budget.UseBudget(used_bytes);
  EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(),
                   -kWindowMs / static_cast<double>(100 * overuse_time_ms));
  EXPECT_EQ(interval_budget.bytes_remaining(), 0u);
}

TEST(IntervalBudgetTest, DontOveruseMoreThanMaxWindow) {
  IntervalBudget interval_budget(kBitrateKbps);
  int overuse_time_ms = 1000;
  int used_bytes = TimeToBytes(kBitrateKbps, overuse_time_ms);
  interval_budget.UseBudget(used_bytes);
  EXPECT_DOUBLE_EQ(interval_budget.budget_ratio(), -1.0);
  EXPECT_EQ(interval_budget.bytes_remaining(), 0u);
}

TEST(IntervalBudgetTest, CanBuildUpUnderuseWhenConfigured) {
  IntervalBudget interval_budget(kBitrateKbps, kCanBuildUpUnderuse);
  int delta_time_ms = 50;
  interval_budget.IncreaseBudget(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_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));
}

TEST(IntervalBudgetTest, CanNotBuildUpUnderuseWhenConfigured) {
  IntervalBudget interval_budget(kBitrateKbps, kCanNotBuildUpUnderuse);
  int delta_time_ms = 50;
  interval_budget.IncreaseBudget(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_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));
}

}  // namespace webrtc