bitrate_estimator_tests.cc (12309B)
1 /* 2 * Copyright (c) 2013 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 #include <cstddef> 11 #include <functional> 12 #include <list> 13 #include <memory> 14 #include <optional> 15 #include <string> 16 #include <vector> 17 18 #include "absl/strings/match.h" 19 #include "absl/strings/string_view.h" 20 #include "api/rtp_parameters.h" 21 #include "api/test/create_frame_generator.h" 22 #include "api/test/simulated_network.h" 23 #include "api/test/video/function_video_decoder_factory.h" 24 #include "api/video/video_codec_type.h" 25 #include "api/video_codecs/sdp_video_format.h" 26 #include "call/call.h" 27 #include "call/video_receive_stream.h" 28 #include "call/video_send_stream.h" 29 #include "rtc_base/checks.h" 30 #include "rtc_base/event.h" 31 #include "rtc_base/logging.h" 32 #include "rtc_base/synchronization/mutex.h" 33 #include "rtc_base/task_queue_for_test.h" 34 #include "rtc_base/thread_annotations.h" 35 #include "test/call_test.h" 36 #include "test/encoder_settings.h" 37 #include "test/fake_decoder.h" 38 #include "test/frame_generator_capturer.h" 39 #include "test/gtest.h" 40 #include "test/video_test_constants.h" 41 #include "video/config/video_encoder_config.h" 42 43 namespace webrtc { 44 namespace { 45 // Note: If you consider to re-use this class, think twice and instead consider 46 // writing tests that don't depend on the logging system. 47 class LogObserver { 48 public: 49 LogObserver() { LogMessage::AddLogToStream(&callback_, LS_INFO); } 50 51 ~LogObserver() { LogMessage::RemoveLogToStream(&callback_); } 52 53 void PushExpectedLogLine(absl::string_view expected_log_line) { 54 callback_.PushExpectedLogLine(expected_log_line); 55 } 56 57 bool Wait() { return callback_.Wait(); } 58 59 private: 60 class Callback : public LogSink { 61 public: 62 void OnLogMessage(const std::string& message) override { 63 OnLogMessage(absl::string_view(message)); 64 } 65 66 void OnLogMessage(absl::string_view message) override { 67 MutexLock lock(&mutex_); 68 // Ignore log lines that are due to missing AST extensions, these are 69 // logged when we switch back from AST to TOF until the wrapping bitrate 70 // estimator gives up on using AST. 71 if (absl::StrContains(message, "BitrateEstimator") && 72 !absl::StrContains(message, "packet is missing")) { 73 received_log_lines_.push_back(std::string(message)); 74 } 75 76 int num_popped = 0; 77 while (!received_log_lines_.empty() && !expected_log_lines_.empty()) { 78 std::string a = received_log_lines_.front(); 79 std::string b = expected_log_lines_.front(); 80 received_log_lines_.pop_front(); 81 expected_log_lines_.pop_front(); 82 num_popped++; 83 EXPECT_TRUE(a.find(b) != absl::string_view::npos) << a << " != " << b; 84 } 85 if (expected_log_lines_.empty()) { 86 if (num_popped > 0) { 87 done_.Set(); 88 } 89 return; 90 } 91 } 92 93 bool Wait() { 94 return done_.Wait(test::VideoTestConstants::kDefaultTimeout); 95 } 96 97 void PushExpectedLogLine(absl::string_view expected_log_line) { 98 MutexLock lock(&mutex_); 99 expected_log_lines_.emplace_back(expected_log_line); 100 } 101 102 private: 103 typedef std::list<std::string> Strings; 104 Mutex mutex_; 105 Strings received_log_lines_ RTC_GUARDED_BY(mutex_); 106 Strings expected_log_lines_ RTC_GUARDED_BY(mutex_); 107 Event done_; 108 }; 109 110 Callback callback_; 111 }; 112 } // namespace 113 114 static const int kTOFExtensionId = 4; 115 static const int kASTExtensionId = 5; 116 117 class BitrateEstimatorTest : public test::CallTest { 118 public: 119 BitrateEstimatorTest() : receive_config_(nullptr) {} 120 121 ~BitrateEstimatorTest() override { EXPECT_TRUE(streams_.empty()); } 122 123 void SetUp() override { 124 SendTask(task_queue(), [this]() { 125 RegisterRtpExtension( 126 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId)); 127 RegisterRtpExtension( 128 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId)); 129 130 CreateCalls(); 131 132 CreateSendTransport(BuiltInNetworkBehaviorConfig(), /*observer=*/nullptr); 133 CreateReceiveTransport(BuiltInNetworkBehaviorConfig(), 134 /*observer=*/nullptr); 135 136 VideoSendStream::Config video_send_config(send_transport_.get()); 137 video_send_config.rtp.ssrcs.push_back( 138 test::VideoTestConstants::kVideoSendSsrcs[0]); 139 video_send_config.encoder_settings.encoder_factory = 140 &fake_encoder_factory_; 141 video_send_config.encoder_settings.bitrate_allocator_factory = 142 bitrate_allocator_factory_.get(); 143 video_send_config.rtp.payload_name = "FAKE"; 144 video_send_config.rtp.payload_type = 145 test::VideoTestConstants::kFakeVideoSendPayloadType; 146 SetVideoSendConfig(video_send_config); 147 VideoEncoderConfig video_encoder_config; 148 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config); 149 SetVideoEncoderConfig(video_encoder_config); 150 151 receive_config_ = 152 VideoReceiveStreamInterface::Config(receive_transport_.get()); 153 // receive_config_.decoders will be set by every stream separately. 154 receive_config_.rtp.remote_ssrc = GetVideoSendConfig()->rtp.ssrcs[0]; 155 receive_config_.rtp.local_ssrc = 156 test::VideoTestConstants::kReceiverLocalVideoSsrc; 157 }); 158 } 159 160 void TearDown() override { 161 SendTask(task_queue(), [this]() { 162 for (auto* stream : streams_) { 163 stream->StopSending(); 164 delete stream; 165 } 166 streams_.clear(); 167 DestroyCalls(); 168 }); 169 } 170 171 protected: 172 friend class Stream; 173 174 class Stream { 175 public: 176 explicit Stream(BitrateEstimatorTest* test) 177 : test_(test), 178 is_sending_receiving_(false), 179 send_stream_(nullptr), 180 frame_generator_capturer_(), 181 decoder_factory_( 182 []() { return std::make_unique<test::FakeDecoder>(); }) { 183 test_->GetVideoSendConfig()->rtp.ssrcs[0]++; 184 send_stream_ = test_->sender_call_->CreateVideoSendStream( 185 test_->GetVideoSendConfig()->Copy(), 186 test_->GetVideoEncoderConfig()->Copy()); 187 RTC_DCHECK_EQ(1, test_->GetVideoEncoderConfig()->number_of_streams); 188 frame_generator_capturer_ = 189 std::make_unique<test::FrameGeneratorCapturer>( 190 &test->env().clock(), 191 test::CreateSquareFrameGenerator( 192 test::VideoTestConstants::kDefaultWidth, 193 test::VideoTestConstants::kDefaultHeight, std::nullopt, 194 std::nullopt), 195 test::VideoTestConstants::kDefaultFramerate, 196 test->env().task_queue_factory()); 197 frame_generator_capturer_->Init(); 198 frame_generator_capturer_->Start(); 199 send_stream_->SetSource(frame_generator_capturer_.get(), 200 DegradationPreference::MAINTAIN_FRAMERATE); 201 send_stream_->Start(); 202 203 VideoReceiveStreamInterface::Decoder decoder; 204 test_->receive_config_.decoder_factory = &decoder_factory_; 205 decoder.payload_type = test_->GetVideoSendConfig()->rtp.payload_type; 206 decoder.video_format = 207 SdpVideoFormat(test_->GetVideoSendConfig()->rtp.payload_name); 208 test_->receive_config_.decoders.clear(); 209 test_->receive_config_.decoders.push_back(decoder); 210 test_->receive_config_.rtp.remote_ssrc = 211 test_->GetVideoSendConfig()->rtp.ssrcs[0]; 212 test_->receive_config_.rtp.local_ssrc++; 213 test_->receive_config_.renderer = &test->fake_renderer_; 214 video_receive_stream_ = test_->receiver_call_->CreateVideoReceiveStream( 215 test_->receive_config_.Copy()); 216 video_receive_stream_->Start(); 217 is_sending_receiving_ = true; 218 } 219 220 ~Stream() { 221 EXPECT_FALSE(is_sending_receiving_); 222 test_->sender_call_->DestroyVideoSendStream(send_stream_); 223 frame_generator_capturer_.reset(nullptr); 224 send_stream_ = nullptr; 225 if (video_receive_stream_) { 226 test_->receiver_call_->DestroyVideoReceiveStream(video_receive_stream_); 227 video_receive_stream_ = nullptr; 228 } 229 } 230 231 void StopSending() { 232 if (is_sending_receiving_) { 233 send_stream_->Stop(); 234 if (video_receive_stream_) { 235 video_receive_stream_->Stop(); 236 } 237 is_sending_receiving_ = false; 238 } 239 } 240 241 private: 242 BitrateEstimatorTest* test_; 243 bool is_sending_receiving_; 244 VideoSendStream* send_stream_; 245 VideoReceiveStreamInterface* video_receive_stream_; 246 std::unique_ptr<test::FrameGeneratorCapturer> frame_generator_capturer_; 247 248 test::FunctionVideoDecoderFactory decoder_factory_; 249 }; 250 251 LogObserver receiver_log_; 252 VideoReceiveStreamInterface::Config receive_config_; 253 std::vector<Stream*> streams_; 254 }; 255 256 static const char* kAbsSendTimeLog = 257 "RemoteBitrateEstimatorAbsSendTime: Instantiating."; 258 static const char* kSingleStreamLog = 259 "RemoteBitrateEstimatorSingleStream: Instantiating."; 260 261 TEST_F(BitrateEstimatorTest, InstantiatesTOFPerDefaultForVideo) { 262 SendTask(task_queue(), [this]() { 263 GetVideoSendConfig()->rtp.extensions.push_back( 264 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId)); 265 receiver_log_.PushExpectedLogLine(kSingleStreamLog); 266 receiver_log_.PushExpectedLogLine(kSingleStreamLog); 267 streams_.push_back(new Stream(this)); 268 }); 269 EXPECT_TRUE(receiver_log_.Wait()); 270 } 271 272 TEST_F(BitrateEstimatorTest, ImmediatelySwitchToASTForVideo) { 273 SendTask(task_queue(), [this]() { 274 GetVideoSendConfig()->rtp.extensions.push_back( 275 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId)); 276 receiver_log_.PushExpectedLogLine(kSingleStreamLog); 277 receiver_log_.PushExpectedLogLine(kSingleStreamLog); 278 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE."); 279 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog); 280 streams_.push_back(new Stream(this)); 281 }); 282 EXPECT_TRUE(receiver_log_.Wait()); 283 } 284 285 TEST_F(BitrateEstimatorTest, SwitchesToASTForVideo) { 286 SendTask(task_queue(), [this]() { 287 GetVideoSendConfig()->rtp.extensions.push_back( 288 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId)); 289 receiver_log_.PushExpectedLogLine(kSingleStreamLog); 290 receiver_log_.PushExpectedLogLine(kSingleStreamLog); 291 streams_.push_back(new Stream(this)); 292 }); 293 EXPECT_TRUE(receiver_log_.Wait()); 294 295 SendTask(task_queue(), [this]() { 296 GetVideoSendConfig()->rtp.extensions[0] = 297 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId); 298 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE."); 299 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog); 300 streams_.push_back(new Stream(this)); 301 }); 302 EXPECT_TRUE(receiver_log_.Wait()); 303 } 304 305 // This test is flaky. See webrtc:5790. 306 TEST_F(BitrateEstimatorTest, DISABLED_SwitchesToASTThenBackToTOFForVideo) { 307 SendTask(task_queue(), [this]() { 308 GetVideoSendConfig()->rtp.extensions.push_back( 309 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId)); 310 receiver_log_.PushExpectedLogLine(kSingleStreamLog); 311 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog); 312 receiver_log_.PushExpectedLogLine(kSingleStreamLog); 313 streams_.push_back(new Stream(this)); 314 }); 315 EXPECT_TRUE(receiver_log_.Wait()); 316 317 SendTask(task_queue(), [this]() { 318 GetVideoSendConfig()->rtp.extensions[0] = 319 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId); 320 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog); 321 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE."); 322 streams_.push_back(new Stream(this)); 323 }); 324 EXPECT_TRUE(receiver_log_.Wait()); 325 326 SendTask(task_queue(), [this]() { 327 GetVideoSendConfig()->rtp.extensions[0] = 328 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId); 329 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog); 330 receiver_log_.PushExpectedLogLine( 331 "WrappingBitrateEstimator: Switching to transmission time offset RBE."); 332 streams_.push_back(new Stream(this)); 333 streams_[0]->StopSending(); 334 streams_[1]->StopSending(); 335 }); 336 EXPECT_TRUE(receiver_log_.Wait()); 337 } 338 } // namespace webrtc