blob: 77c6829898f4034d03cfe2ee6c0584ad95b785cd [file] [log] [blame]
ivica5d6a06c2015-09-17 05:30:24 -07001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "video/video_quality_test.h"
perkj9fdbda62016-09-15 09:19:20 -070011
perkja49cbd32016-09-16 07:53:41 -070012#include <stdio.h>
ivica5d6a06c2015-09-17 05:30:24 -070013#include <algorithm>
14#include <deque>
15#include <map>
sprangce4aef12015-11-02 07:23:20 -080016#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080017#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070018#include <vector>
19
Elad Alon83ccca12017-10-04 13:18:26 +020020#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Magnus Jedvert46a27652017-11-13 14:10:02 +010021#include "media/engine/internalencoderfactory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "media/engine/webrtcvideoengine.h"
23#include "modules/audio_mixer/audio_mixer_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/rtp_rtcp/source/rtp_format.h"
25#include "modules/rtp_rtcp/source/rtp_utility.h"
26#include "modules/video_coding/codecs/h264/include/h264.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/video_coding/codecs/vp8/include/vp8_common_types.h"
28#include "modules/video_coding/codecs/vp9/include/vp9.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/cpu_time.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/flags.h"
31#include "rtc_base/format_macros.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/memory_usage.h"
33#include "rtc_base/pathutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "system_wrappers/include/cpu_info.h"
Mirko Bonadei5f99e652017-12-19 13:40:40 +010035#include "test/rtp_file_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "test/run_loop.h"
37#include "test/statistics.h"
38#include "test/testsupport/fileutils.h"
39#include "test/testsupport/frame_writer.h"
Edward Lemurd1d67432018-01-02 11:54:37 +010040#include "test/testsupport/perf_test.h"
Edward Lemuraf8659a2017-09-27 14:46:24 +020041#include "test/testsupport/test_artifacts.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/vcm_capturer.h"
43#include "test/video_renderer.h"
44#include "voice_engine/include/voe_base.h"
minyue73208662016-08-18 06:28:55 -070045
ilnikee42d192017-08-22 07:16:20 -070046DEFINE_bool(save_worst_frame,
47 false,
48 "Enable saving a frame with the lowest PSNR to a jpeg file in the "
Edward Lemuraf8659a2017-09-27 14:46:24 +020049 "test_artifacts_dir");
ilnikee42d192017-08-22 07:16:20 -070050
minyue73208662016-08-18 06:28:55 -070051namespace {
52
53constexpr int kSendStatsPollingIntervalMs = 1000;
minyue20c84cc2017-04-10 16:57:57 -070054
minyue73208662016-08-18 06:28:55 -070055constexpr size_t kMaxComparisons = 10;
56constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080057constexpr int kOpusMinBitrateBps = 6000;
58constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 00:53:12 -080059constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 04:21:04 -080060constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
61constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 06:28:55 -070062
sprang1168fd42017-06-21 09:00:17 -070063constexpr int kDefaultMaxQp = cricket::WebRtcVideoChannel::kDefaultQpMax;
64
minyue73208662016-08-18 06:28:55 -070065struct 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
peaha9cc40b2017-06-29 08:32:09 -070078void CreateVoiceEngine(
79 VoiceEngineState* voe,
Fredrik Solenbergd3195342017-11-21 20:33:05 +010080 webrtc::AudioDeviceModule* adm,
peaha9cc40b2017-06-29 08:32:09 -070081 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory) {
minyue73208662016-08-18 06:28:55 -070082 voe->voice_engine = webrtc::VoiceEngine::Create();
83 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
Fredrik Solenbergd3195342017-11-21 20:33:05 +010084 EXPECT_EQ(0, adm->Init());
Fredrik Solenberg2a877972017-12-15 16:42:15 +010085 EXPECT_EQ(0, voe->base->Init(adm, nullptr, decoder_factory));
solenberg88499ec2016-09-07 07:34:41 -070086 webrtc::VoEBase::ChannelConfig config;
87 config.enable_voice_pacing = true;
88 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 06:28:55 -070089 EXPECT_GE(voe->send_channel_id, 0);
90 voe->receive_channel_id = voe->base->CreateChannel();
91 EXPECT_GE(voe->receive_channel_id, 0);
92}
93
94void DestroyVoiceEngine(VoiceEngineState* voe) {
95 voe->base->DeleteChannel(voe->send_channel_id);
96 voe->send_channel_id = -1;
97 voe->base->DeleteChannel(voe->receive_channel_id);
98 voe->receive_channel_id = -1;
99 voe->base->Release();
100 voe->base = nullptr;
minyue73208662016-08-18 06:28:55 -0700101
102 webrtc::VoiceEngine::Delete(voe->voice_engine);
103 voe->voice_engine = nullptr;
104}
105
perkjfa10b552016-10-02 23:45:26 -0700106class VideoStreamFactory
107 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
108 public:
109 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
110 : streams_(streams) {}
111
112 private:
113 std::vector<webrtc::VideoStream> CreateEncoderStreams(
114 int width,
115 int height,
116 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -0800117 // The highest layer must match the incoming resolution.
118 std::vector<webrtc::VideoStream> streams = streams_;
119 streams[streams_.size() - 1].height = height;
120 streams[streams_.size() - 1].width = width;
121 return streams;
perkjfa10b552016-10-02 23:45:26 -0700122 }
123
124 std::vector<webrtc::VideoStream> streams_;
125};
126
brandtr504b95e2016-12-21 02:54:35 -0800127bool IsFlexfec(int payload_type) {
128 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
129}
130
minyue73208662016-08-18 06:28:55 -0700131} // namespace
ivica5d6a06c2015-09-17 05:30:24 -0700132
133namespace webrtc {
134
ivica5d6a06c2015-09-17 05:30:24 -0700135class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -0700136 public Transport,
ilnik1e7732c2017-02-23 05:07:56 -0800137 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 05:30:24 -0700138 public:
sprangce4aef12015-11-02 07:23:20 -0800139 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700140 const std::string& test_label,
141 double avg_psnr_threshold,
142 double avg_ssim_threshold,
143 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800144 FILE* graph_data_output_file,
145 const std::string& graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -0800146 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 09:16:05 -0800147 uint32_t rtx_ssrc_to_analyze,
ilnikcb8c1462017-03-09 09:23:30 -0800148 size_t selected_stream,
ilnik1e7732c2017-02-23 05:07:56 -0800149 int selected_sl,
150 int selected_tl,
ilnik6b826ef2017-06-16 06:53:48 -0700151 bool is_quick_test_enabled,
ilnik98436952017-07-13 00:47:03 -0700152 Clock* clock,
153 std::string rtp_dump_name)
perkja49cbd32016-09-16 07:53:41 -0700154 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700155 receiver_(nullptr),
stefan889d9652017-07-05 03:03:02 -0700156 call_(nullptr),
ivica5d6a06c2015-09-17 05:30:24 -0700157 send_stream_(nullptr),
philipelfd870db2017-01-23 03:22:15 -0800158 receive_stream_(nullptr),
ilnik6b826ef2017-06-16 06:53:48 -0700159 captured_frame_forwarder_(this, clock),
ivica5d6a06c2015-09-17 05:30:24 -0700160 test_label_(test_label),
161 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800162 graph_title_(graph_title),
163 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 09:16:05 -0800164 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 09:23:30 -0800165 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 05:07:56 -0800166 selected_sl_(selected_sl),
167 selected_tl_(selected_tl),
pbos14fe7082016-04-20 06:35:56 -0700168 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100169 encode_timing_proxy_(this),
stefan889d9652017-07-05 03:03:02 -0700170 last_fec_bytes_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700171 frames_to_process_(duration_frames),
172 frames_recorded_(0),
173 frames_processed_(0),
174 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700175 dropped_frames_before_first_encode_(0),
176 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700177 last_render_time_(0),
178 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 05:07:56 -0800179 total_media_bytes_(0),
180 first_sending_time_(0),
181 last_sending_time_(0),
ilnikdf92c5c2017-02-23 02:08:44 -0800182 cpu_time_(0),
183 wallclock_time_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700184 avg_psnr_threshold_(avg_psnr_threshold),
185 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 00:53:12 -0800186 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100187 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100188 comparison_available_event_(false, false),
ilnik98436952017-07-13 00:47:03 -0700189 done_(true, false),
190 clock_(clock),
191 start_ms_(clock->TimeInMilliseconds()) {
ivica5d6a06c2015-09-17 05:30:24 -0700192 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
193
194 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
195 // so that we don't accidentally starve "real" worker threads (codec etc).
196 // Also, don't allocate more than kMaxComparisonThreads, even if there are
197 // spare cores.
198
199 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800200 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700201 static const uint32_t kMinCoresLeft = 4;
202 static const uint32_t kMaxComparisonThreads = 8;
203
204 if (num_cores <= kMinCoresLeft) {
205 num_cores = 1;
206 } else {
207 num_cores -= kMinCoresLeft;
208 num_cores = std::min(num_cores, kMaxComparisonThreads);
209 }
210
211 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100212 rtc::PlatformThread* thread =
213 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
214 thread->Start();
215 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700216 }
ilnik98436952017-07-13 00:47:03 -0700217
218 if (!rtp_dump_name.empty()) {
219 fprintf(stdout, "Writing rtp dump to %s\n", rtp_dump_name.c_str());
220 rtp_file_writer_.reset(test::RtpFileWriter::Create(
221 test::RtpFileWriter::kRtpDump, rtp_dump_name));
222 }
ivica5d6a06c2015-09-17 05:30:24 -0700223 }
224
225 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100226 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
227 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700228 delete thread;
229 }
230 }
231
232 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
233
ilnik6b826ef2017-06-16 06:53:48 -0700234 void SetSource(test::VideoCapturer* video_capturer, bool respect_sink_wants) {
235 if (respect_sink_wants)
236 captured_frame_forwarder_.SetSource(video_capturer);
237 rtc::VideoSinkWants wants;
238 video_capturer->AddOrUpdateSink(InputInterface(), wants);
239 }
240
stefan889d9652017-07-05 03:03:02 -0700241 void SetCall(Call* call) {
242 rtc::CritScope lock(&crit_);
243 RTC_DCHECK(!call_);
244 call_ = call;
245 }
246
perkja49cbd32016-09-16 07:53:41 -0700247 void SetSendStream(VideoSendStream* stream) {
248 rtc::CritScope lock(&crit_);
249 RTC_DCHECK(!send_stream_);
250 send_stream_ = stream;
251 }
252
philipelfd870db2017-01-23 03:22:15 -0800253 void SetReceiveStream(VideoReceiveStream* stream) {
254 rtc::CritScope lock(&crit_);
255 RTC_DCHECK(!receive_stream_);
256 receive_stream_ = stream;
257 }
258
perkja49cbd32016-09-16 07:53:41 -0700259 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
260 return &captured_frame_forwarder_;
261 }
262 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
263 return &captured_frame_forwarder_;
264 }
265
ivica5d6a06c2015-09-17 05:30:24 -0700266 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100267 rtc::CopyOnWriteBuffer packet,
ivica5d6a06c2015-09-17 05:30:24 -0700268 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700269 // Ignore timestamps of RTCP packets. They're not synchronized with
270 // RTP packet timestamps and so they would confuse wrap_handler_.
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100271 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size())) {
272 return receiver_->DeliverPacket(media_type, std::move(packet),
273 packet_time);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700274 }
ilnik98436952017-07-13 00:47:03 -0700275
276 if (rtp_file_writer_) {
277 test::RtpPacket p;
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100278 memcpy(p.data, packet.cdata(), packet.size());
279 p.length = packet.size();
280 p.original_length = packet.size();
ilnik98436952017-07-13 00:47:03 -0700281 p.time_ms = clock_->TimeInMilliseconds() - start_ms_;
282 rtp_file_writer_->WritePacket(&p);
283 }
284
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100285 RtpUtility::RtpHeaderParser parser(packet.cdata(), packet.size());
ivica5d6a06c2015-09-17 05:30:24 -0700286 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800287 parser.Parse(&header);
ilnik46a00212017-02-10 09:16:05 -0800288 if (!IsFlexfec(header.payloadType) &&
289 (header.ssrc == ssrc_to_analyze_ ||
290 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 02:54:35 -0800291 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
292 // (FlexFEC and media are sent on different SSRCs, which have different
293 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800294 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 05:30:24 -0700295 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800296 int64_t timestamp =
297 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
298 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700299 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
300 }
301
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100302 return receiver_->DeliverPacket(media_type, std::move(packet), packet_time);
ivica5d6a06c2015-09-17 05:30:24 -0700303 }
304
pbos14fe7082016-04-20 06:35:56 -0700305 void PreEncodeOnFrame(const VideoFrame& video_frame) {
306 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800307 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 06:35:56 -0700308 while (frames_.front().timestamp() != video_frame.timestamp()) {
309 ++dropped_frames_before_first_encode_;
310 frames_.pop_front();
311 RTC_CHECK(!frames_.empty());
312 }
ilnik3dd5ad92017-02-09 04:58:53 -0800313 first_encoded_timestamp_ =
314 rtc::Optional<uint32_t>(video_frame.timestamp());
315 }
316 }
317
318 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
319 rtc::CritScope lock(&crit_);
320 if (!first_sent_timestamp_ &&
ilnikcb8c1462017-03-09 09:23:30 -0800321 encoded_frame.stream_id_ == selected_stream_) {
ilnik3dd5ad92017-02-09 04:58:53 -0800322 first_sent_timestamp_ = rtc::Optional<uint32_t>(encoded_frame.timestamp_);
pbos14fe7082016-04-20 06:35:56 -0700323 }
324 }
325
stefan1d8a5062015-10-02 03:39:33 -0700326 bool SendRtp(const uint8_t* packet,
327 size_t length,
328 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800329 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700330 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800331 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700332
sprangce4aef12015-11-02 07:23:20 -0800333 int64_t current_time =
334 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 02:23:28 -0800335
sprangce4aef12015-11-02 07:23:20 -0800336 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700337 {
338 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800339 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 08:32:53 -0800340 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 04:58:53 -0800341 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 04:08:56 -0800342 }
ilnik3dd5ad92017-02-09 04:58:53 -0800343
344 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 02:54:35 -0800345 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
346 // (FlexFEC and media are sent on different SSRCs, which have different
347 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800348 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 02:54:35 -0800349 int64_t timestamp =
350 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
351 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 05:07:56 -0800352
353 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 02:54:35 -0800354 encoded_frame_sizes_[timestamp] +=
355 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 05:07:56 -0800356 total_media_bytes_ +=
357 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 02:54:35 -0800358 }
ilnik1e7732c2017-02-23 05:07:56 -0800359 if (first_sending_time_ == 0)
360 first_sending_time_ = current_time;
361 last_sending_time_ = current_time;
sprangce4aef12015-11-02 07:23:20 -0800362 }
ivica5d6a06c2015-09-17 05:30:24 -0700363 }
sprangce4aef12015-11-02 07:23:20 -0800364 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700365 }
366
367 bool SendRtcp(const uint8_t* packet, size_t length) override {
368 return transport_->SendRtcp(packet, length);
369 }
370
nisseeb83a1a2016-03-21 01:27:56 -0700371 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700372 int64_t render_time_ms =
373 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700374
375 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 02:08:44 -0800376
377 StartExcludingCpuThreadTime();
378
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700379 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800380 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700381
sprang16daaa52016-03-09 01:30:24 -0800382 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700383 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700384 // No previous frame rendered, this one was dropped after sending but
385 // before rendering.
386 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 07:02:22 -0800387 } else {
388 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
389 render_time_ms);
pbos14fe7082016-04-20 06:35:56 -0700390 }
ivica5d6a06c2015-09-17 05:30:24 -0700391 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700392 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700393 }
394
395 VideoFrame reference_frame = frames_.front();
396 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800397 int64_t reference_timestamp =
398 wrap_handler_.Unwrap(reference_frame.timestamp());
399 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800400 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100401 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800402 ++send_timestamp;
403 }
sprang16daaa52016-03-09 01:30:24 -0800404 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700405
406 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
407
nisse97f0b932016-05-26 09:44:40 -0700408 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ilnikdf92c5c2017-02-23 02:08:44 -0800409
410 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 05:30:24 -0700411 }
412
ivica5d6a06c2015-09-17 05:30:24 -0700413 void Wait() {
414 // Frame comparisons can be very expensive. Wait for test to be done, but
415 // at time-out check if frames_processed is going up. If so, give it more
416 // time, otherwise fail. Hopefully this will reduce test flakiness.
417
Peter Boström8c38e8b2015-11-26 17:45:47 +0100418 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800419
ivica5d6a06c2015-09-17 05:30:24 -0700420 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700421 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100422 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700423 int frames_processed;
424 {
425 rtc::CritScope crit(&comparison_lock_);
426 frames_processed = frames_processed_;
427 }
428
429 // Print some output so test infrastructure won't think we've crashed.
430 const char* kKeepAliveMessages[3] = {
431 "Uh, I'm-I'm not quite dead, sir.",
432 "Uh, I-I think uh, I could pull through, sir.",
433 "Actually, I think I'm all right to come with you--"};
434 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
435
436 if (last_frames_processed == -1) {
437 last_frames_processed = frames_processed;
438 continue;
439 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100440 if (frames_processed == last_frames_processed) {
441 EXPECT_GT(frames_processed, last_frames_processed)
442 << "Analyzer stalled while waiting for test to finish.";
443 done_.Set();
444 break;
445 }
ivica5d6a06c2015-09-17 05:30:24 -0700446 last_frames_processed = frames_processed;
447 }
448
449 if (iteration > 0)
450 printf("- Farewell, sweet Concorde!\n");
451
Peter Boström8c38e8b2015-11-26 17:45:47 +0100452 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700453 }
454
pbos14fe7082016-04-20 06:35:56 -0700455 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
456 return &pre_encode_proxy_;
457 }
Peter Boströme4499152016-02-05 11:13:28 +0100458 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
459
ilnikdf92c5c2017-02-23 02:08:44 -0800460 void StartMeasuringCpuProcessTime() {
461 rtc::CritScope lock(&cpu_measurement_lock_);
462 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
463 wallclock_time_ -= rtc::SystemTimeNanos();
464 }
465
466 void StopMeasuringCpuProcessTime() {
467 rtc::CritScope lock(&cpu_measurement_lock_);
468 cpu_time_ += rtc::GetProcessCpuTimeNanos();
469 wallclock_time_ += rtc::SystemTimeNanos();
470 }
471
472 void StartExcludingCpuThreadTime() {
473 rtc::CritScope lock(&cpu_measurement_lock_);
474 cpu_time_ += rtc::GetThreadCpuTimeNanos();
475 }
476
477 void StopExcludingCpuThreadTime() {
478 rtc::CritScope lock(&cpu_measurement_lock_);
479 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
480 }
481
482 double GetCpuUsagePercent() {
483 rtc::CritScope lock(&cpu_measurement_lock_);
484 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
485 }
486
sprangce4aef12015-11-02 07:23:20 -0800487 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700488 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700489
490 private:
491 struct FrameComparison {
492 FrameComparison()
493 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800494 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700495 send_time_ms(0),
496 recv_time_ms(0),
497 render_time_ms(0),
498 encoded_frame_size(0) {}
499
500 FrameComparison(const VideoFrame& reference,
501 const VideoFrame& render,
502 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800503 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700504 int64_t send_time_ms,
505 int64_t recv_time_ms,
506 int64_t render_time_ms,
507 size_t encoded_frame_size)
508 : reference(reference),
509 render(render),
510 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800511 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700512 send_time_ms(send_time_ms),
513 recv_time_ms(recv_time_ms),
514 render_time_ms(render_time_ms),
515 encoded_frame_size(encoded_frame_size) {}
516
nissedf2ceb82016-12-15 06:29:53 -0800517 FrameComparison(bool dropped,
518 int64_t input_time_ms,
519 int64_t send_time_ms,
520 int64_t recv_time_ms,
521 int64_t render_time_ms,
522 size_t encoded_frame_size)
523 : dropped(dropped),
524 input_time_ms(input_time_ms),
525 send_time_ms(send_time_ms),
526 recv_time_ms(recv_time_ms),
527 render_time_ms(render_time_ms),
528 encoded_frame_size(encoded_frame_size) {}
529
530 rtc::Optional<VideoFrame> reference;
531 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700532 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800533 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700534 int64_t send_time_ms;
535 int64_t recv_time_ms;
536 int64_t render_time_ms;
537 size_t encoded_frame_size;
538 };
539
540 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700541 Sample(int dropped,
542 int64_t input_time_ms,
543 int64_t send_time_ms,
544 int64_t recv_time_ms,
545 int64_t render_time_ms,
546 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700547 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700548 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700549 : dropped(dropped),
550 input_time_ms(input_time_ms),
551 send_time_ms(send_time_ms),
552 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700553 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700554 encoded_frame_size(encoded_frame_size),
555 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700556 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700557
ivica8d15bd62015-10-07 02:43:12 -0700558 int dropped;
559 int64_t input_time_ms;
560 int64_t send_time_ms;
561 int64_t recv_time_ms;
562 int64_t render_time_ms;
563 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700564 double psnr;
565 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700566 };
567
Peter Boströme4499152016-02-05 11:13:28 +0100568 // This class receives the send-side OnEncodeTiming and is provided to not
569 // conflict with the receiver-side pre_decode_callback.
570 class OnEncodeTimingProxy : public EncodedFrameObserver {
571 public:
572 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
573
ilnik3dd5ad92017-02-09 04:58:53 -0800574 void EncodedFrameCallback(const EncodedFrame& frame) override {
575 parent_->PostEncodeFrameCallback(frame);
576 }
Peter Boströme4499152016-02-05 11:13:28 +0100577
578 private:
579 VideoAnalyzer* const parent_;
580 };
581
pbos14fe7082016-04-20 06:35:56 -0700582 // This class receives the send-side OnFrame callback and is provided to not
583 // conflict with the receiver-side renderer callback.
584 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
585 public:
586 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
587
588 void OnFrame(const VideoFrame& video_frame) override {
589 parent_->PreEncodeOnFrame(video_frame);
590 }
591
592 private:
593 VideoAnalyzer* const parent_;
594 };
595
ilnik1e7732c2017-02-23 05:07:56 -0800596 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
597 size_t length,
598 const RTPHeader& header) {
ilnik863f03b2017-07-11 02:38:36 -0700599 if (header.payloadType != test::CallTest::kPayloadTypeVP9 &&
600 header.payloadType != test::CallTest::kPayloadTypeVP8) {
ilnik1e7732c2017-02-23 05:07:56 -0800601 return true;
602 } else {
603 // Get VP8 and VP9 specific header to check layers indexes.
604 const uint8_t* payload = packet + header.headerLength;
605 const size_t payload_length = length - header.headerLength;
606 const size_t payload_data_length = payload_length - header.paddingLength;
ilnik863f03b2017-07-11 02:38:36 -0700607 const bool is_vp8 = header.payloadType == test::CallTest::kPayloadTypeVP8;
ilnik1e7732c2017-02-23 05:07:56 -0800608 std::unique_ptr<RtpDepacketizer> depacketizer(
609 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
610 RtpDepacketizer::ParsedPayload parsed_payload;
611 bool result =
612 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
613 RTC_DCHECK(result);
614 const int temporal_idx = static_cast<int>(
615 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
616 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
617 const int spatial_idx = static_cast<int>(
618 is_vp8 ? kNoSpatialIdx
619 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
620 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
621 temporal_idx <= selected_tl_) &&
622 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
623 spatial_idx <= selected_sl_);
624 }
625 }
626
ivica5d6a06c2015-09-17 05:30:24 -0700627 void AddFrameComparison(const VideoFrame& reference,
628 const VideoFrame& render,
629 bool dropped,
630 int64_t render_time_ms)
danilchapa37de392017-09-09 04:17:22 -0700631 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800632 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
633 int64_t send_time_ms = send_times_[reference_timestamp];
634 send_times_.erase(reference_timestamp);
635 int64_t recv_time_ms = recv_times_[reference_timestamp];
636 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700637
sprangce4aef12015-11-02 07:23:20 -0800638 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800639 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800640 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800641 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800642 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
643 if (it != encoded_frame_sizes_.end())
644 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700645
ivica5d6a06c2015-09-17 05:30:24 -0700646 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700647 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800648 comparisons_.push_back(FrameComparison(reference, render, dropped,
649 reference.ntp_time_ms(),
650 send_time_ms, recv_time_ms,
651 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700652 } else {
nissedf2ceb82016-12-15 06:29:53 -0800653 comparisons_.push_back(FrameComparison(dropped,
654 reference.ntp_time_ms(),
655 send_time_ms, recv_time_ms,
656 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700657 }
Peter Boström5811a392015-12-10 13:02:50 +0100658 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700659 }
660
tommi0f8b4032017-02-22 11:22:05 -0800661 static void PollStatsThread(void* obj) {
662 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 05:30:24 -0700663 }
664
tommi0f8b4032017-02-22 11:22:05 -0800665 void PollStats() {
666 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
667 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700668
stefan889d9652017-07-05 03:03:02 -0700669 Call::Stats call_stats = call_->GetStats();
670 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
671
tommi0f8b4032017-02-22 11:22:05 -0800672 VideoSendStream::Stats send_stats = send_stream_->GetStats();
673 // It's not certain that we yet have estimates for any of these stats.
674 // Check that they are positive before mixing them in.
675 if (send_stats.encode_frame_rate > 0)
676 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
677 if (send_stats.avg_encode_time_ms > 0)
678 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
679 if (send_stats.encode_usage_percent > 0)
680 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
681 if (send_stats.media_bitrate_bps > 0)
682 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
stefan889d9652017-07-05 03:03:02 -0700683 size_t fec_bytes = 0;
684 for (auto kv : send_stats.substreams) {
685 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
686 kv.second.rtp_stats.fec.padding_bytes;
687 }
688 fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8);
689 last_fec_bytes_ = fec_bytes;
philipelfd870db2017-01-23 03:22:15 -0800690
tommi0f8b4032017-02-22 11:22:05 -0800691 if (receive_stream_ != nullptr) {
692 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
693 if (receive_stats.decode_ms > 0)
694 decode_time_ms_.AddSample(receive_stats.decode_ms);
695 if (receive_stats.max_decode_ms > 0)
696 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
697 }
ilnikdaa574d2017-03-01 06:46:05 -0800698
699 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 03:22:15 -0800700 }
ivica5d6a06c2015-09-17 05:30:24 -0700701 }
702
703 static bool FrameComparisonThread(void* obj) {
704 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
705 }
706
707 bool CompareFrames() {
708 if (AllFramesRecorded())
709 return false;
710
ivica5d6a06c2015-09-17 05:30:24 -0700711 FrameComparison comparison;
712
713 if (!PopComparison(&comparison)) {
714 // Wait until new comparison task is available, or test is done.
715 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100716 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700717 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100718 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700719 return false;
720 }
721 return true; // Try again.
722 }
723
ilnikdf92c5c2017-02-23 02:08:44 -0800724 StartExcludingCpuThreadTime();
725
ivica5d6a06c2015-09-17 05:30:24 -0700726 PerformFrameComparison(comparison);
727
ilnikdf92c5c2017-02-23 02:08:44 -0800728 StopExcludingCpuThreadTime();
729
ivica5d6a06c2015-09-17 05:30:24 -0700730 if (FrameProcessed()) {
731 PrintResults();
732 if (graph_data_output_file_)
733 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100734 done_.Set();
735 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700736 return false;
737 }
738
739 return true;
740 }
741
742 bool PopComparison(FrameComparison* comparison) {
743 rtc::CritScope crit(&comparison_lock_);
744 // If AllFramesRecorded() is true, it means we have already popped
745 // frames_to_process_ frames from comparisons_, so there is no more work
746 // for this thread to be done. frames_processed_ might still be lower if
747 // all comparisons are not done, but those frames are currently being
748 // worked on by other threads.
749 if (comparisons_.empty() || AllFramesRecorded())
750 return false;
751
752 *comparison = comparisons_.front();
753 comparisons_.pop_front();
754
755 FrameRecorded();
756 return true;
757 }
758
759 // Increment counter for number of frames received for comparison.
760 void FrameRecorded() {
761 rtc::CritScope crit(&comparison_lock_);
762 ++frames_recorded_;
763 }
764
765 // Returns true if all frames to be compared have been taken from the queue.
766 bool AllFramesRecorded() {
767 rtc::CritScope crit(&comparison_lock_);
768 assert(frames_recorded_ <= frames_to_process_);
769 return frames_recorded_ == frames_to_process_;
770 }
771
772 // Increase count of number of frames processed. Returns true if this was the
773 // last frame to be processed.
774 bool FrameProcessed() {
775 rtc::CritScope crit(&comparison_lock_);
776 ++frames_processed_;
777 assert(frames_processed_ <= frames_to_process_);
778 return frames_processed_ == frames_to_process_;
779 }
780
781 void PrintResults() {
ilnikdf92c5c2017-02-23 02:08:44 -0800782 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -0700783 rtc::CritScope crit(&comparison_lock_);
784 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800785 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 01:02:15 -0700786 PrintResult("sender_time", sender_time_, " ms");
787 PrintResult("receiver_time", receiver_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700788 PrintResult("total_delay_incl_network", end_to_end_, " ms");
789 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700790 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800791 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800792 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
stefan889d9652017-07-05 03:03:02 -0700793 PrintResult("fec_bitrate", fec_bitrate_bps_, " bps");
794 PrintResult("send_bandwidth", send_bandwidth_bps_, " bps");
philipelfd870db2017-01-23 03:22:15 -0800795
ilnik59cac992017-07-25 05:45:03 -0700796 if (worst_frame_) {
Edward Lemurd1d67432018-01-02 11:54:37 +0100797 test::PrintResult("min_psnr", "", test_label_.c_str(), worst_frame_->psnr,
798 "dB", false);
ilnik59cac992017-07-25 05:45:03 -0700799 }
800
philipelfd870db2017-01-23 03:22:15 -0800801 if (receive_stream_ != nullptr) {
802 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800803 }
ivica5d6a06c2015-09-17 05:30:24 -0700804
Edward Lemurd1d67432018-01-02 11:54:37 +0100805 test::PrintResult("dropped_frames", "", test_label_.c_str(),
806 dropped_frames_, "frames", false);
807 test::PrintResult("cpu_usage", "", test_label_.c_str(),
808 GetCpuUsagePercent(), "%", false);
ilnikdaa574d2017-03-01 06:46:05 -0800809
810#if defined(WEBRTC_WIN)
811 // On Linux and Mac in Resident Set some unused pages may be counted.
812 // Therefore this metric will depend on order in which tests are run and
813 // will be flaky.
814 PrintResult("memory_usage", memory_usage_, " bytes");
815#endif
ilnik59cac992017-07-25 05:45:03 -0700816
ilnikee42d192017-08-22 07:16:20 -0700817 // Saving only the worst frame for manual analysis. Intention here is to
818 // only detect video corruptions and not to track picture quality. Thus,
819 // jpeg is used here.
oprypin9b2f20c2017-08-29 05:51:57 -0700820 if (FLAG_save_worst_frame && worst_frame_) {
ilnikee42d192017-08-22 07:16:20 -0700821 std::string output_dir;
Edward Lemuraf8659a2017-09-27 14:46:24 +0200822 test::GetTestArtifactsDir(&output_dir);
ilnikee42d192017-08-22 07:16:20 -0700823 std::string output_path =
824 rtc::Pathname(output_dir, test_label_ + ".jpg").pathname();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100825 RTC_LOG(LS_INFO) << "Saving worst frame to " << output_path;
ilnikee42d192017-08-22 07:16:20 -0700826 test::JpegFrameWriter frame_writer(output_path);
827 RTC_CHECK(frame_writer.WriteFrame(worst_frame_->frame,
828 100 /*best quality*/));
ilnik59cac992017-07-25 05:45:03 -0700829 }
ilnikdaa574d2017-03-01 06:46:05 -0800830
ilnik9ae0d762017-02-15 00:53:12 -0800831 // Disable quality check for quick test, as quality checks may fail
832 // because too few samples were collected.
833 if (!is_quick_test_enabled_) {
834 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
835 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
836 }
ivica5d6a06c2015-09-17 05:30:24 -0700837 }
838
839 void PerformFrameComparison(const FrameComparison& comparison) {
840 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700841 double psnr = -1.0;
842 double ssim = -1.0;
ilnik6b826ef2017-06-16 06:53:48 -0700843 if (comparison.reference && !comparison.dropped) {
nissedf2ceb82016-12-15 06:29:53 -0800844 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
845 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700846 }
ivica5d6a06c2015-09-17 05:30:24 -0700847
ivica5d6a06c2015-09-17 05:30:24 -0700848 rtc::CritScope crit(&comparison_lock_);
ilnik59cac992017-07-25 05:45:03 -0700849
850 if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) {
851 worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render});
852 }
853
ivica5d6a06c2015-09-17 05:30:24 -0700854 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800855 samples_.push_back(Sample(
856 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
857 comparison.recv_time_ms, comparison.render_time_ms,
858 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700859 }
stefanb1797672016-08-11 07:00:57 -0700860 if (psnr >= 0.0)
861 psnr_.AddSample(psnr);
862 if (ssim >= 0.0)
863 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700864
865 if (comparison.dropped) {
866 ++dropped_frames_;
867 return;
868 }
869 if (last_render_time_ != 0)
870 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
871 last_render_time_ = comparison.render_time_ms;
872
nissedf2ceb82016-12-15 06:29:53 -0800873 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800874 if (comparison.recv_time_ms > 0) {
875 // If recv_time_ms == 0, this frame consisted of a packets which were all
876 // lost in the transport. Since we were able to render the frame, however,
877 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
878 // happens internally in Call, and we can therefore here not know which
879 // FEC packets that protected the lost media packets. Consequently, we
880 // were not able to record a meaningful recv_time_ms. We therefore skip
881 // this sample.
882 //
883 // The reasoning above does not hold for ULPFEC and RTX, as for those
884 // strategies the timestamp of the received packets is set to the
885 // timestamp of the protected/retransmitted media packet. I.e., then
886 // recv_time_ms != 0, even though the media packets were lost.
887 receiver_time_.AddSample(comparison.render_time_ms -
888 comparison.recv_time_ms);
889 }
nissedf2ceb82016-12-15 06:29:53 -0800890 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700891 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
892 }
893
894 void PrintResult(const char* result_type,
895 test::Statistics stats,
896 const char* unit) {
Edward Lemurd1d67432018-01-02 11:54:37 +0100897 test::PrintResultMeanAndError(result_type, "", test_label_.c_str(),
898 stats.Mean(), stats.StandardDeviation(), unit,
899 false);
ivica5d6a06c2015-09-17 05:30:24 -0700900 }
901
902 void PrintSamplesToFile(void) {
903 FILE* out = graph_data_output_file_;
904 rtc::CritScope crit(&comparison_lock_);
905 std::sort(samples_.begin(), samples_.end(),
906 [](const Sample& A, const Sample& B) -> bool {
907 return A.input_time_ms < B.input_time_ms;
908 });
909
sprangce4aef12015-11-02 07:23:20 -0800910 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700911 fprintf(out, "%" PRIuS "\n", samples_.size());
912 fprintf(out,
913 "dropped "
914 "input_time_ms "
915 "send_time_ms "
916 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700917 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700918 "encoded_frame_size "
919 "psnr "
920 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700921 "encode_time_ms\n");
ivica5d6a06c2015-09-17 05:30:24 -0700922 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700923 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
Niels Möller6b642f72017-12-08 14:11:14 +0100924 " %lf %lf\n",
ivica8d15bd62015-10-07 02:43:12 -0700925 sample.dropped, sample.input_time_ms, sample.send_time_ms,
926 sample.recv_time_ms, sample.render_time_ms,
Niels Möller6b642f72017-12-08 14:11:14 +0100927 sample.encoded_frame_size, sample.psnr, sample.ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700928 }
929 }
930
ilnik1e7732c2017-02-23 05:07:56 -0800931 double GetAverageMediaBitrateBps() {
932 if (last_sending_time_ == first_sending_time_) {
933 return 0;
934 } else {
935 return static_cast<double>(total_media_bytes_) * 8 /
936 (last_sending_time_ - first_sending_time_) *
937 rtc::kNumMillisecsPerSec;
938 }
939 }
940
perkja49cbd32016-09-16 07:53:41 -0700941 // Implements VideoSinkInterface to receive captured frames from a
942 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
943 // as a source to VideoSendStream.
944 // It forwards all input frames to the VideoAnalyzer for later comparison and
945 // forwards the captured frames to the VideoSendStream.
946 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
947 public rtc::VideoSourceInterface<VideoFrame> {
948 public:
ilnik6b826ef2017-06-16 06:53:48 -0700949 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer, Clock* clock)
950 : analyzer_(analyzer),
951 send_stream_input_(nullptr),
952 video_capturer_(nullptr),
953 clock_(clock) {}
954
955 void SetSource(test::VideoCapturer* video_capturer) {
956 video_capturer_ = video_capturer;
957 }
perkja49cbd32016-09-16 07:53:41 -0700958
959 private:
960 void OnFrame(const VideoFrame& video_frame) override {
961 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -0800962 // Frames from the capturer does not have a rtp timestamp.
963 // Create one so it can be used for comparison.
964 RTC_DCHECK_EQ(0, video_frame.timestamp());
ilnik6b826ef2017-06-16 06:53:48 -0700965 if (video_frame.ntp_time_ms() == 0)
966 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
perkja49cbd32016-09-16 07:53:41 -0700967 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -0800968 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -0700969 rtc::CritScope lock(&crit_);
970 if (send_stream_input_)
ilnikb82ac6a2017-05-02 00:48:41 -0700971 send_stream_input_->OnFrame(copy);
perkja49cbd32016-09-16 07:53:41 -0700972 }
973
974 // Called when |send_stream_.SetSource()| is called.
975 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
976 const rtc::VideoSinkWants& wants) override {
ilnik267041c2017-06-27 07:21:01 -0700977 {
978 rtc::CritScope lock(&crit_);
979 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
980 send_stream_input_ = sink;
981 }
ilnik6b826ef2017-06-16 06:53:48 -0700982 if (video_capturer_) {
983 video_capturer_->AddOrUpdateSink(this, wants);
984 }
perkja49cbd32016-09-16 07:53:41 -0700985 }
986
987 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
988 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
989 rtc::CritScope lock(&crit_);
990 RTC_DCHECK(sink == send_stream_input_);
991 send_stream_input_ = nullptr;
992 }
993
994 VideoAnalyzer* const analyzer_;
995 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700996 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_
997 RTC_GUARDED_BY(crit_);
ilnik6b826ef2017-06-16 06:53:48 -0700998 test::VideoCapturer* video_capturer_;
999 Clock* clock_;
perkja49cbd32016-09-16 07:53:41 -07001000 };
1001
1002 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
1003 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -08001004 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -07001005 }
1006
stefan889d9652017-07-05 03:03:02 -07001007 Call* call_;
perkja49cbd32016-09-16 07:53:41 -07001008 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -08001009 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -07001010 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -07001011 const std::string test_label_;
1012 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -08001013 const std::string graph_title_;
1014 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -08001015 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 09:23:30 -08001016 const size_t selected_stream_;
ilnik1e7732c2017-02-23 05:07:56 -08001017 const int selected_sl_;
1018 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -07001019 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +01001020 OnEncodeTimingProxy encode_timing_proxy_;
danilchapa37de392017-09-09 04:17:22 -07001021 std::vector<Sample> samples_ RTC_GUARDED_BY(comparison_lock_);
danilchapa37de392017-09-09 04:17:22 -07001022 test::Statistics sender_time_ RTC_GUARDED_BY(comparison_lock_);
1023 test::Statistics receiver_time_ RTC_GUARDED_BY(comparison_lock_);
1024 test::Statistics psnr_ RTC_GUARDED_BY(comparison_lock_);
1025 test::Statistics ssim_ RTC_GUARDED_BY(comparison_lock_);
1026 test::Statistics end_to_end_ RTC_GUARDED_BY(comparison_lock_);
1027 test::Statistics rendered_delta_ RTC_GUARDED_BY(comparison_lock_);
1028 test::Statistics encoded_frame_size_ RTC_GUARDED_BY(comparison_lock_);
1029 test::Statistics encode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
1030 test::Statistics encode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
1031 test::Statistics encode_usage_percent_ RTC_GUARDED_BY(comparison_lock_);
1032 test::Statistics decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
1033 test::Statistics decode_time_max_ms_ RTC_GUARDED_BY(comparison_lock_);
1034 test::Statistics media_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
1035 test::Statistics fec_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
1036 test::Statistics send_bandwidth_bps_ RTC_GUARDED_BY(comparison_lock_);
1037 test::Statistics memory_usage_ RTC_GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 06:46:05 -08001038
ilnik59cac992017-07-25 05:45:03 -07001039 struct FrameWithPsnr {
1040 double psnr;
1041 VideoFrame frame;
1042 };
1043
1044 // Rendered frame with worst PSNR is saved for further analysis.
danilchapa37de392017-09-09 04:17:22 -07001045 rtc::Optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_);
ilnik59cac992017-07-25 05:45:03 -07001046
stefan889d9652017-07-05 03:03:02 -07001047 size_t last_fec_bytes_;
ivica5d6a06c2015-09-17 05:30:24 -07001048
1049 const int frames_to_process_;
1050 int frames_recorded_;
1051 int frames_processed_;
1052 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -07001053 int dropped_frames_before_first_encode_;
1054 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -07001055 int64_t last_render_time_;
1056 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -08001057 int64_t total_media_bytes_;
1058 int64_t first_sending_time_;
1059 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -07001060
danilchapa37de392017-09-09 04:17:22 -07001061 int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
1062 int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
ilnikdf92c5c2017-02-23 02:08:44 -08001063 rtc::CriticalSection cpu_measurement_lock_;
1064
ivica5d6a06c2015-09-17 05:30:24 -07001065 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07001066 std::deque<VideoFrame> frames_ RTC_GUARDED_BY(crit_);
1067 rtc::Optional<VideoFrame> last_rendered_frame_ RTC_GUARDED_BY(crit_);
1068 rtc::TimestampWrapAroundHandler wrap_handler_ RTC_GUARDED_BY(crit_);
1069 std::map<int64_t, int64_t> send_times_ RTC_GUARDED_BY(crit_);
1070 std::map<int64_t, int64_t> recv_times_ RTC_GUARDED_BY(crit_);
1071 std::map<int64_t, size_t> encoded_frame_sizes_ RTC_GUARDED_BY(crit_);
1072 rtc::Optional<uint32_t> first_encoded_timestamp_ RTC_GUARDED_BY(crit_);
1073 rtc::Optional<uint32_t> first_sent_timestamp_ RTC_GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -07001074 const double avg_psnr_threshold_;
1075 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -08001076 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -07001077
1078 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001079 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1080 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001081 rtc::Event comparison_available_event_;
danilchapa37de392017-09-09 04:17:22 -07001082 std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001083 rtc::Event done_;
ilnik98436952017-07-13 00:47:03 -07001084
1085 std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
1086 Clock* const clock_;
1087 const int64_t start_ms_;
ivica5d6a06c2015-09-17 05:30:24 -07001088};
1089
palmkviste75f2042016-09-28 06:19:48 -07001090VideoQualityTest::VideoQualityTest()
minyue20c84cc2017-04-10 16:57:57 -07001091 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {
1092 payload_type_map_ = test::CallTest::payload_type_map_;
1093 RTC_DCHECK(payload_type_map_.find(kPayloadTypeH264) ==
1094 payload_type_map_.end());
1095 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP8) ==
1096 payload_type_map_.end());
1097 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP9) ==
1098 payload_type_map_.end());
1099 payload_type_map_[kPayloadTypeH264] = webrtc::MediaType::VIDEO;
1100 payload_type_map_[kPayloadTypeVP8] = webrtc::MediaType::VIDEO;
1101 payload_type_map_[kPayloadTypeVP9] = webrtc::MediaType::VIDEO;
1102}
ivica5d6a06c2015-09-17 05:30:24 -07001103
minyue626bc952016-10-31 05:47:02 -07001104VideoQualityTest::Params::Params()
ilnik98436952017-07-13 00:47:03 -07001105 : call({false, Call::Config::BitrateConfig(), 0}),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001106 video{{false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
1107 false, ""},
1108 {false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
1109 false, ""}},
minyue4c8b9422017-03-21 04:11:43 -07001110 audio({false, false, false}),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001111 screenshare{{false, false, 10, 0}, {false, false, 10, 0}},
minyue626bc952016-10-31 05:47:02 -07001112 analyzer({"", 0.0, 0.0, 0, "", ""}),
1113 pipe(),
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001114 ss{{std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()},
1115 {std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}},
ilnik98436952017-07-13 00:47:03 -07001116 logging({false, "", "", ""}) {}
minyue626bc952016-10-31 05:47:02 -07001117
1118VideoQualityTest::Params::~Params() = default;
1119
ivica5d6a06c2015-09-17 05:30:24 -07001120void VideoQualityTest::TestBody() {}
1121
sprangce4aef12015-11-02 07:23:20 -08001122std::string VideoQualityTest::GenerateGraphTitle() const {
1123 std::stringstream ss;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001124 ss << params_.video[0].codec;
1125 ss << " (" << params_.video[0].target_bitrate_bps / 1000 << "kbps";
1126 ss << ", " << params_.video[0].fps << " FPS";
1127 if (params_.screenshare[0].scroll_duration)
1128 ss << ", " << params_.screenshare[0].scroll_duration << "s scroll";
1129 if (params_.ss[0].streams.size() > 1)
1130 ss << ", Stream #" << params_.ss[0].selected_stream;
1131 if (params_.ss[0].num_spatial_layers > 1)
1132 ss << ", Layer #" << params_.ss[0].selected_sl;
sprangce4aef12015-11-02 07:23:20 -08001133 ss << ")";
1134 return ss.str();
1135}
1136
1137void VideoQualityTest::CheckParams() {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001138 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1139 // Iterate over primary and secondary video streams.
1140 if (!params_.video[video_idx].enabled)
1141 return;
1142 // Add a default stream in none specified.
1143 if (params_.ss[video_idx].streams.empty())
1144 params_.ss[video_idx].streams.push_back(
1145 VideoQualityTest::DefaultVideoStream(params_, video_idx));
1146 if (params_.ss[video_idx].num_spatial_layers == 0)
1147 params_.ss[video_idx].num_spatial_layers = 1;
sprangce4aef12015-11-02 07:23:20 -08001148
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001149 if (params_.pipe.loss_percent != 0 ||
1150 params_.pipe.queue_length_packets != 0) {
1151 // Since LayerFilteringTransport changes the sequence numbers, we can't
1152 // use that feature with pack loss, since the NACK request would end up
1153 // retransmitting the wrong packets.
1154 RTC_CHECK(params_.ss[video_idx].selected_sl == -1 ||
1155 params_.ss[video_idx].selected_sl ==
1156 params_.ss[video_idx].num_spatial_layers - 1);
1157 RTC_CHECK(params_.video[video_idx].selected_tl == -1 ||
1158 params_.video[video_idx].selected_tl ==
1159 params_.video[video_idx].num_temporal_layers - 1);
1160 }
sprangce4aef12015-11-02 07:23:20 -08001161
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001162 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as
1163 // it does in some parts of the code?
1164 RTC_CHECK_GE(params_.video[video_idx].max_bitrate_bps,
1165 params_.video[video_idx].target_bitrate_bps);
1166 RTC_CHECK_GE(params_.video[video_idx].target_bitrate_bps,
1167 params_.video[video_idx].min_bitrate_bps);
1168 RTC_CHECK_LT(params_.video[video_idx].selected_tl,
1169 params_.video[video_idx].num_temporal_layers);
1170 RTC_CHECK_LE(params_.ss[video_idx].selected_stream,
1171 params_.ss[video_idx].streams.size());
1172 for (const VideoStream& stream : params_.ss[video_idx].streams) {
1173 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1174 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1175 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
1176 }
1177 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1178 // the total bitrate? We anyway have to update them in the case bitrate
1179 // estimator changes the total bitrates.
1180 RTC_CHECK_GE(params_.ss[video_idx].num_spatial_layers, 1);
1181 RTC_CHECK_LE(params_.ss[video_idx].selected_sl,
1182 params_.ss[video_idx].num_spatial_layers);
1183 RTC_CHECK(
1184 params_.ss[video_idx].spatial_layers.empty() ||
1185 params_.ss[video_idx].spatial_layers.size() ==
1186 static_cast<size_t>(params_.ss[video_idx].num_spatial_layers));
1187 if (params_.video[video_idx].codec == "VP8") {
1188 RTC_CHECK_EQ(params_.ss[video_idx].num_spatial_layers, 1);
1189 } else if (params_.video[video_idx].codec == "VP9") {
1190 RTC_CHECK_EQ(params_.ss[video_idx].streams.size(), 1);
1191 }
1192 RTC_CHECK_GE(params_.call.num_thumbnails, 0);
1193 if (params_.call.num_thumbnails > 0) {
1194 RTC_CHECK_EQ(params_.ss[video_idx].num_spatial_layers, 1);
1195 RTC_CHECK_EQ(params_.ss[video_idx].streams.size(), 3);
1196 RTC_CHECK_EQ(params_.video[video_idx].num_temporal_layers, 3);
1197 RTC_CHECK_EQ(params_.video[video_idx].codec, "VP8");
1198 }
1199 // Dual streams with FEC not supported in tests yet.
1200 RTC_CHECK(!params_.video[video_idx].flexfec || num_video_streams_ == 1);
1201 RTC_CHECK(!params_.video[video_idx].ulpfec || num_video_streams_ == 1);
ilnika014cc52017-03-07 04:21:04 -08001202 }
sprangce4aef12015-11-02 07:23:20 -08001203}
1204
1205// Static.
1206std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1207 // Parse comma separated nonnegative integers, where some elements may be
1208 // empty. The empty values are replaced with -1.
1209 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1210 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1211 std::vector<int> result;
1212 if (str.empty())
1213 return result;
1214
1215 const char* p = str.c_str();
1216 int value = -1;
1217 int pos;
1218 while (*p) {
1219 if (*p == ',') {
1220 result.push_back(value);
1221 value = -1;
1222 ++p;
1223 continue;
1224 }
1225 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1226 << "Unexpected non-number value.";
1227 p += pos;
1228 }
1229 result.push_back(value);
1230 return result;
1231}
1232
1233// Static.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001234VideoStream VideoQualityTest::DefaultVideoStream(const Params& params,
1235 size_t video_idx) {
sprangce4aef12015-11-02 07:23:20 -08001236 VideoStream stream;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001237 stream.width = params.video[video_idx].width;
1238 stream.height = params.video[video_idx].height;
1239 stream.max_framerate = params.video[video_idx].fps;
1240 stream.min_bitrate_bps = params.video[video_idx].min_bitrate_bps;
1241 stream.target_bitrate_bps = params.video[video_idx].target_bitrate_bps;
1242 stream.max_bitrate_bps = params.video[video_idx].max_bitrate_bps;
sprang1168fd42017-06-21 09:00:17 -07001243 stream.max_qp = kDefaultMaxQp;
sprang6ef1b342017-03-13 02:01:32 -07001244 // TODO(sprang): Can we make this less of a hack?
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001245 if (params.video[video_idx].num_temporal_layers == 2) {
sprang6ef1b342017-03-13 02:01:32 -07001246 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001247 } else if (params.video[video_idx].num_temporal_layers == 3) {
sprang6ef1b342017-03-13 02:01:32 -07001248 stream.temporal_layer_thresholds_bps.push_back(stream.max_bitrate_bps / 4);
1249 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
sprangff19d352017-09-06 07:14:02 -07001250 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001251 RTC_CHECK_LE(params.video[video_idx].num_temporal_layers,
1252 kMaxTemporalStreams);
1253 for (int i = 0; i < params.video[video_idx].num_temporal_layers - 1; ++i) {
sprangff19d352017-09-06 07:14:02 -07001254 stream.temporal_layer_thresholds_bps.push_back(static_cast<int>(
1255 stream.max_bitrate_bps * kVp8LayerRateAlloction[0][i] + 0.5));
1256 }
ilnika014cc52017-03-07 04:21:04 -08001257 }
1258 return stream;
1259}
1260
1261// Static.
1262VideoStream VideoQualityTest::DefaultThumbnailStream() {
1263 VideoStream stream;
1264 stream.width = 320;
1265 stream.height = 180;
1266 stream.max_framerate = 7;
1267 stream.min_bitrate_bps = 7500;
1268 stream.target_bitrate_bps = 37500;
1269 stream.max_bitrate_bps = 50000;
sprang1168fd42017-06-21 09:00:17 -07001270 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 07:23:20 -08001271 return stream;
1272}
1273
1274// Static.
1275void VideoQualityTest::FillScalabilitySettings(
1276 Params* params,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001277 size_t video_idx,
sprangce4aef12015-11-02 07:23:20 -08001278 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 09:00:17 -07001279 int num_streams,
sprangce4aef12015-11-02 07:23:20 -08001280 size_t selected_stream,
1281 int num_spatial_layers,
1282 int selected_sl,
1283 const std::vector<std::string>& sl_descriptors) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001284 if (params->ss[video_idx].streams.empty() &&
1285 params->ss[video_idx].infer_streams) {
sprang1168fd42017-06-21 09:00:17 -07001286 webrtc::VideoEncoderConfig encoder_config;
1287 encoder_config.content_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001288 params->screenshare[video_idx].enabled
sprang1168fd42017-06-21 09:00:17 -07001289 ? webrtc::VideoEncoderConfig::ContentType::kScreen
1290 : webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001291 encoder_config.max_bitrate_bps = params->video[video_idx].max_bitrate_bps;
1292 encoder_config.min_transmit_bitrate_bps =
1293 params->video[video_idx].min_transmit_bps;
sprang1168fd42017-06-21 09:00:17 -07001294 encoder_config.number_of_streams = num_streams;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001295 encoder_config.spatial_layers = params->ss[video_idx].spatial_layers;
sprang1168fd42017-06-21 09:00:17 -07001296 encoder_config.video_stream_factory =
1297 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001298 params->video[video_idx].codec, kDefaultMaxQp,
1299 params->video[video_idx].fps,
1300 params->screenshare[video_idx].enabled, true);
1301 params->ss[video_idx].streams =
sprang1168fd42017-06-21 09:00:17 -07001302 encoder_config.video_stream_factory->CreateEncoderStreams(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001303 static_cast<int>(params->video[video_idx].width),
1304 static_cast<int>(params->video[video_idx].height), encoder_config);
sprang1168fd42017-06-21 09:00:17 -07001305 } else {
1306 // Read VideoStream and SpatialLayer elements from a list of comma separated
1307 // lists. To use a default value for an element, use -1 or leave empty.
1308 // Validity checks performed in CheckParams.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001309 RTC_CHECK(params->ss[video_idx].streams.empty());
sprang1168fd42017-06-21 09:00:17 -07001310 for (auto descriptor : stream_descriptors) {
1311 if (descriptor.empty())
1312 continue;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001313 VideoStream stream =
1314 VideoQualityTest::DefaultVideoStream(*params, video_idx);
sprang1168fd42017-06-21 09:00:17 -07001315 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1316 if (v[0] != -1)
1317 stream.width = static_cast<size_t>(v[0]);
1318 if (v[1] != -1)
1319 stream.height = static_cast<size_t>(v[1]);
1320 if (v[2] != -1)
1321 stream.max_framerate = v[2];
1322 if (v[3] != -1)
1323 stream.min_bitrate_bps = v[3];
1324 if (v[4] != -1)
1325 stream.target_bitrate_bps = v[4];
1326 if (v[5] != -1)
1327 stream.max_bitrate_bps = v[5];
1328 if (v.size() > 6 && v[6] != -1)
1329 stream.max_qp = v[6];
1330 if (v.size() > 7) {
1331 stream.temporal_layer_thresholds_bps.clear();
1332 stream.temporal_layer_thresholds_bps.insert(
1333 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1334 } else {
1335 // Automatic TL thresholds for more than two layers not supported.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001336 RTC_CHECK_LE(params->video[video_idx].num_temporal_layers, 2);
sprang1168fd42017-06-21 09:00:17 -07001337 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001338 params->ss[video_idx].streams.push_back(stream);
sprangce4aef12015-11-02 07:23:20 -08001339 }
sprangce4aef12015-11-02 07:23:20 -08001340 }
sprangce4aef12015-11-02 07:23:20 -08001341
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001342 params->ss[video_idx].num_spatial_layers = std::max(1, num_spatial_layers);
1343 params->ss[video_idx].selected_stream = selected_stream;
sprang1168fd42017-06-21 09:00:17 -07001344
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001345 params->ss[video_idx].selected_sl = selected_sl;
1346 RTC_CHECK(params->ss[video_idx].spatial_layers.empty());
sprangce4aef12015-11-02 07:23:20 -08001347 for (auto descriptor : sl_descriptors) {
1348 if (descriptor.empty())
1349 continue;
1350 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1351 RTC_CHECK_GT(v[2], 0);
1352
1353 SpatialLayer layer;
1354 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1355 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1356 layer.target_bitrate_bps = v[2];
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001357 params->ss[video_idx].spatial_layers.push_back(layer);
sprangce4aef12015-11-02 07:23:20 -08001358 }
1359}
1360
minyuea27172d2016-11-01 05:59:29 -07001361void VideoQualityTest::SetupVideo(Transport* send_transport,
1362 Transport* recv_transport) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001363 size_t total_streams_used = 0;
1364 size_t num_flexfec_streams = params_.video[0].flexfec ? 1 : 0;
1365 CreateAudioAndFecSendConfigs(0, num_flexfec_streams, send_transport);
1366 CreateMatchingAudioAndFecConfigs(recv_transport);
1367 video_receive_configs_.clear();
1368 video_send_configs_.clear();
1369 video_encoders_.clear();
1370 video_encoder_configs_.clear();
1371 allocated_decoders_.clear();
1372 bool decode_all_receive_streams = true;
1373 size_t num_video_substreams = params_.ss[0].streams.size();
1374 RTC_CHECK(num_video_streams_ > 0);
1375 video_encoder_configs_.resize(num_video_streams_);
1376 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1377 video_send_configs_.push_back(VideoSendStream::Config(send_transport));
1378 video_encoders_.push_back(nullptr);
1379 video_encoder_configs_.push_back(VideoEncoderConfig());
1380 num_video_substreams = params_.ss[video_idx].streams.size();
1381 RTC_CHECK_GT(num_video_substreams, 0);
1382 CreateVideoSendConfig(&video_send_configs_[video_idx], num_video_substreams,
1383 total_streams_used, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001384
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001385 int payload_type;
1386 if (params_.video[video_idx].codec == "H264") {
1387 video_encoders_[video_idx] =
1388 H264Encoder::Create(cricket::VideoCodec("H264"));
1389 payload_type = kPayloadTypeH264;
1390 } else if (params_.video[video_idx].codec == "VP8") {
1391 if (params_.screenshare[video_idx].enabled &&
1392 params_.ss[video_idx].streams.size() > 1) {
1393 // Simulcast screenshare needs a simulcast encoder adapter to work,
1394 // since encoders usually can't natively do simulcast with different
1395 // frame rates for the different layers.
1396 video_encoders_[video_idx].reset(
1397 new SimulcastEncoderAdapter(new InternalEncoderFactory()));
1398 } else {
1399 video_encoders_[video_idx] = VP8Encoder::Create();
1400 }
1401 payload_type = kPayloadTypeVP8;
1402 } else if (params_.video[video_idx].codec == "VP9") {
1403 video_encoders_[video_idx] = VP9Encoder::Create();
1404 payload_type = kPayloadTypeVP9;
ilnikcb8c1462017-03-09 09:23:30 -08001405 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001406 RTC_NOTREACHED() << "Codec not supported!";
1407 return;
ilnikcb8c1462017-03-09 09:23:30 -08001408 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001409 video_send_configs_[video_idx].encoder_settings.encoder =
1410 video_encoders_[video_idx].get();
1411 video_send_configs_[video_idx].encoder_settings.payload_name =
1412 params_.video[video_idx].codec;
1413 video_send_configs_[video_idx].encoder_settings.payload_type = payload_type;
1414 video_send_configs_[video_idx].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1415 video_send_configs_[video_idx].rtp.rtx.payload_type = kSendRtxPayloadType;
1416 for (size_t i = 0; i < num_video_substreams; ++i) {
1417 video_send_configs_[video_idx].rtp.rtx.ssrcs.push_back(
1418 kSendRtxSsrcs[i + total_streams_used]);
ilnik9fd9f6c2017-03-02 08:10:10 -08001419 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001420 video_send_configs_[video_idx].rtp.extensions.clear();
1421 if (params_.call.send_side_bwe) {
1422 video_send_configs_[video_idx].rtp.extensions.push_back(
1423 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1424 test::kTransportSequenceNumberExtensionId));
1425 } else {
1426 video_send_configs_[video_idx].rtp.extensions.push_back(RtpExtension(
1427 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1428 }
1429 video_send_configs_[video_idx].rtp.extensions.push_back(
1430 RtpExtension(RtpExtension::kVideoContentTypeUri,
1431 test::kVideoContentTypeExtensionId));
1432 video_send_configs_[video_idx].rtp.extensions.push_back(RtpExtension(
1433 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
1434
1435 video_encoder_configs_[video_idx].min_transmit_bitrate_bps =
1436 params_.video[video_idx].min_transmit_bps;
1437
1438 video_send_configs_[video_idx].suspend_below_min_bitrate =
1439 params_.video[video_idx].suspend_below_min_bitrate;
1440
1441 video_encoder_configs_[video_idx].number_of_streams =
1442 params_.ss[video_idx].streams.size();
1443 video_encoder_configs_[video_idx].max_bitrate_bps = 0;
1444 for (size_t i = 0; i < params_.ss[video_idx].streams.size(); ++i) {
1445 video_encoder_configs_[video_idx].max_bitrate_bps +=
1446 params_.ss[video_idx].streams[i].max_bitrate_bps;
1447 }
1448 if (params_.ss[video_idx].infer_streams) {
1449 video_encoder_configs_[video_idx].video_stream_factory =
1450 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1451 params_.video[video_idx].codec,
1452 params_.ss[video_idx].streams[0].max_qp,
1453 params_.video[video_idx].fps,
1454 params_.screenshare[video_idx].enabled, true);
1455 } else {
1456 video_encoder_configs_[video_idx].video_stream_factory =
1457 new rtc::RefCountedObject<VideoStreamFactory>(
1458 params_.ss[video_idx].streams);
1459 }
1460
1461 video_encoder_configs_[video_idx].spatial_layers =
1462 params_.ss[video_idx].spatial_layers;
1463
1464 std::vector<VideoReceiveStream::Config> new_receive_configs =
1465 CreateMatchingVideoReceiveConfigs(video_send_configs_[video_idx],
1466 recv_transport);
1467
1468 decode_all_receive_streams = params_.ss[video_idx].selected_stream ==
1469 params_.ss[video_idx].streams.size();
1470
1471 for (size_t i = 0; i < num_video_substreams; ++i) {
1472 new_receive_configs[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1473 new_receive_configs[i].rtp.rtx_ssrc =
1474 kSendRtxSsrcs[i + total_streams_used];
1475 new_receive_configs[i]
1476 .rtp.rtx_associated_payload_types[kSendRtxPayloadType] = payload_type;
1477 new_receive_configs[i].rtp.transport_cc = params_.call.send_side_bwe;
1478 new_receive_configs[i].rtp.remb = !params_.call.send_side_bwe;
1479 // Enable RTT calculation so NTP time estimator will work.
1480 new_receive_configs[i].rtp.rtcp_xr.receiver_reference_time_report = true;
1481 // Force fake decoders on non-selected simulcast streams.
1482 if (!decode_all_receive_streams &&
1483 i != params_.ss[video_idx].selected_stream) {
1484 VideoReceiveStream::Decoder decoder;
1485 decoder.decoder = new test::FakeDecoder();
1486 decoder.payload_type =
1487 video_send_configs_[video_idx].encoder_settings.payload_type;
1488 decoder.payload_name =
1489 video_send_configs_[video_idx].encoder_settings.payload_name;
1490 new_receive_configs[i].decoders.clear();
1491 allocated_decoders_.emplace_back(decoder.decoder);
1492 new_receive_configs[i].decoders.push_back(decoder);
1493 }
1494 }
1495
1496 for (VideoReceiveStream::Config& config : new_receive_configs) {
1497 video_receive_configs_.push_back(config.Copy());
1498 }
1499
1500 if (params_.screenshare[video_idx].enabled) {
1501 // Fill out codec settings.
1502 video_encoder_configs_[video_idx].content_type =
1503 VideoEncoderConfig::ContentType::kScreen;
1504 degradation_preference_ =
1505 VideoSendStream::DegradationPreference::kMaintainResolution;
1506 if (params_.video[video_idx].codec == "VP8") {
1507 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1508 vp8_settings.denoisingOn = false;
1509 vp8_settings.frameDroppingOn = false;
1510 vp8_settings.numberOfTemporalLayers = static_cast<unsigned char>(
1511 params_.video[video_idx].num_temporal_layers);
1512 video_encoder_configs_[video_idx].encoder_specific_settings =
1513 new rtc::RefCountedObject<
1514 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1515 } else if (params_.video[video_idx].codec == "VP9") {
1516 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1517 vp9_settings.denoisingOn = false;
1518 vp9_settings.frameDroppingOn = false;
1519 vp9_settings.numberOfTemporalLayers = static_cast<unsigned char>(
1520 params_.video[video_idx].num_temporal_layers);
1521 vp9_settings.numberOfSpatialLayers = static_cast<unsigned char>(
1522 params_.ss[video_idx].num_spatial_layers);
1523 video_encoder_configs_[video_idx].encoder_specific_settings =
1524 new rtc::RefCountedObject<
1525 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1526 }
1527 } else if (params_.ss[video_idx].num_spatial_layers > 1) {
1528 // If SVC mode without screenshare, still need to set codec specifics.
1529 RTC_CHECK(params_.video[video_idx].codec == "VP9");
1530 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1531 vp9_settings.numberOfTemporalLayers = static_cast<unsigned char>(
1532 params_.video[video_idx].num_temporal_layers);
1533 vp9_settings.numberOfSpatialLayers =
1534 static_cast<unsigned char>(params_.ss[video_idx].num_spatial_layers);
1535 video_encoder_configs_[video_idx].encoder_specific_settings =
1536 new rtc::RefCountedObject<
1537 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1538 }
1539 total_streams_used += num_video_substreams;
sprangce4aef12015-11-02 07:23:20 -08001540 }
brandtr1293aca2016-11-16 22:47:29 -08001541
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001542 // FEC supported only for single video stream mode yet.
1543 if (params_.video[0].flexfec) {
Ilya Nikolaevskiy38aaf692017-12-22 14:39:09 +01001544 video_send_configs_[0].rtp.flexfec.payload_type = kFlexfecPayloadType;
1545 video_send_configs_[0].rtp.flexfec.ssrc = kFlexfecSendSsrc;
sprang1168fd42017-06-21 09:00:17 -07001546 if (decode_all_receive_streams) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001547 for (uint32_t media_ssrc : video_send_configs_[0].rtp.ssrcs) {
1548 video_send_configs_[0].rtp.flexfec.protected_media_ssrcs.push_back(
sprang1168fd42017-06-21 09:00:17 -07001549 media_ssrc);
1550 }
1551 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001552 video_send_configs_[0].rtp.flexfec.protected_media_ssrcs = {
1553 kVideoSendSsrcs[params_.ss[0].selected_stream]};
sprang1168fd42017-06-21 09:00:17 -07001554 }
brandtr1293aca2016-11-16 22:47:29 -08001555
brandtr8313a6f2017-01-13 07:41:19 -08001556 // The matching receive config is _not_ created by
1557 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1558 // Set up the receive config manually instead.
1559 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001560 flexfec_receive_config.payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001561 video_send_configs_[0].rtp.flexfec.payload_type;
1562 flexfec_receive_config.remote_ssrc =
1563 video_send_configs_[0].rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001564 flexfec_receive_config.protected_media_ssrcs =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001565 video_send_configs_[0].rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001566 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001567 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1568 if (params_.call.send_side_bwe) {
1569 flexfec_receive_config.rtp_header_extensions.push_back(
1570 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1571 test::kTransportSequenceNumberExtensionId));
1572 } else {
1573 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1574 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1575 }
brandtr1293aca2016-11-16 22:47:29 -08001576 flexfec_receive_configs_.push_back(flexfec_receive_config);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001577 if (num_video_substreams > 0) {
brandtr7cd28b92017-09-22 00:26:25 -07001578 video_receive_configs_[0].rtp.protected_by_flexfec = true;
1579 }
brandtr1293aca2016-11-16 22:47:29 -08001580 }
1581
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001582 if (params_.video[0].ulpfec) {
1583 video_send_configs_[0].rtp.ulpfec.red_payload_type = kRedPayloadType;
1584 video_send_configs_[0].rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1585 video_send_configs_[0].rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
brandtr1293aca2016-11-16 22:47:29 -08001586
sprang1168fd42017-06-21 09:00:17 -07001587 if (decode_all_receive_streams) {
1588 for (auto it = video_receive_configs_.begin();
1589 it != video_receive_configs_.end(); ++it) {
nisse3b3622f2017-09-26 02:49:21 -07001590 it->rtp.red_payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001591 video_send_configs_[0].rtp.ulpfec.red_payload_type;
nisse3b3622f2017-09-26 02:49:21 -07001592 it->rtp.ulpfec_payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001593 video_send_configs_[0].rtp.ulpfec.ulpfec_payload_type;
1594 it->rtp.rtx_associated_payload_types
1595 [video_send_configs_[0].rtp.ulpfec.red_rtx_payload_type] =
1596 video_send_configs_[0].rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 09:00:17 -07001597 }
1598 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001599 video_receive_configs_[params_.ss[0].selected_stream]
1600 .rtp.red_payload_type =
1601 video_send_configs_[0].rtp.ulpfec.red_payload_type;
1602 video_receive_configs_[params_.ss[0].selected_stream]
nisse3b3622f2017-09-26 02:49:21 -07001603 .rtp.ulpfec_payload_type =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001604 video_send_configs_[0].rtp.ulpfec.ulpfec_payload_type;
1605 video_receive_configs_[params_.ss[0].selected_stream]
1606 .rtp.rtx_associated_payload_types
1607 [video_send_configs_[0].rtp.ulpfec.red_rtx_payload_type] =
1608 video_send_configs_[0].rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 09:00:17 -07001609 }
brandtr1293aca2016-11-16 22:47:29 -08001610 }
ivica5d6a06c2015-09-17 05:30:24 -07001611}
1612
ilnika014cc52017-03-07 04:21:04 -08001613void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1614 Transport* recv_transport) {
ilnik98436952017-07-13 00:47:03 -07001615 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001616 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1617
1618 // Thumbnails will be send in the other way: from receiver_call to
1619 // sender_call.
1620 VideoSendStream::Config thumbnail_send_config(recv_transport);
1621 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1622 thumbnail_send_config.encoder_settings.encoder =
1623 thumbnail_encoders_.back().get();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001624 thumbnail_send_config.encoder_settings.payload_name =
1625 params_.video[0].codec;
ilnika014cc52017-03-07 04:21:04 -08001626 thumbnail_send_config.encoder_settings.payload_type = kPayloadTypeVP8;
1627 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1628 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1629 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1630 thumbnail_send_config.rtp.extensions.clear();
1631 if (params_.call.send_side_bwe) {
1632 thumbnail_send_config.rtp.extensions.push_back(
1633 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1634 test::kTransportSequenceNumberExtensionId));
1635 } else {
1636 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1637 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1638 }
1639
1640 VideoEncoderConfig thumbnail_encoder_config;
1641 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1642 thumbnail_send_config.suspend_below_min_bitrate =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001643 params_.video[0].suspend_below_min_bitrate;
ilnika014cc52017-03-07 04:21:04 -08001644 thumbnail_encoder_config.number_of_streams = 1;
1645 thumbnail_encoder_config.max_bitrate_bps = 50000;
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001646 if (params_.ss[0].infer_streams) {
ilnik6b826ef2017-06-16 06:53:48 -07001647 thumbnail_encoder_config.video_stream_factory =
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001648 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss[0].streams);
ilnik6b826ef2017-06-16 06:53:48 -07001649 } else {
1650 thumbnail_encoder_config.video_stream_factory =
1651 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001652 params_.video[0].codec, params_.ss[0].streams[0].max_qp,
1653 params_.video[0].fps, params_.screenshare[0].enabled, true);
ilnik6b826ef2017-06-16 06:53:48 -07001654 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001655 thumbnail_encoder_config.spatial_layers = params_.ss[0].spatial_layers;
ilnika014cc52017-03-07 04:21:04 -08001656
1657 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1658 thumbnail_receive_config.rtp.remb = false;
1659 thumbnail_receive_config.rtp.transport_cc = true;
1660 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1661 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1662 thumbnail_receive_config.rtp.extensions.push_back(extension);
1663 thumbnail_receive_config.renderer = &fake_renderer_;
1664
1665 VideoReceiveStream::Decoder decoder =
1666 test::CreateMatchingDecoder(thumbnail_send_config.encoder_settings);
1667 allocated_decoders_.push_back(
1668 std::unique_ptr<VideoDecoder>(decoder.decoder));
1669 thumbnail_receive_config.decoders.clear();
1670 thumbnail_receive_config.decoders.push_back(decoder);
1671 thumbnail_receive_config.rtp.remote_ssrc =
1672 thumbnail_send_config.rtp.ssrcs[0];
1673
1674 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1675 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
nisse26e3abb2017-08-25 04:44:25 -07001676 thumbnail_receive_config.rtp
1677 .rtx_associated_payload_types[kSendRtxPayloadType] = kPayloadTypeVP8;
ilnika014cc52017-03-07 04:21:04 -08001678 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1679 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1680
1681 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1682 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1683 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1684 }
1685
ilnik98436952017-07-13 00:47:03 -07001686 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001687 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1688 thumbnail_send_configs_[i].Copy(),
1689 thumbnail_encoder_configs_[i].Copy()));
1690 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1691 thumbnail_receive_configs_[i].Copy()));
1692 }
1693}
1694
1695void VideoQualityTest::DestroyThumbnailStreams() {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001696 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_) {
ilnika014cc52017-03-07 04:21:04 -08001697 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001698 }
ilnika014cc52017-03-07 04:21:04 -08001699 thumbnail_send_streams_.clear();
1700 for (VideoReceiveStream* thumbnail_receive_stream :
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001701 thumbnail_receive_streams_) {
ilnika014cc52017-03-07 04:21:04 -08001702 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001703 }
ilnika014cc52017-03-07 04:21:04 -08001704 thumbnail_send_streams_.clear();
1705 thumbnail_receive_streams_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07001706 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1707 thumbnail_capturers_) {
1708 video_caputurer.reset();
1709 }
ilnika014cc52017-03-07 04:21:04 -08001710}
1711
ilnika014cc52017-03-07 04:21:04 -08001712void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1713 VideoStream thumbnail = DefaultThumbnailStream();
1714 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1715 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1716 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
1717 thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -08001718 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -08001719 }
1720}
1721
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001722std::unique_ptr<test::FrameGenerator> VideoQualityTest::CreateFrameGenerator(
1723 size_t video_idx) {
1724 // Setup frame generator.
1725 const size_t kWidth = 1850;
1726 const size_t kHeight = 1110;
1727 std::unique_ptr<test::FrameGenerator> frame_generator;
1728 if (params_.screenshare[video_idx].generate_slides) {
1729 frame_generator = test::FrameGenerator::CreateSlideGenerator(
1730 kWidth, kHeight,
1731 params_.screenshare[video_idx].slide_change_interval *
1732 params_.video[video_idx].fps);
ivica5d6a06c2015-09-17 05:30:24 -07001733 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001734 std::vector<std::string> slides = params_.screenshare[video_idx].slides;
1735 if (slides.size() == 0) {
1736 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1737 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1738 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1739 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1740 }
1741 if (params_.screenshare[video_idx].scroll_duration == 0) {
1742 // Cycle image every slide_change_interval seconds.
1743 frame_generator = test::FrameGenerator::CreateFromYuvFile(
1744 slides, kWidth, kHeight,
1745 params_.screenshare[video_idx].slide_change_interval *
1746 params_.video[video_idx].fps);
ivica5d6a06c2015-09-17 05:30:24 -07001747 } else {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001748 RTC_CHECK_LE(params_.video[video_idx].width, kWidth);
1749 RTC_CHECK_LE(params_.video[video_idx].height, kHeight);
1750 RTC_CHECK_GT(params_.screenshare[video_idx].slide_change_interval, 0);
1751 const int kPauseDurationMs =
1752 (params_.screenshare[video_idx].slide_change_interval -
1753 params_.screenshare[video_idx].scroll_duration) *
1754 1000;
1755 RTC_CHECK_LE(params_.screenshare[video_idx].scroll_duration,
1756 params_.screenshare[video_idx].slide_change_interval);
1757
1758 frame_generator = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1759 clock_, slides, kWidth, kHeight, params_.video[video_idx].width,
1760 params_.video[video_idx].height,
1761 params_.screenshare[video_idx].scroll_duration * 1000,
1762 kPauseDurationMs);
ivica5d6a06c2015-09-17 05:30:24 -07001763 }
1764 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001765 return frame_generator;
1766}
1767
1768void VideoQualityTest::CreateCapturers() {
1769 video_capturers_.resize(num_video_streams_);
1770 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1771 if (params_.screenshare[video_idx].enabled) {
1772 std::unique_ptr<test::FrameGenerator> frame_generator =
1773 CreateFrameGenerator(video_idx);
1774 test::FrameGeneratorCapturer* frame_generator_capturer =
1775 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator),
1776 params_.video[video_idx].fps);
1777 EXPECT_TRUE(frame_generator_capturer->Init());
1778 video_capturers_[video_idx].reset(frame_generator_capturer);
1779 } else {
1780 if (params_.video[video_idx].clip_name == "Generator") {
1781 video_capturers_[video_idx].reset(test::FrameGeneratorCapturer::Create(
1782 static_cast<int>(params_.video[video_idx].width),
1783 static_cast<int>(params_.video[video_idx].height),
1784 params_.video[video_idx].fps, clock_));
1785 } else if (params_.video[video_idx].clip_name.empty()) {
1786 video_capturers_[video_idx].reset(test::VcmCapturer::Create(
1787 params_.video[video_idx].width, params_.video[video_idx].height,
1788 params_.video[video_idx].fps,
1789 params_.video[video_idx].capture_device_index));
1790 if (!video_capturers_[video_idx]) {
1791 // Failed to get actual camera, use chroma generator as backup.
1792 video_capturers_[video_idx].reset(
1793 test::FrameGeneratorCapturer::Create(
1794 static_cast<int>(params_.video[video_idx].width),
1795 static_cast<int>(params_.video[video_idx].height),
1796 params_.video[video_idx].fps, clock_));
1797 }
1798 } else {
1799 video_capturers_[video_idx].reset(
1800 test::FrameGeneratorCapturer::CreateFromYuvFile(
1801 test::ResourcePath(params_.video[video_idx].clip_name, "yuv"),
1802 params_.video[video_idx].width, params_.video[video_idx].height,
1803 params_.video[video_idx].fps, clock_));
1804 ASSERT_TRUE(video_capturers_[video_idx])
1805 << "Could not create capturer for "
1806 << params_.video[video_idx].clip_name
1807 << ".yuv. Is this resource file present?";
1808 }
1809 }
1810 RTC_DCHECK(video_capturers_[video_idx].get());
1811 }
ivica5d6a06c2015-09-17 05:30:24 -07001812}
1813
Christoffer Rodbrob4bb4eb2017-11-13 13:03:52 +01001814std::unique_ptr<test::LayerFilteringTransport>
1815VideoQualityTest::CreateSendTransport() {
1816 return rtc::MakeUnique<test::LayerFilteringTransport>(
1817 &task_queue_, params_.pipe, sender_call_.get(), kPayloadTypeVP8,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001818 kPayloadTypeVP9, params_.video[0].selected_tl, params_.ss[0].selected_sl,
1819 payload_type_map_, kVideoSendSsrcs[0],
1820 static_cast<uint32_t>(kVideoSendSsrcs[0] + params_.ss[0].streams.size() -
1821 1));
Christoffer Rodbrob4bb4eb2017-11-13 13:03:52 +01001822}
1823
1824std::unique_ptr<test::DirectTransport>
1825VideoQualityTest::CreateReceiveTransport() {
1826 return rtc::MakeUnique<test::DirectTransport>(
1827 &task_queue_, params_.pipe, receiver_call_.get(), payload_type_map_);
1828}
1829
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001830void VideoQualityTest::CreateVideoStreams() {
1831 RTC_DCHECK(video_send_streams_.empty());
1832 RTC_DCHECK(video_receive_streams_.empty());
1833 RTC_DCHECK_EQ(video_send_configs_.size(), num_video_streams_);
1834 for (size_t i = 0; i < video_send_configs_.size(); ++i) {
1835 video_send_streams_.push_back(sender_call_->CreateVideoSendStream(
1836 video_send_configs_[i].Copy(), video_encoder_configs_[i].Copy()));
1837 }
1838 for (size_t i = 0; i < video_receive_configs_.size(); ++i) {
1839 video_receive_streams_.push_back(receiver_call_->CreateVideoReceiveStream(
1840 video_receive_configs_[i].Copy()));
1841 }
1842
1843 AssociateFlexfecStreamsWithVideoStreams();
1844}
1845
1846void VideoQualityTest::DestroyStreams() {
1847 CallTest::DestroyStreams();
1848
1849 for (VideoSendStream* video_send_stream : video_send_streams_)
1850 sender_call_->DestroyVideoSendStream(video_send_stream);
1851}
1852
sprang7a975f72015-10-12 06:33:21 -07001853void VideoQualityTest::RunWithAnalyzer(const Params& params) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001854 num_video_streams_ = params.call.dual_video ? 2 : 1;
eladalon413ee9a2017-08-22 04:02:52 -07001855 std::unique_ptr<test::LayerFilteringTransport> send_transport;
1856 std::unique_ptr<test::DirectTransport> recv_transport;
1857 FILE* graph_data_output_file = nullptr;
1858 std::unique_ptr<VideoAnalyzer> analyzer;
1859
sprangce4aef12015-11-02 07:23:20 -08001860 params_ = params;
1861
minyue626bc952016-10-31 05:47:02 -07001862 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001863 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1864 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001865 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001866
sprangce4aef12015-11-02 07:23:20 -08001867 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001868 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001869 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001870 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001871 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1872 << "!";
ivica87f83a92015-10-08 05:13:32 -07001873 }
sprang7a975f72015-10-12 06:33:21 -07001874
ilnik98436952017-07-13 00:47:03 -07001875 if (!params.logging.rtc_event_log_name.empty()) {
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001876 send_event_log_ =
1877 RtcEventLog::Create(clock_, RtcEventLog::EncodingType::Legacy);
1878 recv_event_log_ =
1879 RtcEventLog::Create(clock_, RtcEventLog::EncodingType::Legacy);
1880 std::unique_ptr<RtcEventLogOutputFile> send_output(
Bjorn Tereliusde939432017-11-20 17:38:14 +01001881 rtc::MakeUnique<RtcEventLogOutputFile>(
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001882 params.logging.rtc_event_log_name + "_send",
1883 RtcEventLog::kUnlimitedOutput));
1884 std::unique_ptr<RtcEventLogOutputFile> recv_output(
1885 rtc::MakeUnique<RtcEventLogOutputFile>(
1886 params.logging.rtc_event_log_name + "_recv",
1887 RtcEventLog::kUnlimitedOutput));
1888 bool event_log_started =
1889 send_event_log_->StartLogging(std::move(send_output),
1890 RtcEventLog::kImmediateOutput) &&
1891 recv_event_log_->StartLogging(std::move(recv_output),
1892 RtcEventLog::kImmediateOutput);
ilnik98436952017-07-13 00:47:03 -07001893 RTC_DCHECK(event_log_started);
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001894 } else {
1895 send_event_log_ = RtcEventLog::CreateNull();
1896 recv_event_log_ = RtcEventLog::CreateNull();
ilnik98436952017-07-13 00:47:03 -07001897 }
1898
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001899 Call::Config send_call_config(send_event_log_.get());
1900 Call::Config recv_call_config(recv_event_log_.get());
1901 send_call_config.bitrate_config = params.call.call_bitrate_config;
1902 recv_call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001903
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +01001904 task_queue_.SendTask([this, &send_call_config, &recv_call_config,
1905 &send_transport, &recv_transport]() {
1906 CreateCalls(send_call_config, recv_call_config);
1907 send_transport = CreateSendTransport();
1908 recv_transport = CreateReceiveTransport();
1909 });
stefanf116bd02015-10-27 08:29:42 -07001910
sprangce4aef12015-11-02 07:23:20 -08001911 std::string graph_title = params_.analyzer.graph_title;
1912 if (graph_title.empty())
1913 graph_title = VideoQualityTest::GenerateGraphTitle();
sprangc1b57a12017-02-28 08:50:47 -08001914 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
eladalon413ee9a2017-08-22 04:02:52 -07001915 analyzer = rtc::MakeUnique<VideoAnalyzer>(
1916 send_transport.get(), params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001917 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001918 is_quick_test_enabled
1919 ? kFramesSentInQuickTest
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001920 : params_.analyzer.test_durations_secs * params_.video[0].fps,
sprangce4aef12015-11-02 07:23:20 -08001921 graph_data_output_file, graph_title,
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001922 kVideoSendSsrcs[params_.ss[0].selected_stream],
1923 kSendRtxSsrcs[params_.ss[0].selected_stream],
1924 static_cast<size_t>(params_.ss[0].selected_stream),
1925 params.ss[0].selected_sl, params_.video[0].selected_tl,
1926 is_quick_test_enabled, clock_, params_.logging.rtp_dump_name);
ivica5d6a06c2015-09-17 05:30:24 -07001927
eladalon413ee9a2017-08-22 04:02:52 -07001928 task_queue_.SendTask([&]() {
1929 analyzer->SetCall(sender_call_.get());
1930 analyzer->SetReceiver(receiver_call_->Receiver());
1931 send_transport->SetReceiver(analyzer.get());
1932 recv_transport->SetReceiver(sender_call_->Receiver());
ivica5d6a06c2015-09-17 05:30:24 -07001933
eladalon413ee9a2017-08-22 04:02:52 -07001934 SetupVideo(analyzer.get(), recv_transport.get());
1935 SetupThumbnails(analyzer.get(), recv_transport.get());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001936 video_receive_configs_[params_.ss[0].selected_stream].renderer =
eladalon413ee9a2017-08-22 04:02:52 -07001937 analyzer.get();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001938 video_send_configs_[0].pre_encode_callback = analyzer->pre_encode_proxy();
1939 RTC_DCHECK(!video_send_configs_[0].post_encode_callback);
1940 video_send_configs_[0].post_encode_callback =
1941 analyzer->encode_timing_proxy();
kthelgason2bc68642017-02-07 07:02:22 -08001942
eladalon413ee9a2017-08-22 04:02:52 -07001943 CreateFlexfecStreams();
1944 CreateVideoStreams();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001945 analyzer->SetSendStream(video_send_streams_[0]);
eladalon413ee9a2017-08-22 04:02:52 -07001946 if (video_receive_streams_.size() == 1)
1947 analyzer->SetReceiveStream(video_receive_streams_[0]);
ivica5d6a06c2015-09-17 05:30:24 -07001948
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001949 video_send_streams_[0]->SetSource(analyzer->OutputInterface(),
1950 degradation_preference_);
eladalon413ee9a2017-08-22 04:02:52 -07001951 SetupThumbnailCapturers(params_.call.num_thumbnails);
1952 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1953 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1954 degradation_preference_);
1955 }
ilnika014cc52017-03-07 04:21:04 -08001956
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001957 CreateCapturers();
ivicac1cc8542015-10-08 03:44:06 -07001958
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001959 analyzer->SetSource(video_capturers_[0].get(), params_.ss[0].infer_streams);
ilnika014cc52017-03-07 04:21:04 -08001960
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001961 for (size_t video_idx = 1; video_idx < num_video_streams_; ++video_idx) {
1962 video_send_streams_[video_idx]->SetSource(
1963 video_capturers_[video_idx].get(), degradation_preference_);
1964 }
1965
1966 StartEncodedFrameLogs(video_send_streams_[0]);
1967 StartEncodedFrameLogs(
1968 video_receive_streams_[params_.ss[0].selected_stream]);
1969 for (VideoSendStream* video_send_stream : video_send_streams_)
1970 video_send_stream->Start();
eladalon413ee9a2017-08-22 04:02:52 -07001971 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1972 thumbnail_send_stream->Start();
1973 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1974 receive_stream->Start();
1975 for (VideoReceiveStream* thumbnail_receive_stream :
1976 thumbnail_receive_streams_)
1977 thumbnail_receive_stream->Start();
ilnika014cc52017-03-07 04:21:04 -08001978
eladalon413ee9a2017-08-22 04:02:52 -07001979 analyzer->StartMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -07001980
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001981 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1982 video_capturers_[video_idx]->Start();
1983 }
eladalon413ee9a2017-08-22 04:02:52 -07001984 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1985 thumbnail_capturers_) {
1986 video_caputurer->Start();
1987 }
1988 });
ivica5d6a06c2015-09-17 05:30:24 -07001989
eladalon413ee9a2017-08-22 04:02:52 -07001990 analyzer->Wait();
ivica5d6a06c2015-09-17 05:30:24 -07001991
Ilya Nikolaevskiy2c72fe82017-10-02 13:01:04 +02001992 event_log_->StopLogging();
1993
eladalon413ee9a2017-08-22 04:02:52 -07001994 task_queue_.SendTask([&]() {
1995 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1996 thumbnail_capturers_)
1997 video_caputurer->Stop();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01001998 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
1999 video_capturers_[video_idx]->Stop();
2000 }
eladalon413ee9a2017-08-22 04:02:52 -07002001 for (VideoReceiveStream* thumbnail_receive_stream :
2002 thumbnail_receive_streams_)
2003 thumbnail_receive_stream->Stop();
2004 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2005 receive_stream->Stop();
2006 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
2007 thumbnail_send_stream->Stop();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002008 for (VideoSendStream* video_send_stream : video_send_streams_)
2009 video_send_stream->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07002010
eladalon413ee9a2017-08-22 04:02:52 -07002011 DestroyStreams();
2012 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -07002013
eladalon413ee9a2017-08-22 04:02:52 -07002014 if (graph_data_output_file)
2015 fclose(graph_data_output_file);
2016
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002017 video_capturers_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07002018 send_transport.reset();
2019 recv_transport.reset();
2020
2021 DestroyCalls();
2022 });
ivica5d6a06c2015-09-17 05:30:24 -07002023}
2024
minyuea27172d2016-11-01 05:59:29 -07002025void VideoQualityTest::SetupAudio(int send_channel_id,
2026 int receive_channel_id,
minyuea27172d2016-11-01 05:59:29 -07002027 Transport* transport,
2028 AudioReceiveStream** audio_receive_stream) {
2029 audio_send_config_ = AudioSendStream::Config(transport);
2030 audio_send_config_.voe_channel_id = send_channel_id;
2031 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
2032
2033 // Add extension to enable audio send side BWE, and allow audio bit rate
2034 // adaptation.
2035 audio_send_config_.rtp.extensions.clear();
2036 if (params_.call.send_side_bwe) {
2037 audio_send_config_.rtp.extensions.push_back(
2038 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
2039 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08002040 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
2041 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07002042 }
ossu20a4b3f2017-04-27 02:08:52 -07002043 audio_send_config_.send_codec_spec =
2044 rtc::Optional<AudioSendStream::Config::SendCodecSpec>(
2045 {kAudioSendPayloadType,
2046 {"OPUS", 48000, 2,
2047 {{"usedtx", (params_.audio.dtx ? "1" : "0")},
2048 {"stereo", "1"}}}});
Rasmus Brandt31027342017-09-29 13:48:12 +00002049 audio_send_config_.encoder_factory = encoder_factory_;
sprang1168fd42017-06-21 09:00:17 -07002050 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
minyuea27172d2016-11-01 05:59:29 -07002051
2052 AudioReceiveStream::Config audio_config;
2053 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
2054 audio_config.rtcp_send_transport = transport;
2055 audio_config.voe_channel_id = receive_channel_id;
2056 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
2057 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
2058 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
Rasmus Brandt31027342017-09-29 13:48:12 +00002059 audio_config.decoder_factory = decoder_factory_;
minyue20c84cc2017-04-10 16:57:57 -07002060 audio_config.decoder_map = {{kAudioSendPayloadType, {"OPUS", 48000, 2}}};
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002061 if (params_.video[0].enabled && params_.audio.sync_video)
minyuea27172d2016-11-01 05:59:29 -07002062 audio_config.sync_group = kSyncGroup;
2063
sprang1168fd42017-06-21 09:00:17 -07002064 *audio_receive_stream =
2065 receiver_call_->CreateAudioReceiveStream(audio_config);
minyuea27172d2016-11-01 05:59:29 -07002066}
2067
minyue73208662016-08-18 06:28:55 -07002068void VideoQualityTest::RunWithRenderers(const Params& params) {
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002069 num_video_streams_ = params.call.dual_video ? 2 : 1;
eladalon413ee9a2017-08-22 04:02:52 -07002070 std::unique_ptr<test::LayerFilteringTransport> send_transport;
2071 std::unique_ptr<test::DirectTransport> recv_transport;
minyue73208662016-08-18 06:28:55 -07002072 ::VoiceEngineState voe;
minyuea27172d2016-11-01 05:59:29 -07002073 std::unique_ptr<test::VideoRenderer> local_preview;
eladalon413ee9a2017-08-22 04:02:52 -07002074 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers;
minyue73208662016-08-18 06:28:55 -07002075 AudioReceiveStream* audio_receive_stream = nullptr;
minyue73208662016-08-18 06:28:55 -07002076
eladalon413ee9a2017-08-22 04:02:52 -07002077 task_queue_.SendTask([&]() {
2078 params_ = params;
2079 CheckParams();
palmkviste75f2042016-09-28 06:19:48 -07002080
eladalon413ee9a2017-08-22 04:02:52 -07002081 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
2082 // match the full stack tests.
2083 Call::Config call_config(event_log_.get());
2084 call_config.bitrate_config = params_.call.call_bitrate_config;
Niels Möller2bf9e732017-08-14 11:26:16 +02002085
Fredrik Solenberg2a877972017-12-15 16:42:15 +01002086 rtc::scoped_refptr<test::FakeAudioDevice> fake_audio_device =
2087 new rtc::RefCountedObject<test::FakeAudioDevice>(
2088 test::FakeAudioDevice::CreatePulsedNoiseCapturer(32000, 48000),
2089 test::FakeAudioDevice::CreateDiscardRenderer(48000),
2090 1.f);
ivica5d6a06c2015-09-17 05:30:24 -07002091
eladalon413ee9a2017-08-22 04:02:52 -07002092 if (params_.audio.enabled) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +01002093 CreateVoiceEngine(&voe, fake_audio_device.get(), decoder_factory_);
eladalon413ee9a2017-08-22 04:02:52 -07002094 AudioState::Config audio_state_config;
2095 audio_state_config.voice_engine = voe.voice_engine;
2096 audio_state_config.audio_mixer = AudioMixerImpl::Create();
Fredrik Solenberg2a877972017-12-15 16:42:15 +01002097 audio_state_config.audio_processing = AudioProcessing::Create();
2098 audio_state_config.audio_device_module = fake_audio_device;
eladalon413ee9a2017-08-22 04:02:52 -07002099 call_config.audio_state = AudioState::Create(audio_state_config);
Fredrik Solenbergd3195342017-11-21 20:33:05 +01002100 fake_audio_device->RegisterAudioCallback(
2101 call_config.audio_state->audio_transport());
eladalon413ee9a2017-08-22 04:02:52 -07002102 }
minyue73208662016-08-18 06:28:55 -07002103
eladalon413ee9a2017-08-22 04:02:52 -07002104 CreateCalls(call_config, call_config);
2105
2106 // TODO(minyue): consider if this is a good transport even for audio only
2107 // calls.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002108 send_transport = CreateSendTransport();
eladalon413ee9a2017-08-22 04:02:52 -07002109
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002110 recv_transport = CreateReceiveTransport();
eladalon413ee9a2017-08-22 04:02:52 -07002111
2112 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
2113 // least share as much code as possible. That way this test would also match
2114 // the full stack tests better.
2115 send_transport->SetReceiver(receiver_call_->Receiver());
2116 recv_transport->SetReceiver(sender_call_->Receiver());
2117
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002118 if (params_.video[0].enabled) {
eladalon413ee9a2017-08-22 04:02:52 -07002119 // Create video renderers.
2120 local_preview.reset(test::VideoRenderer::Create(
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002121 "Local Preview", params_.video[0].width, params_.video[0].height));
eladalon413ee9a2017-08-22 04:02:52 -07002122
2123 SetupVideo(send_transport.get(), recv_transport.get());
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002124 video_send_configs_[0].pre_encode_callback = local_preview.get();
eladalon413ee9a2017-08-22 04:02:52 -07002125
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002126 size_t num_streams_processed = 0;
2127 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
2128 const size_t selected_stream_id = params_.ss[video_idx].selected_stream;
2129 const size_t num_streams = params_.ss[video_idx].streams.size();
2130 if (selected_stream_id == num_streams) {
2131 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
2132 std::ostringstream oss;
2133 oss << "Loopback Video #" << video_idx << " - Stream #"
2134 << static_cast<int>(stream_id);
2135 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2136 oss.str().c_str(),
2137 params_.ss[video_idx].streams[stream_id].width,
2138 params_.ss[video_idx].streams[stream_id].height));
2139 video_receive_configs_[stream_id + num_streams_processed].renderer =
2140 loopback_renderers.back().get();
2141 if (params_.audio.enabled && params_.audio.sync_video)
2142 video_receive_configs_[stream_id + num_streams_processed]
2143 .sync_group = kSyncGroup;
2144 }
2145 } else {
2146 std::ostringstream oss;
2147 oss << "Loopback Video #" << video_idx;
2148 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2149 oss.str().c_str(),
2150 params_.ss[video_idx].streams[selected_stream_id].width,
2151 params_.ss[video_idx].streams[selected_stream_id].height));
2152 video_receive_configs_[selected_stream_id + num_streams_processed]
2153 .renderer = loopback_renderers.back().get();
eladalon413ee9a2017-08-22 04:02:52 -07002154 if (params_.audio.enabled && params_.audio.sync_video)
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002155 video_receive_configs_[num_streams_processed + selected_stream_id]
2156 .sync_group = kSyncGroup;
eladalon413ee9a2017-08-22 04:02:52 -07002157 }
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002158 num_streams_processed += num_streams;
eladalon413ee9a2017-08-22 04:02:52 -07002159 }
eladalon413ee9a2017-08-22 04:02:52 -07002160 CreateFlexfecStreams();
2161 CreateVideoStreams();
2162
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002163 CreateCapturers();
2164 for (size_t video_idx = 0; video_idx < num_video_streams_; ++video_idx) {
2165 video_send_streams_[video_idx]->SetSource(
2166 video_capturers_[video_idx].get(), degradation_preference_);
2167 }
eladalon413ee9a2017-08-22 04:02:52 -07002168 }
2169
2170 if (params_.audio.enabled) {
2171 SetupAudio(voe.send_channel_id, voe.receive_channel_id,
2172 send_transport.get(), &audio_receive_stream);
2173 }
2174
2175 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2176 StartEncodedFrameLogs(receive_stream);
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002177 StartEncodedFrameLogs(video_send_streams_[0]);
eladalon413ee9a2017-08-22 04:02:52 -07002178
2179 // Start sending and receiving video.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002180 if (params_.video[0].enabled) {
eladalon413ee9a2017-08-22 04:02:52 -07002181 for (VideoReceiveStream* video_receive_stream : video_receive_streams_)
2182 video_receive_stream->Start();
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002183 for (VideoSendStream* video_send_stream : video_send_streams_)
2184 video_send_stream->Start();
2185 for (auto& video_capturer : video_capturers_)
2186 video_capturer->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002187 }
2188
2189 if (params_.audio.enabled) {
2190 // Start receiving audio.
2191 audio_receive_stream->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002192
2193 // Start sending audio.
2194 audio_send_stream_->Start();
eladalon413ee9a2017-08-22 04:02:52 -07002195 }
2196 });
minyue73208662016-08-18 06:28:55 -07002197
ivica5d6a06c2015-09-17 05:30:24 -07002198 test::PressEnterToContinue();
2199
eladalon413ee9a2017-08-22 04:02:52 -07002200 task_queue_.SendTask([&]() {
2201 if (params_.audio.enabled) {
2202 // Stop sending audio.
eladalon413ee9a2017-08-22 04:02:52 -07002203 audio_send_stream_->Stop();
minyue73208662016-08-18 06:28:55 -07002204
eladalon413ee9a2017-08-22 04:02:52 -07002205 // Stop receiving audio.
eladalon413ee9a2017-08-22 04:02:52 -07002206 audio_receive_stream->Stop();
eladalon413ee9a2017-08-22 04:02:52 -07002207 }
minyue73208662016-08-18 06:28:55 -07002208
eladalon413ee9a2017-08-22 04:02:52 -07002209 // Stop receiving and sending video.
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002210 if (params_.video[0].enabled) {
2211 for (auto& video_capturer : video_capturers_)
2212 video_capturer->Stop();
2213 for (VideoSendStream* video_send_stream : video_send_streams_)
2214 video_send_stream->Stop();
2215 DestroyStreams();
brandtr1293aca2016-11-16 22:47:29 -08002216 }
minyue73208662016-08-18 06:28:55 -07002217
Ilya Nikolaevskiy255d1cd2017-12-21 18:02:59 +01002218 video_capturers_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07002219 send_transport.reset();
2220 recv_transport.reset();
sprang1168fd42017-06-21 09:00:17 -07002221
eladalon413ee9a2017-08-22 04:02:52 -07002222 if (params_.audio.enabled)
2223 DestroyVoiceEngine(&voe);
2224
2225 local_preview.reset();
2226 loopback_renderers.clear();
2227
2228 DestroyCalls();
2229 });
ivica5d6a06c2015-09-17 05:30:24 -07002230}
2231
palmkviste75f2042016-09-28 06:19:48 -07002232void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002233 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002234 std::ostringstream str;
2235 str << send_logs_++;
2236 std::string prefix =
ilnik98436952017-07-13 00:47:03 -07002237 params_.logging.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07002238 stream->EnableEncodedFrameRecording(
2239 std::vector<rtc::PlatformFile>(
2240 {rtc::CreatePlatformFile(prefix + "1.ivf"),
2241 rtc::CreatePlatformFile(prefix + "2.ivf"),
2242 rtc::CreatePlatformFile(prefix + "3.ivf")}),
ilnik98436952017-07-13 00:47:03 -07002243 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002244 }
2245}
ilnikcb8c1462017-03-09 09:23:30 -08002246
palmkviste75f2042016-09-28 06:19:48 -07002247void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002248 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002249 std::ostringstream str;
2250 str << receive_logs_++;
2251 std::string path =
ilnik98436952017-07-13 00:47:03 -07002252 params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07002253 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
ilnik98436952017-07-13 00:47:03 -07002254 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002255 }
2256}
ivica5d6a06c2015-09-17 05:30:24 -07002257} // namespace webrtc