blob: 14baae7e8164b8d7bd1726c2a7f152589635e2dd [file] [log] [blame]
sprang@webrtc.org70e2d112014-09-24 14:06:56 +00001/* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
2 *
3 * Use of this source code is governed by a BSD-style license
4 * that can be found in the LICENSE file in the root of the source
5 * tree. An additional intellectual property rights grant can be found
6 * in the file PATENTS. All contributing project authors may
7 * be found in the AUTHORS file in the root of the source tree.
8 */
9
Yves Gerey3e707812018-11-28 16:47:49 +010010#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020011
Yves Gerey3e707812018-11-28 16:47:49 +010012#include <memory>
13#include <vector>
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include "absl/types/optional.h"
16#include "api/array_view.h"
17#include "modules/video_coding/jitter_estimator.h"
Erik Språngb1e031a2018-11-01 11:20:49 +010018#include "rtc_base/experiments/jitter_upper_bound_experiment.h"
Erik Språngb1e031a2018-11-01 11:20:49 +010019#include "rtc_base/numerics/histogram_percentile_counter.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "system_wrappers/include/clock.h"
Erik Språngb1e031a2018-11-01 11:20:49 +010023#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "test/gtest.h"
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000025
26namespace webrtc {
27
Erik Språngb1e031a2018-11-01 11:20:49 +010028class TestVCMJitterEstimator : public ::testing::Test {
29 protected:
30 TestVCMJitterEstimator() : fake_clock_(0) {}
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000031
Erik Språngb1e031a2018-11-01 11:20:49 +010032 virtual void SetUp() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020033 estimator_ = std::make_unique<VCMJitterEstimator>(&fake_clock_);
Erik Språngb1e031a2018-11-01 11:20:49 +010034 }
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000035
36 void AdvanceClock(int64_t microseconds) {
37 fake_clock_.AdvanceTimeMicroseconds(microseconds);
38 }
39
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000040 SimulatedClock fake_clock_;
Erik Språngb1e031a2018-11-01 11:20:49 +010041 std::unique_ptr<VCMJitterEstimator> estimator_;
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000042};
43
44// Generates some simple test data in the form of a sawtooth wave.
45class ValueGenerator {
46 public:
oprypin60c56682017-03-24 03:22:49 -070047 explicit ValueGenerator(int32_t amplitude)
48 : amplitude_(amplitude), counter_(0) {}
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000049 virtual ~ValueGenerator() {}
50
Åsa Persson3fcc5be2019-04-04 09:40:27 +020051 int64_t Delay() const { return ((counter_ % 11) - 5) * amplitude_; }
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000052
Åsa Persson3fcc5be2019-04-04 09:40:27 +020053 uint32_t FrameSize() const { return 1000 + Delay(); }
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000054
55 void Advance() { ++counter_; }
56
57 private:
58 const int32_t amplitude_;
59 int64_t counter_;
60};
61
62// 5 fps, disable jitter delay altogether.
63TEST_F(TestVCMJitterEstimator, TestLowRate) {
64 ValueGenerator gen(10);
Erik Språngb1e031a2018-11-01 11:20:49 +010065 uint64_t time_delta_us = rtc::kNumMicrosecsPerSec / 5;
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000066 for (int i = 0; i < 60; ++i) {
Erik Språngb1e031a2018-11-01 11:20:49 +010067 estimator_->UpdateEstimate(gen.Delay(), gen.FrameSize());
68 AdvanceClock(time_delta_us);
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000069 if (i > 2)
“Michaele0f37042019-06-04 10:04:12 -050070 EXPECT_EQ(estimator_->GetJitterEstimate(0, absl::nullopt), 0);
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000071 gen.Advance();
72 }
73}
74
Åsa Persson3361af32020-05-18 11:01:33 +020075TEST_F(TestVCMJitterEstimator, TestLowRateDisabled) {
76 test::ScopedFieldTrials field_trials(
77 "WebRTC-ReducedJitterDelayKillSwitch/Enabled/");
78 SetUp();
79
80 ValueGenerator gen(10);
81 uint64_t time_delta_us = rtc::kNumMicrosecsPerSec / 5;
82 for (int i = 0; i < 60; ++i) {
83 estimator_->UpdateEstimate(gen.Delay(), gen.FrameSize());
84 AdvanceClock(time_delta_us);
85 if (i > 2)
86 EXPECT_GT(estimator_->GetJitterEstimate(0, absl::nullopt), 0);
87 gen.Advance();
88 }
89}
90
Erik Språngb1e031a2018-11-01 11:20:49 +010091TEST_F(TestVCMJitterEstimator, TestUpperBound) {
92 struct TestContext {
“Michaele0f37042019-06-04 10:04:12 -050093 TestContext()
94 : upper_bound(0.0),
95 rtt_mult(0),
96 rtt_mult_add_cap_ms(absl::nullopt),
97 percentiles(1000) {}
Erik Språngb1e031a2018-11-01 11:20:49 +010098 double upper_bound;
“Michaele0f37042019-06-04 10:04:12 -050099 double rtt_mult;
100 absl::optional<double> rtt_mult_add_cap_ms;
Erik Språngb1e031a2018-11-01 11:20:49 +0100101 rtc::HistogramPercentileCounter percentiles;
102 };
“Michaele0f37042019-06-04 10:04:12 -0500103 std::vector<TestContext> test_cases(4);
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000104
“Michaele0f37042019-06-04 10:04:12 -0500105 // Large upper bound, rtt_mult = 0, and nullopt for rtt_mult addition cap.
106 test_cases[0].upper_bound = 100.0;
107 test_cases[0].rtt_mult = 0;
108 test_cases[0].rtt_mult_add_cap_ms = absl::nullopt;
109 // Small upper bound, rtt_mult = 0, and nullopt for rtt_mult addition cap.
110 test_cases[1].upper_bound = 3.5;
111 test_cases[1].rtt_mult = 0;
112 test_cases[1].rtt_mult_add_cap_ms = absl::nullopt;
113 // Large upper bound, rtt_mult = 1, and large rtt_mult addition cap value.
114 test_cases[2].upper_bound = 1000.0;
115 test_cases[2].rtt_mult = 1.0;
116 test_cases[2].rtt_mult_add_cap_ms = 200.0;
117 // Large upper bound, rtt_mult = 1, and small rtt_mult addition cap value.
118 test_cases[3].upper_bound = 1000.0;
119 test_cases[3].rtt_mult = 1.0;
120 test_cases[3].rtt_mult_add_cap_ms = 10.0;
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000121
“Michaele0f37042019-06-04 10:04:12 -0500122 // Test jitter buffer upper_bound and rtt_mult addition cap sizes.
Erik Språngb1e031a2018-11-01 11:20:49 +0100123 for (TestContext& context : test_cases) {
124 // Set up field trial and reset jitter estimator.
125 char string_buf[64];
126 rtc::SimpleStringBuilder ssb(string_buf);
127 ssb << JitterUpperBoundExperiment::kJitterUpperBoundExperimentName
128 << "/Enabled-" << context.upper_bound << "/";
129 test::ScopedFieldTrials field_trials(ssb.str());
130 SetUp();
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000131
Erik Språngb1e031a2018-11-01 11:20:49 +0100132 ValueGenerator gen(50);
133 uint64_t time_delta_us = rtc::kNumMicrosecsPerSec / 30;
“Michaele0f37042019-06-04 10:04:12 -0500134 constexpr int64_t kRttMs = 250;
Erik Språngb1e031a2018-11-01 11:20:49 +0100135 for (int i = 0; i < 100; ++i) {
136 estimator_->UpdateEstimate(gen.Delay(), gen.FrameSize());
137 AdvanceClock(time_delta_us);
“Michaele0f37042019-06-04 10:04:12 -0500138 estimator_->FrameNacked(); // To test rtt_mult.
139 estimator_->UpdateRtt(kRttMs); // To test rtt_mult.
Erik Språngb1e031a2018-11-01 11:20:49 +0100140 context.percentiles.Add(
“Michaele0f37042019-06-04 10:04:12 -0500141 static_cast<uint32_t>(estimator_->GetJitterEstimate(
142 context.rtt_mult, context.rtt_mult_add_cap_ms)));
Erik Språngb1e031a2018-11-01 11:20:49 +0100143 gen.Advance();
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000144 }
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000145 }
146
Erik Språngb1e031a2018-11-01 11:20:49 +0100147 // Median should be similar after three seconds. Allow 5% error margin.
148 uint32_t median_unbound = *test_cases[0].percentiles.GetPercentile(0.5);
149 uint32_t median_bounded = *test_cases[1].percentiles.GetPercentile(0.5);
150 EXPECT_NEAR(median_unbound, median_bounded, (median_unbound * 5) / 100);
151
152 // Max should be lower for the bounded case.
153 uint32_t max_unbound = *test_cases[0].percentiles.GetPercentile(1.0);
154 uint32_t max_bounded = *test_cases[1].percentiles.GetPercentile(1.0);
155 EXPECT_GT(max_unbound, static_cast<uint32_t>(max_bounded * 1.25));
“Michaele0f37042019-06-04 10:04:12 -0500156
157 // With rtt_mult = 1, max should be lower with small rtt_mult add cap value.
158 max_unbound = *test_cases[2].percentiles.GetPercentile(1.0);
159 max_bounded = *test_cases[3].percentiles.GetPercentile(1.0);
160 EXPECT_GT(max_unbound, static_cast<uint32_t>(max_bounded * 1.25));
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000161}
Erik Språngb1e031a2018-11-01 11:20:49 +0100162
oprypin60c56682017-03-24 03:22:49 -0700163} // namespace webrtc