non_sender_rtt_test.cc (3325B)
1 /* 2 * Copyright (c) 2021 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 11 #include <cstdint> 12 #include <vector> 13 14 #include "api/task_queue/task_queue_base.h" 15 #include "api/test/rtc_error_matchers.h" 16 #include "api/test/simulated_network.h" 17 #include "api/units/time_delta.h" 18 #include "audio/test/audio_end_to_end_test.h" 19 #include "call/audio_receive_stream.h" 20 #include "call/audio_send_stream.h" 21 #include "rtc_base/task_queue_for_test.h" 22 #include "test/call_test.h" 23 #include "test/gmock.h" 24 #include "test/gtest.h" 25 #include "test/wait_until.h" 26 27 namespace webrtc { 28 namespace test { 29 30 using ::testing::IsTrue; 31 using NonSenderRttTest = CallTest; 32 33 TEST_F(NonSenderRttTest, NonSenderRttStats) { 34 class NonSenderRttTest : public AudioEndToEndTest { 35 public: 36 const int kLongTimeoutMs = 20000; 37 const int64_t kRttMs = 30; 38 39 explicit NonSenderRttTest(TaskQueueBase* task_queue) 40 : task_queue_(task_queue) {} 41 42 BuiltInNetworkBehaviorConfig GetSendTransportConfig() const override { 43 BuiltInNetworkBehaviorConfig pipe_config; 44 pipe_config.queue_delay_ms = kRttMs / 2; 45 return pipe_config; 46 } 47 48 void ModifyAudioConfigs(AudioSendStream::Config* send_config, 49 std::vector<AudioReceiveStreamInterface::Config>* 50 receive_configs) override { 51 ASSERT_EQ(receive_configs->size(), 1U); 52 (*receive_configs)[0].enable_non_sender_rtt = true; 53 AudioEndToEndTest::ModifyAudioConfigs(send_config, receive_configs); 54 send_config->send_codec_spec->enable_non_sender_rtt = true; 55 } 56 57 void PerformTest() override { 58 // Wait until we have an RTT measurement, but no longer than 59 // `kLongTimeoutMs`. This usually takes around 5 seconds, but in rare 60 // cases it can take more than 10 seconds. 61 EXPECT_THAT( 62 WaitUntil([&] { return HasRoundTripTimeMeasurement(); }, IsTrue(), 63 {.timeout = TimeDelta::Millis(kLongTimeoutMs)}), 64 IsRtcOk()); 65 } 66 67 void OnStreamsStopped() override { 68 AudioReceiveStreamInterface::Stats recv_stats = 69 receive_stream()->GetStats(/*get_and_clear_legacy_stats=*/true); 70 EXPECT_GT(recv_stats.round_trip_time_measurements, 0); 71 ASSERT_TRUE(recv_stats.round_trip_time.has_value()); 72 EXPECT_GT(recv_stats.round_trip_time->ms(), 0); 73 EXPECT_GE(recv_stats.total_round_trip_time.ms(), 74 recv_stats.round_trip_time->ms()); 75 } 76 77 protected: 78 bool HasRoundTripTimeMeasurement() { 79 bool has_rtt = false; 80 // GetStats() can only be called on `task_queue_`, block while we check. 81 SendTask(task_queue_, [this, &has_rtt]() { 82 if (receive_stream() && 83 receive_stream()->GetStats(true).round_trip_time_measurements > 0) { 84 has_rtt = true; 85 } 86 }); 87 return has_rtt; 88 } 89 90 private: 91 TaskQueueBase* task_queue_; 92 } test(task_queue()); 93 94 RunBaseTest(&test); 95 } 96 97 } // namespace test 98 } // namespace webrtc