blob: d360fb6c5694506e6e480297fc1ed173e79ab150 [file] [log] [blame]
Artem Titova6a273d2019-02-07 16:43:51 +01001/*
2 * Copyright (c) 2019 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/pc/e2e/peer_connection_quality_test.h"
11
Mirko Bonadei71318802019-02-18 18:52:43 +010012#include <algorithm>
Artem Titova6a273d2019-02-07 16:43:51 +010013#include <set>
14#include <utility>
15
16#include "absl/memory/memory.h"
Artem Titovef3fd9c2019-06-13 16:36:52 +020017#include "api/jsep.h"
Artem Titovbf9e01a2019-02-14 10:51:27 +010018#include "api/media_stream_interface.h"
Artem Titova6a273d2019-02-07 16:43:51 +010019#include "api/peer_connection_interface.h"
Niels Möllerd8b9ed72019-05-08 13:53:51 +020020#include "api/rtc_event_log_output_file.h"
Artem Titova6a273d2019-02-07 16:43:51 +010021#include "api/scoped_refptr.h"
Danil Chapovalovd0e0ed82019-04-18 14:48:24 +020022#include "api/task_queue/default_task_queue_factory.h"
Artem Titovd57628f2019-03-22 12:34:25 +010023#include "api/test/video_quality_analyzer_interface.h"
Artem Titova6a273d2019-02-07 16:43:51 +010024#include "api/units/time_delta.h"
Mirko Bonadeice7a4fb2019-02-25 11:45:07 +010025#include "logging/rtc_event_log/rtc_event_log.h"
Artem Titovf65a89b2019-05-07 11:56:44 +020026#include "pc/sdp_utils.h"
Artem Titovbf9e01a2019-02-14 10:51:27 +010027#include "pc/test/mock_peer_connection_observers.h"
Artem Titova6a273d2019-02-07 16:43:51 +010028#include "rtc_base/bind.h"
29#include "rtc_base/gunit.h"
Mirko Bonadei71318802019-02-18 18:52:43 +010030#include "rtc_base/numerics/safe_conversions.h"
Artem Titov9f97c9a2019-02-08 00:35:13 +010031#include "system_wrappers/include/cpu_info.h"
Artem Titov23702422019-05-27 15:28:30 +020032#include "system_wrappers/include/field_trial.h"
Mirko Bonadei3830d9b2019-02-28 14:07:56 +010033#include "test/pc/e2e/analyzer/audio/default_audio_quality_analyzer.h"
Artem Titov859abef2019-03-01 11:11:09 +010034#include "test/pc/e2e/analyzer/video/default_video_quality_analyzer.h"
Mirko Bonadei12ae4f42019-02-26 15:19:07 +010035#include "test/pc/e2e/stats_poller.h"
Artem Titova6a273d2019-02-07 16:43:51 +010036#include "test/testsupport/file_utils.h"
37
38namespace webrtc {
Artem Titov0b443142019-03-20 11:11:08 +010039namespace webrtc_pc_e2e {
Artem Titova6a273d2019-02-07 16:43:51 +010040namespace {
41
42using VideoConfig = PeerConnectionE2EQualityTestFixture::VideoConfig;
43
44constexpr int kDefaultTimeoutMs = 10000;
45constexpr char kSignalThreadName[] = "signaling_thread";
Artem Titov9f97c9a2019-02-08 00:35:13 +010046// 1 signaling, 2 network, 2 worker and 2 extra for codecs etc.
47constexpr int kPeerConnectionUsedThreads = 7;
48// Framework has extra thread for network layer and extra thread for peer
49// connection stats polling.
50constexpr int kFrameworkUsedThreads = 2;
51constexpr int kMaxVideoAnalyzerThreads = 8;
Artem Titova6a273d2019-02-07 16:43:51 +010052
Mirko Bonadei12ae4f42019-02-26 15:19:07 +010053constexpr TimeDelta kStatsUpdateInterval = TimeDelta::Seconds<1>();
54constexpr TimeDelta kStatsPollingStopTimeout = TimeDelta::Seconds<1>();
55
Artem Titov4d29ef02019-05-20 10:43:16 +020056constexpr TimeDelta kAliveMessageLogInterval = TimeDelta::Seconds<30>();
57
Artem Titov16850592019-07-10 14:34:02 +020058constexpr int kQuickTestModeRunDurationMs = 100;
59
Artem Titov23702422019-05-27 15:28:30 +020060// Field trials to enable Flex FEC advertising and receiving.
61constexpr char kFlexFecEnabledFieldTrials[] =
62 "WebRTC-FlexFEC-03-Advertised/Enabled/WebRTC-FlexFEC-03/Enabled/";
63
Artem Titova6a273d2019-02-07 16:43:51 +010064std::string VideoConfigSourcePresenceToString(const VideoConfig& video_config) {
65 char buf[1024];
66 rtc::SimpleStringBuilder builder(buf);
67 builder << "video_config.generator=" << video_config.generator.has_value()
68 << "; video_config.input_file_name="
69 << video_config.input_file_name.has_value()
70 << "; video_config.screen_share_config="
71 << video_config.screen_share_config.has_value() << ";";
72 return builder.str();
73}
74
Artem Titovbf9e01a2019-02-14 10:51:27 +010075class FixturePeerConnectionObserver : public MockPeerConnectionObserver {
76 public:
77 // |on_track_callback| will be called when any new track will be added to peer
78 // connection.
79 // |on_connected_callback| will be called when peer connection will come to
80 // either connected or completed state. Client should notice that in the case
81 // of reconnect this callback can be called again, so it should be tolerant
82 // to such behavior.
83 FixturePeerConnectionObserver(
84 std::function<void(rtc::scoped_refptr<RtpTransceiverInterface>)>
85 on_track_callback,
86 std::function<void()> on_connected_callback)
87 : on_track_callback_(std::move(on_track_callback)),
88 on_connected_callback_(std::move(on_connected_callback)) {}
89
90 void OnTrack(
91 rtc::scoped_refptr<RtpTransceiverInterface> transceiver) override {
92 MockPeerConnectionObserver::OnTrack(transceiver);
93 on_track_callback_(transceiver);
94 }
95
96 void OnIceConnectionChange(
97 PeerConnectionInterface::IceConnectionState new_state) override {
98 MockPeerConnectionObserver::OnIceConnectionChange(new_state);
99 if (ice_connected_) {
100 on_connected_callback_();
101 }
102 }
103
104 private:
105 std::function<void(rtc::scoped_refptr<RtpTransceiverInterface>)>
106 on_track_callback_;
107 std::function<void()> on_connected_callback_;
108};
109
Artem Titova6a273d2019-02-07 16:43:51 +0100110} // namespace
111
112PeerConnectionE2EQualityTest::PeerConnectionE2EQualityTest(
Artem Titov59835852019-02-27 17:44:13 +0100113 std::string test_case_name,
Mirko Bonadeif5d88082019-02-20 16:16:54 +0100114 std::unique_ptr<AudioQualityAnalyzerInterface> audio_quality_analyzer,
115 std::unique_ptr<VideoQualityAnalyzerInterface> video_quality_analyzer)
Artem Titov59835852019-02-27 17:44:13 +0100116 : clock_(Clock::GetRealTimeClock()),
Danil Chapovalovd0e0ed82019-04-18 14:48:24 +0200117 task_queue_factory_(CreateDefaultTaskQueueFactory()),
Artem Titovba82e002019-03-15 15:57:53 +0100118 test_case_name_(std::move(test_case_name)) {
Artem Titova6a273d2019-02-07 16:43:51 +0100119 // Create default video quality analyzer. We will always create an analyzer,
120 // even if there are no video streams, because it will be installed into video
121 // encoder/decoder factories.
Mirko Bonadeif5d88082019-02-20 16:16:54 +0100122 if (video_quality_analyzer == nullptr) {
Artem Titov859abef2019-03-01 11:11:09 +0100123 video_quality_analyzer = absl::make_unique<DefaultVideoQualityAnalyzer>();
Artem Titova6a273d2019-02-07 16:43:51 +0100124 }
125 encoded_image_id_controller_ =
Artem Titov32232e92019-02-20 21:13:14 +0100126 absl::make_unique<SingleProcessEncodedImageDataInjector>();
Artem Titova6a273d2019-02-07 16:43:51 +0100127 video_quality_analyzer_injection_helper_ =
128 absl::make_unique<VideoQualityAnalyzerInjectionHelper>(
Mirko Bonadeif5d88082019-02-20 16:16:54 +0100129 std::move(video_quality_analyzer), encoded_image_id_controller_.get(),
Artem Titova6a273d2019-02-07 16:43:51 +0100130 encoded_image_id_controller_.get());
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100131
132 if (audio_quality_analyzer == nullptr) {
133 audio_quality_analyzer = absl::make_unique<DefaultAudioQualityAnalyzer>();
134 }
135 audio_quality_analyzer_.swap(audio_quality_analyzer);
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100136}
137
Artem Titovba82e002019-03-15 15:57:53 +0100138void PeerConnectionE2EQualityTest::ExecuteAt(
139 TimeDelta target_time_since_start,
140 std::function<void(TimeDelta)> func) {
141 ExecuteTask(target_time_since_start, absl::nullopt, func);
142}
143
144void PeerConnectionE2EQualityTest::ExecuteEvery(
145 TimeDelta initial_delay_since_start,
146 TimeDelta interval,
147 std::function<void(TimeDelta)> func) {
148 ExecuteTask(initial_delay_since_start, interval, func);
149}
150
151void PeerConnectionE2EQualityTest::ExecuteTask(
152 TimeDelta initial_delay_since_start,
153 absl::optional<TimeDelta> interval,
154 std::function<void(TimeDelta)> func) {
155 RTC_CHECK(initial_delay_since_start.IsFinite() &&
156 initial_delay_since_start >= TimeDelta::Zero());
157 RTC_CHECK(!interval ||
158 (interval->IsFinite() && *interval > TimeDelta::Zero()));
159 rtc::CritScope crit(&lock_);
160 ScheduledActivity activity(initial_delay_since_start, interval, func);
161 if (start_time_.IsInfinite()) {
162 scheduled_activities_.push(std::move(activity));
163 } else {
164 PostTask(std::move(activity));
165 }
166}
167
168void PeerConnectionE2EQualityTest::PostTask(ScheduledActivity activity) {
169 // Because start_time_ will never change at this point copy it to local
170 // variable to capture in in lambda without requirement to hold a lock.
171 Timestamp start_time = start_time_;
172
173 TimeDelta remaining_delay =
174 activity.initial_delay_since_start == TimeDelta::Zero()
175 ? TimeDelta::Zero()
176 : activity.initial_delay_since_start - (Now() - start_time_);
177 if (remaining_delay < TimeDelta::Zero()) {
178 RTC_LOG(WARNING) << "Executing late task immediately, late by="
179 << ToString(remaining_delay.Abs());
180 remaining_delay = TimeDelta::Zero();
181 }
182
183 if (activity.interval) {
184 if (remaining_delay == TimeDelta::Zero()) {
185 repeating_task_handles_.push_back(RepeatingTaskHandle::Start(
186 task_queue_->Get(), [activity, start_time, this]() {
187 activity.func(Now() - start_time);
188 return *activity.interval;
189 }));
190 return;
191 }
192 repeating_task_handles_.push_back(RepeatingTaskHandle::DelayedStart(
193 task_queue_->Get(), remaining_delay, [activity, start_time, this]() {
194 activity.func(Now() - start_time);
195 return *activity.interval;
196 }));
197 return;
198 }
199
200 if (remaining_delay == TimeDelta::Zero()) {
201 task_queue_->PostTask(
202 [activity, start_time, this]() { activity.func(Now() - start_time); });
203 return;
204 }
205
206 task_queue_->PostDelayedTask(
207 [activity, start_time, this]() { activity.func(Now() - start_time); },
208 remaining_delay.ms());
209}
210
Artem Titov18459222019-04-24 11:09:35 +0200211void PeerConnectionE2EQualityTest::AddQualityMetricsReporter(
212 std::unique_ptr<QualityMetricsReporter> quality_metrics_reporter) {
213 quality_metrics_reporters_.push_back(std::move(quality_metrics_reporter));
214}
215
Artem Titovd09bc552019-03-20 11:18:58 +0100216void PeerConnectionE2EQualityTest::AddPeer(
217 rtc::Thread* network_thread,
218 rtc::NetworkManager* network_manager,
219 rtc::FunctionView<void(PeerConfigurer*)> configurer) {
220 peer_configurations_.push_back(
221 absl::make_unique<PeerConfigurerImpl>(network_thread, network_manager));
222 configurer(peer_configurations_.back().get());
223}
224
Jonas Olssona4d87372019-07-05 19:08:33 +0200225void PeerConnectionE2EQualityTest::Run(RunParams run_params) {
Artem Titovd09bc552019-03-20 11:18:58 +0100226 RTC_CHECK_EQ(peer_configurations_.size(), 2)
227 << "Only peer to peer calls are allowed, please add 2 peers";
228
229 std::unique_ptr<Params> alice_params =
230 peer_configurations_[0]->ReleaseParams();
231 std::unique_ptr<InjectableComponents> alice_components =
232 peer_configurations_[0]->ReleaseComponents();
233 std::unique_ptr<Params> bob_params = peer_configurations_[1]->ReleaseParams();
234 std::unique_ptr<InjectableComponents> bob_components =
235 peer_configurations_[1]->ReleaseComponents();
236 peer_configurations_.clear();
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100237
Artem Titov9a7e7212019-02-28 16:34:17 +0100238 SetDefaultValuesForMissingParams({alice_params.get(), bob_params.get()});
Artem Titovade945d2019-04-02 18:31:48 +0200239 ValidateParams(run_params, {alice_params.get(), bob_params.get()});
Artem Titov23702422019-05-27 15:28:30 +0200240 SetupRequiredFieldTrials(run_params);
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100241
242 // Print test summary
243 RTC_LOG(INFO)
244 << "Media quality test: Alice will make a call to Bob with media video="
245 << !alice_params->video_configs.empty()
246 << "; audio=" << alice_params->audio_config.has_value()
247 << ". Bob will respond with media video="
248 << !bob_params->video_configs.empty()
249 << "; audio=" << bob_params->audio_config.has_value();
250
251 const std::unique_ptr<rtc::Thread> signaling_thread = rtc::Thread::Create();
252 signaling_thread->SetName(kSignalThreadName, nullptr);
253 signaling_thread->Start();
Artem Titova6a273d2019-02-07 16:43:51 +0100254
Artem Titov70f80e52019-04-12 13:13:39 +0200255 // Create a |task_queue_|.
256 task_queue_ = absl::make_unique<TaskQueueForTest>("pc_e2e_quality_test");
257
Artem Titova6a273d2019-02-07 16:43:51 +0100258 // Create call participants: Alice and Bob.
259 // Audio streams are intercepted in AudioDeviceModule, so if it is required to
260 // catch output of Alice's stream, Alice's output_dump_file_name should be
261 // passed to Bob's TestPeer setup as audio output file name.
Artem Titovbc558ce2019-07-08 19:13:21 +0200262 absl::optional<TestPeer::RemotePeerAudioConfig> alice_remote_audio_config =
263 TestPeer::CreateRemoteAudioConfig(bob_params->audio_config);
264 absl::optional<TestPeer::RemotePeerAudioConfig> bob_remote_audio_config =
265 TestPeer::CreateRemoteAudioConfig(alice_params->audio_config);
Artem Titovbf9e01a2019-02-14 10:51:27 +0100266 // Copy Alice and Bob video configs to correctly pass them into lambdas.
267 std::vector<VideoConfig> alice_video_configs = alice_params->video_configs;
268 std::vector<VideoConfig> bob_video_configs = bob_params->video_configs;
269
Artem Titova6a273d2019-02-07 16:43:51 +0100270 alice_ = TestPeer::CreateTestPeer(
271 std::move(alice_components), std::move(alice_params),
Artem Titovbf9e01a2019-02-14 10:51:27 +0100272 absl::make_unique<FixturePeerConnectionObserver>(
273 [this, bob_video_configs](
274 rtc::scoped_refptr<RtpTransceiverInterface> transceiver) {
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200275 OnTrackCallback(transceiver, bob_video_configs);
Artem Titovbf9e01a2019-02-14 10:51:27 +0100276 },
277 [this]() { StartVideo(alice_video_sources_); }),
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100278 video_quality_analyzer_injection_helper_.get(), signaling_thread.get(),
Artem Titovbc558ce2019-07-08 19:13:21 +0200279 alice_remote_audio_config, run_params.video_encoder_bitrate_multiplier,
280 task_queue_.get());
Artem Titova6a273d2019-02-07 16:43:51 +0100281 bob_ = TestPeer::CreateTestPeer(
282 std::move(bob_components), std::move(bob_params),
Artem Titovbf9e01a2019-02-14 10:51:27 +0100283 absl::make_unique<FixturePeerConnectionObserver>(
284 [this, alice_video_configs](
285 rtc::scoped_refptr<RtpTransceiverInterface> transceiver) {
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200286 OnTrackCallback(transceiver, alice_video_configs);
Artem Titovbf9e01a2019-02-14 10:51:27 +0100287 },
288 [this]() { StartVideo(bob_video_sources_); }),
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100289 video_quality_analyzer_injection_helper_.get(), signaling_thread.get(),
Artem Titovbc558ce2019-07-08 19:13:21 +0200290 bob_remote_audio_config, run_params.video_encoder_bitrate_multiplier,
291 task_queue_.get());
Artem Titov9f97c9a2019-02-08 00:35:13 +0100292
293 int num_cores = CpuInfo::DetectNumberOfCores();
294 RTC_DCHECK_GE(num_cores, 1);
295
296 int video_analyzer_threads =
297 num_cores - kPeerConnectionUsedThreads - kFrameworkUsedThreads;
298 if (video_analyzer_threads <= 0) {
299 video_analyzer_threads = 1;
300 }
301 video_analyzer_threads =
302 std::min(video_analyzer_threads, kMaxVideoAnalyzerThreads);
303 RTC_LOG(INFO) << "video_analyzer_threads=" << video_analyzer_threads;
304
Artem Titov59835852019-02-27 17:44:13 +0100305 video_quality_analyzer_injection_helper_->Start(test_case_name_,
306 video_analyzer_threads);
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200307 audio_quality_analyzer_->Start(test_case_name_, &analyzer_helper_);
Artem Titov18459222019-04-24 11:09:35 +0200308 for (auto& reporter : quality_metrics_reporters_) {
309 reporter->Start(test_case_name_);
310 }
Mirko Bonadeice7a4fb2019-02-25 11:45:07 +0100311
312 // Start RTCEventLog recording if requested.
313 if (alice_->params()->rtc_event_log_path) {
314 auto alice_rtc_event_log = absl::make_unique<webrtc::RtcEventLogOutputFile>(
315 alice_->params()->rtc_event_log_path.value());
316 alice_->pc()->StartRtcEventLog(std::move(alice_rtc_event_log),
317 webrtc::RtcEventLog::kImmediateOutput);
318 }
319 if (bob_->params()->rtc_event_log_path) {
320 auto bob_rtc_event_log = absl::make_unique<webrtc::RtcEventLogOutputFile>(
321 bob_->params()->rtc_event_log_path.value());
322 bob_->pc()->StartRtcEventLog(std::move(bob_rtc_event_log),
323 webrtc::RtcEventLog::kImmediateOutput);
324 }
325
Artem Titov4d29ef02019-05-20 10:43:16 +0200326 // Setup alive logging. It is done to prevent test infra to think that test is
327 // dead.
328 RepeatingTaskHandle::DelayedStart(task_queue_->Get(),
329 kAliveMessageLogInterval, []() {
330 std::printf("Test is still running...\n");
331 return kAliveMessageLogInterval;
332 });
333
Artem Titovba82e002019-03-15 15:57:53 +0100334 // Setup call.
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100335 signaling_thread->Invoke<void>(
Artem Titova6a273d2019-02-07 16:43:51 +0100336 RTC_FROM_HERE,
Artem Titovf65a89b2019-05-07 11:56:44 +0200337 rtc::Bind(&PeerConnectionE2EQualityTest::SetupCallOnSignalingThread, this,
338 run_params));
Artem Titovba82e002019-03-15 15:57:53 +0100339 {
340 rtc::CritScope crit(&lock_);
341 start_time_ = Now();
342 while (!scheduled_activities_.empty()) {
343 PostTask(std::move(scheduled_activities_.front()));
344 scheduled_activities_.pop();
345 }
346 }
Mirko Bonadei71318802019-02-18 18:52:43 +0100347
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100348 StatsPoller stats_poller({audio_quality_analyzer_.get(),
349 video_quality_analyzer_injection_helper_.get()},
350 {{"alice", alice_.get()}, {"bob", bob_.get()}});
351
Artem Titovba82e002019-03-15 15:57:53 +0100352 task_queue_->PostTask([&stats_poller, this]() {
353 RTC_DCHECK_RUN_ON(task_queue_.get());
Sebastian Janssoncda86dd2019-03-11 17:26:36 +0100354 stats_polling_task_ =
Artem Titovba82e002019-03-15 15:57:53 +0100355 RepeatingTaskHandle::Start(task_queue_->Get(), [this, &stats_poller]() {
356 RTC_DCHECK_RUN_ON(task_queue_.get());
Sebastian Janssoncda86dd2019-03-11 17:26:36 +0100357 stats_poller.PollStatsAndNotifyObservers();
358 return kStatsUpdateInterval;
359 });
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100360 });
361
Mirko Bonadei71318802019-02-18 18:52:43 +0100362 rtc::Event done;
Artem Titov16850592019-07-10 14:34:02 +0200363 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
364 if (is_quick_test_enabled) {
365 done.Wait(kQuickTestModeRunDurationMs);
366 } else {
367 done.Wait(run_params.run_duration.ms());
368 }
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100369
370 rtc::Event stats_polling_stopped;
Artem Titovba82e002019-03-15 15:57:53 +0100371 task_queue_->PostTask([&stats_polling_stopped, this]() {
372 RTC_DCHECK_RUN_ON(task_queue_.get());
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100373 stats_polling_task_.Stop();
374 stats_polling_stopped.Set();
375 });
376 bool no_timeout = stats_polling_stopped.Wait(kStatsPollingStopTimeout.ms());
377 RTC_CHECK(no_timeout) << "Failed to stop Stats polling after "
378 << kStatsPollingStopTimeout.seconds() << " seconds.";
Mirko Bonadei71318802019-02-18 18:52:43 +0100379
Artem Titov70f80e52019-04-12 13:13:39 +0200380 // We need to detach AEC dumping from peers, because dump uses |task_queue_|
381 // inside.
382 alice_->DetachAecDump();
383 bob_->DetachAecDump();
Artem Titov4d29ef02019-05-20 10:43:16 +0200384 // Stop all client started tasks on task queue to prevent their access to any
385 // call related objects after these objects will be destroyed during call tear
386 // down.
387 task_queue_->SendTask([this]() {
388 rtc::CritScope crit(&lock_);
389 for (auto& handle : repeating_task_handles_) {
390 handle.Stop();
391 }
392 });
Artem Titovba82e002019-03-15 15:57:53 +0100393 // Tear down the call.
Mirko Bonadei71318802019-02-18 18:52:43 +0100394 signaling_thread->Invoke<void>(
395 RTC_FROM_HERE,
396 rtc::Bind(&PeerConnectionE2EQualityTest::TearDownCallOnSignalingThread,
397 this));
Artem Titovb93c4e62019-05-02 10:52:07 +0200398 Timestamp end_time = Now();
399 {
400 rtc::CritScope crit(&lock_);
401 real_test_duration_ = end_time - start_time_;
402 }
Mirko Bonadei71318802019-02-18 18:52:43 +0100403
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200404 audio_quality_analyzer_->Stop();
Artem Titov9f97c9a2019-02-08 00:35:13 +0100405 video_quality_analyzer_injection_helper_->Stop();
Artem Titov18459222019-04-24 11:09:35 +0200406 for (auto& reporter : quality_metrics_reporters_) {
407 reporter->StopAndReportResults();
408 }
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100409
Artem Titov4d29ef02019-05-20 10:43:16 +0200410 // Reset |task_queue_| after test to cleanup.
411 task_queue_.reset();
412
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100413 // Ensuring that TestPeers have been destroyed in order to correctly close
414 // Audio dumps.
415 RTC_CHECK(!alice_);
416 RTC_CHECK(!bob_);
417 // Ensuring that FrameGeneratorCapturerVideoTrackSource and VideoFrameWriter
418 // are destroyed on the right thread.
Artem Titovbf9e01a2019-02-14 10:51:27 +0100419 RTC_CHECK(alice_video_sources_.empty());
420 RTC_CHECK(bob_video_sources_.empty());
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100421 RTC_CHECK(video_writers_.empty());
Artem Titova6a273d2019-02-07 16:43:51 +0100422}
423
Artem Titov9a7e7212019-02-28 16:34:17 +0100424void PeerConnectionE2EQualityTest::SetDefaultValuesForMissingParams(
Artem Titova6a273d2019-02-07 16:43:51 +0100425 std::vector<Params*> params) {
Artem Titov3481db22019-02-28 13:13:15 +0100426 int video_counter = 0;
427 int audio_counter = 0;
Artem Titova6a273d2019-02-07 16:43:51 +0100428 std::set<std::string> video_labels;
Artem Titov3481db22019-02-28 13:13:15 +0100429 std::set<std::string> audio_labels;
Artem Titova6a273d2019-02-07 16:43:51 +0100430 for (auto* p : params) {
431 for (auto& video_config : p->video_configs) {
Artem Titov9a7e7212019-02-28 16:34:17 +0100432 if (!video_config.generator && !video_config.input_file_name &&
433 !video_config.screen_share_config) {
434 video_config.generator = VideoGeneratorType::kDefault;
435 }
Artem Titova6a273d2019-02-07 16:43:51 +0100436 if (!video_config.stream_label) {
437 std::string label;
438 do {
Artem Titov3481db22019-02-28 13:13:15 +0100439 label = "_auto_video_stream_label_" + std::to_string(video_counter);
440 ++video_counter;
Artem Titova6a273d2019-02-07 16:43:51 +0100441 } while (!video_labels.insert(label).second);
442 video_config.stream_label = label;
443 }
444 }
Artem Titov3481db22019-02-28 13:13:15 +0100445 if (p->audio_config) {
446 if (!p->audio_config->stream_label) {
447 std::string label;
448 do {
449 label = "_auto_audio_stream_label_" + std::to_string(audio_counter);
450 ++audio_counter;
451 } while (!audio_labels.insert(label).second);
452 p->audio_config->stream_label = label;
453 }
454 }
Artem Titova6a273d2019-02-07 16:43:51 +0100455 }
456}
457
Artem Titovade945d2019-04-02 18:31:48 +0200458void PeerConnectionE2EQualityTest::ValidateParams(const RunParams& run_params,
459 std::vector<Params*> params) {
460 RTC_CHECK_GT(run_params.video_encoder_bitrate_multiplier, 0.0);
461
Artem Titova6a273d2019-02-07 16:43:51 +0100462 std::set<std::string> video_labels;
Artem Titov3481db22019-02-28 13:13:15 +0100463 std::set<std::string> audio_labels;
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100464 int media_streams_count = 0;
465
Artem Titovef3fd9c2019-06-13 16:36:52 +0200466 for (size_t i = 0; i < params.size(); ++i) {
467 Params* p = params[i];
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100468 if (p->audio_config) {
469 media_streams_count++;
470 }
471 media_streams_count += p->video_configs.size();
472
Artem Titova6a273d2019-02-07 16:43:51 +0100473 // Validate that each video config has exactly one of |generator|,
474 // |input_file_name| or |screen_share_config| set. Also validate that all
475 // video stream labels are unique.
476 for (auto& video_config : p->video_configs) {
477 RTC_CHECK(video_config.stream_label);
478 bool inserted =
479 video_labels.insert(video_config.stream_label.value()).second;
480 RTC_CHECK(inserted) << "Duplicate video_config.stream_label="
481 << video_config.stream_label.value();
482 RTC_CHECK(video_config.generator || video_config.input_file_name ||
483 video_config.screen_share_config)
484 << VideoConfigSourcePresenceToString(video_config);
485 RTC_CHECK(!(video_config.input_file_name && video_config.generator))
486 << VideoConfigSourcePresenceToString(video_config);
487 RTC_CHECK(
488 !(video_config.input_file_name && video_config.screen_share_config))
489 << VideoConfigSourcePresenceToString(video_config);
490 RTC_CHECK(!(video_config.screen_share_config && video_config.generator))
491 << VideoConfigSourcePresenceToString(video_config);
Artem Titov7581ff72019-05-15 15:45:33 +0200492
493 if (video_config.screen_share_config) {
494 if (video_config.screen_share_config->slides_yuv_file_names.empty()) {
495 if (video_config.screen_share_config->scrolling_params) {
496 // If we have scrolling params, then its |source_width| and
497 // |source_heigh| will be used as width and height of video input,
498 // so we have to validate it against width and height of default
499 // input.
500 RTC_CHECK_EQ(video_config.screen_share_config->scrolling_params
501 ->source_width,
502 kDefaultSlidesWidth);
503 RTC_CHECK_EQ(video_config.screen_share_config->scrolling_params
504 ->source_height,
505 kDefaultSlidesHeight);
506 } else {
507 RTC_CHECK_EQ(video_config.width, kDefaultSlidesWidth);
508 RTC_CHECK_EQ(video_config.height, kDefaultSlidesHeight);
509 }
510 }
511 if (video_config.screen_share_config->scrolling_params) {
512 RTC_CHECK_LE(
513 video_config.screen_share_config->scrolling_params->duration,
514 video_config.screen_share_config->slide_change_interval);
515 RTC_CHECK_GE(
516 video_config.screen_share_config->scrolling_params->source_width,
517 video_config.width);
518 RTC_CHECK_GE(
519 video_config.screen_share_config->scrolling_params->source_height,
520 video_config.height);
521 }
522 }
Artem Titovef3fd9c2019-06-13 16:36:52 +0200523 if (video_config.simulcast_config) {
524 // We support simulcast only for Vp8 for now.
525 // RTC_CHECK_EQ(run_params.video_codec_name, cricket::kVp8CodecName);
526 // Also we support simulcast only from caller.
527 RTC_CHECK_EQ(i, 0)
528 << "Only simulcast stream from first peer is supported";
529 }
Artem Titova6a273d2019-02-07 16:43:51 +0100530 }
531 if (p->audio_config) {
Artem Titov3481db22019-02-28 13:13:15 +0100532 bool inserted =
533 audio_labels.insert(p->audio_config->stream_label.value()).second;
534 RTC_CHECK(inserted) << "Duplicate audio_config.stream_label="
535 << p->audio_config->stream_label.value();
Artem Titova6a273d2019-02-07 16:43:51 +0100536 // Check that if mode input file name specified only if mode is kFile.
537 if (p->audio_config.value().mode == AudioConfig::Mode::kGenerated) {
538 RTC_CHECK(!p->audio_config.value().input_file_name);
539 }
540 if (p->audio_config.value().mode == AudioConfig::Mode::kFile) {
541 RTC_CHECK(p->audio_config.value().input_file_name);
Artem Titov0b443142019-03-20 11:11:08 +0100542 RTC_CHECK(
Artem Titov70f80e52019-04-12 13:13:39 +0200543 test::FileExists(p->audio_config.value().input_file_name.value()))
544 << p->audio_config.value().input_file_name.value()
545 << " doesn't exist";
Artem Titova6a273d2019-02-07 16:43:51 +0100546 }
547 }
548 }
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100549
550 RTC_CHECK_GT(media_streams_count, 0) << "No media in the call.";
Artem Titova6a273d2019-02-07 16:43:51 +0100551}
552
Artem Titov23702422019-05-27 15:28:30 +0200553void PeerConnectionE2EQualityTest::SetupRequiredFieldTrials(
554 const RunParams& run_params) {
555 std::string field_trials = "";
556 if (run_params.use_flex_fec) {
557 field_trials += kFlexFecEnabledFieldTrials;
558 }
559 if (!field_trials.empty()) {
560 override_field_trials_ = absl::make_unique<test::ScopedFieldTrials>(
561 field_trial::GetFieldTrialString() + field_trials);
562 }
563}
564
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200565void PeerConnectionE2EQualityTest::OnTrackCallback(
Artem Titovbf9e01a2019-02-14 10:51:27 +0100566 rtc::scoped_refptr<RtpTransceiverInterface> transceiver,
567 std::vector<VideoConfig> remote_video_configs) {
568 const rtc::scoped_refptr<MediaStreamTrackInterface>& track =
569 transceiver->receiver()->track();
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200570 RTC_CHECK_EQ(transceiver->receiver()->stream_ids().size(), 1);
571 std::string stream_label = transceiver->receiver()->stream_ids().front();
572 analyzer_helper_.AddTrackToStreamMapping(track->id(), stream_label);
Artem Titovbf9e01a2019-02-14 10:51:27 +0100573 if (track->kind() != MediaStreamTrackInterface::kVideoKind) {
574 return;
575 }
576
577 VideoConfig* video_config = nullptr;
578 for (auto& config : remote_video_configs) {
Artem Titov7c554152019-02-28 10:25:52 +0100579 if (config.stream_label == stream_label) {
Artem Titovbf9e01a2019-02-14 10:51:27 +0100580 video_config = &config;
581 break;
582 }
583 }
584 RTC_CHECK(video_config);
Artem Titov0b443142019-03-20 11:11:08 +0100585 test::VideoFrameWriter* writer = MaybeCreateVideoWriter(
Artem Titovbf9e01a2019-02-14 10:51:27 +0100586 video_config->output_dump_file_name, *video_config);
587 // It is safe to cast here, because it is checked above that
588 // track->kind() is kVideoKind.
589 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
590 std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>> video_sink =
591 video_quality_analyzer_injection_helper_->CreateVideoSink(writer);
592 video_track->AddOrUpdateSink(video_sink.get(), rtc::VideoSinkWants());
593 output_video_sinks_.push_back(std::move(video_sink));
594}
595
Artem Titovf65a89b2019-05-07 11:56:44 +0200596void PeerConnectionE2EQualityTest::SetupCallOnSignalingThread(
597 const RunParams& run_params) {
Artem Titov7c554152019-02-28 10:25:52 +0100598 // We need receive-only transceivers for Bob's media stream, so there will
599 // be media section in SDP for that streams in Alice's offer, because it is
600 // forbidden to add new media sections in answer in Unified Plan.
601 RtpTransceiverInit receive_only_transceiver_init;
602 receive_only_transceiver_init.direction = RtpTransceiverDirection::kRecvOnly;
Artem Titovef3fd9c2019-06-13 16:36:52 +0200603 int alice_transceivers_counter = 0;
Artem Titov7c554152019-02-28 10:25:52 +0100604 if (bob_->params()->audio_config) {
605 // Setup receive audio transceiver if Bob has audio to send. If we'll need
606 // multiple audio streams, then we need transceiver for each Bob's audio
607 // stream.
Artem Titov7f2a67f2019-06-13 19:45:55 +0200608 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
609 alice_->AddTransceiver(cricket::MediaType::MEDIA_TYPE_AUDIO,
610 receive_only_transceiver_init);
611 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200612 alice_transceivers_counter++;
613 }
614
615 for (auto& video_config : alice_->params()->video_configs) {
616 if (video_config.simulcast_config) {
617 RtpTransceiverInit transceiver_params;
618 transceiver_params.direction = RtpTransceiverDirection::kSendOnly;
619 for (int i = 0;
620 i < video_config.simulcast_config->simulcast_streams_count; ++i) {
621 RtpEncodingParameters enc_params;
622 // We need to be sure, that all rids will be unique with all mids.
623 enc_params.rid = std::to_string(alice_transceivers_counter) + "000" +
624 std::to_string(i);
625 transceiver_params.send_encodings.push_back(enc_params);
626 }
Artem Titov7f2a67f2019-06-13 19:45:55 +0200627 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
628 alice_->AddTransceiver(cricket::MediaType::MEDIA_TYPE_VIDEO,
629 transceiver_params);
630 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200631 alice_transceivers_counter++;
632 }
Artem Titov7c554152019-02-28 10:25:52 +0100633 }
634 for (size_t i = 0; i < bob_->params()->video_configs.size(); ++i) {
Artem Titov7f2a67f2019-06-13 19:45:55 +0200635 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
636 alice_->AddTransceiver(cricket::MediaType::MEDIA_TYPE_VIDEO,
637 receive_only_transceiver_init);
638 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200639 alice_transceivers_counter++;
Artem Titov7c554152019-02-28 10:25:52 +0100640 }
641 // Then add media for Alice and Bob
642 alice_video_sources_ = MaybeAddMedia(alice_.get());
643 bob_video_sources_ = MaybeAddMedia(bob_.get());
Artem Titova6a273d2019-02-07 16:43:51 +0100644
Artem Titovf65a89b2019-05-07 11:56:44 +0200645 SetPeerCodecPreferences(alice_.get(), run_params);
646 SetPeerCodecPreferences(bob_.get(), run_params);
647
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100648 SetupCall();
Mirko Bonadei71318802019-02-18 18:52:43 +0100649}
Artem Titova6a273d2019-02-07 16:43:51 +0100650
Mirko Bonadei71318802019-02-18 18:52:43 +0100651void PeerConnectionE2EQualityTest::TearDownCallOnSignalingThread() {
Artem Titova6a273d2019-02-07 16:43:51 +0100652 TearDownCall();
Artem Titova6a273d2019-02-07 16:43:51 +0100653}
654
Artem Titovbf9e01a2019-02-14 10:51:27 +0100655std::vector<rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource>>
Artem Titov7c554152019-02-28 10:25:52 +0100656PeerConnectionE2EQualityTest::MaybeAddMedia(TestPeer* peer) {
657 MaybeAddAudio(peer);
658 return MaybeAddVideo(peer);
Artem Titova6a273d2019-02-07 16:43:51 +0100659}
660
Artem Titovbf9e01a2019-02-14 10:51:27 +0100661std::vector<rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource>>
Artem Titov7c554152019-02-28 10:25:52 +0100662PeerConnectionE2EQualityTest::MaybeAddVideo(TestPeer* peer) {
Artem Titova6a273d2019-02-07 16:43:51 +0100663 // Params here valid because of pre-run validation.
664 Params* params = peer->params();
Artem Titovbf9e01a2019-02-14 10:51:27 +0100665 std::vector<rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource>> out;
Artem Titova6a273d2019-02-07 16:43:51 +0100666 for (auto video_config : params->video_configs) {
667 // Create video generator.
Artem Titov0b443142019-03-20 11:11:08 +0100668 std::unique_ptr<test::FrameGenerator> frame_generator =
Artem Titova6a273d2019-02-07 16:43:51 +0100669 CreateFrameGenerator(video_config);
670
671 // Wrap it to inject video quality analyzer and enable dump of input video
672 // if required.
Artem Titov0b443142019-03-20 11:11:08 +0100673 test::VideoFrameWriter* writer =
Artem Titova6a273d2019-02-07 16:43:51 +0100674 MaybeCreateVideoWriter(video_config.input_dump_file_name, video_config);
675 frame_generator =
676 video_quality_analyzer_injection_helper_->WrapFrameGenerator(
677 video_config.stream_label.value(), std::move(frame_generator),
678 writer);
679
680 // Setup FrameGenerator into peer connection.
Danil Chapovalovd0e0ed82019-04-18 14:48:24 +0200681 auto capturer = absl::make_unique<test::FrameGeneratorCapturer>(
682 clock_, std::move(frame_generator), video_config.fps,
683 *task_queue_factory_);
684 capturer->Init();
Artem Titova6a273d2019-02-07 16:43:51 +0100685 rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource> source =
686 new rtc::RefCountedObject<FrameGeneratorCapturerVideoTrackSource>(
Artem Titov232b6a12019-05-29 11:05:01 +0200687 std::move(capturer),
688 /*is_screencast=*/video_config.screen_share_config.has_value());
Artem Titovbf9e01a2019-02-14 10:51:27 +0100689 out.push_back(source);
Artem Titova6a273d2019-02-07 16:43:51 +0100690 RTC_LOG(INFO) << "Adding video with video_config.stream_label="
691 << video_config.stream_label.value();
692 rtc::scoped_refptr<VideoTrackInterface> track =
693 peer->pc_factory()->CreateVideoTrack(video_config.stream_label.value(),
694 source);
Artem Titov232b6a12019-05-29 11:05:01 +0200695 if (video_config.screen_share_config) {
696 track->set_content_hint(VideoTrackInterface::ContentHint::kText);
697 }
Artem Titov7c554152019-02-28 10:25:52 +0100698 peer->AddTrack(track, {video_config.stream_label.value()});
Artem Titova6a273d2019-02-07 16:43:51 +0100699 }
Artem Titovbf9e01a2019-02-14 10:51:27 +0100700 return out;
Artem Titova6a273d2019-02-07 16:43:51 +0100701}
702
Artem Titov0b443142019-03-20 11:11:08 +0100703std::unique_ptr<test::FrameGenerator>
Artem Titova6a273d2019-02-07 16:43:51 +0100704PeerConnectionE2EQualityTest::CreateFrameGenerator(
705 const VideoConfig& video_config) {
706 if (video_config.generator) {
Artem Titov0b443142019-03-20 11:11:08 +0100707 absl::optional<test::FrameGenerator::OutputType> frame_generator_type =
Artem Titova6a273d2019-02-07 16:43:51 +0100708 absl::nullopt;
709 if (video_config.generator == VideoGeneratorType::kDefault) {
Artem Titov0b443142019-03-20 11:11:08 +0100710 frame_generator_type = test::FrameGenerator::OutputType::I420;
Artem Titova6a273d2019-02-07 16:43:51 +0100711 } else if (video_config.generator == VideoGeneratorType::kI420A) {
Artem Titov0b443142019-03-20 11:11:08 +0100712 frame_generator_type = test::FrameGenerator::OutputType::I420A;
Artem Titova6a273d2019-02-07 16:43:51 +0100713 } else if (video_config.generator == VideoGeneratorType::kI010) {
Artem Titov0b443142019-03-20 11:11:08 +0100714 frame_generator_type = test::FrameGenerator::OutputType::I010;
Artem Titova6a273d2019-02-07 16:43:51 +0100715 }
Artem Titov0b443142019-03-20 11:11:08 +0100716 return test::FrameGenerator::CreateSquareGenerator(
Artem Titova6a273d2019-02-07 16:43:51 +0100717 static_cast<int>(video_config.width),
718 static_cast<int>(video_config.height), frame_generator_type,
719 absl::nullopt);
720 }
721 if (video_config.input_file_name) {
Artem Titov0b443142019-03-20 11:11:08 +0100722 return test::FrameGenerator::CreateFromYuvFile(
Artem Titova6a273d2019-02-07 16:43:51 +0100723 std::vector<std::string>(/*count=*/1,
724 video_config.input_file_name.value()),
725 video_config.width, video_config.height, /*frame_repeat_count=*/1);
726 }
727 if (video_config.screen_share_config) {
Artem Titov7581ff72019-05-15 15:45:33 +0200728 return CreateScreenShareFrameGenerator(video_config);
Artem Titova6a273d2019-02-07 16:43:51 +0100729 }
730 RTC_NOTREACHED() << "Unsupported video_config input source";
731 return nullptr;
732}
733
Artem Titov7581ff72019-05-15 15:45:33 +0200734std::unique_ptr<test::FrameGenerator>
735PeerConnectionE2EQualityTest::CreateScreenShareFrameGenerator(
736 const VideoConfig& video_config) {
737 RTC_CHECK(video_config.screen_share_config);
738 if (video_config.screen_share_config->generate_slides) {
739 return test::FrameGenerator::CreateSlideGenerator(
740 video_config.width, video_config.height,
741 video_config.screen_share_config->slide_change_interval.seconds() *
742 video_config.fps);
743 }
744 std::vector<std::string> slides =
745 video_config.screen_share_config->slides_yuv_file_names;
746 if (slides.empty()) {
747 // If slides is empty we need to add default slides as source. In such case
748 // video width and height is validated to be equal to kDefaultSlidesWidth
749 // and kDefaultSlidesHeight.
750 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
751 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
752 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
753 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
754 }
755 if (!video_config.screen_share_config->scrolling_params) {
756 // Cycle image every slide_change_interval seconds.
757 return test::FrameGenerator::CreateFromYuvFile(
758 slides, video_config.width, video_config.height,
759 video_config.screen_share_config->slide_change_interval.seconds() *
760 video_config.fps);
761 }
762
763 // |pause_duration| is nonnegative. It is validated in ValidateParams(...).
764 TimeDelta pause_duration =
765 video_config.screen_share_config->slide_change_interval -
766 video_config.screen_share_config->scrolling_params->duration;
767
768 return test::FrameGenerator::CreateScrollingInputFromYuvFiles(
769 clock_, slides,
770 video_config.screen_share_config->scrolling_params->source_width,
771 video_config.screen_share_config->scrolling_params->source_height,
772 video_config.width, video_config.height,
773 video_config.screen_share_config->scrolling_params->duration.ms(),
774 pause_duration.ms());
775}
776
Artem Titov7c554152019-02-28 10:25:52 +0100777void PeerConnectionE2EQualityTest::MaybeAddAudio(TestPeer* peer) {
778 if (!peer->params()->audio_config) {
779 return;
780 }
Artem Titov3481db22019-02-28 13:13:15 +0100781 const AudioConfig& audio_config = peer->params()->audio_config.value();
Artem Titova6a273d2019-02-07 16:43:51 +0100782 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Artem Titov3481db22019-02-28 13:13:15 +0100783 peer->pc_factory()->CreateAudioSource(audio_config.audio_options);
Artem Titova6a273d2019-02-07 16:43:51 +0100784 rtc::scoped_refptr<AudioTrackInterface> track =
Artem Titov3481db22019-02-28 13:13:15 +0100785 peer->pc_factory()->CreateAudioTrack(*audio_config.stream_label, source);
786 peer->AddTrack(track, {*audio_config.stream_label});
Artem Titova6a273d2019-02-07 16:43:51 +0100787}
788
Artem Titovf65a89b2019-05-07 11:56:44 +0200789void PeerConnectionE2EQualityTest::SetPeerCodecPreferences(
790 TestPeer* peer,
791 const RunParams& run_params) {
Artem Titovef3fd9c2019-06-13 16:36:52 +0200792 std::vector<RtpCodecCapability> with_rtx_video_capabilities =
Artem Titov7f2a67f2019-06-13 19:45:55 +0200793 FilterVideoCodecCapabilities(
Artem Titovef3fd9c2019-06-13 16:36:52 +0200794 run_params.video_codec_name, run_params.video_codec_required_params,
795 true, run_params.use_ulp_fec, run_params.use_flex_fec,
796 peer->pc_factory()
797 ->GetRtpSenderCapabilities(cricket::MediaType::MEDIA_TYPE_VIDEO)
798 .codecs);
799 std::vector<RtpCodecCapability> without_rtx_video_capabilities =
Artem Titov7f2a67f2019-06-13 19:45:55 +0200800 FilterVideoCodecCapabilities(
Artem Titovef3fd9c2019-06-13 16:36:52 +0200801 run_params.video_codec_name, run_params.video_codec_required_params,
802 false, run_params.use_ulp_fec, run_params.use_flex_fec,
803 peer->pc_factory()
804 ->GetRtpSenderCapabilities(cricket::MediaType::MEDIA_TYPE_VIDEO)
805 .codecs);
Artem Titovf65a89b2019-05-07 11:56:44 +0200806
807 // Set codecs for transceivers
808 for (auto transceiver : peer->pc()->GetTransceivers()) {
809 if (transceiver->media_type() == cricket::MediaType::MEDIA_TYPE_VIDEO) {
Artem Titovef3fd9c2019-06-13 16:36:52 +0200810 if (transceiver->sender()->init_send_encodings().size() > 1) {
811 // If transceiver's sender has more then 1 send encodings, it means it
812 // has multiple simulcast streams, so we need disable RTX on it.
Artem Titov7f2a67f2019-06-13 19:45:55 +0200813 RTCError result =
814 transceiver->SetCodecPreferences(without_rtx_video_capabilities);
815 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200816 } else {
Artem Titov7f2a67f2019-06-13 19:45:55 +0200817 RTCError result =
818 transceiver->SetCodecPreferences(with_rtx_video_capabilities);
819 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200820 }
Artem Titovf65a89b2019-05-07 11:56:44 +0200821 }
822 }
823}
824
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100825void PeerConnectionE2EQualityTest::SetupCall() {
Artem Titov594597c2019-07-18 13:39:41 +0200826 std::map<std::string, VideoSimulcastConfig> stream_label_to_simulcast_config;
827 // We add only Alice here, because simulcast/svc is supported only from the
828 // first peer.
829 for (auto& video_config : alice_->params()->video_configs) {
830 if (video_config.simulcast_config) {
831 stream_label_to_simulcast_config.insert(
832 {*video_config.stream_label, *video_config.simulcast_config});
833 }
834 }
835 PatchingParams patching_params(stream_label_to_simulcast_config);
836 SignalingInterceptor signaling_interceptor(patching_params);
Artem Titova6a273d2019-02-07 16:43:51 +0100837 // Connect peers.
Artem Titovef3fd9c2019-06-13 16:36:52 +0200838 ExchangeOfferAnswer(&signaling_interceptor);
Artem Titova6a273d2019-02-07 16:43:51 +0100839 // Do the SDP negotiation, and also exchange ice candidates.
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100840 ASSERT_EQ_WAIT(alice_->signaling_state(), PeerConnectionInterface::kStable,
Artem Titova6a273d2019-02-07 16:43:51 +0100841 kDefaultTimeoutMs);
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100842 ASSERT_TRUE_WAIT(alice_->IsIceGatheringDone(), kDefaultTimeoutMs);
843 ASSERT_TRUE_WAIT(bob_->IsIceGatheringDone(), kDefaultTimeoutMs);
Artem Titova6a273d2019-02-07 16:43:51 +0100844
Artem Titovef3fd9c2019-06-13 16:36:52 +0200845 ExchangeIceCandidates(&signaling_interceptor);
Artem Titova6a273d2019-02-07 16:43:51 +0100846 // This means that ICE and DTLS are connected.
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100847 ASSERT_TRUE_WAIT(bob_->IsIceConnected(), kDefaultTimeoutMs);
848 ASSERT_TRUE_WAIT(alice_->IsIceConnected(), kDefaultTimeoutMs);
Artem Titova6a273d2019-02-07 16:43:51 +0100849}
850
Artem Titovef3fd9c2019-06-13 16:36:52 +0200851void PeerConnectionE2EQualityTest::ExchangeOfferAnswer(
852 SignalingInterceptor* signaling_interceptor) {
853 std::string log_output;
854
855 auto offer = alice_->CreateOffer();
856 RTC_CHECK(offer);
857 offer->ToString(&log_output);
858 RTC_LOG(INFO) << "Original offer: " << log_output;
859 LocalAndRemoteSdp patch_result =
860 signaling_interceptor->PatchOffer(std::move(offer));
861 patch_result.local_sdp->ToString(&log_output);
862 RTC_LOG(INFO) << "Offer to set as local description: " << log_output;
863 patch_result.remote_sdp->ToString(&log_output);
864 RTC_LOG(INFO) << "Offer to set as remote description: " << log_output;
865
866 bool set_local_offer =
867 alice_->SetLocalDescription(std::move(patch_result.local_sdp));
868 RTC_CHECK(set_local_offer);
869 bool set_remote_offer =
870 bob_->SetRemoteDescription(std::move(patch_result.remote_sdp));
871 RTC_CHECK(set_remote_offer);
872 auto answer = bob_->CreateAnswer();
873 RTC_CHECK(answer);
874 answer->ToString(&log_output);
875 RTC_LOG(INFO) << "Original answer: " << log_output;
876 patch_result = signaling_interceptor->PatchAnswer(std::move(answer));
877 patch_result.local_sdp->ToString(&log_output);
878 RTC_LOG(INFO) << "Answer to set as local description: " << log_output;
879 patch_result.remote_sdp->ToString(&log_output);
880 RTC_LOG(INFO) << "Answer to set as remote description: " << log_output;
881
882 bool set_local_answer =
883 bob_->SetLocalDescription(std::move(patch_result.local_sdp));
884 RTC_CHECK(set_local_answer);
885 bool set_remote_answer =
886 alice_->SetRemoteDescription(std::move(patch_result.remote_sdp));
887 RTC_CHECK(set_remote_answer);
888}
889
890void PeerConnectionE2EQualityTest::ExchangeIceCandidates(
891 SignalingInterceptor* signaling_interceptor) {
892 // Connect an ICE candidate pairs.
893 std::vector<std::unique_ptr<IceCandidateInterface>> alice_candidates =
894 signaling_interceptor->PatchOffererIceCandidates(
895 alice_->observer()->GetAllCandidates());
896 for (auto& candidate : alice_candidates) {
897 std::string candidate_str;
898 RTC_CHECK(candidate->ToString(&candidate_str));
899 RTC_LOG(INFO) << "Alice ICE candidate(mid= " << candidate->sdp_mid()
900 << "): " << candidate_str;
901 }
902 ASSERT_TRUE(bob_->AddIceCandidates(std::move(alice_candidates)));
903 std::vector<std::unique_ptr<IceCandidateInterface>> bob_candidates =
904 signaling_interceptor->PatchAnswererIceCandidates(
905 bob_->observer()->GetAllCandidates());
906 for (auto& candidate : bob_candidates) {
907 std::string candidate_str;
908 RTC_CHECK(candidate->ToString(&candidate_str));
909 RTC_LOG(INFO) << "Bob ICE candidate(mid= " << candidate->sdp_mid()
910 << "): " << candidate_str;
911 }
912 ASSERT_TRUE(alice_->AddIceCandidates(std::move(bob_candidates)));
913}
914
Artem Titovbf9e01a2019-02-14 10:51:27 +0100915void PeerConnectionE2EQualityTest::StartVideo(
916 const std::vector<
917 rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource>>& sources) {
918 for (auto& source : sources) {
919 if (source->state() != MediaSourceInterface::SourceState::kLive) {
920 source->Start();
Artem Titova6a273d2019-02-07 16:43:51 +0100921 }
Artem Titova6a273d2019-02-07 16:43:51 +0100922 }
923}
924
925void PeerConnectionE2EQualityTest::TearDownCall() {
Artem Titovbf9e01a2019-02-14 10:51:27 +0100926 for (const auto& video_source : alice_video_sources_) {
927 video_source->Stop();
928 }
929 for (const auto& video_source : bob_video_sources_) {
Artem Titova6a273d2019-02-07 16:43:51 +0100930 video_source->Stop();
931 }
932
933 alice_->pc()->Close();
934 bob_->pc()->Close();
935
936 for (const auto& video_writer : video_writers_) {
937 video_writer->Close();
938 }
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100939
Artem Titovbf9e01a2019-02-14 10:51:27 +0100940 alice_video_sources_.clear();
941 bob_video_sources_.clear();
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100942 video_writers_.clear();
943 alice_.reset();
944 bob_.reset();
Artem Titova6a273d2019-02-07 16:43:51 +0100945}
946
Artem Titov0b443142019-03-20 11:11:08 +0100947test::VideoFrameWriter* PeerConnectionE2EQualityTest::MaybeCreateVideoWriter(
Artem Titova6a273d2019-02-07 16:43:51 +0100948 absl::optional<std::string> file_name,
949 const VideoConfig& config) {
950 if (!file_name) {
951 return nullptr;
952 }
Artem Titovef3fd9c2019-06-13 16:36:52 +0200953 // TODO(titovartem) create only one file writer for simulcast video track.
Artem Titov0b443142019-03-20 11:11:08 +0100954 auto video_writer = absl::make_unique<test::VideoFrameWriter>(
Artem Titova6a273d2019-02-07 16:43:51 +0100955 file_name.value(), config.width, config.height, config.fps);
Artem Titov0b443142019-03-20 11:11:08 +0100956 test::VideoFrameWriter* out = video_writer.get();
Artem Titova6a273d2019-02-07 16:43:51 +0100957 video_writers_.push_back(std::move(video_writer));
958 return out;
959}
960
Artem Titovba82e002019-03-15 15:57:53 +0100961Timestamp PeerConnectionE2EQualityTest::Now() const {
Sebastian Janssonb64ad0e2019-06-19 09:39:34 +0200962 return clock_->CurrentTime();
Artem Titovba82e002019-03-15 15:57:53 +0100963}
964
965PeerConnectionE2EQualityTest::ScheduledActivity::ScheduledActivity(
966 TimeDelta initial_delay_since_start,
967 absl::optional<TimeDelta> interval,
968 std::function<void(TimeDelta)> func)
969 : initial_delay_since_start(initial_delay_since_start),
970 interval(std::move(interval)),
971 func(std::move(func)) {}
972
Artem Titov0b443142019-03-20 11:11:08 +0100973} // namespace webrtc_pc_e2e
Artem Titova6a273d2019-02-07 16:43:51 +0100974} // namespace webrtc