blob: 944790e8f92d255e4abefc9dc69f10763f551ccc [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 Titov23702422019-05-27 15:28:30 +020058// Field trials to enable Flex FEC advertising and receiving.
59constexpr char kFlexFecEnabledFieldTrials[] =
60 "WebRTC-FlexFEC-03-Advertised/Enabled/WebRTC-FlexFEC-03/Enabled/";
61
Artem Titova6a273d2019-02-07 16:43:51 +010062std::string VideoConfigSourcePresenceToString(const VideoConfig& video_config) {
63 char buf[1024];
64 rtc::SimpleStringBuilder builder(buf);
65 builder << "video_config.generator=" << video_config.generator.has_value()
66 << "; video_config.input_file_name="
67 << video_config.input_file_name.has_value()
68 << "; video_config.screen_share_config="
69 << video_config.screen_share_config.has_value() << ";";
70 return builder.str();
71}
72
Artem Titovbf9e01a2019-02-14 10:51:27 +010073class FixturePeerConnectionObserver : public MockPeerConnectionObserver {
74 public:
75 // |on_track_callback| will be called when any new track will be added to peer
76 // connection.
77 // |on_connected_callback| will be called when peer connection will come to
78 // either connected or completed state. Client should notice that in the case
79 // of reconnect this callback can be called again, so it should be tolerant
80 // to such behavior.
81 FixturePeerConnectionObserver(
82 std::function<void(rtc::scoped_refptr<RtpTransceiverInterface>)>
83 on_track_callback,
84 std::function<void()> on_connected_callback)
85 : on_track_callback_(std::move(on_track_callback)),
86 on_connected_callback_(std::move(on_connected_callback)) {}
87
88 void OnTrack(
89 rtc::scoped_refptr<RtpTransceiverInterface> transceiver) override {
90 MockPeerConnectionObserver::OnTrack(transceiver);
91 on_track_callback_(transceiver);
92 }
93
94 void OnIceConnectionChange(
95 PeerConnectionInterface::IceConnectionState new_state) override {
96 MockPeerConnectionObserver::OnIceConnectionChange(new_state);
97 if (ice_connected_) {
98 on_connected_callback_();
99 }
100 }
101
102 private:
103 std::function<void(rtc::scoped_refptr<RtpTransceiverInterface>)>
104 on_track_callback_;
105 std::function<void()> on_connected_callback_;
106};
107
Artem Titova6a273d2019-02-07 16:43:51 +0100108} // namespace
109
110PeerConnectionE2EQualityTest::PeerConnectionE2EQualityTest(
Artem Titov59835852019-02-27 17:44:13 +0100111 std::string test_case_name,
Mirko Bonadeif5d88082019-02-20 16:16:54 +0100112 std::unique_ptr<AudioQualityAnalyzerInterface> audio_quality_analyzer,
113 std::unique_ptr<VideoQualityAnalyzerInterface> video_quality_analyzer)
Artem Titov59835852019-02-27 17:44:13 +0100114 : clock_(Clock::GetRealTimeClock()),
Danil Chapovalovd0e0ed82019-04-18 14:48:24 +0200115 task_queue_factory_(CreateDefaultTaskQueueFactory()),
Artem Titovba82e002019-03-15 15:57:53 +0100116 test_case_name_(std::move(test_case_name)) {
Artem Titova6a273d2019-02-07 16:43:51 +0100117 // Create default video quality analyzer. We will always create an analyzer,
118 // even if there are no video streams, because it will be installed into video
119 // encoder/decoder factories.
Mirko Bonadeif5d88082019-02-20 16:16:54 +0100120 if (video_quality_analyzer == nullptr) {
Artem Titov859abef2019-03-01 11:11:09 +0100121 video_quality_analyzer = absl::make_unique<DefaultVideoQualityAnalyzer>();
Artem Titova6a273d2019-02-07 16:43:51 +0100122 }
123 encoded_image_id_controller_ =
Artem Titov32232e92019-02-20 21:13:14 +0100124 absl::make_unique<SingleProcessEncodedImageDataInjector>();
Artem Titova6a273d2019-02-07 16:43:51 +0100125 video_quality_analyzer_injection_helper_ =
126 absl::make_unique<VideoQualityAnalyzerInjectionHelper>(
Mirko Bonadeif5d88082019-02-20 16:16:54 +0100127 std::move(video_quality_analyzer), encoded_image_id_controller_.get(),
Artem Titova6a273d2019-02-07 16:43:51 +0100128 encoded_image_id_controller_.get());
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100129
130 if (audio_quality_analyzer == nullptr) {
131 audio_quality_analyzer = absl::make_unique<DefaultAudioQualityAnalyzer>();
132 }
133 audio_quality_analyzer_.swap(audio_quality_analyzer);
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100134}
135
Artem Titovba82e002019-03-15 15:57:53 +0100136void PeerConnectionE2EQualityTest::ExecuteAt(
137 TimeDelta target_time_since_start,
138 std::function<void(TimeDelta)> func) {
139 ExecuteTask(target_time_since_start, absl::nullopt, func);
140}
141
142void PeerConnectionE2EQualityTest::ExecuteEvery(
143 TimeDelta initial_delay_since_start,
144 TimeDelta interval,
145 std::function<void(TimeDelta)> func) {
146 ExecuteTask(initial_delay_since_start, interval, func);
147}
148
149void PeerConnectionE2EQualityTest::ExecuteTask(
150 TimeDelta initial_delay_since_start,
151 absl::optional<TimeDelta> interval,
152 std::function<void(TimeDelta)> func) {
153 RTC_CHECK(initial_delay_since_start.IsFinite() &&
154 initial_delay_since_start >= TimeDelta::Zero());
155 RTC_CHECK(!interval ||
156 (interval->IsFinite() && *interval > TimeDelta::Zero()));
157 rtc::CritScope crit(&lock_);
158 ScheduledActivity activity(initial_delay_since_start, interval, func);
159 if (start_time_.IsInfinite()) {
160 scheduled_activities_.push(std::move(activity));
161 } else {
162 PostTask(std::move(activity));
163 }
164}
165
166void PeerConnectionE2EQualityTest::PostTask(ScheduledActivity activity) {
167 // Because start_time_ will never change at this point copy it to local
168 // variable to capture in in lambda without requirement to hold a lock.
169 Timestamp start_time = start_time_;
170
171 TimeDelta remaining_delay =
172 activity.initial_delay_since_start == TimeDelta::Zero()
173 ? TimeDelta::Zero()
174 : activity.initial_delay_since_start - (Now() - start_time_);
175 if (remaining_delay < TimeDelta::Zero()) {
176 RTC_LOG(WARNING) << "Executing late task immediately, late by="
177 << ToString(remaining_delay.Abs());
178 remaining_delay = TimeDelta::Zero();
179 }
180
181 if (activity.interval) {
182 if (remaining_delay == TimeDelta::Zero()) {
183 repeating_task_handles_.push_back(RepeatingTaskHandle::Start(
184 task_queue_->Get(), [activity, start_time, this]() {
185 activity.func(Now() - start_time);
186 return *activity.interval;
187 }));
188 return;
189 }
190 repeating_task_handles_.push_back(RepeatingTaskHandle::DelayedStart(
191 task_queue_->Get(), remaining_delay, [activity, start_time, this]() {
192 activity.func(Now() - start_time);
193 return *activity.interval;
194 }));
195 return;
196 }
197
198 if (remaining_delay == TimeDelta::Zero()) {
199 task_queue_->PostTask(
200 [activity, start_time, this]() { activity.func(Now() - start_time); });
201 return;
202 }
203
204 task_queue_->PostDelayedTask(
205 [activity, start_time, this]() { activity.func(Now() - start_time); },
206 remaining_delay.ms());
207}
208
Artem Titov18459222019-04-24 11:09:35 +0200209void PeerConnectionE2EQualityTest::AddQualityMetricsReporter(
210 std::unique_ptr<QualityMetricsReporter> quality_metrics_reporter) {
211 quality_metrics_reporters_.push_back(std::move(quality_metrics_reporter));
212}
213
Artem Titovd09bc552019-03-20 11:18:58 +0100214void PeerConnectionE2EQualityTest::AddPeer(
215 rtc::Thread* network_thread,
216 rtc::NetworkManager* network_manager,
217 rtc::FunctionView<void(PeerConfigurer*)> configurer) {
218 peer_configurations_.push_back(
219 absl::make_unique<PeerConfigurerImpl>(network_thread, network_manager));
220 configurer(peer_configurations_.back().get());
221}
222
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100223void PeerConnectionE2EQualityTest::Run(
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100224 RunParams run_params) {
Artem Titovd09bc552019-03-20 11:18:58 +0100225 RTC_CHECK_EQ(peer_configurations_.size(), 2)
226 << "Only peer to peer calls are allowed, please add 2 peers";
227
228 std::unique_ptr<Params> alice_params =
229 peer_configurations_[0]->ReleaseParams();
230 std::unique_ptr<InjectableComponents> alice_components =
231 peer_configurations_[0]->ReleaseComponents();
232 std::unique_ptr<Params> bob_params = peer_configurations_[1]->ReleaseParams();
233 std::unique_ptr<InjectableComponents> bob_components =
234 peer_configurations_[1]->ReleaseComponents();
235 peer_configurations_.clear();
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100236
Artem Titov9a7e7212019-02-28 16:34:17 +0100237 SetDefaultValuesForMissingParams({alice_params.get(), bob_params.get()});
Artem Titovade945d2019-04-02 18:31:48 +0200238 ValidateParams(run_params, {alice_params.get(), bob_params.get()});
Artem Titov23702422019-05-27 15:28:30 +0200239 SetupRequiredFieldTrials(run_params);
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100240
241 // Print test summary
242 RTC_LOG(INFO)
243 << "Media quality test: Alice will make a call to Bob with media video="
244 << !alice_params->video_configs.empty()
245 << "; audio=" << alice_params->audio_config.has_value()
246 << ". Bob will respond with media video="
247 << !bob_params->video_configs.empty()
248 << "; audio=" << bob_params->audio_config.has_value();
249
250 const std::unique_ptr<rtc::Thread> signaling_thread = rtc::Thread::Create();
251 signaling_thread->SetName(kSignalThreadName, nullptr);
252 signaling_thread->Start();
Artem Titova6a273d2019-02-07 16:43:51 +0100253
Artem Titov70f80e52019-04-12 13:13:39 +0200254 // Create a |task_queue_|.
255 task_queue_ = absl::make_unique<TaskQueueForTest>("pc_e2e_quality_test");
256
Artem Titova6a273d2019-02-07 16:43:51 +0100257 // Create call participants: Alice and Bob.
258 // Audio streams are intercepted in AudioDeviceModule, so if it is required to
259 // catch output of Alice's stream, Alice's output_dump_file_name should be
260 // passed to Bob's TestPeer setup as audio output file name.
261 absl::optional<std::string> alice_audio_output_dump_file_name =
262 bob_params->audio_config ? bob_params->audio_config->output_dump_file_name
263 : absl::nullopt;
264 absl::optional<std::string> bob_audio_output_dump_file_name =
265 alice_params->audio_config
266 ? alice_params->audio_config->output_dump_file_name
267 : absl::nullopt;
Artem Titovbf9e01a2019-02-14 10:51:27 +0100268 // Copy Alice and Bob video configs to correctly pass them into lambdas.
269 std::vector<VideoConfig> alice_video_configs = alice_params->video_configs;
270 std::vector<VideoConfig> bob_video_configs = bob_params->video_configs;
271
Artem Titova6a273d2019-02-07 16:43:51 +0100272 alice_ = TestPeer::CreateTestPeer(
273 std::move(alice_components), std::move(alice_params),
Artem Titovbf9e01a2019-02-14 10:51:27 +0100274 absl::make_unique<FixturePeerConnectionObserver>(
275 [this, bob_video_configs](
276 rtc::scoped_refptr<RtpTransceiverInterface> transceiver) {
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200277 OnTrackCallback(transceiver, bob_video_configs);
Artem Titovbf9e01a2019-02-14 10:51:27 +0100278 },
279 [this]() { StartVideo(alice_video_sources_); }),
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100280 video_quality_analyzer_injection_helper_.get(), signaling_thread.get(),
Artem Titovade945d2019-04-02 18:31:48 +0200281 alice_audio_output_dump_file_name,
Artem Titov70f80e52019-04-12 13:13:39 +0200282 run_params.video_encoder_bitrate_multiplier, task_queue_.get());
Artem Titova6a273d2019-02-07 16:43:51 +0100283 bob_ = TestPeer::CreateTestPeer(
284 std::move(bob_components), std::move(bob_params),
Artem Titovbf9e01a2019-02-14 10:51:27 +0100285 absl::make_unique<FixturePeerConnectionObserver>(
286 [this, alice_video_configs](
287 rtc::scoped_refptr<RtpTransceiverInterface> transceiver) {
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200288 OnTrackCallback(transceiver, alice_video_configs);
Artem Titovbf9e01a2019-02-14 10:51:27 +0100289 },
290 [this]() { StartVideo(bob_video_sources_); }),
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100291 video_quality_analyzer_injection_helper_.get(), signaling_thread.get(),
Artem Titovade945d2019-04-02 18:31:48 +0200292 bob_audio_output_dump_file_name,
Artem Titov70f80e52019-04-12 13:13:39 +0200293 run_params.video_encoder_bitrate_multiplier, task_queue_.get());
Artem Titov9f97c9a2019-02-08 00:35:13 +0100294
295 int num_cores = CpuInfo::DetectNumberOfCores();
296 RTC_DCHECK_GE(num_cores, 1);
297
298 int video_analyzer_threads =
299 num_cores - kPeerConnectionUsedThreads - kFrameworkUsedThreads;
300 if (video_analyzer_threads <= 0) {
301 video_analyzer_threads = 1;
302 }
303 video_analyzer_threads =
304 std::min(video_analyzer_threads, kMaxVideoAnalyzerThreads);
305 RTC_LOG(INFO) << "video_analyzer_threads=" << video_analyzer_threads;
306
Artem Titov59835852019-02-27 17:44:13 +0100307 video_quality_analyzer_injection_helper_->Start(test_case_name_,
308 video_analyzer_threads);
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200309 audio_quality_analyzer_->Start(test_case_name_, &analyzer_helper_);
Artem Titov18459222019-04-24 11:09:35 +0200310 for (auto& reporter : quality_metrics_reporters_) {
311 reporter->Start(test_case_name_);
312 }
Mirko Bonadeice7a4fb2019-02-25 11:45:07 +0100313
314 // Start RTCEventLog recording if requested.
315 if (alice_->params()->rtc_event_log_path) {
316 auto alice_rtc_event_log = absl::make_unique<webrtc::RtcEventLogOutputFile>(
317 alice_->params()->rtc_event_log_path.value());
318 alice_->pc()->StartRtcEventLog(std::move(alice_rtc_event_log),
319 webrtc::RtcEventLog::kImmediateOutput);
320 }
321 if (bob_->params()->rtc_event_log_path) {
322 auto bob_rtc_event_log = absl::make_unique<webrtc::RtcEventLogOutputFile>(
323 bob_->params()->rtc_event_log_path.value());
324 bob_->pc()->StartRtcEventLog(std::move(bob_rtc_event_log),
325 webrtc::RtcEventLog::kImmediateOutput);
326 }
327
Artem Titov4d29ef02019-05-20 10:43:16 +0200328 // Setup alive logging. It is done to prevent test infra to think that test is
329 // dead.
330 RepeatingTaskHandle::DelayedStart(task_queue_->Get(),
331 kAliveMessageLogInterval, []() {
332 std::printf("Test is still running...\n");
333 return kAliveMessageLogInterval;
334 });
335
Artem Titovba82e002019-03-15 15:57:53 +0100336 // Setup call.
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100337 signaling_thread->Invoke<void>(
Artem Titova6a273d2019-02-07 16:43:51 +0100338 RTC_FROM_HERE,
Artem Titovf65a89b2019-05-07 11:56:44 +0200339 rtc::Bind(&PeerConnectionE2EQualityTest::SetupCallOnSignalingThread, this,
340 run_params));
Artem Titovba82e002019-03-15 15:57:53 +0100341 {
342 rtc::CritScope crit(&lock_);
343 start_time_ = Now();
344 while (!scheduled_activities_.empty()) {
345 PostTask(std::move(scheduled_activities_.front()));
346 scheduled_activities_.pop();
347 }
348 }
Mirko Bonadei71318802019-02-18 18:52:43 +0100349
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100350 StatsPoller stats_poller({audio_quality_analyzer_.get(),
351 video_quality_analyzer_injection_helper_.get()},
352 {{"alice", alice_.get()}, {"bob", bob_.get()}});
353
Artem Titovba82e002019-03-15 15:57:53 +0100354 task_queue_->PostTask([&stats_poller, this]() {
355 RTC_DCHECK_RUN_ON(task_queue_.get());
Sebastian Janssoncda86dd2019-03-11 17:26:36 +0100356 stats_polling_task_ =
Artem Titovba82e002019-03-15 15:57:53 +0100357 RepeatingTaskHandle::Start(task_queue_->Get(), [this, &stats_poller]() {
358 RTC_DCHECK_RUN_ON(task_queue_.get());
Sebastian Janssoncda86dd2019-03-11 17:26:36 +0100359 stats_poller.PollStatsAndNotifyObservers();
360 return kStatsUpdateInterval;
361 });
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100362 });
363
Mirko Bonadei71318802019-02-18 18:52:43 +0100364 rtc::Event done;
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100365 done.Wait(run_params.run_duration.ms());
366
367 rtc::Event stats_polling_stopped;
Artem Titovba82e002019-03-15 15:57:53 +0100368 task_queue_->PostTask([&stats_polling_stopped, this]() {
369 RTC_DCHECK_RUN_ON(task_queue_.get());
Mirko Bonadei12ae4f42019-02-26 15:19:07 +0100370 stats_polling_task_.Stop();
371 stats_polling_stopped.Set();
372 });
373 bool no_timeout = stats_polling_stopped.Wait(kStatsPollingStopTimeout.ms());
374 RTC_CHECK(no_timeout) << "Failed to stop Stats polling after "
375 << kStatsPollingStopTimeout.seconds() << " seconds.";
Mirko Bonadei71318802019-02-18 18:52:43 +0100376
Artem Titov70f80e52019-04-12 13:13:39 +0200377 // We need to detach AEC dumping from peers, because dump uses |task_queue_|
378 // inside.
379 alice_->DetachAecDump();
380 bob_->DetachAecDump();
Artem Titov4d29ef02019-05-20 10:43:16 +0200381 // Stop all client started tasks on task queue to prevent their access to any
382 // call related objects after these objects will be destroyed during call tear
383 // down.
384 task_queue_->SendTask([this]() {
385 rtc::CritScope crit(&lock_);
386 for (auto& handle : repeating_task_handles_) {
387 handle.Stop();
388 }
389 });
Artem Titovba82e002019-03-15 15:57:53 +0100390 // Tear down the call.
Mirko Bonadei71318802019-02-18 18:52:43 +0100391 signaling_thread->Invoke<void>(
392 RTC_FROM_HERE,
393 rtc::Bind(&PeerConnectionE2EQualityTest::TearDownCallOnSignalingThread,
394 this));
Artem Titovb93c4e62019-05-02 10:52:07 +0200395 Timestamp end_time = Now();
396 {
397 rtc::CritScope crit(&lock_);
398 real_test_duration_ = end_time - start_time_;
399 }
Mirko Bonadei71318802019-02-18 18:52:43 +0100400
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200401 audio_quality_analyzer_->Stop();
Artem Titov9f97c9a2019-02-08 00:35:13 +0100402 video_quality_analyzer_injection_helper_->Stop();
Artem Titov18459222019-04-24 11:09:35 +0200403 for (auto& reporter : quality_metrics_reporters_) {
404 reporter->StopAndReportResults();
405 }
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100406
Artem Titov4d29ef02019-05-20 10:43:16 +0200407 // Reset |task_queue_| after test to cleanup.
408 task_queue_.reset();
409
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100410 // Ensuring that TestPeers have been destroyed in order to correctly close
411 // Audio dumps.
412 RTC_CHECK(!alice_);
413 RTC_CHECK(!bob_);
414 // Ensuring that FrameGeneratorCapturerVideoTrackSource and VideoFrameWriter
415 // are destroyed on the right thread.
Artem Titovbf9e01a2019-02-14 10:51:27 +0100416 RTC_CHECK(alice_video_sources_.empty());
417 RTC_CHECK(bob_video_sources_.empty());
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100418 RTC_CHECK(video_writers_.empty());
Artem Titova6a273d2019-02-07 16:43:51 +0100419}
420
Artem Titov9a7e7212019-02-28 16:34:17 +0100421void PeerConnectionE2EQualityTest::SetDefaultValuesForMissingParams(
Artem Titova6a273d2019-02-07 16:43:51 +0100422 std::vector<Params*> params) {
Artem Titov3481db22019-02-28 13:13:15 +0100423 int video_counter = 0;
424 int audio_counter = 0;
Artem Titova6a273d2019-02-07 16:43:51 +0100425 std::set<std::string> video_labels;
Artem Titov3481db22019-02-28 13:13:15 +0100426 std::set<std::string> audio_labels;
Artem Titova6a273d2019-02-07 16:43:51 +0100427 for (auto* p : params) {
428 for (auto& video_config : p->video_configs) {
Artem Titov9a7e7212019-02-28 16:34:17 +0100429 if (!video_config.generator && !video_config.input_file_name &&
430 !video_config.screen_share_config) {
431 video_config.generator = VideoGeneratorType::kDefault;
432 }
Artem Titova6a273d2019-02-07 16:43:51 +0100433 if (!video_config.stream_label) {
434 std::string label;
435 do {
Artem Titov3481db22019-02-28 13:13:15 +0100436 label = "_auto_video_stream_label_" + std::to_string(video_counter);
437 ++video_counter;
Artem Titova6a273d2019-02-07 16:43:51 +0100438 } while (!video_labels.insert(label).second);
439 video_config.stream_label = label;
440 }
441 }
Artem Titov3481db22019-02-28 13:13:15 +0100442 if (p->audio_config) {
443 if (!p->audio_config->stream_label) {
444 std::string label;
445 do {
446 label = "_auto_audio_stream_label_" + std::to_string(audio_counter);
447 ++audio_counter;
448 } while (!audio_labels.insert(label).second);
449 p->audio_config->stream_label = label;
450 }
451 }
Artem Titova6a273d2019-02-07 16:43:51 +0100452 }
453}
454
Artem Titovade945d2019-04-02 18:31:48 +0200455void PeerConnectionE2EQualityTest::ValidateParams(const RunParams& run_params,
456 std::vector<Params*> params) {
457 RTC_CHECK_GT(run_params.video_encoder_bitrate_multiplier, 0.0);
458
Artem Titova6a273d2019-02-07 16:43:51 +0100459 std::set<std::string> video_labels;
Artem Titov3481db22019-02-28 13:13:15 +0100460 std::set<std::string> audio_labels;
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100461 int media_streams_count = 0;
462
Artem Titovef3fd9c2019-06-13 16:36:52 +0200463 for (size_t i = 0; i < params.size(); ++i) {
464 Params* p = params[i];
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100465 if (p->audio_config) {
466 media_streams_count++;
467 }
468 media_streams_count += p->video_configs.size();
469
Artem Titova6a273d2019-02-07 16:43:51 +0100470 // Validate that each video config has exactly one of |generator|,
471 // |input_file_name| or |screen_share_config| set. Also validate that all
472 // video stream labels are unique.
473 for (auto& video_config : p->video_configs) {
474 RTC_CHECK(video_config.stream_label);
475 bool inserted =
476 video_labels.insert(video_config.stream_label.value()).second;
477 RTC_CHECK(inserted) << "Duplicate video_config.stream_label="
478 << video_config.stream_label.value();
479 RTC_CHECK(video_config.generator || video_config.input_file_name ||
480 video_config.screen_share_config)
481 << VideoConfigSourcePresenceToString(video_config);
482 RTC_CHECK(!(video_config.input_file_name && video_config.generator))
483 << VideoConfigSourcePresenceToString(video_config);
484 RTC_CHECK(
485 !(video_config.input_file_name && video_config.screen_share_config))
486 << VideoConfigSourcePresenceToString(video_config);
487 RTC_CHECK(!(video_config.screen_share_config && video_config.generator))
488 << VideoConfigSourcePresenceToString(video_config);
Artem Titov7581ff72019-05-15 15:45:33 +0200489
490 if (video_config.screen_share_config) {
491 if (video_config.screen_share_config->slides_yuv_file_names.empty()) {
492 if (video_config.screen_share_config->scrolling_params) {
493 // If we have scrolling params, then its |source_width| and
494 // |source_heigh| will be used as width and height of video input,
495 // so we have to validate it against width and height of default
496 // input.
497 RTC_CHECK_EQ(video_config.screen_share_config->scrolling_params
498 ->source_width,
499 kDefaultSlidesWidth);
500 RTC_CHECK_EQ(video_config.screen_share_config->scrolling_params
501 ->source_height,
502 kDefaultSlidesHeight);
503 } else {
504 RTC_CHECK_EQ(video_config.width, kDefaultSlidesWidth);
505 RTC_CHECK_EQ(video_config.height, kDefaultSlidesHeight);
506 }
507 }
508 if (video_config.screen_share_config->scrolling_params) {
509 RTC_CHECK_LE(
510 video_config.screen_share_config->scrolling_params->duration,
511 video_config.screen_share_config->slide_change_interval);
512 RTC_CHECK_GE(
513 video_config.screen_share_config->scrolling_params->source_width,
514 video_config.width);
515 RTC_CHECK_GE(
516 video_config.screen_share_config->scrolling_params->source_height,
517 video_config.height);
518 }
519 }
Artem Titovef3fd9c2019-06-13 16:36:52 +0200520 if (video_config.simulcast_config) {
521 // We support simulcast only for Vp8 for now.
522 // RTC_CHECK_EQ(run_params.video_codec_name, cricket::kVp8CodecName);
523 // Also we support simulcast only from caller.
524 RTC_CHECK_EQ(i, 0)
525 << "Only simulcast stream from first peer is supported";
526 }
Artem Titova6a273d2019-02-07 16:43:51 +0100527 }
528 if (p->audio_config) {
Artem Titov3481db22019-02-28 13:13:15 +0100529 bool inserted =
530 audio_labels.insert(p->audio_config->stream_label.value()).second;
531 RTC_CHECK(inserted) << "Duplicate audio_config.stream_label="
532 << p->audio_config->stream_label.value();
Artem Titova6a273d2019-02-07 16:43:51 +0100533 // Check that if mode input file name specified only if mode is kFile.
534 if (p->audio_config.value().mode == AudioConfig::Mode::kGenerated) {
535 RTC_CHECK(!p->audio_config.value().input_file_name);
536 }
537 if (p->audio_config.value().mode == AudioConfig::Mode::kFile) {
538 RTC_CHECK(p->audio_config.value().input_file_name);
Artem Titov0b443142019-03-20 11:11:08 +0100539 RTC_CHECK(
Artem Titov70f80e52019-04-12 13:13:39 +0200540 test::FileExists(p->audio_config.value().input_file_name.value()))
541 << p->audio_config.value().input_file_name.value()
542 << " doesn't exist";
Artem Titova6a273d2019-02-07 16:43:51 +0100543 }
544 }
545 }
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100546
547 RTC_CHECK_GT(media_streams_count, 0) << "No media in the call.";
Artem Titova6a273d2019-02-07 16:43:51 +0100548}
549
Artem Titov23702422019-05-27 15:28:30 +0200550void PeerConnectionE2EQualityTest::SetupRequiredFieldTrials(
551 const RunParams& run_params) {
552 std::string field_trials = "";
553 if (run_params.use_flex_fec) {
554 field_trials += kFlexFecEnabledFieldTrials;
555 }
556 if (!field_trials.empty()) {
557 override_field_trials_ = absl::make_unique<test::ScopedFieldTrials>(
558 field_trial::GetFieldTrialString() + field_trials);
559 }
560}
561
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200562void PeerConnectionE2EQualityTest::OnTrackCallback(
Artem Titovbf9e01a2019-02-14 10:51:27 +0100563 rtc::scoped_refptr<RtpTransceiverInterface> transceiver,
564 std::vector<VideoConfig> remote_video_configs) {
565 const rtc::scoped_refptr<MediaStreamTrackInterface>& track =
566 transceiver->receiver()->track();
Mirko Bonadeif948eb62019-04-05 15:13:23 +0200567 RTC_CHECK_EQ(transceiver->receiver()->stream_ids().size(), 1);
568 std::string stream_label = transceiver->receiver()->stream_ids().front();
569 analyzer_helper_.AddTrackToStreamMapping(track->id(), stream_label);
Artem Titovbf9e01a2019-02-14 10:51:27 +0100570 if (track->kind() != MediaStreamTrackInterface::kVideoKind) {
571 return;
572 }
573
574 VideoConfig* video_config = nullptr;
575 for (auto& config : remote_video_configs) {
Artem Titov7c554152019-02-28 10:25:52 +0100576 if (config.stream_label == stream_label) {
Artem Titovbf9e01a2019-02-14 10:51:27 +0100577 video_config = &config;
578 break;
579 }
580 }
581 RTC_CHECK(video_config);
Artem Titov0b443142019-03-20 11:11:08 +0100582 test::VideoFrameWriter* writer = MaybeCreateVideoWriter(
Artem Titovbf9e01a2019-02-14 10:51:27 +0100583 video_config->output_dump_file_name, *video_config);
584 // It is safe to cast here, because it is checked above that
585 // track->kind() is kVideoKind.
586 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
587 std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>> video_sink =
588 video_quality_analyzer_injection_helper_->CreateVideoSink(writer);
589 video_track->AddOrUpdateSink(video_sink.get(), rtc::VideoSinkWants());
590 output_video_sinks_.push_back(std::move(video_sink));
591}
592
Artem Titovf65a89b2019-05-07 11:56:44 +0200593void PeerConnectionE2EQualityTest::SetupCallOnSignalingThread(
594 const RunParams& run_params) {
Artem Titov7c554152019-02-28 10:25:52 +0100595 // We need receive-only transceivers for Bob's media stream, so there will
596 // be media section in SDP for that streams in Alice's offer, because it is
597 // forbidden to add new media sections in answer in Unified Plan.
598 RtpTransceiverInit receive_only_transceiver_init;
599 receive_only_transceiver_init.direction = RtpTransceiverDirection::kRecvOnly;
Artem Titovef3fd9c2019-06-13 16:36:52 +0200600 int alice_transceivers_counter = 0;
Artem Titov7c554152019-02-28 10:25:52 +0100601 if (bob_->params()->audio_config) {
602 // Setup receive audio transceiver if Bob has audio to send. If we'll need
603 // multiple audio streams, then we need transceiver for each Bob's audio
604 // stream.
Artem Titov7f2a67f2019-06-13 19:45:55 +0200605 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
606 alice_->AddTransceiver(cricket::MediaType::MEDIA_TYPE_AUDIO,
607 receive_only_transceiver_init);
608 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200609 alice_transceivers_counter++;
610 }
611
612 for (auto& video_config : alice_->params()->video_configs) {
613 if (video_config.simulcast_config) {
614 RtpTransceiverInit transceiver_params;
615 transceiver_params.direction = RtpTransceiverDirection::kSendOnly;
616 for (int i = 0;
617 i < video_config.simulcast_config->simulcast_streams_count; ++i) {
618 RtpEncodingParameters enc_params;
619 // We need to be sure, that all rids will be unique with all mids.
620 enc_params.rid = std::to_string(alice_transceivers_counter) + "000" +
621 std::to_string(i);
622 transceiver_params.send_encodings.push_back(enc_params);
623 }
Artem Titov7f2a67f2019-06-13 19:45:55 +0200624 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
625 alice_->AddTransceiver(cricket::MediaType::MEDIA_TYPE_VIDEO,
626 transceiver_params);
627 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200628 alice_transceivers_counter++;
629 }
Artem Titov7c554152019-02-28 10:25:52 +0100630 }
631 for (size_t i = 0; i < bob_->params()->video_configs.size(); ++i) {
Artem Titov7f2a67f2019-06-13 19:45:55 +0200632 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
633 alice_->AddTransceiver(cricket::MediaType::MEDIA_TYPE_VIDEO,
634 receive_only_transceiver_init);
635 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200636 alice_transceivers_counter++;
Artem Titov7c554152019-02-28 10:25:52 +0100637 }
638 // Then add media for Alice and Bob
639 alice_video_sources_ = MaybeAddMedia(alice_.get());
640 bob_video_sources_ = MaybeAddMedia(bob_.get());
Artem Titova6a273d2019-02-07 16:43:51 +0100641
Artem Titovf65a89b2019-05-07 11:56:44 +0200642 SetPeerCodecPreferences(alice_.get(), run_params);
643 SetPeerCodecPreferences(bob_.get(), run_params);
644
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100645 SetupCall();
Mirko Bonadei71318802019-02-18 18:52:43 +0100646}
Artem Titova6a273d2019-02-07 16:43:51 +0100647
Mirko Bonadei71318802019-02-18 18:52:43 +0100648void PeerConnectionE2EQualityTest::TearDownCallOnSignalingThread() {
Artem Titova6a273d2019-02-07 16:43:51 +0100649 TearDownCall();
Artem Titova6a273d2019-02-07 16:43:51 +0100650}
651
Artem Titovbf9e01a2019-02-14 10:51:27 +0100652std::vector<rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource>>
Artem Titov7c554152019-02-28 10:25:52 +0100653PeerConnectionE2EQualityTest::MaybeAddMedia(TestPeer* peer) {
654 MaybeAddAudio(peer);
655 return MaybeAddVideo(peer);
Artem Titova6a273d2019-02-07 16:43:51 +0100656}
657
Artem Titovbf9e01a2019-02-14 10:51:27 +0100658std::vector<rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource>>
Artem Titov7c554152019-02-28 10:25:52 +0100659PeerConnectionE2EQualityTest::MaybeAddVideo(TestPeer* peer) {
Artem Titova6a273d2019-02-07 16:43:51 +0100660 // Params here valid because of pre-run validation.
661 Params* params = peer->params();
Artem Titovbf9e01a2019-02-14 10:51:27 +0100662 std::vector<rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource>> out;
Artem Titova6a273d2019-02-07 16:43:51 +0100663 for (auto video_config : params->video_configs) {
664 // Create video generator.
Artem Titov0b443142019-03-20 11:11:08 +0100665 std::unique_ptr<test::FrameGenerator> frame_generator =
Artem Titova6a273d2019-02-07 16:43:51 +0100666 CreateFrameGenerator(video_config);
667
668 // Wrap it to inject video quality analyzer and enable dump of input video
669 // if required.
Artem Titov0b443142019-03-20 11:11:08 +0100670 test::VideoFrameWriter* writer =
Artem Titova6a273d2019-02-07 16:43:51 +0100671 MaybeCreateVideoWriter(video_config.input_dump_file_name, video_config);
672 frame_generator =
673 video_quality_analyzer_injection_helper_->WrapFrameGenerator(
674 video_config.stream_label.value(), std::move(frame_generator),
675 writer);
676
677 // Setup FrameGenerator into peer connection.
Danil Chapovalovd0e0ed82019-04-18 14:48:24 +0200678 auto capturer = absl::make_unique<test::FrameGeneratorCapturer>(
679 clock_, std::move(frame_generator), video_config.fps,
680 *task_queue_factory_);
681 capturer->Init();
Artem Titova6a273d2019-02-07 16:43:51 +0100682 rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource> source =
683 new rtc::RefCountedObject<FrameGeneratorCapturerVideoTrackSource>(
Artem Titov232b6a12019-05-29 11:05:01 +0200684 std::move(capturer),
685 /*is_screencast=*/video_config.screen_share_config.has_value());
Artem Titovbf9e01a2019-02-14 10:51:27 +0100686 out.push_back(source);
Artem Titova6a273d2019-02-07 16:43:51 +0100687 RTC_LOG(INFO) << "Adding video with video_config.stream_label="
688 << video_config.stream_label.value();
689 rtc::scoped_refptr<VideoTrackInterface> track =
690 peer->pc_factory()->CreateVideoTrack(video_config.stream_label.value(),
691 source);
Artem Titov232b6a12019-05-29 11:05:01 +0200692 if (video_config.screen_share_config) {
693 track->set_content_hint(VideoTrackInterface::ContentHint::kText);
694 }
Artem Titov7c554152019-02-28 10:25:52 +0100695 peer->AddTrack(track, {video_config.stream_label.value()});
Artem Titova6a273d2019-02-07 16:43:51 +0100696 }
Artem Titovbf9e01a2019-02-14 10:51:27 +0100697 return out;
Artem Titova6a273d2019-02-07 16:43:51 +0100698}
699
Artem Titov0b443142019-03-20 11:11:08 +0100700std::unique_ptr<test::FrameGenerator>
Artem Titova6a273d2019-02-07 16:43:51 +0100701PeerConnectionE2EQualityTest::CreateFrameGenerator(
702 const VideoConfig& video_config) {
703 if (video_config.generator) {
Artem Titov0b443142019-03-20 11:11:08 +0100704 absl::optional<test::FrameGenerator::OutputType> frame_generator_type =
Artem Titova6a273d2019-02-07 16:43:51 +0100705 absl::nullopt;
706 if (video_config.generator == VideoGeneratorType::kDefault) {
Artem Titov0b443142019-03-20 11:11:08 +0100707 frame_generator_type = test::FrameGenerator::OutputType::I420;
Artem Titova6a273d2019-02-07 16:43:51 +0100708 } else if (video_config.generator == VideoGeneratorType::kI420A) {
Artem Titov0b443142019-03-20 11:11:08 +0100709 frame_generator_type = test::FrameGenerator::OutputType::I420A;
Artem Titova6a273d2019-02-07 16:43:51 +0100710 } else if (video_config.generator == VideoGeneratorType::kI010) {
Artem Titov0b443142019-03-20 11:11:08 +0100711 frame_generator_type = test::FrameGenerator::OutputType::I010;
Artem Titova6a273d2019-02-07 16:43:51 +0100712 }
Artem Titov0b443142019-03-20 11:11:08 +0100713 return test::FrameGenerator::CreateSquareGenerator(
Artem Titova6a273d2019-02-07 16:43:51 +0100714 static_cast<int>(video_config.width),
715 static_cast<int>(video_config.height), frame_generator_type,
716 absl::nullopt);
717 }
718 if (video_config.input_file_name) {
Artem Titov0b443142019-03-20 11:11:08 +0100719 return test::FrameGenerator::CreateFromYuvFile(
Artem Titova6a273d2019-02-07 16:43:51 +0100720 std::vector<std::string>(/*count=*/1,
721 video_config.input_file_name.value()),
722 video_config.width, video_config.height, /*frame_repeat_count=*/1);
723 }
724 if (video_config.screen_share_config) {
Artem Titov7581ff72019-05-15 15:45:33 +0200725 return CreateScreenShareFrameGenerator(video_config);
Artem Titova6a273d2019-02-07 16:43:51 +0100726 }
727 RTC_NOTREACHED() << "Unsupported video_config input source";
728 return nullptr;
729}
730
Artem Titov7581ff72019-05-15 15:45:33 +0200731std::unique_ptr<test::FrameGenerator>
732PeerConnectionE2EQualityTest::CreateScreenShareFrameGenerator(
733 const VideoConfig& video_config) {
734 RTC_CHECK(video_config.screen_share_config);
735 if (video_config.screen_share_config->generate_slides) {
736 return test::FrameGenerator::CreateSlideGenerator(
737 video_config.width, video_config.height,
738 video_config.screen_share_config->slide_change_interval.seconds() *
739 video_config.fps);
740 }
741 std::vector<std::string> slides =
742 video_config.screen_share_config->slides_yuv_file_names;
743 if (slides.empty()) {
744 // If slides is empty we need to add default slides as source. In such case
745 // video width and height is validated to be equal to kDefaultSlidesWidth
746 // and kDefaultSlidesHeight.
747 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
748 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
749 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
750 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
751 }
752 if (!video_config.screen_share_config->scrolling_params) {
753 // Cycle image every slide_change_interval seconds.
754 return test::FrameGenerator::CreateFromYuvFile(
755 slides, video_config.width, video_config.height,
756 video_config.screen_share_config->slide_change_interval.seconds() *
757 video_config.fps);
758 }
759
760 // |pause_duration| is nonnegative. It is validated in ValidateParams(...).
761 TimeDelta pause_duration =
762 video_config.screen_share_config->slide_change_interval -
763 video_config.screen_share_config->scrolling_params->duration;
764
765 return test::FrameGenerator::CreateScrollingInputFromYuvFiles(
766 clock_, slides,
767 video_config.screen_share_config->scrolling_params->source_width,
768 video_config.screen_share_config->scrolling_params->source_height,
769 video_config.width, video_config.height,
770 video_config.screen_share_config->scrolling_params->duration.ms(),
771 pause_duration.ms());
772}
773
Artem Titov7c554152019-02-28 10:25:52 +0100774void PeerConnectionE2EQualityTest::MaybeAddAudio(TestPeer* peer) {
775 if (!peer->params()->audio_config) {
776 return;
777 }
Artem Titov3481db22019-02-28 13:13:15 +0100778 const AudioConfig& audio_config = peer->params()->audio_config.value();
Artem Titova6a273d2019-02-07 16:43:51 +0100779 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Artem Titov3481db22019-02-28 13:13:15 +0100780 peer->pc_factory()->CreateAudioSource(audio_config.audio_options);
Artem Titova6a273d2019-02-07 16:43:51 +0100781 rtc::scoped_refptr<AudioTrackInterface> track =
Artem Titov3481db22019-02-28 13:13:15 +0100782 peer->pc_factory()->CreateAudioTrack(*audio_config.stream_label, source);
783 peer->AddTrack(track, {*audio_config.stream_label});
Artem Titova6a273d2019-02-07 16:43:51 +0100784}
785
Artem Titovf65a89b2019-05-07 11:56:44 +0200786void PeerConnectionE2EQualityTest::SetPeerCodecPreferences(
787 TestPeer* peer,
788 const RunParams& run_params) {
Artem Titovef3fd9c2019-06-13 16:36:52 +0200789 std::vector<RtpCodecCapability> with_rtx_video_capabilities =
Artem Titov7f2a67f2019-06-13 19:45:55 +0200790 FilterVideoCodecCapabilities(
Artem Titovef3fd9c2019-06-13 16:36:52 +0200791 run_params.video_codec_name, run_params.video_codec_required_params,
792 true, run_params.use_ulp_fec, run_params.use_flex_fec,
793 peer->pc_factory()
794 ->GetRtpSenderCapabilities(cricket::MediaType::MEDIA_TYPE_VIDEO)
795 .codecs);
796 std::vector<RtpCodecCapability> without_rtx_video_capabilities =
Artem Titov7f2a67f2019-06-13 19:45:55 +0200797 FilterVideoCodecCapabilities(
Artem Titovef3fd9c2019-06-13 16:36:52 +0200798 run_params.video_codec_name, run_params.video_codec_required_params,
799 false, run_params.use_ulp_fec, run_params.use_flex_fec,
800 peer->pc_factory()
801 ->GetRtpSenderCapabilities(cricket::MediaType::MEDIA_TYPE_VIDEO)
802 .codecs);
Artem Titovf65a89b2019-05-07 11:56:44 +0200803
804 // Set codecs for transceivers
805 for (auto transceiver : peer->pc()->GetTransceivers()) {
806 if (transceiver->media_type() == cricket::MediaType::MEDIA_TYPE_VIDEO) {
Artem Titovef3fd9c2019-06-13 16:36:52 +0200807 if (transceiver->sender()->init_send_encodings().size() > 1) {
808 // If transceiver's sender has more then 1 send encodings, it means it
809 // has multiple simulcast streams, so we need disable RTX on it.
Artem Titov7f2a67f2019-06-13 19:45:55 +0200810 RTCError result =
811 transceiver->SetCodecPreferences(without_rtx_video_capabilities);
812 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200813 } else {
Artem Titov7f2a67f2019-06-13 19:45:55 +0200814 RTCError result =
815 transceiver->SetCodecPreferences(with_rtx_video_capabilities);
816 RTC_CHECK(result.ok());
Artem Titovef3fd9c2019-06-13 16:36:52 +0200817 }
Artem Titovf65a89b2019-05-07 11:56:44 +0200818 }
819 }
820}
821
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100822void PeerConnectionE2EQualityTest::SetupCall() {
Artem Titovef3fd9c2019-06-13 16:36:52 +0200823 SignalingInterceptor signaling_interceptor;
Artem Titova6a273d2019-02-07 16:43:51 +0100824 // Connect peers.
Artem Titovef3fd9c2019-06-13 16:36:52 +0200825 ExchangeOfferAnswer(&signaling_interceptor);
Artem Titova6a273d2019-02-07 16:43:51 +0100826 // Do the SDP negotiation, and also exchange ice candidates.
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100827 ASSERT_EQ_WAIT(alice_->signaling_state(), PeerConnectionInterface::kStable,
Artem Titova6a273d2019-02-07 16:43:51 +0100828 kDefaultTimeoutMs);
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100829 ASSERT_TRUE_WAIT(alice_->IsIceGatheringDone(), kDefaultTimeoutMs);
830 ASSERT_TRUE_WAIT(bob_->IsIceGatheringDone(), kDefaultTimeoutMs);
Artem Titova6a273d2019-02-07 16:43:51 +0100831
Artem Titovef3fd9c2019-06-13 16:36:52 +0200832 ExchangeIceCandidates(&signaling_interceptor);
Artem Titova6a273d2019-02-07 16:43:51 +0100833 // This means that ICE and DTLS are connected.
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100834 ASSERT_TRUE_WAIT(bob_->IsIceConnected(), kDefaultTimeoutMs);
835 ASSERT_TRUE_WAIT(alice_->IsIceConnected(), kDefaultTimeoutMs);
Artem Titova6a273d2019-02-07 16:43:51 +0100836}
837
Artem Titovef3fd9c2019-06-13 16:36:52 +0200838void PeerConnectionE2EQualityTest::ExchangeOfferAnswer(
839 SignalingInterceptor* signaling_interceptor) {
840 std::string log_output;
841
842 auto offer = alice_->CreateOffer();
843 RTC_CHECK(offer);
844 offer->ToString(&log_output);
845 RTC_LOG(INFO) << "Original offer: " << log_output;
846 LocalAndRemoteSdp patch_result =
847 signaling_interceptor->PatchOffer(std::move(offer));
848 patch_result.local_sdp->ToString(&log_output);
849 RTC_LOG(INFO) << "Offer to set as local description: " << log_output;
850 patch_result.remote_sdp->ToString(&log_output);
851 RTC_LOG(INFO) << "Offer to set as remote description: " << log_output;
852
853 bool set_local_offer =
854 alice_->SetLocalDescription(std::move(patch_result.local_sdp));
855 RTC_CHECK(set_local_offer);
856 bool set_remote_offer =
857 bob_->SetRemoteDescription(std::move(patch_result.remote_sdp));
858 RTC_CHECK(set_remote_offer);
859 auto answer = bob_->CreateAnswer();
860 RTC_CHECK(answer);
861 answer->ToString(&log_output);
862 RTC_LOG(INFO) << "Original answer: " << log_output;
863 patch_result = signaling_interceptor->PatchAnswer(std::move(answer));
864 patch_result.local_sdp->ToString(&log_output);
865 RTC_LOG(INFO) << "Answer to set as local description: " << log_output;
866 patch_result.remote_sdp->ToString(&log_output);
867 RTC_LOG(INFO) << "Answer to set as remote description: " << log_output;
868
869 bool set_local_answer =
870 bob_->SetLocalDescription(std::move(patch_result.local_sdp));
871 RTC_CHECK(set_local_answer);
872 bool set_remote_answer =
873 alice_->SetRemoteDescription(std::move(patch_result.remote_sdp));
874 RTC_CHECK(set_remote_answer);
875}
876
877void PeerConnectionE2EQualityTest::ExchangeIceCandidates(
878 SignalingInterceptor* signaling_interceptor) {
879 // Connect an ICE candidate pairs.
880 std::vector<std::unique_ptr<IceCandidateInterface>> alice_candidates =
881 signaling_interceptor->PatchOffererIceCandidates(
882 alice_->observer()->GetAllCandidates());
883 for (auto& candidate : alice_candidates) {
884 std::string candidate_str;
885 RTC_CHECK(candidate->ToString(&candidate_str));
886 RTC_LOG(INFO) << "Alice ICE candidate(mid= " << candidate->sdp_mid()
887 << "): " << candidate_str;
888 }
889 ASSERT_TRUE(bob_->AddIceCandidates(std::move(alice_candidates)));
890 std::vector<std::unique_ptr<IceCandidateInterface>> bob_candidates =
891 signaling_interceptor->PatchAnswererIceCandidates(
892 bob_->observer()->GetAllCandidates());
893 for (auto& candidate : bob_candidates) {
894 std::string candidate_str;
895 RTC_CHECK(candidate->ToString(&candidate_str));
896 RTC_LOG(INFO) << "Bob ICE candidate(mid= " << candidate->sdp_mid()
897 << "): " << candidate_str;
898 }
899 ASSERT_TRUE(alice_->AddIceCandidates(std::move(bob_candidates)));
900}
901
Artem Titovbf9e01a2019-02-14 10:51:27 +0100902void PeerConnectionE2EQualityTest::StartVideo(
903 const std::vector<
904 rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource>>& sources) {
905 for (auto& source : sources) {
906 if (source->state() != MediaSourceInterface::SourceState::kLive) {
907 source->Start();
Artem Titova6a273d2019-02-07 16:43:51 +0100908 }
Artem Titova6a273d2019-02-07 16:43:51 +0100909 }
910}
911
912void PeerConnectionE2EQualityTest::TearDownCall() {
Artem Titovbf9e01a2019-02-14 10:51:27 +0100913 for (const auto& video_source : alice_video_sources_) {
914 video_source->Stop();
915 }
916 for (const auto& video_source : bob_video_sources_) {
Artem Titova6a273d2019-02-07 16:43:51 +0100917 video_source->Stop();
918 }
919
920 alice_->pc()->Close();
921 bob_->pc()->Close();
922
923 for (const auto& video_writer : video_writers_) {
924 video_writer->Close();
925 }
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100926
Artem Titovbf9e01a2019-02-14 10:51:27 +0100927 alice_video_sources_.clear();
928 bob_video_sources_.clear();
Mirko Bonadei2bd54a12019-02-13 09:07:55 +0100929 video_writers_.clear();
930 alice_.reset();
931 bob_.reset();
Artem Titova6a273d2019-02-07 16:43:51 +0100932}
933
Artem Titov0b443142019-03-20 11:11:08 +0100934test::VideoFrameWriter* PeerConnectionE2EQualityTest::MaybeCreateVideoWriter(
Artem Titova6a273d2019-02-07 16:43:51 +0100935 absl::optional<std::string> file_name,
936 const VideoConfig& config) {
937 if (!file_name) {
938 return nullptr;
939 }
Artem Titovef3fd9c2019-06-13 16:36:52 +0200940 // TODO(titovartem) create only one file writer for simulcast video track.
Artem Titov0b443142019-03-20 11:11:08 +0100941 auto video_writer = absl::make_unique<test::VideoFrameWriter>(
Artem Titova6a273d2019-02-07 16:43:51 +0100942 file_name.value(), config.width, config.height, config.fps);
Artem Titov0b443142019-03-20 11:11:08 +0100943 test::VideoFrameWriter* out = video_writer.get();
Artem Titova6a273d2019-02-07 16:43:51 +0100944 video_writers_.push_back(std::move(video_writer));
945 return out;
946}
947
Artem Titovba82e002019-03-15 15:57:53 +0100948Timestamp PeerConnectionE2EQualityTest::Now() const {
Sebastian Janssonb64ad0e2019-06-19 09:39:34 +0200949 return clock_->CurrentTime();
Artem Titovba82e002019-03-15 15:57:53 +0100950}
951
952PeerConnectionE2EQualityTest::ScheduledActivity::ScheduledActivity(
953 TimeDelta initial_delay_since_start,
954 absl::optional<TimeDelta> interval,
955 std::function<void(TimeDelta)> func)
956 : initial_delay_since_start(initial_delay_since_start),
957 interval(std::move(interval)),
958 func(std::move(func)) {}
959
Artem Titov0b443142019-03-20 11:11:08 +0100960} // namespace webrtc_pc_e2e
Artem Titova6a273d2019-02-07 16:43:51 +0100961} // namespace webrtc