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> |
| 11 | #include <memory> |
| 12 | #include <vector> |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include "absl/memory/memory.h" |
| 15 | #include "absl/types/optional.h" |
| 16 | #include "api/array_view.h" |
| 17 | #include "modules/video_coding/jitter_estimator.h" |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 18 | #include "rtc_base/experiments/jitter_upper_bound_experiment.h" |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 19 | #include "rtc_base/numerics/histogram_percentile_counter.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | #include "rtc_base/strings/string_builder.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 21 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "system_wrappers/include/clock.h" |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 23 | #include "test/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "test/gtest.h" |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 28 | class TestVCMJitterEstimator : public ::testing::Test { |
| 29 | protected: |
| 30 | TestVCMJitterEstimator() : fake_clock_(0) {} |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 31 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 32 | virtual void SetUp() { |
| 33 | estimator_ = absl::make_unique<VCMJitterEstimator>(&fake_clock_, 0, 0); |
| 34 | } |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 35 | |
| 36 | void AdvanceClock(int64_t microseconds) { |
| 37 | fake_clock_.AdvanceTimeMicroseconds(microseconds); |
| 38 | } |
| 39 | |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 40 | SimulatedClock fake_clock_; |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 41 | std::unique_ptr<VCMJitterEstimator> estimator_; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | // Generates some simple test data in the form of a sawtooth wave. |
| 45 | class ValueGenerator { |
| 46 | public: |
oprypin | 60c5668 | 2017-03-24 03:22:49 -0700 | [diff] [blame] | 47 | explicit ValueGenerator(int32_t amplitude) |
| 48 | : amplitude_(amplitude), counter_(0) {} |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 49 | virtual ~ValueGenerator() {} |
| 50 | |
| 51 | int64_t Delay() { return ((counter_ % 11) - 5) * amplitude_; } |
| 52 | |
| 53 | uint32_t FrameSize() { return 1000 + Delay(); } |
| 54 | |
| 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. |
| 63 | TEST_F(TestVCMJitterEstimator, TestLowRate) { |
| 64 | ValueGenerator gen(10); |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 65 | uint64_t time_delta_us = rtc::kNumMicrosecsPerSec / 5; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 66 | for (int i = 0; i < 60; ++i) { |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 67 | estimator_->UpdateEstimate(gen.Delay(), gen.FrameSize()); |
| 68 | AdvanceClock(time_delta_us); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 69 | if (i > 2) |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 70 | EXPECT_EQ(estimator_->GetJitterEstimate(0), 0); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 71 | gen.Advance(); |
| 72 | } |
| 73 | } |
| 74 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 75 | TEST_F(TestVCMJitterEstimator, TestUpperBound) { |
| 76 | struct TestContext { |
| 77 | TestContext() : upper_bound(0.0), percentiles(1000) {} |
| 78 | double upper_bound; |
| 79 | rtc::HistogramPercentileCounter percentiles; |
| 80 | }; |
| 81 | std::vector<TestContext> test_cases(2); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 82 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 83 | test_cases[0].upper_bound = 100.0; // First use essentially no cap. |
| 84 | test_cases[1].upper_bound = 3.5; // Second, reasonably small cap. |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 85 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 86 | for (TestContext& context : test_cases) { |
| 87 | // Set up field trial and reset jitter estimator. |
| 88 | char string_buf[64]; |
| 89 | rtc::SimpleStringBuilder ssb(string_buf); |
| 90 | ssb << JitterUpperBoundExperiment::kJitterUpperBoundExperimentName |
| 91 | << "/Enabled-" << context.upper_bound << "/"; |
| 92 | test::ScopedFieldTrials field_trials(ssb.str()); |
| 93 | SetUp(); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 94 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 95 | ValueGenerator gen(50); |
| 96 | uint64_t time_delta_us = rtc::kNumMicrosecsPerSec / 30; |
| 97 | for (int i = 0; i < 100; ++i) { |
| 98 | estimator_->UpdateEstimate(gen.Delay(), gen.FrameSize()); |
| 99 | AdvanceClock(time_delta_us); |
| 100 | context.percentiles.Add( |
| 101 | static_cast<uint32_t>(estimator_->GetJitterEstimate(0))); |
| 102 | gen.Advance(); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 103 | } |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 106 | // Median should be similar after three seconds. Allow 5% error margin. |
| 107 | uint32_t median_unbound = *test_cases[0].percentiles.GetPercentile(0.5); |
| 108 | uint32_t median_bounded = *test_cases[1].percentiles.GetPercentile(0.5); |
| 109 | EXPECT_NEAR(median_unbound, median_bounded, (median_unbound * 5) / 100); |
| 110 | |
| 111 | // Max should be lower for the bounded case. |
| 112 | uint32_t max_unbound = *test_cases[0].percentiles.GetPercentile(1.0); |
| 113 | uint32_t max_bounded = *test_cases[1].percentiles.GetPercentile(1.0); |
| 114 | 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] | 115 | } |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 116 | |
oprypin | 60c5668 | 2017-03-24 03:22:49 -0700 | [diff] [blame] | 117 | } // namespace webrtc |