blob: affadc0a765dad1217d0d2acb694f21e3a057166 [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>
sprangce4aef12015-11-02 07:23:20 -080016#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080017#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070018#include <vector>
19
ivica5d6a06c2015-09-17 05:30:24 -070020#include "webrtc/base/checks.h"
ilnikdf92c5c2017-02-23 02:08:44 -080021#include "webrtc/base/cpu_time.h"
Peter Boström5811a392015-12-10 13:02:50 +010022#include "webrtc/base/event.h"
ivica5d6a06c2015-09-17 05:30:24 -070023#include "webrtc/base/format_macros.h"
nissec7fe3c22016-04-20 03:25:36 -070024#include "webrtc/base/optional.h"
palmkviste75f2042016-09-28 06:19:48 -070025#include "webrtc/base/platform_file.h"
sprange1f2f1f2016-02-01 02:04:52 -080026#include "webrtc/base/timeutils.h"
ossuf515ab82016-12-07 04:52:58 -080027#include "webrtc/call/call.h"
ivica5d6a06c2015-09-17 05:30:24 -070028#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
skvlad11a9cbf2016-10-07 11:53:05 -070029#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
aleloi10111bc2016-11-17 06:48:48 -080030#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010031#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
ilnik1e7732c2017-02-23 05:07:56 -080032#include "webrtc/modules/rtp_rtcp/source/rtp_format.h"
sprangce4aef12015-11-02 07:23:20 -080033#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
magjed509e4fe2016-11-18 01:34:11 -080034#include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
35#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
36#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010037#include "webrtc/system_wrappers/include/cpu_info.h"
ilnik9ae0d762017-02-15 00:53:12 -080038#include "webrtc/system_wrappers/include/field_trial.h"
kwibergac9f8762016-09-30 22:29:43 -070039#include "webrtc/test/gtest.h"
ivica5d6a06c2015-09-17 05:30:24 -070040#include "webrtc/test/layer_filtering_transport.h"
41#include "webrtc/test/run_loop.h"
42#include "webrtc/test/statistics.h"
43#include "webrtc/test/testsupport/fileutils.h"
perkja49cbd32016-09-16 07:53:41 -070044#include "webrtc/test/vcm_capturer.h"
ivica5d6a06c2015-09-17 05:30:24 -070045#include "webrtc/test/video_renderer.h"
minyue73208662016-08-18 06:28:55 -070046#include "webrtc/voice_engine/include/voe_base.h"
minyue73208662016-08-18 06:28:55 -070047
48namespace {
49
50constexpr int kSendStatsPollingIntervalMs = 1000;
51constexpr int kPayloadTypeH264 = 122;
52constexpr int kPayloadTypeVP8 = 123;
53constexpr int kPayloadTypeVP9 = 124;
54constexpr size_t kMaxComparisons = 10;
55constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080056constexpr int kOpusMinBitrateBps = 6000;
57constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 00:53:12 -080058constexpr int kFramesSentInQuickTest = 1;
minyue73208662016-08-18 06:28:55 -070059
60struct VoiceEngineState {
61 VoiceEngineState()
62 : voice_engine(nullptr),
63 base(nullptr),
minyue73208662016-08-18 06:28:55 -070064 send_channel_id(-1),
65 receive_channel_id(-1) {}
66
67 webrtc::VoiceEngine* voice_engine;
68 webrtc::VoEBase* base;
minyue73208662016-08-18 06:28:55 -070069 int send_channel_id;
70 int receive_channel_id;
71};
72
73void CreateVoiceEngine(VoiceEngineState* voe,
74 rtc::scoped_refptr<webrtc::AudioDecoderFactory>
75 decoder_factory) {
76 voe->voice_engine = webrtc::VoiceEngine::Create();
77 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
minyue73208662016-08-18 06:28:55 -070078 EXPECT_EQ(0, voe->base->Init(nullptr, nullptr, decoder_factory));
solenberg88499ec2016-09-07 07:34:41 -070079 webrtc::VoEBase::ChannelConfig config;
80 config.enable_voice_pacing = true;
81 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 06:28:55 -070082 EXPECT_GE(voe->send_channel_id, 0);
83 voe->receive_channel_id = voe->base->CreateChannel();
84 EXPECT_GE(voe->receive_channel_id, 0);
85}
86
87void DestroyVoiceEngine(VoiceEngineState* voe) {
88 voe->base->DeleteChannel(voe->send_channel_id);
89 voe->send_channel_id = -1;
90 voe->base->DeleteChannel(voe->receive_channel_id);
91 voe->receive_channel_id = -1;
92 voe->base->Release();
93 voe->base = nullptr;
minyue73208662016-08-18 06:28:55 -070094
95 webrtc::VoiceEngine::Delete(voe->voice_engine);
96 voe->voice_engine = nullptr;
97}
98
perkjfa10b552016-10-02 23:45:26 -070099class VideoStreamFactory
100 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
101 public:
102 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
103 : streams_(streams) {}
104
105 private:
106 std::vector<webrtc::VideoStream> CreateEncoderStreams(
107 int width,
108 int height,
109 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -0800110 // The highest layer must match the incoming resolution.
111 std::vector<webrtc::VideoStream> streams = streams_;
112 streams[streams_.size() - 1].height = height;
113 streams[streams_.size() - 1].width = width;
114 return streams;
perkjfa10b552016-10-02 23:45:26 -0700115 }
116
117 std::vector<webrtc::VideoStream> streams_;
118};
119
brandtr504b95e2016-12-21 02:54:35 -0800120bool IsFlexfec(int payload_type) {
121 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
122}
123
minyue73208662016-08-18 06:28:55 -0700124} // namespace
ivica5d6a06c2015-09-17 05:30:24 -0700125
126namespace webrtc {
127
ivica5d6a06c2015-09-17 05:30:24 -0700128class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -0700129 public Transport,
ilnik1e7732c2017-02-23 05:07:56 -0800130 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 05:30:24 -0700131 public:
sprangce4aef12015-11-02 07:23:20 -0800132 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700133 const std::string& test_label,
134 double avg_psnr_threshold,
135 double avg_ssim_threshold,
136 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800137 FILE* graph_data_output_file,
138 const std::string& graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -0800139 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 09:16:05 -0800140 uint32_t rtx_ssrc_to_analyze,
ilnik2a8c2f52017-02-15 02:23:28 -0800141 uint32_t selected_stream_width,
142 uint32_t selected_stream_height,
ilnik1e7732c2017-02-23 05:07:56 -0800143 int selected_sl,
144 int selected_tl,
ilnik9ae0d762017-02-15 00:53:12 -0800145 bool is_quick_test_enabled)
perkja49cbd32016-09-16 07:53:41 -0700146 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700147 receiver_(nullptr),
148 send_stream_(nullptr),
philipelfd870db2017-01-23 03:22:15 -0800149 receive_stream_(nullptr),
perkja49cbd32016-09-16 07:53:41 -0700150 captured_frame_forwarder_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700151 test_label_(test_label),
152 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800153 graph_title_(graph_title),
154 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 09:16:05 -0800155 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnik2a8c2f52017-02-15 02:23:28 -0800156 selected_stream_width_(selected_stream_width),
157 selected_stream_height_(selected_stream_height),
ilnik1e7732c2017-02-23 05:07:56 -0800158 selected_sl_(selected_sl),
159 selected_tl_(selected_tl),
pbos14fe7082016-04-20 06:35:56 -0700160 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100161 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700162 frames_to_process_(duration_frames),
163 frames_recorded_(0),
164 frames_processed_(0),
165 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700166 dropped_frames_before_first_encode_(0),
167 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700168 last_render_time_(0),
169 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 05:07:56 -0800170 total_media_bytes_(0),
171 first_sending_time_(0),
172 last_sending_time_(0),
ilnikdf92c5c2017-02-23 02:08:44 -0800173 cpu_time_(0),
174 wallclock_time_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700175 avg_psnr_threshold_(avg_psnr_threshold),
176 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 00:53:12 -0800177 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100178 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100179 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +0100180 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -0700181 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
182
183 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
184 // so that we don't accidentally starve "real" worker threads (codec etc).
185 // Also, don't allocate more than kMaxComparisonThreads, even if there are
186 // spare cores.
187
188 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800189 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700190 static const uint32_t kMinCoresLeft = 4;
191 static const uint32_t kMaxComparisonThreads = 8;
192
193 if (num_cores <= kMinCoresLeft) {
194 num_cores = 1;
195 } else {
196 num_cores -= kMinCoresLeft;
197 num_cores = std::min(num_cores, kMaxComparisonThreads);
198 }
199
200 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100201 rtc::PlatformThread* thread =
202 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
203 thread->Start();
204 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700205 }
ivica5d6a06c2015-09-17 05:30:24 -0700206 }
207
208 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100209 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
210 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700211 delete thread;
212 }
213 }
214
215 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
216
perkja49cbd32016-09-16 07:53:41 -0700217 void SetSendStream(VideoSendStream* stream) {
218 rtc::CritScope lock(&crit_);
219 RTC_DCHECK(!send_stream_);
220 send_stream_ = stream;
221 }
222
philipelfd870db2017-01-23 03:22:15 -0800223 void SetReceiveStream(VideoReceiveStream* stream) {
224 rtc::CritScope lock(&crit_);
225 RTC_DCHECK(!receive_stream_);
226 receive_stream_ = stream;
227 }
228
perkja49cbd32016-09-16 07:53:41 -0700229 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
230 return &captured_frame_forwarder_;
231 }
232 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
233 return &captured_frame_forwarder_;
234 }
235
ivica5d6a06c2015-09-17 05:30:24 -0700236 DeliveryStatus DeliverPacket(MediaType media_type,
237 const uint8_t* packet,
238 size_t length,
239 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700240 // Ignore timestamps of RTCP packets. They're not synchronized with
241 // RTP packet timestamps and so they would confuse wrap_handler_.
242 if (RtpHeaderParser::IsRtcp(packet, length)) {
243 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
244 }
sprangce4aef12015-11-02 07:23:20 -0800245 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700246 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800247 parser.Parse(&header);
ilnik46a00212017-02-10 09:16:05 -0800248 if (!IsFlexfec(header.payloadType) &&
249 (header.ssrc == ssrc_to_analyze_ ||
250 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 02:54:35 -0800251 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
252 // (FlexFEC and media are sent on different SSRCs, which have different
253 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800254 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 05:30:24 -0700255 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800256 int64_t timestamp =
257 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
258 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700259 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
260 }
261
262 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
263 }
264
Peter Boströme4499152016-02-05 11:13:28 +0100265 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700266 rtc::CritScope crit(&comparison_lock_);
267 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
268 }
269
pbos14fe7082016-04-20 06:35:56 -0700270 void PreEncodeOnFrame(const VideoFrame& video_frame) {
271 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800272 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 06:35:56 -0700273 while (frames_.front().timestamp() != video_frame.timestamp()) {
274 ++dropped_frames_before_first_encode_;
275 frames_.pop_front();
276 RTC_CHECK(!frames_.empty());
277 }
ilnik3dd5ad92017-02-09 04:58:53 -0800278 first_encoded_timestamp_ =
279 rtc::Optional<uint32_t>(video_frame.timestamp());
280 }
281 }
282
283 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
284 rtc::CritScope lock(&crit_);
285 if (!first_sent_timestamp_ &&
ilnik2a8c2f52017-02-15 02:23:28 -0800286 encoded_frame.encoded_width_ == selected_stream_width_ &&
287 encoded_frame.encoded_height_ == selected_stream_height_) {
ilnik3dd5ad92017-02-09 04:58:53 -0800288 first_sent_timestamp_ = rtc::Optional<uint32_t>(encoded_frame.timestamp_);
pbos14fe7082016-04-20 06:35:56 -0700289 }
290 }
291
stefan1d8a5062015-10-02 03:39:33 -0700292 bool SendRtp(const uint8_t* packet,
293 size_t length,
294 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800295 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700296 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800297 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700298
sprangce4aef12015-11-02 07:23:20 -0800299 int64_t current_time =
300 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 02:23:28 -0800301
sprangce4aef12015-11-02 07:23:20 -0800302 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700303 {
304 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800305 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 08:32:53 -0800306 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 04:58:53 -0800307 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 04:08:56 -0800308 }
ilnik3dd5ad92017-02-09 04:58:53 -0800309
310 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 02:54:35 -0800311 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
312 // (FlexFEC and media are sent on different SSRCs, which have different
313 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800314 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 02:54:35 -0800315 int64_t timestamp =
316 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
317 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 05:07:56 -0800318
319 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 02:54:35 -0800320 encoded_frame_sizes_[timestamp] +=
321 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 05:07:56 -0800322 total_media_bytes_ +=
323 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 02:54:35 -0800324 }
ilnik1e7732c2017-02-23 05:07:56 -0800325 if (first_sending_time_ == 0)
326 first_sending_time_ = current_time;
327 last_sending_time_ = current_time;
sprangce4aef12015-11-02 07:23:20 -0800328 }
ivica5d6a06c2015-09-17 05:30:24 -0700329 }
sprangce4aef12015-11-02 07:23:20 -0800330 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700331 }
332
333 bool SendRtcp(const uint8_t* packet, size_t length) override {
334 return transport_->SendRtcp(packet, length);
335 }
336
nisseeb83a1a2016-03-21 01:27:56 -0700337 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700338 int64_t render_time_ms =
339 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700340
341 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 02:08:44 -0800342
343 StartExcludingCpuThreadTime();
344
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700345 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800346 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700347
sprang16daaa52016-03-09 01:30:24 -0800348 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700349 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700350 // No previous frame rendered, this one was dropped after sending but
351 // before rendering.
352 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 07:02:22 -0800353 } else {
354 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
355 render_time_ms);
pbos14fe7082016-04-20 06:35:56 -0700356 }
ivica5d6a06c2015-09-17 05:30:24 -0700357 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700358 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700359 }
360
361 VideoFrame reference_frame = frames_.front();
362 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800363 int64_t reference_timestamp =
364 wrap_handler_.Unwrap(reference_frame.timestamp());
365 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800366 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100367 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800368 ++send_timestamp;
369 }
sprang16daaa52016-03-09 01:30:24 -0800370 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700371
372 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
373
nisse97f0b932016-05-26 09:44:40 -0700374 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ilnikdf92c5c2017-02-23 02:08:44 -0800375
376 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 05:30:24 -0700377 }
378
ivica5d6a06c2015-09-17 05:30:24 -0700379 void Wait() {
380 // Frame comparisons can be very expensive. Wait for test to be done, but
381 // at time-out check if frames_processed is going up. If so, give it more
382 // time, otherwise fail. Hopefully this will reduce test flakiness.
383
Peter Boström8c38e8b2015-11-26 17:45:47 +0100384 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800385
ivica5d6a06c2015-09-17 05:30:24 -0700386 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700387 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100388 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700389 int frames_processed;
390 {
391 rtc::CritScope crit(&comparison_lock_);
392 frames_processed = frames_processed_;
393 }
394
395 // Print some output so test infrastructure won't think we've crashed.
396 const char* kKeepAliveMessages[3] = {
397 "Uh, I'm-I'm not quite dead, sir.",
398 "Uh, I-I think uh, I could pull through, sir.",
399 "Actually, I think I'm all right to come with you--"};
400 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
401
402 if (last_frames_processed == -1) {
403 last_frames_processed = frames_processed;
404 continue;
405 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100406 if (frames_processed == last_frames_processed) {
407 EXPECT_GT(frames_processed, last_frames_processed)
408 << "Analyzer stalled while waiting for test to finish.";
409 done_.Set();
410 break;
411 }
ivica5d6a06c2015-09-17 05:30:24 -0700412 last_frames_processed = frames_processed;
413 }
414
415 if (iteration > 0)
416 printf("- Farewell, sweet Concorde!\n");
417
Peter Boström8c38e8b2015-11-26 17:45:47 +0100418 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700419 }
420
pbos14fe7082016-04-20 06:35:56 -0700421 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
422 return &pre_encode_proxy_;
423 }
Peter Boströme4499152016-02-05 11:13:28 +0100424 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
425
ilnikdf92c5c2017-02-23 02:08:44 -0800426 void StartMeasuringCpuProcessTime() {
427 rtc::CritScope lock(&cpu_measurement_lock_);
428 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
429 wallclock_time_ -= rtc::SystemTimeNanos();
430 }
431
432 void StopMeasuringCpuProcessTime() {
433 rtc::CritScope lock(&cpu_measurement_lock_);
434 cpu_time_ += rtc::GetProcessCpuTimeNanos();
435 wallclock_time_ += rtc::SystemTimeNanos();
436 }
437
438 void StartExcludingCpuThreadTime() {
439 rtc::CritScope lock(&cpu_measurement_lock_);
440 cpu_time_ += rtc::GetThreadCpuTimeNanos();
441 }
442
443 void StopExcludingCpuThreadTime() {
444 rtc::CritScope lock(&cpu_measurement_lock_);
445 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
446 }
447
448 double GetCpuUsagePercent() {
449 rtc::CritScope lock(&cpu_measurement_lock_);
450 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
451 }
452
sprangce4aef12015-11-02 07:23:20 -0800453 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700454 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700455
456 private:
457 struct FrameComparison {
458 FrameComparison()
459 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800460 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700461 send_time_ms(0),
462 recv_time_ms(0),
463 render_time_ms(0),
464 encoded_frame_size(0) {}
465
466 FrameComparison(const VideoFrame& reference,
467 const VideoFrame& render,
468 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800469 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700470 int64_t send_time_ms,
471 int64_t recv_time_ms,
472 int64_t render_time_ms,
473 size_t encoded_frame_size)
474 : reference(reference),
475 render(render),
476 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800477 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700478 send_time_ms(send_time_ms),
479 recv_time_ms(recv_time_ms),
480 render_time_ms(render_time_ms),
481 encoded_frame_size(encoded_frame_size) {}
482
nissedf2ceb82016-12-15 06:29:53 -0800483 FrameComparison(bool dropped,
484 int64_t input_time_ms,
485 int64_t send_time_ms,
486 int64_t recv_time_ms,
487 int64_t render_time_ms,
488 size_t encoded_frame_size)
489 : dropped(dropped),
490 input_time_ms(input_time_ms),
491 send_time_ms(send_time_ms),
492 recv_time_ms(recv_time_ms),
493 render_time_ms(render_time_ms),
494 encoded_frame_size(encoded_frame_size) {}
495
496 rtc::Optional<VideoFrame> reference;
497 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700498 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800499 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700500 int64_t send_time_ms;
501 int64_t recv_time_ms;
502 int64_t render_time_ms;
503 size_t encoded_frame_size;
504 };
505
506 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700507 Sample(int dropped,
508 int64_t input_time_ms,
509 int64_t send_time_ms,
510 int64_t recv_time_ms,
511 int64_t render_time_ms,
512 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700513 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700514 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700515 : dropped(dropped),
516 input_time_ms(input_time_ms),
517 send_time_ms(send_time_ms),
518 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700519 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700520 encoded_frame_size(encoded_frame_size),
521 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700522 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700523
ivica8d15bd62015-10-07 02:43:12 -0700524 int dropped;
525 int64_t input_time_ms;
526 int64_t send_time_ms;
527 int64_t recv_time_ms;
528 int64_t render_time_ms;
529 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700530 double psnr;
531 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700532 };
533
Peter Boströme4499152016-02-05 11:13:28 +0100534 // This class receives the send-side OnEncodeTiming and is provided to not
535 // conflict with the receiver-side pre_decode_callback.
536 class OnEncodeTimingProxy : public EncodedFrameObserver {
537 public:
538 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
539
540 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
541 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
542 }
ilnik3dd5ad92017-02-09 04:58:53 -0800543 void EncodedFrameCallback(const EncodedFrame& frame) override {
544 parent_->PostEncodeFrameCallback(frame);
545 }
Peter Boströme4499152016-02-05 11:13:28 +0100546
547 private:
548 VideoAnalyzer* const parent_;
549 };
550
pbos14fe7082016-04-20 06:35:56 -0700551 // This class receives the send-side OnFrame callback and is provided to not
552 // conflict with the receiver-side renderer callback.
553 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
554 public:
555 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
556
557 void OnFrame(const VideoFrame& video_frame) override {
558 parent_->PreEncodeOnFrame(video_frame);
559 }
560
561 private:
562 VideoAnalyzer* const parent_;
563 };
564
ilnik1e7732c2017-02-23 05:07:56 -0800565 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
566 size_t length,
567 const RTPHeader& header) {
568 if (header.payloadType != kPayloadTypeVP9 &&
569 header.payloadType != kPayloadTypeVP8) {
570 return true;
571 } else {
572 // Get VP8 and VP9 specific header to check layers indexes.
573 const uint8_t* payload = packet + header.headerLength;
574 const size_t payload_length = length - header.headerLength;
575 const size_t payload_data_length = payload_length - header.paddingLength;
576 const bool is_vp8 = header.payloadType == kPayloadTypeVP8;
577 std::unique_ptr<RtpDepacketizer> depacketizer(
578 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
579 RtpDepacketizer::ParsedPayload parsed_payload;
580 bool result =
581 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
582 RTC_DCHECK(result);
583 const int temporal_idx = static_cast<int>(
584 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
585 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
586 const int spatial_idx = static_cast<int>(
587 is_vp8 ? kNoSpatialIdx
588 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
589 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
590 temporal_idx <= selected_tl_) &&
591 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
592 spatial_idx <= selected_sl_);
593 }
594 }
595
ivica5d6a06c2015-09-17 05:30:24 -0700596 void AddFrameComparison(const VideoFrame& reference,
597 const VideoFrame& render,
598 bool dropped,
599 int64_t render_time_ms)
600 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800601 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
602 int64_t send_time_ms = send_times_[reference_timestamp];
603 send_times_.erase(reference_timestamp);
604 int64_t recv_time_ms = recv_times_[reference_timestamp];
605 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700606
sprangce4aef12015-11-02 07:23:20 -0800607 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800608 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800609 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800610 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800611 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
612 if (it != encoded_frame_sizes_.end())
613 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700614
ivica5d6a06c2015-09-17 05:30:24 -0700615 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700616 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800617 comparisons_.push_back(FrameComparison(reference, render, dropped,
618 reference.ntp_time_ms(),
619 send_time_ms, recv_time_ms,
620 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700621 } else {
nissedf2ceb82016-12-15 06:29:53 -0800622 comparisons_.push_back(FrameComparison(dropped,
623 reference.ntp_time_ms(),
624 send_time_ms, recv_time_ms,
625 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700626 }
Peter Boström5811a392015-12-10 13:02:50 +0100627 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700628 }
629
tommi0f8b4032017-02-22 11:22:05 -0800630 static void PollStatsThread(void* obj) {
631 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 05:30:24 -0700632 }
633
tommi0f8b4032017-02-22 11:22:05 -0800634 void PollStats() {
635 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
636 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700637
tommi0f8b4032017-02-22 11:22:05 -0800638 VideoSendStream::Stats send_stats = send_stream_->GetStats();
639 // It's not certain that we yet have estimates for any of these stats.
640 // Check that they are positive before mixing them in.
641 if (send_stats.encode_frame_rate > 0)
642 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
643 if (send_stats.avg_encode_time_ms > 0)
644 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
645 if (send_stats.encode_usage_percent > 0)
646 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
647 if (send_stats.media_bitrate_bps > 0)
648 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
philipelfd870db2017-01-23 03:22:15 -0800649
tommi0f8b4032017-02-22 11:22:05 -0800650 if (receive_stream_ != nullptr) {
651 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
652 if (receive_stats.decode_ms > 0)
653 decode_time_ms_.AddSample(receive_stats.decode_ms);
654 if (receive_stats.max_decode_ms > 0)
655 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
656 }
philipelfd870db2017-01-23 03:22:15 -0800657 }
ivica5d6a06c2015-09-17 05:30:24 -0700658 }
659
660 static bool FrameComparisonThread(void* obj) {
661 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
662 }
663
664 bool CompareFrames() {
665 if (AllFramesRecorded())
666 return false;
667
ivica5d6a06c2015-09-17 05:30:24 -0700668 FrameComparison comparison;
669
670 if (!PopComparison(&comparison)) {
671 // Wait until new comparison task is available, or test is done.
672 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100673 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700674 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100675 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700676 return false;
677 }
678 return true; // Try again.
679 }
680
ilnikdf92c5c2017-02-23 02:08:44 -0800681 StartExcludingCpuThreadTime();
682
ivica5d6a06c2015-09-17 05:30:24 -0700683 PerformFrameComparison(comparison);
684
ilnikdf92c5c2017-02-23 02:08:44 -0800685 StopExcludingCpuThreadTime();
686
ivica5d6a06c2015-09-17 05:30:24 -0700687 if (FrameProcessed()) {
688 PrintResults();
689 if (graph_data_output_file_)
690 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100691 done_.Set();
692 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700693 return false;
694 }
695
696 return true;
697 }
698
699 bool PopComparison(FrameComparison* comparison) {
700 rtc::CritScope crit(&comparison_lock_);
701 // If AllFramesRecorded() is true, it means we have already popped
702 // frames_to_process_ frames from comparisons_, so there is no more work
703 // for this thread to be done. frames_processed_ might still be lower if
704 // all comparisons are not done, but those frames are currently being
705 // worked on by other threads.
706 if (comparisons_.empty() || AllFramesRecorded())
707 return false;
708
709 *comparison = comparisons_.front();
710 comparisons_.pop_front();
711
712 FrameRecorded();
713 return true;
714 }
715
716 // Increment counter for number of frames received for comparison.
717 void FrameRecorded() {
718 rtc::CritScope crit(&comparison_lock_);
719 ++frames_recorded_;
720 }
721
722 // Returns true if all frames to be compared have been taken from the queue.
723 bool AllFramesRecorded() {
724 rtc::CritScope crit(&comparison_lock_);
725 assert(frames_recorded_ <= frames_to_process_);
726 return frames_recorded_ == frames_to_process_;
727 }
728
729 // Increase count of number of frames processed. Returns true if this was the
730 // last frame to be processed.
731 bool FrameProcessed() {
732 rtc::CritScope crit(&comparison_lock_);
733 ++frames_processed_;
734 assert(frames_processed_ <= frames_to_process_);
735 return frames_processed_ == frames_to_process_;
736 }
737
738 void PrintResults() {
ilnikdf92c5c2017-02-23 02:08:44 -0800739 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -0700740 rtc::CritScope crit(&comparison_lock_);
741 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800742 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700743 PrintResult("sender_time", sender_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700744 PrintResult("receiver_time", receiver_time_, " ms");
745 PrintResult("total_delay_incl_network", end_to_end_, " ms");
746 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
747 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
748 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800749 PrintResult("encode_time", encode_time_ms_, " ms");
750 PrintResult("encode_usage_percent", encode_usage_percent_, " percent");
751 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
752
ilnik1e7732c2017-02-23 05:07:56 -0800753 printf("RESULT actual_bitrate: %s = %.6lf bps\n", test_label_.c_str(),
754 GetAverageMediaBitrateBps());
755
philipelfd870db2017-01-23 03:22:15 -0800756 if (receive_stream_ != nullptr) {
757 PrintResult("decode_time", decode_time_ms_, " ms");
758 PrintResult("decode_time_max", decode_time_max_ms_, " ms");
759 }
ivica5d6a06c2015-09-17 05:30:24 -0700760
pbos14fe7082016-04-20 06:35:56 -0700761 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
762 dropped_frames_);
763 printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n",
764 test_label_.c_str(), dropped_frames_before_first_encode_);
765 printf("RESULT dropped_frames_before_rendering: %s = %d frames\n",
766 test_label_.c_str(), dropped_frames_before_rendering_);
ilnikdf92c5c2017-02-23 02:08:44 -0800767 printf("RESULT cpu_usage: %s = %lf %%\n", test_label_.c_str(),
768 GetCpuUsagePercent());
ilnik9ae0d762017-02-15 00:53:12 -0800769 // Disable quality check for quick test, as quality checks may fail
770 // because too few samples were collected.
771 if (!is_quick_test_enabled_) {
772 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
773 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
774 }
ivica5d6a06c2015-09-17 05:30:24 -0700775 }
776
777 void PerformFrameComparison(const FrameComparison& comparison) {
778 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700779 double psnr = -1.0;
780 double ssim = -1.0;
nissedf2ceb82016-12-15 06:29:53 -0800781 if (comparison.reference) {
782 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
783 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700784 }
ivica5d6a06c2015-09-17 05:30:24 -0700785
ivica5d6a06c2015-09-17 05:30:24 -0700786 rtc::CritScope crit(&comparison_lock_);
787 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800788 samples_.push_back(Sample(
789 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
790 comparison.recv_time_ms, comparison.render_time_ms,
791 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700792 }
stefanb1797672016-08-11 07:00:57 -0700793 if (psnr >= 0.0)
794 psnr_.AddSample(psnr);
795 if (ssim >= 0.0)
796 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700797
798 if (comparison.dropped) {
799 ++dropped_frames_;
800 return;
801 }
802 if (last_render_time_ != 0)
803 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
804 last_render_time_ = comparison.render_time_ms;
805
nissedf2ceb82016-12-15 06:29:53 -0800806 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800807 if (comparison.recv_time_ms > 0) {
808 // If recv_time_ms == 0, this frame consisted of a packets which were all
809 // lost in the transport. Since we were able to render the frame, however,
810 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
811 // happens internally in Call, and we can therefore here not know which
812 // FEC packets that protected the lost media packets. Consequently, we
813 // were not able to record a meaningful recv_time_ms. We therefore skip
814 // this sample.
815 //
816 // The reasoning above does not hold for ULPFEC and RTX, as for those
817 // strategies the timestamp of the received packets is set to the
818 // timestamp of the protected/retransmitted media packet. I.e., then
819 // recv_time_ms != 0, even though the media packets were lost.
820 receiver_time_.AddSample(comparison.render_time_ms -
821 comparison.recv_time_ms);
822 }
nissedf2ceb82016-12-15 06:29:53 -0800823 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700824 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
825 }
826
827 void PrintResult(const char* result_type,
828 test::Statistics stats,
829 const char* unit) {
830 printf("RESULT %s: %s = {%f, %f}%s\n",
831 result_type,
832 test_label_.c_str(),
833 stats.Mean(),
834 stats.StandardDeviation(),
835 unit);
836 }
837
838 void PrintSamplesToFile(void) {
839 FILE* out = graph_data_output_file_;
840 rtc::CritScope crit(&comparison_lock_);
841 std::sort(samples_.begin(), samples_.end(),
842 [](const Sample& A, const Sample& B) -> bool {
843 return A.input_time_ms < B.input_time_ms;
844 });
845
sprangce4aef12015-11-02 07:23:20 -0800846 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700847 fprintf(out, "%" PRIuS "\n", samples_.size());
848 fprintf(out,
849 "dropped "
850 "input_time_ms "
851 "send_time_ms "
852 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700853 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700854 "encoded_frame_size "
855 "psnr "
856 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700857 "encode_time_ms\n");
858 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700859 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700860 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
861 int encode_time_ms;
862 if (it != samples_encode_time_ms_.end()) {
863 encode_time_ms = it->second;
864 } else {
865 ++missing_encode_time_samples;
866 encode_time_ms = -1;
867 }
868 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
869 " %lf %lf %d\n",
870 sample.dropped, sample.input_time_ms, sample.send_time_ms,
871 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700872 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700873 encode_time_ms);
874 }
875 if (missing_encode_time_samples) {
876 fprintf(stderr,
877 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
878 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700879 }
880 }
881
ilnik1e7732c2017-02-23 05:07:56 -0800882 double GetAverageMediaBitrateBps() {
883 if (last_sending_time_ == first_sending_time_) {
884 return 0;
885 } else {
886 return static_cast<double>(total_media_bytes_) * 8 /
887 (last_sending_time_ - first_sending_time_) *
888 rtc::kNumMillisecsPerSec;
889 }
890 }
891
perkja49cbd32016-09-16 07:53:41 -0700892 // Implements VideoSinkInterface to receive captured frames from a
893 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
894 // as a source to VideoSendStream.
895 // It forwards all input frames to the VideoAnalyzer for later comparison and
896 // forwards the captured frames to the VideoSendStream.
897 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
898 public rtc::VideoSourceInterface<VideoFrame> {
899 public:
900 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer)
901 : analyzer_(analyzer), send_stream_input_(nullptr) {}
902
903 private:
904 void OnFrame(const VideoFrame& video_frame) override {
905 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -0800906 // Frames from the capturer does not have a rtp timestamp.
907 // Create one so it can be used for comparison.
908 RTC_DCHECK_EQ(0, video_frame.timestamp());
perkja49cbd32016-09-16 07:53:41 -0700909 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -0800910 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -0700911 rtc::CritScope lock(&crit_);
912 if (send_stream_input_)
913 send_stream_input_->OnFrame(video_frame);
914 }
915
916 // Called when |send_stream_.SetSource()| is called.
917 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
918 const rtc::VideoSinkWants& wants) override {
919 rtc::CritScope lock(&crit_);
920 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
921 send_stream_input_ = sink;
922 }
923
924 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
925 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
926 rtc::CritScope lock(&crit_);
927 RTC_DCHECK(sink == send_stream_input_);
928 send_stream_input_ = nullptr;
929 }
930
931 VideoAnalyzer* const analyzer_;
932 rtc::CriticalSection crit_;
933 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
934 };
935
936 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
937 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800938 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -0700939 }
940
941 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -0800942 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -0700943 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700944 const std::string test_label_;
945 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800946 const std::string graph_title_;
947 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -0800948 const uint32_t rtx_ssrc_to_analyze_;
ilnik2a8c2f52017-02-15 02:23:28 -0800949 const uint32_t selected_stream_width_;
950 const uint32_t selected_stream_height_;
ilnik1e7732c2017-02-23 05:07:56 -0800951 const int selected_sl_;
952 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -0700953 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100954 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700955 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700956 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700957 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
958 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
959 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
960 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
961 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
962 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
963 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
964 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
philipelfd870db2017-01-23 03:22:15 -0800965 test::Statistics encode_time_ms_ GUARDED_BY(comparison_lock_);
966 test::Statistics encode_usage_percent_ GUARDED_BY(comparison_lock_);
967 test::Statistics decode_time_ms_ GUARDED_BY(comparison_lock_);
968 test::Statistics decode_time_max_ms_ GUARDED_BY(comparison_lock_);
969 test::Statistics media_bitrate_bps_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700970
971 const int frames_to_process_;
972 int frames_recorded_;
973 int frames_processed_;
974 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -0700975 int dropped_frames_before_first_encode_;
976 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -0700977 int64_t last_render_time_;
978 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -0800979 int64_t total_media_bytes_;
980 int64_t first_sending_time_;
981 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -0700982
ilnikdf92c5c2017-02-23 02:08:44 -0800983 int64_t cpu_time_ GUARDED_BY(cpu_measurement_lock_);
984 int64_t wallclock_time_ GUARDED_BY(cpu_measurement_lock_);
985 rtc::CriticalSection cpu_measurement_lock_;
986
ivica5d6a06c2015-09-17 05:30:24 -0700987 rtc::CriticalSection crit_;
988 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -0700989 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800990 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
991 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
992 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
993 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800994 rtc::Optional<uint32_t> first_encoded_timestamp_ GUARDED_BY(crit_);
995 rtc::Optional<uint32_t> first_sent_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700996 const double avg_psnr_threshold_;
997 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -0800998 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -0700999
1000 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001001 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1002 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001003 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -07001004 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001005 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -07001006};
1007
palmkviste75f2042016-09-28 06:19:48 -07001008VideoQualityTest::VideoQualityTest()
1009 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {}
ivica5d6a06c2015-09-17 05:30:24 -07001010
minyue626bc952016-10-31 05:47:02 -07001011VideoQualityTest::Params::Params()
1012 : call({false, Call::Config::BitrateConfig()}),
1013 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
brandtr1293aca2016-11-16 22:47:29 -08001014 false, "", ""}),
minyue626bc952016-10-31 05:47:02 -07001015 audio({false, false}),
1016 screenshare({false, 10, 0}),
1017 analyzer({"", 0.0, 0.0, 0, "", ""}),
1018 pipe(),
1019 logs(false),
1020 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}) {}
1021
1022VideoQualityTest::Params::~Params() = default;
1023
ivica5d6a06c2015-09-17 05:30:24 -07001024void VideoQualityTest::TestBody() {}
1025
sprangce4aef12015-11-02 07:23:20 -08001026std::string VideoQualityTest::GenerateGraphTitle() const {
1027 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -07001028 ss << params_.video.codec;
1029 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
1030 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -08001031 if (params_.screenshare.scroll_duration)
1032 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
1033 if (params_.ss.streams.size() > 1)
1034 ss << ", Stream #" << params_.ss.selected_stream;
1035 if (params_.ss.num_spatial_layers > 1)
1036 ss << ", Layer #" << params_.ss.selected_sl;
1037 ss << ")";
1038 return ss.str();
1039}
1040
1041void VideoQualityTest::CheckParams() {
stefan7de8d642017-02-07 07:14:08 -08001042 if (!params_.video.enabled)
1043 return;
sprangce4aef12015-11-02 07:23:20 -08001044 // Add a default stream in none specified.
1045 if (params_.ss.streams.empty())
1046 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
1047 if (params_.ss.num_spatial_layers == 0)
1048 params_.ss.num_spatial_layers = 1;
1049
1050 if (params_.pipe.loss_percent != 0 ||
1051 params_.pipe.queue_length_packets != 0) {
1052 // Since LayerFilteringTransport changes the sequence numbers, we can't
1053 // use that feature with pack loss, since the NACK request would end up
1054 // retransmitting the wrong packets.
1055 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -08001056 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -07001057 RTC_CHECK(params_.video.selected_tl == -1 ||
1058 params_.video.selected_tl ==
1059 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001060 }
1061
1062 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
1063 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -07001064 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
1065 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
1066 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprangce4aef12015-11-02 07:23:20 -08001067 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
1068 for (const VideoStream& stream : params_.ss.streams) {
1069 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1070 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1071 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
kwiberg352444f2016-11-28 15:58:53 -08001072 RTC_CHECK_EQ(stream.temporal_layer_thresholds_bps.size(),
minyue626bc952016-10-31 05:47:02 -07001073 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001074 }
1075 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1076 // the total bitrate? We anyway have to update them in the case bitrate
1077 // estimator changes the total bitrates.
1078 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
1079 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
1080 RTC_CHECK(params_.ss.spatial_layers.empty() ||
1081 params_.ss.spatial_layers.size() ==
1082 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -07001083 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -08001084 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -07001085 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -08001086 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -08001087 }
1088}
1089
1090// Static.
1091std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1092 // Parse comma separated nonnegative integers, where some elements may be
1093 // empty. The empty values are replaced with -1.
1094 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1095 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1096 std::vector<int> result;
1097 if (str.empty())
1098 return result;
1099
1100 const char* p = str.c_str();
1101 int value = -1;
1102 int pos;
1103 while (*p) {
1104 if (*p == ',') {
1105 result.push_back(value);
1106 value = -1;
1107 ++p;
1108 continue;
1109 }
1110 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1111 << "Unexpected non-number value.";
1112 p += pos;
1113 }
1114 result.push_back(value);
1115 return result;
1116}
1117
1118// Static.
1119VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
1120 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -07001121 stream.width = params.video.width;
1122 stream.height = params.video.height;
1123 stream.max_framerate = params.video.fps;
1124 stream.min_bitrate_bps = params.video.min_bitrate_bps;
1125 stream.target_bitrate_bps = params.video.target_bitrate_bps;
1126 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprangce4aef12015-11-02 07:23:20 -08001127 stream.max_qp = 52;
minyue626bc952016-10-31 05:47:02 -07001128 if (params.video.num_temporal_layers == 2)
sprangce4aef12015-11-02 07:23:20 -08001129 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
1130 return stream;
1131}
1132
1133// Static.
1134void VideoQualityTest::FillScalabilitySettings(
1135 Params* params,
1136 const std::vector<std::string>& stream_descriptors,
1137 size_t selected_stream,
1138 int num_spatial_layers,
1139 int selected_sl,
1140 const std::vector<std::string>& sl_descriptors) {
1141 // Read VideoStream and SpatialLayer elements from a list of comma separated
1142 // lists. To use a default value for an element, use -1 or leave empty.
1143 // Validity checks performed in CheckParams.
1144
1145 RTC_CHECK(params->ss.streams.empty());
1146 for (auto descriptor : stream_descriptors) {
1147 if (descriptor.empty())
1148 continue;
1149 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
1150 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1151 if (v[0] != -1)
1152 stream.width = static_cast<size_t>(v[0]);
1153 if (v[1] != -1)
1154 stream.height = static_cast<size_t>(v[1]);
1155 if (v[2] != -1)
1156 stream.max_framerate = v[2];
1157 if (v[3] != -1)
1158 stream.min_bitrate_bps = v[3];
1159 if (v[4] != -1)
1160 stream.target_bitrate_bps = v[4];
1161 if (v[5] != -1)
1162 stream.max_bitrate_bps = v[5];
1163 if (v.size() > 6 && v[6] != -1)
1164 stream.max_qp = v[6];
1165 if (v.size() > 7) {
1166 stream.temporal_layer_thresholds_bps.clear();
1167 stream.temporal_layer_thresholds_bps.insert(
1168 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1169 } else {
1170 // Automatic TL thresholds for more than two layers not supported.
minyue626bc952016-10-31 05:47:02 -07001171 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
sprangce4aef12015-11-02 07:23:20 -08001172 }
1173 params->ss.streams.push_back(stream);
1174 }
1175 params->ss.selected_stream = selected_stream;
1176
1177 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
1178 params->ss.selected_sl = selected_sl;
1179 RTC_CHECK(params->ss.spatial_layers.empty());
1180 for (auto descriptor : sl_descriptors) {
1181 if (descriptor.empty())
1182 continue;
1183 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1184 RTC_CHECK_GT(v[2], 0);
1185
1186 SpatialLayer layer;
1187 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1188 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1189 layer.target_bitrate_bps = v[2];
1190 params->ss.spatial_layers.push_back(layer);
1191 }
1192}
1193
minyuea27172d2016-11-01 05:59:29 -07001194void VideoQualityTest::SetupVideo(Transport* send_transport,
1195 Transport* recv_transport) {
sprangce4aef12015-11-02 07:23:20 -08001196 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -07001197 trace_to_stderr_.reset(new test::TraceToStderr);
1198
brandtr8313a6f2017-01-13 07:41:19 -08001199 size_t num_video_streams = params_.ss.streams.size();
1200 size_t num_flexfec_streams = params_.video.flexfec ? 1 : 0;
1201 CreateSendConfig(num_video_streams, 0, num_flexfec_streams, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001202
1203 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001204 if (params_.video.codec == "H264") {
magjedceecea42016-11-28 07:20:21 -08001205 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001206 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001207 } else if (params_.video.codec == "VP8") {
magjed509e4fe2016-11-18 01:34:11 -08001208 video_encoder_.reset(VP8Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001209 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001210 } else if (params_.video.codec == "VP9") {
magjed509e4fe2016-11-18 01:34:11 -08001211 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001212 payload_type = kPayloadTypeVP9;
1213 } else {
1214 RTC_NOTREACHED() << "Codec not supported!";
1215 return;
1216 }
minyuea27172d2016-11-01 05:59:29 -07001217 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001218 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001219 video_send_config_.encoder_settings.payload_type = payload_type;
1220 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1221 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
brandtr8313a6f2017-01-13 07:41:19 -08001222 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001223 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001224
stefanff483612015-12-21 03:14:00 -08001225 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001226 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001227 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001228 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001229 test::kTransportSequenceNumberExtensionId));
1230 } else {
stefanff483612015-12-21 03:14:00 -08001231 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001232 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik SprĂ¥ng6b8d3552015-09-24 15:06:57 +02001233 }
1234
stefanff483612015-12-21 03:14:00 -08001235 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001236 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001237
brandtr1293aca2016-11-16 22:47:29 -08001238 video_send_config_.suspend_below_min_bitrate =
1239 params_.video.suspend_below_min_bitrate;
1240
perkjfa10b552016-10-02 23:45:26 -07001241 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1242 video_encoder_config_.max_bitrate_bps = 0;
1243 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1244 video_encoder_config_.max_bitrate_bps +=
1245 params_.ss.streams[i].max_bitrate_bps;
1246 }
1247 video_encoder_config_.video_stream_factory =
1248 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1249
stefanff483612015-12-21 03:14:00 -08001250 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001251
1252 CreateMatchingReceiveConfigs(recv_transport);
1253
brandtr8313a6f2017-01-13 07:41:19 -08001254 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001255 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
brandtr14742122017-01-27 04:53:07 -08001256 video_receive_configs_[i].rtp.rtx_ssrc = kSendRtxSsrcs[i];
1257 video_receive_configs_[i].rtp.rtx_payload_types[payload_type] =
sprangce4aef12015-11-02 07:23:20 -08001258 kSendRtxPayloadType;
minyue626bc952016-10-31 05:47:02 -07001259 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
Stefan Holmer85d5ac72017-02-09 16:25:16 +01001260 video_receive_configs_[i].rtp.remb = !params_.call.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -08001261 }
brandtr1293aca2016-11-16 22:47:29 -08001262
1263 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 07:41:19 -08001264 // Override send config constructed by CreateSendConfig.
brandtr1293aca2016-11-16 22:47:29 -08001265 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1266 kVideoSendSsrcs[params_.ss.selected_stream]};
1267
brandtr8313a6f2017-01-13 07:41:19 -08001268 // The matching receive config is _not_ created by
1269 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1270 // Set up the receive config manually instead.
1271 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001272 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 06:59:19 -08001273 video_send_config_.rtp.flexfec.payload_type;
1274 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001275 flexfec_receive_config.protected_media_ssrcs =
1276 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001277 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001278 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1279 if (params_.call.send_side_bwe) {
1280 flexfec_receive_config.rtp_header_extensions.push_back(
1281 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1282 test::kTransportSequenceNumberExtensionId));
1283 } else {
1284 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1285 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1286 }
brandtr1293aca2016-11-16 22:47:29 -08001287 flexfec_receive_configs_.push_back(flexfec_receive_config);
1288 }
1289
1290 if (params_.video.ulpfec) {
1291 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1292 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1293 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1294
1295 video_receive_configs_[params_.ss.selected_stream]
1296 .rtp.ulpfec.red_payload_type =
1297 video_send_config_.rtp.ulpfec.red_payload_type;
1298 video_receive_configs_[params_.ss.selected_stream]
1299 .rtp.ulpfec.ulpfec_payload_type =
1300 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1301 video_receive_configs_[params_.ss.selected_stream]
1302 .rtp.ulpfec.red_rtx_payload_type =
1303 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1304 }
ivica5d6a06c2015-09-17 05:30:24 -07001305}
1306
ilnik2a8c2f52017-02-15 02:23:28 -08001307void VideoQualityTest::SetupScreenshareOrSVC() {
1308 if (params_.screenshare.enabled) {
1309 // Fill out codec settings.
1310 video_encoder_config_.content_type =
1311 VideoEncoderConfig::ContentType::kScreen;
1312 degradation_preference_ =
1313 VideoSendStream::DegradationPreference::kMaintainResolution;
1314 if (params_.video.codec == "VP8") {
1315 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1316 vp8_settings.denoisingOn = false;
1317 vp8_settings.frameDroppingOn = false;
1318 vp8_settings.numberOfTemporalLayers =
1319 static_cast<unsigned char>(params_.video.num_temporal_layers);
1320 video_encoder_config_.encoder_specific_settings =
1321 new rtc::RefCountedObject<
1322 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1323 } else if (params_.video.codec == "VP9") {
1324 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1325 vp9_settings.denoisingOn = false;
1326 vp9_settings.frameDroppingOn = false;
1327 vp9_settings.numberOfTemporalLayers =
1328 static_cast<unsigned char>(params_.video.num_temporal_layers);
1329 vp9_settings.numberOfSpatialLayers =
1330 static_cast<unsigned char>(params_.ss.num_spatial_layers);
1331 video_encoder_config_.encoder_specific_settings =
1332 new rtc::RefCountedObject<
1333 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1334 }
1335 // Setup frame generator.
1336 const size_t kWidth = 1850;
1337 const size_t kHeight = 1110;
1338 std::vector<std::string> slides;
1339 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1340 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1341 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1342 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
ivica5d6a06c2015-09-17 05:30:24 -07001343
ilnik2a8c2f52017-02-15 02:23:28 -08001344 if (params_.screenshare.scroll_duration == 0) {
1345 // Cycle image every slide_change_interval seconds.
perkja8ba1952017-02-27 06:52:10 -08001346 frame_generator_ = test::FrameGenerator::CreateFromYuvFile(
ilnik2a8c2f52017-02-15 02:23:28 -08001347 slides, kWidth, kHeight,
perkja8ba1952017-02-27 06:52:10 -08001348 params_.screenshare.slide_change_interval * params_.video.fps);
ilnik2a8c2f52017-02-15 02:23:28 -08001349 } else {
1350 RTC_CHECK_LE(params_.video.width, kWidth);
1351 RTC_CHECK_LE(params_.video.height, kHeight);
1352 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1353 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1354 params_.screenshare.scroll_duration) *
1355 1000;
1356 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1357 params_.screenshare.slide_change_interval);
1358
perkja8ba1952017-02-27 06:52:10 -08001359 frame_generator_ = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1360 clock_, slides, kWidth, kHeight, params_.video.width,
1361 params_.video.height, params_.screenshare.scroll_duration * 1000,
1362 kPauseDurationMs);
ilnik2a8c2f52017-02-15 02:23:28 -08001363 }
1364 } else if (params_.ss.num_spatial_layers > 1) { // For non-screenshare case.
1365 RTC_CHECK(params_.video.codec == "VP9");
kthelgason29a44e32016-09-27 03:52:02 -07001366 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
kthelgason29a44e32016-09-27 03:52:02 -07001367 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001368 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001369 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001370 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001371 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1372 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001373 }
ivica5d6a06c2015-09-17 05:30:24 -07001374}
1375
perkja49cbd32016-09-16 07:53:41 -07001376void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001377 if (params_.screenshare.enabled) {
1378 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja8ba1952017-02-27 06:52:10 -08001379 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator_),
minyue626bc952016-10-31 05:47:02 -07001380 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001381 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001382 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001383 } else {
sprangce4aef12015-11-02 07:23:20 -08001384 if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001385 video_capturer_.reset(test::VcmCapturer::Create(
minyue626bc952016-10-31 05:47:02 -07001386 params_.video.width, params_.video.height, params_.video.fps));
sprang1bed2e42017-01-23 08:46:51 -08001387 if (!video_capturer_) {
1388 // Failed to get actual camera, use chroma generator as backup.
1389 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
perkja8ba1952017-02-27 06:52:10 -08001390 static_cast<int>(params_.video.width),
1391 static_cast<int>(params_.video.height), params_.video.fps, clock_));
sprang1bed2e42017-01-23 08:46:51 -08001392 }
ivica5d6a06c2015-09-17 05:30:24 -07001393 } else {
minyuea27172d2016-11-01 05:59:29 -07001394 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001395 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001396 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001397 clock_));
minyuea27172d2016-11-01 05:59:29 -07001398 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1399 << params_.video.clip_name
1400 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001401 }
1402 }
sprang1bed2e42017-01-23 08:46:51 -08001403 RTC_DCHECK(video_capturer_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001404}
1405
sprang7a975f72015-10-12 06:33:21 -07001406void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001407 params_ = params;
1408
minyue626bc952016-10-31 05:47:02 -07001409 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001410 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1411 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001412 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001413
1414 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001415 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001416 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001417 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001418 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001419 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1420 << "!";
ivica87f83a92015-10-08 05:13:32 -07001421 }
sprang7a975f72015-10-12 06:33:21 -07001422
skvlad11a9cbf2016-10-07 11:53:05 -07001423 webrtc::RtcEventLogNullImpl event_log;
1424 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001425 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001426 CreateCalls(call_config, call_config);
1427
ivica87f83a92015-10-08 05:13:32 -07001428 test::LayerFilteringTransport send_transport(
minyue626bc952016-10-31 05:47:02 -07001429 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
1430 params_.video.selected_tl, params_.ss.selected_sl);
1431 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get());
stefanf116bd02015-10-27 08:29:42 -07001432
sprangce4aef12015-11-02 07:23:20 -08001433 std::string graph_title = params_.analyzer.graph_title;
1434 if (graph_title.empty())
1435 graph_title = VideoQualityTest::GenerateGraphTitle();
1436
1437 // In the case of different resolutions, the functions calculating PSNR and
1438 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
1439 // aborts if the average psnr/ssim are below the given threshold, which is
1440 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
1441 // abort.
1442 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
sprangce4aef12015-11-02 07:23:20 -08001443
sprangc1b57a12017-02-28 08:50:47 -08001444 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
ivica5d6a06c2015-09-17 05:30:24 -07001445 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001446 &send_transport, params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001447
1448 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001449 is_quick_test_enabled
1450 ? kFramesSentInQuickTest
1451 : params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001452 graph_data_output_file, graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -08001453 kVideoSendSsrcs[params_.ss.selected_stream],
ilnik46a00212017-02-10 09:16:05 -08001454 kSendRtxSsrcs[params_.ss.selected_stream],
ilnik3dd5ad92017-02-09 04:58:53 -08001455 static_cast<uint32_t>(selected_stream.width),
ilnik1e7732c2017-02-23 05:07:56 -08001456 static_cast<uint32_t>(selected_stream.height), params.ss.selected_sl,
1457 params_.video.selected_tl, is_quick_test_enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001458 analyzer.SetReceiver(receiver_call_->Receiver());
1459 send_transport.SetReceiver(&analyzer);
1460 recv_transport.SetReceiver(sender_call_->Receiver());
1461
minyuea27172d2016-11-01 05:59:29 -07001462 SetupVideo(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001463 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001464 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
Peter Boströme4499152016-02-05 11:13:28 +01001465 RTC_DCHECK(!video_send_config_.post_encode_callback);
1466 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001467
ilnik2a8c2f52017-02-15 02:23:28 -08001468 SetupScreenshareOrSVC();
ivica5d6a06c2015-09-17 05:30:24 -07001469
brandtr1293aca2016-11-16 22:47:29 -08001470 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001471 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001472 analyzer.SetSendStream(video_send_stream_);
philipelfd870db2017-01-23 03:22:15 -08001473 if (video_receive_streams_.size() == 1)
1474 analyzer.SetReceiveStream(video_receive_streams_[0]);
kthelgason2bc68642017-02-07 07:02:22 -08001475
1476 video_send_stream_->SetSource(analyzer.OutputInterface(),
1477 degradation_preference_);
ivica5d6a06c2015-09-17 05:30:24 -07001478
perkja49cbd32016-09-16 07:53:41 -07001479 CreateCapturer();
1480 rtc::VideoSinkWants wants;
minyuea27172d2016-11-01 05:59:29 -07001481 video_capturer_->AddOrUpdateSink(analyzer.InputInterface(), wants);
ivicac1cc8542015-10-08 03:44:06 -07001482
palmkviste75f2042016-09-28 06:19:48 -07001483 StartEncodedFrameLogs(video_send_stream_);
1484 StartEncodedFrameLogs(video_receive_streams_[0]);
stefanff483612015-12-21 03:14:00 -08001485 video_send_stream_->Start();
1486 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1487 receive_stream->Start();
brandtr1293aca2016-11-16 22:47:29 -08001488 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1489 receive_stream->Start();
ilnikdf92c5c2017-02-23 02:08:44 -08001490 analyzer.StartMeasuringCpuProcessTime();
minyuea27172d2016-11-01 05:59:29 -07001491 video_capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001492
1493 analyzer.Wait();
1494
1495 send_transport.StopSending();
1496 recv_transport.StopSending();
1497
minyuea27172d2016-11-01 05:59:29 -07001498 video_capturer_->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001499 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1500 receive_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001501 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1502 receive_stream->Stop();
1503 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001504
1505 DestroyStreams();
1506
1507 if (graph_data_output_file)
1508 fclose(graph_data_output_file);
1509}
1510
minyuea27172d2016-11-01 05:59:29 -07001511void VideoQualityTest::SetupAudio(int send_channel_id,
1512 int receive_channel_id,
1513 Call* call,
1514 Transport* transport,
1515 AudioReceiveStream** audio_receive_stream) {
1516 audio_send_config_ = AudioSendStream::Config(transport);
1517 audio_send_config_.voe_channel_id = send_channel_id;
1518 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1519
1520 // Add extension to enable audio send side BWE, and allow audio bit rate
1521 // adaptation.
1522 audio_send_config_.rtp.extensions.clear();
1523 if (params_.call.send_side_bwe) {
1524 audio_send_config_.rtp.extensions.push_back(
1525 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1526 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001527 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1528 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001529 }
1530 audio_send_config_.send_codec_spec.codec_inst =
1531 CodecInst{120, "OPUS", 48000, 960, 2, 64000};
1532
1533 audio_send_stream_ = call->CreateAudioSendStream(audio_send_config_);
1534
1535 AudioReceiveStream::Config audio_config;
1536 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1537 audio_config.rtcp_send_transport = transport;
1538 audio_config.voe_channel_id = receive_channel_id;
1539 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1540 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1541 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1542 audio_config.decoder_factory = decoder_factory_;
1543 if (params_.video.enabled && params_.audio.sync_video)
1544 audio_config.sync_group = kSyncGroup;
1545
1546 *audio_receive_stream = call->CreateAudioReceiveStream(audio_config);
1547}
1548
minyue73208662016-08-18 06:28:55 -07001549void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001550 params_ = params;
1551 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001552
ivica5d6a06c2015-09-17 05:30:24 -07001553 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1554 // match the full stack tests.
skvlad11a9cbf2016-10-07 11:53:05 -07001555 webrtc::RtcEventLogNullImpl event_log;
1556 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001557 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001558
1559 ::VoiceEngineState voe;
minyue626bc952016-10-31 05:47:02 -07001560 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001561 CreateVoiceEngine(&voe, decoder_factory_);
1562 AudioState::Config audio_state_config;
1563 audio_state_config.voice_engine = voe.voice_engine;
aleloi10111bc2016-11-17 06:48:48 -08001564 audio_state_config.audio_mixer = AudioMixerImpl::Create();
minyue73208662016-08-18 06:28:55 -07001565 call_config.audio_state = AudioState::Create(audio_state_config);
1566 }
1567
kwiberg27f982b2016-03-01 11:52:33 -08001568 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001569
minyuea27172d2016-11-01 05:59:29 -07001570 // TODO(minyue): consider if this is a good transport even for audio only
1571 // calls.
ivica5d6a06c2015-09-17 05:30:24 -07001572 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001573 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue626bc952016-10-31 05:47:02 -07001574 params.video.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001575 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1576 // least share as much code as possible. That way this test would also match
1577 // the full stack tests better.
1578 transport.SetReceiver(call->Receiver());
1579
minyuea27172d2016-11-01 05:59:29 -07001580 VideoReceiveStream* video_receive_stream = nullptr;
brandtr1293aca2016-11-16 22:47:29 -08001581 FlexfecReceiveStream* flexfec_receive_stream = nullptr;
minyuea27172d2016-11-01 05:59:29 -07001582 std::unique_ptr<test::VideoRenderer> local_preview;
1583 std::unique_ptr<test::VideoRenderer> loopback_video;
1584 if (params_.video.enabled) {
1585 // Create video renderers.
1586 local_preview.reset(test::VideoRenderer::Create(
1587 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 05:13:32 -07001588
minyuea27172d2016-11-01 05:59:29 -07001589 size_t stream_id = params_.ss.selected_stream;
1590 std::string title = "Loopback Video";
1591 if (params_.ss.streams.size() > 1) {
1592 std::ostringstream s;
1593 s << stream_id;
1594 title += " - Stream #" + s.str();
1595 }
sprangce4aef12015-11-02 07:23:20 -08001596
minyuea27172d2016-11-01 05:59:29 -07001597 loopback_video.reset(test::VideoRenderer::Create(
1598 title.c_str(), params_.ss.streams[stream_id].width,
1599 params_.ss.streams[stream_id].height));
mflodman48a4beb2016-07-01 13:03:59 +02001600
minyuea27172d2016-11-01 05:59:29 -07001601 SetupVideo(&transport, &transport);
minyuea27172d2016-11-01 05:59:29 -07001602 video_send_config_.pre_encode_callback = local_preview.get();
1603 video_receive_configs_[stream_id].renderer = loopback_video.get();
1604 if (params_.audio.enabled && params_.audio.sync_video)
1605 video_receive_configs_[stream_id].sync_group = kSyncGroup;
1606
minyuea27172d2016-11-01 05:59:29 -07001607 if (params_.screenshare.enabled)
ilnik2a8c2f52017-02-15 02:23:28 -08001608 SetupScreenshareOrSVC();
minyuea27172d2016-11-01 05:59:29 -07001609
1610 video_send_stream_ = call->CreateVideoSendStream(
1611 video_send_config_.Copy(), video_encoder_config_.Copy());
brandtr1293aca2016-11-16 22:47:29 -08001612 if (params_.video.flexfec) {
1613 RTC_DCHECK(!flexfec_receive_configs_.empty());
1614 flexfec_receive_stream =
1615 call->CreateFlexfecReceiveStream(flexfec_receive_configs_[0]);
1616 }
minyuea27172d2016-11-01 05:59:29 -07001617 video_receive_stream = call->CreateVideoReceiveStream(
1618 video_receive_configs_[stream_id].Copy());
1619 CreateCapturer();
kthelgason2bc68642017-02-07 07:02:22 -08001620 video_send_stream_->SetSource(video_capturer_.get(),
1621 degradation_preference_);
philipel274c1dc2016-05-04 06:21:01 -07001622 }
1623
minyue73208662016-08-18 06:28:55 -07001624 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 05:47:02 -07001625 if (params_.audio.enabled) {
minyuea27172d2016-11-01 05:59:29 -07001626 SetupAudio(voe.send_channel_id, voe.receive_channel_id, call.get(),
1627 &transport, &audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001628 }
1629
palmkviste75f2042016-09-28 06:19:48 -07001630 StartEncodedFrameLogs(video_receive_stream);
1631 StartEncodedFrameLogs(video_send_stream_);
1632
minyue73208662016-08-18 06:28:55 -07001633 // Start sending and receiving video.
minyuea27172d2016-11-01 05:59:29 -07001634 if (params_.video.enabled) {
brandtr1293aca2016-11-16 22:47:29 -08001635 if (flexfec_receive_stream)
1636 flexfec_receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001637 video_receive_stream->Start();
1638 video_send_stream_->Start();
1639 video_capturer_->Start();
1640 }
ivica5d6a06c2015-09-17 05:30:24 -07001641
minyue626bc952016-10-31 05:47:02 -07001642 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001643 // Start receiving audio.
1644 audio_receive_stream->Start();
1645 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 06:28:55 -07001646
1647 // Start sending audio.
1648 audio_send_stream_->Start();
1649 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1650 }
1651
ivica5d6a06c2015-09-17 05:30:24 -07001652 test::PressEnterToContinue();
1653
minyue626bc952016-10-31 05:47:02 -07001654 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001655 // Stop sending audio.
1656 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1657 audio_send_stream_->Stop();
1658
1659 // Stop receiving audio.
minyue73208662016-08-18 06:28:55 -07001660 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
1661 audio_receive_stream->Stop();
minyuea27172d2016-11-01 05:59:29 -07001662 call->DestroyAudioSendStream(audio_send_stream_);
1663 call->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001664 }
1665
1666 // Stop receiving and sending video.
minyuea27172d2016-11-01 05:59:29 -07001667 if (params_.video.enabled) {
1668 video_capturer_->Stop();
1669 video_send_stream_->Stop();
1670 video_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001671 if (flexfec_receive_stream) {
1672 flexfec_receive_stream->Stop();
1673 call->DestroyFlexfecReceiveStream(flexfec_receive_stream);
1674 }
minyuea27172d2016-11-01 05:59:29 -07001675 call->DestroyVideoReceiveStream(video_receive_stream);
1676 call->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 06:28:55 -07001677 }
1678
ivica5d6a06c2015-09-17 05:30:24 -07001679 transport.StopSending();
minyue626bc952016-10-31 05:47:02 -07001680 if (params_.audio.enabled)
minyue73208662016-08-18 06:28:55 -07001681 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07001682}
1683
palmkviste75f2042016-09-28 06:19:48 -07001684void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001685 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001686 std::ostringstream str;
1687 str << send_logs_++;
1688 std::string prefix =
minyue626bc952016-10-31 05:47:02 -07001689 params_.video.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07001690 stream->EnableEncodedFrameRecording(
1691 std::vector<rtc::PlatformFile>(
1692 {rtc::CreatePlatformFile(prefix + "1.ivf"),
1693 rtc::CreatePlatformFile(prefix + "2.ivf"),
1694 rtc::CreatePlatformFile(prefix + "3.ivf")}),
1695 10000000);
1696 }
1697}
1698void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001699 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001700 std::ostringstream str;
1701 str << receive_logs_++;
1702 std::string path =
minyue626bc952016-10-31 05:47:02 -07001703 params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07001704 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
1705 10000000);
1706 }
1707}
1708
ivica5d6a06c2015-09-17 05:30:24 -07001709} // namespace webrtc