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