blob: aa5ff7121d9359e378cace1941a080e480a6a2d1 [file] [log] [blame]
ivica5d6a06c2015-09-17 05:30:24 -07001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
perkja49cbd32016-09-16 07:53:41 -070010#include "webrtc/video/video_quality_test.h"
perkj9fdbda62016-09-15 09:19:20 -070011
perkja49cbd32016-09-16 07:53:41 -070012#include <stdio.h>
ivica5d6a06c2015-09-17 05:30:24 -070013#include <algorithm>
14#include <deque>
15#include <map>
ilnikcb8c1462017-03-09 09:23:30 -080016#include <set>
sprangce4aef12015-11-02 07:23:20 -080017#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080018#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070019#include <vector>
20
ossuf515ab82016-12-07 04:52:58 -080021#include "webrtc/call/call.h"
ivica5d6a06c2015-09-17 05:30:24 -070022#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
skvlad11a9cbf2016-10-07 11:53:05 -070023#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
sprang67561a62017-06-15 06:34:42 -070024#include "webrtc/media/engine/webrtcvideoengine.h"
aleloi10111bc2016-11-17 06:48:48 -080025#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010026#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
ilnik1e7732c2017-02-23 05:07:56 -080027#include "webrtc/modules/rtp_rtcp/source/rtp_format.h"
sprangce4aef12015-11-02 07:23:20 -080028#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
magjed509e4fe2016-11-18 01:34:11 -080029#include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
30#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
ilnikcb8c1462017-03-09 09:23:30 -080031#include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h"
magjed509e4fe2016-11-18 01:34:11 -080032#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020033#include "webrtc/rtc_base/checks.h"
34#include "webrtc/rtc_base/cpu_time.h"
35#include "webrtc/rtc_base/event.h"
36#include "webrtc/rtc_base/format_macros.h"
37#include "webrtc/rtc_base/memory_usage.h"
38#include "webrtc/rtc_base/optional.h"
39#include "webrtc/rtc_base/platform_file.h"
40#include "webrtc/rtc_base/timeutils.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010041#include "webrtc/system_wrappers/include/cpu_info.h"
ilnik9ae0d762017-02-15 00:53:12 -080042#include "webrtc/system_wrappers/include/field_trial.h"
kwibergac9f8762016-09-30 22:29:43 -070043#include "webrtc/test/gtest.h"
ivica5d6a06c2015-09-17 05:30:24 -070044#include "webrtc/test/layer_filtering_transport.h"
45#include "webrtc/test/run_loop.h"
46#include "webrtc/test/statistics.h"
47#include "webrtc/test/testsupport/fileutils.h"
ilnik59cac992017-07-25 05:45:03 -070048#include "webrtc/test/testsupport/frame_writer.h"
perkja49cbd32016-09-16 07:53:41 -070049#include "webrtc/test/vcm_capturer.h"
ivica5d6a06c2015-09-17 05:30:24 -070050#include "webrtc/test/video_renderer.h"
minyue73208662016-08-18 06:28:55 -070051#include "webrtc/voice_engine/include/voe_base.h"
minyue73208662016-08-18 06:28:55 -070052
ilnik98436952017-07-13 00:47:03 -070053#include "webrtc/test/rtp_file_writer.h"
54
minyue73208662016-08-18 06:28:55 -070055namespace {
56
57constexpr int kSendStatsPollingIntervalMs = 1000;
minyue20c84cc2017-04-10 16:57:57 -070058
minyue73208662016-08-18 06:28:55 -070059constexpr size_t kMaxComparisons = 10;
60constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080061constexpr int kOpusMinBitrateBps = 6000;
62constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 00:53:12 -080063constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 04:21:04 -080064constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
65constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 06:28:55 -070066
sprang1168fd42017-06-21 09:00:17 -070067constexpr int kDefaultMaxQp = cricket::WebRtcVideoChannel::kDefaultQpMax;
68
minyue73208662016-08-18 06:28:55 -070069struct VoiceEngineState {
70 VoiceEngineState()
71 : voice_engine(nullptr),
72 base(nullptr),
minyue73208662016-08-18 06:28:55 -070073 send_channel_id(-1),
74 receive_channel_id(-1) {}
75
76 webrtc::VoiceEngine* voice_engine;
77 webrtc::VoEBase* base;
minyue73208662016-08-18 06:28:55 -070078 int send_channel_id;
79 int receive_channel_id;
80};
81
peaha9cc40b2017-06-29 08:32:09 -070082void CreateVoiceEngine(
83 VoiceEngineState* voe,
84 webrtc::AudioProcessing* apm,
85 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory) {
minyue73208662016-08-18 06:28:55 -070086 voe->voice_engine = webrtc::VoiceEngine::Create();
87 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
peaha9cc40b2017-06-29 08:32:09 -070088 EXPECT_EQ(0, voe->base->Init(nullptr, apm, decoder_factory));
solenberg88499ec2016-09-07 07:34:41 -070089 webrtc::VoEBase::ChannelConfig config;
90 config.enable_voice_pacing = true;
91 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 06:28:55 -070092 EXPECT_GE(voe->send_channel_id, 0);
93 voe->receive_channel_id = voe->base->CreateChannel();
94 EXPECT_GE(voe->receive_channel_id, 0);
95}
96
97void DestroyVoiceEngine(VoiceEngineState* voe) {
98 voe->base->DeleteChannel(voe->send_channel_id);
99 voe->send_channel_id = -1;
100 voe->base->DeleteChannel(voe->receive_channel_id);
101 voe->receive_channel_id = -1;
102 voe->base->Release();
103 voe->base = nullptr;
minyue73208662016-08-18 06:28:55 -0700104
105 webrtc::VoiceEngine::Delete(voe->voice_engine);
106 voe->voice_engine = nullptr;
107}
108
perkjfa10b552016-10-02 23:45:26 -0700109class VideoStreamFactory
110 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
111 public:
112 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
113 : streams_(streams) {}
114
115 private:
116 std::vector<webrtc::VideoStream> CreateEncoderStreams(
117 int width,
118 int height,
119 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -0800120 // The highest layer must match the incoming resolution.
121 std::vector<webrtc::VideoStream> streams = streams_;
122 streams[streams_.size() - 1].height = height;
123 streams[streams_.size() - 1].width = width;
124 return streams;
perkjfa10b552016-10-02 23:45:26 -0700125 }
126
127 std::vector<webrtc::VideoStream> streams_;
128};
129
brandtr504b95e2016-12-21 02:54:35 -0800130bool IsFlexfec(int payload_type) {
131 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
132}
133
minyue73208662016-08-18 06:28:55 -0700134} // namespace
ivica5d6a06c2015-09-17 05:30:24 -0700135
136namespace webrtc {
137
ivica5d6a06c2015-09-17 05:30:24 -0700138class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -0700139 public Transport,
ilnik1e7732c2017-02-23 05:07:56 -0800140 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 05:30:24 -0700141 public:
sprangce4aef12015-11-02 07:23:20 -0800142 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700143 const std::string& test_label,
144 double avg_psnr_threshold,
145 double avg_ssim_threshold,
146 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800147 FILE* graph_data_output_file,
148 const std::string& graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -0800149 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 09:16:05 -0800150 uint32_t rtx_ssrc_to_analyze,
ilnikcb8c1462017-03-09 09:23:30 -0800151 size_t selected_stream,
ilnik1e7732c2017-02-23 05:07:56 -0800152 int selected_sl,
153 int selected_tl,
ilnik6b826ef2017-06-16 06:53:48 -0700154 bool is_quick_test_enabled,
ilnik98436952017-07-13 00:47:03 -0700155 Clock* clock,
156 std::string rtp_dump_name)
perkja49cbd32016-09-16 07:53:41 -0700157 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700158 receiver_(nullptr),
stefan889d9652017-07-05 03:03:02 -0700159 call_(nullptr),
ivica5d6a06c2015-09-17 05:30:24 -0700160 send_stream_(nullptr),
philipelfd870db2017-01-23 03:22:15 -0800161 receive_stream_(nullptr),
ilnik6b826ef2017-06-16 06:53:48 -0700162 captured_frame_forwarder_(this, clock),
ivica5d6a06c2015-09-17 05:30:24 -0700163 test_label_(test_label),
164 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800165 graph_title_(graph_title),
166 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 09:16:05 -0800167 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 09:23:30 -0800168 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 05:07:56 -0800169 selected_sl_(selected_sl),
170 selected_tl_(selected_tl),
pbos14fe7082016-04-20 06:35:56 -0700171 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100172 encode_timing_proxy_(this),
stefan889d9652017-07-05 03:03:02 -0700173 last_fec_bytes_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700174 frames_to_process_(duration_frames),
175 frames_recorded_(0),
176 frames_processed_(0),
177 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700178 dropped_frames_before_first_encode_(0),
179 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700180 last_render_time_(0),
181 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 05:07:56 -0800182 total_media_bytes_(0),
183 first_sending_time_(0),
184 last_sending_time_(0),
ilnikdf92c5c2017-02-23 02:08:44 -0800185 cpu_time_(0),
186 wallclock_time_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700187 avg_psnr_threshold_(avg_psnr_threshold),
188 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 00:53:12 -0800189 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100190 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100191 comparison_available_event_(false, false),
ilnik98436952017-07-13 00:47:03 -0700192 done_(true, false),
193 clock_(clock),
194 start_ms_(clock->TimeInMilliseconds()) {
ivica5d6a06c2015-09-17 05:30:24 -0700195 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
196
197 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
198 // so that we don't accidentally starve "real" worker threads (codec etc).
199 // Also, don't allocate more than kMaxComparisonThreads, even if there are
200 // spare cores.
201
202 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800203 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700204 static const uint32_t kMinCoresLeft = 4;
205 static const uint32_t kMaxComparisonThreads = 8;
206
207 if (num_cores <= kMinCoresLeft) {
208 num_cores = 1;
209 } else {
210 num_cores -= kMinCoresLeft;
211 num_cores = std::min(num_cores, kMaxComparisonThreads);
212 }
213
214 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100215 rtc::PlatformThread* thread =
216 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
217 thread->Start();
218 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700219 }
ilnik98436952017-07-13 00:47:03 -0700220
221 if (!rtp_dump_name.empty()) {
222 fprintf(stdout, "Writing rtp dump to %s\n", rtp_dump_name.c_str());
223 rtp_file_writer_.reset(test::RtpFileWriter::Create(
224 test::RtpFileWriter::kRtpDump, rtp_dump_name));
225 }
ivica5d6a06c2015-09-17 05:30:24 -0700226 }
227
228 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100229 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
230 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700231 delete thread;
232 }
233 }
234
235 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
236
ilnik6b826ef2017-06-16 06:53:48 -0700237 void SetSource(test::VideoCapturer* video_capturer, bool respect_sink_wants) {
238 if (respect_sink_wants)
239 captured_frame_forwarder_.SetSource(video_capturer);
240 rtc::VideoSinkWants wants;
241 video_capturer->AddOrUpdateSink(InputInterface(), wants);
242 }
243
stefan889d9652017-07-05 03:03:02 -0700244 void SetCall(Call* call) {
245 rtc::CritScope lock(&crit_);
246 RTC_DCHECK(!call_);
247 call_ = call;
248 }
249
perkja49cbd32016-09-16 07:53:41 -0700250 void SetSendStream(VideoSendStream* stream) {
251 rtc::CritScope lock(&crit_);
252 RTC_DCHECK(!send_stream_);
253 send_stream_ = stream;
254 }
255
philipelfd870db2017-01-23 03:22:15 -0800256 void SetReceiveStream(VideoReceiveStream* stream) {
257 rtc::CritScope lock(&crit_);
258 RTC_DCHECK(!receive_stream_);
259 receive_stream_ = stream;
260 }
261
perkja49cbd32016-09-16 07:53:41 -0700262 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
263 return &captured_frame_forwarder_;
264 }
265 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
266 return &captured_frame_forwarder_;
267 }
268
ivica5d6a06c2015-09-17 05:30:24 -0700269 DeliveryStatus DeliverPacket(MediaType media_type,
270 const uint8_t* packet,
271 size_t length,
272 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700273 // Ignore timestamps of RTCP packets. They're not synchronized with
274 // RTP packet timestamps and so they would confuse wrap_handler_.
275 if (RtpHeaderParser::IsRtcp(packet, length)) {
276 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
277 }
ilnik98436952017-07-13 00:47:03 -0700278
279 if (rtp_file_writer_) {
280 test::RtpPacket p;
281 memcpy(p.data, packet, length);
282 p.length = length;
283 p.original_length = length;
284 p.time_ms = clock_->TimeInMilliseconds() - start_ms_;
285 rtp_file_writer_->WritePacket(&p);
286 }
287
sprangce4aef12015-11-02 07:23:20 -0800288 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700289 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800290 parser.Parse(&header);
ilnik46a00212017-02-10 09:16:05 -0800291 if (!IsFlexfec(header.payloadType) &&
292 (header.ssrc == ssrc_to_analyze_ ||
293 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 02:54:35 -0800294 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
295 // (FlexFEC and media are sent on different SSRCs, which have different
296 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800297 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 05:30:24 -0700298 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800299 int64_t timestamp =
300 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
301 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700302 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
303 }
304
305 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
306 }
307
Peter Boströme4499152016-02-05 11:13:28 +0100308 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700309 rtc::CritScope crit(&comparison_lock_);
310 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
311 }
312
pbos14fe7082016-04-20 06:35:56 -0700313 void PreEncodeOnFrame(const VideoFrame& video_frame) {
314 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800315 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 06:35:56 -0700316 while (frames_.front().timestamp() != video_frame.timestamp()) {
317 ++dropped_frames_before_first_encode_;
318 frames_.pop_front();
319 RTC_CHECK(!frames_.empty());
320 }
ilnik3dd5ad92017-02-09 04:58:53 -0800321 first_encoded_timestamp_ =
322 rtc::Optional<uint32_t>(video_frame.timestamp());
323 }
324 }
325
326 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
327 rtc::CritScope lock(&crit_);
328 if (!first_sent_timestamp_ &&
ilnikcb8c1462017-03-09 09:23:30 -0800329 encoded_frame.stream_id_ == selected_stream_) {
ilnik3dd5ad92017-02-09 04:58:53 -0800330 first_sent_timestamp_ = rtc::Optional<uint32_t>(encoded_frame.timestamp_);
pbos14fe7082016-04-20 06:35:56 -0700331 }
332 }
333
stefan1d8a5062015-10-02 03:39:33 -0700334 bool SendRtp(const uint8_t* packet,
335 size_t length,
336 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800337 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700338 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800339 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700340
sprangce4aef12015-11-02 07:23:20 -0800341 int64_t current_time =
342 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 02:23:28 -0800343
sprangce4aef12015-11-02 07:23:20 -0800344 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700345 {
346 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800347 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 08:32:53 -0800348 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 04:58:53 -0800349 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 04:08:56 -0800350 }
ilnik3dd5ad92017-02-09 04:58:53 -0800351
352 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 02:54:35 -0800353 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
354 // (FlexFEC and media are sent on different SSRCs, which have different
355 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800356 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 02:54:35 -0800357 int64_t timestamp =
358 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
359 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 05:07:56 -0800360
361 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 02:54:35 -0800362 encoded_frame_sizes_[timestamp] +=
363 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 05:07:56 -0800364 total_media_bytes_ +=
365 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 02:54:35 -0800366 }
ilnik1e7732c2017-02-23 05:07:56 -0800367 if (first_sending_time_ == 0)
368 first_sending_time_ = current_time;
369 last_sending_time_ = current_time;
sprangce4aef12015-11-02 07:23:20 -0800370 }
ivica5d6a06c2015-09-17 05:30:24 -0700371 }
sprangce4aef12015-11-02 07:23:20 -0800372 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700373 }
374
375 bool SendRtcp(const uint8_t* packet, size_t length) override {
376 return transport_->SendRtcp(packet, length);
377 }
378
nisseeb83a1a2016-03-21 01:27:56 -0700379 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700380 int64_t render_time_ms =
381 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700382
383 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 02:08:44 -0800384
385 StartExcludingCpuThreadTime();
386
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700387 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800388 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700389
sprang16daaa52016-03-09 01:30:24 -0800390 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700391 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700392 // No previous frame rendered, this one was dropped after sending but
393 // before rendering.
394 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 07:02:22 -0800395 } else {
396 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
397 render_time_ms);
pbos14fe7082016-04-20 06:35:56 -0700398 }
ivica5d6a06c2015-09-17 05:30:24 -0700399 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700400 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700401 }
402
403 VideoFrame reference_frame = frames_.front();
404 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800405 int64_t reference_timestamp =
406 wrap_handler_.Unwrap(reference_frame.timestamp());
407 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800408 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100409 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800410 ++send_timestamp;
411 }
sprang16daaa52016-03-09 01:30:24 -0800412 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700413
414 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
415
nisse97f0b932016-05-26 09:44:40 -0700416 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ilnikdf92c5c2017-02-23 02:08:44 -0800417
418 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 05:30:24 -0700419 }
420
ivica5d6a06c2015-09-17 05:30:24 -0700421 void Wait() {
422 // Frame comparisons can be very expensive. Wait for test to be done, but
423 // at time-out check if frames_processed is going up. If so, give it more
424 // time, otherwise fail. Hopefully this will reduce test flakiness.
425
Peter Boström8c38e8b2015-11-26 17:45:47 +0100426 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800427
ivica5d6a06c2015-09-17 05:30:24 -0700428 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700429 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100430 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700431 int frames_processed;
432 {
433 rtc::CritScope crit(&comparison_lock_);
434 frames_processed = frames_processed_;
435 }
436
437 // Print some output so test infrastructure won't think we've crashed.
438 const char* kKeepAliveMessages[3] = {
439 "Uh, I'm-I'm not quite dead, sir.",
440 "Uh, I-I think uh, I could pull through, sir.",
441 "Actually, I think I'm all right to come with you--"};
442 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
443
444 if (last_frames_processed == -1) {
445 last_frames_processed = frames_processed;
446 continue;
447 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100448 if (frames_processed == last_frames_processed) {
449 EXPECT_GT(frames_processed, last_frames_processed)
450 << "Analyzer stalled while waiting for test to finish.";
451 done_.Set();
452 break;
453 }
ivica5d6a06c2015-09-17 05:30:24 -0700454 last_frames_processed = frames_processed;
455 }
456
457 if (iteration > 0)
458 printf("- Farewell, sweet Concorde!\n");
459
Peter Boström8c38e8b2015-11-26 17:45:47 +0100460 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700461 }
462
pbos14fe7082016-04-20 06:35:56 -0700463 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
464 return &pre_encode_proxy_;
465 }
Peter Boströme4499152016-02-05 11:13:28 +0100466 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
467
ilnikdf92c5c2017-02-23 02:08:44 -0800468 void StartMeasuringCpuProcessTime() {
469 rtc::CritScope lock(&cpu_measurement_lock_);
470 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
471 wallclock_time_ -= rtc::SystemTimeNanos();
472 }
473
474 void StopMeasuringCpuProcessTime() {
475 rtc::CritScope lock(&cpu_measurement_lock_);
476 cpu_time_ += rtc::GetProcessCpuTimeNanos();
477 wallclock_time_ += rtc::SystemTimeNanos();
478 }
479
480 void StartExcludingCpuThreadTime() {
481 rtc::CritScope lock(&cpu_measurement_lock_);
482 cpu_time_ += rtc::GetThreadCpuTimeNanos();
483 }
484
485 void StopExcludingCpuThreadTime() {
486 rtc::CritScope lock(&cpu_measurement_lock_);
487 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
488 }
489
490 double GetCpuUsagePercent() {
491 rtc::CritScope lock(&cpu_measurement_lock_);
492 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
493 }
494
sprangce4aef12015-11-02 07:23:20 -0800495 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700496 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700497
498 private:
499 struct FrameComparison {
500 FrameComparison()
501 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800502 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700503 send_time_ms(0),
504 recv_time_ms(0),
505 render_time_ms(0),
506 encoded_frame_size(0) {}
507
508 FrameComparison(const VideoFrame& reference,
509 const VideoFrame& render,
510 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800511 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700512 int64_t send_time_ms,
513 int64_t recv_time_ms,
514 int64_t render_time_ms,
515 size_t encoded_frame_size)
516 : reference(reference),
517 render(render),
518 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800519 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700520 send_time_ms(send_time_ms),
521 recv_time_ms(recv_time_ms),
522 render_time_ms(render_time_ms),
523 encoded_frame_size(encoded_frame_size) {}
524
nissedf2ceb82016-12-15 06:29:53 -0800525 FrameComparison(bool dropped,
526 int64_t input_time_ms,
527 int64_t send_time_ms,
528 int64_t recv_time_ms,
529 int64_t render_time_ms,
530 size_t encoded_frame_size)
531 : dropped(dropped),
532 input_time_ms(input_time_ms),
533 send_time_ms(send_time_ms),
534 recv_time_ms(recv_time_ms),
535 render_time_ms(render_time_ms),
536 encoded_frame_size(encoded_frame_size) {}
537
538 rtc::Optional<VideoFrame> reference;
539 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700540 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800541 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700542 int64_t send_time_ms;
543 int64_t recv_time_ms;
544 int64_t render_time_ms;
545 size_t encoded_frame_size;
546 };
547
548 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700549 Sample(int dropped,
550 int64_t input_time_ms,
551 int64_t send_time_ms,
552 int64_t recv_time_ms,
553 int64_t render_time_ms,
554 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700555 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700556 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700557 : dropped(dropped),
558 input_time_ms(input_time_ms),
559 send_time_ms(send_time_ms),
560 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700561 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700562 encoded_frame_size(encoded_frame_size),
563 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700564 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700565
ivica8d15bd62015-10-07 02:43:12 -0700566 int dropped;
567 int64_t input_time_ms;
568 int64_t send_time_ms;
569 int64_t recv_time_ms;
570 int64_t render_time_ms;
571 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700572 double psnr;
573 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700574 };
575
Peter Boströme4499152016-02-05 11:13:28 +0100576 // This class receives the send-side OnEncodeTiming and is provided to not
577 // conflict with the receiver-side pre_decode_callback.
578 class OnEncodeTimingProxy : public EncodedFrameObserver {
579 public:
580 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
581
582 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
583 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
584 }
ilnik3dd5ad92017-02-09 04:58:53 -0800585 void EncodedFrameCallback(const EncodedFrame& frame) override {
586 parent_->PostEncodeFrameCallback(frame);
587 }
Peter Boströme4499152016-02-05 11:13:28 +0100588
589 private:
590 VideoAnalyzer* const parent_;
591 };
592
pbos14fe7082016-04-20 06:35:56 -0700593 // This class receives the send-side OnFrame callback and is provided to not
594 // conflict with the receiver-side renderer callback.
595 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
596 public:
597 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
598
599 void OnFrame(const VideoFrame& video_frame) override {
600 parent_->PreEncodeOnFrame(video_frame);
601 }
602
603 private:
604 VideoAnalyzer* const parent_;
605 };
606
ilnik1e7732c2017-02-23 05:07:56 -0800607 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
608 size_t length,
609 const RTPHeader& header) {
ilnik863f03b2017-07-11 02:38:36 -0700610 if (header.payloadType != test::CallTest::kPayloadTypeVP9 &&
611 header.payloadType != test::CallTest::kPayloadTypeVP8) {
ilnik1e7732c2017-02-23 05:07:56 -0800612 return true;
613 } else {
614 // Get VP8 and VP9 specific header to check layers indexes.
615 const uint8_t* payload = packet + header.headerLength;
616 const size_t payload_length = length - header.headerLength;
617 const size_t payload_data_length = payload_length - header.paddingLength;
ilnik863f03b2017-07-11 02:38:36 -0700618 const bool is_vp8 = header.payloadType == test::CallTest::kPayloadTypeVP8;
ilnik1e7732c2017-02-23 05:07:56 -0800619 std::unique_ptr<RtpDepacketizer> depacketizer(
620 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
621 RtpDepacketizer::ParsedPayload parsed_payload;
622 bool result =
623 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
624 RTC_DCHECK(result);
625 const int temporal_idx = static_cast<int>(
626 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
627 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
628 const int spatial_idx = static_cast<int>(
629 is_vp8 ? kNoSpatialIdx
630 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
631 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
632 temporal_idx <= selected_tl_) &&
633 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
634 spatial_idx <= selected_sl_);
635 }
636 }
637
ivica5d6a06c2015-09-17 05:30:24 -0700638 void AddFrameComparison(const VideoFrame& reference,
639 const VideoFrame& render,
640 bool dropped,
641 int64_t render_time_ms)
642 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800643 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
644 int64_t send_time_ms = send_times_[reference_timestamp];
645 send_times_.erase(reference_timestamp);
646 int64_t recv_time_ms = recv_times_[reference_timestamp];
647 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700648
sprangce4aef12015-11-02 07:23:20 -0800649 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800650 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800651 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800652 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800653 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
654 if (it != encoded_frame_sizes_.end())
655 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700656
ivica5d6a06c2015-09-17 05:30:24 -0700657 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700658 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800659 comparisons_.push_back(FrameComparison(reference, render, dropped,
660 reference.ntp_time_ms(),
661 send_time_ms, recv_time_ms,
662 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700663 } else {
nissedf2ceb82016-12-15 06:29:53 -0800664 comparisons_.push_back(FrameComparison(dropped,
665 reference.ntp_time_ms(),
666 send_time_ms, recv_time_ms,
667 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700668 }
Peter Boström5811a392015-12-10 13:02:50 +0100669 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700670 }
671
tommi0f8b4032017-02-22 11:22:05 -0800672 static void PollStatsThread(void* obj) {
673 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 05:30:24 -0700674 }
675
tommi0f8b4032017-02-22 11:22:05 -0800676 void PollStats() {
677 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
678 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700679
stefan889d9652017-07-05 03:03:02 -0700680 Call::Stats call_stats = call_->GetStats();
681 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
682
tommi0f8b4032017-02-22 11:22:05 -0800683 VideoSendStream::Stats send_stats = send_stream_->GetStats();
684 // It's not certain that we yet have estimates for any of these stats.
685 // Check that they are positive before mixing them in.
686 if (send_stats.encode_frame_rate > 0)
687 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
688 if (send_stats.avg_encode_time_ms > 0)
689 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
690 if (send_stats.encode_usage_percent > 0)
691 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
692 if (send_stats.media_bitrate_bps > 0)
693 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
stefan889d9652017-07-05 03:03:02 -0700694 size_t fec_bytes = 0;
695 for (auto kv : send_stats.substreams) {
696 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
697 kv.second.rtp_stats.fec.padding_bytes;
698 }
699 fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8);
700 last_fec_bytes_ = fec_bytes;
philipelfd870db2017-01-23 03:22:15 -0800701
tommi0f8b4032017-02-22 11:22:05 -0800702 if (receive_stream_ != nullptr) {
703 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
704 if (receive_stats.decode_ms > 0)
705 decode_time_ms_.AddSample(receive_stats.decode_ms);
706 if (receive_stats.max_decode_ms > 0)
707 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
708 }
ilnikdaa574d2017-03-01 06:46:05 -0800709
710 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 03:22:15 -0800711 }
ivica5d6a06c2015-09-17 05:30:24 -0700712 }
713
714 static bool FrameComparisonThread(void* obj) {
715 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
716 }
717
718 bool CompareFrames() {
719 if (AllFramesRecorded())
720 return false;
721
ivica5d6a06c2015-09-17 05:30:24 -0700722 FrameComparison comparison;
723
724 if (!PopComparison(&comparison)) {
725 // Wait until new comparison task is available, or test is done.
726 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100727 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700728 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100729 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700730 return false;
731 }
732 return true; // Try again.
733 }
734
ilnikdf92c5c2017-02-23 02:08:44 -0800735 StartExcludingCpuThreadTime();
736
ivica5d6a06c2015-09-17 05:30:24 -0700737 PerformFrameComparison(comparison);
738
ilnikdf92c5c2017-02-23 02:08:44 -0800739 StopExcludingCpuThreadTime();
740
ivica5d6a06c2015-09-17 05:30:24 -0700741 if (FrameProcessed()) {
742 PrintResults();
743 if (graph_data_output_file_)
744 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100745 done_.Set();
746 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700747 return false;
748 }
749
750 return true;
751 }
752
753 bool PopComparison(FrameComparison* comparison) {
754 rtc::CritScope crit(&comparison_lock_);
755 // If AllFramesRecorded() is true, it means we have already popped
756 // frames_to_process_ frames from comparisons_, so there is no more work
757 // for this thread to be done. frames_processed_ might still be lower if
758 // all comparisons are not done, but those frames are currently being
759 // worked on by other threads.
760 if (comparisons_.empty() || AllFramesRecorded())
761 return false;
762
763 *comparison = comparisons_.front();
764 comparisons_.pop_front();
765
766 FrameRecorded();
767 return true;
768 }
769
770 // Increment counter for number of frames received for comparison.
771 void FrameRecorded() {
772 rtc::CritScope crit(&comparison_lock_);
773 ++frames_recorded_;
774 }
775
776 // Returns true if all frames to be compared have been taken from the queue.
777 bool AllFramesRecorded() {
778 rtc::CritScope crit(&comparison_lock_);
779 assert(frames_recorded_ <= frames_to_process_);
780 return frames_recorded_ == frames_to_process_;
781 }
782
783 // Increase count of number of frames processed. Returns true if this was the
784 // last frame to be processed.
785 bool FrameProcessed() {
786 rtc::CritScope crit(&comparison_lock_);
787 ++frames_processed_;
788 assert(frames_processed_ <= frames_to_process_);
789 return frames_processed_ == frames_to_process_;
790 }
791
792 void PrintResults() {
ilnikdf92c5c2017-02-23 02:08:44 -0800793 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -0700794 rtc::CritScope crit(&comparison_lock_);
795 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800796 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 01:02:15 -0700797 PrintResult("sender_time", sender_time_, " ms");
798 PrintResult("receiver_time", receiver_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700799 PrintResult("total_delay_incl_network", end_to_end_, " ms");
800 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700801 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800802 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800803 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
stefan889d9652017-07-05 03:03:02 -0700804 PrintResult("fec_bitrate", fec_bitrate_bps_, " bps");
805 PrintResult("send_bandwidth", send_bandwidth_bps_, " bps");
philipelfd870db2017-01-23 03:22:15 -0800806
ilnik59cac992017-07-25 05:45:03 -0700807 if (worst_frame_) {
808 printf("RESULT min_psnr: %s = %lf dB\n", test_label_.c_str(),
809 worst_frame_->psnr);
810 }
811
philipelfd870db2017-01-23 03:22:15 -0800812 if (receive_stream_ != nullptr) {
813 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800814 }
ivica5d6a06c2015-09-17 05:30:24 -0700815
pbos14fe7082016-04-20 06:35:56 -0700816 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
817 dropped_frames_);
ilnikdf92c5c2017-02-23 02:08:44 -0800818 printf("RESULT cpu_usage: %s = %lf %%\n", test_label_.c_str(),
819 GetCpuUsagePercent());
ilnikdaa574d2017-03-01 06:46:05 -0800820
821#if defined(WEBRTC_WIN)
822 // On Linux and Mac in Resident Set some unused pages may be counted.
823 // Therefore this metric will depend on order in which tests are run and
824 // will be flaky.
825 PrintResult("memory_usage", memory_usage_, " bytes");
826#endif
ilnik59cac992017-07-25 05:45:03 -0700827 // TODO(ilnik): enable frame writing for android, once jpeg frame writer
828 // is implemented.
829
830#if !defined(WEBRTC_ANDROID)
831 if (worst_frame_) {
832 test::Y4mFrameWriterImpl frame_writer(test_label_ + ".y4m",
833 worst_frame_->frame.width(),
834 worst_frame_->frame.height(), 1);
835 bool res = frame_writer.Init();
836 RTC_DCHECK(res);
837 size_t length =
838 CalcBufferSize(VideoType::kI420, worst_frame_->frame.width(),
839 worst_frame_->frame.height());
840 rtc::Buffer extracted_buffer(length);
841 size_t extracted_length =
842 ExtractBuffer(worst_frame_->frame.video_frame_buffer()->ToI420(),
843 length, extracted_buffer.data());
844 RTC_DCHECK_EQ(extracted_length, frame_writer.FrameLength());
845 res = frame_writer.WriteFrame(extracted_buffer.data());
846 RTC_DCHECK(res);
847 frame_writer.Close();
848 }
849#endif
ilnikdaa574d2017-03-01 06:46:05 -0800850
ilnik9ae0d762017-02-15 00:53:12 -0800851 // Disable quality check for quick test, as quality checks may fail
852 // because too few samples were collected.
853 if (!is_quick_test_enabled_) {
854 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
855 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
856 }
ivica5d6a06c2015-09-17 05:30:24 -0700857 }
858
859 void PerformFrameComparison(const FrameComparison& comparison) {
860 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700861 double psnr = -1.0;
862 double ssim = -1.0;
ilnik6b826ef2017-06-16 06:53:48 -0700863 if (comparison.reference && !comparison.dropped) {
nissedf2ceb82016-12-15 06:29:53 -0800864 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
865 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700866 }
ivica5d6a06c2015-09-17 05:30:24 -0700867
ivica5d6a06c2015-09-17 05:30:24 -0700868 rtc::CritScope crit(&comparison_lock_);
ilnik59cac992017-07-25 05:45:03 -0700869
870 if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) {
871 worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render});
872 }
873
ivica5d6a06c2015-09-17 05:30:24 -0700874 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800875 samples_.push_back(Sample(
876 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
877 comparison.recv_time_ms, comparison.render_time_ms,
878 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700879 }
stefanb1797672016-08-11 07:00:57 -0700880 if (psnr >= 0.0)
881 psnr_.AddSample(psnr);
882 if (ssim >= 0.0)
883 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700884
885 if (comparison.dropped) {
886 ++dropped_frames_;
887 return;
888 }
889 if (last_render_time_ != 0)
890 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
891 last_render_time_ = comparison.render_time_ms;
892
nissedf2ceb82016-12-15 06:29:53 -0800893 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800894 if (comparison.recv_time_ms > 0) {
895 // If recv_time_ms == 0, this frame consisted of a packets which were all
896 // lost in the transport. Since we were able to render the frame, however,
897 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
898 // happens internally in Call, and we can therefore here not know which
899 // FEC packets that protected the lost media packets. Consequently, we
900 // were not able to record a meaningful recv_time_ms. We therefore skip
901 // this sample.
902 //
903 // The reasoning above does not hold for ULPFEC and RTX, as for those
904 // strategies the timestamp of the received packets is set to the
905 // timestamp of the protected/retransmitted media packet. I.e., then
906 // recv_time_ms != 0, even though the media packets were lost.
907 receiver_time_.AddSample(comparison.render_time_ms -
908 comparison.recv_time_ms);
909 }
nissedf2ceb82016-12-15 06:29:53 -0800910 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700911 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
912 }
913
914 void PrintResult(const char* result_type,
915 test::Statistics stats,
916 const char* unit) {
917 printf("RESULT %s: %s = {%f, %f}%s\n",
918 result_type,
919 test_label_.c_str(),
920 stats.Mean(),
921 stats.StandardDeviation(),
922 unit);
923 }
924
925 void PrintSamplesToFile(void) {
926 FILE* out = graph_data_output_file_;
927 rtc::CritScope crit(&comparison_lock_);
928 std::sort(samples_.begin(), samples_.end(),
929 [](const Sample& A, const Sample& B) -> bool {
930 return A.input_time_ms < B.input_time_ms;
931 });
932
sprangce4aef12015-11-02 07:23:20 -0800933 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700934 fprintf(out, "%" PRIuS "\n", samples_.size());
935 fprintf(out,
936 "dropped "
937 "input_time_ms "
938 "send_time_ms "
939 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700940 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700941 "encoded_frame_size "
942 "psnr "
943 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700944 "encode_time_ms\n");
945 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700946 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700947 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
948 int encode_time_ms;
949 if (it != samples_encode_time_ms_.end()) {
950 encode_time_ms = it->second;
951 } else {
952 ++missing_encode_time_samples;
953 encode_time_ms = -1;
954 }
955 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
956 " %lf %lf %d\n",
957 sample.dropped, sample.input_time_ms, sample.send_time_ms,
958 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700959 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700960 encode_time_ms);
961 }
962 if (missing_encode_time_samples) {
963 fprintf(stderr,
964 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
965 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700966 }
967 }
968
ilnik1e7732c2017-02-23 05:07:56 -0800969 double GetAverageMediaBitrateBps() {
970 if (last_sending_time_ == first_sending_time_) {
971 return 0;
972 } else {
973 return static_cast<double>(total_media_bytes_) * 8 /
974 (last_sending_time_ - first_sending_time_) *
975 rtc::kNumMillisecsPerSec;
976 }
977 }
978
perkja49cbd32016-09-16 07:53:41 -0700979 // Implements VideoSinkInterface to receive captured frames from a
980 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
981 // as a source to VideoSendStream.
982 // It forwards all input frames to the VideoAnalyzer for later comparison and
983 // forwards the captured frames to the VideoSendStream.
984 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
985 public rtc::VideoSourceInterface<VideoFrame> {
986 public:
ilnik6b826ef2017-06-16 06:53:48 -0700987 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer, Clock* clock)
988 : analyzer_(analyzer),
989 send_stream_input_(nullptr),
990 video_capturer_(nullptr),
991 clock_(clock) {}
992
993 void SetSource(test::VideoCapturer* video_capturer) {
994 video_capturer_ = video_capturer;
995 }
perkja49cbd32016-09-16 07:53:41 -0700996
997 private:
998 void OnFrame(const VideoFrame& video_frame) override {
999 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -08001000 // Frames from the capturer does not have a rtp timestamp.
1001 // Create one so it can be used for comparison.
1002 RTC_DCHECK_EQ(0, video_frame.timestamp());
ilnik6b826ef2017-06-16 06:53:48 -07001003 if (video_frame.ntp_time_ms() == 0)
1004 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
perkja49cbd32016-09-16 07:53:41 -07001005 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -08001006 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -07001007 rtc::CritScope lock(&crit_);
1008 if (send_stream_input_)
ilnikb82ac6a2017-05-02 00:48:41 -07001009 send_stream_input_->OnFrame(copy);
perkja49cbd32016-09-16 07:53:41 -07001010 }
1011
1012 // Called when |send_stream_.SetSource()| is called.
1013 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
1014 const rtc::VideoSinkWants& wants) override {
ilnik267041c2017-06-27 07:21:01 -07001015 {
1016 rtc::CritScope lock(&crit_);
1017 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
1018 send_stream_input_ = sink;
1019 }
ilnik6b826ef2017-06-16 06:53:48 -07001020 if (video_capturer_) {
1021 video_capturer_->AddOrUpdateSink(this, wants);
1022 }
perkja49cbd32016-09-16 07:53:41 -07001023 }
1024
1025 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
1026 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
1027 rtc::CritScope lock(&crit_);
1028 RTC_DCHECK(sink == send_stream_input_);
1029 send_stream_input_ = nullptr;
1030 }
1031
1032 VideoAnalyzer* const analyzer_;
1033 rtc::CriticalSection crit_;
1034 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
ilnik6b826ef2017-06-16 06:53:48 -07001035 test::VideoCapturer* video_capturer_;
1036 Clock* clock_;
perkja49cbd32016-09-16 07:53:41 -07001037 };
1038
1039 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
1040 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -08001041 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -07001042 }
1043
stefan889d9652017-07-05 03:03:02 -07001044 Call* call_;
perkja49cbd32016-09-16 07:53:41 -07001045 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -08001046 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -07001047 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -07001048 const std::string test_label_;
1049 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -08001050 const std::string graph_title_;
1051 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -08001052 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 09:23:30 -08001053 const size_t selected_stream_;
ilnik1e7732c2017-02-23 05:07:56 -08001054 const int selected_sl_;
1055 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -07001056 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +01001057 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -07001058 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -07001059 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -07001060 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
1061 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
1062 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
1063 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
1064 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
1065 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
1066 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
1067 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
philipelfd870db2017-01-23 03:22:15 -08001068 test::Statistics encode_time_ms_ GUARDED_BY(comparison_lock_);
1069 test::Statistics encode_usage_percent_ GUARDED_BY(comparison_lock_);
1070 test::Statistics decode_time_ms_ GUARDED_BY(comparison_lock_);
1071 test::Statistics decode_time_max_ms_ GUARDED_BY(comparison_lock_);
1072 test::Statistics media_bitrate_bps_ GUARDED_BY(comparison_lock_);
stefan889d9652017-07-05 03:03:02 -07001073 test::Statistics fec_bitrate_bps_ GUARDED_BY(comparison_lock_);
1074 test::Statistics send_bandwidth_bps_ GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 06:46:05 -08001075 test::Statistics memory_usage_ GUARDED_BY(comparison_lock_);
1076
ilnik59cac992017-07-25 05:45:03 -07001077 struct FrameWithPsnr {
1078 double psnr;
1079 VideoFrame frame;
1080 };
1081
1082 // Rendered frame with worst PSNR is saved for further analysis.
1083 rtc::Optional<FrameWithPsnr> worst_frame_ GUARDED_BY(comparison_lock_);
1084
stefan889d9652017-07-05 03:03:02 -07001085 size_t last_fec_bytes_;
ivica5d6a06c2015-09-17 05:30:24 -07001086
1087 const int frames_to_process_;
1088 int frames_recorded_;
1089 int frames_processed_;
1090 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -07001091 int dropped_frames_before_first_encode_;
1092 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -07001093 int64_t last_render_time_;
1094 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -08001095 int64_t total_media_bytes_;
1096 int64_t first_sending_time_;
1097 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -07001098
ilnikdf92c5c2017-02-23 02:08:44 -08001099 int64_t cpu_time_ GUARDED_BY(cpu_measurement_lock_);
1100 int64_t wallclock_time_ GUARDED_BY(cpu_measurement_lock_);
1101 rtc::CriticalSection cpu_measurement_lock_;
1102
ivica5d6a06c2015-09-17 05:30:24 -07001103 rtc::CriticalSection crit_;
1104 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -07001105 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -08001106 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
1107 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
1108 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
1109 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
ilnik3dd5ad92017-02-09 04:58:53 -08001110 rtc::Optional<uint32_t> first_encoded_timestamp_ GUARDED_BY(crit_);
1111 rtc::Optional<uint32_t> first_sent_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -07001112 const double avg_psnr_threshold_;
1113 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -08001114 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -07001115
1116 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001117 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1118 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001119 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -07001120 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001121 rtc::Event done_;
ilnik98436952017-07-13 00:47:03 -07001122
1123 std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
1124 Clock* const clock_;
1125 const int64_t start_ms_;
ivica5d6a06c2015-09-17 05:30:24 -07001126};
1127
magjed6cc25612017-07-10 03:26:36 -07001128class Vp8EncoderFactory : public cricket::WebRtcVideoEncoderFactory {
ilnikcb8c1462017-03-09 09:23:30 -08001129 public:
magjed6cc25612017-07-10 03:26:36 -07001130 Vp8EncoderFactory() {
1131 supported_codecs_.push_back(cricket::VideoCodec("VP8"));
1132 }
ilnikcb8c1462017-03-09 09:23:30 -08001133 ~Vp8EncoderFactory() override { RTC_CHECK(live_encoders_.empty()); }
1134
magjed6cc25612017-07-10 03:26:36 -07001135 const std::vector<cricket::VideoCodec>& supported_codecs() const override {
1136 return supported_codecs_;
1137 }
1138
1139 VideoEncoder* CreateVideoEncoder(const cricket::VideoCodec& codec) override {
ilnikcb8c1462017-03-09 09:23:30 -08001140 VideoEncoder* encoder = VP8Encoder::Create();
1141 live_encoders_.insert(encoder);
1142 return encoder;
1143 }
1144
magjed6cc25612017-07-10 03:26:36 -07001145 void DestroyVideoEncoder(VideoEncoder* encoder) override {
ilnikcb8c1462017-03-09 09:23:30 -08001146 auto it = live_encoders_.find(encoder);
1147 RTC_CHECK(it != live_encoders_.end());
1148 live_encoders_.erase(it);
1149 delete encoder;
1150 }
1151
magjed6cc25612017-07-10 03:26:36 -07001152 private:
1153 std::vector<cricket::VideoCodec> supported_codecs_;
ilnikcb8c1462017-03-09 09:23:30 -08001154 std::set<VideoEncoder*> live_encoders_;
1155};
1156
palmkviste75f2042016-09-28 06:19:48 -07001157VideoQualityTest::VideoQualityTest()
minyue20c84cc2017-04-10 16:57:57 -07001158 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {
1159 payload_type_map_ = test::CallTest::payload_type_map_;
1160 RTC_DCHECK(payload_type_map_.find(kPayloadTypeH264) ==
1161 payload_type_map_.end());
1162 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP8) ==
1163 payload_type_map_.end());
1164 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP9) ==
1165 payload_type_map_.end());
1166 payload_type_map_[kPayloadTypeH264] = webrtc::MediaType::VIDEO;
1167 payload_type_map_[kPayloadTypeVP8] = webrtc::MediaType::VIDEO;
1168 payload_type_map_[kPayloadTypeVP9] = webrtc::MediaType::VIDEO;
1169}
ivica5d6a06c2015-09-17 05:30:24 -07001170
minyue626bc952016-10-31 05:47:02 -07001171VideoQualityTest::Params::Params()
ilnik98436952017-07-13 00:47:03 -07001172 : call({false, Call::Config::BitrateConfig(), 0}),
minyue626bc952016-10-31 05:47:02 -07001173 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
ilnik98436952017-07-13 00:47:03 -07001174 false, ""}),
minyue4c8b9422017-03-21 04:11:43 -07001175 audio({false, false, false}),
minyue626bc952016-10-31 05:47:02 -07001176 screenshare({false, 10, 0}),
1177 analyzer({"", 0.0, 0.0, 0, "", ""}),
1178 pipe(),
ilnika014cc52017-03-07 04:21:04 -08001179 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}),
ilnik98436952017-07-13 00:47:03 -07001180 logging({false, "", "", ""}) {}
minyue626bc952016-10-31 05:47:02 -07001181
1182VideoQualityTest::Params::~Params() = default;
1183
ivica5d6a06c2015-09-17 05:30:24 -07001184void VideoQualityTest::TestBody() {}
1185
sprangce4aef12015-11-02 07:23:20 -08001186std::string VideoQualityTest::GenerateGraphTitle() const {
1187 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -07001188 ss << params_.video.codec;
1189 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
1190 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -08001191 if (params_.screenshare.scroll_duration)
1192 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
1193 if (params_.ss.streams.size() > 1)
1194 ss << ", Stream #" << params_.ss.selected_stream;
1195 if (params_.ss.num_spatial_layers > 1)
1196 ss << ", Layer #" << params_.ss.selected_sl;
1197 ss << ")";
1198 return ss.str();
1199}
1200
1201void VideoQualityTest::CheckParams() {
stefan7de8d642017-02-07 07:14:08 -08001202 if (!params_.video.enabled)
1203 return;
sprangce4aef12015-11-02 07:23:20 -08001204 // Add a default stream in none specified.
1205 if (params_.ss.streams.empty())
1206 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
1207 if (params_.ss.num_spatial_layers == 0)
1208 params_.ss.num_spatial_layers = 1;
1209
1210 if (params_.pipe.loss_percent != 0 ||
1211 params_.pipe.queue_length_packets != 0) {
1212 // Since LayerFilteringTransport changes the sequence numbers, we can't
1213 // use that feature with pack loss, since the NACK request would end up
1214 // retransmitting the wrong packets.
1215 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -08001216 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -07001217 RTC_CHECK(params_.video.selected_tl == -1 ||
1218 params_.video.selected_tl ==
1219 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001220 }
1221
1222 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
1223 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -07001224 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
1225 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
1226 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprang1168fd42017-06-21 09:00:17 -07001227 RTC_CHECK_LE(params_.ss.selected_stream, params_.ss.streams.size());
sprangce4aef12015-11-02 07:23:20 -08001228 for (const VideoStream& stream : params_.ss.streams) {
1229 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1230 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1231 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
sprangce4aef12015-11-02 07:23:20 -08001232 }
1233 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1234 // the total bitrate? We anyway have to update them in the case bitrate
1235 // estimator changes the total bitrates.
1236 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
1237 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
1238 RTC_CHECK(params_.ss.spatial_layers.empty() ||
1239 params_.ss.spatial_layers.size() ==
1240 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -07001241 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -08001242 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -07001243 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -08001244 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -08001245 }
ilnik98436952017-07-13 00:47:03 -07001246 RTC_CHECK_GE(params_.call.num_thumbnails, 0);
1247 if (params_.call.num_thumbnails > 0) {
ilnika014cc52017-03-07 04:21:04 -08001248 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
1249 RTC_CHECK_EQ(params_.ss.streams.size(), 3);
1250 RTC_CHECK_EQ(params_.video.num_temporal_layers, 3);
1251 RTC_CHECK_EQ(params_.video.codec, "VP8");
1252 }
sprangce4aef12015-11-02 07:23:20 -08001253}
1254
1255// Static.
1256std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1257 // Parse comma separated nonnegative integers, where some elements may be
1258 // empty. The empty values are replaced with -1.
1259 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1260 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1261 std::vector<int> result;
1262 if (str.empty())
1263 return result;
1264
1265 const char* p = str.c_str();
1266 int value = -1;
1267 int pos;
1268 while (*p) {
1269 if (*p == ',') {
1270 result.push_back(value);
1271 value = -1;
1272 ++p;
1273 continue;
1274 }
1275 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1276 << "Unexpected non-number value.";
1277 p += pos;
1278 }
1279 result.push_back(value);
1280 return result;
1281}
1282
1283// Static.
1284VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
1285 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -07001286 stream.width = params.video.width;
1287 stream.height = params.video.height;
1288 stream.max_framerate = params.video.fps;
1289 stream.min_bitrate_bps = params.video.min_bitrate_bps;
1290 stream.target_bitrate_bps = params.video.target_bitrate_bps;
1291 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprang1168fd42017-06-21 09:00:17 -07001292 stream.max_qp = kDefaultMaxQp;
sprang6ef1b342017-03-13 02:01:32 -07001293 // TODO(sprang): Can we make this less of a hack?
1294 if (params.video.num_temporal_layers == 2) {
1295 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
1296 } else if (params.video.num_temporal_layers == 3) {
1297 stream.temporal_layer_thresholds_bps.push_back(stream.max_bitrate_bps / 4);
1298 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
ilnika014cc52017-03-07 04:21:04 -08001299 }
1300 return stream;
1301}
1302
1303// Static.
1304VideoStream VideoQualityTest::DefaultThumbnailStream() {
1305 VideoStream stream;
1306 stream.width = 320;
1307 stream.height = 180;
1308 stream.max_framerate = 7;
1309 stream.min_bitrate_bps = 7500;
1310 stream.target_bitrate_bps = 37500;
1311 stream.max_bitrate_bps = 50000;
sprang1168fd42017-06-21 09:00:17 -07001312 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 07:23:20 -08001313 return stream;
1314}
1315
1316// Static.
1317void VideoQualityTest::FillScalabilitySettings(
1318 Params* params,
1319 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 09:00:17 -07001320 int num_streams,
sprangce4aef12015-11-02 07:23:20 -08001321 size_t selected_stream,
1322 int num_spatial_layers,
1323 int selected_sl,
1324 const std::vector<std::string>& sl_descriptors) {
sprang1168fd42017-06-21 09:00:17 -07001325 if (params->ss.streams.empty() && params->ss.infer_streams) {
1326 webrtc::VideoEncoderConfig encoder_config;
1327 encoder_config.content_type =
1328 params->screenshare.enabled
1329 ? webrtc::VideoEncoderConfig::ContentType::kScreen
1330 : webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo;
1331 encoder_config.max_bitrate_bps = params->video.max_bitrate_bps;
1332 encoder_config.min_transmit_bitrate_bps = params->video.min_transmit_bps;
1333 encoder_config.number_of_streams = num_streams;
1334 encoder_config.spatial_layers = params->ss.spatial_layers;
1335 encoder_config.video_stream_factory =
1336 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1337 params->video.codec, kDefaultMaxQp, params->video.fps,
1338 params->screenshare.enabled, true);
1339 params->ss.streams =
1340 encoder_config.video_stream_factory->CreateEncoderStreams(
1341 static_cast<int>(params->video.width),
1342 static_cast<int>(params->video.height), encoder_config);
1343 } else {
1344 // Read VideoStream and SpatialLayer elements from a list of comma separated
1345 // lists. To use a default value for an element, use -1 or leave empty.
1346 // Validity checks performed in CheckParams.
1347 RTC_CHECK(params->ss.streams.empty());
1348 for (auto descriptor : stream_descriptors) {
1349 if (descriptor.empty())
1350 continue;
1351 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
1352 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1353 if (v[0] != -1)
1354 stream.width = static_cast<size_t>(v[0]);
1355 if (v[1] != -1)
1356 stream.height = static_cast<size_t>(v[1]);
1357 if (v[2] != -1)
1358 stream.max_framerate = v[2];
1359 if (v[3] != -1)
1360 stream.min_bitrate_bps = v[3];
1361 if (v[4] != -1)
1362 stream.target_bitrate_bps = v[4];
1363 if (v[5] != -1)
1364 stream.max_bitrate_bps = v[5];
1365 if (v.size() > 6 && v[6] != -1)
1366 stream.max_qp = v[6];
1367 if (v.size() > 7) {
1368 stream.temporal_layer_thresholds_bps.clear();
1369 stream.temporal_layer_thresholds_bps.insert(
1370 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1371 } else {
1372 // Automatic TL thresholds for more than two layers not supported.
1373 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
1374 }
1375 params->ss.streams.push_back(stream);
sprangce4aef12015-11-02 07:23:20 -08001376 }
sprangce4aef12015-11-02 07:23:20 -08001377 }
sprangce4aef12015-11-02 07:23:20 -08001378
sprang1168fd42017-06-21 09:00:17 -07001379 params->ss.num_spatial_layers = std::max(1, num_spatial_layers);
1380 params->ss.selected_stream = selected_stream;
1381
sprangce4aef12015-11-02 07:23:20 -08001382 params->ss.selected_sl = selected_sl;
1383 RTC_CHECK(params->ss.spatial_layers.empty());
1384 for (auto descriptor : sl_descriptors) {
1385 if (descriptor.empty())
1386 continue;
1387 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1388 RTC_CHECK_GT(v[2], 0);
1389
1390 SpatialLayer layer;
1391 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1392 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1393 layer.target_bitrate_bps = v[2];
1394 params->ss.spatial_layers.push_back(layer);
1395 }
1396}
1397
minyuea27172d2016-11-01 05:59:29 -07001398void VideoQualityTest::SetupVideo(Transport* send_transport,
1399 Transport* recv_transport) {
ilnik98436952017-07-13 00:47:03 -07001400 if (params_.logging.logs)
ivica5d6a06c2015-09-17 05:30:24 -07001401 trace_to_stderr_.reset(new test::TraceToStderr);
1402
brandtr8313a6f2017-01-13 07:41:19 -08001403 size_t num_video_streams = params_.ss.streams.size();
1404 size_t num_flexfec_streams = params_.video.flexfec ? 1 : 0;
1405 CreateSendConfig(num_video_streams, 0, num_flexfec_streams, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001406
1407 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001408 if (params_.video.codec == "H264") {
magjedceecea42016-11-28 07:20:21 -08001409 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001410 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001411 } else if (params_.video.codec == "VP8") {
ilnikcb8c1462017-03-09 09:23:30 -08001412 if (params_.screenshare.enabled && params_.ss.streams.size() > 1) {
1413 // Simulcast screenshare needs a simulcast encoder adapter to work, since
1414 // encoders usually can't natively do simulcast with different frame rates
1415 // for the different layers.
1416 video_encoder_.reset(
1417 new SimulcastEncoderAdapter(new Vp8EncoderFactory()));
1418 } else {
1419 video_encoder_.reset(VP8Encoder::Create());
1420 }
ivica5d6a06c2015-09-17 05:30:24 -07001421 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001422 } else if (params_.video.codec == "VP9") {
magjed509e4fe2016-11-18 01:34:11 -08001423 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001424 payload_type = kPayloadTypeVP9;
1425 } else {
1426 RTC_NOTREACHED() << "Codec not supported!";
1427 return;
1428 }
minyuea27172d2016-11-01 05:59:29 -07001429 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001430 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001431 video_send_config_.encoder_settings.payload_type = payload_type;
1432 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1433 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
brandtr8313a6f2017-01-13 07:41:19 -08001434 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001435 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001436
stefanff483612015-12-21 03:14:00 -08001437 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001438 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001439 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001440 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001441 test::kTransportSequenceNumberExtensionId));
1442 } else {
stefanff483612015-12-21 03:14:00 -08001443 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001444 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +02001445 }
ilnik00d802b2017-04-11 10:34:31 -07001446 video_send_config_.rtp.extensions.push_back(RtpExtension(
1447 RtpExtension::kVideoContentTypeUri, test::kVideoContentTypeExtensionId));
ilnik04f4d122017-06-19 07:18:55 -07001448 video_send_config_.rtp.extensions.push_back(RtpExtension(
1449 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +02001450
stefanff483612015-12-21 03:14:00 -08001451 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001452 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001453
brandtr1293aca2016-11-16 22:47:29 -08001454 video_send_config_.suspend_below_min_bitrate =
1455 params_.video.suspend_below_min_bitrate;
1456
perkjfa10b552016-10-02 23:45:26 -07001457 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1458 video_encoder_config_.max_bitrate_bps = 0;
1459 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1460 video_encoder_config_.max_bitrate_bps +=
1461 params_.ss.streams[i].max_bitrate_bps;
1462 }
ilnik6b826ef2017-06-16 06:53:48 -07001463 if (params_.ss.infer_streams) {
1464 video_encoder_config_.video_stream_factory =
1465 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1466 params_.video.codec, params_.ss.streams[0].max_qp,
1467 params_.video.fps, params_.screenshare.enabled, true);
1468 } else {
1469 video_encoder_config_.video_stream_factory =
1470 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1471 }
perkjfa10b552016-10-02 23:45:26 -07001472
stefanff483612015-12-21 03:14:00 -08001473 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001474
1475 CreateMatchingReceiveConfigs(recv_transport);
1476
sprang1168fd42017-06-21 09:00:17 -07001477 const bool decode_all_receive_streams =
1478 params_.ss.selected_stream == params_.ss.streams.size();
1479
brandtr8313a6f2017-01-13 07:41:19 -08001480 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001481 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
brandtr14742122017-01-27 04:53:07 -08001482 video_receive_configs_[i].rtp.rtx_ssrc = kSendRtxSsrcs[i];
1483 video_receive_configs_[i].rtp.rtx_payload_types[payload_type] =
sprangce4aef12015-11-02 07:23:20 -08001484 kSendRtxPayloadType;
minyue626bc952016-10-31 05:47:02 -07001485 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
Stefan Holmer85d5ac72017-02-09 16:25:16 +01001486 video_receive_configs_[i].rtp.remb = !params_.call.send_side_bwe;
ilnik00d802b2017-04-11 10:34:31 -07001487 // Enable RTT calculation so NTP time estimator will work.
1488 video_receive_configs_[i].rtp.rtcp_xr.receiver_reference_time_report = true;
ilnik9fd9f6c2017-03-02 08:10:10 -08001489 // Force fake decoders on non-selected simulcast streams.
sprang1168fd42017-06-21 09:00:17 -07001490 if (!decode_all_receive_streams && i != params_.ss.selected_stream) {
ilnik9fd9f6c2017-03-02 08:10:10 -08001491 VideoReceiveStream::Decoder decoder;
1492 decoder.decoder = new test::FakeDecoder();
1493 decoder.payload_type = video_send_config_.encoder_settings.payload_type;
1494 decoder.payload_name = video_send_config_.encoder_settings.payload_name;
1495 video_receive_configs_[i].decoders.clear();
1496 allocated_decoders_.emplace_back(decoder.decoder);
1497 video_receive_configs_[i].decoders.push_back(decoder);
1498 }
sprangce4aef12015-11-02 07:23:20 -08001499 }
brandtr1293aca2016-11-16 22:47:29 -08001500
1501 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 07:41:19 -08001502 // Override send config constructed by CreateSendConfig.
sprang1168fd42017-06-21 09:00:17 -07001503 if (decode_all_receive_streams) {
1504 for (uint32_t media_ssrc : video_send_config_.rtp.ssrcs) {
1505 video_send_config_.rtp.flexfec.protected_media_ssrcs.push_back(
1506 media_ssrc);
1507 }
1508 } else {
1509 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1510 kVideoSendSsrcs[params_.ss.selected_stream]};
1511 }
brandtr1293aca2016-11-16 22:47:29 -08001512
brandtr8313a6f2017-01-13 07:41:19 -08001513 // The matching receive config is _not_ created by
1514 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1515 // Set up the receive config manually instead.
1516 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001517 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 06:59:19 -08001518 video_send_config_.rtp.flexfec.payload_type;
1519 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001520 flexfec_receive_config.protected_media_ssrcs =
1521 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001522 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001523 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1524 if (params_.call.send_side_bwe) {
1525 flexfec_receive_config.rtp_header_extensions.push_back(
1526 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1527 test::kTransportSequenceNumberExtensionId));
1528 } else {
1529 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1530 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1531 }
brandtr1293aca2016-11-16 22:47:29 -08001532 flexfec_receive_configs_.push_back(flexfec_receive_config);
1533 }
1534
1535 if (params_.video.ulpfec) {
1536 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1537 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1538 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1539
sprang1168fd42017-06-21 09:00:17 -07001540 if (decode_all_receive_streams) {
1541 for (auto it = video_receive_configs_.begin();
1542 it != video_receive_configs_.end(); ++it) {
1543 it->rtp.ulpfec.red_payload_type =
1544 video_send_config_.rtp.ulpfec.red_payload_type;
1545 it->rtp.ulpfec.ulpfec_payload_type =
1546 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1547 it->rtp.ulpfec.red_rtx_payload_type =
1548 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1549 }
1550 } else {
1551 video_receive_configs_[params_.ss.selected_stream]
1552 .rtp.ulpfec.red_payload_type =
1553 video_send_config_.rtp.ulpfec.red_payload_type;
1554 video_receive_configs_[params_.ss.selected_stream]
1555 .rtp.ulpfec.ulpfec_payload_type =
1556 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1557 video_receive_configs_[params_.ss.selected_stream]
1558 .rtp.ulpfec.red_rtx_payload_type =
1559 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1560 }
brandtr1293aca2016-11-16 22:47:29 -08001561 }
ivica5d6a06c2015-09-17 05:30:24 -07001562}
1563
ilnika014cc52017-03-07 04:21:04 -08001564void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1565 Transport* recv_transport) {
ilnik98436952017-07-13 00:47:03 -07001566 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001567 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1568
1569 // Thumbnails will be send in the other way: from receiver_call to
1570 // sender_call.
1571 VideoSendStream::Config thumbnail_send_config(recv_transport);
1572 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1573 thumbnail_send_config.encoder_settings.encoder =
1574 thumbnail_encoders_.back().get();
1575 thumbnail_send_config.encoder_settings.payload_name = params_.video.codec;
1576 thumbnail_send_config.encoder_settings.payload_type = kPayloadTypeVP8;
1577 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1578 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1579 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1580 thumbnail_send_config.rtp.extensions.clear();
1581 if (params_.call.send_side_bwe) {
1582 thumbnail_send_config.rtp.extensions.push_back(
1583 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1584 test::kTransportSequenceNumberExtensionId));
1585 } else {
1586 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1587 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1588 }
1589
1590 VideoEncoderConfig thumbnail_encoder_config;
1591 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1592 thumbnail_send_config.suspend_below_min_bitrate =
1593 params_.video.suspend_below_min_bitrate;
1594 thumbnail_encoder_config.number_of_streams = 1;
1595 thumbnail_encoder_config.max_bitrate_bps = 50000;
ilnik6b826ef2017-06-16 06:53:48 -07001596 if (params_.ss.infer_streams) {
1597 thumbnail_encoder_config.video_stream_factory =
1598 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1599 } else {
1600 thumbnail_encoder_config.video_stream_factory =
1601 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1602 params_.video.codec, params_.ss.streams[0].max_qp,
1603 params_.video.fps, params_.screenshare.enabled, true);
1604 }
ilnika014cc52017-03-07 04:21:04 -08001605 thumbnail_encoder_config.spatial_layers = params_.ss.spatial_layers;
1606
1607 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1608 thumbnail_receive_config.rtp.remb = false;
1609 thumbnail_receive_config.rtp.transport_cc = true;
1610 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1611 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1612 thumbnail_receive_config.rtp.extensions.push_back(extension);
1613 thumbnail_receive_config.renderer = &fake_renderer_;
1614
1615 VideoReceiveStream::Decoder decoder =
1616 test::CreateMatchingDecoder(thumbnail_send_config.encoder_settings);
1617 allocated_decoders_.push_back(
1618 std::unique_ptr<VideoDecoder>(decoder.decoder));
1619 thumbnail_receive_config.decoders.clear();
1620 thumbnail_receive_config.decoders.push_back(decoder);
1621 thumbnail_receive_config.rtp.remote_ssrc =
1622 thumbnail_send_config.rtp.ssrcs[0];
1623
1624 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1625 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
1626 thumbnail_receive_config.rtp.rtx_payload_types[kPayloadTypeVP8] =
1627 kSendRtxPayloadType;
1628 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1629 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1630
1631 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1632 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1633 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1634 }
1635
ilnik98436952017-07-13 00:47:03 -07001636 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 04:21:04 -08001637 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1638 thumbnail_send_configs_[i].Copy(),
1639 thumbnail_encoder_configs_[i].Copy()));
1640 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1641 thumbnail_receive_configs_[i].Copy()));
1642 }
1643}
1644
1645void VideoQualityTest::DestroyThumbnailStreams() {
1646 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1647 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
1648 thumbnail_send_streams_.clear();
1649 for (VideoReceiveStream* thumbnail_receive_stream :
1650 thumbnail_receive_streams_)
1651 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
1652 thumbnail_send_streams_.clear();
1653 thumbnail_receive_streams_.clear();
1654}
1655
ilnik2a8c2f52017-02-15 02:23:28 -08001656void VideoQualityTest::SetupScreenshareOrSVC() {
1657 if (params_.screenshare.enabled) {
1658 // Fill out codec settings.
1659 video_encoder_config_.content_type =
1660 VideoEncoderConfig::ContentType::kScreen;
1661 degradation_preference_ =
1662 VideoSendStream::DegradationPreference::kMaintainResolution;
1663 if (params_.video.codec == "VP8") {
1664 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1665 vp8_settings.denoisingOn = false;
1666 vp8_settings.frameDroppingOn = false;
1667 vp8_settings.numberOfTemporalLayers =
1668 static_cast<unsigned char>(params_.video.num_temporal_layers);
1669 video_encoder_config_.encoder_specific_settings =
1670 new rtc::RefCountedObject<
1671 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1672 } else if (params_.video.codec == "VP9") {
1673 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1674 vp9_settings.denoisingOn = false;
1675 vp9_settings.frameDroppingOn = false;
1676 vp9_settings.numberOfTemporalLayers =
1677 static_cast<unsigned char>(params_.video.num_temporal_layers);
1678 vp9_settings.numberOfSpatialLayers =
1679 static_cast<unsigned char>(params_.ss.num_spatial_layers);
1680 video_encoder_config_.encoder_specific_settings =
1681 new rtc::RefCountedObject<
1682 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1683 }
1684 // Setup frame generator.
1685 const size_t kWidth = 1850;
1686 const size_t kHeight = 1110;
ilnik8d8185c2017-04-12 04:52:55 -07001687 std::vector<std::string> slides = params_.screenshare.slides;
1688 if (slides.size() == 0) {
1689 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1690 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1691 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1692 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1693 }
ilnik2a8c2f52017-02-15 02:23:28 -08001694 if (params_.screenshare.scroll_duration == 0) {
1695 // Cycle image every slide_change_interval seconds.
perkja8ba1952017-02-27 06:52:10 -08001696 frame_generator_ = test::FrameGenerator::CreateFromYuvFile(
ilnik2a8c2f52017-02-15 02:23:28 -08001697 slides, kWidth, kHeight,
perkja8ba1952017-02-27 06:52:10 -08001698 params_.screenshare.slide_change_interval * params_.video.fps);
ilnik2a8c2f52017-02-15 02:23:28 -08001699 } else {
1700 RTC_CHECK_LE(params_.video.width, kWidth);
1701 RTC_CHECK_LE(params_.video.height, kHeight);
1702 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1703 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1704 params_.screenshare.scroll_duration) *
1705 1000;
1706 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1707 params_.screenshare.slide_change_interval);
1708
perkja8ba1952017-02-27 06:52:10 -08001709 frame_generator_ = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1710 clock_, slides, kWidth, kHeight, params_.video.width,
1711 params_.video.height, params_.screenshare.scroll_duration * 1000,
1712 kPauseDurationMs);
ilnik2a8c2f52017-02-15 02:23:28 -08001713 }
1714 } else if (params_.ss.num_spatial_layers > 1) { // For non-screenshare case.
1715 RTC_CHECK(params_.video.codec == "VP9");
kthelgason29a44e32016-09-27 03:52:02 -07001716 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
kthelgason29a44e32016-09-27 03:52:02 -07001717 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001718 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001719 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001720 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001721 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1722 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001723 }
ivica5d6a06c2015-09-17 05:30:24 -07001724}
1725
ilnika014cc52017-03-07 04:21:04 -08001726void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1727 VideoStream thumbnail = DefaultThumbnailStream();
1728 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1729 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1730 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
1731 thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -08001732 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -08001733 }
1734}
1735
perkja49cbd32016-09-16 07:53:41 -07001736void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001737 if (params_.screenshare.enabled) {
1738 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja8ba1952017-02-27 06:52:10 -08001739 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator_),
minyue626bc952016-10-31 05:47:02 -07001740 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001741 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001742 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001743 } else {
ilnik6b826ef2017-06-16 06:53:48 -07001744 if (params_.video.clip_name == "Generator") {
1745 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
1746 static_cast<int>(params_.video.width),
1747 static_cast<int>(params_.video.height), params_.video.fps, clock_));
1748 } else if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001749 video_capturer_.reset(test::VcmCapturer::Create(
Tarun Chawla8e857d12017-05-31 19:20:57 +05301750 params_.video.width, params_.video.height, params_.video.fps,
1751 params_.video.capture_device_index));
sprang1bed2e42017-01-23 08:46:51 -08001752 if (!video_capturer_) {
1753 // Failed to get actual camera, use chroma generator as backup.
1754 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
perkja8ba1952017-02-27 06:52:10 -08001755 static_cast<int>(params_.video.width),
1756 static_cast<int>(params_.video.height), params_.video.fps, clock_));
sprang1bed2e42017-01-23 08:46:51 -08001757 }
ivica5d6a06c2015-09-17 05:30:24 -07001758 } else {
minyuea27172d2016-11-01 05:59:29 -07001759 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001760 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001761 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001762 clock_));
minyuea27172d2016-11-01 05:59:29 -07001763 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1764 << params_.video.clip_name
1765 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001766 }
1767 }
sprang1bed2e42017-01-23 08:46:51 -08001768 RTC_DCHECK(video_capturer_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001769}
1770
sprang7a975f72015-10-12 06:33:21 -07001771void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001772 params_ = params;
1773
minyue626bc952016-10-31 05:47:02 -07001774 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001775 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1776 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001777 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001778
1779 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001780 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001781 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001782 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001783 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001784 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1785 << "!";
ivica87f83a92015-10-08 05:13:32 -07001786 }
sprang7a975f72015-10-12 06:33:21 -07001787
ilnik98436952017-07-13 00:47:03 -07001788 if (!params.logging.rtc_event_log_name.empty()) {
1789 event_log_ = RtcEventLog::Create(clock_);
1790 bool event_log_started =
1791 event_log_->StartLogging(params.logging.rtc_event_log_name, -1);
1792 RTC_DCHECK(event_log_started);
1793 }
1794
philipel4fb651d2017-04-10 03:54:05 -07001795 Call::Config call_config(event_log_.get());
minyue626bc952016-10-31 05:47:02 -07001796 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001797 CreateCalls(call_config, call_config);
1798
ilnik68af10d2017-03-02 04:59:33 -08001799 test::LayerFilteringTransport send_transport(
1800 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue20c84cc2017-04-10 16:57:57 -07001801 params_.video.selected_tl, params_.ss.selected_sl, payload_type_map_);
1802
1803 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get(),
1804 payload_type_map_);
stefanf116bd02015-10-27 08:29:42 -07001805
sprangce4aef12015-11-02 07:23:20 -08001806 std::string graph_title = params_.analyzer.graph_title;
1807 if (graph_title.empty())
1808 graph_title = VideoQualityTest::GenerateGraphTitle();
1809
sprangc1b57a12017-02-28 08:50:47 -08001810 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
ivica5d6a06c2015-09-17 05:30:24 -07001811 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001812 &send_transport, params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001813 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001814 is_quick_test_enabled
1815 ? kFramesSentInQuickTest
1816 : params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001817 graph_data_output_file, graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -08001818 kVideoSendSsrcs[params_.ss.selected_stream],
ilnik46a00212017-02-10 09:16:05 -08001819 kSendRtxSsrcs[params_.ss.selected_stream],
ilnikcb8c1462017-03-09 09:23:30 -08001820 static_cast<size_t>(params_.ss.selected_stream), params.ss.selected_sl,
ilnik98436952017-07-13 00:47:03 -07001821 params_.video.selected_tl, is_quick_test_enabled, clock_,
1822 params_.logging.rtp_dump_name);
stefan889d9652017-07-05 03:03:02 -07001823 analyzer.SetCall(sender_call_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001824 analyzer.SetReceiver(receiver_call_->Receiver());
1825 send_transport.SetReceiver(&analyzer);
1826 recv_transport.SetReceiver(sender_call_->Receiver());
1827
minyuea27172d2016-11-01 05:59:29 -07001828 SetupVideo(&analyzer, &recv_transport);
ilnika014cc52017-03-07 04:21:04 -08001829 SetupThumbnails(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001830 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001831 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
Peter Boströme4499152016-02-05 11:13:28 +01001832 RTC_DCHECK(!video_send_config_.post_encode_callback);
1833 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001834
ilnik2a8c2f52017-02-15 02:23:28 -08001835 SetupScreenshareOrSVC();
ivica5d6a06c2015-09-17 05:30:24 -07001836
brandtr1293aca2016-11-16 22:47:29 -08001837 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001838 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001839 analyzer.SetSendStream(video_send_stream_);
philipelfd870db2017-01-23 03:22:15 -08001840 if (video_receive_streams_.size() == 1)
1841 analyzer.SetReceiveStream(video_receive_streams_[0]);
kthelgason2bc68642017-02-07 07:02:22 -08001842
1843 video_send_stream_->SetSource(analyzer.OutputInterface(),
1844 degradation_preference_);
ivica5d6a06c2015-09-17 05:30:24 -07001845
ilnik98436952017-07-13 00:47:03 -07001846 SetupThumbnailCapturers(params_.call.num_thumbnails);
ilnika014cc52017-03-07 04:21:04 -08001847 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1848 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1849 degradation_preference_);
1850 }
1851
perkja49cbd32016-09-16 07:53:41 -07001852 CreateCapturer();
ilnika014cc52017-03-07 04:21:04 -08001853
ilnik6b826ef2017-06-16 06:53:48 -07001854 analyzer.SetSource(video_capturer_.get(), params_.ss.infer_streams);
ivicac1cc8542015-10-08 03:44:06 -07001855
palmkviste75f2042016-09-28 06:19:48 -07001856 StartEncodedFrameLogs(video_send_stream_);
ilnik98436952017-07-13 00:47:03 -07001857 StartEncodedFrameLogs(video_receive_streams_[params_.ss.selected_stream]);
stefanff483612015-12-21 03:14:00 -08001858 video_send_stream_->Start();
ilnika014cc52017-03-07 04:21:04 -08001859 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1860 thumbnail_send_stream->Start();
stefanff483612015-12-21 03:14:00 -08001861 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1862 receive_stream->Start();
brandtr1293aca2016-11-16 22:47:29 -08001863 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1864 receive_stream->Start();
ilnika014cc52017-03-07 04:21:04 -08001865 for (VideoReceiveStream* thumbnail_receive_stream :
1866 thumbnail_receive_streams_)
1867 thumbnail_receive_stream->Start();
1868
ilnikdf92c5c2017-02-23 02:08:44 -08001869 analyzer.StartMeasuringCpuProcessTime();
ilnika014cc52017-03-07 04:21:04 -08001870
minyuea27172d2016-11-01 05:59:29 -07001871 video_capturer_->Start();
ilnika014cc52017-03-07 04:21:04 -08001872 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1873 thumbnail_capturers_) {
1874 video_caputurer->Start();
1875 }
ivica5d6a06c2015-09-17 05:30:24 -07001876
1877 analyzer.Wait();
1878
1879 send_transport.StopSending();
1880 recv_transport.StopSending();
1881
ilnika014cc52017-03-07 04:21:04 -08001882 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1883 thumbnail_capturers_)
1884 video_caputurer->Stop();
minyuea27172d2016-11-01 05:59:29 -07001885 video_capturer_->Stop();
ilnika014cc52017-03-07 04:21:04 -08001886 for (VideoReceiveStream* thumbnail_receive_stream :
1887 thumbnail_receive_streams_)
1888 thumbnail_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001889 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1890 receive_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001891 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1892 receive_stream->Stop();
ilnika014cc52017-03-07 04:21:04 -08001893 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1894 thumbnail_send_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001895 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001896
1897 DestroyStreams();
ilnika014cc52017-03-07 04:21:04 -08001898 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -07001899
ilnik98436952017-07-13 00:47:03 -07001900 event_log_->StopLogging();
ivica5d6a06c2015-09-17 05:30:24 -07001901 if (graph_data_output_file)
1902 fclose(graph_data_output_file);
1903}
1904
minyuea27172d2016-11-01 05:59:29 -07001905void VideoQualityTest::SetupAudio(int send_channel_id,
1906 int receive_channel_id,
minyuea27172d2016-11-01 05:59:29 -07001907 Transport* transport,
1908 AudioReceiveStream** audio_receive_stream) {
1909 audio_send_config_ = AudioSendStream::Config(transport);
1910 audio_send_config_.voe_channel_id = send_channel_id;
1911 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1912
1913 // Add extension to enable audio send side BWE, and allow audio bit rate
1914 // adaptation.
1915 audio_send_config_.rtp.extensions.clear();
1916 if (params_.call.send_side_bwe) {
1917 audio_send_config_.rtp.extensions.push_back(
1918 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1919 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001920 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1921 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001922 }
ossu20a4b3f2017-04-27 02:08:52 -07001923 audio_send_config_.send_codec_spec =
1924 rtc::Optional<AudioSendStream::Config::SendCodecSpec>(
1925 {kAudioSendPayloadType,
1926 {"OPUS", 48000, 2,
1927 {{"usedtx", (params_.audio.dtx ? "1" : "0")},
1928 {"stereo", "1"}}}});
minyue48368ad2017-05-10 04:06:11 -07001929 audio_send_config_.encoder_factory = encoder_factory_;
sprang1168fd42017-06-21 09:00:17 -07001930 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
minyuea27172d2016-11-01 05:59:29 -07001931
1932 AudioReceiveStream::Config audio_config;
1933 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1934 audio_config.rtcp_send_transport = transport;
1935 audio_config.voe_channel_id = receive_channel_id;
1936 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1937 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1938 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1939 audio_config.decoder_factory = decoder_factory_;
minyue20c84cc2017-04-10 16:57:57 -07001940 audio_config.decoder_map = {{kAudioSendPayloadType, {"OPUS", 48000, 2}}};
minyuea27172d2016-11-01 05:59:29 -07001941 if (params_.video.enabled && params_.audio.sync_video)
1942 audio_config.sync_group = kSyncGroup;
1943
sprang1168fd42017-06-21 09:00:17 -07001944 *audio_receive_stream =
1945 receiver_call_->CreateAudioReceiveStream(audio_config);
minyuea27172d2016-11-01 05:59:29 -07001946}
1947
minyue73208662016-08-18 06:28:55 -07001948void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001949 params_ = params;
1950 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001951
ivica5d6a06c2015-09-17 05:30:24 -07001952 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1953 // match the full stack tests.
philipel4fb651d2017-04-10 03:54:05 -07001954 Call::Config call_config(event_log_.get());
minyue626bc952016-10-31 05:47:02 -07001955 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001956
1957 ::VoiceEngineState voe;
peaha9cc40b2017-06-29 08:32:09 -07001958 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing(
1959 webrtc::AudioProcessing::Create());
1960
minyue626bc952016-10-31 05:47:02 -07001961 if (params_.audio.enabled) {
peaha9cc40b2017-06-29 08:32:09 -07001962 CreateVoiceEngine(&voe, audio_processing.get(), decoder_factory_);
minyue73208662016-08-18 06:28:55 -07001963 AudioState::Config audio_state_config;
1964 audio_state_config.voice_engine = voe.voice_engine;
aleloi10111bc2016-11-17 06:48:48 -08001965 audio_state_config.audio_mixer = AudioMixerImpl::Create();
peaha9cc40b2017-06-29 08:32:09 -07001966 audio_state_config.audio_processing = audio_processing;
minyue73208662016-08-18 06:28:55 -07001967 call_config.audio_state = AudioState::Create(audio_state_config);
1968 }
1969
sprang1168fd42017-06-21 09:00:17 -07001970 CreateCalls(call_config, call_config);
ivica5d6a06c2015-09-17 05:30:24 -07001971
minyuea27172d2016-11-01 05:59:29 -07001972 // TODO(minyue): consider if this is a good transport even for audio only
1973 // calls.
sprang1168fd42017-06-21 09:00:17 -07001974 test::LayerFilteringTransport send_transport(
1975 params.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue20c84cc2017-04-10 16:57:57 -07001976 params.video.selected_tl, params_.ss.selected_sl, payload_type_map_);
1977
sprang1168fd42017-06-21 09:00:17 -07001978 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get(),
1979 payload_type_map_);
1980
ivica5d6a06c2015-09-17 05:30:24 -07001981 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1982 // least share as much code as possible. That way this test would also match
1983 // the full stack tests better.
sprang1168fd42017-06-21 09:00:17 -07001984 send_transport.SetReceiver(receiver_call_->Receiver());
1985 recv_transport.SetReceiver(sender_call_->Receiver());
ivica5d6a06c2015-09-17 05:30:24 -07001986
minyuea27172d2016-11-01 05:59:29 -07001987 std::unique_ptr<test::VideoRenderer> local_preview;
sprang1168fd42017-06-21 09:00:17 -07001988 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers_;
minyuea27172d2016-11-01 05:59:29 -07001989 if (params_.video.enabled) {
1990 // Create video renderers.
1991 local_preview.reset(test::VideoRenderer::Create(
1992 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 05:13:32 -07001993
sprang1168fd42017-06-21 09:00:17 -07001994 const size_t selected_stream_id = params_.ss.selected_stream;
1995 const size_t num_streams = params_.ss.streams.size();
1996
1997 if (selected_stream_id == num_streams) {
1998 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
1999 std::ostringstream oss;
2000 oss << "Loopback Video - Stream #" << static_cast<int>(stream_id);
2001 loopback_renderers_.emplace_back(test::VideoRenderer::Create(
2002 oss.str().c_str(), params_.ss.streams[stream_id].width,
2003 params_.ss.streams[stream_id].height));
2004 }
2005 } else {
2006 loopback_renderers_.emplace_back(test::VideoRenderer::Create(
2007 "Loopback Video", params_.ss.streams[selected_stream_id].width,
2008 params_.ss.streams[selected_stream_id].height));
minyuea27172d2016-11-01 05:59:29 -07002009 }
sprangce4aef12015-11-02 07:23:20 -08002010
sprang1168fd42017-06-21 09:00:17 -07002011 SetupVideo(&send_transport, &recv_transport);
mflodman48a4beb2016-07-01 13:03:59 +02002012
minyuea27172d2016-11-01 05:59:29 -07002013 video_send_config_.pre_encode_callback = local_preview.get();
sprang1168fd42017-06-21 09:00:17 -07002014 if (selected_stream_id == num_streams) {
2015 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
2016 video_receive_configs_[stream_id].renderer =
2017 loopback_renderers_[stream_id].get();
2018 if (params_.audio.enabled && params_.audio.sync_video)
2019 video_receive_configs_[stream_id].sync_group = kSyncGroup;
2020 }
2021 } else {
2022 video_receive_configs_[selected_stream_id].renderer =
2023 loopback_renderers_.back().get();
2024 if (params_.audio.enabled && params_.audio.sync_video)
2025 video_receive_configs_[selected_stream_id].sync_group = kSyncGroup;
2026 }
minyuea27172d2016-11-01 05:59:29 -07002027
ilnik68af10d2017-03-02 04:59:33 -08002028 if (params_.screenshare.enabled)
2029 SetupScreenshareOrSVC();
minyuea27172d2016-11-01 05:59:29 -07002030
sprang1168fd42017-06-21 09:00:17 -07002031 CreateFlexfecStreams();
2032 CreateVideoStreams();
2033
minyuea27172d2016-11-01 05:59:29 -07002034 CreateCapturer();
kthelgason2bc68642017-02-07 07:02:22 -08002035 video_send_stream_->SetSource(video_capturer_.get(),
2036 degradation_preference_);
philipel274c1dc2016-05-04 06:21:01 -07002037 }
2038
minyue73208662016-08-18 06:28:55 -07002039 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 05:47:02 -07002040 if (params_.audio.enabled) {
sprang1168fd42017-06-21 09:00:17 -07002041 SetupAudio(voe.send_channel_id, voe.receive_channel_id, &send_transport,
2042 &audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07002043 }
2044
sprang1168fd42017-06-21 09:00:17 -07002045 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2046 StartEncodedFrameLogs(receive_stream);
palmkviste75f2042016-09-28 06:19:48 -07002047 StartEncodedFrameLogs(video_send_stream_);
2048
minyue73208662016-08-18 06:28:55 -07002049 // Start sending and receiving video.
minyuea27172d2016-11-01 05:59:29 -07002050 if (params_.video.enabled) {
brandtrcb8f0452017-06-30 02:34:20 -07002051 for (FlexfecReceiveStream* flexfec_receive_stream :
2052 flexfec_receive_streams_) {
brandtr1293aca2016-11-16 22:47:29 -08002053 flexfec_receive_stream->Start();
brandtrcb8f0452017-06-30 02:34:20 -07002054 }
sprang1168fd42017-06-21 09:00:17 -07002055 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2056 receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07002057 video_send_stream_->Start();
2058 video_capturer_->Start();
2059 }
ivica5d6a06c2015-09-17 05:30:24 -07002060
minyue626bc952016-10-31 05:47:02 -07002061 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07002062 // Start receiving audio.
2063 audio_receive_stream->Start();
2064 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 06:28:55 -07002065
2066 // Start sending audio.
2067 audio_send_stream_->Start();
2068 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
2069 }
2070
ivica5d6a06c2015-09-17 05:30:24 -07002071 test::PressEnterToContinue();
2072
minyue626bc952016-10-31 05:47:02 -07002073 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07002074 // Stop sending audio.
2075 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
2076 audio_send_stream_->Stop();
2077
2078 // Stop receiving audio.
minyue73208662016-08-18 06:28:55 -07002079 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
2080 audio_receive_stream->Stop();
sprang1168fd42017-06-21 09:00:17 -07002081 sender_call_->DestroyAudioSendStream(audio_send_stream_);
2082 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07002083 }
2084
2085 // Stop receiving and sending video.
minyuea27172d2016-11-01 05:59:29 -07002086 if (params_.video.enabled) {
2087 video_capturer_->Stop();
2088 video_send_stream_->Stop();
brandtrcb8f0452017-06-30 02:34:20 -07002089 for (FlexfecReceiveStream* flexfec_receive_stream :
2090 flexfec_receive_streams_) {
brandtr1293aca2016-11-16 22:47:29 -08002091 flexfec_receive_stream->Stop();
sprang1168fd42017-06-21 09:00:17 -07002092 receiver_call_->DestroyFlexfecReceiveStream(flexfec_receive_stream);
brandtr1293aca2016-11-16 22:47:29 -08002093 }
brandtrcb8f0452017-06-30 02:34:20 -07002094 for (VideoReceiveStream* receive_stream : video_receive_streams_) {
2095 receive_stream->Stop();
sprang1168fd42017-06-21 09:00:17 -07002096 receiver_call_->DestroyVideoReceiveStream(receive_stream);
brandtrcb8f0452017-06-30 02:34:20 -07002097 }
sprang1168fd42017-06-21 09:00:17 -07002098 sender_call_->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 06:28:55 -07002099 }
2100
sprang1168fd42017-06-21 09:00:17 -07002101 send_transport.StopSending();
2102 recv_transport.StopSending();
2103
minyue626bc952016-10-31 05:47:02 -07002104 if (params_.audio.enabled)
minyue73208662016-08-18 06:28:55 -07002105 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07002106}
2107
palmkviste75f2042016-09-28 06:19:48 -07002108void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002109 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002110 std::ostringstream str;
2111 str << send_logs_++;
2112 std::string prefix =
ilnik98436952017-07-13 00:47:03 -07002113 params_.logging.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07002114 stream->EnableEncodedFrameRecording(
2115 std::vector<rtc::PlatformFile>(
2116 {rtc::CreatePlatformFile(prefix + "1.ivf"),
2117 rtc::CreatePlatformFile(prefix + "2.ivf"),
2118 rtc::CreatePlatformFile(prefix + "3.ivf")}),
ilnik98436952017-07-13 00:47:03 -07002119 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002120 }
2121}
ilnikcb8c1462017-03-09 09:23:30 -08002122
palmkviste75f2042016-09-28 06:19:48 -07002123void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
ilnik98436952017-07-13 00:47:03 -07002124 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002125 std::ostringstream str;
2126 str << receive_logs_++;
2127 std::string path =
ilnik98436952017-07-13 00:47:03 -07002128 params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07002129 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
ilnik98436952017-07-13 00:47:03 -07002130 100000000);
palmkviste75f2042016-09-28 06:19:48 -07002131 }
2132}
ivica5d6a06c2015-09-17 05:30:24 -07002133} // namespace webrtc