| 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 |
| 11 | #include <algorithm> |
| 12 | #include <limits> |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 |
| 16 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
| 17 | #include "api/rtc_event_log/rtc_event_log.h" |
| 18 | #include "api/task_queue/task_queue_base.h" |
| 19 | #include "api/test/simulated_network.h" |
| 20 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
| 21 | #include "api/video/video_bitrate_allocation.h" |
| 22 | #include "api/video_codecs/video_encoder.h" |
| 23 | #include "api/video_codecs/video_encoder_config.h" |
| 24 | #include "call/call.h" |
| 25 | #include "call/fake_network_pipe.h" |
| 26 | #include "call/simulated_network.h" |
| 27 | #include "media/engine/internal_encoder_factory.h" |
| 28 | #include "media/engine/simulcast_encoder_adapter.h" |
| 29 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 30 | #include "modules/audio_device/include/test_audio_device.h" |
| 31 | #include "modules/audio_mixer/audio_mixer_impl.h" |
| 32 | #include "modules/rtp_rtcp/source/rtp_packet.h" |
| 33 | #include "rtc_base/checks.h" |
| 34 | #include "rtc_base/synchronization/mutex.h" |
| 35 | #include "rtc_base/task_queue_for_test.h" |
| 36 | #include "rtc_base/task_utils/pending_task_safety_flag.h" |
| 37 | #include "rtc_base/thread.h" |
| 38 | #include "rtc_base/thread_annotations.h" |
| 39 | #include "system_wrappers/include/metrics.h" |
| 40 | #include "test/call_test.h" |
| 41 | #include "test/direct_transport.h" |
| 42 | #include "test/drifting_clock.h" |
| 43 | #include "test/encoder_settings.h" |
| 44 | #include "test/fake_encoder.h" |
| 45 | #include "test/field_trial.h" |
| 46 | #include "test/frame_generator_capturer.h" |
| 47 | #include "test/gtest.h" |
| 48 | #include "test/null_transport.h" |
| 49 | #include "test/rtp_rtcp_observer.h" |
| 50 | #include "test/testsupport/file_utils.h" |
| 51 | #include "test/testsupport/perf_test.h" |
| 52 | #include "test/video_encoder_proxy_factory.h" |
| 53 | #include "video/transport_adapter.h" |
| 54 |
| 55 | using webrtc::test::DriftingClock; |
| 56 |
| 57 | namespace webrtc { |
| 58 | namespace { |
| 59 | enum : int { // The first valid value is 1. |
| 60 | kTransportSequenceNumberExtensionId = 1, |
| 61 | }; |
| 62 | } // namespace |
| 63 |
| 64 | class CallPerfTest : public test::CallTest { |
| 65 | public: |
| 66 | CallPerfTest() { |
| 67 | RegisterRtpExtension(RtpExtension(RtpExtension::kTransportSequenceNumberUri, |
| 68 | kTransportSequenceNumberExtensionId)); |
| 69 | } |
| 70 |
| 71 | protected: |
| 72 | enum class FecMode { kOn, kOff }; |
| 73 | enum class CreateOrder { kAudioFirst, kVideoFirst }; |
| 74 | void TestAudioVideoSync(FecMode fec, |
| 75 | CreateOrder create_first, |
| 76 | float video_ntp_speed, |
| 77 | float video_rtp_speed, |
| 78 | float audio_rtp_speed, |
| 79 | const std::string& test_label); |
| 80 |
| 81 | void TestMinTransmitBitrate(bool pad_to_min_bitrate); |
| 82 |
| 83 | void TestCaptureNtpTime(const BuiltInNetworkBehaviorConfig& net_config, |
| 84 | int threshold_ms, |
| 85 | int start_time_ms, |
| 86 | int run_time_ms); |
| 87 | void TestMinAudioVideoBitrate(int test_bitrate_from, |
| 88 | int test_bitrate_to, |
| 89 | int test_bitrate_step, |
| 90 | int min_bwe, |
| 91 | int start_bwe, |
| 92 | int max_bwe); |
| 93 | void TestEncodeFramerate(VideoEncoderFactory* encoder_factory, |
| 94 | const std::string& payload_name, |
| 95 | const std::vector<int>& max_framerates); |
| 96 | }; |
| 97 |
| 98 | class VideoRtcpAndSyncObserver : public test::RtpRtcpObserver, |
| 99 | public rtc::VideoSinkInterface<VideoFrame> { |
| 100 | static const int kInSyncThresholdMs = 50; |
| 101 | static const int kStartupTimeMs = 2000; |
| 102 | static const int kMinRunTimeMs = 30000; |
| 103 |
| 104 | public: |
| 105 | explicit VideoRtcpAndSyncObserver(TaskQueueBase* task_queue, |
| 106 | Clock* clock, |
| 107 | const std::string& test_label) |
| 108 | : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs), |
| 109 | clock_(clock), |
| 110 | test_label_(test_label), |
| 111 | creation_time_ms_(clock_->TimeInMilliseconds()), |
| 112 | task_queue_(task_queue) {} |
| 113 |
| 114 | void OnFrame(const VideoFrame& video_frame) override { |
| 115 | task_queue_->PostTask(ToQueuedTask([this]() { CheckStats(); })); |
| 116 | } |
| 117 |
| 118 | void CheckStats() { |
| 119 | if (!receive_stream_) |
| 120 | return; |
| 121 |
| 122 | VideoReceiveStream::Stats stats = receive_stream_->GetStats(); |
| 123 | if (stats.sync_offset_ms == std::numeric_limits<int>::max()) |
| 124 | return; |
| 125 |
| 126 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 127 | int64_t time_since_creation = now_ms - creation_time_ms_; |
| 128 | // During the first couple of seconds audio and video can falsely be |
| 129 | // estimated as being synchronized. We don't want to trigger on those. |
| 130 | if (time_since_creation < kStartupTimeMs) |
| 131 | return; |
| 132 | if (std::abs(stats.sync_offset_ms) < kInSyncThresholdMs) { |
| 133 | if (first_time_in_sync_ == -1) { |
| 134 | first_time_in_sync_ = now_ms; |
| 135 | webrtc::test::PrintResult("sync_convergence_time", test_label_, |
| 136 | "synchronization", time_since_creation, "ms", |
| 137 | false); |
| 138 | } |
| 139 | if (time_since_creation > kMinRunTimeMs) |
| 140 | observation_complete_.Set(); |
| 141 | } |
| 142 | if (first_time_in_sync_ != -1) |
| 143 | sync_offset_ms_list_.push_back(stats.sync_offset_ms); |
| 144 | } |
| 145 |
| 146 | void set_receive_stream(VideoReceiveStream* receive_stream) { |
| 147 | RTC_DCHECK_EQ(task_queue_, TaskQueueBase::Current()); |
| 148 | // Note that receive_stream may be nullptr. |
| 149 | receive_stream_ = receive_stream; |
| 150 | } |
| 151 |
| 152 | void PrintResults() { |
| 153 | test::PrintResultList("stream_offset", test_label_, "synchronization", |
| 154 | sync_offset_ms_list_, "ms", false); |
| 155 | } |
| 156 |
| 157 | private: |
| 158 | Clock* const clock_; |
| 159 | const std::string test_label_; |
| 160 | const int64_t creation_time_ms_; |
| 161 | int64_t first_time_in_sync_ = -1; |
| 162 | VideoReceiveStream* receive_stream_ = nullptr; |
| 163 | std::vector<double> sync_offset_ms_list_; |
| 164 | TaskQueueBase* const task_queue_; |
| 165 | }; |
| 166 |
| 167 | void CallPerfTest::TestAudioVideoSync(FecMode fec, |
| 168 | CreateOrder create_first, |
| 169 | float video_ntp_speed, |
| 170 | float video_rtp_speed, |
| 171 | float audio_rtp_speed, |
| 172 | const std::string& test_label) { |
| 173 | const char* kSyncGroup = "av_sync"; |
| 174 | const uint32_t kAudioSendSsrc = 1234; |
| 175 | const uint32_t kAudioRecvSsrc = 5678; |
| 176 |
| 177 | BuiltInNetworkBehaviorConfig audio_net_config; |
| 178 | audio_net_config.queue_delay_ms = 500; |
| 179 | audio_net_config.loss_percent = 5; |
| 180 |
| 181 | auto observer = std::make_unique<VideoRtcpAndSyncObserver>( |
| 182 | task_queue(), Clock::GetRealTimeClock(), test_label); |
| 183 |
| 184 | std::map<uint8_t, MediaType> audio_pt_map; |
| 185 | std::map<uint8_t, MediaType> video_pt_map; |
| 186 |
| 187 | std::unique_ptr<test::PacketTransport> audio_send_transport; |
| 188 | std::unique_ptr<test::PacketTransport> video_send_transport; |
| 189 | std::unique_ptr<test::PacketTransport> receive_transport; |
| 190 |
| 191 | AudioSendStream* audio_send_stream; |
| 192 | AudioReceiveStream* audio_receive_stream; |
| 193 | std::unique_ptr<DriftingClock> drifting_clock; |
| 194 |
| 195 | SendTask(RTC_FROM_HERE, task_queue(), [&]() { |
| 196 | metrics::Reset(); |
| 197 | rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device = |
| 198 | TestAudioDeviceModule::Create( |
| 199 | task_queue_factory_.get(), |
| 200 | TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000), |
| 201 | TestAudioDeviceModule::CreateDiscardRenderer(48000), |
| 202 | audio_rtp_speed); |
| 203 | EXPECT_EQ(0, fake_audio_device->Init()); |
| 204 |
| 205 | AudioState::Config send_audio_state_config; |
| 206 | send_audio_state_config.audio_mixer = AudioMixerImpl::Create(); |
| 207 | send_audio_state_config.audio_processing = |
| 208 | AudioProcessingBuilder().Create(); |
| 209 | send_audio_state_config.audio_device_module = fake_audio_device; |
| 210 | Call::Config sender_config(send_event_log_.get()); |
| 211 |
| 212 | auto audio_state = AudioState::Create(send_audio_state_config); |
| 213 | fake_audio_device->RegisterAudioCallback(audio_state->audio_transport()); |
| 214 | sender_config.audio_state = audio_state; |
| 215 | Call::Config receiver_config(recv_event_log_.get()); |
| 216 | receiver_config.audio_state = audio_state; |
| 217 | CreateCalls(sender_config, receiver_config); |
| 218 |
| 219 | std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_), |
| 220 | std::inserter(audio_pt_map, audio_pt_map.end()), |
| 221 | [](const std::pair<const uint8_t, MediaType>& pair) { |
| 222 | return pair.second == MediaType::AUDIO; |
| 223 | }); |
| 224 | std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_), |
| 225 | std::inserter(video_pt_map, video_pt_map.end()), |
| 226 | [](const std::pair<const uint8_t, MediaType>& pair) { |
| 227 | return pair.second == MediaType::VIDEO; |
| 228 | }); |
| 229 |
| 230 | audio_send_transport = std::make_unique<test::PacketTransport>( |
| 231 | task_queue(), sender_call_.get(), observer.get(), |
| 232 | test::PacketTransport::kSender, audio_pt_map, |
| 233 | std::make_unique<FakeNetworkPipe>( |
| 234 | Clock::GetRealTimeClock(), |
| 235 | std::make_unique<SimulatedNetwork>(audio_net_config))); |
| 236 | audio_send_transport->SetReceiver(receiver_call_->Receiver()); |
| 237 |
| 238 | video_send_transport = std::make_unique<test::PacketTransport>( |
| 239 | task_queue(), sender_call_.get(), observer.get(), |
| 240 | test::PacketTransport::kSender, video_pt_map, |
| 241 | std::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(), |
| 242 | std::make_unique<SimulatedNetwork>( |
| 243 | BuiltInNetworkBehaviorConfig()))); |
| 244 | video_send_transport->SetReceiver(receiver_call_->Receiver()); |
| 245 |
| 246 | receive_transport = std::make_unique<test::PacketTransport>( |
| 247 | task_queue(), receiver_call_.get(), observer.get(), |
| 248 | test::PacketTransport::kReceiver, payload_type_map_, |
| 249 | std::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(), |
| 250 | std::make_unique<SimulatedNetwork>( |
| 251 | BuiltInNetworkBehaviorConfig()))); |
| 252 | receive_transport->SetReceiver(sender_call_->Receiver()); |
| 253 |
| 254 | CreateSendConfig(1, 0, 0, video_send_transport.get()); |
| 255 | CreateMatchingReceiveConfigs(receive_transport.get()); |
| 256 |
| 257 | AudioSendStream::Config audio_send_config(audio_send_transport.get()); |
| 258 | audio_send_config.rtp.ssrc = kAudioSendSsrc; |
| 259 | audio_send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec( |
| 260 | kAudioSendPayloadType, {"ISAC", 16000, 1}); |
| 261 | audio_send_config.encoder_factory = CreateBuiltinAudioEncoderFactory(); |
| 262 | audio_send_stream = sender_call_->CreateAudioSendStream(audio_send_config); |
| 263 |
| 264 | GetVideoSendConfig()->rtp.nack.rtp_history_ms = kNackRtpHistoryMs; |
| 265 | if (fec == FecMode::kOn) { |
| 266 | GetVideoSendConfig()->rtp.ulpfec.red_payload_type = kRedPayloadType; |
| 267 | GetVideoSendConfig()->rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType; |
| 268 | video_receive_configs_[0].rtp.red_payload_type = kRedPayloadType; |
| 269 | video_receive_configs_[0].rtp.ulpfec_payload_type = kUlpfecPayloadType; |
| 270 | } |
| 271 | video_receive_configs_[0].rtp.nack.rtp_history_ms = 1000; |
| 272 | video_receive_configs_[0].renderer = observer.get(); |
| 273 | video_receive_configs_[0].sync_group = kSyncGroup; |
| 274 |
| 275 | AudioReceiveStream::Config audio_recv_config; |
| 276 | audio_recv_config.rtp.remote_ssrc = kAudioSendSsrc; |
| 277 | audio_recv_config.rtp.local_ssrc = kAudioRecvSsrc; |
| 278 | audio_recv_config.rtcp_send_transport = receive_transport.get(); |
| 279 | audio_recv_config.sync_group = kSyncGroup; |
| 280 | audio_recv_config.decoder_factory = audio_decoder_factory_; |
| 281 | audio_recv_config.decoder_map = { |
| 282 | {kAudioSendPayloadType, {"ISAC", 16000, 1}}}; |
| 283 |
| 284 | if (create_first == CreateOrder::kAudioFirst) { |
| 285 | audio_receive_stream = |
| 286 | receiver_call_->CreateAudioReceiveStream(audio_recv_config); |
| 287 | CreateVideoStreams(); |
| 288 | } else { |
| 289 | CreateVideoStreams(); |
| 290 | audio_receive_stream = |
| 291 | receiver_call_->CreateAudioReceiveStream(audio_recv_config); |
| 292 | } |
| 293 | EXPECT_EQ(1u, video_receive_streams_.size()); |
| 294 | observer->set_receive_stream(video_receive_streams_[0]); |
| 295 | drifting_clock = std::make_unique<DriftingClock>(clock_, video_ntp_speed); |
| 296 | CreateFrameGeneratorCapturerWithDrift(drifting_clock.get(), video_rtp_speed, |
| 297 | kDefaultFramerate, kDefaultWidth, |
| 298 | kDefaultHeight); |
| 299 |
| 300 | Start(); |
| 301 |
| 302 | audio_send_stream->Start(); |
| 303 | audio_receive_stream->Start(); |
| 304 | }); |
| 305 |
| 306 | EXPECT_TRUE(observer->Wait()) |
| 307 | << "Timed out while waiting for audio and video to be synchronized."; |
| 308 |
| 309 | SendTask(RTC_FROM_HERE, task_queue(), [&]() { |
| 310 | // Clear the pointer to the receive stream since it will now be deleted. |
| 311 | observer->set_receive_stream(nullptr); |
| 312 |
| 313 | audio_send_stream->Stop(); |
| 314 | audio_receive_stream->Stop(); |
| 315 |
| 316 | Stop(); |
| 317 |
| 318 | DestroyStreams(); |
| 319 |
| 320 | sender_call_->DestroyAudioSendStream(audio_send_stream); |
| 321 | receiver_call_->DestroyAudioReceiveStream(audio_receive_stream); |
| 322 |
| 323 | DestroyCalls(); |
| 324 | // Call may post periodic rtcp packet to the transport on the process |
| 325 | // thread, thus transport should be destroyed after the call objects. |
| 326 | // Though transports keep pointers to the call objects, transports handle |
| 327 | // packets on the task_queue() and thus wouldn't create a race while current |
| 328 | // destruction happens in the same task as destruction of the call objects. |
| 329 | video_send_transport.reset(); |
| 330 | audio_send_transport.reset(); |
| 331 | receive_transport.reset(); |
| 332 | }); |
| 333 |
| 334 | observer->PrintResults(); |
| 335 |
| 336 | // In quick test synchronization may not be achieved in time. |
| 337 | if (!field_trial::IsEnabled("WebRTC-QuickPerfTest")) { |
| 338 | // TODO(bugs.webrtc.org/10417): Reenable this for iOS |
| 339 | #if !defined(WEBRTC_IOS) |
| 340 | EXPECT_METRIC_EQ(1, metrics::NumSamples("WebRTC.Video.AVSyncOffsetInMs")); |
| 341 | #endif |
| 342 | } |
| 343 |
| 344 | task_queue()->PostTask( |
| 345 | ToQueuedTask([to_delete = observer.release()]() { delete to_delete; })); |
| 346 | } |
| 347 |
| 348 | TEST_F(CallPerfTest, Synchronization_PlaysOutAudioAndVideoWithoutClockDrift) { |
| 349 | TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst, |
| 350 | DriftingClock::kNoDrift, DriftingClock::kNoDrift, |
| 351 | DriftingClock::kNoDrift, "_video_no_drift"); |
| 352 | } |
| 353 |
| 354 | TEST_F(CallPerfTest, Synchronization_PlaysOutAudioAndVideoWithVideoNtpDrift) { |
| 355 | TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst, |
| 356 | DriftingClock::PercentsFaster(10.0f), |
| 357 | DriftingClock::kNoDrift, DriftingClock::kNoDrift, |
| 358 | "_video_ntp_drift"); |
| 359 | } |
| 360 |
| 361 | TEST_F(CallPerfTest, |
| 362 | Synchronization_PlaysOutAudioAndVideoWithAudioFasterThanVideoDrift) { |
| 363 | TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst, |
| 364 | DriftingClock::kNoDrift, |
| 365 | DriftingClock::PercentsSlower(30.0f), |
| 366 | DriftingClock::PercentsFaster(30.0f), "_audio_faster"); |
| 367 | } |
| 368 |
| 369 | TEST_F(CallPerfTest, |
| 370 | Synchronization_PlaysOutAudioAndVideoWithVideoFasterThanAudioDrift) { |
| 371 | TestAudioVideoSync(FecMode::kOn, CreateOrder::kVideoFirst, |
| 372 | DriftingClock::kNoDrift, |
| 373 | DriftingClock::PercentsFaster(30.0f), |
| 374 | DriftingClock::PercentsSlower(30.0f), "_video_faster"); |
| 375 | } |
| 376 |
| 377 | void CallPerfTest::TestCaptureNtpTime( |
| 378 | const BuiltInNetworkBehaviorConfig& net_config, |
| 379 | int threshold_ms, |
| 380 | int start_time_ms, |
| 381 | int run_time_ms) { |
| 382 | class CaptureNtpTimeObserver : public test::EndToEndTest, |
| 383 | public rtc::VideoSinkInterface<VideoFrame> { |
| 384 | public: |
| 385 | CaptureNtpTimeObserver(const BuiltInNetworkBehaviorConfig& net_config, |
| 386 | int threshold_ms, |
| 387 | int start_time_ms, |
| 388 | int run_time_ms) |
| 389 | : EndToEndTest(kLongTimeoutMs), |
| 390 | net_config_(net_config), |
| 391 | clock_(Clock::GetRealTimeClock()), |
| 392 | threshold_ms_(threshold_ms), |
| 393 | start_time_ms_(start_time_ms), |
| 394 | run_time_ms_(run_time_ms), |
| 395 | creation_time_ms_(clock_->TimeInMilliseconds()), |
| 396 | capturer_(nullptr), |
| 397 | rtp_start_timestamp_set_(false), |
| 398 | rtp_start_timestamp_(0) {} |
| 399 |
| 400 | private: |
| 401 | std::unique_ptr<test::PacketTransport> CreateSendTransport( |
| 402 | TaskQueueBase* task_queue, |
| 403 | Call* sender_call) override { |
| 404 | return std::make_unique<test::PacketTransport>( |
| 405 | task_queue, sender_call, this, test::PacketTransport::kSender, |
| 406 | payload_type_map_, |
| 407 | std::make_unique<FakeNetworkPipe>( |
| 408 | Clock::GetRealTimeClock(), |
| 409 | std::make_unique<SimulatedNetwork>(net_config_))); |
| 410 | } |
| 411 |
| 412 | std::unique_ptr<test::PacketTransport> CreateReceiveTransport( |
| 413 | TaskQueueBase* task_queue) override { |
| 414 | return std::make_unique<test::PacketTransport>( |
| 415 | task_queue, nullptr, this, test::PacketTransport::kReceiver, |
| 416 | payload_type_map_, |
| 417 | std::make_unique<FakeNetworkPipe>( |
| 418 | Clock::GetRealTimeClock(), |
| 419 | std::make_unique<SimulatedNetwork>(net_config_))); |
| 420 | } |
| 421 |
| 422 | void OnFrame(const VideoFrame& video_frame) override { |
| 423 | MutexLock lock(&mutex_); |
| 424 | if (video_frame.ntp_time_ms() <= 0) { |
| 425 | // Haven't got enough RTCP SR in order to calculate the capture ntp |
| 426 | // time. |
| 427 | return; |
| 428 | } |
| 429 |
| 430 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 431 | int64_t time_since_creation = now_ms - creation_time_ms_; |
| 432 | if (time_since_creation < start_time_ms_) { |
| 433 | // Wait for `start_time_ms_` before start measuring. |
| 434 | return; |
| 435 | } |
| 436 |
| 437 | if (time_since_creation > run_time_ms_) { |
| 438 | observation_complete_.Set(); |
| 439 | } |
| 440 |
| 441 | FrameCaptureTimeList::iterator iter = |
| 442 | capture_time_list_.find(video_frame.timestamp()); |
| 443 | EXPECT_TRUE(iter != capture_time_list_.end()); |
| 444 |
| 445 | // The real capture time has been wrapped to uint32_t before converted |
| 446 | // to rtp timestamp in the sender side. So here we convert the estimated |
| 447 | // capture time to a uint32_t 90k timestamp also for comparing. |
| 448 | uint32_t estimated_capture_timestamp = |
| 449 | 90 * static_cast<uint32_t>(video_frame.ntp_time_ms()); |
| 450 | uint32_t real_capture_timestamp = iter->second; |
| 451 | int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp; |
| 452 | time_offset_ms = time_offset_ms / 90; |
| 453 | time_offset_ms_list_.push_back(time_offset_ms); |
| 454 |
| 455 | EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_); |
| 456 | } |
| 457 |
| 458 | Action OnSendRtp(const uint8_t* packet, size_t length) override { |
| 459 | MutexLock lock(&mutex_); |
| 460 | RtpPacket rtp_packet; |
| 461 | EXPECT_TRUE(rtp_packet.Parse(packet, length)); |
| 462 |
| 463 | if (!rtp_start_timestamp_set_) { |
| 464 | // Calculate the rtp timestamp offset in order to calculate the real |
| 465 | // capture time. |
| 466 | uint32_t first_capture_timestamp = |
| 467 | 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time()); |
| 468 | rtp_start_timestamp_ = rtp_packet.Timestamp() - first_capture_timestamp; |
| 469 | rtp_start_timestamp_set_ = true; |
| 470 | } |
| 471 |
| 472 | uint32_t capture_timestamp = |
| 473 | rtp_packet.Timestamp() - rtp_start_timestamp_; |
| 474 | capture_time_list_.insert( |
| 475 | capture_time_list_.end(), |
| 476 | std::make_pair(rtp_packet.Timestamp(), capture_timestamp)); |
| 477 | return SEND_PACKET; |
| 478 | } |
| 479 |
| 480 | void OnFrameGeneratorCapturerCreated( |
| 481 | test::FrameGeneratorCapturer* frame_generator_capturer) override { |
| 482 | capturer_ = frame_generator_capturer; |
| 483 | } |
| 484 |
| 485 | void ModifyVideoConfigs( |
| 486 | VideoSendStream::Config* send_config, |
| 487 | std::vector<VideoReceiveStream::Config>* receive_configs, |
| 488 | VideoEncoderConfig* encoder_config) override { |
| 489 | (*receive_configs)[0].renderer = this; |
| 490 | // Enable the receiver side rtt calculation. |
| 491 | (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true; |
| 492 | } |
| 493 |
| 494 | void PerformTest() override { |
| 495 | EXPECT_TRUE(Wait()) << "Timed out while waiting for estimated capture " |
| 496 | "NTP time to be within bounds."; |
| 497 | test::PrintResultList("capture_ntp_time", "", "real - estimated", |
| 498 | time_offset_ms_list_, "ms", true); |
| 499 | } |
| 500 |
| 501 | Mutex mutex_; |
| 502 | const BuiltInNetworkBehaviorConfig net_config_; |
| 503 | Clock* const clock_; |
| 504 | const int threshold_ms_; |
| 505 | const int start_time_ms_; |
| 506 | const int run_time_ms_; |
| 507 | const int64_t creation_time_ms_; |
| 508 | test::FrameGeneratorCapturer* capturer_; |
| 509 | bool rtp_start_timestamp_set_; |
| 510 | uint32_t rtp_start_timestamp_; |
| 511 | typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList; |
| 512 | FrameCaptureTimeList capture_time_list_ RTC_GUARDED_BY(&mutex_); |
| 513 | std::vector<double> time_offset_ms_list_; |
| 514 | } test(net_config, threshold_ms, start_time_ms, run_time_ms); |
| 515 |
| 516 | RunBaseTest(&test); |
| 517 | } |
| 518 |
| 519 | // Flaky tests, disabled on Mac and Windows due to webrtc:8291. |
| 520 | #if !(defined(WEBRTC_MAC) || defined(WEBRTC_WIN)) |
| 521 | TEST_F(CallPerfTest, Real_Estimated_CaptureNtpTimeWithNetworkDelay) { |
| 522 | BuiltInNetworkBehaviorConfig net_config; |
| 523 | net_config.queue_delay_ms = 100; |
| 524 | // TODO(wu): lower the threshold as the calculation/estimation becomes more |
| 525 | // accurate. |
| 526 | const int kThresholdMs = 100; |
| 527 | const int kStartTimeMs = 10000; |
| 528 | const int kRunTimeMs = 20000; |
| 529 | TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs); |
| 530 | } |
| 531 |
| 532 | TEST_F(CallPerfTest, Real_Estimated_CaptureNtpTimeWithNetworkJitter) { |
| 533 | BuiltInNetworkBehaviorConfig net_config; |
| 534 | net_config.queue_delay_ms = 100; |
| 535 | net_config.delay_standard_deviation_ms = 10; |
| 536 | // TODO(wu): lower the threshold as the calculation/estimation becomes more |
| 537 | // accurate. |
| 538 | const int kThresholdMs = 100; |
| 539 | const int kStartTimeMs = 10000; |
| 540 | const int kRunTimeMs = 20000; |
| 541 | TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs); |
| 542 | } |
| 543 | #endif |
| 544 |
| 545 | TEST_F(CallPerfTest, ReceivesCpuOveruseAndUnderuse) { |
| 546 | // Minimal normal usage at the start, then 30s overuse to allow filter to |
| 547 | // settle, and then 80s underuse to allow plenty of time for rampup again. |
| 548 | test::ScopedFieldTrials fake_overuse_settings( |
| 549 | "WebRTC-ForceSimulatedOveruseIntervalMs/1-30000-80000/"); |
| 550 |
| 551 | class LoadObserver : public test::SendTest, |
| 552 | public test::FrameGeneratorCapturer::SinkWantsObserver { |
| 553 | public: |
| 554 | LoadObserver() : SendTest(kLongTimeoutMs), test_phase_(TestPhase::kInit) {} |
| 555 |
| 556 | void OnFrameGeneratorCapturerCreated( |
| 557 | test::FrameGeneratorCapturer* frame_generator_capturer) override { |
| 558 | frame_generator_capturer->SetSinkWantsObserver(this); |
| 559 | // Set a high initial resolution to be sure that we can scale down. |
| 560 | frame_generator_capturer->ChangeResolution(1920, 1080); |
| 561 | } |
| 562 |
| 563 | // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink |
| 564 | // is called. |
| 565 | // TODO(sprang): Add integration test for maintain-framerate mode? |
| 566 | void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 567 | const rtc::VideoSinkWants& wants) override { |
| 568 | // The sink wants can change either because an adaptation happened (i.e. |
| 569 | // the pixels or frame rate changed) or for other reasons, such as encoded |
| 570 | // resolutions being communicated (happens whenever we capture a new frame |
| 571 | // size). In this test, we only care about adaptations. |
| 572 | bool did_adapt = |
| 573 | last_wants_.max_pixel_count != wants.max_pixel_count || |
| 574 | last_wants_.target_pixel_count != wants.target_pixel_count || |
| 575 | last_wants_.max_framerate_fps != wants.max_framerate_fps; |
| 576 | last_wants_ = wants; |
| 577 | if (!did_adapt) { |
| 578 | return; |
| 579 | } |
| 580 | // At kStart expect CPU overuse. Then expect CPU underuse when the encoder |
| 581 | // delay has been decreased. |
| 582 | switch (test_phase_) { |
| 583 | case TestPhase::kInit: |
| 584 | // Max framerate should be set initially. |
| 585 | if (wants.max_framerate_fps != std::numeric_limits<int>::max() && |
| 586 | wants.max_pixel_count == std::numeric_limits<int>::max()) { |
| 587 | test_phase_ = TestPhase::kStart; |
| 588 | } else { |
| 589 | ADD_FAILURE() << "Got unexpected adaptation request, max res = " |
| 590 | << wants.max_pixel_count << ", target res = " |
| 591 | << wants.target_pixel_count.value_or(-1) |
| 592 | << ", max fps = " << wants.max_framerate_fps; |
| 593 | } |
| 594 | break; |
| 595 | case TestPhase::kStart: |
| 596 | if (wants.max_pixel_count < std::numeric_limits<int>::max()) { |
| 597 | // On adapting down, VideoStreamEncoder::VideoSourceProxy will set |
| 598 | // only the max pixel count, leaving the target unset. |
| 599 | test_phase_ = TestPhase::kAdaptedDown; |
| 600 | } else { |
| 601 | ADD_FAILURE() << "Got unexpected adaptation request, max res = " |
| 602 | << wants.max_pixel_count << ", target res = " |
| 603 | << wants.target_pixel_count.value_or(-1) |
| 604 | << ", max fps = " << wants.max_framerate_fps; |
| 605 | } |
| 606 | break; |
| 607 | case TestPhase::kAdaptedDown: |
| 608 | // On adapting up, the adaptation counter will again be at zero, and |
| 609 | // so all constraints will be reset. |
| 610 | if (wants.max_pixel_count == std::numeric_limits<int>::max() && |
| 611 | !wants.target_pixel_count) { |
| 612 | test_phase_ = TestPhase::kAdaptedUp; |
| 613 | observation_complete_.Set(); |
| 614 | } else { |
| 615 | ADD_FAILURE() << "Got unexpected adaptation request, max res = " |
| 616 | << wants.max_pixel_count << ", target res = " |
| 617 | << wants.target_pixel_count.value_or(-1) |
| 618 | << ", max fps = " << wants.max_framerate_fps; |
| 619 | } |
| 620 | break; |
| 621 | case TestPhase::kAdaptedUp: |
| 622 | ADD_FAILURE() << "Got unexpected adaptation request, max res = " |
| 623 | << wants.max_pixel_count << ", target res = " |
| 624 | << wants.target_pixel_count.value_or(-1) |
| 625 | << ", max fps = " << wants.max_framerate_fps; |
| 626 | } |
| 627 | } |
| 628 |
| 629 | void ModifyVideoConfigs( |
| 630 | VideoSendStream::Config* send_config, |
| 631 | std::vector<VideoReceiveStream::Config>* receive_configs, |
| 632 | VideoEncoderConfig* encoder_config) override {} |
| 633 |
| 634 | void PerformTest() override { |
| 635 | EXPECT_TRUE(Wait()) << "Timed out before receiving an overuse callback."; |
| 636 | } |
| 637 |
| 638 | enum class TestPhase { |
| 639 | kInit, |
| 640 | kStart, |
| 641 | kAdaptedDown, |
| 642 | kAdaptedUp |
| 643 | } test_phase_; |
| 644 |
| 645 | private: |
| 646 | rtc::VideoSinkWants last_wants_; |
| 647 | } test; |
| 648 |
| 649 | RunBaseTest(&test); |
| 650 | } |
| 651 |
| 652 | void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) { |
| 653 | static const int kMaxEncodeBitrateKbps = 30; |
| 654 | static const int kMinTransmitBitrateBps = 150000; |
| 655 | static const int kMinAcceptableTransmitBitrate = 130; |
| 656 | static const int kMaxAcceptableTransmitBitrate = 170; |
| 657 | static const int kNumBitrateObservationsInRange = 100; |
| 658 | static const int kAcceptableBitrateErrorMargin = 15; // +- 7 |
| 659 | class BitrateObserver : public test::EndToEndTest { |
| 660 | public: |
| 661 | explicit BitrateObserver(bool using_min_transmit_bitrate, |
| 662 | TaskQueueBase* task_queue) |
| 663 | : EndToEndTest(kLongTimeoutMs), |
| 664 | send_stream_(nullptr), |
| 665 | converged_(false), |
| 666 | pad_to_min_bitrate_(using_min_transmit_bitrate), |
| 667 | min_acceptable_bitrate_(using_min_transmit_bitrate |
| 668 | ? kMinAcceptableTransmitBitrate |
| 669 | : (kMaxEncodeBitrateKbps - |
| 670 | kAcceptableBitrateErrorMargin / 2)), |
| 671 | max_acceptable_bitrate_(using_min_transmit_bitrate |
| 672 | ? kMaxAcceptableTransmitBitrate |
| 673 | : (kMaxEncodeBitrateKbps + |
| 674 | kAcceptableBitrateErrorMargin / 2)), |
| 675 | num_bitrate_observations_in_range_(0), |
| 676 | task_queue_(task_queue), |
| 677 | task_safety_flag_(PendingTaskSafetyFlag::CreateDetached()) {} |
| 678 |
| 679 | private: |
| 680 | // TODO(holmer): Run this with a timer instead of once per packet. |
| 681 | Action OnSendRtp(const uint8_t* packet, size_t length) override { |
| 682 | task_queue_->PostTask(ToQueuedTask(task_safety_flag_, [this]() { |
| 683 | VideoSendStream::Stats stats = send_stream_->GetStats(); |
| 684 |
| 685 | if (!stats.substreams.empty()) { |
| 686 | RTC_DCHECK_EQ(1, stats.substreams.size()); |
| 687 | int bitrate_kbps = |
| 688 | stats.substreams.begin()->second.total_bitrate_bps / 1000; |
| 689 | if (bitrate_kbps > min_acceptable_bitrate_ && |
| 690 | bitrate_kbps < max_acceptable_bitrate_) { |
| 691 | converged_ = true; |
| 692 | ++num_bitrate_observations_in_range_; |
| 693 | if (num_bitrate_observations_in_range_ == |
| 694 | kNumBitrateObservationsInRange) |
| 695 | observation_complete_.Set(); |
| 696 | } |
| 697 | if (converged_) |
| 698 | bitrate_kbps_list_.push_back(bitrate_kbps); |
| 699 | } |
| 700 | })); |
| 701 | return SEND_PACKET; |
| 702 | } |
| 703 |
| 704 | void OnVideoStreamsCreated( |
| 705 | VideoSendStream* send_stream, |
| 706 | const std::vector<VideoReceiveStream*>& receive_streams) override { |
| 707 | send_stream_ = send_stream; |
| 708 | } |
| 709 |
| 710 | void OnStreamsStopped() override { task_safety_flag_->SetNotAlive(); } |
| 711 |
| 712 | void ModifyVideoConfigs( |
| 713 | VideoSendStream::Config* send_config, |
| 714 | std::vector<VideoReceiveStream::Config>* receive_configs, |
| 715 | VideoEncoderConfig* encoder_config) override { |
| 716 | if (pad_to_min_bitrate_) { |
| 717 | encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps; |
| 718 | } else { |
| 719 | RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps); |
| 720 | } |
| 721 | } |
| 722 |
| 723 | void PerformTest() override { |
| 724 | EXPECT_TRUE(Wait()) << "Timeout while waiting for send-bitrate stats."; |
| 725 | test::PrintResultList( |
| 726 | "bitrate_stats_", |
| 727 | (pad_to_min_bitrate_ ? "min_transmit_bitrate" |
| 728 | : "without_min_transmit_bitrate"), |
| 729 | "bitrate_kbps", bitrate_kbps_list_, "kbps", false); |
| 730 | } |
| 731 |
| 732 | VideoSendStream* send_stream_; |
| 733 | bool converged_; |
| 734 | const bool pad_to_min_bitrate_; |
| 735 | const int min_acceptable_bitrate_; |
| 736 | const int max_acceptable_bitrate_; |
| 737 | int num_bitrate_observations_in_range_; |
| 738 | std::vector<double> bitrate_kbps_list_; |
| 739 | TaskQueueBase* task_queue_; |
| 740 | rtc::scoped_refptr<PendingTaskSafetyFlag> task_safety_flag_; |
| 741 | } test(pad_to_min_bitrate, task_queue()); |
| 742 |
| 743 | fake_encoder_max_bitrate_ = kMaxEncodeBitrateKbps; |
| 744 | RunBaseTest(&test); |
| 745 | } |
| 746 |
| 747 | TEST_F(CallPerfTest, Bitrate_Kbps_PadsToMinTransmitBitrate) { |
| 748 | TestMinTransmitBitrate(true); |
| 749 | } |
| 750 |
| 751 | TEST_F(CallPerfTest, Bitrate_Kbps_NoPadWithoutMinTransmitBitrate) { |
| 752 | TestMinTransmitBitrate(false); |
| 753 | } |
| 754 |
| 755 | // TODO(bugs.webrtc.org/8878) |
| 756 | #if defined(WEBRTC_MAC) |
| 757 | #define MAYBE_KeepsHighBitrateWhenReconfiguringSender \ |
| 758 | DISABLED_KeepsHighBitrateWhenReconfiguringSender |
| 759 | #else |
| 760 | #define MAYBE_KeepsHighBitrateWhenReconfiguringSender \ |
| 761 | KeepsHighBitrateWhenReconfiguringSender |
| 762 | #endif |
| 763 | TEST_F(CallPerfTest, MAYBE_KeepsHighBitrateWhenReconfiguringSender) { |
| 764 | static const uint32_t kInitialBitrateKbps = 400; |
| 765 | static const uint32_t kReconfigureThresholdKbps = 600; |
| 766 |
| 767 | // We get lower bitrate than expected by this test if the following field |
| 768 | // trial is enabled. |
| 769 | test::ScopedFieldTrials field_trials( |
| 770 | "WebRTC-SendSideBwe-WithOverhead/Disabled/"); |
| 771 |
| 772 | class VideoStreamFactory |
| 773 | : public VideoEncoderConfig::VideoStreamFactoryInterface { |
| 774 | public: |
| 775 | VideoStreamFactory() {} |
| 776 |
| 777 | private: |
| 778 | std::vector<VideoStream> CreateEncoderStreams( |
| 779 | int width, |
| 780 | int height, |
| 781 | const VideoEncoderConfig& encoder_config) override { |
| 782 | std::vector<VideoStream> streams = |
| 783 | test::CreateVideoStreams(width, height, encoder_config); |
| 784 | streams[0].min_bitrate_bps = 50000; |
| 785 | streams[0].target_bitrate_bps = streams[0].max_bitrate_bps = 2000000; |
| 786 | return streams; |
| 787 | } |
| 788 | }; |
| 789 |
| 790 | class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder { |
| 791 | public: |
| 792 | explicit BitrateObserver(TaskQueueBase* task_queue) |
| 793 | : EndToEndTest(kDefaultTimeoutMs), |
| 794 | FakeEncoder(Clock::GetRealTimeClock()), |
| 795 | encoder_inits_(0), |
| 796 | last_set_bitrate_kbps_(0), |
| 797 | send_stream_(nullptr), |
| 798 | frame_generator_(nullptr), |
| 799 | encoder_factory_(this), |
| 800 | bitrate_allocator_factory_( |
| 801 | CreateBuiltinVideoBitrateAllocatorFactory()), |
| 802 | task_queue_(task_queue) {} |
| 803 |
| 804 | int32_t InitEncode(const VideoCodec* config, |
| 805 | const VideoEncoder::Settings& settings) override { |
| 806 | ++encoder_inits_; |
| 807 | if (encoder_inits_ == 1) { |
| 808 | // First time initialization. Frame size is known. |
| 809 | // `expected_bitrate` is affected by bandwidth estimation before the |
| 810 | // first frame arrives to the encoder. |
| 811 | uint32_t expected_bitrate = last_set_bitrate_kbps_ > 0 |
| 812 | ? last_set_bitrate_kbps_ |
| 813 | : kInitialBitrateKbps; |
| 814 | EXPECT_EQ(expected_bitrate, config->startBitrate) |
| 815 | << "Encoder not initialized at expected bitrate."; |
| 816 | EXPECT_EQ(kDefaultWidth, config->width); |
| 817 | EXPECT_EQ(kDefaultHeight, config->height); |
| 818 | } else if (encoder_inits_ == 2) { |
| 819 | EXPECT_EQ(2 * kDefaultWidth, config->width); |
| 820 | EXPECT_EQ(2 * kDefaultHeight, config->height); |
| 821 | EXPECT_GE(last_set_bitrate_kbps_, kReconfigureThresholdKbps); |
| 822 | EXPECT_GT(config->startBitrate, kReconfigureThresholdKbps) |
| 823 | << "Encoder reconfigured with bitrate too far away from last set."; |
| 824 | observation_complete_.Set(); |
| 825 | } |
| 826 | return FakeEncoder::InitEncode(config, settings); |
| 827 | } |
| 828 |
| 829 | void SetRates(const RateControlParameters& parameters) override { |
| 830 | last_set_bitrate_kbps_ = parameters.bitrate.get_sum_kbps(); |
| 831 | if (encoder_inits_ == 1 && |
| 832 | parameters.bitrate.get_sum_kbps() > kReconfigureThresholdKbps) { |
| 833 | time_to_reconfigure_.Set(); |
| 834 | } |
| 835 | FakeEncoder::SetRates(parameters); |
| 836 | } |
| 837 |
| 838 | void ModifySenderBitrateConfig( |
| 839 | BitrateConstraints* bitrate_config) override { |
| 840 | bitrate_config->start_bitrate_bps = kInitialBitrateKbps * 1000; |
| 841 | } |
| 842 |
| 843 | void ModifyVideoConfigs( |
| 844 | VideoSendStream::Config* send_config, |
| 845 | std::vector<VideoReceiveStream::Config>* receive_configs, |
| 846 | VideoEncoderConfig* encoder_config) override { |
| 847 | send_config->encoder_settings.encoder_factory = &encoder_factory_; |
| 848 | send_config->encoder_settings.bitrate_allocator_factory = |
| 849 | bitrate_allocator_factory_.get(); |
| 850 | encoder_config->max_bitrate_bps = 2 * kReconfigureThresholdKbps * 1000; |
| 851 | encoder_config->video_stream_factory = |
| 852 | rtc::make_ref_counted<VideoStreamFactory>(); |
| 853 |
| 854 | encoder_config_ = encoder_config->Copy(); |
| 855 | } |
| 856 |
| 857 | void OnVideoStreamsCreated( |
| 858 | VideoSendStream* send_stream, |
| 859 | const std::vector<VideoReceiveStream*>& receive_streams) override { |
| 860 | send_stream_ = send_stream; |
| 861 | } |
| 862 |
| 863 | void OnFrameGeneratorCapturerCreated( |
| 864 | test::FrameGeneratorCapturer* frame_generator_capturer) override { |
| 865 | frame_generator_ = frame_generator_capturer; |
| 866 | } |
| 867 |
| 868 | void PerformTest() override { |
| 869 | ASSERT_TRUE(time_to_reconfigure_.Wait(kDefaultTimeoutMs)) |
| 870 | << "Timed out before receiving an initial high bitrate."; |
| 871 | frame_generator_->ChangeResolution(kDefaultWidth * 2, kDefaultHeight * 2); |
| 872 | SendTask(RTC_FROM_HERE, task_queue_, [&]() { |
| 873 | send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy()); |
| 874 | }); |
| 875 | EXPECT_TRUE(Wait()) |
| 876 | << "Timed out while waiting for a couple of high bitrate estimates " |
| 877 | "after reconfiguring the send stream."; |
| 878 | } |
| 879 |
| 880 | private: |
| 881 | rtc::Event time_to_reconfigure_; |
| 882 | int encoder_inits_; |
| 883 | uint32_t last_set_bitrate_kbps_; |
| 884 | VideoSendStream* send_stream_; |
| 885 | test::FrameGeneratorCapturer* frame_generator_; |
| 886 | test::VideoEncoderProxyFactory encoder_factory_; |
| 887 | std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_; |
| 888 | VideoEncoderConfig encoder_config_; |
| 889 | TaskQueueBase* task_queue_; |
| 890 | } test(task_queue()); |
| 891 |
| 892 | RunBaseTest(&test); |
| 893 | } |
| 894 |
| 895 | // Discovers the minimal supported audio+video bitrate. The test bitrate is |
| 896 | // considered supported if Rtt does not go above 400ms with the network |
| 897 | // contrained to the test bitrate. |
| 898 | // |
| 899 | // |test_bitrate_from test_bitrate_to| bitrate constraint range |
| 900 | // `test_bitrate_step` bitrate constraint update step during the test |
| 901 | // |min_bwe max_bwe| BWE range |
| 902 | // `start_bwe` initial BWE |
| 903 | void CallPerfTest::TestMinAudioVideoBitrate(int test_bitrate_from, |
| 904 | int test_bitrate_to, |
| 905 | int test_bitrate_step, |
| 906 | int min_bwe, |
| 907 | int start_bwe, |
| 908 | int max_bwe) { |
| 909 | static const std::string kAudioTrackId = "audio_track_0"; |
| 910 | static constexpr int kOpusBitrateFbBps = 32000; |
| 911 | static constexpr int kBitrateStabilizationMs = 10000; |
| 912 | static constexpr int kBitrateMeasurements = 10; |
| 913 | static constexpr int kBitrateMeasurementMs = 1000; |
| 914 | static constexpr int kShortDelayMs = 10; |
| 915 | static constexpr int kMinGoodRttMs = 400; |
| 916 |
| 917 | class MinVideoAndAudioBitrateTester : public test::EndToEndTest { |
| 918 | public: |
| 919 | MinVideoAndAudioBitrateTester(int test_bitrate_from, |
| 920 | int test_bitrate_to, |
| 921 | int test_bitrate_step, |
| 922 | int min_bwe, |
| 923 | int start_bwe, |
| 924 | int max_bwe, |
| 925 | TaskQueueBase* task_queue) |
| 926 | : EndToEndTest(), |
| 927 | test_bitrate_from_(test_bitrate_from), |
| 928 | test_bitrate_to_(test_bitrate_to), |
| 929 | test_bitrate_step_(test_bitrate_step), |
| 930 | min_bwe_(min_bwe), |
| 931 | start_bwe_(start_bwe), |
| 932 | max_bwe_(max_bwe), |
| 933 | task_queue_(task_queue) {} |
| 934 |
| 935 | protected: |
| 936 | BuiltInNetworkBehaviorConfig GetFakeNetworkPipeConfig() { |
| 937 | BuiltInNetworkBehaviorConfig pipe_config; |
| 938 | pipe_config.link_capacity_kbps = test_bitrate_from_; |
| 939 | return pipe_config; |
| 940 | } |
| 941 |
| 942 | std::unique_ptr<test::PacketTransport> CreateSendTransport( |
| 943 | TaskQueueBase* task_queue, |
| 944 | Call* sender_call) override { |
| 945 | auto network = |
| 946 | std::make_unique<SimulatedNetwork>(GetFakeNetworkPipeConfig()); |
| 947 | send_simulated_network_ = network.get(); |
| 948 | return std::make_unique<test::PacketTransport>( |
| 949 | task_queue, sender_call, this, test::PacketTransport::kSender, |
| 950 | test::CallTest::payload_type_map_, |
| 951 | std::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(), |
| 952 | std::move(network))); |
| 953 | } |
| 954 |
| 955 | std::unique_ptr<test::PacketTransport> CreateReceiveTransport( |
| 956 | TaskQueueBase* task_queue) override { |
| 957 | auto network = |
| 958 | std::make_unique<SimulatedNetwork>(GetFakeNetworkPipeConfig()); |
| 959 | receive_simulated_network_ = network.get(); |
| 960 | return std::make_unique<test::PacketTransport>( |
| 961 | task_queue, nullptr, this, test::PacketTransport::kReceiver, |
| 962 | test::CallTest::payload_type_map_, |
| 963 | std::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(), |
| 964 | std::move(network))); |
| 965 | } |
| 966 |
| 967 | void PerformTest() override { |
| 968 | // Quick test mode, just to exercise all the code paths without actually |
| 969 | // caring about performance measurements. |
| 970 | const bool quick_perf_test = |
| 971 | field_trial::IsEnabled("WebRTC-QuickPerfTest"); |
| 972 | int last_passed_test_bitrate = -1; |
| 973 | for (int test_bitrate = test_bitrate_from_; |
| 974 | test_bitrate_from_ < test_bitrate_to_ |
| 975 | ? test_bitrate <= test_bitrate_to_ |
| 976 | : test_bitrate >= test_bitrate_to_; |
| 977 | test_bitrate += test_bitrate_step_) { |
| 978 | BuiltInNetworkBehaviorConfig pipe_config; |
| 979 | pipe_config.link_capacity_kbps = test_bitrate; |
| 980 | send_simulated_network_->SetConfig(pipe_config); |
| 981 | receive_simulated_network_->SetConfig(pipe_config); |
| 982 |
| 983 | rtc::Thread::SleepMs(quick_perf_test ? kShortDelayMs |
| 984 | : kBitrateStabilizationMs); |
| 985 |
| 986 | int64_t avg_rtt = 0; |
| 987 | for (int i = 0; i < kBitrateMeasurements; i++) { |
| 988 | Call::Stats call_stats; |
| 989 | SendTask(RTC_FROM_HERE, task_queue_, [this, &call_stats]() { |
| 990 | call_stats = sender_call_->GetStats(); |
| 991 | }); |
| 992 | avg_rtt += call_stats.rtt_ms; |
| 993 | rtc::Thread::SleepMs(quick_perf_test ? kShortDelayMs |
| 994 | : kBitrateMeasurementMs); |
| 995 | } |
| 996 | avg_rtt = avg_rtt / kBitrateMeasurements; |
| 997 | if (avg_rtt > kMinGoodRttMs) { |
| 998 | break; |
| 999 | } else { |
| 1000 | last_passed_test_bitrate = test_bitrate; |
| 1001 | } |
| 1002 | } |
| 1003 | EXPECT_GT(last_passed_test_bitrate, -1) |
| 1004 | << "Minimum supported bitrate out of the test scope"; |
| 1005 | webrtc::test::PrintResult("min_test_bitrate_", "", "min_bitrate", |
| 1006 | last_passed_test_bitrate, "kbps", false); |
| 1007 | } |
| 1008 |
| 1009 | void OnCallsCreated(Call* sender_call, Call* receiver_call) override { |
| 1010 | sender_call_ = sender_call; |
| 1011 | BitrateConstraints bitrate_config; |
| 1012 | bitrate_config.min_bitrate_bps = min_bwe_; |
| 1013 | bitrate_config.start_bitrate_bps = start_bwe_; |
| 1014 | bitrate_config.max_bitrate_bps = max_bwe_; |
| 1015 | sender_call->GetTransportControllerSend()->SetSdpBitrateParameters( |
| 1016 | bitrate_config); |
| 1017 | } |
| 1018 |
| 1019 | size_t GetNumVideoStreams() const override { return 1; } |
| 1020 |
| 1021 | size_t GetNumAudioStreams() const override { return 1; } |
| 1022 |
| 1023 | void ModifyAudioConfigs( |
| 1024 | AudioSendStream::Config* send_config, |
| 1025 | std::vector<AudioReceiveStream::Config>* receive_configs) override { |
| 1026 | send_config->send_codec_spec->target_bitrate_bps = |
| 1027 | absl::optional<int>(kOpusBitrateFbBps); |
| 1028 | } |
| 1029 |
| 1030 | private: |
| 1031 | const int test_bitrate_from_; |
| 1032 | const int test_bitrate_to_; |
| 1033 | const int test_bitrate_step_; |
| 1034 | const int min_bwe_; |
| 1035 | const int start_bwe_; |
| 1036 | const int max_bwe_; |
| 1037 | SimulatedNetwork* send_simulated_network_; |
| 1038 | SimulatedNetwork* receive_simulated_network_; |
| 1039 | Call* sender_call_; |
| 1040 | TaskQueueBase* const task_queue_; |
| 1041 | } test(test_bitrate_from, test_bitrate_to, test_bitrate_step, min_bwe, |
| 1042 | start_bwe, max_bwe, task_queue()); |
| 1043 |
| 1044 | RunBaseTest(&test); |
| 1045 | } |
| 1046 |
| 1047 | // TODO(bugs.webrtc.org/8878) |
| 1048 | #if defined(WEBRTC_MAC) |
| 1049 | #define MAYBE_Min_Bitrate_VideoAndAudio DISABLED_Min_Bitrate_VideoAndAudio |
| 1050 | #else |
| 1051 | #define MAYBE_Min_Bitrate_VideoAndAudio Min_Bitrate_VideoAndAudio |
| 1052 | #endif |
| 1053 | TEST_F(CallPerfTest, MAYBE_Min_Bitrate_VideoAndAudio) { |
| 1054 | TestMinAudioVideoBitrate(110, 40, -10, 10000, 70000, 200000); |
| 1055 | } |
| 1056 |
| 1057 | void CallPerfTest::TestEncodeFramerate(VideoEncoderFactory* encoder_factory, |
| 1058 | const std::string& payload_name, |
| 1059 | const std::vector<int>& max_framerates) { |
| 1060 | static constexpr double kAllowedFpsDiff = 1.5; |
| 1061 | static constexpr TimeDelta kMinGetStatsInterval = TimeDelta::Millis(400); |
| 1062 | static constexpr TimeDelta kMinRunTime = TimeDelta::Seconds(15); |
| 1063 | static constexpr DataRate kMaxBitrate = DataRate::KilobitsPerSec(1000); |
| 1064 |
| 1065 | class FramerateObserver |
| 1066 | : public test::EndToEndTest, |
| 1067 | public test::FrameGeneratorCapturer::SinkWantsObserver { |
| 1068 | public: |
| 1069 | FramerateObserver(VideoEncoderFactory* encoder_factory, |
| 1070 | const std::string& payload_name, |
| 1071 | const std::vector<int>& max_framerates, |
| 1072 | TaskQueueBase* task_queue) |
| 1073 | : EndToEndTest(kDefaultTimeoutMs), |
| 1074 | clock_(Clock::GetRealTimeClock()), |
| 1075 | encoder_factory_(encoder_factory), |
| 1076 | payload_name_(payload_name), |
| 1077 | max_framerates_(max_framerates), |
| 1078 | task_queue_(task_queue), |
| 1079 | start_time_(clock_->CurrentTime()), |
| 1080 | last_getstats_time_(start_time_), |
| 1081 | send_stream_(nullptr) {} |
| 1082 |
| 1083 | void OnFrameGeneratorCapturerCreated( |
| 1084 | test::FrameGeneratorCapturer* frame_generator_capturer) override { |
| 1085 | frame_generator_capturer->ChangeResolution(640, 360); |
| 1086 | } |
| 1087 |
| 1088 | void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 1089 | const rtc::VideoSinkWants& wants) override {} |
| 1090 |
| 1091 | void ModifySenderBitrateConfig( |
| 1092 | BitrateConstraints* bitrate_config) override { |
| 1093 | bitrate_config->start_bitrate_bps = kMaxBitrate.bps() / 2; |
| 1094 | } |
| 1095 |
| 1096 | void OnVideoStreamsCreated( |
| 1097 | VideoSendStream* send_stream, |
| 1098 | const std::vector<VideoReceiveStream*>& receive_streams) override { |
| 1099 | send_stream_ = send_stream; |
| 1100 | } |
| 1101 |
| 1102 | size_t GetNumVideoStreams() const override { |
| 1103 | return max_framerates_.size(); |
| 1104 | } |
| 1105 |
| 1106 | void ModifyVideoConfigs( |
| 1107 | VideoSendStream::Config* send_config, |
| 1108 | std::vector<VideoReceiveStream::Config>* receive_configs, |
| 1109 | VideoEncoderConfig* encoder_config) override { |
| 1110 | send_config->encoder_settings.encoder_factory = encoder_factory_; |
| 1111 | send_config->rtp.payload_name = payload_name_; |
| 1112 | send_config->rtp.payload_type = test::CallTest::kVideoSendPayloadType; |
| 1113 | encoder_config->video_format.name = payload_name_; |
| 1114 | encoder_config->codec_type = PayloadStringToCodecType(payload_name_); |
| 1115 | encoder_config->max_bitrate_bps = kMaxBitrate.bps(); |
| 1116 | for (size_t i = 0; i < max_framerates_.size(); ++i) { |
| 1117 | encoder_config->simulcast_layers[i].max_framerate = max_framerates_[i]; |
| 1118 | configured_framerates_[send_config->rtp.ssrcs[i]] = max_framerates_[i]; |
| 1119 | } |
| 1120 | } |
| 1121 |
| 1122 | void PerformTest() override { |
| 1123 | EXPECT_TRUE(Wait()) << "Timeout while waiting for framerate stats."; |
| 1124 | } |
| 1125 |
| 1126 | void VerifyStats() const { |
| 1127 | double input_fps = 0.0; |
| 1128 | for (const auto& configured_framerate : configured_framerates_) { |
| 1129 | input_fps = std::max(configured_framerate.second, input_fps); |
| 1130 | } |
| 1131 | for (const auto& encode_frame_rate_list : encode_frame_rate_lists_) { |
| 1132 | const std::vector<double>& values = encode_frame_rate_list.second; |
| 1133 | test::PrintResultList("substream", "", "encode_frame_rate", values, |
| 1134 | "fps", false); |
| 1135 | double average_fps = |
| 1136 | std::accumulate(values.begin(), values.end(), 0.0) / values.size(); |
| 1137 | uint32_t ssrc = encode_frame_rate_list.first; |
| 1138 | double expected_fps = configured_framerates_.find(ssrc)->second; |
| 1139 | if (expected_fps != input_fps) |
| 1140 | EXPECT_NEAR(expected_fps, average_fps, kAllowedFpsDiff); |
| 1141 | } |
| 1142 | } |
| 1143 |
| 1144 | Action OnSendRtp(const uint8_t* packet, size_t length) override { |
| 1145 | const Timestamp now = clock_->CurrentTime(); |
| 1146 | if (now - last_getstats_time_ > kMinGetStatsInterval) { |
| 1147 | last_getstats_time_ = now; |
| 1148 | task_queue_->PostTask(ToQueuedTask([this, now]() { |
| 1149 | VideoSendStream::Stats stats = send_stream_->GetStats(); |
| 1150 | for (const auto& stat : stats.substreams) { |
| 1151 | encode_frame_rate_lists_[stat.first].push_back( |
| 1152 | stat.second.encode_frame_rate); |
| 1153 | } |
| 1154 | if (now - start_time_ > kMinRunTime) { |
| 1155 | VerifyStats(); |
| 1156 | observation_complete_.Set(); |
| 1157 | } |
| 1158 | })); |
| 1159 | } |
| 1160 | return SEND_PACKET; |
| 1161 | } |
| 1162 |
| 1163 | Clock* const clock_; |
| 1164 | VideoEncoderFactory* const encoder_factory_; |
| 1165 | const std::string payload_name_; |
| 1166 | const std::vector<int> max_framerates_; |
| 1167 | TaskQueueBase* const task_queue_; |
| 1168 | const Timestamp start_time_; |
| 1169 | Timestamp last_getstats_time_; |
| 1170 | VideoSendStream* send_stream_; |
| 1171 | std::map<uint32_t, std::vector<double>> encode_frame_rate_lists_; |
| 1172 | std::map<uint32_t, double> configured_framerates_; |
| 1173 | } test(encoder_factory, payload_name, max_framerates, task_queue()); |
| 1174 |
| 1175 | RunBaseTest(&test); |
| 1176 | } |
| 1177 |
| 1178 | TEST_F(CallPerfTest, TestEncodeFramerateVp8Simulcast) { |
| 1179 | InternalEncoderFactory internal_encoder_factory; |
| 1180 | test::FunctionVideoEncoderFactory encoder_factory( |
| 1181 | [&internal_encoder_factory]() { |
| 1182 | return std::make_unique<SimulcastEncoderAdapter>( |
| 1183 | &internal_encoder_factory, SdpVideoFormat("VP8")); |
| 1184 | }); |
| 1185 |
| 1186 | TestEncodeFramerate(&encoder_factory, "VP8", |
| 1187 | /*max_framerates=*/{20, 30}); |
| 1188 | } |
| 1189 |
| 1190 | TEST_F(CallPerfTest, TestEncodeFramerateVp8SimulcastLowerInputFps) { |
| 1191 | InternalEncoderFactory internal_encoder_factory; |
| 1192 | test::FunctionVideoEncoderFactory encoder_factory( |
| 1193 | [&internal_encoder_factory]() { |
| 1194 | return std::make_unique<SimulcastEncoderAdapter>( |
| 1195 | &internal_encoder_factory, SdpVideoFormat("VP8")); |
| 1196 | }); |
| 1197 |
| 1198 | TestEncodeFramerate(&encoder_factory, "VP8", |
| 1199 | /*max_framerates=*/{14, 20}); |
| 1200 | } |
| 1201 |
| 1202 | } // namespace webrtc |
| 1203 |