blob: e2eb9131991df5360d67532ed881c28479664677 [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 */
10#include <stdio.h>
11
12#include <algorithm>
13#include <deque>
14#include <map>
sprangce4aef12015-11-02 07:23:20 -080015#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080016#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070017#include <vector>
18
19#include "testing/gtest/include/gtest/gtest.h"
20
21#include "webrtc/base/checks.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"
sprange1f2f1f2016-02-01 02:04:52 -080024#include "webrtc/base/timeutils.h"
ivica5d6a06c2015-09-17 05:30:24 -070025#include "webrtc/call.h"
26#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010027#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
sprangce4aef12015-11-02 07:23:20 -080028#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010029#include "webrtc/system_wrappers/include/cpu_info.h"
ivica5d6a06c2015-09-17 05:30:24 -070030#include "webrtc/test/layer_filtering_transport.h"
31#include "webrtc/test/run_loop.h"
32#include "webrtc/test/statistics.h"
33#include "webrtc/test/testsupport/fileutils.h"
34#include "webrtc/test/video_renderer.h"
35#include "webrtc/video/video_quality_test.h"
36
37namespace webrtc {
38
39static const int kSendStatsPollingIntervalMs = 1000;
hbosbab934b2016-01-27 01:36:03 -080040static const int kPayloadTypeH264 = 122;
ivica5d6a06c2015-09-17 05:30:24 -070041static const int kPayloadTypeVP8 = 123;
42static const int kPayloadTypeVP9 = 124;
43
44class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -070045 public Transport,
Peter Boströmfb647a62016-03-14 16:58:44 +010046 public I420FrameCallback,
ivica5d6a06c2015-09-17 05:30:24 -070047 public VideoRenderer,
48 public VideoCaptureInput,
Peter Boströme4499152016-02-05 11:13:28 +010049 public EncodedFrameObserver {
ivica5d6a06c2015-09-17 05:30:24 -070050 public:
sprangce4aef12015-11-02 07:23:20 -080051 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -070052 const std::string& test_label,
53 double avg_psnr_threshold,
54 double avg_ssim_threshold,
55 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -080056 FILE* graph_data_output_file,
57 const std::string& graph_title,
58 uint32_t ssrc_to_analyze)
ivica8d15bd62015-10-07 02:43:12 -070059 : input_(nullptr),
ivica5d6a06c2015-09-17 05:30:24 -070060 transport_(transport),
61 receiver_(nullptr),
62 send_stream_(nullptr),
63 test_label_(test_label),
64 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -080065 graph_title_(graph_title),
66 ssrc_to_analyze_(ssrc_to_analyze),
Peter Boströme4499152016-02-05 11:13:28 +010067 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 05:30:24 -070068 frames_to_process_(duration_frames),
69 frames_recorded_(0),
70 frames_processed_(0),
71 dropped_frames_(0),
Peter Boströmfb647a62016-03-14 16:58:44 +010072 dropped_frames_before_first_encode_(0),
73 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -070074 last_render_time_(0),
75 rtp_timestamp_delta_(0),
76 avg_psnr_threshold_(avg_psnr_threshold),
77 avg_ssim_threshold_(avg_ssim_threshold),
Peter Boström8c38e8b2015-11-26 17:45:47 +010078 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +010079 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +010080 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -070081 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
82
83 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
84 // so that we don't accidentally starve "real" worker threads (codec etc).
85 // Also, don't allocate more than kMaxComparisonThreads, even if there are
86 // spare cores.
87
88 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
89 RTC_DCHECK_GE(num_cores, 1u);
90 static const uint32_t kMinCoresLeft = 4;
91 static const uint32_t kMaxComparisonThreads = 8;
92
93 if (num_cores <= kMinCoresLeft) {
94 num_cores = 1;
95 } else {
96 num_cores -= kMinCoresLeft;
97 num_cores = std::min(num_cores, kMaxComparisonThreads);
98 }
99
100 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100101 rtc::PlatformThread* thread =
102 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
103 thread->Start();
104 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700105 }
ivica5d6a06c2015-09-17 05:30:24 -0700106 }
107
108 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100109 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
110 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700111 delete thread;
112 }
113 }
114
115 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
116
117 DeliveryStatus DeliverPacket(MediaType media_type,
118 const uint8_t* packet,
119 size_t length,
120 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700121 // Ignore timestamps of RTCP packets. They're not synchronized with
122 // RTP packet timestamps and so they would confuse wrap_handler_.
123 if (RtpHeaderParser::IsRtcp(packet, length)) {
124 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
125 }
126
sprangce4aef12015-11-02 07:23:20 -0800127 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700128 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800129 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700130 {
131 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800132 int64_t timestamp =
133 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
134 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700135 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
136 }
137
138 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
139 }
140
Peter Boströme4499152016-02-05 11:13:28 +0100141 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700142 rtc::CritScope crit(&comparison_lock_);
143 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
144 }
145
ivica5d6a06c2015-09-17 05:30:24 -0700146 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
147 VideoFrame copy = video_frame;
148 copy.set_timestamp(copy.ntp_time_ms() * 90);
ivica5d6a06c2015-09-17 05:30:24 -0700149 {
150 rtc::CritScope lock(&crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700151 frames_.push_back(copy);
152 }
153
154 input_->IncomingCapturedFrame(video_frame);
155 }
156
Peter Boströmfb647a62016-03-14 16:58:44 +0100157 void FrameCallback(VideoFrame* video_frame) {
158 rtc::CritScope lock(&crit_);
159 if (first_send_frame_.IsZeroSize() && rtp_timestamp_delta_ == 0) {
160 while (frames_.front().timestamp() != video_frame->timestamp()) {
161 ++dropped_frames_before_first_encode_;
162 frames_.pop_front();
163 RTC_CHECK(!frames_.empty());
164 }
165 first_send_frame_ = *video_frame;
166 }
167 }
168
stefan1d8a5062015-10-02 03:39:33 -0700169 bool SendRtp(const uint8_t* packet,
170 size_t length,
171 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800172 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700173 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800174 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700175
sprangce4aef12015-11-02 07:23:20 -0800176 int64_t current_time =
177 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
178 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700179 {
180 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800181
Peter Boströmfb647a62016-03-14 16:58:44 +0100182 if (!first_send_frame_.IsZeroSize()) {
sprang1b3530b2016-03-10 01:32:53 -0800183 rtp_timestamp_delta_ = header.timestamp - first_send_frame_.timestamp();
ivica5d6a06c2015-09-17 05:30:24 -0700184 first_send_frame_.Reset();
185 }
sprang1b3530b2016-03-10 01:32:53 -0800186 int64_t timestamp =
187 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
sprangce4aef12015-11-02 07:23:20 -0800188 send_times_[timestamp] = current_time;
189 if (!transport_->DiscardedLastPacket() &&
190 header.ssrc == ssrc_to_analyze_) {
191 encoded_frame_sizes_[timestamp] +=
192 length - (header.headerLength + header.paddingLength);
193 }
ivica5d6a06c2015-09-17 05:30:24 -0700194 }
sprangce4aef12015-11-02 07:23:20 -0800195 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700196 }
197
198 bool SendRtcp(const uint8_t* packet, size_t length) override {
199 return transport_->SendRtcp(packet, length);
200 }
201
202 void EncodedFrameCallback(const EncodedFrame& frame) override {
203 rtc::CritScope lock(&comparison_lock_);
204 if (frames_recorded_ < frames_to_process_)
205 encoded_frame_size_.AddSample(frame.length_);
206 }
207
nisseeb83a1a2016-03-21 01:27:56 -0700208 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700209 int64_t render_time_ms =
210 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700211
212 rtc::CritScope lock(&crit_);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700213 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800214 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700215
sprang16daaa52016-03-09 01:30:24 -0800216 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
Peter Boströmfb647a62016-03-14 16:58:44 +0100217 if (last_rendered_frame_.IsZeroSize()) {
218 // No previous frame rendered, this one was dropped after sending but
219 // before rendering.
220 ++dropped_frames_before_rendering_;
221 frames_.pop_front();
222 RTC_CHECK(!frames_.empty());
223 continue;
224 }
ivica5d6a06c2015-09-17 05:30:24 -0700225 AddFrameComparison(frames_.front(), last_rendered_frame_, true,
226 render_time_ms);
227 frames_.pop_front();
Peter Boströmfb647a62016-03-14 16:58:44 +0100228 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700229 }
230
231 VideoFrame reference_frame = frames_.front();
232 frames_.pop_front();
233 assert(!reference_frame.IsZeroSize());
sprang16daaa52016-03-09 01:30:24 -0800234 int64_t reference_timestamp =
235 wrap_handler_.Unwrap(reference_frame.timestamp());
236 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800237 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100238 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800239 ++send_timestamp;
240 }
sprang16daaa52016-03-09 01:30:24 -0800241 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700242
243 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
244
245 last_rendered_frame_ = video_frame;
246 }
247
ivica5d6a06c2015-09-17 05:30:24 -0700248 void Wait() {
249 // Frame comparisons can be very expensive. Wait for test to be done, but
250 // at time-out check if frames_processed is going up. If so, give it more
251 // time, otherwise fail. Hopefully this will reduce test flakiness.
252
Peter Boström8c38e8b2015-11-26 17:45:47 +0100253 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800254
ivica5d6a06c2015-09-17 05:30:24 -0700255 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700256 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100257 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700258 int frames_processed;
259 {
260 rtc::CritScope crit(&comparison_lock_);
261 frames_processed = frames_processed_;
262 }
263
264 // Print some output so test infrastructure won't think we've crashed.
265 const char* kKeepAliveMessages[3] = {
266 "Uh, I'm-I'm not quite dead, sir.",
267 "Uh, I-I think uh, I could pull through, sir.",
268 "Actually, I think I'm all right to come with you--"};
269 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
270
271 if (last_frames_processed == -1) {
272 last_frames_processed = frames_processed;
273 continue;
274 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100275 if (frames_processed == last_frames_processed) {
276 EXPECT_GT(frames_processed, last_frames_processed)
277 << "Analyzer stalled while waiting for test to finish.";
278 done_.Set();
279 break;
280 }
ivica5d6a06c2015-09-17 05:30:24 -0700281 last_frames_processed = frames_processed;
282 }
283
284 if (iteration > 0)
285 printf("- Farewell, sweet Concorde!\n");
286
Peter Boström8c38e8b2015-11-26 17:45:47 +0100287 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700288 }
289
Peter Boströme4499152016-02-05 11:13:28 +0100290 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
291
ivica5d6a06c2015-09-17 05:30:24 -0700292 VideoCaptureInput* input_;
sprangce4aef12015-11-02 07:23:20 -0800293 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700294 PacketReceiver* receiver_;
295 VideoSendStream* send_stream_;
296
297 private:
298 struct FrameComparison {
299 FrameComparison()
300 : dropped(false),
301 send_time_ms(0),
302 recv_time_ms(0),
303 render_time_ms(0),
304 encoded_frame_size(0) {}
305
306 FrameComparison(const VideoFrame& reference,
307 const VideoFrame& render,
308 bool dropped,
309 int64_t send_time_ms,
310 int64_t recv_time_ms,
311 int64_t render_time_ms,
312 size_t encoded_frame_size)
313 : reference(reference),
314 render(render),
315 dropped(dropped),
316 send_time_ms(send_time_ms),
317 recv_time_ms(recv_time_ms),
318 render_time_ms(render_time_ms),
319 encoded_frame_size(encoded_frame_size) {}
320
321 VideoFrame reference;
322 VideoFrame render;
323 bool dropped;
324 int64_t send_time_ms;
325 int64_t recv_time_ms;
326 int64_t render_time_ms;
327 size_t encoded_frame_size;
328 };
329
330 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700331 Sample(int dropped,
332 int64_t input_time_ms,
333 int64_t send_time_ms,
334 int64_t recv_time_ms,
335 int64_t render_time_ms,
336 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700337 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700338 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700339 : dropped(dropped),
340 input_time_ms(input_time_ms),
341 send_time_ms(send_time_ms),
342 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700343 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700344 encoded_frame_size(encoded_frame_size),
345 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700346 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700347
ivica8d15bd62015-10-07 02:43:12 -0700348 int dropped;
349 int64_t input_time_ms;
350 int64_t send_time_ms;
351 int64_t recv_time_ms;
352 int64_t render_time_ms;
353 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700354 double psnr;
355 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700356 };
357
Peter Boströme4499152016-02-05 11:13:28 +0100358 // This class receives the send-side OnEncodeTiming and is provided to not
359 // conflict with the receiver-side pre_decode_callback.
360 class OnEncodeTimingProxy : public EncodedFrameObserver {
361 public:
362 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
363
364 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
365 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
366 }
367 void EncodedFrameCallback(const EncodedFrame& frame) override {}
368
369 private:
370 VideoAnalyzer* const parent_;
371 };
372
ivica5d6a06c2015-09-17 05:30:24 -0700373 void AddFrameComparison(const VideoFrame& reference,
374 const VideoFrame& render,
375 bool dropped,
376 int64_t render_time_ms)
377 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
Peter Boströmfb647a62016-03-14 16:58:44 +0100378 RTC_DCHECK(!render.IsZeroSize());
sprange1f2f1f2016-02-01 02:04:52 -0800379 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
380 int64_t send_time_ms = send_times_[reference_timestamp];
381 send_times_.erase(reference_timestamp);
382 int64_t recv_time_ms = recv_times_[reference_timestamp];
383 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700384
sprangce4aef12015-11-02 07:23:20 -0800385 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800386 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800387 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800388 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800389 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
390 if (it != encoded_frame_sizes_.end())
391 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700392
393 VideoFrame reference_copy;
394 VideoFrame render_copy;
395 reference_copy.CopyFrame(reference);
396 render_copy.CopyFrame(render);
397
398 rtc::CritScope crit(&comparison_lock_);
399 comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped,
400 send_time_ms, recv_time_ms,
401 render_time_ms, encoded_size));
Peter Boström5811a392015-12-10 13:02:50 +0100402 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700403 }
404
405 static bool PollStatsThread(void* obj) {
406 return static_cast<VideoAnalyzer*>(obj)->PollStats();
407 }
408
409 bool PollStats() {
Peter Boströmdd45eb62016-01-19 15:22:32 +0100410 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 13:02:50 +0100411 return false;
ivica5d6a06c2015-09-17 05:30:24 -0700412
413 VideoSendStream::Stats stats = send_stream_->GetStats();
414
415 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 17:30:49 +0100416 // It's not certain that we yet have estimates for any of these stats. Check
417 // that they are positive before mixing them in.
418 if (stats.encode_frame_rate > 0)
419 encode_frame_rate_.AddSample(stats.encode_frame_rate);
420 if (stats.avg_encode_time_ms > 0)
421 encode_time_ms.AddSample(stats.avg_encode_time_ms);
422 if (stats.encode_usage_percent > 0)
423 encode_usage_percent.AddSample(stats.encode_usage_percent);
424 if (stats.media_bitrate_bps > 0)
425 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 05:30:24 -0700426
427 return true;
428 }
429
430 static bool FrameComparisonThread(void* obj) {
431 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
432 }
433
434 bool CompareFrames() {
435 if (AllFramesRecorded())
436 return false;
437
438 VideoFrame reference;
439 VideoFrame render;
440 FrameComparison comparison;
441
442 if (!PopComparison(&comparison)) {
443 // Wait until new comparison task is available, or test is done.
444 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100445 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700446 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100447 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700448 return false;
449 }
450 return true; // Try again.
451 }
452
453 PerformFrameComparison(comparison);
454
455 if (FrameProcessed()) {
456 PrintResults();
457 if (graph_data_output_file_)
458 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100459 done_.Set();
460 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700461 return false;
462 }
463
464 return true;
465 }
466
467 bool PopComparison(FrameComparison* comparison) {
468 rtc::CritScope crit(&comparison_lock_);
469 // If AllFramesRecorded() is true, it means we have already popped
470 // frames_to_process_ frames from comparisons_, so there is no more work
471 // for this thread to be done. frames_processed_ might still be lower if
472 // all comparisons are not done, but those frames are currently being
473 // worked on by other threads.
474 if (comparisons_.empty() || AllFramesRecorded())
475 return false;
476
477 *comparison = comparisons_.front();
478 comparisons_.pop_front();
479
480 FrameRecorded();
481 return true;
482 }
483
484 // Increment counter for number of frames received for comparison.
485 void FrameRecorded() {
486 rtc::CritScope crit(&comparison_lock_);
487 ++frames_recorded_;
488 }
489
490 // Returns true if all frames to be compared have been taken from the queue.
491 bool AllFramesRecorded() {
492 rtc::CritScope crit(&comparison_lock_);
493 assert(frames_recorded_ <= frames_to_process_);
494 return frames_recorded_ == frames_to_process_;
495 }
496
497 // Increase count of number of frames processed. Returns true if this was the
498 // last frame to be processed.
499 bool FrameProcessed() {
500 rtc::CritScope crit(&comparison_lock_);
501 ++frames_processed_;
502 assert(frames_processed_ <= frames_to_process_);
503 return frames_processed_ == frames_to_process_;
504 }
505
506 void PrintResults() {
507 rtc::CritScope crit(&comparison_lock_);
508 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800509 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700510 PrintResult("sender_time", sender_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700511 PrintResult("receiver_time", receiver_time_, " ms");
512 PrintResult("total_delay_incl_network", end_to_end_, " ms");
513 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
514 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
515 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
516 PrintResult("encode_time", encode_time_ms, " ms");
517 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
518 PrintResult("media_bitrate", media_bitrate_bps, " bps");
519
Peter Boströmfb647a62016-03-14 16:58:44 +0100520 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
521 dropped_frames_);
522 printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n",
523 test_label_.c_str(), dropped_frames_before_first_encode_);
524 printf("RESULT dropped_frames_before_rendering: %s = %d frames\n",
525 test_label_.c_str(), dropped_frames_before_rendering_);
526
ivica5d6a06c2015-09-17 05:30:24 -0700527 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
528 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
529 }
530
531 void PerformFrameComparison(const FrameComparison& comparison) {
532 // Perform expensive psnr and ssim calculations while not holding lock.
533 double psnr = I420PSNR(&comparison.reference, &comparison.render);
534 double ssim = I420SSIM(&comparison.reference, &comparison.render);
535
536 int64_t input_time_ms = comparison.reference.ntp_time_ms();
537
538 rtc::CritScope crit(&comparison_lock_);
539 if (graph_data_output_file_) {
540 samples_.push_back(
541 Sample(comparison.dropped, input_time_ms, comparison.send_time_ms,
ivica8d15bd62015-10-07 02:43:12 -0700542 comparison.recv_time_ms, comparison.render_time_ms,
543 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700544 }
545 psnr_.AddSample(psnr);
546 ssim_.AddSample(ssim);
547
548 if (comparison.dropped) {
549 ++dropped_frames_;
550 return;
551 }
552 if (last_render_time_ != 0)
553 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
554 last_render_time_ = comparison.render_time_ms;
555
556 sender_time_.AddSample(comparison.send_time_ms - input_time_ms);
557 receiver_time_.AddSample(comparison.render_time_ms -
558 comparison.recv_time_ms);
559 end_to_end_.AddSample(comparison.render_time_ms - input_time_ms);
560 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
561 }
562
563 void PrintResult(const char* result_type,
564 test::Statistics stats,
565 const char* unit) {
566 printf("RESULT %s: %s = {%f, %f}%s\n",
567 result_type,
568 test_label_.c_str(),
569 stats.Mean(),
570 stats.StandardDeviation(),
571 unit);
572 }
573
574 void PrintSamplesToFile(void) {
575 FILE* out = graph_data_output_file_;
576 rtc::CritScope crit(&comparison_lock_);
577 std::sort(samples_.begin(), samples_.end(),
578 [](const Sample& A, const Sample& B) -> bool {
579 return A.input_time_ms < B.input_time_ms;
580 });
581
sprangce4aef12015-11-02 07:23:20 -0800582 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700583 fprintf(out, "%" PRIuS "\n", samples_.size());
584 fprintf(out,
585 "dropped "
586 "input_time_ms "
587 "send_time_ms "
588 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700589 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700590 "encoded_frame_size "
591 "psnr "
592 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700593 "encode_time_ms\n");
594 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700595 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700596 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
597 int encode_time_ms;
598 if (it != samples_encode_time_ms_.end()) {
599 encode_time_ms = it->second;
600 } else {
601 ++missing_encode_time_samples;
602 encode_time_ms = -1;
603 }
604 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
605 " %lf %lf %d\n",
606 sample.dropped, sample.input_time_ms, sample.send_time_ms,
607 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700608 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700609 encode_time_ms);
610 }
611 if (missing_encode_time_samples) {
612 fprintf(stderr,
613 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
614 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700615 }
616 }
617
618 const std::string test_label_;
619 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800620 const std::string graph_title_;
621 const uint32_t ssrc_to_analyze_;
Peter Boströme4499152016-02-05 11:13:28 +0100622 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700623 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700624 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700625 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
626 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
627 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
628 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
629 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
630 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
631 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
632 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
633 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
634 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
635 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
636
637 const int frames_to_process_;
638 int frames_recorded_;
639 int frames_processed_;
640 int dropped_frames_;
Peter Boströmfb647a62016-03-14 16:58:44 +0100641 int dropped_frames_before_first_encode_;
642 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -0700643 int64_t last_render_time_;
644 uint32_t rtp_timestamp_delta_;
645
646 rtc::CriticalSection crit_;
647 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
648 VideoFrame last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800649 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
650 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
651 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
652 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700653 VideoFrame first_send_frame_ GUARDED_BY(crit_);
654 const double avg_psnr_threshold_;
655 const double avg_ssim_threshold_;
656
657 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100658 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
659 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +0100660 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -0700661 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +0100662 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -0700663};
664
ivica87f83a92015-10-08 05:13:32 -0700665VideoQualityTest::VideoQualityTest() : clock_(Clock::GetRealTimeClock()) {}
ivica5d6a06c2015-09-17 05:30:24 -0700666
667void VideoQualityTest::TestBody() {}
668
sprangce4aef12015-11-02 07:23:20 -0800669std::string VideoQualityTest::GenerateGraphTitle() const {
670 std::stringstream ss;
671 ss << params_.common.codec;
672 ss << " (" << params_.common.target_bitrate_bps / 1000 << "kbps";
673 ss << ", " << params_.common.fps << " FPS";
674 if (params_.screenshare.scroll_duration)
675 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
676 if (params_.ss.streams.size() > 1)
677 ss << ", Stream #" << params_.ss.selected_stream;
678 if (params_.ss.num_spatial_layers > 1)
679 ss << ", Layer #" << params_.ss.selected_sl;
680 ss << ")";
681 return ss.str();
682}
683
684void VideoQualityTest::CheckParams() {
685 // Add a default stream in none specified.
686 if (params_.ss.streams.empty())
687 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
688 if (params_.ss.num_spatial_layers == 0)
689 params_.ss.num_spatial_layers = 1;
690
691 if (params_.pipe.loss_percent != 0 ||
692 params_.pipe.queue_length_packets != 0) {
693 // Since LayerFilteringTransport changes the sequence numbers, we can't
694 // use that feature with pack loss, since the NACK request would end up
695 // retransmitting the wrong packets.
696 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800697 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800698 RTC_CHECK(params_.common.selected_tl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800699 params_.common.selected_tl ==
700 params_.common.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800701 }
702
703 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
704 // does in some parts of the code?
705 RTC_CHECK_GE(params_.common.max_bitrate_bps,
706 params_.common.target_bitrate_bps);
707 RTC_CHECK_GE(params_.common.target_bitrate_bps,
708 params_.common.min_bitrate_bps);
709 RTC_CHECK_LT(params_.common.selected_tl, params_.common.num_temporal_layers);
710 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
711 for (const VideoStream& stream : params_.ss.streams) {
712 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
713 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
714 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
715 RTC_CHECK_EQ(static_cast<int>(stream.temporal_layer_thresholds_bps.size()),
716 params_.common.num_temporal_layers - 1);
717 }
718 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
719 // the total bitrate? We anyway have to update them in the case bitrate
720 // estimator changes the total bitrates.
721 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
722 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
723 RTC_CHECK(params_.ss.spatial_layers.empty() ||
724 params_.ss.spatial_layers.size() ==
725 static_cast<size_t>(params_.ss.num_spatial_layers));
726 if (params_.common.codec == "VP8") {
727 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
728 } else if (params_.common.codec == "VP9") {
729 RTC_CHECK_EQ(params_.ss.streams.size(), 1u);
730 }
731}
732
733// Static.
734std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
735 // Parse comma separated nonnegative integers, where some elements may be
736 // empty. The empty values are replaced with -1.
737 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
738 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
739 std::vector<int> result;
740 if (str.empty())
741 return result;
742
743 const char* p = str.c_str();
744 int value = -1;
745 int pos;
746 while (*p) {
747 if (*p == ',') {
748 result.push_back(value);
749 value = -1;
750 ++p;
751 continue;
752 }
753 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
754 << "Unexpected non-number value.";
755 p += pos;
756 }
757 result.push_back(value);
758 return result;
759}
760
761// Static.
762VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
763 VideoStream stream;
764 stream.width = params.common.width;
765 stream.height = params.common.height;
766 stream.max_framerate = params.common.fps;
767 stream.min_bitrate_bps = params.common.min_bitrate_bps;
768 stream.target_bitrate_bps = params.common.target_bitrate_bps;
769 stream.max_bitrate_bps = params.common.max_bitrate_bps;
770 stream.max_qp = 52;
771 if (params.common.num_temporal_layers == 2)
772 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
773 return stream;
774}
775
776// Static.
777void VideoQualityTest::FillScalabilitySettings(
778 Params* params,
779 const std::vector<std::string>& stream_descriptors,
780 size_t selected_stream,
781 int num_spatial_layers,
782 int selected_sl,
783 const std::vector<std::string>& sl_descriptors) {
784 // Read VideoStream and SpatialLayer elements from a list of comma separated
785 // lists. To use a default value for an element, use -1 or leave empty.
786 // Validity checks performed in CheckParams.
787
788 RTC_CHECK(params->ss.streams.empty());
789 for (auto descriptor : stream_descriptors) {
790 if (descriptor.empty())
791 continue;
792 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
793 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
794 if (v[0] != -1)
795 stream.width = static_cast<size_t>(v[0]);
796 if (v[1] != -1)
797 stream.height = static_cast<size_t>(v[1]);
798 if (v[2] != -1)
799 stream.max_framerate = v[2];
800 if (v[3] != -1)
801 stream.min_bitrate_bps = v[3];
802 if (v[4] != -1)
803 stream.target_bitrate_bps = v[4];
804 if (v[5] != -1)
805 stream.max_bitrate_bps = v[5];
806 if (v.size() > 6 && v[6] != -1)
807 stream.max_qp = v[6];
808 if (v.size() > 7) {
809 stream.temporal_layer_thresholds_bps.clear();
810 stream.temporal_layer_thresholds_bps.insert(
811 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
812 } else {
813 // Automatic TL thresholds for more than two layers not supported.
814 RTC_CHECK_LE(params->common.num_temporal_layers, 2);
815 }
816 params->ss.streams.push_back(stream);
817 }
818 params->ss.selected_stream = selected_stream;
819
820 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
821 params->ss.selected_sl = selected_sl;
822 RTC_CHECK(params->ss.spatial_layers.empty());
823 for (auto descriptor : sl_descriptors) {
824 if (descriptor.empty())
825 continue;
826 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
827 RTC_CHECK_GT(v[2], 0);
828
829 SpatialLayer layer;
830 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
831 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
832 layer.target_bitrate_bps = v[2];
833 params->ss.spatial_layers.push_back(layer);
834 }
835}
836
837void VideoQualityTest::SetupCommon(Transport* send_transport,
838 Transport* recv_transport) {
839 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -0700840 trace_to_stderr_.reset(new test::TraceToStderr);
841
sprangce4aef12015-11-02 07:23:20 -0800842 size_t num_streams = params_.ss.streams.size();
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100843 CreateSendConfig(num_streams, 0, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -0700844
845 int payload_type;
hbosbab934b2016-01-27 01:36:03 -0800846 if (params_.common.codec == "H264") {
847 encoder_.reset(VideoEncoder::Create(VideoEncoder::kH264));
848 payload_type = kPayloadTypeH264;
849 } else if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700850 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp8));
851 payload_type = kPayloadTypeVP8;
sprangce4aef12015-11-02 07:23:20 -0800852 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700853 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp9));
854 payload_type = kPayloadTypeVP9;
855 } else {
856 RTC_NOTREACHED() << "Codec not supported!";
857 return;
858 }
stefanff483612015-12-21 03:14:00 -0800859 video_send_config_.encoder_settings.encoder = encoder_.get();
860 video_send_config_.encoder_settings.payload_name = params_.common.codec;
861 video_send_config_.encoder_settings.payload_type = payload_type;
862 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
863 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
sprangce4aef12015-11-02 07:23:20 -0800864 for (size_t i = 0; i < num_streams; ++i)
stefanff483612015-12-21 03:14:00 -0800865 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -0700866
stefanff483612015-12-21 03:14:00 -0800867 video_send_config_.rtp.extensions.clear();
sprangce4aef12015-11-02 07:23:20 -0800868 if (params_.common.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -0800869 video_send_config_.rtp.extensions.push_back(
Stefan Holmer12952972015-10-29 15:13:24 +0100870 RtpExtension(RtpExtension::kTransportSequenceNumber,
871 test::kTransportSequenceNumberExtensionId));
872 } else {
stefanff483612015-12-21 03:14:00 -0800873 video_send_config_.rtp.extensions.push_back(RtpExtension(
Stefan Holmer12952972015-10-29 15:13:24 +0100874 RtpExtension::kAbsSendTime, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +0200875 }
876
stefanff483612015-12-21 03:14:00 -0800877 video_encoder_config_.min_transmit_bitrate_bps =
878 params_.common.min_transmit_bps;
879 video_encoder_config_.streams = params_.ss.streams;
880 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -0700881
882 CreateMatchingReceiveConfigs(recv_transport);
883
sprangce4aef12015-11-02 07:23:20 -0800884 for (size_t i = 0; i < num_streams; ++i) {
stefanff483612015-12-21 03:14:00 -0800885 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 13:29:59 +0100886 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
887 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 07:23:20 -0800888 kSendRtxPayloadType;
stefanff483612015-12-21 03:14:00 -0800889 video_receive_configs_[i].rtp.transport_cc = params_.common.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -0800890 }
ivica5d6a06c2015-09-17 05:30:24 -0700891}
892
sprangce4aef12015-11-02 07:23:20 -0800893void VideoQualityTest::SetupScreenshare() {
894 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 05:30:24 -0700895
896 // Fill out codec settings.
stefanff483612015-12-21 03:14:00 -0800897 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
sprangce4aef12015-11-02 07:23:20 -0800898 if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700899 codec_settings_.VP8 = VideoEncoder::GetDefaultVp8Settings();
900 codec_settings_.VP8.denoisingOn = false;
901 codec_settings_.VP8.frameDroppingOn = false;
902 codec_settings_.VP8.numberOfTemporalLayers =
sprangce4aef12015-11-02 07:23:20 -0800903 static_cast<unsigned char>(params_.common.num_temporal_layers);
stefanff483612015-12-21 03:14:00 -0800904 video_encoder_config_.encoder_specific_settings = &codec_settings_.VP8;
sprangce4aef12015-11-02 07:23:20 -0800905 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700906 codec_settings_.VP9 = VideoEncoder::GetDefaultVp9Settings();
907 codec_settings_.VP9.denoisingOn = false;
908 codec_settings_.VP9.frameDroppingOn = false;
909 codec_settings_.VP9.numberOfTemporalLayers =
sprangce4aef12015-11-02 07:23:20 -0800910 static_cast<unsigned char>(params_.common.num_temporal_layers);
stefanff483612015-12-21 03:14:00 -0800911 video_encoder_config_.encoder_specific_settings = &codec_settings_.VP9;
sprangce4aef12015-11-02 07:23:20 -0800912 codec_settings_.VP9.numberOfSpatialLayers =
913 static_cast<unsigned char>(params_.ss.num_spatial_layers);
ivica5d6a06c2015-09-17 05:30:24 -0700914 }
915
916 // Setup frame generator.
917 const size_t kWidth = 1850;
918 const size_t kHeight = 1110;
919 std::vector<std::string> slides;
920 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
921 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
922 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
923 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
924
sprangce4aef12015-11-02 07:23:20 -0800925 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 05:30:24 -0700926 // Cycle image every slide_change_interval seconds.
927 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
928 slides, kWidth, kHeight,
sprangce4aef12015-11-02 07:23:20 -0800929 params_.screenshare.slide_change_interval * params_.common.fps));
ivica5d6a06c2015-09-17 05:30:24 -0700930 } else {
sprangce4aef12015-11-02 07:23:20 -0800931 RTC_CHECK_LE(params_.common.width, kWidth);
932 RTC_CHECK_LE(params_.common.height, kHeight);
933 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
934 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
935 params_.screenshare.scroll_duration) *
936 1000;
937 RTC_CHECK_LE(params_.screenshare.scroll_duration,
938 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 05:30:24 -0700939
sprangce4aef12015-11-02 07:23:20 -0800940 frame_generator_.reset(
941 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
942 clock_, slides, kWidth, kHeight, params_.common.width,
943 params_.common.height, params_.screenshare.scroll_duration * 1000,
944 kPauseDurationMs));
ivica5d6a06c2015-09-17 05:30:24 -0700945 }
946}
947
sprangce4aef12015-11-02 07:23:20 -0800948void VideoQualityTest::CreateCapturer(VideoCaptureInput* input) {
949 if (params_.screenshare.enabled) {
950 test::FrameGeneratorCapturer* frame_generator_capturer =
ivica2d4e6c52015-09-23 01:57:06 -0700951 new test::FrameGeneratorCapturer(
sprangce4aef12015-11-02 07:23:20 -0800952 clock_, input, frame_generator_.release(), params_.common.fps);
ivica2d4e6c52015-09-23 01:57:06 -0700953 EXPECT_TRUE(frame_generator_capturer->Init());
954 capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -0700955 } else {
sprangce4aef12015-11-02 07:23:20 -0800956 if (params_.video.clip_name.empty()) {
957 capturer_.reset(test::VideoCapturer::Create(input, params_.common.width,
958 params_.common.height,
959 params_.common.fps, clock_));
ivica5d6a06c2015-09-17 05:30:24 -0700960 } else {
ivica2d4e6c52015-09-23 01:57:06 -0700961 capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
sprangce4aef12015-11-02 07:23:20 -0800962 input, test::ResourcePath(params_.video.clip_name, "yuv"),
963 params_.common.width, params_.common.height, params_.common.fps,
ivica2d4e6c52015-09-23 01:57:06 -0700964 clock_));
965 ASSERT_TRUE(capturer_.get() != nullptr)
sprangce4aef12015-11-02 07:23:20 -0800966 << "Could not create capturer for " << params_.video.clip_name
ivica5d6a06c2015-09-17 05:30:24 -0700967 << ".yuv. Is this resource file present?";
968 }
969 }
970}
971
sprang7a975f72015-10-12 06:33:21 -0700972void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -0800973 params_ = params;
974
ivica5d6a06c2015-09-17 05:30:24 -0700975 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
976 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -0800977 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -0700978
979 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -0800980 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -0700981 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -0800982 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
ivica5d6a06c2015-09-17 05:30:24 -0700983 RTC_CHECK(graph_data_output_file != nullptr)
sprangce4aef12015-11-02 07:23:20 -0800984 << "Can't open the file " << params_.analyzer.graph_data_output_filename
985 << "!";
ivica87f83a92015-10-08 05:13:32 -0700986 }
sprang7a975f72015-10-12 06:33:21 -0700987
stefanf116bd02015-10-27 08:29:42 -0700988 Call::Config call_config;
989 call_config.bitrate_config = params.common.call_bitrate_config;
990 CreateCalls(call_config, call_config);
991
ivica87f83a92015-10-08 05:13:32 -0700992 test::LayerFilteringTransport send_transport(
stefanf116bd02015-10-27 08:29:42 -0700993 params.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -0800994 params.common.selected_tl, params_.ss.selected_sl);
stefanf116bd02015-10-27 08:29:42 -0700995 test::DirectTransport recv_transport(params.pipe, receiver_call_.get());
996
sprangce4aef12015-11-02 07:23:20 -0800997 std::string graph_title = params_.analyzer.graph_title;
998 if (graph_title.empty())
999 graph_title = VideoQualityTest::GenerateGraphTitle();
1000
1001 // In the case of different resolutions, the functions calculating PSNR and
1002 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
1003 // aborts if the average psnr/ssim are below the given threshold, which is
1004 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
1005 // abort.
1006 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
1007 int selected_sl = params_.ss.selected_sl != -1
1008 ? params_.ss.selected_sl
1009 : params_.ss.num_spatial_layers - 1;
1010 bool disable_quality_check =
1011 selected_stream.width != params_.common.width ||
1012 selected_stream.height != params_.common.height ||
1013 (!params_.ss.spatial_layers.empty() &&
1014 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
1015 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
1016 if (disable_quality_check) {
1017 fprintf(stderr,
1018 "Warning: Calculating PSNR and SSIM for downsized resolution "
1019 "not implemented yet! Skipping PSNR and SSIM calculations!");
1020 }
1021
ivica5d6a06c2015-09-17 05:30:24 -07001022 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001023 &send_transport, params_.analyzer.test_label,
1024 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
1025 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
1026 params_.analyzer.test_durations_secs * params_.common.fps,
1027 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001028 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 05:30:24 -07001029
ivica5d6a06c2015-09-17 05:30:24 -07001030 analyzer.SetReceiver(receiver_call_->Receiver());
1031 send_transport.SetReceiver(&analyzer);
1032 recv_transport.SetReceiver(sender_call_->Receiver());
1033
sprangce4aef12015-11-02 07:23:20 -08001034 SetupCommon(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001035 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
Peter Boströmfb647a62016-03-14 16:58:44 +01001036 video_send_config_.pre_encode_callback = &analyzer;
stefanff483612015-12-21 03:14:00 -08001037 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 05:30:24 -07001038 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 11:13:28 +01001039 RTC_DCHECK(!video_send_config_.post_encode_callback);
1040 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001041
sprangce4aef12015-11-02 07:23:20 -08001042 if (params_.screenshare.enabled)
1043 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001044
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001045 CreateVideoStreams();
stefanff483612015-12-21 03:14:00 -08001046 analyzer.input_ = video_send_stream_->Input();
1047 analyzer.send_stream_ = video_send_stream_;
ivica5d6a06c2015-09-17 05:30:24 -07001048
sprangce4aef12015-11-02 07:23:20 -08001049 CreateCapturer(&analyzer);
ivicac1cc8542015-10-08 03:44:06 -07001050
stefanff483612015-12-21 03:14:00 -08001051 video_send_stream_->Start();
1052 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1053 receive_stream->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001054 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001055
1056 analyzer.Wait();
1057
1058 send_transport.StopSending();
1059 recv_transport.StopSending();
1060
ivica2d4e6c52015-09-23 01:57:06 -07001061 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001062 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1063 receive_stream->Stop();
1064 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001065
1066 DestroyStreams();
1067
1068 if (graph_data_output_file)
1069 fclose(graph_data_output_file);
1070}
1071
sprang7a975f72015-10-12 06:33:21 -07001072void VideoQualityTest::RunWithVideoRenderer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001073 params_ = params;
1074 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001075
kwiberg27f982b2016-03-01 11:52:33 -08001076 std::unique_ptr<test::VideoRenderer> local_preview(
sprangce4aef12015-11-02 07:23:20 -08001077 test::VideoRenderer::Create("Local Preview", params_.common.width,
1078 params_.common.height));
1079 size_t stream_id = params_.ss.selected_stream;
mflodmand1590b22015-12-09 07:07:59 -08001080 std::string title = "Loopback Video";
1081 if (params_.ss.streams.size() > 1) {
1082 std::ostringstream s;
1083 s << stream_id;
1084 title += " - Stream #" + s.str();
sprangce4aef12015-11-02 07:23:20 -08001085 }
mflodmand1590b22015-12-09 07:07:59 -08001086
kwiberg27f982b2016-03-01 11:52:33 -08001087 std::unique_ptr<test::VideoRenderer> loopback_video(
mflodmand1590b22015-12-09 07:07:59 -08001088 test::VideoRenderer::Create(title.c_str(),
1089 params_.ss.streams[stream_id].width,
sprangce4aef12015-11-02 07:23:20 -08001090 params_.ss.streams[stream_id].height));
ivica5d6a06c2015-09-17 05:30:24 -07001091
1092 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1093 // match the full stack tests.
1094 Call::Config call_config;
sprangce4aef12015-11-02 07:23:20 -08001095 call_config.bitrate_config = params_.common.call_bitrate_config;
kwiberg27f982b2016-03-01 11:52:33 -08001096 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001097
1098 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001099 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -08001100 params.common.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001101 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1102 // least share as much code as possible. That way this test would also match
1103 // the full stack tests better.
1104 transport.SetReceiver(call->Receiver());
1105
sprangce4aef12015-11-02 07:23:20 -08001106 SetupCommon(&transport, &transport);
ivica87f83a92015-10-08 05:13:32 -07001107
stefanff483612015-12-21 03:14:00 -08001108 video_send_config_.local_renderer = local_preview.get();
1109 video_receive_configs_[stream_id].renderer = loopback_video.get();
sprangce4aef12015-11-02 07:23:20 -08001110
1111 if (params_.screenshare.enabled)
1112 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001113
stefanff483612015-12-21 03:14:00 -08001114 video_send_stream_ =
1115 call->CreateVideoSendStream(video_send_config_, video_encoder_config_);
ivica5d6a06c2015-09-17 05:30:24 -07001116 VideoReceiveStream* receive_stream =
stefanff483612015-12-21 03:14:00 -08001117 call->CreateVideoReceiveStream(video_receive_configs_[stream_id]);
1118 CreateCapturer(video_send_stream_->Input());
ivica5d6a06c2015-09-17 05:30:24 -07001119
1120 receive_stream->Start();
stefanff483612015-12-21 03:14:00 -08001121 video_send_stream_->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001122 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001123
1124 test::PressEnterToContinue();
1125
ivica2d4e6c52015-09-23 01:57:06 -07001126 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001127 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001128 receive_stream->Stop();
1129
1130 call->DestroyVideoReceiveStream(receive_stream);
stefanff483612015-12-21 03:14:00 -08001131 call->DestroyVideoSendStream(video_send_stream_);
ivica5d6a06c2015-09-17 05:30:24 -07001132
1133 transport.StopSending();
1134}
1135
1136} // namespace webrtc