blob: a401d6dd42aec5a7dc8a143b77de3e656b3672ec [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;
stefanb1797672016-08-11 07:00:57 -070044static const size_t kMaxComparisons = 10;
ivica5d6a06c2015-09-17 05:30:24 -070045
46class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -070047 public Transport,
nisse7ade7b32016-03-23 04:48:10 -070048 public rtc::VideoSinkInterface<VideoFrame>,
ivica5d6a06c2015-09-17 05:30:24 -070049 public VideoCaptureInput,
Peter Boströme4499152016-02-05 11:13:28 +010050 public EncodedFrameObserver {
ivica5d6a06c2015-09-17 05:30:24 -070051 public:
sprangce4aef12015-11-02 07:23:20 -080052 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -070053 const std::string& test_label,
54 double avg_psnr_threshold,
55 double avg_ssim_threshold,
56 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -080057 FILE* graph_data_output_file,
58 const std::string& graph_title,
59 uint32_t ssrc_to_analyze)
ivica8d15bd62015-10-07 02:43:12 -070060 : input_(nullptr),
ivica5d6a06c2015-09-17 05:30:24 -070061 transport_(transport),
62 receiver_(nullptr),
63 send_stream_(nullptr),
64 test_label_(test_label),
65 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -080066 graph_title_(graph_title),
67 ssrc_to_analyze_(ssrc_to_analyze),
pbos14fe7082016-04-20 06:35:56 -070068 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +010069 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 05:30:24 -070070 frames_to_process_(duration_frames),
71 frames_recorded_(0),
72 frames_processed_(0),
73 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -070074 dropped_frames_before_first_encode_(0),
75 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -070076 last_render_time_(0),
77 rtp_timestamp_delta_(0),
78 avg_psnr_threshold_(avg_psnr_threshold),
79 avg_ssim_threshold_(avg_ssim_threshold),
Peter Boström8c38e8b2015-11-26 17:45:47 +010080 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +010081 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +010082 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -070083 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
84
85 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
86 // so that we don't accidentally starve "real" worker threads (codec etc).
87 // Also, don't allocate more than kMaxComparisonThreads, even if there are
88 // spare cores.
89
90 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
91 RTC_DCHECK_GE(num_cores, 1u);
92 static const uint32_t kMinCoresLeft = 4;
93 static const uint32_t kMaxComparisonThreads = 8;
94
95 if (num_cores <= kMinCoresLeft) {
96 num_cores = 1;
97 } else {
98 num_cores -= kMinCoresLeft;
99 num_cores = std::min(num_cores, kMaxComparisonThreads);
100 }
101
102 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100103 rtc::PlatformThread* thread =
104 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
105 thread->Start();
106 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700107 }
ivica5d6a06c2015-09-17 05:30:24 -0700108 }
109
110 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100111 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
112 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700113 delete thread;
114 }
115 }
116
117 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
118
119 DeliveryStatus DeliverPacket(MediaType media_type,
120 const uint8_t* packet,
121 size_t length,
122 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700123 // Ignore timestamps of RTCP packets. They're not synchronized with
124 // RTP packet timestamps and so they would confuse wrap_handler_.
125 if (RtpHeaderParser::IsRtcp(packet, length)) {
126 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
127 }
128
sprangce4aef12015-11-02 07:23:20 -0800129 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700130 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800131 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700132 {
133 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800134 int64_t timestamp =
135 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
136 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700137 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
138 }
139
140 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
141 }
142
Peter Boströme4499152016-02-05 11:13:28 +0100143 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700144 rtc::CritScope crit(&comparison_lock_);
145 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
146 }
147
ivica5d6a06c2015-09-17 05:30:24 -0700148 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
149 VideoFrame copy = video_frame;
150 copy.set_timestamp(copy.ntp_time_ms() * 90);
ivica5d6a06c2015-09-17 05:30:24 -0700151 {
152 rtc::CritScope lock(&crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700153 frames_.push_back(copy);
154 }
155
156 input_->IncomingCapturedFrame(video_frame);
157 }
158
pbos14fe7082016-04-20 06:35:56 -0700159 void PreEncodeOnFrame(const VideoFrame& video_frame) {
160 rtc::CritScope lock(&crit_);
161 if (!first_send_timestamp_ && rtp_timestamp_delta_ == 0) {
162 while (frames_.front().timestamp() != video_frame.timestamp()) {
163 ++dropped_frames_before_first_encode_;
164 frames_.pop_front();
165 RTC_CHECK(!frames_.empty());
166 }
167 first_send_timestamp_ = rtc::Optional<uint32_t>(video_frame.timestamp());
168 }
169 }
170
stefan1d8a5062015-10-02 03:39:33 -0700171 bool SendRtp(const uint8_t* packet,
172 size_t length,
173 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800174 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700175 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800176 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700177
sprangce4aef12015-11-02 07:23:20 -0800178 int64_t current_time =
179 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
180 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700181 {
182 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800183
Peter Boström81cbd922016-03-22 12:19:07 +0100184 if (rtp_timestamp_delta_ == 0) {
nissec7fe3c22016-04-20 03:25:36 -0700185 rtp_timestamp_delta_ = header.timestamp - *first_send_timestamp_;
186 first_send_timestamp_ = rtc::Optional<uint32_t>();
ivica5d6a06c2015-09-17 05:30:24 -0700187 }
sprang1b3530b2016-03-10 01:32:53 -0800188 int64_t timestamp =
189 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
sprangce4aef12015-11-02 07:23:20 -0800190 send_times_[timestamp] = current_time;
191 if (!transport_->DiscardedLastPacket() &&
192 header.ssrc == ssrc_to_analyze_) {
193 encoded_frame_sizes_[timestamp] +=
194 length - (header.headerLength + header.paddingLength);
195 }
ivica5d6a06c2015-09-17 05:30:24 -0700196 }
sprangce4aef12015-11-02 07:23:20 -0800197 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700198 }
199
200 bool SendRtcp(const uint8_t* packet, size_t length) override {
201 return transport_->SendRtcp(packet, length);
202 }
203
204 void EncodedFrameCallback(const EncodedFrame& frame) override {
205 rtc::CritScope lock(&comparison_lock_);
206 if (frames_recorded_ < frames_to_process_)
207 encoded_frame_size_.AddSample(frame.length_);
208 }
209
nisseeb83a1a2016-03-21 01:27:56 -0700210 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700211 int64_t render_time_ms =
212 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700213
214 rtc::CritScope lock(&crit_);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700215 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800216 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700217
sprang16daaa52016-03-09 01:30:24 -0800218 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700219 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700220 // No previous frame rendered, this one was dropped after sending but
221 // before rendering.
222 ++dropped_frames_before_rendering_;
223 frames_.pop_front();
224 RTC_CHECK(!frames_.empty());
225 continue;
226 }
nisse97f0b932016-05-26 09:44:40 -0700227 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
ivica5d6a06c2015-09-17 05:30:24 -0700228 render_time_ms);
229 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700230 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700231 }
232
233 VideoFrame reference_frame = frames_.front();
234 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800235 int64_t reference_timestamp =
236 wrap_handler_.Unwrap(reference_frame.timestamp());
237 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800238 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100239 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800240 ++send_timestamp;
241 }
sprang16daaa52016-03-09 01:30:24 -0800242 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700243
244 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
245
nisse97f0b932016-05-26 09:44:40 -0700246 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ivica5d6a06c2015-09-17 05:30:24 -0700247 }
248
ivica5d6a06c2015-09-17 05:30:24 -0700249 void Wait() {
250 // Frame comparisons can be very expensive. Wait for test to be done, but
251 // at time-out check if frames_processed is going up. If so, give it more
252 // time, otherwise fail. Hopefully this will reduce test flakiness.
253
Peter Boström8c38e8b2015-11-26 17:45:47 +0100254 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800255
ivica5d6a06c2015-09-17 05:30:24 -0700256 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700257 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100258 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700259 int frames_processed;
260 {
261 rtc::CritScope crit(&comparison_lock_);
262 frames_processed = frames_processed_;
263 }
264
265 // Print some output so test infrastructure won't think we've crashed.
266 const char* kKeepAliveMessages[3] = {
267 "Uh, I'm-I'm not quite dead, sir.",
268 "Uh, I-I think uh, I could pull through, sir.",
269 "Actually, I think I'm all right to come with you--"};
270 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
271
272 if (last_frames_processed == -1) {
273 last_frames_processed = frames_processed;
274 continue;
275 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100276 if (frames_processed == last_frames_processed) {
277 EXPECT_GT(frames_processed, last_frames_processed)
278 << "Analyzer stalled while waiting for test to finish.";
279 done_.Set();
280 break;
281 }
ivica5d6a06c2015-09-17 05:30:24 -0700282 last_frames_processed = frames_processed;
283 }
284
285 if (iteration > 0)
286 printf("- Farewell, sweet Concorde!\n");
287
Peter Boström8c38e8b2015-11-26 17:45:47 +0100288 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700289 }
290
pbos14fe7082016-04-20 06:35:56 -0700291 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
292 return &pre_encode_proxy_;
293 }
Peter Boströme4499152016-02-05 11:13:28 +0100294 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
295
ivica5d6a06c2015-09-17 05:30:24 -0700296 VideoCaptureInput* input_;
sprangce4aef12015-11-02 07:23:20 -0800297 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700298 PacketReceiver* receiver_;
299 VideoSendStream* send_stream_;
300
301 private:
302 struct FrameComparison {
303 FrameComparison()
304 : dropped(false),
305 send_time_ms(0),
306 recv_time_ms(0),
307 render_time_ms(0),
308 encoded_frame_size(0) {}
309
310 FrameComparison(const VideoFrame& reference,
311 const VideoFrame& render,
312 bool dropped,
313 int64_t send_time_ms,
314 int64_t recv_time_ms,
315 int64_t render_time_ms,
316 size_t encoded_frame_size)
317 : reference(reference),
318 render(render),
319 dropped(dropped),
320 send_time_ms(send_time_ms),
321 recv_time_ms(recv_time_ms),
322 render_time_ms(render_time_ms),
323 encoded_frame_size(encoded_frame_size) {}
324
325 VideoFrame reference;
326 VideoFrame render;
327 bool dropped;
328 int64_t send_time_ms;
329 int64_t recv_time_ms;
330 int64_t render_time_ms;
331 size_t encoded_frame_size;
332 };
333
334 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700335 Sample(int dropped,
336 int64_t input_time_ms,
337 int64_t send_time_ms,
338 int64_t recv_time_ms,
339 int64_t render_time_ms,
340 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700341 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700342 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700343 : dropped(dropped),
344 input_time_ms(input_time_ms),
345 send_time_ms(send_time_ms),
346 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700347 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700348 encoded_frame_size(encoded_frame_size),
349 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700350 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700351
ivica8d15bd62015-10-07 02:43:12 -0700352 int dropped;
353 int64_t input_time_ms;
354 int64_t send_time_ms;
355 int64_t recv_time_ms;
356 int64_t render_time_ms;
357 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700358 double psnr;
359 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700360 };
361
Peter Boströme4499152016-02-05 11:13:28 +0100362 // This class receives the send-side OnEncodeTiming and is provided to not
363 // conflict with the receiver-side pre_decode_callback.
364 class OnEncodeTimingProxy : public EncodedFrameObserver {
365 public:
366 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
367
368 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
369 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
370 }
371 void EncodedFrameCallback(const EncodedFrame& frame) override {}
372
373 private:
374 VideoAnalyzer* const parent_;
375 };
376
pbos14fe7082016-04-20 06:35:56 -0700377 // This class receives the send-side OnFrame callback and is provided to not
378 // conflict with the receiver-side renderer callback.
379 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
380 public:
381 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
382
383 void OnFrame(const VideoFrame& video_frame) override {
384 parent_->PreEncodeOnFrame(video_frame);
385 }
386
387 private:
388 VideoAnalyzer* const parent_;
389 };
390
ivica5d6a06c2015-09-17 05:30:24 -0700391 void AddFrameComparison(const VideoFrame& reference,
392 const VideoFrame& render,
393 bool dropped,
394 int64_t render_time_ms)
395 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800396 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
397 int64_t send_time_ms = send_times_[reference_timestamp];
398 send_times_.erase(reference_timestamp);
399 int64_t recv_time_ms = recv_times_[reference_timestamp];
400 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700401
sprangce4aef12015-11-02 07:23:20 -0800402 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800403 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800404 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800405 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800406 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
407 if (it != encoded_frame_sizes_.end())
408 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700409
410 VideoFrame reference_copy;
411 VideoFrame render_copy;
ivica5d6a06c2015-09-17 05:30:24 -0700412
413 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700414 if (comparisons_.size() < kMaxComparisons) {
415 reference_copy.CopyFrame(reference);
416 render_copy.CopyFrame(render);
417 } else {
418 // Copy the time to ensure that delay calculations can still be made.
419 reference_copy.set_ntp_time_ms(reference.ntp_time_ms());
420 render_copy.set_ntp_time_ms(render.ntp_time_ms());
421 }
ivica5d6a06c2015-09-17 05:30:24 -0700422 comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped,
423 send_time_ms, recv_time_ms,
424 render_time_ms, encoded_size));
Peter Boström5811a392015-12-10 13:02:50 +0100425 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700426 }
427
428 static bool PollStatsThread(void* obj) {
429 return static_cast<VideoAnalyzer*>(obj)->PollStats();
430 }
431
432 bool PollStats() {
Peter Boströmdd45eb62016-01-19 15:22:32 +0100433 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 13:02:50 +0100434 return false;
ivica5d6a06c2015-09-17 05:30:24 -0700435
436 VideoSendStream::Stats stats = send_stream_->GetStats();
437
438 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 17:30:49 +0100439 // It's not certain that we yet have estimates for any of these stats. Check
440 // that they are positive before mixing them in.
441 if (stats.encode_frame_rate > 0)
442 encode_frame_rate_.AddSample(stats.encode_frame_rate);
443 if (stats.avg_encode_time_ms > 0)
444 encode_time_ms.AddSample(stats.avg_encode_time_ms);
445 if (stats.encode_usage_percent > 0)
446 encode_usage_percent.AddSample(stats.encode_usage_percent);
447 if (stats.media_bitrate_bps > 0)
448 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 05:30:24 -0700449
450 return true;
451 }
452
453 static bool FrameComparisonThread(void* obj) {
454 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
455 }
456
457 bool CompareFrames() {
458 if (AllFramesRecorded())
459 return false;
460
461 VideoFrame reference;
462 VideoFrame render;
463 FrameComparison comparison;
464
465 if (!PopComparison(&comparison)) {
466 // Wait until new comparison task is available, or test is done.
467 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100468 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700469 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100470 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700471 return false;
472 }
473 return true; // Try again.
474 }
475
476 PerformFrameComparison(comparison);
477
478 if (FrameProcessed()) {
479 PrintResults();
480 if (graph_data_output_file_)
481 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100482 done_.Set();
483 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700484 return false;
485 }
486
487 return true;
488 }
489
490 bool PopComparison(FrameComparison* comparison) {
491 rtc::CritScope crit(&comparison_lock_);
492 // If AllFramesRecorded() is true, it means we have already popped
493 // frames_to_process_ frames from comparisons_, so there is no more work
494 // for this thread to be done. frames_processed_ might still be lower if
495 // all comparisons are not done, but those frames are currently being
496 // worked on by other threads.
497 if (comparisons_.empty() || AllFramesRecorded())
498 return false;
499
500 *comparison = comparisons_.front();
501 comparisons_.pop_front();
502
503 FrameRecorded();
504 return true;
505 }
506
507 // Increment counter for number of frames received for comparison.
508 void FrameRecorded() {
509 rtc::CritScope crit(&comparison_lock_);
510 ++frames_recorded_;
511 }
512
513 // Returns true if all frames to be compared have been taken from the queue.
514 bool AllFramesRecorded() {
515 rtc::CritScope crit(&comparison_lock_);
516 assert(frames_recorded_ <= frames_to_process_);
517 return frames_recorded_ == frames_to_process_;
518 }
519
520 // Increase count of number of frames processed. Returns true if this was the
521 // last frame to be processed.
522 bool FrameProcessed() {
523 rtc::CritScope crit(&comparison_lock_);
524 ++frames_processed_;
525 assert(frames_processed_ <= frames_to_process_);
526 return frames_processed_ == frames_to_process_;
527 }
528
529 void PrintResults() {
530 rtc::CritScope crit(&comparison_lock_);
531 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800532 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700533 PrintResult("sender_time", sender_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700534 PrintResult("receiver_time", receiver_time_, " ms");
535 PrintResult("total_delay_incl_network", end_to_end_, " ms");
536 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
537 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
538 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
539 PrintResult("encode_time", encode_time_ms, " ms");
540 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
541 PrintResult("media_bitrate", media_bitrate_bps, " bps");
542
pbos14fe7082016-04-20 06:35:56 -0700543 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
544 dropped_frames_);
545 printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n",
546 test_label_.c_str(), dropped_frames_before_first_encode_);
547 printf("RESULT dropped_frames_before_rendering: %s = %d frames\n",
548 test_label_.c_str(), dropped_frames_before_rendering_);
549
ivica5d6a06c2015-09-17 05:30:24 -0700550 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
551 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
552 }
553
554 void PerformFrameComparison(const FrameComparison& comparison) {
555 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700556 double psnr = -1.0;
557 double ssim = -1.0;
558 if (!comparison.reference.IsZeroSize()) {
559 psnr = I420PSNR(&comparison.reference, &comparison.render);
560 ssim = I420SSIM(&comparison.reference, &comparison.render);
561 }
ivica5d6a06c2015-09-17 05:30:24 -0700562
563 int64_t input_time_ms = comparison.reference.ntp_time_ms();
564
565 rtc::CritScope crit(&comparison_lock_);
566 if (graph_data_output_file_) {
567 samples_.push_back(
568 Sample(comparison.dropped, input_time_ms, comparison.send_time_ms,
ivica8d15bd62015-10-07 02:43:12 -0700569 comparison.recv_time_ms, comparison.render_time_ms,
570 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700571 }
stefanb1797672016-08-11 07:00:57 -0700572 if (psnr >= 0.0)
573 psnr_.AddSample(psnr);
574 if (ssim >= 0.0)
575 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700576
577 if (comparison.dropped) {
578 ++dropped_frames_;
579 return;
580 }
581 if (last_render_time_ != 0)
582 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
583 last_render_time_ = comparison.render_time_ms;
584
585 sender_time_.AddSample(comparison.send_time_ms - input_time_ms);
586 receiver_time_.AddSample(comparison.render_time_ms -
587 comparison.recv_time_ms);
588 end_to_end_.AddSample(comparison.render_time_ms - input_time_ms);
589 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
590 }
591
592 void PrintResult(const char* result_type,
593 test::Statistics stats,
594 const char* unit) {
595 printf("RESULT %s: %s = {%f, %f}%s\n",
596 result_type,
597 test_label_.c_str(),
598 stats.Mean(),
599 stats.StandardDeviation(),
600 unit);
601 }
602
603 void PrintSamplesToFile(void) {
604 FILE* out = graph_data_output_file_;
605 rtc::CritScope crit(&comparison_lock_);
606 std::sort(samples_.begin(), samples_.end(),
607 [](const Sample& A, const Sample& B) -> bool {
608 return A.input_time_ms < B.input_time_ms;
609 });
610
sprangce4aef12015-11-02 07:23:20 -0800611 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700612 fprintf(out, "%" PRIuS "\n", samples_.size());
613 fprintf(out,
614 "dropped "
615 "input_time_ms "
616 "send_time_ms "
617 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700618 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700619 "encoded_frame_size "
620 "psnr "
621 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700622 "encode_time_ms\n");
623 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700624 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700625 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
626 int encode_time_ms;
627 if (it != samples_encode_time_ms_.end()) {
628 encode_time_ms = it->second;
629 } else {
630 ++missing_encode_time_samples;
631 encode_time_ms = -1;
632 }
633 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
634 " %lf %lf %d\n",
635 sample.dropped, sample.input_time_ms, sample.send_time_ms,
636 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700637 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700638 encode_time_ms);
639 }
640 if (missing_encode_time_samples) {
641 fprintf(stderr,
642 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
643 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700644 }
645 }
646
647 const std::string test_label_;
648 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800649 const std::string graph_title_;
650 const uint32_t ssrc_to_analyze_;
pbos14fe7082016-04-20 06:35:56 -0700651 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100652 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700653 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700654 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700655 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
656 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
657 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
658 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
659 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
660 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
661 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
662 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
663 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
664 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
665 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
666
667 const int frames_to_process_;
668 int frames_recorded_;
669 int frames_processed_;
670 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -0700671 int dropped_frames_before_first_encode_;
672 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -0700673 int64_t last_render_time_;
674 uint32_t rtp_timestamp_delta_;
675
676 rtc::CriticalSection crit_;
677 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -0700678 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800679 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
680 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
681 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
682 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
nissec7fe3c22016-04-20 03:25:36 -0700683 rtc::Optional<uint32_t> first_send_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700684 const double avg_psnr_threshold_;
685 const double avg_ssim_threshold_;
686
687 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100688 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
689 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +0100690 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -0700691 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +0100692 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -0700693};
694
ivica87f83a92015-10-08 05:13:32 -0700695VideoQualityTest::VideoQualityTest() : clock_(Clock::GetRealTimeClock()) {}
ivica5d6a06c2015-09-17 05:30:24 -0700696
697void VideoQualityTest::TestBody() {}
698
sprangce4aef12015-11-02 07:23:20 -0800699std::string VideoQualityTest::GenerateGraphTitle() const {
700 std::stringstream ss;
701 ss << params_.common.codec;
702 ss << " (" << params_.common.target_bitrate_bps / 1000 << "kbps";
703 ss << ", " << params_.common.fps << " FPS";
704 if (params_.screenshare.scroll_duration)
705 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
706 if (params_.ss.streams.size() > 1)
707 ss << ", Stream #" << params_.ss.selected_stream;
708 if (params_.ss.num_spatial_layers > 1)
709 ss << ", Layer #" << params_.ss.selected_sl;
710 ss << ")";
711 return ss.str();
712}
713
714void VideoQualityTest::CheckParams() {
715 // Add a default stream in none specified.
716 if (params_.ss.streams.empty())
717 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
718 if (params_.ss.num_spatial_layers == 0)
719 params_.ss.num_spatial_layers = 1;
720
721 if (params_.pipe.loss_percent != 0 ||
722 params_.pipe.queue_length_packets != 0) {
723 // Since LayerFilteringTransport changes the sequence numbers, we can't
724 // use that feature with pack loss, since the NACK request would end up
725 // retransmitting the wrong packets.
726 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800727 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800728 RTC_CHECK(params_.common.selected_tl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800729 params_.common.selected_tl ==
730 params_.common.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800731 }
732
733 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
734 // does in some parts of the code?
735 RTC_CHECK_GE(params_.common.max_bitrate_bps,
736 params_.common.target_bitrate_bps);
737 RTC_CHECK_GE(params_.common.target_bitrate_bps,
738 params_.common.min_bitrate_bps);
739 RTC_CHECK_LT(params_.common.selected_tl, params_.common.num_temporal_layers);
740 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
741 for (const VideoStream& stream : params_.ss.streams) {
742 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
743 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
744 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
745 RTC_CHECK_EQ(static_cast<int>(stream.temporal_layer_thresholds_bps.size()),
746 params_.common.num_temporal_layers - 1);
747 }
748 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
749 // the total bitrate? We anyway have to update them in the case bitrate
750 // estimator changes the total bitrates.
751 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
752 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
753 RTC_CHECK(params_.ss.spatial_layers.empty() ||
754 params_.ss.spatial_layers.size() ==
755 static_cast<size_t>(params_.ss.num_spatial_layers));
756 if (params_.common.codec == "VP8") {
757 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
758 } else if (params_.common.codec == "VP9") {
759 RTC_CHECK_EQ(params_.ss.streams.size(), 1u);
760 }
761}
762
763// Static.
764std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
765 // Parse comma separated nonnegative integers, where some elements may be
766 // empty. The empty values are replaced with -1.
767 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
768 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
769 std::vector<int> result;
770 if (str.empty())
771 return result;
772
773 const char* p = str.c_str();
774 int value = -1;
775 int pos;
776 while (*p) {
777 if (*p == ',') {
778 result.push_back(value);
779 value = -1;
780 ++p;
781 continue;
782 }
783 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
784 << "Unexpected non-number value.";
785 p += pos;
786 }
787 result.push_back(value);
788 return result;
789}
790
791// Static.
792VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
793 VideoStream stream;
794 stream.width = params.common.width;
795 stream.height = params.common.height;
796 stream.max_framerate = params.common.fps;
797 stream.min_bitrate_bps = params.common.min_bitrate_bps;
798 stream.target_bitrate_bps = params.common.target_bitrate_bps;
799 stream.max_bitrate_bps = params.common.max_bitrate_bps;
800 stream.max_qp = 52;
801 if (params.common.num_temporal_layers == 2)
802 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
803 return stream;
804}
805
806// Static.
807void VideoQualityTest::FillScalabilitySettings(
808 Params* params,
809 const std::vector<std::string>& stream_descriptors,
810 size_t selected_stream,
811 int num_spatial_layers,
812 int selected_sl,
813 const std::vector<std::string>& sl_descriptors) {
814 // Read VideoStream and SpatialLayer elements from a list of comma separated
815 // lists. To use a default value for an element, use -1 or leave empty.
816 // Validity checks performed in CheckParams.
817
818 RTC_CHECK(params->ss.streams.empty());
819 for (auto descriptor : stream_descriptors) {
820 if (descriptor.empty())
821 continue;
822 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
823 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
824 if (v[0] != -1)
825 stream.width = static_cast<size_t>(v[0]);
826 if (v[1] != -1)
827 stream.height = static_cast<size_t>(v[1]);
828 if (v[2] != -1)
829 stream.max_framerate = v[2];
830 if (v[3] != -1)
831 stream.min_bitrate_bps = v[3];
832 if (v[4] != -1)
833 stream.target_bitrate_bps = v[4];
834 if (v[5] != -1)
835 stream.max_bitrate_bps = v[5];
836 if (v.size() > 6 && v[6] != -1)
837 stream.max_qp = v[6];
838 if (v.size() > 7) {
839 stream.temporal_layer_thresholds_bps.clear();
840 stream.temporal_layer_thresholds_bps.insert(
841 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
842 } else {
843 // Automatic TL thresholds for more than two layers not supported.
844 RTC_CHECK_LE(params->common.num_temporal_layers, 2);
845 }
846 params->ss.streams.push_back(stream);
847 }
848 params->ss.selected_stream = selected_stream;
849
850 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
851 params->ss.selected_sl = selected_sl;
852 RTC_CHECK(params->ss.spatial_layers.empty());
853 for (auto descriptor : sl_descriptors) {
854 if (descriptor.empty())
855 continue;
856 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
857 RTC_CHECK_GT(v[2], 0);
858
859 SpatialLayer layer;
860 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
861 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
862 layer.target_bitrate_bps = v[2];
863 params->ss.spatial_layers.push_back(layer);
864 }
865}
866
867void VideoQualityTest::SetupCommon(Transport* send_transport,
868 Transport* recv_transport) {
869 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -0700870 trace_to_stderr_.reset(new test::TraceToStderr);
871
sprangce4aef12015-11-02 07:23:20 -0800872 size_t num_streams = params_.ss.streams.size();
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100873 CreateSendConfig(num_streams, 0, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -0700874
875 int payload_type;
hbosbab934b2016-01-27 01:36:03 -0800876 if (params_.common.codec == "H264") {
877 encoder_.reset(VideoEncoder::Create(VideoEncoder::kH264));
878 payload_type = kPayloadTypeH264;
879 } else if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700880 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp8));
881 payload_type = kPayloadTypeVP8;
sprangce4aef12015-11-02 07:23:20 -0800882 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700883 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp9));
884 payload_type = kPayloadTypeVP9;
885 } else {
886 RTC_NOTREACHED() << "Codec not supported!";
887 return;
888 }
stefanff483612015-12-21 03:14:00 -0800889 video_send_config_.encoder_settings.encoder = encoder_.get();
890 video_send_config_.encoder_settings.payload_name = params_.common.codec;
891 video_send_config_.encoder_settings.payload_type = payload_type;
892 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
893 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
sprangce4aef12015-11-02 07:23:20 -0800894 for (size_t i = 0; i < num_streams; ++i)
stefanff483612015-12-21 03:14:00 -0800895 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -0700896
stefanff483612015-12-21 03:14:00 -0800897 video_send_config_.rtp.extensions.clear();
sprangce4aef12015-11-02 07:23:20 -0800898 if (params_.common.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -0800899 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700900 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +0100901 test::kTransportSequenceNumberExtensionId));
902 } else {
stefanff483612015-12-21 03:14:00 -0800903 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700904 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +0200905 }
906
stefanff483612015-12-21 03:14:00 -0800907 video_encoder_config_.min_transmit_bitrate_bps =
908 params_.common.min_transmit_bps;
909 video_encoder_config_.streams = params_.ss.streams;
910 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -0700911
912 CreateMatchingReceiveConfigs(recv_transport);
913
sprangce4aef12015-11-02 07:23:20 -0800914 for (size_t i = 0; i < num_streams; ++i) {
stefanff483612015-12-21 03:14:00 -0800915 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 13:29:59 +0100916 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
917 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 07:23:20 -0800918 kSendRtxPayloadType;
stefanff483612015-12-21 03:14:00 -0800919 video_receive_configs_[i].rtp.transport_cc = params_.common.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -0800920 }
ivica5d6a06c2015-09-17 05:30:24 -0700921}
922
sprangce4aef12015-11-02 07:23:20 -0800923void VideoQualityTest::SetupScreenshare() {
924 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 05:30:24 -0700925
926 // Fill out codec settings.
stefanff483612015-12-21 03:14:00 -0800927 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
sprangce4aef12015-11-02 07:23:20 -0800928 if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700929 codec_settings_.VP8 = VideoEncoder::GetDefaultVp8Settings();
930 codec_settings_.VP8.denoisingOn = false;
931 codec_settings_.VP8.frameDroppingOn = false;
932 codec_settings_.VP8.numberOfTemporalLayers =
sprangce4aef12015-11-02 07:23:20 -0800933 static_cast<unsigned char>(params_.common.num_temporal_layers);
stefanff483612015-12-21 03:14:00 -0800934 video_encoder_config_.encoder_specific_settings = &codec_settings_.VP8;
sprangce4aef12015-11-02 07:23:20 -0800935 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700936 codec_settings_.VP9 = VideoEncoder::GetDefaultVp9Settings();
937 codec_settings_.VP9.denoisingOn = false;
938 codec_settings_.VP9.frameDroppingOn = false;
939 codec_settings_.VP9.numberOfTemporalLayers =
sprangce4aef12015-11-02 07:23:20 -0800940 static_cast<unsigned char>(params_.common.num_temporal_layers);
stefanff483612015-12-21 03:14:00 -0800941 video_encoder_config_.encoder_specific_settings = &codec_settings_.VP9;
sprangce4aef12015-11-02 07:23:20 -0800942 codec_settings_.VP9.numberOfSpatialLayers =
943 static_cast<unsigned char>(params_.ss.num_spatial_layers);
ivica5d6a06c2015-09-17 05:30:24 -0700944 }
945
946 // Setup frame generator.
947 const size_t kWidth = 1850;
948 const size_t kHeight = 1110;
949 std::vector<std::string> slides;
950 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
951 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
952 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
953 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
954
sprangce4aef12015-11-02 07:23:20 -0800955 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 05:30:24 -0700956 // Cycle image every slide_change_interval seconds.
957 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
958 slides, kWidth, kHeight,
sprangce4aef12015-11-02 07:23:20 -0800959 params_.screenshare.slide_change_interval * params_.common.fps));
ivica5d6a06c2015-09-17 05:30:24 -0700960 } else {
sprangce4aef12015-11-02 07:23:20 -0800961 RTC_CHECK_LE(params_.common.width, kWidth);
962 RTC_CHECK_LE(params_.common.height, kHeight);
963 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
964 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
965 params_.screenshare.scroll_duration) *
966 1000;
967 RTC_CHECK_LE(params_.screenshare.scroll_duration,
968 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 05:30:24 -0700969
sprangce4aef12015-11-02 07:23:20 -0800970 frame_generator_.reset(
971 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
972 clock_, slides, kWidth, kHeight, params_.common.width,
973 params_.common.height, params_.screenshare.scroll_duration * 1000,
974 kPauseDurationMs));
ivica5d6a06c2015-09-17 05:30:24 -0700975 }
976}
977
sprangce4aef12015-11-02 07:23:20 -0800978void VideoQualityTest::CreateCapturer(VideoCaptureInput* input) {
979 if (params_.screenshare.enabled) {
980 test::FrameGeneratorCapturer* frame_generator_capturer =
ivica2d4e6c52015-09-23 01:57:06 -0700981 new test::FrameGeneratorCapturer(
sprangce4aef12015-11-02 07:23:20 -0800982 clock_, input, frame_generator_.release(), params_.common.fps);
ivica2d4e6c52015-09-23 01:57:06 -0700983 EXPECT_TRUE(frame_generator_capturer->Init());
984 capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -0700985 } else {
sprangce4aef12015-11-02 07:23:20 -0800986 if (params_.video.clip_name.empty()) {
987 capturer_.reset(test::VideoCapturer::Create(input, params_.common.width,
988 params_.common.height,
989 params_.common.fps, clock_));
ivica5d6a06c2015-09-17 05:30:24 -0700990 } else {
ivica2d4e6c52015-09-23 01:57:06 -0700991 capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
sprangce4aef12015-11-02 07:23:20 -0800992 input, test::ResourcePath(params_.video.clip_name, "yuv"),
993 params_.common.width, params_.common.height, params_.common.fps,
ivica2d4e6c52015-09-23 01:57:06 -0700994 clock_));
Peter Boström74f6e9e2016-04-04 17:56:10 +0200995 ASSERT_TRUE(capturer_) << "Could not create capturer for "
996 << params_.video.clip_name
997 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -0700998 }
999 }
1000}
1001
sprang7a975f72015-10-12 06:33:21 -07001002void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001003 params_ = params;
1004
ivica5d6a06c2015-09-17 05:30:24 -07001005 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1006 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001007 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001008
1009 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001010 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001011 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001012 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001013 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001014 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1015 << "!";
ivica87f83a92015-10-08 05:13:32 -07001016 }
sprang7a975f72015-10-12 06:33:21 -07001017
stefanf116bd02015-10-27 08:29:42 -07001018 Call::Config call_config;
1019 call_config.bitrate_config = params.common.call_bitrate_config;
1020 CreateCalls(call_config, call_config);
1021
ivica87f83a92015-10-08 05:13:32 -07001022 test::LayerFilteringTransport send_transport(
stefanf116bd02015-10-27 08:29:42 -07001023 params.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -08001024 params.common.selected_tl, params_.ss.selected_sl);
stefanf116bd02015-10-27 08:29:42 -07001025 test::DirectTransport recv_transport(params.pipe, receiver_call_.get());
1026
sprangce4aef12015-11-02 07:23:20 -08001027 std::string graph_title = params_.analyzer.graph_title;
1028 if (graph_title.empty())
1029 graph_title = VideoQualityTest::GenerateGraphTitle();
1030
1031 // In the case of different resolutions, the functions calculating PSNR and
1032 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
1033 // aborts if the average psnr/ssim are below the given threshold, which is
1034 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
1035 // abort.
1036 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
1037 int selected_sl = params_.ss.selected_sl != -1
1038 ? params_.ss.selected_sl
1039 : params_.ss.num_spatial_layers - 1;
1040 bool disable_quality_check =
1041 selected_stream.width != params_.common.width ||
1042 selected_stream.height != params_.common.height ||
1043 (!params_.ss.spatial_layers.empty() &&
1044 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
1045 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
1046 if (disable_quality_check) {
1047 fprintf(stderr,
1048 "Warning: Calculating PSNR and SSIM for downsized resolution "
1049 "not implemented yet! Skipping PSNR and SSIM calculations!");
1050 }
1051
ivica5d6a06c2015-09-17 05:30:24 -07001052 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001053 &send_transport, params_.analyzer.test_label,
1054 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
1055 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
1056 params_.analyzer.test_durations_secs * params_.common.fps,
1057 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001058 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 05:30:24 -07001059
ivica5d6a06c2015-09-17 05:30:24 -07001060 analyzer.SetReceiver(receiver_call_->Receiver());
1061 send_transport.SetReceiver(&analyzer);
1062 recv_transport.SetReceiver(sender_call_->Receiver());
1063
sprangce4aef12015-11-02 07:23:20 -08001064 SetupCommon(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001065 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001066 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
stefanff483612015-12-21 03:14:00 -08001067 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 05:30:24 -07001068 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 11:13:28 +01001069 RTC_DCHECK(!video_send_config_.post_encode_callback);
1070 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001071
sprangce4aef12015-11-02 07:23:20 -08001072 if (params_.screenshare.enabled)
1073 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001074
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001075 CreateVideoStreams();
stefanff483612015-12-21 03:14:00 -08001076 analyzer.input_ = video_send_stream_->Input();
1077 analyzer.send_stream_ = video_send_stream_;
ivica5d6a06c2015-09-17 05:30:24 -07001078
sprangce4aef12015-11-02 07:23:20 -08001079 CreateCapturer(&analyzer);
ivicac1cc8542015-10-08 03:44:06 -07001080
stefanff483612015-12-21 03:14:00 -08001081 video_send_stream_->Start();
1082 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1083 receive_stream->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001084 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001085
1086 analyzer.Wait();
1087
1088 send_transport.StopSending();
1089 recv_transport.StopSending();
1090
ivica2d4e6c52015-09-23 01:57:06 -07001091 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001092 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1093 receive_stream->Stop();
1094 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001095
1096 DestroyStreams();
1097
1098 if (graph_data_output_file)
1099 fclose(graph_data_output_file);
1100}
1101
sprang7a975f72015-10-12 06:33:21 -07001102void VideoQualityTest::RunWithVideoRenderer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001103 params_ = params;
1104 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001105
kwiberg27f982b2016-03-01 11:52:33 -08001106 std::unique_ptr<test::VideoRenderer> local_preview(
sprangce4aef12015-11-02 07:23:20 -08001107 test::VideoRenderer::Create("Local Preview", params_.common.width,
1108 params_.common.height));
1109 size_t stream_id = params_.ss.selected_stream;
mflodmand1590b22015-12-09 07:07:59 -08001110 std::string title = "Loopback Video";
1111 if (params_.ss.streams.size() > 1) {
1112 std::ostringstream s;
1113 s << stream_id;
1114 title += " - Stream #" + s.str();
sprangce4aef12015-11-02 07:23:20 -08001115 }
mflodmand1590b22015-12-09 07:07:59 -08001116
kwiberg27f982b2016-03-01 11:52:33 -08001117 std::unique_ptr<test::VideoRenderer> loopback_video(
mflodmand1590b22015-12-09 07:07:59 -08001118 test::VideoRenderer::Create(title.c_str(),
1119 params_.ss.streams[stream_id].width,
sprangce4aef12015-11-02 07:23:20 -08001120 params_.ss.streams[stream_id].height));
ivica5d6a06c2015-09-17 05:30:24 -07001121
1122 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1123 // match the full stack tests.
1124 Call::Config call_config;
sprangce4aef12015-11-02 07:23:20 -08001125 call_config.bitrate_config = params_.common.call_bitrate_config;
kwiberg27f982b2016-03-01 11:52:33 -08001126 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001127
1128 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001129 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
sprangce4aef12015-11-02 07:23:20 -08001130 params.common.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001131 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1132 // least share as much code as possible. That way this test would also match
1133 // the full stack tests better.
1134 transport.SetReceiver(call->Receiver());
1135
sprangce4aef12015-11-02 07:23:20 -08001136 SetupCommon(&transport, &transport);
ivica87f83a92015-10-08 05:13:32 -07001137
stefanff483612015-12-21 03:14:00 -08001138 video_send_config_.local_renderer = local_preview.get();
1139 video_receive_configs_[stream_id].renderer = loopback_video.get();
sprangce4aef12015-11-02 07:23:20 -08001140
mflodman48a4beb2016-07-01 13:03:59 +02001141 video_send_config_.suspend_below_min_bitrate =
1142 params_.common.suspend_below_min_bitrate;
1143
philipel274c1dc2016-05-04 06:21:01 -07001144 if (params.common.fec) {
1145 video_send_config_.rtp.fec.red_payload_type = kRedPayloadType;
1146 video_send_config_.rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
1147 video_receive_configs_[stream_id].rtp.fec.red_payload_type =
1148 kRedPayloadType;
1149 video_receive_configs_[stream_id].rtp.fec.ulpfec_payload_type =
1150 kUlpfecPayloadType;
1151 }
1152
sprangce4aef12015-11-02 07:23:20 -08001153 if (params_.screenshare.enabled)
1154 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001155
stefanff483612015-12-21 03:14:00 -08001156 video_send_stream_ =
1157 call->CreateVideoSendStream(video_send_config_, video_encoder_config_);
ivica5d6a06c2015-09-17 05:30:24 -07001158 VideoReceiveStream* receive_stream =
Tommi733b5472016-06-10 17:58:01 +02001159 call->CreateVideoReceiveStream(video_receive_configs_[stream_id].Copy());
stefanff483612015-12-21 03:14:00 -08001160 CreateCapturer(video_send_stream_->Input());
ivica5d6a06c2015-09-17 05:30:24 -07001161
1162 receive_stream->Start();
stefanff483612015-12-21 03:14:00 -08001163 video_send_stream_->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001164 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001165
1166 test::PressEnterToContinue();
1167
ivica2d4e6c52015-09-23 01:57:06 -07001168 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001169 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001170 receive_stream->Stop();
1171
1172 call->DestroyVideoReceiveStream(receive_stream);
stefanff483612015-12-21 03:14:00 -08001173 call->DestroyVideoSendStream(video_send_stream_);
ivica5d6a06c2015-09-17 05:30:24 -07001174
1175 transport.StopSending();
1176}
1177
1178} // namespace webrtc