blob: 057a8a2ab71aa2d2be32b1e19e65e31860cf3138 [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"
nissec7fe3c22016-04-20 03:25:36 -070024#include "webrtc/base/optional.h"
sprange1f2f1f2016-02-01 02:04:52 -080025#include "webrtc/base/timeutils.h"
ivica5d6a06c2015-09-17 05:30:24 -070026#include "webrtc/call.h"
27#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
sprangce4aef12015-11-02 07:23:20 -080029#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010030#include "webrtc/system_wrappers/include/cpu_info.h"
ivica5d6a06c2015-09-17 05:30:24 -070031#include "webrtc/test/layer_filtering_transport.h"
32#include "webrtc/test/run_loop.h"
33#include "webrtc/test/statistics.h"
34#include "webrtc/test/testsupport/fileutils.h"
35#include "webrtc/test/video_renderer.h"
36#include "webrtc/video/video_quality_test.h"
37
38namespace webrtc {
39
40static const int kSendStatsPollingIntervalMs = 1000;
hbosbab934b2016-01-27 01:36:03 -080041static const int kPayloadTypeH264 = 122;
ivica5d6a06c2015-09-17 05:30:24 -070042static const int kPayloadTypeVP8 = 123;
43static const int kPayloadTypeVP9 = 124;
44
45class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -070046 public Transport,
nisse7ade7b32016-03-23 04:48:10 -070047 public rtc::VideoSinkInterface<VideoFrame>,
ivica5d6a06c2015-09-17 05:30:24 -070048 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),
72 last_render_time_(0),
73 rtp_timestamp_delta_(0),
74 avg_psnr_threshold_(avg_psnr_threshold),
75 avg_ssim_threshold_(avg_ssim_threshold),
Peter Boström8c38e8b2015-11-26 17:45:47 +010076 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +010077 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +010078 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -070079 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
80
81 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
82 // so that we don't accidentally starve "real" worker threads (codec etc).
83 // Also, don't allocate more than kMaxComparisonThreads, even if there are
84 // spare cores.
85
86 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
87 RTC_DCHECK_GE(num_cores, 1u);
88 static const uint32_t kMinCoresLeft = 4;
89 static const uint32_t kMaxComparisonThreads = 8;
90
91 if (num_cores <= kMinCoresLeft) {
92 num_cores = 1;
93 } else {
94 num_cores -= kMinCoresLeft;
95 num_cores = std::min(num_cores, kMaxComparisonThreads);
96 }
97
98 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +010099 rtc::PlatformThread* thread =
100 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
101 thread->Start();
102 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700103 }
ivica5d6a06c2015-09-17 05:30:24 -0700104 }
105
106 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100107 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
108 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700109 delete thread;
110 }
111 }
112
113 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
114
115 DeliveryStatus DeliverPacket(MediaType media_type,
116 const uint8_t* packet,
117 size_t length,
118 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700119 // Ignore timestamps of RTCP packets. They're not synchronized with
120 // RTP packet timestamps and so they would confuse wrap_handler_.
121 if (RtpHeaderParser::IsRtcp(packet, length)) {
122 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
123 }
124
sprangce4aef12015-11-02 07:23:20 -0800125 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700126 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800127 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700128 {
129 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800130 int64_t timestamp =
131 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
132 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700133 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
134 }
135
136 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
137 }
138
Peter Boströme4499152016-02-05 11:13:28 +0100139 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700140 rtc::CritScope crit(&comparison_lock_);
141 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
142 }
143
ivica5d6a06c2015-09-17 05:30:24 -0700144 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
145 VideoFrame copy = video_frame;
146 copy.set_timestamp(copy.ntp_time_ms() * 90);
Peter Boström81cbd922016-03-22 12:19:07 +0100147
ivica5d6a06c2015-09-17 05:30:24 -0700148 {
149 rtc::CritScope lock(&crit_);
nissec7fe3c22016-04-20 03:25:36 -0700150 if (!first_send_timestamp_ && rtp_timestamp_delta_ == 0)
151 first_send_timestamp_ = rtc::Optional<uint32_t>(copy.timestamp());
Peter Boström81cbd922016-03-22 12:19:07 +0100152
ivica5d6a06c2015-09-17 05:30:24 -0700153 frames_.push_back(copy);
154 }
155
156 input_->IncomingCapturedFrame(video_frame);
157 }
158
stefan1d8a5062015-10-02 03:39:33 -0700159 bool SendRtp(const uint8_t* packet,
160 size_t length,
161 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800162 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700163 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800164 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700165
sprangce4aef12015-11-02 07:23:20 -0800166 int64_t current_time =
167 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
168 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700169 {
170 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800171
Peter Boström81cbd922016-03-22 12:19:07 +0100172 if (rtp_timestamp_delta_ == 0) {
nissec7fe3c22016-04-20 03:25:36 -0700173 rtp_timestamp_delta_ = header.timestamp - *first_send_timestamp_;
174 first_send_timestamp_ = rtc::Optional<uint32_t>();
ivica5d6a06c2015-09-17 05:30:24 -0700175 }
sprang1b3530b2016-03-10 01:32:53 -0800176 int64_t timestamp =
177 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
sprangce4aef12015-11-02 07:23:20 -0800178 send_times_[timestamp] = current_time;
179 if (!transport_->DiscardedLastPacket() &&
180 header.ssrc == ssrc_to_analyze_) {
181 encoded_frame_sizes_[timestamp] +=
182 length - (header.headerLength + header.paddingLength);
183 }
ivica5d6a06c2015-09-17 05:30:24 -0700184 }
sprangce4aef12015-11-02 07:23:20 -0800185 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700186 }
187
188 bool SendRtcp(const uint8_t* packet, size_t length) override {
189 return transport_->SendRtcp(packet, length);
190 }
191
192 void EncodedFrameCallback(const EncodedFrame& frame) override {
193 rtc::CritScope lock(&comparison_lock_);
194 if (frames_recorded_ < frames_to_process_)
195 encoded_frame_size_.AddSample(frame.length_);
196 }
197
nisseeb83a1a2016-03-21 01:27:56 -0700198 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700199 int64_t render_time_ms =
200 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700201
202 rtc::CritScope lock(&crit_);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700203 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800204 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700205
sprang16daaa52016-03-09 01:30:24 -0800206 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
ivica5d6a06c2015-09-17 05:30:24 -0700207 AddFrameComparison(frames_.front(), last_rendered_frame_, true,
208 render_time_ms);
209 frames_.pop_front();
210 }
211
212 VideoFrame reference_frame = frames_.front();
213 frames_.pop_front();
214 assert(!reference_frame.IsZeroSize());
sprang16daaa52016-03-09 01:30:24 -0800215 int64_t reference_timestamp =
216 wrap_handler_.Unwrap(reference_frame.timestamp());
217 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800218 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100219 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800220 ++send_timestamp;
221 }
sprang16daaa52016-03-09 01:30:24 -0800222 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700223
224 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
225
226 last_rendered_frame_ = video_frame;
227 }
228
ivica5d6a06c2015-09-17 05:30:24 -0700229 void Wait() {
230 // Frame comparisons can be very expensive. Wait for test to be done, but
231 // at time-out check if frames_processed is going up. If so, give it more
232 // time, otherwise fail. Hopefully this will reduce test flakiness.
233
Peter Boström8c38e8b2015-11-26 17:45:47 +0100234 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800235
ivica5d6a06c2015-09-17 05:30:24 -0700236 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700237 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100238 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700239 int frames_processed;
240 {
241 rtc::CritScope crit(&comparison_lock_);
242 frames_processed = frames_processed_;
243 }
244
245 // Print some output so test infrastructure won't think we've crashed.
246 const char* kKeepAliveMessages[3] = {
247 "Uh, I'm-I'm not quite dead, sir.",
248 "Uh, I-I think uh, I could pull through, sir.",
249 "Actually, I think I'm all right to come with you--"};
250 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
251
252 if (last_frames_processed == -1) {
253 last_frames_processed = frames_processed;
254 continue;
255 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100256 if (frames_processed == last_frames_processed) {
257 EXPECT_GT(frames_processed, last_frames_processed)
258 << "Analyzer stalled while waiting for test to finish.";
259 done_.Set();
260 break;
261 }
ivica5d6a06c2015-09-17 05:30:24 -0700262 last_frames_processed = frames_processed;
263 }
264
265 if (iteration > 0)
266 printf("- Farewell, sweet Concorde!\n");
267
Peter Boström8c38e8b2015-11-26 17:45:47 +0100268 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700269 }
270
Peter Boströme4499152016-02-05 11:13:28 +0100271 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
272
ivica5d6a06c2015-09-17 05:30:24 -0700273 VideoCaptureInput* input_;
sprangce4aef12015-11-02 07:23:20 -0800274 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700275 PacketReceiver* receiver_;
276 VideoSendStream* send_stream_;
277
278 private:
279 struct FrameComparison {
280 FrameComparison()
281 : dropped(false),
282 send_time_ms(0),
283 recv_time_ms(0),
284 render_time_ms(0),
285 encoded_frame_size(0) {}
286
287 FrameComparison(const VideoFrame& reference,
288 const VideoFrame& render,
289 bool dropped,
290 int64_t send_time_ms,
291 int64_t recv_time_ms,
292 int64_t render_time_ms,
293 size_t encoded_frame_size)
294 : reference(reference),
295 render(render),
296 dropped(dropped),
297 send_time_ms(send_time_ms),
298 recv_time_ms(recv_time_ms),
299 render_time_ms(render_time_ms),
300 encoded_frame_size(encoded_frame_size) {}
301
302 VideoFrame reference;
303 VideoFrame render;
304 bool dropped;
305 int64_t send_time_ms;
306 int64_t recv_time_ms;
307 int64_t render_time_ms;
308 size_t encoded_frame_size;
309 };
310
311 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700312 Sample(int dropped,
313 int64_t input_time_ms,
314 int64_t send_time_ms,
315 int64_t recv_time_ms,
316 int64_t render_time_ms,
317 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700318 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700319 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700320 : dropped(dropped),
321 input_time_ms(input_time_ms),
322 send_time_ms(send_time_ms),
323 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700324 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700325 encoded_frame_size(encoded_frame_size),
326 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700327 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700328
ivica8d15bd62015-10-07 02:43:12 -0700329 int dropped;
330 int64_t input_time_ms;
331 int64_t send_time_ms;
332 int64_t recv_time_ms;
333 int64_t render_time_ms;
334 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700335 double psnr;
336 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700337 };
338
Peter Boströme4499152016-02-05 11:13:28 +0100339 // This class receives the send-side OnEncodeTiming and is provided to not
340 // conflict with the receiver-side pre_decode_callback.
341 class OnEncodeTimingProxy : public EncodedFrameObserver {
342 public:
343 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
344
345 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
346 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
347 }
348 void EncodedFrameCallback(const EncodedFrame& frame) override {}
349
350 private:
351 VideoAnalyzer* const parent_;
352 };
353
ivica5d6a06c2015-09-17 05:30:24 -0700354 void AddFrameComparison(const VideoFrame& reference,
355 const VideoFrame& render,
356 bool dropped,
357 int64_t render_time_ms)
358 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800359 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
360 int64_t send_time_ms = send_times_[reference_timestamp];
361 send_times_.erase(reference_timestamp);
362 int64_t recv_time_ms = recv_times_[reference_timestamp];
363 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700364
sprangce4aef12015-11-02 07:23:20 -0800365 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800366 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800367 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800368 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800369 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
370 if (it != encoded_frame_sizes_.end())
371 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700372
373 VideoFrame reference_copy;
374 VideoFrame render_copy;
375 reference_copy.CopyFrame(reference);
376 render_copy.CopyFrame(render);
377
378 rtc::CritScope crit(&comparison_lock_);
379 comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped,
380 send_time_ms, recv_time_ms,
381 render_time_ms, encoded_size));
Peter Boström5811a392015-12-10 13:02:50 +0100382 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700383 }
384
385 static bool PollStatsThread(void* obj) {
386 return static_cast<VideoAnalyzer*>(obj)->PollStats();
387 }
388
389 bool PollStats() {
Peter Boströmdd45eb62016-01-19 15:22:32 +0100390 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 13:02:50 +0100391 return false;
ivica5d6a06c2015-09-17 05:30:24 -0700392
393 VideoSendStream::Stats stats = send_stream_->GetStats();
394
395 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 17:30:49 +0100396 // It's not certain that we yet have estimates for any of these stats. Check
397 // that they are positive before mixing them in.
398 if (stats.encode_frame_rate > 0)
399 encode_frame_rate_.AddSample(stats.encode_frame_rate);
400 if (stats.avg_encode_time_ms > 0)
401 encode_time_ms.AddSample(stats.avg_encode_time_ms);
402 if (stats.encode_usage_percent > 0)
403 encode_usage_percent.AddSample(stats.encode_usage_percent);
404 if (stats.media_bitrate_bps > 0)
405 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 05:30:24 -0700406
407 return true;
408 }
409
410 static bool FrameComparisonThread(void* obj) {
411 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
412 }
413
414 bool CompareFrames() {
415 if (AllFramesRecorded())
416 return false;
417
418 VideoFrame reference;
419 VideoFrame render;
420 FrameComparison comparison;
421
422 if (!PopComparison(&comparison)) {
423 // Wait until new comparison task is available, or test is done.
424 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100425 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700426 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100427 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700428 return false;
429 }
430 return true; // Try again.
431 }
432
433 PerformFrameComparison(comparison);
434
435 if (FrameProcessed()) {
436 PrintResults();
437 if (graph_data_output_file_)
438 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100439 done_.Set();
440 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700441 return false;
442 }
443
444 return true;
445 }
446
447 bool PopComparison(FrameComparison* comparison) {
448 rtc::CritScope crit(&comparison_lock_);
449 // If AllFramesRecorded() is true, it means we have already popped
450 // frames_to_process_ frames from comparisons_, so there is no more work
451 // for this thread to be done. frames_processed_ might still be lower if
452 // all comparisons are not done, but those frames are currently being
453 // worked on by other threads.
454 if (comparisons_.empty() || AllFramesRecorded())
455 return false;
456
457 *comparison = comparisons_.front();
458 comparisons_.pop_front();
459
460 FrameRecorded();
461 return true;
462 }
463
464 // Increment counter for number of frames received for comparison.
465 void FrameRecorded() {
466 rtc::CritScope crit(&comparison_lock_);
467 ++frames_recorded_;
468 }
469
470 // Returns true if all frames to be compared have been taken from the queue.
471 bool AllFramesRecorded() {
472 rtc::CritScope crit(&comparison_lock_);
473 assert(frames_recorded_ <= frames_to_process_);
474 return frames_recorded_ == frames_to_process_;
475 }
476
477 // Increase count of number of frames processed. Returns true if this was the
478 // last frame to be processed.
479 bool FrameProcessed() {
480 rtc::CritScope crit(&comparison_lock_);
481 ++frames_processed_;
482 assert(frames_processed_ <= frames_to_process_);
483 return frames_processed_ == frames_to_process_;
484 }
485
486 void PrintResults() {
487 rtc::CritScope crit(&comparison_lock_);
488 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800489 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700490 PrintResult("sender_time", sender_time_, " ms");
Peter Boström81cbd922016-03-22 12:19:07 +0100491 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
492 dropped_frames_);
ivica5d6a06c2015-09-17 05:30:24 -0700493 PrintResult("receiver_time", receiver_time_, " ms");
494 PrintResult("total_delay_incl_network", end_to_end_, " ms");
495 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
496 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
497 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
498 PrintResult("encode_time", encode_time_ms, " ms");
499 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
500 PrintResult("media_bitrate", media_bitrate_bps, " bps");
501
502 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
503 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
504 }
505
506 void PerformFrameComparison(const FrameComparison& comparison) {
507 // Perform expensive psnr and ssim calculations while not holding lock.
508 double psnr = I420PSNR(&comparison.reference, &comparison.render);
509 double ssim = I420SSIM(&comparison.reference, &comparison.render);
510
511 int64_t input_time_ms = comparison.reference.ntp_time_ms();
512
513 rtc::CritScope crit(&comparison_lock_);
514 if (graph_data_output_file_) {
515 samples_.push_back(
516 Sample(comparison.dropped, input_time_ms, comparison.send_time_ms,
ivica8d15bd62015-10-07 02:43:12 -0700517 comparison.recv_time_ms, comparison.render_time_ms,
518 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700519 }
520 psnr_.AddSample(psnr);
521 ssim_.AddSample(ssim);
522
523 if (comparison.dropped) {
524 ++dropped_frames_;
525 return;
526 }
527 if (last_render_time_ != 0)
528 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
529 last_render_time_ = comparison.render_time_ms;
530
531 sender_time_.AddSample(comparison.send_time_ms - input_time_ms);
532 receiver_time_.AddSample(comparison.render_time_ms -
533 comparison.recv_time_ms);
534 end_to_end_.AddSample(comparison.render_time_ms - input_time_ms);
535 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
536 }
537
538 void PrintResult(const char* result_type,
539 test::Statistics stats,
540 const char* unit) {
541 printf("RESULT %s: %s = {%f, %f}%s\n",
542 result_type,
543 test_label_.c_str(),
544 stats.Mean(),
545 stats.StandardDeviation(),
546 unit);
547 }
548
549 void PrintSamplesToFile(void) {
550 FILE* out = graph_data_output_file_;
551 rtc::CritScope crit(&comparison_lock_);
552 std::sort(samples_.begin(), samples_.end(),
553 [](const Sample& A, const Sample& B) -> bool {
554 return A.input_time_ms < B.input_time_ms;
555 });
556
sprangce4aef12015-11-02 07:23:20 -0800557 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700558 fprintf(out, "%" PRIuS "\n", samples_.size());
559 fprintf(out,
560 "dropped "
561 "input_time_ms "
562 "send_time_ms "
563 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700564 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700565 "encoded_frame_size "
566 "psnr "
567 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700568 "encode_time_ms\n");
569 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700570 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700571 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
572 int encode_time_ms;
573 if (it != samples_encode_time_ms_.end()) {
574 encode_time_ms = it->second;
575 } else {
576 ++missing_encode_time_samples;
577 encode_time_ms = -1;
578 }
579 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
580 " %lf %lf %d\n",
581 sample.dropped, sample.input_time_ms, sample.send_time_ms,
582 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700583 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700584 encode_time_ms);
585 }
586 if (missing_encode_time_samples) {
587 fprintf(stderr,
588 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
589 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700590 }
591 }
592
593 const std::string test_label_;
594 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800595 const std::string graph_title_;
596 const uint32_t ssrc_to_analyze_;
Peter Boströme4499152016-02-05 11:13:28 +0100597 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700598 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700599 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700600 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
601 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
602 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
603 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
604 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
605 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
606 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
607 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
608 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
609 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
610 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
611
612 const int frames_to_process_;
613 int frames_recorded_;
614 int frames_processed_;
615 int dropped_frames_;
616 int64_t last_render_time_;
617 uint32_t rtp_timestamp_delta_;
618
619 rtc::CriticalSection crit_;
620 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
621 VideoFrame last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800622 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
623 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
624 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
625 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
nissec7fe3c22016-04-20 03:25:36 -0700626 rtc::Optional<uint32_t> first_send_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700627 const double avg_psnr_threshold_;
628 const double avg_ssim_threshold_;
629
630 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100631 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
632 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +0100633 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -0700634 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +0100635 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -0700636};
637
ivica87f83a92015-10-08 05:13:32 -0700638VideoQualityTest::VideoQualityTest() : clock_(Clock::GetRealTimeClock()) {}
ivica5d6a06c2015-09-17 05:30:24 -0700639
640void VideoQualityTest::TestBody() {}
641
sprangce4aef12015-11-02 07:23:20 -0800642std::string VideoQualityTest::GenerateGraphTitle() const {
643 std::stringstream ss;
644 ss << params_.common.codec;
645 ss << " (" << params_.common.target_bitrate_bps / 1000 << "kbps";
646 ss << ", " << params_.common.fps << " FPS";
647 if (params_.screenshare.scroll_duration)
648 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
649 if (params_.ss.streams.size() > 1)
650 ss << ", Stream #" << params_.ss.selected_stream;
651 if (params_.ss.num_spatial_layers > 1)
652 ss << ", Layer #" << params_.ss.selected_sl;
653 ss << ")";
654 return ss.str();
655}
656
657void VideoQualityTest::CheckParams() {
658 // Add a default stream in none specified.
659 if (params_.ss.streams.empty())
660 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
661 if (params_.ss.num_spatial_layers == 0)
662 params_.ss.num_spatial_layers = 1;
663
664 if (params_.pipe.loss_percent != 0 ||
665 params_.pipe.queue_length_packets != 0) {
666 // Since LayerFilteringTransport changes the sequence numbers, we can't
667 // use that feature with pack loss, since the NACK request would end up
668 // retransmitting the wrong packets.
669 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800670 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800671 RTC_CHECK(params_.common.selected_tl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800672 params_.common.selected_tl ==
673 params_.common.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800674 }
675
676 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
677 // does in some parts of the code?
678 RTC_CHECK_GE(params_.common.max_bitrate_bps,
679 params_.common.target_bitrate_bps);
680 RTC_CHECK_GE(params_.common.target_bitrate_bps,
681 params_.common.min_bitrate_bps);
682 RTC_CHECK_LT(params_.common.selected_tl, params_.common.num_temporal_layers);
683 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
684 for (const VideoStream& stream : params_.ss.streams) {
685 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
686 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
687 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
688 RTC_CHECK_EQ(static_cast<int>(stream.temporal_layer_thresholds_bps.size()),
689 params_.common.num_temporal_layers - 1);
690 }
691 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
692 // the total bitrate? We anyway have to update them in the case bitrate
693 // estimator changes the total bitrates.
694 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
695 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
696 RTC_CHECK(params_.ss.spatial_layers.empty() ||
697 params_.ss.spatial_layers.size() ==
698 static_cast<size_t>(params_.ss.num_spatial_layers));
699 if (params_.common.codec == "VP8") {
700 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
701 } else if (params_.common.codec == "VP9") {
702 RTC_CHECK_EQ(params_.ss.streams.size(), 1u);
703 }
704}
705
706// Static.
707std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
708 // Parse comma separated nonnegative integers, where some elements may be
709 // empty. The empty values are replaced with -1.
710 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
711 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
712 std::vector<int> result;
713 if (str.empty())
714 return result;
715
716 const char* p = str.c_str();
717 int value = -1;
718 int pos;
719 while (*p) {
720 if (*p == ',') {
721 result.push_back(value);
722 value = -1;
723 ++p;
724 continue;
725 }
726 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
727 << "Unexpected non-number value.";
728 p += pos;
729 }
730 result.push_back(value);
731 return result;
732}
733
734// Static.
735VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
736 VideoStream stream;
737 stream.width = params.common.width;
738 stream.height = params.common.height;
739 stream.max_framerate = params.common.fps;
740 stream.min_bitrate_bps = params.common.min_bitrate_bps;
741 stream.target_bitrate_bps = params.common.target_bitrate_bps;
742 stream.max_bitrate_bps = params.common.max_bitrate_bps;
743 stream.max_qp = 52;
744 if (params.common.num_temporal_layers == 2)
745 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
746 return stream;
747}
748
749// Static.
750void VideoQualityTest::FillScalabilitySettings(
751 Params* params,
752 const std::vector<std::string>& stream_descriptors,
753 size_t selected_stream,
754 int num_spatial_layers,
755 int selected_sl,
756 const std::vector<std::string>& sl_descriptors) {
757 // Read VideoStream and SpatialLayer elements from a list of comma separated
758 // lists. To use a default value for an element, use -1 or leave empty.
759 // Validity checks performed in CheckParams.
760
761 RTC_CHECK(params->ss.streams.empty());
762 for (auto descriptor : stream_descriptors) {
763 if (descriptor.empty())
764 continue;
765 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
766 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
767 if (v[0] != -1)
768 stream.width = static_cast<size_t>(v[0]);
769 if (v[1] != -1)
770 stream.height = static_cast<size_t>(v[1]);
771 if (v[2] != -1)
772 stream.max_framerate = v[2];
773 if (v[3] != -1)
774 stream.min_bitrate_bps = v[3];
775 if (v[4] != -1)
776 stream.target_bitrate_bps = v[4];
777 if (v[5] != -1)
778 stream.max_bitrate_bps = v[5];
779 if (v.size() > 6 && v[6] != -1)
780 stream.max_qp = v[6];
781 if (v.size() > 7) {
782 stream.temporal_layer_thresholds_bps.clear();
783 stream.temporal_layer_thresholds_bps.insert(
784 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
785 } else {
786 // Automatic TL thresholds for more than two layers not supported.
787 RTC_CHECK_LE(params->common.num_temporal_layers, 2);
788 }
789 params->ss.streams.push_back(stream);
790 }
791 params->ss.selected_stream = selected_stream;
792
793 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
794 params->ss.selected_sl = selected_sl;
795 RTC_CHECK(params->ss.spatial_layers.empty());
796 for (auto descriptor : sl_descriptors) {
797 if (descriptor.empty())
798 continue;
799 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
800 RTC_CHECK_GT(v[2], 0);
801
802 SpatialLayer layer;
803 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
804 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
805 layer.target_bitrate_bps = v[2];
806 params->ss.spatial_layers.push_back(layer);
807 }
808}
809
810void VideoQualityTest::SetupCommon(Transport* send_transport,
811 Transport* recv_transport) {
812 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -0700813 trace_to_stderr_.reset(new test::TraceToStderr);
814
sprangce4aef12015-11-02 07:23:20 -0800815 size_t num_streams = params_.ss.streams.size();
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100816 CreateSendConfig(num_streams, 0, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -0700817
818 int payload_type;
hbosbab934b2016-01-27 01:36:03 -0800819 if (params_.common.codec == "H264") {
820 encoder_.reset(VideoEncoder::Create(VideoEncoder::kH264));
821 payload_type = kPayloadTypeH264;
822 } else if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700823 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp8));
824 payload_type = kPayloadTypeVP8;
sprangce4aef12015-11-02 07:23:20 -0800825 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700826 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp9));
827 payload_type = kPayloadTypeVP9;
828 } else {
829 RTC_NOTREACHED() << "Codec not supported!";
830 return;
831 }
stefanff483612015-12-21 03:14:00 -0800832 video_send_config_.encoder_settings.encoder = encoder_.get();
833 video_send_config_.encoder_settings.payload_name = params_.common.codec;
834 video_send_config_.encoder_settings.payload_type = payload_type;
835 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
836 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
sprangce4aef12015-11-02 07:23:20 -0800837 for (size_t i = 0; i < num_streams; ++i)
stefanff483612015-12-21 03:14:00 -0800838 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -0700839
stefanff483612015-12-21 03:14:00 -0800840 video_send_config_.rtp.extensions.clear();
sprangce4aef12015-11-02 07:23:20 -0800841 if (params_.common.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -0800842 video_send_config_.rtp.extensions.push_back(
Stefan Holmer12952972015-10-29 15:13:24 +0100843 RtpExtension(RtpExtension::kTransportSequenceNumber,
844 test::kTransportSequenceNumberExtensionId));
845 } else {
stefanff483612015-12-21 03:14:00 -0800846 video_send_config_.rtp.extensions.push_back(RtpExtension(
Stefan Holmer12952972015-10-29 15:13:24 +0100847 RtpExtension::kAbsSendTime, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +0200848 }
849
stefanff483612015-12-21 03:14:00 -0800850 video_encoder_config_.min_transmit_bitrate_bps =
851 params_.common.min_transmit_bps;
852 video_encoder_config_.streams = params_.ss.streams;
853 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -0700854
855 CreateMatchingReceiveConfigs(recv_transport);
856
sprangce4aef12015-11-02 07:23:20 -0800857 for (size_t i = 0; i < num_streams; ++i) {
stefanff483612015-12-21 03:14:00 -0800858 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 13:29:59 +0100859 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
860 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 07:23:20 -0800861 kSendRtxPayloadType;
stefanff483612015-12-21 03:14:00 -0800862 video_receive_configs_[i].rtp.transport_cc = params_.common.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -0800863 }
ivica5d6a06c2015-09-17 05:30:24 -0700864}
865
sprangce4aef12015-11-02 07:23:20 -0800866void VideoQualityTest::SetupScreenshare() {
867 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 05:30:24 -0700868
869 // Fill out codec settings.
stefanff483612015-12-21 03:14:00 -0800870 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
sprangce4aef12015-11-02 07:23:20 -0800871 if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700872 codec_settings_.VP8 = VideoEncoder::GetDefaultVp8Settings();
873 codec_settings_.VP8.denoisingOn = false;
874 codec_settings_.VP8.frameDroppingOn = false;
875 codec_settings_.VP8.numberOfTemporalLayers =
sprangce4aef12015-11-02 07:23:20 -0800876 static_cast<unsigned char>(params_.common.num_temporal_layers);
stefanff483612015-12-21 03:14:00 -0800877 video_encoder_config_.encoder_specific_settings = &codec_settings_.VP8;
sprangce4aef12015-11-02 07:23:20 -0800878 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700879 codec_settings_.VP9 = VideoEncoder::GetDefaultVp9Settings();
880 codec_settings_.VP9.denoisingOn = false;
881 codec_settings_.VP9.frameDroppingOn = false;
882 codec_settings_.VP9.numberOfTemporalLayers =
sprangce4aef12015-11-02 07:23:20 -0800883 static_cast<unsigned char>(params_.common.num_temporal_layers);
stefanff483612015-12-21 03:14:00 -0800884 video_encoder_config_.encoder_specific_settings = &codec_settings_.VP9;
sprangce4aef12015-11-02 07:23:20 -0800885 codec_settings_.VP9.numberOfSpatialLayers =
886 static_cast<unsigned char>(params_.ss.num_spatial_layers);
ivica5d6a06c2015-09-17 05:30:24 -0700887 }
888
889 // Setup frame generator.
890 const size_t kWidth = 1850;
891 const size_t kHeight = 1110;
892 std::vector<std::string> slides;
893 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
894 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
895 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
896 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
897
sprangce4aef12015-11-02 07:23:20 -0800898 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 05:30:24 -0700899 // Cycle image every slide_change_interval seconds.
900 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
901 slides, kWidth, kHeight,
sprangce4aef12015-11-02 07:23:20 -0800902 params_.screenshare.slide_change_interval * params_.common.fps));
ivica5d6a06c2015-09-17 05:30:24 -0700903 } else {
sprangce4aef12015-11-02 07:23:20 -0800904 RTC_CHECK_LE(params_.common.width, kWidth);
905 RTC_CHECK_LE(params_.common.height, kHeight);
906 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
907 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
908 params_.screenshare.scroll_duration) *
909 1000;
910 RTC_CHECK_LE(params_.screenshare.scroll_duration,
911 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 05:30:24 -0700912
sprangce4aef12015-11-02 07:23:20 -0800913 frame_generator_.reset(
914 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
915 clock_, slides, kWidth, kHeight, params_.common.width,
916 params_.common.height, params_.screenshare.scroll_duration * 1000,
917 kPauseDurationMs));
ivica5d6a06c2015-09-17 05:30:24 -0700918 }
919}
920
sprangce4aef12015-11-02 07:23:20 -0800921void VideoQualityTest::CreateCapturer(VideoCaptureInput* input) {
922 if (params_.screenshare.enabled) {
923 test::FrameGeneratorCapturer* frame_generator_capturer =
ivica2d4e6c52015-09-23 01:57:06 -0700924 new test::FrameGeneratorCapturer(
sprangce4aef12015-11-02 07:23:20 -0800925 clock_, input, frame_generator_.release(), params_.common.fps);
ivica2d4e6c52015-09-23 01:57:06 -0700926 EXPECT_TRUE(frame_generator_capturer->Init());
927 capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -0700928 } else {
sprangce4aef12015-11-02 07:23:20 -0800929 if (params_.video.clip_name.empty()) {
930 capturer_.reset(test::VideoCapturer::Create(input, params_.common.width,
931 params_.common.height,
932 params_.common.fps, clock_));
ivica5d6a06c2015-09-17 05:30:24 -0700933 } else {
ivica2d4e6c52015-09-23 01:57:06 -0700934 capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
sprangce4aef12015-11-02 07:23:20 -0800935 input, test::ResourcePath(params_.video.clip_name, "yuv"),
936 params_.common.width, params_.common.height, params_.common.fps,
ivica2d4e6c52015-09-23 01:57:06 -0700937 clock_));
Peter Boström74f6e9e2016-04-04 17:56:10 +0200938 ASSERT_TRUE(capturer_) << "Could not create capturer for "
939 << params_.video.clip_name
940 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -0700941 }
942 }
943}
944
sprang7a975f72015-10-12 06:33:21 -0700945void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -0800946 params_ = params;
947
ivica5d6a06c2015-09-17 05:30:24 -0700948 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
949 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -0800950 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -0700951
952 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -0800953 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -0700954 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -0800955 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +0200956 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -0800957 << "Can't open the file " << params_.analyzer.graph_data_output_filename
958 << "!";
ivica87f83a92015-10-08 05:13:32 -0700959 }
sprang7a975f72015-10-12 06:33:21 -0700960
stefanf116bd02015-10-27 08:29:42 -0700961 Call::Config call_config;
962 call_config.bitrate_config = params.common.call_bitrate_config;
963 CreateCalls(call_config, call_config);
964
ivica87f83a92015-10-08 05:13:32 -0700965 test::LayerFilteringTransport send_transport(
stefanf116bd02015-10-27 08:29:42 -0700966 params.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -0800967 params.common.selected_tl, params_.ss.selected_sl);
stefanf116bd02015-10-27 08:29:42 -0700968 test::DirectTransport recv_transport(params.pipe, receiver_call_.get());
969
sprangce4aef12015-11-02 07:23:20 -0800970 std::string graph_title = params_.analyzer.graph_title;
971 if (graph_title.empty())
972 graph_title = VideoQualityTest::GenerateGraphTitle();
973
974 // In the case of different resolutions, the functions calculating PSNR and
975 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
976 // aborts if the average psnr/ssim are below the given threshold, which is
977 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
978 // abort.
979 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
980 int selected_sl = params_.ss.selected_sl != -1
981 ? params_.ss.selected_sl
982 : params_.ss.num_spatial_layers - 1;
983 bool disable_quality_check =
984 selected_stream.width != params_.common.width ||
985 selected_stream.height != params_.common.height ||
986 (!params_.ss.spatial_layers.empty() &&
987 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
988 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
989 if (disable_quality_check) {
990 fprintf(stderr,
991 "Warning: Calculating PSNR and SSIM for downsized resolution "
992 "not implemented yet! Skipping PSNR and SSIM calculations!");
993 }
994
ivica5d6a06c2015-09-17 05:30:24 -0700995 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -0800996 &send_transport, params_.analyzer.test_label,
997 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
998 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
999 params_.analyzer.test_durations_secs * params_.common.fps,
1000 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001001 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 05:30:24 -07001002
ivica5d6a06c2015-09-17 05:30:24 -07001003 analyzer.SetReceiver(receiver_call_->Receiver());
1004 send_transport.SetReceiver(&analyzer);
1005 recv_transport.SetReceiver(sender_call_->Receiver());
1006
sprangce4aef12015-11-02 07:23:20 -08001007 SetupCommon(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001008 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
1009 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 05:30:24 -07001010 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 11:13:28 +01001011 RTC_DCHECK(!video_send_config_.post_encode_callback);
1012 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001013
sprangce4aef12015-11-02 07:23:20 -08001014 if (params_.screenshare.enabled)
1015 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001016
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001017 CreateVideoStreams();
stefanff483612015-12-21 03:14:00 -08001018 analyzer.input_ = video_send_stream_->Input();
1019 analyzer.send_stream_ = video_send_stream_;
ivica5d6a06c2015-09-17 05:30:24 -07001020
sprangce4aef12015-11-02 07:23:20 -08001021 CreateCapturer(&analyzer);
ivicac1cc8542015-10-08 03:44:06 -07001022
stefanff483612015-12-21 03:14:00 -08001023 video_send_stream_->Start();
1024 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1025 receive_stream->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001026 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001027
1028 analyzer.Wait();
1029
1030 send_transport.StopSending();
1031 recv_transport.StopSending();
1032
ivica2d4e6c52015-09-23 01:57:06 -07001033 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001034 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1035 receive_stream->Stop();
1036 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001037
1038 DestroyStreams();
1039
1040 if (graph_data_output_file)
1041 fclose(graph_data_output_file);
1042}
1043
sprang7a975f72015-10-12 06:33:21 -07001044void VideoQualityTest::RunWithVideoRenderer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001045 params_ = params;
1046 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001047
kwiberg27f982b2016-03-01 11:52:33 -08001048 std::unique_ptr<test::VideoRenderer> local_preview(
sprangce4aef12015-11-02 07:23:20 -08001049 test::VideoRenderer::Create("Local Preview", params_.common.width,
1050 params_.common.height));
1051 size_t stream_id = params_.ss.selected_stream;
mflodmand1590b22015-12-09 07:07:59 -08001052 std::string title = "Loopback Video";
1053 if (params_.ss.streams.size() > 1) {
1054 std::ostringstream s;
1055 s << stream_id;
1056 title += " - Stream #" + s.str();
sprangce4aef12015-11-02 07:23:20 -08001057 }
mflodmand1590b22015-12-09 07:07:59 -08001058
kwiberg27f982b2016-03-01 11:52:33 -08001059 std::unique_ptr<test::VideoRenderer> loopback_video(
mflodmand1590b22015-12-09 07:07:59 -08001060 test::VideoRenderer::Create(title.c_str(),
1061 params_.ss.streams[stream_id].width,
sprangce4aef12015-11-02 07:23:20 -08001062 params_.ss.streams[stream_id].height));
ivica5d6a06c2015-09-17 05:30:24 -07001063
1064 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1065 // match the full stack tests.
1066 Call::Config call_config;
sprangce4aef12015-11-02 07:23:20 -08001067 call_config.bitrate_config = params_.common.call_bitrate_config;
kwiberg27f982b2016-03-01 11:52:33 -08001068 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001069
1070 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001071 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -08001072 params.common.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001073 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1074 // least share as much code as possible. That way this test would also match
1075 // the full stack tests better.
1076 transport.SetReceiver(call->Receiver());
1077
sprangce4aef12015-11-02 07:23:20 -08001078 SetupCommon(&transport, &transport);
ivica87f83a92015-10-08 05:13:32 -07001079
stefanff483612015-12-21 03:14:00 -08001080 video_send_config_.local_renderer = local_preview.get();
1081 video_receive_configs_[stream_id].renderer = loopback_video.get();
sprangce4aef12015-11-02 07:23:20 -08001082
1083 if (params_.screenshare.enabled)
1084 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001085
stefanff483612015-12-21 03:14:00 -08001086 video_send_stream_ =
1087 call->CreateVideoSendStream(video_send_config_, video_encoder_config_);
ivica5d6a06c2015-09-17 05:30:24 -07001088 VideoReceiveStream* receive_stream =
stefanff483612015-12-21 03:14:00 -08001089 call->CreateVideoReceiveStream(video_receive_configs_[stream_id]);
1090 CreateCapturer(video_send_stream_->Input());
ivica5d6a06c2015-09-17 05:30:24 -07001091
1092 receive_stream->Start();
stefanff483612015-12-21 03:14:00 -08001093 video_send_stream_->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001094 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001095
1096 test::PressEnterToContinue();
1097
ivica2d4e6c52015-09-23 01:57:06 -07001098 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001099 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001100 receive_stream->Stop();
1101
1102 call->DestroyVideoReceiveStream(receive_stream);
stefanff483612015-12-21 03:14:00 -08001103 call->DestroyVideoSendStream(video_send_stream_);
ivica5d6a06c2015-09-17 05:30:24 -07001104
1105 transport.StopSending();
1106}
1107
1108} // namespace webrtc