Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Danil Chapovalov | 44db436 | 2019-09-30 02:16:28 | [diff] [blame] | 13 | #include "api/task_queue/task_queue_base.h" |
Artem Titov | 46c4e60 | 2018-08-17 12:26:54 | [diff] [blame] | 14 | #include "api/test/simulated_network.h" |
Jiawei Ou | c2ebe21 | 2018-11-08 18:02:56 | [diff] [blame] | 15 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Erik Språng | f93eda1 | 2019-01-16 16:10:57 | [diff] [blame] | 16 | #include "api/video/video_bitrate_allocation.h" |
Artem Titov | 4e199e9 | 2018-08-20 11:30:39 | [diff] [blame] | 17 | #include "call/fake_network_pipe.h" |
| 18 | #include "call/simulated_network.h" |
Tomas Gunnarsson | fae0562 | 2020-06-03 06:54:39 | [diff] [blame] | 19 | #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h" |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 20 | #include "rtc_base/rate_limiter.h" |
Markus Handell | 9bbff07 | 2020-07-07 12:23:18 | [diff] [blame] | 21 | #include "rtc_base/synchronization/mutex.h" |
Danil Chapovalov | 82a3f0a | 2019-10-21 07:24:27 | [diff] [blame] | 22 | #include "rtc_base/task_queue_for_test.h" |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 23 | #include "rtc_base/task_utils/to_queued_task.h" |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 24 | #include "system_wrappers/include/sleep.h" |
| 25 | #include "test/call_test.h" |
Niels Möller | 4db138e | 2018-04-19 07:04:13 | [diff] [blame] | 26 | #include "test/fake_encoder.h" |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 27 | #include "test/field_trial.h" |
| 28 | #include "test/gtest.h" |
| 29 | #include "test/rtcp_packet_parser.h" |
| 30 | #include "test/rtp_rtcp_observer.h" |
Niels Möller | cbcbc22 | 2018-09-28 07:07:24 | [diff] [blame] | 31 | #include "test/video_encoder_proxy_factory.h" |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 32 | |
| 33 | namespace webrtc { |
Elad Alon | d8d3248 | 2019-02-18 22:45:57 | [diff] [blame] | 34 | namespace { |
| 35 | enum : int { // The first valid value is 1. |
| 36 | kAbsSendTimeExtensionId = 1, |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 37 | kTransportSequenceNumberId, |
Elad Alon | d8d3248 | 2019-02-18 22:45:57 | [diff] [blame] | 38 | }; |
| 39 | } // namespace |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 40 | |
Ilya Nikolaevskiy | cb96062 | 2018-09-04 09:07:31 | [diff] [blame] | 41 | class BandwidthEndToEndTest : public test::CallTest { |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 42 | public: |
Ilya Nikolaevskiy | cb96062 | 2018-09-04 09:07:31 | [diff] [blame] | 43 | BandwidthEndToEndTest() = default; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 44 | }; |
| 45 | |
Ilya Nikolaevskiy | cb96062 | 2018-09-04 09:07:31 | [diff] [blame] | 46 | TEST_F(BandwidthEndToEndTest, ReceiveStreamSendsRemb) { |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 47 | class RembObserver : public test::EndToEndTest { |
| 48 | public: |
| 49 | RembObserver() : EndToEndTest(kDefaultTimeoutMs) {} |
| 50 | |
| 51 | void ModifyVideoConfigs( |
| 52 | VideoSendStream::Config* send_config, |
| 53 | std::vector<VideoReceiveStream::Config>* receive_configs, |
| 54 | VideoEncoderConfig* encoder_config) override { |
| 55 | send_config->rtp.extensions.clear(); |
Elad Alon | d8d3248 | 2019-02-18 22:45:57 | [diff] [blame] | 56 | send_config->rtp.extensions.push_back( |
| 57 | RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeExtensionId)); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 58 | (*receive_configs)[0].rtp.transport_cc = false; |
| 59 | } |
| 60 | |
| 61 | Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { |
| 62 | test::RtcpPacketParser parser; |
| 63 | EXPECT_TRUE(parser.Parse(packet, length)); |
| 64 | |
| 65 | if (parser.remb()->num_packets() > 0) { |
| 66 | EXPECT_EQ(kReceiverLocalVideoSsrc, parser.remb()->sender_ssrc()); |
| 67 | EXPECT_LT(0U, parser.remb()->bitrate_bps()); |
| 68 | EXPECT_EQ(1U, parser.remb()->ssrcs().size()); |
| 69 | EXPECT_EQ(kVideoSendSsrcs[0], parser.remb()->ssrcs()[0]); |
| 70 | observation_complete_.Set(); |
| 71 | } |
| 72 | |
| 73 | return SEND_PACKET; |
| 74 | } |
| 75 | void PerformTest() override { |
| 76 | EXPECT_TRUE(Wait()) << "Timed out while waiting for a " |
| 77 | "receiver RTCP REMB packet to be " |
| 78 | "sent."; |
| 79 | } |
| 80 | } test; |
| 81 | |
| 82 | RunBaseTest(&test); |
| 83 | } |
| 84 | |
| 85 | class BandwidthStatsTest : public test::EndToEndTest { |
| 86 | public: |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 87 | BandwidthStatsTest(bool send_side_bwe, TaskQueueBase* task_queue) |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 88 | : EndToEndTest(test::CallTest::kDefaultTimeoutMs), |
| 89 | sender_call_(nullptr), |
| 90 | receiver_call_(nullptr), |
| 91 | has_seen_pacer_delay_(false), |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 92 | send_side_bwe_(send_side_bwe), |
| 93 | task_queue_(task_queue) {} |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 94 | |
Danil Chapovalov | 2f2049a | 2019-11-06 12:28:11 | [diff] [blame] | 95 | ~BandwidthStatsTest() override { |
| 96 | // Block until all already posted tasks run to avoid races when such task |
| 97 | // accesses |this|. |
| 98 | SendTask(RTC_FROM_HERE, task_queue_, [] {}); |
| 99 | } |
| 100 | |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 101 | void ModifyVideoConfigs( |
| 102 | VideoSendStream::Config* send_config, |
| 103 | std::vector<VideoReceiveStream::Config>* receive_configs, |
| 104 | VideoEncoderConfig* encoder_config) override { |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 105 | send_config->rtp.extensions.clear(); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 106 | if (!send_side_bwe_) { |
Elad Alon | d8d3248 | 2019-02-18 22:45:57 | [diff] [blame] | 107 | send_config->rtp.extensions.push_back( |
| 108 | RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeExtensionId)); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 109 | (*receive_configs)[0].rtp.transport_cc = false; |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 110 | } else { |
| 111 | send_config->rtp.extensions.push_back( |
| 112 | RtpExtension(RtpExtension::kTransportSequenceNumberUri, |
| 113 | kTransportSequenceNumberId)); |
| 114 | (*receive_configs)[0].rtp.transport_cc = true; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 115 | } |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 116 | |
| 117 | // Force a too high encoder bitrate to make sure we get pacer delay. |
| 118 | encoder_config->number_of_streams = 1; |
| 119 | encoder_config->max_bitrate_bps = kMaxBitrateBps * 2; |
| 120 | encoder_config->simulcast_layers[0].min_bitrate_bps = kMaxBitrateBps * 2; |
| 121 | encoder_config->simulcast_layers[0].target_bitrate_bps = kMaxBitrateBps * 2; |
| 122 | encoder_config->simulcast_layers[0].max_bitrate_bps = kMaxBitrateBps * 2; |
| 123 | } |
| 124 | |
| 125 | void ModifySenderBitrateConfig(BitrateConstraints* bitrate_config) override { |
| 126 | bitrate_config->max_bitrate_bps = kMaxBitrateBps; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 127 | } |
| 128 | |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 129 | // Called on the pacer thread. |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 130 | Action OnSendRtp(const uint8_t* packet, size_t length) override { |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 131 | // Stats need to be fetched on the thread where the caller objects were |
| 132 | // constructed. |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 133 | task_queue_->PostTask(ToQueuedTask([this]() { |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 134 | if (!sender_call_ || !receiver_call_) { |
| 135 | return; |
| 136 | } |
| 137 | |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 138 | Call::Stats sender_stats = sender_call_->GetStats(); |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 139 | if (!has_seen_pacer_delay_) { |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 140 | has_seen_pacer_delay_ = sender_stats.pacer_delay_ms > 0; |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 141 | } |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 142 | |
| 143 | if (sender_stats.send_bandwidth_bps > 0 && has_seen_pacer_delay_) { |
| 144 | Call::Stats receiver_stats = receiver_call_->GetStats(); |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 145 | if (send_side_bwe_ || receiver_stats.recv_bandwidth_bps > 0) { |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 146 | observation_complete_.Set(); |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 147 | } |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 148 | } |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 149 | })); |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 150 | |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 151 | return SEND_PACKET; |
| 152 | } |
| 153 | |
| 154 | void OnCallsCreated(Call* sender_call, Call* receiver_call) override { |
| 155 | sender_call_ = sender_call; |
| 156 | receiver_call_ = receiver_call; |
| 157 | } |
| 158 | |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 159 | void OnStreamsStopped() override { |
| 160 | sender_call_ = nullptr; |
| 161 | receiver_call_ = nullptr; |
| 162 | } |
| 163 | |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 164 | void PerformTest() override { |
| 165 | EXPECT_TRUE(Wait()) << "Timed out while waiting for " |
| 166 | "non-zero bandwidth stats."; |
| 167 | } |
| 168 | |
| 169 | private: |
Erik Språng | 8d65e9a | 2019-10-30 15:48:28 | [diff] [blame] | 170 | static const int kMaxBitrateBps = 3000000; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 171 | Call* sender_call_; |
| 172 | Call* receiver_call_; |
| 173 | bool has_seen_pacer_delay_; |
| 174 | const bool send_side_bwe_; |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 175 | TaskQueueBase* const task_queue_; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 176 | }; |
| 177 | |
Ilya Nikolaevskiy | cb96062 | 2018-09-04 09:07:31 | [diff] [blame] | 178 | TEST_F(BandwidthEndToEndTest, VerifySendSideBweStats) { |
Danil Chapovalov | d15a028 | 2019-10-22 08:48:17 | [diff] [blame] | 179 | BandwidthStatsTest test(true, task_queue()); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 180 | RunBaseTest(&test); |
| 181 | } |
| 182 | |
Ilya Nikolaevskiy | cb96062 | 2018-09-04 09:07:31 | [diff] [blame] | 183 | TEST_F(BandwidthEndToEndTest, VerifyRecvSideBweStats) { |
Danil Chapovalov | d15a028 | 2019-10-22 08:48:17 | [diff] [blame] | 184 | BandwidthStatsTest test(false, task_queue()); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 185 | RunBaseTest(&test); |
| 186 | } |
| 187 | |
| 188 | // Verifies that it's possible to limit the send BWE by sending a REMB. |
| 189 | // This is verified by allowing the send BWE to ramp-up to >1000 kbps, |
| 190 | // then have the test generate a REMB of 500 kbps and verify that the send BWE |
| 191 | // is reduced to exactly 500 kbps. Then a REMB of 1000 kbps is generated and the |
| 192 | // test verifies that the send BWE ramps back up to exactly 1000 kbps. |
Ilya Nikolaevskiy | cb96062 | 2018-09-04 09:07:31 | [diff] [blame] | 193 | TEST_F(BandwidthEndToEndTest, RembWithSendSideBwe) { |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 194 | class BweObserver : public test::EndToEndTest { |
| 195 | public: |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 196 | explicit BweObserver(TaskQueueBase* task_queue) |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 197 | : EndToEndTest(kDefaultTimeoutMs), |
| 198 | sender_call_(nullptr), |
| 199 | clock_(Clock::GetRealTimeClock()), |
| 200 | sender_ssrc_(0), |
| 201 | remb_bitrate_bps_(1000000), |
| 202 | receive_transport_(nullptr), |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 203 | state_(kWaitForFirstRampUp), |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 204 | retransmission_rate_limiter_(clock_, 1000), |
| 205 | task_queue_(task_queue) {} |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 206 | |
Danil Chapovalov | 2f2049a | 2019-11-06 12:28:11 | [diff] [blame] | 207 | ~BweObserver() override { |
| 208 | // Block until all already posted tasks run to avoid races when such task |
Tommi | 3a5742c | 2020-05-20 07:32:51 | [diff] [blame] | 209 | // accesses |this|. Also make sure we free |rtp_rtcp_| on the correct |
| 210 | // thread/task queue. |
| 211 | SendTask(RTC_FROM_HERE, task_queue_, [this]() { rtp_rtcp_ = nullptr; }); |
Danil Chapovalov | 2f2049a | 2019-11-06 12:28:11 | [diff] [blame] | 212 | } |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 213 | |
Danil Chapovalov | 44db436 | 2019-09-30 02:16:28 | [diff] [blame] | 214 | std::unique_ptr<test::PacketTransport> CreateReceiveTransport( |
| 215 | TaskQueueBase* task_queue) override { |
| 216 | auto receive_transport = std::make_unique<test::PacketTransport>( |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 217 | task_queue, nullptr, this, test::PacketTransport::kReceiver, |
Artem Titov | 4e199e9 | 2018-08-20 11:30:39 | [diff] [blame] | 218 | payload_type_map_, |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 219 | std::make_unique<FakeNetworkPipe>( |
| 220 | Clock::GetRealTimeClock(), std::make_unique<SimulatedNetwork>( |
Artem Titov | 75e3647 | 2018-10-08 10:28:56 | [diff] [blame] | 221 | BuiltInNetworkBehaviorConfig()))); |
Danil Chapovalov | 44db436 | 2019-09-30 02:16:28 | [diff] [blame] | 222 | receive_transport_ = receive_transport.get(); |
| 223 | return receive_transport; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 224 | } |
| 225 | |
Niels Möller | de8e6e6 | 2018-11-13 14:10:33 | [diff] [blame] | 226 | void ModifySenderBitrateConfig( |
| 227 | BitrateConstraints* bitrate_config) override { |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 228 | // Set a high start bitrate to reduce the test completion time. |
Niels Möller | de8e6e6 | 2018-11-13 14:10:33 | [diff] [blame] | 229 | bitrate_config->start_bitrate_bps = remb_bitrate_bps_; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void ModifyVideoConfigs( |
| 233 | VideoSendStream::Config* send_config, |
| 234 | std::vector<VideoReceiveStream::Config>* receive_configs, |
| 235 | VideoEncoderConfig* encoder_config) override { |
| 236 | ASSERT_EQ(1u, send_config->rtp.ssrcs.size()); |
| 237 | sender_ssrc_ = send_config->rtp.ssrcs[0]; |
| 238 | |
| 239 | encoder_config->max_bitrate_bps = 2000000; |
| 240 | |
| 241 | ASSERT_EQ(1u, receive_configs->size()); |
Tomas Gunnarsson | f25761d | 2020-06-03 20:55:33 | [diff] [blame] | 242 | RtpRtcpInterface::Configuration config; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 243 | config.receiver_only = true; |
| 244 | config.clock = clock_; |
| 245 | config.outgoing_transport = receive_transport_; |
| 246 | config.retransmission_rate_limiter = &retransmission_rate_limiter_; |
Erik Språng | 54d5d2c | 2019-08-20 15:22:36 | [diff] [blame] | 247 | config.local_media_ssrc = (*receive_configs)[0].rtp.local_ssrc; |
Niels Moller | 2accc7d | 2021-01-12 15:54:16 | [diff] [blame] | 248 | rtp_rtcp_ = ModuleRtpRtcpImpl2::Create(config); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 249 | rtp_rtcp_->SetRemoteSSRC((*receive_configs)[0].rtp.remote_ssrc); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 250 | rtp_rtcp_->SetRTCPStatus(RtcpMode::kReducedSize); |
| 251 | } |
| 252 | |
| 253 | void OnCallsCreated(Call* sender_call, Call* receiver_call) override { |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 254 | RTC_DCHECK(sender_call); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 255 | sender_call_ = sender_call; |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 256 | task_queue_->PostTask(ToQueuedTask([this]() { PollStats(); })); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void PollStats() { |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 260 | Call::Stats stats = sender_call_->GetStats(); |
| 261 | switch (state_) { |
| 262 | case kWaitForFirstRampUp: |
| 263 | if (stats.send_bandwidth_bps >= remb_bitrate_bps_) { |
| 264 | state_ = kWaitForRemb; |
| 265 | remb_bitrate_bps_ /= 2; |
| 266 | rtp_rtcp_->SetRemb( |
| 267 | remb_bitrate_bps_, |
| 268 | std::vector<uint32_t>(&sender_ssrc_, &sender_ssrc_ + 1)); |
| 269 | rtp_rtcp_->SendRTCP(kRtcpRr); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 270 | } |
Tommi | e80885a | 2019-08-05 13:08:17 | [diff] [blame] | 271 | break; |
| 272 | |
| 273 | case kWaitForRemb: |
| 274 | if (stats.send_bandwidth_bps == remb_bitrate_bps_) { |
| 275 | state_ = kWaitForSecondRampUp; |
| 276 | remb_bitrate_bps_ *= 2; |
| 277 | rtp_rtcp_->SetRemb( |
| 278 | remb_bitrate_bps_, |
| 279 | std::vector<uint32_t>(&sender_ssrc_, &sender_ssrc_ + 1)); |
| 280 | rtp_rtcp_->SendRTCP(kRtcpRr); |
| 281 | } |
| 282 | break; |
| 283 | |
| 284 | case kWaitForSecondRampUp: |
| 285 | if (stats.send_bandwidth_bps == remb_bitrate_bps_) { |
| 286 | observation_complete_.Set(); |
| 287 | return; |
| 288 | } |
| 289 | break; |
| 290 | } |
| 291 | |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 292 | task_queue_->PostDelayedTask(ToQueuedTask([this] { PollStats(); }), 1000); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void PerformTest() override { |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 296 | EXPECT_TRUE(Wait()) |
| 297 | << "Timed out while waiting for bitrate to change according to REMB."; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | private: |
| 301 | enum TestState { kWaitForFirstRampUp, kWaitForRemb, kWaitForSecondRampUp }; |
| 302 | |
| 303 | Call* sender_call_; |
| 304 | Clock* const clock_; |
| 305 | uint32_t sender_ssrc_; |
| 306 | int remb_bitrate_bps_; |
Tomas Gunnarsson | f25761d | 2020-06-03 20:55:33 | [diff] [blame] | 307 | std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp_; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 308 | test::PacketTransport* receive_transport_; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 309 | TestState state_; |
| 310 | RateLimiter retransmission_rate_limiter_; |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 311 | TaskQueueBase* const task_queue_; |
Danil Chapovalov | d15a028 | 2019-10-22 08:48:17 | [diff] [blame] | 312 | } test(task_queue()); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 313 | |
| 314 | RunBaseTest(&test); |
| 315 | } |
| 316 | |
Ilya Nikolaevskiy | cb96062 | 2018-09-04 09:07:31 | [diff] [blame] | 317 | TEST_F(BandwidthEndToEndTest, ReportsSetEncoderRates) { |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 318 | // If these fields trial are on, we get lower bitrates than expected by this |
| 319 | // test, due to the packetization overhead and encoder pushback. |
| 320 | webrtc::test::ScopedFieldTrials field_trials( |
| 321 | std::string(field_trial::GetFieldTrialString()) + |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 322 | "WebRTC-VideoRateControl/bitrate_adjuster:false/"); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 323 | class EncoderRateStatsTest : public test::EndToEndTest, |
| 324 | public test::FakeEncoder { |
| 325 | public: |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 326 | explicit EncoderRateStatsTest(TaskQueueBase* task_queue) |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 327 | : EndToEndTest(kDefaultTimeoutMs), |
| 328 | FakeEncoder(Clock::GetRealTimeClock()), |
| 329 | task_queue_(task_queue), |
| 330 | send_stream_(nullptr), |
Niels Möller | 4db138e | 2018-04-19 07:04:13 | [diff] [blame] | 331 | encoder_factory_(this), |
Jiawei Ou | c2ebe21 | 2018-11-08 18:02:56 | [diff] [blame] | 332 | bitrate_allocator_factory_( |
| 333 | CreateBuiltinVideoBitrateAllocatorFactory()), |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 334 | bitrate_kbps_(0) {} |
| 335 | |
| 336 | void OnVideoStreamsCreated( |
| 337 | VideoSendStream* send_stream, |
| 338 | const std::vector<VideoReceiveStream*>& receive_streams) override { |
| 339 | send_stream_ = send_stream; |
| 340 | } |
| 341 | |
| 342 | void ModifyVideoConfigs( |
| 343 | VideoSendStream::Config* send_config, |
| 344 | std::vector<VideoReceiveStream::Config>* receive_configs, |
| 345 | VideoEncoderConfig* encoder_config) override { |
Niels Möller | 4db138e | 2018-04-19 07:04:13 | [diff] [blame] | 346 | send_config->encoder_settings.encoder_factory = &encoder_factory_; |
Jiawei Ou | c2ebe21 | 2018-11-08 18:02:56 | [diff] [blame] | 347 | send_config->encoder_settings.bitrate_allocator_factory = |
| 348 | bitrate_allocator_factory_.get(); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 349 | RTC_DCHECK_EQ(1, encoder_config->number_of_streams); |
| 350 | } |
| 351 | |
Erik Språng | 16cb8f5 | 2019-04-12 11:59:09 | [diff] [blame] | 352 | void SetRates(const RateControlParameters& parameters) override { |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 353 | // Make sure not to trigger on any default zero bitrates. |
Erik Språng | 16cb8f5 | 2019-04-12 11:59:09 | [diff] [blame] | 354 | if (parameters.bitrate.get_sum_bps() == 0) |
| 355 | return; |
Markus Handell | 9bbff07 | 2020-07-07 12:23:18 | [diff] [blame] | 356 | MutexLock lock(&mutex_); |
Erik Språng | 16cb8f5 | 2019-04-12 11:59:09 | [diff] [blame] | 357 | bitrate_kbps_ = parameters.bitrate.get_sum_kbps(); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 358 | observation_complete_.Set(); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void PerformTest() override { |
| 362 | ASSERT_TRUE(Wait()) |
| 363 | << "Timed out while waiting for encoder SetRates() call."; |
| 364 | |
Danil Chapovalov | 82a3f0a | 2019-10-21 07:24:27 | [diff] [blame] | 365 | SendTask(RTC_FROM_HERE, task_queue_, [this]() { |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 366 | WaitForEncoderTargetBitrateMatchStats(); |
| 367 | send_stream_->Stop(); |
| 368 | WaitForStatsReportZeroTargetBitrate(); |
| 369 | send_stream_->Start(); |
| 370 | WaitForEncoderTargetBitrateMatchStats(); |
| 371 | }); |
| 372 | } |
| 373 | |
| 374 | void WaitForEncoderTargetBitrateMatchStats() { |
| 375 | for (int i = 0; i < kDefaultTimeoutMs; ++i) { |
| 376 | VideoSendStream::Stats stats = send_stream_->GetStats(); |
| 377 | { |
Markus Handell | 9bbff07 | 2020-07-07 12:23:18 | [diff] [blame] | 378 | MutexLock lock(&mutex_); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 379 | if ((stats.target_media_bitrate_bps + 500) / 1000 == |
| 380 | static_cast<int>(bitrate_kbps_)) { |
| 381 | return; |
| 382 | } |
| 383 | } |
| 384 | SleepMs(1); |
| 385 | } |
| 386 | FAIL() |
| 387 | << "Timed out waiting for stats reporting the currently set bitrate."; |
| 388 | } |
| 389 | |
| 390 | void WaitForStatsReportZeroTargetBitrate() { |
| 391 | for (int i = 0; i < kDefaultTimeoutMs; ++i) { |
| 392 | if (send_stream_->GetStats().target_media_bitrate_bps == 0) { |
| 393 | return; |
| 394 | } |
| 395 | SleepMs(1); |
| 396 | } |
| 397 | FAIL() << "Timed out waiting for stats reporting zero bitrate."; |
| 398 | } |
| 399 | |
| 400 | private: |
Danil Chapovalov | 85a1000 | 2019-10-21 13:00:53 | [diff] [blame] | 401 | TaskQueueBase* const task_queue_; |
Markus Handell | 9bbff07 | 2020-07-07 12:23:18 | [diff] [blame] | 402 | Mutex mutex_; |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 403 | VideoSendStream* send_stream_; |
Niels Möller | cbcbc22 | 2018-09-28 07:07:24 | [diff] [blame] | 404 | test::VideoEncoderProxyFactory encoder_factory_; |
Jiawei Ou | c2ebe21 | 2018-11-08 18:02:56 | [diff] [blame] | 405 | std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_; |
Markus Handell | 9bbff07 | 2020-07-07 12:23:18 | [diff] [blame] | 406 | uint32_t bitrate_kbps_ RTC_GUARDED_BY(mutex_); |
Danil Chapovalov | d15a028 | 2019-10-22 08:48:17 | [diff] [blame] | 407 | } test(task_queue()); |
Sebastian Jansson | c501713 | 2018-02-02 15:24:16 | [diff] [blame] | 408 | |
| 409 | RunBaseTest(&test); |
| 410 | } |
| 411 | } // namespace webrtc |