blob: 6861151a2d1c6e9066e312d0a939ff15054fc698 [file] [log] [blame]
Sebastian Jansson98b07e92018-09-27 13:47:01 +02001/*
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 */
Jonas Olssona4d87372019-07-05 19:08:33 +020010#include "test/scenario/scenario.h"
11
Sebastian Jansson105a10a2019-04-01 09:18:14 +020012#include <atomic>
Sebastian Jansson98b07e92018-09-27 13:47:01 +020013
Andrey Logvinf9ee0e02021-01-14 09:50:32 +000014#include "api/test/network_emulation/create_cross_traffic.h"
15#include "api/test/network_emulation/cross_traffic.h"
Erik Språng000953c2020-06-26 17:03:19 +020016#include "test/field_trial.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020017#include "test/gtest.h"
Sebastian Jansson58c71db2019-05-22 16:20:56 +020018#include "test/logging/memory_log_writer.h"
Sebastian Jansson7150d8c2019-04-09 14:18:09 +020019#include "test/scenario/stats_collection.h"
20
Sebastian Jansson98b07e92018-09-27 13:47:01 +020021namespace webrtc {
22namespace test {
23TEST(ScenarioTest, StartsAndStopsWithoutErrors) {
Sebastian Jansson105a10a2019-04-01 09:18:14 +020024 std::atomic<bool> packet_received(false);
25 std::atomic<bool> bitrate_changed(false);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020026 Scenario s;
27 CallClientConfig call_client_config;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +010028 call_client_config.transport.rates.start_rate = DataRate::KilobitsPerSec(300);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020029 auto* alice = s.CreateClient("alice", call_client_config);
30 auto* bob = s.CreateClient("bob", call_client_config);
Sebastian Janssonef86d142019-04-15 14:42:42 +020031 NetworkSimulationConfig network_config;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020032 auto alice_net = s.CreateSimulationNode(network_config);
33 auto bob_net = s.CreateSimulationNode(network_config);
Sebastian Jansson800e1212018-10-22 11:49:03 +020034 auto route = s.CreateRoutes(alice, {alice_net}, bob, {bob_net});
Sebastian Jansson98b07e92018-09-27 13:47:01 +020035
36 VideoStreamConfig video_stream_config;
Sebastian Jansson800e1212018-10-22 11:49:03 +020037 s.CreateVideoStream(route->forward(), video_stream_config);
38 s.CreateVideoStream(route->reverse(), video_stream_config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020039
40 AudioStreamConfig audio_stream_config;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +010041 audio_stream_config.encoder.min_rate = DataRate::KilobitsPerSec(6);
42 audio_stream_config.encoder.max_rate = DataRate::KilobitsPerSec(64);
Sebastian Jansson82858412018-10-11 19:48:05 +020043 audio_stream_config.encoder.allocate_bitrate = true;
44 audio_stream_config.stream.in_bandwidth_estimation = false;
Sebastian Jansson800e1212018-10-22 11:49:03 +020045 s.CreateAudioStream(route->forward(), audio_stream_config);
46 s.CreateAudioStream(route->reverse(), audio_stream_config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020047
Sebastian Janssona4c22b92019-04-15 21:10:00 +020048 RandomWalkConfig cross_traffic_config;
Andrey Logvinf9ee0e02021-01-14 09:50:32 +000049 s.net()->StartCrossTraffic(CreateRandomWalkCrossTraffic(
50 s.net()->CreateCrossTrafficRoute({alice_net}), cross_traffic_config));
Sebastian Jansson98b07e92018-09-27 13:47:01 +020051
Sebastian Jansson98b07e92018-09-27 13:47:01 +020052 s.NetworkDelayedAction({alice_net, bob_net}, 100,
53 [&packet_received] { packet_received = true; });
Danil Chapovalov0c626af2020-02-10 11:16:00 +010054 s.Every(TimeDelta::Millis(10), [alice, bob, &bitrate_changed] {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020055 if (alice->GetStats().send_bandwidth_bps != 300000 &&
56 bob->GetStats().send_bandwidth_bps != 300000)
57 bitrate_changed = true;
58 });
Danil Chapovalov0c626af2020-02-10 11:16:00 +010059 s.RunUntil(TimeDelta::Seconds(2), TimeDelta::Millis(5),
Sebastian Jansson98b07e92018-09-27 13:47:01 +020060 [&bitrate_changed, &packet_received] {
61 return packet_received && bitrate_changed;
62 });
63 EXPECT_TRUE(packet_received);
64 EXPECT_TRUE(bitrate_changed);
65}
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +020066namespace {
67void 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);
Sebastian Janssonef86d142019-04-15 14:42:42 +020071 NetworkSimulationConfig network_config;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +010072 network_config.bandwidth = DataRate::KilobitsPerSec(1000);
Danil Chapovalov0c626af2020-02-10 11:16:00 +010073 network_config.delay = TimeDelta::Millis(50);
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +020074 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
Patrik Höglund7d3f6022020-03-02 14:45:21 +010094TEST(ScenarioTest, SimTimeEncoding) {
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +020095 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);
Patrik Höglundcdda76d2020-02-20 12:07:29 +0100101 s.RunFor(TimeDelta::Seconds(2));
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +0200102 }
103 // Regression tests based on previous runs.
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +0200104 EXPECT_EQ(analyzer.stats().lost_count, 0);
Patrik Höglund7d3f6022020-03-02 14:45:21 +0100105 EXPECT_NEAR(analyzer.stats().psnr_with_freeze.Mean(), 38, 5);
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +0200106}
107
Sebastian Jansson86941182019-04-09 15:15:24 +0200108// TODO(bugs.webrtc.org/10515): Remove this when performance has been improved.
Sebastian Jansson7237c152019-04-08 16:47:49 +0200109#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
114TEST(ScenarioTest, MAYBE_RealTimeEncoding) {
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +0200115 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);
Patrik Höglundcdda76d2020-02-20 12:07:29 +0100121 s.RunFor(TimeDelta::Seconds(2));
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +0200122 }
123 // Regression tests based on previous runs.
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +0200124 EXPECT_LT(analyzer.stats().lost_count, 2);
Sebastian Janssonf72de7b2020-06-11 14:09:17 +0200125 // This far below expected but ensures that we get something.
126 EXPECT_GT(analyzer.stats().psnr_with_freeze.Mean(), 10);
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +0200127}
128
129TEST(ScenarioTest, SimTimeFakeing) {
130 Scenario s("scenario/encode_sim", false);
131 SetupVideoCall(s, nullptr);
Patrik Höglundcdda76d2020-02-20 12:07:29 +0100132 s.RunFor(TimeDelta::Seconds(2));
Sebastian Janssoncf2df2f2019-04-02 11:51:28 +0200133}
134
Sebastian Jansson58c71db2019-05-22 16:20:56 +0200135TEST(ScenarioTest, WritesToRtcEventLog) {
136 MemoryLogStorage storage;
137 {
138 Scenario s(storage.CreateFactory(), false);
139 SetupVideoCall(s, nullptr);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100140 s.RunFor(TimeDelta::Seconds(1));
Sebastian Jansson58c71db2019-05-22 16:20:56 +0200141 }
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
Erik Språng000953c2020-06-26 17:03:19 +0200147TEST(ScenarioTest,
148 RetransmitsVideoPacketsInAudioAndVideoCallWithSendSideBweAndLoss) {
149 // Make sure audio packets are included in transport feedback.
150 test::ScopedFieldTrials override_field_trials(
Jakob Ivarsson47a03e82020-11-23 15:05:44 +0100151 "WebRTC-Audio-ABWENoTWCC/Disabled/");
Erik Språng000953c2020-06-26 17:03:19 +0200152
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;
Tomas Gunnarsson788d8052021-05-03 16:23:08 +0200185
186 VideoSendStream::Stats stats;
187 alice->SendTask([&]() { stats = video->send()->GetStats(); });
188
189 for (const auto& substream : stats.substreams) {
Erik Språng000953c2020-06-26 17:03:19 +0200190 retransmit_packets += substream.second.rtp_stats.retransmitted.packets;
191 }
192 EXPECT_GT(retransmit_packets, 0);
193}
194
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200195} // namespace test
196} // namespace webrtc