blob: 61072663dacd371f23e677a06f2ad5d048bc5f60 [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
Elad Alon83ccca12017-10-04 13:18:26 +020020#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Magnus Jedvert46a27652017-11-13 14:10:02 +010021#include "media/engine/internalencoderfactory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "media/engine/webrtcvideoengine.h"
23#include "modules/audio_mixer/audio_mixer_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/rtp_rtcp/source/rtp_format.h"
25#include "modules/rtp_rtcp/source/rtp_utility.h"
26#include "modules/video_coding/codecs/h264/include/h264.h"
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080027#include "modules/video_coding/codecs/multiplex/include/multiplex_encoder_adapter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/video_coding/codecs/vp8/include/vp8_common_types.h"
29#include "modules/video_coding/codecs/vp9/include/vp9.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/cpu_time.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/flags.h"
32#include "rtc_base/format_macros.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/memory_usage.h"
34#include "rtc_base/pathutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "system_wrappers/include/cpu_info.h"
Mirko Bonadei5f99e652017-12-19 13:40:40 +010036#include "test/rtp_file_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "test/run_loop.h"
38#include "test/statistics.h"
39#include "test/testsupport/fileutils.h"
40#include "test/testsupport/frame_writer.h"
Edward Lemurd1d67432018-01-02 11:54:37 +010041#include "test/testsupport/perf_test.h"
Edward Lemuraf8659a2017-09-27 14:46:24 +020042#include "test/testsupport/test_artifacts.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "test/vcm_capturer.h"
44#include "test/video_renderer.h"
minyue73208662016-08-18 06:28:55 -070045
ilnikee42d192017-08-22 07:16:20 -070046DEFINE_bool(save_worst_frame,
47 false,
48 "Enable saving a frame with the lowest PSNR to a jpeg file in the "
Edward Lemuraf8659a2017-09-27 14:46:24 +020049 "test_artifacts_dir");
ilnikee42d192017-08-22 07:16:20 -070050
minyue73208662016-08-18 06:28:55 -070051namespace {
52
53constexpr int kSendStatsPollingIntervalMs = 1000;
minyue20c84cc2017-04-10 16:57:57 -070054
minyue73208662016-08-18 06:28:55 -070055constexpr size_t kMaxComparisons = 10;
56constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080057constexpr int kOpusMinBitrateBps = 6000;
58constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 00:53:12 -080059constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 04:21:04 -080060constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
61constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 06:28:55 -070062
sprang1168fd42017-06-21 09:00:17 -070063constexpr int kDefaultMaxQp = cricket::WebRtcVideoChannel::kDefaultQpMax;
64
perkjfa10b552016-10-02 23:45:26 -070065class VideoStreamFactory
66 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
67 public:
68 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
69 : streams_(streams) {}
70
71 private:
72 std::vector<webrtc::VideoStream> CreateEncoderStreams(
73 int width,
74 int height,
75 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -080076 // The highest layer must match the incoming resolution.
77 std::vector<webrtc::VideoStream> streams = streams_;
78 streams[streams_.size() - 1].height = height;
79 streams[streams_.size() - 1].width = width;
Seth Hampson24722b32017-12-22 09:36:42 -080080
81 streams[0].bitrate_priority = encoder_config.bitrate_priority;
mflodmand79f97b2016-12-15 07:24:33 -080082 return streams;
perkjfa10b552016-10-02 23:45:26 -070083 }
84
85 std::vector<webrtc::VideoStream> streams_;
86};
87
brandtr504b95e2016-12-21 02:54:35 -080088bool IsFlexfec(int payload_type) {
89 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
90}
91
minyue73208662016-08-18 06:28:55 -070092} // namespace
ivica5d6a06c2015-09-17 05:30:24 -070093
94namespace webrtc {
95
ivica5d6a06c2015-09-17 05:30:24 -070096class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -070097 public Transport,
ilnik1e7732c2017-02-23 05:07:56 -080098 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 05:30:24 -070099 public:
sprangce4aef12015-11-02 07:23:20 -0800100 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700101 const std::string& test_label,
102 double avg_psnr_threshold,
103 double avg_ssim_threshold,
104 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800105 FILE* graph_data_output_file,
106 const std::string& graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -0800107 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 09:16:05 -0800108 uint32_t rtx_ssrc_to_analyze,
ilnikcb8c1462017-03-09 09:23:30 -0800109 size_t selected_stream,
ilnik1e7732c2017-02-23 05:07:56 -0800110 int selected_sl,
111 int selected_tl,
ilnik6b826ef2017-06-16 06:53:48 -0700112 bool is_quick_test_enabled,
ilnik98436952017-07-13 00:47:03 -0700113 Clock* clock,
114 std::string rtp_dump_name)
perkja49cbd32016-09-16 07:53:41 -0700115 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700116 receiver_(nullptr),
stefan889d9652017-07-05 03:03:02 -0700117 call_(nullptr),
ivica5d6a06c2015-09-17 05:30:24 -0700118 send_stream_(nullptr),
philipelfd870db2017-01-23 03:22:15 -0800119 receive_stream_(nullptr),
ilnik6b826ef2017-06-16 06:53:48 -0700120 captured_frame_forwarder_(this, clock),
ivica5d6a06c2015-09-17 05:30:24 -0700121 test_label_(test_label),
122 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800123 graph_title_(graph_title),
124 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 09:16:05 -0800125 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 09:23:30 -0800126 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 05:07:56 -0800127 selected_sl_(selected_sl),
128 selected_tl_(selected_tl),
pbos14fe7082016-04-20 06:35:56 -0700129 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100130 encode_timing_proxy_(this),
stefan889d9652017-07-05 03:03:02 -0700131 last_fec_bytes_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700132 frames_to_process_(duration_frames),
133 frames_recorded_(0),
134 frames_processed_(0),
135 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700136 dropped_frames_before_first_encode_(0),
137 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700138 last_render_time_(0),
Stefan Holmer32e930f2018-02-09 09:43:45 +0100139 last_render_delta_ms_(0),
140 last_unfreeze_time_ms_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700141 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 05:07:56 -0800142 total_media_bytes_(0),
143 first_sending_time_(0),
144 last_sending_time_(0),
ilnikdf92c5c2017-02-23 02:08:44 -0800145 cpu_time_(0),
146 wallclock_time_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700147 avg_psnr_threshold_(avg_psnr_threshold),
148 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 00:53:12 -0800149 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100150 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100151 comparison_available_event_(false, false),
ilnik98436952017-07-13 00:47:03 -0700152 done_(true, false),
153 clock_(clock),
154 start_ms_(clock->TimeInMilliseconds()) {
ivica5d6a06c2015-09-17 05:30:24 -0700155 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
156
157 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
158 // so that we don't accidentally starve "real" worker threads (codec etc).
159 // Also, don't allocate more than kMaxComparisonThreads, even if there are
160 // spare cores.
161
162 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800163 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700164 static const uint32_t kMinCoresLeft = 4;
165 static const uint32_t kMaxComparisonThreads = 8;
166
167 if (num_cores <= kMinCoresLeft) {
168 num_cores = 1;
169 } else {
170 num_cores -= kMinCoresLeft;
171 num_cores = std::min(num_cores, kMaxComparisonThreads);
172 }
173
174 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100175 rtc::PlatformThread* thread =
176 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
177 thread->Start();
178 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700179 }
ilnik98436952017-07-13 00:47:03 -0700180
181 if (!rtp_dump_name.empty()) {
182 fprintf(stdout, "Writing rtp dump to %s\n", rtp_dump_name.c_str());
183 rtp_file_writer_.reset(test::RtpFileWriter::Create(
184 test::RtpFileWriter::kRtpDump, rtp_dump_name));
185 }
ivica5d6a06c2015-09-17 05:30:24 -0700186 }
187
188 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100189 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
190 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700191 delete thread;
192 }
193 }
194
195 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
196
ilnik6b826ef2017-06-16 06:53:48 -0700197 void SetSource(test::VideoCapturer* video_capturer, bool respect_sink_wants) {
198 if (respect_sink_wants)
199 captured_frame_forwarder_.SetSource(video_capturer);
200 rtc::VideoSinkWants wants;
201 video_capturer->AddOrUpdateSink(InputInterface(), wants);
202 }
203
stefan889d9652017-07-05 03:03:02 -0700204 void SetCall(Call* call) {
205 rtc::CritScope lock(&crit_);
206 RTC_DCHECK(!call_);
207 call_ = call;
208 }
209
perkja49cbd32016-09-16 07:53:41 -0700210 void SetSendStream(VideoSendStream* stream) {
211 rtc::CritScope lock(&crit_);
212 RTC_DCHECK(!send_stream_);
213 send_stream_ = stream;
214 }
215
philipelfd870db2017-01-23 03:22:15 -0800216 void SetReceiveStream(VideoReceiveStream* stream) {
217 rtc::CritScope lock(&crit_);
218 RTC_DCHECK(!receive_stream_);
219 receive_stream_ = stream;
220 }
221
perkja49cbd32016-09-16 07:53:41 -0700222 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
223 return &captured_frame_forwarder_;
224 }
225 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
226 return &captured_frame_forwarder_;
227 }
228
ivica5d6a06c2015-09-17 05:30:24 -0700229 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100230 rtc::CopyOnWriteBuffer packet,
ivica5d6a06c2015-09-17 05:30:24 -0700231 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700232 // Ignore timestamps of RTCP packets. They're not synchronized with
233 // RTP packet timestamps and so they would confuse wrap_handler_.
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100234 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size())) {
235 return receiver_->DeliverPacket(media_type, std::move(packet),
236 packet_time);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700237 }
ilnik98436952017-07-13 00:47:03 -0700238
239 if (rtp_file_writer_) {
240 test::RtpPacket p;
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100241 memcpy(p.data, packet.cdata(), packet.size());
242 p.length = packet.size();
243 p.original_length = packet.size();
ilnik98436952017-07-13 00:47:03 -0700244 p.time_ms = clock_->TimeInMilliseconds() - start_ms_;
245 rtp_file_writer_->WritePacket(&p);
246 }
247
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100248 RtpUtility::RtpHeaderParser parser(packet.cdata(), packet.size());
ivica5d6a06c2015-09-17 05:30:24 -0700249 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800250 parser.Parse(&header);
ilnik46a00212017-02-10 09:16:05 -0800251 if (!IsFlexfec(header.payloadType) &&
252 (header.ssrc == ssrc_to_analyze_ ||
253 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 02:54:35 -0800254 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
255 // (FlexFEC and media are sent on different SSRCs, which have different
256 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800257 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 05:30:24 -0700258 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800259 int64_t timestamp =
260 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
261 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700262 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
263 }
264
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100265 return receiver_->DeliverPacket(media_type, std::move(packet), packet_time);
ivica5d6a06c2015-09-17 05:30:24 -0700266 }
267
pbos14fe7082016-04-20 06:35:56 -0700268 void PreEncodeOnFrame(const VideoFrame& video_frame) {
269 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800270 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 06:35:56 -0700271 while (frames_.front().timestamp() != video_frame.timestamp()) {
272 ++dropped_frames_before_first_encode_;
273 frames_.pop_front();
274 RTC_CHECK(!frames_.empty());
275 }
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100276 first_encoded_timestamp_ = video_frame.timestamp();
ilnik3dd5ad92017-02-09 04:58:53 -0800277 }
278 }
279
280 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
281 rtc::CritScope lock(&crit_);
282 if (!first_sent_timestamp_ &&
ilnikcb8c1462017-03-09 09:23:30 -0800283 encoded_frame.stream_id_ == selected_stream_) {
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100284 first_sent_timestamp_ = encoded_frame.timestamp_;
pbos14fe7082016-04-20 06:35:56 -0700285 }
286 }
287
stefan1d8a5062015-10-02 03:39:33 -0700288 bool SendRtp(const uint8_t* packet,
289 size_t length,
290 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800291 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700292 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800293 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700294
sprangce4aef12015-11-02 07:23:20 -0800295 int64_t current_time =
296 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 02:23:28 -0800297
sprangce4aef12015-11-02 07:23:20 -0800298 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700299 {
300 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800301 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 08:32:53 -0800302 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 04:58:53 -0800303 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 04:08:56 -0800304 }
ilnik3dd5ad92017-02-09 04:58:53 -0800305
306 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 02:54:35 -0800307 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
308 // (FlexFEC and media are sent on different SSRCs, which have different
309 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800310 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 02:54:35 -0800311 int64_t timestamp =
312 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
313 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 05:07:56 -0800314
315 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 02:54:35 -0800316 encoded_frame_sizes_[timestamp] +=
317 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 05:07:56 -0800318 total_media_bytes_ +=
319 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 02:54:35 -0800320 }
ilnik1e7732c2017-02-23 05:07:56 -0800321 if (first_sending_time_ == 0)
322 first_sending_time_ = current_time;
323 last_sending_time_ = current_time;
sprangce4aef12015-11-02 07:23:20 -0800324 }
ivica5d6a06c2015-09-17 05:30:24 -0700325 }
sprangce4aef12015-11-02 07:23:20 -0800326 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700327 }
328
329 bool SendRtcp(const uint8_t* packet, size_t length) override {
330 return transport_->SendRtcp(packet, length);
331 }
332
nisseeb83a1a2016-03-21 01:27:56 -0700333 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700334 int64_t render_time_ms =
335 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700336
337 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 02:08:44 -0800338
339 StartExcludingCpuThreadTime();
340
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700341 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800342 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700343
sprang16daaa52016-03-09 01:30:24 -0800344 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700345 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700346 // No previous frame rendered, this one was dropped after sending but
347 // before rendering.
348 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 07:02:22 -0800349 } else {
350 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
351 render_time_ms);
pbos14fe7082016-04-20 06:35:56 -0700352 }
ivica5d6a06c2015-09-17 05:30:24 -0700353 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700354 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700355 }
356
357 VideoFrame reference_frame = frames_.front();
358 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800359 int64_t reference_timestamp =
360 wrap_handler_.Unwrap(reference_frame.timestamp());
361 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800362 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100363 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800364 ++send_timestamp;
365 }
sprang16daaa52016-03-09 01:30:24 -0800366 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700367
368 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
369
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100370 last_rendered_frame_ = video_frame;
ilnikdf92c5c2017-02-23 02:08:44 -0800371
372 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 05:30:24 -0700373 }
374
ivica5d6a06c2015-09-17 05:30:24 -0700375 void Wait() {
376 // Frame comparisons can be very expensive. Wait for test to be done, but
377 // at time-out check if frames_processed is going up. If so, give it more
378 // time, otherwise fail. Hopefully this will reduce test flakiness.
379
Peter Boström8c38e8b2015-11-26 17:45:47 +0100380 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800381
ivica5d6a06c2015-09-17 05:30:24 -0700382 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700383 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100384 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700385 int frames_processed;
386 {
387 rtc::CritScope crit(&comparison_lock_);
388 frames_processed = frames_processed_;
389 }
390
391 // Print some output so test infrastructure won't think we've crashed.
392 const char* kKeepAliveMessages[3] = {
393 "Uh, I'm-I'm not quite dead, sir.",
394 "Uh, I-I think uh, I could pull through, sir.",
395 "Actually, I think I'm all right to come with you--"};
396 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
397
398 if (last_frames_processed == -1) {
399 last_frames_processed = frames_processed;
400 continue;
401 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100402 if (frames_processed == last_frames_processed) {
403 EXPECT_GT(frames_processed, last_frames_processed)
404 << "Analyzer stalled while waiting for test to finish.";
405 done_.Set();
406 break;
407 }
ivica5d6a06c2015-09-17 05:30:24 -0700408 last_frames_processed = frames_processed;
409 }
410
411 if (iteration > 0)
412 printf("- Farewell, sweet Concorde!\n");
413
Peter Boström8c38e8b2015-11-26 17:45:47 +0100414 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700415 }
416
pbos14fe7082016-04-20 06:35:56 -0700417 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
418 return &pre_encode_proxy_;
419 }
Peter Boströme4499152016-02-05 11:13:28 +0100420 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
421
ilnikdf92c5c2017-02-23 02:08:44 -0800422 void StartMeasuringCpuProcessTime() {
423 rtc::CritScope lock(&cpu_measurement_lock_);
424 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
425 wallclock_time_ -= rtc::SystemTimeNanos();
426 }
427
428 void StopMeasuringCpuProcessTime() {
429 rtc::CritScope lock(&cpu_measurement_lock_);
430 cpu_time_ += rtc::GetProcessCpuTimeNanos();
431 wallclock_time_ += rtc::SystemTimeNanos();
432 }
433
434 void StartExcludingCpuThreadTime() {
435 rtc::CritScope lock(&cpu_measurement_lock_);
436 cpu_time_ += rtc::GetThreadCpuTimeNanos();
437 }
438
439 void StopExcludingCpuThreadTime() {
440 rtc::CritScope lock(&cpu_measurement_lock_);
441 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
442 }
443
444 double GetCpuUsagePercent() {
445 rtc::CritScope lock(&cpu_measurement_lock_);
446 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
447 }
448
sprangce4aef12015-11-02 07:23:20 -0800449 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700450 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700451
452 private:
453 struct FrameComparison {
454 FrameComparison()
455 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800456 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700457 send_time_ms(0),
458 recv_time_ms(0),
459 render_time_ms(0),
460 encoded_frame_size(0) {}
461
462 FrameComparison(const VideoFrame& reference,
463 const VideoFrame& render,
464 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800465 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700466 int64_t send_time_ms,
467 int64_t recv_time_ms,
468 int64_t render_time_ms,
469 size_t encoded_frame_size)
470 : reference(reference),
471 render(render),
472 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800473 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700474 send_time_ms(send_time_ms),
475 recv_time_ms(recv_time_ms),
476 render_time_ms(render_time_ms),
477 encoded_frame_size(encoded_frame_size) {}
478
nissedf2ceb82016-12-15 06:29:53 -0800479 FrameComparison(bool dropped,
480 int64_t input_time_ms,
481 int64_t send_time_ms,
482 int64_t recv_time_ms,
483 int64_t render_time_ms,
484 size_t encoded_frame_size)
485 : dropped(dropped),
486 input_time_ms(input_time_ms),
487 send_time_ms(send_time_ms),
488 recv_time_ms(recv_time_ms),
489 render_time_ms(render_time_ms),
490 encoded_frame_size(encoded_frame_size) {}
491
492 rtc::Optional<VideoFrame> reference;
493 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700494 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800495 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700496 int64_t send_time_ms;
497 int64_t recv_time_ms;
498 int64_t render_time_ms;
499 size_t encoded_frame_size;
500 };
501
502 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700503 Sample(int dropped,
504 int64_t input_time_ms,
505 int64_t send_time_ms,
506 int64_t recv_time_ms,
507 int64_t render_time_ms,
508 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700509 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700510 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700511 : dropped(dropped),
512 input_time_ms(input_time_ms),
513 send_time_ms(send_time_ms),
514 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700515 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700516 encoded_frame_size(encoded_frame_size),
517 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700518 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700519
ivica8d15bd62015-10-07 02:43:12 -0700520 int dropped;
521 int64_t input_time_ms;
522 int64_t send_time_ms;
523 int64_t recv_time_ms;
524 int64_t render_time_ms;
525 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700526 double psnr;
527 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700528 };
529
Peter Boströme4499152016-02-05 11:13:28 +0100530 // This class receives the send-side OnEncodeTiming and is provided to not
531 // conflict with the receiver-side pre_decode_callback.
532 class OnEncodeTimingProxy : public EncodedFrameObserver {
533 public:
534 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
535
ilnik3dd5ad92017-02-09 04:58:53 -0800536 void EncodedFrameCallback(const EncodedFrame& frame) override {
537 parent_->PostEncodeFrameCallback(frame);
538 }
Peter Boströme4499152016-02-05 11:13:28 +0100539
540 private:
541 VideoAnalyzer* const parent_;
542 };
543
pbos14fe7082016-04-20 06:35:56 -0700544 // This class receives the send-side OnFrame callback and is provided to not
545 // conflict with the receiver-side renderer callback.
546 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
547 public:
548 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
549
550 void OnFrame(const VideoFrame& video_frame) override {
551 parent_->PreEncodeOnFrame(video_frame);
552 }
553
554 private:
555 VideoAnalyzer* const parent_;
556 };
557
ilnik1e7732c2017-02-23 05:07:56 -0800558 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
559 size_t length,
560 const RTPHeader& header) {
ilnik863f03b2017-07-11 02:38:36 -0700561 if (header.payloadType != test::CallTest::kPayloadTypeVP9 &&
562 header.payloadType != test::CallTest::kPayloadTypeVP8) {
ilnik1e7732c2017-02-23 05:07:56 -0800563 return true;
564 } else {
565 // Get VP8 and VP9 specific header to check layers indexes.
566 const uint8_t* payload = packet + header.headerLength;
567 const size_t payload_length = length - header.headerLength;
568 const size_t payload_data_length = payload_length - header.paddingLength;
ilnik863f03b2017-07-11 02:38:36 -0700569 const bool is_vp8 = header.payloadType == test::CallTest::kPayloadTypeVP8;
ilnik1e7732c2017-02-23 05:07:56 -0800570 std::unique_ptr<RtpDepacketizer> depacketizer(
571 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
572 RtpDepacketizer::ParsedPayload parsed_payload;
573 bool result =
574 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
575 RTC_DCHECK(result);
576 const int temporal_idx = static_cast<int>(
577 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
578 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
579 const int spatial_idx = static_cast<int>(
580 is_vp8 ? kNoSpatialIdx
581 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
582 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
583 temporal_idx <= selected_tl_) &&
584 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
585 spatial_idx <= selected_sl_);
586 }
587 }
588
ivica5d6a06c2015-09-17 05:30:24 -0700589 void AddFrameComparison(const VideoFrame& reference,
590 const VideoFrame& render,
591 bool dropped,
592 int64_t render_time_ms)
danilchapa37de392017-09-09 04:17:22 -0700593 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800594 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
595 int64_t send_time_ms = send_times_[reference_timestamp];
596 send_times_.erase(reference_timestamp);
597 int64_t recv_time_ms = recv_times_[reference_timestamp];
598 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700599
sprangce4aef12015-11-02 07:23:20 -0800600 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800601 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800602 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800603 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800604 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
605 if (it != encoded_frame_sizes_.end())
606 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700607
ivica5d6a06c2015-09-17 05:30:24 -0700608 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700609 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800610 comparisons_.push_back(FrameComparison(reference, render, dropped,
611 reference.ntp_time_ms(),
612 send_time_ms, recv_time_ms,
613 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700614 } else {
nissedf2ceb82016-12-15 06:29:53 -0800615 comparisons_.push_back(FrameComparison(dropped,
616 reference.ntp_time_ms(),
617 send_time_ms, recv_time_ms,
618 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700619 }
Peter Boström5811a392015-12-10 13:02:50 +0100620 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700621 }
622
tommi0f8b4032017-02-22 11:22:05 -0800623 static void PollStatsThread(void* obj) {
624 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 05:30:24 -0700625 }
626
tommi0f8b4032017-02-22 11:22:05 -0800627 void PollStats() {
628 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
629 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700630
stefan889d9652017-07-05 03:03:02 -0700631 Call::Stats call_stats = call_->GetStats();
632 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
633
tommi0f8b4032017-02-22 11:22:05 -0800634 VideoSendStream::Stats send_stats = send_stream_->GetStats();
635 // It's not certain that we yet have estimates for any of these stats.
636 // Check that they are positive before mixing them in.
637 if (send_stats.encode_frame_rate > 0)
638 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
639 if (send_stats.avg_encode_time_ms > 0)
640 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
641 if (send_stats.encode_usage_percent > 0)
642 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
643 if (send_stats.media_bitrate_bps > 0)
644 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
stefan889d9652017-07-05 03:03:02 -0700645 size_t fec_bytes = 0;
646 for (auto kv : send_stats.substreams) {
647 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
648 kv.second.rtp_stats.fec.padding_bytes;
649 }
650 fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8);
651 last_fec_bytes_ = fec_bytes;
philipelfd870db2017-01-23 03:22:15 -0800652
tommi0f8b4032017-02-22 11:22:05 -0800653 if (receive_stream_ != nullptr) {
654 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
655 if (receive_stats.decode_ms > 0)
656 decode_time_ms_.AddSample(receive_stats.decode_ms);
657 if (receive_stats.max_decode_ms > 0)
658 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
659 }
ilnikdaa574d2017-03-01 06:46:05 -0800660
661 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 03:22:15 -0800662 }
ivica5d6a06c2015-09-17 05:30:24 -0700663 }
664
665 static bool FrameComparisonThread(void* obj) {
666 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
667 }
668
669 bool CompareFrames() {
670 if (AllFramesRecorded())
671 return false;
672
ivica5d6a06c2015-09-17 05:30:24 -0700673 FrameComparison comparison;
674
675 if (!PopComparison(&comparison)) {
676 // Wait until new comparison task is available, or test is done.
677 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100678 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700679 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100680 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700681 return false;
682 }
683 return true; // Try again.
684 }
685
ilnikdf92c5c2017-02-23 02:08:44 -0800686 StartExcludingCpuThreadTime();
687
ivica5d6a06c2015-09-17 05:30:24 -0700688 PerformFrameComparison(comparison);
689
ilnikdf92c5c2017-02-23 02:08:44 -0800690 StopExcludingCpuThreadTime();
691
ivica5d6a06c2015-09-17 05:30:24 -0700692 if (FrameProcessed()) {
693 PrintResults();
694 if (graph_data_output_file_)
695 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100696 done_.Set();
697 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700698 return false;
699 }
700
701 return true;
702 }
703
704 bool PopComparison(FrameComparison* comparison) {
705 rtc::CritScope crit(&comparison_lock_);
706 // If AllFramesRecorded() is true, it means we have already popped
707 // frames_to_process_ frames from comparisons_, so there is no more work
708 // for this thread to be done. frames_processed_ might still be lower if
709 // all comparisons are not done, but those frames are currently being
710 // worked on by other threads.
711 if (comparisons_.empty() || AllFramesRecorded())
712 return false;
713
714 *comparison = comparisons_.front();
715 comparisons_.pop_front();
716
717 FrameRecorded();
718 return true;
719 }
720
721 // Increment counter for number of frames received for comparison.
722 void FrameRecorded() {
723 rtc::CritScope crit(&comparison_lock_);
724 ++frames_recorded_;
725 }
726
727 // Returns true if all frames to be compared have been taken from the queue.
728 bool AllFramesRecorded() {
729 rtc::CritScope crit(&comparison_lock_);
730 assert(frames_recorded_ <= frames_to_process_);
731 return frames_recorded_ == frames_to_process_;
732 }
733
734 // Increase count of number of frames processed. Returns true if this was the
735 // last frame to be processed.
736 bool FrameProcessed() {
737 rtc::CritScope crit(&comparison_lock_);
738 ++frames_processed_;
739 assert(frames_processed_ <= frames_to_process_);
740 return frames_processed_ == frames_to_process_;
741 }
742
743 void PrintResults() {
ilnikdf92c5c2017-02-23 02:08:44 -0800744 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -0700745 rtc::CritScope crit(&comparison_lock_);
Stefan Holmer32e930f2018-02-09 09:43:45 +0100746 // Record the time from the last freeze until the last rendered frame to
747 // ensure we cover the full timespan of the session. Otherwise the metric
748 // would penalize an early freeze followed by no freezes until the end.
749 time_between_freezes_.AddSample(last_render_time_ - last_unfreeze_time_ms_);
ivica5d6a06c2015-09-17 05:30:24 -0700750 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800751 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 01:02:15 -0700752 PrintResult("sender_time", sender_time_, " ms");
753 PrintResult("receiver_time", receiver_time_, " ms");
Ying Wang338bd622018-02-09 16:21:26 +0100754 PrintResult("network_time", network_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700755 PrintResult("total_delay_incl_network", end_to_end_, " ms");
756 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700757 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800758 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800759 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
stefan889d9652017-07-05 03:03:02 -0700760 PrintResult("fec_bitrate", fec_bitrate_bps_, " bps");
761 PrintResult("send_bandwidth", send_bandwidth_bps_, " bps");
Stefan Holmer32e930f2018-02-09 09:43:45 +0100762 PrintResult("time_between_freezes", time_between_freezes_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800763
ilnik59cac992017-07-25 05:45:03 -0700764 if (worst_frame_) {
Edward Lemurd1d67432018-01-02 11:54:37 +0100765 test::PrintResult("min_psnr", "", test_label_.c_str(), worst_frame_->psnr,
766 "dB", false);
ilnik59cac992017-07-25 05:45:03 -0700767 }
768
philipelfd870db2017-01-23 03:22:15 -0800769 if (receive_stream_ != nullptr) {
770 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800771 }
ivica5d6a06c2015-09-17 05:30:24 -0700772
Edward Lemurd1d67432018-01-02 11:54:37 +0100773 test::PrintResult("dropped_frames", "", test_label_.c_str(),
774 dropped_frames_, "frames", false);
775 test::PrintResult("cpu_usage", "", test_label_.c_str(),
776 GetCpuUsagePercent(), "%", false);
ilnikdaa574d2017-03-01 06:46:05 -0800777
778#if defined(WEBRTC_WIN)
779 // On Linux and Mac in Resident Set some unused pages may be counted.
780 // Therefore this metric will depend on order in which tests are run and
781 // will be flaky.
782 PrintResult("memory_usage", memory_usage_, " bytes");
783#endif
ilnik59cac992017-07-25 05:45:03 -0700784
ilnikee42d192017-08-22 07:16:20 -0700785 // Saving only the worst frame for manual analysis. Intention here is to
786 // only detect video corruptions and not to track picture quality. Thus,
787 // jpeg is used here.
oprypin9b2f20c2017-08-29 05:51:57 -0700788 if (FLAG_save_worst_frame && worst_frame_) {
ilnikee42d192017-08-22 07:16:20 -0700789 std::string output_dir;
Edward Lemuraf8659a2017-09-27 14:46:24 +0200790 test::GetTestArtifactsDir(&output_dir);
ilnikee42d192017-08-22 07:16:20 -0700791 std::string output_path =
792 rtc::Pathname(output_dir, test_label_ + ".jpg").pathname();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100793 RTC_LOG(LS_INFO) << "Saving worst frame to " << output_path;
ilnikee42d192017-08-22 07:16:20 -0700794 test::JpegFrameWriter frame_writer(output_path);
795 RTC_CHECK(frame_writer.WriteFrame(worst_frame_->frame,
796 100 /*best quality*/));
ilnik59cac992017-07-25 05:45:03 -0700797 }
ilnikdaa574d2017-03-01 06:46:05 -0800798
ilnik9ae0d762017-02-15 00:53:12 -0800799 // Disable quality check for quick test, as quality checks may fail
800 // because too few samples were collected.
801 if (!is_quick_test_enabled_) {
802 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
803 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
804 }
ivica5d6a06c2015-09-17 05:30:24 -0700805 }
806
807 void PerformFrameComparison(const FrameComparison& comparison) {
808 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700809 double psnr = -1.0;
810 double ssim = -1.0;
ilnik6b826ef2017-06-16 06:53:48 -0700811 if (comparison.reference && !comparison.dropped) {
nissedf2ceb82016-12-15 06:29:53 -0800812 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
813 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700814 }
ivica5d6a06c2015-09-17 05:30:24 -0700815
ivica5d6a06c2015-09-17 05:30:24 -0700816 rtc::CritScope crit(&comparison_lock_);
ilnik59cac992017-07-25 05:45:03 -0700817
818 if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) {
819 worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render});
820 }
821
ivica5d6a06c2015-09-17 05:30:24 -0700822 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800823 samples_.push_back(Sample(
824 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
825 comparison.recv_time_ms, comparison.render_time_ms,
826 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700827 }
stefanb1797672016-08-11 07:00:57 -0700828 if (psnr >= 0.0)
829 psnr_.AddSample(psnr);
830 if (ssim >= 0.0)
831 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700832
833 if (comparison.dropped) {
834 ++dropped_frames_;
835 return;
836 }
Stefan Holmer32e930f2018-02-09 09:43:45 +0100837 if (last_unfreeze_time_ms_ == 0)
838 last_unfreeze_time_ms_ = comparison.render_time_ms;
839 if (last_render_time_ != 0) {
840 const int64_t render_delta_ms =
841 comparison.render_time_ms - last_render_time_;
842 rendered_delta_.AddSample(render_delta_ms);
843 if (last_render_delta_ms_ != 0 &&
844 render_delta_ms - last_render_delta_ms_ > 150) {
845 time_between_freezes_.AddSample(last_render_time_ -
846 last_unfreeze_time_ms_);
847 last_unfreeze_time_ms_ = comparison.render_time_ms;
848 }
849 last_render_delta_ms_ = render_delta_ms;
850 }
ivica5d6a06c2015-09-17 05:30:24 -0700851 last_render_time_ = comparison.render_time_ms;
852
nissedf2ceb82016-12-15 06:29:53 -0800853 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800854 if (comparison.recv_time_ms > 0) {
855 // If recv_time_ms == 0, this frame consisted of a packets which were all
856 // lost in the transport. Since we were able to render the frame, however,
857 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
858 // happens internally in Call, and we can therefore here not know which
859 // FEC packets that protected the lost media packets. Consequently, we
860 // were not able to record a meaningful recv_time_ms. We therefore skip
861 // this sample.
862 //
863 // The reasoning above does not hold for ULPFEC and RTX, as for those
864 // strategies the timestamp of the received packets is set to the
865 // timestamp of the protected/retransmitted media packet. I.e., then
866 // recv_time_ms != 0, even though the media packets were lost.
867 receiver_time_.AddSample(comparison.render_time_ms -
868 comparison.recv_time_ms);
Ying Wang338bd622018-02-09 16:21:26 +0100869 network_time_.AddSample(comparison.recv_time_ms -
870 comparison.send_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800871 }
nissedf2ceb82016-12-15 06:29:53 -0800872 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700873 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
874 }
875
876 void PrintResult(const char* result_type,
877 test::Statistics stats,
878 const char* unit) {
Edward Lemurd1d67432018-01-02 11:54:37 +0100879 test::PrintResultMeanAndError(result_type, "", test_label_.c_str(),
880 stats.Mean(), stats.StandardDeviation(), unit,
881 false);
ivica5d6a06c2015-09-17 05:30:24 -0700882 }
883
884 void PrintSamplesToFile(void) {
885 FILE* out = graph_data_output_file_;
886 rtc::CritScope crit(&comparison_lock_);
887 std::sort(samples_.begin(), samples_.end(),
888 [](const Sample& A, const Sample& B) -> bool {
889 return A.input_time_ms < B.input_time_ms;
890 });
891
sprangce4aef12015-11-02 07:23:20 -0800892 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700893 fprintf(out, "%" PRIuS "\n", samples_.size());
894 fprintf(out,
895 "dropped "
896 "input_time_ms "
897 "send_time_ms "
898 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700899 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700900 "encoded_frame_size "
901 "psnr "
902 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700903 "encode_time_ms\n");
ivica5d6a06c2015-09-17 05:30:24 -0700904 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700905 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
Niels Möller6b642f72017-12-08 14:11:14 +0100906 " %lf %lf\n",
ivica8d15bd62015-10-07 02:43:12 -0700907 sample.dropped, sample.input_time_ms, sample.send_time_ms,
908 sample.recv_time_ms, sample.render_time_ms,
Niels Möller6b642f72017-12-08 14:11:14 +0100909 sample.encoded_frame_size, sample.psnr, sample.ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700910 }
911 }
912
ilnik1e7732c2017-02-23 05:07:56 -0800913 double GetAverageMediaBitrateBps() {
914 if (last_sending_time_ == first_sending_time_) {
915 return 0;
916 } else {
917 return static_cast<double>(total_media_bytes_) * 8 /
918 (last_sending_time_ - first_sending_time_) *
919 rtc::kNumMillisecsPerSec;
920 }
921 }
922
perkja49cbd32016-09-16 07:53:41 -0700923 // Implements VideoSinkInterface to receive captured frames from a
924 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
925 // as a source to VideoSendStream.
926 // It forwards all input frames to the VideoAnalyzer for later comparison and
927 // forwards the captured frames to the VideoSendStream.
928 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
929 public rtc::VideoSourceInterface<VideoFrame> {
930 public:
ilnik6b826ef2017-06-16 06:53:48 -0700931 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer, Clock* clock)
932 : analyzer_(analyzer),
933 send_stream_input_(nullptr),
934 video_capturer_(nullptr),
935 clock_(clock) {}
936
937 void SetSource(test::VideoCapturer* video_capturer) {
938 video_capturer_ = video_capturer;
939 }
perkja49cbd32016-09-16 07:53:41 -0700940
941 private:
942 void OnFrame(const VideoFrame& video_frame) override {
943 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -0800944 // Frames from the capturer does not have a rtp timestamp.
945 // Create one so it can be used for comparison.
946 RTC_DCHECK_EQ(0, video_frame.timestamp());
ilnik6b826ef2017-06-16 06:53:48 -0700947 if (video_frame.ntp_time_ms() == 0)
948 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
perkja49cbd32016-09-16 07:53:41 -0700949 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -0800950 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -0700951 rtc::CritScope lock(&crit_);
952 if (send_stream_input_)
ilnikb82ac6a2017-05-02 00:48:41 -0700953 send_stream_input_->OnFrame(copy);
perkja49cbd32016-09-16 07:53:41 -0700954 }
955
956 // Called when |send_stream_.SetSource()| is called.
957 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
958 const rtc::VideoSinkWants& wants) override {
ilnik267041c2017-06-27 07:21:01 -0700959 {
960 rtc::CritScope lock(&crit_);
961 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
962 send_stream_input_ = sink;
963 }
ilnik6b826ef2017-06-16 06:53:48 -0700964 if (video_capturer_) {
965 video_capturer_->AddOrUpdateSink(this, wants);
966 }
perkja49cbd32016-09-16 07:53:41 -0700967 }
968
969 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
970 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
971 rtc::CritScope lock(&crit_);
972 RTC_DCHECK(sink == send_stream_input_);
973 send_stream_input_ = nullptr;
974 }
975
976 VideoAnalyzer* const analyzer_;
977 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700978 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_
979 RTC_GUARDED_BY(crit_);
ilnik6b826ef2017-06-16 06:53:48 -0700980 test::VideoCapturer* video_capturer_;
981 Clock* clock_;
perkja49cbd32016-09-16 07:53:41 -0700982 };
983
984 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
985 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800986 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -0700987 }
988
stefan889d9652017-07-05 03:03:02 -0700989 Call* call_;
perkja49cbd32016-09-16 07:53:41 -0700990 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -0800991 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -0700992 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700993 const std::string test_label_;
994 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800995 const std::string graph_title_;
996 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -0800997 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 09:23:30 -0800998 const size_t selected_stream_;
ilnik1e7732c2017-02-23 05:07:56 -0800999 const int selected_sl_;
1000 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -07001001 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +01001002 OnEncodeTimingProxy encode_timing_proxy_;
danilchapa37de392017-09-09 04:17:22 -07001003 std::vector<Sample> samples_ RTC_GUARDED_BY(comparison_lock_);
danilchapa37de392017-09-09 04:17:22 -07001004 test::Statistics sender_time_ RTC_GUARDED_BY(comparison_lock_);
1005 test::Statistics receiver_time_ RTC_GUARDED_BY(comparison_lock_);
Ying Wang338bd622018-02-09 16:21:26 +01001006 test::Statistics network_time_ RTC_GUARDED_BY(comparison_lock_);
danilchapa37de392017-09-09 04:17:22 -07001007 test::Statistics psnr_ RTC_GUARDED_BY(comparison_lock_);
1008 test::Statistics ssim_ RTC_GUARDED_BY(comparison_lock_);
1009 test::Statistics end_to_end_ RTC_GUARDED_BY(comparison_lock_);
1010 test::Statistics rendered_delta_ RTC_GUARDED_BY(comparison_lock_);
1011 test::Statistics encoded_frame_size_ RTC_GUARDED_BY(comparison_lock_);
1012 test::Statistics encode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
1013 test::Statistics encode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
1014 test::Statistics encode_usage_percent_ RTC_GUARDED_BY(comparison_lock_);
1015 test::Statistics decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
1016 test::Statistics decode_time_max_ms_ RTC_GUARDED_BY(comparison_lock_);
1017 test::Statistics media_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
1018 test::Statistics fec_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
1019 test::Statistics send_bandwidth_bps_ RTC_GUARDED_BY(comparison_lock_);
1020 test::Statistics memory_usage_ RTC_GUARDED_BY(comparison_lock_);
Stefan Holmer32e930f2018-02-09 09:43:45 +01001021 test::Statistics time_between_freezes_ RTC_GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 06:46:05 -08001022
ilnik59cac992017-07-25 05:45:03 -07001023 struct FrameWithPsnr {
1024 double psnr;
1025 VideoFrame frame;
1026 };
1027
1028 // Rendered frame with worst PSNR is saved for further analysis.
danilchapa37de392017-09-09 04:17:22 -07001029 rtc::Optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_);
ilnik59cac992017-07-25 05:45:03 -07001030
stefan889d9652017-07-05 03:03:02 -07001031 size_t last_fec_bytes_;
ivica5d6a06c2015-09-17 05:30:24 -07001032
1033 const int frames_to_process_;
1034 int frames_recorded_;
1035 int frames_processed_;
1036 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -07001037 int dropped_frames_before_first_encode_;
1038 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -07001039 int64_t last_render_time_;
Stefan Holmer32e930f2018-02-09 09:43:45 +01001040 int64_t last_render_delta_ms_;
1041 int64_t last_unfreeze_time_ms_;
ivica5d6a06c2015-09-17 05:30:24 -07001042 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -08001043 int64_t total_media_bytes_;
1044 int64_t first_sending_time_;
1045 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -07001046
danilchapa37de392017-09-09 04:17:22 -07001047 int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
1048 int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
ilnikdf92c5c2017-02-23 02:08:44 -08001049 rtc::CriticalSection cpu_measurement_lock_;
1050
ivica5d6a06c2015-09-17 05:30:24 -07001051 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07001052 std::deque<VideoFrame> frames_ RTC_GUARDED_BY(crit_);
1053 rtc::Optional<VideoFrame> last_rendered_frame_ RTC_GUARDED_BY(crit_);
1054 rtc::TimestampWrapAroundHandler wrap_handler_ RTC_GUARDED_BY(crit_);
1055 std::map<int64_t, int64_t> send_times_ RTC_GUARDED_BY(crit_);
1056 std::map<int64_t, int64_t> recv_times_ RTC_GUARDED_BY(crit_);
1057 std::map<int64_t, size_t> encoded_frame_sizes_ RTC_GUARDED_BY(crit_);
1058 rtc::Optional<uint32_t> first_encoded_timestamp_ RTC_GUARDED_BY(crit_);
1059 rtc::Optional<uint32_t> first_sent_timestamp_ RTC_GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -07001060 const double avg_psnr_threshold_;
1061 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -08001062 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -07001063
1064 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001065 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1066 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001067 rtc::Event comparison_available_event_;
danilchapa37de392017-09-09 04:17:22 -07001068 std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001069 rtc::Event done_;
ilnik98436952017-07-13 00:47:03 -07001070
1071 std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
1072 Clock* const clock_;
1073 const int64_t start_ms_;
ivica5d6a06c2015-09-17 05:30:24 -07001074};
1075
palmkviste75f2042016-09-28 06:19:48 -07001076VideoQualityTest::VideoQualityTest()
minyue20c84cc2017-04-10 16:57:57 -07001077 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {
1078 payload_type_map_ = test::CallTest::payload_type_map_;
1079 RTC_DCHECK(payload_type_map_.find(kPayloadTypeH264) ==
1080 payload_type_map_.end());
1081 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP8) ==
1082 payload_type_map_.end());
1083 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP9) ==
1084 payload_type_map_.end());
1085 payload_type_map_[kPayloadTypeH264] = webrtc::MediaType::VIDEO;
1086 payload_type_map_[kPayloadTypeVP8] = webrtc::MediaType::VIDEO;
1087 payload_type_map_[kPayloadTypeVP9] = webrtc::MediaType::VIDEO;
1088}
ivica5d6a06c2015-09-17 05:30:24 -07001089
Ying Wang3b790f32018-01-19 17:58:57 +01001090VideoQualityTest::VideoQualityTest(
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001091 std::unique_ptr<FecControllerFactoryInterface> fec_controller_factory)
Ying Wang3b790f32018-01-19 17:58:57 +01001092 : VideoQualityTest() {
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001093 fec_controller_factory_ = std::move(fec_controller_factory);
Ying Wang3b790f32018-01-19 17:58:57 +01001094}
1095
minyue626bc952016-10-31 05:47:02 -07001096VideoQualityTest::Params::Params()
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01001097 : call({false, BitrateConstraints(), 0}),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001098 video{{false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
1099 false, ""},
1100 {false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
1101 false, ""}},
minyue4c8b9422017-03-21 04:11:43 -07001102 audio({false, false, false}),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001103 screenshare{{false, false, 10, 0}, {false, false, 10, 0}},
minyue626bc952016-10-31 05:47:02 -07001104 analyzer({"", 0.0, 0.0, 0, "", ""}),
1105 pipe(),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001106 ss{{std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()},
1107 {std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}},
ilnik98436952017-07-13 00:47:03 -07001108 logging({false, "", "", ""}) {}
minyue626bc952016-10-31 05:47:02 -07001109
1110VideoQualityTest::Params::~Params() = default;
1111
ivica5d6a06c2015-09-17 05:30:24 -07001112void VideoQualityTest::TestBody() {}
1113
sprangce4aef12015-11-02 07:23:20 -08001114std::string VideoQualityTest::GenerateGraphTitle() const {
1115 std::stringstream ss;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001116 ss << params_.video[0].codec;
1117 ss << " (" << params_.video[0].target_bitrate_bps / 1000 << "kbps";
1118 ss << ", " << params_.video[0].fps << " FPS";
1119 if (params_.screenshare[0].scroll_duration)
1120 ss << ", " << params_.screenshare[0].scroll_duration << "s scroll";
1121 if (params_.ss[0].streams.size() > 1)
1122 ss << ", Stream #" << params_.ss[0].selected_stream;
1123 if (params_.ss[0].num_spatial_layers > 1)
1124 ss << ", Layer #" << params_.ss[0].selected_sl;
sprangce4aef12015-11-02 07:23:20 -08001125 ss << ")";
1126 return ss.str();
1127}
1128
1129void VideoQualityTest::CheckParams() {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001130 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1131 // Iterate over primary and secondary video streams.
1132 if (!params_.video[video_idx].enabled)
1133 return;
1134 // Add a default stream in none specified.
1135 if (params_.ss[video_idx].streams.empty())
1136 params_.ss[video_idx].streams.push_back(
1137 VideoQualityTest::DefaultVideoStream(params_, video_idx));
1138 if (params_.ss[video_idx].num_spatial_layers == 0)
1139 params_.ss[video_idx].num_spatial_layers = 1;
sprangce4aef12015-11-02 07:23:20 -08001140
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001141 if (params_.pipe.loss_percent != 0 ||
1142 params_.pipe.queue_length_packets != 0) {
1143 // Since LayerFilteringTransport changes the sequence numbers, we can't
1144 // use that feature with pack loss, since the NACK request would end up
1145 // retransmitting the wrong packets.
1146 RTC_CHECK(params_.ss[video_idx].selected_sl == -1 ||
1147 params_.ss[video_idx].selected_sl ==
1148 params_.ss[video_idx].num_spatial_layers - 1);
1149 RTC_CHECK(params_.video[video_idx].selected_tl == -1 ||
1150 params_.video[video_idx].selected_tl ==
1151 params_.video[video_idx].num_temporal_layers - 1);
1152 }
sprangce4aef12015-11-02 07:23:20 -08001153
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001154 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as
1155 // it does in some parts of the code?
1156 RTC_CHECK_GE(params_.video[video_idx].max_bitrate_bps,
1157 params_.video[video_idx].target_bitrate_bps);
1158 RTC_CHECK_GE(params_.video[video_idx].target_bitrate_bps,
1159 params_.video[video_idx].min_bitrate_bps);
1160 RTC_CHECK_LT(params_.video[video_idx].selected_tl,
1161 params_.video[video_idx].num_temporal_layers);
1162 RTC_CHECK_LE(params_.ss[video_idx].selected_stream,
1163 params_.ss[video_idx].streams.size());
1164 for (const VideoStream& stream : params_.ss[video_idx].streams) {
1165 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1166 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1167 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
1168 }
1169 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1170 // the total bitrate? We anyway have to update them in the case bitrate
1171 // estimator changes the total bitrates.
1172 RTC_CHECK_GE(params_.ss[video_idx].num_spatial_layers, 1);
1173 RTC_CHECK_LE(params_.ss[video_idx].selected_sl,
1174 params_.ss[video_idx].num_spatial_layers);
1175 RTC_CHECK(
1176 params_.ss[video_idx].spatial_layers.empty() ||
1177 params_.ss[video_idx].spatial_layers.size() ==
1178 static_cast<size_t>(params_.ss[video_idx].num_spatial_layers));
1179 if (params_.video[video_idx].codec == "VP8") {
1180 RTC_CHECK_EQ(params_.ss[video_idx].num_spatial_layers, 1);
1181 } else if (params_.video[video_idx].codec == "VP9") {
1182 RTC_CHECK_EQ(params_.ss[video_idx].streams.size(), 1);
1183 }
1184 RTC_CHECK_GE(params_.call.num_thumbnails, 0);
1185 if (params_.call.num_thumbnails > 0) {
1186 RTC_CHECK_EQ(params_.ss[video_idx].num_spatial_layers, 1);
1187 RTC_CHECK_EQ(params_.ss[video_idx].streams.size(), 3);
1188 RTC_CHECK_EQ(params_.video[video_idx].num_temporal_layers, 3);
1189 RTC_CHECK_EQ(params_.video[video_idx].codec, "VP8");
1190 }
1191 // Dual streams with FEC not supported in tests yet.
1192 RTC_CHECK(!params_.video[video_idx].flexfec || num_video_streams_ == 1);
1193 RTC_CHECK(!params_.video[video_idx].ulpfec || num_video_streams_ == 1);
ilnika014cc52017-03-07 04:21:04 -08001194 }
sprangce4aef12015-11-02 07:23:20 -08001195}
1196
1197// Static.
1198std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1199 // Parse comma separated nonnegative integers, where some elements may be
1200 // empty. The empty values are replaced with -1.
1201 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1202 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1203 std::vector<int> result;
1204 if (str.empty())
1205 return result;
1206
1207 const char* p = str.c_str();
1208 int value = -1;
1209 int pos;
1210 while (*p) {
1211 if (*p == ',') {
1212 result.push_back(value);
1213 value = -1;
1214 ++p;
1215 continue;
1216 }
1217 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1218 << "Unexpected non-number value.";
1219 p += pos;
1220 }
1221 result.push_back(value);
1222 return result;
1223}
1224
1225// Static.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001226VideoStream VideoQualityTest::DefaultVideoStream(const Params& params,
1227 size_t video_idx) {
sprangce4aef12015-11-02 07:23:20 -08001228 VideoStream stream;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001229 stream.width = params.video[video_idx].width;
1230 stream.height = params.video[video_idx].height;
1231 stream.max_framerate = params.video[video_idx].fps;
1232 stream.min_bitrate_bps = params.video[video_idx].min_bitrate_bps;
1233 stream.target_bitrate_bps = params.video[video_idx].target_bitrate_bps;
1234 stream.max_bitrate_bps = params.video[video_idx].max_bitrate_bps;
sprang1168fd42017-06-21 09:00:17 -07001235 stream.max_qp = kDefaultMaxQp;
Sergey Silkina796a7e2018-03-01 15:11:29 +01001236 stream.num_temporal_layers = params.video[video_idx].num_temporal_layers;
Seth Hampson46e31ba2018-01-18 10:39:54 -08001237 stream.active = true;
ilnika014cc52017-03-07 04:21:04 -08001238 return stream;
1239}
1240
1241// Static.
1242VideoStream VideoQualityTest::DefaultThumbnailStream() {
1243 VideoStream stream;
1244 stream.width = 320;
1245 stream.height = 180;
1246 stream.max_framerate = 7;
1247 stream.min_bitrate_bps = 7500;
1248 stream.target_bitrate_bps = 37500;
1249 stream.max_bitrate_bps = 50000;
sprang1168fd42017-06-21 09:00:17 -07001250 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 07:23:20 -08001251 return stream;
1252}
1253
1254// Static.
1255void VideoQualityTest::FillScalabilitySettings(
1256 Params* params,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001257 size_t video_idx,
sprangce4aef12015-11-02 07:23:20 -08001258 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 09:00:17 -07001259 int num_streams,
sprangce4aef12015-11-02 07:23:20 -08001260 size_t selected_stream,
1261 int num_spatial_layers,
1262 int selected_sl,
1263 const std::vector<std::string>& sl_descriptors) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001264 if (params->ss[video_idx].streams.empty() &&
1265 params->ss[video_idx].infer_streams) {
sprang1168fd42017-06-21 09:00:17 -07001266 webrtc::VideoEncoderConfig encoder_config;
Niels Möller04dd1762018-03-23 16:05:22 +01001267 encoder_config.codec_type =
1268 PayloadStringToCodecType(params->video[video_idx].codec);
sprang1168fd42017-06-21 09:00:17 -07001269 encoder_config.content_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001270 params->screenshare[video_idx].enabled
sprang1168fd42017-06-21 09:00:17 -07001271 ? webrtc::VideoEncoderConfig::ContentType::kScreen
1272 : webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001273 encoder_config.max_bitrate_bps = params->video[video_idx].max_bitrate_bps;
1274 encoder_config.min_transmit_bitrate_bps =
1275 params->video[video_idx].min_transmit_bps;
sprang1168fd42017-06-21 09:00:17 -07001276 encoder_config.number_of_streams = num_streams;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001277 encoder_config.spatial_layers = params->ss[video_idx].spatial_layers;
Seth Hampson8234ead2018-02-02 15:16:24 -08001278 encoder_config.simulcast_layers = std::vector<VideoStream>(num_streams);
sprang1168fd42017-06-21 09:00:17 -07001279 encoder_config.video_stream_factory =
1280 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001281 params->video[video_idx].codec, kDefaultMaxQp,
1282 params->video[video_idx].fps,
1283 params->screenshare[video_idx].enabled, true);
1284 params->ss[video_idx].streams =
sprang1168fd42017-06-21 09:00:17 -07001285 encoder_config.video_stream_factory->CreateEncoderStreams(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001286 static_cast<int>(params->video[video_idx].width),
1287 static_cast<int>(params->video[video_idx].height), encoder_config);
sprang1168fd42017-06-21 09:00:17 -07001288 } else {
1289 // Read VideoStream and SpatialLayer elements from a list of comma separated
1290 // lists. To use a default value for an element, use -1 or leave empty.
1291 // Validity checks performed in CheckParams.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001292 RTC_CHECK(params->ss[video_idx].streams.empty());
sprang1168fd42017-06-21 09:00:17 -07001293 for (auto descriptor : stream_descriptors) {
1294 if (descriptor.empty())
1295 continue;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001296 VideoStream stream =
1297 VideoQualityTest::DefaultVideoStream(*params, video_idx);
sprang1168fd42017-06-21 09:00:17 -07001298 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1299 if (v[0] != -1)
1300 stream.width = static_cast<size_t>(v[0]);
1301 if (v[1] != -1)
1302 stream.height = static_cast<size_t>(v[1]);
1303 if (v[2] != -1)
1304 stream.max_framerate = v[2];
1305 if (v[3] != -1)
1306 stream.min_bitrate_bps = v[3];
1307 if (v[4] != -1)
1308 stream.target_bitrate_bps = v[4];
1309 if (v[5] != -1)
1310 stream.max_bitrate_bps = v[5];
1311 if (v.size() > 6 && v[6] != -1)
1312 stream.max_qp = v[6];
Sergey Silkina796a7e2018-03-01 15:11:29 +01001313 if (v.size() > 7 && v[7] != -1) {
1314 stream.num_temporal_layers = v[7];
sprang1168fd42017-06-21 09:00:17 -07001315 } else {
1316 // Automatic TL thresholds for more than two layers not supported.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001317 RTC_CHECK_LE(params->video[video_idx].num_temporal_layers, 2);
sprang1168fd42017-06-21 09:00:17 -07001318 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001319 params->ss[video_idx].streams.push_back(stream);
sprangce4aef12015-11-02 07:23:20 -08001320 }
sprangce4aef12015-11-02 07:23:20 -08001321 }
sprangce4aef12015-11-02 07:23:20 -08001322
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001323 params->ss[video_idx].num_spatial_layers = std::max(1, num_spatial_layers);
1324 params->ss[video_idx].selected_stream = selected_stream;
sprang1168fd42017-06-21 09:00:17 -07001325
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001326 params->ss[video_idx].selected_sl = selected_sl;
1327 RTC_CHECK(params->ss[video_idx].spatial_layers.empty());
sprangce4aef12015-11-02 07:23:20 -08001328 for (auto descriptor : sl_descriptors) {
1329 if (descriptor.empty())
1330 continue;
1331 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
Sergey Silkin13e74342018-03-02 12:28:00 +01001332 RTC_CHECK_EQ(v.size(), 7);
sprangce4aef12015-11-02 07:23:20 -08001333
Sergey Silkin13e74342018-03-02 12:28:00 +01001334 SpatialLayer layer = {0};
1335 layer.width = v[0];
1336 layer.height = v[1];
1337 layer.numberOfTemporalLayers = v[2];
1338 layer.maxBitrate = v[3];
1339 layer.minBitrate = v[4];
1340 layer.targetBitrate = v[5];
1341 layer.qpMax = v[6];
1342 layer.active = true;
1343
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001344 params->ss[video_idx].spatial_layers.push_back(layer);
sprangce4aef12015-11-02 07:23:20 -08001345 }
1346}
1347
minyuea27172d2016-11-01 05:59:29 -07001348void VideoQualityTest::SetupVideo(Transport* send_transport,
1349 Transport* recv_transport) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001350 size_t total_streams_used = 0;
1351 size_t num_flexfec_streams = params_.video[0].flexfec ? 1 : 0;
1352 CreateAudioAndFecSendConfigs(0, num_flexfec_streams, send_transport);
1353 CreateMatchingAudioAndFecConfigs(recv_transport);
1354 video_receive_configs_.clear();
1355 video_send_configs_.clear();
1356 video_encoders_.clear();
1357 video_encoder_configs_.clear();
1358 allocated_decoders_.clear();
1359 bool decode_all_receive_streams = true;
1360 size_t num_video_substreams = params_.ss[0].streams.size();
1361 RTC_CHECK(num_video_streams_ > 0);
1362 video_encoder_configs_.resize(num_video_streams_);
1363 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1364 video_send_configs_.push_back(VideoSendStream::Config(send_transport));
1365 video_encoders_.push_back(nullptr);
1366 video_encoder_configs_.push_back(VideoEncoderConfig());
1367 num_video_substreams = params_.ss[video_idx].streams.size();
1368 RTC_CHECK_GT(num_video_substreams, 0);
1369 CreateVideoSendConfig(&video_send_configs_[video_idx], num_video_substreams,
1370 total_streams_used, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001371
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001372 int payload_type;
1373 if (params_.video[video_idx].codec == "H264") {
1374 video_encoders_[video_idx] =
1375 H264Encoder::Create(cricket::VideoCodec("H264"));
1376 payload_type = kPayloadTypeH264;
1377 } else if (params_.video[video_idx].codec == "VP8") {
1378 if (params_.screenshare[video_idx].enabled &&
1379 params_.ss[video_idx].streams.size() > 1) {
1380 // Simulcast screenshare needs a simulcast encoder adapter to work,
1381 // since encoders usually can't natively do simulcast with different
1382 // frame rates for the different layers.
1383 video_encoders_[video_idx].reset(
1384 new SimulcastEncoderAdapter(new InternalEncoderFactory()));
1385 } else {
1386 video_encoders_[video_idx] = VP8Encoder::Create();
1387 }
1388 payload_type = kPayloadTypeVP8;
1389 } else if (params_.video[video_idx].codec == "VP9") {
1390 video_encoders_[video_idx] = VP9Encoder::Create();
1391 payload_type = kPayloadTypeVP9;
Emircan Uysaler03e6ec92018-03-09 15:03:26 -08001392 } else if (params_.video[video_idx].codec == "multiplex") {
1393 video_encoders_[video_idx] = rtc::MakeUnique<MultiplexEncoderAdapter>(
1394 new InternalEncoderFactory(), SdpVideoFormat(cricket::kVp9CodecName));
1395 payload_type = kPayloadTypeVP9;
ilnikcb8c1462017-03-09 09:23:30 -08001396 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001397 RTC_NOTREACHED() << "Codec not supported!";
1398 return;
ilnikcb8c1462017-03-09 09:23:30 -08001399 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001400 video_send_configs_[video_idx].encoder_settings.encoder =
1401 video_encoders_[video_idx].get();
Niels Möller04dd1762018-03-23 16:05:22 +01001402 video_send_configs_[video_idx].rtp.payload_name =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001403 params_.video[video_idx].codec;
Niels Möller04dd1762018-03-23 16:05:22 +01001404 video_send_configs_[video_idx].rtp.payload_type = payload_type;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001405 video_send_configs_[video_idx].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1406 video_send_configs_[video_idx].rtp.rtx.payload_type = kSendRtxPayloadType;
1407 for (size_t i = 0; i < num_video_substreams; ++i) {
1408 video_send_configs_[video_idx].rtp.rtx.ssrcs.push_back(
1409 kSendRtxSsrcs[i + total_streams_used]);
ilnik9fd9f6c2017-03-02 08:10:10 -08001410 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001411 video_send_configs_[video_idx].rtp.extensions.clear();
1412 if (params_.call.send_side_bwe) {
1413 video_send_configs_[video_idx].rtp.extensions.push_back(
1414 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1415 test::kTransportSequenceNumberExtensionId));
1416 } else {
1417 video_send_configs_[video_idx].rtp.extensions.push_back(RtpExtension(
1418 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1419 }
1420 video_send_configs_[video_idx].rtp.extensions.push_back(
1421 RtpExtension(RtpExtension::kVideoContentTypeUri,
1422 test::kVideoContentTypeExtensionId));
1423 video_send_configs_[video_idx].rtp.extensions.push_back(RtpExtension(
1424 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
1425
Niels Möller04dd1762018-03-23 16:05:22 +01001426 video_encoder_configs_[video_idx].codec_type =
1427 PayloadStringToCodecType(params_.video[video_idx].codec);
1428
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001429 video_encoder_configs_[video_idx].min_transmit_bitrate_bps =
1430 params_.video[video_idx].min_transmit_bps;
1431
1432 video_send_configs_[video_idx].suspend_below_min_bitrate =
1433 params_.video[video_idx].suspend_below_min_bitrate;
1434
1435 video_encoder_configs_[video_idx].number_of_streams =
1436 params_.ss[video_idx].streams.size();
1437 video_encoder_configs_[video_idx].max_bitrate_bps = 0;
1438 for (size_t i = 0; i < params_.ss[video_idx].streams.size(); ++i) {
1439 video_encoder_configs_[video_idx].max_bitrate_bps +=
1440 params_.ss[video_idx].streams[i].max_bitrate_bps;
1441 }
1442 if (params_.ss[video_idx].infer_streams) {
Seth Hampson8234ead2018-02-02 15:16:24 -08001443 video_encoder_configs_[video_idx].simulcast_layers =
1444 std::vector<VideoStream>(params_.ss[video_idx].streams.size());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001445 video_encoder_configs_[video_idx].video_stream_factory =
1446 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1447 params_.video[video_idx].codec,
1448 params_.ss[video_idx].streams[0].max_qp,
1449 params_.video[video_idx].fps,
1450 params_.screenshare[video_idx].enabled, true);
1451 } else {
1452 video_encoder_configs_[video_idx].video_stream_factory =
1453 new rtc::RefCountedObject<VideoStreamFactory>(
1454 params_.ss[video_idx].streams);
1455 }
1456
1457 video_encoder_configs_[video_idx].spatial_layers =
1458 params_.ss[video_idx].spatial_layers;
1459
1460 std::vector<VideoReceiveStream::Config> new_receive_configs =
1461 CreateMatchingVideoReceiveConfigs(video_send_configs_[video_idx],
1462 recv_transport);
1463
1464 decode_all_receive_streams = params_.ss[video_idx].selected_stream ==
1465 params_.ss[video_idx].streams.size();
1466
1467 for (size_t i = 0; i < num_video_substreams; ++i) {
1468 new_receive_configs[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1469 new_receive_configs[i].rtp.rtx_ssrc =
1470 kSendRtxSsrcs[i + total_streams_used];
1471 new_receive_configs[i]
1472 .rtp.rtx_associated_payload_types[kSendRtxPayloadType] = payload_type;
1473 new_receive_configs[i].rtp.transport_cc = params_.call.send_side_bwe;
1474 new_receive_configs[i].rtp.remb = !params_.call.send_side_bwe;
1475 // Enable RTT calculation so NTP time estimator will work.
1476 new_receive_configs[i].rtp.rtcp_xr.receiver_reference_time_report = true;
1477 // Force fake decoders on non-selected simulcast streams.
1478 if (!decode_all_receive_streams &&
1479 i != params_.ss[video_idx].selected_stream) {
1480 VideoReceiveStream::Decoder decoder;
1481 decoder.decoder = new test::FakeDecoder();
Niels Möller04dd1762018-03-23 16:05:22 +01001482 decoder.payload_type = video_send_configs_[video_idx].rtp.payload_type;
1483 decoder.payload_name = video_send_configs_[video_idx].rtp.payload_name;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001484 new_receive_configs[i].decoders.clear();
1485 allocated_decoders_.emplace_back(decoder.decoder);
1486 new_receive_configs[i].decoders.push_back(decoder);
1487 }
1488 }
1489
1490 for (VideoReceiveStream::Config& config : new_receive_configs) {
1491 video_receive_configs_.push_back(config.Copy());
1492 }
1493
1494 if (params_.screenshare[video_idx].enabled) {
1495 // Fill out codec settings.
1496 video_encoder_configs_[video_idx].content_type =
1497 VideoEncoderConfig::ContentType::kScreen;
1498 degradation_preference_ =
1499 VideoSendStream::DegradationPreference::kMaintainResolution;
1500 if (params_.video[video_idx].codec == "VP8") {
1501 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1502 vp8_settings.denoisingOn = false;
1503 vp8_settings.frameDroppingOn = false;
1504 vp8_settings.numberOfTemporalLayers = static_cast<unsigned char>(
1505 params_.video[video_idx].num_temporal_layers);
1506 video_encoder_configs_[video_idx].encoder_specific_settings =
1507 new rtc::RefCountedObject<
1508 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1509 } else if (params_.video[video_idx].codec == "VP9") {
1510 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1511 vp9_settings.denoisingOn = false;
1512 vp9_settings.frameDroppingOn = false;
1513 vp9_settings.numberOfTemporalLayers = static_cast<unsigned char>(
1514 params_.video[video_idx].num_temporal_layers);
1515 vp9_settings.numberOfSpatialLayers = static_cast<unsigned char>(
1516 params_.ss[video_idx].num_spatial_layers);
1517 video_encoder_configs_[video_idx].encoder_specific_settings =
1518 new rtc::RefCountedObject<
1519 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1520 }
1521 } else if (params_.ss[video_idx].num_spatial_layers > 1) {
1522 // If SVC mode without screenshare, still need to set codec specifics.
1523 RTC_CHECK(params_.video[video_idx].codec == "VP9");
1524 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1525 vp9_settings.numberOfTemporalLayers = static_cast<unsigned char>(
1526 params_.video[video_idx].num_temporal_layers);
1527 vp9_settings.numberOfSpatialLayers =
1528 static_cast<unsigned char>(params_.ss[video_idx].num_spatial_layers);
1529 video_encoder_configs_[video_idx].encoder_specific_settings =
1530 new rtc::RefCountedObject<
1531 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1532 }
1533 total_streams_used += num_video_substreams;
sprangce4aef12015-11-02 07:23:20 -08001534 }
brandtr1293aca2016-11-16 22:47:29 -08001535
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001536 // FEC supported only for single video stream mode yet.
1537 if (params_.video[0].flexfec) {
Ilya Nikolaevskiy38aaf692017-12-22 14:39:09 +01001538 video_send_configs_[0].rtp.flexfec.payload_type = kFlexfecPayloadType;
1539 video_send_configs_[0].rtp.flexfec.ssrc = kFlexfecSendSsrc;
sprang1168fd42017-06-21 09:00:17 -07001540 if (decode_all_receive_streams) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001541 for (uint32_t media_ssrc : video_send_configs_[0].rtp.ssrcs) {
1542 video_send_configs_[0].rtp.flexfec.protected_media_ssrcs.push_back(
sprang1168fd42017-06-21 09:00:17 -07001543 media_ssrc);
1544 }
1545 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001546 video_send_configs_[0].rtp.flexfec.protected_media_ssrcs = {
1547 kVideoSendSsrcs[params_.ss[0].selected_stream]};
sprang1168fd42017-06-21 09:00:17 -07001548 }
brandtr1293aca2016-11-16 22:47:29 -08001549
brandtr8313a6f2017-01-13 07:41:19 -08001550 // The matching receive config is _not_ created by
1551 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1552 // Set up the receive config manually instead.
1553 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001554 flexfec_receive_config.payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001555 video_send_configs_[0].rtp.flexfec.payload_type;
1556 flexfec_receive_config.remote_ssrc =
1557 video_send_configs_[0].rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001558 flexfec_receive_config.protected_media_ssrcs =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001559 video_send_configs_[0].rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001560 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001561 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1562 if (params_.call.send_side_bwe) {
1563 flexfec_receive_config.rtp_header_extensions.push_back(
1564 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1565 test::kTransportSequenceNumberExtensionId));
1566 } else {
1567 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1568 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1569 }
brandtr1293aca2016-11-16 22:47:29 -08001570 flexfec_receive_configs_.push_back(flexfec_receive_config);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001571 if (num_video_substreams > 0) {
brandtr7cd28b92017-09-22 00:26:25 -07001572 video_receive_configs_[0].rtp.protected_by_flexfec = true;
1573 }
brandtr1293aca2016-11-16 22:47:29 -08001574 }
1575
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001576 if (params_.video[0].ulpfec) {
1577 video_send_configs_[0].rtp.ulpfec.red_payload_type = kRedPayloadType;
1578 video_send_configs_[0].rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1579 video_send_configs_[0].rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
brandtr1293aca2016-11-16 22:47:29 -08001580
sprang1168fd42017-06-21 09:00:17 -07001581 if (decode_all_receive_streams) {
1582 for (auto it = video_receive_configs_.begin();
1583 it != video_receive_configs_.end(); ++it) {
nisse3b3622f2017-09-26 02:49:21 -07001584 it->rtp.red_payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001585 video_send_configs_[0].rtp.ulpfec.red_payload_type;
nisse3b3622f2017-09-26 02:49:21 -07001586 it->rtp.ulpfec_payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001587 video_send_configs_[0].rtp.ulpfec.ulpfec_payload_type;
1588 it->rtp.rtx_associated_payload_types
1589 [video_send_configs_[0].rtp.ulpfec.red_rtx_payload_type] =
1590 video_send_configs_[0].rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 09:00:17 -07001591 }
1592 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001593 video_receive_configs_[params_.ss[0].selected_stream]
1594 .rtp.red_payload_type =
1595 video_send_configs_[0].rtp.ulpfec.red_payload_type;
1596 video_receive_configs_[params_.ss[0].selected_stream]
nisse3b3622f2017-09-26 02:49:21 -07001597 .rtp.ulpfec_payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001598 video_send_configs_[0].rtp.ulpfec.ulpfec_payload_type;
1599 video_receive_configs_[params_.ss[0].selected_stream]
1600 .rtp.rtx_associated_payload_types
1601 [video_send_configs_[0].rtp.ulpfec.red_rtx_payload_type] =
1602 video_send_configs_[0].rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 09:00:17 -07001603 }
brandtr1293aca2016-11-16 22:47:29 -08001604 }
ivica5d6a06c2015-09-17 05:30:24 -07001605}
1606
ilnika014cc52017-03-07 04:21:04 -08001607void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1608 Transport* recv_transport) {
ilnik98436952017-07-13 00:47:03 -07001609 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001610 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1611
1612 // Thumbnails will be send in the other way: from receiver_call to
1613 // sender_call.
1614 VideoSendStream::Config thumbnail_send_config(recv_transport);
1615 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1616 thumbnail_send_config.encoder_settings.encoder =
1617 thumbnail_encoders_.back().get();
Niels Möller04dd1762018-03-23 16:05:22 +01001618 thumbnail_send_config.rtp.payload_name = params_.video[0].codec;
1619 thumbnail_send_config.rtp.payload_type = kPayloadTypeVP8;
ilnika014cc52017-03-07 04:21:04 -08001620 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1621 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1622 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1623 thumbnail_send_config.rtp.extensions.clear();
1624 if (params_.call.send_side_bwe) {
1625 thumbnail_send_config.rtp.extensions.push_back(
1626 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1627 test::kTransportSequenceNumberExtensionId));
1628 } else {
1629 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1630 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1631 }
1632
1633 VideoEncoderConfig thumbnail_encoder_config;
Niels Möller04dd1762018-03-23 16:05:22 +01001634 thumbnail_encoder_config.codec_type = kVideoCodecVP8;
ilnika014cc52017-03-07 04:21:04 -08001635 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1636 thumbnail_send_config.suspend_below_min_bitrate =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001637 params_.video[0].suspend_below_min_bitrate;
ilnika014cc52017-03-07 04:21:04 -08001638 thumbnail_encoder_config.number_of_streams = 1;
1639 thumbnail_encoder_config.max_bitrate_bps = 50000;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001640 if (params_.ss[0].infer_streams) {
ilnik6b826ef2017-06-16 06:53:48 -07001641 thumbnail_encoder_config.video_stream_factory =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001642 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss[0].streams);
ilnik6b826ef2017-06-16 06:53:48 -07001643 } else {
Seth Hampson8234ead2018-02-02 15:16:24 -08001644 thumbnail_encoder_config.simulcast_layers = std::vector<VideoStream>(1);
ilnik6b826ef2017-06-16 06:53:48 -07001645 thumbnail_encoder_config.video_stream_factory =
1646 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001647 params_.video[0].codec, params_.ss[0].streams[0].max_qp,
1648 params_.video[0].fps, params_.screenshare[0].enabled, true);
ilnik6b826ef2017-06-16 06:53:48 -07001649 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001650 thumbnail_encoder_config.spatial_layers = params_.ss[0].spatial_layers;
ilnika014cc52017-03-07 04:21:04 -08001651
1652 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1653 thumbnail_receive_config.rtp.remb = false;
1654 thumbnail_receive_config.rtp.transport_cc = true;
1655 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1656 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1657 thumbnail_receive_config.rtp.extensions.push_back(extension);
1658 thumbnail_receive_config.renderer = &fake_renderer_;
1659
1660 VideoReceiveStream::Decoder decoder =
Niels Möller04dd1762018-03-23 16:05:22 +01001661 test::CreateMatchingDecoder(thumbnail_send_config);
ilnika014cc52017-03-07 04:21:04 -08001662 allocated_decoders_.push_back(
1663 std::unique_ptr<VideoDecoder>(decoder.decoder));
1664 thumbnail_receive_config.decoders.clear();
1665 thumbnail_receive_config.decoders.push_back(decoder);
1666 thumbnail_receive_config.rtp.remote_ssrc =
1667 thumbnail_send_config.rtp.ssrcs[0];
1668
1669 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1670 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
nisse26e3abb2017-08-25 04:44:25 -07001671 thumbnail_receive_config.rtp
1672 .rtx_associated_payload_types[kSendRtxPayloadType] = kPayloadTypeVP8;
ilnika014cc52017-03-07 04:21:04 -08001673 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1674 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1675
1676 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1677 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1678 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1679 }
1680
ilnik98436952017-07-13 00:47:03 -07001681 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001682 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1683 thumbnail_send_configs_[i].Copy(),
1684 thumbnail_encoder_configs_[i].Copy()));
1685 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1686 thumbnail_receive_configs_[i].Copy()));
1687 }
1688}
1689
1690void VideoQualityTest::DestroyThumbnailStreams() {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001691 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_) {
ilnika014cc52017-03-07 04:21:04 -08001692 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001693 }
ilnika014cc52017-03-07 04:21:04 -08001694 thumbnail_send_streams_.clear();
1695 for (VideoReceiveStream* thumbnail_receive_stream :
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001696 thumbnail_receive_streams_) {
ilnika014cc52017-03-07 04:21:04 -08001697 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001698 }
ilnika014cc52017-03-07 04:21:04 -08001699 thumbnail_send_streams_.clear();
1700 thumbnail_receive_streams_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07001701 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1702 thumbnail_capturers_) {
1703 video_caputurer.reset();
1704 }
ilnika014cc52017-03-07 04:21:04 -08001705}
1706
ilnika014cc52017-03-07 04:21:04 -08001707void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1708 VideoStream thumbnail = DefaultThumbnailStream();
1709 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1710 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1711 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
Emircan Uysaler03e6ec92018-03-09 15:03:26 -08001712 rtc::nullopt, rtc::nullopt, thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -08001713 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -08001714 }
1715}
1716
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001717std::unique_ptr<test::FrameGenerator> VideoQualityTest::CreateFrameGenerator(
1718 size_t video_idx) {
1719 // Setup frame generator.
1720 const size_t kWidth = 1850;
1721 const size_t kHeight = 1110;
1722 std::unique_ptr<test::FrameGenerator> frame_generator;
1723 if (params_.screenshare[video_idx].generate_slides) {
1724 frame_generator = test::FrameGenerator::CreateSlideGenerator(
1725 kWidth, kHeight,
1726 params_.screenshare[video_idx].slide_change_interval *
1727 params_.video[video_idx].fps);
ivica5d6a06c2015-09-17 05:30:24 -07001728 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001729 std::vector<std::string> slides = params_.screenshare[video_idx].slides;
1730 if (slides.size() == 0) {
1731 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1732 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1733 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1734 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1735 }
1736 if (params_.screenshare[video_idx].scroll_duration == 0) {
1737 // Cycle image every slide_change_interval seconds.
1738 frame_generator = test::FrameGenerator::CreateFromYuvFile(
1739 slides, kWidth, kHeight,
1740 params_.screenshare[video_idx].slide_change_interval *
1741 params_.video[video_idx].fps);
ivica5d6a06c2015-09-17 05:30:24 -07001742 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001743 RTC_CHECK_LE(params_.video[video_idx].width, kWidth);
1744 RTC_CHECK_LE(params_.video[video_idx].height, kHeight);
1745 RTC_CHECK_GT(params_.screenshare[video_idx].slide_change_interval, 0);
1746 const int kPauseDurationMs =
1747 (params_.screenshare[video_idx].slide_change_interval -
1748 params_.screenshare[video_idx].scroll_duration) *
1749 1000;
1750 RTC_CHECK_LE(params_.screenshare[video_idx].scroll_duration,
1751 params_.screenshare[video_idx].slide_change_interval);
1752
1753 frame_generator = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1754 clock_, slides, kWidth, kHeight, params_.video[video_idx].width,
1755 params_.video[video_idx].height,
1756 params_.screenshare[video_idx].scroll_duration * 1000,
1757 kPauseDurationMs);
ivica5d6a06c2015-09-17 05:30:24 -07001758 }
1759 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001760 return frame_generator;
1761}
1762
1763void VideoQualityTest::CreateCapturers() {
1764 video_capturers_.resize(num_video_streams_);
1765 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1766 if (params_.screenshare[video_idx].enabled) {
1767 std::unique_ptr<test::FrameGenerator> frame_generator =
1768 CreateFrameGenerator(video_idx);
1769 test::FrameGeneratorCapturer* frame_generator_capturer =
1770 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator),
1771 params_.video[video_idx].fps);
1772 EXPECT_TRUE(frame_generator_capturer->Init());
1773 video_capturers_[video_idx].reset(frame_generator_capturer);
1774 } else {
1775 if (params_.video[video_idx].clip_name == "Generator") {
1776 video_capturers_[video_idx].reset(test::FrameGeneratorCapturer::Create(
1777 static_cast<int>(params_.video[video_idx].width),
Emircan Uysaler03e6ec92018-03-09 15:03:26 -08001778 static_cast<int>(params_.video[video_idx].height), rtc::nullopt,
1779 rtc::nullopt, params_.video[video_idx].fps, clock_));
1780 } else if (params_.video[video_idx].clip_name == "GeneratorI420A") {
1781 video_capturers_[video_idx].reset(test::FrameGeneratorCapturer::Create(
1782 static_cast<int>(params_.video[video_idx].width),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001783 static_cast<int>(params_.video[video_idx].height),
Emircan Uysaler03e6ec92018-03-09 15:03:26 -08001784 test::FrameGenerator::OutputType::I420A, rtc::nullopt,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001785 params_.video[video_idx].fps, clock_));
1786 } else if (params_.video[video_idx].clip_name.empty()) {
1787 video_capturers_[video_idx].reset(test::VcmCapturer::Create(
1788 params_.video[video_idx].width, params_.video[video_idx].height,
1789 params_.video[video_idx].fps,
1790 params_.video[video_idx].capture_device_index));
1791 if (!video_capturers_[video_idx]) {
1792 // Failed to get actual camera, use chroma generator as backup.
1793 video_capturers_[video_idx].reset(
1794 test::FrameGeneratorCapturer::Create(
1795 static_cast<int>(params_.video[video_idx].width),
1796 static_cast<int>(params_.video[video_idx].height),
Emircan Uysaler03e6ec92018-03-09 15:03:26 -08001797 rtc::nullopt, rtc::nullopt, params_.video[video_idx].fps,
1798 clock_));
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001799 }
1800 } else {
1801 video_capturers_[video_idx].reset(
1802 test::FrameGeneratorCapturer::CreateFromYuvFile(
1803 test::ResourcePath(params_.video[video_idx].clip_name, "yuv"),
1804 params_.video[video_idx].width, params_.video[video_idx].height,
1805 params_.video[video_idx].fps, clock_));
1806 ASSERT_TRUE(video_capturers_[video_idx])
1807 << "Could not create capturer for "
1808 << params_.video[video_idx].clip_name
1809 << ".yuv. Is this resource file present?";
1810 }
1811 }
1812 RTC_DCHECK(video_capturers_[video_idx].get());
1813 }
ivica5d6a06c2015-09-17 05:30:24 -07001814}
1815
Christoffer Rodbrob4bb4eb2017-11-13 13:03:52 +01001816std::unique_ptr<test::LayerFilteringTransport>
1817VideoQualityTest::CreateSendTransport() {
1818 return rtc::MakeUnique<test::LayerFilteringTransport>(
1819 &task_queue_, params_.pipe, sender_call_.get(), kPayloadTypeVP8,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001820 kPayloadTypeVP9, params_.video[0].selected_tl, params_.ss[0].selected_sl,
1821 payload_type_map_, kVideoSendSsrcs[0],
1822 static_cast<uint32_t>(kVideoSendSsrcs[0] + params_.ss[0].streams.size() -
1823 1));
Christoffer Rodbrob4bb4eb2017-11-13 13:03:52 +01001824}
1825
1826std::unique_ptr<test::DirectTransport>
1827VideoQualityTest::CreateReceiveTransport() {
1828 return rtc::MakeUnique<test::DirectTransport>(
1829 &task_queue_, params_.pipe, receiver_call_.get(), payload_type_map_);
1830}
1831
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001832void VideoQualityTest::CreateVideoStreams() {
1833 RTC_DCHECK(video_send_streams_.empty());
1834 RTC_DCHECK(video_receive_streams_.empty());
1835 RTC_DCHECK_EQ(video_send_configs_.size(), num_video_streams_);
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001836
Stefan Holmerc6b224a2018-02-06 13:52:44 +01001837 // We currently only support testing external fec controllers with a single
1838 // VideoSendStream.
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001839 if (fec_controller_factory_.get()) {
Stefan Holmerc6b224a2018-02-06 13:52:44 +01001840 RTC_DCHECK_LE(video_send_configs_.size(), 1);
1841 }
Ilya Nikolaevskiy4c09d7a2018-03-02 13:37:00 +01001842
1843 // TODO(http://crbug/818127):
1844 // Remove this workaround when ALR is not screenshare-specific.
1845 std::list<size_t> streams_creation_order;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001846 for (size_t i = 0; i < video_send_configs_.size(); ++i) {
Ilya Nikolaevskiy4c09d7a2018-03-02 13:37:00 +01001847 // If dual streams are created, add the screenshare stream last.
1848 if (video_encoder_configs_[i].content_type ==
1849 VideoEncoderConfig::ContentType::kScreen) {
1850 streams_creation_order.push_back(i);
Stefan Holmerc6b224a2018-02-06 13:52:44 +01001851 } else {
Ilya Nikolaevskiy4c09d7a2018-03-02 13:37:00 +01001852 streams_creation_order.push_front(i);
1853 }
1854 }
1855
1856 video_send_streams_.resize(video_send_configs_.size(), nullptr);
1857
1858 for (size_t i : streams_creation_order) {
1859 if (fec_controller_factory_.get()) {
1860 video_send_streams_[i] = sender_call_->CreateVideoSendStream(
1861 video_send_configs_[i].Copy(), video_encoder_configs_[i].Copy(),
1862 fec_controller_factory_->CreateFecController());
1863 } else {
1864 video_send_streams_[i] = sender_call_->CreateVideoSendStream(
1865 video_send_configs_[i].Copy(), video_encoder_configs_[i].Copy());
Stefan Holmerc6b224a2018-02-06 13:52:44 +01001866 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001867 }
1868 for (size_t i = 0; i < video_receive_configs_.size(); ++i) {
1869 video_receive_streams_.push_back(receiver_call_->CreateVideoReceiveStream(
1870 video_receive_configs_[i].Copy()));
1871 }
1872
1873 AssociateFlexfecStreamsWithVideoStreams();
1874}
1875
1876void VideoQualityTest::DestroyStreams() {
1877 CallTest::DestroyStreams();
1878
1879 for (VideoSendStream* video_send_stream : video_send_streams_)
1880 sender_call_->DestroyVideoSendStream(video_send_stream);
1881}
1882
sprang7a975f72015-10-12 06:33:21 -07001883void VideoQualityTest::RunWithAnalyzer(const Params& params) {
Ilya Nikolaevskiyad556762018-01-17 11:48:59 +01001884 rtc::LogMessage::SetLogToStderr(params.logging.logs);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001885 num_video_streams_ = params.call.dual_video ? 2 : 1;
eladalon413ee9a2017-08-22 04:02:52 -07001886 std::unique_ptr<test::LayerFilteringTransport> send_transport;
1887 std::unique_ptr<test::DirectTransport> recv_transport;
1888 FILE* graph_data_output_file = nullptr;
1889 std::unique_ptr<VideoAnalyzer> analyzer;
1890
sprangce4aef12015-11-02 07:23:20 -08001891 params_ = params;
1892
minyue626bc952016-10-31 05:47:02 -07001893 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001894 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1895 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001896 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001897
sprangce4aef12015-11-02 07:23:20 -08001898 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001899 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001900 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001901 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001902 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1903 << "!";
ivica87f83a92015-10-08 05:13:32 -07001904 }
sprang7a975f72015-10-12 06:33:21 -07001905
ilnik98436952017-07-13 00:47:03 -07001906 if (!params.logging.rtc_event_log_name.empty()) {
Bjorn Terelius0a6a2b72018-01-19 17:52:38 +01001907 send_event_log_ = RtcEventLog::Create(RtcEventLog::EncodingType::Legacy);
1908 recv_event_log_ = RtcEventLog::Create(RtcEventLog::EncodingType::Legacy);
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001909 std::unique_ptr<RtcEventLogOutputFile> send_output(
Bjorn Tereliusde939432017-11-20 17:38:14 +01001910 rtc::MakeUnique<RtcEventLogOutputFile>(
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001911 params.logging.rtc_event_log_name + "_send",
1912 RtcEventLog::kUnlimitedOutput));
1913 std::unique_ptr<RtcEventLogOutputFile> recv_output(
1914 rtc::MakeUnique<RtcEventLogOutputFile>(
1915 params.logging.rtc_event_log_name + "_recv",
1916 RtcEventLog::kUnlimitedOutput));
1917 bool event_log_started =
1918 send_event_log_->StartLogging(std::move(send_output),
1919 RtcEventLog::kImmediateOutput) &&
1920 recv_event_log_->StartLogging(std::move(recv_output),
1921 RtcEventLog::kImmediateOutput);
ilnik98436952017-07-13 00:47:03 -07001922 RTC_DCHECK(event_log_started);
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001923 } else {
1924 send_event_log_ = RtcEventLog::CreateNull();
1925 recv_event_log_ = RtcEventLog::CreateNull();
ilnik98436952017-07-13 00:47:03 -07001926 }
1927
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001928 Call::Config send_call_config(send_event_log_.get());
1929 Call::Config recv_call_config(recv_event_log_.get());
1930 send_call_config.bitrate_config = params.call.call_bitrate_config;
1931 recv_call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001932
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001933 task_queue_.SendTask([this, &send_call_config, &recv_call_config,
1934 &send_transport, &recv_transport]() {
1935 CreateCalls(send_call_config, recv_call_config);
1936 send_transport = CreateSendTransport();
1937 recv_transport = CreateReceiveTransport();
1938 });
stefanf116bd02015-10-27 08:29:42 -07001939
sprangce4aef12015-11-02 07:23:20 -08001940 std::string graph_title = params_.analyzer.graph_title;
1941 if (graph_title.empty())
1942 graph_title = VideoQualityTest::GenerateGraphTitle();
sprangc1b57a12017-02-28 08:50:47 -08001943 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
eladalon413ee9a2017-08-22 04:02:52 -07001944 analyzer = rtc::MakeUnique<VideoAnalyzer>(
1945 send_transport.get(), params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001946 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001947 is_quick_test_enabled
1948 ? kFramesSentInQuickTest
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001949 : params_.analyzer.test_durations_secs * params_.video[0].fps,
sprangce4aef12015-11-02 07:23:20 -08001950 graph_data_output_file, graph_title,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001951 kVideoSendSsrcs[params_.ss[0].selected_stream],
1952 kSendRtxSsrcs[params_.ss[0].selected_stream],
1953 static_cast<size_t>(params_.ss[0].selected_stream),
1954 params.ss[0].selected_sl, params_.video[0].selected_tl,
1955 is_quick_test_enabled, clock_, params_.logging.rtp_dump_name);
ivica5d6a06c2015-09-17 05:30:24 -07001956
eladalon413ee9a2017-08-22 04:02:52 -07001957 task_queue_.SendTask([&]() {
1958 analyzer->SetCall(sender_call_.get());
1959 analyzer->SetReceiver(receiver_call_->Receiver());
1960 send_transport->SetReceiver(analyzer.get());
1961 recv_transport->SetReceiver(sender_call_->Receiver());
ivica5d6a06c2015-09-17 05:30:24 -07001962
eladalon413ee9a2017-08-22 04:02:52 -07001963 SetupVideo(analyzer.get(), recv_transport.get());
1964 SetupThumbnails(analyzer.get(), recv_transport.get());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001965 video_receive_configs_[params_.ss[0].selected_stream].renderer =
eladalon413ee9a2017-08-22 04:02:52 -07001966 analyzer.get();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001967 video_send_configs_[0].pre_encode_callback = analyzer->pre_encode_proxy();
1968 RTC_DCHECK(!video_send_configs_[0].post_encode_callback);
1969 video_send_configs_[0].post_encode_callback =
1970 analyzer->encode_timing_proxy();
kthelgason2bc68642017-02-07 07:02:22 -08001971
eladalon413ee9a2017-08-22 04:02:52 -07001972 CreateFlexfecStreams();
1973 CreateVideoStreams();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001974 analyzer->SetSendStream(video_send_streams_[0]);
eladalon413ee9a2017-08-22 04:02:52 -07001975 if (video_receive_streams_.size() == 1)
1976 analyzer->SetReceiveStream(video_receive_streams_[0]);
ivica5d6a06c2015-09-17 05:30:24 -07001977
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001978 video_send_streams_[0]->SetSource(analyzer->OutputInterface(),
1979 degradation_preference_);
eladalon413ee9a2017-08-22 04:02:52 -07001980 SetupThumbnailCapturers(params_.call.num_thumbnails);
1981 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1982 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1983 degradation_preference_);
1984 }
ilnika014cc52017-03-07 04:21:04 -08001985
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001986 CreateCapturers();
ivicac1cc8542015-10-08 03:44:06 -07001987
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001988 analyzer->SetSource(video_capturers_[0].get(), params_.ss[0].infer_streams);
ilnika014cc52017-03-07 04:21:04 -08001989
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001990 for (size_t video_idx = 1; video_idx < num_video_streams_; ++video_idx) {
1991 video_send_streams_[video_idx]->SetSource(
1992 video_capturers_[video_idx].get(), degradation_preference_);
1993 }
1994
1995 StartEncodedFrameLogs(video_send_streams_[0]);
1996 StartEncodedFrameLogs(
1997 video_receive_streams_[params_.ss[0].selected_stream]);
1998 for (VideoSendStream* video_send_stream : video_send_streams_)
1999 video_send_stream->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002000 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
2001 thumbnail_send_stream->Start();
2002 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2003 receive_stream->Start();
2004 for (VideoReceiveStream* thumbnail_receive_stream :
2005 thumbnail_receive_streams_)
2006 thumbnail_receive_stream->Start();
ilnika014cc52017-03-07 04:21:04 -08002007
eladalon413ee9a2017-08-22 04:02:52 -07002008 analyzer->StartMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -07002009
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002010 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
2011 video_capturers_[video_idx]->Start();
2012 }
eladalon413ee9a2017-08-22 04:02:52 -07002013 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
2014 thumbnail_capturers_) {
2015 video_caputurer->Start();
2016 }
2017 });
ivica5d6a06c2015-09-17 05:30:24 -07002018
eladalon413ee9a2017-08-22 04:02:52 -07002019 analyzer->Wait();
ivica5d6a06c2015-09-17 05:30:24 -07002020
Ilya Nikolaevskiy2c72fe82017-10-02 13:01:04 +02002021 event_log_->StopLogging();
2022
eladalon413ee9a2017-08-22 04:02:52 -07002023 task_queue_.SendTask([&]() {
2024 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
2025 thumbnail_capturers_)
2026 video_caputurer->Stop();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002027 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
2028 video_capturers_[video_idx]->Stop();
2029 }
eladalon413ee9a2017-08-22 04:02:52 -07002030 for (VideoReceiveStream* thumbnail_receive_stream :
2031 thumbnail_receive_streams_)
2032 thumbnail_receive_stream->Stop();
2033 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2034 receive_stream->Stop();
2035 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
2036 thumbnail_send_stream->Stop();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002037 for (VideoSendStream* video_send_stream : video_send_streams_)
2038 video_send_stream->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07002039
eladalon413ee9a2017-08-22 04:02:52 -07002040 DestroyStreams();
2041 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -07002042
eladalon413ee9a2017-08-22 04:02:52 -07002043 if (graph_data_output_file)
2044 fclose(graph_data_output_file);
2045
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002046 video_capturers_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07002047 send_transport.reset();
2048 recv_transport.reset();
2049
2050 DestroyCalls();
2051 });
ivica5d6a06c2015-09-17 05:30:24 -07002052}
2053
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01002054void VideoQualityTest::SetupAudio(Transport* transport,
minyuea27172d2016-11-01 05:59:29 -07002055 AudioReceiveStream** audio_receive_stream) {
2056 audio_send_config_ = AudioSendStream::Config(transport);
minyuea27172d2016-11-01 05:59:29 -07002057 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
2058
2059 // Add extension to enable audio send side BWE, and allow audio bit rate
2060 // adaptation.
2061 audio_send_config_.rtp.extensions.clear();
2062 if (params_.call.send_side_bwe) {
2063 audio_send_config_.rtp.extensions.push_back(
2064 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
2065 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08002066 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
2067 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07002068 }
Oskar Sundbom8e07c132018-01-08 16:45:42 +01002069 audio_send_config_.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
2070 kAudioSendPayloadType,
2071 {"OPUS", 48000, 2,
2072 {{"usedtx", (params_.audio.dtx ? "1" : "0")},
2073 {"stereo", "1"}}});
Niels Möller2784a032018-03-28 14:16:04 +02002074 audio_send_config_.encoder_factory = audio_encoder_factory_;
sprang1168fd42017-06-21 09:00:17 -07002075 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
minyuea27172d2016-11-01 05:59:29 -07002076
2077 AudioReceiveStream::Config audio_config;
2078 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
2079 audio_config.rtcp_send_transport = transport;
minyuea27172d2016-11-01 05:59:29 -07002080 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
2081 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
2082 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
Niels Möller2784a032018-03-28 14:16:04 +02002083 audio_config.decoder_factory = audio_decoder_factory_;
minyue20c84cc2017-04-10 16:57:57 -07002084 audio_config.decoder_map = {{kAudioSendPayloadType, {"OPUS", 48000, 2}}};
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002085 if (params_.video[0].enabled && params_.audio.sync_video)
minyuea27172d2016-11-01 05:59:29 -07002086 audio_config.sync_group = kSyncGroup;
2087
sprang1168fd42017-06-21 09:00:17 -07002088 *audio_receive_stream =
2089 receiver_call_->CreateAudioReceiveStream(audio_config);
minyuea27172d2016-11-01 05:59:29 -07002090}
2091
minyue73208662016-08-18 06:28:55 -07002092void VideoQualityTest::RunWithRenderers(const Params& params) {
Ilya Nikolaevskiyad556762018-01-17 11:48:59 +01002093 rtc::LogMessage::SetLogToStderr(params.logging.logs);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002094 num_video_streams_ = params.call.dual_video ? 2 : 1;
eladalon413ee9a2017-08-22 04:02:52 -07002095 std::unique_ptr<test::LayerFilteringTransport> send_transport;
2096 std::unique_ptr<test::DirectTransport> recv_transport;
minyuea27172d2016-11-01 05:59:29 -07002097 std::unique_ptr<test::VideoRenderer> local_preview;
eladalon413ee9a2017-08-22 04:02:52 -07002098 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers;
minyue73208662016-08-18 06:28:55 -07002099 AudioReceiveStream* audio_receive_stream = nullptr;
minyue73208662016-08-18 06:28:55 -07002100
eladalon413ee9a2017-08-22 04:02:52 -07002101 task_queue_.SendTask([&]() {
2102 params_ = params;
2103 CheckParams();
palmkviste75f2042016-09-28 06:19:48 -07002104
eladalon413ee9a2017-08-22 04:02:52 -07002105 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
2106 // match the full stack tests.
2107 Call::Config call_config(event_log_.get());
2108 call_config.bitrate_config = params_.call.call_bitrate_config;
Niels Möller2bf9e732017-08-14 11:26:16 +02002109
Artem Titov3faa8322018-03-07 14:44:00 +01002110 rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device =
2111 TestAudioDeviceModule::CreateTestAudioDeviceModule(
2112 TestAudioDeviceModule::CreatePulsedNoiseCapturer(32000, 48000),
2113 TestAudioDeviceModule::CreateDiscardRenderer(48000), 1.f);
ivica5d6a06c2015-09-17 05:30:24 -07002114
eladalon413ee9a2017-08-22 04:02:52 -07002115 if (params_.audio.enabled) {
eladalon413ee9a2017-08-22 04:02:52 -07002116 AudioState::Config audio_state_config;
eladalon413ee9a2017-08-22 04:02:52 -07002117 audio_state_config.audio_mixer = AudioMixerImpl::Create();
Ivo Creusen62337e52018-01-09 14:17:33 +01002118 audio_state_config.audio_processing = AudioProcessingBuilder().Create();
Fredrik Solenberg2a877972017-12-15 16:42:15 +01002119 audio_state_config.audio_device_module = fake_audio_device;
eladalon413ee9a2017-08-22 04:02:52 -07002120 call_config.audio_state = AudioState::Create(audio_state_config);
Fredrik Solenbergd3195342017-11-21 20:33:05 +01002121 fake_audio_device->RegisterAudioCallback(
2122 call_config.audio_state->audio_transport());
eladalon413ee9a2017-08-22 04:02:52 -07002123 }
minyue73208662016-08-18 06:28:55 -07002124
eladalon413ee9a2017-08-22 04:02:52 -07002125 CreateCalls(call_config, call_config);
2126
2127 // TODO(minyue): consider if this is a good transport even for audio only
2128 // calls.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002129 send_transport = CreateSendTransport();
eladalon413ee9a2017-08-22 04:02:52 -07002130
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002131 recv_transport = CreateReceiveTransport();
eladalon413ee9a2017-08-22 04:02:52 -07002132
2133 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
2134 // least share as much code as possible. That way this test would also match
2135 // the full stack tests better.
2136 send_transport->SetReceiver(receiver_call_->Receiver());
2137 recv_transport->SetReceiver(sender_call_->Receiver());
2138
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002139 if (params_.video[0].enabled) {
eladalon413ee9a2017-08-22 04:02:52 -07002140 // Create video renderers.
2141 local_preview.reset(test::VideoRenderer::Create(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002142 "Local Preview", params_.video[0].width, params_.video[0].height));
eladalon413ee9a2017-08-22 04:02:52 -07002143
2144 SetupVideo(send_transport.get(), recv_transport.get());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002145 video_send_configs_[0].pre_encode_callback = local_preview.get();
eladalon413ee9a2017-08-22 04:02:52 -07002146
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002147 size_t num_streams_processed = 0;
2148 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
2149 const size_t selected_stream_id = params_.ss[video_idx].selected_stream;
2150 const size_t num_streams = params_.ss[video_idx].streams.size();
2151 if (selected_stream_id == num_streams) {
2152 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
2153 std::ostringstream oss;
2154 oss << "Loopback Video #" << video_idx << " - Stream #"
2155 << static_cast<int>(stream_id);
2156 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2157 oss.str().c_str(),
2158 params_.ss[video_idx].streams[stream_id].width,
2159 params_.ss[video_idx].streams[stream_id].height));
2160 video_receive_configs_[stream_id + num_streams_processed].renderer =
2161 loopback_renderers.back().get();
2162 if (params_.audio.enabled && params_.audio.sync_video)
2163 video_receive_configs_[stream_id + num_streams_processed]
2164 .sync_group = kSyncGroup;
2165 }
2166 } else {
2167 std::ostringstream oss;
2168 oss << "Loopback Video #" << video_idx;
2169 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2170 oss.str().c_str(),
2171 params_.ss[video_idx].streams[selected_stream_id].width,
2172 params_.ss[video_idx].streams[selected_stream_id].height));
2173 video_receive_configs_[selected_stream_id + num_streams_processed]
2174 .renderer = loopback_renderers.back().get();
eladalon413ee9a2017-08-22 04:02:52 -07002175 if (params_.audio.enabled && params_.audio.sync_video)
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002176 video_receive_configs_[num_streams_processed + selected_stream_id]
2177 .sync_group = kSyncGroup;
eladalon413ee9a2017-08-22 04:02:52 -07002178 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002179 num_streams_processed += num_streams;
eladalon413ee9a2017-08-22 04:02:52 -07002180 }
eladalon413ee9a2017-08-22 04:02:52 -07002181 CreateFlexfecStreams();
2182 CreateVideoStreams();
2183
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002184 CreateCapturers();
2185 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
2186 video_send_streams_[video_idx]->SetSource(
2187 video_capturers_[video_idx].get(), degradation_preference_);
2188 }
eladalon413ee9a2017-08-22 04:02:52 -07002189 }
2190
2191 if (params_.audio.enabled) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01002192 SetupAudio(send_transport.get(), &audio_receive_stream);
eladalon413ee9a2017-08-22 04:02:52 -07002193 }
2194
2195 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2196 StartEncodedFrameLogs(receive_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002197 StartEncodedFrameLogs(video_send_streams_[0]);
eladalon413ee9a2017-08-22 04:02:52 -07002198
2199 // Start sending and receiving video.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002200 if (params_.video[0].enabled) {
eladalon413ee9a2017-08-22 04:02:52 -07002201 for (VideoReceiveStream* video_receive_stream : video_receive_streams_)
2202 video_receive_stream->Start();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002203 for (VideoSendStream* video_send_stream : video_send_streams_)
2204 video_send_stream->Start();
2205 for (auto& video_capturer : video_capturers_)
2206 video_capturer->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002207 }
2208
2209 if (params_.audio.enabled) {
2210 // Start receiving audio.
2211 audio_receive_stream->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002212
2213 // Start sending audio.
2214 audio_send_stream_->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002215 }
2216 });
minyue73208662016-08-18 06:28:55 -07002217
ivica5d6a06c2015-09-17 05:30:24 -07002218 test::PressEnterToContinue();
2219
eladalon413ee9a2017-08-22 04:02:52 -07002220 task_queue_.SendTask([&]() {
2221 if (params_.audio.enabled) {
2222 // Stop sending audio.
eladalon413ee9a2017-08-22 04:02:52 -07002223 audio_send_stream_->Stop();
minyue73208662016-08-18 06:28:55 -07002224
eladalon413ee9a2017-08-22 04:02:52 -07002225 // Stop receiving audio.
eladalon413ee9a2017-08-22 04:02:52 -07002226 audio_receive_stream->Stop();
eladalon413ee9a2017-08-22 04:02:52 -07002227 }
minyue73208662016-08-18 06:28:55 -07002228
eladalon413ee9a2017-08-22 04:02:52 -07002229 // Stop receiving and sending video.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002230 if (params_.video[0].enabled) {
2231 for (auto& video_capturer : video_capturers_)
2232 video_capturer->Stop();
2233 for (VideoSendStream* video_send_stream : video_send_streams_)
2234 video_send_stream->Stop();
2235 DestroyStreams();
brandtr1293aca2016-11-16 22:47:29 -08002236 }
minyue73208662016-08-18 06:28:55 -07002237
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002238 video_capturers_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07002239 send_transport.reset();
2240 recv_transport.reset();
sprang1168fd42017-06-21 09:00:17 -07002241
eladalon413ee9a2017-08-22 04:02:52 -07002242 local_preview.reset();
2243 loopback_renderers.clear();
2244
2245 DestroyCalls();
2246 });
ivica5d6a06c2015-09-17 05:30:24 -07002247}
2248
palmkviste75f2042016-09-28 06:19:48 -07002249void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002250 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002251 std::ostringstream str;
2252 str << send_logs_++;
2253 std::string prefix =
ilnik98436952017-07-13 00:47:03 -07002254 params_.logging.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07002255 stream->EnableEncodedFrameRecording(
2256 std::vector<rtc::PlatformFile>(
2257 {rtc::CreatePlatformFile(prefix + "1.ivf"),
2258 rtc::CreatePlatformFile(prefix + "2.ivf"),
2259 rtc::CreatePlatformFile(prefix + "3.ivf")}),
ilnik98436952017-07-13 00:47:03 -07002260 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002261 }
2262}
ilnikcb8c1462017-03-09 09:23:30 -08002263
palmkviste75f2042016-09-28 06:19:48 -07002264void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002265 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002266 std::ostringstream str;
2267 str << receive_logs_++;
2268 std::string path =
ilnik98436952017-07-13 00:47:03 -07002269 params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07002270 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
ilnik98436952017-07-13 00:47:03 -07002271 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002272 }
2273}
ivica5d6a06c2015-09-17 05:30:24 -07002274} // namespace webrtc