blob: 60f957c456ab2312773e551fe7e7a9bef0e0fc51 [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,
nisse7ade7b32016-03-23 04:48:10 -070046 public rtc::VideoSinkInterface<VideoFrame>,
ivica5d6a06c2015-09-17 05:30:24 -070047 public VideoCaptureInput,
Peter Boströme4499152016-02-05 11:13:28 +010048 public EncodedFrameObserver {
ivica5d6a06c2015-09-17 05:30:24 -070049 public:
sprangce4aef12015-11-02 07:23:20 -080050 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -070051 const std::string& test_label,
52 double avg_psnr_threshold,
53 double avg_ssim_threshold,
54 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -080055 FILE* graph_data_output_file,
56 const std::string& graph_title,
57 uint32_t ssrc_to_analyze)
ivica8d15bd62015-10-07 02:43:12 -070058 : input_(nullptr),
ivica5d6a06c2015-09-17 05:30:24 -070059 transport_(transport),
60 receiver_(nullptr),
61 send_stream_(nullptr),
62 test_label_(test_label),
63 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -080064 graph_title_(graph_title),
65 ssrc_to_analyze_(ssrc_to_analyze),
Peter Boströme4499152016-02-05 11:13:28 +010066 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 05:30:24 -070067 frames_to_process_(duration_frames),
68 frames_recorded_(0),
69 frames_processed_(0),
70 dropped_frames_(0),
71 last_render_time_(0),
72 rtp_timestamp_delta_(0),
73 avg_psnr_threshold_(avg_psnr_threshold),
74 avg_ssim_threshold_(avg_ssim_threshold),
Peter Boström8c38e8b2015-11-26 17:45:47 +010075 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +010076 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +010077 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -070078 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
79
80 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
81 // so that we don't accidentally starve "real" worker threads (codec etc).
82 // Also, don't allocate more than kMaxComparisonThreads, even if there are
83 // spare cores.
84
85 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
86 RTC_DCHECK_GE(num_cores, 1u);
87 static const uint32_t kMinCoresLeft = 4;
88 static const uint32_t kMaxComparisonThreads = 8;
89
90 if (num_cores <= kMinCoresLeft) {
91 num_cores = 1;
92 } else {
93 num_cores -= kMinCoresLeft;
94 num_cores = std::min(num_cores, kMaxComparisonThreads);
95 }
96
97 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +010098 rtc::PlatformThread* thread =
99 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
100 thread->Start();
101 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700102 }
ivica5d6a06c2015-09-17 05:30:24 -0700103 }
104
105 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100106 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
107 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700108 delete thread;
109 }
110 }
111
112 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
113
114 DeliveryStatus DeliverPacket(MediaType media_type,
115 const uint8_t* packet,
116 size_t length,
117 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700118 // Ignore timestamps of RTCP packets. They're not synchronized with
119 // RTP packet timestamps and so they would confuse wrap_handler_.
120 if (RtpHeaderParser::IsRtcp(packet, length)) {
121 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
122 }
123
sprangce4aef12015-11-02 07:23:20 -0800124 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700125 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800126 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700127 {
128 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800129 int64_t timestamp =
130 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
131 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700132 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
133 }
134
135 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
136 }
137
Peter Boströme4499152016-02-05 11:13:28 +0100138 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700139 rtc::CritScope crit(&comparison_lock_);
140 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
141 }
142
ivica5d6a06c2015-09-17 05:30:24 -0700143 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
144 VideoFrame copy = video_frame;
145 copy.set_timestamp(copy.ntp_time_ms() * 90);
Peter Boström81cbd922016-03-22 12:19:07 +0100146
ivica5d6a06c2015-09-17 05:30:24 -0700147 {
148 rtc::CritScope lock(&crit_);
Peter Boström81cbd922016-03-22 12:19:07 +0100149 if (first_send_frame_.IsZeroSize() && rtp_timestamp_delta_ == 0)
150 first_send_frame_ = copy;
151
ivica5d6a06c2015-09-17 05:30:24 -0700152 frames_.push_back(copy);
153 }
154
155 input_->IncomingCapturedFrame(video_frame);
156 }
157
stefan1d8a5062015-10-02 03:39:33 -0700158 bool SendRtp(const uint8_t* packet,
159 size_t length,
160 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800161 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700162 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800163 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700164
sprangce4aef12015-11-02 07:23:20 -0800165 int64_t current_time =
166 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
167 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700168 {
169 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800170
Peter Boström81cbd922016-03-22 12:19:07 +0100171 if (rtp_timestamp_delta_ == 0) {
sprang1b3530b2016-03-10 01:32:53 -0800172 rtp_timestamp_delta_ = header.timestamp - first_send_frame_.timestamp();
ivica5d6a06c2015-09-17 05:30:24 -0700173 first_send_frame_.Reset();
174 }
sprang1b3530b2016-03-10 01:32:53 -0800175 int64_t timestamp =
176 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
sprangce4aef12015-11-02 07:23:20 -0800177 send_times_[timestamp] = current_time;
178 if (!transport_->DiscardedLastPacket() &&
179 header.ssrc == ssrc_to_analyze_) {
180 encoded_frame_sizes_[timestamp] +=
181 length - (header.headerLength + header.paddingLength);
182 }
ivica5d6a06c2015-09-17 05:30:24 -0700183 }
sprangce4aef12015-11-02 07:23:20 -0800184 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700185 }
186
187 bool SendRtcp(const uint8_t* packet, size_t length) override {
188 return transport_->SendRtcp(packet, length);
189 }
190
191 void EncodedFrameCallback(const EncodedFrame& frame) override {
192 rtc::CritScope lock(&comparison_lock_);
193 if (frames_recorded_ < frames_to_process_)
194 encoded_frame_size_.AddSample(frame.length_);
195 }
196
nisseeb83a1a2016-03-21 01:27:56 -0700197 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700198 int64_t render_time_ms =
199 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700200
201 rtc::CritScope lock(&crit_);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700202 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800203 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700204
sprang16daaa52016-03-09 01:30:24 -0800205 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
ivica5d6a06c2015-09-17 05:30:24 -0700206 AddFrameComparison(frames_.front(), last_rendered_frame_, true,
207 render_time_ms);
208 frames_.pop_front();
209 }
210
211 VideoFrame reference_frame = frames_.front();
212 frames_.pop_front();
213 assert(!reference_frame.IsZeroSize());
sprang16daaa52016-03-09 01:30:24 -0800214 int64_t reference_timestamp =
215 wrap_handler_.Unwrap(reference_frame.timestamp());
216 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800217 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100218 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800219 ++send_timestamp;
220 }
sprang16daaa52016-03-09 01:30:24 -0800221 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700222
223 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
224
225 last_rendered_frame_ = video_frame;
226 }
227
ivica5d6a06c2015-09-17 05:30:24 -0700228 void Wait() {
229 // Frame comparisons can be very expensive. Wait for test to be done, but
230 // at time-out check if frames_processed is going up. If so, give it more
231 // time, otherwise fail. Hopefully this will reduce test flakiness.
232
Peter Boström8c38e8b2015-11-26 17:45:47 +0100233 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800234
ivica5d6a06c2015-09-17 05:30:24 -0700235 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700236 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100237 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700238 int frames_processed;
239 {
240 rtc::CritScope crit(&comparison_lock_);
241 frames_processed = frames_processed_;
242 }
243
244 // Print some output so test infrastructure won't think we've crashed.
245 const char* kKeepAliveMessages[3] = {
246 "Uh, I'm-I'm not quite dead, sir.",
247 "Uh, I-I think uh, I could pull through, sir.",
248 "Actually, I think I'm all right to come with you--"};
249 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
250
251 if (last_frames_processed == -1) {
252 last_frames_processed = frames_processed;
253 continue;
254 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100255 if (frames_processed == last_frames_processed) {
256 EXPECT_GT(frames_processed, last_frames_processed)
257 << "Analyzer stalled while waiting for test to finish.";
258 done_.Set();
259 break;
260 }
ivica5d6a06c2015-09-17 05:30:24 -0700261 last_frames_processed = frames_processed;
262 }
263
264 if (iteration > 0)
265 printf("- Farewell, sweet Concorde!\n");
266
Peter Boström8c38e8b2015-11-26 17:45:47 +0100267 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700268 }
269
Peter Boströme4499152016-02-05 11:13:28 +0100270 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
271
ivica5d6a06c2015-09-17 05:30:24 -0700272 VideoCaptureInput* input_;
sprangce4aef12015-11-02 07:23:20 -0800273 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700274 PacketReceiver* receiver_;
275 VideoSendStream* send_stream_;
276
277 private:
278 struct FrameComparison {
279 FrameComparison()
280 : dropped(false),
281 send_time_ms(0),
282 recv_time_ms(0),
283 render_time_ms(0),
284 encoded_frame_size(0) {}
285
286 FrameComparison(const VideoFrame& reference,
287 const VideoFrame& render,
288 bool dropped,
289 int64_t send_time_ms,
290 int64_t recv_time_ms,
291 int64_t render_time_ms,
292 size_t encoded_frame_size)
293 : reference(reference),
294 render(render),
295 dropped(dropped),
296 send_time_ms(send_time_ms),
297 recv_time_ms(recv_time_ms),
298 render_time_ms(render_time_ms),
299 encoded_frame_size(encoded_frame_size) {}
300
301 VideoFrame reference;
302 VideoFrame render;
303 bool dropped;
304 int64_t send_time_ms;
305 int64_t recv_time_ms;
306 int64_t render_time_ms;
307 size_t encoded_frame_size;
308 };
309
310 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700311 Sample(int dropped,
312 int64_t input_time_ms,
313 int64_t send_time_ms,
314 int64_t recv_time_ms,
315 int64_t render_time_ms,
316 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700317 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700318 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700319 : dropped(dropped),
320 input_time_ms(input_time_ms),
321 send_time_ms(send_time_ms),
322 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700323 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700324 encoded_frame_size(encoded_frame_size),
325 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700326 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700327
ivica8d15bd62015-10-07 02:43:12 -0700328 int dropped;
329 int64_t input_time_ms;
330 int64_t send_time_ms;
331 int64_t recv_time_ms;
332 int64_t render_time_ms;
333 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700334 double psnr;
335 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700336 };
337
Peter Boströme4499152016-02-05 11:13:28 +0100338 // This class receives the send-side OnEncodeTiming and is provided to not
339 // conflict with the receiver-side pre_decode_callback.
340 class OnEncodeTimingProxy : public EncodedFrameObserver {
341 public:
342 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
343
344 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
345 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
346 }
347 void EncodedFrameCallback(const EncodedFrame& frame) override {}
348
349 private:
350 VideoAnalyzer* const parent_;
351 };
352
ivica5d6a06c2015-09-17 05:30:24 -0700353 void AddFrameComparison(const VideoFrame& reference,
354 const VideoFrame& render,
355 bool dropped,
356 int64_t render_time_ms)
357 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800358 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
359 int64_t send_time_ms = send_times_[reference_timestamp];
360 send_times_.erase(reference_timestamp);
361 int64_t recv_time_ms = recv_times_[reference_timestamp];
362 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700363
sprangce4aef12015-11-02 07:23:20 -0800364 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800365 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800366 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800367 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800368 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
369 if (it != encoded_frame_sizes_.end())
370 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700371
372 VideoFrame reference_copy;
373 VideoFrame render_copy;
374 reference_copy.CopyFrame(reference);
375 render_copy.CopyFrame(render);
376
377 rtc::CritScope crit(&comparison_lock_);
378 comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped,
379 send_time_ms, recv_time_ms,
380 render_time_ms, encoded_size));
Peter Boström5811a392015-12-10 13:02:50 +0100381 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700382 }
383
384 static bool PollStatsThread(void* obj) {
385 return static_cast<VideoAnalyzer*>(obj)->PollStats();
386 }
387
388 bool PollStats() {
Peter Boströmdd45eb62016-01-19 15:22:32 +0100389 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 13:02:50 +0100390 return false;
ivica5d6a06c2015-09-17 05:30:24 -0700391
392 VideoSendStream::Stats stats = send_stream_->GetStats();
393
394 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 17:30:49 +0100395 // It's not certain that we yet have estimates for any of these stats. Check
396 // that they are positive before mixing them in.
397 if (stats.encode_frame_rate > 0)
398 encode_frame_rate_.AddSample(stats.encode_frame_rate);
399 if (stats.avg_encode_time_ms > 0)
400 encode_time_ms.AddSample(stats.avg_encode_time_ms);
401 if (stats.encode_usage_percent > 0)
402 encode_usage_percent.AddSample(stats.encode_usage_percent);
403 if (stats.media_bitrate_bps > 0)
404 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 05:30:24 -0700405
406 return true;
407 }
408
409 static bool FrameComparisonThread(void* obj) {
410 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
411 }
412
413 bool CompareFrames() {
414 if (AllFramesRecorded())
415 return false;
416
417 VideoFrame reference;
418 VideoFrame render;
419 FrameComparison comparison;
420
421 if (!PopComparison(&comparison)) {
422 // Wait until new comparison task is available, or test is done.
423 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100424 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700425 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100426 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700427 return false;
428 }
429 return true; // Try again.
430 }
431
432 PerformFrameComparison(comparison);
433
434 if (FrameProcessed()) {
435 PrintResults();
436 if (graph_data_output_file_)
437 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100438 done_.Set();
439 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700440 return false;
441 }
442
443 return true;
444 }
445
446 bool PopComparison(FrameComparison* comparison) {
447 rtc::CritScope crit(&comparison_lock_);
448 // If AllFramesRecorded() is true, it means we have already popped
449 // frames_to_process_ frames from comparisons_, so there is no more work
450 // for this thread to be done. frames_processed_ might still be lower if
451 // all comparisons are not done, but those frames are currently being
452 // worked on by other threads.
453 if (comparisons_.empty() || AllFramesRecorded())
454 return false;
455
456 *comparison = comparisons_.front();
457 comparisons_.pop_front();
458
459 FrameRecorded();
460 return true;
461 }
462
463 // Increment counter for number of frames received for comparison.
464 void FrameRecorded() {
465 rtc::CritScope crit(&comparison_lock_);
466 ++frames_recorded_;
467 }
468
469 // Returns true if all frames to be compared have been taken from the queue.
470 bool AllFramesRecorded() {
471 rtc::CritScope crit(&comparison_lock_);
472 assert(frames_recorded_ <= frames_to_process_);
473 return frames_recorded_ == frames_to_process_;
474 }
475
476 // Increase count of number of frames processed. Returns true if this was the
477 // last frame to be processed.
478 bool FrameProcessed() {
479 rtc::CritScope crit(&comparison_lock_);
480 ++frames_processed_;
481 assert(frames_processed_ <= frames_to_process_);
482 return frames_processed_ == frames_to_process_;
483 }
484
485 void PrintResults() {
486 rtc::CritScope crit(&comparison_lock_);
487 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800488 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700489 PrintResult("sender_time", sender_time_, " ms");
Peter Boström81cbd922016-03-22 12:19:07 +0100490 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
491 dropped_frames_);
ivica5d6a06c2015-09-17 05:30:24 -0700492 PrintResult("receiver_time", receiver_time_, " ms");
493 PrintResult("total_delay_incl_network", end_to_end_, " ms");
494 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
495 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
496 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
497 PrintResult("encode_time", encode_time_ms, " ms");
498 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
499 PrintResult("media_bitrate", media_bitrate_bps, " bps");
500
501 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
502 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
503 }
504
505 void PerformFrameComparison(const FrameComparison& comparison) {
506 // Perform expensive psnr and ssim calculations while not holding lock.
507 double psnr = I420PSNR(&comparison.reference, &comparison.render);
508 double ssim = I420SSIM(&comparison.reference, &comparison.render);
509
510 int64_t input_time_ms = comparison.reference.ntp_time_ms();
511
512 rtc::CritScope crit(&comparison_lock_);
513 if (graph_data_output_file_) {
514 samples_.push_back(
515 Sample(comparison.dropped, input_time_ms, comparison.send_time_ms,
ivica8d15bd62015-10-07 02:43:12 -0700516 comparison.recv_time_ms, comparison.render_time_ms,
517 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700518 }
519 psnr_.AddSample(psnr);
520 ssim_.AddSample(ssim);
521
522 if (comparison.dropped) {
523 ++dropped_frames_;
524 return;
525 }
526 if (last_render_time_ != 0)
527 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
528 last_render_time_ = comparison.render_time_ms;
529
530 sender_time_.AddSample(comparison.send_time_ms - input_time_ms);
531 receiver_time_.AddSample(comparison.render_time_ms -
532 comparison.recv_time_ms);
533 end_to_end_.AddSample(comparison.render_time_ms - input_time_ms);
534 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
535 }
536
537 void PrintResult(const char* result_type,
538 test::Statistics stats,
539 const char* unit) {
540 printf("RESULT %s: %s = {%f, %f}%s\n",
541 result_type,
542 test_label_.c_str(),
543 stats.Mean(),
544 stats.StandardDeviation(),
545 unit);
546 }
547
548 void PrintSamplesToFile(void) {
549 FILE* out = graph_data_output_file_;
550 rtc::CritScope crit(&comparison_lock_);
551 std::sort(samples_.begin(), samples_.end(),
552 [](const Sample& A, const Sample& B) -> bool {
553 return A.input_time_ms < B.input_time_ms;
554 });
555
sprangce4aef12015-11-02 07:23:20 -0800556 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700557 fprintf(out, "%" PRIuS "\n", samples_.size());
558 fprintf(out,
559 "dropped "
560 "input_time_ms "
561 "send_time_ms "
562 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700563 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700564 "encoded_frame_size "
565 "psnr "
566 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700567 "encode_time_ms\n");
568 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700569 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700570 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
571 int encode_time_ms;
572 if (it != samples_encode_time_ms_.end()) {
573 encode_time_ms = it->second;
574 } else {
575 ++missing_encode_time_samples;
576 encode_time_ms = -1;
577 }
578 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
579 " %lf %lf %d\n",
580 sample.dropped, sample.input_time_ms, sample.send_time_ms,
581 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700582 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700583 encode_time_ms);
584 }
585 if (missing_encode_time_samples) {
586 fprintf(stderr,
587 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
588 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700589 }
590 }
591
592 const std::string test_label_;
593 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800594 const std::string graph_title_;
595 const uint32_t ssrc_to_analyze_;
Peter Boströme4499152016-02-05 11:13:28 +0100596 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700597 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700598 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700599 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
600 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
601 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
602 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
603 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
604 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
605 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
606 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
607 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
608 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
609 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
610
611 const int frames_to_process_;
612 int frames_recorded_;
613 int frames_processed_;
614 int dropped_frames_;
615 int64_t last_render_time_;
616 uint32_t rtp_timestamp_delta_;
617
618 rtc::CriticalSection crit_;
619 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
620 VideoFrame last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800621 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
622 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
623 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
624 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700625 VideoFrame first_send_frame_ GUARDED_BY(crit_);
626 const double avg_psnr_threshold_;
627 const double avg_ssim_threshold_;
628
629 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100630 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
631 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +0100632 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -0700633 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +0100634 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -0700635};
636
ivica87f83a92015-10-08 05:13:32 -0700637VideoQualityTest::VideoQualityTest() : clock_(Clock::GetRealTimeClock()) {}
ivica5d6a06c2015-09-17 05:30:24 -0700638
639void VideoQualityTest::TestBody() {}
640
sprangce4aef12015-11-02 07:23:20 -0800641std::string VideoQualityTest::GenerateGraphTitle() const {
642 std::stringstream ss;
643 ss << params_.common.codec;
644 ss << " (" << params_.common.target_bitrate_bps / 1000 << "kbps";
645 ss << ", " << params_.common.fps << " FPS";
646 if (params_.screenshare.scroll_duration)
647 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
648 if (params_.ss.streams.size() > 1)
649 ss << ", Stream #" << params_.ss.selected_stream;
650 if (params_.ss.num_spatial_layers > 1)
651 ss << ", Layer #" << params_.ss.selected_sl;
652 ss << ")";
653 return ss.str();
654}
655
656void VideoQualityTest::CheckParams() {
657 // Add a default stream in none specified.
658 if (params_.ss.streams.empty())
659 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
660 if (params_.ss.num_spatial_layers == 0)
661 params_.ss.num_spatial_layers = 1;
662
663 if (params_.pipe.loss_percent != 0 ||
664 params_.pipe.queue_length_packets != 0) {
665 // Since LayerFilteringTransport changes the sequence numbers, we can't
666 // use that feature with pack loss, since the NACK request would end up
667 // retransmitting the wrong packets.
668 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800669 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800670 RTC_CHECK(params_.common.selected_tl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800671 params_.common.selected_tl ==
672 params_.common.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800673 }
674
675 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
676 // does in some parts of the code?
677 RTC_CHECK_GE(params_.common.max_bitrate_bps,
678 params_.common.target_bitrate_bps);
679 RTC_CHECK_GE(params_.common.target_bitrate_bps,
680 params_.common.min_bitrate_bps);
681 RTC_CHECK_LT(params_.common.selected_tl, params_.common.num_temporal_layers);
682 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
683 for (const VideoStream& stream : params_.ss.streams) {
684 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
685 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
686 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
687 RTC_CHECK_EQ(static_cast<int>(stream.temporal_layer_thresholds_bps.size()),
688 params_.common.num_temporal_layers - 1);
689 }
690 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
691 // the total bitrate? We anyway have to update them in the case bitrate
692 // estimator changes the total bitrates.
693 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
694 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
695 RTC_CHECK(params_.ss.spatial_layers.empty() ||
696 params_.ss.spatial_layers.size() ==
697 static_cast<size_t>(params_.ss.num_spatial_layers));
698 if (params_.common.codec == "VP8") {
699 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
700 } else if (params_.common.codec == "VP9") {
701 RTC_CHECK_EQ(params_.ss.streams.size(), 1u);
702 }
703}
704
705// Static.
706std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
707 // Parse comma separated nonnegative integers, where some elements may be
708 // empty. The empty values are replaced with -1.
709 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
710 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
711 std::vector<int> result;
712 if (str.empty())
713 return result;
714
715 const char* p = str.c_str();
716 int value = -1;
717 int pos;
718 while (*p) {
719 if (*p == ',') {
720 result.push_back(value);
721 value = -1;
722 ++p;
723 continue;
724 }
725 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
726 << "Unexpected non-number value.";
727 p += pos;
728 }
729 result.push_back(value);
730 return result;
731}
732
733// Static.
734VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
735 VideoStream stream;
736 stream.width = params.common.width;
737 stream.height = params.common.height;
738 stream.max_framerate = params.common.fps;
739 stream.min_bitrate_bps = params.common.min_bitrate_bps;
740 stream.target_bitrate_bps = params.common.target_bitrate_bps;
741 stream.max_bitrate_bps = params.common.max_bitrate_bps;
742 stream.max_qp = 52;
743 if (params.common.num_temporal_layers == 2)
744 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
745 return stream;
746}
747
748// Static.
749void VideoQualityTest::FillScalabilitySettings(
750 Params* params,
751 const std::vector<std::string>& stream_descriptors,
752 size_t selected_stream,
753 int num_spatial_layers,
754 int selected_sl,
755 const std::vector<std::string>& sl_descriptors) {
756 // Read VideoStream and SpatialLayer elements from a list of comma separated
757 // lists. To use a default value for an element, use -1 or leave empty.
758 // Validity checks performed in CheckParams.
759
760 RTC_CHECK(params->ss.streams.empty());
761 for (auto descriptor : stream_descriptors) {
762 if (descriptor.empty())
763 continue;
764 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
765 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
766 if (v[0] != -1)
767 stream.width = static_cast<size_t>(v[0]);
768 if (v[1] != -1)
769 stream.height = static_cast<size_t>(v[1]);
770 if (v[2] != -1)
771 stream.max_framerate = v[2];
772 if (v[3] != -1)
773 stream.min_bitrate_bps = v[3];
774 if (v[4] != -1)
775 stream.target_bitrate_bps = v[4];
776 if (v[5] != -1)
777 stream.max_bitrate_bps = v[5];
778 if (v.size() > 6 && v[6] != -1)
779 stream.max_qp = v[6];
780 if (v.size() > 7) {
781 stream.temporal_layer_thresholds_bps.clear();
782 stream.temporal_layer_thresholds_bps.insert(
783 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
784 } else {
785 // Automatic TL thresholds for more than two layers not supported.
786 RTC_CHECK_LE(params->common.num_temporal_layers, 2);
787 }
788 params->ss.streams.push_back(stream);
789 }
790 params->ss.selected_stream = selected_stream;
791
792 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
793 params->ss.selected_sl = selected_sl;
794 RTC_CHECK(params->ss.spatial_layers.empty());
795 for (auto descriptor : sl_descriptors) {
796 if (descriptor.empty())
797 continue;
798 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
799 RTC_CHECK_GT(v[2], 0);
800
801 SpatialLayer layer;
802 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
803 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
804 layer.target_bitrate_bps = v[2];
805 params->ss.spatial_layers.push_back(layer);
806 }
807}
808
809void VideoQualityTest::SetupCommon(Transport* send_transport,
810 Transport* recv_transport) {
811 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -0700812 trace_to_stderr_.reset(new test::TraceToStderr);
813
sprangce4aef12015-11-02 07:23:20 -0800814 size_t num_streams = params_.ss.streams.size();
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100815 CreateSendConfig(num_streams, 0, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -0700816
817 int payload_type;
hbosbab934b2016-01-27 01:36:03 -0800818 if (params_.common.codec == "H264") {
819 encoder_.reset(VideoEncoder::Create(VideoEncoder::kH264));
820 payload_type = kPayloadTypeH264;
821 } else if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700822 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp8));
823 payload_type = kPayloadTypeVP8;
sprangce4aef12015-11-02 07:23:20 -0800824 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700825 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp9));
826 payload_type = kPayloadTypeVP9;
827 } else {
828 RTC_NOTREACHED() << "Codec not supported!";
829 return;
830 }
stefanff483612015-12-21 03:14:00 -0800831 video_send_config_.encoder_settings.encoder = encoder_.get();
832 video_send_config_.encoder_settings.payload_name = params_.common.codec;
833 video_send_config_.encoder_settings.payload_type = payload_type;
834 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
835 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
sprangce4aef12015-11-02 07:23:20 -0800836 for (size_t i = 0; i < num_streams; ++i)
stefanff483612015-12-21 03:14:00 -0800837 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -0700838
stefanff483612015-12-21 03:14:00 -0800839 video_send_config_.rtp.extensions.clear();
sprangce4aef12015-11-02 07:23:20 -0800840 if (params_.common.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -0800841 video_send_config_.rtp.extensions.push_back(
Stefan Holmer12952972015-10-29 15:13:24 +0100842 RtpExtension(RtpExtension::kTransportSequenceNumber,
843 test::kTransportSequenceNumberExtensionId));
844 } else {
stefanff483612015-12-21 03:14:00 -0800845 video_send_config_.rtp.extensions.push_back(RtpExtension(
Stefan Holmer12952972015-10-29 15:13:24 +0100846 RtpExtension::kAbsSendTime, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +0200847 }
848
stefanff483612015-12-21 03:14:00 -0800849 video_encoder_config_.min_transmit_bitrate_bps =
850 params_.common.min_transmit_bps;
851 video_encoder_config_.streams = params_.ss.streams;
852 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -0700853
854 CreateMatchingReceiveConfigs(recv_transport);
855
sprangce4aef12015-11-02 07:23:20 -0800856 for (size_t i = 0; i < num_streams; ++i) {
stefanff483612015-12-21 03:14:00 -0800857 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 13:29:59 +0100858 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
859 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 07:23:20 -0800860 kSendRtxPayloadType;
stefanff483612015-12-21 03:14:00 -0800861 video_receive_configs_[i].rtp.transport_cc = params_.common.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -0800862 }
ivica5d6a06c2015-09-17 05:30:24 -0700863}
864
sprangce4aef12015-11-02 07:23:20 -0800865void VideoQualityTest::SetupScreenshare() {
866 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 05:30:24 -0700867
868 // Fill out codec settings.
stefanff483612015-12-21 03:14:00 -0800869 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
sprangce4aef12015-11-02 07:23:20 -0800870 if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700871 codec_settings_.VP8 = VideoEncoder::GetDefaultVp8Settings();
872 codec_settings_.VP8.denoisingOn = false;
873 codec_settings_.VP8.frameDroppingOn = false;
874 codec_settings_.VP8.numberOfTemporalLayers =
sprangce4aef12015-11-02 07:23:20 -0800875 static_cast<unsigned char>(params_.common.num_temporal_layers);
stefanff483612015-12-21 03:14:00 -0800876 video_encoder_config_.encoder_specific_settings = &codec_settings_.VP8;
sprangce4aef12015-11-02 07:23:20 -0800877 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700878 codec_settings_.VP9 = VideoEncoder::GetDefaultVp9Settings();
879 codec_settings_.VP9.denoisingOn = false;
880 codec_settings_.VP9.frameDroppingOn = false;
881 codec_settings_.VP9.numberOfTemporalLayers =
sprangce4aef12015-11-02 07:23:20 -0800882 static_cast<unsigned char>(params_.common.num_temporal_layers);
stefanff483612015-12-21 03:14:00 -0800883 video_encoder_config_.encoder_specific_settings = &codec_settings_.VP9;
sprangce4aef12015-11-02 07:23:20 -0800884 codec_settings_.VP9.numberOfSpatialLayers =
885 static_cast<unsigned char>(params_.ss.num_spatial_layers);
ivica5d6a06c2015-09-17 05:30:24 -0700886 }
887
888 // Setup frame generator.
889 const size_t kWidth = 1850;
890 const size_t kHeight = 1110;
891 std::vector<std::string> slides;
892 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
893 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
894 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
895 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
896
sprangce4aef12015-11-02 07:23:20 -0800897 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 05:30:24 -0700898 // Cycle image every slide_change_interval seconds.
899 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
900 slides, kWidth, kHeight,
sprangce4aef12015-11-02 07:23:20 -0800901 params_.screenshare.slide_change_interval * params_.common.fps));
ivica5d6a06c2015-09-17 05:30:24 -0700902 } else {
sprangce4aef12015-11-02 07:23:20 -0800903 RTC_CHECK_LE(params_.common.width, kWidth);
904 RTC_CHECK_LE(params_.common.height, kHeight);
905 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
906 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
907 params_.screenshare.scroll_duration) *
908 1000;
909 RTC_CHECK_LE(params_.screenshare.scroll_duration,
910 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 05:30:24 -0700911
sprangce4aef12015-11-02 07:23:20 -0800912 frame_generator_.reset(
913 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
914 clock_, slides, kWidth, kHeight, params_.common.width,
915 params_.common.height, params_.screenshare.scroll_duration * 1000,
916 kPauseDurationMs));
ivica5d6a06c2015-09-17 05:30:24 -0700917 }
918}
919
sprangce4aef12015-11-02 07:23:20 -0800920void VideoQualityTest::CreateCapturer(VideoCaptureInput* input) {
921 if (params_.screenshare.enabled) {
922 test::FrameGeneratorCapturer* frame_generator_capturer =
ivica2d4e6c52015-09-23 01:57:06 -0700923 new test::FrameGeneratorCapturer(
sprangce4aef12015-11-02 07:23:20 -0800924 clock_, input, frame_generator_.release(), params_.common.fps);
ivica2d4e6c52015-09-23 01:57:06 -0700925 EXPECT_TRUE(frame_generator_capturer->Init());
926 capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -0700927 } else {
sprangce4aef12015-11-02 07:23:20 -0800928 if (params_.video.clip_name.empty()) {
929 capturer_.reset(test::VideoCapturer::Create(input, params_.common.width,
930 params_.common.height,
931 params_.common.fps, clock_));
ivica5d6a06c2015-09-17 05:30:24 -0700932 } else {
ivica2d4e6c52015-09-23 01:57:06 -0700933 capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
sprangce4aef12015-11-02 07:23:20 -0800934 input, test::ResourcePath(params_.video.clip_name, "yuv"),
935 params_.common.width, params_.common.height, params_.common.fps,
ivica2d4e6c52015-09-23 01:57:06 -0700936 clock_));
Peter Boström74f6e9e2016-04-04 17:56:10 +0200937 ASSERT_TRUE(capturer_) << "Could not create capturer for "
938 << params_.video.clip_name
939 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -0700940 }
941 }
942}
943
sprang7a975f72015-10-12 06:33:21 -0700944void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -0800945 params_ = params;
946
ivica5d6a06c2015-09-17 05:30:24 -0700947 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
948 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -0800949 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -0700950
951 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -0800952 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -0700953 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -0800954 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +0200955 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -0800956 << "Can't open the file " << params_.analyzer.graph_data_output_filename
957 << "!";
ivica87f83a92015-10-08 05:13:32 -0700958 }
sprang7a975f72015-10-12 06:33:21 -0700959
stefanf116bd02015-10-27 08:29:42 -0700960 Call::Config call_config;
961 call_config.bitrate_config = params.common.call_bitrate_config;
962 CreateCalls(call_config, call_config);
963
ivica87f83a92015-10-08 05:13:32 -0700964 test::LayerFilteringTransport send_transport(
stefanf116bd02015-10-27 08:29:42 -0700965 params.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -0800966 params.common.selected_tl, params_.ss.selected_sl);
stefanf116bd02015-10-27 08:29:42 -0700967 test::DirectTransport recv_transport(params.pipe, receiver_call_.get());
968
sprangce4aef12015-11-02 07:23:20 -0800969 std::string graph_title = params_.analyzer.graph_title;
970 if (graph_title.empty())
971 graph_title = VideoQualityTest::GenerateGraphTitle();
972
973 // In the case of different resolutions, the functions calculating PSNR and
974 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
975 // aborts if the average psnr/ssim are below the given threshold, which is
976 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
977 // abort.
978 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
979 int selected_sl = params_.ss.selected_sl != -1
980 ? params_.ss.selected_sl
981 : params_.ss.num_spatial_layers - 1;
982 bool disable_quality_check =
983 selected_stream.width != params_.common.width ||
984 selected_stream.height != params_.common.height ||
985 (!params_.ss.spatial_layers.empty() &&
986 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
987 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
988 if (disable_quality_check) {
989 fprintf(stderr,
990 "Warning: Calculating PSNR and SSIM for downsized resolution "
991 "not implemented yet! Skipping PSNR and SSIM calculations!");
992 }
993
ivica5d6a06c2015-09-17 05:30:24 -0700994 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -0800995 &send_transport, params_.analyzer.test_label,
996 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
997 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
998 params_.analyzer.test_durations_secs * params_.common.fps,
999 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001000 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 05:30:24 -07001001
ivica5d6a06c2015-09-17 05:30:24 -07001002 analyzer.SetReceiver(receiver_call_->Receiver());
1003 send_transport.SetReceiver(&analyzer);
1004 recv_transport.SetReceiver(sender_call_->Receiver());
1005
sprangce4aef12015-11-02 07:23:20 -08001006 SetupCommon(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001007 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
1008 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 05:30:24 -07001009 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 11:13:28 +01001010 RTC_DCHECK(!video_send_config_.post_encode_callback);
1011 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001012
sprangce4aef12015-11-02 07:23:20 -08001013 if (params_.screenshare.enabled)
1014 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001015
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001016 CreateVideoStreams();
stefanff483612015-12-21 03:14:00 -08001017 analyzer.input_ = video_send_stream_->Input();
1018 analyzer.send_stream_ = video_send_stream_;
ivica5d6a06c2015-09-17 05:30:24 -07001019
sprangce4aef12015-11-02 07:23:20 -08001020 CreateCapturer(&analyzer);
ivicac1cc8542015-10-08 03:44:06 -07001021
stefanff483612015-12-21 03:14:00 -08001022 video_send_stream_->Start();
1023 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1024 receive_stream->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001025 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001026
1027 analyzer.Wait();
1028
1029 send_transport.StopSending();
1030 recv_transport.StopSending();
1031
ivica2d4e6c52015-09-23 01:57:06 -07001032 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001033 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1034 receive_stream->Stop();
1035 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001036
1037 DestroyStreams();
1038
1039 if (graph_data_output_file)
1040 fclose(graph_data_output_file);
1041}
1042
sprang7a975f72015-10-12 06:33:21 -07001043void VideoQualityTest::RunWithVideoRenderer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001044 params_ = params;
1045 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001046
kwiberg27f982b2016-03-01 11:52:33 -08001047 std::unique_ptr<test::VideoRenderer> local_preview(
sprangce4aef12015-11-02 07:23:20 -08001048 test::VideoRenderer::Create("Local Preview", params_.common.width,
1049 params_.common.height));
1050 size_t stream_id = params_.ss.selected_stream;
mflodmand1590b22015-12-09 07:07:59 -08001051 std::string title = "Loopback Video";
1052 if (params_.ss.streams.size() > 1) {
1053 std::ostringstream s;
1054 s << stream_id;
1055 title += " - Stream #" + s.str();
sprangce4aef12015-11-02 07:23:20 -08001056 }
mflodmand1590b22015-12-09 07:07:59 -08001057
kwiberg27f982b2016-03-01 11:52:33 -08001058 std::unique_ptr<test::VideoRenderer> loopback_video(
mflodmand1590b22015-12-09 07:07:59 -08001059 test::VideoRenderer::Create(title.c_str(),
1060 params_.ss.streams[stream_id].width,
sprangce4aef12015-11-02 07:23:20 -08001061 params_.ss.streams[stream_id].height));
ivica5d6a06c2015-09-17 05:30:24 -07001062
1063 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1064 // match the full stack tests.
1065 Call::Config call_config;
sprangce4aef12015-11-02 07:23:20 -08001066 call_config.bitrate_config = params_.common.call_bitrate_config;
kwiberg27f982b2016-03-01 11:52:33 -08001067 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001068
1069 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001070 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -08001071 params.common.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001072 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1073 // least share as much code as possible. That way this test would also match
1074 // the full stack tests better.
1075 transport.SetReceiver(call->Receiver());
1076
sprangce4aef12015-11-02 07:23:20 -08001077 SetupCommon(&transport, &transport);
ivica87f83a92015-10-08 05:13:32 -07001078
stefanff483612015-12-21 03:14:00 -08001079 video_send_config_.local_renderer = local_preview.get();
1080 video_receive_configs_[stream_id].renderer = loopback_video.get();
sprangce4aef12015-11-02 07:23:20 -08001081
1082 if (params_.screenshare.enabled)
1083 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001084
stefanff483612015-12-21 03:14:00 -08001085 video_send_stream_ =
1086 call->CreateVideoSendStream(video_send_config_, video_encoder_config_);
ivica5d6a06c2015-09-17 05:30:24 -07001087 VideoReceiveStream* receive_stream =
stefanff483612015-12-21 03:14:00 -08001088 call->CreateVideoReceiveStream(video_receive_configs_[stream_id]);
1089 CreateCapturer(video_send_stream_->Input());
ivica5d6a06c2015-09-17 05:30:24 -07001090
1091 receive_stream->Start();
stefanff483612015-12-21 03:14:00 -08001092 video_send_stream_->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001093 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001094
1095 test::PressEnterToContinue();
1096
ivica2d4e6c52015-09-23 01:57:06 -07001097 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001098 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001099 receive_stream->Stop();
1100
1101 call->DestroyVideoReceiveStream(receive_stream);
stefanff483612015-12-21 03:14:00 -08001102 call->DestroyVideoSendStream(video_send_stream_);
ivica5d6a06c2015-09-17 05:30:24 -07001103
1104 transport.StopSending();
1105}
1106
1107} // namespace webrtc