blob: d300d1190a77fe61acd8c6e6d3b936ccffea4dc2 [file] [log] [blame]
ivica5d6a06c2015-09-17 12:30:241/*
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 04:47:3110#include "video/video_quality_test.h"
perkj9fdbda62016-09-15 16:19:2011
perkja49cbd32016-09-16 14:53:4112#include <stdio.h>
ivica5d6a06c2015-09-17 12:30:2413#include <algorithm>
14#include <deque>
15#include <map>
ilnikcb8c1462017-03-09 17:23:3016#include <set>
sprangce4aef12015-11-02 15:23:2017#include <sstream>
mflodmand1590b22015-12-09 15:07:5918#include <string>
ivica5d6a06c2015-09-17 12:30:2419#include <vector>
20
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "api/optional.h"
22#include "call/call.h"
23#include "common_video/libyuv/include/webrtc_libyuv.h"
Elad Alon83ccca12017-10-04 11:18:2624#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3125#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 12:46:2454#include "test/testsupport/test_artifacts.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3155#include "test/vcm_capturer.h"
56#include "test/video_renderer.h"
57#include "voice_engine/include/voe_base.h"
minyue73208662016-08-18 13:28:5558
Mirko Bonadei92ea95e2017-09-15 04:47:3159#include "test/rtp_file_writer.h"
ilnik98436952017-07-13 07:47:0360
ilnikee42d192017-08-22 14:16:2061DEFINE_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 12:46:2464 "test_artifacts_dir");
ilnikee42d192017-08-22 14:16:2065
minyue73208662016-08-18 13:28:5566namespace {
67
68constexpr int kSendStatsPollingIntervalMs = 1000;
minyue20c84cc2017-04-10 23:57:5769
minyue73208662016-08-18 13:28:5570constexpr size_t kMaxComparisons = 10;
71constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 17:29:2272constexpr int kOpusMinBitrateBps = 6000;
73constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 08:53:1274constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 12:21:0475constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
76constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 13:28:5577
sprang1168fd42017-06-21 16:00:1778constexpr int kDefaultMaxQp = cricket::WebRtcVideoChannel::kDefaultQpMax;
79
minyue73208662016-08-18 13:28:5580struct VoiceEngineState {
81 VoiceEngineState()
82 : voice_engine(nullptr),
83 base(nullptr),
minyue73208662016-08-18 13:28:5584 send_channel_id(-1),
85 receive_channel_id(-1) {}
86
87 webrtc::VoiceEngine* voice_engine;
88 webrtc::VoEBase* base;
minyue73208662016-08-18 13:28:5589 int send_channel_id;
90 int receive_channel_id;
91};
92
peaha9cc40b2017-06-29 15:32:0993void CreateVoiceEngine(
94 VoiceEngineState* voe,
95 webrtc::AudioProcessing* apm,
96 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory) {
minyue73208662016-08-18 13:28:5597 voe->voice_engine = webrtc::VoiceEngine::Create();
98 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
peaha9cc40b2017-06-29 15:32:0999 EXPECT_EQ(0, voe->base->Init(nullptr, apm, decoder_factory));
solenberg88499ec2016-09-07 14:34:41100 webrtc::VoEBase::ChannelConfig config;
101 config.enable_voice_pacing = true;
102 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 13:28:55103 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 13:28:55115
116 webrtc::VoiceEngine::Delete(voe->voice_engine);
117 voe->voice_engine = nullptr;
118}
119
perkjfa10b552016-10-03 06:45:26120class 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 15:24:33131 // 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-03 06:45:26136 }
137
138 std::vector<webrtc::VideoStream> streams_;
139};
140
brandtr504b95e2016-12-21 10:54:35141bool IsFlexfec(int payload_type) {
142 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
143}
144
minyue73208662016-08-18 13:28:55145} // namespace
ivica5d6a06c2015-09-17 12:30:24146
147namespace webrtc {
148
ivica5d6a06c2015-09-17 12:30:24149class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 16:59:31150 public Transport,
ilnik1e7732c2017-02-23 13:07:56151 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 12:30:24152 public:
sprangce4aef12015-11-02 15:23:20153 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 12:30:24154 const std::string& test_label,
155 double avg_psnr_threshold,
156 double avg_ssim_threshold,
157 int duration_frames,
sprangce4aef12015-11-02 15:23:20158 FILE* graph_data_output_file,
159 const std::string& graph_title,
ilnik3dd5ad92017-02-09 12:58:53160 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 17:16:05161 uint32_t rtx_ssrc_to_analyze,
ilnikcb8c1462017-03-09 17:23:30162 size_t selected_stream,
ilnik1e7732c2017-02-23 13:07:56163 int selected_sl,
164 int selected_tl,
ilnik6b826ef2017-06-16 13:53:48165 bool is_quick_test_enabled,
ilnik98436952017-07-13 07:47:03166 Clock* clock,
167 std::string rtp_dump_name)
perkja49cbd32016-09-16 14:53:41168 : transport_(transport),
ivica5d6a06c2015-09-17 12:30:24169 receiver_(nullptr),
stefan889d9652017-07-05 10:03:02170 call_(nullptr),
ivica5d6a06c2015-09-17 12:30:24171 send_stream_(nullptr),
philipelfd870db2017-01-23 11:22:15172 receive_stream_(nullptr),
ilnik6b826ef2017-06-16 13:53:48173 captured_frame_forwarder_(this, clock),
ivica5d6a06c2015-09-17 12:30:24174 test_label_(test_label),
175 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 15:23:20176 graph_title_(graph_title),
177 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 17:16:05178 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 17:23:30179 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 13:07:56180 selected_sl_(selected_sl),
181 selected_tl_(selected_tl),
pbos14fe7082016-04-20 13:35:56182 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 10:13:28183 encode_timing_proxy_(this),
stefan889d9652017-07-05 10:03:02184 last_fec_bytes_(0),
ivica5d6a06c2015-09-17 12:30:24185 frames_to_process_(duration_frames),
186 frames_recorded_(0),
187 frames_processed_(0),
188 dropped_frames_(0),
pbos14fe7082016-04-20 13:35:56189 dropped_frames_before_first_encode_(0),
190 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 12:30:24191 last_render_time_(0),
192 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 13:07:56193 total_media_bytes_(0),
194 first_sending_time_(0),
195 last_sending_time_(0),
ilnikdf92c5c2017-02-23 10:08:44196 cpu_time_(0),
197 wallclock_time_(0),
ivica5d6a06c2015-09-17 12:30:24198 avg_psnr_threshold_(avg_psnr_threshold),
199 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 08:53:12200 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 16:45:47201 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 12:02:50202 comparison_available_event_(false, false),
ilnik98436952017-07-13 07:47:03203 done_(true, false),
204 clock_(clock),
205 start_ms_(clock->TimeInMilliseconds()) {
ivica5d6a06c2015-09-17 12:30:24206 // 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 23:21:39214 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 12:30:24215 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 16:45:47226 rtc::PlatformThread* thread =
227 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
228 thread->Start();
229 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 12:30:24230 }
ilnik98436952017-07-13 07:47:03231
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 12:30:24237 }
238
239 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 16:45:47240 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
241 thread->Stop();
ivica5d6a06c2015-09-17 12:30:24242 delete thread;
243 }
244 }
245
246 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
247
ilnik6b826ef2017-06-16 13:53:48248 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 10:03:02255 void SetCall(Call* call) {
256 rtc::CritScope lock(&crit_);
257 RTC_DCHECK(!call_);
258 call_ = call;
259 }
260
perkja49cbd32016-09-16 14:53:41261 void SetSendStream(VideoSendStream* stream) {
262 rtc::CritScope lock(&crit_);
263 RTC_DCHECK(!send_stream_);
264 send_stream_ = stream;
265 }
266
philipelfd870db2017-01-23 11:22:15267 void SetReceiveStream(VideoReceiveStream* stream) {
268 rtc::CritScope lock(&crit_);
269 RTC_DCHECK(!receive_stream_);
270 receive_stream_ = stream;
271 }
272
perkja49cbd32016-09-16 14:53:41273 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 12:30:24280 DeliveryStatus DeliverPacket(MediaType media_type,
281 const uint8_t* packet,
282 size_t length,
283 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 18:41:03284 // 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 07:47:03289
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 15:23:20299 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 12:30:24300 RTPHeader header;
danilchapf6975f42015-12-28 18:18:46301 parser.Parse(&header);
ilnik46a00212017-02-10 17:16:05302 if (!IsFlexfec(header.payloadType) &&
303 (header.ssrc == ssrc_to_analyze_ ||
304 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 10:54:35305 // 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 17:16:05308 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 12:30:24309 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 09:30:24310 int64_t timestamp =
311 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
312 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 12:30:24313 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
314 }
315
316 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
317 }
318
Peter Boströme4499152016-02-05 10:13:28319 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 09:43:12320 rtc::CritScope crit(&comparison_lock_);
321 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
322 }
323
pbos14fe7082016-04-20 13:35:56324 void PreEncodeOnFrame(const VideoFrame& video_frame) {
325 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 12:58:53326 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 13:35:56327 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 12:58:53332 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 17:23:30340 encoded_frame.stream_id_ == selected_stream_) {
ilnik3dd5ad92017-02-09 12:58:53341 first_sent_timestamp_ = rtc::Optional<uint32_t>(encoded_frame.timestamp_);
pbos14fe7082016-04-20 13:35:56342 }
343 }
344
stefan1d8a5062015-10-02 10:39:33345 bool SendRtp(const uint8_t* packet,
346 size_t length,
347 const PacketOptions& options) override {
sprangce4aef12015-11-02 15:23:20348 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 12:30:24349 RTPHeader header;
danilchapf6975f42015-12-28 18:18:46350 parser.Parse(&header);
ivica5d6a06c2015-09-17 12:30:24351
sprangce4aef12015-11-02 15:23:20352 int64_t current_time =
353 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 10:23:28354
sprangce4aef12015-11-02 15:23:20355 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 12:30:24356 {
357 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 12:58:53358 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 16:32:53359 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 12:58:53360 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 12:08:56361 }
ilnik3dd5ad92017-02-09 12:58:53362
363 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 10:54:35364 // 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 17:16:05367 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 10:54:35368 int64_t timestamp =
369 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
370 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 13:07:56371
372 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 10:54:35373 encoded_frame_sizes_[timestamp] +=
374 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 13:07:56375 total_media_bytes_ +=
376 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 10:54:35377 }
ilnik1e7732c2017-02-23 13:07:56378 if (first_sending_time_ == 0)
379 first_sending_time_ = current_time;
380 last_sending_time_ = current_time;
sprangce4aef12015-11-02 15:23:20381 }
ivica5d6a06c2015-09-17 12:30:24382 }
sprangce4aef12015-11-02 15:23:20383 return result;
ivica5d6a06c2015-09-17 12:30:24384 }
385
386 bool SendRtcp(const uint8_t* packet, size_t length) override {
387 return transport_->SendRtcp(packet, length);
388 }
389
nisseeb83a1a2016-03-21 08:27:56390 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 12:30:24391 int64_t render_time_ms =
392 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 12:30:24393
394 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 10:08:44395
396 StartExcludingCpuThreadTime();
397
Taylor Brandstetter433b95a2016-03-18 18:41:03398 int64_t send_timestamp =
sprang16daaa52016-03-09 09:30:24399 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 12:30:24400
sprang16daaa52016-03-09 09:30:24401 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 16:44:40402 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 13:35:56403 // No previous frame rendered, this one was dropped after sending but
404 // before rendering.
405 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 15:02:22406 } else {
407 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
408 render_time_ms);
pbos14fe7082016-04-20 13:35:56409 }
ivica5d6a06c2015-09-17 12:30:24410 frames_.pop_front();
pbos14fe7082016-04-20 13:35:56411 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 12:30:24412 }
413
414 VideoFrame reference_frame = frames_.front();
415 frames_.pop_front();
sprang16daaa52016-03-09 09:30:24416 int64_t reference_timestamp =
417 wrap_handler_.Unwrap(reference_frame.timestamp());
418 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 15:23:20419 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 10:13:28420 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 15:23:20421 ++send_timestamp;
422 }
sprang16daaa52016-03-09 09:30:24423 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 12:30:24424
425 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
426
nisse97f0b932016-05-26 16:44:40427 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ilnikdf92c5c2017-02-23 10:08:44428
429 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 12:30:24430 }
431
ivica5d6a06c2015-09-17 12:30:24432 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 16:45:47437 stats_polling_thread_.Start();
sprangce4aef12015-11-02 15:23:20438
ivica5d6a06c2015-09-17 12:30:24439 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 12:30:24440 int iteration = 0;
Peter Boström5811a392015-12-10 12:02:50441 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 12:30:24442 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 14:22:32459 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 12:30:24465 last_frames_processed = frames_processed;
466 }
467
468 if (iteration > 0)
469 printf("- Farewell, sweet Concorde!\n");
470
Peter Boström8c38e8b2015-11-26 16:45:47471 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 12:30:24472 }
473
pbos14fe7082016-04-20 13:35:56474 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
475 return &pre_encode_proxy_;
476 }
Peter Boströme4499152016-02-05 10:13:28477 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
478
ilnikdf92c5c2017-02-23 10:08:44479 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 15:23:20506 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 12:30:24507 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 12:30:24508
509 private:
510 struct FrameComparison {
511 FrameComparison()
512 : dropped(false),
nissedf2ceb82016-12-15 14:29:53513 input_time_ms(0),
ivica5d6a06c2015-09-17 12:30:24514 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 14:29:53522 int64_t input_time_ms,
ivica5d6a06c2015-09-17 12:30:24523 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 14:29:53530 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 12:30:24531 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 14:29:53536 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 12:30:24551 bool dropped;
nissedf2ceb82016-12-15 14:29:53552 int64_t input_time_ms;
ivica5d6a06c2015-09-17 12:30:24553 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 09:43:12560 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 12:30:24566 double psnr,
ivica8d15bd62015-10-07 09:43:12567 double ssim)
ivica5d6a06c2015-09-17 12:30:24568 : 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 09:43:12572 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 12:30:24573 encoded_frame_size(encoded_frame_size),
574 psnr(psnr),
ivica8d15bd62015-10-07 09:43:12575 ssim(ssim) {}
ivica5d6a06c2015-09-17 12:30:24576
ivica8d15bd62015-10-07 09:43:12577 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 12:30:24583 double psnr;
584 double ssim;
ivica5d6a06c2015-09-17 12:30:24585 };
586
Peter Boströme4499152016-02-05 10:13:28587 // 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 12:58:53596 void EncodedFrameCallback(const EncodedFrame& frame) override {
597 parent_->PostEncodeFrameCallback(frame);
598 }
Peter Boströme4499152016-02-05 10:13:28599
600 private:
601 VideoAnalyzer* const parent_;
602 };
603
pbos14fe7082016-04-20 13:35:56604 // 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 13:07:56618 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
619 size_t length,
620 const RTPHeader& header) {
ilnik863f03b2017-07-11 09:38:36621 if (header.payloadType != test::CallTest::kPayloadTypeVP9 &&
622 header.payloadType != test::CallTest::kPayloadTypeVP8) {
ilnik1e7732c2017-02-23 13:07:56623 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 09:38:36629 const bool is_vp8 = header.payloadType == test::CallTest::kPayloadTypeVP8;
ilnik1e7732c2017-02-23 13:07:56630 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 12:30:24649 void AddFrameComparison(const VideoFrame& reference,
650 const VideoFrame& render,
651 bool dropped,
652 int64_t render_time_ms)
danilchapa37de392017-09-09 11:17:22653 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 10:04:52654 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 12:30:24659
sprangce4aef12015-11-02 15:23:20660 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 10:04:52661 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 15:23:20662 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 10:04:52663 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 15:23:20664 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 12:30:24667
ivica5d6a06c2015-09-17 12:30:24668 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 14:00:57669 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 14:29:53670 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 14:00:57674 } else {
nissedf2ceb82016-12-15 14:29:53675 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 14:00:57679 }
Peter Boström5811a392015-12-10 12:02:50680 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 12:30:24681 }
682
tommi0f8b4032017-02-22 19:22:05683 static void PollStatsThread(void* obj) {
684 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 12:30:24685 }
686
tommi0f8b4032017-02-22 19:22:05687 void PollStats() {
688 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
689 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 12:30:24690
stefan889d9652017-07-05 10:03:02691 Call::Stats call_stats = call_->GetStats();
692 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
693
tommi0f8b4032017-02-22 19:22:05694 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 10:03:02705 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 11:22:15712
tommi0f8b4032017-02-22 19:22:05713 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 14:46:05720
721 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 11:22:15722 }
ivica5d6a06c2015-09-17 12:30:24723 }
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 12:30:24733 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 12:02:50738 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 12:30:24739 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 12:02:50740 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 12:30:24741 return false;
742 }
743 return true; // Try again.
744 }
745
ilnikdf92c5c2017-02-23 10:08:44746 StartExcludingCpuThreadTime();
747
ivica5d6a06c2015-09-17 12:30:24748 PerformFrameComparison(comparison);
749
ilnikdf92c5c2017-02-23 10:08:44750 StopExcludingCpuThreadTime();
751
ivica5d6a06c2015-09-17 12:30:24752 if (FrameProcessed()) {
753 PrintResults();
754 if (graph_data_output_file_)
755 PrintSamplesToFile();
Peter Boström5811a392015-12-10 12:02:50756 done_.Set();
757 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 12:30:24758 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 10:08:44804 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 12:30:24805 rtc::CritScope crit(&comparison_lock_);
806 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 19:21:51807 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 08:02:15808 PrintResult("sender_time", sender_time_, " ms");
809 PrintResult("receiver_time", receiver_time_, " ms");
ivica5d6a06c2015-09-17 12:30:24810 PrintResult("total_delay_incl_network", end_to_end_, " ms");
811 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 12:30:24812 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 11:22:15813 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 11:22:15814 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
stefan889d9652017-07-05 10:03:02815 PrintResult("fec_bitrate", fec_bitrate_bps_, " bps");
816 PrintResult("send_bandwidth", send_bandwidth_bps_, " bps");
philipelfd870db2017-01-23 11:22:15817
ilnik59cac992017-07-25 12:45:03818 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 11:22:15823 if (receive_stream_ != nullptr) {
824 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 11:22:15825 }
ivica5d6a06c2015-09-17 12:30:24826
pbos14fe7082016-04-20 13:35:56827 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
828 dropped_frames_);
ilnikdf92c5c2017-02-23 10:08:44829 printf("RESULT cpu_usage: %s = %lf %%\n", test_label_.c_str(),
830 GetCpuUsagePercent());
ilnikdaa574d2017-03-01 14:46:05831
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 12:45:03838
ilnikee42d192017-08-22 14:16:20839 // 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 12:51:57842 if (FLAG_save_worst_frame && worst_frame_) {
ilnikee42d192017-08-22 14:16:20843 std::string output_dir;
Edward Lemuraf8659a2017-09-27 12:46:24844 test::GetTestArtifactsDir(&output_dir);
ilnikee42d192017-08-22 14:16:20845 std::string output_path =
846 rtc::Pathname(output_dir, test_label_ + ".jpg").pathname();
Mirko Bonadei675513b2017-11-09 10:09:25847 RTC_LOG(LS_INFO) << "Saving worst frame to " << output_path;
ilnikee42d192017-08-22 14:16:20848 test::JpegFrameWriter frame_writer(output_path);
849 RTC_CHECK(frame_writer.WriteFrame(worst_frame_->frame,
850 100 /*best quality*/));
ilnik59cac992017-07-25 12:45:03851 }
ilnikdaa574d2017-03-01 14:46:05852
ilnik9ae0d762017-02-15 08:53:12853 // 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 12:30:24859 }
860
861 void PerformFrameComparison(const FrameComparison& comparison) {
862 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 14:00:57863 double psnr = -1.0;
864 double ssim = -1.0;
ilnik6b826ef2017-06-16 13:53:48865 if (comparison.reference && !comparison.dropped) {
nissedf2ceb82016-12-15 14:29:53866 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
867 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 14:00:57868 }
ivica5d6a06c2015-09-17 12:30:24869
ivica5d6a06c2015-09-17 12:30:24870 rtc::CritScope crit(&comparison_lock_);
ilnik59cac992017-07-25 12:45:03871
872 if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) {
873 worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render});
874 }
875
ivica5d6a06c2015-09-17 12:30:24876 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 14:29:53877 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 12:30:24881 }
stefanb1797672016-08-11 14:00:57882 if (psnr >= 0.0)
883 psnr_.AddSample(psnr);
884 if (ssim >= 0.0)
885 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 12:30:24886
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 14:29:53895 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 10:54:35896 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 14:29:53912 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 12:30:24913 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 15:23:20935 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 12:30:24936 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 09:43:12942 "render_time_ms "
ivica5d6a06c2015-09-17 12:30:24943 "encoded_frame_size "
944 "psnr "
945 "ssim "
ivica8d15bd62015-10-07 09:43:12946 "encode_time_ms\n");
947 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 12:30:24948 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 09:43:12949 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 12:30:24961 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 09:43:12962 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 12:30:24968 }
969 }
970
ilnik1e7732c2017-02-23 13:07:56971 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 14:53:41981 // 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 13:53:48989 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 14:53:41998
999 private:
1000 void OnFrame(const VideoFrame& video_frame) override {
1001 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 12:58:531002 // 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 13:53:481005 if (video_frame.ntp_time_ms() == 0)
1006 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
perkja49cbd32016-09-16 14:53:411007 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 12:58:531008 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 14:53:411009 rtc::CritScope lock(&crit_);
1010 if (send_stream_input_)
ilnikb82ac6a2017-05-02 07:48:411011 send_stream_input_->OnFrame(copy);
perkja49cbd32016-09-16 14:53:411012 }
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 14:21:011017 {
1018 rtc::CritScope lock(&crit_);
1019 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
1020 send_stream_input_ = sink;
1021 }
ilnik6b826ef2017-06-16 13:53:481022 if (video_capturer_) {
1023 video_capturer_->AddOrUpdateSink(this, wants);
1024 }
perkja49cbd32016-09-16 14:53:411025 }
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 11:17:221036 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_
1037 RTC_GUARDED_BY(crit_);
ilnik6b826ef2017-06-16 13:53:481038 test::VideoCapturer* video_capturer_;
1039 Clock* clock_;
perkja49cbd32016-09-16 14:53:411040 };
1041
1042 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
1043 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 12:58:531044 frames_.push_back(video_frame);
perkja49cbd32016-09-16 14:53:411045 }
1046
stefan889d9652017-07-05 10:03:021047 Call* call_;
perkja49cbd32016-09-16 14:53:411048 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 11:22:151049 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 14:53:411050 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 12:30:241051 const std::string test_label_;
1052 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 15:23:201053 const std::string graph_title_;
1054 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 17:16:051055 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 17:23:301056 const size_t selected_stream_;
ilnik1e7732c2017-02-23 13:07:561057 const int selected_sl_;
1058 const int selected_tl_;
pbos14fe7082016-04-20 13:35:561059 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 10:13:281060 OnEncodeTimingProxy encode_timing_proxy_;
danilchapa37de392017-09-09 11:17:221061 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 14:46:051080
ilnik59cac992017-07-25 12:45:031081 struct FrameWithPsnr {
1082 double psnr;
1083 VideoFrame frame;
1084 };
1085
1086 // Rendered frame with worst PSNR is saved for further analysis.
danilchapa37de392017-09-09 11:17:221087 rtc::Optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_);
ilnik59cac992017-07-25 12:45:031088
stefan889d9652017-07-05 10:03:021089 size_t last_fec_bytes_;
ivica5d6a06c2015-09-17 12:30:241090
1091 const int frames_to_process_;
1092 int frames_recorded_;
1093 int frames_processed_;
1094 int dropped_frames_;
pbos14fe7082016-04-20 13:35:561095 int dropped_frames_before_first_encode_;
1096 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 12:30:241097 int64_t last_render_time_;
1098 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 13:07:561099 int64_t total_media_bytes_;
1100 int64_t first_sending_time_;
1101 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 12:30:241102
danilchapa37de392017-09-09 11:17:221103 int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
1104 int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
ilnikdf92c5c2017-02-23 10:08:441105 rtc::CriticalSection cpu_measurement_lock_;
1106
ivica5d6a06c2015-09-17 12:30:241107 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 11:17:221108 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 12:30:241116 const double avg_psnr_threshold_;
1117 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 08:53:121118 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 12:30:241119
1120 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 16:45:471121 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1122 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 12:02:501123 rtc::Event comparison_available_event_;
danilchapa37de392017-09-09 11:17:221124 std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 12:02:501125 rtc::Event done_;
ilnik98436952017-07-13 07:47:031126
1127 std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
1128 Clock* const clock_;
1129 const int64_t start_ms_;
ivica5d6a06c2015-09-17 12:30:241130};
1131
Rasmus Brandt31027342017-09-29 13:48:121132class 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 13:19:481161VideoQualityTest::VideoQualityTest()
minyue20c84cc2017-04-10 23:57:571162 : 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 12:30:241174
minyue626bc952016-10-31 12:47:021175VideoQualityTest::Params::Params()
ilnik98436952017-07-13 07:47:031176 : call({false, Call::Config::BitrateConfig(), 0}),
Rasmus Brandt31027342017-09-29 13:48:121177 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
1178 false, ""}),
minyue4c8b9422017-03-21 11:11:431179 audio({false, false, false}),
erikvarga579de6f2017-08-29 16:12:571180 screenshare({false, false, 10, 0}),
minyue626bc952016-10-31 12:47:021181 analyzer({"", 0.0, 0.0, 0, "", ""}),
1182 pipe(),
ilnika014cc52017-03-07 12:21:041183 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}),
ilnik98436952017-07-13 07:47:031184 logging({false, "", "", ""}) {}
minyue626bc952016-10-31 12:47:021185
1186VideoQualityTest::Params::~Params() = default;
1187
ivica5d6a06c2015-09-17 12:30:241188void VideoQualityTest::TestBody() {}
1189
sprangce4aef12015-11-02 15:23:201190std::string VideoQualityTest::GenerateGraphTitle() const {
1191 std::stringstream ss;
minyue626bc952016-10-31 12:47:021192 ss << params_.video.codec;
1193 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
1194 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 15:23:201195 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 15:14:081206 if (!params_.video.enabled)
1207 return;
sprangce4aef12015-11-02 15:23:201208 // 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 14:10:231220 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 12:47:021221 RTC_CHECK(params_.video.selected_tl == -1 ||
1222 params_.video.selected_tl ==
1223 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 15:23:201224 }
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 12:47:021228 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 16:00:171231 RTC_CHECK_LE(params_.ss.selected_stream, params_.ss.streams.size());
sprangce4aef12015-11-02 15:23:201232 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 15:23:201236 }
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 12:47:021245 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 15:23:201246 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 12:47:021247 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 23:21:391248 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 15:23:201249 }
ilnik98436952017-07-13 07:47:031250 RTC_CHECK_GE(params_.call.num_thumbnails, 0);
1251 if (params_.call.num_thumbnails > 0) {
ilnika014cc52017-03-07 12:21:041252 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 15:23:201257}
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 12:47:021290 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 16:00:171296 stream.max_qp = kDefaultMaxQp;
sprang6ef1b342017-03-13 09:01:321297 // 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 14:14:021303 } 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 12:21:041309 }
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 16:00:171322 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 15:23:201323 return stream;
1324}
1325
1326// Static.
1327void VideoQualityTest::FillScalabilitySettings(
1328 Params* params,
1329 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 16:00:171330 int num_streams,
sprangce4aef12015-11-02 15:23:201331 size_t selected_stream,
1332 int num_spatial_layers,
1333 int selected_sl,
1334 const std::vector<std::string>& sl_descriptors) {
sprang1168fd42017-06-21 16:00:171335 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 15:23:201386 }
sprangce4aef12015-11-02 15:23:201387 }
sprangce4aef12015-11-02 15:23:201388
sprang1168fd42017-06-21 16:00:171389 params->ss.num_spatial_layers = std::max(1, num_spatial_layers);
1390 params->ss.selected_stream = selected_stream;
1391
sprangce4aef12015-11-02 15:23:201392 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 12:59:291408void VideoQualityTest::SetupVideo(Transport* send_transport,
1409 Transport* recv_transport) {
brandtr8313a6f2017-01-13 15:41:191410 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 12:30:241413
1414 int payload_type;
minyue626bc952016-10-31 12:47:021415 if (params_.video.codec == "H264") {
Rasmus Brandt31027342017-09-29 13:48:121416 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 09:36:031417 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 12:47:021418 } else if (params_.video.codec == "VP8") {
ilnikcb8c1462017-03-09 17:23:301419 if (params_.screenshare.enabled && params_.ss.streams.size() > 1) {
Rasmus Brandt31027342017-09-29 13:48:121420 // 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 17:23:301423 video_encoder_.reset(
Rasmus Brandt31027342017-09-29 13:48:121424 new SimulcastEncoderAdapter(new Vp8EncoderFactory()));
ilnikcb8c1462017-03-09 17:23:301425 } else {
Rasmus Brandt31027342017-09-29 13:48:121426 video_encoder_.reset(VP8Encoder::Create());
ilnikcb8c1462017-03-09 17:23:301427 }
ivica5d6a06c2015-09-17 12:30:241428 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 12:47:021429 } else if (params_.video.codec == "VP9") {
Rasmus Brandt31027342017-09-29 13:48:121430 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 12:30:241431 payload_type = kPayloadTypeVP9;
1432 } else {
1433 RTC_NOTREACHED() << "Codec not supported!";
1434 return;
1435 }
minyuea27172d2016-11-01 12:59:291436 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 12:47:021437 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 11:14:001438 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 15:41:191441 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 11:14:001442 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 12:30:241443
stefanff483612015-12-21 11:14:001444 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 12:47:021445 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 11:14:001446 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 18:24:551447 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 14:13:241448 test::kTransportSequenceNumberExtensionId));
1449 } else {
stefanff483612015-12-21 11:14:001450 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 18:24:551451 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 13:06:571452 }
ilnik00d802b2017-04-11 17:34:311453 video_send_config_.rtp.extensions.push_back(RtpExtension(
1454 RtpExtension::kVideoContentTypeUri, test::kVideoContentTypeExtensionId));
ilnik04f4d122017-06-19 14:18:551455 video_send_config_.rtp.extensions.push_back(RtpExtension(
1456 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
Erik Språng6b8d3552015-09-24 13:06:571457
stefanff483612015-12-21 11:14:001458 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 12:47:021459 params_.video.min_transmit_bps;
perkjfa10b552016-10-03 06:45:261460
brandtr1293aca2016-11-17 06:47:291461 video_send_config_.suspend_below_min_bitrate =
1462 params_.video.suspend_below_min_bitrate;
1463
perkjfa10b552016-10-03 06:45:261464 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 13:53:481470 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-03 06:45:261479
stefanff483612015-12-21 11:14:001480 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 12:30:241481
1482 CreateMatchingReceiveConfigs(recv_transport);
1483
sprang1168fd42017-06-21 16:00:171484 const bool decode_all_receive_streams =
1485 params_.ss.selected_stream == params_.ss.streams.size();
1486
brandtr8313a6f2017-01-13 15:41:191487 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 11:14:001488 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
brandtr14742122017-01-27 12:53:071489 video_receive_configs_[i].rtp.rtx_ssrc = kSendRtxSsrcs[i];
nisse26e3abb2017-08-25 11:44:251490 video_receive_configs_[i]
1491 .rtp.rtx_associated_payload_types[kSendRtxPayloadType] = payload_type;
minyue626bc952016-10-31 12:47:021492 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
Stefan Holmer85d5ac72017-02-09 15:25:161493 video_receive_configs_[i].rtp.remb = !params_.call.send_side_bwe;
ilnik00d802b2017-04-11 17:34:311494 // 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 16:10:101496 // Force fake decoders on non-selected simulcast streams.
sprang1168fd42017-06-21 16:00:171497 if (!decode_all_receive_streams && i != params_.ss.selected_stream) {
ilnik9fd9f6c2017-03-02 16:10:101498 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 15:23:201506 }
brandtr1293aca2016-11-17 06:47:291507
1508 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 15:41:191509 // Override send config constructed by CreateSendConfig.
sprang1168fd42017-06-21 16:00:171510 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-17 06:47:291519
brandtr8313a6f2017-01-13 15:41:191520 // 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 12:17:531524 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 14:59:191525 video_send_config_.rtp.flexfec.payload_type;
1526 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-17 06:47:291527 flexfec_receive_config.protected_media_ssrcs =
1528 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 09:33:541529 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 14:37:181530 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-17 06:47:291539 flexfec_receive_configs_.push_back(flexfec_receive_config);
brandtr7cd28b92017-09-22 07:26:251540 if (num_video_streams > 0) {
1541 video_receive_configs_[0].rtp.protected_by_flexfec = true;
1542 }
brandtr1293aca2016-11-17 06:47:291543 }
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 16:00:171550 if (decode_all_receive_streams) {
1551 for (auto it = video_receive_configs_.begin();
1552 it != video_receive_configs_.end(); ++it) {
nisse3b3622f2017-09-26 09:49:211553 it->rtp.red_payload_type =
sprang1168fd42017-06-21 16:00:171554 video_send_config_.rtp.ulpfec.red_payload_type;
nisse3b3622f2017-09-26 09:49:211555 it->rtp.ulpfec_payload_type =
sprang1168fd42017-06-21 16:00:171556 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
nisseca5706d2017-09-11 09:32:161557 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 16:00:171560 }
1561 } else {
nisse3b3622f2017-09-26 09:49:211562 video_receive_configs_[params_.ss.selected_stream].rtp.red_payload_type =
sprang1168fd42017-06-21 16:00:171563 video_send_config_.rtp.ulpfec.red_payload_type;
1564 video_receive_configs_[params_.ss.selected_stream]
nisse3b3622f2017-09-26 09:49:211565 .rtp.ulpfec_payload_type =
sprang1168fd42017-06-21 16:00:171566 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1567 video_receive_configs_[params_.ss.selected_stream]
nisseca5706d2017-09-11 09:32:161568 .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 16:00:171571 }
brandtr1293aca2016-11-17 06:47:291572 }
ivica5d6a06c2015-09-17 12:30:241573}
1574
ilnika014cc52017-03-07 12:21:041575void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1576 Transport* recv_transport) {
ilnik98436952017-07-13 07:47:031577 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 12:21:041578 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 13:53:481607 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 12:21:041616 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 11:44:251637 thumbnail_receive_config.rtp
1638 .rtx_associated_payload_types[kSendRtxPayloadType] = kPayloadTypeVP8;
ilnika014cc52017-03-07 12:21:041639 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 07:47:031647 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 12:21:041648 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 11:02:521665 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1666 thumbnail_capturers_) {
1667 video_caputurer.reset();
1668 }
ilnika014cc52017-03-07 12:21:041669}
1670
ilnik2a8c2f52017-02-15 10:23:281671void 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 16:12:571702 if (params_.screenshare.generate_slides) {
1703 frame_generator_ = test::FrameGenerator::CreateSlideGenerator(
1704 kWidth, kHeight,
perkja8ba1952017-02-27 14:52:101705 params_.screenshare.slide_change_interval * params_.video.fps);
ilnik2a8c2f52017-02-15 10:23:281706 } else {
erikvarga579de6f2017-08-29 16:12:571707 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 10:23:281730
erikvarga579de6f2017-08-29 16:12:571731 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 10:23:281737 }
1738 } else if (params_.ss.num_spatial_layers > 1) { // For non-screenshare case.
1739 RTC_CHECK(params_.video.codec == "VP9");
kthelgason29a44e32016-09-27 10:52:021740 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
kthelgason29a44e32016-09-27 10:52:021741 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 12:47:021742 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 10:52:021743 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 15:23:201744 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 10:52:021745 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1746 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 12:30:241747 }
ivica5d6a06c2015-09-17 12:30:241748}
1749
ilnika014cc52017-03-07 12:21:041750void 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 14:15:271756 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 12:21:041757 }
1758}
1759
perkja49cbd32016-09-16 14:53:411760void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 15:23:201761 if (params_.screenshare.enabled) {
1762 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja8ba1952017-02-27 14:52:101763 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator_),
minyue626bc952016-10-31 12:47:021764 params_.video.fps);
ivica2d4e6c52015-09-23 08:57:061765 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 12:59:291766 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 12:30:241767 } else {
ilnik6b826ef2017-06-16 13:53:481768 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 12:59:291773 video_capturer_.reset(test::VcmCapturer::Create(
Tarun Chawla8e857d12017-05-31 13:50:571774 params_.video.width, params_.video.height, params_.video.fps,
1775 params_.video.capture_device_index));
sprang1bed2e42017-01-23 16:46:511776 if (!video_capturer_) {
1777 // Failed to get actual camera, use chroma generator as backup.
1778 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
perkja8ba1952017-02-27 14:52:101779 static_cast<int>(params_.video.width),
1780 static_cast<int>(params_.video.height), params_.video.fps, clock_));
sprang1bed2e42017-01-23 16:46:511781 }
ivica5d6a06c2015-09-17 12:30:241782 } else {
minyuea27172d2016-11-01 12:59:291783 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 14:53:411784 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 12:47:021785 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 08:57:061786 clock_));
minyuea27172d2016-11-01 12:59:291787 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1788 << params_.video.clip_name
1789 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 12:30:241790 }
1791 }
sprang1bed2e42017-01-23 16:46:511792 RTC_DCHECK(video_capturer_.get());
ivica5d6a06c2015-09-17 12:30:241793}
1794
sprang7a975f72015-10-12 13:33:211795void VideoQualityTest::RunWithAnalyzer(const Params& params) {
eladalon413ee9a2017-08-22 11:02:521796 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 15:23:201801 params_ = params;
1802
minyue626bc952016-10-31 12:47:021803 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 12:30:241804 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1805 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 15:23:201806 CheckParams();
ivica5d6a06c2015-09-17 12:30:241807
sprangce4aef12015-11-02 15:23:201808 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 12:30:241809 graph_data_output_file =
sprangce4aef12015-11-02 15:23:201810 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 15:56:101811 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 15:23:201812 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1813 << "!";
ivica87f83a92015-10-08 12:13:321814 }
sprang7a975f72015-10-12 13:33:211815
ilnik98436952017-07-13 07:47:031816 if (!params.logging.rtc_event_log_name.empty()) {
Elad Alon4a87e1c2017-10-03 14:11:341817 event_log_ = RtcEventLog::Create(clock_, RtcEventLog::EncodingType::Legacy);
ilnik98436952017-07-13 07:47:031818 bool event_log_started =
Elad Alon83ccca12017-10-04 11:18:261819 event_log_->StartLogging(rtc::MakeUnique<RtcEventLogOutputFile>(
1820 params.logging.rtc_event_log_name, RtcEventLog::kUnlimitedOutput));
ilnik98436952017-07-13 07:47:031821 RTC_DCHECK(event_log_started);
1822 }
1823
philipel4fb651d2017-04-10 10:54:051824 Call::Config call_config(event_log_.get());
minyue626bc952016-10-31 12:47:021825 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 15:29:421826
eladalon413ee9a2017-08-22 11:02:521827 task_queue_.SendTask([this, &call_config, &send_transport,
1828 &recv_transport]() {
1829 CreateCalls(call_config, call_config);
minyue20c84cc2017-04-10 23:57:571830
eladalon413ee9a2017-08-22 11:02:521831 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 15:29:421839
sprangce4aef12015-11-02 15:23:201840 std::string graph_title = params_.analyzer.graph_title;
1841 if (graph_title.empty())
1842 graph_title = VideoQualityTest::GenerateGraphTitle();
sprangc1b57a12017-02-28 16:50:471843 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
eladalon413ee9a2017-08-22 11:02:521844 analyzer = rtc::MakeUnique<VideoAnalyzer>(
1845 send_transport.get(), params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 10:23:281846 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 08:53:121847 is_quick_test_enabled
1848 ? kFramesSentInQuickTest
1849 : params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 15:23:201850 graph_data_output_file, graph_title,
ilnik3dd5ad92017-02-09 12:58:531851 kVideoSendSsrcs[params_.ss.selected_stream],
ilnik46a00212017-02-10 17:16:051852 kSendRtxSsrcs[params_.ss.selected_stream],
ilnikcb8c1462017-03-09 17:23:301853 static_cast<size_t>(params_.ss.selected_stream), params.ss.selected_sl,
ilnik98436952017-07-13 07:47:031854 params_.video.selected_tl, is_quick_test_enabled, clock_,
1855 params_.logging.rtp_dump_name);
ivica5d6a06c2015-09-17 12:30:241856
eladalon413ee9a2017-08-22 11:02:521857 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 12:30:241862
eladalon413ee9a2017-08-22 11:02:521863 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 12:30:241870
eladalon413ee9a2017-08-22 11:02:521871 SetupScreenshareOrSVC();
kthelgason2bc68642017-02-07 15:02:221872
eladalon413ee9a2017-08-22 11:02:521873 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 12:30:241878
eladalon413ee9a2017-08-22 11:02:521879 video_send_stream_->SetSource(analyzer->OutputInterface(),
1880 degradation_preference_);
ilnika014cc52017-03-07 12:21:041881
eladalon413ee9a2017-08-22 11:02:521882 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 12:21:041887
eladalon413ee9a2017-08-22 11:02:521888 CreateCapturer();
ivicac1cc8542015-10-08 10:44:061889
eladalon413ee9a2017-08-22 11:02:521890 analyzer->SetSource(video_capturer_.get(), params_.ss.infer_streams);
ilnika014cc52017-03-07 12:21:041891
eladalon413ee9a2017-08-22 11:02:521892 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 12:21:041902
eladalon413ee9a2017-08-22 11:02:521903 analyzer->StartMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 12:30:241904
eladalon413ee9a2017-08-22 11:02:521905 video_capturer_->Start();
1906 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1907 thumbnail_capturers_) {
1908 video_caputurer->Start();
1909 }
1910 });
ivica5d6a06c2015-09-17 12:30:241911
eladalon413ee9a2017-08-22 11:02:521912 analyzer->Wait();
ivica5d6a06c2015-09-17 12:30:241913
Ilya Nikolaevskiy2c72fe82017-10-02 11:01:041914 event_log_->StopLogging();
1915
eladalon413ee9a2017-08-22 11:02:521916 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 12:30:241929
eladalon413ee9a2017-08-22 11:02:521930 DestroyStreams();
1931 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 12:30:241932
eladalon413ee9a2017-08-22 11:02:521933 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 12:30:241942}
1943
minyuea27172d2016-11-01 12:59:291944void VideoQualityTest::SetupAudio(int send_channel_id,
1945 int receive_channel_id,
minyuea27172d2016-11-01 12:59:291946 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 17:29:221959 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1960 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 12:59:291961 }
ossu20a4b3f2017-04-27 09:08:521962 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:121968 audio_send_config_.encoder_factory = encoder_factory_;
sprang1168fd42017-06-21 16:00:171969 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
minyuea27172d2016-11-01 12:59:291970
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:121978 audio_config.decoder_factory = decoder_factory_;
minyue20c84cc2017-04-10 23:57:571979 audio_config.decoder_map = {{kAudioSendPayloadType, {"OPUS", 48000, 2}}};
minyuea27172d2016-11-01 12:59:291980 if (params_.video.enabled && params_.audio.sync_video)
1981 audio_config.sync_group = kSyncGroup;
1982
sprang1168fd42017-06-21 16:00:171983 *audio_receive_stream =
1984 receiver_call_->CreateAudioReceiveStream(audio_config);
minyuea27172d2016-11-01 12:59:291985}
1986
minyue73208662016-08-18 13:28:551987void VideoQualityTest::RunWithRenderers(const Params& params) {
eladalon413ee9a2017-08-22 11:02:521988 std::unique_ptr<test::LayerFilteringTransport> send_transport;
1989 std::unique_ptr<test::DirectTransport> recv_transport;
minyue73208662016-08-18 13:28:551990 ::VoiceEngineState voe;
minyuea27172d2016-11-01 12:59:291991 std::unique_ptr<test::VideoRenderer> local_preview;
eladalon413ee9a2017-08-22 11:02:521992 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers;
minyue73208662016-08-18 13:28:551993 AudioReceiveStream* audio_receive_stream = nullptr;
minyue73208662016-08-18 13:28:551994
eladalon413ee9a2017-08-22 11:02:521995 task_queue_.SendTask([&]() {
1996 params_ = params;
1997 CheckParams();
palmkviste75f2042016-09-28 13:19:481998
eladalon413ee9a2017-08-22 11:02:521999 // 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 09:26:162003
eladalon413ee9a2017-08-22 11:02:522004 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing(
2005 webrtc::AudioProcessing::Create());
ivica5d6a06c2015-09-17 12:30:242006
eladalon413ee9a2017-08-22 11:02:522007 if (params_.audio.enabled) {
Rasmus Brandt31027342017-09-29 13:48:122008 CreateVoiceEngine(&voe, audio_processing.get(), decoder_factory_);
eladalon413ee9a2017-08-22 11:02:522009 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 13:28:552015
eladalon413ee9a2017-08-22 11:02:522016 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 13:28:552112
ivica5d6a06c2015-09-17 12:30:242113 test::PressEnterToContinue();
2114
eladalon413ee9a2017-08-22 11:02:522115 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 13:28:552120
eladalon413ee9a2017-08-22 11:02:522121 // 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 13:28:552127
eladalon413ee9a2017-08-22 11:02:522128 // 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 14:39:072139 }
eladalon413ee9a2017-08-22 11:02:522140 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-17 06:47:292145 }
minyue73208662016-08-18 13:28:552146
eladalon413ee9a2017-08-22 11:02:522147 video_capturer_.reset();
2148 send_transport.reset();
2149 recv_transport.reset();
sprang1168fd42017-06-21 16:00:172150
eladalon413ee9a2017-08-22 11:02:522151 if (params_.audio.enabled)
2152 DestroyVoiceEngine(&voe);
2153
2154 local_preview.reset();
2155 loopback_renderers.clear();
2156
2157 DestroyCalls();
2158 });
ivica5d6a06c2015-09-17 12:30:242159}
2160
palmkviste75f2042016-09-28 13:19:482161void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
ilnik98436952017-07-13 07:47:032162 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 13:19:482163 std::ostringstream str;
2164 str << send_logs_++;
2165 std::string prefix =
ilnik98436952017-07-13 07:47:032166 params_.logging.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 13:19:482167 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 07:47:032172 100000000);
palmkviste75f2042016-09-28 13:19:482173 }
2174}
ilnikcb8c1462017-03-09 17:23:302175
palmkviste75f2042016-09-28 13:19:482176void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
ilnik98436952017-07-13 07:47:032177 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 13:19:482178 std::ostringstream str;
2179 str << receive_logs_++;
2180 std::string path =
ilnik98436952017-07-13 07:47:032181 params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 13:19:482182 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
ilnik98436952017-07-13 07:47:032183 100000000);
palmkviste75f2042016-09-28 13:19:482184 }
2185}
ivica5d6a06c2015-09-17 12:30:242186} // namespace webrtc