blob: 84dbb5104e7c970a0634857cdb82b018e56c02a7 [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,
ivica5d6a06c2015-09-17 05:30:24 -070046 public VideoRenderer,
47 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 {
sprangce4aef12015-11-02 07:23:20 -0800118 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700119 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800120 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700121 {
122 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800123 int64_t timestamp = wrap_handler_.Unwrap(header.timestamp);
124 recv_times_[timestamp - rtp_timestamp_delta_] =
ivica5d6a06c2015-09-17 05:30:24 -0700125 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
126 }
127
128 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
129 }
130
Peter Boströme4499152016-02-05 11:13:28 +0100131 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700132 rtc::CritScope crit(&comparison_lock_);
133 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
134 }
135
ivica5d6a06c2015-09-17 05:30:24 -0700136 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
137 VideoFrame copy = video_frame;
138 copy.set_timestamp(copy.ntp_time_ms() * 90);
139
140 {
141 rtc::CritScope lock(&crit_);
142 if (first_send_frame_.IsZeroSize() && rtp_timestamp_delta_ == 0)
143 first_send_frame_ = copy;
144
145 frames_.push_back(copy);
146 }
147
148 input_->IncomingCapturedFrame(video_frame);
149 }
150
stefan1d8a5062015-10-02 03:39:33 -0700151 bool SendRtp(const uint8_t* packet,
152 size_t length,
153 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800154 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700155 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800156 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700157
sprangce4aef12015-11-02 07:23:20 -0800158 int64_t current_time =
159 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
160 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700161 {
162 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800163 int64_t timestamp = wrap_handler_.Unwrap(header.timestamp);
164
ivica5d6a06c2015-09-17 05:30:24 -0700165 if (rtp_timestamp_delta_ == 0) {
sprange1f2f1f2016-02-01 02:04:52 -0800166 rtp_timestamp_delta_ = timestamp - first_send_frame_.timestamp();
ivica5d6a06c2015-09-17 05:30:24 -0700167 first_send_frame_.Reset();
168 }
sprange1f2f1f2016-02-01 02:04:52 -0800169 timestamp -= rtp_timestamp_delta_;
sprangce4aef12015-11-02 07:23:20 -0800170 send_times_[timestamp] = current_time;
171 if (!transport_->DiscardedLastPacket() &&
172 header.ssrc == ssrc_to_analyze_) {
173 encoded_frame_sizes_[timestamp] +=
174 length - (header.headerLength + header.paddingLength);
175 }
ivica5d6a06c2015-09-17 05:30:24 -0700176 }
sprangce4aef12015-11-02 07:23:20 -0800177 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700178 }
179
180 bool SendRtcp(const uint8_t* packet, size_t length) override {
181 return transport_->SendRtcp(packet, length);
182 }
183
184 void EncodedFrameCallback(const EncodedFrame& frame) override {
185 rtc::CritScope lock(&comparison_lock_);
186 if (frames_recorded_ < frames_to_process_)
187 encoded_frame_size_.AddSample(frame.length_);
188 }
189
190 void RenderFrame(const VideoFrame& video_frame,
191 int time_to_render_ms) override {
192 int64_t render_time_ms =
193 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
194 uint32_t send_timestamp = video_frame.timestamp() - rtp_timestamp_delta_;
195
196 rtc::CritScope lock(&crit_);
197
198 while (frames_.front().timestamp() < send_timestamp) {
199 AddFrameComparison(frames_.front(), last_rendered_frame_, true,
200 render_time_ms);
201 frames_.pop_front();
202 }
203
204 VideoFrame reference_frame = frames_.front();
205 frames_.pop_front();
206 assert(!reference_frame.IsZeroSize());
sprangce4aef12015-11-02 07:23:20 -0800207 if (send_timestamp == reference_frame.timestamp() - 1) {
208 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100209 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800210 ++send_timestamp;
211 }
ivica5d6a06c2015-09-17 05:30:24 -0700212 EXPECT_EQ(reference_frame.timestamp(), send_timestamp);
213 assert(reference_frame.timestamp() == send_timestamp);
214
215 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
216
217 last_rendered_frame_ = video_frame;
218 }
219
220 bool IsTextureSupported() const override { return false; }
221
222 void Wait() {
223 // Frame comparisons can be very expensive. Wait for test to be done, but
224 // at time-out check if frames_processed is going up. If so, give it more
225 // time, otherwise fail. Hopefully this will reduce test flakiness.
226
Peter Boström8c38e8b2015-11-26 17:45:47 +0100227 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800228
ivica5d6a06c2015-09-17 05:30:24 -0700229 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700230 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100231 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700232 int frames_processed;
233 {
234 rtc::CritScope crit(&comparison_lock_);
235 frames_processed = frames_processed_;
236 }
237
238 // Print some output so test infrastructure won't think we've crashed.
239 const char* kKeepAliveMessages[3] = {
240 "Uh, I'm-I'm not quite dead, sir.",
241 "Uh, I-I think uh, I could pull through, sir.",
242 "Actually, I think I'm all right to come with you--"};
243 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
244
245 if (last_frames_processed == -1) {
246 last_frames_processed = frames_processed;
247 continue;
248 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100249 if (frames_processed == last_frames_processed) {
250 EXPECT_GT(frames_processed, last_frames_processed)
251 << "Analyzer stalled while waiting for test to finish.";
252 done_.Set();
253 break;
254 }
ivica5d6a06c2015-09-17 05:30:24 -0700255 last_frames_processed = frames_processed;
256 }
257
258 if (iteration > 0)
259 printf("- Farewell, sweet Concorde!\n");
260
Peter Boström8c38e8b2015-11-26 17:45:47 +0100261 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700262 }
263
Peter Boströme4499152016-02-05 11:13:28 +0100264 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
265
ivica5d6a06c2015-09-17 05:30:24 -0700266 VideoCaptureInput* input_;
sprangce4aef12015-11-02 07:23:20 -0800267 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700268 PacketReceiver* receiver_;
269 VideoSendStream* send_stream_;
270
271 private:
272 struct FrameComparison {
273 FrameComparison()
274 : dropped(false),
275 send_time_ms(0),
276 recv_time_ms(0),
277 render_time_ms(0),
278 encoded_frame_size(0) {}
279
280 FrameComparison(const VideoFrame& reference,
281 const VideoFrame& render,
282 bool dropped,
283 int64_t send_time_ms,
284 int64_t recv_time_ms,
285 int64_t render_time_ms,
286 size_t encoded_frame_size)
287 : reference(reference),
288 render(render),
289 dropped(dropped),
290 send_time_ms(send_time_ms),
291 recv_time_ms(recv_time_ms),
292 render_time_ms(render_time_ms),
293 encoded_frame_size(encoded_frame_size) {}
294
295 VideoFrame reference;
296 VideoFrame render;
297 bool dropped;
298 int64_t send_time_ms;
299 int64_t recv_time_ms;
300 int64_t render_time_ms;
301 size_t encoded_frame_size;
302 };
303
304 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700305 Sample(int dropped,
306 int64_t input_time_ms,
307 int64_t send_time_ms,
308 int64_t recv_time_ms,
309 int64_t render_time_ms,
310 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700311 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700312 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700313 : dropped(dropped),
314 input_time_ms(input_time_ms),
315 send_time_ms(send_time_ms),
316 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700317 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700318 encoded_frame_size(encoded_frame_size),
319 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700320 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700321
ivica8d15bd62015-10-07 02:43:12 -0700322 int dropped;
323 int64_t input_time_ms;
324 int64_t send_time_ms;
325 int64_t recv_time_ms;
326 int64_t render_time_ms;
327 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700328 double psnr;
329 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700330 };
331
Peter Boströme4499152016-02-05 11:13:28 +0100332 // This class receives the send-side OnEncodeTiming and is provided to not
333 // conflict with the receiver-side pre_decode_callback.
334 class OnEncodeTimingProxy : public EncodedFrameObserver {
335 public:
336 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
337
338 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
339 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
340 }
341 void EncodedFrameCallback(const EncodedFrame& frame) override {}
342
343 private:
344 VideoAnalyzer* const parent_;
345 };
346
ivica5d6a06c2015-09-17 05:30:24 -0700347 void AddFrameComparison(const VideoFrame& reference,
348 const VideoFrame& render,
349 bool dropped,
350 int64_t render_time_ms)
351 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800352 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
353 int64_t send_time_ms = send_times_[reference_timestamp];
354 send_times_.erase(reference_timestamp);
355 int64_t recv_time_ms = recv_times_[reference_timestamp];
356 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700357
sprangce4aef12015-11-02 07:23:20 -0800358 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800359 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800360 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800361 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800362 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
363 if (it != encoded_frame_sizes_.end())
364 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700365
366 VideoFrame reference_copy;
367 VideoFrame render_copy;
368 reference_copy.CopyFrame(reference);
369 render_copy.CopyFrame(render);
370
371 rtc::CritScope crit(&comparison_lock_);
372 comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped,
373 send_time_ms, recv_time_ms,
374 render_time_ms, encoded_size));
Peter Boström5811a392015-12-10 13:02:50 +0100375 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700376 }
377
378 static bool PollStatsThread(void* obj) {
379 return static_cast<VideoAnalyzer*>(obj)->PollStats();
380 }
381
382 bool PollStats() {
Peter Boströmdd45eb62016-01-19 15:22:32 +0100383 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 13:02:50 +0100384 return false;
ivica5d6a06c2015-09-17 05:30:24 -0700385
386 VideoSendStream::Stats stats = send_stream_->GetStats();
387
388 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 17:30:49 +0100389 // It's not certain that we yet have estimates for any of these stats. Check
390 // that they are positive before mixing them in.
391 if (stats.encode_frame_rate > 0)
392 encode_frame_rate_.AddSample(stats.encode_frame_rate);
393 if (stats.avg_encode_time_ms > 0)
394 encode_time_ms.AddSample(stats.avg_encode_time_ms);
395 if (stats.encode_usage_percent > 0)
396 encode_usage_percent.AddSample(stats.encode_usage_percent);
397 if (stats.media_bitrate_bps > 0)
398 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 05:30:24 -0700399
400 return true;
401 }
402
403 static bool FrameComparisonThread(void* obj) {
404 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
405 }
406
407 bool CompareFrames() {
408 if (AllFramesRecorded())
409 return false;
410
411 VideoFrame reference;
412 VideoFrame render;
413 FrameComparison comparison;
414
415 if (!PopComparison(&comparison)) {
416 // Wait until new comparison task is available, or test is done.
417 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100418 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700419 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100420 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700421 return false;
422 }
423 return true; // Try again.
424 }
425
426 PerformFrameComparison(comparison);
427
428 if (FrameProcessed()) {
429 PrintResults();
430 if (graph_data_output_file_)
431 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100432 done_.Set();
433 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700434 return false;
435 }
436
437 return true;
438 }
439
440 bool PopComparison(FrameComparison* comparison) {
441 rtc::CritScope crit(&comparison_lock_);
442 // If AllFramesRecorded() is true, it means we have already popped
443 // frames_to_process_ frames from comparisons_, so there is no more work
444 // for this thread to be done. frames_processed_ might still be lower if
445 // all comparisons are not done, but those frames are currently being
446 // worked on by other threads.
447 if (comparisons_.empty() || AllFramesRecorded())
448 return false;
449
450 *comparison = comparisons_.front();
451 comparisons_.pop_front();
452
453 FrameRecorded();
454 return true;
455 }
456
457 // Increment counter for number of frames received for comparison.
458 void FrameRecorded() {
459 rtc::CritScope crit(&comparison_lock_);
460 ++frames_recorded_;
461 }
462
463 // Returns true if all frames to be compared have been taken from the queue.
464 bool AllFramesRecorded() {
465 rtc::CritScope crit(&comparison_lock_);
466 assert(frames_recorded_ <= frames_to_process_);
467 return frames_recorded_ == frames_to_process_;
468 }
469
470 // Increase count of number of frames processed. Returns true if this was the
471 // last frame to be processed.
472 bool FrameProcessed() {
473 rtc::CritScope crit(&comparison_lock_);
474 ++frames_processed_;
475 assert(frames_processed_ <= frames_to_process_);
476 return frames_processed_ == frames_to_process_;
477 }
478
479 void PrintResults() {
480 rtc::CritScope crit(&comparison_lock_);
481 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800482 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700483 PrintResult("sender_time", sender_time_, " ms");
484 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
485 dropped_frames_);
486 PrintResult("receiver_time", receiver_time_, " ms");
487 PrintResult("total_delay_incl_network", end_to_end_, " ms");
488 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
489 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
490 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
491 PrintResult("encode_time", encode_time_ms, " ms");
492 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
493 PrintResult("media_bitrate", media_bitrate_bps, " bps");
494
495 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
496 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
497 }
498
499 void PerformFrameComparison(const FrameComparison& comparison) {
500 // Perform expensive psnr and ssim calculations while not holding lock.
501 double psnr = I420PSNR(&comparison.reference, &comparison.render);
502 double ssim = I420SSIM(&comparison.reference, &comparison.render);
503
504 int64_t input_time_ms = comparison.reference.ntp_time_ms();
505
506 rtc::CritScope crit(&comparison_lock_);
507 if (graph_data_output_file_) {
508 samples_.push_back(
509 Sample(comparison.dropped, input_time_ms, comparison.send_time_ms,
ivica8d15bd62015-10-07 02:43:12 -0700510 comparison.recv_time_ms, comparison.render_time_ms,
511 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700512 }
513 psnr_.AddSample(psnr);
514 ssim_.AddSample(ssim);
515
516 if (comparison.dropped) {
517 ++dropped_frames_;
518 return;
519 }
520 if (last_render_time_ != 0)
521 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
522 last_render_time_ = comparison.render_time_ms;
523
524 sender_time_.AddSample(comparison.send_time_ms - input_time_ms);
525 receiver_time_.AddSample(comparison.render_time_ms -
526 comparison.recv_time_ms);
527 end_to_end_.AddSample(comparison.render_time_ms - input_time_ms);
528 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
529 }
530
531 void PrintResult(const char* result_type,
532 test::Statistics stats,
533 const char* unit) {
534 printf("RESULT %s: %s = {%f, %f}%s\n",
535 result_type,
536 test_label_.c_str(),
537 stats.Mean(),
538 stats.StandardDeviation(),
539 unit);
540 }
541
542 void PrintSamplesToFile(void) {
543 FILE* out = graph_data_output_file_;
544 rtc::CritScope crit(&comparison_lock_);
545 std::sort(samples_.begin(), samples_.end(),
546 [](const Sample& A, const Sample& B) -> bool {
547 return A.input_time_ms < B.input_time_ms;
548 });
549
sprangce4aef12015-11-02 07:23:20 -0800550 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700551 fprintf(out, "%" PRIuS "\n", samples_.size());
552 fprintf(out,
553 "dropped "
554 "input_time_ms "
555 "send_time_ms "
556 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700557 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700558 "encoded_frame_size "
559 "psnr "
560 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700561 "encode_time_ms\n");
562 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700563 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700564 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
565 int encode_time_ms;
566 if (it != samples_encode_time_ms_.end()) {
567 encode_time_ms = it->second;
568 } else {
569 ++missing_encode_time_samples;
570 encode_time_ms = -1;
571 }
572 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
573 " %lf %lf %d\n",
574 sample.dropped, sample.input_time_ms, sample.send_time_ms,
575 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700576 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700577 encode_time_ms);
578 }
579 if (missing_encode_time_samples) {
580 fprintf(stderr,
581 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
582 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700583 }
584 }
585
586 const std::string test_label_;
587 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800588 const std::string graph_title_;
589 const uint32_t ssrc_to_analyze_;
Peter Boströme4499152016-02-05 11:13:28 +0100590 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700591 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700592 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700593 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
594 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
595 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
596 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
597 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
598 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
599 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
600 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
601 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
602 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
603 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
604
605 const int frames_to_process_;
606 int frames_recorded_;
607 int frames_processed_;
608 int dropped_frames_;
609 int64_t last_render_time_;
610 uint32_t rtp_timestamp_delta_;
611
612 rtc::CriticalSection crit_;
613 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
614 VideoFrame last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800615 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
616 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
617 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
618 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700619 VideoFrame first_send_frame_ GUARDED_BY(crit_);
620 const double avg_psnr_threshold_;
621 const double avg_ssim_threshold_;
622
623 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100624 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
625 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +0100626 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -0700627 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +0100628 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -0700629};
630
ivica87f83a92015-10-08 05:13:32 -0700631VideoQualityTest::VideoQualityTest() : clock_(Clock::GetRealTimeClock()) {}
ivica5d6a06c2015-09-17 05:30:24 -0700632
633void VideoQualityTest::TestBody() {}
634
sprangce4aef12015-11-02 07:23:20 -0800635std::string VideoQualityTest::GenerateGraphTitle() const {
636 std::stringstream ss;
637 ss << params_.common.codec;
638 ss << " (" << params_.common.target_bitrate_bps / 1000 << "kbps";
639 ss << ", " << params_.common.fps << " FPS";
640 if (params_.screenshare.scroll_duration)
641 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
642 if (params_.ss.streams.size() > 1)
643 ss << ", Stream #" << params_.ss.selected_stream;
644 if (params_.ss.num_spatial_layers > 1)
645 ss << ", Layer #" << params_.ss.selected_sl;
646 ss << ")";
647 return ss.str();
648}
649
650void VideoQualityTest::CheckParams() {
651 // Add a default stream in none specified.
652 if (params_.ss.streams.empty())
653 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
654 if (params_.ss.num_spatial_layers == 0)
655 params_.ss.num_spatial_layers = 1;
656
657 if (params_.pipe.loss_percent != 0 ||
658 params_.pipe.queue_length_packets != 0) {
659 // Since LayerFilteringTransport changes the sequence numbers, we can't
660 // use that feature with pack loss, since the NACK request would end up
661 // retransmitting the wrong packets.
662 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800663 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800664 RTC_CHECK(params_.common.selected_tl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800665 params_.common.selected_tl ==
666 params_.common.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800667 }
668
669 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
670 // does in some parts of the code?
671 RTC_CHECK_GE(params_.common.max_bitrate_bps,
672 params_.common.target_bitrate_bps);
673 RTC_CHECK_GE(params_.common.target_bitrate_bps,
674 params_.common.min_bitrate_bps);
675 RTC_CHECK_LT(params_.common.selected_tl, params_.common.num_temporal_layers);
676 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
677 for (const VideoStream& stream : params_.ss.streams) {
678 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
679 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
680 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
681 RTC_CHECK_EQ(static_cast<int>(stream.temporal_layer_thresholds_bps.size()),
682 params_.common.num_temporal_layers - 1);
683 }
684 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
685 // the total bitrate? We anyway have to update them in the case bitrate
686 // estimator changes the total bitrates.
687 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
688 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
689 RTC_CHECK(params_.ss.spatial_layers.empty() ||
690 params_.ss.spatial_layers.size() ==
691 static_cast<size_t>(params_.ss.num_spatial_layers));
692 if (params_.common.codec == "VP8") {
693 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
694 } else if (params_.common.codec == "VP9") {
695 RTC_CHECK_EQ(params_.ss.streams.size(), 1u);
696 }
697}
698
699// Static.
700std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
701 // Parse comma separated nonnegative integers, where some elements may be
702 // empty. The empty values are replaced with -1.
703 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
704 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
705 std::vector<int> result;
706 if (str.empty())
707 return result;
708
709 const char* p = str.c_str();
710 int value = -1;
711 int pos;
712 while (*p) {
713 if (*p == ',') {
714 result.push_back(value);
715 value = -1;
716 ++p;
717 continue;
718 }
719 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
720 << "Unexpected non-number value.";
721 p += pos;
722 }
723 result.push_back(value);
724 return result;
725}
726
727// Static.
728VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
729 VideoStream stream;
730 stream.width = params.common.width;
731 stream.height = params.common.height;
732 stream.max_framerate = params.common.fps;
733 stream.min_bitrate_bps = params.common.min_bitrate_bps;
734 stream.target_bitrate_bps = params.common.target_bitrate_bps;
735 stream.max_bitrate_bps = params.common.max_bitrate_bps;
736 stream.max_qp = 52;
737 if (params.common.num_temporal_layers == 2)
738 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
739 return stream;
740}
741
742// Static.
743void VideoQualityTest::FillScalabilitySettings(
744 Params* params,
745 const std::vector<std::string>& stream_descriptors,
746 size_t selected_stream,
747 int num_spatial_layers,
748 int selected_sl,
749 const std::vector<std::string>& sl_descriptors) {
750 // Read VideoStream and SpatialLayer elements from a list of comma separated
751 // lists. To use a default value for an element, use -1 or leave empty.
752 // Validity checks performed in CheckParams.
753
754 RTC_CHECK(params->ss.streams.empty());
755 for (auto descriptor : stream_descriptors) {
756 if (descriptor.empty())
757 continue;
758 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
759 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
760 if (v[0] != -1)
761 stream.width = static_cast<size_t>(v[0]);
762 if (v[1] != -1)
763 stream.height = static_cast<size_t>(v[1]);
764 if (v[2] != -1)
765 stream.max_framerate = v[2];
766 if (v[3] != -1)
767 stream.min_bitrate_bps = v[3];
768 if (v[4] != -1)
769 stream.target_bitrate_bps = v[4];
770 if (v[5] != -1)
771 stream.max_bitrate_bps = v[5];
772 if (v.size() > 6 && v[6] != -1)
773 stream.max_qp = v[6];
774 if (v.size() > 7) {
775 stream.temporal_layer_thresholds_bps.clear();
776 stream.temporal_layer_thresholds_bps.insert(
777 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
778 } else {
779 // Automatic TL thresholds for more than two layers not supported.
780 RTC_CHECK_LE(params->common.num_temporal_layers, 2);
781 }
782 params->ss.streams.push_back(stream);
783 }
784 params->ss.selected_stream = selected_stream;
785
786 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
787 params->ss.selected_sl = selected_sl;
788 RTC_CHECK(params->ss.spatial_layers.empty());
789 for (auto descriptor : sl_descriptors) {
790 if (descriptor.empty())
791 continue;
792 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
793 RTC_CHECK_GT(v[2], 0);
794
795 SpatialLayer layer;
796 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
797 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
798 layer.target_bitrate_bps = v[2];
799 params->ss.spatial_layers.push_back(layer);
800 }
801}
802
803void VideoQualityTest::SetupCommon(Transport* send_transport,
804 Transport* recv_transport) {
805 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -0700806 trace_to_stderr_.reset(new test::TraceToStderr);
807
sprangce4aef12015-11-02 07:23:20 -0800808 size_t num_streams = params_.ss.streams.size();
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100809 CreateSendConfig(num_streams, 0, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -0700810
811 int payload_type;
hbosbab934b2016-01-27 01:36:03 -0800812 if (params_.common.codec == "H264") {
813 encoder_.reset(VideoEncoder::Create(VideoEncoder::kH264));
814 payload_type = kPayloadTypeH264;
815 } else if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700816 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp8));
817 payload_type = kPayloadTypeVP8;
sprangce4aef12015-11-02 07:23:20 -0800818 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700819 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp9));
820 payload_type = kPayloadTypeVP9;
821 } else {
822 RTC_NOTREACHED() << "Codec not supported!";
823 return;
824 }
stefanff483612015-12-21 03:14:00 -0800825 video_send_config_.encoder_settings.encoder = encoder_.get();
826 video_send_config_.encoder_settings.payload_name = params_.common.codec;
827 video_send_config_.encoder_settings.payload_type = payload_type;
828 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
829 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
sprangce4aef12015-11-02 07:23:20 -0800830 for (size_t i = 0; i < num_streams; ++i)
stefanff483612015-12-21 03:14:00 -0800831 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -0700832
stefanff483612015-12-21 03:14:00 -0800833 video_send_config_.rtp.extensions.clear();
sprangce4aef12015-11-02 07:23:20 -0800834 if (params_.common.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -0800835 video_send_config_.rtp.extensions.push_back(
Stefan Holmer12952972015-10-29 15:13:24 +0100836 RtpExtension(RtpExtension::kTransportSequenceNumber,
837 test::kTransportSequenceNumberExtensionId));
838 } else {
stefanff483612015-12-21 03:14:00 -0800839 video_send_config_.rtp.extensions.push_back(RtpExtension(
Stefan Holmer12952972015-10-29 15:13:24 +0100840 RtpExtension::kAbsSendTime, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +0200841 }
842
stefanff483612015-12-21 03:14:00 -0800843 video_encoder_config_.min_transmit_bitrate_bps =
844 params_.common.min_transmit_bps;
845 video_encoder_config_.streams = params_.ss.streams;
846 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -0700847
848 CreateMatchingReceiveConfigs(recv_transport);
849
sprangce4aef12015-11-02 07:23:20 -0800850 for (size_t i = 0; i < num_streams; ++i) {
stefanff483612015-12-21 03:14:00 -0800851 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 13:29:59 +0100852 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
853 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 07:23:20 -0800854 kSendRtxPayloadType;
stefanff483612015-12-21 03:14:00 -0800855 video_receive_configs_[i].rtp.transport_cc = params_.common.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -0800856 }
ivica5d6a06c2015-09-17 05:30:24 -0700857}
858
sprangce4aef12015-11-02 07:23:20 -0800859void VideoQualityTest::SetupScreenshare() {
860 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 05:30:24 -0700861
862 // Fill out codec settings.
stefanff483612015-12-21 03:14:00 -0800863 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
sprangce4aef12015-11-02 07:23:20 -0800864 if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700865 codec_settings_.VP8 = VideoEncoder::GetDefaultVp8Settings();
866 codec_settings_.VP8.denoisingOn = false;
867 codec_settings_.VP8.frameDroppingOn = false;
868 codec_settings_.VP8.numberOfTemporalLayers =
sprangce4aef12015-11-02 07:23:20 -0800869 static_cast<unsigned char>(params_.common.num_temporal_layers);
stefanff483612015-12-21 03:14:00 -0800870 video_encoder_config_.encoder_specific_settings = &codec_settings_.VP8;
sprangce4aef12015-11-02 07:23:20 -0800871 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700872 codec_settings_.VP9 = VideoEncoder::GetDefaultVp9Settings();
873 codec_settings_.VP9.denoisingOn = false;
874 codec_settings_.VP9.frameDroppingOn = false;
875 codec_settings_.VP9.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_.VP9;
sprangce4aef12015-11-02 07:23:20 -0800878 codec_settings_.VP9.numberOfSpatialLayers =
879 static_cast<unsigned char>(params_.ss.num_spatial_layers);
ivica5d6a06c2015-09-17 05:30:24 -0700880 }
881
882 // Setup frame generator.
883 const size_t kWidth = 1850;
884 const size_t kHeight = 1110;
885 std::vector<std::string> slides;
886 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
887 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
888 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
889 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
890
sprangce4aef12015-11-02 07:23:20 -0800891 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 05:30:24 -0700892 // Cycle image every slide_change_interval seconds.
893 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
894 slides, kWidth, kHeight,
sprangce4aef12015-11-02 07:23:20 -0800895 params_.screenshare.slide_change_interval * params_.common.fps));
ivica5d6a06c2015-09-17 05:30:24 -0700896 } else {
sprangce4aef12015-11-02 07:23:20 -0800897 RTC_CHECK_LE(params_.common.width, kWidth);
898 RTC_CHECK_LE(params_.common.height, kHeight);
899 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
900 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
901 params_.screenshare.scroll_duration) *
902 1000;
903 RTC_CHECK_LE(params_.screenshare.scroll_duration,
904 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 05:30:24 -0700905
sprangce4aef12015-11-02 07:23:20 -0800906 frame_generator_.reset(
907 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
908 clock_, slides, kWidth, kHeight, params_.common.width,
909 params_.common.height, params_.screenshare.scroll_duration * 1000,
910 kPauseDurationMs));
ivica5d6a06c2015-09-17 05:30:24 -0700911 }
912}
913
sprangce4aef12015-11-02 07:23:20 -0800914void VideoQualityTest::CreateCapturer(VideoCaptureInput* input) {
915 if (params_.screenshare.enabled) {
916 test::FrameGeneratorCapturer* frame_generator_capturer =
ivica2d4e6c52015-09-23 01:57:06 -0700917 new test::FrameGeneratorCapturer(
sprangce4aef12015-11-02 07:23:20 -0800918 clock_, input, frame_generator_.release(), params_.common.fps);
ivica2d4e6c52015-09-23 01:57:06 -0700919 EXPECT_TRUE(frame_generator_capturer->Init());
920 capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -0700921 } else {
sprangce4aef12015-11-02 07:23:20 -0800922 if (params_.video.clip_name.empty()) {
923 capturer_.reset(test::VideoCapturer::Create(input, params_.common.width,
924 params_.common.height,
925 params_.common.fps, clock_));
ivica5d6a06c2015-09-17 05:30:24 -0700926 } else {
ivica2d4e6c52015-09-23 01:57:06 -0700927 capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
sprangce4aef12015-11-02 07:23:20 -0800928 input, test::ResourcePath(params_.video.clip_name, "yuv"),
929 params_.common.width, params_.common.height, params_.common.fps,
ivica2d4e6c52015-09-23 01:57:06 -0700930 clock_));
931 ASSERT_TRUE(capturer_.get() != nullptr)
sprangce4aef12015-11-02 07:23:20 -0800932 << "Could not create capturer for " << params_.video.clip_name
ivica5d6a06c2015-09-17 05:30:24 -0700933 << ".yuv. Is this resource file present?";
934 }
935 }
936}
937
sprang7a975f72015-10-12 06:33:21 -0700938void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -0800939 params_ = params;
940
ivica5d6a06c2015-09-17 05:30:24 -0700941 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
942 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -0800943 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -0700944
945 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -0800946 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -0700947 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -0800948 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
ivica5d6a06c2015-09-17 05:30:24 -0700949 RTC_CHECK(graph_data_output_file != nullptr)
sprangce4aef12015-11-02 07:23:20 -0800950 << "Can't open the file " << params_.analyzer.graph_data_output_filename
951 << "!";
ivica87f83a92015-10-08 05:13:32 -0700952 }
sprang7a975f72015-10-12 06:33:21 -0700953
stefanf116bd02015-10-27 08:29:42 -0700954 Call::Config call_config;
955 call_config.bitrate_config = params.common.call_bitrate_config;
956 CreateCalls(call_config, call_config);
957
ivica87f83a92015-10-08 05:13:32 -0700958 test::LayerFilteringTransport send_transport(
stefanf116bd02015-10-27 08:29:42 -0700959 params.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -0800960 params.common.selected_tl, params_.ss.selected_sl);
stefanf116bd02015-10-27 08:29:42 -0700961 test::DirectTransport recv_transport(params.pipe, receiver_call_.get());
962
sprangce4aef12015-11-02 07:23:20 -0800963 std::string graph_title = params_.analyzer.graph_title;
964 if (graph_title.empty())
965 graph_title = VideoQualityTest::GenerateGraphTitle();
966
967 // In the case of different resolutions, the functions calculating PSNR and
968 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
969 // aborts if the average psnr/ssim are below the given threshold, which is
970 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
971 // abort.
972 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
973 int selected_sl = params_.ss.selected_sl != -1
974 ? params_.ss.selected_sl
975 : params_.ss.num_spatial_layers - 1;
976 bool disable_quality_check =
977 selected_stream.width != params_.common.width ||
978 selected_stream.height != params_.common.height ||
979 (!params_.ss.spatial_layers.empty() &&
980 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
981 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
982 if (disable_quality_check) {
983 fprintf(stderr,
984 "Warning: Calculating PSNR and SSIM for downsized resolution "
985 "not implemented yet! Skipping PSNR and SSIM calculations!");
986 }
987
ivica5d6a06c2015-09-17 05:30:24 -0700988 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -0800989 &send_transport, params_.analyzer.test_label,
990 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
991 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
992 params_.analyzer.test_durations_secs * params_.common.fps,
993 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100994 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 05:30:24 -0700995
ivica5d6a06c2015-09-17 05:30:24 -0700996 analyzer.SetReceiver(receiver_call_->Receiver());
997 send_transport.SetReceiver(&analyzer);
998 recv_transport.SetReceiver(sender_call_->Receiver());
999
sprangce4aef12015-11-02 07:23:20 -08001000 SetupCommon(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001001 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
1002 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 05:30:24 -07001003 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 11:13:28 +01001004 RTC_DCHECK(!video_send_config_.post_encode_callback);
1005 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001006
sprangce4aef12015-11-02 07:23:20 -08001007 if (params_.screenshare.enabled)
1008 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001009
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001010 CreateVideoStreams();
stefanff483612015-12-21 03:14:00 -08001011 analyzer.input_ = video_send_stream_->Input();
1012 analyzer.send_stream_ = video_send_stream_;
ivica5d6a06c2015-09-17 05:30:24 -07001013
sprangce4aef12015-11-02 07:23:20 -08001014 CreateCapturer(&analyzer);
ivicac1cc8542015-10-08 03:44:06 -07001015
stefanff483612015-12-21 03:14:00 -08001016 video_send_stream_->Start();
1017 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1018 receive_stream->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001019 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001020
1021 analyzer.Wait();
1022
1023 send_transport.StopSending();
1024 recv_transport.StopSending();
1025
ivica2d4e6c52015-09-23 01:57:06 -07001026 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001027 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1028 receive_stream->Stop();
1029 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001030
1031 DestroyStreams();
1032
1033 if (graph_data_output_file)
1034 fclose(graph_data_output_file);
1035}
1036
sprang7a975f72015-10-12 06:33:21 -07001037void VideoQualityTest::RunWithVideoRenderer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001038 params_ = params;
1039 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001040
kwiberg27f982b2016-03-01 11:52:33 -08001041 std::unique_ptr<test::VideoRenderer> local_preview(
sprangce4aef12015-11-02 07:23:20 -08001042 test::VideoRenderer::Create("Local Preview", params_.common.width,
1043 params_.common.height));
1044 size_t stream_id = params_.ss.selected_stream;
mflodmand1590b22015-12-09 07:07:59 -08001045 std::string title = "Loopback Video";
1046 if (params_.ss.streams.size() > 1) {
1047 std::ostringstream s;
1048 s << stream_id;
1049 title += " - Stream #" + s.str();
sprangce4aef12015-11-02 07:23:20 -08001050 }
mflodmand1590b22015-12-09 07:07:59 -08001051
kwiberg27f982b2016-03-01 11:52:33 -08001052 std::unique_ptr<test::VideoRenderer> loopback_video(
mflodmand1590b22015-12-09 07:07:59 -08001053 test::VideoRenderer::Create(title.c_str(),
1054 params_.ss.streams[stream_id].width,
sprangce4aef12015-11-02 07:23:20 -08001055 params_.ss.streams[stream_id].height));
ivica5d6a06c2015-09-17 05:30:24 -07001056
1057 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1058 // match the full stack tests.
1059 Call::Config call_config;
sprangce4aef12015-11-02 07:23:20 -08001060 call_config.bitrate_config = params_.common.call_bitrate_config;
kwiberg27f982b2016-03-01 11:52:33 -08001061 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001062
1063 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001064 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -08001065 params.common.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001066 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1067 // least share as much code as possible. That way this test would also match
1068 // the full stack tests better.
1069 transport.SetReceiver(call->Receiver());
1070
sprangce4aef12015-11-02 07:23:20 -08001071 SetupCommon(&transport, &transport);
ivica87f83a92015-10-08 05:13:32 -07001072
stefanff483612015-12-21 03:14:00 -08001073 video_send_config_.local_renderer = local_preview.get();
1074 video_receive_configs_[stream_id].renderer = loopback_video.get();
sprangce4aef12015-11-02 07:23:20 -08001075
1076 if (params_.screenshare.enabled)
1077 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001078
stefanff483612015-12-21 03:14:00 -08001079 video_send_stream_ =
1080 call->CreateVideoSendStream(video_send_config_, video_encoder_config_);
ivica5d6a06c2015-09-17 05:30:24 -07001081 VideoReceiveStream* receive_stream =
stefanff483612015-12-21 03:14:00 -08001082 call->CreateVideoReceiveStream(video_receive_configs_[stream_id]);
1083 CreateCapturer(video_send_stream_->Input());
ivica5d6a06c2015-09-17 05:30:24 -07001084
1085 receive_stream->Start();
stefanff483612015-12-21 03:14:00 -08001086 video_send_stream_->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001087 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001088
1089 test::PressEnterToContinue();
1090
ivica2d4e6c52015-09-23 01:57:06 -07001091 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001092 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001093 receive_stream->Stop();
1094
1095 call->DestroyVideoReceiveStream(receive_stream);
stefanff483612015-12-21 03:14:00 -08001096 call->DestroyVideoSendStream(video_send_stream_);
ivica5d6a06c2015-09-17 05:30:24 -07001097
1098 transport.StopSending();
1099}
1100
1101} // namespace webrtc