blob: 3fa07db7fdbcc3f5a3ce60193b019daeb5229ddf [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 */
perkja49cbd32016-09-16 07:53:41 -070010#include "webrtc/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>
ilnikcb8c1462017-03-09 09:23:30 -080016#include <set>
sprangce4aef12015-11-02 07:23:20 -080017#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080018#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070019#include <vector>
20
ivica5d6a06c2015-09-17 05:30:24 -070021#include "webrtc/base/checks.h"
ilnikdf92c5c2017-02-23 02:08:44 -080022#include "webrtc/base/cpu_time.h"
Peter Boström5811a392015-12-10 13:02:50 +010023#include "webrtc/base/event.h"
ivica5d6a06c2015-09-17 05:30:24 -070024#include "webrtc/base/format_macros.h"
ilnikdaa574d2017-03-01 06:46:05 -080025#include "webrtc/base/memory_usage.h"
nissec7fe3c22016-04-20 03:25:36 -070026#include "webrtc/base/optional.h"
palmkviste75f2042016-09-28 06:19:48 -070027#include "webrtc/base/platform_file.h"
sprange1f2f1f2016-02-01 02:04:52 -080028#include "webrtc/base/timeutils.h"
ossuf515ab82016-12-07 04:52:58 -080029#include "webrtc/call/call.h"
ivica5d6a06c2015-09-17 05:30:24 -070030#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
skvlad11a9cbf2016-10-07 11:53:05 -070031#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
aleloi10111bc2016-11-17 06:48:48 -080032#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
ilnik1e7732c2017-02-23 05:07:56 -080034#include "webrtc/modules/rtp_rtcp/source/rtp_format.h"
sprangce4aef12015-11-02 07:23:20 -080035#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
magjed509e4fe2016-11-18 01:34:11 -080036#include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
37#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
ilnikcb8c1462017-03-09 09:23:30 -080038#include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h"
magjed509e4fe2016-11-18 01:34:11 -080039#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010040#include "webrtc/system_wrappers/include/cpu_info.h"
ilnik9ae0d762017-02-15 00:53:12 -080041#include "webrtc/system_wrappers/include/field_trial.h"
kwibergac9f8762016-09-30 22:29:43 -070042#include "webrtc/test/gtest.h"
ivica5d6a06c2015-09-17 05:30:24 -070043#include "webrtc/test/layer_filtering_transport.h"
44#include "webrtc/test/run_loop.h"
45#include "webrtc/test/statistics.h"
46#include "webrtc/test/testsupport/fileutils.h"
perkja49cbd32016-09-16 07:53:41 -070047#include "webrtc/test/vcm_capturer.h"
ivica5d6a06c2015-09-17 05:30:24 -070048#include "webrtc/test/video_renderer.h"
minyue73208662016-08-18 06:28:55 -070049#include "webrtc/voice_engine/include/voe_base.h"
minyue73208662016-08-18 06:28:55 -070050
51namespace {
52
53constexpr int kSendStatsPollingIntervalMs = 1000;
54constexpr int kPayloadTypeH264 = 122;
55constexpr int kPayloadTypeVP8 = 123;
56constexpr int kPayloadTypeVP9 = 124;
57constexpr size_t kMaxComparisons = 10;
58constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080059constexpr int kOpusMinBitrateBps = 6000;
60constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 00:53:12 -080061constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 04:21:04 -080062constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
63constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 06:28:55 -070064
65struct VoiceEngineState {
66 VoiceEngineState()
67 : voice_engine(nullptr),
68 base(nullptr),
minyue73208662016-08-18 06:28:55 -070069 send_channel_id(-1),
70 receive_channel_id(-1) {}
71
72 webrtc::VoiceEngine* voice_engine;
73 webrtc::VoEBase* base;
minyue73208662016-08-18 06:28:55 -070074 int send_channel_id;
75 int receive_channel_id;
76};
77
78void CreateVoiceEngine(VoiceEngineState* voe,
79 rtc::scoped_refptr<webrtc::AudioDecoderFactory>
80 decoder_factory) {
81 voe->voice_engine = webrtc::VoiceEngine::Create();
82 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
minyue73208662016-08-18 06:28:55 -070083 EXPECT_EQ(0, voe->base->Init(nullptr, nullptr, decoder_factory));
solenberg88499ec2016-09-07 07:34:41 -070084 webrtc::VoEBase::ChannelConfig config;
85 config.enable_voice_pacing = true;
86 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 06:28:55 -070087 EXPECT_GE(voe->send_channel_id, 0);
88 voe->receive_channel_id = voe->base->CreateChannel();
89 EXPECT_GE(voe->receive_channel_id, 0);
90}
91
92void DestroyVoiceEngine(VoiceEngineState* voe) {
93 voe->base->DeleteChannel(voe->send_channel_id);
94 voe->send_channel_id = -1;
95 voe->base->DeleteChannel(voe->receive_channel_id);
96 voe->receive_channel_id = -1;
97 voe->base->Release();
98 voe->base = nullptr;
minyue73208662016-08-18 06:28:55 -070099
100 webrtc::VoiceEngine::Delete(voe->voice_engine);
101 voe->voice_engine = nullptr;
102}
103
perkjfa10b552016-10-02 23:45:26 -0700104class VideoStreamFactory
105 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
106 public:
107 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
108 : streams_(streams) {}
109
110 private:
111 std::vector<webrtc::VideoStream> CreateEncoderStreams(
112 int width,
113 int height,
114 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -0800115 // The highest layer must match the incoming resolution.
116 std::vector<webrtc::VideoStream> streams = streams_;
117 streams[streams_.size() - 1].height = height;
118 streams[streams_.size() - 1].width = width;
119 return streams;
perkjfa10b552016-10-02 23:45:26 -0700120 }
121
122 std::vector<webrtc::VideoStream> streams_;
123};
124
brandtr504b95e2016-12-21 02:54:35 -0800125bool IsFlexfec(int payload_type) {
126 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
127}
128
minyue73208662016-08-18 06:28:55 -0700129} // namespace
ivica5d6a06c2015-09-17 05:30:24 -0700130
131namespace webrtc {
132
ivica5d6a06c2015-09-17 05:30:24 -0700133class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -0700134 public Transport,
ilnik1e7732c2017-02-23 05:07:56 -0800135 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 05:30:24 -0700136 public:
sprangce4aef12015-11-02 07:23:20 -0800137 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700138 const std::string& test_label,
139 double avg_psnr_threshold,
140 double avg_ssim_threshold,
141 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800142 FILE* graph_data_output_file,
143 const std::string& graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -0800144 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 09:16:05 -0800145 uint32_t rtx_ssrc_to_analyze,
ilnikcb8c1462017-03-09 09:23:30 -0800146 size_t selected_stream,
ilnik1e7732c2017-02-23 05:07:56 -0800147 int selected_sl,
148 int selected_tl,
ilnik9ae0d762017-02-15 00:53:12 -0800149 bool is_quick_test_enabled)
perkja49cbd32016-09-16 07:53:41 -0700150 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700151 receiver_(nullptr),
152 send_stream_(nullptr),
philipelfd870db2017-01-23 03:22:15 -0800153 receive_stream_(nullptr),
perkja49cbd32016-09-16 07:53:41 -0700154 captured_frame_forwarder_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700155 test_label_(test_label),
156 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800157 graph_title_(graph_title),
158 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 09:16:05 -0800159 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 09:23:30 -0800160 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 05:07:56 -0800161 selected_sl_(selected_sl),
162 selected_tl_(selected_tl),
pbos14fe7082016-04-20 06:35:56 -0700163 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100164 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700165 frames_to_process_(duration_frames),
166 frames_recorded_(0),
167 frames_processed_(0),
168 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700169 dropped_frames_before_first_encode_(0),
170 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700171 last_render_time_(0),
172 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 05:07:56 -0800173 total_media_bytes_(0),
174 first_sending_time_(0),
175 last_sending_time_(0),
ilnikdf92c5c2017-02-23 02:08:44 -0800176 cpu_time_(0),
177 wallclock_time_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700178 avg_psnr_threshold_(avg_psnr_threshold),
179 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 00:53:12 -0800180 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100181 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100182 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +0100183 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -0700184 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
185
186 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
187 // so that we don't accidentally starve "real" worker threads (codec etc).
188 // Also, don't allocate more than kMaxComparisonThreads, even if there are
189 // spare cores.
190
191 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800192 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700193 static const uint32_t kMinCoresLeft = 4;
194 static const uint32_t kMaxComparisonThreads = 8;
195
196 if (num_cores <= kMinCoresLeft) {
197 num_cores = 1;
198 } else {
199 num_cores -= kMinCoresLeft;
200 num_cores = std::min(num_cores, kMaxComparisonThreads);
201 }
202
203 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100204 rtc::PlatformThread* thread =
205 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
206 thread->Start();
207 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700208 }
ivica5d6a06c2015-09-17 05:30:24 -0700209 }
210
211 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100212 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
213 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700214 delete thread;
215 }
216 }
217
218 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
219
perkja49cbd32016-09-16 07:53:41 -0700220 void SetSendStream(VideoSendStream* stream) {
221 rtc::CritScope lock(&crit_);
222 RTC_DCHECK(!send_stream_);
223 send_stream_ = stream;
224 }
225
philipelfd870db2017-01-23 03:22:15 -0800226 void SetReceiveStream(VideoReceiveStream* stream) {
227 rtc::CritScope lock(&crit_);
228 RTC_DCHECK(!receive_stream_);
229 receive_stream_ = stream;
230 }
231
perkja49cbd32016-09-16 07:53:41 -0700232 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
233 return &captured_frame_forwarder_;
234 }
235 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
236 return &captured_frame_forwarder_;
237 }
238
ivica5d6a06c2015-09-17 05:30:24 -0700239 DeliveryStatus DeliverPacket(MediaType media_type,
240 const uint8_t* packet,
241 size_t length,
242 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700243 // Ignore timestamps of RTCP packets. They're not synchronized with
244 // RTP packet timestamps and so they would confuse wrap_handler_.
245 if (RtpHeaderParser::IsRtcp(packet, length)) {
246 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
247 }
sprangce4aef12015-11-02 07:23:20 -0800248 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700249 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800250 parser.Parse(&header);
ilnik46a00212017-02-10 09:16:05 -0800251 if (!IsFlexfec(header.payloadType) &&
252 (header.ssrc == ssrc_to_analyze_ ||
253 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 02:54:35 -0800254 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
255 // (FlexFEC and media are sent on different SSRCs, which have different
256 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800257 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 05:30:24 -0700258 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800259 int64_t timestamp =
260 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
261 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700262 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
263 }
264
265 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
266 }
267
Peter Boströme4499152016-02-05 11:13:28 +0100268 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700269 rtc::CritScope crit(&comparison_lock_);
270 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
271 }
272
pbos14fe7082016-04-20 06:35:56 -0700273 void PreEncodeOnFrame(const VideoFrame& video_frame) {
274 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800275 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 06:35:56 -0700276 while (frames_.front().timestamp() != video_frame.timestamp()) {
277 ++dropped_frames_before_first_encode_;
278 frames_.pop_front();
279 RTC_CHECK(!frames_.empty());
280 }
ilnik3dd5ad92017-02-09 04:58:53 -0800281 first_encoded_timestamp_ =
282 rtc::Optional<uint32_t>(video_frame.timestamp());
283 }
284 }
285
286 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
287 rtc::CritScope lock(&crit_);
288 if (!first_sent_timestamp_ &&
ilnikcb8c1462017-03-09 09:23:30 -0800289 encoded_frame.stream_id_ == selected_stream_) {
ilnik3dd5ad92017-02-09 04:58:53 -0800290 first_sent_timestamp_ = rtc::Optional<uint32_t>(encoded_frame.timestamp_);
pbos14fe7082016-04-20 06:35:56 -0700291 }
292 }
293
stefan1d8a5062015-10-02 03:39:33 -0700294 bool SendRtp(const uint8_t* packet,
295 size_t length,
296 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800297 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700298 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800299 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700300
sprangce4aef12015-11-02 07:23:20 -0800301 int64_t current_time =
302 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 02:23:28 -0800303
sprangce4aef12015-11-02 07:23:20 -0800304 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700305 {
306 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800307 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 08:32:53 -0800308 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 04:58:53 -0800309 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 04:08:56 -0800310 }
ilnik3dd5ad92017-02-09 04:58:53 -0800311
312 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 02:54:35 -0800313 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
314 // (FlexFEC and media are sent on different SSRCs, which have different
315 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800316 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 02:54:35 -0800317 int64_t timestamp =
318 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
319 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 05:07:56 -0800320
321 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 02:54:35 -0800322 encoded_frame_sizes_[timestamp] +=
323 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 05:07:56 -0800324 total_media_bytes_ +=
325 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 02:54:35 -0800326 }
ilnik1e7732c2017-02-23 05:07:56 -0800327 if (first_sending_time_ == 0)
328 first_sending_time_ = current_time;
329 last_sending_time_ = current_time;
sprangce4aef12015-11-02 07:23:20 -0800330 }
ivica5d6a06c2015-09-17 05:30:24 -0700331 }
sprangce4aef12015-11-02 07:23:20 -0800332 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700333 }
334
335 bool SendRtcp(const uint8_t* packet, size_t length) override {
336 return transport_->SendRtcp(packet, length);
337 }
338
nisseeb83a1a2016-03-21 01:27:56 -0700339 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700340 int64_t render_time_ms =
341 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700342
343 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 02:08:44 -0800344
345 StartExcludingCpuThreadTime();
346
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700347 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800348 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700349
sprang16daaa52016-03-09 01:30:24 -0800350 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700351 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700352 // No previous frame rendered, this one was dropped after sending but
353 // before rendering.
354 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 07:02:22 -0800355 } else {
356 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
357 render_time_ms);
pbos14fe7082016-04-20 06:35:56 -0700358 }
ivica5d6a06c2015-09-17 05:30:24 -0700359 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700360 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700361 }
362
363 VideoFrame reference_frame = frames_.front();
364 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800365 int64_t reference_timestamp =
366 wrap_handler_.Unwrap(reference_frame.timestamp());
367 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800368 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100369 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800370 ++send_timestamp;
371 }
sprang16daaa52016-03-09 01:30:24 -0800372 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700373
374 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
375
nisse97f0b932016-05-26 09:44:40 -0700376 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ilnikdf92c5c2017-02-23 02:08:44 -0800377
378 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 05:30:24 -0700379 }
380
ivica5d6a06c2015-09-17 05:30:24 -0700381 void Wait() {
382 // Frame comparisons can be very expensive. Wait for test to be done, but
383 // at time-out check if frames_processed is going up. If so, give it more
384 // time, otherwise fail. Hopefully this will reduce test flakiness.
385
Peter Boström8c38e8b2015-11-26 17:45:47 +0100386 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800387
ivica5d6a06c2015-09-17 05:30:24 -0700388 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700389 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100390 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700391 int frames_processed;
392 {
393 rtc::CritScope crit(&comparison_lock_);
394 frames_processed = frames_processed_;
395 }
396
397 // Print some output so test infrastructure won't think we've crashed.
398 const char* kKeepAliveMessages[3] = {
399 "Uh, I'm-I'm not quite dead, sir.",
400 "Uh, I-I think uh, I could pull through, sir.",
401 "Actually, I think I'm all right to come with you--"};
402 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
403
404 if (last_frames_processed == -1) {
405 last_frames_processed = frames_processed;
406 continue;
407 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100408 if (frames_processed == last_frames_processed) {
409 EXPECT_GT(frames_processed, last_frames_processed)
410 << "Analyzer stalled while waiting for test to finish.";
411 done_.Set();
412 break;
413 }
ivica5d6a06c2015-09-17 05:30:24 -0700414 last_frames_processed = frames_processed;
415 }
416
417 if (iteration > 0)
418 printf("- Farewell, sweet Concorde!\n");
419
Peter Boström8c38e8b2015-11-26 17:45:47 +0100420 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700421 }
422
pbos14fe7082016-04-20 06:35:56 -0700423 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
424 return &pre_encode_proxy_;
425 }
Peter Boströme4499152016-02-05 11:13:28 +0100426 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
427
ilnikdf92c5c2017-02-23 02:08:44 -0800428 void StartMeasuringCpuProcessTime() {
429 rtc::CritScope lock(&cpu_measurement_lock_);
430 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
431 wallclock_time_ -= rtc::SystemTimeNanos();
432 }
433
434 void StopMeasuringCpuProcessTime() {
435 rtc::CritScope lock(&cpu_measurement_lock_);
436 cpu_time_ += rtc::GetProcessCpuTimeNanos();
437 wallclock_time_ += rtc::SystemTimeNanos();
438 }
439
440 void StartExcludingCpuThreadTime() {
441 rtc::CritScope lock(&cpu_measurement_lock_);
442 cpu_time_ += rtc::GetThreadCpuTimeNanos();
443 }
444
445 void StopExcludingCpuThreadTime() {
446 rtc::CritScope lock(&cpu_measurement_lock_);
447 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
448 }
449
450 double GetCpuUsagePercent() {
451 rtc::CritScope lock(&cpu_measurement_lock_);
452 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
453 }
454
sprangce4aef12015-11-02 07:23:20 -0800455 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700456 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700457
458 private:
459 struct FrameComparison {
460 FrameComparison()
461 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800462 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700463 send_time_ms(0),
464 recv_time_ms(0),
465 render_time_ms(0),
466 encoded_frame_size(0) {}
467
468 FrameComparison(const VideoFrame& reference,
469 const VideoFrame& render,
470 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800471 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700472 int64_t send_time_ms,
473 int64_t recv_time_ms,
474 int64_t render_time_ms,
475 size_t encoded_frame_size)
476 : reference(reference),
477 render(render),
478 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800479 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700480 send_time_ms(send_time_ms),
481 recv_time_ms(recv_time_ms),
482 render_time_ms(render_time_ms),
483 encoded_frame_size(encoded_frame_size) {}
484
nissedf2ceb82016-12-15 06:29:53 -0800485 FrameComparison(bool dropped,
486 int64_t input_time_ms,
487 int64_t send_time_ms,
488 int64_t recv_time_ms,
489 int64_t render_time_ms,
490 size_t encoded_frame_size)
491 : dropped(dropped),
492 input_time_ms(input_time_ms),
493 send_time_ms(send_time_ms),
494 recv_time_ms(recv_time_ms),
495 render_time_ms(render_time_ms),
496 encoded_frame_size(encoded_frame_size) {}
497
498 rtc::Optional<VideoFrame> reference;
499 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700500 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800501 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700502 int64_t send_time_ms;
503 int64_t recv_time_ms;
504 int64_t render_time_ms;
505 size_t encoded_frame_size;
506 };
507
508 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700509 Sample(int dropped,
510 int64_t input_time_ms,
511 int64_t send_time_ms,
512 int64_t recv_time_ms,
513 int64_t render_time_ms,
514 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700515 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700516 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700517 : dropped(dropped),
518 input_time_ms(input_time_ms),
519 send_time_ms(send_time_ms),
520 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700521 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700522 encoded_frame_size(encoded_frame_size),
523 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700524 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700525
ivica8d15bd62015-10-07 02:43:12 -0700526 int dropped;
527 int64_t input_time_ms;
528 int64_t send_time_ms;
529 int64_t recv_time_ms;
530 int64_t render_time_ms;
531 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700532 double psnr;
533 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700534 };
535
Peter Boströme4499152016-02-05 11:13:28 +0100536 // This class receives the send-side OnEncodeTiming and is provided to not
537 // conflict with the receiver-side pre_decode_callback.
538 class OnEncodeTimingProxy : public EncodedFrameObserver {
539 public:
540 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
541
542 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
543 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
544 }
ilnik3dd5ad92017-02-09 04:58:53 -0800545 void EncodedFrameCallback(const EncodedFrame& frame) override {
546 parent_->PostEncodeFrameCallback(frame);
547 }
Peter Boströme4499152016-02-05 11:13:28 +0100548
549 private:
550 VideoAnalyzer* const parent_;
551 };
552
pbos14fe7082016-04-20 06:35:56 -0700553 // This class receives the send-side OnFrame callback and is provided to not
554 // conflict with the receiver-side renderer callback.
555 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
556 public:
557 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
558
559 void OnFrame(const VideoFrame& video_frame) override {
560 parent_->PreEncodeOnFrame(video_frame);
561 }
562
563 private:
564 VideoAnalyzer* const parent_;
565 };
566
ilnik1e7732c2017-02-23 05:07:56 -0800567 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
568 size_t length,
569 const RTPHeader& header) {
570 if (header.payloadType != kPayloadTypeVP9 &&
571 header.payloadType != kPayloadTypeVP8) {
572 return true;
573 } else {
574 // Get VP8 and VP9 specific header to check layers indexes.
575 const uint8_t* payload = packet + header.headerLength;
576 const size_t payload_length = length - header.headerLength;
577 const size_t payload_data_length = payload_length - header.paddingLength;
578 const bool is_vp8 = header.payloadType == kPayloadTypeVP8;
579 std::unique_ptr<RtpDepacketizer> depacketizer(
580 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
581 RtpDepacketizer::ParsedPayload parsed_payload;
582 bool result =
583 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
584 RTC_DCHECK(result);
585 const int temporal_idx = static_cast<int>(
586 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
587 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
588 const int spatial_idx = static_cast<int>(
589 is_vp8 ? kNoSpatialIdx
590 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
591 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
592 temporal_idx <= selected_tl_) &&
593 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
594 spatial_idx <= selected_sl_);
595 }
596 }
597
ivica5d6a06c2015-09-17 05:30:24 -0700598 void AddFrameComparison(const VideoFrame& reference,
599 const VideoFrame& render,
600 bool dropped,
601 int64_t render_time_ms)
602 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800603 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
604 int64_t send_time_ms = send_times_[reference_timestamp];
605 send_times_.erase(reference_timestamp);
606 int64_t recv_time_ms = recv_times_[reference_timestamp];
607 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700608
sprangce4aef12015-11-02 07:23:20 -0800609 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800610 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800611 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800612 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800613 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
614 if (it != encoded_frame_sizes_.end())
615 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700616
ivica5d6a06c2015-09-17 05:30:24 -0700617 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700618 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800619 comparisons_.push_back(FrameComparison(reference, render, dropped,
620 reference.ntp_time_ms(),
621 send_time_ms, recv_time_ms,
622 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700623 } else {
nissedf2ceb82016-12-15 06:29:53 -0800624 comparisons_.push_back(FrameComparison(dropped,
625 reference.ntp_time_ms(),
626 send_time_ms, recv_time_ms,
627 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700628 }
Peter Boström5811a392015-12-10 13:02:50 +0100629 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700630 }
631
tommi0f8b4032017-02-22 11:22:05 -0800632 static void PollStatsThread(void* obj) {
633 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 05:30:24 -0700634 }
635
tommi0f8b4032017-02-22 11:22:05 -0800636 void PollStats() {
637 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
638 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700639
tommi0f8b4032017-02-22 11:22:05 -0800640 VideoSendStream::Stats send_stats = send_stream_->GetStats();
641 // It's not certain that we yet have estimates for any of these stats.
642 // Check that they are positive before mixing them in.
643 if (send_stats.encode_frame_rate > 0)
644 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
645 if (send_stats.avg_encode_time_ms > 0)
646 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
647 if (send_stats.encode_usage_percent > 0)
648 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
649 if (send_stats.media_bitrate_bps > 0)
650 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
philipelfd870db2017-01-23 03:22:15 -0800651
tommi0f8b4032017-02-22 11:22:05 -0800652 if (receive_stream_ != nullptr) {
653 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
654 if (receive_stats.decode_ms > 0)
655 decode_time_ms_.AddSample(receive_stats.decode_ms);
656 if (receive_stats.max_decode_ms > 0)
657 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
658 }
ilnikdaa574d2017-03-01 06:46:05 -0800659
660 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 03:22:15 -0800661 }
ivica5d6a06c2015-09-17 05:30:24 -0700662 }
663
664 static bool FrameComparisonThread(void* obj) {
665 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
666 }
667
668 bool CompareFrames() {
669 if (AllFramesRecorded())
670 return false;
671
ivica5d6a06c2015-09-17 05:30:24 -0700672 FrameComparison comparison;
673
674 if (!PopComparison(&comparison)) {
675 // Wait until new comparison task is available, or test is done.
676 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100677 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700678 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100679 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700680 return false;
681 }
682 return true; // Try again.
683 }
684
ilnikdf92c5c2017-02-23 02:08:44 -0800685 StartExcludingCpuThreadTime();
686
ivica5d6a06c2015-09-17 05:30:24 -0700687 PerformFrameComparison(comparison);
688
ilnikdf92c5c2017-02-23 02:08:44 -0800689 StopExcludingCpuThreadTime();
690
ivica5d6a06c2015-09-17 05:30:24 -0700691 if (FrameProcessed()) {
692 PrintResults();
693 if (graph_data_output_file_)
694 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100695 done_.Set();
696 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700697 return false;
698 }
699
700 return true;
701 }
702
703 bool PopComparison(FrameComparison* comparison) {
704 rtc::CritScope crit(&comparison_lock_);
705 // If AllFramesRecorded() is true, it means we have already popped
706 // frames_to_process_ frames from comparisons_, so there is no more work
707 // for this thread to be done. frames_processed_ might still be lower if
708 // all comparisons are not done, but those frames are currently being
709 // worked on by other threads.
710 if (comparisons_.empty() || AllFramesRecorded())
711 return false;
712
713 *comparison = comparisons_.front();
714 comparisons_.pop_front();
715
716 FrameRecorded();
717 return true;
718 }
719
720 // Increment counter for number of frames received for comparison.
721 void FrameRecorded() {
722 rtc::CritScope crit(&comparison_lock_);
723 ++frames_recorded_;
724 }
725
726 // Returns true if all frames to be compared have been taken from the queue.
727 bool AllFramesRecorded() {
728 rtc::CritScope crit(&comparison_lock_);
729 assert(frames_recorded_ <= frames_to_process_);
730 return frames_recorded_ == frames_to_process_;
731 }
732
733 // Increase count of number of frames processed. Returns true if this was the
734 // last frame to be processed.
735 bool FrameProcessed() {
736 rtc::CritScope crit(&comparison_lock_);
737 ++frames_processed_;
738 assert(frames_processed_ <= frames_to_process_);
739 return frames_processed_ == frames_to_process_;
740 }
741
742 void PrintResults() {
ilnikdf92c5c2017-02-23 02:08:44 -0800743 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -0700744 rtc::CritScope crit(&comparison_lock_);
745 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800746 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 01:02:15 -0700747 PrintResult("sender_time", sender_time_, " ms");
748 PrintResult("receiver_time", receiver_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700749 PrintResult("total_delay_incl_network", end_to_end_, " ms");
750 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700751 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800752 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800753 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
754
755 if (receive_stream_ != nullptr) {
756 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800757 }
ivica5d6a06c2015-09-17 05:30:24 -0700758
pbos14fe7082016-04-20 06:35:56 -0700759 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
760 dropped_frames_);
ilnikdf92c5c2017-02-23 02:08:44 -0800761 printf("RESULT cpu_usage: %s = %lf %%\n", test_label_.c_str(),
762 GetCpuUsagePercent());
ilnikdaa574d2017-03-01 06:46:05 -0800763
764#if defined(WEBRTC_WIN)
765 // On Linux and Mac in Resident Set some unused pages may be counted.
766 // Therefore this metric will depend on order in which tests are run and
767 // will be flaky.
768 PrintResult("memory_usage", memory_usage_, " bytes");
769#endif
770
ilnik9ae0d762017-02-15 00:53:12 -0800771 // Disable quality check for quick test, as quality checks may fail
772 // because too few samples were collected.
773 if (!is_quick_test_enabled_) {
774 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
775 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
776 }
ivica5d6a06c2015-09-17 05:30:24 -0700777 }
778
779 void PerformFrameComparison(const FrameComparison& comparison) {
780 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700781 double psnr = -1.0;
782 double ssim = -1.0;
nissedf2ceb82016-12-15 06:29:53 -0800783 if (comparison.reference) {
784 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
785 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700786 }
ivica5d6a06c2015-09-17 05:30:24 -0700787
ivica5d6a06c2015-09-17 05:30:24 -0700788 rtc::CritScope crit(&comparison_lock_);
789 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800790 samples_.push_back(Sample(
791 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
792 comparison.recv_time_ms, comparison.render_time_ms,
793 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700794 }
stefanb1797672016-08-11 07:00:57 -0700795 if (psnr >= 0.0)
796 psnr_.AddSample(psnr);
797 if (ssim >= 0.0)
798 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700799
800 if (comparison.dropped) {
801 ++dropped_frames_;
802 return;
803 }
804 if (last_render_time_ != 0)
805 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
806 last_render_time_ = comparison.render_time_ms;
807
nissedf2ceb82016-12-15 06:29:53 -0800808 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800809 if (comparison.recv_time_ms > 0) {
810 // If recv_time_ms == 0, this frame consisted of a packets which were all
811 // lost in the transport. Since we were able to render the frame, however,
812 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
813 // happens internally in Call, and we can therefore here not know which
814 // FEC packets that protected the lost media packets. Consequently, we
815 // were not able to record a meaningful recv_time_ms. We therefore skip
816 // this sample.
817 //
818 // The reasoning above does not hold for ULPFEC and RTX, as for those
819 // strategies the timestamp of the received packets is set to the
820 // timestamp of the protected/retransmitted media packet. I.e., then
821 // recv_time_ms != 0, even though the media packets were lost.
822 receiver_time_.AddSample(comparison.render_time_ms -
823 comparison.recv_time_ms);
824 }
nissedf2ceb82016-12-15 06:29:53 -0800825 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700826 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
827 }
828
829 void PrintResult(const char* result_type,
830 test::Statistics stats,
831 const char* unit) {
832 printf("RESULT %s: %s = {%f, %f}%s\n",
833 result_type,
834 test_label_.c_str(),
835 stats.Mean(),
836 stats.StandardDeviation(),
837 unit);
838 }
839
840 void PrintSamplesToFile(void) {
841 FILE* out = graph_data_output_file_;
842 rtc::CritScope crit(&comparison_lock_);
843 std::sort(samples_.begin(), samples_.end(),
844 [](const Sample& A, const Sample& B) -> bool {
845 return A.input_time_ms < B.input_time_ms;
846 });
847
sprangce4aef12015-11-02 07:23:20 -0800848 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700849 fprintf(out, "%" PRIuS "\n", samples_.size());
850 fprintf(out,
851 "dropped "
852 "input_time_ms "
853 "send_time_ms "
854 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700855 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700856 "encoded_frame_size "
857 "psnr "
858 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700859 "encode_time_ms\n");
860 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700861 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700862 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
863 int encode_time_ms;
864 if (it != samples_encode_time_ms_.end()) {
865 encode_time_ms = it->second;
866 } else {
867 ++missing_encode_time_samples;
868 encode_time_ms = -1;
869 }
870 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
871 " %lf %lf %d\n",
872 sample.dropped, sample.input_time_ms, sample.send_time_ms,
873 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700874 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700875 encode_time_ms);
876 }
877 if (missing_encode_time_samples) {
878 fprintf(stderr,
879 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
880 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700881 }
882 }
883
ilnik1e7732c2017-02-23 05:07:56 -0800884 double GetAverageMediaBitrateBps() {
885 if (last_sending_time_ == first_sending_time_) {
886 return 0;
887 } else {
888 return static_cast<double>(total_media_bytes_) * 8 /
889 (last_sending_time_ - first_sending_time_) *
890 rtc::kNumMillisecsPerSec;
891 }
892 }
893
perkja49cbd32016-09-16 07:53:41 -0700894 // Implements VideoSinkInterface to receive captured frames from a
895 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
896 // as a source to VideoSendStream.
897 // It forwards all input frames to the VideoAnalyzer for later comparison and
898 // forwards the captured frames to the VideoSendStream.
899 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
900 public rtc::VideoSourceInterface<VideoFrame> {
901 public:
902 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer)
903 : analyzer_(analyzer), send_stream_input_(nullptr) {}
904
905 private:
906 void OnFrame(const VideoFrame& video_frame) override {
907 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -0800908 // Frames from the capturer does not have a rtp timestamp.
909 // Create one so it can be used for comparison.
910 RTC_DCHECK_EQ(0, video_frame.timestamp());
perkja49cbd32016-09-16 07:53:41 -0700911 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -0800912 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -0700913 rtc::CritScope lock(&crit_);
914 if (send_stream_input_)
915 send_stream_input_->OnFrame(video_frame);
916 }
917
918 // Called when |send_stream_.SetSource()| is called.
919 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
920 const rtc::VideoSinkWants& wants) override {
921 rtc::CritScope lock(&crit_);
922 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
923 send_stream_input_ = sink;
924 }
925
926 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
927 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
928 rtc::CritScope lock(&crit_);
929 RTC_DCHECK(sink == send_stream_input_);
930 send_stream_input_ = nullptr;
931 }
932
933 VideoAnalyzer* const analyzer_;
934 rtc::CriticalSection crit_;
935 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
936 };
937
938 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
939 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800940 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -0700941 }
942
943 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -0800944 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -0700945 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700946 const std::string test_label_;
947 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800948 const std::string graph_title_;
949 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -0800950 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 09:23:30 -0800951 const size_t selected_stream_;
ilnik1e7732c2017-02-23 05:07:56 -0800952 const int selected_sl_;
953 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -0700954 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100955 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700956 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700957 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700958 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
959 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
960 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
961 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
962 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
963 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
964 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
965 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
philipelfd870db2017-01-23 03:22:15 -0800966 test::Statistics encode_time_ms_ GUARDED_BY(comparison_lock_);
967 test::Statistics encode_usage_percent_ GUARDED_BY(comparison_lock_);
968 test::Statistics decode_time_ms_ GUARDED_BY(comparison_lock_);
969 test::Statistics decode_time_max_ms_ GUARDED_BY(comparison_lock_);
970 test::Statistics media_bitrate_bps_ GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 06:46:05 -0800971 test::Statistics memory_usage_ GUARDED_BY(comparison_lock_);
972
ivica5d6a06c2015-09-17 05:30:24 -0700973
974 const int frames_to_process_;
975 int frames_recorded_;
976 int frames_processed_;
977 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -0700978 int dropped_frames_before_first_encode_;
979 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -0700980 int64_t last_render_time_;
981 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -0800982 int64_t total_media_bytes_;
983 int64_t first_sending_time_;
984 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -0700985
ilnikdf92c5c2017-02-23 02:08:44 -0800986 int64_t cpu_time_ GUARDED_BY(cpu_measurement_lock_);
987 int64_t wallclock_time_ GUARDED_BY(cpu_measurement_lock_);
988 rtc::CriticalSection cpu_measurement_lock_;
989
ivica5d6a06c2015-09-17 05:30:24 -0700990 rtc::CriticalSection crit_;
991 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -0700992 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800993 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
994 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
995 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
996 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800997 rtc::Optional<uint32_t> first_encoded_timestamp_ GUARDED_BY(crit_);
998 rtc::Optional<uint32_t> first_sent_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700999 const double avg_psnr_threshold_;
1000 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -08001001 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -07001002
1003 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001004 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1005 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001006 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -07001007 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001008 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -07001009};
1010
ilnikcb8c1462017-03-09 09:23:30 -08001011class Vp8EncoderFactory : public VideoEncoderFactory {
1012 public:
1013 Vp8EncoderFactory() = default;
1014 ~Vp8EncoderFactory() override { RTC_CHECK(live_encoders_.empty()); }
1015
1016 VideoEncoder* Create() override {
1017 VideoEncoder* encoder = VP8Encoder::Create();
1018 live_encoders_.insert(encoder);
1019 return encoder;
1020 }
1021
1022 void Destroy(VideoEncoder* encoder) override {
1023 auto it = live_encoders_.find(encoder);
1024 RTC_CHECK(it != live_encoders_.end());
1025 live_encoders_.erase(it);
1026 delete encoder;
1027 }
1028
1029 std::set<VideoEncoder*> live_encoders_;
1030};
1031
palmkviste75f2042016-09-28 06:19:48 -07001032VideoQualityTest::VideoQualityTest()
1033 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {}
ivica5d6a06c2015-09-17 05:30:24 -07001034
minyue626bc952016-10-31 05:47:02 -07001035VideoQualityTest::Params::Params()
1036 : call({false, Call::Config::BitrateConfig()}),
1037 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
brandtr1293aca2016-11-16 22:47:29 -08001038 false, "", ""}),
minyue4c8b9422017-03-21 04:11:43 -07001039 audio({false, false, false}),
minyue626bc952016-10-31 05:47:02 -07001040 screenshare({false, 10, 0}),
1041 analyzer({"", 0.0, 0.0, 0, "", ""}),
1042 pipe(),
1043 logs(false),
ilnika014cc52017-03-07 04:21:04 -08001044 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}),
1045 num_thumbnails(0) {}
minyue626bc952016-10-31 05:47:02 -07001046
1047VideoQualityTest::Params::~Params() = default;
1048
ivica5d6a06c2015-09-17 05:30:24 -07001049void VideoQualityTest::TestBody() {}
1050
sprangce4aef12015-11-02 07:23:20 -08001051std::string VideoQualityTest::GenerateGraphTitle() const {
1052 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -07001053 ss << params_.video.codec;
1054 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
1055 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -08001056 if (params_.screenshare.scroll_duration)
1057 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
1058 if (params_.ss.streams.size() > 1)
1059 ss << ", Stream #" << params_.ss.selected_stream;
1060 if (params_.ss.num_spatial_layers > 1)
1061 ss << ", Layer #" << params_.ss.selected_sl;
1062 ss << ")";
1063 return ss.str();
1064}
1065
1066void VideoQualityTest::CheckParams() {
stefan7de8d642017-02-07 07:14:08 -08001067 if (!params_.video.enabled)
1068 return;
sprangce4aef12015-11-02 07:23:20 -08001069 // Add a default stream in none specified.
1070 if (params_.ss.streams.empty())
1071 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
1072 if (params_.ss.num_spatial_layers == 0)
1073 params_.ss.num_spatial_layers = 1;
1074
1075 if (params_.pipe.loss_percent != 0 ||
1076 params_.pipe.queue_length_packets != 0) {
1077 // Since LayerFilteringTransport changes the sequence numbers, we can't
1078 // use that feature with pack loss, since the NACK request would end up
1079 // retransmitting the wrong packets.
1080 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -08001081 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -07001082 RTC_CHECK(params_.video.selected_tl == -1 ||
1083 params_.video.selected_tl ==
1084 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001085 }
1086
1087 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
1088 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -07001089 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
1090 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
1091 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprangce4aef12015-11-02 07:23:20 -08001092 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
1093 for (const VideoStream& stream : params_.ss.streams) {
1094 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1095 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1096 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
ilnikcb8c1462017-03-09 09:23:30 -08001097 RTC_CHECK_LE(stream.temporal_layer_thresholds_bps.size(),
minyue626bc952016-10-31 05:47:02 -07001098 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001099 }
1100 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1101 // the total bitrate? We anyway have to update them in the case bitrate
1102 // estimator changes the total bitrates.
1103 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
1104 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
1105 RTC_CHECK(params_.ss.spatial_layers.empty() ||
1106 params_.ss.spatial_layers.size() ==
1107 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -07001108 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -08001109 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -07001110 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -08001111 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -08001112 }
ilnika014cc52017-03-07 04:21:04 -08001113 RTC_CHECK_GE(params_.num_thumbnails, 0);
1114 if (params_.num_thumbnails > 0) {
1115 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
1116 RTC_CHECK_EQ(params_.ss.streams.size(), 3);
1117 RTC_CHECK_EQ(params_.video.num_temporal_layers, 3);
1118 RTC_CHECK_EQ(params_.video.codec, "VP8");
1119 }
sprangce4aef12015-11-02 07:23:20 -08001120}
1121
1122// Static.
1123std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1124 // Parse comma separated nonnegative integers, where some elements may be
1125 // empty. The empty values are replaced with -1.
1126 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1127 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1128 std::vector<int> result;
1129 if (str.empty())
1130 return result;
1131
1132 const char* p = str.c_str();
1133 int value = -1;
1134 int pos;
1135 while (*p) {
1136 if (*p == ',') {
1137 result.push_back(value);
1138 value = -1;
1139 ++p;
1140 continue;
1141 }
1142 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1143 << "Unexpected non-number value.";
1144 p += pos;
1145 }
1146 result.push_back(value);
1147 return result;
1148}
1149
1150// Static.
1151VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
1152 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -07001153 stream.width = params.video.width;
1154 stream.height = params.video.height;
1155 stream.max_framerate = params.video.fps;
1156 stream.min_bitrate_bps = params.video.min_bitrate_bps;
1157 stream.target_bitrate_bps = params.video.target_bitrate_bps;
1158 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprangce4aef12015-11-02 07:23:20 -08001159 stream.max_qp = 52;
sprang6ef1b342017-03-13 02:01:32 -07001160 // TODO(sprang): Can we make this less of a hack?
1161 if (params.video.num_temporal_layers == 2) {
1162 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
1163 } else if (params.video.num_temporal_layers == 3) {
1164 stream.temporal_layer_thresholds_bps.push_back(stream.max_bitrate_bps / 4);
1165 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
ilnika014cc52017-03-07 04:21:04 -08001166 }
1167 return stream;
1168}
1169
1170// Static.
1171VideoStream VideoQualityTest::DefaultThumbnailStream() {
1172 VideoStream stream;
1173 stream.width = 320;
1174 stream.height = 180;
1175 stream.max_framerate = 7;
1176 stream.min_bitrate_bps = 7500;
1177 stream.target_bitrate_bps = 37500;
1178 stream.max_bitrate_bps = 50000;
1179 stream.max_qp = 52;
sprangce4aef12015-11-02 07:23:20 -08001180 return stream;
1181}
1182
1183// Static.
1184void VideoQualityTest::FillScalabilitySettings(
1185 Params* params,
1186 const std::vector<std::string>& stream_descriptors,
1187 size_t selected_stream,
1188 int num_spatial_layers,
1189 int selected_sl,
1190 const std::vector<std::string>& sl_descriptors) {
1191 // Read VideoStream and SpatialLayer elements from a list of comma separated
1192 // lists. To use a default value for an element, use -1 or leave empty.
1193 // Validity checks performed in CheckParams.
1194
1195 RTC_CHECK(params->ss.streams.empty());
1196 for (auto descriptor : stream_descriptors) {
1197 if (descriptor.empty())
1198 continue;
1199 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
1200 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1201 if (v[0] != -1)
1202 stream.width = static_cast<size_t>(v[0]);
1203 if (v[1] != -1)
1204 stream.height = static_cast<size_t>(v[1]);
1205 if (v[2] != -1)
1206 stream.max_framerate = v[2];
1207 if (v[3] != -1)
1208 stream.min_bitrate_bps = v[3];
1209 if (v[4] != -1)
1210 stream.target_bitrate_bps = v[4];
1211 if (v[5] != -1)
1212 stream.max_bitrate_bps = v[5];
1213 if (v.size() > 6 && v[6] != -1)
1214 stream.max_qp = v[6];
1215 if (v.size() > 7) {
1216 stream.temporal_layer_thresholds_bps.clear();
1217 stream.temporal_layer_thresholds_bps.insert(
1218 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1219 } else {
1220 // Automatic TL thresholds for more than two layers not supported.
minyue626bc952016-10-31 05:47:02 -07001221 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
sprangce4aef12015-11-02 07:23:20 -08001222 }
1223 params->ss.streams.push_back(stream);
1224 }
1225 params->ss.selected_stream = selected_stream;
1226
1227 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
1228 params->ss.selected_sl = selected_sl;
1229 RTC_CHECK(params->ss.spatial_layers.empty());
1230 for (auto descriptor : sl_descriptors) {
1231 if (descriptor.empty())
1232 continue;
1233 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1234 RTC_CHECK_GT(v[2], 0);
1235
1236 SpatialLayer layer;
1237 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1238 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1239 layer.target_bitrate_bps = v[2];
1240 params->ss.spatial_layers.push_back(layer);
1241 }
1242}
1243
minyuea27172d2016-11-01 05:59:29 -07001244void VideoQualityTest::SetupVideo(Transport* send_transport,
1245 Transport* recv_transport) {
sprangce4aef12015-11-02 07:23:20 -08001246 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -07001247 trace_to_stderr_.reset(new test::TraceToStderr);
1248
brandtr8313a6f2017-01-13 07:41:19 -08001249 size_t num_video_streams = params_.ss.streams.size();
1250 size_t num_flexfec_streams = params_.video.flexfec ? 1 : 0;
1251 CreateSendConfig(num_video_streams, 0, num_flexfec_streams, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001252
1253 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001254 if (params_.video.codec == "H264") {
magjedceecea42016-11-28 07:20:21 -08001255 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001256 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001257 } else if (params_.video.codec == "VP8") {
ilnikcb8c1462017-03-09 09:23:30 -08001258 if (params_.screenshare.enabled && params_.ss.streams.size() > 1) {
1259 // Simulcast screenshare needs a simulcast encoder adapter to work, since
1260 // encoders usually can't natively do simulcast with different frame rates
1261 // for the different layers.
1262 video_encoder_.reset(
1263 new SimulcastEncoderAdapter(new Vp8EncoderFactory()));
1264 } else {
1265 video_encoder_.reset(VP8Encoder::Create());
1266 }
ivica5d6a06c2015-09-17 05:30:24 -07001267 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001268 } else if (params_.video.codec == "VP9") {
magjed509e4fe2016-11-18 01:34:11 -08001269 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001270 payload_type = kPayloadTypeVP9;
1271 } else {
1272 RTC_NOTREACHED() << "Codec not supported!";
1273 return;
1274 }
minyuea27172d2016-11-01 05:59:29 -07001275 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001276 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001277 video_send_config_.encoder_settings.payload_type = payload_type;
1278 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1279 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
brandtr8313a6f2017-01-13 07:41:19 -08001280 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001281 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001282
stefanff483612015-12-21 03:14:00 -08001283 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001284 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001285 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001286 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001287 test::kTransportSequenceNumberExtensionId));
1288 } else {
stefanff483612015-12-21 03:14:00 -08001289 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001290 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik SprĂ¥ng6b8d3552015-09-24 15:06:57 +02001291 }
1292
stefanff483612015-12-21 03:14:00 -08001293 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001294 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001295
brandtr1293aca2016-11-16 22:47:29 -08001296 video_send_config_.suspend_below_min_bitrate =
1297 params_.video.suspend_below_min_bitrate;
1298
perkjfa10b552016-10-02 23:45:26 -07001299 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1300 video_encoder_config_.max_bitrate_bps = 0;
1301 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1302 video_encoder_config_.max_bitrate_bps +=
1303 params_.ss.streams[i].max_bitrate_bps;
1304 }
1305 video_encoder_config_.video_stream_factory =
1306 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1307
stefanff483612015-12-21 03:14:00 -08001308 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001309
1310 CreateMatchingReceiveConfigs(recv_transport);
1311
brandtr8313a6f2017-01-13 07:41:19 -08001312 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001313 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
brandtr14742122017-01-27 04:53:07 -08001314 video_receive_configs_[i].rtp.rtx_ssrc = kSendRtxSsrcs[i];
1315 video_receive_configs_[i].rtp.rtx_payload_types[payload_type] =
sprangce4aef12015-11-02 07:23:20 -08001316 kSendRtxPayloadType;
minyue626bc952016-10-31 05:47:02 -07001317 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
Stefan Holmer85d5ac72017-02-09 16:25:16 +01001318 video_receive_configs_[i].rtp.remb = !params_.call.send_side_bwe;
ilnik9fd9f6c2017-03-02 08:10:10 -08001319 // Force fake decoders on non-selected simulcast streams.
1320 if (i != params_.ss.selected_stream) {
1321 VideoReceiveStream::Decoder decoder;
1322 decoder.decoder = new test::FakeDecoder();
1323 decoder.payload_type = video_send_config_.encoder_settings.payload_type;
1324 decoder.payload_name = video_send_config_.encoder_settings.payload_name;
1325 video_receive_configs_[i].decoders.clear();
1326 allocated_decoders_.emplace_back(decoder.decoder);
1327 video_receive_configs_[i].decoders.push_back(decoder);
1328 }
sprangce4aef12015-11-02 07:23:20 -08001329 }
brandtr1293aca2016-11-16 22:47:29 -08001330
1331 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 07:41:19 -08001332 // Override send config constructed by CreateSendConfig.
brandtr1293aca2016-11-16 22:47:29 -08001333 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1334 kVideoSendSsrcs[params_.ss.selected_stream]};
1335
brandtr8313a6f2017-01-13 07:41:19 -08001336 // The matching receive config is _not_ created by
1337 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1338 // Set up the receive config manually instead.
1339 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001340 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 06:59:19 -08001341 video_send_config_.rtp.flexfec.payload_type;
1342 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001343 flexfec_receive_config.protected_media_ssrcs =
1344 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001345 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001346 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1347 if (params_.call.send_side_bwe) {
1348 flexfec_receive_config.rtp_header_extensions.push_back(
1349 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1350 test::kTransportSequenceNumberExtensionId));
1351 } else {
1352 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1353 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1354 }
brandtr1293aca2016-11-16 22:47:29 -08001355 flexfec_receive_configs_.push_back(flexfec_receive_config);
1356 }
1357
1358 if (params_.video.ulpfec) {
1359 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1360 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1361 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1362
1363 video_receive_configs_[params_.ss.selected_stream]
1364 .rtp.ulpfec.red_payload_type =
1365 video_send_config_.rtp.ulpfec.red_payload_type;
1366 video_receive_configs_[params_.ss.selected_stream]
1367 .rtp.ulpfec.ulpfec_payload_type =
1368 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1369 video_receive_configs_[params_.ss.selected_stream]
1370 .rtp.ulpfec.red_rtx_payload_type =
1371 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1372 }
ivica5d6a06c2015-09-17 05:30:24 -07001373}
1374
ilnika014cc52017-03-07 04:21:04 -08001375void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1376 Transport* recv_transport) {
1377 for (int i = 0; i < params_.num_thumbnails; ++i) {
1378 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1379
1380 // Thumbnails will be send in the other way: from receiver_call to
1381 // sender_call.
1382 VideoSendStream::Config thumbnail_send_config(recv_transport);
1383 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1384 thumbnail_send_config.encoder_settings.encoder =
1385 thumbnail_encoders_.back().get();
1386 thumbnail_send_config.encoder_settings.payload_name = params_.video.codec;
1387 thumbnail_send_config.encoder_settings.payload_type = kPayloadTypeVP8;
1388 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1389 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1390 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1391 thumbnail_send_config.rtp.extensions.clear();
1392 if (params_.call.send_side_bwe) {
1393 thumbnail_send_config.rtp.extensions.push_back(
1394 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1395 test::kTransportSequenceNumberExtensionId));
1396 } else {
1397 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1398 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1399 }
1400
1401 VideoEncoderConfig thumbnail_encoder_config;
1402 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1403 thumbnail_send_config.suspend_below_min_bitrate =
1404 params_.video.suspend_below_min_bitrate;
1405 thumbnail_encoder_config.number_of_streams = 1;
1406 thumbnail_encoder_config.max_bitrate_bps = 50000;
1407 thumbnail_encoder_config.video_stream_factory =
1408 new rtc::RefCountedObject<VideoStreamFactory>(
1409 std::vector<webrtc::VideoStream>{DefaultThumbnailStream()});
1410 thumbnail_encoder_config.spatial_layers = params_.ss.spatial_layers;
1411
1412 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1413 thumbnail_receive_config.rtp.remb = false;
1414 thumbnail_receive_config.rtp.transport_cc = true;
1415 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1416 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1417 thumbnail_receive_config.rtp.extensions.push_back(extension);
1418 thumbnail_receive_config.renderer = &fake_renderer_;
1419
1420 VideoReceiveStream::Decoder decoder =
1421 test::CreateMatchingDecoder(thumbnail_send_config.encoder_settings);
1422 allocated_decoders_.push_back(
1423 std::unique_ptr<VideoDecoder>(decoder.decoder));
1424 thumbnail_receive_config.decoders.clear();
1425 thumbnail_receive_config.decoders.push_back(decoder);
1426 thumbnail_receive_config.rtp.remote_ssrc =
1427 thumbnail_send_config.rtp.ssrcs[0];
1428
1429 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1430 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
1431 thumbnail_receive_config.rtp.rtx_payload_types[kPayloadTypeVP8] =
1432 kSendRtxPayloadType;
1433 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1434 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1435
1436 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1437 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1438 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1439 }
1440
1441 for (int i = 0; i < params_.num_thumbnails; ++i) {
1442 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1443 thumbnail_send_configs_[i].Copy(),
1444 thumbnail_encoder_configs_[i].Copy()));
1445 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1446 thumbnail_receive_configs_[i].Copy()));
1447 }
1448}
1449
1450void VideoQualityTest::DestroyThumbnailStreams() {
1451 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1452 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
1453 thumbnail_send_streams_.clear();
1454 for (VideoReceiveStream* thumbnail_receive_stream :
1455 thumbnail_receive_streams_)
1456 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
1457 thumbnail_send_streams_.clear();
1458 thumbnail_receive_streams_.clear();
1459}
1460
ilnik2a8c2f52017-02-15 02:23:28 -08001461void VideoQualityTest::SetupScreenshareOrSVC() {
1462 if (params_.screenshare.enabled) {
1463 // Fill out codec settings.
1464 video_encoder_config_.content_type =
1465 VideoEncoderConfig::ContentType::kScreen;
1466 degradation_preference_ =
1467 VideoSendStream::DegradationPreference::kMaintainResolution;
1468 if (params_.video.codec == "VP8") {
1469 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1470 vp8_settings.denoisingOn = false;
1471 vp8_settings.frameDroppingOn = false;
1472 vp8_settings.numberOfTemporalLayers =
1473 static_cast<unsigned char>(params_.video.num_temporal_layers);
1474 video_encoder_config_.encoder_specific_settings =
1475 new rtc::RefCountedObject<
1476 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1477 } else if (params_.video.codec == "VP9") {
1478 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1479 vp9_settings.denoisingOn = false;
1480 vp9_settings.frameDroppingOn = false;
1481 vp9_settings.numberOfTemporalLayers =
1482 static_cast<unsigned char>(params_.video.num_temporal_layers);
1483 vp9_settings.numberOfSpatialLayers =
1484 static_cast<unsigned char>(params_.ss.num_spatial_layers);
1485 video_encoder_config_.encoder_specific_settings =
1486 new rtc::RefCountedObject<
1487 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1488 }
1489 // Setup frame generator.
1490 const size_t kWidth = 1850;
1491 const size_t kHeight = 1110;
1492 std::vector<std::string> slides;
1493 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1494 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1495 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1496 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
ivica5d6a06c2015-09-17 05:30:24 -07001497
ilnik2a8c2f52017-02-15 02:23:28 -08001498 if (params_.screenshare.scroll_duration == 0) {
1499 // Cycle image every slide_change_interval seconds.
perkja8ba1952017-02-27 06:52:10 -08001500 frame_generator_ = test::FrameGenerator::CreateFromYuvFile(
ilnik2a8c2f52017-02-15 02:23:28 -08001501 slides, kWidth, kHeight,
perkja8ba1952017-02-27 06:52:10 -08001502 params_.screenshare.slide_change_interval * params_.video.fps);
ilnik2a8c2f52017-02-15 02:23:28 -08001503 } else {
1504 RTC_CHECK_LE(params_.video.width, kWidth);
1505 RTC_CHECK_LE(params_.video.height, kHeight);
1506 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1507 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1508 params_.screenshare.scroll_duration) *
1509 1000;
1510 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1511 params_.screenshare.slide_change_interval);
1512
perkja8ba1952017-02-27 06:52:10 -08001513 frame_generator_ = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1514 clock_, slides, kWidth, kHeight, params_.video.width,
1515 params_.video.height, params_.screenshare.scroll_duration * 1000,
1516 kPauseDurationMs);
ilnik2a8c2f52017-02-15 02:23:28 -08001517 }
1518 } else if (params_.ss.num_spatial_layers > 1) { // For non-screenshare case.
1519 RTC_CHECK(params_.video.codec == "VP9");
kthelgason29a44e32016-09-27 03:52:02 -07001520 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
kthelgason29a44e32016-09-27 03:52:02 -07001521 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001522 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001523 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001524 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001525 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1526 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001527 }
ivica5d6a06c2015-09-17 05:30:24 -07001528}
1529
ilnika014cc52017-03-07 04:21:04 -08001530void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1531 VideoStream thumbnail = DefaultThumbnailStream();
1532 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1533 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1534 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
1535 thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -08001536 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -08001537 }
1538}
1539
perkja49cbd32016-09-16 07:53:41 -07001540void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001541 if (params_.screenshare.enabled) {
1542 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja8ba1952017-02-27 06:52:10 -08001543 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator_),
minyue626bc952016-10-31 05:47:02 -07001544 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001545 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001546 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001547 } else {
sprangce4aef12015-11-02 07:23:20 -08001548 if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001549 video_capturer_.reset(test::VcmCapturer::Create(
minyue626bc952016-10-31 05:47:02 -07001550 params_.video.width, params_.video.height, params_.video.fps));
sprang1bed2e42017-01-23 08:46:51 -08001551 if (!video_capturer_) {
1552 // Failed to get actual camera, use chroma generator as backup.
1553 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
perkja8ba1952017-02-27 06:52:10 -08001554 static_cast<int>(params_.video.width),
1555 static_cast<int>(params_.video.height), params_.video.fps, clock_));
sprang1bed2e42017-01-23 08:46:51 -08001556 }
ivica5d6a06c2015-09-17 05:30:24 -07001557 } else {
minyuea27172d2016-11-01 05:59:29 -07001558 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001559 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001560 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001561 clock_));
minyuea27172d2016-11-01 05:59:29 -07001562 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1563 << params_.video.clip_name
1564 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001565 }
1566 }
sprang1bed2e42017-01-23 08:46:51 -08001567 RTC_DCHECK(video_capturer_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001568}
1569
sprang7a975f72015-10-12 06:33:21 -07001570void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001571 params_ = params;
1572
minyue626bc952016-10-31 05:47:02 -07001573 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001574 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1575 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001576 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001577
1578 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001579 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001580 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001581 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001582 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001583 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1584 << "!";
ivica87f83a92015-10-08 05:13:32 -07001585 }
sprang7a975f72015-10-12 06:33:21 -07001586
skvlad11a9cbf2016-10-07 11:53:05 -07001587 webrtc::RtcEventLogNullImpl event_log;
1588 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001589 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001590 CreateCalls(call_config, call_config);
1591
ilnik68af10d2017-03-02 04:59:33 -08001592 test::LayerFilteringTransport send_transport(
1593 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
1594 params_.video.selected_tl, params_.ss.selected_sl);
nissee5ad5ca2017-03-29 23:57:43 -07001595 test::DirectTransport recv_transport(
1596 params_.pipe, receiver_call_.get(), MediaType::VIDEO);
stefanf116bd02015-10-27 08:29:42 -07001597
sprangce4aef12015-11-02 07:23:20 -08001598 std::string graph_title = params_.analyzer.graph_title;
1599 if (graph_title.empty())
1600 graph_title = VideoQualityTest::GenerateGraphTitle();
1601
sprangc1b57a12017-02-28 08:50:47 -08001602 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
ivica5d6a06c2015-09-17 05:30:24 -07001603 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001604 &send_transport, params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001605 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001606 is_quick_test_enabled
1607 ? kFramesSentInQuickTest
1608 : params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001609 graph_data_output_file, graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -08001610 kVideoSendSsrcs[params_.ss.selected_stream],
ilnik46a00212017-02-10 09:16:05 -08001611 kSendRtxSsrcs[params_.ss.selected_stream],
ilnikcb8c1462017-03-09 09:23:30 -08001612 static_cast<size_t>(params_.ss.selected_stream), params.ss.selected_sl,
ilnik1e7732c2017-02-23 05:07:56 -08001613 params_.video.selected_tl, is_quick_test_enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001614 analyzer.SetReceiver(receiver_call_->Receiver());
1615 send_transport.SetReceiver(&analyzer);
1616 recv_transport.SetReceiver(sender_call_->Receiver());
1617
minyuea27172d2016-11-01 05:59:29 -07001618 SetupVideo(&analyzer, &recv_transport);
ilnika014cc52017-03-07 04:21:04 -08001619 SetupThumbnails(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001620 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001621 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
Peter Boströme4499152016-02-05 11:13:28 +01001622 RTC_DCHECK(!video_send_config_.post_encode_callback);
1623 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001624
ilnik2a8c2f52017-02-15 02:23:28 -08001625 SetupScreenshareOrSVC();
ivica5d6a06c2015-09-17 05:30:24 -07001626
brandtr1293aca2016-11-16 22:47:29 -08001627 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001628 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001629 analyzer.SetSendStream(video_send_stream_);
philipelfd870db2017-01-23 03:22:15 -08001630 if (video_receive_streams_.size() == 1)
1631 analyzer.SetReceiveStream(video_receive_streams_[0]);
kthelgason2bc68642017-02-07 07:02:22 -08001632
1633 video_send_stream_->SetSource(analyzer.OutputInterface(),
1634 degradation_preference_);
ivica5d6a06c2015-09-17 05:30:24 -07001635
ilnika014cc52017-03-07 04:21:04 -08001636 SetupThumbnailCapturers(params_.num_thumbnails);
1637 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1638 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1639 degradation_preference_);
1640 }
1641
perkja49cbd32016-09-16 07:53:41 -07001642 CreateCapturer();
ilnika014cc52017-03-07 04:21:04 -08001643
perkja49cbd32016-09-16 07:53:41 -07001644 rtc::VideoSinkWants wants;
minyuea27172d2016-11-01 05:59:29 -07001645 video_capturer_->AddOrUpdateSink(analyzer.InputInterface(), wants);
ivicac1cc8542015-10-08 03:44:06 -07001646
palmkviste75f2042016-09-28 06:19:48 -07001647 StartEncodedFrameLogs(video_send_stream_);
1648 StartEncodedFrameLogs(video_receive_streams_[0]);
stefanff483612015-12-21 03:14:00 -08001649 video_send_stream_->Start();
ilnika014cc52017-03-07 04:21:04 -08001650 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1651 thumbnail_send_stream->Start();
stefanff483612015-12-21 03:14:00 -08001652 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1653 receive_stream->Start();
brandtr1293aca2016-11-16 22:47:29 -08001654 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1655 receive_stream->Start();
ilnika014cc52017-03-07 04:21:04 -08001656 for (VideoReceiveStream* thumbnail_receive_stream :
1657 thumbnail_receive_streams_)
1658 thumbnail_receive_stream->Start();
1659
ilnikdf92c5c2017-02-23 02:08:44 -08001660 analyzer.StartMeasuringCpuProcessTime();
ilnika014cc52017-03-07 04:21:04 -08001661
minyuea27172d2016-11-01 05:59:29 -07001662 video_capturer_->Start();
ilnika014cc52017-03-07 04:21:04 -08001663 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1664 thumbnail_capturers_) {
1665 video_caputurer->Start();
1666 }
ivica5d6a06c2015-09-17 05:30:24 -07001667
1668 analyzer.Wait();
1669
1670 send_transport.StopSending();
1671 recv_transport.StopSending();
1672
ilnika014cc52017-03-07 04:21:04 -08001673 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1674 thumbnail_capturers_)
1675 video_caputurer->Stop();
minyuea27172d2016-11-01 05:59:29 -07001676 video_capturer_->Stop();
ilnika014cc52017-03-07 04:21:04 -08001677 for (VideoReceiveStream* thumbnail_receive_stream :
1678 thumbnail_receive_streams_)
1679 thumbnail_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001680 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1681 receive_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001682 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1683 receive_stream->Stop();
ilnika014cc52017-03-07 04:21:04 -08001684 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1685 thumbnail_send_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001686 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001687
1688 DestroyStreams();
ilnika014cc52017-03-07 04:21:04 -08001689 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -07001690
1691 if (graph_data_output_file)
1692 fclose(graph_data_output_file);
1693}
1694
minyuea27172d2016-11-01 05:59:29 -07001695void VideoQualityTest::SetupAudio(int send_channel_id,
1696 int receive_channel_id,
1697 Call* call,
1698 Transport* transport,
1699 AudioReceiveStream** audio_receive_stream) {
1700 audio_send_config_ = AudioSendStream::Config(transport);
1701 audio_send_config_.voe_channel_id = send_channel_id;
1702 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1703
1704 // Add extension to enable audio send side BWE, and allow audio bit rate
1705 // adaptation.
1706 audio_send_config_.rtp.extensions.clear();
1707 if (params_.call.send_side_bwe) {
1708 audio_send_config_.rtp.extensions.push_back(
1709 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1710 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001711 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1712 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001713 }
1714 audio_send_config_.send_codec_spec.codec_inst =
1715 CodecInst{120, "OPUS", 48000, 960, 2, 64000};
minyue4c8b9422017-03-21 04:11:43 -07001716 audio_send_config_.send_codec_spec.enable_opus_dtx = params_.audio.dtx;
minyuea27172d2016-11-01 05:59:29 -07001717 audio_send_stream_ = call->CreateAudioSendStream(audio_send_config_);
1718
1719 AudioReceiveStream::Config audio_config;
1720 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1721 audio_config.rtcp_send_transport = transport;
1722 audio_config.voe_channel_id = receive_channel_id;
1723 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1724 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1725 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1726 audio_config.decoder_factory = decoder_factory_;
1727 if (params_.video.enabled && params_.audio.sync_video)
1728 audio_config.sync_group = kSyncGroup;
1729
1730 *audio_receive_stream = call->CreateAudioReceiveStream(audio_config);
1731}
1732
minyue73208662016-08-18 06:28:55 -07001733void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001734 params_ = params;
1735 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001736
ivica5d6a06c2015-09-17 05:30:24 -07001737 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1738 // match the full stack tests.
skvlad11a9cbf2016-10-07 11:53:05 -07001739 webrtc::RtcEventLogNullImpl event_log;
1740 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001741 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001742
1743 ::VoiceEngineState voe;
minyue626bc952016-10-31 05:47:02 -07001744 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001745 CreateVoiceEngine(&voe, decoder_factory_);
1746 AudioState::Config audio_state_config;
1747 audio_state_config.voice_engine = voe.voice_engine;
aleloi10111bc2016-11-17 06:48:48 -08001748 audio_state_config.audio_mixer = AudioMixerImpl::Create();
minyue73208662016-08-18 06:28:55 -07001749 call_config.audio_state = AudioState::Create(audio_state_config);
1750 }
1751
kwiberg27f982b2016-03-01 11:52:33 -08001752 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001753
minyuea27172d2016-11-01 05:59:29 -07001754 // TODO(minyue): consider if this is a good transport even for audio only
1755 // calls.
ivica5d6a06c2015-09-17 05:30:24 -07001756 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001757 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
ilnik68af10d2017-03-02 04:59:33 -08001758 params.video.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001759 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1760 // least share as much code as possible. That way this test would also match
1761 // the full stack tests better.
1762 transport.SetReceiver(call->Receiver());
1763
minyuea27172d2016-11-01 05:59:29 -07001764 VideoReceiveStream* video_receive_stream = nullptr;
brandtr1293aca2016-11-16 22:47:29 -08001765 FlexfecReceiveStream* flexfec_receive_stream = nullptr;
minyuea27172d2016-11-01 05:59:29 -07001766 std::unique_ptr<test::VideoRenderer> local_preview;
1767 std::unique_ptr<test::VideoRenderer> loopback_video;
1768 if (params_.video.enabled) {
1769 // Create video renderers.
1770 local_preview.reset(test::VideoRenderer::Create(
1771 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 05:13:32 -07001772
minyuea27172d2016-11-01 05:59:29 -07001773 size_t stream_id = params_.ss.selected_stream;
1774 std::string title = "Loopback Video";
1775 if (params_.ss.streams.size() > 1) {
1776 std::ostringstream s;
1777 s << stream_id;
1778 title += " - Stream #" + s.str();
1779 }
sprangce4aef12015-11-02 07:23:20 -08001780
minyuea27172d2016-11-01 05:59:29 -07001781 loopback_video.reset(test::VideoRenderer::Create(
1782 title.c_str(), params_.ss.streams[stream_id].width,
1783 params_.ss.streams[stream_id].height));
mflodman48a4beb2016-07-01 13:03:59 +02001784
minyuea27172d2016-11-01 05:59:29 -07001785 SetupVideo(&transport, &transport);
minyuea27172d2016-11-01 05:59:29 -07001786 video_send_config_.pre_encode_callback = local_preview.get();
1787 video_receive_configs_[stream_id].renderer = loopback_video.get();
1788 if (params_.audio.enabled && params_.audio.sync_video)
1789 video_receive_configs_[stream_id].sync_group = kSyncGroup;
1790
ilnik68af10d2017-03-02 04:59:33 -08001791 if (params_.screenshare.enabled)
1792 SetupScreenshareOrSVC();
minyuea27172d2016-11-01 05:59:29 -07001793
1794 video_send_stream_ = call->CreateVideoSendStream(
1795 video_send_config_.Copy(), video_encoder_config_.Copy());
brandtr1293aca2016-11-16 22:47:29 -08001796 if (params_.video.flexfec) {
1797 RTC_DCHECK(!flexfec_receive_configs_.empty());
1798 flexfec_receive_stream =
1799 call->CreateFlexfecReceiveStream(flexfec_receive_configs_[0]);
1800 }
minyuea27172d2016-11-01 05:59:29 -07001801 video_receive_stream = call->CreateVideoReceiveStream(
1802 video_receive_configs_[stream_id].Copy());
1803 CreateCapturer();
kthelgason2bc68642017-02-07 07:02:22 -08001804 video_send_stream_->SetSource(video_capturer_.get(),
1805 degradation_preference_);
philipel274c1dc2016-05-04 06:21:01 -07001806 }
1807
minyue73208662016-08-18 06:28:55 -07001808 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 05:47:02 -07001809 if (params_.audio.enabled) {
minyuea27172d2016-11-01 05:59:29 -07001810 SetupAudio(voe.send_channel_id, voe.receive_channel_id, call.get(),
1811 &transport, &audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001812 }
1813
palmkviste75f2042016-09-28 06:19:48 -07001814 StartEncodedFrameLogs(video_receive_stream);
1815 StartEncodedFrameLogs(video_send_stream_);
1816
minyue73208662016-08-18 06:28:55 -07001817 // Start sending and receiving video.
minyuea27172d2016-11-01 05:59:29 -07001818 if (params_.video.enabled) {
brandtr1293aca2016-11-16 22:47:29 -08001819 if (flexfec_receive_stream)
1820 flexfec_receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001821 video_receive_stream->Start();
1822 video_send_stream_->Start();
1823 video_capturer_->Start();
1824 }
ivica5d6a06c2015-09-17 05:30:24 -07001825
minyue626bc952016-10-31 05:47:02 -07001826 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001827 // Start receiving audio.
1828 audio_receive_stream->Start();
1829 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 06:28:55 -07001830
1831 // Start sending audio.
1832 audio_send_stream_->Start();
1833 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1834 }
1835
ivica5d6a06c2015-09-17 05:30:24 -07001836 test::PressEnterToContinue();
1837
minyue626bc952016-10-31 05:47:02 -07001838 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001839 // Stop sending audio.
1840 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1841 audio_send_stream_->Stop();
1842
1843 // Stop receiving audio.
minyue73208662016-08-18 06:28:55 -07001844 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
1845 audio_receive_stream->Stop();
minyuea27172d2016-11-01 05:59:29 -07001846 call->DestroyAudioSendStream(audio_send_stream_);
1847 call->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001848 }
1849
1850 // Stop receiving and sending video.
minyuea27172d2016-11-01 05:59:29 -07001851 if (params_.video.enabled) {
1852 video_capturer_->Stop();
1853 video_send_stream_->Stop();
1854 video_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001855 if (flexfec_receive_stream) {
1856 flexfec_receive_stream->Stop();
1857 call->DestroyFlexfecReceiveStream(flexfec_receive_stream);
1858 }
minyuea27172d2016-11-01 05:59:29 -07001859 call->DestroyVideoReceiveStream(video_receive_stream);
1860 call->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 06:28:55 -07001861 }
1862
ivica5d6a06c2015-09-17 05:30:24 -07001863 transport.StopSending();
minyue626bc952016-10-31 05:47:02 -07001864 if (params_.audio.enabled)
minyue73208662016-08-18 06:28:55 -07001865 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07001866}
1867
palmkviste75f2042016-09-28 06:19:48 -07001868void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001869 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001870 std::ostringstream str;
1871 str << send_logs_++;
1872 std::string prefix =
minyue626bc952016-10-31 05:47:02 -07001873 params_.video.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07001874 stream->EnableEncodedFrameRecording(
1875 std::vector<rtc::PlatformFile>(
1876 {rtc::CreatePlatformFile(prefix + "1.ivf"),
1877 rtc::CreatePlatformFile(prefix + "2.ivf"),
1878 rtc::CreatePlatformFile(prefix + "3.ivf")}),
1879 10000000);
1880 }
1881}
ilnikcb8c1462017-03-09 09:23:30 -08001882
palmkviste75f2042016-09-28 06:19:48 -07001883void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001884 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001885 std::ostringstream str;
1886 str << receive_logs_++;
1887 std::string path =
minyue626bc952016-10-31 05:47:02 -07001888 params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07001889 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
1890 10000000);
1891 }
1892}
ivica5d6a06c2015-09-17 05:30:24 -07001893} // namespace webrtc