sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 1 | /* 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 Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 10 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | #include <memory> |
| 13 | #include <vector> |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
| 16 | #include "absl/types/optional.h" |
| 17 | #include "api/array_view.h" |
| 18 | #include "modules/video_coding/jitter_estimator.h" |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 19 | #include "rtc_base/experiments/jitter_upper_bound_experiment.h" |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 20 | #include "rtc_base/numerics/histogram_percentile_counter.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 21 | #include "rtc_base/strings/string_builder.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "system_wrappers/include/clock.h" |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 24 | #include "test/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "test/gtest.h" |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 29 | class TestVCMJitterEstimator : public ::testing::Test { |
| 30 | protected: |
| 31 | TestVCMJitterEstimator() : fake_clock_(0) {} |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 32 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 33 | virtual void SetUp() { |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 34 | estimator_ = absl::make_unique<VCMJitterEstimator>(&fake_clock_); |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 35 | } |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 36 | |
| 37 | void AdvanceClock(int64_t microseconds) { |
| 38 | fake_clock_.AdvanceTimeMicroseconds(microseconds); |
| 39 | } |
| 40 | |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 41 | SimulatedClock fake_clock_; |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 42 | std::unique_ptr<VCMJitterEstimator> estimator_; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | // Generates some simple test data in the form of a sawtooth wave. |
| 46 | class ValueGenerator { |
| 47 | public: |
oprypin | 60c5668 | 2017-03-24 03:22:49 -0700 | [diff] [blame] | 48 | explicit ValueGenerator(int32_t amplitude) |
| 49 | : amplitude_(amplitude), counter_(0) {} |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 50 | virtual ~ValueGenerator() {} |
| 51 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 52 | int64_t Delay() const { return ((counter_ % 11) - 5) * amplitude_; } |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 53 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 54 | uint32_t FrameSize() const { return 1000 + Delay(); } |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 55 | |
| 56 | void Advance() { ++counter_; } |
| 57 | |
| 58 | private: |
| 59 | const int32_t amplitude_; |
| 60 | int64_t counter_; |
| 61 | }; |
| 62 | |
| 63 | // 5 fps, disable jitter delay altogether. |
| 64 | TEST_F(TestVCMJitterEstimator, TestLowRate) { |
| 65 | ValueGenerator gen(10); |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 66 | uint64_t time_delta_us = rtc::kNumMicrosecsPerSec / 5; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 67 | for (int i = 0; i < 60; ++i) { |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 68 | estimator_->UpdateEstimate(gen.Delay(), gen.FrameSize()); |
| 69 | AdvanceClock(time_delta_us); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 70 | if (i > 2) |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 71 | EXPECT_EQ(estimator_->GetJitterEstimate(0, absl::nullopt), 0); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 72 | gen.Advance(); |
| 73 | } |
| 74 | } |
| 75 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 76 | TEST_F(TestVCMJitterEstimator, TestUpperBound) { |
| 77 | struct TestContext { |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 78 | TestContext() |
| 79 | : upper_bound(0.0), |
| 80 | rtt_mult(0), |
| 81 | rtt_mult_add_cap_ms(absl::nullopt), |
| 82 | percentiles(1000) {} |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 83 | double upper_bound; |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 84 | double rtt_mult; |
| 85 | absl::optional<double> rtt_mult_add_cap_ms; |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 86 | rtc::HistogramPercentileCounter percentiles; |
| 87 | }; |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 88 | std::vector<TestContext> test_cases(4); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 89 | |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 90 | // Large upper bound, rtt_mult = 0, and nullopt for rtt_mult addition cap. |
| 91 | test_cases[0].upper_bound = 100.0; |
| 92 | test_cases[0].rtt_mult = 0; |
| 93 | test_cases[0].rtt_mult_add_cap_ms = absl::nullopt; |
| 94 | // Small upper bound, rtt_mult = 0, and nullopt for rtt_mult addition cap. |
| 95 | test_cases[1].upper_bound = 3.5; |
| 96 | test_cases[1].rtt_mult = 0; |
| 97 | test_cases[1].rtt_mult_add_cap_ms = absl::nullopt; |
| 98 | // Large upper bound, rtt_mult = 1, and large rtt_mult addition cap value. |
| 99 | test_cases[2].upper_bound = 1000.0; |
| 100 | test_cases[2].rtt_mult = 1.0; |
| 101 | test_cases[2].rtt_mult_add_cap_ms = 200.0; |
| 102 | // Large upper bound, rtt_mult = 1, and small rtt_mult addition cap value. |
| 103 | test_cases[3].upper_bound = 1000.0; |
| 104 | test_cases[3].rtt_mult = 1.0; |
| 105 | test_cases[3].rtt_mult_add_cap_ms = 10.0; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 106 | |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 107 | // Test jitter buffer upper_bound and rtt_mult addition cap sizes. |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 108 | for (TestContext& context : test_cases) { |
| 109 | // Set up field trial and reset jitter estimator. |
| 110 | char string_buf[64]; |
| 111 | rtc::SimpleStringBuilder ssb(string_buf); |
| 112 | ssb << JitterUpperBoundExperiment::kJitterUpperBoundExperimentName |
| 113 | << "/Enabled-" << context.upper_bound << "/"; |
| 114 | test::ScopedFieldTrials field_trials(ssb.str()); |
| 115 | SetUp(); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 116 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 117 | ValueGenerator gen(50); |
| 118 | uint64_t time_delta_us = rtc::kNumMicrosecsPerSec / 30; |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 119 | constexpr int64_t kRttMs = 250; |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 120 | for (int i = 0; i < 100; ++i) { |
| 121 | estimator_->UpdateEstimate(gen.Delay(), gen.FrameSize()); |
| 122 | AdvanceClock(time_delta_us); |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 123 | estimator_->FrameNacked(); // To test rtt_mult. |
| 124 | estimator_->UpdateRtt(kRttMs); // To test rtt_mult. |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 125 | context.percentiles.Add( |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 126 | static_cast<uint32_t>(estimator_->GetJitterEstimate( |
| 127 | context.rtt_mult, context.rtt_mult_add_cap_ms))); |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 128 | gen.Advance(); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 129 | } |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 132 | // Median should be similar after three seconds. Allow 5% error margin. |
| 133 | uint32_t median_unbound = *test_cases[0].percentiles.GetPercentile(0.5); |
| 134 | uint32_t median_bounded = *test_cases[1].percentiles.GetPercentile(0.5); |
| 135 | EXPECT_NEAR(median_unbound, median_bounded, (median_unbound * 5) / 100); |
| 136 | |
| 137 | // Max should be lower for the bounded case. |
| 138 | uint32_t max_unbound = *test_cases[0].percentiles.GetPercentile(1.0); |
| 139 | uint32_t max_bounded = *test_cases[1].percentiles.GetPercentile(1.0); |
| 140 | EXPECT_GT(max_unbound, static_cast<uint32_t>(max_bounded * 1.25)); |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 141 | |
| 142 | // With rtt_mult = 1, max should be lower with small rtt_mult add cap value. |
| 143 | max_unbound = *test_cases[2].percentiles.GetPercentile(1.0); |
| 144 | max_bounded = *test_cases[3].percentiles.GetPercentile(1.0); |
| 145 | EXPECT_GT(max_unbound, static_cast<uint32_t>(max_bounded * 1.25)); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 146 | } |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 147 | |
oprypin | 60c5668 | 2017-03-24 03:22:49 -0700 | [diff] [blame] | 148 | } // namespace webrtc |