blob: 6428dc29ca40388b2cc1c9ca071134f889c66667 [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>
ilnikcb8c1462017-03-09 09:23:30 -080016#include <set>
sprangce4aef12015-11-02 07:23:20 -080017#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080018#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070019#include <vector>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/optional.h"
22#include "call/call.h"
23#include "common_video/libyuv/include/webrtc_libyuv.h"
Elad Alon83ccca12017-10-04 13:18:26 +020024#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "logging/rtc_event_log/rtc_event_log.h"
26#include "media/engine/webrtcvideoengine.h"
27#include "modules/audio_mixer/audio_mixer_impl.h"
28#include "modules/rtp_rtcp/include/rtp_header_parser.h"
29#include "modules/rtp_rtcp/source/rtp_format.h"
30#include "modules/rtp_rtcp/source/rtp_utility.h"
31#include "modules/video_coding/codecs/h264/include/h264.h"
32#include "modules/video_coding/codecs/vp8/include/vp8.h"
33#include "modules/video_coding/codecs/vp8/include/vp8_common_types.h"
34#include "modules/video_coding/codecs/vp9/include/vp9.h"
35#include "rtc_base/checks.h"
36#include "rtc_base/cpu_time.h"
37#include "rtc_base/event.h"
38#include "rtc_base/flags.h"
39#include "rtc_base/format_macros.h"
40#include "rtc_base/logging.h"
41#include "rtc_base/memory_usage.h"
42#include "rtc_base/pathutils.h"
43#include "rtc_base/platform_file.h"
44#include "rtc_base/ptr_util.h"
45#include "rtc_base/timeutils.h"
46#include "system_wrappers/include/cpu_info.h"
47#include "system_wrappers/include/field_trial.h"
48#include "test/gtest.h"
49#include "test/layer_filtering_transport.h"
50#include "test/run_loop.h"
51#include "test/statistics.h"
52#include "test/testsupport/fileutils.h"
53#include "test/testsupport/frame_writer.h"
Edward Lemuraf8659a2017-09-27 14:46:24 +020054#include "test/testsupport/test_artifacts.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "test/vcm_capturer.h"
56#include "test/video_renderer.h"
57#include "voice_engine/include/voe_base.h"
minyue73208662016-08-18 06:28:55 -070058
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "test/rtp_file_writer.h"
ilnik98436952017-07-13 00:47:03 -070060
ilnikee42d192017-08-22 07:16:20 -070061DEFINE_bool(save_worst_frame,
62 false,
63 "Enable saving a frame with the lowest PSNR to a jpeg file in the "
Edward Lemuraf8659a2017-09-27 14:46:24 +020064 "test_artifacts_dir");
ilnikee42d192017-08-22 07:16:20 -070065
minyue73208662016-08-18 06:28:55 -070066namespace {
67
68constexpr int kSendStatsPollingIntervalMs = 1000;
minyue20c84cc2017-04-10 16:57:57 -070069
minyue73208662016-08-18 06:28:55 -070070constexpr size_t kMaxComparisons = 10;
71constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080072constexpr int kOpusMinBitrateBps = 6000;
73constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 00:53:12 -080074constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 04:21:04 -080075constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
76constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 06:28:55 -070077
sprang1168fd42017-06-21 09:00:17 -070078constexpr int kDefaultMaxQp = cricket::WebRtcVideoChannel::kDefaultQpMax;
79
minyue73208662016-08-18 06:28:55 -070080struct VoiceEngineState {
81 VoiceEngineState()
82 : voice_engine(nullptr),
83 base(nullptr),
minyue73208662016-08-18 06:28:55 -070084 send_channel_id(-1),
85 receive_channel_id(-1) {}
86
87 webrtc::VoiceEngine* voice_engine;
88 webrtc::VoEBase* base;
minyue73208662016-08-18 06:28:55 -070089 int send_channel_id;
90 int receive_channel_id;
91};
92
peaha9cc40b2017-06-29 08:32:09 -070093void CreateVoiceEngine(
94 VoiceEngineState* voe,
95 webrtc::AudioProcessing* apm,
96 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory) {
minyue73208662016-08-18 06:28:55 -070097 voe->voice_engine = webrtc::VoiceEngine::Create();
98 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
peaha9cc40b2017-06-29 08:32:09 -070099 EXPECT_EQ(0, voe->base->Init(nullptr, apm, decoder_factory));
solenberg88499ec2016-09-07 07:34:41 -0700100 webrtc::VoEBase::ChannelConfig config;
101 config.enable_voice_pacing = true;
102 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 06:28:55 -0700103 EXPECT_GE(voe->send_channel_id, 0);
104 voe->receive_channel_id = voe->base->CreateChannel();
105 EXPECT_GE(voe->receive_channel_id, 0);
106}
107
108void DestroyVoiceEngine(VoiceEngineState* voe) {
109 voe->base->DeleteChannel(voe->send_channel_id);
110 voe->send_channel_id = -1;
111 voe->base->DeleteChannel(voe->receive_channel_id);
112 voe->receive_channel_id = -1;
113 voe->base->Release();
114 voe->base = nullptr;
minyue73208662016-08-18 06:28:55 -0700115
116 webrtc::VoiceEngine::Delete(voe->voice_engine);
117 voe->voice_engine = nullptr;
118}
119
perkjfa10b552016-10-02 23:45:26 -0700120class VideoStreamFactory
121 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
122 public:
123 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
124 : streams_(streams) {}
125
126 private:
127 std::vector<webrtc::VideoStream> CreateEncoderStreams(
128 int width,
129 int height,
130 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -0800131 // The highest layer must match the incoming resolution.
132 std::vector<webrtc::VideoStream> streams = streams_;
133 streams[streams_.size() - 1].height = height;
134 streams[streams_.size() - 1].width = width;
135 return streams;
perkjfa10b552016-10-02 23:45:26 -0700136 }
137
138 std::vector<webrtc::VideoStream> streams_;
139};
140
brandtr504b95e2016-12-21 02:54:35 -0800141bool IsFlexfec(int payload_type) {
142 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
143}
144
minyue73208662016-08-18 06:28:55 -0700145} // namespace
ivica5d6a06c2015-09-17 05:30:24 -0700146
147namespace webrtc {
148
ivica5d6a06c2015-09-17 05:30:24 -0700149class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -0700150 public Transport,
ilnik1e7732c2017-02-23 05:07:56 -0800151 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 05:30:24 -0700152 public:
sprangce4aef12015-11-02 07:23:20 -0800153 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700154 const std::string& test_label,
155 double avg_psnr_threshold,
156 double avg_ssim_threshold,
157 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800158 FILE* graph_data_output_file,
159 const std::string& graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -0800160 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 09:16:05 -0800161 uint32_t rtx_ssrc_to_analyze,
ilnikcb8c1462017-03-09 09:23:30 -0800162 size_t selected_stream,
ilnik1e7732c2017-02-23 05:07:56 -0800163 int selected_sl,
164 int selected_tl,
ilnik6b826ef2017-06-16 06:53:48 -0700165 bool is_quick_test_enabled,
ilnik98436952017-07-13 00:47:03 -0700166 Clock* clock,
167 std::string rtp_dump_name)
perkja49cbd32016-09-16 07:53:41 -0700168 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700169 receiver_(nullptr),
stefan889d9652017-07-05 03:03:02 -0700170 call_(nullptr),
ivica5d6a06c2015-09-17 05:30:24 -0700171 send_stream_(nullptr),
philipelfd870db2017-01-23 03:22:15 -0800172 receive_stream_(nullptr),
ilnik6b826ef2017-06-16 06:53:48 -0700173 captured_frame_forwarder_(this, clock),
ivica5d6a06c2015-09-17 05:30:24 -0700174 test_label_(test_label),
175 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800176 graph_title_(graph_title),
177 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 09:16:05 -0800178 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 09:23:30 -0800179 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 05:07:56 -0800180 selected_sl_(selected_sl),
181 selected_tl_(selected_tl),
pbos14fe7082016-04-20 06:35:56 -0700182 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100183 encode_timing_proxy_(this),
stefan889d9652017-07-05 03:03:02 -0700184 last_fec_bytes_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700185 frames_to_process_(duration_frames),
186 frames_recorded_(0),
187 frames_processed_(0),
188 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700189 dropped_frames_before_first_encode_(0),
190 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700191 last_render_time_(0),
192 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 05:07:56 -0800193 total_media_bytes_(0),
194 first_sending_time_(0),
195 last_sending_time_(0),
ilnikdf92c5c2017-02-23 02:08:44 -0800196 cpu_time_(0),
197 wallclock_time_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700198 avg_psnr_threshold_(avg_psnr_threshold),
199 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 00:53:12 -0800200 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100201 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100202 comparison_available_event_(false, false),
ilnik98436952017-07-13 00:47:03 -0700203 done_(true, false),
204 clock_(clock),
205 start_ms_(clock->TimeInMilliseconds()) {
ivica5d6a06c2015-09-17 05:30:24 -0700206 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
207
208 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
209 // so that we don't accidentally starve "real" worker threads (codec etc).
210 // Also, don't allocate more than kMaxComparisonThreads, even if there are
211 // spare cores.
212
213 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800214 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700215 static const uint32_t kMinCoresLeft = 4;
216 static const uint32_t kMaxComparisonThreads = 8;
217
218 if (num_cores <= kMinCoresLeft) {
219 num_cores = 1;
220 } else {
221 num_cores -= kMinCoresLeft;
222 num_cores = std::min(num_cores, kMaxComparisonThreads);
223 }
224
225 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100226 rtc::PlatformThread* thread =
227 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
228 thread->Start();
229 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700230 }
ilnik98436952017-07-13 00:47:03 -0700231
232 if (!rtp_dump_name.empty()) {
233 fprintf(stdout, "Writing rtp dump to %s\n", rtp_dump_name.c_str());
234 rtp_file_writer_.reset(test::RtpFileWriter::Create(
235 test::RtpFileWriter::kRtpDump, rtp_dump_name));
236 }
ivica5d6a06c2015-09-17 05:30:24 -0700237 }
238
239 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100240 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
241 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700242 delete thread;
243 }
244 }
245
246 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
247
ilnik6b826ef2017-06-16 06:53:48 -0700248 void SetSource(test::VideoCapturer* video_capturer, bool respect_sink_wants) {
249 if (respect_sink_wants)
250 captured_frame_forwarder_.SetSource(video_capturer);
251 rtc::VideoSinkWants wants;
252 video_capturer->AddOrUpdateSink(InputInterface(), wants);
253 }
254
stefan889d9652017-07-05 03:03:02 -0700255 void SetCall(Call* call) {
256 rtc::CritScope lock(&crit_);
257 RTC_DCHECK(!call_);
258 call_ = call;
259 }
260
perkja49cbd32016-09-16 07:53:41 -0700261 void SetSendStream(VideoSendStream* stream) {
262 rtc::CritScope lock(&crit_);
263 RTC_DCHECK(!send_stream_);
264 send_stream_ = stream;
265 }
266
philipelfd870db2017-01-23 03:22:15 -0800267 void SetReceiveStream(VideoReceiveStream* stream) {
268 rtc::CritScope lock(&crit_);
269 RTC_DCHECK(!receive_stream_);
270 receive_stream_ = stream;
271 }
272
perkja49cbd32016-09-16 07:53:41 -0700273 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
274 return &captured_frame_forwarder_;
275 }
276 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
277 return &captured_frame_forwarder_;
278 }
279
ivica5d6a06c2015-09-17 05:30:24 -0700280 DeliveryStatus DeliverPacket(MediaType media_type,
281 const uint8_t* packet,
282 size_t length,
283 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700284 // Ignore timestamps of RTCP packets. They're not synchronized with
285 // RTP packet timestamps and so they would confuse wrap_handler_.
286 if (RtpHeaderParser::IsRtcp(packet, length)) {
287 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
288 }
ilnik98436952017-07-13 00:47:03 -0700289
290 if (rtp_file_writer_) {
291 test::RtpPacket p;
292 memcpy(p.data, packet, length);
293 p.length = length;
294 p.original_length = length;
295 p.time_ms = clock_->TimeInMilliseconds() - start_ms_;
296 rtp_file_writer_->WritePacket(&p);
297 }
298
sprangce4aef12015-11-02 07:23:20 -0800299 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700300 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800301 parser.Parse(&header);
ilnik46a00212017-02-10 09:16:05 -0800302 if (!IsFlexfec(header.payloadType) &&
303 (header.ssrc == ssrc_to_analyze_ ||
304 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 02:54:35 -0800305 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
306 // (FlexFEC and media are sent on different SSRCs, which have different
307 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800308 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 05:30:24 -0700309 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800310 int64_t timestamp =
311 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
312 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700313 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
314 }
315
316 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
317 }
318
Peter Boströme4499152016-02-05 11:13:28 +0100319 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700320 rtc::CritScope crit(&comparison_lock_);
321 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
322 }
323
pbos14fe7082016-04-20 06:35:56 -0700324 void PreEncodeOnFrame(const VideoFrame& video_frame) {
325 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800326 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 06:35:56 -0700327 while (frames_.front().timestamp() != video_frame.timestamp()) {
328 ++dropped_frames_before_first_encode_;
329 frames_.pop_front();
330 RTC_CHECK(!frames_.empty());
331 }
ilnik3dd5ad92017-02-09 04:58:53 -0800332 first_encoded_timestamp_ =
333 rtc::Optional<uint32_t>(video_frame.timestamp());
334 }
335 }
336
337 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
338 rtc::CritScope lock(&crit_);
339 if (!first_sent_timestamp_ &&
ilnikcb8c1462017-03-09 09:23:30 -0800340 encoded_frame.stream_id_ == selected_stream_) {
ilnik3dd5ad92017-02-09 04:58:53 -0800341 first_sent_timestamp_ = rtc::Optional<uint32_t>(encoded_frame.timestamp_);
pbos14fe7082016-04-20 06:35:56 -0700342 }
343 }
344
stefan1d8a5062015-10-02 03:39:33 -0700345 bool SendRtp(const uint8_t* packet,
346 size_t length,
347 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800348 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700349 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800350 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700351
sprangce4aef12015-11-02 07:23:20 -0800352 int64_t current_time =
353 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 02:23:28 -0800354
sprangce4aef12015-11-02 07:23:20 -0800355 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700356 {
357 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800358 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 08:32:53 -0800359 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 04:58:53 -0800360 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 04:08:56 -0800361 }
ilnik3dd5ad92017-02-09 04:58:53 -0800362
363 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 02:54:35 -0800364 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
365 // (FlexFEC and media are sent on different SSRCs, which have different
366 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800367 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 02:54:35 -0800368 int64_t timestamp =
369 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
370 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 05:07:56 -0800371
372 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 02:54:35 -0800373 encoded_frame_sizes_[timestamp] +=
374 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 05:07:56 -0800375 total_media_bytes_ +=
376 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 02:54:35 -0800377 }
ilnik1e7732c2017-02-23 05:07:56 -0800378 if (first_sending_time_ == 0)
379 first_sending_time_ = current_time;
380 last_sending_time_ = current_time;
sprangce4aef12015-11-02 07:23:20 -0800381 }
ivica5d6a06c2015-09-17 05:30:24 -0700382 }
sprangce4aef12015-11-02 07:23:20 -0800383 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700384 }
385
386 bool SendRtcp(const uint8_t* packet, size_t length) override {
387 return transport_->SendRtcp(packet, length);
388 }
389
nisseeb83a1a2016-03-21 01:27:56 -0700390 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700391 int64_t render_time_ms =
392 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700393
394 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 02:08:44 -0800395
396 StartExcludingCpuThreadTime();
397
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700398 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800399 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700400
sprang16daaa52016-03-09 01:30:24 -0800401 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700402 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700403 // No previous frame rendered, this one was dropped after sending but
404 // before rendering.
405 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 07:02:22 -0800406 } else {
407 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
408 render_time_ms);
pbos14fe7082016-04-20 06:35:56 -0700409 }
ivica5d6a06c2015-09-17 05:30:24 -0700410 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700411 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700412 }
413
414 VideoFrame reference_frame = frames_.front();
415 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800416 int64_t reference_timestamp =
417 wrap_handler_.Unwrap(reference_frame.timestamp());
418 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800419 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100420 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800421 ++send_timestamp;
422 }
sprang16daaa52016-03-09 01:30:24 -0800423 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700424
425 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
426
nisse97f0b932016-05-26 09:44:40 -0700427 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ilnikdf92c5c2017-02-23 02:08:44 -0800428
429 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 05:30:24 -0700430 }
431
ivica5d6a06c2015-09-17 05:30:24 -0700432 void Wait() {
433 // Frame comparisons can be very expensive. Wait for test to be done, but
434 // at time-out check if frames_processed is going up. If so, give it more
435 // time, otherwise fail. Hopefully this will reduce test flakiness.
436
Peter Boström8c38e8b2015-11-26 17:45:47 +0100437 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800438
ivica5d6a06c2015-09-17 05:30:24 -0700439 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700440 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100441 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700442 int frames_processed;
443 {
444 rtc::CritScope crit(&comparison_lock_);
445 frames_processed = frames_processed_;
446 }
447
448 // Print some output so test infrastructure won't think we've crashed.
449 const char* kKeepAliveMessages[3] = {
450 "Uh, I'm-I'm not quite dead, sir.",
451 "Uh, I-I think uh, I could pull through, sir.",
452 "Actually, I think I'm all right to come with you--"};
453 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
454
455 if (last_frames_processed == -1) {
456 last_frames_processed = frames_processed;
457 continue;
458 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100459 if (frames_processed == last_frames_processed) {
460 EXPECT_GT(frames_processed, last_frames_processed)
461 << "Analyzer stalled while waiting for test to finish.";
462 done_.Set();
463 break;
464 }
ivica5d6a06c2015-09-17 05:30:24 -0700465 last_frames_processed = frames_processed;
466 }
467
468 if (iteration > 0)
469 printf("- Farewell, sweet Concorde!\n");
470
Peter Boström8c38e8b2015-11-26 17:45:47 +0100471 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700472 }
473
pbos14fe7082016-04-20 06:35:56 -0700474 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
475 return &pre_encode_proxy_;
476 }
Peter Boströme4499152016-02-05 11:13:28 +0100477 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
478
ilnikdf92c5c2017-02-23 02:08:44 -0800479 void StartMeasuringCpuProcessTime() {
480 rtc::CritScope lock(&cpu_measurement_lock_);
481 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
482 wallclock_time_ -= rtc::SystemTimeNanos();
483 }
484
485 void StopMeasuringCpuProcessTime() {
486 rtc::CritScope lock(&cpu_measurement_lock_);
487 cpu_time_ += rtc::GetProcessCpuTimeNanos();
488 wallclock_time_ += rtc::SystemTimeNanos();
489 }
490
491 void StartExcludingCpuThreadTime() {
492 rtc::CritScope lock(&cpu_measurement_lock_);
493 cpu_time_ += rtc::GetThreadCpuTimeNanos();
494 }
495
496 void StopExcludingCpuThreadTime() {
497 rtc::CritScope lock(&cpu_measurement_lock_);
498 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
499 }
500
501 double GetCpuUsagePercent() {
502 rtc::CritScope lock(&cpu_measurement_lock_);
503 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
504 }
505
sprangce4aef12015-11-02 07:23:20 -0800506 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700507 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700508
509 private:
510 struct FrameComparison {
511 FrameComparison()
512 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800513 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700514 send_time_ms(0),
515 recv_time_ms(0),
516 render_time_ms(0),
517 encoded_frame_size(0) {}
518
519 FrameComparison(const VideoFrame& reference,
520 const VideoFrame& render,
521 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800522 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700523 int64_t send_time_ms,
524 int64_t recv_time_ms,
525 int64_t render_time_ms,
526 size_t encoded_frame_size)
527 : reference(reference),
528 render(render),
529 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800530 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700531 send_time_ms(send_time_ms),
532 recv_time_ms(recv_time_ms),
533 render_time_ms(render_time_ms),
534 encoded_frame_size(encoded_frame_size) {}
535
nissedf2ceb82016-12-15 06:29:53 -0800536 FrameComparison(bool dropped,
537 int64_t input_time_ms,
538 int64_t send_time_ms,
539 int64_t recv_time_ms,
540 int64_t render_time_ms,
541 size_t encoded_frame_size)
542 : dropped(dropped),
543 input_time_ms(input_time_ms),
544 send_time_ms(send_time_ms),
545 recv_time_ms(recv_time_ms),
546 render_time_ms(render_time_ms),
547 encoded_frame_size(encoded_frame_size) {}
548
549 rtc::Optional<VideoFrame> reference;
550 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700551 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800552 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700553 int64_t send_time_ms;
554 int64_t recv_time_ms;
555 int64_t render_time_ms;
556 size_t encoded_frame_size;
557 };
558
559 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700560 Sample(int dropped,
561 int64_t input_time_ms,
562 int64_t send_time_ms,
563 int64_t recv_time_ms,
564 int64_t render_time_ms,
565 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700566 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700567 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700568 : dropped(dropped),
569 input_time_ms(input_time_ms),
570 send_time_ms(send_time_ms),
571 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700572 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700573 encoded_frame_size(encoded_frame_size),
574 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700575 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700576
ivica8d15bd62015-10-07 02:43:12 -0700577 int dropped;
578 int64_t input_time_ms;
579 int64_t send_time_ms;
580 int64_t recv_time_ms;
581 int64_t render_time_ms;
582 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700583 double psnr;
584 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700585 };
586
Peter Boströme4499152016-02-05 11:13:28 +0100587 // This class receives the send-side OnEncodeTiming and is provided to not
588 // conflict with the receiver-side pre_decode_callback.
589 class OnEncodeTimingProxy : public EncodedFrameObserver {
590 public:
591 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
592
593 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
594 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
595 }
ilnik3dd5ad92017-02-09 04:58:53 -0800596 void EncodedFrameCallback(const EncodedFrame& frame) override {
597 parent_->PostEncodeFrameCallback(frame);
598 }
Peter Boströme4499152016-02-05 11:13:28 +0100599
600 private:
601 VideoAnalyzer* const parent_;
602 };
603
pbos14fe7082016-04-20 06:35:56 -0700604 // This class receives the send-side OnFrame callback and is provided to not
605 // conflict with the receiver-side renderer callback.
606 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
607 public:
608 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
609
610 void OnFrame(const VideoFrame& video_frame) override {
611 parent_->PreEncodeOnFrame(video_frame);
612 }
613
614 private:
615 VideoAnalyzer* const parent_;
616 };
617
ilnik1e7732c2017-02-23 05:07:56 -0800618 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
619 size_t length,
620 const RTPHeader& header) {
ilnik863f03b2017-07-11 02:38:36 -0700621 if (header.payloadType != test::CallTest::kPayloadTypeVP9 &&
622 header.payloadType != test::CallTest::kPayloadTypeVP8) {
ilnik1e7732c2017-02-23 05:07:56 -0800623 return true;
624 } else {
625 // Get VP8 and VP9 specific header to check layers indexes.
626 const uint8_t* payload = packet + header.headerLength;
627 const size_t payload_length = length - header.headerLength;
628 const size_t payload_data_length = payload_length - header.paddingLength;
ilnik863f03b2017-07-11 02:38:36 -0700629 const bool is_vp8 = header.payloadType == test::CallTest::kPayloadTypeVP8;
ilnik1e7732c2017-02-23 05:07:56 -0800630 std::unique_ptr<RtpDepacketizer> depacketizer(
631 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
632 RtpDepacketizer::ParsedPayload parsed_payload;
633 bool result =
634 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
635 RTC_DCHECK(result);
636 const int temporal_idx = static_cast<int>(
637 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
638 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
639 const int spatial_idx = static_cast<int>(
640 is_vp8 ? kNoSpatialIdx
641 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
642 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
643 temporal_idx <= selected_tl_) &&
644 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
645 spatial_idx <= selected_sl_);
646 }
647 }
648
ivica5d6a06c2015-09-17 05:30:24 -0700649 void AddFrameComparison(const VideoFrame& reference,
650 const VideoFrame& render,
651 bool dropped,
652 int64_t render_time_ms)
danilchapa37de392017-09-09 04:17:22 -0700653 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800654 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
655 int64_t send_time_ms = send_times_[reference_timestamp];
656 send_times_.erase(reference_timestamp);
657 int64_t recv_time_ms = recv_times_[reference_timestamp];
658 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700659
sprangce4aef12015-11-02 07:23:20 -0800660 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800661 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800662 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800663 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800664 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
665 if (it != encoded_frame_sizes_.end())
666 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700667
ivica5d6a06c2015-09-17 05:30:24 -0700668 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700669 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800670 comparisons_.push_back(FrameComparison(reference, render, dropped,
671 reference.ntp_time_ms(),
672 send_time_ms, recv_time_ms,
673 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700674 } else {
nissedf2ceb82016-12-15 06:29:53 -0800675 comparisons_.push_back(FrameComparison(dropped,
676 reference.ntp_time_ms(),
677 send_time_ms, recv_time_ms,
678 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700679 }
Peter Boström5811a392015-12-10 13:02:50 +0100680 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700681 }
682
tommi0f8b4032017-02-22 11:22:05 -0800683 static void PollStatsThread(void* obj) {
684 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 05:30:24 -0700685 }
686
tommi0f8b4032017-02-22 11:22:05 -0800687 void PollStats() {
688 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
689 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700690
stefan889d9652017-07-05 03:03:02 -0700691 Call::Stats call_stats = call_->GetStats();
692 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
693
tommi0f8b4032017-02-22 11:22:05 -0800694 VideoSendStream::Stats send_stats = send_stream_->GetStats();
695 // It's not certain that we yet have estimates for any of these stats.
696 // Check that they are positive before mixing them in.
697 if (send_stats.encode_frame_rate > 0)
698 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
699 if (send_stats.avg_encode_time_ms > 0)
700 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
701 if (send_stats.encode_usage_percent > 0)
702 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
703 if (send_stats.media_bitrate_bps > 0)
704 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
stefan889d9652017-07-05 03:03:02 -0700705 size_t fec_bytes = 0;
706 for (auto kv : send_stats.substreams) {
707 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
708 kv.second.rtp_stats.fec.padding_bytes;
709 }
710 fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8);
711 last_fec_bytes_ = fec_bytes;
philipelfd870db2017-01-23 03:22:15 -0800712
tommi0f8b4032017-02-22 11:22:05 -0800713 if (receive_stream_ != nullptr) {
714 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
715 if (receive_stats.decode_ms > 0)
716 decode_time_ms_.AddSample(receive_stats.decode_ms);
717 if (receive_stats.max_decode_ms > 0)
718 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
719 }
ilnikdaa574d2017-03-01 06:46:05 -0800720
721 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 03:22:15 -0800722 }
ivica5d6a06c2015-09-17 05:30:24 -0700723 }
724
725 static bool FrameComparisonThread(void* obj) {
726 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
727 }
728
729 bool CompareFrames() {
730 if (AllFramesRecorded())
731 return false;
732
ivica5d6a06c2015-09-17 05:30:24 -0700733 FrameComparison comparison;
734
735 if (!PopComparison(&comparison)) {
736 // Wait until new comparison task is available, or test is done.
737 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100738 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700739 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100740 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700741 return false;
742 }
743 return true; // Try again.
744 }
745
ilnikdf92c5c2017-02-23 02:08:44 -0800746 StartExcludingCpuThreadTime();
747
ivica5d6a06c2015-09-17 05:30:24 -0700748 PerformFrameComparison(comparison);
749
ilnikdf92c5c2017-02-23 02:08:44 -0800750 StopExcludingCpuThreadTime();
751
ivica5d6a06c2015-09-17 05:30:24 -0700752 if (FrameProcessed()) {
753 PrintResults();
754 if (graph_data_output_file_)
755 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100756 done_.Set();
757 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700758 return false;
759 }
760
761 return true;
762 }
763
764 bool PopComparison(FrameComparison* comparison) {
765 rtc::CritScope crit(&comparison_lock_);
766 // If AllFramesRecorded() is true, it means we have already popped
767 // frames_to_process_ frames from comparisons_, so there is no more work
768 // for this thread to be done. frames_processed_ might still be lower if
769 // all comparisons are not done, but those frames are currently being
770 // worked on by other threads.
771 if (comparisons_.empty() || AllFramesRecorded())
772 return false;
773
774 *comparison = comparisons_.front();
775 comparisons_.pop_front();
776
777 FrameRecorded();
778 return true;
779 }
780
781 // Increment counter for number of frames received for comparison.
782 void FrameRecorded() {
783 rtc::CritScope crit(&comparison_lock_);
784 ++frames_recorded_;
785 }
786
787 // Returns true if all frames to be compared have been taken from the queue.
788 bool AllFramesRecorded() {
789 rtc::CritScope crit(&comparison_lock_);
790 assert(frames_recorded_ <= frames_to_process_);
791 return frames_recorded_ == frames_to_process_;
792 }
793
794 // Increase count of number of frames processed. Returns true if this was the
795 // last frame to be processed.
796 bool FrameProcessed() {
797 rtc::CritScope crit(&comparison_lock_);
798 ++frames_processed_;
799 assert(frames_processed_ <= frames_to_process_);
800 return frames_processed_ == frames_to_process_;
801 }
802
803 void PrintResults() {
ilnikdf92c5c2017-02-23 02:08:44 -0800804 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -0700805 rtc::CritScope crit(&comparison_lock_);
806 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800807 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 01:02:15 -0700808 PrintResult("sender_time", sender_time_, " ms");
809 PrintResult("receiver_time", receiver_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700810 PrintResult("total_delay_incl_network", end_to_end_, " ms");
811 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700812 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800813 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800814 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
stefan889d9652017-07-05 03:03:02 -0700815 PrintResult("fec_bitrate", fec_bitrate_bps_, " bps");
816 PrintResult("send_bandwidth", send_bandwidth_bps_, " bps");
philipelfd870db2017-01-23 03:22:15 -0800817
ilnik59cac992017-07-25 05:45:03 -0700818 if (worst_frame_) {
819 printf("RESULT min_psnr: %s = %lf dB\n", test_label_.c_str(),
820 worst_frame_->psnr);
821 }
822
philipelfd870db2017-01-23 03:22:15 -0800823 if (receive_stream_ != nullptr) {
824 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800825 }
ivica5d6a06c2015-09-17 05:30:24 -0700826
pbos14fe7082016-04-20 06:35:56 -0700827 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
828 dropped_frames_);
ilnikdf92c5c2017-02-23 02:08:44 -0800829 printf("RESULT cpu_usage: %s = %lf %%\n", test_label_.c_str(),
830 GetCpuUsagePercent());
ilnikdaa574d2017-03-01 06:46:05 -0800831
832#if defined(WEBRTC_WIN)
833 // On Linux and Mac in Resident Set some unused pages may be counted.
834 // Therefore this metric will depend on order in which tests are run and
835 // will be flaky.
836 PrintResult("memory_usage", memory_usage_, " bytes");
837#endif
ilnik59cac992017-07-25 05:45:03 -0700838
ilnikee42d192017-08-22 07:16:20 -0700839 // Saving only the worst frame for manual analysis. Intention here is to
840 // only detect video corruptions and not to track picture quality. Thus,
841 // jpeg is used here.
oprypin9b2f20c2017-08-29 05:51:57 -0700842 if (FLAG_save_worst_frame && worst_frame_) {
ilnikee42d192017-08-22 07:16:20 -0700843 std::string output_dir;
Edward Lemuraf8659a2017-09-27 14:46:24 +0200844 test::GetTestArtifactsDir(&output_dir);
ilnikee42d192017-08-22 07:16:20 -0700845 std::string output_path =
846 rtc::Pathname(output_dir, test_label_ + ".jpg").pathname();
847 LOG(LS_INFO) << "Saving worst frame to " << output_path;
848 test::JpegFrameWriter frame_writer(output_path);
849 RTC_CHECK(frame_writer.WriteFrame(worst_frame_->frame,
850 100 /*best quality*/));
ilnik59cac992017-07-25 05:45:03 -0700851 }
ilnikdaa574d2017-03-01 06:46:05 -0800852
ilnik9ae0d762017-02-15 00:53:12 -0800853 // Disable quality check for quick test, as quality checks may fail
854 // because too few samples were collected.
855 if (!is_quick_test_enabled_) {
856 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
857 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
858 }
ivica5d6a06c2015-09-17 05:30:24 -0700859 }
860
861 void PerformFrameComparison(const FrameComparison& comparison) {
862 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700863 double psnr = -1.0;
864 double ssim = -1.0;
ilnik6b826ef2017-06-16 06:53:48 -0700865 if (comparison.reference && !comparison.dropped) {
nissedf2ceb82016-12-15 06:29:53 -0800866 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
867 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700868 }
ivica5d6a06c2015-09-17 05:30:24 -0700869
ivica5d6a06c2015-09-17 05:30:24 -0700870 rtc::CritScope crit(&comparison_lock_);
ilnik59cac992017-07-25 05:45:03 -0700871
872 if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) {
873 worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render});
874 }
875
ivica5d6a06c2015-09-17 05:30:24 -0700876 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800877 samples_.push_back(Sample(
878 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
879 comparison.recv_time_ms, comparison.render_time_ms,
880 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700881 }
stefanb1797672016-08-11 07:00:57 -0700882 if (psnr >= 0.0)
883 psnr_.AddSample(psnr);
884 if (ssim >= 0.0)
885 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700886
887 if (comparison.dropped) {
888 ++dropped_frames_;
889 return;
890 }
891 if (last_render_time_ != 0)
892 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
893 last_render_time_ = comparison.render_time_ms;
894
nissedf2ceb82016-12-15 06:29:53 -0800895 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800896 if (comparison.recv_time_ms > 0) {
897 // If recv_time_ms == 0, this frame consisted of a packets which were all
898 // lost in the transport. Since we were able to render the frame, however,
899 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
900 // happens internally in Call, and we can therefore here not know which
901 // FEC packets that protected the lost media packets. Consequently, we
902 // were not able to record a meaningful recv_time_ms. We therefore skip
903 // this sample.
904 //
905 // The reasoning above does not hold for ULPFEC and RTX, as for those
906 // strategies the timestamp of the received packets is set to the
907 // timestamp of the protected/retransmitted media packet. I.e., then
908 // recv_time_ms != 0, even though the media packets were lost.
909 receiver_time_.AddSample(comparison.render_time_ms -
910 comparison.recv_time_ms);
911 }
nissedf2ceb82016-12-15 06:29:53 -0800912 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700913 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
914 }
915
916 void PrintResult(const char* result_type,
917 test::Statistics stats,
918 const char* unit) {
919 printf("RESULT %s: %s = {%f, %f}%s\n",
920 result_type,
921 test_label_.c_str(),
922 stats.Mean(),
923 stats.StandardDeviation(),
924 unit);
925 }
926
927 void PrintSamplesToFile(void) {
928 FILE* out = graph_data_output_file_;
929 rtc::CritScope crit(&comparison_lock_);
930 std::sort(samples_.begin(), samples_.end(),
931 [](const Sample& A, const Sample& B) -> bool {
932 return A.input_time_ms < B.input_time_ms;
933 });
934
sprangce4aef12015-11-02 07:23:20 -0800935 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700936 fprintf(out, "%" PRIuS "\n", samples_.size());
937 fprintf(out,
938 "dropped "
939 "input_time_ms "
940 "send_time_ms "
941 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700942 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700943 "encoded_frame_size "
944 "psnr "
945 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700946 "encode_time_ms\n");
947 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700948 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700949 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
950 int encode_time_ms;
951 if (it != samples_encode_time_ms_.end()) {
952 encode_time_ms = it->second;
953 } else {
954 ++missing_encode_time_samples;
955 encode_time_ms = -1;
956 }
957 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
958 " %lf %lf %d\n",
959 sample.dropped, sample.input_time_ms, sample.send_time_ms,
960 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700961 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700962 encode_time_ms);
963 }
964 if (missing_encode_time_samples) {
965 fprintf(stderr,
966 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
967 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700968 }
969 }
970
ilnik1e7732c2017-02-23 05:07:56 -0800971 double GetAverageMediaBitrateBps() {
972 if (last_sending_time_ == first_sending_time_) {
973 return 0;
974 } else {
975 return static_cast<double>(total_media_bytes_) * 8 /
976 (last_sending_time_ - first_sending_time_) *
977 rtc::kNumMillisecsPerSec;
978 }
979 }
980
perkja49cbd32016-09-16 07:53:41 -0700981 // Implements VideoSinkInterface to receive captured frames from a
982 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
983 // as a source to VideoSendStream.
984 // It forwards all input frames to the VideoAnalyzer for later comparison and
985 // forwards the captured frames to the VideoSendStream.
986 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
987 public rtc::VideoSourceInterface<VideoFrame> {
988 public:
ilnik6b826ef2017-06-16 06:53:48 -0700989 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer, Clock* clock)
990 : analyzer_(analyzer),
991 send_stream_input_(nullptr),
992 video_capturer_(nullptr),
993 clock_(clock) {}
994
995 void SetSource(test::VideoCapturer* video_capturer) {
996 video_capturer_ = video_capturer;
997 }
perkja49cbd32016-09-16 07:53:41 -0700998
999 private:
1000 void OnFrame(const VideoFrame& video_frame) override {
1001 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -08001002 // Frames from the capturer does not have a rtp timestamp.
1003 // Create one so it can be used for comparison.
1004 RTC_DCHECK_EQ(0, video_frame.timestamp());
ilnik6b826ef2017-06-16 06:53:48 -07001005 if (video_frame.ntp_time_ms() == 0)
1006 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
perkja49cbd32016-09-16 07:53:41 -07001007 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -08001008 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -07001009 rtc::CritScope lock(&crit_);
1010 if (send_stream_input_)
ilnikb82ac6a2017-05-02 00:48:41 -07001011 send_stream_input_->OnFrame(copy);
perkja49cbd32016-09-16 07:53:41 -07001012 }
1013
1014 // Called when |send_stream_.SetSource()| is called.
1015 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
1016 const rtc::VideoSinkWants& wants) override {
ilnik267041c2017-06-27 07:21:01 -07001017 {
1018 rtc::CritScope lock(&crit_);
1019 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
1020 send_stream_input_ = sink;
1021 }
ilnik6b826ef2017-06-16 06:53:48 -07001022 if (video_capturer_) {
1023 video_capturer_->AddOrUpdateSink(this, wants);
1024 }
perkja49cbd32016-09-16 07:53:41 -07001025 }
1026
1027 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
1028 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
1029 rtc::CritScope lock(&crit_);
1030 RTC_DCHECK(sink == send_stream_input_);
1031 send_stream_input_ = nullptr;
1032 }
1033
1034 VideoAnalyzer* const analyzer_;
1035 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07001036 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_
1037 RTC_GUARDED_BY(crit_);
ilnik6b826ef2017-06-16 06:53:48 -07001038 test::VideoCapturer* video_capturer_;
1039 Clock* clock_;
perkja49cbd32016-09-16 07:53:41 -07001040 };
1041
1042 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
1043 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -08001044 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -07001045 }
1046
stefan889d9652017-07-05 03:03:02 -07001047 Call* call_;
perkja49cbd32016-09-16 07:53:41 -07001048 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -08001049 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -07001050 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -07001051 const std::string test_label_;
1052 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -08001053 const std::string graph_title_;
1054 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -08001055 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 09:23:30 -08001056 const size_t selected_stream_;
ilnik1e7732c2017-02-23 05:07:56 -08001057 const int selected_sl_;
1058 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -07001059 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +01001060 OnEncodeTimingProxy encode_timing_proxy_;
danilchapa37de392017-09-09 04:17:22 -07001061 std::vector<Sample> samples_ RTC_GUARDED_BY(comparison_lock_);
1062 std::map<int64_t, int> samples_encode_time_ms_
1063 RTC_GUARDED_BY(comparison_lock_);
1064 test::Statistics sender_time_ RTC_GUARDED_BY(comparison_lock_);
1065 test::Statistics receiver_time_ RTC_GUARDED_BY(comparison_lock_);
1066 test::Statistics psnr_ RTC_GUARDED_BY(comparison_lock_);
1067 test::Statistics ssim_ RTC_GUARDED_BY(comparison_lock_);
1068 test::Statistics end_to_end_ RTC_GUARDED_BY(comparison_lock_);
1069 test::Statistics rendered_delta_ RTC_GUARDED_BY(comparison_lock_);
1070 test::Statistics encoded_frame_size_ RTC_GUARDED_BY(comparison_lock_);
1071 test::Statistics encode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
1072 test::Statistics encode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
1073 test::Statistics encode_usage_percent_ RTC_GUARDED_BY(comparison_lock_);
1074 test::Statistics decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
1075 test::Statistics decode_time_max_ms_ RTC_GUARDED_BY(comparison_lock_);
1076 test::Statistics media_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
1077 test::Statistics fec_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
1078 test::Statistics send_bandwidth_bps_ RTC_GUARDED_BY(comparison_lock_);
1079 test::Statistics memory_usage_ RTC_GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 06:46:05 -08001080
ilnik59cac992017-07-25 05:45:03 -07001081 struct FrameWithPsnr {
1082 double psnr;
1083 VideoFrame frame;
1084 };
1085
1086 // Rendered frame with worst PSNR is saved for further analysis.
danilchapa37de392017-09-09 04:17:22 -07001087 rtc::Optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_);
ilnik59cac992017-07-25 05:45:03 -07001088
stefan889d9652017-07-05 03:03:02 -07001089 size_t last_fec_bytes_;
ivica5d6a06c2015-09-17 05:30:24 -07001090
1091 const int frames_to_process_;
1092 int frames_recorded_;
1093 int frames_processed_;
1094 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -07001095 int dropped_frames_before_first_encode_;
1096 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -07001097 int64_t last_render_time_;
1098 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -08001099 int64_t total_media_bytes_;
1100 int64_t first_sending_time_;
1101 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -07001102
danilchapa37de392017-09-09 04:17:22 -07001103 int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
1104 int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
ilnikdf92c5c2017-02-23 02:08:44 -08001105 rtc::CriticalSection cpu_measurement_lock_;
1106
ivica5d6a06c2015-09-17 05:30:24 -07001107 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07001108 std::deque<VideoFrame> frames_ RTC_GUARDED_BY(crit_);
1109 rtc::Optional<VideoFrame> last_rendered_frame_ RTC_GUARDED_BY(crit_);
1110 rtc::TimestampWrapAroundHandler wrap_handler_ RTC_GUARDED_BY(crit_);
1111 std::map<int64_t, int64_t> send_times_ RTC_GUARDED_BY(crit_);
1112 std::map<int64_t, int64_t> recv_times_ RTC_GUARDED_BY(crit_);
1113 std::map<int64_t, size_t> encoded_frame_sizes_ RTC_GUARDED_BY(crit_);
1114 rtc::Optional<uint32_t> first_encoded_timestamp_ RTC_GUARDED_BY(crit_);
1115 rtc::Optional<uint32_t> first_sent_timestamp_ RTC_GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -07001116 const double avg_psnr_threshold_;
1117 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -08001118 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -07001119
1120 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001121 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1122 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001123 rtc::Event comparison_available_event_;
danilchapa37de392017-09-09 04:17:22 -07001124 std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001125 rtc::Event done_;
ilnik98436952017-07-13 00:47:03 -07001126
1127 std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
1128 Clock* const clock_;
1129 const int64_t start_ms_;
ivica5d6a06c2015-09-17 05:30:24 -07001130};
1131
Rasmus Brandt31027342017-09-29 13:48:12 +00001132class Vp8EncoderFactory : public cricket::WebRtcVideoEncoderFactory {
1133 public:
1134 Vp8EncoderFactory() {
1135 supported_codecs_.push_back(cricket::VideoCodec("VP8"));
1136 }
1137 ~Vp8EncoderFactory() override { RTC_CHECK(live_encoders_.empty()); }
1138
1139 const std::vector<cricket::VideoCodec>& supported_codecs() const override {
1140 return supported_codecs_;
1141 }
1142
1143 VideoEncoder* CreateVideoEncoder(const cricket::VideoCodec& codec) override {
1144 VideoEncoder* encoder = VP8Encoder::Create();
1145 live_encoders_.insert(encoder);
1146 return encoder;
1147 }
1148
1149 void DestroyVideoEncoder(VideoEncoder* encoder) override {
1150 auto it = live_encoders_.find(encoder);
1151 RTC_CHECK(it != live_encoders_.end());
1152 live_encoders_.erase(it);
1153 delete encoder;
1154 }
1155
1156 private:
1157 std::vector<cricket::VideoCodec> supported_codecs_;
1158 std::set<VideoEncoder*> live_encoders_;
1159};
1160
palmkviste75f2042016-09-28 06:19:48 -07001161VideoQualityTest::VideoQualityTest()
minyue20c84cc2017-04-10 16:57:57 -07001162 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {
1163 payload_type_map_ = test::CallTest::payload_type_map_;
1164 RTC_DCHECK(payload_type_map_.find(kPayloadTypeH264) ==
1165 payload_type_map_.end());
1166 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP8) ==
1167 payload_type_map_.end());
1168 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP9) ==
1169 payload_type_map_.end());
1170 payload_type_map_[kPayloadTypeH264] = webrtc::MediaType::VIDEO;
1171 payload_type_map_[kPayloadTypeVP8] = webrtc::MediaType::VIDEO;
1172 payload_type_map_[kPayloadTypeVP9] = webrtc::MediaType::VIDEO;
1173}
ivica5d6a06c2015-09-17 05:30:24 -07001174
minyue626bc952016-10-31 05:47:02 -07001175VideoQualityTest::Params::Params()
ilnik98436952017-07-13 00:47:03 -07001176 : call({false, Call::Config::BitrateConfig(), 0}),
Rasmus Brandt31027342017-09-29 13:48:12 +00001177 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
1178 false, ""}),
minyue4c8b9422017-03-21 04:11:43 -07001179 audio({false, false, false}),
erikvarga579de6f2017-08-29 09:12:57 -07001180 screenshare({false, false, 10, 0}),
minyue626bc952016-10-31 05:47:02 -07001181 analyzer({"", 0.0, 0.0, 0, "", ""}),
1182 pipe(),
ilnika014cc52017-03-07 04:21:04 -08001183 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}),
ilnik98436952017-07-13 00:47:03 -07001184 logging({false, "", "", ""}) {}
minyue626bc952016-10-31 05:47:02 -07001185
1186VideoQualityTest::Params::~Params() = default;
1187
ivica5d6a06c2015-09-17 05:30:24 -07001188void VideoQualityTest::TestBody() {}
1189
sprangce4aef12015-11-02 07:23:20 -08001190std::string VideoQualityTest::GenerateGraphTitle() const {
1191 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -07001192 ss << params_.video.codec;
1193 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
1194 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -08001195 if (params_.screenshare.scroll_duration)
1196 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
1197 if (params_.ss.streams.size() > 1)
1198 ss << ", Stream #" << params_.ss.selected_stream;
1199 if (params_.ss.num_spatial_layers > 1)
1200 ss << ", Layer #" << params_.ss.selected_sl;
1201 ss << ")";
1202 return ss.str();
1203}
1204
1205void VideoQualityTest::CheckParams() {
stefan7de8d642017-02-07 07:14:08 -08001206 if (!params_.video.enabled)
1207 return;
sprangce4aef12015-11-02 07:23:20 -08001208 // Add a default stream in none specified.
1209 if (params_.ss.streams.empty())
1210 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
1211 if (params_.ss.num_spatial_layers == 0)
1212 params_.ss.num_spatial_layers = 1;
1213
1214 if (params_.pipe.loss_percent != 0 ||
1215 params_.pipe.queue_length_packets != 0) {
1216 // Since LayerFilteringTransport changes the sequence numbers, we can't
1217 // use that feature with pack loss, since the NACK request would end up
1218 // retransmitting the wrong packets.
1219 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -08001220 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -07001221 RTC_CHECK(params_.video.selected_tl == -1 ||
1222 params_.video.selected_tl ==
1223 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001224 }
1225
1226 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
1227 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -07001228 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
1229 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
1230 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprang1168fd42017-06-21 09:00:17 -07001231 RTC_CHECK_LE(params_.ss.selected_stream, params_.ss.streams.size());
sprangce4aef12015-11-02 07:23:20 -08001232 for (const VideoStream& stream : params_.ss.streams) {
1233 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1234 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1235 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
sprangce4aef12015-11-02 07:23:20 -08001236 }
1237 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1238 // the total bitrate? We anyway have to update them in the case bitrate
1239 // estimator changes the total bitrates.
1240 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
1241 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
1242 RTC_CHECK(params_.ss.spatial_layers.empty() ||
1243 params_.ss.spatial_layers.size() ==
1244 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -07001245 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -08001246 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -07001247 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -08001248 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -08001249 }
ilnik98436952017-07-13 00:47:03 -07001250 RTC_CHECK_GE(params_.call.num_thumbnails, 0);
1251 if (params_.call.num_thumbnails > 0) {
ilnika014cc52017-03-07 04:21:04 -08001252 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
1253 RTC_CHECK_EQ(params_.ss.streams.size(), 3);
1254 RTC_CHECK_EQ(params_.video.num_temporal_layers, 3);
1255 RTC_CHECK_EQ(params_.video.codec, "VP8");
1256 }
sprangce4aef12015-11-02 07:23:20 -08001257}
1258
1259// Static.
1260std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1261 // Parse comma separated nonnegative integers, where some elements may be
1262 // empty. The empty values are replaced with -1.
1263 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1264 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1265 std::vector<int> result;
1266 if (str.empty())
1267 return result;
1268
1269 const char* p = str.c_str();
1270 int value = -1;
1271 int pos;
1272 while (*p) {
1273 if (*p == ',') {
1274 result.push_back(value);
1275 value = -1;
1276 ++p;
1277 continue;
1278 }
1279 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1280 << "Unexpected non-number value.";
1281 p += pos;
1282 }
1283 result.push_back(value);
1284 return result;
1285}
1286
1287// Static.
1288VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
1289 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -07001290 stream.width = params.video.width;
1291 stream.height = params.video.height;
1292 stream.max_framerate = params.video.fps;
1293 stream.min_bitrate_bps = params.video.min_bitrate_bps;
1294 stream.target_bitrate_bps = params.video.target_bitrate_bps;
1295 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprang1168fd42017-06-21 09:00:17 -07001296 stream.max_qp = kDefaultMaxQp;
sprang6ef1b342017-03-13 02:01:32 -07001297 // TODO(sprang): Can we make this less of a hack?
1298 if (params.video.num_temporal_layers == 2) {
1299 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
1300 } else if (params.video.num_temporal_layers == 3) {
1301 stream.temporal_layer_thresholds_bps.push_back(stream.max_bitrate_bps / 4);
1302 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
sprangff19d352017-09-06 07:14:02 -07001303 } else {
1304 RTC_CHECK_LE(params.video.num_temporal_layers, kMaxTemporalStreams);
1305 for (int i = 0; i < params.video.num_temporal_layers - 1; ++i) {
1306 stream.temporal_layer_thresholds_bps.push_back(static_cast<int>(
1307 stream.max_bitrate_bps * kVp8LayerRateAlloction[0][i] + 0.5));
1308 }
ilnika014cc52017-03-07 04:21:04 -08001309 }
1310 return stream;
1311}
1312
1313// Static.
1314VideoStream VideoQualityTest::DefaultThumbnailStream() {
1315 VideoStream stream;
1316 stream.width = 320;
1317 stream.height = 180;
1318 stream.max_framerate = 7;
1319 stream.min_bitrate_bps = 7500;
1320 stream.target_bitrate_bps = 37500;
1321 stream.max_bitrate_bps = 50000;
sprang1168fd42017-06-21 09:00:17 -07001322 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 07:23:20 -08001323 return stream;
1324}
1325
1326// Static.
1327void VideoQualityTest::FillScalabilitySettings(
1328 Params* params,
1329 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 09:00:17 -07001330 int num_streams,
sprangce4aef12015-11-02 07:23:20 -08001331 size_t selected_stream,
1332 int num_spatial_layers,
1333 int selected_sl,
1334 const std::vector<std::string>& sl_descriptors) {
sprang1168fd42017-06-21 09:00:17 -07001335 if (params->ss.streams.empty() && params->ss.infer_streams) {
1336 webrtc::VideoEncoderConfig encoder_config;
1337 encoder_config.content_type =
1338 params->screenshare.enabled
1339 ? webrtc::VideoEncoderConfig::ContentType::kScreen
1340 : webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo;
1341 encoder_config.max_bitrate_bps = params->video.max_bitrate_bps;
1342 encoder_config.min_transmit_bitrate_bps = params->video.min_transmit_bps;
1343 encoder_config.number_of_streams = num_streams;
1344 encoder_config.spatial_layers = params->ss.spatial_layers;
1345 encoder_config.video_stream_factory =
1346 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1347 params->video.codec, kDefaultMaxQp, params->video.fps,
1348 params->screenshare.enabled, true);
1349 params->ss.streams =
1350 encoder_config.video_stream_factory->CreateEncoderStreams(
1351 static_cast<int>(params->video.width),
1352 static_cast<int>(params->video.height), encoder_config);
1353 } else {
1354 // Read VideoStream and SpatialLayer elements from a list of comma separated
1355 // lists. To use a default value for an element, use -1 or leave empty.
1356 // Validity checks performed in CheckParams.
1357 RTC_CHECK(params->ss.streams.empty());
1358 for (auto descriptor : stream_descriptors) {
1359 if (descriptor.empty())
1360 continue;
1361 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
1362 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1363 if (v[0] != -1)
1364 stream.width = static_cast<size_t>(v[0]);
1365 if (v[1] != -1)
1366 stream.height = static_cast<size_t>(v[1]);
1367 if (v[2] != -1)
1368 stream.max_framerate = v[2];
1369 if (v[3] != -1)
1370 stream.min_bitrate_bps = v[3];
1371 if (v[4] != -1)
1372 stream.target_bitrate_bps = v[4];
1373 if (v[5] != -1)
1374 stream.max_bitrate_bps = v[5];
1375 if (v.size() > 6 && v[6] != -1)
1376 stream.max_qp = v[6];
1377 if (v.size() > 7) {
1378 stream.temporal_layer_thresholds_bps.clear();
1379 stream.temporal_layer_thresholds_bps.insert(
1380 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1381 } else {
1382 // Automatic TL thresholds for more than two layers not supported.
1383 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
1384 }
1385 params->ss.streams.push_back(stream);
sprangce4aef12015-11-02 07:23:20 -08001386 }
sprangce4aef12015-11-02 07:23:20 -08001387 }
sprangce4aef12015-11-02 07:23:20 -08001388
sprang1168fd42017-06-21 09:00:17 -07001389 params->ss.num_spatial_layers = std::max(1, num_spatial_layers);
1390 params->ss.selected_stream = selected_stream;
1391
sprangce4aef12015-11-02 07:23:20 -08001392 params->ss.selected_sl = selected_sl;
1393 RTC_CHECK(params->ss.spatial_layers.empty());
1394 for (auto descriptor : sl_descriptors) {
1395 if (descriptor.empty())
1396 continue;
1397 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1398 RTC_CHECK_GT(v[2], 0);
1399
1400 SpatialLayer layer;
1401 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1402 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1403 layer.target_bitrate_bps = v[2];
1404 params->ss.spatial_layers.push_back(layer);
1405 }
1406}
1407
minyuea27172d2016-11-01 05:59:29 -07001408void VideoQualityTest::SetupVideo(Transport* send_transport,
1409 Transport* recv_transport) {
brandtr8313a6f2017-01-13 07:41:19 -08001410 size_t num_video_streams = params_.ss.streams.size();
1411 size_t num_flexfec_streams = params_.video.flexfec ? 1 : 0;
1412 CreateSendConfig(num_video_streams, 0, num_flexfec_streams, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001413
1414 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001415 if (params_.video.codec == "H264") {
Rasmus Brandt31027342017-09-29 13:48:12 +00001416 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001417 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001418 } else if (params_.video.codec == "VP8") {
ilnikcb8c1462017-03-09 09:23:30 -08001419 if (params_.screenshare.enabled && params_.ss.streams.size() > 1) {
Rasmus Brandt31027342017-09-29 13:48:12 +00001420 // Simulcast screenshare needs a simulcast encoder adapter to work, since
1421 // encoders usually can't natively do simulcast with different frame rates
1422 // for the different layers.
ilnikcb8c1462017-03-09 09:23:30 -08001423 video_encoder_.reset(
Rasmus Brandt31027342017-09-29 13:48:12 +00001424 new SimulcastEncoderAdapter(new Vp8EncoderFactory()));
ilnikcb8c1462017-03-09 09:23:30 -08001425 } else {
Rasmus Brandt31027342017-09-29 13:48:12 +00001426 video_encoder_.reset(VP8Encoder::Create());
ilnikcb8c1462017-03-09 09:23:30 -08001427 }
ivica5d6a06c2015-09-17 05:30:24 -07001428 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001429 } else if (params_.video.codec == "VP9") {
Rasmus Brandt31027342017-09-29 13:48:12 +00001430 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001431 payload_type = kPayloadTypeVP9;
1432 } else {
1433 RTC_NOTREACHED() << "Codec not supported!";
1434 return;
1435 }
minyuea27172d2016-11-01 05:59:29 -07001436 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001437 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001438 video_send_config_.encoder_settings.payload_type = payload_type;
1439 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1440 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
brandtr8313a6f2017-01-13 07:41:19 -08001441 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001442 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001443
stefanff483612015-12-21 03:14:00 -08001444 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001445 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001446 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001447 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001448 test::kTransportSequenceNumberExtensionId));
1449 } else {
stefanff483612015-12-21 03:14:00 -08001450 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001451 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +02001452 }
ilnik00d802b2017-04-11 10:34:31 -07001453 video_send_config_.rtp.extensions.push_back(RtpExtension(
1454 RtpExtension::kVideoContentTypeUri, test::kVideoContentTypeExtensionId));
ilnik04f4d122017-06-19 07:18:55 -07001455 video_send_config_.rtp.extensions.push_back(RtpExtension(
1456 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +02001457
stefanff483612015-12-21 03:14:00 -08001458 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001459 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001460
brandtr1293aca2016-11-16 22:47:29 -08001461 video_send_config_.suspend_below_min_bitrate =
1462 params_.video.suspend_below_min_bitrate;
1463
perkjfa10b552016-10-02 23:45:26 -07001464 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1465 video_encoder_config_.max_bitrate_bps = 0;
1466 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1467 video_encoder_config_.max_bitrate_bps +=
1468 params_.ss.streams[i].max_bitrate_bps;
1469 }
ilnik6b826ef2017-06-16 06:53:48 -07001470 if (params_.ss.infer_streams) {
1471 video_encoder_config_.video_stream_factory =
1472 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1473 params_.video.codec, params_.ss.streams[0].max_qp,
1474 params_.video.fps, params_.screenshare.enabled, true);
1475 } else {
1476 video_encoder_config_.video_stream_factory =
1477 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1478 }
perkjfa10b552016-10-02 23:45:26 -07001479
stefanff483612015-12-21 03:14:00 -08001480 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001481
1482 CreateMatchingReceiveConfigs(recv_transport);
1483
sprang1168fd42017-06-21 09:00:17 -07001484 const bool decode_all_receive_streams =
1485 params_.ss.selected_stream == params_.ss.streams.size();
1486
brandtr8313a6f2017-01-13 07:41:19 -08001487 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001488 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
brandtr14742122017-01-27 04:53:07 -08001489 video_receive_configs_[i].rtp.rtx_ssrc = kSendRtxSsrcs[i];
nisse26e3abb2017-08-25 04:44:25 -07001490 video_receive_configs_[i]
1491 .rtp.rtx_associated_payload_types[kSendRtxPayloadType] = payload_type;
minyue626bc952016-10-31 05:47:02 -07001492 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
Stefan Holmer85d5ac72017-02-09 16:25:16 +01001493 video_receive_configs_[i].rtp.remb = !params_.call.send_side_bwe;
ilnik00d802b2017-04-11 10:34:31 -07001494 // Enable RTT calculation so NTP time estimator will work.
1495 video_receive_configs_[i].rtp.rtcp_xr.receiver_reference_time_report = true;
ilnik9fd9f6c2017-03-02 08:10:10 -08001496 // Force fake decoders on non-selected simulcast streams.
sprang1168fd42017-06-21 09:00:17 -07001497 if (!decode_all_receive_streams && i != params_.ss.selected_stream) {
ilnik9fd9f6c2017-03-02 08:10:10 -08001498 VideoReceiveStream::Decoder decoder;
1499 decoder.decoder = new test::FakeDecoder();
1500 decoder.payload_type = video_send_config_.encoder_settings.payload_type;
1501 decoder.payload_name = video_send_config_.encoder_settings.payload_name;
1502 video_receive_configs_[i].decoders.clear();
1503 allocated_decoders_.emplace_back(decoder.decoder);
1504 video_receive_configs_[i].decoders.push_back(decoder);
1505 }
sprangce4aef12015-11-02 07:23:20 -08001506 }
brandtr1293aca2016-11-16 22:47:29 -08001507
1508 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 07:41:19 -08001509 // Override send config constructed by CreateSendConfig.
sprang1168fd42017-06-21 09:00:17 -07001510 if (decode_all_receive_streams) {
1511 for (uint32_t media_ssrc : video_send_config_.rtp.ssrcs) {
1512 video_send_config_.rtp.flexfec.protected_media_ssrcs.push_back(
1513 media_ssrc);
1514 }
1515 } else {
1516 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1517 kVideoSendSsrcs[params_.ss.selected_stream]};
1518 }
brandtr1293aca2016-11-16 22:47:29 -08001519
brandtr8313a6f2017-01-13 07:41:19 -08001520 // The matching receive config is _not_ created by
1521 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1522 // Set up the receive config manually instead.
1523 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001524 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 06:59:19 -08001525 video_send_config_.rtp.flexfec.payload_type;
1526 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001527 flexfec_receive_config.protected_media_ssrcs =
1528 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001529 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001530 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1531 if (params_.call.send_side_bwe) {
1532 flexfec_receive_config.rtp_header_extensions.push_back(
1533 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1534 test::kTransportSequenceNumberExtensionId));
1535 } else {
1536 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1537 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1538 }
brandtr1293aca2016-11-16 22:47:29 -08001539 flexfec_receive_configs_.push_back(flexfec_receive_config);
brandtr7cd28b92017-09-22 00:26:25 -07001540 if (num_video_streams > 0) {
1541 video_receive_configs_[0].rtp.protected_by_flexfec = true;
1542 }
brandtr1293aca2016-11-16 22:47:29 -08001543 }
1544
1545 if (params_.video.ulpfec) {
1546 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1547 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1548 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1549
sprang1168fd42017-06-21 09:00:17 -07001550 if (decode_all_receive_streams) {
1551 for (auto it = video_receive_configs_.begin();
1552 it != video_receive_configs_.end(); ++it) {
nisse3b3622f2017-09-26 02:49:21 -07001553 it->rtp.red_payload_type =
sprang1168fd42017-06-21 09:00:17 -07001554 video_send_config_.rtp.ulpfec.red_payload_type;
nisse3b3622f2017-09-26 02:49:21 -07001555 it->rtp.ulpfec_payload_type =
sprang1168fd42017-06-21 09:00:17 -07001556 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
nisseca5706d2017-09-11 02:32:16 -07001557 it->rtp.rtx_associated_payload_types[video_send_config_.rtp.ulpfec
1558 .red_rtx_payload_type] =
1559 video_send_config_.rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 09:00:17 -07001560 }
1561 } else {
nisse3b3622f2017-09-26 02:49:21 -07001562 video_receive_configs_[params_.ss.selected_stream].rtp.red_payload_type =
sprang1168fd42017-06-21 09:00:17 -07001563 video_send_config_.rtp.ulpfec.red_payload_type;
1564 video_receive_configs_[params_.ss.selected_stream]
nisse3b3622f2017-09-26 02:49:21 -07001565 .rtp.ulpfec_payload_type =
sprang1168fd42017-06-21 09:00:17 -07001566 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1567 video_receive_configs_[params_.ss.selected_stream]
nisseca5706d2017-09-11 02:32:16 -07001568 .rtp.rtx_associated_payload_types[video_send_config_.rtp.ulpfec
1569 .red_rtx_payload_type] =
1570 video_send_config_.rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 09:00:17 -07001571 }
brandtr1293aca2016-11-16 22:47:29 -08001572 }
ivica5d6a06c2015-09-17 05:30:24 -07001573}
1574
ilnika014cc52017-03-07 04:21:04 -08001575void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1576 Transport* recv_transport) {
ilnik98436952017-07-13 00:47:03 -07001577 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001578 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1579
1580 // Thumbnails will be send in the other way: from receiver_call to
1581 // sender_call.
1582 VideoSendStream::Config thumbnail_send_config(recv_transport);
1583 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1584 thumbnail_send_config.encoder_settings.encoder =
1585 thumbnail_encoders_.back().get();
1586 thumbnail_send_config.encoder_settings.payload_name = params_.video.codec;
1587 thumbnail_send_config.encoder_settings.payload_type = kPayloadTypeVP8;
1588 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1589 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1590 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1591 thumbnail_send_config.rtp.extensions.clear();
1592 if (params_.call.send_side_bwe) {
1593 thumbnail_send_config.rtp.extensions.push_back(
1594 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1595 test::kTransportSequenceNumberExtensionId));
1596 } else {
1597 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1598 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1599 }
1600
1601 VideoEncoderConfig thumbnail_encoder_config;
1602 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1603 thumbnail_send_config.suspend_below_min_bitrate =
1604 params_.video.suspend_below_min_bitrate;
1605 thumbnail_encoder_config.number_of_streams = 1;
1606 thumbnail_encoder_config.max_bitrate_bps = 50000;
ilnik6b826ef2017-06-16 06:53:48 -07001607 if (params_.ss.infer_streams) {
1608 thumbnail_encoder_config.video_stream_factory =
1609 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1610 } else {
1611 thumbnail_encoder_config.video_stream_factory =
1612 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1613 params_.video.codec, params_.ss.streams[0].max_qp,
1614 params_.video.fps, params_.screenshare.enabled, true);
1615 }
ilnika014cc52017-03-07 04:21:04 -08001616 thumbnail_encoder_config.spatial_layers = params_.ss.spatial_layers;
1617
1618 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1619 thumbnail_receive_config.rtp.remb = false;
1620 thumbnail_receive_config.rtp.transport_cc = true;
1621 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1622 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1623 thumbnail_receive_config.rtp.extensions.push_back(extension);
1624 thumbnail_receive_config.renderer = &fake_renderer_;
1625
1626 VideoReceiveStream::Decoder decoder =
1627 test::CreateMatchingDecoder(thumbnail_send_config.encoder_settings);
1628 allocated_decoders_.push_back(
1629 std::unique_ptr<VideoDecoder>(decoder.decoder));
1630 thumbnail_receive_config.decoders.clear();
1631 thumbnail_receive_config.decoders.push_back(decoder);
1632 thumbnail_receive_config.rtp.remote_ssrc =
1633 thumbnail_send_config.rtp.ssrcs[0];
1634
1635 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1636 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
nisse26e3abb2017-08-25 04:44:25 -07001637 thumbnail_receive_config.rtp
1638 .rtx_associated_payload_types[kSendRtxPayloadType] = kPayloadTypeVP8;
ilnika014cc52017-03-07 04:21:04 -08001639 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1640 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1641
1642 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1643 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1644 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1645 }
1646
ilnik98436952017-07-13 00:47:03 -07001647 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001648 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1649 thumbnail_send_configs_[i].Copy(),
1650 thumbnail_encoder_configs_[i].Copy()));
1651 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1652 thumbnail_receive_configs_[i].Copy()));
1653 }
1654}
1655
1656void VideoQualityTest::DestroyThumbnailStreams() {
1657 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1658 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
1659 thumbnail_send_streams_.clear();
1660 for (VideoReceiveStream* thumbnail_receive_stream :
1661 thumbnail_receive_streams_)
1662 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
1663 thumbnail_send_streams_.clear();
1664 thumbnail_receive_streams_.clear();
eladalon413ee9a2017-08-22 04:02:52 -07001665 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1666 thumbnail_capturers_) {
1667 video_caputurer.reset();
1668 }
ilnika014cc52017-03-07 04:21:04 -08001669}
1670
ilnik2a8c2f52017-02-15 02:23:28 -08001671void VideoQualityTest::SetupScreenshareOrSVC() {
1672 if (params_.screenshare.enabled) {
1673 // Fill out codec settings.
1674 video_encoder_config_.content_type =
1675 VideoEncoderConfig::ContentType::kScreen;
1676 degradation_preference_ =
1677 VideoSendStream::DegradationPreference::kMaintainResolution;
1678 if (params_.video.codec == "VP8") {
1679 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1680 vp8_settings.denoisingOn = false;
1681 vp8_settings.frameDroppingOn = false;
1682 vp8_settings.numberOfTemporalLayers =
1683 static_cast<unsigned char>(params_.video.num_temporal_layers);
1684 video_encoder_config_.encoder_specific_settings =
1685 new rtc::RefCountedObject<
1686 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1687 } else if (params_.video.codec == "VP9") {
1688 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1689 vp9_settings.denoisingOn = false;
1690 vp9_settings.frameDroppingOn = false;
1691 vp9_settings.numberOfTemporalLayers =
1692 static_cast<unsigned char>(params_.video.num_temporal_layers);
1693 vp9_settings.numberOfSpatialLayers =
1694 static_cast<unsigned char>(params_.ss.num_spatial_layers);
1695 video_encoder_config_.encoder_specific_settings =
1696 new rtc::RefCountedObject<
1697 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1698 }
1699 // Setup frame generator.
1700 const size_t kWidth = 1850;
1701 const size_t kHeight = 1110;
erikvarga579de6f2017-08-29 09:12:57 -07001702 if (params_.screenshare.generate_slides) {
1703 frame_generator_ = test::FrameGenerator::CreateSlideGenerator(
1704 kWidth, kHeight,
perkja8ba1952017-02-27 06:52:10 -08001705 params_.screenshare.slide_change_interval * params_.video.fps);
ilnik2a8c2f52017-02-15 02:23:28 -08001706 } else {
erikvarga579de6f2017-08-29 09:12:57 -07001707 std::vector<std::string> slides = params_.screenshare.slides;
1708 if (slides.size() == 0) {
1709 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1710 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1711 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1712 slides.push_back(
1713 test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1714 }
1715 if (params_.screenshare.scroll_duration == 0) {
1716 // Cycle image every slide_change_interval seconds.
1717 frame_generator_ = test::FrameGenerator::CreateFromYuvFile(
1718 slides, kWidth, kHeight,
1719 params_.screenshare.slide_change_interval * params_.video.fps);
1720 } else {
1721 RTC_CHECK_LE(params_.video.width, kWidth);
1722 RTC_CHECK_LE(params_.video.height, kHeight);
1723 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1724 const int kPauseDurationMs =
1725 (params_.screenshare.slide_change_interval -
1726 params_.screenshare.scroll_duration) *
1727 1000;
1728 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1729 params_.screenshare.slide_change_interval);
ilnik2a8c2f52017-02-15 02:23:28 -08001730
erikvarga579de6f2017-08-29 09:12:57 -07001731 frame_generator_ =
1732 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1733 clock_, slides, kWidth, kHeight, params_.video.width,
1734 params_.video.height,
1735 params_.screenshare.scroll_duration * 1000, kPauseDurationMs);
1736 }
ilnik2a8c2f52017-02-15 02:23:28 -08001737 }
1738 } else if (params_.ss.num_spatial_layers > 1) { // For non-screenshare case.
1739 RTC_CHECK(params_.video.codec == "VP9");
kthelgason29a44e32016-09-27 03:52:02 -07001740 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
kthelgason29a44e32016-09-27 03:52:02 -07001741 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001742 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001743 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001744 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001745 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1746 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001747 }
ivica5d6a06c2015-09-17 05:30:24 -07001748}
1749
ilnika014cc52017-03-07 04:21:04 -08001750void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1751 VideoStream thumbnail = DefaultThumbnailStream();
1752 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1753 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1754 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
1755 thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -08001756 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -08001757 }
1758}
1759
perkja49cbd32016-09-16 07:53:41 -07001760void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001761 if (params_.screenshare.enabled) {
1762 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja8ba1952017-02-27 06:52:10 -08001763 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator_),
minyue626bc952016-10-31 05:47:02 -07001764 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001765 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001766 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001767 } else {
ilnik6b826ef2017-06-16 06:53:48 -07001768 if (params_.video.clip_name == "Generator") {
1769 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
1770 static_cast<int>(params_.video.width),
1771 static_cast<int>(params_.video.height), params_.video.fps, clock_));
1772 } else if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001773 video_capturer_.reset(test::VcmCapturer::Create(
Tarun Chawla8e857d12017-05-31 19:20:57 +05301774 params_.video.width, params_.video.height, params_.video.fps,
1775 params_.video.capture_device_index));
sprang1bed2e42017-01-23 08:46:51 -08001776 if (!video_capturer_) {
1777 // Failed to get actual camera, use chroma generator as backup.
1778 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
perkja8ba1952017-02-27 06:52:10 -08001779 static_cast<int>(params_.video.width),
1780 static_cast<int>(params_.video.height), params_.video.fps, clock_));
sprang1bed2e42017-01-23 08:46:51 -08001781 }
ivica5d6a06c2015-09-17 05:30:24 -07001782 } else {
minyuea27172d2016-11-01 05:59:29 -07001783 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001784 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001785 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001786 clock_));
minyuea27172d2016-11-01 05:59:29 -07001787 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1788 << params_.video.clip_name
1789 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001790 }
1791 }
sprang1bed2e42017-01-23 08:46:51 -08001792 RTC_DCHECK(video_capturer_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001793}
1794
sprang7a975f72015-10-12 06:33:21 -07001795void VideoQualityTest::RunWithAnalyzer(const Params& params) {
eladalon413ee9a2017-08-22 04:02:52 -07001796 std::unique_ptr<test::LayerFilteringTransport> send_transport;
1797 std::unique_ptr<test::DirectTransport> recv_transport;
1798 FILE* graph_data_output_file = nullptr;
1799 std::unique_ptr<VideoAnalyzer> analyzer;
1800
sprangce4aef12015-11-02 07:23:20 -08001801 params_ = params;
1802
minyue626bc952016-10-31 05:47:02 -07001803 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001804 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1805 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001806 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001807
sprangce4aef12015-11-02 07:23:20 -08001808 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001809 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001810 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001811 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001812 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1813 << "!";
ivica87f83a92015-10-08 05:13:32 -07001814 }
sprang7a975f72015-10-12 06:33:21 -07001815
ilnik98436952017-07-13 00:47:03 -07001816 if (!params.logging.rtc_event_log_name.empty()) {
Elad Alon4a87e1c2017-10-03 16:11:34 +02001817 event_log_ = RtcEventLog::Create(clock_, RtcEventLog::EncodingType::Legacy);
ilnik98436952017-07-13 00:47:03 -07001818 bool event_log_started =
Elad Alon83ccca12017-10-04 13:18:26 +02001819 event_log_->StartLogging(rtc::MakeUnique<RtcEventLogOutputFile>(
1820 params.logging.rtc_event_log_name, RtcEventLog::kUnlimitedOutput));
ilnik98436952017-07-13 00:47:03 -07001821 RTC_DCHECK(event_log_started);
1822 }
1823
philipel4fb651d2017-04-10 03:54:05 -07001824 Call::Config call_config(event_log_.get());
minyue626bc952016-10-31 05:47:02 -07001825 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001826
eladalon413ee9a2017-08-22 04:02:52 -07001827 task_queue_.SendTask([this, &call_config, &send_transport,
1828 &recv_transport]() {
1829 CreateCalls(call_config, call_config);
minyue20c84cc2017-04-10 16:57:57 -07001830
eladalon413ee9a2017-08-22 04:02:52 -07001831 send_transport = rtc::MakeUnique<test::LayerFilteringTransport>(
1832 &task_queue_, params_.pipe, sender_call_.get(), kPayloadTypeVP8,
1833 kPayloadTypeVP9, params_.video.selected_tl, params_.ss.selected_sl,
1834 payload_type_map_);
1835
1836 recv_transport = rtc::MakeUnique<test::DirectTransport>(
1837 &task_queue_, params_.pipe, receiver_call_.get(), payload_type_map_);
1838 });
stefanf116bd02015-10-27 08:29:42 -07001839
sprangce4aef12015-11-02 07:23:20 -08001840 std::string graph_title = params_.analyzer.graph_title;
1841 if (graph_title.empty())
1842 graph_title = VideoQualityTest::GenerateGraphTitle();
sprangc1b57a12017-02-28 08:50:47 -08001843 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
eladalon413ee9a2017-08-22 04:02:52 -07001844 analyzer = rtc::MakeUnique<VideoAnalyzer>(
1845 send_transport.get(), params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001846 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001847 is_quick_test_enabled
1848 ? kFramesSentInQuickTest
1849 : params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001850 graph_data_output_file, graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -08001851 kVideoSendSsrcs[params_.ss.selected_stream],
ilnik46a00212017-02-10 09:16:05 -08001852 kSendRtxSsrcs[params_.ss.selected_stream],
ilnikcb8c1462017-03-09 09:23:30 -08001853 static_cast<size_t>(params_.ss.selected_stream), params.ss.selected_sl,
ilnik98436952017-07-13 00:47:03 -07001854 params_.video.selected_tl, is_quick_test_enabled, clock_,
1855 params_.logging.rtp_dump_name);
ivica5d6a06c2015-09-17 05:30:24 -07001856
eladalon413ee9a2017-08-22 04:02:52 -07001857 task_queue_.SendTask([&]() {
1858 analyzer->SetCall(sender_call_.get());
1859 analyzer->SetReceiver(receiver_call_->Receiver());
1860 send_transport->SetReceiver(analyzer.get());
1861 recv_transport->SetReceiver(sender_call_->Receiver());
ivica5d6a06c2015-09-17 05:30:24 -07001862
eladalon413ee9a2017-08-22 04:02:52 -07001863 SetupVideo(analyzer.get(), recv_transport.get());
1864 SetupThumbnails(analyzer.get(), recv_transport.get());
1865 video_receive_configs_[params_.ss.selected_stream].renderer =
1866 analyzer.get();
1867 video_send_config_.pre_encode_callback = analyzer->pre_encode_proxy();
1868 RTC_DCHECK(!video_send_config_.post_encode_callback);
1869 video_send_config_.post_encode_callback = analyzer->encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001870
eladalon413ee9a2017-08-22 04:02:52 -07001871 SetupScreenshareOrSVC();
kthelgason2bc68642017-02-07 07:02:22 -08001872
eladalon413ee9a2017-08-22 04:02:52 -07001873 CreateFlexfecStreams();
1874 CreateVideoStreams();
1875 analyzer->SetSendStream(video_send_stream_);
1876 if (video_receive_streams_.size() == 1)
1877 analyzer->SetReceiveStream(video_receive_streams_[0]);
ivica5d6a06c2015-09-17 05:30:24 -07001878
eladalon413ee9a2017-08-22 04:02:52 -07001879 video_send_stream_->SetSource(analyzer->OutputInterface(),
1880 degradation_preference_);
ilnika014cc52017-03-07 04:21:04 -08001881
eladalon413ee9a2017-08-22 04:02:52 -07001882 SetupThumbnailCapturers(params_.call.num_thumbnails);
1883 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1884 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1885 degradation_preference_);
1886 }
ilnika014cc52017-03-07 04:21:04 -08001887
eladalon413ee9a2017-08-22 04:02:52 -07001888 CreateCapturer();
ivicac1cc8542015-10-08 03:44:06 -07001889
eladalon413ee9a2017-08-22 04:02:52 -07001890 analyzer->SetSource(video_capturer_.get(), params_.ss.infer_streams);
ilnika014cc52017-03-07 04:21:04 -08001891
eladalon413ee9a2017-08-22 04:02:52 -07001892 StartEncodedFrameLogs(video_send_stream_);
1893 StartEncodedFrameLogs(video_receive_streams_[params_.ss.selected_stream]);
1894 video_send_stream_->Start();
1895 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1896 thumbnail_send_stream->Start();
1897 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1898 receive_stream->Start();
1899 for (VideoReceiveStream* thumbnail_receive_stream :
1900 thumbnail_receive_streams_)
1901 thumbnail_receive_stream->Start();
ilnika014cc52017-03-07 04:21:04 -08001902
eladalon413ee9a2017-08-22 04:02:52 -07001903 analyzer->StartMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -07001904
eladalon413ee9a2017-08-22 04:02:52 -07001905 video_capturer_->Start();
1906 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1907 thumbnail_capturers_) {
1908 video_caputurer->Start();
1909 }
1910 });
ivica5d6a06c2015-09-17 05:30:24 -07001911
eladalon413ee9a2017-08-22 04:02:52 -07001912 analyzer->Wait();
ivica5d6a06c2015-09-17 05:30:24 -07001913
Ilya Nikolaevskiy2c72fe82017-10-02 13:01:04 +02001914 event_log_->StopLogging();
1915
eladalon413ee9a2017-08-22 04:02:52 -07001916 task_queue_.SendTask([&]() {
1917 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1918 thumbnail_capturers_)
1919 video_caputurer->Stop();
1920 video_capturer_->Stop();
1921 for (VideoReceiveStream* thumbnail_receive_stream :
1922 thumbnail_receive_streams_)
1923 thumbnail_receive_stream->Stop();
1924 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1925 receive_stream->Stop();
1926 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1927 thumbnail_send_stream->Stop();
1928 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001929
eladalon413ee9a2017-08-22 04:02:52 -07001930 DestroyStreams();
1931 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -07001932
eladalon413ee9a2017-08-22 04:02:52 -07001933 if (graph_data_output_file)
1934 fclose(graph_data_output_file);
1935
1936 video_capturer_.reset();
1937 send_transport.reset();
1938 recv_transport.reset();
1939
1940 DestroyCalls();
1941 });
ivica5d6a06c2015-09-17 05:30:24 -07001942}
1943
minyuea27172d2016-11-01 05:59:29 -07001944void VideoQualityTest::SetupAudio(int send_channel_id,
1945 int receive_channel_id,
minyuea27172d2016-11-01 05:59:29 -07001946 Transport* transport,
1947 AudioReceiveStream** audio_receive_stream) {
1948 audio_send_config_ = AudioSendStream::Config(transport);
1949 audio_send_config_.voe_channel_id = send_channel_id;
1950 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1951
1952 // Add extension to enable audio send side BWE, and allow audio bit rate
1953 // adaptation.
1954 audio_send_config_.rtp.extensions.clear();
1955 if (params_.call.send_side_bwe) {
1956 audio_send_config_.rtp.extensions.push_back(
1957 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1958 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001959 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1960 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001961 }
ossu20a4b3f2017-04-27 02:08:52 -07001962 audio_send_config_.send_codec_spec =
1963 rtc::Optional<AudioSendStream::Config::SendCodecSpec>(
1964 {kAudioSendPayloadType,
1965 {"OPUS", 48000, 2,
1966 {{"usedtx", (params_.audio.dtx ? "1" : "0")},
1967 {"stereo", "1"}}}});
Rasmus Brandt31027342017-09-29 13:48:12 +00001968 audio_send_config_.encoder_factory = encoder_factory_;
sprang1168fd42017-06-21 09:00:17 -07001969 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
minyuea27172d2016-11-01 05:59:29 -07001970
1971 AudioReceiveStream::Config audio_config;
1972 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1973 audio_config.rtcp_send_transport = transport;
1974 audio_config.voe_channel_id = receive_channel_id;
1975 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1976 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1977 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
Rasmus Brandt31027342017-09-29 13:48:12 +00001978 audio_config.decoder_factory = decoder_factory_;
minyue20c84cc2017-04-10 16:57:57 -07001979 audio_config.decoder_map = {{kAudioSendPayloadType, {"OPUS", 48000, 2}}};
minyuea27172d2016-11-01 05:59:29 -07001980 if (params_.video.enabled && params_.audio.sync_video)
1981 audio_config.sync_group = kSyncGroup;
1982
sprang1168fd42017-06-21 09:00:17 -07001983 *audio_receive_stream =
1984 receiver_call_->CreateAudioReceiveStream(audio_config);
minyuea27172d2016-11-01 05:59:29 -07001985}
1986
minyue73208662016-08-18 06:28:55 -07001987void VideoQualityTest::RunWithRenderers(const Params& params) {
eladalon413ee9a2017-08-22 04:02:52 -07001988 std::unique_ptr<test::LayerFilteringTransport> send_transport;
1989 std::unique_ptr<test::DirectTransport> recv_transport;
minyue73208662016-08-18 06:28:55 -07001990 ::VoiceEngineState voe;
minyuea27172d2016-11-01 05:59:29 -07001991 std::unique_ptr<test::VideoRenderer> local_preview;
eladalon413ee9a2017-08-22 04:02:52 -07001992 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers;
minyue73208662016-08-18 06:28:55 -07001993 AudioReceiveStream* audio_receive_stream = nullptr;
minyue73208662016-08-18 06:28:55 -07001994
eladalon413ee9a2017-08-22 04:02:52 -07001995 task_queue_.SendTask([&]() {
1996 params_ = params;
1997 CheckParams();
palmkviste75f2042016-09-28 06:19:48 -07001998
eladalon413ee9a2017-08-22 04:02:52 -07001999 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
2000 // match the full stack tests.
2001 Call::Config call_config(event_log_.get());
2002 call_config.bitrate_config = params_.call.call_bitrate_config;
Niels Möller2bf9e732017-08-14 11:26:16 +02002003
eladalon413ee9a2017-08-22 04:02:52 -07002004 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing(
2005 webrtc::AudioProcessing::Create());
ivica5d6a06c2015-09-17 05:30:24 -07002006
eladalon413ee9a2017-08-22 04:02:52 -07002007 if (params_.audio.enabled) {
Rasmus Brandt31027342017-09-29 13:48:12 +00002008 CreateVoiceEngine(&voe, audio_processing.get(), decoder_factory_);
eladalon413ee9a2017-08-22 04:02:52 -07002009 AudioState::Config audio_state_config;
2010 audio_state_config.voice_engine = voe.voice_engine;
2011 audio_state_config.audio_mixer = AudioMixerImpl::Create();
2012 audio_state_config.audio_processing = audio_processing;
2013 call_config.audio_state = AudioState::Create(audio_state_config);
2014 }
minyue73208662016-08-18 06:28:55 -07002015
eladalon413ee9a2017-08-22 04:02:52 -07002016 CreateCalls(call_config, call_config);
2017
2018 // TODO(minyue): consider if this is a good transport even for audio only
2019 // calls.
2020 send_transport = rtc::MakeUnique<test::LayerFilteringTransport>(
2021 &task_queue_, params.pipe, sender_call_.get(), kPayloadTypeVP8,
2022 kPayloadTypeVP9, params.video.selected_tl, params_.ss.selected_sl,
2023 payload_type_map_);
2024
2025 recv_transport = rtc::MakeUnique<test::DirectTransport>(
2026 &task_queue_, params_.pipe, receiver_call_.get(), payload_type_map_);
2027
2028 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
2029 // least share as much code as possible. That way this test would also match
2030 // the full stack tests better.
2031 send_transport->SetReceiver(receiver_call_->Receiver());
2032 recv_transport->SetReceiver(sender_call_->Receiver());
2033
2034 if (params_.video.enabled) {
2035 // Create video renderers.
2036 local_preview.reset(test::VideoRenderer::Create(
2037 "Local Preview", params_.video.width, params_.video.height));
2038
2039 const size_t selected_stream_id = params_.ss.selected_stream;
2040 const size_t num_streams = params_.ss.streams.size();
2041
2042 if (selected_stream_id == num_streams) {
2043 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
2044 std::ostringstream oss;
2045 oss << "Loopback Video - Stream #" << static_cast<int>(stream_id);
2046 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2047 oss.str().c_str(), params_.ss.streams[stream_id].width,
2048 params_.ss.streams[stream_id].height));
2049 }
2050 } else {
2051 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2052 "Loopback Video", params_.ss.streams[selected_stream_id].width,
2053 params_.ss.streams[selected_stream_id].height));
2054 }
2055
2056 SetupVideo(send_transport.get(), recv_transport.get());
2057
2058 video_send_config_.pre_encode_callback = local_preview.get();
2059 if (selected_stream_id == num_streams) {
2060 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
2061 video_receive_configs_[stream_id].renderer =
2062 loopback_renderers[stream_id].get();
2063 if (params_.audio.enabled && params_.audio.sync_video)
2064 video_receive_configs_[stream_id].sync_group = kSyncGroup;
2065 }
2066 } else {
2067 video_receive_configs_[selected_stream_id].renderer =
2068 loopback_renderers.back().get();
2069 if (params_.audio.enabled && params_.audio.sync_video)
2070 video_receive_configs_[selected_stream_id].sync_group = kSyncGroup;
2071 }
2072
2073 if (params_.screenshare.enabled)
2074 SetupScreenshareOrSVC();
2075
2076 CreateFlexfecStreams();
2077 CreateVideoStreams();
2078
2079 CreateCapturer();
2080 video_send_stream_->SetSource(video_capturer_.get(),
2081 degradation_preference_);
2082 }
2083
2084 if (params_.audio.enabled) {
2085 SetupAudio(voe.send_channel_id, voe.receive_channel_id,
2086 send_transport.get(), &audio_receive_stream);
2087 }
2088
2089 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2090 StartEncodedFrameLogs(receive_stream);
2091 StartEncodedFrameLogs(video_send_stream_);
2092
2093 // Start sending and receiving video.
2094 if (params_.video.enabled) {
2095 for (VideoReceiveStream* video_receive_stream : video_receive_streams_)
2096 video_receive_stream->Start();
2097
2098 video_send_stream_->Start();
2099 video_capturer_->Start();
2100 }
2101
2102 if (params_.audio.enabled) {
2103 // Start receiving audio.
2104 audio_receive_stream->Start();
2105 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
2106
2107 // Start sending audio.
2108 audio_send_stream_->Start();
2109 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
2110 }
2111 });
minyue73208662016-08-18 06:28:55 -07002112
ivica5d6a06c2015-09-17 05:30:24 -07002113 test::PressEnterToContinue();
2114
eladalon413ee9a2017-08-22 04:02:52 -07002115 task_queue_.SendTask([&]() {
2116 if (params_.audio.enabled) {
2117 // Stop sending audio.
2118 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
2119 audio_send_stream_->Stop();
minyue73208662016-08-18 06:28:55 -07002120
eladalon413ee9a2017-08-22 04:02:52 -07002121 // Stop receiving audio.
2122 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
2123 audio_receive_stream->Stop();
2124 sender_call_->DestroyAudioSendStream(audio_send_stream_);
2125 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
2126 }
minyue73208662016-08-18 06:28:55 -07002127
eladalon413ee9a2017-08-22 04:02:52 -07002128 // Stop receiving and sending video.
2129 if (params_.video.enabled) {
2130 video_capturer_->Stop();
2131 video_send_stream_->Stop();
2132 for (FlexfecReceiveStream* flexfec_receive_stream :
2133 flexfec_receive_streams_) {
2134 for (VideoReceiveStream* video_receive_stream :
2135 video_receive_streams_) {
2136 video_receive_stream->RemoveSecondarySink(flexfec_receive_stream);
2137 }
2138 receiver_call_->DestroyFlexfecReceiveStream(flexfec_receive_stream);
eladalonc0d481a2017-08-02 07:39:07 -07002139 }
eladalon413ee9a2017-08-22 04:02:52 -07002140 for (VideoReceiveStream* receive_stream : video_receive_streams_) {
2141 receive_stream->Stop();
2142 receiver_call_->DestroyVideoReceiveStream(receive_stream);
2143 }
2144 sender_call_->DestroyVideoSendStream(video_send_stream_);
brandtr1293aca2016-11-16 22:47:29 -08002145 }
minyue73208662016-08-18 06:28:55 -07002146
eladalon413ee9a2017-08-22 04:02:52 -07002147 video_capturer_.reset();
2148 send_transport.reset();
2149 recv_transport.reset();
sprang1168fd42017-06-21 09:00:17 -07002150
eladalon413ee9a2017-08-22 04:02:52 -07002151 if (params_.audio.enabled)
2152 DestroyVoiceEngine(&voe);
2153
2154 local_preview.reset();
2155 loopback_renderers.clear();
2156
2157 DestroyCalls();
2158 });
ivica5d6a06c2015-09-17 05:30:24 -07002159}
2160
palmkviste75f2042016-09-28 06:19:48 -07002161void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002162 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002163 std::ostringstream str;
2164 str << send_logs_++;
2165 std::string prefix =
ilnik98436952017-07-13 00:47:03 -07002166 params_.logging.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07002167 stream->EnableEncodedFrameRecording(
2168 std::vector<rtc::PlatformFile>(
2169 {rtc::CreatePlatformFile(prefix + "1.ivf"),
2170 rtc::CreatePlatformFile(prefix + "2.ivf"),
2171 rtc::CreatePlatformFile(prefix + "3.ivf")}),
ilnik98436952017-07-13 00:47:03 -07002172 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002173 }
2174}
ilnikcb8c1462017-03-09 09:23:30 -08002175
palmkviste75f2042016-09-28 06:19:48 -07002176void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002177 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002178 std::ostringstream str;
2179 str << receive_logs_++;
2180 std::string path =
ilnik98436952017-07-13 00:47:03 -07002181 params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07002182 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
ilnik98436952017-07-13 00:47:03 -07002183 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002184 }
2185}
ivica5d6a06c2015-09-17 05:30:24 -07002186} // namespace webrtc