xref: /aosp_15_r20/external/webrtc/test/scenario/scenario_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #include "test/scenario/scenario.h"
11 
12 #include <atomic>
13 
14 #include "api/test/network_emulation/create_cross_traffic.h"
15 #include "api/test/network_emulation/cross_traffic.h"
16 #include "test/field_trial.h"
17 #include "test/gtest.h"
18 #include "test/logging/memory_log_writer.h"
19 #include "test/scenario/stats_collection.h"
20 
21 namespace webrtc {
22 namespace test {
TEST(ScenarioTest,StartsAndStopsWithoutErrors)23 TEST(ScenarioTest, StartsAndStopsWithoutErrors) {
24   std::atomic<bool> packet_received(false);
25   std::atomic<bool> bitrate_changed(false);
26   Scenario s;
27   CallClientConfig call_client_config;
28   call_client_config.transport.rates.start_rate = DataRate::KilobitsPerSec(300);
29   auto* alice = s.CreateClient("alice", call_client_config);
30   auto* bob = s.CreateClient("bob", call_client_config);
31   NetworkSimulationConfig network_config;
32   auto alice_net = s.CreateSimulationNode(network_config);
33   auto bob_net = s.CreateSimulationNode(network_config);
34   auto route = s.CreateRoutes(alice, {alice_net}, bob, {bob_net});
35 
36   VideoStreamConfig video_stream_config;
37   s.CreateVideoStream(route->forward(), video_stream_config);
38   s.CreateVideoStream(route->reverse(), video_stream_config);
39 
40   AudioStreamConfig audio_stream_config;
41   audio_stream_config.encoder.min_rate = DataRate::KilobitsPerSec(6);
42   audio_stream_config.encoder.max_rate = DataRate::KilobitsPerSec(64);
43   audio_stream_config.encoder.allocate_bitrate = true;
44   audio_stream_config.stream.in_bandwidth_estimation = false;
45   s.CreateAudioStream(route->forward(), audio_stream_config);
46   s.CreateAudioStream(route->reverse(), audio_stream_config);
47 
48   RandomWalkConfig cross_traffic_config;
49   s.net()->StartCrossTraffic(CreateRandomWalkCrossTraffic(
50       s.net()->CreateCrossTrafficRoute({alice_net}), cross_traffic_config));
51 
52   s.NetworkDelayedAction({alice_net, bob_net}, 100,
53                          [&packet_received] { packet_received = true; });
54   s.Every(TimeDelta::Millis(10), [alice, bob, &bitrate_changed] {
55     if (alice->GetStats().send_bandwidth_bps != 300000 &&
56         bob->GetStats().send_bandwidth_bps != 300000)
57       bitrate_changed = true;
58   });
59   s.RunUntil(TimeDelta::Seconds(2), TimeDelta::Millis(5),
60              [&bitrate_changed, &packet_received] {
61                return packet_received && bitrate_changed;
62              });
63   EXPECT_TRUE(packet_received);
64   EXPECT_TRUE(bitrate_changed);
65 }
66 namespace {
SetupVideoCall(Scenario & s,VideoQualityAnalyzer * analyzer)67 void SetupVideoCall(Scenario& s, VideoQualityAnalyzer* analyzer) {
68   CallClientConfig call_config;
69   auto* alice = s.CreateClient("alice", call_config);
70   auto* bob = s.CreateClient("bob", call_config);
71   NetworkSimulationConfig network_config;
72   network_config.bandwidth = DataRate::KilobitsPerSec(1000);
73   network_config.delay = TimeDelta::Millis(50);
74   auto alice_net = s.CreateSimulationNode(network_config);
75   auto bob_net = s.CreateSimulationNode(network_config);
76   auto route = s.CreateRoutes(alice, {alice_net}, bob, {bob_net});
77   VideoStreamConfig video;
78   if (analyzer) {
79     video.source.capture = VideoStreamConfig::Source::Capture::kVideoFile;
80     video.source.video_file.name = "foreman_cif";
81     video.source.video_file.width = 352;
82     video.source.video_file.height = 288;
83     video.source.framerate = 30;
84     video.encoder.codec = VideoStreamConfig::Encoder::Codec::kVideoCodecVP8;
85     video.encoder.implementation =
86         VideoStreamConfig::Encoder::Implementation::kSoftware;
87     video.hooks.frame_pair_handlers = {analyzer->Handler()};
88   }
89   s.CreateVideoStream(route->forward(), video);
90   s.CreateAudioStream(route->forward(), AudioStreamConfig());
91 }
92 }  // namespace
93 
TEST(ScenarioTest,SimTimeEncoding)94 TEST(ScenarioTest, SimTimeEncoding) {
95   VideoQualityAnalyzerConfig analyzer_config;
96   analyzer_config.psnr_coverage = 0.1;
97   VideoQualityAnalyzer analyzer(analyzer_config);
98   {
99     Scenario s("scenario/encode_sim", false);
100     SetupVideoCall(s, &analyzer);
101     s.RunFor(TimeDelta::Seconds(2));
102   }
103   // Regression tests based on previous runs.
104   EXPECT_EQ(analyzer.stats().lost_count, 0);
105   EXPECT_NEAR(analyzer.stats().psnr_with_freeze.Mean(), 38, 5);
106 }
107 
108 // TODO(bugs.webrtc.org/10515): Remove this when performance has been improved.
109 #if defined(WEBRTC_IOS) && defined(WEBRTC_ARCH_ARM64) && !defined(NDEBUG)
110 #define MAYBE_RealTimeEncoding DISABLED_RealTimeEncoding
111 #else
112 #define MAYBE_RealTimeEncoding RealTimeEncoding
113 #endif
TEST(ScenarioTest,MAYBE_RealTimeEncoding)114 TEST(ScenarioTest, MAYBE_RealTimeEncoding) {
115   VideoQualityAnalyzerConfig analyzer_config;
116   analyzer_config.psnr_coverage = 0.1;
117   VideoQualityAnalyzer analyzer(analyzer_config);
118   {
119     Scenario s("scenario/encode_real", true);
120     SetupVideoCall(s, &analyzer);
121     s.RunFor(TimeDelta::Seconds(2));
122   }
123   // Regression tests based on previous runs.
124   EXPECT_LT(analyzer.stats().lost_count, 2);
125   // This far below expected but ensures that we get something.
126   EXPECT_GT(analyzer.stats().psnr_with_freeze.Mean(), 10);
127 }
128 
TEST(ScenarioTest,SimTimeFakeing)129 TEST(ScenarioTest, SimTimeFakeing) {
130   Scenario s("scenario/encode_sim", false);
131   SetupVideoCall(s, nullptr);
132   s.RunFor(TimeDelta::Seconds(2));
133 }
134 
TEST(ScenarioTest,WritesToRtcEventLog)135 TEST(ScenarioTest, WritesToRtcEventLog) {
136   MemoryLogStorage storage;
137   {
138     Scenario s(storage.CreateFactory(), false);
139     SetupVideoCall(s, nullptr);
140     s.RunFor(TimeDelta::Seconds(1));
141   }
142   auto logs = storage.logs();
143   // We expect that a rtc event log has been created and that it has some data.
144   EXPECT_GE(storage.logs().at("alice.rtc.dat").size(), 1u);
145 }
146 
TEST(ScenarioTest,RetransmitsVideoPacketsInAudioAndVideoCallWithSendSideBweAndLoss)147 TEST(ScenarioTest,
148      RetransmitsVideoPacketsInAudioAndVideoCallWithSendSideBweAndLoss) {
149   // Make sure audio packets are included in transport feedback.
150   test::ScopedFieldTrials override_field_trials(
151       "WebRTC-Audio-ABWENoTWCC/Disabled/");
152 
153   Scenario s;
154   CallClientConfig call_client_config;
155   call_client_config.transport.rates.start_rate = DataRate::KilobitsPerSec(300);
156   auto* alice = s.CreateClient("alice", call_client_config);
157   auto* bob = s.CreateClient("bob", call_client_config);
158   NetworkSimulationConfig network_config;
159   // Add some loss and delay.
160   network_config.delay = TimeDelta::Millis(200);
161   network_config.loss_rate = 0.05;
162   auto alice_net = s.CreateSimulationNode(network_config);
163   auto bob_net = s.CreateSimulationNode(network_config);
164   auto route = s.CreateRoutes(alice, {alice_net}, bob, {bob_net});
165 
166   // First add an audio stream, then a video stream.
167   // Needed to make sure audio RTP module is selected first when sending
168   // transport feedback message.
169   AudioStreamConfig audio_stream_config;
170   audio_stream_config.encoder.min_rate = DataRate::KilobitsPerSec(6);
171   audio_stream_config.encoder.max_rate = DataRate::KilobitsPerSec(64);
172   audio_stream_config.encoder.allocate_bitrate = true;
173   audio_stream_config.stream.in_bandwidth_estimation = true;
174   s.CreateAudioStream(route->forward(), audio_stream_config);
175   s.CreateAudioStream(route->reverse(), audio_stream_config);
176 
177   VideoStreamConfig video_stream_config;
178   auto video = s.CreateVideoStream(route->forward(), video_stream_config);
179   s.CreateVideoStream(route->reverse(), video_stream_config);
180 
181   // Run for 10 seconds.
182   s.RunFor(TimeDelta::Seconds(10));
183   // Make sure retransmissions have happened.
184   int retransmit_packets = 0;
185 
186   VideoSendStream::Stats stats;
187   alice->SendTask([&]() { stats = video->send()->GetStats(); });
188 
189   for (const auto& substream : stats.substreams) {
190     retransmit_packets += substream.second.rtp_stats.retransmitted.packets;
191   }
192   EXPECT_GT(retransmit_packets, 0);
193 }
194 
195 }  // namespace test
196 }  // namespace webrtc
197