blob: c438274fcaac472a2c7ba64cc45bde8ef21231b0 [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"
Peter Boström5811a392015-12-10 13:02:50 +010021#include "webrtc/base/event.h"
ivica5d6a06c2015-09-17 05:30:24 -070022#include "webrtc/base/format_macros.h"
nissec7fe3c22016-04-20 03:25:36 -070023#include "webrtc/base/optional.h"
palmkviste75f2042016-09-28 06:19:48 -070024#include "webrtc/base/platform_file.h"
sprange1f2f1f2016-02-01 02:04:52 -080025#include "webrtc/base/timeutils.h"
ossuf515ab82016-12-07 04:52:58 -080026#include "webrtc/call/call.h"
ivica5d6a06c2015-09-17 05:30:24 -070027#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
skvlad11a9cbf2016-10-07 11:53:05 -070028#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
aleloi10111bc2016-11-17 06:48:48 -080029#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
sprangce4aef12015-11-02 07:23:20 -080031#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
magjed509e4fe2016-11-18 01:34:11 -080032#include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
33#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
34#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010035#include "webrtc/system_wrappers/include/cpu_info.h"
kwibergac9f8762016-09-30 22:29:43 -070036#include "webrtc/test/gtest.h"
ivica5d6a06c2015-09-17 05:30:24 -070037#include "webrtc/test/layer_filtering_transport.h"
38#include "webrtc/test/run_loop.h"
39#include "webrtc/test/statistics.h"
40#include "webrtc/test/testsupport/fileutils.h"
perkja49cbd32016-09-16 07:53:41 -070041#include "webrtc/test/vcm_capturer.h"
ivica5d6a06c2015-09-17 05:30:24 -070042#include "webrtc/test/video_renderer.h"
minyue73208662016-08-18 06:28:55 -070043#include "webrtc/voice_engine/include/voe_base.h"
minyue73208662016-08-18 06:28:55 -070044
45namespace {
46
47constexpr int kSendStatsPollingIntervalMs = 1000;
48constexpr int kPayloadTypeH264 = 122;
49constexpr int kPayloadTypeVP8 = 123;
50constexpr int kPayloadTypeVP9 = 124;
51constexpr size_t kMaxComparisons = 10;
52constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080053constexpr int kOpusMinBitrateBps = 6000;
54constexpr int kOpusBitrateFbBps = 32000;
minyue73208662016-08-18 06:28:55 -070055
56struct VoiceEngineState {
57 VoiceEngineState()
58 : voice_engine(nullptr),
59 base(nullptr),
minyue73208662016-08-18 06:28:55 -070060 send_channel_id(-1),
61 receive_channel_id(-1) {}
62
63 webrtc::VoiceEngine* voice_engine;
64 webrtc::VoEBase* base;
minyue73208662016-08-18 06:28:55 -070065 int send_channel_id;
66 int receive_channel_id;
67};
68
69void CreateVoiceEngine(VoiceEngineState* voe,
70 rtc::scoped_refptr<webrtc::AudioDecoderFactory>
71 decoder_factory) {
72 voe->voice_engine = webrtc::VoiceEngine::Create();
73 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
minyue73208662016-08-18 06:28:55 -070074 EXPECT_EQ(0, voe->base->Init(nullptr, nullptr, decoder_factory));
solenberg88499ec2016-09-07 07:34:41 -070075 webrtc::VoEBase::ChannelConfig config;
76 config.enable_voice_pacing = true;
77 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 06:28:55 -070078 EXPECT_GE(voe->send_channel_id, 0);
79 voe->receive_channel_id = voe->base->CreateChannel();
80 EXPECT_GE(voe->receive_channel_id, 0);
81}
82
83void DestroyVoiceEngine(VoiceEngineState* voe) {
84 voe->base->DeleteChannel(voe->send_channel_id);
85 voe->send_channel_id = -1;
86 voe->base->DeleteChannel(voe->receive_channel_id);
87 voe->receive_channel_id = -1;
88 voe->base->Release();
89 voe->base = nullptr;
minyue73208662016-08-18 06:28:55 -070090
91 webrtc::VoiceEngine::Delete(voe->voice_engine);
92 voe->voice_engine = nullptr;
93}
94
perkjfa10b552016-10-02 23:45:26 -070095class VideoStreamFactory
96 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
97 public:
98 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
99 : streams_(streams) {}
100
101 private:
102 std::vector<webrtc::VideoStream> CreateEncoderStreams(
103 int width,
104 int height,
105 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -0800106 // The highest layer must match the incoming resolution.
107 std::vector<webrtc::VideoStream> streams = streams_;
108 streams[streams_.size() - 1].height = height;
109 streams[streams_.size() - 1].width = width;
110 return streams;
perkjfa10b552016-10-02 23:45:26 -0700111 }
112
113 std::vector<webrtc::VideoStream> streams_;
114};
115
brandtr504b95e2016-12-21 02:54:35 -0800116bool IsFlexfec(int payload_type) {
117 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
118}
119
minyue73208662016-08-18 06:28:55 -0700120} // namespace
ivica5d6a06c2015-09-17 05:30:24 -0700121
122namespace webrtc {
123
ivica5d6a06c2015-09-17 05:30:24 -0700124class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -0700125 public Transport,
nisse7ade7b32016-03-23 04:48:10 -0700126 public rtc::VideoSinkInterface<VideoFrame>,
Peter Boströme4499152016-02-05 11:13:28 +0100127 public EncodedFrameObserver {
ivica5d6a06c2015-09-17 05:30:24 -0700128 public:
sprangce4aef12015-11-02 07:23:20 -0800129 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700130 const std::string& test_label,
131 double avg_psnr_threshold,
132 double avg_ssim_threshold,
133 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800134 FILE* graph_data_output_file,
135 const std::string& graph_title,
136 uint32_t ssrc_to_analyze)
perkja49cbd32016-09-16 07:53:41 -0700137 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700138 receiver_(nullptr),
139 send_stream_(nullptr),
perkja49cbd32016-09-16 07:53:41 -0700140 captured_frame_forwarder_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700141 test_label_(test_label),
142 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800143 graph_title_(graph_title),
144 ssrc_to_analyze_(ssrc_to_analyze),
pbos14fe7082016-04-20 06:35:56 -0700145 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100146 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700147 frames_to_process_(duration_frames),
148 frames_recorded_(0),
149 frames_processed_(0),
150 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700151 dropped_frames_before_first_encode_(0),
152 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700153 last_render_time_(0),
154 rtp_timestamp_delta_(0),
155 avg_psnr_threshold_(avg_psnr_threshold),
156 avg_ssim_threshold_(avg_ssim_threshold),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100157 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100158 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +0100159 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -0700160 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
161
162 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
163 // so that we don't accidentally starve "real" worker threads (codec etc).
164 // Also, don't allocate more than kMaxComparisonThreads, even if there are
165 // spare cores.
166
167 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800168 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700169 static const uint32_t kMinCoresLeft = 4;
170 static const uint32_t kMaxComparisonThreads = 8;
171
172 if (num_cores <= kMinCoresLeft) {
173 num_cores = 1;
174 } else {
175 num_cores -= kMinCoresLeft;
176 num_cores = std::min(num_cores, kMaxComparisonThreads);
177 }
178
179 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100180 rtc::PlatformThread* thread =
181 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
182 thread->Start();
183 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700184 }
ivica5d6a06c2015-09-17 05:30:24 -0700185 }
186
187 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100188 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
189 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700190 delete thread;
191 }
192 }
193
194 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
195
perkja49cbd32016-09-16 07:53:41 -0700196 void SetSendStream(VideoSendStream* stream) {
197 rtc::CritScope lock(&crit_);
198 RTC_DCHECK(!send_stream_);
199 send_stream_ = stream;
200 }
201
202 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
203 return &captured_frame_forwarder_;
204 }
205 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
206 return &captured_frame_forwarder_;
207 }
208
ivica5d6a06c2015-09-17 05:30:24 -0700209 DeliveryStatus DeliverPacket(MediaType media_type,
210 const uint8_t* packet,
211 size_t length,
212 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700213 // Ignore timestamps of RTCP packets. They're not synchronized with
214 // RTP packet timestamps and so they would confuse wrap_handler_.
215 if (RtpHeaderParser::IsRtcp(packet, length)) {
216 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
217 }
218
sprangce4aef12015-11-02 07:23:20 -0800219 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700220 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800221 parser.Parse(&header);
brandtr504b95e2016-12-21 02:54:35 -0800222 if (!IsFlexfec(header.payloadType)) {
223 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
224 // (FlexFEC and media are sent on different SSRCs, which have different
225 // timestamps spaces.)
ivica5d6a06c2015-09-17 05:30:24 -0700226 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800227 int64_t timestamp =
228 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
229 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700230 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
231 }
232
233 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
234 }
235
Peter Boströme4499152016-02-05 11:13:28 +0100236 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700237 rtc::CritScope crit(&comparison_lock_);
238 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
239 }
240
pbos14fe7082016-04-20 06:35:56 -0700241 void PreEncodeOnFrame(const VideoFrame& video_frame) {
242 rtc::CritScope lock(&crit_);
243 if (!first_send_timestamp_ && rtp_timestamp_delta_ == 0) {
244 while (frames_.front().timestamp() != video_frame.timestamp()) {
245 ++dropped_frames_before_first_encode_;
246 frames_.pop_front();
247 RTC_CHECK(!frames_.empty());
248 }
249 first_send_timestamp_ = rtc::Optional<uint32_t>(video_frame.timestamp());
250 }
251 }
252
stefan1d8a5062015-10-02 03:39:33 -0700253 bool SendRtp(const uint8_t* packet,
254 size_t length,
255 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800256 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700257 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800258 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700259
sprangce4aef12015-11-02 07:23:20 -0800260 int64_t current_time =
261 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
262 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700263 {
264 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800265
Peter Boström81cbd922016-03-22 12:19:07 +0100266 if (rtp_timestamp_delta_ == 0) {
nissec7fe3c22016-04-20 03:25:36 -0700267 rtp_timestamp_delta_ = header.timestamp - *first_send_timestamp_;
268 first_send_timestamp_ = rtc::Optional<uint32_t>();
ivica5d6a06c2015-09-17 05:30:24 -0700269 }
brandtr504b95e2016-12-21 02:54:35 -0800270 if (!IsFlexfec(header.payloadType)) {
271 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
272 // (FlexFEC and media are sent on different SSRCs, which have different
273 // timestamps spaces.)
274 int64_t timestamp =
275 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
276 send_times_[timestamp] = current_time;
277 if (!transport_->DiscardedLastPacket() &&
278 header.ssrc == ssrc_to_analyze_) {
279 encoded_frame_sizes_[timestamp] +=
280 length - (header.headerLength + header.paddingLength);
281 }
sprangce4aef12015-11-02 07:23:20 -0800282 }
ivica5d6a06c2015-09-17 05:30:24 -0700283 }
sprangce4aef12015-11-02 07:23:20 -0800284 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700285 }
286
287 bool SendRtcp(const uint8_t* packet, size_t length) override {
288 return transport_->SendRtcp(packet, length);
289 }
290
291 void EncodedFrameCallback(const EncodedFrame& frame) override {
292 rtc::CritScope lock(&comparison_lock_);
293 if (frames_recorded_ < frames_to_process_)
294 encoded_frame_size_.AddSample(frame.length_);
295 }
296
nisseeb83a1a2016-03-21 01:27:56 -0700297 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700298 int64_t render_time_ms =
299 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700300
301 rtc::CritScope lock(&crit_);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700302 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800303 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700304
sprang16daaa52016-03-09 01:30:24 -0800305 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700306 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700307 // No previous frame rendered, this one was dropped after sending but
308 // before rendering.
309 ++dropped_frames_before_rendering_;
310 frames_.pop_front();
311 RTC_CHECK(!frames_.empty());
312 continue;
313 }
nisse97f0b932016-05-26 09:44:40 -0700314 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
ivica5d6a06c2015-09-17 05:30:24 -0700315 render_time_ms);
316 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700317 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700318 }
319
320 VideoFrame reference_frame = frames_.front();
321 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800322 int64_t reference_timestamp =
323 wrap_handler_.Unwrap(reference_frame.timestamp());
324 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800325 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100326 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800327 ++send_timestamp;
328 }
sprang16daaa52016-03-09 01:30:24 -0800329 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700330
331 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
332
nisse97f0b932016-05-26 09:44:40 -0700333 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ivica5d6a06c2015-09-17 05:30:24 -0700334 }
335
ivica5d6a06c2015-09-17 05:30:24 -0700336 void Wait() {
337 // Frame comparisons can be very expensive. Wait for test to be done, but
338 // at time-out check if frames_processed is going up. If so, give it more
339 // time, otherwise fail. Hopefully this will reduce test flakiness.
340
Peter Boström8c38e8b2015-11-26 17:45:47 +0100341 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800342
ivica5d6a06c2015-09-17 05:30:24 -0700343 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700344 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100345 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700346 int frames_processed;
347 {
348 rtc::CritScope crit(&comparison_lock_);
349 frames_processed = frames_processed_;
350 }
351
352 // Print some output so test infrastructure won't think we've crashed.
353 const char* kKeepAliveMessages[3] = {
354 "Uh, I'm-I'm not quite dead, sir.",
355 "Uh, I-I think uh, I could pull through, sir.",
356 "Actually, I think I'm all right to come with you--"};
357 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
358
359 if (last_frames_processed == -1) {
360 last_frames_processed = frames_processed;
361 continue;
362 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100363 if (frames_processed == last_frames_processed) {
364 EXPECT_GT(frames_processed, last_frames_processed)
365 << "Analyzer stalled while waiting for test to finish.";
366 done_.Set();
367 break;
368 }
ivica5d6a06c2015-09-17 05:30:24 -0700369 last_frames_processed = frames_processed;
370 }
371
372 if (iteration > 0)
373 printf("- Farewell, sweet Concorde!\n");
374
Peter Boström8c38e8b2015-11-26 17:45:47 +0100375 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700376 }
377
pbos14fe7082016-04-20 06:35:56 -0700378 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
379 return &pre_encode_proxy_;
380 }
Peter Boströme4499152016-02-05 11:13:28 +0100381 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
382
sprangce4aef12015-11-02 07:23:20 -0800383 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700384 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700385
386 private:
387 struct FrameComparison {
388 FrameComparison()
389 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800390 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700391 send_time_ms(0),
392 recv_time_ms(0),
393 render_time_ms(0),
394 encoded_frame_size(0) {}
395
396 FrameComparison(const VideoFrame& reference,
397 const VideoFrame& render,
398 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800399 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700400 int64_t send_time_ms,
401 int64_t recv_time_ms,
402 int64_t render_time_ms,
403 size_t encoded_frame_size)
404 : reference(reference),
405 render(render),
406 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800407 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700408 send_time_ms(send_time_ms),
409 recv_time_ms(recv_time_ms),
410 render_time_ms(render_time_ms),
411 encoded_frame_size(encoded_frame_size) {}
412
nissedf2ceb82016-12-15 06:29:53 -0800413 FrameComparison(bool dropped,
414 int64_t input_time_ms,
415 int64_t send_time_ms,
416 int64_t recv_time_ms,
417 int64_t render_time_ms,
418 size_t encoded_frame_size)
419 : dropped(dropped),
420 input_time_ms(input_time_ms),
421 send_time_ms(send_time_ms),
422 recv_time_ms(recv_time_ms),
423 render_time_ms(render_time_ms),
424 encoded_frame_size(encoded_frame_size) {}
425
426 rtc::Optional<VideoFrame> reference;
427 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700428 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800429 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700430 int64_t send_time_ms;
431 int64_t recv_time_ms;
432 int64_t render_time_ms;
433 size_t encoded_frame_size;
434 };
435
436 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700437 Sample(int dropped,
438 int64_t input_time_ms,
439 int64_t send_time_ms,
440 int64_t recv_time_ms,
441 int64_t render_time_ms,
442 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700443 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700444 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700445 : dropped(dropped),
446 input_time_ms(input_time_ms),
447 send_time_ms(send_time_ms),
448 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700449 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700450 encoded_frame_size(encoded_frame_size),
451 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700452 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700453
ivica8d15bd62015-10-07 02:43:12 -0700454 int dropped;
455 int64_t input_time_ms;
456 int64_t send_time_ms;
457 int64_t recv_time_ms;
458 int64_t render_time_ms;
459 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700460 double psnr;
461 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700462 };
463
Peter Boströme4499152016-02-05 11:13:28 +0100464 // This class receives the send-side OnEncodeTiming and is provided to not
465 // conflict with the receiver-side pre_decode_callback.
466 class OnEncodeTimingProxy : public EncodedFrameObserver {
467 public:
468 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
469
470 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
471 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
472 }
473 void EncodedFrameCallback(const EncodedFrame& frame) override {}
474
475 private:
476 VideoAnalyzer* const parent_;
477 };
478
pbos14fe7082016-04-20 06:35:56 -0700479 // This class receives the send-side OnFrame callback and is provided to not
480 // conflict with the receiver-side renderer callback.
481 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
482 public:
483 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
484
485 void OnFrame(const VideoFrame& video_frame) override {
486 parent_->PreEncodeOnFrame(video_frame);
487 }
488
489 private:
490 VideoAnalyzer* const parent_;
491 };
492
ivica5d6a06c2015-09-17 05:30:24 -0700493 void AddFrameComparison(const VideoFrame& reference,
494 const VideoFrame& render,
495 bool dropped,
496 int64_t render_time_ms)
497 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800498 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
499 int64_t send_time_ms = send_times_[reference_timestamp];
500 send_times_.erase(reference_timestamp);
501 int64_t recv_time_ms = recv_times_[reference_timestamp];
502 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700503
sprangce4aef12015-11-02 07:23:20 -0800504 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800505 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800506 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800507 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800508 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
509 if (it != encoded_frame_sizes_.end())
510 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700511
ivica5d6a06c2015-09-17 05:30:24 -0700512 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700513 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800514 comparisons_.push_back(FrameComparison(reference, render, dropped,
515 reference.ntp_time_ms(),
516 send_time_ms, recv_time_ms,
517 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700518 } else {
nissedf2ceb82016-12-15 06:29:53 -0800519 comparisons_.push_back(FrameComparison(dropped,
520 reference.ntp_time_ms(),
521 send_time_ms, recv_time_ms,
522 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700523 }
Peter Boström5811a392015-12-10 13:02:50 +0100524 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700525 }
526
527 static bool PollStatsThread(void* obj) {
528 return static_cast<VideoAnalyzer*>(obj)->PollStats();
529 }
530
531 bool PollStats() {
Peter Boströmdd45eb62016-01-19 15:22:32 +0100532 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 13:02:50 +0100533 return false;
ivica5d6a06c2015-09-17 05:30:24 -0700534
535 VideoSendStream::Stats stats = send_stream_->GetStats();
536
537 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 17:30:49 +0100538 // It's not certain that we yet have estimates for any of these stats. Check
539 // that they are positive before mixing them in.
540 if (stats.encode_frame_rate > 0)
541 encode_frame_rate_.AddSample(stats.encode_frame_rate);
542 if (stats.avg_encode_time_ms > 0)
543 encode_time_ms.AddSample(stats.avg_encode_time_ms);
544 if (stats.encode_usage_percent > 0)
545 encode_usage_percent.AddSample(stats.encode_usage_percent);
546 if (stats.media_bitrate_bps > 0)
547 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 05:30:24 -0700548
549 return true;
550 }
551
552 static bool FrameComparisonThread(void* obj) {
553 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
554 }
555
556 bool CompareFrames() {
557 if (AllFramesRecorded())
558 return false;
559
ivica5d6a06c2015-09-17 05:30:24 -0700560 FrameComparison comparison;
561
562 if (!PopComparison(&comparison)) {
563 // Wait until new comparison task is available, or test is done.
564 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100565 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700566 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100567 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700568 return false;
569 }
570 return true; // Try again.
571 }
572
573 PerformFrameComparison(comparison);
574
575 if (FrameProcessed()) {
576 PrintResults();
577 if (graph_data_output_file_)
578 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100579 done_.Set();
580 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700581 return false;
582 }
583
584 return true;
585 }
586
587 bool PopComparison(FrameComparison* comparison) {
588 rtc::CritScope crit(&comparison_lock_);
589 // If AllFramesRecorded() is true, it means we have already popped
590 // frames_to_process_ frames from comparisons_, so there is no more work
591 // for this thread to be done. frames_processed_ might still be lower if
592 // all comparisons are not done, but those frames are currently being
593 // worked on by other threads.
594 if (comparisons_.empty() || AllFramesRecorded())
595 return false;
596
597 *comparison = comparisons_.front();
598 comparisons_.pop_front();
599
600 FrameRecorded();
601 return true;
602 }
603
604 // Increment counter for number of frames received for comparison.
605 void FrameRecorded() {
606 rtc::CritScope crit(&comparison_lock_);
607 ++frames_recorded_;
608 }
609
610 // Returns true if all frames to be compared have been taken from the queue.
611 bool AllFramesRecorded() {
612 rtc::CritScope crit(&comparison_lock_);
613 assert(frames_recorded_ <= frames_to_process_);
614 return frames_recorded_ == frames_to_process_;
615 }
616
617 // Increase count of number of frames processed. Returns true if this was the
618 // last frame to be processed.
619 bool FrameProcessed() {
620 rtc::CritScope crit(&comparison_lock_);
621 ++frames_processed_;
622 assert(frames_processed_ <= frames_to_process_);
623 return frames_processed_ == frames_to_process_;
624 }
625
626 void PrintResults() {
627 rtc::CritScope crit(&comparison_lock_);
628 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800629 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700630 PrintResult("sender_time", sender_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700631 PrintResult("receiver_time", receiver_time_, " ms");
632 PrintResult("total_delay_incl_network", end_to_end_, " ms");
633 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
634 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
635 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
636 PrintResult("encode_time", encode_time_ms, " ms");
637 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
638 PrintResult("media_bitrate", media_bitrate_bps, " bps");
639
pbos14fe7082016-04-20 06:35:56 -0700640 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
641 dropped_frames_);
642 printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n",
643 test_label_.c_str(), dropped_frames_before_first_encode_);
644 printf("RESULT dropped_frames_before_rendering: %s = %d frames\n",
645 test_label_.c_str(), dropped_frames_before_rendering_);
646
ivica5d6a06c2015-09-17 05:30:24 -0700647 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
648 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
649 }
650
651 void PerformFrameComparison(const FrameComparison& comparison) {
652 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700653 double psnr = -1.0;
654 double ssim = -1.0;
nissedf2ceb82016-12-15 06:29:53 -0800655 if (comparison.reference) {
656 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
657 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700658 }
ivica5d6a06c2015-09-17 05:30:24 -0700659
ivica5d6a06c2015-09-17 05:30:24 -0700660 rtc::CritScope crit(&comparison_lock_);
661 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800662 samples_.push_back(Sample(
663 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
664 comparison.recv_time_ms, comparison.render_time_ms,
665 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700666 }
stefanb1797672016-08-11 07:00:57 -0700667 if (psnr >= 0.0)
668 psnr_.AddSample(psnr);
669 if (ssim >= 0.0)
670 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700671
672 if (comparison.dropped) {
673 ++dropped_frames_;
674 return;
675 }
676 if (last_render_time_ != 0)
677 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
678 last_render_time_ = comparison.render_time_ms;
679
nissedf2ceb82016-12-15 06:29:53 -0800680 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800681 if (comparison.recv_time_ms > 0) {
682 // If recv_time_ms == 0, this frame consisted of a packets which were all
683 // lost in the transport. Since we were able to render the frame, however,
684 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
685 // happens internally in Call, and we can therefore here not know which
686 // FEC packets that protected the lost media packets. Consequently, we
687 // were not able to record a meaningful recv_time_ms. We therefore skip
688 // this sample.
689 //
690 // The reasoning above does not hold for ULPFEC and RTX, as for those
691 // strategies the timestamp of the received packets is set to the
692 // timestamp of the protected/retransmitted media packet. I.e., then
693 // recv_time_ms != 0, even though the media packets were lost.
694 receiver_time_.AddSample(comparison.render_time_ms -
695 comparison.recv_time_ms);
696 }
nissedf2ceb82016-12-15 06:29:53 -0800697 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700698 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
699 }
700
701 void PrintResult(const char* result_type,
702 test::Statistics stats,
703 const char* unit) {
704 printf("RESULT %s: %s = {%f, %f}%s\n",
705 result_type,
706 test_label_.c_str(),
707 stats.Mean(),
708 stats.StandardDeviation(),
709 unit);
710 }
711
712 void PrintSamplesToFile(void) {
713 FILE* out = graph_data_output_file_;
714 rtc::CritScope crit(&comparison_lock_);
715 std::sort(samples_.begin(), samples_.end(),
716 [](const Sample& A, const Sample& B) -> bool {
717 return A.input_time_ms < B.input_time_ms;
718 });
719
sprangce4aef12015-11-02 07:23:20 -0800720 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700721 fprintf(out, "%" PRIuS "\n", samples_.size());
722 fprintf(out,
723 "dropped "
724 "input_time_ms "
725 "send_time_ms "
726 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700727 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700728 "encoded_frame_size "
729 "psnr "
730 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700731 "encode_time_ms\n");
732 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700733 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700734 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
735 int encode_time_ms;
736 if (it != samples_encode_time_ms_.end()) {
737 encode_time_ms = it->second;
738 } else {
739 ++missing_encode_time_samples;
740 encode_time_ms = -1;
741 }
742 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
743 " %lf %lf %d\n",
744 sample.dropped, sample.input_time_ms, sample.send_time_ms,
745 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700746 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700747 encode_time_ms);
748 }
749 if (missing_encode_time_samples) {
750 fprintf(stderr,
751 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
752 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700753 }
754 }
755
perkja49cbd32016-09-16 07:53:41 -0700756 // Implements VideoSinkInterface to receive captured frames from a
757 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
758 // as a source to VideoSendStream.
759 // It forwards all input frames to the VideoAnalyzer for later comparison and
760 // forwards the captured frames to the VideoSendStream.
761 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
762 public rtc::VideoSourceInterface<VideoFrame> {
763 public:
764 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer)
765 : analyzer_(analyzer), send_stream_input_(nullptr) {}
766
767 private:
768 void OnFrame(const VideoFrame& video_frame) override {
769 VideoFrame copy = video_frame;
770 copy.set_timestamp(copy.ntp_time_ms() * 90);
771
772 analyzer_->AddCapturedFrameForComparison(video_frame);
773 rtc::CritScope lock(&crit_);
774 if (send_stream_input_)
775 send_stream_input_->OnFrame(video_frame);
776 }
777
778 // Called when |send_stream_.SetSource()| is called.
779 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
780 const rtc::VideoSinkWants& wants) override {
781 rtc::CritScope lock(&crit_);
782 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
783 send_stream_input_ = sink;
784 }
785
786 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
787 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
788 rtc::CritScope lock(&crit_);
789 RTC_DCHECK(sink == send_stream_input_);
790 send_stream_input_ = nullptr;
791 }
792
793 VideoAnalyzer* const analyzer_;
794 rtc::CriticalSection crit_;
795 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
796 };
797
798 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
799 rtc::CritScope lock(&crit_);
kwibergaf476c72016-11-28 15:21:39 -0800800 RTC_DCHECK_EQ(0, video_frame.timestamp());
perkja49cbd32016-09-16 07:53:41 -0700801 // Frames from the capturer does not have a rtp timestamp. Create one so it
802 // can be used for comparison.
803 VideoFrame copy = video_frame;
804 copy.set_timestamp(copy.ntp_time_ms() * 90);
805 frames_.push_back(copy);
806 }
807
808 VideoSendStream* send_stream_;
809 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700810 const std::string test_label_;
811 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800812 const std::string graph_title_;
813 const uint32_t ssrc_to_analyze_;
pbos14fe7082016-04-20 06:35:56 -0700814 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100815 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700816 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700817 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700818 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
819 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
820 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
821 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
822 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
823 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
824 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
825 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
826 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
827 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
828 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
829
830 const int frames_to_process_;
831 int frames_recorded_;
832 int frames_processed_;
833 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -0700834 int dropped_frames_before_first_encode_;
835 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -0700836 int64_t last_render_time_;
837 uint32_t rtp_timestamp_delta_;
838
839 rtc::CriticalSection crit_;
840 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -0700841 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800842 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
843 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
844 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
845 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
nissec7fe3c22016-04-20 03:25:36 -0700846 rtc::Optional<uint32_t> first_send_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700847 const double avg_psnr_threshold_;
848 const double avg_ssim_threshold_;
849
850 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100851 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
852 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +0100853 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -0700854 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +0100855 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -0700856};
857
palmkviste75f2042016-09-28 06:19:48 -0700858VideoQualityTest::VideoQualityTest()
859 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {}
ivica5d6a06c2015-09-17 05:30:24 -0700860
minyue626bc952016-10-31 05:47:02 -0700861VideoQualityTest::Params::Params()
862 : call({false, Call::Config::BitrateConfig()}),
863 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
brandtr1293aca2016-11-16 22:47:29 -0800864 false, "", ""}),
minyue626bc952016-10-31 05:47:02 -0700865 audio({false, false}),
866 screenshare({false, 10, 0}),
867 analyzer({"", 0.0, 0.0, 0, "", ""}),
868 pipe(),
869 logs(false),
870 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}) {}
871
872VideoQualityTest::Params::~Params() = default;
873
ivica5d6a06c2015-09-17 05:30:24 -0700874void VideoQualityTest::TestBody() {}
875
sprangce4aef12015-11-02 07:23:20 -0800876std::string VideoQualityTest::GenerateGraphTitle() const {
877 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -0700878 ss << params_.video.codec;
879 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
880 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -0800881 if (params_.screenshare.scroll_duration)
882 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
883 if (params_.ss.streams.size() > 1)
884 ss << ", Stream #" << params_.ss.selected_stream;
885 if (params_.ss.num_spatial_layers > 1)
886 ss << ", Layer #" << params_.ss.selected_sl;
887 ss << ")";
888 return ss.str();
889}
890
891void VideoQualityTest::CheckParams() {
892 // Add a default stream in none specified.
893 if (params_.ss.streams.empty())
894 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
895 if (params_.ss.num_spatial_layers == 0)
896 params_.ss.num_spatial_layers = 1;
897
898 if (params_.pipe.loss_percent != 0 ||
899 params_.pipe.queue_length_packets != 0) {
900 // Since LayerFilteringTransport changes the sequence numbers, we can't
901 // use that feature with pack loss, since the NACK request would end up
902 // retransmitting the wrong packets.
903 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800904 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -0700905 RTC_CHECK(params_.video.selected_tl == -1 ||
906 params_.video.selected_tl ==
907 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800908 }
909
910 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
911 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -0700912 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
913 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
914 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprangce4aef12015-11-02 07:23:20 -0800915 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
916 for (const VideoStream& stream : params_.ss.streams) {
917 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
918 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
919 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
kwiberg352444f2016-11-28 15:58:53 -0800920 RTC_CHECK_EQ(stream.temporal_layer_thresholds_bps.size(),
minyue626bc952016-10-31 05:47:02 -0700921 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800922 }
923 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
924 // the total bitrate? We anyway have to update them in the case bitrate
925 // estimator changes the total bitrates.
926 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
927 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
928 RTC_CHECK(params_.ss.spatial_layers.empty() ||
929 params_.ss.spatial_layers.size() ==
930 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -0700931 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -0800932 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -0700933 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -0800934 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -0800935 }
936}
937
938// Static.
939std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
940 // Parse comma separated nonnegative integers, where some elements may be
941 // empty. The empty values are replaced with -1.
942 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
943 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
944 std::vector<int> result;
945 if (str.empty())
946 return result;
947
948 const char* p = str.c_str();
949 int value = -1;
950 int pos;
951 while (*p) {
952 if (*p == ',') {
953 result.push_back(value);
954 value = -1;
955 ++p;
956 continue;
957 }
958 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
959 << "Unexpected non-number value.";
960 p += pos;
961 }
962 result.push_back(value);
963 return result;
964}
965
966// Static.
967VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
968 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -0700969 stream.width = params.video.width;
970 stream.height = params.video.height;
971 stream.max_framerate = params.video.fps;
972 stream.min_bitrate_bps = params.video.min_bitrate_bps;
973 stream.target_bitrate_bps = params.video.target_bitrate_bps;
974 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprangce4aef12015-11-02 07:23:20 -0800975 stream.max_qp = 52;
minyue626bc952016-10-31 05:47:02 -0700976 if (params.video.num_temporal_layers == 2)
sprangce4aef12015-11-02 07:23:20 -0800977 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
978 return stream;
979}
980
981// Static.
982void VideoQualityTest::FillScalabilitySettings(
983 Params* params,
984 const std::vector<std::string>& stream_descriptors,
985 size_t selected_stream,
986 int num_spatial_layers,
987 int selected_sl,
988 const std::vector<std::string>& sl_descriptors) {
989 // Read VideoStream and SpatialLayer elements from a list of comma separated
990 // lists. To use a default value for an element, use -1 or leave empty.
991 // Validity checks performed in CheckParams.
992
993 RTC_CHECK(params->ss.streams.empty());
994 for (auto descriptor : stream_descriptors) {
995 if (descriptor.empty())
996 continue;
997 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
998 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
999 if (v[0] != -1)
1000 stream.width = static_cast<size_t>(v[0]);
1001 if (v[1] != -1)
1002 stream.height = static_cast<size_t>(v[1]);
1003 if (v[2] != -1)
1004 stream.max_framerate = v[2];
1005 if (v[3] != -1)
1006 stream.min_bitrate_bps = v[3];
1007 if (v[4] != -1)
1008 stream.target_bitrate_bps = v[4];
1009 if (v[5] != -1)
1010 stream.max_bitrate_bps = v[5];
1011 if (v.size() > 6 && v[6] != -1)
1012 stream.max_qp = v[6];
1013 if (v.size() > 7) {
1014 stream.temporal_layer_thresholds_bps.clear();
1015 stream.temporal_layer_thresholds_bps.insert(
1016 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1017 } else {
1018 // Automatic TL thresholds for more than two layers not supported.
minyue626bc952016-10-31 05:47:02 -07001019 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
sprangce4aef12015-11-02 07:23:20 -08001020 }
1021 params->ss.streams.push_back(stream);
1022 }
1023 params->ss.selected_stream = selected_stream;
1024
1025 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
1026 params->ss.selected_sl = selected_sl;
1027 RTC_CHECK(params->ss.spatial_layers.empty());
1028 for (auto descriptor : sl_descriptors) {
1029 if (descriptor.empty())
1030 continue;
1031 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1032 RTC_CHECK_GT(v[2], 0);
1033
1034 SpatialLayer layer;
1035 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1036 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1037 layer.target_bitrate_bps = v[2];
1038 params->ss.spatial_layers.push_back(layer);
1039 }
1040}
1041
minyuea27172d2016-11-01 05:59:29 -07001042void VideoQualityTest::SetupVideo(Transport* send_transport,
1043 Transport* recv_transport) {
sprangce4aef12015-11-02 07:23:20 -08001044 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -07001045 trace_to_stderr_.reset(new test::TraceToStderr);
1046
brandtr8313a6f2017-01-13 07:41:19 -08001047 size_t num_video_streams = params_.ss.streams.size();
1048 size_t num_flexfec_streams = params_.video.flexfec ? 1 : 0;
1049 CreateSendConfig(num_video_streams, 0, num_flexfec_streams, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001050
1051 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001052 if (params_.video.codec == "H264") {
magjedceecea42016-11-28 07:20:21 -08001053 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001054 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001055 } else if (params_.video.codec == "VP8") {
magjed509e4fe2016-11-18 01:34:11 -08001056 video_encoder_.reset(VP8Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001057 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001058 } else if (params_.video.codec == "VP9") {
magjed509e4fe2016-11-18 01:34:11 -08001059 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001060 payload_type = kPayloadTypeVP9;
1061 } else {
1062 RTC_NOTREACHED() << "Codec not supported!";
1063 return;
1064 }
minyuea27172d2016-11-01 05:59:29 -07001065 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001066 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001067 video_send_config_.encoder_settings.payload_type = payload_type;
1068 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1069 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
brandtr8313a6f2017-01-13 07:41:19 -08001070 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001071 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001072
stefanff483612015-12-21 03:14:00 -08001073 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001074 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001075 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001076 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001077 test::kTransportSequenceNumberExtensionId));
1078 } else {
stefanff483612015-12-21 03:14:00 -08001079 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001080 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik SprĂ¥ng6b8d3552015-09-24 15:06:57 +02001081 }
1082
stefanff483612015-12-21 03:14:00 -08001083 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001084 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001085
brandtr1293aca2016-11-16 22:47:29 -08001086 video_send_config_.suspend_below_min_bitrate =
1087 params_.video.suspend_below_min_bitrate;
1088
perkjfa10b552016-10-02 23:45:26 -07001089 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1090 video_encoder_config_.max_bitrate_bps = 0;
1091 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1092 video_encoder_config_.max_bitrate_bps +=
1093 params_.ss.streams[i].max_bitrate_bps;
1094 }
1095 video_encoder_config_.video_stream_factory =
1096 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1097
stefanff483612015-12-21 03:14:00 -08001098 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001099
1100 CreateMatchingReceiveConfigs(recv_transport);
1101
brandtr8313a6f2017-01-13 07:41:19 -08001102 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001103 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 13:29:59 +01001104 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
1105 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 07:23:20 -08001106 kSendRtxPayloadType;
minyue626bc952016-10-31 05:47:02 -07001107 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -08001108 }
brandtr1293aca2016-11-16 22:47:29 -08001109
1110 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 07:41:19 -08001111 // Override send config constructed by CreateSendConfig.
brandtr1293aca2016-11-16 22:47:29 -08001112 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1113 kVideoSendSsrcs[params_.ss.selected_stream]};
1114
brandtr8313a6f2017-01-13 07:41:19 -08001115 // The matching receive config is _not_ created by
1116 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1117 // Set up the receive config manually instead.
1118 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001119 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 06:59:19 -08001120 video_send_config_.rtp.flexfec.payload_type;
1121 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001122 flexfec_receive_config.protected_media_ssrcs =
1123 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001124 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001125 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1126 if (params_.call.send_side_bwe) {
1127 flexfec_receive_config.rtp_header_extensions.push_back(
1128 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1129 test::kTransportSequenceNumberExtensionId));
1130 } else {
1131 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1132 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1133 }
brandtr1293aca2016-11-16 22:47:29 -08001134 flexfec_receive_configs_.push_back(flexfec_receive_config);
1135 }
1136
1137 if (params_.video.ulpfec) {
1138 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1139 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1140 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1141
1142 video_receive_configs_[params_.ss.selected_stream]
1143 .rtp.ulpfec.red_payload_type =
1144 video_send_config_.rtp.ulpfec.red_payload_type;
1145 video_receive_configs_[params_.ss.selected_stream]
1146 .rtp.ulpfec.ulpfec_payload_type =
1147 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1148 video_receive_configs_[params_.ss.selected_stream]
1149 .rtp.ulpfec.red_rtx_payload_type =
1150 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1151 }
ivica5d6a06c2015-09-17 05:30:24 -07001152}
1153
sprangce4aef12015-11-02 07:23:20 -08001154void VideoQualityTest::SetupScreenshare() {
1155 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001156
1157 // Fill out codec settings.
stefanff483612015-12-21 03:14:00 -08001158 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
minyue626bc952016-10-31 05:47:02 -07001159 if (params_.video.codec == "VP8") {
kthelgason29a44e32016-09-27 03:52:02 -07001160 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1161 vp8_settings.denoisingOn = false;
1162 vp8_settings.frameDroppingOn = false;
1163 vp8_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001164 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001165 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1166 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
minyue626bc952016-10-31 05:47:02 -07001167 } else if (params_.video.codec == "VP9") {
kthelgason29a44e32016-09-27 03:52:02 -07001168 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1169 vp9_settings.denoisingOn = false;
1170 vp9_settings.frameDroppingOn = false;
1171 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001172 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001173 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001174 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001175 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1176 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001177 }
1178
1179 // Setup frame generator.
1180 const size_t kWidth = 1850;
1181 const size_t kHeight = 1110;
1182 std::vector<std::string> slides;
1183 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1184 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1185 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1186 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1187
sprangce4aef12015-11-02 07:23:20 -08001188 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 05:30:24 -07001189 // Cycle image every slide_change_interval seconds.
1190 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
1191 slides, kWidth, kHeight,
minyue626bc952016-10-31 05:47:02 -07001192 params_.screenshare.slide_change_interval * params_.video.fps));
ivica5d6a06c2015-09-17 05:30:24 -07001193 } else {
minyue626bc952016-10-31 05:47:02 -07001194 RTC_CHECK_LE(params_.video.width, kWidth);
1195 RTC_CHECK_LE(params_.video.height, kHeight);
sprangce4aef12015-11-02 07:23:20 -08001196 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1197 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1198 params_.screenshare.scroll_duration) *
1199 1000;
1200 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1201 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 05:30:24 -07001202
sprangce4aef12015-11-02 07:23:20 -08001203 frame_generator_.reset(
1204 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
minyue626bc952016-10-31 05:47:02 -07001205 clock_, slides, kWidth, kHeight, params_.video.width,
1206 params_.video.height, params_.screenshare.scroll_duration * 1000,
sprangce4aef12015-11-02 07:23:20 -08001207 kPauseDurationMs));
ivica5d6a06c2015-09-17 05:30:24 -07001208 }
1209}
1210
perkja49cbd32016-09-16 07:53:41 -07001211void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001212 if (params_.screenshare.enabled) {
1213 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja49cbd32016-09-16 07:53:41 -07001214 new test::FrameGeneratorCapturer(clock_, frame_generator_.release(),
minyue626bc952016-10-31 05:47:02 -07001215 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001216 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001217 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001218 } else {
sprangce4aef12015-11-02 07:23:20 -08001219 if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001220 video_capturer_.reset(test::VcmCapturer::Create(
minyue626bc952016-10-31 05:47:02 -07001221 params_.video.width, params_.video.height, params_.video.fps));
ivica5d6a06c2015-09-17 05:30:24 -07001222 } else {
minyuea27172d2016-11-01 05:59:29 -07001223 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001224 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001225 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001226 clock_));
minyuea27172d2016-11-01 05:59:29 -07001227 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1228 << params_.video.clip_name
1229 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001230 }
1231 }
1232}
1233
sprang7a975f72015-10-12 06:33:21 -07001234void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001235 params_ = params;
1236
minyue626bc952016-10-31 05:47:02 -07001237 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001238 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1239 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001240 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001241
1242 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001243 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001244 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001245 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001246 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001247 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1248 << "!";
ivica87f83a92015-10-08 05:13:32 -07001249 }
sprang7a975f72015-10-12 06:33:21 -07001250
skvlad11a9cbf2016-10-07 11:53:05 -07001251 webrtc::RtcEventLogNullImpl event_log;
1252 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001253 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001254 CreateCalls(call_config, call_config);
1255
ivica87f83a92015-10-08 05:13:32 -07001256 test::LayerFilteringTransport send_transport(
minyue626bc952016-10-31 05:47:02 -07001257 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
1258 params_.video.selected_tl, params_.ss.selected_sl);
1259 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get());
stefanf116bd02015-10-27 08:29:42 -07001260
sprangce4aef12015-11-02 07:23:20 -08001261 std::string graph_title = params_.analyzer.graph_title;
1262 if (graph_title.empty())
1263 graph_title = VideoQualityTest::GenerateGraphTitle();
1264
1265 // In the case of different resolutions, the functions calculating PSNR and
1266 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
1267 // aborts if the average psnr/ssim are below the given threshold, which is
1268 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
1269 // abort.
1270 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
1271 int selected_sl = params_.ss.selected_sl != -1
1272 ? params_.ss.selected_sl
1273 : params_.ss.num_spatial_layers - 1;
1274 bool disable_quality_check =
minyue626bc952016-10-31 05:47:02 -07001275 selected_stream.width != params_.video.width ||
1276 selected_stream.height != params_.video.height ||
sprangce4aef12015-11-02 07:23:20 -08001277 (!params_.ss.spatial_layers.empty() &&
1278 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
1279 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
1280 if (disable_quality_check) {
1281 fprintf(stderr,
1282 "Warning: Calculating PSNR and SSIM for downsized resolution "
1283 "not implemented yet! Skipping PSNR and SSIM calculations!");
1284 }
1285
ivica5d6a06c2015-09-17 05:30:24 -07001286 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001287 &send_transport, params_.analyzer.test_label,
1288 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
1289 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
minyue626bc952016-10-31 05:47:02 -07001290 params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001291 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001292 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 05:30:24 -07001293
ivica5d6a06c2015-09-17 05:30:24 -07001294 analyzer.SetReceiver(receiver_call_->Receiver());
1295 send_transport.SetReceiver(&analyzer);
1296 recv_transport.SetReceiver(sender_call_->Receiver());
1297
minyuea27172d2016-11-01 05:59:29 -07001298 SetupVideo(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001299 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001300 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
stefanff483612015-12-21 03:14:00 -08001301 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 05:30:24 -07001302 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 11:13:28 +01001303 RTC_DCHECK(!video_send_config_.post_encode_callback);
1304 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001305
sprangce4aef12015-11-02 07:23:20 -08001306 if (params_.screenshare.enabled)
1307 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001308
brandtr1293aca2016-11-16 22:47:29 -08001309 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001310 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001311 analyzer.SetSendStream(video_send_stream_);
perkj803d97f2016-11-01 11:45:46 -07001312 video_send_stream_->SetSource(
1313 analyzer.OutputInterface(),
1314 VideoSendStream::DegradationPreference::kBalanced);
ivica5d6a06c2015-09-17 05:30:24 -07001315
perkja49cbd32016-09-16 07:53:41 -07001316 CreateCapturer();
1317 rtc::VideoSinkWants wants;
minyuea27172d2016-11-01 05:59:29 -07001318 video_capturer_->AddOrUpdateSink(analyzer.InputInterface(), wants);
ivicac1cc8542015-10-08 03:44:06 -07001319
palmkviste75f2042016-09-28 06:19:48 -07001320 StartEncodedFrameLogs(video_send_stream_);
1321 StartEncodedFrameLogs(video_receive_streams_[0]);
stefanff483612015-12-21 03:14:00 -08001322 video_send_stream_->Start();
1323 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1324 receive_stream->Start();
brandtr1293aca2016-11-16 22:47:29 -08001325 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1326 receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001327 video_capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001328
1329 analyzer.Wait();
1330
1331 send_transport.StopSending();
1332 recv_transport.StopSending();
1333
minyuea27172d2016-11-01 05:59:29 -07001334 video_capturer_->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001335 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1336 receive_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001337 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1338 receive_stream->Stop();
1339 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001340
1341 DestroyStreams();
1342
1343 if (graph_data_output_file)
1344 fclose(graph_data_output_file);
1345}
1346
minyuea27172d2016-11-01 05:59:29 -07001347void VideoQualityTest::SetupAudio(int send_channel_id,
1348 int receive_channel_id,
1349 Call* call,
1350 Transport* transport,
1351 AudioReceiveStream** audio_receive_stream) {
1352 audio_send_config_ = AudioSendStream::Config(transport);
1353 audio_send_config_.voe_channel_id = send_channel_id;
1354 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1355
1356 // Add extension to enable audio send side BWE, and allow audio bit rate
1357 // adaptation.
1358 audio_send_config_.rtp.extensions.clear();
1359 if (params_.call.send_side_bwe) {
1360 audio_send_config_.rtp.extensions.push_back(
1361 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1362 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001363 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1364 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001365 }
1366 audio_send_config_.send_codec_spec.codec_inst =
1367 CodecInst{120, "OPUS", 48000, 960, 2, 64000};
1368
1369 audio_send_stream_ = call->CreateAudioSendStream(audio_send_config_);
1370
1371 AudioReceiveStream::Config audio_config;
1372 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1373 audio_config.rtcp_send_transport = transport;
1374 audio_config.voe_channel_id = receive_channel_id;
1375 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1376 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1377 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1378 audio_config.decoder_factory = decoder_factory_;
1379 if (params_.video.enabled && params_.audio.sync_video)
1380 audio_config.sync_group = kSyncGroup;
1381
1382 *audio_receive_stream = call->CreateAudioReceiveStream(audio_config);
1383}
1384
minyue73208662016-08-18 06:28:55 -07001385void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001386 params_ = params;
1387 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001388
ivica5d6a06c2015-09-17 05:30:24 -07001389 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1390 // match the full stack tests.
skvlad11a9cbf2016-10-07 11:53:05 -07001391 webrtc::RtcEventLogNullImpl event_log;
1392 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001393 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001394
1395 ::VoiceEngineState voe;
minyue626bc952016-10-31 05:47:02 -07001396 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001397 CreateVoiceEngine(&voe, decoder_factory_);
1398 AudioState::Config audio_state_config;
1399 audio_state_config.voice_engine = voe.voice_engine;
aleloi10111bc2016-11-17 06:48:48 -08001400 audio_state_config.audio_mixer = AudioMixerImpl::Create();
minyue73208662016-08-18 06:28:55 -07001401 call_config.audio_state = AudioState::Create(audio_state_config);
1402 }
1403
kwiberg27f982b2016-03-01 11:52:33 -08001404 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001405
minyuea27172d2016-11-01 05:59:29 -07001406 // TODO(minyue): consider if this is a good transport even for audio only
1407 // calls.
ivica5d6a06c2015-09-17 05:30:24 -07001408 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001409 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue626bc952016-10-31 05:47:02 -07001410 params.video.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001411 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1412 // least share as much code as possible. That way this test would also match
1413 // the full stack tests better.
1414 transport.SetReceiver(call->Receiver());
1415
minyuea27172d2016-11-01 05:59:29 -07001416 VideoReceiveStream* video_receive_stream = nullptr;
brandtr1293aca2016-11-16 22:47:29 -08001417 FlexfecReceiveStream* flexfec_receive_stream = nullptr;
minyuea27172d2016-11-01 05:59:29 -07001418 std::unique_ptr<test::VideoRenderer> local_preview;
1419 std::unique_ptr<test::VideoRenderer> loopback_video;
1420 if (params_.video.enabled) {
1421 // Create video renderers.
1422 local_preview.reset(test::VideoRenderer::Create(
1423 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 05:13:32 -07001424
minyuea27172d2016-11-01 05:59:29 -07001425 size_t stream_id = params_.ss.selected_stream;
1426 std::string title = "Loopback Video";
1427 if (params_.ss.streams.size() > 1) {
1428 std::ostringstream s;
1429 s << stream_id;
1430 title += " - Stream #" + s.str();
1431 }
sprangce4aef12015-11-02 07:23:20 -08001432
minyuea27172d2016-11-01 05:59:29 -07001433 loopback_video.reset(test::VideoRenderer::Create(
1434 title.c_str(), params_.ss.streams[stream_id].width,
1435 params_.ss.streams[stream_id].height));
mflodman48a4beb2016-07-01 13:03:59 +02001436
minyuea27172d2016-11-01 05:59:29 -07001437 SetupVideo(&transport, &transport);
minyuea27172d2016-11-01 05:59:29 -07001438 video_send_config_.pre_encode_callback = local_preview.get();
1439 video_receive_configs_[stream_id].renderer = loopback_video.get();
1440 if (params_.audio.enabled && params_.audio.sync_video)
1441 video_receive_configs_[stream_id].sync_group = kSyncGroup;
1442
minyuea27172d2016-11-01 05:59:29 -07001443 if (params_.screenshare.enabled)
1444 SetupScreenshare();
1445
1446 video_send_stream_ = call->CreateVideoSendStream(
1447 video_send_config_.Copy(), video_encoder_config_.Copy());
brandtr1293aca2016-11-16 22:47:29 -08001448 if (params_.video.flexfec) {
1449 RTC_DCHECK(!flexfec_receive_configs_.empty());
1450 flexfec_receive_stream =
1451 call->CreateFlexfecReceiveStream(flexfec_receive_configs_[0]);
1452 }
minyuea27172d2016-11-01 05:59:29 -07001453 video_receive_stream = call->CreateVideoReceiveStream(
1454 video_receive_configs_[stream_id].Copy());
1455 CreateCapturer();
perkj803d97f2016-11-01 11:45:46 -07001456 video_send_stream_->SetSource(
1457 video_capturer_.get(),
1458 VideoSendStream::DegradationPreference::kBalanced);
philipel274c1dc2016-05-04 06:21:01 -07001459 }
1460
minyue73208662016-08-18 06:28:55 -07001461 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 05:47:02 -07001462 if (params_.audio.enabled) {
minyuea27172d2016-11-01 05:59:29 -07001463 SetupAudio(voe.send_channel_id, voe.receive_channel_id, call.get(),
1464 &transport, &audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001465 }
1466
palmkviste75f2042016-09-28 06:19:48 -07001467 StartEncodedFrameLogs(video_receive_stream);
1468 StartEncodedFrameLogs(video_send_stream_);
1469
minyue73208662016-08-18 06:28:55 -07001470 // Start sending and receiving video.
minyuea27172d2016-11-01 05:59:29 -07001471 if (params_.video.enabled) {
brandtr1293aca2016-11-16 22:47:29 -08001472 if (flexfec_receive_stream)
1473 flexfec_receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001474 video_receive_stream->Start();
1475 video_send_stream_->Start();
1476 video_capturer_->Start();
1477 }
ivica5d6a06c2015-09-17 05:30:24 -07001478
minyue626bc952016-10-31 05:47:02 -07001479 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001480 // Start receiving audio.
1481 audio_receive_stream->Start();
1482 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 06:28:55 -07001483
1484 // Start sending audio.
1485 audio_send_stream_->Start();
1486 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1487 }
1488
ivica5d6a06c2015-09-17 05:30:24 -07001489 test::PressEnterToContinue();
1490
minyue626bc952016-10-31 05:47:02 -07001491 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001492 // Stop sending audio.
1493 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1494 audio_send_stream_->Stop();
1495
1496 // Stop receiving audio.
minyue73208662016-08-18 06:28:55 -07001497 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
1498 audio_receive_stream->Stop();
minyuea27172d2016-11-01 05:59:29 -07001499 call->DestroyAudioSendStream(audio_send_stream_);
1500 call->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001501 }
1502
1503 // Stop receiving and sending video.
minyuea27172d2016-11-01 05:59:29 -07001504 if (params_.video.enabled) {
1505 video_capturer_->Stop();
1506 video_send_stream_->Stop();
1507 video_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001508 if (flexfec_receive_stream) {
1509 flexfec_receive_stream->Stop();
1510 call->DestroyFlexfecReceiveStream(flexfec_receive_stream);
1511 }
minyuea27172d2016-11-01 05:59:29 -07001512 call->DestroyVideoReceiveStream(video_receive_stream);
1513 call->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 06:28:55 -07001514 }
1515
ivica5d6a06c2015-09-17 05:30:24 -07001516 transport.StopSending();
minyue626bc952016-10-31 05:47:02 -07001517 if (params_.audio.enabled)
minyue73208662016-08-18 06:28:55 -07001518 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07001519}
1520
palmkviste75f2042016-09-28 06:19:48 -07001521void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001522 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001523 std::ostringstream str;
1524 str << send_logs_++;
1525 std::string prefix =
minyue626bc952016-10-31 05:47:02 -07001526 params_.video.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07001527 stream->EnableEncodedFrameRecording(
1528 std::vector<rtc::PlatformFile>(
1529 {rtc::CreatePlatformFile(prefix + "1.ivf"),
1530 rtc::CreatePlatformFile(prefix + "2.ivf"),
1531 rtc::CreatePlatformFile(prefix + "3.ivf")}),
1532 10000000);
1533 }
1534}
1535void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001536 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001537 std::ostringstream str;
1538 str << receive_logs_++;
1539 std::string path =
minyue626bc952016-10-31 05:47:02 -07001540 params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07001541 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
1542 10000000);
1543 }
1544}
1545
ivica5d6a06c2015-09-17 05:30:24 -07001546} // namespace webrtc