blob: 4c3c731dd79831ce817e3cdb4f18871126c0b42b [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");
ivica5d6a06c2015-09-17 05:30:24 -0700747 PrintResult("total_delay_incl_network", end_to_end_, " ms");
748 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700749 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800750 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800751 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
752
753 if (receive_stream_ != nullptr) {
754 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800755 }
ivica5d6a06c2015-09-17 05:30:24 -0700756
pbos14fe7082016-04-20 06:35:56 -0700757 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
758 dropped_frames_);
ilnikdf92c5c2017-02-23 02:08:44 -0800759 printf("RESULT cpu_usage: %s = %lf %%\n", test_label_.c_str(),
760 GetCpuUsagePercent());
ilnikdaa574d2017-03-01 06:46:05 -0800761
762#if defined(WEBRTC_WIN)
763 // On Linux and Mac in Resident Set some unused pages may be counted.
764 // Therefore this metric will depend on order in which tests are run and
765 // will be flaky.
766 PrintResult("memory_usage", memory_usage_, " bytes");
767#endif
768
ilnik9ae0d762017-02-15 00:53:12 -0800769 // Disable quality check for quick test, as quality checks may fail
770 // because too few samples were collected.
771 if (!is_quick_test_enabled_) {
772 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
773 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
774 }
ivica5d6a06c2015-09-17 05:30:24 -0700775 }
776
777 void PerformFrameComparison(const FrameComparison& comparison) {
778 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700779 double psnr = -1.0;
780 double ssim = -1.0;
nissedf2ceb82016-12-15 06:29:53 -0800781 if (comparison.reference) {
782 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
783 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700784 }
ivica5d6a06c2015-09-17 05:30:24 -0700785
ivica5d6a06c2015-09-17 05:30:24 -0700786 rtc::CritScope crit(&comparison_lock_);
787 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800788 samples_.push_back(Sample(
789 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
790 comparison.recv_time_ms, comparison.render_time_ms,
791 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700792 }
stefanb1797672016-08-11 07:00:57 -0700793 if (psnr >= 0.0)
794 psnr_.AddSample(psnr);
795 if (ssim >= 0.0)
796 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700797
798 if (comparison.dropped) {
799 ++dropped_frames_;
800 return;
801 }
802 if (last_render_time_ != 0)
803 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
804 last_render_time_ = comparison.render_time_ms;
805
nissedf2ceb82016-12-15 06:29:53 -0800806 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800807 if (comparison.recv_time_ms > 0) {
808 // If recv_time_ms == 0, this frame consisted of a packets which were all
809 // lost in the transport. Since we were able to render the frame, however,
810 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
811 // happens internally in Call, and we can therefore here not know which
812 // FEC packets that protected the lost media packets. Consequently, we
813 // were not able to record a meaningful recv_time_ms. We therefore skip
814 // this sample.
815 //
816 // The reasoning above does not hold for ULPFEC and RTX, as for those
817 // strategies the timestamp of the received packets is set to the
818 // timestamp of the protected/retransmitted media packet. I.e., then
819 // recv_time_ms != 0, even though the media packets were lost.
820 receiver_time_.AddSample(comparison.render_time_ms -
821 comparison.recv_time_ms);
822 }
nissedf2ceb82016-12-15 06:29:53 -0800823 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700824 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
825 }
826
827 void PrintResult(const char* result_type,
828 test::Statistics stats,
829 const char* unit) {
830 printf("RESULT %s: %s = {%f, %f}%s\n",
831 result_type,
832 test_label_.c_str(),
833 stats.Mean(),
834 stats.StandardDeviation(),
835 unit);
836 }
837
838 void PrintSamplesToFile(void) {
839 FILE* out = graph_data_output_file_;
840 rtc::CritScope crit(&comparison_lock_);
841 std::sort(samples_.begin(), samples_.end(),
842 [](const Sample& A, const Sample& B) -> bool {
843 return A.input_time_ms < B.input_time_ms;
844 });
845
sprangce4aef12015-11-02 07:23:20 -0800846 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700847 fprintf(out, "%" PRIuS "\n", samples_.size());
848 fprintf(out,
849 "dropped "
850 "input_time_ms "
851 "send_time_ms "
852 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700853 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700854 "encoded_frame_size "
855 "psnr "
856 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700857 "encode_time_ms\n");
858 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700859 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700860 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
861 int encode_time_ms;
862 if (it != samples_encode_time_ms_.end()) {
863 encode_time_ms = it->second;
864 } else {
865 ++missing_encode_time_samples;
866 encode_time_ms = -1;
867 }
868 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
869 " %lf %lf %d\n",
870 sample.dropped, sample.input_time_ms, sample.send_time_ms,
871 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700872 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700873 encode_time_ms);
874 }
875 if (missing_encode_time_samples) {
876 fprintf(stderr,
877 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
878 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700879 }
880 }
881
ilnik1e7732c2017-02-23 05:07:56 -0800882 double GetAverageMediaBitrateBps() {
883 if (last_sending_time_ == first_sending_time_) {
884 return 0;
885 } else {
886 return static_cast<double>(total_media_bytes_) * 8 /
887 (last_sending_time_ - first_sending_time_) *
888 rtc::kNumMillisecsPerSec;
889 }
890 }
891
perkja49cbd32016-09-16 07:53:41 -0700892 // Implements VideoSinkInterface to receive captured frames from a
893 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
894 // as a source to VideoSendStream.
895 // It forwards all input frames to the VideoAnalyzer for later comparison and
896 // forwards the captured frames to the VideoSendStream.
897 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
898 public rtc::VideoSourceInterface<VideoFrame> {
899 public:
900 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer)
901 : analyzer_(analyzer), send_stream_input_(nullptr) {}
902
903 private:
904 void OnFrame(const VideoFrame& video_frame) override {
905 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -0800906 // Frames from the capturer does not have a rtp timestamp.
907 // Create one so it can be used for comparison.
908 RTC_DCHECK_EQ(0, video_frame.timestamp());
perkja49cbd32016-09-16 07:53:41 -0700909 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -0800910 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -0700911 rtc::CritScope lock(&crit_);
912 if (send_stream_input_)
913 send_stream_input_->OnFrame(video_frame);
914 }
915
916 // Called when |send_stream_.SetSource()| is called.
917 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
918 const rtc::VideoSinkWants& wants) override {
919 rtc::CritScope lock(&crit_);
920 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
921 send_stream_input_ = sink;
922 }
923
924 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
925 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
926 rtc::CritScope lock(&crit_);
927 RTC_DCHECK(sink == send_stream_input_);
928 send_stream_input_ = nullptr;
929 }
930
931 VideoAnalyzer* const analyzer_;
932 rtc::CriticalSection crit_;
933 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
934 };
935
936 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
937 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800938 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -0700939 }
940
941 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -0800942 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -0700943 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700944 const std::string test_label_;
945 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800946 const std::string graph_title_;
947 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -0800948 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 09:23:30 -0800949 const size_t selected_stream_;
ilnik1e7732c2017-02-23 05:07:56 -0800950 const int selected_sl_;
951 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -0700952 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100953 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700954 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700955 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700956 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
957 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
958 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
959 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
960 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
961 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
962 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
963 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
philipelfd870db2017-01-23 03:22:15 -0800964 test::Statistics encode_time_ms_ GUARDED_BY(comparison_lock_);
965 test::Statistics encode_usage_percent_ GUARDED_BY(comparison_lock_);
966 test::Statistics decode_time_ms_ GUARDED_BY(comparison_lock_);
967 test::Statistics decode_time_max_ms_ GUARDED_BY(comparison_lock_);
968 test::Statistics media_bitrate_bps_ GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 06:46:05 -0800969 test::Statistics memory_usage_ GUARDED_BY(comparison_lock_);
970
ivica5d6a06c2015-09-17 05:30:24 -0700971
972 const int frames_to_process_;
973 int frames_recorded_;
974 int frames_processed_;
975 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -0700976 int dropped_frames_before_first_encode_;
977 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -0700978 int64_t last_render_time_;
979 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -0800980 int64_t total_media_bytes_;
981 int64_t first_sending_time_;
982 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -0700983
ilnikdf92c5c2017-02-23 02:08:44 -0800984 int64_t cpu_time_ GUARDED_BY(cpu_measurement_lock_);
985 int64_t wallclock_time_ GUARDED_BY(cpu_measurement_lock_);
986 rtc::CriticalSection cpu_measurement_lock_;
987
ivica5d6a06c2015-09-17 05:30:24 -0700988 rtc::CriticalSection crit_;
989 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -0700990 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800991 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
992 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
993 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
994 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800995 rtc::Optional<uint32_t> first_encoded_timestamp_ GUARDED_BY(crit_);
996 rtc::Optional<uint32_t> first_sent_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700997 const double avg_psnr_threshold_;
998 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -0800999 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -07001000
1001 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001002 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1003 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001004 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -07001005 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001006 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -07001007};
1008
ilnikcb8c1462017-03-09 09:23:30 -08001009class Vp8EncoderFactory : public VideoEncoderFactory {
1010 public:
1011 Vp8EncoderFactory() = default;
1012 ~Vp8EncoderFactory() override { RTC_CHECK(live_encoders_.empty()); }
1013
1014 VideoEncoder* Create() override {
1015 VideoEncoder* encoder = VP8Encoder::Create();
1016 live_encoders_.insert(encoder);
1017 return encoder;
1018 }
1019
1020 void Destroy(VideoEncoder* encoder) override {
1021 auto it = live_encoders_.find(encoder);
1022 RTC_CHECK(it != live_encoders_.end());
1023 live_encoders_.erase(it);
1024 delete encoder;
1025 }
1026
1027 std::set<VideoEncoder*> live_encoders_;
1028};
1029
palmkviste75f2042016-09-28 06:19:48 -07001030VideoQualityTest::VideoQualityTest()
1031 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {}
ivica5d6a06c2015-09-17 05:30:24 -07001032
minyue626bc952016-10-31 05:47:02 -07001033VideoQualityTest::Params::Params()
1034 : call({false, Call::Config::BitrateConfig()}),
1035 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
brandtr1293aca2016-11-16 22:47:29 -08001036 false, "", ""}),
minyue4c8b9422017-03-21 04:11:43 -07001037 audio({false, false, false}),
minyue626bc952016-10-31 05:47:02 -07001038 screenshare({false, 10, 0}),
1039 analyzer({"", 0.0, 0.0, 0, "", ""}),
1040 pipe(),
1041 logs(false),
ilnika014cc52017-03-07 04:21:04 -08001042 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}),
1043 num_thumbnails(0) {}
minyue626bc952016-10-31 05:47:02 -07001044
1045VideoQualityTest::Params::~Params() = default;
1046
ivica5d6a06c2015-09-17 05:30:24 -07001047void VideoQualityTest::TestBody() {}
1048
sprangce4aef12015-11-02 07:23:20 -08001049std::string VideoQualityTest::GenerateGraphTitle() const {
1050 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -07001051 ss << params_.video.codec;
1052 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
1053 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -08001054 if (params_.screenshare.scroll_duration)
1055 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
1056 if (params_.ss.streams.size() > 1)
1057 ss << ", Stream #" << params_.ss.selected_stream;
1058 if (params_.ss.num_spatial_layers > 1)
1059 ss << ", Layer #" << params_.ss.selected_sl;
1060 ss << ")";
1061 return ss.str();
1062}
1063
1064void VideoQualityTest::CheckParams() {
stefan7de8d642017-02-07 07:14:08 -08001065 if (!params_.video.enabled)
1066 return;
sprangce4aef12015-11-02 07:23:20 -08001067 // Add a default stream in none specified.
1068 if (params_.ss.streams.empty())
1069 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
1070 if (params_.ss.num_spatial_layers == 0)
1071 params_.ss.num_spatial_layers = 1;
1072
1073 if (params_.pipe.loss_percent != 0 ||
1074 params_.pipe.queue_length_packets != 0) {
1075 // Since LayerFilteringTransport changes the sequence numbers, we can't
1076 // use that feature with pack loss, since the NACK request would end up
1077 // retransmitting the wrong packets.
1078 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -08001079 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -07001080 RTC_CHECK(params_.video.selected_tl == -1 ||
1081 params_.video.selected_tl ==
1082 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001083 }
1084
1085 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
1086 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -07001087 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
1088 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
1089 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprangce4aef12015-11-02 07:23:20 -08001090 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
1091 for (const VideoStream& stream : params_.ss.streams) {
1092 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1093 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1094 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
ilnikcb8c1462017-03-09 09:23:30 -08001095 RTC_CHECK_LE(stream.temporal_layer_thresholds_bps.size(),
minyue626bc952016-10-31 05:47:02 -07001096 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001097 }
1098 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1099 // the total bitrate? We anyway have to update them in the case bitrate
1100 // estimator changes the total bitrates.
1101 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
1102 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
1103 RTC_CHECK(params_.ss.spatial_layers.empty() ||
1104 params_.ss.spatial_layers.size() ==
1105 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -07001106 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -08001107 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -07001108 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -08001109 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -08001110 }
ilnika014cc52017-03-07 04:21:04 -08001111 RTC_CHECK_GE(params_.num_thumbnails, 0);
1112 if (params_.num_thumbnails > 0) {
1113 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
1114 RTC_CHECK_EQ(params_.ss.streams.size(), 3);
1115 RTC_CHECK_EQ(params_.video.num_temporal_layers, 3);
1116 RTC_CHECK_EQ(params_.video.codec, "VP8");
1117 }
sprangce4aef12015-11-02 07:23:20 -08001118}
1119
1120// Static.
1121std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1122 // Parse comma separated nonnegative integers, where some elements may be
1123 // empty. The empty values are replaced with -1.
1124 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1125 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1126 std::vector<int> result;
1127 if (str.empty())
1128 return result;
1129
1130 const char* p = str.c_str();
1131 int value = -1;
1132 int pos;
1133 while (*p) {
1134 if (*p == ',') {
1135 result.push_back(value);
1136 value = -1;
1137 ++p;
1138 continue;
1139 }
1140 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1141 << "Unexpected non-number value.";
1142 p += pos;
1143 }
1144 result.push_back(value);
1145 return result;
1146}
1147
1148// Static.
1149VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
1150 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -07001151 stream.width = params.video.width;
1152 stream.height = params.video.height;
1153 stream.max_framerate = params.video.fps;
1154 stream.min_bitrate_bps = params.video.min_bitrate_bps;
1155 stream.target_bitrate_bps = params.video.target_bitrate_bps;
1156 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprangce4aef12015-11-02 07:23:20 -08001157 stream.max_qp = 52;
sprang6ef1b342017-03-13 02:01:32 -07001158 // TODO(sprang): Can we make this less of a hack?
1159 if (params.video.num_temporal_layers == 2) {
1160 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
1161 } else if (params.video.num_temporal_layers == 3) {
1162 stream.temporal_layer_thresholds_bps.push_back(stream.max_bitrate_bps / 4);
1163 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
ilnika014cc52017-03-07 04:21:04 -08001164 }
1165 return stream;
1166}
1167
1168// Static.
1169VideoStream VideoQualityTest::DefaultThumbnailStream() {
1170 VideoStream stream;
1171 stream.width = 320;
1172 stream.height = 180;
1173 stream.max_framerate = 7;
1174 stream.min_bitrate_bps = 7500;
1175 stream.target_bitrate_bps = 37500;
1176 stream.max_bitrate_bps = 50000;
1177 stream.max_qp = 52;
sprangce4aef12015-11-02 07:23:20 -08001178 return stream;
1179}
1180
1181// Static.
1182void VideoQualityTest::FillScalabilitySettings(
1183 Params* params,
1184 const std::vector<std::string>& stream_descriptors,
1185 size_t selected_stream,
1186 int num_spatial_layers,
1187 int selected_sl,
1188 const std::vector<std::string>& sl_descriptors) {
1189 // Read VideoStream and SpatialLayer elements from a list of comma separated
1190 // lists. To use a default value for an element, use -1 or leave empty.
1191 // Validity checks performed in CheckParams.
1192
1193 RTC_CHECK(params->ss.streams.empty());
1194 for (auto descriptor : stream_descriptors) {
1195 if (descriptor.empty())
1196 continue;
1197 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
1198 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1199 if (v[0] != -1)
1200 stream.width = static_cast<size_t>(v[0]);
1201 if (v[1] != -1)
1202 stream.height = static_cast<size_t>(v[1]);
1203 if (v[2] != -1)
1204 stream.max_framerate = v[2];
1205 if (v[3] != -1)
1206 stream.min_bitrate_bps = v[3];
1207 if (v[4] != -1)
1208 stream.target_bitrate_bps = v[4];
1209 if (v[5] != -1)
1210 stream.max_bitrate_bps = v[5];
1211 if (v.size() > 6 && v[6] != -1)
1212 stream.max_qp = v[6];
1213 if (v.size() > 7) {
1214 stream.temporal_layer_thresholds_bps.clear();
1215 stream.temporal_layer_thresholds_bps.insert(
1216 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1217 } else {
1218 // Automatic TL thresholds for more than two layers not supported.
minyue626bc952016-10-31 05:47:02 -07001219 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
sprangce4aef12015-11-02 07:23:20 -08001220 }
1221 params->ss.streams.push_back(stream);
1222 }
1223 params->ss.selected_stream = selected_stream;
1224
1225 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
1226 params->ss.selected_sl = selected_sl;
1227 RTC_CHECK(params->ss.spatial_layers.empty());
1228 for (auto descriptor : sl_descriptors) {
1229 if (descriptor.empty())
1230 continue;
1231 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1232 RTC_CHECK_GT(v[2], 0);
1233
1234 SpatialLayer layer;
1235 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1236 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1237 layer.target_bitrate_bps = v[2];
1238 params->ss.spatial_layers.push_back(layer);
1239 }
1240}
1241
minyuea27172d2016-11-01 05:59:29 -07001242void VideoQualityTest::SetupVideo(Transport* send_transport,
1243 Transport* recv_transport) {
sprangce4aef12015-11-02 07:23:20 -08001244 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -07001245 trace_to_stderr_.reset(new test::TraceToStderr);
1246
brandtr8313a6f2017-01-13 07:41:19 -08001247 size_t num_video_streams = params_.ss.streams.size();
1248 size_t num_flexfec_streams = params_.video.flexfec ? 1 : 0;
1249 CreateSendConfig(num_video_streams, 0, num_flexfec_streams, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001250
1251 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001252 if (params_.video.codec == "H264") {
magjedceecea42016-11-28 07:20:21 -08001253 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001254 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001255 } else if (params_.video.codec == "VP8") {
ilnikcb8c1462017-03-09 09:23:30 -08001256 if (params_.screenshare.enabled && params_.ss.streams.size() > 1) {
1257 // Simulcast screenshare needs a simulcast encoder adapter to work, since
1258 // encoders usually can't natively do simulcast with different frame rates
1259 // for the different layers.
1260 video_encoder_.reset(
1261 new SimulcastEncoderAdapter(new Vp8EncoderFactory()));
1262 } else {
1263 video_encoder_.reset(VP8Encoder::Create());
1264 }
ivica5d6a06c2015-09-17 05:30:24 -07001265 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001266 } else if (params_.video.codec == "VP9") {
magjed509e4fe2016-11-18 01:34:11 -08001267 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001268 payload_type = kPayloadTypeVP9;
1269 } else {
1270 RTC_NOTREACHED() << "Codec not supported!";
1271 return;
1272 }
minyuea27172d2016-11-01 05:59:29 -07001273 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001274 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001275 video_send_config_.encoder_settings.payload_type = payload_type;
1276 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1277 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
brandtr8313a6f2017-01-13 07:41:19 -08001278 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001279 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001280
stefanff483612015-12-21 03:14:00 -08001281 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001282 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001283 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001284 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001285 test::kTransportSequenceNumberExtensionId));
1286 } else {
stefanff483612015-12-21 03:14:00 -08001287 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001288 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik SprĂ¥ng6b8d3552015-09-24 15:06:57 +02001289 }
1290
stefanff483612015-12-21 03:14:00 -08001291 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001292 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001293
brandtr1293aca2016-11-16 22:47:29 -08001294 video_send_config_.suspend_below_min_bitrate =
1295 params_.video.suspend_below_min_bitrate;
1296
perkjfa10b552016-10-02 23:45:26 -07001297 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1298 video_encoder_config_.max_bitrate_bps = 0;
1299 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1300 video_encoder_config_.max_bitrate_bps +=
1301 params_.ss.streams[i].max_bitrate_bps;
1302 }
1303 video_encoder_config_.video_stream_factory =
1304 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1305
stefanff483612015-12-21 03:14:00 -08001306 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001307
1308 CreateMatchingReceiveConfigs(recv_transport);
1309
brandtr8313a6f2017-01-13 07:41:19 -08001310 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001311 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
brandtr14742122017-01-27 04:53:07 -08001312 video_receive_configs_[i].rtp.rtx_ssrc = kSendRtxSsrcs[i];
1313 video_receive_configs_[i].rtp.rtx_payload_types[payload_type] =
sprangce4aef12015-11-02 07:23:20 -08001314 kSendRtxPayloadType;
minyue626bc952016-10-31 05:47:02 -07001315 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
Stefan Holmer85d5ac72017-02-09 16:25:16 +01001316 video_receive_configs_[i].rtp.remb = !params_.call.send_side_bwe;
ilnik9fd9f6c2017-03-02 08:10:10 -08001317 // Force fake decoders on non-selected simulcast streams.
1318 if (i != params_.ss.selected_stream) {
1319 VideoReceiveStream::Decoder decoder;
1320 decoder.decoder = new test::FakeDecoder();
1321 decoder.payload_type = video_send_config_.encoder_settings.payload_type;
1322 decoder.payload_name = video_send_config_.encoder_settings.payload_name;
1323 video_receive_configs_[i].decoders.clear();
1324 allocated_decoders_.emplace_back(decoder.decoder);
1325 video_receive_configs_[i].decoders.push_back(decoder);
1326 }
sprangce4aef12015-11-02 07:23:20 -08001327 }
brandtr1293aca2016-11-16 22:47:29 -08001328
1329 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 07:41:19 -08001330 // Override send config constructed by CreateSendConfig.
brandtr1293aca2016-11-16 22:47:29 -08001331 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1332 kVideoSendSsrcs[params_.ss.selected_stream]};
1333
brandtr8313a6f2017-01-13 07:41:19 -08001334 // The matching receive config is _not_ created by
1335 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1336 // Set up the receive config manually instead.
1337 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001338 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 06:59:19 -08001339 video_send_config_.rtp.flexfec.payload_type;
1340 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001341 flexfec_receive_config.protected_media_ssrcs =
1342 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001343 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001344 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1345 if (params_.call.send_side_bwe) {
1346 flexfec_receive_config.rtp_header_extensions.push_back(
1347 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1348 test::kTransportSequenceNumberExtensionId));
1349 } else {
1350 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1351 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1352 }
brandtr1293aca2016-11-16 22:47:29 -08001353 flexfec_receive_configs_.push_back(flexfec_receive_config);
1354 }
1355
1356 if (params_.video.ulpfec) {
1357 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1358 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1359 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1360
1361 video_receive_configs_[params_.ss.selected_stream]
1362 .rtp.ulpfec.red_payload_type =
1363 video_send_config_.rtp.ulpfec.red_payload_type;
1364 video_receive_configs_[params_.ss.selected_stream]
1365 .rtp.ulpfec.ulpfec_payload_type =
1366 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1367 video_receive_configs_[params_.ss.selected_stream]
1368 .rtp.ulpfec.red_rtx_payload_type =
1369 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1370 }
ivica5d6a06c2015-09-17 05:30:24 -07001371}
1372
ilnika014cc52017-03-07 04:21:04 -08001373void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1374 Transport* recv_transport) {
1375 for (int i = 0; i < params_.num_thumbnails; ++i) {
1376 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1377
1378 // Thumbnails will be send in the other way: from receiver_call to
1379 // sender_call.
1380 VideoSendStream::Config thumbnail_send_config(recv_transport);
1381 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1382 thumbnail_send_config.encoder_settings.encoder =
1383 thumbnail_encoders_.back().get();
1384 thumbnail_send_config.encoder_settings.payload_name = params_.video.codec;
1385 thumbnail_send_config.encoder_settings.payload_type = kPayloadTypeVP8;
1386 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1387 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1388 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1389 thumbnail_send_config.rtp.extensions.clear();
1390 if (params_.call.send_side_bwe) {
1391 thumbnail_send_config.rtp.extensions.push_back(
1392 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1393 test::kTransportSequenceNumberExtensionId));
1394 } else {
1395 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1396 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1397 }
1398
1399 VideoEncoderConfig thumbnail_encoder_config;
1400 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1401 thumbnail_send_config.suspend_below_min_bitrate =
1402 params_.video.suspend_below_min_bitrate;
1403 thumbnail_encoder_config.number_of_streams = 1;
1404 thumbnail_encoder_config.max_bitrate_bps = 50000;
1405 thumbnail_encoder_config.video_stream_factory =
1406 new rtc::RefCountedObject<VideoStreamFactory>(
1407 std::vector<webrtc::VideoStream>{DefaultThumbnailStream()});
1408 thumbnail_encoder_config.spatial_layers = params_.ss.spatial_layers;
1409
1410 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1411 thumbnail_receive_config.rtp.remb = false;
1412 thumbnail_receive_config.rtp.transport_cc = true;
1413 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1414 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1415 thumbnail_receive_config.rtp.extensions.push_back(extension);
1416 thumbnail_receive_config.renderer = &fake_renderer_;
1417
1418 VideoReceiveStream::Decoder decoder =
1419 test::CreateMatchingDecoder(thumbnail_send_config.encoder_settings);
1420 allocated_decoders_.push_back(
1421 std::unique_ptr<VideoDecoder>(decoder.decoder));
1422 thumbnail_receive_config.decoders.clear();
1423 thumbnail_receive_config.decoders.push_back(decoder);
1424 thumbnail_receive_config.rtp.remote_ssrc =
1425 thumbnail_send_config.rtp.ssrcs[0];
1426
1427 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1428 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
1429 thumbnail_receive_config.rtp.rtx_payload_types[kPayloadTypeVP8] =
1430 kSendRtxPayloadType;
1431 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1432 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1433
1434 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1435 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1436 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1437 }
1438
1439 for (int i = 0; i < params_.num_thumbnails; ++i) {
1440 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1441 thumbnail_send_configs_[i].Copy(),
1442 thumbnail_encoder_configs_[i].Copy()));
1443 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1444 thumbnail_receive_configs_[i].Copy()));
1445 }
1446}
1447
1448void VideoQualityTest::DestroyThumbnailStreams() {
1449 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1450 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
1451 thumbnail_send_streams_.clear();
1452 for (VideoReceiveStream* thumbnail_receive_stream :
1453 thumbnail_receive_streams_)
1454 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
1455 thumbnail_send_streams_.clear();
1456 thumbnail_receive_streams_.clear();
1457}
1458
ilnik2a8c2f52017-02-15 02:23:28 -08001459void VideoQualityTest::SetupScreenshareOrSVC() {
1460 if (params_.screenshare.enabled) {
1461 // Fill out codec settings.
1462 video_encoder_config_.content_type =
1463 VideoEncoderConfig::ContentType::kScreen;
1464 degradation_preference_ =
1465 VideoSendStream::DegradationPreference::kMaintainResolution;
1466 if (params_.video.codec == "VP8") {
1467 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1468 vp8_settings.denoisingOn = false;
1469 vp8_settings.frameDroppingOn = false;
1470 vp8_settings.numberOfTemporalLayers =
1471 static_cast<unsigned char>(params_.video.num_temporal_layers);
1472 video_encoder_config_.encoder_specific_settings =
1473 new rtc::RefCountedObject<
1474 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1475 } else if (params_.video.codec == "VP9") {
1476 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1477 vp9_settings.denoisingOn = false;
1478 vp9_settings.frameDroppingOn = false;
1479 vp9_settings.numberOfTemporalLayers =
1480 static_cast<unsigned char>(params_.video.num_temporal_layers);
1481 vp9_settings.numberOfSpatialLayers =
1482 static_cast<unsigned char>(params_.ss.num_spatial_layers);
1483 video_encoder_config_.encoder_specific_settings =
1484 new rtc::RefCountedObject<
1485 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1486 }
1487 // Setup frame generator.
1488 const size_t kWidth = 1850;
1489 const size_t kHeight = 1110;
1490 std::vector<std::string> slides;
1491 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1492 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1493 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1494 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
ivica5d6a06c2015-09-17 05:30:24 -07001495
ilnik2a8c2f52017-02-15 02:23:28 -08001496 if (params_.screenshare.scroll_duration == 0) {
1497 // Cycle image every slide_change_interval seconds.
perkja8ba1952017-02-27 06:52:10 -08001498 frame_generator_ = test::FrameGenerator::CreateFromYuvFile(
ilnik2a8c2f52017-02-15 02:23:28 -08001499 slides, kWidth, kHeight,
perkja8ba1952017-02-27 06:52:10 -08001500 params_.screenshare.slide_change_interval * params_.video.fps);
ilnik2a8c2f52017-02-15 02:23:28 -08001501 } else {
1502 RTC_CHECK_LE(params_.video.width, kWidth);
1503 RTC_CHECK_LE(params_.video.height, kHeight);
1504 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1505 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1506 params_.screenshare.scroll_duration) *
1507 1000;
1508 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1509 params_.screenshare.slide_change_interval);
1510
perkja8ba1952017-02-27 06:52:10 -08001511 frame_generator_ = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1512 clock_, slides, kWidth, kHeight, params_.video.width,
1513 params_.video.height, params_.screenshare.scroll_duration * 1000,
1514 kPauseDurationMs);
ilnik2a8c2f52017-02-15 02:23:28 -08001515 }
1516 } else if (params_.ss.num_spatial_layers > 1) { // For non-screenshare case.
1517 RTC_CHECK(params_.video.codec == "VP9");
kthelgason29a44e32016-09-27 03:52:02 -07001518 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
kthelgason29a44e32016-09-27 03:52:02 -07001519 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001520 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001521 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001522 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001523 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1524 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001525 }
ivica5d6a06c2015-09-17 05:30:24 -07001526}
1527
ilnika014cc52017-03-07 04:21:04 -08001528void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1529 VideoStream thumbnail = DefaultThumbnailStream();
1530 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1531 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1532 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
1533 thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -08001534 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -08001535 }
1536}
1537
perkja49cbd32016-09-16 07:53:41 -07001538void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001539 if (params_.screenshare.enabled) {
1540 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja8ba1952017-02-27 06:52:10 -08001541 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator_),
minyue626bc952016-10-31 05:47:02 -07001542 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001543 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001544 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001545 } else {
sprangce4aef12015-11-02 07:23:20 -08001546 if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001547 video_capturer_.reset(test::VcmCapturer::Create(
minyue626bc952016-10-31 05:47:02 -07001548 params_.video.width, params_.video.height, params_.video.fps));
sprang1bed2e42017-01-23 08:46:51 -08001549 if (!video_capturer_) {
1550 // Failed to get actual camera, use chroma generator as backup.
1551 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
perkja8ba1952017-02-27 06:52:10 -08001552 static_cast<int>(params_.video.width),
1553 static_cast<int>(params_.video.height), params_.video.fps, clock_));
sprang1bed2e42017-01-23 08:46:51 -08001554 }
ivica5d6a06c2015-09-17 05:30:24 -07001555 } else {
minyuea27172d2016-11-01 05:59:29 -07001556 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001557 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001558 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001559 clock_));
minyuea27172d2016-11-01 05:59:29 -07001560 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1561 << params_.video.clip_name
1562 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001563 }
1564 }
sprang1bed2e42017-01-23 08:46:51 -08001565 RTC_DCHECK(video_capturer_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001566}
1567
sprang7a975f72015-10-12 06:33:21 -07001568void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001569 params_ = params;
1570
minyue626bc952016-10-31 05:47:02 -07001571 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001572 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1573 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001574 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001575
1576 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001577 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001578 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001579 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001580 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001581 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1582 << "!";
ivica87f83a92015-10-08 05:13:32 -07001583 }
sprang7a975f72015-10-12 06:33:21 -07001584
skvlad11a9cbf2016-10-07 11:53:05 -07001585 webrtc::RtcEventLogNullImpl event_log;
1586 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001587 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001588 CreateCalls(call_config, call_config);
1589
ilnik68af10d2017-03-02 04:59:33 -08001590 test::LayerFilteringTransport send_transport(
1591 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
1592 params_.video.selected_tl, params_.ss.selected_sl);
1593 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get());
stefanf116bd02015-10-27 08:29:42 -07001594
sprangce4aef12015-11-02 07:23:20 -08001595 std::string graph_title = params_.analyzer.graph_title;
1596 if (graph_title.empty())
1597 graph_title = VideoQualityTest::GenerateGraphTitle();
1598
sprangc1b57a12017-02-28 08:50:47 -08001599 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
ivica5d6a06c2015-09-17 05:30:24 -07001600 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001601 &send_transport, params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001602 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001603 is_quick_test_enabled
1604 ? kFramesSentInQuickTest
1605 : params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001606 graph_data_output_file, graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -08001607 kVideoSendSsrcs[params_.ss.selected_stream],
ilnik46a00212017-02-10 09:16:05 -08001608 kSendRtxSsrcs[params_.ss.selected_stream],
ilnikcb8c1462017-03-09 09:23:30 -08001609 static_cast<size_t>(params_.ss.selected_stream), params.ss.selected_sl,
ilnik1e7732c2017-02-23 05:07:56 -08001610 params_.video.selected_tl, is_quick_test_enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001611 analyzer.SetReceiver(receiver_call_->Receiver());
1612 send_transport.SetReceiver(&analyzer);
1613 recv_transport.SetReceiver(sender_call_->Receiver());
1614
minyuea27172d2016-11-01 05:59:29 -07001615 SetupVideo(&analyzer, &recv_transport);
ilnika014cc52017-03-07 04:21:04 -08001616 SetupThumbnails(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001617 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001618 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
Peter Boströme4499152016-02-05 11:13:28 +01001619 RTC_DCHECK(!video_send_config_.post_encode_callback);
1620 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001621
ilnik2a8c2f52017-02-15 02:23:28 -08001622 SetupScreenshareOrSVC();
ivica5d6a06c2015-09-17 05:30:24 -07001623
brandtr1293aca2016-11-16 22:47:29 -08001624 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001625 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001626 analyzer.SetSendStream(video_send_stream_);
philipelfd870db2017-01-23 03:22:15 -08001627 if (video_receive_streams_.size() == 1)
1628 analyzer.SetReceiveStream(video_receive_streams_[0]);
kthelgason2bc68642017-02-07 07:02:22 -08001629
1630 video_send_stream_->SetSource(analyzer.OutputInterface(),
1631 degradation_preference_);
ivica5d6a06c2015-09-17 05:30:24 -07001632
ilnika014cc52017-03-07 04:21:04 -08001633 SetupThumbnailCapturers(params_.num_thumbnails);
1634 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1635 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1636 degradation_preference_);
1637 }
1638
perkja49cbd32016-09-16 07:53:41 -07001639 CreateCapturer();
ilnika014cc52017-03-07 04:21:04 -08001640
perkja49cbd32016-09-16 07:53:41 -07001641 rtc::VideoSinkWants wants;
minyuea27172d2016-11-01 05:59:29 -07001642 video_capturer_->AddOrUpdateSink(analyzer.InputInterface(), wants);
ivicac1cc8542015-10-08 03:44:06 -07001643
palmkviste75f2042016-09-28 06:19:48 -07001644 StartEncodedFrameLogs(video_send_stream_);
1645 StartEncodedFrameLogs(video_receive_streams_[0]);
stefanff483612015-12-21 03:14:00 -08001646 video_send_stream_->Start();
ilnika014cc52017-03-07 04:21:04 -08001647 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1648 thumbnail_send_stream->Start();
stefanff483612015-12-21 03:14:00 -08001649 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1650 receive_stream->Start();
brandtr1293aca2016-11-16 22:47:29 -08001651 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1652 receive_stream->Start();
ilnika014cc52017-03-07 04:21:04 -08001653 for (VideoReceiveStream* thumbnail_receive_stream :
1654 thumbnail_receive_streams_)
1655 thumbnail_receive_stream->Start();
1656
ilnikdf92c5c2017-02-23 02:08:44 -08001657 analyzer.StartMeasuringCpuProcessTime();
ilnika014cc52017-03-07 04:21:04 -08001658
minyuea27172d2016-11-01 05:59:29 -07001659 video_capturer_->Start();
ilnika014cc52017-03-07 04:21:04 -08001660 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1661 thumbnail_capturers_) {
1662 video_caputurer->Start();
1663 }
ivica5d6a06c2015-09-17 05:30:24 -07001664
1665 analyzer.Wait();
1666
1667 send_transport.StopSending();
1668 recv_transport.StopSending();
1669
ilnika014cc52017-03-07 04:21:04 -08001670 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1671 thumbnail_capturers_)
1672 video_caputurer->Stop();
minyuea27172d2016-11-01 05:59:29 -07001673 video_capturer_->Stop();
ilnika014cc52017-03-07 04:21:04 -08001674 for (VideoReceiveStream* thumbnail_receive_stream :
1675 thumbnail_receive_streams_)
1676 thumbnail_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001677 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1678 receive_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001679 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1680 receive_stream->Stop();
ilnika014cc52017-03-07 04:21:04 -08001681 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1682 thumbnail_send_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001683 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001684
1685 DestroyStreams();
ilnika014cc52017-03-07 04:21:04 -08001686 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -07001687
1688 if (graph_data_output_file)
1689 fclose(graph_data_output_file);
1690}
1691
minyuea27172d2016-11-01 05:59:29 -07001692void VideoQualityTest::SetupAudio(int send_channel_id,
1693 int receive_channel_id,
1694 Call* call,
1695 Transport* transport,
1696 AudioReceiveStream** audio_receive_stream) {
1697 audio_send_config_ = AudioSendStream::Config(transport);
1698 audio_send_config_.voe_channel_id = send_channel_id;
1699 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1700
1701 // Add extension to enable audio send side BWE, and allow audio bit rate
1702 // adaptation.
1703 audio_send_config_.rtp.extensions.clear();
1704 if (params_.call.send_side_bwe) {
1705 audio_send_config_.rtp.extensions.push_back(
1706 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1707 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001708 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1709 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001710 }
1711 audio_send_config_.send_codec_spec.codec_inst =
1712 CodecInst{120, "OPUS", 48000, 960, 2, 64000};
minyue4c8b9422017-03-21 04:11:43 -07001713 audio_send_config_.send_codec_spec.enable_opus_dtx = params_.audio.dtx;
minyuea27172d2016-11-01 05:59:29 -07001714 audio_send_stream_ = call->CreateAudioSendStream(audio_send_config_);
1715
1716 AudioReceiveStream::Config audio_config;
1717 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1718 audio_config.rtcp_send_transport = transport;
1719 audio_config.voe_channel_id = receive_channel_id;
1720 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1721 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1722 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1723 audio_config.decoder_factory = decoder_factory_;
1724 if (params_.video.enabled && params_.audio.sync_video)
1725 audio_config.sync_group = kSyncGroup;
1726
1727 *audio_receive_stream = call->CreateAudioReceiveStream(audio_config);
1728}
1729
minyue73208662016-08-18 06:28:55 -07001730void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001731 params_ = params;
1732 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001733
ivica5d6a06c2015-09-17 05:30:24 -07001734 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1735 // match the full stack tests.
skvlad11a9cbf2016-10-07 11:53:05 -07001736 webrtc::RtcEventLogNullImpl event_log;
1737 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001738 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001739
1740 ::VoiceEngineState voe;
minyue626bc952016-10-31 05:47:02 -07001741 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001742 CreateVoiceEngine(&voe, decoder_factory_);
1743 AudioState::Config audio_state_config;
1744 audio_state_config.voice_engine = voe.voice_engine;
aleloi10111bc2016-11-17 06:48:48 -08001745 audio_state_config.audio_mixer = AudioMixerImpl::Create();
minyue73208662016-08-18 06:28:55 -07001746 call_config.audio_state = AudioState::Create(audio_state_config);
1747 }
1748
kwiberg27f982b2016-03-01 11:52:33 -08001749 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001750
minyuea27172d2016-11-01 05:59:29 -07001751 // TODO(minyue): consider if this is a good transport even for audio only
1752 // calls.
ivica5d6a06c2015-09-17 05:30:24 -07001753 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001754 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
ilnik68af10d2017-03-02 04:59:33 -08001755 params.video.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001756 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1757 // least share as much code as possible. That way this test would also match
1758 // the full stack tests better.
1759 transport.SetReceiver(call->Receiver());
1760
minyuea27172d2016-11-01 05:59:29 -07001761 VideoReceiveStream* video_receive_stream = nullptr;
brandtr1293aca2016-11-16 22:47:29 -08001762 FlexfecReceiveStream* flexfec_receive_stream = nullptr;
minyuea27172d2016-11-01 05:59:29 -07001763 std::unique_ptr<test::VideoRenderer> local_preview;
1764 std::unique_ptr<test::VideoRenderer> loopback_video;
1765 if (params_.video.enabled) {
1766 // Create video renderers.
1767 local_preview.reset(test::VideoRenderer::Create(
1768 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 05:13:32 -07001769
minyuea27172d2016-11-01 05:59:29 -07001770 size_t stream_id = params_.ss.selected_stream;
1771 std::string title = "Loopback Video";
1772 if (params_.ss.streams.size() > 1) {
1773 std::ostringstream s;
1774 s << stream_id;
1775 title += " - Stream #" + s.str();
1776 }
sprangce4aef12015-11-02 07:23:20 -08001777
minyuea27172d2016-11-01 05:59:29 -07001778 loopback_video.reset(test::VideoRenderer::Create(
1779 title.c_str(), params_.ss.streams[stream_id].width,
1780 params_.ss.streams[stream_id].height));
mflodman48a4beb2016-07-01 13:03:59 +02001781
minyuea27172d2016-11-01 05:59:29 -07001782 SetupVideo(&transport, &transport);
minyuea27172d2016-11-01 05:59:29 -07001783 video_send_config_.pre_encode_callback = local_preview.get();
1784 video_receive_configs_[stream_id].renderer = loopback_video.get();
1785 if (params_.audio.enabled && params_.audio.sync_video)
1786 video_receive_configs_[stream_id].sync_group = kSyncGroup;
1787
ilnik68af10d2017-03-02 04:59:33 -08001788 if (params_.screenshare.enabled)
1789 SetupScreenshareOrSVC();
minyuea27172d2016-11-01 05:59:29 -07001790
1791 video_send_stream_ = call->CreateVideoSendStream(
1792 video_send_config_.Copy(), video_encoder_config_.Copy());
brandtr1293aca2016-11-16 22:47:29 -08001793 if (params_.video.flexfec) {
1794 RTC_DCHECK(!flexfec_receive_configs_.empty());
1795 flexfec_receive_stream =
1796 call->CreateFlexfecReceiveStream(flexfec_receive_configs_[0]);
1797 }
minyuea27172d2016-11-01 05:59:29 -07001798 video_receive_stream = call->CreateVideoReceiveStream(
1799 video_receive_configs_[stream_id].Copy());
1800 CreateCapturer();
kthelgason2bc68642017-02-07 07:02:22 -08001801 video_send_stream_->SetSource(video_capturer_.get(),
1802 degradation_preference_);
philipel274c1dc2016-05-04 06:21:01 -07001803 }
1804
minyue73208662016-08-18 06:28:55 -07001805 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 05:47:02 -07001806 if (params_.audio.enabled) {
minyuea27172d2016-11-01 05:59:29 -07001807 SetupAudio(voe.send_channel_id, voe.receive_channel_id, call.get(),
1808 &transport, &audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001809 }
1810
palmkviste75f2042016-09-28 06:19:48 -07001811 StartEncodedFrameLogs(video_receive_stream);
1812 StartEncodedFrameLogs(video_send_stream_);
1813
minyue73208662016-08-18 06:28:55 -07001814 // Start sending and receiving video.
minyuea27172d2016-11-01 05:59:29 -07001815 if (params_.video.enabled) {
brandtr1293aca2016-11-16 22:47:29 -08001816 if (flexfec_receive_stream)
1817 flexfec_receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001818 video_receive_stream->Start();
1819 video_send_stream_->Start();
1820 video_capturer_->Start();
1821 }
ivica5d6a06c2015-09-17 05:30:24 -07001822
minyue626bc952016-10-31 05:47:02 -07001823 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001824 // Start receiving audio.
1825 audio_receive_stream->Start();
1826 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 06:28:55 -07001827
1828 // Start sending audio.
1829 audio_send_stream_->Start();
1830 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1831 }
1832
ivica5d6a06c2015-09-17 05:30:24 -07001833 test::PressEnterToContinue();
1834
minyue626bc952016-10-31 05:47:02 -07001835 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001836 // Stop sending audio.
1837 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1838 audio_send_stream_->Stop();
1839
1840 // Stop receiving audio.
minyue73208662016-08-18 06:28:55 -07001841 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
1842 audio_receive_stream->Stop();
minyuea27172d2016-11-01 05:59:29 -07001843 call->DestroyAudioSendStream(audio_send_stream_);
1844 call->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001845 }
1846
1847 // Stop receiving and sending video.
minyuea27172d2016-11-01 05:59:29 -07001848 if (params_.video.enabled) {
1849 video_capturer_->Stop();
1850 video_send_stream_->Stop();
1851 video_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001852 if (flexfec_receive_stream) {
1853 flexfec_receive_stream->Stop();
1854 call->DestroyFlexfecReceiveStream(flexfec_receive_stream);
1855 }
minyuea27172d2016-11-01 05:59:29 -07001856 call->DestroyVideoReceiveStream(video_receive_stream);
1857 call->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 06:28:55 -07001858 }
1859
ivica5d6a06c2015-09-17 05:30:24 -07001860 transport.StopSending();
minyue626bc952016-10-31 05:47:02 -07001861 if (params_.audio.enabled)
minyue73208662016-08-18 06:28:55 -07001862 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07001863}
1864
palmkviste75f2042016-09-28 06:19:48 -07001865void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001866 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001867 std::ostringstream str;
1868 str << send_logs_++;
1869 std::string prefix =
minyue626bc952016-10-31 05:47:02 -07001870 params_.video.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07001871 stream->EnableEncodedFrameRecording(
1872 std::vector<rtc::PlatformFile>(
1873 {rtc::CreatePlatformFile(prefix + "1.ivf"),
1874 rtc::CreatePlatformFile(prefix + "2.ivf"),
1875 rtc::CreatePlatformFile(prefix + "3.ivf")}),
1876 10000000);
1877 }
1878}
ilnikcb8c1462017-03-09 09:23:30 -08001879
palmkviste75f2042016-09-28 06:19:48 -07001880void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001881 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001882 std::ostringstream str;
1883 str << receive_logs_++;
1884 std::string path =
minyue626bc952016-10-31 05:47:02 -07001885 params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07001886 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
1887 10000000);
1888 }
1889}
ivica5d6a06c2015-09-17 05:30:24 -07001890} // namespace webrtc