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