blob: ec110ff694e65dceb5da405c8adfb60834dd18b3 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/video_coding/codecs/vp8/include/vp8_common_types.h"
28#include "modules/video_coding/codecs/vp9/include/vp9.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/cpu_time.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/flags.h"
31#include "rtc_base/format_macros.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/memory_usage.h"
33#include "rtc_base/pathutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "system_wrappers/include/cpu_info.h"
Mirko Bonadei5f99e652017-12-19 13:40:40 +010035#include "test/rtp_file_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "test/run_loop.h"
37#include "test/statistics.h"
38#include "test/testsupport/fileutils.h"
39#include "test/testsupport/frame_writer.h"
Edward Lemurd1d67432018-01-02 11:54:37 +010040#include "test/testsupport/perf_test.h"
Edward Lemuraf8659a2017-09-27 14:46:24 +020041#include "test/testsupport/test_artifacts.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/vcm_capturer.h"
43#include "test/video_renderer.h"
minyue73208662016-08-18 06:28:55 -070044
ilnikee42d192017-08-22 07:16:20 -070045DEFINE_bool(save_worst_frame,
46 false,
47 "Enable saving a frame with the lowest PSNR to a jpeg file in the "
Edward Lemuraf8659a2017-09-27 14:46:24 +020048 "test_artifacts_dir");
ilnikee42d192017-08-22 07:16:20 -070049
minyue73208662016-08-18 06:28:55 -070050namespace {
51
52constexpr int kSendStatsPollingIntervalMs = 1000;
minyue20c84cc2017-04-10 16:57:57 -070053
minyue73208662016-08-18 06:28:55 -070054constexpr size_t kMaxComparisons = 10;
55constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080056constexpr int kOpusMinBitrateBps = 6000;
57constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 00:53:12 -080058constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 04:21:04 -080059constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
60constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 06:28:55 -070061
sprang1168fd42017-06-21 09:00:17 -070062constexpr int kDefaultMaxQp = cricket::WebRtcVideoChannel::kDefaultQpMax;
63
perkjfa10b552016-10-02 23:45:26 -070064class VideoStreamFactory
65 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
66 public:
67 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
68 : streams_(streams) {}
69
70 private:
71 std::vector<webrtc::VideoStream> CreateEncoderStreams(
72 int width,
73 int height,
74 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -080075 // The highest layer must match the incoming resolution.
76 std::vector<webrtc::VideoStream> streams = streams_;
77 streams[streams_.size() - 1].height = height;
78 streams[streams_.size() - 1].width = width;
Seth Hampson24722b32017-12-22 09:36:42 -080079
80 streams[0].bitrate_priority = encoder_config.bitrate_priority;
mflodmand79f97b2016-12-15 07:24:33 -080081 return streams;
perkjfa10b552016-10-02 23:45:26 -070082 }
83
84 std::vector<webrtc::VideoStream> streams_;
85};
86
brandtr504b95e2016-12-21 02:54:35 -080087bool IsFlexfec(int payload_type) {
88 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
89}
90
minyue73208662016-08-18 06:28:55 -070091} // namespace
ivica5d6a06c2015-09-17 05:30:24 -070092
93namespace webrtc {
94
ivica5d6a06c2015-09-17 05:30:24 -070095class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -070096 public Transport,
ilnik1e7732c2017-02-23 05:07:56 -080097 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 05:30:24 -070098 public:
sprangce4aef12015-11-02 07:23:20 -080099 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700100 const std::string& test_label,
101 double avg_psnr_threshold,
102 double avg_ssim_threshold,
103 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800104 FILE* graph_data_output_file,
105 const std::string& graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -0800106 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 09:16:05 -0800107 uint32_t rtx_ssrc_to_analyze,
ilnikcb8c1462017-03-09 09:23:30 -0800108 size_t selected_stream,
ilnik1e7732c2017-02-23 05:07:56 -0800109 int selected_sl,
110 int selected_tl,
ilnik6b826ef2017-06-16 06:53:48 -0700111 bool is_quick_test_enabled,
ilnik98436952017-07-13 00:47:03 -0700112 Clock* clock,
113 std::string rtp_dump_name)
perkja49cbd32016-09-16 07:53:41 -0700114 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700115 receiver_(nullptr),
stefan889d9652017-07-05 03:03:02 -0700116 call_(nullptr),
ivica5d6a06c2015-09-17 05:30:24 -0700117 send_stream_(nullptr),
philipelfd870db2017-01-23 03:22:15 -0800118 receive_stream_(nullptr),
ilnik6b826ef2017-06-16 06:53:48 -0700119 captured_frame_forwarder_(this, clock),
ivica5d6a06c2015-09-17 05:30:24 -0700120 test_label_(test_label),
121 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800122 graph_title_(graph_title),
123 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 09:16:05 -0800124 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 09:23:30 -0800125 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 05:07:56 -0800126 selected_sl_(selected_sl),
127 selected_tl_(selected_tl),
pbos14fe7082016-04-20 06:35:56 -0700128 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100129 encode_timing_proxy_(this),
stefan889d9652017-07-05 03:03:02 -0700130 last_fec_bytes_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700131 frames_to_process_(duration_frames),
132 frames_recorded_(0),
133 frames_processed_(0),
134 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700135 dropped_frames_before_first_encode_(0),
136 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700137 last_render_time_(0),
138 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 05:07:56 -0800139 total_media_bytes_(0),
140 first_sending_time_(0),
141 last_sending_time_(0),
ilnikdf92c5c2017-02-23 02:08:44 -0800142 cpu_time_(0),
143 wallclock_time_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700144 avg_psnr_threshold_(avg_psnr_threshold),
145 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 00:53:12 -0800146 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100147 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100148 comparison_available_event_(false, false),
ilnik98436952017-07-13 00:47:03 -0700149 done_(true, false),
150 clock_(clock),
151 start_ms_(clock->TimeInMilliseconds()) {
ivica5d6a06c2015-09-17 05:30:24 -0700152 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
153
154 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
155 // so that we don't accidentally starve "real" worker threads (codec etc).
156 // Also, don't allocate more than kMaxComparisonThreads, even if there are
157 // spare cores.
158
159 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800160 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700161 static const uint32_t kMinCoresLeft = 4;
162 static const uint32_t kMaxComparisonThreads = 8;
163
164 if (num_cores <= kMinCoresLeft) {
165 num_cores = 1;
166 } else {
167 num_cores -= kMinCoresLeft;
168 num_cores = std::min(num_cores, kMaxComparisonThreads);
169 }
170
171 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100172 rtc::PlatformThread* thread =
173 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
174 thread->Start();
175 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700176 }
ilnik98436952017-07-13 00:47:03 -0700177
178 if (!rtp_dump_name.empty()) {
179 fprintf(stdout, "Writing rtp dump to %s\n", rtp_dump_name.c_str());
180 rtp_file_writer_.reset(test::RtpFileWriter::Create(
181 test::RtpFileWriter::kRtpDump, rtp_dump_name));
182 }
ivica5d6a06c2015-09-17 05:30:24 -0700183 }
184
185 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100186 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
187 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700188 delete thread;
189 }
190 }
191
192 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
193
ilnik6b826ef2017-06-16 06:53:48 -0700194 void SetSource(test::VideoCapturer* video_capturer, bool respect_sink_wants) {
195 if (respect_sink_wants)
196 captured_frame_forwarder_.SetSource(video_capturer);
197 rtc::VideoSinkWants wants;
198 video_capturer->AddOrUpdateSink(InputInterface(), wants);
199 }
200
stefan889d9652017-07-05 03:03:02 -0700201 void SetCall(Call* call) {
202 rtc::CritScope lock(&crit_);
203 RTC_DCHECK(!call_);
204 call_ = call;
205 }
206
perkja49cbd32016-09-16 07:53:41 -0700207 void SetSendStream(VideoSendStream* stream) {
208 rtc::CritScope lock(&crit_);
209 RTC_DCHECK(!send_stream_);
210 send_stream_ = stream;
211 }
212
philipelfd870db2017-01-23 03:22:15 -0800213 void SetReceiveStream(VideoReceiveStream* stream) {
214 rtc::CritScope lock(&crit_);
215 RTC_DCHECK(!receive_stream_);
216 receive_stream_ = stream;
217 }
218
perkja49cbd32016-09-16 07:53:41 -0700219 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
220 return &captured_frame_forwarder_;
221 }
222 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
223 return &captured_frame_forwarder_;
224 }
225
ivica5d6a06c2015-09-17 05:30:24 -0700226 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100227 rtc::CopyOnWriteBuffer packet,
ivica5d6a06c2015-09-17 05:30:24 -0700228 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700229 // Ignore timestamps of RTCP packets. They're not synchronized with
230 // RTP packet timestamps and so they would confuse wrap_handler_.
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100231 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size())) {
232 return receiver_->DeliverPacket(media_type, std::move(packet),
233 packet_time);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700234 }
ilnik98436952017-07-13 00:47:03 -0700235
236 if (rtp_file_writer_) {
237 test::RtpPacket p;
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100238 memcpy(p.data, packet.cdata(), packet.size());
239 p.length = packet.size();
240 p.original_length = packet.size();
ilnik98436952017-07-13 00:47:03 -0700241 p.time_ms = clock_->TimeInMilliseconds() - start_ms_;
242 rtp_file_writer_->WritePacket(&p);
243 }
244
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100245 RtpUtility::RtpHeaderParser parser(packet.cdata(), packet.size());
ivica5d6a06c2015-09-17 05:30:24 -0700246 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800247 parser.Parse(&header);
ilnik46a00212017-02-10 09:16:05 -0800248 if (!IsFlexfec(header.payloadType) &&
249 (header.ssrc == ssrc_to_analyze_ ||
250 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 02:54:35 -0800251 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
252 // (FlexFEC and media are sent on different SSRCs, which have different
253 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800254 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 05:30:24 -0700255 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800256 int64_t timestamp =
257 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
258 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700259 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
260 }
261
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100262 return receiver_->DeliverPacket(media_type, std::move(packet), packet_time);
ivica5d6a06c2015-09-17 05:30:24 -0700263 }
264
pbos14fe7082016-04-20 06:35:56 -0700265 void PreEncodeOnFrame(const VideoFrame& video_frame) {
266 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800267 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 06:35:56 -0700268 while (frames_.front().timestamp() != video_frame.timestamp()) {
269 ++dropped_frames_before_first_encode_;
270 frames_.pop_front();
271 RTC_CHECK(!frames_.empty());
272 }
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100273 first_encoded_timestamp_ = video_frame.timestamp();
ilnik3dd5ad92017-02-09 04:58:53 -0800274 }
275 }
276
277 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
278 rtc::CritScope lock(&crit_);
279 if (!first_sent_timestamp_ &&
ilnikcb8c1462017-03-09 09:23:30 -0800280 encoded_frame.stream_id_ == selected_stream_) {
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100281 first_sent_timestamp_ = encoded_frame.timestamp_;
pbos14fe7082016-04-20 06:35:56 -0700282 }
283 }
284
stefan1d8a5062015-10-02 03:39:33 -0700285 bool SendRtp(const uint8_t* packet,
286 size_t length,
287 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800288 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700289 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800290 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700291
sprangce4aef12015-11-02 07:23:20 -0800292 int64_t current_time =
293 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 02:23:28 -0800294
sprangce4aef12015-11-02 07:23:20 -0800295 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700296 {
297 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800298 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 08:32:53 -0800299 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 04:58:53 -0800300 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 04:08:56 -0800301 }
ilnik3dd5ad92017-02-09 04:58:53 -0800302
303 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 02:54:35 -0800304 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
305 // (FlexFEC and media are sent on different SSRCs, which have different
306 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800307 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 02:54:35 -0800308 int64_t timestamp =
309 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
310 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 05:07:56 -0800311
312 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 02:54:35 -0800313 encoded_frame_sizes_[timestamp] +=
314 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 05:07:56 -0800315 total_media_bytes_ +=
316 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 02:54:35 -0800317 }
ilnik1e7732c2017-02-23 05:07:56 -0800318 if (first_sending_time_ == 0)
319 first_sending_time_ = current_time;
320 last_sending_time_ = current_time;
sprangce4aef12015-11-02 07:23:20 -0800321 }
ivica5d6a06c2015-09-17 05:30:24 -0700322 }
sprangce4aef12015-11-02 07:23:20 -0800323 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700324 }
325
326 bool SendRtcp(const uint8_t* packet, size_t length) override {
327 return transport_->SendRtcp(packet, length);
328 }
329
nisseeb83a1a2016-03-21 01:27:56 -0700330 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700331 int64_t render_time_ms =
332 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700333
334 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 02:08:44 -0800335
336 StartExcludingCpuThreadTime();
337
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700338 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800339 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700340
sprang16daaa52016-03-09 01:30:24 -0800341 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700342 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700343 // No previous frame rendered, this one was dropped after sending but
344 // before rendering.
345 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 07:02:22 -0800346 } else {
347 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
348 render_time_ms);
pbos14fe7082016-04-20 06:35:56 -0700349 }
ivica5d6a06c2015-09-17 05:30:24 -0700350 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700351 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700352 }
353
354 VideoFrame reference_frame = frames_.front();
355 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800356 int64_t reference_timestamp =
357 wrap_handler_.Unwrap(reference_frame.timestamp());
358 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800359 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100360 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800361 ++send_timestamp;
362 }
sprang16daaa52016-03-09 01:30:24 -0800363 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700364
365 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
366
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100367 last_rendered_frame_ = video_frame;
ilnikdf92c5c2017-02-23 02:08:44 -0800368
369 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 05:30:24 -0700370 }
371
ivica5d6a06c2015-09-17 05:30:24 -0700372 void Wait() {
373 // Frame comparisons can be very expensive. Wait for test to be done, but
374 // at time-out check if frames_processed is going up. If so, give it more
375 // time, otherwise fail. Hopefully this will reduce test flakiness.
376
Peter Boström8c38e8b2015-11-26 17:45:47 +0100377 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800378
ivica5d6a06c2015-09-17 05:30:24 -0700379 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700380 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100381 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700382 int frames_processed;
383 {
384 rtc::CritScope crit(&comparison_lock_);
385 frames_processed = frames_processed_;
386 }
387
388 // Print some output so test infrastructure won't think we've crashed.
389 const char* kKeepAliveMessages[3] = {
390 "Uh, I'm-I'm not quite dead, sir.",
391 "Uh, I-I think uh, I could pull through, sir.",
392 "Actually, I think I'm all right to come with you--"};
393 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
394
395 if (last_frames_processed == -1) {
396 last_frames_processed = frames_processed;
397 continue;
398 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100399 if (frames_processed == last_frames_processed) {
400 EXPECT_GT(frames_processed, last_frames_processed)
401 << "Analyzer stalled while waiting for test to finish.";
402 done_.Set();
403 break;
404 }
ivica5d6a06c2015-09-17 05:30:24 -0700405 last_frames_processed = frames_processed;
406 }
407
408 if (iteration > 0)
409 printf("- Farewell, sweet Concorde!\n");
410
Peter Boström8c38e8b2015-11-26 17:45:47 +0100411 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700412 }
413
pbos14fe7082016-04-20 06:35:56 -0700414 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
415 return &pre_encode_proxy_;
416 }
Peter Boströme4499152016-02-05 11:13:28 +0100417 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
418
ilnikdf92c5c2017-02-23 02:08:44 -0800419 void StartMeasuringCpuProcessTime() {
420 rtc::CritScope lock(&cpu_measurement_lock_);
421 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
422 wallclock_time_ -= rtc::SystemTimeNanos();
423 }
424
425 void StopMeasuringCpuProcessTime() {
426 rtc::CritScope lock(&cpu_measurement_lock_);
427 cpu_time_ += rtc::GetProcessCpuTimeNanos();
428 wallclock_time_ += rtc::SystemTimeNanos();
429 }
430
431 void StartExcludingCpuThreadTime() {
432 rtc::CritScope lock(&cpu_measurement_lock_);
433 cpu_time_ += rtc::GetThreadCpuTimeNanos();
434 }
435
436 void StopExcludingCpuThreadTime() {
437 rtc::CritScope lock(&cpu_measurement_lock_);
438 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
439 }
440
441 double GetCpuUsagePercent() {
442 rtc::CritScope lock(&cpu_measurement_lock_);
443 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
444 }
445
sprangce4aef12015-11-02 07:23:20 -0800446 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700447 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700448
449 private:
450 struct FrameComparison {
451 FrameComparison()
452 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800453 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700454 send_time_ms(0),
455 recv_time_ms(0),
456 render_time_ms(0),
457 encoded_frame_size(0) {}
458
459 FrameComparison(const VideoFrame& reference,
460 const VideoFrame& render,
461 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800462 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700463 int64_t send_time_ms,
464 int64_t recv_time_ms,
465 int64_t render_time_ms,
466 size_t encoded_frame_size)
467 : reference(reference),
468 render(render),
469 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800470 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700471 send_time_ms(send_time_ms),
472 recv_time_ms(recv_time_ms),
473 render_time_ms(render_time_ms),
474 encoded_frame_size(encoded_frame_size) {}
475
nissedf2ceb82016-12-15 06:29:53 -0800476 FrameComparison(bool dropped,
477 int64_t input_time_ms,
478 int64_t send_time_ms,
479 int64_t recv_time_ms,
480 int64_t render_time_ms,
481 size_t encoded_frame_size)
482 : dropped(dropped),
483 input_time_ms(input_time_ms),
484 send_time_ms(send_time_ms),
485 recv_time_ms(recv_time_ms),
486 render_time_ms(render_time_ms),
487 encoded_frame_size(encoded_frame_size) {}
488
489 rtc::Optional<VideoFrame> reference;
490 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700491 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800492 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700493 int64_t send_time_ms;
494 int64_t recv_time_ms;
495 int64_t render_time_ms;
496 size_t encoded_frame_size;
497 };
498
499 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700500 Sample(int dropped,
501 int64_t input_time_ms,
502 int64_t send_time_ms,
503 int64_t recv_time_ms,
504 int64_t render_time_ms,
505 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700506 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700507 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700508 : dropped(dropped),
509 input_time_ms(input_time_ms),
510 send_time_ms(send_time_ms),
511 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700512 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700513 encoded_frame_size(encoded_frame_size),
514 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700515 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700516
ivica8d15bd62015-10-07 02:43:12 -0700517 int dropped;
518 int64_t input_time_ms;
519 int64_t send_time_ms;
520 int64_t recv_time_ms;
521 int64_t render_time_ms;
522 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700523 double psnr;
524 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700525 };
526
Peter Boströme4499152016-02-05 11:13:28 +0100527 // This class receives the send-side OnEncodeTiming and is provided to not
528 // conflict with the receiver-side pre_decode_callback.
529 class OnEncodeTimingProxy : public EncodedFrameObserver {
530 public:
531 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
532
ilnik3dd5ad92017-02-09 04:58:53 -0800533 void EncodedFrameCallback(const EncodedFrame& frame) override {
534 parent_->PostEncodeFrameCallback(frame);
535 }
Peter Boströme4499152016-02-05 11:13:28 +0100536
537 private:
538 VideoAnalyzer* const parent_;
539 };
540
pbos14fe7082016-04-20 06:35:56 -0700541 // This class receives the send-side OnFrame callback and is provided to not
542 // conflict with the receiver-side renderer callback.
543 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
544 public:
545 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
546
547 void OnFrame(const VideoFrame& video_frame) override {
548 parent_->PreEncodeOnFrame(video_frame);
549 }
550
551 private:
552 VideoAnalyzer* const parent_;
553 };
554
ilnik1e7732c2017-02-23 05:07:56 -0800555 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
556 size_t length,
557 const RTPHeader& header) {
ilnik863f03b2017-07-11 02:38:36 -0700558 if (header.payloadType != test::CallTest::kPayloadTypeVP9 &&
559 header.payloadType != test::CallTest::kPayloadTypeVP8) {
ilnik1e7732c2017-02-23 05:07:56 -0800560 return true;
561 } else {
562 // Get VP8 and VP9 specific header to check layers indexes.
563 const uint8_t* payload = packet + header.headerLength;
564 const size_t payload_length = length - header.headerLength;
565 const size_t payload_data_length = payload_length - header.paddingLength;
ilnik863f03b2017-07-11 02:38:36 -0700566 const bool is_vp8 = header.payloadType == test::CallTest::kPayloadTypeVP8;
ilnik1e7732c2017-02-23 05:07:56 -0800567 std::unique_ptr<RtpDepacketizer> depacketizer(
568 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
569 RtpDepacketizer::ParsedPayload parsed_payload;
570 bool result =
571 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
572 RTC_DCHECK(result);
573 const int temporal_idx = static_cast<int>(
574 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
575 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
576 const int spatial_idx = static_cast<int>(
577 is_vp8 ? kNoSpatialIdx
578 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
579 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
580 temporal_idx <= selected_tl_) &&
581 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
582 spatial_idx <= selected_sl_);
583 }
584 }
585
ivica5d6a06c2015-09-17 05:30:24 -0700586 void AddFrameComparison(const VideoFrame& reference,
587 const VideoFrame& render,
588 bool dropped,
589 int64_t render_time_ms)
danilchapa37de392017-09-09 04:17:22 -0700590 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800591 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
592 int64_t send_time_ms = send_times_[reference_timestamp];
593 send_times_.erase(reference_timestamp);
594 int64_t recv_time_ms = recv_times_[reference_timestamp];
595 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700596
sprangce4aef12015-11-02 07:23:20 -0800597 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800598 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800599 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800600 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800601 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
602 if (it != encoded_frame_sizes_.end())
603 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700604
ivica5d6a06c2015-09-17 05:30:24 -0700605 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700606 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800607 comparisons_.push_back(FrameComparison(reference, render, dropped,
608 reference.ntp_time_ms(),
609 send_time_ms, recv_time_ms,
610 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700611 } else {
nissedf2ceb82016-12-15 06:29:53 -0800612 comparisons_.push_back(FrameComparison(dropped,
613 reference.ntp_time_ms(),
614 send_time_ms, recv_time_ms,
615 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700616 }
Peter Boström5811a392015-12-10 13:02:50 +0100617 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700618 }
619
tommi0f8b4032017-02-22 11:22:05 -0800620 static void PollStatsThread(void* obj) {
621 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 05:30:24 -0700622 }
623
tommi0f8b4032017-02-22 11:22:05 -0800624 void PollStats() {
625 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
626 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700627
stefan889d9652017-07-05 03:03:02 -0700628 Call::Stats call_stats = call_->GetStats();
629 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
630
tommi0f8b4032017-02-22 11:22:05 -0800631 VideoSendStream::Stats send_stats = send_stream_->GetStats();
632 // It's not certain that we yet have estimates for any of these stats.
633 // Check that they are positive before mixing them in.
634 if (send_stats.encode_frame_rate > 0)
635 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
636 if (send_stats.avg_encode_time_ms > 0)
637 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
638 if (send_stats.encode_usage_percent > 0)
639 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
640 if (send_stats.media_bitrate_bps > 0)
641 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
stefan889d9652017-07-05 03:03:02 -0700642 size_t fec_bytes = 0;
643 for (auto kv : send_stats.substreams) {
644 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
645 kv.second.rtp_stats.fec.padding_bytes;
646 }
647 fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8);
648 last_fec_bytes_ = fec_bytes;
philipelfd870db2017-01-23 03:22:15 -0800649
tommi0f8b4032017-02-22 11:22:05 -0800650 if (receive_stream_ != nullptr) {
651 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
652 if (receive_stats.decode_ms > 0)
653 decode_time_ms_.AddSample(receive_stats.decode_ms);
654 if (receive_stats.max_decode_ms > 0)
655 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
656 }
ilnikdaa574d2017-03-01 06:46:05 -0800657
658 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 03:22:15 -0800659 }
ivica5d6a06c2015-09-17 05:30:24 -0700660 }
661
662 static bool FrameComparisonThread(void* obj) {
663 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
664 }
665
666 bool CompareFrames() {
667 if (AllFramesRecorded())
668 return false;
669
ivica5d6a06c2015-09-17 05:30:24 -0700670 FrameComparison comparison;
671
672 if (!PopComparison(&comparison)) {
673 // Wait until new comparison task is available, or test is done.
674 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100675 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700676 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100677 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700678 return false;
679 }
680 return true; // Try again.
681 }
682
ilnikdf92c5c2017-02-23 02:08:44 -0800683 StartExcludingCpuThreadTime();
684
ivica5d6a06c2015-09-17 05:30:24 -0700685 PerformFrameComparison(comparison);
686
ilnikdf92c5c2017-02-23 02:08:44 -0800687 StopExcludingCpuThreadTime();
688
ivica5d6a06c2015-09-17 05:30:24 -0700689 if (FrameProcessed()) {
690 PrintResults();
691 if (graph_data_output_file_)
692 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100693 done_.Set();
694 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700695 return false;
696 }
697
698 return true;
699 }
700
701 bool PopComparison(FrameComparison* comparison) {
702 rtc::CritScope crit(&comparison_lock_);
703 // If AllFramesRecorded() is true, it means we have already popped
704 // frames_to_process_ frames from comparisons_, so there is no more work
705 // for this thread to be done. frames_processed_ might still be lower if
706 // all comparisons are not done, but those frames are currently being
707 // worked on by other threads.
708 if (comparisons_.empty() || AllFramesRecorded())
709 return false;
710
711 *comparison = comparisons_.front();
712 comparisons_.pop_front();
713
714 FrameRecorded();
715 return true;
716 }
717
718 // Increment counter for number of frames received for comparison.
719 void FrameRecorded() {
720 rtc::CritScope crit(&comparison_lock_);
721 ++frames_recorded_;
722 }
723
724 // Returns true if all frames to be compared have been taken from the queue.
725 bool AllFramesRecorded() {
726 rtc::CritScope crit(&comparison_lock_);
727 assert(frames_recorded_ <= frames_to_process_);
728 return frames_recorded_ == frames_to_process_;
729 }
730
731 // Increase count of number of frames processed. Returns true if this was the
732 // last frame to be processed.
733 bool FrameProcessed() {
734 rtc::CritScope crit(&comparison_lock_);
735 ++frames_processed_;
736 assert(frames_processed_ <= frames_to_process_);
737 return frames_processed_ == frames_to_process_;
738 }
739
740 void PrintResults() {
ilnikdf92c5c2017-02-23 02:08:44 -0800741 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -0700742 rtc::CritScope crit(&comparison_lock_);
743 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800744 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 01:02:15 -0700745 PrintResult("sender_time", sender_time_, " ms");
746 PrintResult("receiver_time", receiver_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700747 PrintResult("total_delay_incl_network", end_to_end_, " ms");
748 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700749 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800750 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800751 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
stefan889d9652017-07-05 03:03:02 -0700752 PrintResult("fec_bitrate", fec_bitrate_bps_, " bps");
753 PrintResult("send_bandwidth", send_bandwidth_bps_, " bps");
philipelfd870db2017-01-23 03:22:15 -0800754
ilnik59cac992017-07-25 05:45:03 -0700755 if (worst_frame_) {
Edward Lemurd1d67432018-01-02 11:54:37 +0100756 test::PrintResult("min_psnr", "", test_label_.c_str(), worst_frame_->psnr,
757 "dB", false);
ilnik59cac992017-07-25 05:45:03 -0700758 }
759
philipelfd870db2017-01-23 03:22:15 -0800760 if (receive_stream_ != nullptr) {
761 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800762 }
ivica5d6a06c2015-09-17 05:30:24 -0700763
Edward Lemurd1d67432018-01-02 11:54:37 +0100764 test::PrintResult("dropped_frames", "", test_label_.c_str(),
765 dropped_frames_, "frames", false);
766 test::PrintResult("cpu_usage", "", test_label_.c_str(),
767 GetCpuUsagePercent(), "%", false);
ilnikdaa574d2017-03-01 06:46:05 -0800768
769#if defined(WEBRTC_WIN)
770 // On Linux and Mac in Resident Set some unused pages may be counted.
771 // Therefore this metric will depend on order in which tests are run and
772 // will be flaky.
773 PrintResult("memory_usage", memory_usage_, " bytes");
774#endif
ilnik59cac992017-07-25 05:45:03 -0700775
ilnikee42d192017-08-22 07:16:20 -0700776 // Saving only the worst frame for manual analysis. Intention here is to
777 // only detect video corruptions and not to track picture quality. Thus,
778 // jpeg is used here.
oprypin9b2f20c2017-08-29 05:51:57 -0700779 if (FLAG_save_worst_frame && worst_frame_) {
ilnikee42d192017-08-22 07:16:20 -0700780 std::string output_dir;
Edward Lemuraf8659a2017-09-27 14:46:24 +0200781 test::GetTestArtifactsDir(&output_dir);
ilnikee42d192017-08-22 07:16:20 -0700782 std::string output_path =
783 rtc::Pathname(output_dir, test_label_ + ".jpg").pathname();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100784 RTC_LOG(LS_INFO) << "Saving worst frame to " << output_path;
ilnikee42d192017-08-22 07:16:20 -0700785 test::JpegFrameWriter frame_writer(output_path);
786 RTC_CHECK(frame_writer.WriteFrame(worst_frame_->frame,
787 100 /*best quality*/));
ilnik59cac992017-07-25 05:45:03 -0700788 }
ilnikdaa574d2017-03-01 06:46:05 -0800789
ilnik9ae0d762017-02-15 00:53:12 -0800790 // Disable quality check for quick test, as quality checks may fail
791 // because too few samples were collected.
792 if (!is_quick_test_enabled_) {
793 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
794 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
795 }
ivica5d6a06c2015-09-17 05:30:24 -0700796 }
797
798 void PerformFrameComparison(const FrameComparison& comparison) {
799 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700800 double psnr = -1.0;
801 double ssim = -1.0;
ilnik6b826ef2017-06-16 06:53:48 -0700802 if (comparison.reference && !comparison.dropped) {
nissedf2ceb82016-12-15 06:29:53 -0800803 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
804 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700805 }
ivica5d6a06c2015-09-17 05:30:24 -0700806
ivica5d6a06c2015-09-17 05:30:24 -0700807 rtc::CritScope crit(&comparison_lock_);
ilnik59cac992017-07-25 05:45:03 -0700808
809 if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) {
810 worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render});
811 }
812
ivica5d6a06c2015-09-17 05:30:24 -0700813 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800814 samples_.push_back(Sample(
815 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
816 comparison.recv_time_ms, comparison.render_time_ms,
817 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700818 }
stefanb1797672016-08-11 07:00:57 -0700819 if (psnr >= 0.0)
820 psnr_.AddSample(psnr);
821 if (ssim >= 0.0)
822 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700823
824 if (comparison.dropped) {
825 ++dropped_frames_;
826 return;
827 }
828 if (last_render_time_ != 0)
829 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
830 last_render_time_ = comparison.render_time_ms;
831
nissedf2ceb82016-12-15 06:29:53 -0800832 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800833 if (comparison.recv_time_ms > 0) {
834 // If recv_time_ms == 0, this frame consisted of a packets which were all
835 // lost in the transport. Since we were able to render the frame, however,
836 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
837 // happens internally in Call, and we can therefore here not know which
838 // FEC packets that protected the lost media packets. Consequently, we
839 // were not able to record a meaningful recv_time_ms. We therefore skip
840 // this sample.
841 //
842 // The reasoning above does not hold for ULPFEC and RTX, as for those
843 // strategies the timestamp of the received packets is set to the
844 // timestamp of the protected/retransmitted media packet. I.e., then
845 // recv_time_ms != 0, even though the media packets were lost.
846 receiver_time_.AddSample(comparison.render_time_ms -
847 comparison.recv_time_ms);
848 }
nissedf2ceb82016-12-15 06:29:53 -0800849 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700850 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
851 }
852
853 void PrintResult(const char* result_type,
854 test::Statistics stats,
855 const char* unit) {
Edward Lemurd1d67432018-01-02 11:54:37 +0100856 test::PrintResultMeanAndError(result_type, "", test_label_.c_str(),
857 stats.Mean(), stats.StandardDeviation(), unit,
858 false);
ivica5d6a06c2015-09-17 05:30:24 -0700859 }
860
861 void PrintSamplesToFile(void) {
862 FILE* out = graph_data_output_file_;
863 rtc::CritScope crit(&comparison_lock_);
864 std::sort(samples_.begin(), samples_.end(),
865 [](const Sample& A, const Sample& B) -> bool {
866 return A.input_time_ms < B.input_time_ms;
867 });
868
sprangce4aef12015-11-02 07:23:20 -0800869 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700870 fprintf(out, "%" PRIuS "\n", samples_.size());
871 fprintf(out,
872 "dropped "
873 "input_time_ms "
874 "send_time_ms "
875 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700876 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700877 "encoded_frame_size "
878 "psnr "
879 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700880 "encode_time_ms\n");
ivica5d6a06c2015-09-17 05:30:24 -0700881 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700882 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
Niels Möller6b642f72017-12-08 14:11:14 +0100883 " %lf %lf\n",
ivica8d15bd62015-10-07 02:43:12 -0700884 sample.dropped, sample.input_time_ms, sample.send_time_ms,
885 sample.recv_time_ms, sample.render_time_ms,
Niels Möller6b642f72017-12-08 14:11:14 +0100886 sample.encoded_frame_size, sample.psnr, sample.ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700887 }
888 }
889
ilnik1e7732c2017-02-23 05:07:56 -0800890 double GetAverageMediaBitrateBps() {
891 if (last_sending_time_ == first_sending_time_) {
892 return 0;
893 } else {
894 return static_cast<double>(total_media_bytes_) * 8 /
895 (last_sending_time_ - first_sending_time_) *
896 rtc::kNumMillisecsPerSec;
897 }
898 }
899
perkja49cbd32016-09-16 07:53:41 -0700900 // Implements VideoSinkInterface to receive captured frames from a
901 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
902 // as a source to VideoSendStream.
903 // It forwards all input frames to the VideoAnalyzer for later comparison and
904 // forwards the captured frames to the VideoSendStream.
905 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
906 public rtc::VideoSourceInterface<VideoFrame> {
907 public:
ilnik6b826ef2017-06-16 06:53:48 -0700908 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer, Clock* clock)
909 : analyzer_(analyzer),
910 send_stream_input_(nullptr),
911 video_capturer_(nullptr),
912 clock_(clock) {}
913
914 void SetSource(test::VideoCapturer* video_capturer) {
915 video_capturer_ = video_capturer;
916 }
perkja49cbd32016-09-16 07:53:41 -0700917
918 private:
919 void OnFrame(const VideoFrame& video_frame) override {
920 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -0800921 // Frames from the capturer does not have a rtp timestamp.
922 // Create one so it can be used for comparison.
923 RTC_DCHECK_EQ(0, video_frame.timestamp());
ilnik6b826ef2017-06-16 06:53:48 -0700924 if (video_frame.ntp_time_ms() == 0)
925 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
perkja49cbd32016-09-16 07:53:41 -0700926 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -0800927 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -0700928 rtc::CritScope lock(&crit_);
929 if (send_stream_input_)
ilnikb82ac6a2017-05-02 00:48:41 -0700930 send_stream_input_->OnFrame(copy);
perkja49cbd32016-09-16 07:53:41 -0700931 }
932
933 // Called when |send_stream_.SetSource()| is called.
934 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
935 const rtc::VideoSinkWants& wants) override {
ilnik267041c2017-06-27 07:21:01 -0700936 {
937 rtc::CritScope lock(&crit_);
938 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
939 send_stream_input_ = sink;
940 }
ilnik6b826ef2017-06-16 06:53:48 -0700941 if (video_capturer_) {
942 video_capturer_->AddOrUpdateSink(this, wants);
943 }
perkja49cbd32016-09-16 07:53:41 -0700944 }
945
946 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
947 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
948 rtc::CritScope lock(&crit_);
949 RTC_DCHECK(sink == send_stream_input_);
950 send_stream_input_ = nullptr;
951 }
952
953 VideoAnalyzer* const analyzer_;
954 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700955 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_
956 RTC_GUARDED_BY(crit_);
ilnik6b826ef2017-06-16 06:53:48 -0700957 test::VideoCapturer* video_capturer_;
958 Clock* clock_;
perkja49cbd32016-09-16 07:53:41 -0700959 };
960
961 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
962 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800963 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -0700964 }
965
stefan889d9652017-07-05 03:03:02 -0700966 Call* call_;
perkja49cbd32016-09-16 07:53:41 -0700967 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -0800968 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -0700969 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700970 const std::string test_label_;
971 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800972 const std::string graph_title_;
973 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -0800974 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 09:23:30 -0800975 const size_t selected_stream_;
ilnik1e7732c2017-02-23 05:07:56 -0800976 const int selected_sl_;
977 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -0700978 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100979 OnEncodeTimingProxy encode_timing_proxy_;
danilchapa37de392017-09-09 04:17:22 -0700980 std::vector<Sample> samples_ RTC_GUARDED_BY(comparison_lock_);
danilchapa37de392017-09-09 04:17:22 -0700981 test::Statistics sender_time_ RTC_GUARDED_BY(comparison_lock_);
982 test::Statistics receiver_time_ RTC_GUARDED_BY(comparison_lock_);
983 test::Statistics psnr_ RTC_GUARDED_BY(comparison_lock_);
984 test::Statistics ssim_ RTC_GUARDED_BY(comparison_lock_);
985 test::Statistics end_to_end_ RTC_GUARDED_BY(comparison_lock_);
986 test::Statistics rendered_delta_ RTC_GUARDED_BY(comparison_lock_);
987 test::Statistics encoded_frame_size_ RTC_GUARDED_BY(comparison_lock_);
988 test::Statistics encode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
989 test::Statistics encode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
990 test::Statistics encode_usage_percent_ RTC_GUARDED_BY(comparison_lock_);
991 test::Statistics decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
992 test::Statistics decode_time_max_ms_ RTC_GUARDED_BY(comparison_lock_);
993 test::Statistics media_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
994 test::Statistics fec_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
995 test::Statistics send_bandwidth_bps_ RTC_GUARDED_BY(comparison_lock_);
996 test::Statistics memory_usage_ RTC_GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 06:46:05 -0800997
ilnik59cac992017-07-25 05:45:03 -0700998 struct FrameWithPsnr {
999 double psnr;
1000 VideoFrame frame;
1001 };
1002
1003 // Rendered frame with worst PSNR is saved for further analysis.
danilchapa37de392017-09-09 04:17:22 -07001004 rtc::Optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_);
ilnik59cac992017-07-25 05:45:03 -07001005
stefan889d9652017-07-05 03:03:02 -07001006 size_t last_fec_bytes_;
ivica5d6a06c2015-09-17 05:30:24 -07001007
1008 const int frames_to_process_;
1009 int frames_recorded_;
1010 int frames_processed_;
1011 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -07001012 int dropped_frames_before_first_encode_;
1013 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -07001014 int64_t last_render_time_;
1015 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -08001016 int64_t total_media_bytes_;
1017 int64_t first_sending_time_;
1018 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -07001019
danilchapa37de392017-09-09 04:17:22 -07001020 int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
1021 int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
ilnikdf92c5c2017-02-23 02:08:44 -08001022 rtc::CriticalSection cpu_measurement_lock_;
1023
ivica5d6a06c2015-09-17 05:30:24 -07001024 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07001025 std::deque<VideoFrame> frames_ RTC_GUARDED_BY(crit_);
1026 rtc::Optional<VideoFrame> last_rendered_frame_ RTC_GUARDED_BY(crit_);
1027 rtc::TimestampWrapAroundHandler wrap_handler_ RTC_GUARDED_BY(crit_);
1028 std::map<int64_t, int64_t> send_times_ RTC_GUARDED_BY(crit_);
1029 std::map<int64_t, int64_t> recv_times_ RTC_GUARDED_BY(crit_);
1030 std::map<int64_t, size_t> encoded_frame_sizes_ RTC_GUARDED_BY(crit_);
1031 rtc::Optional<uint32_t> first_encoded_timestamp_ RTC_GUARDED_BY(crit_);
1032 rtc::Optional<uint32_t> first_sent_timestamp_ RTC_GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -07001033 const double avg_psnr_threshold_;
1034 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -08001035 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -07001036
1037 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001038 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1039 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001040 rtc::Event comparison_available_event_;
danilchapa37de392017-09-09 04:17:22 -07001041 std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001042 rtc::Event done_;
ilnik98436952017-07-13 00:47:03 -07001043
1044 std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
1045 Clock* const clock_;
1046 const int64_t start_ms_;
ivica5d6a06c2015-09-17 05:30:24 -07001047};
1048
palmkviste75f2042016-09-28 06:19:48 -07001049VideoQualityTest::VideoQualityTest()
minyue20c84cc2017-04-10 16:57:57 -07001050 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {
1051 payload_type_map_ = test::CallTest::payload_type_map_;
1052 RTC_DCHECK(payload_type_map_.find(kPayloadTypeH264) ==
1053 payload_type_map_.end());
1054 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP8) ==
1055 payload_type_map_.end());
1056 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP9) ==
1057 payload_type_map_.end());
1058 payload_type_map_[kPayloadTypeH264] = webrtc::MediaType::VIDEO;
1059 payload_type_map_[kPayloadTypeVP8] = webrtc::MediaType::VIDEO;
1060 payload_type_map_[kPayloadTypeVP9] = webrtc::MediaType::VIDEO;
1061}
ivica5d6a06c2015-09-17 05:30:24 -07001062
minyue626bc952016-10-31 05:47:02 -07001063VideoQualityTest::Params::Params()
ilnik98436952017-07-13 00:47:03 -07001064 : call({false, Call::Config::BitrateConfig(), 0}),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001065 video{{false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
1066 false, ""},
1067 {false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
1068 false, ""}},
minyue4c8b9422017-03-21 04:11:43 -07001069 audio({false, false, false}),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001070 screenshare{{false, false, 10, 0}, {false, false, 10, 0}},
minyue626bc952016-10-31 05:47:02 -07001071 analyzer({"", 0.0, 0.0, 0, "", ""}),
1072 pipe(),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001073 ss{{std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()},
1074 {std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}},
ilnik98436952017-07-13 00:47:03 -07001075 logging({false, "", "", ""}) {}
minyue626bc952016-10-31 05:47:02 -07001076
1077VideoQualityTest::Params::~Params() = default;
1078
ivica5d6a06c2015-09-17 05:30:24 -07001079void VideoQualityTest::TestBody() {}
1080
sprangce4aef12015-11-02 07:23:20 -08001081std::string VideoQualityTest::GenerateGraphTitle() const {
1082 std::stringstream ss;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001083 ss << params_.video[0].codec;
1084 ss << " (" << params_.video[0].target_bitrate_bps / 1000 << "kbps";
1085 ss << ", " << params_.video[0].fps << " FPS";
1086 if (params_.screenshare[0].scroll_duration)
1087 ss << ", " << params_.screenshare[0].scroll_duration << "s scroll";
1088 if (params_.ss[0].streams.size() > 1)
1089 ss << ", Stream #" << params_.ss[0].selected_stream;
1090 if (params_.ss[0].num_spatial_layers > 1)
1091 ss << ", Layer #" << params_.ss[0].selected_sl;
sprangce4aef12015-11-02 07:23:20 -08001092 ss << ")";
1093 return ss.str();
1094}
1095
1096void VideoQualityTest::CheckParams() {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001097 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1098 // Iterate over primary and secondary video streams.
1099 if (!params_.video[video_idx].enabled)
1100 return;
1101 // Add a default stream in none specified.
1102 if (params_.ss[video_idx].streams.empty())
1103 params_.ss[video_idx].streams.push_back(
1104 VideoQualityTest::DefaultVideoStream(params_, video_idx));
1105 if (params_.ss[video_idx].num_spatial_layers == 0)
1106 params_.ss[video_idx].num_spatial_layers = 1;
sprangce4aef12015-11-02 07:23:20 -08001107
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001108 if (params_.pipe.loss_percent != 0 ||
1109 params_.pipe.queue_length_packets != 0) {
1110 // Since LayerFilteringTransport changes the sequence numbers, we can't
1111 // use that feature with pack loss, since the NACK request would end up
1112 // retransmitting the wrong packets.
1113 RTC_CHECK(params_.ss[video_idx].selected_sl == -1 ||
1114 params_.ss[video_idx].selected_sl ==
1115 params_.ss[video_idx].num_spatial_layers - 1);
1116 RTC_CHECK(params_.video[video_idx].selected_tl == -1 ||
1117 params_.video[video_idx].selected_tl ==
1118 params_.video[video_idx].num_temporal_layers - 1);
1119 }
sprangce4aef12015-11-02 07:23:20 -08001120
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001121 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as
1122 // it does in some parts of the code?
1123 RTC_CHECK_GE(params_.video[video_idx].max_bitrate_bps,
1124 params_.video[video_idx].target_bitrate_bps);
1125 RTC_CHECK_GE(params_.video[video_idx].target_bitrate_bps,
1126 params_.video[video_idx].min_bitrate_bps);
1127 RTC_CHECK_LT(params_.video[video_idx].selected_tl,
1128 params_.video[video_idx].num_temporal_layers);
1129 RTC_CHECK_LE(params_.ss[video_idx].selected_stream,
1130 params_.ss[video_idx].streams.size());
1131 for (const VideoStream& stream : params_.ss[video_idx].streams) {
1132 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1133 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1134 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
1135 }
1136 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1137 // the total bitrate? We anyway have to update them in the case bitrate
1138 // estimator changes the total bitrates.
1139 RTC_CHECK_GE(params_.ss[video_idx].num_spatial_layers, 1);
1140 RTC_CHECK_LE(params_.ss[video_idx].selected_sl,
1141 params_.ss[video_idx].num_spatial_layers);
1142 RTC_CHECK(
1143 params_.ss[video_idx].spatial_layers.empty() ||
1144 params_.ss[video_idx].spatial_layers.size() ==
1145 static_cast<size_t>(params_.ss[video_idx].num_spatial_layers));
1146 if (params_.video[video_idx].codec == "VP8") {
1147 RTC_CHECK_EQ(params_.ss[video_idx].num_spatial_layers, 1);
1148 } else if (params_.video[video_idx].codec == "VP9") {
1149 RTC_CHECK_EQ(params_.ss[video_idx].streams.size(), 1);
1150 }
1151 RTC_CHECK_GE(params_.call.num_thumbnails, 0);
1152 if (params_.call.num_thumbnails > 0) {
1153 RTC_CHECK_EQ(params_.ss[video_idx].num_spatial_layers, 1);
1154 RTC_CHECK_EQ(params_.ss[video_idx].streams.size(), 3);
1155 RTC_CHECK_EQ(params_.video[video_idx].num_temporal_layers, 3);
1156 RTC_CHECK_EQ(params_.video[video_idx].codec, "VP8");
1157 }
1158 // Dual streams with FEC not supported in tests yet.
1159 RTC_CHECK(!params_.video[video_idx].flexfec || num_video_streams_ == 1);
1160 RTC_CHECK(!params_.video[video_idx].ulpfec || num_video_streams_ == 1);
ilnika014cc52017-03-07 04:21:04 -08001161 }
sprangce4aef12015-11-02 07:23:20 -08001162}
1163
1164// Static.
1165std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1166 // Parse comma separated nonnegative integers, where some elements may be
1167 // empty. The empty values are replaced with -1.
1168 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1169 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1170 std::vector<int> result;
1171 if (str.empty())
1172 return result;
1173
1174 const char* p = str.c_str();
1175 int value = -1;
1176 int pos;
1177 while (*p) {
1178 if (*p == ',') {
1179 result.push_back(value);
1180 value = -1;
1181 ++p;
1182 continue;
1183 }
1184 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1185 << "Unexpected non-number value.";
1186 p += pos;
1187 }
1188 result.push_back(value);
1189 return result;
1190}
1191
1192// Static.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001193VideoStream VideoQualityTest::DefaultVideoStream(const Params& params,
1194 size_t video_idx) {
sprangce4aef12015-11-02 07:23:20 -08001195 VideoStream stream;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001196 stream.width = params.video[video_idx].width;
1197 stream.height = params.video[video_idx].height;
1198 stream.max_framerate = params.video[video_idx].fps;
1199 stream.min_bitrate_bps = params.video[video_idx].min_bitrate_bps;
1200 stream.target_bitrate_bps = params.video[video_idx].target_bitrate_bps;
1201 stream.max_bitrate_bps = params.video[video_idx].max_bitrate_bps;
sprang1168fd42017-06-21 09:00:17 -07001202 stream.max_qp = kDefaultMaxQp;
sprang6ef1b342017-03-13 02:01:32 -07001203 // TODO(sprang): Can we make this less of a hack?
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001204 if (params.video[video_idx].num_temporal_layers == 2) {
sprang6ef1b342017-03-13 02:01:32 -07001205 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001206 } else if (params.video[video_idx].num_temporal_layers == 3) {
sprang6ef1b342017-03-13 02:01:32 -07001207 stream.temporal_layer_thresholds_bps.push_back(stream.max_bitrate_bps / 4);
1208 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
sprangff19d352017-09-06 07:14:02 -07001209 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001210 RTC_CHECK_LE(params.video[video_idx].num_temporal_layers,
1211 kMaxTemporalStreams);
1212 for (int i = 0; i < params.video[video_idx].num_temporal_layers - 1; ++i) {
sprangff19d352017-09-06 07:14:02 -07001213 stream.temporal_layer_thresholds_bps.push_back(static_cast<int>(
1214 stream.max_bitrate_bps * kVp8LayerRateAlloction[0][i] + 0.5));
1215 }
ilnika014cc52017-03-07 04:21:04 -08001216 }
1217 return stream;
1218}
1219
1220// Static.
1221VideoStream VideoQualityTest::DefaultThumbnailStream() {
1222 VideoStream stream;
1223 stream.width = 320;
1224 stream.height = 180;
1225 stream.max_framerate = 7;
1226 stream.min_bitrate_bps = 7500;
1227 stream.target_bitrate_bps = 37500;
1228 stream.max_bitrate_bps = 50000;
sprang1168fd42017-06-21 09:00:17 -07001229 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 07:23:20 -08001230 return stream;
1231}
1232
1233// Static.
1234void VideoQualityTest::FillScalabilitySettings(
1235 Params* params,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001236 size_t video_idx,
sprangce4aef12015-11-02 07:23:20 -08001237 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 09:00:17 -07001238 int num_streams,
sprangce4aef12015-11-02 07:23:20 -08001239 size_t selected_stream,
1240 int num_spatial_layers,
1241 int selected_sl,
1242 const std::vector<std::string>& sl_descriptors) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001243 if (params->ss[video_idx].streams.empty() &&
1244 params->ss[video_idx].infer_streams) {
sprang1168fd42017-06-21 09:00:17 -07001245 webrtc::VideoEncoderConfig encoder_config;
1246 encoder_config.content_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001247 params->screenshare[video_idx].enabled
sprang1168fd42017-06-21 09:00:17 -07001248 ? webrtc::VideoEncoderConfig::ContentType::kScreen
1249 : webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001250 encoder_config.max_bitrate_bps = params->video[video_idx].max_bitrate_bps;
1251 encoder_config.min_transmit_bitrate_bps =
1252 params->video[video_idx].min_transmit_bps;
sprang1168fd42017-06-21 09:00:17 -07001253 encoder_config.number_of_streams = num_streams;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001254 encoder_config.spatial_layers = params->ss[video_idx].spatial_layers;
sprang1168fd42017-06-21 09:00:17 -07001255 encoder_config.video_stream_factory =
1256 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001257 params->video[video_idx].codec, kDefaultMaxQp,
1258 params->video[video_idx].fps,
1259 params->screenshare[video_idx].enabled, true);
1260 params->ss[video_idx].streams =
sprang1168fd42017-06-21 09:00:17 -07001261 encoder_config.video_stream_factory->CreateEncoderStreams(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001262 static_cast<int>(params->video[video_idx].width),
1263 static_cast<int>(params->video[video_idx].height), encoder_config);
sprang1168fd42017-06-21 09:00:17 -07001264 } else {
1265 // Read VideoStream and SpatialLayer elements from a list of comma separated
1266 // lists. To use a default value for an element, use -1 or leave empty.
1267 // Validity checks performed in CheckParams.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001268 RTC_CHECK(params->ss[video_idx].streams.empty());
sprang1168fd42017-06-21 09:00:17 -07001269 for (auto descriptor : stream_descriptors) {
1270 if (descriptor.empty())
1271 continue;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001272 VideoStream stream =
1273 VideoQualityTest::DefaultVideoStream(*params, video_idx);
sprang1168fd42017-06-21 09:00:17 -07001274 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1275 if (v[0] != -1)
1276 stream.width = static_cast<size_t>(v[0]);
1277 if (v[1] != -1)
1278 stream.height = static_cast<size_t>(v[1]);
1279 if (v[2] != -1)
1280 stream.max_framerate = v[2];
1281 if (v[3] != -1)
1282 stream.min_bitrate_bps = v[3];
1283 if (v[4] != -1)
1284 stream.target_bitrate_bps = v[4];
1285 if (v[5] != -1)
1286 stream.max_bitrate_bps = v[5];
1287 if (v.size() > 6 && v[6] != -1)
1288 stream.max_qp = v[6];
1289 if (v.size() > 7) {
1290 stream.temporal_layer_thresholds_bps.clear();
1291 stream.temporal_layer_thresholds_bps.insert(
1292 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1293 } else {
1294 // Automatic TL thresholds for more than two layers not supported.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001295 RTC_CHECK_LE(params->video[video_idx].num_temporal_layers, 2);
sprang1168fd42017-06-21 09:00:17 -07001296 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001297 params->ss[video_idx].streams.push_back(stream);
sprangce4aef12015-11-02 07:23:20 -08001298 }
sprangce4aef12015-11-02 07:23:20 -08001299 }
sprangce4aef12015-11-02 07:23:20 -08001300
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001301 params->ss[video_idx].num_spatial_layers = std::max(1, num_spatial_layers);
1302 params->ss[video_idx].selected_stream = selected_stream;
sprang1168fd42017-06-21 09:00:17 -07001303
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001304 params->ss[video_idx].selected_sl = selected_sl;
1305 RTC_CHECK(params->ss[video_idx].spatial_layers.empty());
sprangce4aef12015-11-02 07:23:20 -08001306 for (auto descriptor : sl_descriptors) {
1307 if (descriptor.empty())
1308 continue;
1309 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1310 RTC_CHECK_GT(v[2], 0);
1311
1312 SpatialLayer layer;
1313 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1314 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1315 layer.target_bitrate_bps = v[2];
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001316 params->ss[video_idx].spatial_layers.push_back(layer);
sprangce4aef12015-11-02 07:23:20 -08001317 }
1318}
1319
minyuea27172d2016-11-01 05:59:29 -07001320void VideoQualityTest::SetupVideo(Transport* send_transport,
1321 Transport* recv_transport) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001322 size_t total_streams_used = 0;
1323 size_t num_flexfec_streams = params_.video[0].flexfec ? 1 : 0;
1324 CreateAudioAndFecSendConfigs(0, num_flexfec_streams, send_transport);
1325 CreateMatchingAudioAndFecConfigs(recv_transport);
1326 video_receive_configs_.clear();
1327 video_send_configs_.clear();
1328 video_encoders_.clear();
1329 video_encoder_configs_.clear();
1330 allocated_decoders_.clear();
1331 bool decode_all_receive_streams = true;
1332 size_t num_video_substreams = params_.ss[0].streams.size();
1333 RTC_CHECK(num_video_streams_ > 0);
1334 video_encoder_configs_.resize(num_video_streams_);
1335 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1336 video_send_configs_.push_back(VideoSendStream::Config(send_transport));
1337 video_encoders_.push_back(nullptr);
1338 video_encoder_configs_.push_back(VideoEncoderConfig());
1339 num_video_substreams = params_.ss[video_idx].streams.size();
1340 RTC_CHECK_GT(num_video_substreams, 0);
1341 CreateVideoSendConfig(&video_send_configs_[video_idx], num_video_substreams,
1342 total_streams_used, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001343
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001344 int payload_type;
1345 if (params_.video[video_idx].codec == "H264") {
1346 video_encoders_[video_idx] =
1347 H264Encoder::Create(cricket::VideoCodec("H264"));
1348 payload_type = kPayloadTypeH264;
1349 } else if (params_.video[video_idx].codec == "VP8") {
1350 if (params_.screenshare[video_idx].enabled &&
1351 params_.ss[video_idx].streams.size() > 1) {
1352 // Simulcast screenshare needs a simulcast encoder adapter to work,
1353 // since encoders usually can't natively do simulcast with different
1354 // frame rates for the different layers.
1355 video_encoders_[video_idx].reset(
1356 new SimulcastEncoderAdapter(new InternalEncoderFactory()));
1357 } else {
1358 video_encoders_[video_idx] = VP8Encoder::Create();
1359 }
1360 payload_type = kPayloadTypeVP8;
1361 } else if (params_.video[video_idx].codec == "VP9") {
1362 video_encoders_[video_idx] = VP9Encoder::Create();
1363 payload_type = kPayloadTypeVP9;
ilnikcb8c1462017-03-09 09:23:30 -08001364 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001365 RTC_NOTREACHED() << "Codec not supported!";
1366 return;
ilnikcb8c1462017-03-09 09:23:30 -08001367 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001368 video_send_configs_[video_idx].encoder_settings.encoder =
1369 video_encoders_[video_idx].get();
1370 video_send_configs_[video_idx].encoder_settings.payload_name =
1371 params_.video[video_idx].codec;
1372 video_send_configs_[video_idx].encoder_settings.payload_type = payload_type;
1373 video_send_configs_[video_idx].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1374 video_send_configs_[video_idx].rtp.rtx.payload_type = kSendRtxPayloadType;
1375 for (size_t i = 0; i < num_video_substreams; ++i) {
1376 video_send_configs_[video_idx].rtp.rtx.ssrcs.push_back(
1377 kSendRtxSsrcs[i + total_streams_used]);
ilnik9fd9f6c2017-03-02 08:10:10 -08001378 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001379 video_send_configs_[video_idx].rtp.extensions.clear();
1380 if (params_.call.send_side_bwe) {
1381 video_send_configs_[video_idx].rtp.extensions.push_back(
1382 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1383 test::kTransportSequenceNumberExtensionId));
1384 } else {
1385 video_send_configs_[video_idx].rtp.extensions.push_back(RtpExtension(
1386 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1387 }
1388 video_send_configs_[video_idx].rtp.extensions.push_back(
1389 RtpExtension(RtpExtension::kVideoContentTypeUri,
1390 test::kVideoContentTypeExtensionId));
1391 video_send_configs_[video_idx].rtp.extensions.push_back(RtpExtension(
1392 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
1393
1394 video_encoder_configs_[video_idx].min_transmit_bitrate_bps =
1395 params_.video[video_idx].min_transmit_bps;
1396
1397 video_send_configs_[video_idx].suspend_below_min_bitrate =
1398 params_.video[video_idx].suspend_below_min_bitrate;
1399
1400 video_encoder_configs_[video_idx].number_of_streams =
1401 params_.ss[video_idx].streams.size();
1402 video_encoder_configs_[video_idx].max_bitrate_bps = 0;
1403 for (size_t i = 0; i < params_.ss[video_idx].streams.size(); ++i) {
1404 video_encoder_configs_[video_idx].max_bitrate_bps +=
1405 params_.ss[video_idx].streams[i].max_bitrate_bps;
1406 }
1407 if (params_.ss[video_idx].infer_streams) {
1408 video_encoder_configs_[video_idx].video_stream_factory =
1409 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1410 params_.video[video_idx].codec,
1411 params_.ss[video_idx].streams[0].max_qp,
1412 params_.video[video_idx].fps,
1413 params_.screenshare[video_idx].enabled, true);
1414 } else {
1415 video_encoder_configs_[video_idx].video_stream_factory =
1416 new rtc::RefCountedObject<VideoStreamFactory>(
1417 params_.ss[video_idx].streams);
1418 }
1419
1420 video_encoder_configs_[video_idx].spatial_layers =
1421 params_.ss[video_idx].spatial_layers;
1422
1423 std::vector<VideoReceiveStream::Config> new_receive_configs =
1424 CreateMatchingVideoReceiveConfigs(video_send_configs_[video_idx],
1425 recv_transport);
1426
1427 decode_all_receive_streams = params_.ss[video_idx].selected_stream ==
1428 params_.ss[video_idx].streams.size();
1429
1430 for (size_t i = 0; i < num_video_substreams; ++i) {
1431 new_receive_configs[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1432 new_receive_configs[i].rtp.rtx_ssrc =
1433 kSendRtxSsrcs[i + total_streams_used];
1434 new_receive_configs[i]
1435 .rtp.rtx_associated_payload_types[kSendRtxPayloadType] = payload_type;
1436 new_receive_configs[i].rtp.transport_cc = params_.call.send_side_bwe;
1437 new_receive_configs[i].rtp.remb = !params_.call.send_side_bwe;
1438 // Enable RTT calculation so NTP time estimator will work.
1439 new_receive_configs[i].rtp.rtcp_xr.receiver_reference_time_report = true;
1440 // Force fake decoders on non-selected simulcast streams.
1441 if (!decode_all_receive_streams &&
1442 i != params_.ss[video_idx].selected_stream) {
1443 VideoReceiveStream::Decoder decoder;
1444 decoder.decoder = new test::FakeDecoder();
1445 decoder.payload_type =
1446 video_send_configs_[video_idx].encoder_settings.payload_type;
1447 decoder.payload_name =
1448 video_send_configs_[video_idx].encoder_settings.payload_name;
1449 new_receive_configs[i].decoders.clear();
1450 allocated_decoders_.emplace_back(decoder.decoder);
1451 new_receive_configs[i].decoders.push_back(decoder);
1452 }
1453 }
1454
1455 for (VideoReceiveStream::Config& config : new_receive_configs) {
1456 video_receive_configs_.push_back(config.Copy());
1457 }
1458
1459 if (params_.screenshare[video_idx].enabled) {
1460 // Fill out codec settings.
1461 video_encoder_configs_[video_idx].content_type =
1462 VideoEncoderConfig::ContentType::kScreen;
1463 degradation_preference_ =
1464 VideoSendStream::DegradationPreference::kMaintainResolution;
1465 if (params_.video[video_idx].codec == "VP8") {
1466 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1467 vp8_settings.denoisingOn = false;
1468 vp8_settings.frameDroppingOn = false;
1469 vp8_settings.numberOfTemporalLayers = static_cast<unsigned char>(
1470 params_.video[video_idx].num_temporal_layers);
1471 video_encoder_configs_[video_idx].encoder_specific_settings =
1472 new rtc::RefCountedObject<
1473 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1474 } else if (params_.video[video_idx].codec == "VP9") {
1475 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1476 vp9_settings.denoisingOn = false;
1477 vp9_settings.frameDroppingOn = false;
1478 vp9_settings.numberOfTemporalLayers = static_cast<unsigned char>(
1479 params_.video[video_idx].num_temporal_layers);
1480 vp9_settings.numberOfSpatialLayers = static_cast<unsigned char>(
1481 params_.ss[video_idx].num_spatial_layers);
1482 video_encoder_configs_[video_idx].encoder_specific_settings =
1483 new rtc::RefCountedObject<
1484 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1485 }
1486 } else if (params_.ss[video_idx].num_spatial_layers > 1) {
1487 // If SVC mode without screenshare, still need to set codec specifics.
1488 RTC_CHECK(params_.video[video_idx].codec == "VP9");
1489 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1490 vp9_settings.numberOfTemporalLayers = static_cast<unsigned char>(
1491 params_.video[video_idx].num_temporal_layers);
1492 vp9_settings.numberOfSpatialLayers =
1493 static_cast<unsigned char>(params_.ss[video_idx].num_spatial_layers);
1494 video_encoder_configs_[video_idx].encoder_specific_settings =
1495 new rtc::RefCountedObject<
1496 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1497 }
1498 total_streams_used += num_video_substreams;
sprangce4aef12015-11-02 07:23:20 -08001499 }
brandtr1293aca2016-11-16 22:47:29 -08001500
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001501 // FEC supported only for single video stream mode yet.
1502 if (params_.video[0].flexfec) {
Ilya Nikolaevskiy38aaf692017-12-22 14:39:09 +01001503 video_send_configs_[0].rtp.flexfec.payload_type = kFlexfecPayloadType;
1504 video_send_configs_[0].rtp.flexfec.ssrc = kFlexfecSendSsrc;
sprang1168fd42017-06-21 09:00:17 -07001505 if (decode_all_receive_streams) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001506 for (uint32_t media_ssrc : video_send_configs_[0].rtp.ssrcs) {
1507 video_send_configs_[0].rtp.flexfec.protected_media_ssrcs.push_back(
sprang1168fd42017-06-21 09:00:17 -07001508 media_ssrc);
1509 }
1510 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001511 video_send_configs_[0].rtp.flexfec.protected_media_ssrcs = {
1512 kVideoSendSsrcs[params_.ss[0].selected_stream]};
sprang1168fd42017-06-21 09:00:17 -07001513 }
brandtr1293aca2016-11-16 22:47:29 -08001514
brandtr8313a6f2017-01-13 07:41:19 -08001515 // The matching receive config is _not_ created by
1516 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1517 // Set up the receive config manually instead.
1518 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001519 flexfec_receive_config.payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001520 video_send_configs_[0].rtp.flexfec.payload_type;
1521 flexfec_receive_config.remote_ssrc =
1522 video_send_configs_[0].rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001523 flexfec_receive_config.protected_media_ssrcs =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001524 video_send_configs_[0].rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001525 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001526 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1527 if (params_.call.send_side_bwe) {
1528 flexfec_receive_config.rtp_header_extensions.push_back(
1529 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1530 test::kTransportSequenceNumberExtensionId));
1531 } else {
1532 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1533 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1534 }
brandtr1293aca2016-11-16 22:47:29 -08001535 flexfec_receive_configs_.push_back(flexfec_receive_config);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001536 if (num_video_substreams > 0) {
brandtr7cd28b92017-09-22 00:26:25 -07001537 video_receive_configs_[0].rtp.protected_by_flexfec = true;
1538 }
brandtr1293aca2016-11-16 22:47:29 -08001539 }
1540
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001541 if (params_.video[0].ulpfec) {
1542 video_send_configs_[0].rtp.ulpfec.red_payload_type = kRedPayloadType;
1543 video_send_configs_[0].rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1544 video_send_configs_[0].rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
brandtr1293aca2016-11-16 22:47:29 -08001545
sprang1168fd42017-06-21 09:00:17 -07001546 if (decode_all_receive_streams) {
1547 for (auto it = video_receive_configs_.begin();
1548 it != video_receive_configs_.end(); ++it) {
nisse3b3622f2017-09-26 02:49:21 -07001549 it->rtp.red_payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001550 video_send_configs_[0].rtp.ulpfec.red_payload_type;
nisse3b3622f2017-09-26 02:49:21 -07001551 it->rtp.ulpfec_payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001552 video_send_configs_[0].rtp.ulpfec.ulpfec_payload_type;
1553 it->rtp.rtx_associated_payload_types
1554 [video_send_configs_[0].rtp.ulpfec.red_rtx_payload_type] =
1555 video_send_configs_[0].rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 09:00:17 -07001556 }
1557 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001558 video_receive_configs_[params_.ss[0].selected_stream]
1559 .rtp.red_payload_type =
1560 video_send_configs_[0].rtp.ulpfec.red_payload_type;
1561 video_receive_configs_[params_.ss[0].selected_stream]
nisse3b3622f2017-09-26 02:49:21 -07001562 .rtp.ulpfec_payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001563 video_send_configs_[0].rtp.ulpfec.ulpfec_payload_type;
1564 video_receive_configs_[params_.ss[0].selected_stream]
1565 .rtp.rtx_associated_payload_types
1566 [video_send_configs_[0].rtp.ulpfec.red_rtx_payload_type] =
1567 video_send_configs_[0].rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 09:00:17 -07001568 }
brandtr1293aca2016-11-16 22:47:29 -08001569 }
ivica5d6a06c2015-09-17 05:30:24 -07001570}
1571
ilnika014cc52017-03-07 04:21:04 -08001572void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1573 Transport* recv_transport) {
ilnik98436952017-07-13 00:47:03 -07001574 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001575 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1576
1577 // Thumbnails will be send in the other way: from receiver_call to
1578 // sender_call.
1579 VideoSendStream::Config thumbnail_send_config(recv_transport);
1580 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1581 thumbnail_send_config.encoder_settings.encoder =
1582 thumbnail_encoders_.back().get();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001583 thumbnail_send_config.encoder_settings.payload_name =
1584 params_.video[0].codec;
ilnika014cc52017-03-07 04:21:04 -08001585 thumbnail_send_config.encoder_settings.payload_type = kPayloadTypeVP8;
1586 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1587 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1588 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1589 thumbnail_send_config.rtp.extensions.clear();
1590 if (params_.call.send_side_bwe) {
1591 thumbnail_send_config.rtp.extensions.push_back(
1592 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1593 test::kTransportSequenceNumberExtensionId));
1594 } else {
1595 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1596 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1597 }
1598
1599 VideoEncoderConfig thumbnail_encoder_config;
1600 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1601 thumbnail_send_config.suspend_below_min_bitrate =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001602 params_.video[0].suspend_below_min_bitrate;
ilnika014cc52017-03-07 04:21:04 -08001603 thumbnail_encoder_config.number_of_streams = 1;
1604 thumbnail_encoder_config.max_bitrate_bps = 50000;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001605 if (params_.ss[0].infer_streams) {
ilnik6b826ef2017-06-16 06:53:48 -07001606 thumbnail_encoder_config.video_stream_factory =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001607 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss[0].streams);
ilnik6b826ef2017-06-16 06:53:48 -07001608 } else {
1609 thumbnail_encoder_config.video_stream_factory =
1610 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001611 params_.video[0].codec, params_.ss[0].streams[0].max_qp,
1612 params_.video[0].fps, params_.screenshare[0].enabled, true);
ilnik6b826ef2017-06-16 06:53:48 -07001613 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001614 thumbnail_encoder_config.spatial_layers = params_.ss[0].spatial_layers;
ilnika014cc52017-03-07 04:21:04 -08001615
1616 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1617 thumbnail_receive_config.rtp.remb = false;
1618 thumbnail_receive_config.rtp.transport_cc = true;
1619 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1620 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1621 thumbnail_receive_config.rtp.extensions.push_back(extension);
1622 thumbnail_receive_config.renderer = &fake_renderer_;
1623
1624 VideoReceiveStream::Decoder decoder =
1625 test::CreateMatchingDecoder(thumbnail_send_config.encoder_settings);
1626 allocated_decoders_.push_back(
1627 std::unique_ptr<VideoDecoder>(decoder.decoder));
1628 thumbnail_receive_config.decoders.clear();
1629 thumbnail_receive_config.decoders.push_back(decoder);
1630 thumbnail_receive_config.rtp.remote_ssrc =
1631 thumbnail_send_config.rtp.ssrcs[0];
1632
1633 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1634 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
nisse26e3abb2017-08-25 04:44:25 -07001635 thumbnail_receive_config.rtp
1636 .rtx_associated_payload_types[kSendRtxPayloadType] = kPayloadTypeVP8;
ilnika014cc52017-03-07 04:21:04 -08001637 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1638 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1639
1640 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1641 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1642 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1643 }
1644
ilnik98436952017-07-13 00:47:03 -07001645 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001646 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1647 thumbnail_send_configs_[i].Copy(),
1648 thumbnail_encoder_configs_[i].Copy()));
1649 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1650 thumbnail_receive_configs_[i].Copy()));
1651 }
1652}
1653
1654void VideoQualityTest::DestroyThumbnailStreams() {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001655 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_) {
ilnika014cc52017-03-07 04:21:04 -08001656 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001657 }
ilnika014cc52017-03-07 04:21:04 -08001658 thumbnail_send_streams_.clear();
1659 for (VideoReceiveStream* thumbnail_receive_stream :
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001660 thumbnail_receive_streams_) {
ilnika014cc52017-03-07 04:21:04 -08001661 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001662 }
ilnika014cc52017-03-07 04:21:04 -08001663 thumbnail_send_streams_.clear();
1664 thumbnail_receive_streams_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07001665 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1666 thumbnail_capturers_) {
1667 video_caputurer.reset();
1668 }
ilnika014cc52017-03-07 04:21:04 -08001669}
1670
ilnika014cc52017-03-07 04:21:04 -08001671void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1672 VideoStream thumbnail = DefaultThumbnailStream();
1673 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1674 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1675 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
1676 thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -08001677 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -08001678 }
1679}
1680
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001681std::unique_ptr<test::FrameGenerator> VideoQualityTest::CreateFrameGenerator(
1682 size_t video_idx) {
1683 // Setup frame generator.
1684 const size_t kWidth = 1850;
1685 const size_t kHeight = 1110;
1686 std::unique_ptr<test::FrameGenerator> frame_generator;
1687 if (params_.screenshare[video_idx].generate_slides) {
1688 frame_generator = test::FrameGenerator::CreateSlideGenerator(
1689 kWidth, kHeight,
1690 params_.screenshare[video_idx].slide_change_interval *
1691 params_.video[video_idx].fps);
ivica5d6a06c2015-09-17 05:30:24 -07001692 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001693 std::vector<std::string> slides = params_.screenshare[video_idx].slides;
1694 if (slides.size() == 0) {
1695 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1696 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1697 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1698 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1699 }
1700 if (params_.screenshare[video_idx].scroll_duration == 0) {
1701 // Cycle image every slide_change_interval seconds.
1702 frame_generator = test::FrameGenerator::CreateFromYuvFile(
1703 slides, kWidth, kHeight,
1704 params_.screenshare[video_idx].slide_change_interval *
1705 params_.video[video_idx].fps);
ivica5d6a06c2015-09-17 05:30:24 -07001706 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001707 RTC_CHECK_LE(params_.video[video_idx].width, kWidth);
1708 RTC_CHECK_LE(params_.video[video_idx].height, kHeight);
1709 RTC_CHECK_GT(params_.screenshare[video_idx].slide_change_interval, 0);
1710 const int kPauseDurationMs =
1711 (params_.screenshare[video_idx].slide_change_interval -
1712 params_.screenshare[video_idx].scroll_duration) *
1713 1000;
1714 RTC_CHECK_LE(params_.screenshare[video_idx].scroll_duration,
1715 params_.screenshare[video_idx].slide_change_interval);
1716
1717 frame_generator = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1718 clock_, slides, kWidth, kHeight, params_.video[video_idx].width,
1719 params_.video[video_idx].height,
1720 params_.screenshare[video_idx].scroll_duration * 1000,
1721 kPauseDurationMs);
ivica5d6a06c2015-09-17 05:30:24 -07001722 }
1723 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001724 return frame_generator;
1725}
1726
1727void VideoQualityTest::CreateCapturers() {
1728 video_capturers_.resize(num_video_streams_);
1729 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1730 if (params_.screenshare[video_idx].enabled) {
1731 std::unique_ptr<test::FrameGenerator> frame_generator =
1732 CreateFrameGenerator(video_idx);
1733 test::FrameGeneratorCapturer* frame_generator_capturer =
1734 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator),
1735 params_.video[video_idx].fps);
1736 EXPECT_TRUE(frame_generator_capturer->Init());
1737 video_capturers_[video_idx].reset(frame_generator_capturer);
1738 } else {
1739 if (params_.video[video_idx].clip_name == "Generator") {
1740 video_capturers_[video_idx].reset(test::FrameGeneratorCapturer::Create(
1741 static_cast<int>(params_.video[video_idx].width),
1742 static_cast<int>(params_.video[video_idx].height),
1743 params_.video[video_idx].fps, clock_));
1744 } else if (params_.video[video_idx].clip_name.empty()) {
1745 video_capturers_[video_idx].reset(test::VcmCapturer::Create(
1746 params_.video[video_idx].width, params_.video[video_idx].height,
1747 params_.video[video_idx].fps,
1748 params_.video[video_idx].capture_device_index));
1749 if (!video_capturers_[video_idx]) {
1750 // Failed to get actual camera, use chroma generator as backup.
1751 video_capturers_[video_idx].reset(
1752 test::FrameGeneratorCapturer::Create(
1753 static_cast<int>(params_.video[video_idx].width),
1754 static_cast<int>(params_.video[video_idx].height),
1755 params_.video[video_idx].fps, clock_));
1756 }
1757 } else {
1758 video_capturers_[video_idx].reset(
1759 test::FrameGeneratorCapturer::CreateFromYuvFile(
1760 test::ResourcePath(params_.video[video_idx].clip_name, "yuv"),
1761 params_.video[video_idx].width, params_.video[video_idx].height,
1762 params_.video[video_idx].fps, clock_));
1763 ASSERT_TRUE(video_capturers_[video_idx])
1764 << "Could not create capturer for "
1765 << params_.video[video_idx].clip_name
1766 << ".yuv. Is this resource file present?";
1767 }
1768 }
1769 RTC_DCHECK(video_capturers_[video_idx].get());
1770 }
ivica5d6a06c2015-09-17 05:30:24 -07001771}
1772
Christoffer Rodbrob4bb4eb2017-11-13 13:03:52 +01001773std::unique_ptr<test::LayerFilteringTransport>
1774VideoQualityTest::CreateSendTransport() {
1775 return rtc::MakeUnique<test::LayerFilteringTransport>(
1776 &task_queue_, params_.pipe, sender_call_.get(), kPayloadTypeVP8,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001777 kPayloadTypeVP9, params_.video[0].selected_tl, params_.ss[0].selected_sl,
1778 payload_type_map_, kVideoSendSsrcs[0],
1779 static_cast<uint32_t>(kVideoSendSsrcs[0] + params_.ss[0].streams.size() -
1780 1));
Christoffer Rodbrob4bb4eb2017-11-13 13:03:52 +01001781}
1782
1783std::unique_ptr<test::DirectTransport>
1784VideoQualityTest::CreateReceiveTransport() {
1785 return rtc::MakeUnique<test::DirectTransport>(
1786 &task_queue_, params_.pipe, receiver_call_.get(), payload_type_map_);
1787}
1788
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001789void VideoQualityTest::CreateVideoStreams() {
1790 RTC_DCHECK(video_send_streams_.empty());
1791 RTC_DCHECK(video_receive_streams_.empty());
1792 RTC_DCHECK_EQ(video_send_configs_.size(), num_video_streams_);
1793 for (size_t i = 0; i < video_send_configs_.size(); ++i) {
1794 video_send_streams_.push_back(sender_call_->CreateVideoSendStream(
1795 video_send_configs_[i].Copy(), video_encoder_configs_[i].Copy()));
1796 }
1797 for (size_t i = 0; i < video_receive_configs_.size(); ++i) {
1798 video_receive_streams_.push_back(receiver_call_->CreateVideoReceiveStream(
1799 video_receive_configs_[i].Copy()));
1800 }
1801
1802 AssociateFlexfecStreamsWithVideoStreams();
1803}
1804
1805void VideoQualityTest::DestroyStreams() {
1806 CallTest::DestroyStreams();
1807
1808 for (VideoSendStream* video_send_stream : video_send_streams_)
1809 sender_call_->DestroyVideoSendStream(video_send_stream);
1810}
1811
sprang7a975f72015-10-12 06:33:21 -07001812void VideoQualityTest::RunWithAnalyzer(const Params& params) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001813 num_video_streams_ = params.call.dual_video ? 2 : 1;
eladalon413ee9a2017-08-22 04:02:52 -07001814 std::unique_ptr<test::LayerFilteringTransport> send_transport;
1815 std::unique_ptr<test::DirectTransport> recv_transport;
1816 FILE* graph_data_output_file = nullptr;
1817 std::unique_ptr<VideoAnalyzer> analyzer;
1818
sprangce4aef12015-11-02 07:23:20 -08001819 params_ = params;
1820
minyue626bc952016-10-31 05:47:02 -07001821 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001822 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1823 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001824 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001825
sprangce4aef12015-11-02 07:23:20 -08001826 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001827 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001828 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001829 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001830 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1831 << "!";
ivica87f83a92015-10-08 05:13:32 -07001832 }
sprang7a975f72015-10-12 06:33:21 -07001833
ilnik98436952017-07-13 00:47:03 -07001834 if (!params.logging.rtc_event_log_name.empty()) {
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001835 send_event_log_ =
1836 RtcEventLog::Create(clock_, RtcEventLog::EncodingType::Legacy);
1837 recv_event_log_ =
1838 RtcEventLog::Create(clock_, RtcEventLog::EncodingType::Legacy);
1839 std::unique_ptr<RtcEventLogOutputFile> send_output(
Bjorn Tereliusde939432017-11-20 17:38:14 +01001840 rtc::MakeUnique<RtcEventLogOutputFile>(
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001841 params.logging.rtc_event_log_name + "_send",
1842 RtcEventLog::kUnlimitedOutput));
1843 std::unique_ptr<RtcEventLogOutputFile> recv_output(
1844 rtc::MakeUnique<RtcEventLogOutputFile>(
1845 params.logging.rtc_event_log_name + "_recv",
1846 RtcEventLog::kUnlimitedOutput));
1847 bool event_log_started =
1848 send_event_log_->StartLogging(std::move(send_output),
1849 RtcEventLog::kImmediateOutput) &&
1850 recv_event_log_->StartLogging(std::move(recv_output),
1851 RtcEventLog::kImmediateOutput);
ilnik98436952017-07-13 00:47:03 -07001852 RTC_DCHECK(event_log_started);
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001853 } else {
1854 send_event_log_ = RtcEventLog::CreateNull();
1855 recv_event_log_ = RtcEventLog::CreateNull();
ilnik98436952017-07-13 00:47:03 -07001856 }
1857
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001858 Call::Config send_call_config(send_event_log_.get());
1859 Call::Config recv_call_config(recv_event_log_.get());
1860 send_call_config.bitrate_config = params.call.call_bitrate_config;
1861 recv_call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001862
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001863 task_queue_.SendTask([this, &send_call_config, &recv_call_config,
1864 &send_transport, &recv_transport]() {
1865 CreateCalls(send_call_config, recv_call_config);
1866 send_transport = CreateSendTransport();
1867 recv_transport = CreateReceiveTransport();
1868 });
stefanf116bd02015-10-27 08:29:42 -07001869
sprangce4aef12015-11-02 07:23:20 -08001870 std::string graph_title = params_.analyzer.graph_title;
1871 if (graph_title.empty())
1872 graph_title = VideoQualityTest::GenerateGraphTitle();
sprangc1b57a12017-02-28 08:50:47 -08001873 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
eladalon413ee9a2017-08-22 04:02:52 -07001874 analyzer = rtc::MakeUnique<VideoAnalyzer>(
1875 send_transport.get(), params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001876 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001877 is_quick_test_enabled
1878 ? kFramesSentInQuickTest
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001879 : params_.analyzer.test_durations_secs * params_.video[0].fps,
sprangce4aef12015-11-02 07:23:20 -08001880 graph_data_output_file, graph_title,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001881 kVideoSendSsrcs[params_.ss[0].selected_stream],
1882 kSendRtxSsrcs[params_.ss[0].selected_stream],
1883 static_cast<size_t>(params_.ss[0].selected_stream),
1884 params.ss[0].selected_sl, params_.video[0].selected_tl,
1885 is_quick_test_enabled, clock_, params_.logging.rtp_dump_name);
ivica5d6a06c2015-09-17 05:30:24 -07001886
eladalon413ee9a2017-08-22 04:02:52 -07001887 task_queue_.SendTask([&]() {
1888 analyzer->SetCall(sender_call_.get());
1889 analyzer->SetReceiver(receiver_call_->Receiver());
1890 send_transport->SetReceiver(analyzer.get());
1891 recv_transport->SetReceiver(sender_call_->Receiver());
ivica5d6a06c2015-09-17 05:30:24 -07001892
eladalon413ee9a2017-08-22 04:02:52 -07001893 SetupVideo(analyzer.get(), recv_transport.get());
1894 SetupThumbnails(analyzer.get(), recv_transport.get());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001895 video_receive_configs_[params_.ss[0].selected_stream].renderer =
eladalon413ee9a2017-08-22 04:02:52 -07001896 analyzer.get();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001897 video_send_configs_[0].pre_encode_callback = analyzer->pre_encode_proxy();
1898 RTC_DCHECK(!video_send_configs_[0].post_encode_callback);
1899 video_send_configs_[0].post_encode_callback =
1900 analyzer->encode_timing_proxy();
kthelgason2bc68642017-02-07 07:02:22 -08001901
eladalon413ee9a2017-08-22 04:02:52 -07001902 CreateFlexfecStreams();
1903 CreateVideoStreams();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001904 analyzer->SetSendStream(video_send_streams_[0]);
eladalon413ee9a2017-08-22 04:02:52 -07001905 if (video_receive_streams_.size() == 1)
1906 analyzer->SetReceiveStream(video_receive_streams_[0]);
ivica5d6a06c2015-09-17 05:30:24 -07001907
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001908 video_send_streams_[0]->SetSource(analyzer->OutputInterface(),
1909 degradation_preference_);
eladalon413ee9a2017-08-22 04:02:52 -07001910 SetupThumbnailCapturers(params_.call.num_thumbnails);
1911 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1912 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1913 degradation_preference_);
1914 }
ilnika014cc52017-03-07 04:21:04 -08001915
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001916 CreateCapturers();
ivicac1cc8542015-10-08 03:44:06 -07001917
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001918 analyzer->SetSource(video_capturers_[0].get(), params_.ss[0].infer_streams);
ilnika014cc52017-03-07 04:21:04 -08001919
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001920 for (size_t video_idx = 1; video_idx < num_video_streams_; ++video_idx) {
1921 video_send_streams_[video_idx]->SetSource(
1922 video_capturers_[video_idx].get(), degradation_preference_);
1923 }
1924
1925 StartEncodedFrameLogs(video_send_streams_[0]);
1926 StartEncodedFrameLogs(
1927 video_receive_streams_[params_.ss[0].selected_stream]);
1928 for (VideoSendStream* video_send_stream : video_send_streams_)
1929 video_send_stream->Start();
eladalon413ee9a2017-08-22 04:02:52 -07001930 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1931 thumbnail_send_stream->Start();
1932 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1933 receive_stream->Start();
1934 for (VideoReceiveStream* thumbnail_receive_stream :
1935 thumbnail_receive_streams_)
1936 thumbnail_receive_stream->Start();
ilnika014cc52017-03-07 04:21:04 -08001937
eladalon413ee9a2017-08-22 04:02:52 -07001938 analyzer->StartMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -07001939
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001940 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1941 video_capturers_[video_idx]->Start();
1942 }
eladalon413ee9a2017-08-22 04:02:52 -07001943 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1944 thumbnail_capturers_) {
1945 video_caputurer->Start();
1946 }
1947 });
ivica5d6a06c2015-09-17 05:30:24 -07001948
eladalon413ee9a2017-08-22 04:02:52 -07001949 analyzer->Wait();
ivica5d6a06c2015-09-17 05:30:24 -07001950
Ilya Nikolaevskiy2c72fe82017-10-02 13:01:04 +02001951 event_log_->StopLogging();
1952
eladalon413ee9a2017-08-22 04:02:52 -07001953 task_queue_.SendTask([&]() {
1954 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1955 thumbnail_capturers_)
1956 video_caputurer->Stop();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001957 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1958 video_capturers_[video_idx]->Stop();
1959 }
eladalon413ee9a2017-08-22 04:02:52 -07001960 for (VideoReceiveStream* thumbnail_receive_stream :
1961 thumbnail_receive_streams_)
1962 thumbnail_receive_stream->Stop();
1963 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1964 receive_stream->Stop();
1965 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1966 thumbnail_send_stream->Stop();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001967 for (VideoSendStream* video_send_stream : video_send_streams_)
1968 video_send_stream->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001969
eladalon413ee9a2017-08-22 04:02:52 -07001970 DestroyStreams();
1971 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -07001972
eladalon413ee9a2017-08-22 04:02:52 -07001973 if (graph_data_output_file)
1974 fclose(graph_data_output_file);
1975
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001976 video_capturers_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07001977 send_transport.reset();
1978 recv_transport.reset();
1979
1980 DestroyCalls();
1981 });
ivica5d6a06c2015-09-17 05:30:24 -07001982}
1983
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001984void VideoQualityTest::SetupAudio(Transport* transport,
minyuea27172d2016-11-01 05:59:29 -07001985 AudioReceiveStream** audio_receive_stream) {
1986 audio_send_config_ = AudioSendStream::Config(transport);
minyuea27172d2016-11-01 05:59:29 -07001987 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1988
1989 // Add extension to enable audio send side BWE, and allow audio bit rate
1990 // adaptation.
1991 audio_send_config_.rtp.extensions.clear();
1992 if (params_.call.send_side_bwe) {
1993 audio_send_config_.rtp.extensions.push_back(
1994 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1995 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001996 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1997 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001998 }
Oskar Sundbom8e07c132018-01-08 16:45:42 +01001999 audio_send_config_.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
2000 kAudioSendPayloadType,
2001 {"OPUS", 48000, 2,
2002 {{"usedtx", (params_.audio.dtx ? "1" : "0")},
2003 {"stereo", "1"}}});
Rasmus Brandt31027342017-09-29 13:48:12 +00002004 audio_send_config_.encoder_factory = encoder_factory_;
sprang1168fd42017-06-21 09:00:17 -07002005 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
minyuea27172d2016-11-01 05:59:29 -07002006
2007 AudioReceiveStream::Config audio_config;
2008 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
2009 audio_config.rtcp_send_transport = transport;
minyuea27172d2016-11-01 05:59:29 -07002010 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
2011 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
2012 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
Rasmus Brandt31027342017-09-29 13:48:12 +00002013 audio_config.decoder_factory = decoder_factory_;
minyue20c84cc2017-04-10 16:57:57 -07002014 audio_config.decoder_map = {{kAudioSendPayloadType, {"OPUS", 48000, 2}}};
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002015 if (params_.video[0].enabled && params_.audio.sync_video)
minyuea27172d2016-11-01 05:59:29 -07002016 audio_config.sync_group = kSyncGroup;
2017
sprang1168fd42017-06-21 09:00:17 -07002018 *audio_receive_stream =
2019 receiver_call_->CreateAudioReceiveStream(audio_config);
minyuea27172d2016-11-01 05:59:29 -07002020}
2021
minyue73208662016-08-18 06:28:55 -07002022void VideoQualityTest::RunWithRenderers(const Params& params) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002023 num_video_streams_ = params.call.dual_video ? 2 : 1;
eladalon413ee9a2017-08-22 04:02:52 -07002024 std::unique_ptr<test::LayerFilteringTransport> send_transport;
2025 std::unique_ptr<test::DirectTransport> recv_transport;
minyuea27172d2016-11-01 05:59:29 -07002026 std::unique_ptr<test::VideoRenderer> local_preview;
eladalon413ee9a2017-08-22 04:02:52 -07002027 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers;
minyue73208662016-08-18 06:28:55 -07002028 AudioReceiveStream* audio_receive_stream = nullptr;
minyue73208662016-08-18 06:28:55 -07002029
eladalon413ee9a2017-08-22 04:02:52 -07002030 task_queue_.SendTask([&]() {
2031 params_ = params;
2032 CheckParams();
palmkviste75f2042016-09-28 06:19:48 -07002033
eladalon413ee9a2017-08-22 04:02:52 -07002034 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
2035 // match the full stack tests.
2036 Call::Config call_config(event_log_.get());
2037 call_config.bitrate_config = params_.call.call_bitrate_config;
Niels Möller2bf9e732017-08-14 11:26:16 +02002038
Fredrik Solenberg2a877972017-12-15 16:42:15 +01002039 rtc::scoped_refptr<test::FakeAudioDevice> fake_audio_device =
2040 new rtc::RefCountedObject<test::FakeAudioDevice>(
2041 test::FakeAudioDevice::CreatePulsedNoiseCapturer(32000, 48000),
2042 test::FakeAudioDevice::CreateDiscardRenderer(48000),
2043 1.f);
ivica5d6a06c2015-09-17 05:30:24 -07002044
eladalon413ee9a2017-08-22 04:02:52 -07002045 if (params_.audio.enabled) {
eladalon413ee9a2017-08-22 04:02:52 -07002046 AudioState::Config audio_state_config;
eladalon413ee9a2017-08-22 04:02:52 -07002047 audio_state_config.audio_mixer = AudioMixerImpl::Create();
Ivo Creusen62337e52018-01-09 14:17:33 +01002048 audio_state_config.audio_processing = AudioProcessingBuilder().Create();
Fredrik Solenberg2a877972017-12-15 16:42:15 +01002049 audio_state_config.audio_device_module = fake_audio_device;
eladalon413ee9a2017-08-22 04:02:52 -07002050 call_config.audio_state = AudioState::Create(audio_state_config);
Fredrik Solenbergd3195342017-11-21 20:33:05 +01002051 fake_audio_device->RegisterAudioCallback(
2052 call_config.audio_state->audio_transport());
eladalon413ee9a2017-08-22 04:02:52 -07002053 }
minyue73208662016-08-18 06:28:55 -07002054
eladalon413ee9a2017-08-22 04:02:52 -07002055 CreateCalls(call_config, call_config);
2056
2057 // TODO(minyue): consider if this is a good transport even for audio only
2058 // calls.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002059 send_transport = CreateSendTransport();
eladalon413ee9a2017-08-22 04:02:52 -07002060
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002061 recv_transport = CreateReceiveTransport();
eladalon413ee9a2017-08-22 04:02:52 -07002062
2063 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
2064 // least share as much code as possible. That way this test would also match
2065 // the full stack tests better.
2066 send_transport->SetReceiver(receiver_call_->Receiver());
2067 recv_transport->SetReceiver(sender_call_->Receiver());
2068
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002069 if (params_.video[0].enabled) {
eladalon413ee9a2017-08-22 04:02:52 -07002070 // Create video renderers.
2071 local_preview.reset(test::VideoRenderer::Create(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002072 "Local Preview", params_.video[0].width, params_.video[0].height));
eladalon413ee9a2017-08-22 04:02:52 -07002073
2074 SetupVideo(send_transport.get(), recv_transport.get());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002075 video_send_configs_[0].pre_encode_callback = local_preview.get();
eladalon413ee9a2017-08-22 04:02:52 -07002076
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002077 size_t num_streams_processed = 0;
2078 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
2079 const size_t selected_stream_id = params_.ss[video_idx].selected_stream;
2080 const size_t num_streams = params_.ss[video_idx].streams.size();
2081 if (selected_stream_id == num_streams) {
2082 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
2083 std::ostringstream oss;
2084 oss << "Loopback Video #" << video_idx << " - Stream #"
2085 << static_cast<int>(stream_id);
2086 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2087 oss.str().c_str(),
2088 params_.ss[video_idx].streams[stream_id].width,
2089 params_.ss[video_idx].streams[stream_id].height));
2090 video_receive_configs_[stream_id + num_streams_processed].renderer =
2091 loopback_renderers.back().get();
2092 if (params_.audio.enabled && params_.audio.sync_video)
2093 video_receive_configs_[stream_id + num_streams_processed]
2094 .sync_group = kSyncGroup;
2095 }
2096 } else {
2097 std::ostringstream oss;
2098 oss << "Loopback Video #" << video_idx;
2099 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2100 oss.str().c_str(),
2101 params_.ss[video_idx].streams[selected_stream_id].width,
2102 params_.ss[video_idx].streams[selected_stream_id].height));
2103 video_receive_configs_[selected_stream_id + num_streams_processed]
2104 .renderer = loopback_renderers.back().get();
eladalon413ee9a2017-08-22 04:02:52 -07002105 if (params_.audio.enabled && params_.audio.sync_video)
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002106 video_receive_configs_[num_streams_processed + selected_stream_id]
2107 .sync_group = kSyncGroup;
eladalon413ee9a2017-08-22 04:02:52 -07002108 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002109 num_streams_processed += num_streams;
eladalon413ee9a2017-08-22 04:02:52 -07002110 }
eladalon413ee9a2017-08-22 04:02:52 -07002111 CreateFlexfecStreams();
2112 CreateVideoStreams();
2113
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002114 CreateCapturers();
2115 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
2116 video_send_streams_[video_idx]->SetSource(
2117 video_capturers_[video_idx].get(), degradation_preference_);
2118 }
eladalon413ee9a2017-08-22 04:02:52 -07002119 }
2120
2121 if (params_.audio.enabled) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01002122 SetupAudio(send_transport.get(), &audio_receive_stream);
eladalon413ee9a2017-08-22 04:02:52 -07002123 }
2124
2125 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2126 StartEncodedFrameLogs(receive_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002127 StartEncodedFrameLogs(video_send_streams_[0]);
eladalon413ee9a2017-08-22 04:02:52 -07002128
2129 // Start sending and receiving video.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002130 if (params_.video[0].enabled) {
eladalon413ee9a2017-08-22 04:02:52 -07002131 for (VideoReceiveStream* video_receive_stream : video_receive_streams_)
2132 video_receive_stream->Start();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002133 for (VideoSendStream* video_send_stream : video_send_streams_)
2134 video_send_stream->Start();
2135 for (auto& video_capturer : video_capturers_)
2136 video_capturer->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002137 }
2138
2139 if (params_.audio.enabled) {
2140 // Start receiving audio.
2141 audio_receive_stream->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002142
2143 // Start sending audio.
2144 audio_send_stream_->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002145 }
2146 });
minyue73208662016-08-18 06:28:55 -07002147
ivica5d6a06c2015-09-17 05:30:24 -07002148 test::PressEnterToContinue();
2149
eladalon413ee9a2017-08-22 04:02:52 -07002150 task_queue_.SendTask([&]() {
2151 if (params_.audio.enabled) {
2152 // Stop sending audio.
eladalon413ee9a2017-08-22 04:02:52 -07002153 audio_send_stream_->Stop();
minyue73208662016-08-18 06:28:55 -07002154
eladalon413ee9a2017-08-22 04:02:52 -07002155 // Stop receiving audio.
eladalon413ee9a2017-08-22 04:02:52 -07002156 audio_receive_stream->Stop();
eladalon413ee9a2017-08-22 04:02:52 -07002157 }
minyue73208662016-08-18 06:28:55 -07002158
eladalon413ee9a2017-08-22 04:02:52 -07002159 // Stop receiving and sending video.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002160 if (params_.video[0].enabled) {
2161 for (auto& video_capturer : video_capturers_)
2162 video_capturer->Stop();
2163 for (VideoSendStream* video_send_stream : video_send_streams_)
2164 video_send_stream->Stop();
2165 DestroyStreams();
brandtr1293aca2016-11-16 22:47:29 -08002166 }
minyue73208662016-08-18 06:28:55 -07002167
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002168 video_capturers_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07002169 send_transport.reset();
2170 recv_transport.reset();
sprang1168fd42017-06-21 09:00:17 -07002171
eladalon413ee9a2017-08-22 04:02:52 -07002172 local_preview.reset();
2173 loopback_renderers.clear();
2174
2175 DestroyCalls();
2176 });
ivica5d6a06c2015-09-17 05:30:24 -07002177}
2178
palmkviste75f2042016-09-28 06:19:48 -07002179void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002180 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002181 std::ostringstream str;
2182 str << send_logs_++;
2183 std::string prefix =
ilnik98436952017-07-13 00:47:03 -07002184 params_.logging.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07002185 stream->EnableEncodedFrameRecording(
2186 std::vector<rtc::PlatformFile>(
2187 {rtc::CreatePlatformFile(prefix + "1.ivf"),
2188 rtc::CreatePlatformFile(prefix + "2.ivf"),
2189 rtc::CreatePlatformFile(prefix + "3.ivf")}),
ilnik98436952017-07-13 00:47:03 -07002190 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002191 }
2192}
ilnikcb8c1462017-03-09 09:23:30 -08002193
palmkviste75f2042016-09-28 06:19:48 -07002194void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002195 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002196 std::ostringstream str;
2197 str << receive_logs_++;
2198 std::string path =
ilnik98436952017-07-13 00:47:03 -07002199 params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07002200 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
ilnik98436952017-07-13 00:47:03 -07002201 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002202 }
2203}
ivica5d6a06c2015-09-17 05:30:24 -07002204} // namespace webrtc