diff --git a/audio/BUILD.gn b/audio/BUILD.gn index fc7e12ef38..d3f2d87c69 100644 --- a/audio/BUILD.gn +++ b/audio/BUILD.gn @@ -198,6 +198,7 @@ if (rtc_include_tests) { "../modules/rtp_rtcp:rtp_rtcp_format", "../modules/utility:utility", "../rtc_base:checks", + "../rtc_base:gunit_helpers", "../rtc_base:macromagic", "../rtc_base:refcount", "../rtc_base:rtc_base_tests_utils", diff --git a/audio/test/non_sender_rtt_test.cc b/audio/test/non_sender_rtt_test.cc index 0c7dc6cbee..278193e335 100644 --- a/audio/test/non_sender_rtt_test.cc +++ b/audio/test/non_sender_rtt_test.cc @@ -9,6 +9,8 @@ */ #include "audio/test/audio_end_to_end_test.h" +#include "rtc_base/gunit.h" +#include "rtc_base/task_queue_for_test.h" #include "system_wrappers/include/sleep.h" #include "test/gtest.h" @@ -20,9 +22,12 @@ using NonSenderRttTest = CallTest; TEST_F(NonSenderRttTest, NonSenderRttStats) { class NonSenderRttTest : public AudioEndToEndTest { public: - const int kTestDurationMs = 10000; + const int kLongTimeoutMs = 20000; const int64_t kRttMs = 30; + explicit NonSenderRttTest(TaskQueueBase* task_queue) + : task_queue_(task_queue) {} + BuiltInNetworkBehaviorConfig GetSendTransportConfig() const override { BuiltInNetworkBehaviorConfig pipe_config; pipe_config.queue_delay_ms = kRttMs / 2; @@ -38,7 +43,12 @@ TEST_F(NonSenderRttTest, NonSenderRttStats) { send_config->send_codec_spec->enable_non_sender_rtt = true; } - void PerformTest() override { SleepMs(kTestDurationMs); } + void PerformTest() override { + // Wait until we have an RTT measurement, but no longer than + // `kLongTimeoutMs`. This usually takes around 5 seconds, but in rare + // cases it can take more than 10 seconds. + EXPECT_TRUE_WAIT(HasRoundTripTimeMeasurement(), kLongTimeoutMs); + } void OnStreamsStopped() override { AudioReceiveStreamInterface::Stats recv_stats = @@ -49,7 +59,23 @@ TEST_F(NonSenderRttTest, NonSenderRttStats) { EXPECT_GE(recv_stats.total_round_trip_time.ms(), recv_stats.round_trip_time->ms()); } - } test; + + protected: + bool HasRoundTripTimeMeasurement() { + bool has_rtt = false; + // GetStats() can only be called on `task_queue_`, block while we check. + SendTask(task_queue_, [this, &has_rtt]() { + if (receive_stream() && + receive_stream()->GetStats(true).round_trip_time_measurements > 0) { + has_rtt = true; + } + }); + return has_rtt; + } + + private: + TaskQueueBase* task_queue_; + } test(task_queue()); RunBaseTest(&test); } diff --git a/call/audio_receive_stream.h b/call/audio_receive_stream.h index 1228861c42..1d33bf2e0b 100644 --- a/call/audio_receive_stream.h +++ b/call/audio_receive_stream.h @@ -99,7 +99,7 @@ class AudioReceiveStreamInterface : public MediaReceiveStreamInterface { uint64_t sender_reports_reports_count = 0; absl::optional round_trip_time; TimeDelta total_round_trip_time = TimeDelta::Zero(); - int round_trip_time_measurements; + int round_trip_time_measurements = 0; }; struct Config {