blob: 03749a701625866e6f10262fda0ab5b3a9daa0a8 [file] [log] [blame]
ivica5d6a06c2015-09-17 05:30:24 -07001/*
2 * Copyright (c) 2015 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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "video/video_quality_test.h"
perkj9fdbda62016-09-15 09:19:20 -070011
perkja49cbd32016-09-16 07:53:41 -070012#include <stdio.h>
ivica5d6a06c2015-09-17 05:30:24 -070013#include <algorithm>
14#include <deque>
15#include <map>
sprangce4aef12015-11-02 07:23:20 -080016#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080017#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070018#include <vector>
19
Artem Titovdd2eebe2018-08-20 13:27:45 +020020#include "call/fake_network_pipe.h"
21#include "call/simulated_network.h"
Elad Alon83ccca12017-10-04 13:18:26 +020022#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
henrika255750b2018-08-27 16:13:37 +020023#include "media/engine/adm_helpers.h"
Magnus Jedvert46a27652017-11-13 14:10:02 +010024#include "media/engine/internalencoderfactory.h"
Niels Möller19a68d42018-04-13 13:37:52 +020025#include "media/engine/vp8_encoder_simulcast_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/engine/webrtcvideoengine.h"
henrika255750b2018-08-27 16:13:37 +020027#include "modules/audio_device/include/audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_mixer/audio_mixer_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/video_coding/codecs/h264/include/h264.h"
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080030#include "modules/video_coding/codecs/multiplex/include/multiplex_encoder_adapter.h"
Niels Möller4db138e2018-04-19 09:04:13 +020031#include "modules/video_coding/codecs/vp8/include/vp8.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/video_coding/codecs/vp9/include/vp9.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "test/run_loop.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "test/testsupport/fileutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "test/video_renderer.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020036#include "video/video_analyzer.h"
henrika255750b2018-08-27 16:13:37 +020037#ifdef WEBRTC_WIN
38#include "modules/audio_device/include/audio_device_factory.h"
39#endif
ilnikee42d192017-08-22 07:16:20 -070040
minyue73208662016-08-18 06:28:55 -070041namespace {
minyue73208662016-08-18 06:28:55 -070042constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080043constexpr int kOpusMinBitrateBps = 6000;
44constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 00:53:12 -080045constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 04:21:04 -080046constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
47constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 06:28:55 -070048
sprang1168fd42017-06-21 09:00:17 -070049constexpr int kDefaultMaxQp = cricket::WebRtcVideoChannel::kDefaultQpMax;
50
perkjfa10b552016-10-02 23:45:26 -070051class VideoStreamFactory
52 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
53 public:
54 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
55 : streams_(streams) {}
56
57 private:
58 std::vector<webrtc::VideoStream> CreateEncoderStreams(
59 int width,
60 int height,
61 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -080062 // The highest layer must match the incoming resolution.
63 std::vector<webrtc::VideoStream> streams = streams_;
64 streams[streams_.size() - 1].height = height;
65 streams[streams_.size() - 1].width = width;
Seth Hampson24722b32017-12-22 09:36:42 -080066
67 streams[0].bitrate_priority = encoder_config.bitrate_priority;
mflodmand79f97b2016-12-15 07:24:33 -080068 return streams;
perkjfa10b552016-10-02 23:45:26 -070069 }
70
71 std::vector<webrtc::VideoStream> streams_;
72};
minyue73208662016-08-18 06:28:55 -070073} // namespace
ivica5d6a06c2015-09-17 05:30:24 -070074
75namespace webrtc {
76
Sebastian Janssone6d7c3e2018-07-11 15:00:41 +020077std::unique_ptr<VideoEncoder> VideoQualityTest::CreateVideoEncoder(
Niels Möller4db138e2018-04-19 09:04:13 +020078 const SdpVideoFormat& format) {
79 if (format.name == "VP8") {
Karl Wiberg918f50c2018-07-05 11:40:33 +020080 return absl::make_unique<VP8EncoderSimulcastProxy>(
81 &internal_encoder_factory_, format);
Niels Möller4db138e2018-04-19 09:04:13 +020082 } else if (format.name == "multiplex") {
Karl Wiberg918f50c2018-07-05 11:40:33 +020083 return absl::make_unique<MultiplexEncoderAdapter>(
Niels Möller4db138e2018-04-19 09:04:13 +020084 &internal_encoder_factory_, SdpVideoFormat(cricket::kVp9CodecName));
Niels Möller4db138e2018-04-19 09:04:13 +020085 }
Emircan Uysaler0823eec2018-07-13 17:10:00 -070086 return internal_encoder_factory_.CreateVideoEncoder(format);
Niels Möller4db138e2018-04-19 09:04:13 +020087}
88
Patrik Höglundb6b29e02018-06-21 16:58:01 +020089VideoQualityTest::VideoQualityTest(
Artem Titove269cb42018-08-29 09:59:23 +020090 std::unique_ptr<InjectionComponents> injection_components)
Sebastian Janssone6d7c3e2018-07-11 15:00:41 +020091 : clock_(Clock::GetRealTimeClock()),
92 video_encoder_factory_([this](const SdpVideoFormat& format) {
93 return this->CreateVideoEncoder(format);
94 }),
95 receive_logs_(0),
Artem Titove269cb42018-08-29 09:59:23 +020096 send_logs_(0),
97 injection_components_(std::move(injection_components)) {
98 if (injection_components_ == nullptr) {
99 injection_components_ = absl::make_unique<InjectionComponents>();
100 }
101
minyue20c84cc2017-04-10 16:57:57 -0700102 payload_type_map_ = test::CallTest::payload_type_map_;
103 RTC_DCHECK(payload_type_map_.find(kPayloadTypeH264) ==
104 payload_type_map_.end());
105 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP8) ==
106 payload_type_map_.end());
107 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP9) ==
108 payload_type_map_.end());
109 payload_type_map_[kPayloadTypeH264] = webrtc::MediaType::VIDEO;
110 payload_type_map_[kPayloadTypeVP8] = webrtc::MediaType::VIDEO;
111 payload_type_map_[kPayloadTypeVP9] = webrtc::MediaType::VIDEO;
ivica5d6a06c2015-09-17 05:30:24 -0700112
Artem Titove269cb42018-08-29 09:59:23 +0200113 fec_controller_factory_ =
114 std::move(injection_components_->fec_controller_factory);
Ying Wang3b790f32018-01-19 17:58:57 +0100115}
116
minyue626bc952016-10-31 05:47:02 -0700117VideoQualityTest::Params::Params()
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100118 : call({false, BitrateConstraints(), 0}),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100119 video{{false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
Niels Möller6aa415e2018-06-07 11:14:13 +0200120 false, false, ""},
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100121 {false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
Niels Möller6aa415e2018-06-07 11:14:13 +0200122 false, false, ""}},
henrika255750b2018-08-27 16:13:37 +0200123 audio({false, false, false, false}),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100124 screenshare{{false, false, 10, 0}, {false, false, 10, 0}},
minyue626bc952016-10-31 05:47:02 -0700125 analyzer({"", 0.0, 0.0, 0, "", ""}),
126 pipe(),
Artem Titovf18b3522018-08-28 16:54:24 +0200127 config(absl::nullopt),
Sergey Silkin57027362018-05-15 09:12:05 +0200128 ss{{std::vector<VideoStream>(), 0, 0, -1, InterLayerPredMode::kOn,
129 std::vector<SpatialLayer>()},
130 {std::vector<VideoStream>(), 0, 0, -1, InterLayerPredMode::kOn,
131 std::vector<SpatialLayer>()}},
Mirko Bonadei45a4c412018-07-31 15:07:28 +0200132 logging({"", "", ""}) {}
minyue626bc952016-10-31 05:47:02 -0700133
134VideoQualityTest::Params::~Params() = default;
135
Artem Titove269cb42018-08-29 09:59:23 +0200136VideoQualityTest::InjectionComponents::InjectionComponents() = default;
137
138VideoQualityTest::InjectionComponents::~InjectionComponents() = default;
139
ivica5d6a06c2015-09-17 05:30:24 -0700140void VideoQualityTest::TestBody() {}
141
sprangce4aef12015-11-02 07:23:20 -0800142std::string VideoQualityTest::GenerateGraphTitle() const {
143 std::stringstream ss;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100144 ss << params_.video[0].codec;
145 ss << " (" << params_.video[0].target_bitrate_bps / 1000 << "kbps";
146 ss << ", " << params_.video[0].fps << " FPS";
147 if (params_.screenshare[0].scroll_duration)
148 ss << ", " << params_.screenshare[0].scroll_duration << "s scroll";
149 if (params_.ss[0].streams.size() > 1)
150 ss << ", Stream #" << params_.ss[0].selected_stream;
151 if (params_.ss[0].num_spatial_layers > 1)
152 ss << ", Layer #" << params_.ss[0].selected_sl;
sprangce4aef12015-11-02 07:23:20 -0800153 ss << ")";
154 return ss.str();
155}
156
Artem Titove269cb42018-08-29 09:59:23 +0200157void VideoQualityTest::CheckParamsAndInjectionComponents() {
158 if (injection_components_ == nullptr) {
159 injection_components_ = absl::make_unique<InjectionComponents>();
160 }
161 if (!params_.config && injection_components_->sender_network == nullptr &&
162 injection_components_->receiver_network == nullptr) {
Artem Titovf18b3522018-08-28 16:54:24 +0200163 // TODO(titovartem) replace with default config creation when removing
164 // pipe.
165 params_.config = params_.pipe;
166 }
Artem Titove269cb42018-08-29 09:59:23 +0200167 RTC_CHECK(
168 (params_.config && injection_components_->sender_network == nullptr &&
169 injection_components_->receiver_network == nullptr) ||
170 (!params_.config && injection_components_->sender_network != nullptr &&
171 injection_components_->receiver_network != nullptr));
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100172 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
173 // Iterate over primary and secondary video streams.
174 if (!params_.video[video_idx].enabled)
175 return;
176 // Add a default stream in none specified.
177 if (params_.ss[video_idx].streams.empty())
178 params_.ss[video_idx].streams.push_back(
179 VideoQualityTest::DefaultVideoStream(params_, video_idx));
180 if (params_.ss[video_idx].num_spatial_layers == 0)
181 params_.ss[video_idx].num_spatial_layers = 1;
sprangce4aef12015-11-02 07:23:20 -0800182
Artem Titove269cb42018-08-29 09:59:23 +0200183 if (params_.config) {
184 if (params_.config->loss_percent != 0 ||
185 params_.config->queue_length_packets != 0) {
186 // Since LayerFilteringTransport changes the sequence numbers, we can't
187 // use that feature with pack loss, since the NACK request would end up
188 // retransmitting the wrong packets.
189 RTC_CHECK(params_.ss[video_idx].selected_sl == -1 ||
190 params_.ss[video_idx].selected_sl ==
191 params_.ss[video_idx].num_spatial_layers - 1);
192 RTC_CHECK(params_.video[video_idx].selected_tl == -1 ||
193 params_.video[video_idx].selected_tl ==
194 params_.video[video_idx].num_temporal_layers - 1);
195 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100196 }
sprangce4aef12015-11-02 07:23:20 -0800197
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100198 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as
199 // it does in some parts of the code?
200 RTC_CHECK_GE(params_.video[video_idx].max_bitrate_bps,
201 params_.video[video_idx].target_bitrate_bps);
202 RTC_CHECK_GE(params_.video[video_idx].target_bitrate_bps,
203 params_.video[video_idx].min_bitrate_bps);
204 RTC_CHECK_LT(params_.video[video_idx].selected_tl,
205 params_.video[video_idx].num_temporal_layers);
206 RTC_CHECK_LE(params_.ss[video_idx].selected_stream,
207 params_.ss[video_idx].streams.size());
208 for (const VideoStream& stream : params_.ss[video_idx].streams) {
209 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
210 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
211 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
212 }
213 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
214 // the total bitrate? We anyway have to update them in the case bitrate
215 // estimator changes the total bitrates.
216 RTC_CHECK_GE(params_.ss[video_idx].num_spatial_layers, 1);
217 RTC_CHECK_LE(params_.ss[video_idx].selected_sl,
218 params_.ss[video_idx].num_spatial_layers);
219 RTC_CHECK(
220 params_.ss[video_idx].spatial_layers.empty() ||
221 params_.ss[video_idx].spatial_layers.size() ==
222 static_cast<size_t>(params_.ss[video_idx].num_spatial_layers));
223 if (params_.video[video_idx].codec == "VP8") {
224 RTC_CHECK_EQ(params_.ss[video_idx].num_spatial_layers, 1);
225 } else if (params_.video[video_idx].codec == "VP9") {
226 RTC_CHECK_EQ(params_.ss[video_idx].streams.size(), 1);
227 }
228 RTC_CHECK_GE(params_.call.num_thumbnails, 0);
229 if (params_.call.num_thumbnails > 0) {
230 RTC_CHECK_EQ(params_.ss[video_idx].num_spatial_layers, 1);
231 RTC_CHECK_EQ(params_.ss[video_idx].streams.size(), 3);
232 RTC_CHECK_EQ(params_.video[video_idx].num_temporal_layers, 3);
233 RTC_CHECK_EQ(params_.video[video_idx].codec, "VP8");
234 }
235 // Dual streams with FEC not supported in tests yet.
236 RTC_CHECK(!params_.video[video_idx].flexfec || num_video_streams_ == 1);
237 RTC_CHECK(!params_.video[video_idx].ulpfec || num_video_streams_ == 1);
ilnika014cc52017-03-07 04:21:04 -0800238 }
sprangce4aef12015-11-02 07:23:20 -0800239}
240
241// Static.
242std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
243 // Parse comma separated nonnegative integers, where some elements may be
244 // empty. The empty values are replaced with -1.
245 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
246 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
247 std::vector<int> result;
248 if (str.empty())
249 return result;
250
251 const char* p = str.c_str();
252 int value = -1;
253 int pos;
254 while (*p) {
255 if (*p == ',') {
256 result.push_back(value);
257 value = -1;
258 ++p;
259 continue;
260 }
261 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
262 << "Unexpected non-number value.";
263 p += pos;
264 }
265 result.push_back(value);
266 return result;
267}
268
269// Static.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100270VideoStream VideoQualityTest::DefaultVideoStream(const Params& params,
271 size_t video_idx) {
sprangce4aef12015-11-02 07:23:20 -0800272 VideoStream stream;
Danil Chapovalov350531e2018-06-08 11:04:04 +0000273 stream.width = params.video[video_idx].width;
274 stream.height = params.video[video_idx].height;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100275 stream.max_framerate = params.video[video_idx].fps;
276 stream.min_bitrate_bps = params.video[video_idx].min_bitrate_bps;
277 stream.target_bitrate_bps = params.video[video_idx].target_bitrate_bps;
278 stream.max_bitrate_bps = params.video[video_idx].max_bitrate_bps;
sprang1168fd42017-06-21 09:00:17 -0700279 stream.max_qp = kDefaultMaxQp;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100280 stream.num_temporal_layers = params.video[video_idx].num_temporal_layers;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800281 stream.active = true;
ilnika014cc52017-03-07 04:21:04 -0800282 return stream;
283}
284
285// Static.
286VideoStream VideoQualityTest::DefaultThumbnailStream() {
287 VideoStream stream;
288 stream.width = 320;
289 stream.height = 180;
290 stream.max_framerate = 7;
291 stream.min_bitrate_bps = 7500;
292 stream.target_bitrate_bps = 37500;
293 stream.max_bitrate_bps = 50000;
sprang1168fd42017-06-21 09:00:17 -0700294 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 07:23:20 -0800295 return stream;
296}
297
298// Static.
299void VideoQualityTest::FillScalabilitySettings(
300 Params* params,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100301 size_t video_idx,
sprangce4aef12015-11-02 07:23:20 -0800302 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 09:00:17 -0700303 int num_streams,
sprangce4aef12015-11-02 07:23:20 -0800304 size_t selected_stream,
305 int num_spatial_layers,
306 int selected_sl,
Sergey Silkin57027362018-05-15 09:12:05 +0200307 InterLayerPredMode inter_layer_pred,
sprangce4aef12015-11-02 07:23:20 -0800308 const std::vector<std::string>& sl_descriptors) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100309 if (params->ss[video_idx].streams.empty() &&
310 params->ss[video_idx].infer_streams) {
sprang1168fd42017-06-21 09:00:17 -0700311 webrtc::VideoEncoderConfig encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200312 encoder_config.codec_type =
313 PayloadStringToCodecType(params->video[video_idx].codec);
sprang1168fd42017-06-21 09:00:17 -0700314 encoder_config.content_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100315 params->screenshare[video_idx].enabled
sprang1168fd42017-06-21 09:00:17 -0700316 ? webrtc::VideoEncoderConfig::ContentType::kScreen
317 : webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100318 encoder_config.max_bitrate_bps = params->video[video_idx].max_bitrate_bps;
319 encoder_config.min_transmit_bitrate_bps =
320 params->video[video_idx].min_transmit_bps;
sprang1168fd42017-06-21 09:00:17 -0700321 encoder_config.number_of_streams = num_streams;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100322 encoder_config.spatial_layers = params->ss[video_idx].spatial_layers;
Seth Hampson8234ead2018-02-02 15:16:24 -0800323 encoder_config.simulcast_layers = std::vector<VideoStream>(num_streams);
sprang1168fd42017-06-21 09:00:17 -0700324 encoder_config.video_stream_factory =
325 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100326 params->video[video_idx].codec, kDefaultMaxQp,
Mirko Bonadei948b7e32018-08-14 07:23:21 +0000327 params->video[video_idx].fps,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100328 params->screenshare[video_idx].enabled, true);
329 params->ss[video_idx].streams =
sprang1168fd42017-06-21 09:00:17 -0700330 encoder_config.video_stream_factory->CreateEncoderStreams(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100331 static_cast<int>(params->video[video_idx].width),
332 static_cast<int>(params->video[video_idx].height), encoder_config);
sprang1168fd42017-06-21 09:00:17 -0700333 } else {
334 // Read VideoStream and SpatialLayer elements from a list of comma separated
335 // lists. To use a default value for an element, use -1 or leave empty.
Artem Titove269cb42018-08-29 09:59:23 +0200336 // Validity checks performed in CheckParamsAndInjectionComponents.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100337 RTC_CHECK(params->ss[video_idx].streams.empty());
sprang1168fd42017-06-21 09:00:17 -0700338 for (auto descriptor : stream_descriptors) {
339 if (descriptor.empty())
340 continue;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100341 VideoStream stream =
342 VideoQualityTest::DefaultVideoStream(*params, video_idx);
sprang1168fd42017-06-21 09:00:17 -0700343 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
344 if (v[0] != -1)
345 stream.width = static_cast<size_t>(v[0]);
346 if (v[1] != -1)
347 stream.height = static_cast<size_t>(v[1]);
348 if (v[2] != -1)
349 stream.max_framerate = v[2];
350 if (v[3] != -1)
351 stream.min_bitrate_bps = v[3];
352 if (v[4] != -1)
353 stream.target_bitrate_bps = v[4];
354 if (v[5] != -1)
355 stream.max_bitrate_bps = v[5];
356 if (v.size() > 6 && v[6] != -1)
357 stream.max_qp = v[6];
Sergey Silkina796a7e2018-03-01 15:11:29 +0100358 if (v.size() > 7 && v[7] != -1) {
359 stream.num_temporal_layers = v[7];
sprang1168fd42017-06-21 09:00:17 -0700360 } else {
361 // Automatic TL thresholds for more than two layers not supported.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100362 RTC_CHECK_LE(params->video[video_idx].num_temporal_layers, 2);
sprang1168fd42017-06-21 09:00:17 -0700363 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100364 params->ss[video_idx].streams.push_back(stream);
sprangce4aef12015-11-02 07:23:20 -0800365 }
sprangce4aef12015-11-02 07:23:20 -0800366 }
sprangce4aef12015-11-02 07:23:20 -0800367
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100368 params->ss[video_idx].num_spatial_layers = std::max(1, num_spatial_layers);
369 params->ss[video_idx].selected_stream = selected_stream;
sprang1168fd42017-06-21 09:00:17 -0700370
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100371 params->ss[video_idx].selected_sl = selected_sl;
Sergey Silkin57027362018-05-15 09:12:05 +0200372 params->ss[video_idx].inter_layer_pred = inter_layer_pred;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100373 RTC_CHECK(params->ss[video_idx].spatial_layers.empty());
sprangce4aef12015-11-02 07:23:20 -0800374 for (auto descriptor : sl_descriptors) {
375 if (descriptor.empty())
376 continue;
377 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
Sergey Silkin13e74342018-03-02 12:28:00 +0100378 RTC_CHECK_EQ(v.size(), 7);
sprangce4aef12015-11-02 07:23:20 -0800379
Sergey Silkin13e74342018-03-02 12:28:00 +0100380 SpatialLayer layer = {0};
381 layer.width = v[0];
382 layer.height = v[1];
383 layer.numberOfTemporalLayers = v[2];
384 layer.maxBitrate = v[3];
385 layer.minBitrate = v[4];
386 layer.targetBitrate = v[5];
387 layer.qpMax = v[6];
388 layer.active = true;
389
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100390 params->ss[video_idx].spatial_layers.push_back(layer);
sprangce4aef12015-11-02 07:23:20 -0800391 }
392}
393
minyuea27172d2016-11-01 05:59:29 -0700394void VideoQualityTest::SetupVideo(Transport* send_transport,
395 Transport* recv_transport) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100396 size_t total_streams_used = 0;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100397 video_receive_configs_.clear();
398 video_send_configs_.clear();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100399 video_encoder_configs_.clear();
400 allocated_decoders_.clear();
401 bool decode_all_receive_streams = true;
402 size_t num_video_substreams = params_.ss[0].streams.size();
403 RTC_CHECK(num_video_streams_ > 0);
404 video_encoder_configs_.resize(num_video_streams_);
405 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
406 video_send_configs_.push_back(VideoSendStream::Config(send_transport));
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100407 video_encoder_configs_.push_back(VideoEncoderConfig());
408 num_video_substreams = params_.ss[video_idx].streams.size();
409 RTC_CHECK_GT(num_video_substreams, 0);
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200410 for (size_t i = 0; i < num_video_substreams; ++i)
411 video_send_configs_[video_idx].rtp.ssrcs.push_back(
412 kVideoSendSsrcs[total_streams_used + i]);
Qingsi Wang2d82ade2018-07-11 06:04:44 +0000413
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100414 int payload_type;
415 if (params_.video[video_idx].codec == "H264") {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100416 payload_type = kPayloadTypeH264;
417 } else if (params_.video[video_idx].codec == "VP8") {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100418 payload_type = kPayloadTypeVP8;
419 } else if (params_.video[video_idx].codec == "VP9") {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100420 payload_type = kPayloadTypeVP9;
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800421 } else if (params_.video[video_idx].codec == "multiplex") {
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800422 payload_type = kPayloadTypeVP9;
ilnikcb8c1462017-03-09 09:23:30 -0800423 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100424 RTC_NOTREACHED() << "Codec not supported!";
425 return;
ilnikcb8c1462017-03-09 09:23:30 -0800426 }
Niels Möller4db138e2018-04-19 09:04:13 +0200427 video_send_configs_[video_idx].encoder_settings.encoder_factory =
428 &video_encoder_factory_;
429
Niels Möller259a4972018-04-05 15:36:51 +0200430 video_send_configs_[video_idx].rtp.payload_name =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100431 params_.video[video_idx].codec;
Niels Möller259a4972018-04-05 15:36:51 +0200432 video_send_configs_[video_idx].rtp.payload_type = payload_type;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100433 video_send_configs_[video_idx].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
434 video_send_configs_[video_idx].rtp.rtx.payload_type = kSendRtxPayloadType;
435 for (size_t i = 0; i < num_video_substreams; ++i) {
436 video_send_configs_[video_idx].rtp.rtx.ssrcs.push_back(
437 kSendRtxSsrcs[i + total_streams_used]);
ilnik9fd9f6c2017-03-02 08:10:10 -0800438 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100439 video_send_configs_[video_idx].rtp.extensions.clear();
440 if (params_.call.send_side_bwe) {
441 video_send_configs_[video_idx].rtp.extensions.push_back(
442 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
443 test::kTransportSequenceNumberExtensionId));
444 } else {
445 video_send_configs_[video_idx].rtp.extensions.push_back(RtpExtension(
446 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
447 }
448 video_send_configs_[video_idx].rtp.extensions.push_back(
449 RtpExtension(RtpExtension::kVideoContentTypeUri,
450 test::kVideoContentTypeExtensionId));
451 video_send_configs_[video_idx].rtp.extensions.push_back(RtpExtension(
452 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
453
Niels Möller4db138e2018-04-19 09:04:13 +0200454 video_encoder_configs_[video_idx].video_format.name =
455 params_.video[video_idx].codec;
456
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700457 video_encoder_configs_[video_idx].video_format.parameters =
458 params_.video[video_idx].sdp_params;
459
Niels Möller259a4972018-04-05 15:36:51 +0200460 video_encoder_configs_[video_idx].codec_type =
461 PayloadStringToCodecType(params_.video[video_idx].codec);
462
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100463 video_encoder_configs_[video_idx].min_transmit_bitrate_bps =
464 params_.video[video_idx].min_transmit_bps;
465
466 video_send_configs_[video_idx].suspend_below_min_bitrate =
467 params_.video[video_idx].suspend_below_min_bitrate;
468
469 video_encoder_configs_[video_idx].number_of_streams =
470 params_.ss[video_idx].streams.size();
471 video_encoder_configs_[video_idx].max_bitrate_bps = 0;
472 for (size_t i = 0; i < params_.ss[video_idx].streams.size(); ++i) {
473 video_encoder_configs_[video_idx].max_bitrate_bps +=
474 params_.ss[video_idx].streams[i].max_bitrate_bps;
475 }
476 if (params_.ss[video_idx].infer_streams) {
Seth Hampson8234ead2018-02-02 15:16:24 -0800477 video_encoder_configs_[video_idx].simulcast_layers =
478 std::vector<VideoStream>(params_.ss[video_idx].streams.size());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100479 video_encoder_configs_[video_idx].video_stream_factory =
480 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
481 params_.video[video_idx].codec,
482 params_.ss[video_idx].streams[0].max_qp,
Mirko Bonadei948b7e32018-08-14 07:23:21 +0000483 params_.video[video_idx].fps,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100484 params_.screenshare[video_idx].enabled, true);
485 } else {
486 video_encoder_configs_[video_idx].video_stream_factory =
487 new rtc::RefCountedObject<VideoStreamFactory>(
488 params_.ss[video_idx].streams);
489 }
490
491 video_encoder_configs_[video_idx].spatial_layers =
492 params_.ss[video_idx].spatial_layers;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100493 decode_all_receive_streams = params_.ss[video_idx].selected_stream ==
494 params_.ss[video_idx].streams.size();
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200495 absl::optional<int> decode_sub_stream;
496 if (!decode_all_receive_streams)
497 decode_sub_stream = params_.ss[video_idx].selected_stream;
498 CreateMatchingVideoReceiveConfigs(
499 video_send_configs_[video_idx], recv_transport,
500 params_.call.send_side_bwe, decode_sub_stream, true, kNackRtpHistoryMs);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100501
502 if (params_.screenshare[video_idx].enabled) {
503 // Fill out codec settings.
504 video_encoder_configs_[video_idx].content_type =
505 VideoEncoderConfig::ContentType::kScreen;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700506 degradation_preference_ = DegradationPreference::MAINTAIN_RESOLUTION;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100507 if (params_.video[video_idx].codec == "VP8") {
508 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
509 vp8_settings.denoisingOn = false;
510 vp8_settings.frameDroppingOn = false;
511 vp8_settings.numberOfTemporalLayers = static_cast<unsigned char>(
512 params_.video[video_idx].num_temporal_layers);
513 video_encoder_configs_[video_idx].encoder_specific_settings =
514 new rtc::RefCountedObject<
515 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
516 } else if (params_.video[video_idx].codec == "VP9") {
517 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
518 vp9_settings.denoisingOn = false;
519 vp9_settings.frameDroppingOn = false;
520 vp9_settings.numberOfTemporalLayers = static_cast<unsigned char>(
521 params_.video[video_idx].num_temporal_layers);
522 vp9_settings.numberOfSpatialLayers = static_cast<unsigned char>(
523 params_.ss[video_idx].num_spatial_layers);
Sergey Silkin57027362018-05-15 09:12:05 +0200524 vp9_settings.interLayerPred = params_.ss[video_idx].inter_layer_pred;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100525 video_encoder_configs_[video_idx].encoder_specific_settings =
526 new rtc::RefCountedObject<
527 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
528 }
529 } else if (params_.ss[video_idx].num_spatial_layers > 1) {
530 // If SVC mode without screenshare, still need to set codec specifics.
531 RTC_CHECK(params_.video[video_idx].codec == "VP9");
532 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
533 vp9_settings.numberOfTemporalLayers = static_cast<unsigned char>(
534 params_.video[video_idx].num_temporal_layers);
535 vp9_settings.numberOfSpatialLayers =
536 static_cast<unsigned char>(params_.ss[video_idx].num_spatial_layers);
Sergey Silkin57027362018-05-15 09:12:05 +0200537 vp9_settings.interLayerPred = params_.ss[video_idx].inter_layer_pred;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100538 video_encoder_configs_[video_idx].encoder_specific_settings =
539 new rtc::RefCountedObject<
540 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
Niels Möller6aa415e2018-06-07 11:14:13 +0200541 } else if (params_.video[video_idx].automatic_scaling) {
542 if (params_.video[video_idx].codec == "VP8") {
543 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
544 vp8_settings.automaticResizeOn = true;
545 video_encoder_configs_[video_idx].encoder_specific_settings =
546 new rtc::RefCountedObject<
547 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
548 } else if (params_.video[video_idx].codec == "VP9") {
549 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
550 vp9_settings.automaticResizeOn = true;
551 video_encoder_configs_[video_idx].encoder_specific_settings =
552 new rtc::RefCountedObject<
553 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
554 } else {
555 RTC_NOTREACHED() << "Automatic scaling not supported for codec "
Yves Gerey665174f2018-06-19 15:03:05 +0200556 << params_.video[video_idx].codec << ", stream "
557 << video_idx;
Niels Möller6aa415e2018-06-07 11:14:13 +0200558 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100559 }
560 total_streams_used += num_video_substreams;
sprangce4aef12015-11-02 07:23:20 -0800561 }
brandtr1293aca2016-11-16 22:47:29 -0800562
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100563 // FEC supported only for single video stream mode yet.
564 if (params_.video[0].flexfec) {
sprang1168fd42017-06-21 09:00:17 -0700565 if (decode_all_receive_streams) {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200566 SetSendFecConfig(GetVideoSendConfig()->rtp.ssrcs);
sprang1168fd42017-06-21 09:00:17 -0700567 } else {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200568 SetSendFecConfig({kVideoSendSsrcs[params_.ss[0].selected_stream]});
sprang1168fd42017-06-21 09:00:17 -0700569 }
brandtr1293aca2016-11-16 22:47:29 -0800570
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200571 CreateMatchingFecConfig(recv_transport, *GetVideoSendConfig());
572 GetFlexFecConfig()->transport_cc = params_.call.send_side_bwe;
brandtrb29e6522016-12-21 06:37:18 -0800573 if (params_.call.send_side_bwe) {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200574 GetFlexFecConfig()->rtp_header_extensions.push_back(
brandtrb29e6522016-12-21 06:37:18 -0800575 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
576 test::kTransportSequenceNumberExtensionId));
577 } else {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200578 GetFlexFecConfig()->rtp_header_extensions.push_back(RtpExtension(
brandtrb29e6522016-12-21 06:37:18 -0800579 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
580 }
brandtr1293aca2016-11-16 22:47:29 -0800581 }
582
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100583 if (params_.video[0].ulpfec) {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200584 SetSendUlpFecConfig(GetVideoSendConfig());
sprang1168fd42017-06-21 09:00:17 -0700585 if (decode_all_receive_streams) {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200586 for (auto& receive_config : video_receive_configs_) {
587 SetReceiveUlpFecConfig(&receive_config);
sprang1168fd42017-06-21 09:00:17 -0700588 }
589 } else {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200590 SetReceiveUlpFecConfig(
591 &video_receive_configs_[params_.ss[0].selected_stream]);
sprang1168fd42017-06-21 09:00:17 -0700592 }
brandtr1293aca2016-11-16 22:47:29 -0800593 }
ivica5d6a06c2015-09-17 05:30:24 -0700594}
595
ilnika014cc52017-03-07 04:21:04 -0800596void VideoQualityTest::SetupThumbnails(Transport* send_transport,
597 Transport* recv_transport) {
ilnik98436952017-07-13 00:47:03 -0700598 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -0800599 // Thumbnails will be send in the other way: from receiver_call to
600 // sender_call.
601 VideoSendStream::Config thumbnail_send_config(recv_transport);
602 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
Niels Möller4db138e2018-04-19 09:04:13 +0200603 // TODO(nisse): Could use a simpler VP8-only encoder factory.
604 thumbnail_send_config.encoder_settings.encoder_factory =
605 &video_encoder_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200606 thumbnail_send_config.rtp.payload_name = params_.video[0].codec;
607 thumbnail_send_config.rtp.payload_type = kPayloadTypeVP8;
ilnika014cc52017-03-07 04:21:04 -0800608 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
609 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
610 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
611 thumbnail_send_config.rtp.extensions.clear();
612 if (params_.call.send_side_bwe) {
613 thumbnail_send_config.rtp.extensions.push_back(
614 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
615 test::kTransportSequenceNumberExtensionId));
616 } else {
617 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
618 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
619 }
620
621 VideoEncoderConfig thumbnail_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200622 thumbnail_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller4db138e2018-04-19 09:04:13 +0200623 thumbnail_encoder_config.video_format.name = "VP8";
ilnika014cc52017-03-07 04:21:04 -0800624 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
625 thumbnail_send_config.suspend_below_min_bitrate =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100626 params_.video[0].suspend_below_min_bitrate;
ilnika014cc52017-03-07 04:21:04 -0800627 thumbnail_encoder_config.number_of_streams = 1;
628 thumbnail_encoder_config.max_bitrate_bps = 50000;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100629 if (params_.ss[0].infer_streams) {
ilnik6b826ef2017-06-16 06:53:48 -0700630 thumbnail_encoder_config.video_stream_factory =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100631 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss[0].streams);
ilnik6b826ef2017-06-16 06:53:48 -0700632 } else {
Seth Hampson8234ead2018-02-02 15:16:24 -0800633 thumbnail_encoder_config.simulcast_layers = std::vector<VideoStream>(1);
ilnik6b826ef2017-06-16 06:53:48 -0700634 thumbnail_encoder_config.video_stream_factory =
635 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100636 params_.video[0].codec, params_.ss[0].streams[0].max_qp,
Mirko Bonadei948b7e32018-08-14 07:23:21 +0000637 params_.video[0].fps, params_.screenshare[0].enabled, true);
ilnik6b826ef2017-06-16 06:53:48 -0700638 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100639 thumbnail_encoder_config.spatial_layers = params_.ss[0].spatial_layers;
ilnika014cc52017-03-07 04:21:04 -0800640
ilnika014cc52017-03-07 04:21:04 -0800641 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
642 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
ilnika014cc52017-03-07 04:21:04 -0800643
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200644 AddMatchingVideoReceiveConfigs(
645 &thumbnail_receive_configs_, thumbnail_send_config, send_transport,
646 params_.call.send_side_bwe, absl::nullopt, false, kNackRtpHistoryMs);
647 }
648 for (size_t i = 0; i < thumbnail_send_configs_.size(); ++i) {
ilnika014cc52017-03-07 04:21:04 -0800649 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
650 thumbnail_send_configs_[i].Copy(),
651 thumbnail_encoder_configs_[i].Copy()));
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200652 }
653 for (size_t i = 0; i < thumbnail_receive_configs_.size(); ++i) {
ilnika014cc52017-03-07 04:21:04 -0800654 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
655 thumbnail_receive_configs_[i].Copy()));
656 }
657}
658
659void VideoQualityTest::DestroyThumbnailStreams() {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100660 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_) {
ilnika014cc52017-03-07 04:21:04 -0800661 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100662 }
ilnika014cc52017-03-07 04:21:04 -0800663 thumbnail_send_streams_.clear();
664 for (VideoReceiveStream* thumbnail_receive_stream :
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100665 thumbnail_receive_streams_) {
ilnika014cc52017-03-07 04:21:04 -0800666 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100667 }
ilnika014cc52017-03-07 04:21:04 -0800668 thumbnail_send_streams_.clear();
669 thumbnail_receive_streams_.clear();
Sebastian Janssonf1f363f2018-08-13 14:24:58 +0200670 for (std::unique_ptr<test::TestVideoCapturer>& video_caputurer :
eladalon413ee9a2017-08-22 04:02:52 -0700671 thumbnail_capturers_) {
672 video_caputurer.reset();
673 }
ilnika014cc52017-03-07 04:21:04 -0800674}
675
ilnika014cc52017-03-07 04:21:04 -0800676void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
677 VideoStream thumbnail = DefaultThumbnailStream();
678 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
679 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
680 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200681 absl::nullopt, absl::nullopt, thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -0800682 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -0800683 }
684}
685
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100686std::unique_ptr<test::FrameGenerator> VideoQualityTest::CreateFrameGenerator(
687 size_t video_idx) {
688 // Setup frame generator.
689 const size_t kWidth = 1850;
690 const size_t kHeight = 1110;
691 std::unique_ptr<test::FrameGenerator> frame_generator;
692 if (params_.screenshare[video_idx].generate_slides) {
693 frame_generator = test::FrameGenerator::CreateSlideGenerator(
694 kWidth, kHeight,
695 params_.screenshare[video_idx].slide_change_interval *
696 params_.video[video_idx].fps);
ivica5d6a06c2015-09-17 05:30:24 -0700697 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100698 std::vector<std::string> slides = params_.screenshare[video_idx].slides;
699 if (slides.size() == 0) {
700 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
701 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
702 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
703 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
704 }
705 if (params_.screenshare[video_idx].scroll_duration == 0) {
706 // Cycle image every slide_change_interval seconds.
707 frame_generator = test::FrameGenerator::CreateFromYuvFile(
708 slides, kWidth, kHeight,
709 params_.screenshare[video_idx].slide_change_interval *
710 params_.video[video_idx].fps);
ivica5d6a06c2015-09-17 05:30:24 -0700711 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100712 RTC_CHECK_LE(params_.video[video_idx].width, kWidth);
713 RTC_CHECK_LE(params_.video[video_idx].height, kHeight);
714 RTC_CHECK_GT(params_.screenshare[video_idx].slide_change_interval, 0);
715 const int kPauseDurationMs =
716 (params_.screenshare[video_idx].slide_change_interval -
717 params_.screenshare[video_idx].scroll_duration) *
718 1000;
719 RTC_CHECK_LE(params_.screenshare[video_idx].scroll_duration,
720 params_.screenshare[video_idx].slide_change_interval);
721
722 frame_generator = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
723 clock_, slides, kWidth, kHeight, params_.video[video_idx].width,
724 params_.video[video_idx].height,
725 params_.screenshare[video_idx].scroll_duration * 1000,
726 kPauseDurationMs);
ivica5d6a06c2015-09-17 05:30:24 -0700727 }
728 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100729 return frame_generator;
730}
731
732void VideoQualityTest::CreateCapturers() {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200733 RTC_DCHECK(video_sources_.empty());
734 RTC_DCHECK(video_capturers_.empty());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100735 video_capturers_.resize(num_video_streams_);
736 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
737 if (params_.screenshare[video_idx].enabled) {
738 std::unique_ptr<test::FrameGenerator> frame_generator =
739 CreateFrameGenerator(video_idx);
740 test::FrameGeneratorCapturer* frame_generator_capturer =
741 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator),
742 params_.video[video_idx].fps);
743 EXPECT_TRUE(frame_generator_capturer->Init());
744 video_capturers_[video_idx].reset(frame_generator_capturer);
745 } else {
746 if (params_.video[video_idx].clip_name == "Generator") {
747 video_capturers_[video_idx].reset(test::FrameGeneratorCapturer::Create(
748 static_cast<int>(params_.video[video_idx].width),
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200749 static_cast<int>(params_.video[video_idx].height), absl::nullopt,
750 absl::nullopt, params_.video[video_idx].fps, clock_));
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800751 } else if (params_.video[video_idx].clip_name == "GeneratorI420A") {
752 video_capturers_[video_idx].reset(test::FrameGeneratorCapturer::Create(
753 static_cast<int>(params_.video[video_idx].width),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100754 static_cast<int>(params_.video[video_idx].height),
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200755 test::FrameGenerator::OutputType::I420A, absl::nullopt,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100756 params_.video[video_idx].fps, clock_));
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700757 } else if (params_.video[video_idx].clip_name == "GeneratorI010") {
758 video_capturers_[video_idx].reset(test::FrameGeneratorCapturer::Create(
759 static_cast<int>(params_.video[video_idx].width),
760 static_cast<int>(params_.video[video_idx].height),
761 test::FrameGenerator::OutputType::I010, absl::nullopt,
762 params_.video[video_idx].fps, clock_));
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100763 } else if (params_.video[video_idx].clip_name.empty()) {
764 video_capturers_[video_idx].reset(test::VcmCapturer::Create(
765 params_.video[video_idx].width, params_.video[video_idx].height,
766 params_.video[video_idx].fps,
767 params_.video[video_idx].capture_device_index));
768 if (!video_capturers_[video_idx]) {
769 // Failed to get actual camera, use chroma generator as backup.
770 video_capturers_[video_idx].reset(
771 test::FrameGeneratorCapturer::Create(
772 static_cast<int>(params_.video[video_idx].width),
773 static_cast<int>(params_.video[video_idx].height),
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200774 absl::nullopt, absl::nullopt, params_.video[video_idx].fps,
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800775 clock_));
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100776 }
777 } else {
778 video_capturers_[video_idx].reset(
779 test::FrameGeneratorCapturer::CreateFromYuvFile(
780 test::ResourcePath(params_.video[video_idx].clip_name, "yuv"),
781 params_.video[video_idx].width, params_.video[video_idx].height,
782 params_.video[video_idx].fps, clock_));
783 ASSERT_TRUE(video_capturers_[video_idx])
784 << "Could not create capturer for "
785 << params_.video[video_idx].clip_name
786 << ".yuv. Is this resource file present?";
787 }
788 }
789 RTC_DCHECK(video_capturers_[video_idx].get());
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200790 video_sources_.push_back(video_capturers_[video_idx].get());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100791 }
ivica5d6a06c2015-09-17 05:30:24 -0700792}
793
Christoffer Rodbro39a44b22018-08-07 17:07:24 +0200794void VideoQualityTest::StartAudioStreams() {
795 audio_send_stream_->Start();
796 for (AudioReceiveStream* audio_recv_stream : audio_receive_streams_)
797 audio_recv_stream->Start();
798}
799
800void VideoQualityTest::StartThumbnailCapture() {
Sebastian Janssonf1f363f2018-08-13 14:24:58 +0200801 for (std::unique_ptr<test::TestVideoCapturer>& capturer :
802 thumbnail_capturers_)
Christoffer Rodbro39a44b22018-08-07 17:07:24 +0200803 capturer->Start();
804}
805
806void VideoQualityTest::StopThumbnailCapture() {
Sebastian Janssonf1f363f2018-08-13 14:24:58 +0200807 for (std::unique_ptr<test::TestVideoCapturer>& capturer :
808 thumbnail_capturers_)
Christoffer Rodbro39a44b22018-08-07 17:07:24 +0200809 capturer->Stop();
810}
811
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200812void VideoQualityTest::StartThumbnails() {
813 for (VideoSendStream* send_stream : thumbnail_send_streams_)
814 send_stream->Start();
815 for (VideoReceiveStream* receive_stream : thumbnail_receive_streams_)
816 receive_stream->Start();
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200817}
818
819void VideoQualityTest::StopThumbnails() {
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200820 for (VideoReceiveStream* receive_stream : thumbnail_receive_streams_)
821 receive_stream->Stop();
822 for (VideoSendStream* send_stream : thumbnail_send_streams_)
823 send_stream->Stop();
824}
825
Christoffer Rodbrob4bb4eb2017-11-13 13:03:52 +0100826std::unique_ptr<test::LayerFilteringTransport>
827VideoQualityTest::CreateSendTransport() {
Artem Titove269cb42018-08-29 09:59:23 +0200828 std::unique_ptr<NetworkSimulationInterface> simulated_network = nullptr;
829 if (injection_components_->sender_network == nullptr) {
830 simulated_network = absl::make_unique<SimulatedNetwork>(*params_.config);
831 } else {
832 simulated_network = std::move(injection_components_->sender_network);
833 }
Karl Wiberg918f50c2018-07-05 11:40:33 +0200834 return absl::make_unique<test::LayerFilteringTransport>(
Artem Titov4e199e92018-08-20 13:30:39 +0200835 &task_queue_,
Artem Titove269cb42018-08-29 09:59:23 +0200836 absl::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(),
837 std::move(simulated_network)),
Artem Titov4e199e92018-08-20 13:30:39 +0200838 sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
839 params_.video[0].selected_tl, params_.ss[0].selected_sl,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100840 payload_type_map_, kVideoSendSsrcs[0],
841 static_cast<uint32_t>(kVideoSendSsrcs[0] + params_.ss[0].streams.size() -
842 1));
Christoffer Rodbrob4bb4eb2017-11-13 13:03:52 +0100843}
844
845std::unique_ptr<test::DirectTransport>
846VideoQualityTest::CreateReceiveTransport() {
Artem Titove269cb42018-08-29 09:59:23 +0200847 std::unique_ptr<NetworkSimulationInterface> simulated_network = nullptr;
848 if (injection_components_->receiver_network == nullptr) {
849 simulated_network = absl::make_unique<SimulatedNetwork>(*params_.config);
850 } else {
851 simulated_network = std::move(injection_components_->receiver_network);
852 }
Karl Wiberg918f50c2018-07-05 11:40:33 +0200853 return absl::make_unique<test::DirectTransport>(
Artem Titovdd2eebe2018-08-20 13:27:45 +0200854 &task_queue_,
Artem Titove269cb42018-08-29 09:59:23 +0200855 absl::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(),
856 std::move(simulated_network)),
Artem Titovdd2eebe2018-08-20 13:27:45 +0200857 receiver_call_.get(), payload_type_map_);
Christoffer Rodbrob4bb4eb2017-11-13 13:03:52 +0100858}
859
sprang7a975f72015-10-12 06:33:21 -0700860void VideoQualityTest::RunWithAnalyzer(const Params& params) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100861 num_video_streams_ = params.call.dual_video ? 2 : 1;
eladalon413ee9a2017-08-22 04:02:52 -0700862 std::unique_ptr<test::LayerFilteringTransport> send_transport;
863 std::unique_ptr<test::DirectTransport> recv_transport;
864 FILE* graph_data_output_file = nullptr;
865 std::unique_ptr<VideoAnalyzer> analyzer;
866
sprangce4aef12015-11-02 07:23:20 -0800867 params_ = params;
ivica5d6a06c2015-09-17 05:30:24 -0700868 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
869 // differentiate between the analyzer and the renderer case.
Artem Titove269cb42018-08-29 09:59:23 +0200870 CheckParamsAndInjectionComponents();
ivica5d6a06c2015-09-17 05:30:24 -0700871
sprangce4aef12015-11-02 07:23:20 -0800872 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -0700873 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -0800874 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +0200875 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -0800876 << "Can't open the file " << params_.analyzer.graph_data_output_filename
877 << "!";
ivica87f83a92015-10-08 05:13:32 -0700878 }
sprang7a975f72015-10-12 06:33:21 -0700879
ilnik98436952017-07-13 00:47:03 -0700880 if (!params.logging.rtc_event_log_name.empty()) {
Bjorn Terelius0a6a2b72018-01-19 17:52:38 +0100881 send_event_log_ = RtcEventLog::Create(RtcEventLog::EncodingType::Legacy);
882 recv_event_log_ = RtcEventLog::Create(RtcEventLog::EncodingType::Legacy);
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100883 std::unique_ptr<RtcEventLogOutputFile> send_output(
Karl Wiberg918f50c2018-07-05 11:40:33 +0200884 absl::make_unique<RtcEventLogOutputFile>(
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100885 params.logging.rtc_event_log_name + "_send",
886 RtcEventLog::kUnlimitedOutput));
887 std::unique_ptr<RtcEventLogOutputFile> recv_output(
Karl Wiberg918f50c2018-07-05 11:40:33 +0200888 absl::make_unique<RtcEventLogOutputFile>(
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100889 params.logging.rtc_event_log_name + "_recv",
890 RtcEventLog::kUnlimitedOutput));
891 bool event_log_started =
892 send_event_log_->StartLogging(std::move(send_output),
893 RtcEventLog::kImmediateOutput) &&
894 recv_event_log_->StartLogging(std::move(recv_output),
895 RtcEventLog::kImmediateOutput);
ilnik98436952017-07-13 00:47:03 -0700896 RTC_DCHECK(event_log_started);
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100897 } else {
898 send_event_log_ = RtcEventLog::CreateNull();
899 recv_event_log_ = RtcEventLog::CreateNull();
ilnik98436952017-07-13 00:47:03 -0700900 }
901
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100902 Call::Config send_call_config(send_event_log_.get());
903 Call::Config recv_call_config(recv_event_log_.get());
904 send_call_config.bitrate_config = params.call.call_bitrate_config;
905 recv_call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -0700906
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100907 task_queue_.SendTask([this, &send_call_config, &recv_call_config,
908 &send_transport, &recv_transport]() {
Christoffer Rodbro39a44b22018-08-07 17:07:24 +0200909 if (params_.audio.enabled)
henrika255750b2018-08-27 16:13:37 +0200910 InitializeAudioDevice(
911 &send_call_config, &recv_call_config, params_.audio.use_real_adm);
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200912
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100913 CreateCalls(send_call_config, recv_call_config);
914 send_transport = CreateSendTransport();
915 recv_transport = CreateReceiveTransport();
916 });
stefanf116bd02015-10-27 08:29:42 -0700917
sprangce4aef12015-11-02 07:23:20 -0800918 std::string graph_title = params_.analyzer.graph_title;
919 if (graph_title.empty())
920 graph_title = VideoQualityTest::GenerateGraphTitle();
sprangc1b57a12017-02-28 08:50:47 -0800921 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
Karl Wiberg918f50c2018-07-05 11:40:33 +0200922 analyzer = absl::make_unique<VideoAnalyzer>(
eladalon413ee9a2017-08-22 04:02:52 -0700923 send_transport.get(), params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -0800924 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -0800925 is_quick_test_enabled
926 ? kFramesSentInQuickTest
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100927 : params_.analyzer.test_durations_secs * params_.video[0].fps,
sprangce4aef12015-11-02 07:23:20 -0800928 graph_data_output_file, graph_title,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100929 kVideoSendSsrcs[params_.ss[0].selected_stream],
930 kSendRtxSsrcs[params_.ss[0].selected_stream],
931 static_cast<size_t>(params_.ss[0].selected_stream),
932 params.ss[0].selected_sl, params_.video[0].selected_tl,
933 is_quick_test_enabled, clock_, params_.logging.rtp_dump_name);
ivica5d6a06c2015-09-17 05:30:24 -0700934
eladalon413ee9a2017-08-22 04:02:52 -0700935 task_queue_.SendTask([&]() {
936 analyzer->SetCall(sender_call_.get());
937 analyzer->SetReceiver(receiver_call_->Receiver());
938 send_transport->SetReceiver(analyzer.get());
939 recv_transport->SetReceiver(sender_call_->Receiver());
ivica5d6a06c2015-09-17 05:30:24 -0700940
eladalon413ee9a2017-08-22 04:02:52 -0700941 SetupVideo(analyzer.get(), recv_transport.get());
942 SetupThumbnails(analyzer.get(), recv_transport.get());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100943 video_receive_configs_[params_.ss[0].selected_stream].renderer =
eladalon413ee9a2017-08-22 04:02:52 -0700944 analyzer.get();
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200945 GetVideoSendConfig()->pre_encode_callback = analyzer->pre_encode_proxy();
946 RTC_DCHECK(!GetVideoSendConfig()->post_encode_callback);
947 GetVideoSendConfig()->post_encode_callback = analyzer.get();
kthelgason2bc68642017-02-07 07:02:22 -0800948
eladalon413ee9a2017-08-22 04:02:52 -0700949 CreateFlexfecStreams();
950 CreateVideoStreams();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100951 analyzer->SetSendStream(video_send_streams_[0]);
eladalon413ee9a2017-08-22 04:02:52 -0700952 if (video_receive_streams_.size() == 1)
953 analyzer->SetReceiveStream(video_receive_streams_[0]);
ivica5d6a06c2015-09-17 05:30:24 -0700954
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200955 GetVideoSendStream()->SetSource(analyzer->OutputInterface(),
956 degradation_preference_);
eladalon413ee9a2017-08-22 04:02:52 -0700957 SetupThumbnailCapturers(params_.call.num_thumbnails);
958 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
959 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
960 degradation_preference_);
961 }
ilnika014cc52017-03-07 04:21:04 -0800962
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100963 CreateCapturers();
ivicac1cc8542015-10-08 03:44:06 -0700964
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100965 analyzer->SetSource(video_capturers_[0].get(), params_.ss[0].infer_streams);
ilnika014cc52017-03-07 04:21:04 -0800966
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100967 for (size_t video_idx = 1; video_idx < num_video_streams_; ++video_idx) {
968 video_send_streams_[video_idx]->SetSource(
969 video_capturers_[video_idx].get(), degradation_preference_);
970 }
971
Sebastian Jansson3bd2c792018-07-13 13:29:03 +0200972 StartEncodedFrameLogs(GetVideoSendStream());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +0100973 StartEncodedFrameLogs(
974 video_receive_streams_[params_.ss[0].selected_stream]);
ilnika014cc52017-03-07 04:21:04 -0800975
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200976 if (params_.audio.enabled) {
977 SetupAudio(send_transport.get());
Christoffer Rodbro39a44b22018-08-07 17:07:24 +0200978 StartAudioStreams();
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200979 analyzer->SetAudioReceiveStream(audio_receive_streams_[0]);
980 }
Christoffer Rodbro39a44b22018-08-07 17:07:24 +0200981 StartVideoStreams();
982 StartThumbnails();
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200983 analyzer->StartMeasuringCpuProcessTime();
Christoffer Rodbro39a44b22018-08-07 17:07:24 +0200984 StartVideoCapture();
985 StartThumbnailCapture();
eladalon413ee9a2017-08-22 04:02:52 -0700986 });
ivica5d6a06c2015-09-17 05:30:24 -0700987
eladalon413ee9a2017-08-22 04:02:52 -0700988 analyzer->Wait();
ivica5d6a06c2015-09-17 05:30:24 -0700989
eladalon413ee9a2017-08-22 04:02:52 -0700990 task_queue_.SendTask([&]() {
Christoffer Rodbro39a44b22018-08-07 17:07:24 +0200991 StopThumbnailCapture();
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200992 StopThumbnails();
993 Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700994
eladalon413ee9a2017-08-22 04:02:52 -0700995 DestroyStreams();
996 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -0700997
eladalon413ee9a2017-08-22 04:02:52 -0700998 if (graph_data_output_file)
999 fclose(graph_data_output_file);
1000
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001001 video_capturers_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07001002 send_transport.reset();
1003 recv_transport.reset();
1004
1005 DestroyCalls();
1006 });
ivica5d6a06c2015-09-17 05:30:24 -07001007}
1008
henrika255750b2018-08-27 16:13:37 +02001009rtc::scoped_refptr<AudioDeviceModule> VideoQualityTest::CreateAudioDevice() {
1010#ifdef WEBRTC_WIN
1011 RTC_LOG(INFO) << "Using latest version of ADM on Windows";
1012 // We must initialize the COM library on a thread before we calling any of
1013 // the library functions. All COM functions in the ADM will return
1014 // CO_E_NOTINITIALIZED otherwise. The legacy ADM for Windows used internal
1015 // COM initialization but the new ADM requires COM to be initialized
1016 // externally.
1017 com_initializer_ = absl::make_unique<webrtc_win::ScopedCOMInitializer>(
1018 webrtc_win::ScopedCOMInitializer::kMTA);
1019 RTC_CHECK(com_initializer_->Succeeded());
1020 RTC_CHECK(webrtc_win::core_audio_utility::IsSupported());
1021 RTC_CHECK(webrtc_win::core_audio_utility::IsMMCSSSupported());
1022 return CreateWindowsCoreAudioAudioDeviceModule();
1023#else
1024 // Use legacy factory method on all platforms except Windows.
1025 return AudioDeviceModule::Create(AudioDeviceModule::kPlatformDefaultAudio);
1026#endif
1027}
1028
Christoffer Rodbroc2a02882018-08-07 14:10:56 +02001029void VideoQualityTest::InitializeAudioDevice(Call::Config* send_call_config,
henrika255750b2018-08-27 16:13:37 +02001030 Call::Config* recv_call_config,
1031 bool use_real_adm) {
1032 rtc::scoped_refptr<AudioDeviceModule> audio_device;
1033 if (use_real_adm) {
1034 // Run test with real ADM (using default audio devices) if user has
1035 // explicitly set the --audio and --use_real_adm command-line flags.
1036 audio_device = CreateAudioDevice();
1037 } else {
1038 // By default, create a test ADM which fakes audio.
1039 audio_device = TestAudioDeviceModule::CreateTestAudioDeviceModule(
1040 TestAudioDeviceModule::CreatePulsedNoiseCapturer(32000, 48000),
1041 TestAudioDeviceModule::CreateDiscardRenderer(48000), 1.f);
1042 }
1043 RTC_CHECK(audio_device);
Christoffer Rodbroc2a02882018-08-07 14:10:56 +02001044
1045 AudioState::Config audio_state_config;
1046 audio_state_config.audio_mixer = AudioMixerImpl::Create();
1047 audio_state_config.audio_processing = AudioProcessingBuilder().Create();
henrika255750b2018-08-27 16:13:37 +02001048 audio_state_config.audio_device_module = audio_device;
Christoffer Rodbroc2a02882018-08-07 14:10:56 +02001049 send_call_config->audio_state = AudioState::Create(audio_state_config);
Christoffer Rodbroc2a02882018-08-07 14:10:56 +02001050 recv_call_config->audio_state = AudioState::Create(audio_state_config);
henrika255750b2018-08-27 16:13:37 +02001051 if (use_real_adm) {
1052 // The real ADM requires extra initialization: setting default devices,
1053 // setting up number of channels etc. Helper class also calls
1054 // AudioDeviceModule::Init().
1055 webrtc::adm_helpers::Init(audio_device.get());
1056 } else {
1057 audio_device->Init();
1058 }
1059 // Always initialize the ADM before injecting a valid audio transport.
1060 RTC_CHECK(audio_device->RegisterAudioCallback(
1061 send_call_config->audio_state->audio_transport()) == 0);
Christoffer Rodbroc2a02882018-08-07 14:10:56 +02001062}
1063
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001064void VideoQualityTest::SetupAudio(Transport* transport) {
1065 AudioSendStream::Config audio_send_config(transport);
1066 audio_send_config.rtp.ssrc = kAudioSendSsrc;
minyuea27172d2016-11-01 05:59:29 -07001067
1068 // Add extension to enable audio send side BWE, and allow audio bit rate
1069 // adaptation.
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001070 audio_send_config.rtp.extensions.clear();
1071 audio_send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
Oskar Sundbom8e07c132018-01-08 16:45:42 +01001072 kAudioSendPayloadType,
Yves Gerey665174f2018-06-19 15:03:05 +02001073 {"OPUS",
1074 48000,
1075 2,
1076 {{"usedtx", (params_.audio.dtx ? "1" : "0")}, {"stereo", "1"}}});
minyuea27172d2016-11-01 05:59:29 -07001077
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001078 if (params_.call.send_side_bwe) {
1079 audio_send_config.rtp.extensions.push_back(
1080 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1081 test::kTransportSequenceNumberExtensionId));
1082 audio_send_config.min_bitrate_bps = kOpusMinBitrateBps;
1083 audio_send_config.max_bitrate_bps = kOpusBitrateFbBps;
1084 audio_send_config.send_codec_spec->transport_cc_enabled = true;
1085 }
1086 audio_send_config.encoder_factory = audio_encoder_factory_;
1087 SetAudioConfig(audio_send_config);
1088
Christoffer Rodbroc2a02882018-08-07 14:10:56 +02001089 std::string sync_group;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001090 if (params_.video[0].enabled && params_.audio.sync_video)
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001091 sync_group = kSyncGroup;
minyuea27172d2016-11-01 05:59:29 -07001092
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001093 CreateMatchingAudioConfigs(transport, sync_group);
1094 CreateAudioStreams();
minyuea27172d2016-11-01 05:59:29 -07001095}
1096
minyue73208662016-08-18 06:28:55 -07001097void VideoQualityTest::RunWithRenderers(const Params& params) {
henrika255750b2018-08-27 16:13:37 +02001098 RTC_LOG(INFO) << __FUNCTION__;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001099 num_video_streams_ = params.call.dual_video ? 2 : 1;
eladalon413ee9a2017-08-22 04:02:52 -07001100 std::unique_ptr<test::LayerFilteringTransport> send_transport;
1101 std::unique_ptr<test::DirectTransport> recv_transport;
minyuea27172d2016-11-01 05:59:29 -07001102 std::unique_ptr<test::VideoRenderer> local_preview;
eladalon413ee9a2017-08-22 04:02:52 -07001103 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers;
Sebastian Jansson8e6602f2018-07-13 10:43:20 +02001104 RtcEventLogNullImpl null_event_log;
minyue73208662016-08-18 06:28:55 -07001105
eladalon413ee9a2017-08-22 04:02:52 -07001106 task_queue_.SendTask([&]() {
1107 params_ = params;
Artem Titove269cb42018-08-29 09:59:23 +02001108 CheckParamsAndInjectionComponents();
palmkviste75f2042016-09-28 06:19:48 -07001109
eladalon413ee9a2017-08-22 04:02:52 -07001110 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1111 // match the full stack tests.
Christoffer Rodbro39a44b22018-08-07 17:07:24 +02001112 Call::Config send_call_config(&null_event_log);
1113 send_call_config.bitrate_config = params_.call.call_bitrate_config;
Christoffer Rodbroc2a02882018-08-07 14:10:56 +02001114 Call::Config recv_call_config(&null_event_log);
ivica5d6a06c2015-09-17 05:30:24 -07001115
Christoffer Rodbro39a44b22018-08-07 17:07:24 +02001116 if (params_.audio.enabled)
henrika255750b2018-08-27 16:13:37 +02001117 InitializeAudioDevice(
1118 &send_call_config, &recv_call_config, params_.audio.use_real_adm);
minyue73208662016-08-18 06:28:55 -07001119
Christoffer Rodbro39a44b22018-08-07 17:07:24 +02001120 CreateCalls(send_call_config, recv_call_config);
eladalon413ee9a2017-08-22 04:02:52 -07001121
1122 // TODO(minyue): consider if this is a good transport even for audio only
1123 // calls.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001124 send_transport = CreateSendTransport();
eladalon413ee9a2017-08-22 04:02:52 -07001125
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001126 recv_transport = CreateReceiveTransport();
eladalon413ee9a2017-08-22 04:02:52 -07001127
1128 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1129 // least share as much code as possible. That way this test would also match
1130 // the full stack tests better.
1131 send_transport->SetReceiver(receiver_call_->Receiver());
1132 recv_transport->SetReceiver(sender_call_->Receiver());
1133
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001134 if (params_.video[0].enabled) {
eladalon413ee9a2017-08-22 04:02:52 -07001135 // Create video renderers.
1136 local_preview.reset(test::VideoRenderer::Create(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001137 "Local Preview", params_.video[0].width, params_.video[0].height));
eladalon413ee9a2017-08-22 04:02:52 -07001138
1139 SetupVideo(send_transport.get(), recv_transport.get());
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001140 GetVideoSendConfig()->pre_encode_callback = local_preview.get();
eladalon413ee9a2017-08-22 04:02:52 -07001141
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001142 size_t num_streams_processed = 0;
1143 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1144 const size_t selected_stream_id = params_.ss[video_idx].selected_stream;
1145 const size_t num_streams = params_.ss[video_idx].streams.size();
1146 if (selected_stream_id == num_streams) {
1147 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
1148 std::ostringstream oss;
1149 oss << "Loopback Video #" << video_idx << " - Stream #"
1150 << static_cast<int>(stream_id);
1151 loopback_renderers.emplace_back(test::VideoRenderer::Create(
1152 oss.str().c_str(),
1153 params_.ss[video_idx].streams[stream_id].width,
1154 params_.ss[video_idx].streams[stream_id].height));
1155 video_receive_configs_[stream_id + num_streams_processed].renderer =
1156 loopback_renderers.back().get();
1157 if (params_.audio.enabled && params_.audio.sync_video)
1158 video_receive_configs_[stream_id + num_streams_processed]
1159 .sync_group = kSyncGroup;
1160 }
1161 } else {
1162 std::ostringstream oss;
1163 oss << "Loopback Video #" << video_idx;
1164 loopback_renderers.emplace_back(test::VideoRenderer::Create(
1165 oss.str().c_str(),
1166 params_.ss[video_idx].streams[selected_stream_id].width,
1167 params_.ss[video_idx].streams[selected_stream_id].height));
1168 video_receive_configs_[selected_stream_id + num_streams_processed]
1169 .renderer = loopback_renderers.back().get();
eladalon413ee9a2017-08-22 04:02:52 -07001170 if (params_.audio.enabled && params_.audio.sync_video)
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001171 video_receive_configs_[num_streams_processed + selected_stream_id]
1172 .sync_group = kSyncGroup;
eladalon413ee9a2017-08-22 04:02:52 -07001173 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001174 num_streams_processed += num_streams;
eladalon413ee9a2017-08-22 04:02:52 -07001175 }
eladalon413ee9a2017-08-22 04:02:52 -07001176 CreateFlexfecStreams();
1177 CreateVideoStreams();
1178
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001179 CreateCapturers();
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001180 ConnectVideoSourcesToStreams();
eladalon413ee9a2017-08-22 04:02:52 -07001181 }
1182
1183 if (params_.audio.enabled) {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001184 SetupAudio(send_transport.get());
eladalon413ee9a2017-08-22 04:02:52 -07001185 }
1186
1187 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1188 StartEncodedFrameLogs(receive_stream);
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001189 StartEncodedFrameLogs(GetVideoSendStream());
1190 Start();
eladalon413ee9a2017-08-22 04:02:52 -07001191 });
minyue73208662016-08-18 06:28:55 -07001192
ivica5d6a06c2015-09-17 05:30:24 -07001193 test::PressEnterToContinue();
1194
eladalon413ee9a2017-08-22 04:02:52 -07001195 task_queue_.SendTask([&]() {
Sebastian Jansson3bd2c792018-07-13 13:29:03 +02001196 Stop();
1197 DestroyStreams();
minyue73208662016-08-18 06:28:55 -07001198
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001199 video_capturers_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07001200 send_transport.reset();
1201 recv_transport.reset();
sprang1168fd42017-06-21 09:00:17 -07001202
eladalon413ee9a2017-08-22 04:02:52 -07001203 local_preview.reset();
1204 loopback_renderers.clear();
1205
1206 DestroyCalls();
1207 });
ivica5d6a06c2015-09-17 05:30:24 -07001208}
1209
palmkviste75f2042016-09-28 06:19:48 -07001210void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
ilnik98436952017-07-13 00:47:03 -07001211 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001212 std::ostringstream str;
1213 str << send_logs_++;
1214 std::string prefix =
ilnik98436952017-07-13 00:47:03 -07001215 params_.logging.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07001216 stream->EnableEncodedFrameRecording(
1217 std::vector<rtc::PlatformFile>(
1218 {rtc::CreatePlatformFile(prefix + "1.ivf"),
1219 rtc::CreatePlatformFile(prefix + "2.ivf"),
1220 rtc::CreatePlatformFile(prefix + "3.ivf")}),
ilnik98436952017-07-13 00:47:03 -07001221 100000000);
palmkviste75f2042016-09-28 06:19:48 -07001222 }
1223}
ilnikcb8c1462017-03-09 09:23:30 -08001224
palmkviste75f2042016-09-28 06:19:48 -07001225void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
ilnik98436952017-07-13 00:47:03 -07001226 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001227 std::ostringstream str;
1228 str << receive_logs_++;
1229 std::string path =
ilnik98436952017-07-13 00:47:03 -07001230 params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07001231 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
ilnik98436952017-07-13 00:47:03 -07001232 100000000);
palmkviste75f2042016-09-28 06:19:48 -07001233 }
1234}
ivica5d6a06c2015-09-17 05:30:24 -07001235} // namespace webrtc